1 /* modechange.c -- file mode manipulation
2 Copyright (C) 1989, 1990, 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie <djm@ai.mit.edu> */
20 /* The ASCII mode string is compiled into a linked list of `struct
21 modechange', which can then be applied to each file to be changed.
22 We do this instead of re-parsing the ASCII string for each file
23 because the compiled form requires less computation to use; when
24 changing the mode of many files, this probably results in a
31 #include "modechange.h"
45 #if STAT_MACROS_BROKEN
49 #if !defined(S_ISDIR) && defined(S_IFDIR)
50 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
53 /* The traditional octal values corresponding to each mode bit. */
66 #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. */
125 static struct mode_change *
126 make_node_op_equals (mode_t new_mode)
128 struct mode_change *p;
129 p = talloc (struct mode_change);
136 p->affected = CHMOD_MODE_BITS; /* Affect all permissions. */
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
158 MODE_STRING, an ASCII string that contains either an octal number
159 specifying an absolute mode, or symbolic mode change operations with
161 [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. */
171 mode_compile (const char *mode_string, unsigned int masked_ops)
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)
185 struct mode_change *p;
187 if (octal_value != (octal_value & ALLM))
190 /* Help the compiler optimize the usual case where mode_t uses
191 the traditional octal representation. */
192 mode = ((S_ISUID == SUID && S_ISGID == SGID && S_ISVTX == SVTX
193 && S_IRUSR == RUSR && S_IWUSR == WUSR && S_IXUSR == XUSR
194 && S_IRGRP == RGRP && S_IWGRP == WGRP && S_IXGRP == XGRP
195 && S_IROTH == ROTH && S_IWOTH == WOTH && S_IXOTH == XOTH)
197 : (mode_t) ((octal_value & SUID ? S_ISUID : 0)
198 | (octal_value & SGID ? S_ISGID : 0)
199 | (octal_value & SVTX ? S_ISVTX : 0)
200 | (octal_value & RUSR ? S_IRUSR : 0)
201 | (octal_value & WUSR ? S_IWUSR : 0)
202 | (octal_value & XUSR ? S_IXUSR : 0)
203 | (octal_value & RGRP ? S_IRGRP : 0)
204 | (octal_value & WGRP ? S_IWGRP : 0)
205 | (octal_value & XGRP ? S_IXGRP : 0)
206 | (octal_value & ROTH ? S_IROTH : 0)
207 | (octal_value & WOTH ? S_IWOTH : 0)
208 | (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);
217 umask_value = umask (0);
218 umask (umask_value); /* Restore the old value. */
221 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
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)
237 switch (*mode_string)
240 affected_bits |= S_ISUID | S_IRWXU;
243 affected_bits |= S_ISGID | S_IRWXG;
246 affected_bits |= S_ISVTX | S_IRWXO;
249 affected_bits |= CHMOD_MODE_BITS;
252 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 == ',');
360 if (*mode_string == 0)
367 /* 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. */
371 mode_create_from_ref (const char *ref_file)
373 struct mode_change *change; /* the only change element */
374 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;
393 /* Return file mode OLDMODE, adjusted as indicated by the list of change
394 operations CHANGES. If OLDMODE is a directory, the type `X'
395 change affects it even if no execute bits were set in OLDMODE.
396 The returned value has the S_IFMT bits cleared. */
399 mode_adjust (mode_t oldmode, const struct mode_change *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)
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);
452 /* Preserve the previous values in `newmode' of bits that are
453 not affected by this change operation. */
454 newmode = (newmode & ~changes->affected) | 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;