1 /* modechange.c -- file mode manipulation
3 Copyright (C) 1989, 1990, 1997, 1998, 1999, 2001, 2003, 2004 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by David MacKenzie <djm@ai.mit.edu> */
22 /* The ASCII mode string is compiled into a linked list of `struct
23 modechange', which can then be applied to each file to be changed.
24 We do this instead of re-parsing the ASCII string for each file
25 because the compiled form requires less computation to use; when
26 changing the mode of many files, this probably results in a
33 #include "modechange.h"
40 #if STAT_MACROS_BROKEN
44 #if !defined(S_ISDIR) && defined(S_IFDIR)
45 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
48 /* The traditional octal values corresponding to each mode bit. */
61 #define ALLM 07777 /* all octal mode bits */
100 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
103 # define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
106 # define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
109 /* All the mode bits that can be affected by chmod. */
110 #define CHMOD_MODE_BITS \
111 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
113 /* Return newly allocated memory to hold one element of type TYPE. */
114 #define talloc(type) ((type *) malloc (sizeof (type)))
116 /* Create a mode_change entry with the specified `=ddd'-style
117 mode change operation, where NEW_MODE is `ddd'. Return the
118 new entry, or NULL upon failure. */
120 static struct mode_change *
121 make_node_op_equals (mode_t new_mode)
123 struct mode_change *p;
124 p = talloc (struct mode_change);
131 p->affected = CHMOD_MODE_BITS; /* Affect all permissions. */
135 /* Append entry E to the end of the link list with the specified
139 mode_append_entry (struct mode_change **head,
140 struct mode_change **tail,
141 struct mode_change *e)
152 /* Return a linked list of file mode change operations created from
153 MODE_STRING, an ASCII string that contains either an octal number
154 specifying an absolute mode, or symbolic mode change operations with
156 [ugoa...][[+-=][rwxXstugo...]...][,...]
157 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
158 should not affect bits set in the umask when no users are given.
159 Operators not selected in MASKED_OPS ignore the umask.
161 Return MODE_INVALID if `mode_string' does not contain a valid
162 representation of file mode change operations;
163 return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
166 mode_compile (const char *mode_string, unsigned int masked_ops)
168 struct mode_change *head; /* First element of the linked list. */
169 struct mode_change *tail; /* An element of the linked list. */
170 unsigned long octal_value; /* The mode value, if octal. */
171 mode_t umask_value; /* The umask value (surprise). */
178 if (xstrtoul (mode_string, NULL, 8, &octal_value, "") == LONGINT_OK)
180 struct mode_change *p;
182 if (octal_value != (octal_value & ALLM))
185 /* Help the compiler optimize the usual case where mode_t uses
186 the traditional octal representation. */
187 mode = ((S_ISUID == SUID && S_ISGID == SGID && S_ISVTX == SVTX
188 && S_IRUSR == RUSR && S_IWUSR == WUSR && S_IXUSR == XUSR
189 && S_IRGRP == RGRP && S_IWGRP == WGRP && S_IXGRP == XGRP
190 && S_IROTH == ROTH && S_IWOTH == WOTH && S_IXOTH == XOTH)
192 : (mode_t) ((octal_value & SUID ? S_ISUID : 0)
193 | (octal_value & SGID ? S_ISGID : 0)
194 | (octal_value & SVTX ? S_ISVTX : 0)
195 | (octal_value & RUSR ? S_IRUSR : 0)
196 | (octal_value & WUSR ? S_IWUSR : 0)
197 | (octal_value & XUSR ? S_IXUSR : 0)
198 | (octal_value & RGRP ? S_IRGRP : 0)
199 | (octal_value & WGRP ? S_IWGRP : 0)
200 | (octal_value & XGRP ? S_IXGRP : 0)
201 | (octal_value & ROTH ? S_IROTH : 0)
202 | (octal_value & WOTH ? S_IWOTH : 0)
203 | (octal_value & XOTH ? S_IXOTH : 0)));
205 p = make_node_op_equals (mode);
207 return MODE_MEMORY_EXHAUSTED;
208 mode_append_entry (&head, &tail, p);
212 umask_value = umask (0);
213 umask (umask_value); /* Restore the old value. */
216 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
219 /* Which bits in the mode are operated on. */
220 mode_t affected_bits = 0;
221 /* `affected_bits' modified by umask. */
222 mode_t affected_masked;
223 /* Operators to actually use umask on. */
224 unsigned int ops_to_mask = 0;
226 bool who_specified_p;
228 /* Turn on all the bits in `affected_bits' for each group given. */
229 for (++mode_string;; ++mode_string)
230 switch (*mode_string)
233 affected_bits |= S_ISUID | S_IRWXU;
236 affected_bits |= S_ISGID | S_IRWXG;
239 affected_bits |= S_ISVTX | S_IRWXO;
242 affected_bits |= CHMOD_MODE_BITS;
245 goto no_more_affected;
249 /* If none specified, affect all bits, except perhaps those
252 who_specified_p = true;
255 who_specified_p = false;
256 affected_bits = CHMOD_MODE_BITS;
257 ops_to_mask = masked_ops;
260 while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
262 struct mode_change *change = talloc (struct mode_change);
266 return MODE_MEMORY_EXHAUSTED;
270 change->op = *mode_string; /* One of "=+-". */
271 affected_masked = affected_bits;
273 /* Per the Single Unix Spec, if `who' is not specified and the
274 `=' operator is used, then clear all the bits first. */
275 if (!who_specified_p &&
276 ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS : 0))
278 struct mode_change *p = make_node_op_equals (0);
280 return MODE_MEMORY_EXHAUSTED;
281 mode_append_entry (&head, &tail, p);
284 if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
285 : *mode_string == '+' ? MODE_MASK_PLUS
287 affected_masked &= ~umask_value;
288 change->affected = affected_masked;
292 /* Add the element to the tail of the list, so the operations
293 are performed in the correct order. */
294 mode_append_entry (&head, &tail, change);
296 /* Set `value' according to the bits set in `affected_masked'. */
297 for (++mode_string;; ++mode_string)
298 switch (*mode_string)
301 change->value |= ((S_IRUSR | S_IRGRP | S_IROTH)
305 change->value |= ((S_IWUSR | S_IWGRP | S_IWOTH)
309 change->flags |= MODE_X_IF_ANY_X;
312 change->value |= ((S_IXUSR | S_IXGRP | S_IXOTH)
316 /* Set the setuid/gid bits if `u' or `g' is selected. */
317 change->value |= (S_ISUID | S_ISGID) & affected_masked;
320 /* Set the "save text image" bit if `o' is selected. */
321 change->value |= S_ISVTX & affected_masked;
324 /* Set the affected bits to the value of the `u' bits
328 change->value = S_IRWXU;
329 change->flags |= MODE_COPY_EXISTING;
332 /* Set the affected bits to the value of the `g' bits
336 change->value = S_IRWXG;
337 change->flags |= MODE_COPY_EXISTING;
340 /* Set the affected bits to the value of the `o' bits
344 change->value = S_IRWXO;
345 change->flags |= MODE_COPY_EXISTING;
352 } while (*mode_string == ',');
353 if (*mode_string == 0)
360 /* Return a file mode change operation that sets permissions to match those
361 of REF_FILE. Return MODE_BAD_REFERENCE if REF_FILE can't be accessed. */
364 mode_create_from_ref (const char *ref_file)
366 struct mode_change *change; /* the only change element */
367 struct stat ref_stats;
369 if (stat (ref_file, &ref_stats))
370 return MODE_BAD_REFERENCE;
372 change = talloc (struct mode_change);
375 return MODE_MEMORY_EXHAUSTED;
379 change->affected = CHMOD_MODE_BITS;
380 change->value = ref_stats.st_mode;
386 /* Return file mode OLDMODE, adjusted as indicated by the list of change
387 operations CHANGES. If OLDMODE is a directory, the type `X'
388 change affects it even if no execute bits were set in OLDMODE.
389 The returned value has the S_IFMT bits cleared. */
392 mode_adjust (mode_t oldmode, const struct mode_change *changes)
394 mode_t newmode; /* The adjusted mode and one operand. */
395 mode_t value; /* The other operand. */
397 newmode = oldmode & CHMOD_MODE_BITS;
399 for (; changes; changes = changes->next)
401 if (changes->flags & MODE_COPY_EXISTING)
403 /* Isolate in `value' the bits in `newmode' to copy, given in
404 the mask `changes->value'. */
405 value = newmode & changes->value;
407 if (changes->value & S_IRWXU)
408 /* Copy `u' permissions onto `g' and `o'. */
409 value |= ( (value & S_IRUSR ? S_IRGRP | S_IROTH : 0)
410 | (value & S_IWUSR ? S_IWGRP | S_IWOTH : 0)
411 | (value & S_IXUSR ? S_IXGRP | S_IXOTH : 0));
412 else if (changes->value & S_IRWXG)
413 /* Copy `g' permissions onto `u' and `o'. */
414 value |= ( (value & S_IRGRP ? S_IRUSR | S_IROTH : 0)
415 | (value & S_IWGRP ? S_IWUSR | S_IWOTH : 0)
416 | (value & S_IXGRP ? S_IXUSR | S_IXOTH : 0));
418 /* Copy `o' permissions onto `u' and `g'. */
419 value |= ( (value & S_IROTH ? S_IRUSR | S_IRGRP : 0)
420 | (value & S_IWOTH ? S_IWUSR | S_IWGRP : 0)
421 | (value & S_IXOTH ? S_IXUSR | S_IXGRP : 0));
423 /* In order to change only `u', `g', or `o' permissions,
424 or some combination thereof, clear unselected bits.
425 This cannot be done in mode_compile because the value
426 to which the `changes->affected' mask is applied depends
427 on the old mode of each file. */
428 value &= changes->affected;
432 value = changes->value;
433 /* If `X', do not affect the execute bits if the file is not a
434 directory and no execute bits are already set. */
435 if ((changes->flags & MODE_X_IF_ANY_X)
436 && !S_ISDIR (oldmode)
437 && (newmode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
438 /* Clear the execute bits. */
439 value &= ~ (S_IXUSR | S_IXGRP | S_IXOTH);
445 /* Preserve the previous values in `newmode' of bits that are
446 not affected by this change operation. */
447 newmode = (newmode & ~changes->affected) | value;
460 /* Free the memory used by the list of file mode change operations
464 mode_free (register struct mode_change *changes)
466 register struct mode_change *next;
470 next = changes->next;