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