1 /* $OpenBSD: chmod.c,v 1.28 2012/12/04 02:24:46 deraadt Exp $ */
2 /* $NetBSD: chmod.c,v 1.12 1995/03/21 09:02:09 cgd Exp $ */
5 * Copyright (c) 1989, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
48 int ischflags
, ischown
, ischgrp
, ischmod
;
49 extern char *__progname
;
51 gid_t
a_gid(const char *);
52 uid_t
a_uid(const char *, int);
53 __dead
void usage(void);
56 main(int argc
, char *argv
[])
64 int Hflag
, Lflag
, Rflag
, ch
, fflag
, fts_options
, hflag
, rval
;
67 u_int32_t fclear
, fset
;
68 char *ep
, *mode
, *cp
, *flags
;
74 setlocale(LC_ALL
, "");
76 ischown
= __progname
[2] == 'o';
77 ischgrp
= __progname
[2] == 'g';
78 ischmod
= __progname
[2] == 'm';
79 ischflags
= __progname
[2] == 'f';
83 Hflag
= Lflag
= Rflag
= fflag
= hflag
= 0;
84 while ((ch
= getopt(argc
, argv
, "HLPRXfghorstuwx")) != -1)
100 case 'f': /* no longer documented. */
105 * In System V (and probably POSIX.2) the -h option
106 * causes chmod to change the mode of the symbolic
107 * link. 4.4BSD's symbolic links don't have modes,
108 * so it's an undocumented noop. Do syntax checking,
115 * "-[rwx]" are valid mode commands. If they are the entire
116 * argument, getopt has moved past them, so decrement optind.
117 * Regardless, we're done argument processing.
119 case 'g': case 'o': case 'r': case 's':
120 case 't': case 'u': case 'w': case 'X': case 'x':
123 if (argv
[optind
- 1][0] == '-' &&
124 argv
[optind
- 1][1] == ch
&&
125 argv
[optind
- 1][2] == '\0')
138 fts_options
= FTS_PHYSICAL
;
142 "the -R and -h options may not be specified together.");
144 fts_options
|= FTS_COMFOLLOW
;
146 fts_options
&= ~FTS_PHYSICAL
;
147 fts_options
|= FTS_LOGICAL
;
153 if (*flags
>= '0' && *flags
<= '7') {
155 val
= strtoul(flags
, &ep
, 8);
159 err(1, "invalid flags: %s", flags
);
161 errx(1, "invalid flags: %s", flags
);
165 if (strtofflags(&flags
, &fset
, &fclear
))
166 errx(1, "invalid flag: %s", flags
);
170 } else if (ischmod
) {
172 if (*mode
>= '0' && *mode
<= '7') {
174 val
= strtol(mode
, &ep
, 8);
175 if (val
> INT_MAX
|| val
< 0)
178 err(1, "invalid file mode: %s", mode
);
180 errx(1, "invalid file mode: %s", mode
);
184 if ((set
= setmode(mode
)) == NULL
)
185 errx(1, "invalid file mode: %s", mode
);
188 } else if (ischown
) {
189 /* Both UID and GID are given. */
190 if ((cp
= strchr(*argv
, ':')) != NULL
) {
195 /* UID and GID are separated by a dot and UID exists. */
196 else if ((cp
= strchr(*argv
, '.')) != NULL
&&
197 (uid
= a_uid(*argv
, 1)) == (uid_t
)-1) {
202 if (uid
== (uid_t
)-1)
203 uid
= a_uid(*argv
, 0);
207 if ((ftsp
= fts_open(++argv
, fts_options
, 0)) == NULL
)
209 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
210 switch (p
->fts_info
) {
213 fts_set(ftsp
, p
, FTS_SKIP
);
218 case FTS_DNR
: /* Warn, chmod, continue. */
219 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
222 case FTS_DP
: /* Already changed at FTS_D. */
227 case FTS_ERR
: /* Warn, continue. */
229 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
232 case FTS_SL
: /* Ignore. */
235 * The only symlinks that end up here are ones that
236 * don't point to anything and ones that we found
237 * doing a physical walk.
239 if (ischflags
|| ischmod
|| !hflag
)
247 if (!chflags(p
->fts_accpath
, fset
))
250 p
->fts_statp
->st_flags
|= fset
;
251 p
->fts_statp
->st_flags
&= fclear
;
252 if (!chflags(p
->fts_accpath
, p
->fts_statp
->st_flags
))
255 warn("%s", p
->fts_path
);
257 } else if (ischmod
&& chmod(p
->fts_accpath
, oct
? omode
:
258 getmode(set
, p
->fts_statp
->st_mode
)) && !fflag
) {
259 warn("%s", p
->fts_path
);
261 } else if (!ischmod
&& !ischflags
&&
262 (hflag
? lchown(p
->fts_accpath
, uid
, gid
) :
263 chown(p
->fts_accpath
, uid
, gid
)) && !fflag
) {
264 warn("%s", p
->fts_path
);
274 * Given a UID or user name in a string, return the UID. If an empty string
275 * was given, returns -1. If silent is 0, exits on invalid user names/UIDs;
276 * otherwise, returns -1.
279 a_uid(const char *s
, int silent
)
285 if (*s
== '\0') /* Argument was "[:.]gid". */
288 /* User name was given. */
289 if ((pw
= getpwnam(s
)) != NULL
)
293 uid
= (uid_t
)strtonum(s
, 0, UID_MAX
, &errstr
);
298 errx(1, "user is %s: %s", errstr
, s
);
305 * Given a GID or group name in a string, return the GID. If an empty string
306 * was given, returns -1. Exits on invalid user names/UIDs.
315 if (*s
== '\0') /* Argument was "uid[:.]". */
318 /* Group name was given. */
319 if ((gr
= getgrnam(s
)) != NULL
)
323 gid
= (gid_t
)strtonum(s
, 0, GID_MAX
, &errstr
);
325 errx(1, "group is %s: %s", errstr
, s
);
333 if (ischmod
|| ischflags
)
335 "usage: %s [-R [-H | -L | -P]] %s file ...\n",
336 __progname
, ischmod
? "mode" : "flags");
339 "usage: %s [-h] [-R [-H | -L | -P]] %s file ...\n",
340 __progname
, ischown
? "owner[:group]" : "group");
343 " %s [-h] [-R [-H | -L | -P]] :group file ...\n",