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