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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
Copyright 2006,2007,2008 Martin Pärtel <martin.partel@gmail.com>
This file is part of bindfs.
bindfs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
bindfs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with bindfs. If not, see <http://www.gnu.org/licenses/>.
*/
#include "permchain.h"
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include "misc.h"
#include "debug.h"
/* constants for permchain->flags */
#define PC_APPLY_FILES 1
#define PC_APPLY_DIRS 2
#define PC_FLAGS_DEFAULT ((PC_APPLY_FILES) | (PC_APPLY_DIRS))
struct permchain {
char op; /* one of '=', '+', '-', 'o' (octal) or '\0' */
char flags; /* see 'PC_' constants above. */
mode_t mask; /* which permissions to apply to */
union {
char operands[16]; /* a subset of rwxXDstugo */
unsigned int octal;
};
struct permchain *next;
};
struct permchain *permchain_create()
{
struct permchain *pc = malloc(sizeof(struct permchain));
pc->mask = 0000;
pc->op = '\0';
memset(pc->operands, '\0', sizeof(pc->operands));
pc->next = NULL;
pc->flags = PC_FLAGS_DEFAULT;
return pc;
}
static int add_chmod_rule_to_permchain(const char *start, const char *end,
struct permchain *pc);
static int add_octal_rule_to_permchain(const char *start, const char *end,
struct permchain *pc);
static mode_t modebits_to_all(int perms); /* e.g. 5 -> 0555 */
static int add_chmod_rule_to_permchain(const char *start, const char *end,
struct permchain *pc)
{
int ret = -1;
int len = end - start;
char *buf = alloca((len + 1) * sizeof(char));
const char *p = buf;
enum {LHS, RHS} state = LHS;
struct permchain *newpc = permchain_create();
char *operands_ptr = newpc->operands;
newpc->flags = 0; /* Reset to PC_FLAGS_DEFAULT in the end if not modified */
memcpy(buf, start, len);
buf[len] = '\0';
while (*p != '\0') {
if (state == LHS) {
switch (*p) {
case 'u':
newpc->mask |= 0700;
break;
case 'g':
newpc->mask |= 0070;
break;
case 'o':
newpc->mask |= 0007;
break;
case 'a':
newpc->mask = 0777;
break;
case 'f':
newpc->flags |= PC_APPLY_FILES;
break;
case 'd':
newpc->flags |= PC_APPLY_DIRS;
break;
case '=':
case '+':
case '-':
if (p == buf) /* first char -> default to 'a' */
newpc->mask = 0777;
newpc->op = *p;
state = RHS;
break;
default:
goto error;
}
} else {
switch (*p) {
case 'r':
case 'w':
case 'x':
case 'X':
case 'D':
case 's':
case 't':
case 'u':
case 'g':
case 'o':
if (!strchr(newpc->operands, *p)) {
*(operands_ptr++) = *p;
}
break;
default:
goto error;
}
}
++p;
}
ret = 0;
error:
if (newpc->flags == 0)
newpc->flags = PC_FLAGS_DEFAULT;
if (ret == 0)
permchain_cat(pc, newpc);
else
permchain_destroy(newpc);
return ret;
}
static int add_octal_rule_to_permchain(const char *start, const char *end,
struct permchain *pc)
{
struct permchain *newpc = permchain_create();
long mode = strtol(start, NULL, 8);
if (mode < 0 || mode > 0777) {
permchain_destroy(newpc);
return -1;
}
newpc->mask = 0777;
newpc->op = 'o';
newpc->octal = mode;
permchain_cat(pc, newpc);
return 0;
}
int add_chmod_rules_to_permchain(const char *rule, struct permchain *pc)
{
int ret = -1;
const char *start, *end;
struct permchain *newpc = permchain_create();
assert(rule != 0);
end = start = rule;
while (*end != '\0') {
/* find delimiter or end of list */
while (*end != ',' && *end != ':' && *end != '\0')
++end;
if (start == end) /* empty rule */
goto error;
assert(start < end);
if (isdigit(*start)) {
if (add_octal_rule_to_permchain(start, end, newpc) != 0)
goto error;
} else {
if (add_chmod_rule_to_permchain(start, end, newpc) != 0)
goto error;
}
if (*end == ',' || *end == ':')
start = ++end;
}
ret = 0;
error:
if (ret == 0)
permchain_cat(pc, newpc);
else
permchain_destroy(newpc);
return ret;
}
void permchain_cat(struct permchain *left, struct permchain *right)
{
while (left->next != NULL)
left = left->next;
left->next = right;
}
mode_t modebits_to_all(int perms)
{
mode_t m = perms;
m |= perms << 3;
m |= perms << 6;
return m;
}
mode_t permchain_apply(struct permchain *pc, mode_t tgtmode)
{
mode_t original_mode = tgtmode;
mode_t mode = 0000;
const char *p;
while (pc != NULL) {
#if BINDFS_DEBUG
if (pc->op == 'o')
DPRINTF("STAT MODE: %o, op = %c %o", tgtmode, pc->op, pc->octal);
else
DPRINTF("STAT MODE: %o, op = %c%s", tgtmode, pc->op, pc->operands);
#endif
if (pc->op == '\0') {
pc = pc->next;
continue;
}
if ((S_ISDIR(tgtmode) && !(pc->flags & PC_APPLY_DIRS))
||
(!S_ISDIR(tgtmode) && !(pc->flags & PC_APPLY_FILES))) {
pc = pc->next;
continue;
}
if (pc->op == '=' || pc->op == '+' || pc->op == '-') {
mode = 0000;
for (p = pc->operands; *p != '\0'; ++p) {
switch (*p) {
case 'r':
mode |= 0444;
break;
case 'w':
mode |= 0222;
break;
case 'x':
mode |= 0111;
break;
case 'X':
if (S_ISDIR(original_mode) || ((original_mode & 0111) != 0))
mode |= 0111;
break;
case 'D':
if (S_ISDIR(original_mode))
mode |= 0111;
break;
case 's':
case 't':
/* ignored */
break;
case 'u':
mode |= modebits_to_all((original_mode & 0700) >> 6);
break;
case 'g':
mode |= modebits_to_all((original_mode & 0070) >> 3);
break;
case 'o':
mode |= modebits_to_all(original_mode & 0007);
break;
default:
assert(0);
}
}
mode &= pc->mask;
}
switch (pc->op) {
case '=':
tgtmode = (tgtmode & ~pc->mask) | mode;
break;
case '+':
tgtmode |= mode;
break;
case '-':
tgtmode &= ~0777 | ~mode;
break;
case 'o':
tgtmode = (tgtmode & ~0777) | pc->octal;
break;
default:
assert(0);
}
pc = pc->next;
DPRINTF(" =>: %o", tgtmode);
}
return tgtmode;
}
void permchain_destroy(struct permchain *pc)
{
struct permchain *next;
while (pc) {
next = pc->next;
free(pc);
pc = next;
}
}
|