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.2 2003/06/17 04:29:52 dillon 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 __P((char *)); 55 void a_uid __P((char *)); 56 void chownerr __P((char *)); 57 u_long id __P((char *, char *)); 58 void usage __P((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(argc, argv) 67 int argc; 68 char *argv[]; 69 { 70 FTS *ftsp; 71 FTSENT *p; 72 int Hflag, Lflag, Pflag, ch, fts_options, hflag, rval; 73 char *cp; 74 75 myname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv; 76 ischown = myname[2] == 'o'; 77 78 Hflag = Lflag = Pflag = hflag = vflag = 0; 79 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1) 80 switch (ch) { 81 case 'H': 82 Hflag = 1; 83 Lflag = Pflag = 0; 84 break; 85 case 'L': 86 Lflag = 1; 87 Hflag = Pflag = 0; 88 break; 89 case 'P': 90 Pflag = 1; 91 Hflag = Lflag = 0; 92 break; 93 case 'R': 94 Rflag = 1; 95 break; 96 case 'f': 97 fflag = 1; 98 break; 99 case 'h': 100 hflag = 1; 101 break; 102 case 'v': 103 vflag = 1; 104 break; 105 case '?': 106 default: 107 usage(); 108 } 109 argv += optind; 110 argc -= optind; 111 112 if (argc < 2) 113 usage(); 114 115 fts_options = FTS_PHYSICAL; 116 if (Rflag) { 117 if (hflag && (Lflag || Hflag)) 118 errx(1, "the -R and -h options may not be specified together"); 119 if (Hflag) 120 fts_options |= FTS_COMFOLLOW; 121 if (Lflag) { 122 fts_options &= ~FTS_PHYSICAL; 123 fts_options |= FTS_LOGICAL; 124 } 125 } 126 127 uid = gid = -1; 128 if (ischown) { 129 if ((cp = strchr(*argv, ':')) != NULL) { 130 *cp++ = '\0'; 131 a_gid(cp); 132 } 133 #ifdef SUPPORT_DOT 134 else if ((cp = strchr(*argv, '.')) != NULL) { 135 warnx("separation of user and group with a period is deprecated"); 136 *cp++ = '\0'; 137 a_gid(cp); 138 } 139 #endif 140 a_uid(*argv); 141 } else 142 a_gid(*argv); 143 144 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) 145 err(1, NULL); 146 147 for (rval = 0; (p = fts_read(ftsp)) != NULL;) { 148 switch (p->fts_info) { 149 case FTS_D: /* Change it at FTS_DP. */ 150 if (!Rflag) 151 fts_set(ftsp, p, FTS_SKIP); 152 continue; 153 case FTS_DNR: /* Warn, chown, continue. */ 154 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 155 rval = 1; 156 break; 157 case FTS_ERR: /* Warn, continue. */ 158 case FTS_NS: 159 warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); 160 rval = 1; 161 continue; 162 case FTS_SL: /* Ignore. */ 163 case FTS_SLNONE: 164 /* 165 * The only symlinks that end up here are ones that 166 * don't point to anything and ones that we found 167 * doing a physical walk. 168 */ 169 if (hflag) 170 break; 171 else 172 continue; 173 default: 174 break; 175 } 176 if ((uid == -1 || uid == p->fts_statp->st_uid) && 177 (gid == -1 || gid == p->fts_statp->st_gid)) 178 continue; 179 if (hflag) { 180 if (lchown(p->fts_accpath, uid, gid) && !fflag) { 181 chownerr(p->fts_path); 182 rval = 1; 183 } else { 184 if (vflag) 185 (void)printf("%s\n", p->fts_accpath); 186 } 187 } else { 188 if (chown(p->fts_accpath, uid, gid) && !fflag) { 189 chownerr(p->fts_path); 190 rval = 1; 191 } else { 192 if (vflag) 193 (void)printf("%s\n", p->fts_accpath); 194 } 195 } 196 } 197 if (errno) 198 err(1, "fts_read"); 199 exit(rval); 200 } 201 202 void 203 a_gid(s) 204 char *s; 205 { 206 struct group *gr; 207 208 if (*s == '\0') /* Argument was "uid[:.]". */ 209 return; 210 gname = s; 211 gid = ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid; 212 } 213 214 void 215 a_uid(s) 216 char *s; 217 { 218 struct passwd *pw; 219 220 if (*s == '\0') /* Argument was "[:.]gid". */ 221 return; 222 uid = ((pw = getpwnam(s)) == NULL) ? id(s, "user") : pw->pw_uid; 223 } 224 225 u_long 226 id(name, type) 227 char *name, *type; 228 { 229 u_long val; 230 char *ep; 231 232 /* 233 * XXX 234 * We know that uid_t's and gid_t's are unsigned longs. 235 */ 236 errno = 0; 237 val = strtoul(name, &ep, 10); 238 if (errno) 239 err(1, "%s", name); 240 if (*ep != '\0') 241 errx(1, "%s: illegal %s name", name, type); 242 return (val); 243 } 244 245 void 246 chownerr(file) 247 char *file; 248 { 249 static int euid = -1, ngroups = -1; 250 gid_t groups[NGROUPS]; 251 252 /* Check for chown without being root. */ 253 if (errno != EPERM || 254 (uid != -1 && euid == -1 && (euid = geteuid()) != 0)) 255 err(1, "%s", file); 256 257 /* Check group membership; kernel just returns EPERM. */ 258 if (gid != -1 && ngroups == -1 && 259 euid == -1 && (euid = geteuid()) != 0) { 260 ngroups = getgroups(NGROUPS, groups); 261 while (--ngroups >= 0 && gid != groups[ngroups]); 262 if (ngroups < 0) 263 errx(1, "you are not a member of group %s", gname); 264 } 265 warn("%s", file); 266 } 267 268 void 269 usage() 270 { 271 (void)fprintf(stderr, "%s\n%s\n%s\n", 272 "usage: chown [-R [-H | -L | -P]] [-f] [-h] [-v] owner[:group] file ...", 273 " chown [-R [-H | -L | -P]] [-f] [-h] [-v] :group file ...", 274 " chgrp [-R [-H | -L | -P]] [-f] [-h] [-v] group file ..."); 275 exit(1); 276 } 277