1 /* modechange.c -- file mode manipulation
3 Copyright (C) 1989, 1990, 1997, 1998, 1999, 2001, 2003 Free Software
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"
39 #if STAT_MACROS_BROKEN
43 #if !defined(S_ISDIR) && defined(S_IFDIR)
44 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
47 /* The traditional octal values corresponding to each mode bit. */
60 #define ALLM 07777 /* all octal mode bits */
99 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
102 # define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
105 # define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
108 /* All the mode bits that can be affected by chmod. */
109 #define CHMOD_MODE_BITS \
110 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
112 /* Return newly allocated memory to hold one element of type TYPE. */
113 #define talloc(type) ((type *) malloc (sizeof (type)))
115 /* Create a mode_change entry with the specified `=ddd'-style
116 mode change operation, where NEW_MODE is `ddd'. Return the
117 new entry, or NULL upon failure. */
119 static struct mode_change *
120 make_node_op_equals (mode_t new_mode)
122 struct mode_change *p;
123 p = talloc (struct mode_change);
130 p->affected = CHMOD_MODE_BITS; /* Affect all permissions. */
134 /* Append entry E to the end of the link list with the specified
138 mode_append_entry (struct mode_change **head,
139 struct mode_change **tail,
140 struct mode_change *e)
151 /* Return a linked list of file mode change operations created from
152 MODE_STRING, an ASCII string that contains either an octal number
153 specifying an absolute mode, or symbolic mode change operations with
155 [ugoa...][[+-=][rwxXstugo...]...][,...]
156 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
157 should not affect bits set in the umask when no users are given.
158 Operators not selected in MASKED_OPS ignore the umask.
160 Return MODE_INVALID if `mode_string' does not contain a valid
161 representation of file mode change operations;
162 return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
165 mode_compile (const char *mode_string, unsigned int masked_ops)
167 struct mode_change *head; /* First element of the linked list. */
168 struct mode_change *tail; /* An element of the linked list. */
169 unsigned long octal_value; /* The mode value, if octal. */
170 mode_t umask_value; /* The umask value (surprise). */
177 if (xstrtoul (mode_string, NULL, 8, &octal_value, "") == LONGINT_OK)
179 struct mode_change *p;
181 if (octal_value != (octal_value & ALLM))
184 /* Help the compiler optimize the usual case where mode_t uses
185 the traditional octal representation. */
186 mode = ((S_ISUID == SUID && S_ISGID == SGID && S_ISVTX == SVTX
187 && S_IRUSR == RUSR && S_IWUSR == WUSR && S_IXUSR == XUSR
188 && S_IRGRP == RGRP && S_IWGRP == WGRP && S_IXGRP == XGRP
189 && S_IROTH == ROTH && S_IWOTH == WOTH && S_IXOTH == XOTH)
191 : (mode_t) ((octal_value & SUID ? S_ISUID : 0)
192 | (octal_value & SGID ? S_ISGID : 0)
193 | (octal_value & SVTX ? S_ISVTX : 0)
194 | (octal_value & RUSR ? S_IRUSR : 0)
195 | (octal_value & WUSR ? S_IWUSR : 0)
196 | (octal_value & XUSR ? S_IXUSR : 0)
197 | (octal_value & RGRP ? S_IRGRP : 0)
198 | (octal_value & WGRP ? S_IWGRP : 0)
199 | (octal_value & XGRP ? S_IXGRP : 0)
200 | (octal_value & ROTH ? S_IROTH : 0)
201 | (octal_value & WOTH ? S_IWOTH : 0)
202 | (octal_value & XOTH ? S_IXOTH : 0)));
204 p = make_node_op_equals (mode);
206 return MODE_MEMORY_EXHAUSTED;
207 mode_append_entry (&head, &tail, p);
211 umask_value = umask (0);
212 umask (umask_value); /* Restore the old value. */
215 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
218 /* Which bits in the mode are operated on. */
219 mode_t affected_bits = 0;
220 /* `affected_bits' modified by umask. */
221 mode_t affected_masked;
222 /* Operators to actually use umask on. */
223 unsigned ops_to_mask = 0;
229 /* Turn on all the bits in `affected_bits' for each group given. */
230 for (++mode_string;; ++mode_string)
231 switch (*mode_string)
234 affected_bits |= S_ISUID | S_IRWXU;
237 affected_bits |= S_ISGID | S_IRWXG;
240 affected_bits |= S_ISVTX | S_IRWXO;
243 affected_bits |= CHMOD_MODE_BITS;
246 goto no_more_affected;
250 /* If none specified, affect all bits, except perhaps those
257 affected_bits = CHMOD_MODE_BITS;
258 ops_to_mask = masked_ops;
261 while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
263 struct mode_change *change = talloc (struct mode_change);
267 return MODE_MEMORY_EXHAUSTED;
271 change->op = *mode_string; /* One of "=+-". */
272 affected_masked = affected_bits;
274 /* Per the Single Unix Spec, if `who' is not specified and the
275 `=' operator is used, then clear all the bits first. */
276 if (!who_specified_p &&
277 ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS : 0))
279 struct mode_change *p = make_node_op_equals (0);
281 return MODE_MEMORY_EXHAUSTED;
282 mode_append_entry (&head, &tail, p);
285 if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
286 : *mode_string == '+' ? MODE_MASK_PLUS
288 affected_masked &= ~umask_value;
289 change->affected = affected_masked;
293 /* Add the element to the tail of the list, so the operations
294 are performed in the correct order. */
295 mode_append_entry (&head, &tail, change);
297 /* Set `value' according to the bits set in `affected_masked'. */
298 for (++mode_string;; ++mode_string)
299 switch (*mode_string)
302 change->value |= ((S_IRUSR | S_IRGRP | S_IROTH)
306 change->value |= ((S_IWUSR | S_IWGRP | S_IWOTH)
310 change->flags |= MODE_X_IF_ANY_X;
313 change->value |= ((S_IXUSR | S_IXGRP | S_IXOTH)
317 /* Set the setuid/gid bits if `u' or `g' is selected. */
318 change->value |= (S_ISUID | S_ISGID) & affected_masked;
321 /* Set the "save text image" bit if `o' is selected. */
322 change->value |= S_ISVTX & affected_masked;
325 /* Set the affected bits to the value of the `u' bits
329 change->value = S_IRWXU;
330 change->flags |= MODE_COPY_EXISTING;
333 /* Set the affected bits to the value of the `g' bits
337 change->value = S_IRWXG;
338 change->flags |= MODE_COPY_EXISTING;
341 /* Set the affected bits to the value of the `o' bits
345 change->value = S_IRWXO;
346 change->flags |= MODE_COPY_EXISTING;
353 } while (*mode_string == ',');
354 if (*mode_string == 0)
361 /* Return a file mode change operation that sets permissions to match those
362 of REF_FILE. Return MODE_BAD_REFERENCE if REF_FILE can't be accessed. */
365 mode_create_from_ref (const char *ref_file)
367 struct mode_change *change; /* the only change element */
368 struct stat ref_stats;
370 if (stat (ref_file, &ref_stats))
371 return MODE_BAD_REFERENCE;
373 change = talloc (struct mode_change);
376 return MODE_MEMORY_EXHAUSTED;
380 change->affected = CHMOD_MODE_BITS;
381 change->value = ref_stats.st_mode;
387 /* Return file mode OLDMODE, adjusted as indicated by the list of change
388 operations CHANGES. If OLDMODE is a directory, the type `X'
389 change affects it even if no execute bits were set in OLDMODE.
390 The returned value has the S_IFMT bits cleared. */
393 mode_adjust (mode_t oldmode, const struct mode_change *changes)
395 mode_t newmode; /* The adjusted mode and one operand. */
396 mode_t value; /* The other operand. */
398 newmode = oldmode & CHMOD_MODE_BITS;
400 for (; changes; changes = changes->next)
402 if (changes->flags & MODE_COPY_EXISTING)
404 /* Isolate in `value' the bits in `newmode' to copy, given in
405 the mask `changes->value'. */
406 value = newmode & changes->value;
408 if (changes->value & S_IRWXU)
409 /* Copy `u' permissions onto `g' and `o'. */
410 value |= ( (value & S_IRUSR ? S_IRGRP | S_IROTH : 0)
411 | (value & S_IWUSR ? S_IWGRP | S_IWOTH : 0)
412 | (value & S_IXUSR ? S_IXGRP | S_IXOTH : 0));
413 else if (changes->value & S_IRWXG)
414 /* Copy `g' permissions onto `u' and `o'. */
415 value |= ( (value & S_IRGRP ? S_IRUSR | S_IROTH : 0)
416 | (value & S_IWGRP ? S_IWUSR | S_IWOTH : 0)
417 | (value & S_IXGRP ? S_IXUSR | S_IXOTH : 0));
419 /* Copy `o' permissions onto `u' and `g'. */
420 value |= ( (value & S_IROTH ? S_IRUSR | S_IRGRP : 0)
421 | (value & S_IWOTH ? S_IWUSR | S_IWGRP : 0)
422 | (value & S_IXOTH ? S_IXUSR | S_IXGRP : 0));
424 /* In order to change only `u', `g', or `o' permissions,
425 or some combination thereof, clear unselected bits.
426 This cannot be done in mode_compile because the value
427 to which the `changes->affected' mask is applied depends
428 on the old mode of each file. */
429 value &= changes->affected;
433 value = changes->value;
434 /* If `X', do not affect the execute bits if the file is not a
435 directory and no execute bits are already set. */
436 if ((changes->flags & MODE_X_IF_ANY_X)
437 && !S_ISDIR (oldmode)
438 && (newmode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
439 /* Clear the execute bits. */
440 value &= ~ (S_IXUSR | S_IXGRP | S_IXOTH);
446 /* Preserve the previous values in `newmode' of bits that are
447 not affected by this change operation. */
448 newmode = (newmode & ~changes->affected) | value;
461 /* Free the memory used by the list of file mode change operations
465 mode_free (register struct mode_change *changes)
467 register struct mode_change *next;
471 next = changes->next;