xref: /openbsd-src/bin/chmod/chmod.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: chmod.c,v 1.12 2000/07/31 19:02:38 ericj 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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgment:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
46 #else
47 static char rcsid[] = "$OpenBSD: chmod.c,v 1.12 2000/07/31 19:02:38 ericj Exp $";
48 #endif
49 #endif /* not lint */
50 
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 
54 #include <err.h>
55 #include <errno.h>
56 #include <fts.h>
57 #include <pwd.h>
58 #include <grp.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <limits.h>
64 #include <locale.h>
65 
66 int ischflags, ischown, ischgrp, ischmod;
67 extern char *__progname;
68 
69 gid_t a_gid __P((const char *));
70 uid_t a_uid __P((const char *));
71 void usage __P((void));
72 
73 int
74 main(argc, argv)
75 	int argc;
76 	char *argv[];
77 {
78 	FTS *ftsp;
79 	FTSENT *p;
80 	mode_t *set;
81 	long val;
82 	int oct, omode;
83 	int Hflag, Lflag, Pflag, Rflag, ch, fflag, fts_options, hflag, rval;
84 	uid_t uid;
85 	gid_t gid;
86 	u_int32_t fclear, fset;
87 	char *ep, *mode, *cp, *flags;
88 #ifdef lint
89 	set = NULL;
90 	oct = omode = 0;
91 #endif
92 
93 	setlocale(LC_ALL, "");
94 
95 	ischown = __progname[2] == 'o';
96 	ischgrp = __progname[2] == 'g';
97 	ischmod = __progname[2] == 'm';
98 	ischflags = __progname[2] == 'f';
99 
100 	uid = -1;
101 	gid = -1;
102 	Hflag = Lflag = Pflag = Rflag = fflag = hflag = 0;
103 	while ((ch = getopt(argc, argv, "HLPRXfghorstuwx")) != -1)
104 		switch (ch) {
105 		case 'H':
106 			Hflag = 1;
107 			Lflag = Pflag = 0;
108 			break;
109 		case 'L':
110 			Lflag = 1;
111 			Hflag = Pflag = 0;
112 			break;
113 		case 'P':
114 			Pflag = 1;
115 			Hflag = Lflag = 0;
116 			break;
117 		case 'R':
118 			Rflag = 1;
119 			break;
120 		case 'f':		/* XXX: undocumented. */
121 			fflag = 1;
122 			break;
123 		case 'h':
124 			/*
125 			 * In System V (and probably POSIX.2) the -h option
126 			 * causes chmod to change the mode of the symbolic
127 			 * link.  4.4BSD's symbolic links don't have modes,
128 			 * so it's an undocumented noop.  Do syntax checking,
129 			 * though.
130 			 */
131 			hflag = 1;
132 			break;
133 		/*
134 		 * XXX
135 		 * "-[rwx]" are valid mode commands.  If they are the entire
136 		 * argument, getopt has moved past them, so decrement optind.
137 		 * Regardless, we're done argument processing.
138 		 */
139 		case 'g': case 'o': case 'r': case 's':
140 		case 't': case 'u': case 'w': case 'X': case 'x':
141 			if (!ischmod)
142 				usage();
143 			if (argv[optind - 1][0] == '-' &&
144 			    argv[optind - 1][1] == ch &&
145 			    argv[optind - 1][2] == '\0')
146 				--optind;
147 			goto done;
148 		default:
149 			usage();
150 		}
151 done:	argv += optind;
152 	argc -= optind;
153 
154 	if (argc < 2)
155 		usage();
156 
157 	fts_options = FTS_PHYSICAL;
158 	if (Rflag) {
159 		if (hflag)
160 			errx(1,
161 		"the -R and -h options may not be specified together.");
162 		if (Hflag)
163 			fts_options |= FTS_COMFOLLOW;
164 		if (Lflag) {
165 			fts_options &= ~FTS_PHYSICAL;
166 			fts_options |= FTS_LOGICAL;
167 		}
168 	}
169 
170 	if (ischflags) {
171 		flags = *argv;
172 		if (*flags >= '0' && *flags <= '7') {
173 			errno = 0;
174 			val = strtoul(flags, &ep, 8);
175 			if (val > UINT_MAX)
176 				errno = ERANGE;
177 			if (errno)
178 				err(1, "invalid flags: %s", flags);
179 			if (*ep)
180 				errx(1, "invalid flags: %s", flags);
181 			fset = val;
182 			oct = 1;
183 		} else {
184 			if (strtofflags(&flags, &fset, &fclear))
185 				errx(1, "invalid flag: %s", flags);
186 			fclear = ~fclear;
187 			oct = 0;
188 		}
189 	} else if (ischmod) {
190 		mode = *argv;
191 		if (*mode >= '0' && *mode <= '7') {
192 			errno = 0;
193 			val = strtol(mode, &ep, 8);
194 			if (val > INT_MAX || val < 0)
195 				errno = ERANGE;
196 			if (errno)
197 				err(1, "invalid file mode: %s", mode);
198 			if (*ep)
199 				errx(1, "invalid file mode: %s", mode);
200 			omode = val;
201 			oct = 1;
202 		} else {
203 			if ((set = setmode(mode)) == NULL)
204 				errx(1, "invalid file mode: %s", mode);
205 			oct = 0;
206 		}
207 	} else if (ischown) {
208 		if ((cp = strchr(*argv, ':')) != NULL) {
209 			*cp++ = '\0';
210 			gid = a_gid(cp);
211 		}
212 #ifdef SUPPORT_DOT
213 		else if ((cp = strchr(*argv, '.')) != NULL) {
214 			*cp++ = '\0';
215 			gid = a_gid(cp);
216 		}
217 #endif
218 		uid = a_uid(*argv);
219 	} else
220 		gid = a_gid(*argv);
221 
222 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
223 		err(1, NULL);
224 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
225 		switch (p->fts_info) {
226 		case FTS_D:
227 			if (!Rflag)
228 				fts_set(ftsp, p, FTS_SKIP);
229 			if (ischmod)
230 				break;
231 			else
232 				continue;
233 		case FTS_DNR:			/* Warn, chmod, continue. */
234 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
235 			rval = 1;
236 			break;
237 		case FTS_DP:			/* Already changed at FTS_D. */
238 			if (ischmod)
239 				continue;
240 			else
241 				break;
242 		case FTS_ERR:			/* Warn, continue. */
243 		case FTS_NS:
244 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
245 			rval = 1;
246 			continue;
247 		case FTS_SL:			/* Ignore. */
248 		case FTS_SLNONE:
249 			/*
250 			 * The only symlinks that end up here are ones that
251 			 * don't point to anything and ones that we found
252 			 * doing a physical walk.
253 			 */
254 			if (ischflags || ischmod || !hflag)
255 				continue;
256 		default:
257 			break;
258 		}
259 		if (ischflags) {
260 			if (oct) {
261 				if (!chflags(p->fts_accpath, fset))
262 					continue;
263 			} else {
264 				p->fts_statp->st_flags |= fset;
265 				p->fts_statp->st_flags &= fclear;
266 				if (!chflags(p->fts_accpath, p->fts_statp->st_flags))
267 					continue;
268 			}
269 			warn("%s", p->fts_path);
270 			rval = 1;
271 		} else if (ischmod && chmod(p->fts_accpath, oct ? omode :
272 		    getmode(set, p->fts_statp->st_mode)) && !fflag) {
273 			warn("%s", p->fts_path);
274 			rval = 1;
275 		} else if (!ischmod && !ischflags &&
276 		    (hflag ? lchown(p->fts_accpath, uid, gid) :
277 		    chown(p->fts_accpath, uid, gid)) && !fflag) {
278 			warn("%s", p->fts_path);
279 			rval = 1;
280 		}
281 	}
282 	if (errno)
283 		err(1, "fts_read");
284 	exit(rval);
285 }
286 
287 uid_t
288 a_uid(s)
289 	const char *s;
290 {
291 	struct passwd *pw;
292 	char *ep;
293 	u_long ul;
294 
295 	if (*s == '\0')			/* Argument was "gid[:.]". */
296 		return -1;
297 
298 	if ((pw = getpwnam(s)) != NULL) {
299 		return pw->pw_uid;
300 	} else {
301 		if ((ul = strtoul(s, &ep, 10)) == ULONG_MAX)
302 			err(1, "%s", s);
303 		if (*ep != '\0')
304 			errx(1, "%s: invalid user name", s);
305 		return (uid_t)ul;
306 	}
307 }
308 
309 gid_t
310 a_gid(s)
311 	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 		return (gid_t)ul;
328 	}
329 }
330 
331 void
332 usage()
333 {
334 	if (ischmod || ischflags)
335 		fprintf(stderr,
336 		    "usage: %s [-R [-H | -L | -P]] %s file ...\n",
337 		    __progname, (ischmod? "mode" : "flags"));
338 	else
339 		fprintf(stderr,
340 		    "usage: %s [-R [-H | -L | -P]] [-f] [-h] %s file ...\n",
341 		    __progname, ischown ? "[owner][:group]" : "group");
342 
343 	exit(1);
344 }
345