Merge from coreutils.
[gnulib.git] / lib / modechange.c
1 /* modechange.c -- file mode manipulation
2
3    Copyright (C) 1989, 1990, 1997, 1998, 1999, 2001, 2003, 2004 Free
4    Software Foundation, Inc.
5
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)
9    any later version.
10
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.
15
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.  */
19
20 /* Written by David MacKenzie <djm@ai.mit.edu> */
21
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
27    performance gain. */
28
29 #if HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include "modechange.h"
34 #include <sys/stat.h>
35 #include "xstrtol.h"
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <stdlib.h>
39
40 #if STAT_MACROS_BROKEN
41 # undef S_ISDIR
42 #endif
43
44 #if !defined(S_ISDIR) && defined(S_IFDIR)
45 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
46 #endif
47
48 /* The traditional octal values corresponding to each mode bit.  */
49 #define SUID 04000
50 #define SGID 02000
51 #define SVTX 01000
52 #define RUSR 00400
53 #define WUSR 00200
54 #define XUSR 00100
55 #define RGRP 00040
56 #define WGRP 00020
57 #define XGRP 00010
58 #define ROTH 00004
59 #define WOTH 00002
60 #define XOTH 00001
61 #define ALLM 07777 /* all octal mode bits */
62
63 #ifndef S_ISUID
64 # define S_ISUID SUID
65 #endif
66 #ifndef S_ISGID
67 # define S_ISGID SGID
68 #endif
69 #ifndef S_ISVTX
70 # define S_ISVTX SVTX
71 #endif
72 #ifndef S_IRUSR
73 # define S_IRUSR RUSR
74 #endif
75 #ifndef S_IWUSR
76 # define S_IWUSR WUSR
77 #endif
78 #ifndef S_IXUSR
79 # define S_IXUSR XUSR
80 #endif
81 #ifndef S_IRGRP
82 # define S_IRGRP RGRP
83 #endif
84 #ifndef S_IWGRP
85 # define S_IWGRP WGRP
86 #endif
87 #ifndef S_IXGRP
88 # define S_IXGRP XGRP
89 #endif
90 #ifndef S_IROTH
91 # define S_IROTH ROTH
92 #endif
93 #ifndef S_IWOTH
94 # define S_IWOTH WOTH
95 #endif
96 #ifndef S_IXOTH
97 # define S_IXOTH XOTH
98 #endif
99 #ifndef S_IRWXU
100 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
101 #endif
102 #ifndef S_IRWXG
103 # define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
104 #endif
105 #ifndef S_IRWXO
106 # define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
107 #endif
108
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)
112
113 /* Return newly allocated memory to hold one element of type TYPE. */
114 #define talloc(type) ((type *) malloc (sizeof (type)))
115
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.  */
119
120 static struct mode_change *
121 make_node_op_equals (mode_t new_mode)
122 {
123   struct mode_change *p;
124   p = talloc (struct mode_change);
125   if (p == NULL)
126     return p;
127   p->next = NULL;
128   p->op = '=';
129   p->flags = 0;
130   p->value = new_mode;
131   p->affected = CHMOD_MODE_BITS;        /* Affect all permissions. */
132   return p;
133 }
134
135 /* Append entry E to the end of the link list with the specified
136    HEAD and TAIL.  */
137
138 static void
139 mode_append_entry (struct mode_change **head,
140                    struct mode_change **tail,
141                    struct mode_change *e)
142 {
143   if (*head == NULL)
144     *head = *tail = e;
145   else
146     {
147       (*tail)->next = e;
148       *tail = e;
149     }
150 }
151
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
155    the form:
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.
160
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. */
164
165 struct mode_change *
166 mode_compile (const char *mode_string, unsigned int masked_ops)
167 {
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). */
172
173   head = NULL;
174 #ifdef lint
175   tail = NULL;
176 #endif
177
178   if (xstrtoul (mode_string, NULL, 8, &octal_value, "") == LONGINT_OK)
179     {
180       struct mode_change *p;
181       mode_t mode;
182       if (octal_value != (octal_value & ALLM))
183         return MODE_INVALID;
184
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)
191               ? octal_value
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)));
204
205       p = make_node_op_equals (mode);
206       if (p == NULL)
207         return MODE_MEMORY_EXHAUSTED;
208       mode_append_entry (&head, &tail, p);
209       return head;
210     }
211
212   umask_value = umask (0);
213   umask (umask_value);          /* Restore the old value. */
214   --mode_string;
215
216   /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
217   do
218     {
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;
225
226       bool who_specified_p;
227
228       /* Turn on all the bits in `affected_bits' for each group given. */
229       for (++mode_string;; ++mode_string)
230         switch (*mode_string)
231           {
232           case 'u':
233             affected_bits |= S_ISUID | S_IRWXU;
234             break;
235           case 'g':
236             affected_bits |= S_ISGID | S_IRWXG;
237             break;
238           case 'o':
239             affected_bits |= S_ISVTX | S_IRWXO;
240             break;
241           case 'a':
242             affected_bits |= CHMOD_MODE_BITS;
243             break;
244           default:
245             goto no_more_affected;
246           }
247
248     no_more_affected:
249       /* If none specified, affect all bits, except perhaps those
250          set in the umask. */
251       if (affected_bits)
252         who_specified_p = true;
253       else
254         {
255           who_specified_p = false;
256           affected_bits = CHMOD_MODE_BITS;
257           ops_to_mask = masked_ops;
258         }
259
260       while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
261         {
262           struct mode_change *change = talloc (struct mode_change);
263           if (change == NULL)
264             {
265               mode_free (head);
266               return MODE_MEMORY_EXHAUSTED;
267             }
268
269           change->next = NULL;
270           change->op = *mode_string;    /* One of "=+-". */
271           affected_masked = affected_bits;
272
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))
277             {
278               struct mode_change *p = make_node_op_equals (0);
279               if (p == NULL)
280                 return MODE_MEMORY_EXHAUSTED;
281               mode_append_entry (&head, &tail, p);
282             }
283
284           if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
285                              : *mode_string == '+' ? MODE_MASK_PLUS
286                              : MODE_MASK_MINUS))
287             affected_masked &= ~umask_value;
288           change->affected = affected_masked;
289           change->value = 0;
290           change->flags = 0;
291
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);
295
296           /* Set `value' according to the bits set in `affected_masked'. */
297           for (++mode_string;; ++mode_string)
298             switch (*mode_string)
299               {
300               case 'r':
301                 change->value |= ((S_IRUSR | S_IRGRP | S_IROTH)
302                                   & affected_masked);
303                 break;
304               case 'w':
305                 change->value |= ((S_IWUSR | S_IWGRP | S_IWOTH)
306                                   & affected_masked);
307                 break;
308               case 'X':
309                 change->flags |= MODE_X_IF_ANY_X;
310                 /* Fall through. */
311               case 'x':
312                 change->value |= ((S_IXUSR | S_IXGRP | S_IXOTH)
313                                   & affected_masked);
314                 break;
315               case 's':
316                 /* Set the setuid/gid bits if `u' or `g' is selected. */
317                 change->value |= (S_ISUID | S_ISGID) & affected_masked;
318                 break;
319               case 't':
320                 /* Set the "save text image" bit if `o' is selected. */
321                 change->value |= S_ISVTX & affected_masked;
322                 break;
323               case 'u':
324                 /* Set the affected bits to the value of the `u' bits
325                    on the same file.  */
326                 if (change->value)
327                   goto invalid;
328                 change->value = S_IRWXU;
329                 change->flags |= MODE_COPY_EXISTING;
330                 break;
331               case 'g':
332                 /* Set the affected bits to the value of the `g' bits
333                    on the same file.  */
334                 if (change->value)
335                   goto invalid;
336                 change->value = S_IRWXG;
337                 change->flags |= MODE_COPY_EXISTING;
338                 break;
339               case 'o':
340                 /* Set the affected bits to the value of the `o' bits
341                    on the same file.  */
342                 if (change->value)
343                   goto invalid;
344                 change->value = S_IRWXO;
345                 change->flags |= MODE_COPY_EXISTING;
346                 break;
347               default:
348                 goto no_more_values;
349               }
350         no_more_values:;
351         }
352   } while (*mode_string == ',');
353   if (*mode_string == 0)
354     return head;
355 invalid:
356   mode_free (head);
357   return MODE_INVALID;
358 }
359
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.  */
362
363 struct mode_change *
364 mode_create_from_ref (const char *ref_file)
365 {
366   struct mode_change *change;   /* the only change element */
367   struct stat ref_stats;
368
369   if (stat (ref_file, &ref_stats))
370     return MODE_BAD_REFERENCE;
371
372   change = talloc (struct mode_change);
373
374   if (change == NULL)
375     return MODE_MEMORY_EXHAUSTED;
376
377   change->op = '=';
378   change->flags = 0;
379   change->affected = CHMOD_MODE_BITS;
380   change->value = ref_stats.st_mode;
381   change->next = NULL;
382
383   return change;
384 }
385
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. */
390
391 mode_t
392 mode_adjust (mode_t oldmode, const struct mode_change *changes)
393 {
394   mode_t newmode;       /* The adjusted mode and one operand. */
395   mode_t value;         /* The other operand. */
396
397   newmode = oldmode & CHMOD_MODE_BITS;
398
399   for (; changes; changes = changes->next)
400     {
401       if (changes->flags & MODE_COPY_EXISTING)
402         {
403           /* Isolate in `value' the bits in `newmode' to copy, given in
404              the mask `changes->value'. */
405           value = newmode & changes->value;
406
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));
417           else
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));
422
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;
429         }
430       else
431         {
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);
440         }
441
442       switch (changes->op)
443         {
444         case '=':
445           /* Preserve the previous values in `newmode' of bits that are
446              not affected by this change operation. */
447           newmode = (newmode & ~changes->affected) | value;
448           break;
449         case '+':
450           newmode |= value;
451           break;
452         case '-':
453           newmode &= ~value;
454           break;
455         }
456     }
457   return newmode;
458 }
459
460 /* Free the memory used by the list of file mode change operations
461    CHANGES. */
462
463 void
464 mode_free (register struct mode_change *changes)
465 {
466   register struct mode_change *next;
467
468   while (changes)
469     {
470       next = changes->next;
471       free (changes);
472       changes = next;
473     }
474 }