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