Debian Codegraph @

chmod.c

raw symbols SCIP
1 /*
2 * Implement the core of the --chmod option.
3 *
4 * Copyright (C) 2002 Scott Howard
5 * Copyright (C) 2005-2020 Wayne Davison
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, visit the http://fsf.org website.
19 */
20
21 #include "rsync.h"
22 #include "itypes.h"
23
24 extern mode_t orig_umask;
25
26 #define FLAG_X_KEEP (1<<0)
27 #define FLAG_DIRS_ONLY (1<<1)
28 #define FLAG_FILES_ONLY (1<<2)
29
30 struct chmod_mode_struct {
31 struct chmod_mode_struct *next;
32 int ModeAND, ModeOR, ModeCOPY_SRC, ModeCOPY_DST, ModeCOPY_AND, ModeOP;
33 char flags;
34 };
35
36 #define CHMOD_ADD 1
37 #define CHMOD_SUB 2
38 #define CHMOD_EQ 3
39 #define CHMOD_SET 4
40
41 #define STATE_ERROR 0
42 #define STATE_1ST_HALF 1
43 #define STATE_2ND_HALF 2
44 #define STATE_OCTAL_NUM 3
45
46 static int mode_dest_special_bits(int where)
47 {
48 int bits = 0;
49
50 if (where & 0100)
51 bits |= S_ISUID;
52 if (where & 0010)
53 bits |= S_ISGID;
54 if (where & 0001)
55 bits |= S_ISVTX;
56
57 return bits;
58 }
59
60 /* Parse a chmod-style argument, and break it down into one or more AND/OR
61 * pairs in a linked list. We return a pointer to new items on success
62 * (appending the items to the specified list), or NULL on error. */
63 struct chmod_mode_struct *parse_chmod(const char *modestr,
64 struct chmod_mode_struct **root_mode_ptr)
65 {
66 int state = STATE_1ST_HALF;
67 int where = 0, what = 0, op = 0, topbits = 0, topoct = 0, flags = 0, copybits = 0;
68 struct chmod_mode_struct *first_mode = NULL, *curr_mode = NULL,
69 *prev_mode = NULL;
70
71 while (state != STATE_ERROR) {
72 if (!*modestr || *modestr == ',') {
73 int bits, where_specified;
74
75 if (!op) {
76 state = STATE_ERROR;
77 break;
78 }
79 prev_mode = curr_mode;
80 curr_mode = new_array(struct chmod_mode_struct, 1);
81 if (prev_mode)
82 prev_mode->next = curr_mode;
83 else
84 first_mode = curr_mode;
85 curr_mode->next = NULL;
86
87 where_specified = where;
88 if (where) {
89 bits = where * what;
90 } else {
91 where = 0111;
92 bits = (where * what) & ~orig_umask;
93 }
94
95 switch (op) {
96 case CHMOD_ADD:
97 curr_mode->ModeAND = CHMOD_BITS;
98 curr_mode->ModeOR = bits + topoct;
99 curr_mode->ModeCOPY_SRC = copybits;
100 curr_mode->ModeCOPY_DST = where;
101 curr_mode->ModeCOPY_AND = where_specified ? CHMOD_BITS : ~orig_umask;
102 curr_mode->ModeOP = op;
103 break;
104 case CHMOD_SUB:
105 curr_mode->ModeAND = CHMOD_BITS - bits - topoct;
106 curr_mode->ModeOR = 0;
107 curr_mode->ModeCOPY_SRC = copybits;
108 curr_mode->ModeCOPY_DST = where;
109 curr_mode->ModeCOPY_AND = where_specified ? CHMOD_BITS : ~orig_umask;
110 curr_mode->ModeOP = op;
111 break;
112 case CHMOD_EQ:
113 curr_mode->ModeAND = CHMOD_BITS - (where * 7) - (topoct ? topbits : 0)
114 - (copybits ? mode_dest_special_bits(where) : 0);
115 curr_mode->ModeOR = bits + topoct;
116 curr_mode->ModeCOPY_SRC = copybits;
117 curr_mode->ModeCOPY_DST = where;
118 curr_mode->ModeCOPY_AND = where_specified ? CHMOD_BITS : ~orig_umask;
119 curr_mode->ModeOP = op;
120 break;
121 case CHMOD_SET:
122 curr_mode->ModeAND = 0;
123 curr_mode->ModeOR = bits;
124 curr_mode->ModeCOPY_SRC = 0;
125 curr_mode->ModeCOPY_DST = 0;
126 curr_mode->ModeCOPY_AND = CHMOD_BITS;
127 curr_mode->ModeOP = op;
128 break;
129 }
130
131 curr_mode->flags = flags;
132
133 if (!*modestr)
134 break;
135 modestr++;
136
137 state = STATE_1ST_HALF;
138 where = what = op = topoct = topbits = flags = copybits = 0;
139 }
140
141 switch (state) {
142 case STATE_1ST_HALF:
143 switch (*modestr) {
144 case 'D':
145 if (flags & FLAG_FILES_ONLY)
146 state = STATE_ERROR;
147 flags |= FLAG_DIRS_ONLY;
148 break;
149 case 'F':
150 if (flags & FLAG_DIRS_ONLY)
151 state = STATE_ERROR;
152 flags |= FLAG_FILES_ONLY;
153 break;
154 case 'u':
155 where |= 0100;
156 topbits |= 04000;
157 break;
158 case 'g':
159 where |= 0010;
160 topbits |= 02000;
161 break;
162 case 'o':
163 where |= 0001;
164 break;
165 case 'a':
166 where |= 0111;
167 topbits |= 06000; /* a+s sets BOTH setuid and setgid (like chmod(1)) */
168 break;
169 case '+':
170 op = CHMOD_ADD;
171 state = STATE_2ND_HALF;
172 break;
173 case '-':
174 op = CHMOD_SUB;
175 state = STATE_2ND_HALF;
176 break;
177 case '=':
178 op = CHMOD_EQ;
179 state = STATE_2ND_HALF;
180 break;
181 default:
182 if (isDigit(modestr) && *modestr < '8' && !where) {
183 op = CHMOD_SET;
184 state = STATE_OCTAL_NUM;
185 where = 1;
186 what = *modestr - '0';
187 } else
188 state = STATE_ERROR;
189 break;
190 }
191 break;
192 case STATE_2ND_HALF:
193 switch (*modestr) {
194 case 'r':
195 if (copybits)
196 state = STATE_ERROR;
197 what |= 4;
198 break;
199 case 'w':
200 if (copybits)
201 state = STATE_ERROR;
202 what |= 2;
203 break;
204 case 'X':
205 if (copybits)
206 state = STATE_ERROR;
207 flags |= FLAG_X_KEEP;
208 /* FALL THROUGH */
209 case 'x':
210 if (copybits)
211 state = STATE_ERROR;
212 what |= 1;
213 break;
214 case 's':
215 if (copybits)
216 state = STATE_ERROR;
217 if (topbits)
218 topoct |= topbits;
219 else
220 topoct = 04000;
221 break;
222 case 't':
223 if (copybits)
224 state = STATE_ERROR;
225 topoct |= 01000;
226 break;
227 case 'u':
228 if (what || topoct || copybits)
229 state = STATE_ERROR;
230 copybits = 0100;
231 break;
232 case 'g':
233 if (what || topoct || copybits)
234 state = STATE_ERROR;
235 copybits = 0010;
236 break;
237 case 'o':
238 if (what || topoct || copybits)
239 state = STATE_ERROR;
240 copybits = 0001;
241 break;
242 default:
243 state = STATE_ERROR;
244 break;
245 }
246 break;
247 case STATE_OCTAL_NUM:
248 if (isDigit(modestr) && *modestr < '8') {
249 what = what*8 + *modestr - '0';
250 if (what > CHMOD_BITS)
251 state = STATE_ERROR;
252 } else
253 state = STATE_ERROR;
254 break;
255 }
256 modestr++;
257 }
258
259 if (state == STATE_ERROR) {
260 free_chmod_mode(first_mode);
261 return NULL;
262 }
263
264 if (!(curr_mode = *root_mode_ptr))
265 *root_mode_ptr = first_mode;
266 else {
267 while (curr_mode->next)
268 curr_mode = curr_mode->next;
269 curr_mode->next = first_mode;
270 }
271
272 return first_mode;
273 }
274
275 static int mode_copy_bits(int mode, int copy_src, int copy_dst, int copy_and)
276 {
277 int copy_bits = 0;
278
279 if (copy_src & 0100)
280 copy_bits |= (mode >> 6) & 7;
281 if (copy_src & 0010)
282 copy_bits |= (mode >> 3) & 7;
283 if (copy_src & 0001)
284 copy_bits |= mode & 7;
285
286 return (copy_dst * copy_bits) & copy_and;
287 }
288
289
290 /* Takes an existing file permission and a list of AND/OR changes, and
291 * create a new permissions. */
292 int tweak_mode(int mode, struct chmod_mode_struct *chmod_modes)
293 {
294 int IsX = mode & 0111;
295 int NonPerm = mode & ~CHMOD_BITS;
296 int copy_bits;
297
298 for ( ; chmod_modes; chmod_modes = chmod_modes->next) {
299 if ((chmod_modes->flags & FLAG_DIRS_ONLY) && !S_ISDIR(NonPerm))
300 continue;
301 if ((chmod_modes->flags & FLAG_FILES_ONLY) && S_ISDIR(NonPerm))
302 continue;
303 copy_bits = mode_copy_bits(mode, chmod_modes->ModeCOPY_SRC,
304 chmod_modes->ModeCOPY_DST,
305 chmod_modes->ModeCOPY_AND);
306 mode &= chmod_modes->ModeAND;
307 if ((chmod_modes->flags & FLAG_X_KEEP) && !IsX && !S_ISDIR(NonPerm))
308 mode |= chmod_modes->ModeOR & ~0111;
309 else
310 mode |= chmod_modes->ModeOR;
311 if (chmod_modes->ModeOP == CHMOD_SUB)
312 mode &= CHMOD_BITS - copy_bits;
313 else
314 mode |= copy_bits;
315 }
316
317 return mode | NonPerm;
318 }
319
320 /* Free the linked list created by parse_chmod. */
321 int free_chmod_mode(struct chmod_mode_struct *chmod_modes)
322 {
323 struct chmod_mode_struct *next;
324
325 while (chmod_modes) {
326 next = chmod_modes->next;
327 free(chmod_modes);
328 chmod_modes = next;
329 }
330 return 0;
331 }