1 /* $OpenBSD: chmod.c,v 1.18 2004/07/01 18:25:47 otto Exp $ */
2 /* $NetBSD: chmod.c,v 1.12 1995/03/21 09:02:09 cgd Exp $ */
3
4 /*
5 * Copyright (c) 1989, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/stat.h>
35
36 #include <err.h>
37 #include <errno.h>
38 #include <fts.h>
39 #include <pwd.h>
40 #include <grp.h>
41 #include <stdbool.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <limits.h>
47 #include <locale.h>
48
49 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
50 The Regents of the University of California. All rights reserved.\n");
51 __SCCSID("@(#)chmod.c 8.8 (Berkeley) 4/1/94");
52 __RCSID("$OpenBSD: chmod.c,v 1.18 2004/07/01 18:25:47 otto Exp $");
53 __RCSID("$NetBSD: chmod.c,v 1.33 2005/10/01 20:09:18 christos Exp $");
54 __RCSID("$MirOS: src/bin/chmod/chmod.c,v 1.3 2007/07/05 23:09:31 tg Exp $");
55
56 int ischflags, ischown, ischgrp, ischmod;
57 extern const char *__progname;
58
59 gid_t a_gid(const char *);
60 uid_t a_uid(const char *, int);
61 void usage(void) __dead;
62
63 int
main(int argc,char * argv[])64 main(int argc, char *argv[])
65 {
66 FTS *ftsp;
67 FTSENT *p;
68 void *set = NULL;
69 long val;
70 bool oct = false;
71 mode_t omode = 0;
72 int Hflag, Lflag, Pflag, Rflag, ch, fflag, fts_options, hflag, rval;
73 uid_t uid;
74 gid_t gid;
75 u_int32_t fclear, fset;
76 char *ep, *mode, *cp, *flags;
77
78 #ifndef __MirBSD__
79 setlocale(LC_ALL, "");
80 #endif
81
82 ischown = __progname[2] == 'o';
83 ischgrp = __progname[2] == 'g';
84 ischmod = __progname[2] == 'm';
85 ischflags = __progname[2] == 'f';
86
87 uid = -1;
88 gid = -1;
89 Hflag = Lflag = Pflag = Rflag = fflag = hflag = 0;
90 while ((ch = getopt(argc, argv, "HLPRXfghorstuwx")) != -1)
91 switch (ch) {
92 case 'H':
93 Hflag = 1;
94 Lflag = Pflag = 0;
95 break;
96 case 'L':
97 Lflag = 1;
98 Hflag = Pflag = 0;
99 break;
100 case 'P':
101 Pflag = 1;
102 Hflag = Lflag = 0;
103 break;
104 case 'R':
105 Rflag = 1;
106 break;
107 case 'f': /* XXX: undocumented. */
108 fflag = 1;
109 break;
110 case 'h':
111 /*
112 * In System V (and probably POSIX.2) the -h option
113 * causes chmod to change the mode of the symbolic
114 * link. 4.4BSD's symbolic links don't have modes,
115 * so it's an undocumented noop. In MirOS #10,
116 * lchmod(2) and lchflags(2) were introduced, and
117 * this option does real work.
118 */
119 hflag = 1;
120 break;
121 /*
122 * XXX
123 * "-[rwx]" are valid mode commands. If they are the entire
124 * argument, getopt has moved past them, so decrement optind.
125 * Regardless, we're done argument processing.
126 */
127 case 'g': case 'o': case 'r': case 's':
128 case 't': case 'u': case 'w': case 'X': case 'x':
129 if (!ischmod)
130 usage();
131 if (argv[optind - 1][0] == '-' &&
132 argv[optind - 1][1] == ch &&
133 argv[optind - 1][2] == '\0')
134 --optind;
135 goto done;
136 default:
137 usage();
138 }
139 done: argv += optind;
140 argc -= optind;
141
142 if (argc < 2)
143 usage();
144
145 fts_options = FTS_PHYSICAL;
146 if (Rflag) {
147 if (hflag)
148 errx(1,
149 "the -R and -h options may not be specified together.");
150 if (Hflag)
151 fts_options |= FTS_COMFOLLOW;
152 if (Lflag) {
153 fts_options &= ~FTS_PHYSICAL;
154 fts_options |= FTS_LOGICAL;
155 }
156 } else if (!hflag)
157 fts_options |= FTS_COMFOLLOW;
158
159 if (ischflags) {
160 flags = *argv;
161 if (*flags >= '0' && *flags <= '7') {
162 errno = 0;
163 val = strtoul(flags, &ep, 8);
164 if (val > (long)(UINT_MAX))
165 errno = ERANGE;
166 if (errno)
167 err(1, "invalid flags: %s", flags);
168 if (*ep)
169 errx(1, "invalid flags: %s", flags);
170 fset = val;
171 oct = true;
172 } else {
173 if (strtofflags(&flags, &fset, &fclear))
174 errx(1, "invalid flag: %s", flags);
175 fclear = ~fclear;
176 oct = false;
177 }
178 } else if (ischmod) {
179 mode = *argv;
180 if (*mode >= '0' && *mode <= '7') {
181 errno = 0;
182 val = strtol(mode, &ep, 8);
183 if (val > INT_MAX || val < 0)
184 errno = ERANGE;
185 if (errno)
186 err(1, "invalid file mode: %s", mode);
187 if (*ep)
188 errx(1, "invalid file mode: %s", mode);
189 omode = val;
190 oct = true;
191 } else {
192 if ((set = setmode(mode)) == NULL)
193 errx(1, "invalid file mode: %s", mode);
194 oct = false;
195 }
196 } else if (ischown) {
197 if ((cp = strchr(*argv, ':')) != NULL) {
198 *cp++ = '\0';
199 gid = a_gid(cp);
200 }
201 #ifdef SUPPORT_DOT
202 else if ((cp = strchr(*argv, '.')) != NULL &&
203 (uid = a_uid(*argv, 1)) == -1) {
204 *cp++ = '\0';
205 gid = a_gid(cp);
206 }
207 #endif
208 if (uid == (uid_t)(-1))
209 uid = a_uid(*argv, 0);
210 } else
211 gid = a_gid(*argv);
212
213 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
214 err(1, NULL);
215 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
216 switch (p->fts_info) {
217 case FTS_D:
218 if (!Rflag)
219 fts_set(ftsp, p, FTS_SKIP);
220 if (ischmod)
221 break;
222 else
223 continue;
224 case FTS_DNR: /* Warn, chmod, continue. */
225 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
226 rval = 1;
227 break;
228 case FTS_DP: /* Already changed at FTS_D. */
229 if (ischmod)
230 continue;
231 else
232 break;
233 case FTS_ERR: /* Warn, continue. */
234 case FTS_NS:
235 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
236 rval = 1;
237 continue;
238 case FTS_SL: /* Ignore. */
239 case FTS_SLNONE:
240 /*
241 * The only symlinks that end up here are ones that
242 * don't point to anything and ones that we found
243 * doing a physical walk.
244 */
245 if (!hflag)
246 continue;
247 /* FALLTHROUGH */
248 default:
249 break;
250 }
251 if (ischflags) {
252 if (oct) {
253 if (!(hflag?lchflags:chflags)(p->fts_accpath,
254 fset))
255 continue;
256 } else {
257 p->fts_statp->st_flags |= fset;
258 p->fts_statp->st_flags &= fclear;
259 if (!(hflag?lchflags:chflags)(p->fts_accpath,
260 p->fts_statp->st_flags))
261 continue;
262 }
263 warn("%s", p->fts_path);
264 rval = 1;
265 } else if (ischmod && (hflag ? lchmod : chmod)(p->fts_accpath,
266 oct ? omode : getmode(set, p->fts_statp->st_mode)) &&
267 !fflag) {
268 warn("%s", p->fts_path);
269 rval = 1;
270 } else if (!ischmod && !ischflags &&
271 (hflag ? lchown : chown)(p->fts_accpath, uid, gid) &&
272 !fflag) {
273 warn("%s", p->fts_path);
274 rval = 1;
275 }
276 }
277 if (errno)
278 err(1, "fts_read");
279 exit(rval);
280 }
281
282 uid_t
a_uid(const char * s,int silent)283 a_uid(const char *s, int silent)
284 {
285 struct passwd *pw;
286 char *ep;
287 u_long ul;
288
289 if (*s == '\0') /* Argument was "gid[:.]". */
290 return -1;
291
292 if ((pw = getpwnam(s)) != NULL) {
293 return pw->pw_uid;
294 } else {
295 if ((ul = strtoul(s, &ep, 10)) == ULONG_MAX) {
296 if (silent)
297 return -1;
298 err(1, "%s", s);
299 }
300 if (*ep != '\0') {
301 if (silent)
302 return -1;
303 errx(1, "%s: invalid user name", s);
304 }
305 /* XXX long -> int */
306 return (uid_t)ul;
307 }
308 }
309
310 gid_t
a_gid(const char * s)311 a_gid(const char *s)
312 {
313 struct group *gr;
314 char *ep;
315 u_long ul;
316
317 if (*s == '\0') /* Argument was "gid[:.]". */
318 return -1;
319
320 if ((gr = getgrnam(s)) != NULL) {
321 return gr->gr_gid;
322 } else {
323 if ((ul = strtoul(s, &ep, 10)) == ULONG_MAX)
324 err(1, "%s", s);
325 if (*ep != '\0')
326 errx(1, "%s: invalid group name", s);
327 /* XXX long -> int */
328 return (gid_t)ul;
329 }
330 }
331
332 void
usage(void)333 usage(void)
334 {
335 if (ischmod || ischflags)
336 fprintf(stderr,
337 "usage: %s [-R [-H | -L | -P]] [-h] %s file ...\n",
338 __progname, (ischmod ? "mode" : "flags"));
339 else
340 fprintf(stderr,
341 "usage: %s [-R [-H | -L | -P]] [-f] [-h] %s file ...\n",
342 __progname, ischown ? "[owner][:group]" : "group");
343 exit(1);
344 }
345