1 /* 2 * Copyright (c) 1988, 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 * 3. 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 32 static const char copyright[] = 33 "@(#) Copyright (c) 1988, 1993, 1994\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35 #endif /* not lint */ 36 37 #ifndef lint 38 static char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94"; 39 #endif /* not lint */ 40 #endif 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 48 #include <err.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <fts.h> 52 #include <grp.h> 53 #include <libgen.h> 54 #include <pwd.h> 55 #include <signal.h> 56 #include <stdint.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 static void a_gid(const char *); 63 static void a_uid(const char *); 64 static void chownerr(const char *); 65 static uid_t id(const char *, const char *); 66 static void usage(void); 67 static void print_info(const FTSENT *, int); 68 69 static uid_t uid; 70 static gid_t gid; 71 static int ischown; 72 static const char *gname; 73 static volatile sig_atomic_t siginfo; 74 75 static void 76 siginfo_handler(int sig __unused) 77 { 78 79 siginfo = 1; 80 } 81 82 int 83 main(int argc, char **argv) 84 { 85 FTS *ftsp; 86 FTSENT *p; 87 int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag; 88 int ch, fts_options, rval; 89 char *cp; 90 91 ischown = (strcmp(basename(argv[0]), "chown") == 0); 92 93 Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0; 94 while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1) 95 switch (ch) { 96 case 'H': 97 Hflag = 1; 98 Lflag = 0; 99 break; 100 case 'L': 101 Lflag = 1; 102 Hflag = 0; 103 break; 104 case 'P': 105 Hflag = Lflag = 0; 106 break; 107 case 'R': 108 Rflag = 1; 109 break; 110 case 'f': 111 fflag = 1; 112 break; 113 case 'h': 114 hflag = 1; 115 break; 116 case 'v': 117 vflag++; 118 break; 119 case 'x': 120 xflag = 1; 121 break; 122 case '?': 123 default: 124 usage(); 125 } 126 argv += optind; 127 argc -= optind; 128 129 if (argc < 2) 130 usage(); 131 132 (void)signal(SIGINFO, siginfo_handler); 133 134 if (Rflag) { 135 if (hflag && (Hflag || Lflag)) 136 errx(1, "the -R%c and -h options may not be " 137 "specified together", Hflag ? 'H' : 'L'); 138 if (Lflag) { 139 fts_options = FTS_LOGICAL; 140 } else { 141 fts_options = FTS_PHYSICAL; 142 143 if (Hflag) { 144 fts_options |= FTS_COMFOLLOW; 145 } 146 } 147 } else if (hflag) { 148 fts_options = FTS_PHYSICAL; 149 } else { 150 fts_options = FTS_LOGICAL; 151 } 152 153 if (xflag) 154 fts_options |= FTS_XDEV; 155 156 uid = (uid_t)-1; 157 gid = (gid_t)-1; 158 if (ischown) { 159 if ((cp = strchr(*argv, ':')) != NULL) { 160 *cp++ = '\0'; 161 a_gid(cp); 162 } 163 #ifdef SUPPORT_DOT 164 else if ((cp = strchr(*argv, '.')) != NULL) { 165 warnx("separation of user and group with a period is deprecated"); 166 *cp++ = '\0'; 167 a_gid(cp); 168 } 169 #endif 170 a_uid(*argv); 171 } else 172 a_gid(*argv); 173 174 if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL) 175 err(1, NULL); 176 177 for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 178 int atflag; 179 180 if ((fts_options & FTS_LOGICAL) || 181 ((fts_options & FTS_COMFOLLOW) && 182 p->fts_level == FTS_ROOTLEVEL)) 183 atflag = 0; 184 else 185 atflag = AT_SYMLINK_NOFOLLOW; 186 187 switch (p->fts_info) { 188 case FTS_D: /* Change it at FTS_DP. */ 189 if (!Rflag) 190 fts_set(ftsp, p, FTS_SKIP); 191 continue; 192 case FTS_DNR: /* Warn, chown. */ 193 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 194 rval = 1; 195 break; 196 case FTS_ERR: /* Warn, continue. */ 197 case FTS_NS: 198 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 199 rval = 1; 200 continue; 201 default: 202 break; 203 } 204 if (siginfo) { 205 print_info(p, 2); 206 siginfo = 0; 207 } 208 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) && 209 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid)) 210 continue; 211 if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag) 212 == -1 && !fflag) { 213 chownerr(p->fts_path); 214 rval = 1; 215 } else if (vflag) 216 print_info(p, vflag); 217 } 218 if (errno) 219 err(1, "fts_read"); 220 exit(rval); 221 } 222 223 static void 224 a_gid(const char *s) 225 { 226 struct group *gr; 227 228 if (*s == '\0') /* Argument was "uid[:.]". */ 229 return; 230 gname = s; 231 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group"); 232 } 233 234 static void 235 a_uid(const char *s) 236 { 237 struct passwd *pw; 238 239 if (*s == '\0') /* Argument was "[:.]gid". */ 240 return; 241 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user"); 242 } 243 244 static uid_t 245 id(const char *name, const char *type) 246 { 247 uid_t val; 248 char *ep; 249 250 /* 251 * XXX 252 * We know that uid_t's and gid_t's are unsigned longs. 253 */ 254 errno = 0; 255 val = strtoul(name, &ep, 10); 256 if (errno || *ep != '\0') 257 errx(1, "%s: illegal %s name", name, type); 258 return (val); 259 } 260 261 static void 262 chownerr(const char *file) 263 { 264 static uid_t euid = -1; 265 static int ngroups = -1; 266 static long ngroups_max; 267 gid_t *groups; 268 269 /* Check for chown without being root. */ 270 if (errno != EPERM || (uid != (uid_t)-1 && 271 euid == (uid_t)-1 && (euid = geteuid()) != 0)) { 272 warn("%s", file); 273 return; 274 } 275 276 /* Check group membership; kernel just returns EPERM. */ 277 if (gid != (gid_t)-1 && ngroups == -1 && 278 euid == (uid_t)-1 && (euid = geteuid()) != 0) { 279 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; 280 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL) 281 err(1, "malloc"); 282 ngroups = getgroups(ngroups_max, groups); 283 while (--ngroups >= 0 && gid != groups[ngroups]); 284 free(groups); 285 if (ngroups < 0) { 286 warnx("you are not a member of group %s", gname); 287 return; 288 } 289 } 290 warn("%s", file); 291 } 292 293 static void 294 usage(void) 295 { 296 297 if (ischown) 298 (void)fprintf(stderr, "%s\n%s\n", 299 "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]" 300 " file ...", 301 " chown [-fhvx] [-R [-H | -L | -P]] :group file ..."); 302 else 303 (void)fprintf(stderr, "%s\n", 304 "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ..."); 305 exit(1); 306 } 307 308 static void 309 print_info(const FTSENT *p, int vflag) 310 { 311 312 printf("%s", p->fts_path); 313 if (vflag > 1) { 314 if (ischown) { 315 printf(": %ju:%ju -> %ju:%ju", 316 (uintmax_t)p->fts_statp->st_uid, 317 (uintmax_t)p->fts_statp->st_gid, 318 (uid == (uid_t)-1) ? 319 (uintmax_t)p->fts_statp->st_uid : (uintmax_t)uid, 320 (gid == (gid_t)-1) ? 321 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid); 322 } else { 323 printf(": %ju -> %ju", (uintmax_t)p->fts_statp->st_gid, 324 (gid == (gid_t)-1) ? 325 (uintmax_t)p->fts_statp->st_gid : (uintmax_t)gid); 326 } 327 } 328 printf("\n"); 329 } 330