aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/acl/libacl-helper.c
blob: c56e6436e9f69cef40514ce57966e76a63ba5073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#define _GNU_SOURCE /* Definition of AT_* constants */

#include "libacl-helper.h"
#include <acl/libacl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/acl.h>

enum hakurei_acl_status hakurei_update_acl(int fd, uid_t uid, acl_perm_t perms)
{
	enum hakurei_acl_status ret;

	bool v;
	int i;
	acl_t acl, acl_d = NULL;
	acl_entry_t entry;
	acl_tag_t tag_type;
	void *qualifier_p;
	acl_permset_t permset;
	acl_perm_t has_perms;

	ret = HAKUREI_ACL_GET_FILE;
	acl = acl_get_file_at(fd, "", AT_EMPTY_PATH, ACL_TYPE_ACCESS);
	if (acl == NULL)
		goto out;

	/* prune entries by uid */
	for (i = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry); i == 1;
	     i = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry)) {
		ret = HAKUREI_ACL_GET_TAG_TYPE;
		if (acl_get_tag_type(entry, &tag_type) != 0)
			goto out;
		if (tag_type != ACL_USER)
			continue;

		ret = HAKUREI_ACL_GET_QUALIFIER;
		qualifier_p = acl_get_qualifier(entry);
		if (qualifier_p == NULL)
			goto out;
		v = *(uid_t *)qualifier_p == uid;
		acl_free(qualifier_p);

		if (!v)
			continue;

		ret = HAKUREI_ACL_GET_PERMSET;
		if (acl_get_permset(entry, &permset) != 0)
			goto out;

		if (perms == 0)
			goto delete;

		/* this is quite ugly, but the standard makes it impossible to
		 * directly obtain these bits */
		has_perms = 0;
#define COPY_PERM_BIT(perm)                      \
	do {                                     \
		ret = HAKUREI_ACL_GET_PERM;      \
		i = acl_get_perm(permset, perm); \
		if (i < 0)                       \
			goto out;                \
		if (i > 0)                       \
			has_perms |= perm;       \
	} while (0)
		COPY_PERM_BIT(ACL_READ);
		COPY_PERM_BIT(ACL_WRITE);
		COPY_PERM_BIT(ACL_EXECUTE);

		if (perms == has_perms) {
			ret = HAKUREI_ACL_DUP;
			acl_d = acl_dup(acl);
			if (acl_d == NULL)
				goto out;

			ret = HAKUREI_ACL_CALC_MASK;
			if (acl_calc_mask(&acl_d) != 0)
				goto out;

			ret = HAKUREI_ACL_OK;
			if (acl_cmp(acl, acl_d) == 0)
				goto out;
		}

delete:
		ret = HAKUREI_ACL_DELETE_ENTRY;
		if (acl_delete_entry(acl, entry) != 0)
			goto out;
	}

	if (perms == 0)
		goto set;

	ret = HAKUREI_ACL_CREATE_ENTRY;
	if (acl_create_entry(&acl, &entry) != 0)
		goto out;

	ret = HAKUREI_ACL_GET_PERMSET;
	if (acl_get_permset(entry, &permset) != 0)
		goto out;

	ret = HAKUREI_ACL_ADD_PERM;
	if (acl_add_perm(permset, perms) != 0)
		goto out;

	ret = HAKUREI_ACL_SET_TAG_TYPE;
	if (acl_set_tag_type(entry, ACL_USER) != 0)
		goto out;

	ret = HAKUREI_ACL_SET_QUALIFIER;
	if (acl_set_qualifier(entry, (void *)&uid) != 0)
		goto out;

set:
	ret = HAKUREI_ACL_CALC_MASK;
	if (acl_calc_mask(&acl) != 0)
		goto out;

	ret = HAKUREI_ACL_VALID;
	if (acl_valid(acl) != 0)
		goto out;

	ret = HAKUREI_ACL_SET_FILE;
	if (acl_set_file_at(fd, "", AT_EMPTY_PATH, ACL_TYPE_ACCESS, acl) == 0)
		ret = HAKUREI_ACL_OK;

out:
	if (acl != NULL)
		acl_free((void *)acl);
	if (acl_d != NULL)
		acl_free((void *)acl_d);
	return ret;
}