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 #if 0 33 #ifndef lint 34 static char const copyright[] = 35 "@(#) Copyright (c) 1989, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94"; 41 #endif /* not lint */ 42 #endif 43 #include <sys/cdefs.h> 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 <signal.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 static volatile sig_atomic_t siginfo; 59 60 static void usage(void) __dead2; 61 static int may_have_nfs4acl(const FTSENT *ent, int hflag); 62 63 static void 64 siginfo_handler(int sig __unused) 65 { 66 67 siginfo = 1; 68 } 69 70 int 71 main(int argc, char *argv[]) 72 { 73 FTS *ftsp; 74 FTSENT *p; 75 mode_t *set; 76 int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval; 77 int vflag; 78 char *mode; 79 mode_t newmode; 80 81 set = NULL; 82 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0; 83 while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1) 84 switch (ch) { 85 case 'H': 86 Hflag = 1; 87 Lflag = 0; 88 break; 89 case 'L': 90 Lflag = 1; 91 Hflag = 0; 92 break; 93 case 'P': 94 Hflag = Lflag = 0; 95 break; 96 case 'R': 97 Rflag = 1; 98 break; 99 case 'f': 100 fflag = 1; 101 break; 102 case 'h': 103 /* 104 * In System V the -h option causes chmod to change 105 * the mode of the symbolic link. 4.4BSD's symbolic 106 * links didn't have modes, so it was an undocumented 107 * noop. In FreeBSD 3.0, lchmod(2) is introduced and 108 * this option does real work. 109 */ 110 hflag = 1; 111 break; 112 /* 113 * XXX 114 * "-[rwx]" are valid mode commands. If they are the entire 115 * argument, getopt has moved past them, so decrement optind. 116 * Regardless, we're done argument processing. 117 */ 118 case 'g': case 'o': case 'r': case 's': 119 case 't': case 'u': case 'w': case 'X': case 'x': 120 if (argv[optind - 1][0] == '-' && 121 argv[optind - 1][1] == ch && 122 argv[optind - 1][2] == '\0') 123 --optind; 124 goto done; 125 case 'v': 126 vflag++; 127 break; 128 case '?': 129 default: 130 usage(); 131 } 132 done: argv += optind; 133 argc -= optind; 134 135 if (argc < 2) 136 usage(); 137 138 (void)signal(SIGINFO, siginfo_handler); 139 140 if (Rflag) { 141 if (hflag) 142 errx(1, "the -R and -h options may not be " 143 "specified together."); 144 if (Lflag) { 145 fts_options = FTS_LOGICAL; 146 } else { 147 fts_options = FTS_PHYSICAL; 148 149 if (Hflag) { 150 fts_options |= FTS_COMFOLLOW; 151 } 152 } 153 } else if (hflag) { 154 fts_options = FTS_PHYSICAL; 155 } else { 156 fts_options = FTS_LOGICAL; 157 } 158 159 mode = *argv; 160 if ((set = setmode(mode)) == NULL) 161 errx(1, "invalid file mode: %s", mode); 162 163 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) 164 err(1, "fts_open"); 165 for (rval = 0; errno = 0, (p = fts_read(ftsp)) != NULL;) { 166 int atflag; 167 168 if ((fts_options & FTS_LOGICAL) || 169 ((fts_options & FTS_COMFOLLOW) && 170 p->fts_level == FTS_ROOTLEVEL)) 171 atflag = 0; 172 else 173 atflag = AT_SYMLINK_NOFOLLOW; 174 175 switch (p->fts_info) { 176 case FTS_D: 177 if (!Rflag) 178 fts_set(ftsp, p, FTS_SKIP); 179 break; 180 case FTS_DNR: /* Warn, chmod. */ 181 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 182 rval = 1; 183 break; 184 case FTS_DP: /* Already changed at FTS_D. */ 185 continue; 186 case FTS_ERR: /* Warn, continue. */ 187 case FTS_NS: 188 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 189 rval = 1; 190 continue; 191 default: 192 break; 193 } 194 newmode = getmode(set, p->fts_statp->st_mode); 195 /* 196 * With NFSv4 ACLs, it is possible that applying a mode 197 * identical to the one computed from an ACL will change 198 * that ACL. 199 */ 200 if (may_have_nfs4acl(p, hflag) == 0 && 201 (newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS)) 202 continue; 203 if (fchmodat(AT_FDCWD, p->fts_accpath, newmode, atflag) == -1 204 && !fflag) { 205 warn("%s", p->fts_path); 206 rval = 1; 207 } else if (vflag || siginfo) { 208 (void)printf("%s", p->fts_path); 209 210 if (vflag > 1 || siginfo) { 211 char m1[12], m2[12]; 212 213 strmode(p->fts_statp->st_mode, m1); 214 strmode((p->fts_statp->st_mode & 215 S_IFMT) | newmode, m2); 216 (void)printf(": 0%o [%s] -> 0%o [%s]", 217 p->fts_statp->st_mode, m1, 218 (p->fts_statp->st_mode & S_IFMT) | 219 newmode, m2); 220 } 221 (void)printf("\n"); 222 siginfo = 0; 223 } 224 } 225 if (errno) 226 err(1, "fts_read"); 227 exit(rval); 228 } 229 230 static void 231 usage(void) 232 { 233 (void)fprintf(stderr, 234 "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n"); 235 exit(1); 236 } 237 238 static int 239 may_have_nfs4acl(const FTSENT *ent, int hflag) 240 { 241 int ret; 242 static dev_t previous_dev = NODEV; 243 static int supports_acls = -1; 244 245 if (previous_dev != ent->fts_statp->st_dev) { 246 previous_dev = ent->fts_statp->st_dev; 247 supports_acls = 0; 248 249 if (hflag) 250 ret = lpathconf(ent->fts_accpath, _PC_ACL_NFS4); 251 else 252 ret = pathconf(ent->fts_accpath, _PC_ACL_NFS4); 253 if (ret > 0) 254 supports_acls = 1; 255 else if (ret < 0 && errno != EINVAL) 256 warn("%s", ent->fts_path); 257 } 258 259 return (supports_acls); 260 } 261