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