-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_mode.c
executable file
·148 lines (132 loc) · 3.31 KB
/
parse_mode.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
/* vi: set sw=4 ts=4: */
/*
* parse_mode implementation for busybox
*
* Copyright (C) 2003 Manuel Novoa III <[email protected]>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
#include "libbb.h"
/* This function is used from NOFORK applets. It must not allocate anything */
#define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
int FAST_FUNC bb_parse_mode(const char *s, unsigned current_mode)
{
static const mode_t who_mask[] = {
S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
S_ISUID | S_IRWXU, /* u */
S_ISGID | S_IRWXG, /* g */
S_IRWXO /* o */
};
static const mode_t perm_mask[] = {
S_IRUSR | S_IRGRP | S_IROTH, /* r */
S_IWUSR | S_IWGRP | S_IWOTH, /* w */
S_IXUSR | S_IXGRP | S_IXOTH, /* x */
S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
S_ISUID | S_ISGID, /* s */
S_ISVTX /* t */
};
static const char who_chars[] ALIGN1 = "augo";
static const char perm_chars[] ALIGN1 = "rwxXst";
const char *p;
mode_t wholist;
mode_t permlist;
mode_t new_mode;
char op;
if ((unsigned char)(*s - '0') < 8) {
unsigned long tmp;
char *e;
tmp = strtoul(s, &e, 8);
if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */
return -1;
}
return tmp;
}
new_mode = current_mode;
/* Note: we allow empty clauses, and hence empty modes.
* We treat an empty mode as no change to perms. */
while (*s) { /* Process clauses. */
if (*s == ',') { /* We allow empty clauses. */
++s;
continue;
}
/* Get a wholist. */
wholist = 0;
WHO_LIST:
p = who_chars;
do {
if (*p == *s) {
wholist |= who_mask[(int)(p-who_chars)];
if (!*++s) {
return -1;
}
goto WHO_LIST;
}
} while (*++p);
do { /* Process action list. */
if ((*s != '+') && (*s != '-')) {
if (*s != '=') {
return -1;
}
/* Since op is '=', clear all bits corresponding to the
* wholist, or all file bits if wholist is empty. */
permlist = ~FILEMODEBITS;
if (wholist) {
permlist = ~wholist;
}
new_mode &= permlist;
}
op = *s++;
/* Check for permcopy. */
p = who_chars + 1; /* Skip 'a' entry. */
do {
if (*p == *s) {
int i = 0;
permlist = who_mask[(int)(p-who_chars)]
& (S_IRWXU | S_IRWXG | S_IRWXO)
& new_mode;
do {
if (permlist & perm_mask[i]) {
permlist |= perm_mask[i];
}
} while (++i < 3);
++s;
goto GOT_ACTION;
}
} while (*++p);
/* It was not a permcopy, so get a permlist. */
permlist = 0;
PERM_LIST:
p = perm_chars;
do {
if (*p == *s) {
if ((*p != 'X')
|| (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
) {
permlist |= perm_mask[(int)(p-perm_chars)];
}
if (!*++s) {
break;
}
goto PERM_LIST;
}
} while (*++p);
GOT_ACTION:
if (permlist) { /* The permlist was nonempty. */
mode_t tmp = wholist;
if (!wholist) {
mode_t u_mask = umask(0);
umask(u_mask);
tmp = ~u_mask;
}
permlist &= tmp;
if (op == '-') {
new_mode &= ~permlist;
} else {
new_mode |= permlist;
}
}
} while (*s && (*s != ','));
}
return new_mode;
}