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