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