file_cmds-116.11.tar.gz
[apple/file_cmds.git] / chmod / chmod.c
1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char const copyright[] =
36 "@(#) Copyright (c) 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __RCSID("$FreeBSD: src/bin/chmod/chmod.c,v 1.27 2002/08/04 05:29:13 obrien Exp $");
47
48 #include <sys/types.h>
49 #include <sys/stat.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <fts.h>
54 #include <limits.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #ifdef __APPLE__
61 #include "chmod_acl.h"
62
63 #endif /*__APPLE__*/
64
65 int fflag = 0;
66
67 int main(int, char *[]);
68 void usage(void);
69
70 int
71 main(int argc, char *argv[])
72 {
73 FTS *ftsp = NULL;
74 FTSENT *p = NULL;
75 mode_t *set = NULL;
76 long val = 0;
77 int oct = 0;
78 int Hflag, Lflag, Rflag, ch, fts_options, hflag, rval;
79 int vflag;
80 char *ep, *mode;
81 mode_t newmode, omode;
82 #ifdef __APPLE__
83 unsigned int acloptflags = 0;
84 int aclpos = -1, inheritance_level = 0;
85 int index = 0, acloptlen = 0, ace_arg_not_required = 0;
86 acl_t acl_input = NULL;
87 #endif /* __APPLE__*/
88 int (*change_mode)(const char *, mode_t);
89
90 set = NULL;
91 omode = 0;
92 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
93 #ifndef __APPLE__
94 while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
95 #else
96 while ((ch = getopt(argc, argv, "ACEHILNPRVXafginorstuvwx")) != -1)
97 #endif
98 switch (ch) {
99 case 'H':
100 Hflag = 1;
101 Lflag = 0;
102 break;
103 case 'L':
104 Lflag = 1;
105 Hflag = 0;
106 break;
107 case 'P':
108 Hflag = Lflag = 0;
109 break;
110 case 'R':
111 Rflag = 1;
112 break;
113 case 'f':
114 fflag = 1;
115 break;
116 #ifndef __APPLE__
117 case 'h':
118 /*
119 * In System V (and probably POSIX.2) the -h option
120 * causes chmod to change the mode of the symbolic
121 * link. 4.4BSD's symbolic links didn't have modes,
122 * so it was an undocumented noop. In FreeBSD 3.0,
123 * lchmod(2) is introduced and this option does real
124 * work.
125 */
126 hflag = 1;
127 break;
128 #else
129 case 'a':
130 if (argv[optind - 1][0] == '-' &&
131 argv[optind - 1][1] == ch)
132 --optind;
133 goto done;
134 case 'E':
135 acloptflags |= ACL_FLAG | ACL_FROM_STDIN;
136 goto done;
137 case 'C':
138 acloptflags |= ACL_FLAG | ACL_CHECK_CANONICITY;
139 ace_arg_not_required = 1;
140 goto done;
141 case 'i':
142 acloptflags |= ACL_FLAG | ACL_REMOVE_INHERIT_FLAG;
143 ace_arg_not_required = 1;
144 goto done;
145 case 'I':
146 acloptflags |= ACL_FLAG | ACL_REMOVE_INHERITED_ENTRIES;
147 ace_arg_not_required = 1;
148 goto done;
149 case 'n':
150 acloptflags |= ACL_FLAG | ACL_NO_TRANSLATE;
151 break;
152 case 'N':
153 acloptflags |= ACL_FLAG | ACL_CLEAR_FLAG;
154 ace_arg_not_required = 1;
155 goto done;
156 case 'V':
157 acloptflags |= ACL_FLAG | ACL_INVOKE_EDITOR;
158 ace_arg_not_required = 1;
159 goto done;
160 #endif /* __APPLE__ */
161 /*
162 * XXX
163 * "-[rwx]" are valid mode commands. If they are the entire
164 * argument, getopt has moved past them, so decrement optind.
165 * Regardless, we're done argument processing.
166 */
167 case 'g': case 'o': case 'r': case 's':
168 case 't': case 'u': case 'w': case 'X': case 'x':
169 if (argv[optind - 1][0] == '-' &&
170 argv[optind - 1][1] == ch &&
171 argv[optind - 1][2] == '\0')
172 --optind;
173 goto done;
174 case 'v':
175 vflag++;
176 break;
177 case '?':
178 default:
179 usage();
180 }
181 done: argv += optind;
182 argc -= optind;
183
184 #ifdef __APPLE__
185 if (argc < ((acloptflags & ACL_FLAG) ? 1 : 2))
186 usage();
187 #else
188 if (argc < 2)
189 usage();
190 #endif
191
192 #ifdef __APPLE__
193 if (!(acloptflags & ACL_FLAG) && ((acloptlen = strlen(argv[0])) > 1) && (argv[0][1] == 'a')) {
194 acloptflags |= ACL_FLAG;
195 switch (argv[0][0]) {
196 case '+':
197 acloptflags |= ACL_SET_FLAG;
198 break;
199 case '-':
200 acloptflags |= ACL_DELETE_FLAG;
201 break;
202 case '=':
203 acloptflags |= ACL_REWRITE_FLAG;
204 break;
205 default:
206 acloptflags &= ~ACL_FLAG;
207 goto apnoacl;
208 }
209
210 if (argc < 3)
211 usage();
212
213 if (acloptlen > 2) {
214 for (index = 2; index < acloptlen; index++) {
215 switch (argv[0][index]) {
216 case '#':
217 acloptflags |= ACL_ORDER_FLAG;
218
219 if (argc < ((acloptflags & ACL_DELETE_FLAG)
220 ? 3 : 4))
221 usage();
222 argv++;
223 argc--;
224 errno = 0;
225 aclpos = strtol(argv[0], &ep, 0);
226
227 if (aclpos > ACL_MAX_ENTRIES
228 || aclpos < 0)
229 errno = ERANGE;
230 if (errno || *ep)
231 err(1, "Invalid ACL entry number: %s", aclpos);
232 if (acloptflags & ACL_DELETE_FLAG)
233 ace_arg_not_required = 1;
234
235 goto apdone;
236 case 'i':
237 acloptflags |= ACL_INHERIT_FLAG;
238 /* The +aii.. syntax to specify
239 * inheritance level is rather unwieldy,
240 * find an alternative.
241 */
242 inheritance_level++;
243 if (inheritance_level > 1)
244 warn("Inheritance across more than one generation is not currently supported");
245 if (inheritance_level >= MAX_INHERITANCE_LEVEL)
246 goto apdone;
247 break;
248 default:
249 errno = EINVAL;
250 usage();
251 }
252 }
253 }
254 apdone:
255 argv++;
256 argc--;
257 }
258 apnoacl:
259 #endif /*__APPLE__*/
260
261 if (Rflag) {
262 fts_options = FTS_PHYSICAL;
263 if (hflag)
264 errx(1,
265 "the -R and -h options may not be specified together.");
266 if (Hflag)
267 fts_options |= FTS_COMFOLLOW;
268 if (Lflag) {
269 fts_options &= ~FTS_PHYSICAL;
270 fts_options |= FTS_LOGICAL;
271 }
272 } else
273 fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
274
275 #ifndef __APPLE__
276 if (hflag)
277 change_mode = lchmod;
278 else
279 change_mode = chmod;
280 #else
281 change_mode = chmod;
282 #endif /* __APPLE__ */
283 #ifdef __APPLE__
284 if (acloptflags & ACL_FROM_STDIN) {
285 int readval = 0, readtotal = 0;
286
287 mode = (char *) malloc(MAX_ACL_TEXT_SIZE);
288
289 if (mode == NULL)
290 err(1, "Unable to allocate mode string");
291 /* Read the ACEs from STDIN */
292 do {
293 readtotal += readval;
294 readval = read(STDIN_FILENO, mode + readtotal,
295 MAX_ACL_TEXT_SIZE);
296 } while ((readval > 0) && (readtotal <= MAX_ACL_TEXT_SIZE));
297
298 if (0 == readtotal)
299 err(1, "-E specified, but read from STDIN failed");
300 else
301 mode[readtotal - 1] = '\0';
302 --argv;
303 }
304 else
305 #endif /* __APPLE */
306 mode = *argv;
307
308 #ifdef __APPLE__
309 if ((acloptflags & ACL_FLAG)) {
310
311 /* Are we deleting by entry number, verifying
312 * canonicity or performing some other operation that
313 * does not require an input entry? If so, there's no
314 * entry to convert.
315 */
316 if (ace_arg_not_required) {
317 --argv;
318 }
319 else {
320 /* Parse the text into an ACL*/
321 acl_input = parse_acl_entries(mode);
322 if (acl_input == NULL) {
323 errno = EINVAL;
324 err(1, "Invalid ACL specification: %s", mode);
325 }
326 }
327 }
328 else {
329 #endif /* __APPLE__*/
330 if (*mode >= '0' && *mode <= '7') {
331 errno = 0;
332 val = strtol(mode, &ep, 8);
333 if (val > USHRT_MAX || val < 0)
334 errno = ERANGE;
335 if (errno)
336 err(1, "Invalid file mode: %s", mode);
337 if (*ep)
338 errx(1, "Invalid file mode: %s", mode);
339 omode = (mode_t)val;
340 oct = 1;
341 } else {
342 if ((set = setmode(mode)) == NULL)
343 errx(1, "Invalid file mode: %s", mode);
344 oct = 0;
345 }
346 #ifdef __APPLE__
347 }
348 #endif /* __APPLE__*/
349 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
350 err(1, "fts_open");
351 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
352 switch (p->fts_info) {
353 case FTS_D: /* Change it at FTS_DP. */
354 if (!Rflag)
355 fts_set(ftsp, p, FTS_SKIP);
356 continue;
357 case FTS_DNR: /* Warn, chmod, continue. */
358 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
359 rval = 1;
360 break;
361 case FTS_ERR: /* Warn, continue. */
362 case FTS_NS:
363 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
364 rval = 1;
365 continue;
366 case FTS_SL: /* Ignore. */
367 case FTS_SLNONE:
368 /*
369 * The only symlinks that end up here are ones that
370 * don't point to anything and ones that we found
371 * doing a physical walk.
372 */
373 if (!hflag)
374 continue;
375 /* else */
376 /* FALLTHROUGH */
377 default:
378 break;
379 }
380 #ifdef __APPLE__
381 /* If an ACL manipulation option was specified, manipulate */
382 if (acloptflags & ACL_FLAG) {
383 if (0 != modify_file_acl(acloptflags, p->fts_accpath, acl_input, aclpos, inheritance_level))
384 rval = 1;
385 }
386 else {
387 #endif /* __APPLE__ */
388 newmode = oct ? omode : getmode(set, p->fts_statp->st_mode);
389 if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
390 continue;
391 if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {
392 warn("%s", p->fts_path);
393 rval = 1;
394 } else {
395 if (vflag) {
396 (void)printf("%s", p->fts_accpath);
397
398 if (vflag > 1) {
399 char m1[12], m2[12];
400
401 strmode(p->fts_statp->st_mode, m1);
402 strmode((p->fts_statp->st_mode &
403 S_IFMT) | newmode, m2);
404
405 (void)printf(": 0%o [%s] -> 0%o [%s]",
406 p->fts_statp->st_mode, m1,
407 (p->fts_statp->st_mode & S_IFMT) |
408 newmode, m2);
409 }
410 (void)printf("\n");
411 }
412
413 }
414 #ifdef __APPLE__
415 }
416 #endif /* __APPLE__*/
417 }
418 if (errno)
419 err(1, "fts_read");
420 #ifdef __APPLE__
421 if (acl_input)
422 acl_free(acl_input);
423 if (mode && (acloptflags & ACL_FROM_STDIN))
424 free(mode);
425
426 #endif /* __APPLE__ */
427 if (set)
428 free(set);
429 exit(rval);
430 }
431
432 void
433 usage(void)
434 {
435 #ifdef __APPLE__
436 (void)fprintf(stderr,
437 "usage: chmod [-fv] [-R [-H | -L | -P]] [-a | +a | =a [i][# [ n]]] mode|entry file ...\n");
438 #else
439 (void)fprintf(stderr,
440 "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
441 #endif /* __APPLE__ */
442 exit(1);
443 }