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