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. */
215 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
216 for (;; mode_string++)
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 int ops_to_mask = 0;
225 bool who_specified_p;
227 /* Turn on all the bits in `affected_bits' for each group given. */
228 for (;; mode_string++)
229 switch (*mode_string)
232 affected_bits |= S_ISUID | S_IRWXU;
235 affected_bits |= S_ISGID | S_IRWXG;
238 affected_bits |= S_ISVTX | S_IRWXO;
241 affected_bits |= CHMOD_MODE_BITS;
244 goto no_more_affected;
248 /* If none specified, affect all bits, except perhaps those
251 who_specified_p = true;
254 who_specified_p = false;
255 affected_bits = CHMOD_MODE_BITS;
256 ops_to_mask = masked_ops;
259 while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
261 struct mode_change *change = talloc (struct mode_change);
265 return MODE_MEMORY_EXHAUSTED;
269 change->op = *mode_string; /* One of "=+-". */
270 affected_masked = affected_bits;
272 /* Per the Single Unix Spec, if `who' is not specified and the
273 `=' operator is used, then clear all the bits first. */
274 if (!who_specified_p &&
275 ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS : 0))
277 struct mode_change *p = make_node_op_equals (0);
279 return MODE_MEMORY_EXHAUSTED;
280 mode_append_entry (&head, &tail, p);
283 if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
284 : *mode_string == '+' ? MODE_MASK_PLUS
286 affected_masked &= ~umask_value;
287 change->affected = affected_masked;
291 /* Add the element to the tail of the list, so the operations
292 are performed in the correct order. */
293 mode_append_entry (&head, &tail, change);
295 /* Set `value' according to the bits set in `affected_masked'. */
296 for (++mode_string;; ++mode_string)
297 switch (*mode_string)
300 change->value |= ((S_IRUSR | S_IRGRP | S_IROTH)
304 change->value |= ((S_IWUSR | S_IWGRP | S_IWOTH)
308 change->flags |= MODE_X_IF_ANY_X;
311 change->value |= ((S_IXUSR | S_IXGRP | S_IXOTH)
315 /* Set the setuid/gid bits if `u' or `g' is selected. */
316 change->value |= (S_ISUID | S_ISGID) & affected_masked;
319 /* Set the "save text image" bit if `o' is selected. */
320 change->value |= S_ISVTX & affected_masked;
323 /* Set the affected bits to the value of the `u' bits
327 change->value = S_IRWXU;
328 change->flags |= MODE_COPY_EXISTING;
331 /* Set the affected bits to the value of the `g' bits
335 change->value = S_IRWXG;
336 change->flags |= MODE_COPY_EXISTING;
339 /* Set the affected bits to the value of the `o' bits
343 change->value = S_IRWXO;
344 change->flags |= MODE_COPY_EXISTING;
352 if (*mode_string != ',')
356 if (*mode_string == 0)
363 /* Return a file mode change operation that sets permissions to match those
364 of REF_FILE. Return MODE_BAD_REFERENCE if REF_FILE can't be accessed. */
367 mode_create_from_ref (const char *ref_file)
369 struct mode_change *change; /* the only change element */
370 struct stat ref_stats;
372 if (stat (ref_file, &ref_stats))
373 return MODE_BAD_REFERENCE;
375 change = talloc (struct mode_change);
378 return MODE_MEMORY_EXHAUSTED;
382 change->affected = CHMOD_MODE_BITS;
383 change->value = ref_stats.st_mode;
389 /* Return file mode OLDMODE, adjusted as indicated by the list of change
390 operations CHANGES. If OLDMODE is a directory, the type `X'
391 change affects it even if no execute bits were set in OLDMODE.
392 The returned value has the S_IFMT bits cleared. */
395 mode_adjust (mode_t oldmode, const struct mode_change *changes)
397 mode_t newmode; /* The adjusted mode and one operand. */
398 mode_t value; /* The other operand. */
400 newmode = oldmode & CHMOD_MODE_BITS;
402 for (; changes; changes = changes->next)
404 if (changes->flags & MODE_COPY_EXISTING)
406 /* Isolate in `value' the bits in `newmode' to copy, given in
407 the mask `changes->value'. */
408 value = newmode & changes->value;
410 if (changes->value & S_IRWXU)
411 /* Copy `u' permissions onto `g' and `o'. */
412 value |= ( (value & S_IRUSR ? S_IRGRP | S_IROTH : 0)
413 | (value & S_IWUSR ? S_IWGRP | S_IWOTH : 0)
414 | (value & S_IXUSR ? S_IXGRP | S_IXOTH : 0));
415 else if (changes->value & S_IRWXG)
416 /* Copy `g' permissions onto `u' and `o'. */
417 value |= ( (value & S_IRGRP ? S_IRUSR | S_IROTH : 0)
418 | (value & S_IWGRP ? S_IWUSR | S_IWOTH : 0)
419 | (value & S_IXGRP ? S_IXUSR | S_IXOTH : 0));
421 /* Copy `o' permissions onto `u' and `g'. */
422 value |= ( (value & S_IROTH ? S_IRUSR | S_IRGRP : 0)
423 | (value & S_IWOTH ? S_IWUSR | S_IWGRP : 0)
424 | (value & S_IXOTH ? S_IXUSR | S_IXGRP : 0));
426 /* In order to change only `u', `g', or `o' permissions,
427 or some combination thereof, clear unselected bits.
428 This cannot be done in mode_compile because the value
429 to which the `changes->affected' mask is applied depends
430 on the old mode of each file. */
431 value &= changes->affected;
435 value = changes->value;
436 /* If `X', do not affect the execute bits if the file is not a
437 directory and no execute bits are already set. */
438 if ((changes->flags & MODE_X_IF_ANY_X)
439 && !S_ISDIR (oldmode)
440 && (newmode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
441 /* Clear the execute bits. */
442 value &= ~ (S_IXUSR | S_IXGRP | S_IXOTH);
448 /* Preserve the previous values in `newmode' of bits that are
449 not affected by this change operation. */
450 newmode = (newmode & ~changes->affected) | value;
463 /* Free the memory used by the list of file mode change operations
467 mode_free (register struct mode_change *changes)
469 register struct mode_change *next;
473 next = changes->next;