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
, Pflag
, 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
= Pflag
= Rflag
= fflag
= hflag
= vflag
= 0;
94 while ((ch
= getopt(argc
, argv
, "HLPRXfghorstuvwx")) != -1)
96 while ((ch
= getopt(argc
, argv
, "ACEHILNPRVXafghinorstuvwx")) != -1)
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
132 if (argv
[optind
- 1][0] == '-' &&
133 argv
[optind
- 1][1] == ch
)
137 acloptflags
|= ACL_FLAG
| ACL_TO_STDOUT
;
138 ace_arg_not_required
= 1;
139 errx(1, "-A not implemented");
142 acloptflags
|= ACL_FLAG
| ACL_FROM_STDIN
;
145 acloptflags
|= ACL_FLAG
| ACL_CHECK_CANONICITY
;
146 ace_arg_not_required
= 1;
149 acloptflags
|= ACL_FLAG
| ACL_REMOVE_INHERIT_FLAG
;
150 ace_arg_not_required
= 1;
153 acloptflags
|= ACL_FLAG
| ACL_REMOVE_INHERITED_ENTRIES
;
154 ace_arg_not_required
= 1;
157 acloptflags
|= ACL_FLAG
| ACL_NO_TRANSLATE
;
160 acloptflags
|= ACL_FLAG
| ACL_CLEAR_FLAG
;
161 ace_arg_not_required
= 1;
164 acloptflags
|= ACL_FLAG
| ACL_INVOKE_EDITOR
;
165 ace_arg_not_required
= 1;
166 errx(1, "-V not implemented");
168 #endif /* __APPLE__ */
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.
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')
189 done
: argv
+= optind
;
193 if (argc
< ((acloptflags
& ACL_FLAG
) ?
1 : 2))
195 if (!Rflag
&& (Hflag
|| Lflag
|| Pflag
))
196 warnx("options -H, -L, -P only useful with -R");
197 #else /* !__APPLE__ */
200 #endif /* __APPLE__ */
203 if (!(acloptflags
& ACL_FLAG
) && ((acloptlen
= strlen(argv
[0])) > 1) && (argv
[0][1] == 'a')) {
204 acloptflags
|= ACL_FLAG
;
205 switch (argv
[0][0]) {
207 acloptflags
|= ACL_SET_FLAG
;
210 acloptflags
|= ACL_DELETE_FLAG
;
213 acloptflags
|= ACL_REWRITE_FLAG
;
216 acloptflags
&= ~ACL_FLAG
;
224 for (index
= 2; index
< acloptlen
; index
++) {
225 switch (argv
[0][index
]) {
227 acloptflags
|= ACL_ORDER_FLAG
;
229 if (argc
< ((acloptflags
& ACL_DELETE_FLAG
)
235 aclpos
= strtol(argv
[0], &ep
, 0);
237 if (aclpos
> ACL_MAX_ENTRIES
241 errx(1, "Invalid ACL entry number: %d", aclpos
);
242 if (acloptflags
& ACL_DELETE_FLAG
)
243 ace_arg_not_required
= 1;
247 acloptflags
|= ACL_INHERIT_FLAG
;
248 /* The +aii.. syntax to specify
249 * inheritance level is rather unwieldy,
250 * find an alternative.
253 if (inheritance_level
> 1)
254 warnx("Inheritance across more than one generation is not currently supported");
255 if (inheritance_level
>= MAX_INHERITANCE_LEVEL
)
272 fts_options
= FTS_PHYSICAL
;
275 "the -R and -h options may not be specified together.");
277 fts_options
|= FTS_COMFOLLOW
;
279 fts_options
&= ~FTS_PHYSICAL
;
280 fts_options
|= FTS_LOGICAL
;
283 fts_options
= hflag ? FTS_PHYSICAL
: FTS_LOGICAL
;
286 change_mode
= lchmod
;
290 if (acloptflags
& ACL_FROM_STDIN
) {
291 int readval
= 0, readtotal
= 0;
293 mode
= (char *) malloc(MAX_ACL_TEXT_SIZE
);
296 err(1, "Unable to allocate mode string");
297 /* Read the ACEs from STDIN */
299 readtotal
+= readval
;
300 readval
= read(STDIN_FILENO
, mode
+ readtotal
,
302 } while ((readval
> 0) && (readtotal
<= MAX_ACL_TEXT_SIZE
));
305 errx(1, "-E specified, but read from STDIN failed");
307 mode
[readtotal
- 1] = '\0';
315 if ((acloptflags
& ACL_FLAG
)) {
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
322 if (ace_arg_not_required
) {
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
);
334 #endif /* __APPLE__*/
335 if (*mode
>= '0' && *mode
<= '7') {
337 val
= strtol(mode
, &ep
, 8);
338 if (val
> USHRT_MAX
|| val
< 0)
341 err(1, "Invalid file mode: %s", mode
);
343 errx(1, "Invalid file mode: %s", mode
);
347 if ((set
= setmode(mode
)) == NULL
)
348 errx(1, "Invalid file mode: %s", mode
);
353 #endif /* __APPLE__*/
354 if ((ftsp
= fts_open(++argv
, fts_options
, 0)) == NULL
)
356 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
357 switch (p
->fts_info
) {
360 (void)fts_set(ftsp
, p
, FTS_SKIP
);
362 case FTS_DNR
: /* Warn, chmod, continue. */
363 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
366 case FTS_DP
: /* Already changed at FTS_D. */
369 if (acloptflags
& ACL_FLAG
) /* don't need stat for -N */
371 case FTS_ERR
: /* Warn, continue. */
372 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
375 case FTS_SL
: /* Ignore. */
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.
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
))
396 #endif /* __APPLE__ */
397 newmode
= oct ? omode
: getmode(set
, p
->fts_statp
->st_mode
);
398 if ((newmode
& ALLPERMS
) == (p
->fts_statp
->st_mode
& ALLPERMS
))
400 if ((*change_mode
)(p
->fts_accpath
, newmode
) && !fflag
) {
401 warn("Unable to change file mode on %s", p
->fts_path
);
405 (void)printf("%s", p
->fts_accpath
);
410 strmode(p
->fts_statp
->st_mode
, m1
);
411 strmode((p
->fts_statp
->st_mode
&
412 S_IFMT
) | newmode
, m2
);
414 (void)printf(": 0%o [%s] -> 0%o [%s]",
415 p
->fts_statp
->st_mode
, m1
,
416 (p
->fts_statp
->st_mode
& S_IFMT
) |
425 #endif /* __APPLE__*/
432 if (mode
&& (acloptflags
& ACL_FROM_STDIN
))
435 #endif /* __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 */
449 (void)fprintf(stderr
,
450 "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
451 #endif /* __APPLE__ */