file_cmds-202.2.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, Pflag, 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 = Pflag = Rflag = fflag = hflag = vflag = 0;
93 #ifndef __APPLE__
94 while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
95 #else
96 while ((ch = getopt(argc, argv, "ACEHILNPRVXafghinorstuvwx")) != -1)
97 #endif
98 switch (ch) {
99 case 'H':
100 Hflag = 1;
101 Lflag = 0;
102 Pflag = 0;
103 break;
104 case 'L':
105 Lflag = 1;
106 Hflag = 0;
107 Pflag = 0;
108 break;
109 case 'P':
110 Hflag = Lflag = 0;
111 Pflag = 1;
112 break;
113 case 'R':
114 Rflag = 1;
115 break;
116 case 'f':
117 fflag = 1;
118 break;
119 case 'h':
120 /*
121 * In System V (and probably POSIX.2) the -h option
122 * causes chmod to change the mode of the symbolic
123 * link. 4.4BSD's symbolic links didn't have modes,
124 * so it was an undocumented noop. In FreeBSD 3.0,
125 * lchmod(2) is introduced and this option does real
126 * work.
127 */
128 hflag = 1;
129 break;
130 #ifdef __APPLE__
131 case 'a':
132 if (argv[optind - 1][0] == '-' &&
133 argv[optind - 1][1] == ch)
134 --optind;
135 goto done;
136 case 'A':
137 acloptflags |= ACL_FLAG | ACL_TO_STDOUT;
138 ace_arg_not_required = 1;
139 errx(1, "-A not implemented");
140 goto done;
141 case 'E':
142 acloptflags |= ACL_FLAG | ACL_FROM_STDIN;
143 goto done;
144 case 'C':
145 acloptflags |= ACL_FLAG | ACL_CHECK_CANONICITY;
146 ace_arg_not_required = 1;
147 goto done;
148 case 'i':
149 acloptflags |= ACL_FLAG | ACL_REMOVE_INHERIT_FLAG;
150 ace_arg_not_required = 1;
151 goto done;
152 case 'I':
153 acloptflags |= ACL_FLAG | ACL_REMOVE_INHERITED_ENTRIES;
154 ace_arg_not_required = 1;
155 goto done;
156 case 'n':
157 acloptflags |= ACL_FLAG | ACL_NO_TRANSLATE;
158 break;
159 case 'N':
160 acloptflags |= ACL_FLAG | ACL_CLEAR_FLAG;
161 ace_arg_not_required = 1;
162 goto done;
163 case 'V':
164 acloptflags |= ACL_FLAG | ACL_INVOKE_EDITOR;
165 ace_arg_not_required = 1;
166 errx(1, "-V not implemented");
167 goto done;
168 #endif /* __APPLE__ */
169 /*
170 * XXX
171 * "-[rwx]" are valid mode commands. If they are the entire
172 * argument, getopt has moved past them, so decrement optind.
173 * Regardless, we're done argument processing.
174 */
175 case 'g': case 'o': case 'r': case 's':
176 case 't': case 'u': case 'w': case 'X': case 'x':
177 if (argv[optind - 1][0] == '-' &&
178 argv[optind - 1][1] == ch &&
179 argv[optind - 1][2] == '\0')
180 --optind;
181 goto done;
182 case 'v':
183 vflag++;
184 break;
185 case '?':
186 default:
187 usage();
188 }
189 done: argv += optind;
190 argc -= optind;
191
192 #ifdef __APPLE__
193 if (argc < ((acloptflags & ACL_FLAG) ? 1 : 2))
194 usage();
195 if (!Rflag && (Hflag || Lflag || Pflag))
196 warnx("options -H, -L, -P only useful with -R");
197 #else /* !__APPLE__ */
198 if (argc < 2)
199 usage();
200 #endif /* __APPLE__ */
201
202 #ifdef __APPLE__
203 if (!(acloptflags & ACL_FLAG) && ((acloptlen = strlen(argv[0])) > 1) && (argv[0][1] == 'a')) {
204 acloptflags |= ACL_FLAG;
205 switch (argv[0][0]) {
206 case '+':
207 acloptflags |= ACL_SET_FLAG;
208 break;
209 case '-':
210 acloptflags |= ACL_DELETE_FLAG;
211 break;
212 case '=':
213 acloptflags |= ACL_REWRITE_FLAG;
214 break;
215 default:
216 acloptflags &= ~ACL_FLAG;
217 goto apnoacl;
218 }
219
220 if (argc < 3)
221 usage();
222
223 if (acloptlen > 2) {
224 for (index = 2; index < acloptlen; index++) {
225 switch (argv[0][index]) {
226 case '#':
227 acloptflags |= ACL_ORDER_FLAG;
228
229 if (argc < ((acloptflags & ACL_DELETE_FLAG)
230 ? 3 : 4))
231 usage();
232 argv++;
233 argc--;
234 errno = 0;
235 aclpos = strtol(argv[0], &ep, 0);
236
237 if (aclpos > ACL_MAX_ENTRIES
238 || aclpos < 0)
239 errno = ERANGE;
240 if (errno || *ep)
241 errx(1, "Invalid ACL entry number: %d", aclpos);
242 if (acloptflags & ACL_DELETE_FLAG)
243 ace_arg_not_required = 1;
244
245 goto apdone;
246 case 'i':
247 acloptflags |= ACL_INHERIT_FLAG;
248 /* The +aii.. syntax to specify
249 * inheritance level is rather unwieldy,
250 * find an alternative.
251 */
252 inheritance_level++;
253 if (inheritance_level > 1)
254 warnx("Inheritance across more than one generation is not currently supported");
255 if (inheritance_level >= MAX_INHERITANCE_LEVEL)
256 goto apdone;
257 break;
258 default:
259 errno = EINVAL;
260 usage();
261 }
262 }
263 }
264 apdone:
265 argv++;
266 argc--;
267 }
268 apnoacl:
269 #endif /*__APPLE__*/
270
271 if (Rflag) {
272 fts_options = FTS_PHYSICAL;
273 if (hflag)
274 errx(1,
275 "the -R and -h options may not be specified together.");
276 if (Hflag)
277 fts_options |= FTS_COMFOLLOW;
278 if (Lflag) {
279 fts_options &= ~FTS_PHYSICAL;
280 fts_options |= FTS_LOGICAL;
281 }
282 } else
283 fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
284
285 if (hflag)
286 change_mode = lchmod;
287 else
288 change_mode = chmod;
289 #ifdef __APPLE__
290 if (acloptflags & ACL_FROM_STDIN) {
291 int readval = 0, readtotal = 0;
292
293 mode = (char *) malloc(MAX_ACL_TEXT_SIZE);
294
295 if (mode == NULL)
296 err(1, "Unable to allocate mode string");
297 /* Read the ACEs from STDIN */
298 do {
299 readtotal += readval;
300 readval = read(STDIN_FILENO, mode + readtotal,
301 MAX_ACL_TEXT_SIZE);
302 } while ((readval > 0) && (readtotal <= MAX_ACL_TEXT_SIZE));
303
304 if (0 == readtotal)
305 errx(1, "-E specified, but read from STDIN failed");
306 else
307 mode[readtotal - 1] = '\0';
308 --argv;
309 }
310 else
311 #endif /* __APPLE */
312 mode = *argv;
313
314 #ifdef __APPLE__
315 if ((acloptflags & ACL_FLAG)) {
316
317 /* Are we deleting by entry number, verifying
318 * canonicity or performing some other operation that
319 * does not require an input entry? If so, there's no
320 * entry to convert.
321 */
322 if (ace_arg_not_required) {
323 --argv;
324 }
325 else {
326 /* Parse the text into an ACL*/
327 acl_input = parse_acl_entries(mode);
328 if (acl_input == NULL) {
329 errx(1, "Invalid ACL specification: %s", mode);
330 }
331 }
332 }
333 else {
334 #endif /* __APPLE__*/
335 if (*mode >= '0' && *mode <= '7') {
336 errno = 0;
337 val = strtol(mode, &ep, 8);
338 if (val > USHRT_MAX || val < 0)
339 errno = ERANGE;
340 if (errno)
341 err(1, "Invalid file mode: %s", mode);
342 if (*ep)
343 errx(1, "Invalid file mode: %s", mode);
344 omode = (mode_t)val;
345 oct = 1;
346 } else {
347 if ((set = setmode(mode)) == NULL)
348 errx(1, "Invalid file mode: %s", mode);
349 oct = 0;
350 }
351 #ifdef __APPLE__
352 }
353 #endif /* __APPLE__*/
354 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
355 err(1, "fts_open");
356 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
357 switch (p->fts_info) {
358 case FTS_D:
359 if (!Rflag)
360 (void)fts_set(ftsp, p, FTS_SKIP);
361 break;
362 case FTS_DNR: /* Warn, chmod, continue. */
363 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
364 rval = 1;
365 break;
366 case FTS_DP: /* Already changed at FTS_D. */
367 continue;
368 case FTS_NS:
369 if (acloptflags & ACL_FLAG) /* don't need stat for -N */
370 break;
371 case FTS_ERR: /* Warn, continue. */
372 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
373 rval = 1;
374 continue;
375 case FTS_SL: /* Ignore. */
376 case FTS_SLNONE:
377 /*
378 * The only symlinks that end up here are ones that
379 * don't point to anything and ones that we found
380 * doing a physical walk.
381 */
382 if (!hflag)
383 continue;
384 /* else */
385 /* FALLTHROUGH */
386 default:
387 break;
388 }
389 #ifdef __APPLE__
390 /* If an ACL manipulation option was specified, manipulate */
391 if (acloptflags & ACL_FLAG) {
392 if (0 != modify_file_acl(acloptflags, p->fts_accpath, acl_input, aclpos, inheritance_level))
393 rval = 1;
394 }
395 else {
396 #endif /* __APPLE__ */
397 newmode = oct ? omode : getmode(set, p->fts_statp->st_mode);
398 if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
399 continue;
400 if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {
401 warn("Unable to change file mode on %s", p->fts_path);
402 rval = 1;
403 } else {
404 if (vflag) {
405 (void)printf("%s", p->fts_accpath);
406
407 if (vflag > 1) {
408 char m1[12], m2[12];
409
410 strmode(p->fts_statp->st_mode, m1);
411 strmode((p->fts_statp->st_mode &
412 S_IFMT) | newmode, m2);
413
414 (void)printf(": 0%o [%s] -> 0%o [%s]",
415 p->fts_statp->st_mode, m1,
416 (p->fts_statp->st_mode & S_IFMT) |
417 newmode, m2);
418 }
419 (void)printf("\n");
420 }
421
422 }
423 #ifdef __APPLE__
424 }
425 #endif /* __APPLE__*/
426 }
427 if (errno)
428 err(1, "fts_read");
429 #ifdef __APPLE__
430 if (acl_input)
431 acl_free(acl_input);
432 if (mode && (acloptflags & ACL_FROM_STDIN))
433 free(mode);
434
435 #endif /* __APPLE__ */
436 if (set)
437 free(set);
438 exit(rval);
439 }
440
441 void
442 usage(void)
443 {
444 #ifdef __APPLE__
445 (void)fprintf(stderr,
446 "usage:\tchmod [-fhv] [-R [-H | -L | -P]] [-a | +a | =a [i][# [ n]]] mode|entry file ...\n"
447 "\tchmod [-fhv] [-R [-H | -L | -P]] [-E | -C | -N | -i | -I] file ...\n"); /* add -A and -V when implemented */
448 #else
449 (void)fprintf(stderr,
450 "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
451 #endif /* __APPLE__ */
452 exit(1);
453 }