]>
Commit | Line | Data |
---|---|---|
44a7a5ab A |
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 | ||
44a7a5ab | 34 | #ifndef lint |
6c780a1f | 35 | static char const copyright[] = |
44a7a5ab | 36 | "@(#) Copyright (c) 1989, 1993, 1994\n\ |
6c780a1f | 37 | The Regents of the University of California. All rights reserved.\n"; |
44a7a5ab A |
38 | #endif /* not lint */ |
39 | ||
40 | #ifndef lint | |
41 | #if 0 | |
42 | static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94"; | |
44a7a5ab A |
43 | #endif |
44 | #endif /* not lint */ | |
6c780a1f A |
45 | #include <sys/cdefs.h> |
46 | __RCSID("$FreeBSD: src/bin/chmod/chmod.c,v 1.27 2002/08/04 05:29:13 obrien Exp $"); | |
44a7a5ab A |
47 | |
48 | #include <sys/types.h> | |
49 | #include <sys/stat.h> | |
50 | ||
51 | #include <err.h> | |
52 | #include <errno.h> | |
53 | #include <fts.h> | |
6c780a1f | 54 | #include <limits.h> |
44a7a5ab A |
55 | #include <stdio.h> |
56 | #include <stdlib.h> | |
57 | #include <string.h> | |
58 | #include <unistd.h> | |
44a7a5ab | 59 | |
c59d3020 A |
60 | #ifdef __APPLE__ |
61 | #include "chmod_acl.h" | |
62 | ||
63 | #endif /*__APPLE__*/ | |
64 | ||
65 | int fflag = 0; | |
66 | ||
6c780a1f A |
67 | int main(int, char *[]); |
68 | void usage(void); | |
44a7a5ab A |
69 | |
70 | int | |
6c780a1f | 71 | main(int argc, char *argv[]) |
44a7a5ab | 72 | { |
c59d3020 A |
73 | FTS *ftsp = NULL; |
74 | FTSENT *p = NULL; | |
75 | mode_t *set = NULL; | |
76 | long val = 0; | |
77 | int oct = 0; | |
40bf83fe | 78 | int Hflag, Lflag, Pflag, Rflag, ch, fts_options, hflag, rval; |
6c780a1f | 79 | int vflag; |
44a7a5ab | 80 | char *ep, *mode; |
6c780a1f | 81 | mode_t newmode, omode; |
c59d3020 A |
82 | #ifdef __APPLE__ |
83 | unsigned int acloptflags = 0; | |
4d0bb651 A |
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; | |
c59d3020 A |
89 | acl_t acl_input = NULL; |
90 | #endif /* __APPLE__*/ | |
6c780a1f | 91 | int (*change_mode)(const char *, mode_t); |
44a7a5ab | 92 | |
6c780a1f A |
93 | set = NULL; |
94 | omode = 0; | |
40bf83fe | 95 | Hflag = Lflag = Pflag = Rflag = fflag = hflag = vflag = 0; |
f383e97b | 96 | #ifndef __APPLE__ |
6c780a1f | 97 | while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1) |
f383e97b | 98 | #else |
864a4b6e | 99 | while ((ch = getopt(argc, argv, "ACEHILNPRVXafghinorstuvwx")) != -1) |
f383e97b | 100 | #endif |
44a7a5ab A |
101 | switch (ch) { |
102 | case 'H': | |
103 | Hflag = 1; | |
104 | Lflag = 0; | |
40bf83fe | 105 | Pflag = 0; |
44a7a5ab A |
106 | break; |
107 | case 'L': | |
108 | Lflag = 1; | |
109 | Hflag = 0; | |
40bf83fe | 110 | Pflag = 0; |
44a7a5ab A |
111 | break; |
112 | case 'P': | |
113 | Hflag = Lflag = 0; | |
40bf83fe | 114 | Pflag = 1; |
44a7a5ab A |
115 | break; |
116 | case 'R': | |
117 | Rflag = 1; | |
118 | break; | |
6c780a1f | 119 | case 'f': |
44a7a5ab A |
120 | fflag = 1; |
121 | break; | |
44a7a5ab A |
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, | |
6c780a1f | 127 | * so it was an undocumented noop. In FreeBSD 3.0, |
44a7a5ab A |
128 | * lchmod(2) is introduced and this option does real |
129 | * work. | |
130 | */ | |
131 | hflag = 1; | |
132 | break; | |
864a4b6e | 133 | #ifdef __APPLE__ |
c59d3020 A |
134 | case 'a': |
135 | if (argv[optind - 1][0] == '-' && | |
136 | argv[optind - 1][1] == ch) | |
137 | --optind; | |
138 | goto done; | |
864a4b6e | 139 | case 'A': |
4d0bb651 A |
140 | // acloptflags |= ACL_FLAG | ACL_TO_STDOUT; |
141 | // ace_arg_not_required = 1; | |
864a4b6e A |
142 | errx(1, "-A not implemented"); |
143 | goto done; | |
c59d3020 A |
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; | |
40558d9b A |
162 | case 'N': |
163 | acloptflags |= ACL_FLAG | ACL_CLEAR_FLAG; | |
164 | ace_arg_not_required = 1; | |
165 | goto done; | |
c59d3020 | 166 | case 'V': |
4d0bb651 A |
167 | // acloptflags |= ACL_FLAG | ACL_INVOKE_EDITOR; |
168 | // ace_arg_not_required = 1; | |
864a4b6e | 169 | errx(1, "-V not implemented"); |
c59d3020 | 170 | goto done; |
6c780a1f | 171 | #endif /* __APPLE__ */ |
44a7a5ab A |
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; | |
6c780a1f A |
185 | case 'v': |
186 | vflag++; | |
187 | break; | |
44a7a5ab A |
188 | case '?': |
189 | default: | |
190 | usage(); | |
191 | } | |
192 | done: argv += optind; | |
193 | argc -= optind; | |
194 | ||
c59d3020 A |
195 | #ifdef __APPLE__ |
196 | if (argc < ((acloptflags & ACL_FLAG) ? 1 : 2)) | |
197 | usage(); | |
40bf83fe A |
198 | if (!Rflag && (Hflag || Lflag || Pflag)) |
199 | warnx("options -H, -L, -P only useful with -R"); | |
200 | #else /* !__APPLE__ */ | |
44a7a5ab A |
201 | if (argc < 2) |
202 | usage(); | |
40bf83fe | 203 | #endif /* __APPLE__ */ |
c59d3020 A |
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) | |
4d0bb651 | 244 | errx(1, "Invalid ACL entry number: %ld", aclpos); |
c59d3020 A |
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) | |
40bf83fe | 257 | warnx("Inheritance across more than one generation is not currently supported"); |
c59d3020 A |
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__*/ | |
44a7a5ab | 273 | |
44a7a5ab | 274 | if (Rflag) { |
6c780a1f | 275 | fts_options = FTS_PHYSICAL; |
44a7a5ab A |
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 | } | |
6c780a1f A |
285 | } else |
286 | fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL; | |
287 | ||
44a7a5ab A |
288 | if (hflag) |
289 | change_mode = lchmod; | |
290 | else | |
6c780a1f | 291 | change_mode = chmod; |
c59d3020 A |
292 | #ifdef __APPLE__ |
293 | if (acloptflags & ACL_FROM_STDIN) { | |
4d0bb651 A |
294 | ssize_t readval = 0; |
295 | size_t readtotal = 0; | |
c59d3020 A |
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) | |
40bf83fe | 309 | errx(1, "-E specified, but read from STDIN failed"); |
c59d3020 A |
310 | else |
311 | mode[readtotal - 1] = '\0'; | |
312 | --argv; | |
44a7a5ab | 313 | } |
c59d3020 A |
314 | else |
315 | #endif /* __APPLE */ | |
316 | mode = *argv; | |
317 | ||
318 | #ifdef __APPLE__ | |
319 | if ((acloptflags & ACL_FLAG)) { | |
44a7a5ab | 320 | |
c59d3020 A |
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) { | |
40bf83fe | 333 | errx(1, "Invalid ACL specification: %s", mode); |
c59d3020 A |
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__*/ | |
44a7a5ab | 358 | if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) |
6c780a1f | 359 | err(1, "fts_open"); |
44a7a5ab A |
360 | for (rval = 0; (p = fts_read(ftsp)) != NULL;) { |
361 | switch (p->fts_info) { | |
864a4b6e | 362 | case FTS_D: |
44a7a5ab | 363 | if (!Rflag) |
864a4b6e A |
364 | (void)fts_set(ftsp, p, FTS_SKIP); |
365 | break; | |
44a7a5ab A |
366 | case FTS_DNR: /* Warn, chmod, continue. */ |
367 | warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); | |
368 | rval = 1; | |
369 | break; | |
864a4b6e A |
370 | case FTS_DP: /* Already changed at FTS_D. */ |
371 | continue; | |
44a7a5ab | 372 | case FTS_NS: |
864a4b6e A |
373 | if (acloptflags & ACL_FLAG) /* don't need stat for -N */ |
374 | break; | |
375 | case FTS_ERR: /* Warn, continue. */ | |
44a7a5ab A |
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 | */ | |
44a7a5ab A |
386 | if (!hflag) |
387 | continue; | |
388 | /* else */ | |
389 | /* FALLTHROUGH */ | |
44a7a5ab A |
390 | default: |
391 | break; | |
392 | } | |
c59d3020 A |
393 | #ifdef __APPLE__ |
394 | /* If an ACL manipulation option was specified, manipulate */ | |
395 | if (acloptflags & ACL_FLAG) { | |
4d0bb651 | 396 | if (0 != modify_file_acl(acloptflags, p->fts_accpath, acl_input, (int)aclpos, inheritance_level, !hflag)) |
c59d3020 A |
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) { | |
40bf83fe | 405 | warn("Unable to change file mode on %s", p->fts_path); |
c59d3020 A |
406 | rval = 1; |
407 | } else { | |
408 | if (vflag) { | |
409 | (void)printf("%s", p->fts_accpath); | |
6c780a1f | 410 | |
c59d3020 A |
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, | |
6c780a1f | 420 | (p->fts_statp->st_mode & S_IFMT) | |
c59d3020 A |
421 | newmode, m2); |
422 | } | |
423 | (void)printf("\n"); | |
6c780a1f | 424 | } |
c59d3020 | 425 | |
6c780a1f | 426 | } |
c59d3020 | 427 | #ifdef __APPLE__ |
44a7a5ab | 428 | } |
c59d3020 | 429 | #endif /* __APPLE__*/ |
44a7a5ab A |
430 | } |
431 | if (errno) | |
432 | err(1, "fts_read"); | |
c59d3020 A |
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); | |
44a7a5ab | 442 | exit(rval); |
44a7a5ab A |
443 | } |
444 | ||
445 | void | |
6c780a1f | 446 | usage(void) |
44a7a5ab | 447 | { |
6c780a1f | 448 | #ifdef __APPLE__ |
44a7a5ab | 449 | (void)fprintf(stderr, |
864a4b6e | 450 | "usage:\tchmod [-fhv] [-R [-H | -L | -P]] [-a | +a | =a [i][# [ n]]] mode|entry file ...\n" |
40bf83fe | 451 | "\tchmod [-fhv] [-R [-H | -L | -P]] [-E | -C | -N | -i | -I] file ...\n"); /* add -A and -V when implemented */ |
44a7a5ab A |
452 | #else |
453 | (void)fprintf(stderr, | |
6c780a1f A |
454 | "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n"); |
455 | #endif /* __APPLE__ */ | |
44a7a5ab | 456 | exit(1); |
44a7a5ab | 457 | } |