66
52
#define ALLM 07777 /* all octal mode bits */
102
# define S_IXOTH XOTH
105
# define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
108
# define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
111
# define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
114
/* All the mode bits that can be affected by chmod. */
115
#define CHMOD_MODE_BITS \
116
(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
118
/* Return newly allocated memory to hold one element of type TYPE. */
119
#define talloc(type) ((type *) malloc (sizeof (type)))
121
/* Create a mode_change entry with the specified `=ddd'-style
122
mode change operation, where NEW_MODE is `ddd'. Return the
123
new entry, or NULL upon failure. */
54
/* Special operations flags. */
57
/* For the sentinel at the end of the mode changes array. */
60
/* The typical case. */
63
/* In addition to the typical case, affect the execute bits if at
64
least one execute bit is set already, or if the file is a
68
/* Instead of the typical case, copy some existing permissions for
69
u, g, or o onto the other two. Which of u, g, or o is copied
70
is determined by which bits are set in the `value' field. */
74
/* Description of a mode change. */
77
char op; /* One of "=+-". */
78
char flag; /* Special operations flag. */
79
mode_t affected; /* Set for u, g, o, or a. */
80
mode_t value; /* Bits to add/remove. */
83
/* Return a mode_change array with the specified `=ddd'-style
84
mode change operation, where NEW_MODE is `ddd'. */
125
86
static struct mode_change *
126
87
make_node_op_equals (mode_t new_mode)
128
struct mode_change *p;
129
p = talloc (struct mode_change);
89
struct mode_change *p = xmalloc (2 * sizeof *p);
91
p->flag = MODE_ORDINARY_CHANGE;
92
p->affected = CHMOD_MODE_BITS;
135
93
p->value = new_mode;
136
p->affected = CHMOD_MODE_BITS; /* Affect all permissions. */
94
p[1].flag = MODE_DONE;
140
/* Append entry E to the end of the link list with the specified
144
mode_append_entry (struct mode_change **head,
145
struct mode_change **tail,
146
struct mode_change *e)
157
/* Return a linked list of file mode change operations created from
98
/* Return a pointer to an array of file mode change operations created from
158
99
MODE_STRING, an ASCII string that contains either an octal number
159
100
specifying an absolute mode, or symbolic mode change operations with
161
102
[ugoa...][[+-=][rwxXstugo...]...][,...]
162
MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
163
should not affect bits set in the umask when no users are given.
164
Operators not selected in MASKED_OPS ignore the umask.
166
Return MODE_INVALID if `mode_string' does not contain a valid
167
representation of file mode change operations;
168
return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
104
Return NULL if `mode_string' does not contain a valid
105
representation of file mode change operations. */
170
107
struct mode_change *
171
mode_compile (const char *mode_string, unsigned int masked_ops)
108
mode_compile (char const *mode_string)
173
struct mode_change *head; /* First element of the linked list. */
174
struct mode_change *tail; /* An element of the linked list. */
175
unsigned long octal_value; /* The mode value, if octal. */
176
mode_t umask_value; /* The umask value (surprise). */
183
if (xstrtoul (mode_string, NULL, 8, &octal_value, "") == LONGINT_OK)
110
/* The array of mode-change directives to be returned. */
111
struct mode_change *mc;
114
if ('0' <= *mode_string && *mode_string < '8')
185
struct mode_change *p;
187
if (octal_value != (octal_value & ALLM))
117
unsigned int octal_value = 0;
121
octal_value = 8 * octal_value + *mode_string++ - '0';
122
if (ALLM < octal_value)
125
while ('0' <= *mode_string && *mode_string < '8');
190
127
/* Help the compiler optimize the usual case where mode_t uses
191
128
the traditional octal representation. */
207
144
| (octal_value & WOTH ? S_IWOTH : 0)
208
145
| (octal_value & XOTH ? S_IXOTH : 0)));
210
p = make_node_op_equals (mode);
212
return MODE_MEMORY_EXHAUSTED;
213
mode_append_entry (&head, &tail, p);
147
return make_node_op_equals (mode);
217
umask_value = umask (0);
218
umask (umask_value); /* Restore the old value. */
150
/* Allocate enough space to hold the result. */
154
for (p = mode_string; *p; p++)
155
needed += (*p == '=' || *p == '+' || *p == '-');
156
mc = xnmalloc (needed, sizeof *mc);
221
/* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
159
/* One loop iteration for each `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'. */
160
for (;; mode_string++)
224
/* Which bits in the mode are operated on. */
225
mode_t affected_bits = 0;
226
/* `affected_bits' modified by umask. */
227
mode_t affected_masked;
228
/* Operators to actually use umask on. */
229
unsigned ops_to_mask = 0;
235
/* Turn on all the bits in `affected_bits' for each group given. */
236
for (++mode_string;; ++mode_string)
162
/* Which bits in the mode are operated on. */
165
/* Turn on all the bits in `affected' for each group given. */
166
for (;; mode_string++)
237
167
switch (*mode_string)
240
affected_bits |= S_ISUID | S_IRWXU;
172
affected |= S_ISUID | S_IRWXU;
243
affected_bits |= S_ISGID | S_IRWXG;
175
affected |= S_ISGID | S_IRWXG;
246
affected_bits |= S_ISVTX | S_IRWXO;
178
affected |= S_ISVTX | S_IRWXO;
249
affected_bits |= CHMOD_MODE_BITS;
181
affected |= CHMOD_MODE_BITS;
183
case '=': case '+': case '-':
252
184
goto no_more_affected;
256
/* If none specified, affect all bits, except perhaps those
263
affected_bits = CHMOD_MODE_BITS;
264
ops_to_mask = masked_ops;
267
while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
269
struct mode_change *change = talloc (struct mode_change);
273
return MODE_MEMORY_EXHAUSTED;
277
change->op = *mode_string; /* One of "=+-". */
278
affected_masked = affected_bits;
280
/* Per the Single Unix Spec, if `who' is not specified and the
281
`=' operator is used, then clear all the bits first. */
282
if (!who_specified_p &&
283
ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS : 0))
285
struct mode_change *p = make_node_op_equals (0);
287
return MODE_MEMORY_EXHAUSTED;
288
mode_append_entry (&head, &tail, p);
291
if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
292
: *mode_string == '+' ? MODE_MASK_PLUS
294
affected_masked &= ~umask_value;
295
change->affected = affected_masked;
299
/* Add the element to the tail of the list, so the operations
300
are performed in the correct order. */
301
mode_append_entry (&head, &tail, change);
303
/* Set `value' according to the bits set in `affected_masked'. */
304
for (++mode_string;; ++mode_string)
305
switch (*mode_string)
308
change->value |= ((S_IRUSR | S_IRGRP | S_IROTH)
312
change->value |= ((S_IWUSR | S_IWGRP | S_IWOTH)
316
change->flags |= MODE_X_IF_ANY_X;
319
change->value |= ((S_IXUSR | S_IXGRP | S_IXOTH)
323
/* Set the setuid/gid bits if `u' or `g' is selected. */
324
change->value |= (S_ISUID | S_ISGID) & affected_masked;
327
/* Set the "save text image" bit if `o' is selected. */
328
change->value |= S_ISVTX & affected_masked;
331
/* Set the affected bits to the value of the `u' bits
335
change->value = S_IRWXU;
336
change->flags |= MODE_COPY_EXISTING;
339
/* Set the affected bits to the value of the `g' bits
343
change->value = S_IRWXG;
344
change->flags |= MODE_COPY_EXISTING;
347
/* Set the affected bits to the value of the `o' bits
351
change->value = S_IRWXO;
352
change->flags |= MODE_COPY_EXISTING;
359
} while (*mode_string == ',');
190
char op = *mode_string++;
192
char flag = MODE_COPY_EXISTING;
193
struct mode_change *change;
195
switch (*mode_string++)
198
/* Set the affected bits to the value of the `u' bits
203
/* Set the affected bits to the value of the `g' bits
208
/* Set the affected bits to the value of the `o' bits
215
flag = MODE_ORDINARY_CHANGE;
217
for (mode_string--;; mode_string++)
218
switch (*mode_string)
221
value |= S_IRUSR | S_IRGRP | S_IROTH;
224
value |= S_IWUSR | S_IWGRP | S_IWOTH;
227
value |= S_IXUSR | S_IXGRP | S_IXOTH;
230
flag = MODE_X_IF_ANY_X;
233
/* Set the setuid/gid bits if `u' or `g' is selected. */
234
value |= S_ISUID | S_ISGID;
237
/* Set the "save text image" bit if `o' is selected. */
246
change = &mc[used++];
249
change->affected = affected;
250
change->value = value;
252
while (*mode_string == '=' || *mode_string == '+'
253
|| *mode_string == '-');
255
if (*mode_string != ',')
360
259
if (*mode_string == 0)
261
mc[used].flag = MODE_DONE;
367
270
/* Return a file mode change operation that sets permissions to match those
368
of REF_FILE. Return MODE_BAD_REFERENCE if REF_FILE can't be accessed. */
271
of REF_FILE. Return NULL (setting errno) if REF_FILE can't be accessed. */
370
273
struct mode_change *
371
274
mode_create_from_ref (const char *ref_file)
373
struct mode_change *change; /* the only change element */
374
276
struct stat ref_stats;
376
if (stat (ref_file, &ref_stats))
377
return MODE_BAD_REFERENCE;
379
change = talloc (struct mode_change);
382
return MODE_MEMORY_EXHAUSTED;
386
change->affected = CHMOD_MODE_BITS;
387
change->value = ref_stats.st_mode;
278
if (stat (ref_file, &ref_stats) != 0)
280
return make_node_op_equals (ref_stats.st_mode);
393
283
/* Return file mode OLDMODE, adjusted as indicated by the list of change
394
operations CHANGES. If OLDMODE is a directory, the type `X'
284
operations CHANGES, which are interpreted assuming the umask is
285
UMASK_VALUE. If OLDMODE is a directory, the type `X'
395
286
change affects it even if no execute bits were set in OLDMODE.
396
The returned value has the S_IFMT bits cleared. */
287
The returned value has the S_IFMT bits cleared. */
399
mode_adjust (mode_t oldmode, const struct mode_change *changes)
290
mode_adjust (mode_t oldmode, struct mode_change const *changes,
401
mode_t newmode; /* The adjusted mode and one operand. */
402
mode_t value; /* The other operand. */
404
newmode = oldmode & CHMOD_MODE_BITS;
406
for (; changes; changes = changes->next)
293
/* The adjusted mode. */
294
mode_t newmode = oldmode & CHMOD_MODE_BITS;
296
for (; changes->flag != MODE_DONE; changes++)
408
if (changes->flags & MODE_COPY_EXISTING)
410
/* Isolate in `value' the bits in `newmode' to copy, given in
411
the mask `changes->value'. */
412
value = newmode & changes->value;
414
if (changes->value & S_IRWXU)
415
/* Copy `u' permissions onto `g' and `o'. */
416
value |= ( (value & S_IRUSR ? S_IRGRP | S_IROTH : 0)
417
| (value & S_IWUSR ? S_IWGRP | S_IWOTH : 0)
418
| (value & S_IXUSR ? S_IXGRP | S_IXOTH : 0));
419
else if (changes->value & S_IRWXG)
420
/* Copy `g' permissions onto `u' and `o'. */
421
value |= ( (value & S_IRGRP ? S_IRUSR | S_IROTH : 0)
422
| (value & S_IWGRP ? S_IWUSR | S_IWOTH : 0)
423
| (value & S_IXGRP ? S_IXUSR | S_IXOTH : 0));
425
/* Copy `o' permissions onto `u' and `g'. */
426
value |= ( (value & S_IROTH ? S_IRUSR | S_IRGRP : 0)
427
| (value & S_IWOTH ? S_IWUSR | S_IWGRP : 0)
428
| (value & S_IXOTH ? S_IXUSR | S_IXGRP : 0));
430
/* In order to change only `u', `g', or `o' permissions,
431
or some combination thereof, clear unselected bits.
432
This cannot be done in mode_compile because the value
433
to which the `changes->affected' mask is applied depends
434
on the old mode of each file. */
435
value &= changes->affected;
439
value = changes->value;
440
/* If `X', do not affect the execute bits if the file is not a
441
directory and no execute bits are already set. */
442
if ((changes->flags & MODE_X_IF_ANY_X)
443
&& !S_ISDIR (oldmode)
444
&& (newmode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
445
/* Clear the execute bits. */
446
value &= ~ (S_IXUSR | S_IXGRP | S_IXOTH);
298
mode_t affected = changes->affected;
299
mode_t value = changes->value;
301
switch (changes->flag)
303
case MODE_ORDINARY_CHANGE:
306
case MODE_COPY_EXISTING:
307
/* Isolate in `value' the bits in `newmode' to copy. */
310
/* Copy the isolated bits to the other two parts. */
311
value |= ((value & (S_IRUSR | S_IRGRP | S_IROTH)
312
? S_IRUSR | S_IRGRP | S_IROTH : 0)
313
| (value & (S_IWUSR | S_IWGRP | S_IWOTH)
314
? S_IWUSR | S_IWGRP | S_IWOTH : 0)
315
| (value & (S_IXUSR | S_IXGRP | S_IXOTH)
316
? S_IXUSR | S_IXGRP | S_IXOTH : 0));
319
case MODE_X_IF_ANY_X:
320
/* Affect the execute bits if execute bits are already set
321
or if the file is a directory. */
322
if ((newmode & (S_IXUSR | S_IXGRP | S_IXOTH)) || S_ISDIR (oldmode))
323
value |= S_IXUSR | S_IXGRP | S_IXOTH;
327
/* If WHO was specified, limit the change to the affected bits.
328
Otherwise, apply the umask. */
329
value &= (affected ? affected : ~umask_value);
449
331
switch (changes->op)
452
/* Preserve the previous values in `newmode' of bits that are
453
not affected by this change operation. */
454
newmode = (newmode & ~changes->affected) | value;
334
/* If WHO was specified, preserve the previous values of
335
bits that are not affected by this change operation.
336
Otherwise, clear all the bits. */
337
newmode = (affected ? newmode & ~affected : 0);
457
340
newmode |= value;
460
344
newmode &= ~value;
467
/* Free the memory used by the list of file mode change operations
471
mode_free (register struct mode_change *changes)
473
register struct mode_change *next;
477
next = changes->next;