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