chmod.c revision 283875
1/*-
2 * Copyright (c) 1989, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static char const copyright[] =
33"@(#) Copyright (c) 1989, 1993, 1994\n\
34	The Regents of the University of California.  All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
39#endif /* not lint */
40#endif
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: stable/10/bin/chmod/chmod.c 283875 2015-06-01 09:04:57Z smh $");
43
44#include <sys/param.h>
45#include <sys/stat.h>
46
47#include <err.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <fts.h>
51#include <limits.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>
56
57static void usage(void);
58static int may_have_nfs4acl(const FTSENT *ent, int hflag);
59
60int
61main(int argc, char *argv[])
62{
63	FTS *ftsp;
64	FTSENT *p;
65	mode_t *set;
66	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
67	int vflag;
68	char *mode;
69	mode_t newmode;
70
71	set = NULL;
72	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
73	while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
74		switch (ch) {
75		case 'H':
76			Hflag = 1;
77			Lflag = 0;
78			break;
79		case 'L':
80			Lflag = 1;
81			Hflag = 0;
82			break;
83		case 'P':
84			Hflag = Lflag = 0;
85			break;
86		case 'R':
87			Rflag = 1;
88			break;
89		case 'f':
90			fflag = 1;
91			break;
92		case 'h':
93			/*
94			 * In System V (and probably POSIX.2) the -h option
95			 * causes chmod to change the mode of the symbolic
96			 * link.  4.4BSD's symbolic links didn't have modes,
97			 * so it was an undocumented noop.  In FreeBSD 3.0,
98			 * lchmod(2) is introduced and this option does real
99			 * work.
100			 */
101			hflag = 1;
102			break;
103		/*
104		 * XXX
105		 * "-[rwx]" are valid mode commands.  If they are the entire
106		 * argument, getopt has moved past them, so decrement optind.
107		 * Regardless, we're done argument processing.
108		 */
109		case 'g': case 'o': case 'r': case 's':
110		case 't': case 'u': case 'w': case 'X': case 'x':
111			if (argv[optind - 1][0] == '-' &&
112			    argv[optind - 1][1] == ch &&
113			    argv[optind - 1][2] == '\0')
114				--optind;
115			goto done;
116		case 'v':
117			vflag++;
118			break;
119		case '?':
120		default:
121			usage();
122		}
123done:	argv += optind;
124	argc -= optind;
125
126	if (argc < 2)
127		usage();
128
129	if (Rflag) {
130		if (hflag)
131			errx(1, "the -R and -h options may not be "
132			    "specified together.");
133		if (Lflag) {
134			fts_options = FTS_LOGICAL;
135		} else {
136			fts_options = FTS_PHYSICAL;
137
138			if (Hflag) {
139				fts_options |= FTS_COMFOLLOW;
140			}
141		}
142	} else if (hflag) {
143		fts_options = FTS_PHYSICAL;
144	} else {
145		fts_options = FTS_LOGICAL;
146	}
147
148	mode = *argv;
149	if ((set = setmode(mode)) == NULL)
150		errx(1, "invalid file mode: %s", mode);
151
152	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
153		err(1, "fts_open");
154	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
155		int atflag;
156
157		if ((fts_options & FTS_LOGICAL) ||
158		    ((fts_options & FTS_COMFOLLOW) &&
159		    p->fts_level == FTS_ROOTLEVEL))
160			atflag = 0;
161		else
162			atflag = AT_SYMLINK_NOFOLLOW;
163
164		switch (p->fts_info) {
165		case FTS_D:			/* Change it at FTS_DP. */
166			if (!Rflag)
167				fts_set(ftsp, p, FTS_SKIP);
168			continue;
169		case FTS_DNR:			/* Warn, chmod. */
170			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
171			rval = 1;
172			break;
173		case FTS_ERR:			/* Warn, continue. */
174		case FTS_NS:
175			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
176			rval = 1;
177			continue;
178		default:
179			break;
180		}
181		newmode = getmode(set, p->fts_statp->st_mode);
182		/*
183		 * With NFSv4 ACLs, it is possible that applying a mode
184		 * identical to the one computed from an ACL will change
185		 * that ACL.
186		 */
187		if (may_have_nfs4acl(p, hflag) == 0 &&
188		    (newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
189				continue;
190		if (fchmodat(AT_FDCWD, p->fts_accpath, newmode, atflag) == -1
191		    && !fflag) {
192			warn("%s", p->fts_path);
193			rval = 1;
194		} else if (vflag) {
195			(void)printf("%s", p->fts_path);
196
197			if (vflag > 1) {
198				char m1[12], m2[12];
199
200				strmode(p->fts_statp->st_mode, m1);
201				strmode((p->fts_statp->st_mode &
202				    S_IFMT) | newmode, m2);
203				(void)printf(": 0%o [%s] -> 0%o [%s]",
204				    p->fts_statp->st_mode, m1,
205				    (p->fts_statp->st_mode & S_IFMT) |
206				    newmode, m2);
207			}
208			(void)printf("\n");
209		}
210	}
211	if (errno)
212		err(1, "fts_read");
213	exit(rval);
214}
215
216static void
217usage(void)
218{
219	(void)fprintf(stderr,
220	    "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n");
221	exit(1);
222}
223
224static int
225may_have_nfs4acl(const FTSENT *ent, int hflag)
226{
227	int ret;
228	static dev_t previous_dev = NODEV;
229	static int supports_acls = -1;
230
231	if (previous_dev != ent->fts_statp->st_dev) {
232		previous_dev = ent->fts_statp->st_dev;
233		supports_acls = 0;
234
235		if (hflag)
236			ret = lpathconf(ent->fts_accpath, _PC_ACL_NFS4);
237		else
238			ret = pathconf(ent->fts_accpath, _PC_ACL_NFS4);
239		if (ret > 0)
240			supports_acls = 1;
241		else if (ret < 0 && errno != EINVAL)
242			warn("%s", ent->fts_path);
243	}
244
245	return (supports_acls);
246}
247