2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static char const copyright
[] =
36 "@(#) Copyright (c) 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94";
45 #include <sys/cdefs.h>
46 __RCSID("$FreeBSD: src/bin/chmod/chmod.c,v 1.27 2002/08/04 05:29:13 obrien Exp $");
48 #include <sys/types.h>
61 #include "chmod_acl.h"
67 int main(int, char *[]);
71 main(int argc
, char *argv
[])
78 int Hflag
, Lflag
, Rflag
, ch
, fts_options
, hflag
, rval
;
81 mode_t newmode
, omode
;
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
;
88 int (*change_mode
)(const char *, mode_t
);
92 Hflag
= Lflag
= Rflag
= fflag
= hflag
= vflag
= 0;
94 while ((ch
= getopt(argc
, argv
, "HLPRXfghorstuvwx")) != -1)
96 while ((ch
= getopt(argc
, argv
, "ACEHILNPRVXafghinorstuvwx")) != -1)
118 * In System V (and probably POSIX.2) the -h option
119 * causes chmod to change the mode of the symbolic
120 * link. 4.4BSD's symbolic links didn't have modes,
121 * so it was an undocumented noop. In FreeBSD 3.0,
122 * lchmod(2) is introduced and this option does real
129 if (argv
[optind
- 1][0] == '-' &&
130 argv
[optind
- 1][1] == ch
)
134 acloptflags
|= ACL_FLAG
| ACL_TO_STDOUT
;
135 ace_arg_not_required
= 1;
136 errx(1, "-A not implemented");
139 acloptflags
|= ACL_FLAG
| ACL_FROM_STDIN
;
142 acloptflags
|= ACL_FLAG
| ACL_CHECK_CANONICITY
;
143 ace_arg_not_required
= 1;
146 acloptflags
|= ACL_FLAG
| ACL_REMOVE_INHERIT_FLAG
;
147 ace_arg_not_required
= 1;
150 acloptflags
|= ACL_FLAG
| ACL_REMOVE_INHERITED_ENTRIES
;
151 ace_arg_not_required
= 1;
154 acloptflags
|= ACL_FLAG
| ACL_NO_TRANSLATE
;
157 acloptflags
|= ACL_FLAG
| ACL_CLEAR_FLAG
;
158 ace_arg_not_required
= 1;
161 acloptflags
|= ACL_FLAG
| ACL_INVOKE_EDITOR
;
162 ace_arg_not_required
= 1;
163 errx(1, "-V not implemented");
165 #endif /* __APPLE__ */
168 * "-[rwx]" are valid mode commands. If they are the entire
169 * argument, getopt has moved past them, so decrement optind.
170 * Regardless, we're done argument processing.
172 case 'g': case 'o': case 'r': case 's':
173 case 't': case 'u': case 'w': case 'X': case 'x':
174 if (argv
[optind
- 1][0] == '-' &&
175 argv
[optind
- 1][1] == ch
&&
176 argv
[optind
- 1][2] == '\0')
186 done
: argv
+= optind
;
190 if (argc
< ((acloptflags
& ACL_FLAG
) ?
1 : 2))
198 if (!(acloptflags
& ACL_FLAG
) && ((acloptlen
= strlen(argv
[0])) > 1) && (argv
[0][1] == 'a')) {
199 acloptflags
|= ACL_FLAG
;
200 switch (argv
[0][0]) {
202 acloptflags
|= ACL_SET_FLAG
;
205 acloptflags
|= ACL_DELETE_FLAG
;
208 acloptflags
|= ACL_REWRITE_FLAG
;
211 acloptflags
&= ~ACL_FLAG
;
219 for (index
= 2; index
< acloptlen
; index
++) {
220 switch (argv
[0][index
]) {
222 acloptflags
|= ACL_ORDER_FLAG
;
224 if (argc
< ((acloptflags
& ACL_DELETE_FLAG
)
230 aclpos
= strtol(argv
[0], &ep
, 0);
232 if (aclpos
> ACL_MAX_ENTRIES
236 err(1, "Invalid ACL entry number: %s", aclpos
);
237 if (acloptflags
& ACL_DELETE_FLAG
)
238 ace_arg_not_required
= 1;
242 acloptflags
|= ACL_INHERIT_FLAG
;
243 /* The +aii.. syntax to specify
244 * inheritance level is rather unwieldy,
245 * find an alternative.
248 if (inheritance_level
> 1)
249 warn("Inheritance across more than one generation is not currently supported");
250 if (inheritance_level
>= MAX_INHERITANCE_LEVEL
)
267 fts_options
= FTS_PHYSICAL
;
270 "the -R and -h options may not be specified together.");
272 fts_options
|= FTS_COMFOLLOW
;
274 fts_options
&= ~FTS_PHYSICAL
;
275 fts_options
|= FTS_LOGICAL
;
278 fts_options
= hflag ? FTS_PHYSICAL
: FTS_LOGICAL
;
281 change_mode
= lchmod
;
285 if (acloptflags
& ACL_FROM_STDIN
) {
286 int readval
= 0, readtotal
= 0;
288 mode
= (char *) malloc(MAX_ACL_TEXT_SIZE
);
291 err(1, "Unable to allocate mode string");
292 /* Read the ACEs from STDIN */
294 readtotal
+= readval
;
295 readval
= read(STDIN_FILENO
, mode
+ readtotal
,
297 } while ((readval
> 0) && (readtotal
<= MAX_ACL_TEXT_SIZE
));
300 err(1, "-E specified, but read from STDIN failed");
302 mode
[readtotal
- 1] = '\0';
310 if ((acloptflags
& ACL_FLAG
)) {
312 /* Are we deleting by entry number, verifying
313 * canonicity or performing some other operation that
314 * does not require an input entry? If so, there's no
317 if (ace_arg_not_required
) {
321 /* Parse the text into an ACL*/
322 acl_input
= parse_acl_entries(mode
);
323 if (acl_input
== NULL
) {
325 err(1, "Invalid ACL specification: %s", mode
);
330 #endif /* __APPLE__*/
331 if (*mode
>= '0' && *mode
<= '7') {
333 val
= strtol(mode
, &ep
, 8);
334 if (val
> USHRT_MAX
|| val
< 0)
337 err(1, "Invalid file mode: %s", mode
);
339 errx(1, "Invalid file mode: %s", mode
);
343 if ((set
= setmode(mode
)) == NULL
)
344 errx(1, "Invalid file mode: %s", mode
);
349 #endif /* __APPLE__*/
350 if ((ftsp
= fts_open(++argv
, fts_options
, 0)) == NULL
)
352 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
353 switch (p
->fts_info
) {
356 (void)fts_set(ftsp
, p
, FTS_SKIP
);
358 case FTS_DNR
: /* Warn, chmod, continue. */
359 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
362 case FTS_DP
: /* Already changed at FTS_D. */
365 if (acloptflags
& ACL_FLAG
) /* don't need stat for -N */
367 case FTS_ERR
: /* Warn, continue. */
368 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
371 case FTS_SL
: /* Ignore. */
374 * The only symlinks that end up here are ones that
375 * don't point to anything and ones that we found
376 * doing a physical walk.
386 /* If an ACL manipulation option was specified, manipulate */
387 if (acloptflags
& ACL_FLAG
) {
388 if (0 != modify_file_acl(acloptflags
, p
->fts_accpath
, acl_input
, aclpos
, inheritance_level
))
392 #endif /* __APPLE__ */
393 newmode
= oct ? omode
: getmode(set
, p
->fts_statp
->st_mode
);
394 if ((newmode
& ALLPERMS
) == (p
->fts_statp
->st_mode
& ALLPERMS
))
396 if ((*change_mode
)(p
->fts_accpath
, newmode
) && !fflag
) {
397 warn("%s", p
->fts_path
);
401 (void)printf("%s", p
->fts_accpath
);
406 strmode(p
->fts_statp
->st_mode
, m1
);
407 strmode((p
->fts_statp
->st_mode
&
408 S_IFMT
) | newmode
, m2
);
410 (void)printf(": 0%o [%s] -> 0%o [%s]",
411 p
->fts_statp
->st_mode
, m1
,
412 (p
->fts_statp
->st_mode
& S_IFMT
) |
421 #endif /* __APPLE__*/
428 if (mode
&& (acloptflags
& ACL_FROM_STDIN
))
431 #endif /* __APPLE__ */
441 (void)fprintf(stderr
,
442 "usage:\tchmod [-fhv] [-R [-H | -L | -P]] [-a | +a | =a [i][# [ n]]] mode|entry file ...\n"
443 "\tchmod [-fhv] [-R [-H | -L | -P]] [-E | -C | -i | -I] file ...\n"); /* add -A and -V when implemented */
445 (void)fprintf(stderr
,
446 "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
447 #endif /* __APPLE__ */