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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California. All rights reserved. 34 * @(#)chown.c 8.8 (Berkeley) 4/4/94 35 * $FreeBSD: src/usr.sbin/chown/chown.c,v 1.15.2.3 2002/08/07 21:24:33 schweikh Exp $ 36 * $DragonFly: src/usr.sbin/chown/chown.c,v 1.4 2003/11/15 23:33:35 eirikn Exp $ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 42 #include <ctype.h> 43 #include <dirent.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <fts.h> 47 #include <grp.h> 48 #include <pwd.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 void a_gid(char *); 55 void a_uid(char *); 56 void chownerr(char *); 57 u_long id(char *, char *); 58 void usage(void); 59 60 uid_t uid; 61 gid_t gid; 62 int Rflag, ischown, fflag, hflag, vflag; 63 char *gname, *myname; 64 65 int 66 main(int argc, char **argv) 67 { 68 FTS *ftsp; 69 FTSENT *p; 70 int Hflag, Lflag, Pflag, ch, fts_options, hflag, rval; 71 char *cp; 72 73 myname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv; 74 ischown = myname[2] == 'o'; 75 76 Hflag = Lflag = Pflag = hflag = vflag = 0; 77 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1) 78 switch (ch) { 79 case 'H': 80 Hflag = 1; 81 Lflag = Pflag = 0; 82 break; 83 case 'L': 84 Lflag = 1; 85 Hflag = Pflag = 0; 86 break; 87 case 'P': 88 Pflag = 1; 89 Hflag = Lflag = 0; 90 break; 91 case 'R': 92 Rflag = 1; 93 break; 94 case 'f': 95 fflag = 1; 96 break; 97 case 'h': 98 hflag = 1; 99 break; 100 case 'v': 101 vflag = 1; 102 break; 103 case '?': 104 default: 105 usage(); 106 } 107 argv += optind; 108 argc -= optind; 109 110 if (argc < 2) 111 usage(); 112 113 fts_options = FTS_PHYSICAL; 114 if (Rflag) { 115 if (hflag && (Lflag || Hflag)) 116 errx(1, "the -R and -h options may not be specified together"); 117 if (Hflag) 118 fts_options |= FTS_COMFOLLOW; 119 if (Lflag) { 120 fts_options &= ~FTS_PHYSICAL; 121 fts_options |= FTS_LOGICAL; 122 } 123 } 124 125 uid = gid = -1; 126 if (ischown) { 127 if ((cp = strchr(*argv, ':')) != NULL) { 128 *cp++ = '\0'; 129 a_gid(cp); 130 } 131 #ifdef SUPPORT_DOT 132 else if ((cp = strchr(*argv, '.')) != NULL) { 133 warnx("separation of user and group with a period is deprecated"); 134 *cp++ = '\0'; 135 a_gid(cp); 136 } 137 #endif 138 a_uid(*argv); 139 } else 140 a_gid(*argv); 141 142 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) 143 err(1, NULL); 144 145 for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 146 switch (p->fts_info) { 147 case FTS_D: /* Change it at FTS_DP. */ 148 if (!Rflag) 149 fts_set(ftsp, p, FTS_SKIP); 150 continue; 151 case FTS_DNR: /* Warn, chown, continue. */ 152 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 153 rval = 1; 154 break; 155 case FTS_ERR: /* Warn, continue. */ 156 case FTS_NS: 157 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 158 rval = 1; 159 continue; 160 case FTS_SL: /* Ignore. */ 161 case FTS_SLNONE: 162 /* 163 * The only symlinks that end up here are ones that 164 * don't point to anything and ones that we found 165 * doing a physical walk. 166 */ 167 if (hflag) 168 break; 169 else 170 continue; 171 default: 172 break; 173 } 174 if ((uid == -1 || uid == p->fts_statp->st_uid) && 175 (gid == -1 || gid == p->fts_statp->st_gid)) 176 continue; 177 if (hflag) { 178 if (lchown(p->fts_accpath, uid, gid) && !fflag) { 179 chownerr(p->fts_path); 180 rval = 1; 181 } else { 182 if (vflag) 183 (void)printf("%s\n", p->fts_accpath); 184 } 185 } else { 186 if (chown(p->fts_accpath, uid, gid) && !fflag) { 187 chownerr(p->fts_path); 188 rval = 1; 189 } else { 190 if (vflag) 191 (void)printf("%s\n", p->fts_accpath); 192 } 193 } 194 } 195 if (errno) 196 err(1, "fts_read"); 197 exit(rval); 198 } 199 200 void 201 a_gid(char *s) 202 { 203 struct group *gr; 204 205 if (*s == '\0') /* Argument was "uid[:.]". */ 206 return; 207 gname = s; 208 gid = ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid; 209 } 210 211 void 212 a_uid(char *s) 213 { 214 struct passwd *pw; 215 216 if (*s == '\0') /* Argument was "[:.]gid". */ 217 return; 218 uid = ((pw = getpwnam(s)) == NULL) ? id(s, "user") : pw->pw_uid; 219 } 220 221 u_long 222 id(char *name, char *type) 223 { 224 u_long val; 225 char *ep; 226 227 /* 228 * XXX 229 * We know that uid_t's and gid_t's are unsigned longs. 230 */ 231 errno = 0; 232 val = strtoul(name, &ep, 10); 233 if (errno) 234 err(1, "%s", name); 235 if (*ep != '\0') 236 errx(1, "%s: illegal %s name", name, type); 237 return (val); 238 } 239 240 void 241 chownerr(char *file) 242 { 243 static int euid = -1, ngroups = -1; 244 gid_t groups[NGROUPS]; 245 246 /* Check for chown without being root. */ 247 if (errno != EPERM || 248 (uid != -1 && euid == -1 && (euid = geteuid()) != 0)) 249 err(1, "%s", file); 250 251 /* Check group membership; kernel just returns EPERM. */ 252 if (gid != -1 && ngroups == -1 && 253 euid == -1 && (euid = geteuid()) != 0) { 254 ngroups = getgroups(NGROUPS, groups); 255 while (--ngroups >= 0 && gid != groups[ngroups]); 256 if (ngroups < 0) 257 errx(1, "you are not a member of group %s", gname); 258 } 259 warn("%s", file); 260 } 261 262 void 263 usage(void) 264 { 265 (void)fprintf(stderr, "%s\n%s\n%s\n", 266 "usage: chown [-R [-H | -L | -P]] [-f] [-h] [-v] owner[:group] file ...", 267 " chown [-R [-H | -L | -P]] [-f] [-h] [-v] :group file ...", 268 " chgrp [-R [-H | -L | -P]] [-f] [-h] [-v] group file ..."); 269 exit(1); 270 } 271