forked from HuxyUK/xpenology-3.x
/
syno_acl.c
285 lines (238 loc) · 5.96 KB
/
syno_acl.c
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* linux/fs/syno_acl.c
*
* Copyright (c) 2000-2013 Synology Inc.
*/
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/atomic.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/syno_acl.h>
#include <linux/module.h>
struct syno_acl *syno_acl_alloc(int count, gfp_t flags)
{
const size_t size = sizeof(struct syno_acl) +
count * sizeof(struct syno_acl_entry);
struct syno_acl *acl = kmalloc(size, flags);
if (acl) {
atomic_set(&acl->a_refcount, 1);
acl->a_count = count;
}
return acl;
}
EXPORT_SYMBOL(syno_acl_alloc);
/*
* Clone an ACL.
*/
struct syno_acl *syno_acl_clone(const struct syno_acl *acl, gfp_t flags)
{
struct syno_acl *clone = NULL;
if (acl) {
int size = sizeof(struct syno_acl) + acl->a_count *
sizeof(struct syno_acl_entry);
clone = kmemdup(acl, size, flags);
if (clone)
atomic_set(&clone->a_refcount, 1);
}
return clone;
}
EXPORT_SYMBOL(syno_acl_clone);
/*
* Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
*/
int syno_acl_valid(const struct syno_acl *acl)
{
const struct syno_acl_entry *pa, *pe;
FOREACH_SYNOACL_ENTRY(pa, acl, pe) {
if (pa->e_perm & ~(SYNO_PERM_FULL_CONTROL)){
return -EINVAL;
}
if (pa->e_tag & ~(SYNO_ACL_TAG_ALL)){
return -EINVAL;
}
if (SYNO_ACL_ALLOW != pa->e_allow && SYNO_ACL_DENY != pa->e_allow){
return -EINVAL;
}
if (pa->e_inherit & ~(SYNO_ACL_INHERIT_ALL)){
return -EINVAL;
}
}
return 0;
}
EXPORT_SYMBOL(syno_acl_valid);
/*
* Re-allocate a new ACL with the specified number of entries.
*
* The caller must ensure the acl is only referenced once.
*/
struct syno_acl *syno_acl_realloc(struct syno_acl *acl, unsigned int counts, gfp_t flags)
{
struct syno_acl *acl_re;
const size_t size = sizeof(struct syno_acl) +
counts * sizeof(struct syno_acl_entry);
if (!acl) {
return NULL;
}
if (atomic_read(&acl->a_refcount) != 1) {
printk(KERN_ERR" acl reference count: %d \n ", atomic_read(&acl->a_refcount));
return NULL;
}
/* assert(atomic_read(acl->a_refcount) == 1); */
acl_re = krealloc(acl, size, flags);
if (acl_re) {
acl_re->a_count = counts;
}
return acl_re;
}
EXPORT_SYMBOL(syno_acl_realloc);
/*---------xattr -------------*/
static inline int
ace_syno_from_xattr(struct syno_acl_entry *pAce, syno_acl_xattr_entry *pEntry)
{
unsigned short tag = le16_to_cpu(pEntry->e_tag);
//ID: user/group/everyone
if (SYNO_ACL_XATTR_TAG_ID_GROUP & tag) {
pAce->e_tag = SYNO_ACL_GROUP;
pAce->e_id = le32_to_cpu(pEntry->e_id);
} else if (SYNO_ACL_XATTR_TAG_ID_EVERYONE & tag) {
pAce->e_tag = SYNO_ACL_EVERYONE;
pAce->e_id = SYNO_ACL_UNDEFINED_ID;
} else if (SYNO_ACL_XATTR_TAG_ID_USER & tag) {
pAce->e_tag = SYNO_ACL_USER;
pAce->e_id = le32_to_cpu(pEntry->e_id);
} else if (SYNO_ACL_XATTR_TAG_ID_OWNER & tag) {
pAce->e_tag = SYNO_ACL_OWNER;
pAce->e_id = SYNO_ACL_UNDEFINED_ID;
} else {
return -1;
}
//Allow/Deny
if (SYNO_ACL_XATTR_TAG_IS_DENY & tag) {
pAce->e_allow = SYNO_ACL_DENY;
} else if (SYNO_ACL_XATTR_TAG_IS_ALLOW & tag){
pAce->e_allow = SYNO_ACL_ALLOW;
} else {
return -1;
}
//Permission
pAce->e_perm = le32_to_cpu(pEntry->e_perm);
//Inherit
pAce->e_inherit = le16_to_cpu(pEntry->e_inherit);
//level
pAce->e_level = le32_to_cpu(pEntry->e_level);
return 0;
}
static inline int
ace_syno_to_xattr(const struct syno_acl_entry *pAce, syno_acl_xattr_entry *pEntry)
{
int ret = 0;
unsigned short tag = 0;
//ID: user/group/everyone
switch(pAce->e_tag){
case SYNO_ACL_GROUP:
tag |= SYNO_ACL_XATTR_TAG_ID_GROUP;
break;
case SYNO_ACL_EVERYONE:
tag |= SYNO_ACL_XATTR_TAG_ID_EVERYONE;
break;
case SYNO_ACL_USER:
tag |= SYNO_ACL_XATTR_TAG_ID_USER;
break;
case SYNO_ACL_OWNER:
tag |= SYNO_ACL_XATTR_TAG_ID_OWNER;
break;
default:
ret = -EINVAL;
goto Err;
}
//Allow/Deny
switch(pAce->e_allow){
case SYNO_ACL_DENY:
tag |= SYNO_ACL_XATTR_TAG_IS_DENY;
break;
case SYNO_ACL_ALLOW:
tag |= SYNO_ACL_XATTR_TAG_IS_ALLOW;
break;
default:
ret = -EINVAL;
goto Err;
}
pEntry->e_tag = cpu_to_le16(tag);
pEntry->e_inherit = cpu_to_le16(pAce->e_inherit);
pEntry->e_perm = cpu_to_le32(pAce->e_perm);
pEntry->e_id = cpu_to_le32(pAce->e_id);
pEntry->e_level = cpu_to_le32(pAce->e_level);
Err:
return ret;
}
/*
* Convert from extended attribute to in-memory representation.
*/
struct syno_acl *syno_acl_from_xattr(const void *value, size_t size)
{
syno_acl_xattr_header *header;
syno_acl_xattr_entry *entry, *end;
int count;
struct syno_acl *acl;
struct syno_acl_entry *acl_e;
if (!value)
return NULL;
if (size < sizeof(syno_acl_xattr_header)){
return ERR_PTR(-EINVAL);
}
header = (syno_acl_xattr_header *)value;
entry = (syno_acl_xattr_entry *)(header+1);
if (header->a_version != cpu_to_le16(SYNO_ACL_XATTR_VERSION))
return ERR_PTR(-EOPNOTSUPP);
count = syno_acl_xattr_count(size);
if (count < 0){
return ERR_PTR(-EINVAL);
}
if (count == 0)
return NULL;
acl = syno_acl_alloc(count, GFP_KERNEL);
if (!acl)
return ERR_PTR(-ENOMEM);
acl_e = acl->a_entries;
end = entry + count;
for (; entry != end; acl_e++, entry++) {
if (0 > ace_syno_from_xattr(acl_e, entry)){
goto fail;
}
}
return acl;
fail:
syno_acl_release(acl);
return ERR_PTR(-EINVAL);
}
EXPORT_SYMBOL(syno_acl_from_xattr);
/*
* Convert from in-memory to extended attribute representation.
*/
int syno_acl_to_xattr(const struct syno_acl *acl, void *buffer, size_t size)
{
syno_acl_xattr_header *ext_acl = NULL;
syno_acl_xattr_entry *ext_entry = NULL;
int real_size, i;
int ret;
if (!acl) {
return 0;
}
real_size = syno_acl_xattr_size(acl->a_count);
if (!buffer)
return real_size;
if (real_size > size)
return -ERANGE;
ext_acl = (syno_acl_xattr_header *)buffer;
ext_entry = ext_acl->a_entries;
ext_acl->a_version = cpu_to_le16(SYNO_ACL_XATTR_VERSION);
for (i = 0; i < acl->a_count; i++, ext_entry++) {
ret = ace_syno_to_xattr(&(acl->a_entries[i]), ext_entry);
if (0 > ret) {
return ret;
}
}
return real_size;
}
EXPORT_SYMBOL(syno_acl_to_xattr);