2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)chown.c 8.8 (Berkeley) 4/4/94
31 * $FreeBSD: src/usr.sbin/chown/chown.c,v 1.15.2.3 2002/08/07 21:24:33 schweikh Exp $
32 * $DragonFly: src/usr.sbin/chown/chown.c,v 1.8 2004/12/18 22:48:02 swildner Exp $
35 #include <sys/param.h>
50 static void a_gid(char *);
51 static void a_uid(char *);
52 static void chownerr(const char *);
53 static uid_t id(const char *, const char *);
54 static void usage(void);
58 static int Rflag, ischown, fflag, vflag;
59 static char *gname, *myname;
62 main(int argc, char **argv)
66 int Hflag, Lflag, Pflag, ch, fts_options, hflag, rval;
69 myname = (cp = strrchr(*argv, '/')) ? cp + 1 : *argv;
70 ischown = myname[2] == 'o';
72 Hflag = Lflag = Pflag = hflag = vflag = 0;
73 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
108 fts_options = FTS_PHYSICAL;
110 if (hflag && (Lflag || Hflag))
111 errx(1, "the -R and -h options may not be specified together");
113 fts_options |= FTS_COMFOLLOW;
115 fts_options &= ~FTS_PHYSICAL;
116 fts_options |= FTS_LOGICAL;
122 if ((cp = strchr(*argv, ':')) != NULL) {
127 else if ((cp = strchr(*argv, '.')) != NULL) {
128 warnx("separation of user and group with a period is deprecated");
137 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
140 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
141 switch (p->fts_info) {
142 case FTS_D: /* Change it at FTS_DP. */
144 fts_set(ftsp, p, FTS_SKIP);
146 case FTS_DNR: /* Warn, chown, continue. */
147 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
150 case FTS_ERR: /* Warn, continue. */
152 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
155 case FTS_SL: /* Ignore. */
158 * The only symlinks that end up here are ones that
159 * don't point to anything and ones that we found
160 * doing a physical walk.
169 if ((uid == (uid_t)(-1) || uid == p->fts_statp->st_uid) &&
170 (gid == (gid_t)(-1) || gid == p->fts_statp->st_gid))
173 if (lchown(p->fts_accpath, uid, gid) && !fflag) {
174 chownerr(p->fts_path);
178 printf("%s\n", p->fts_accpath);
181 if (chown(p->fts_accpath, uid, gid) && !fflag) {
182 chownerr(p->fts_path);
186 printf("%s", p->fts_accpath);
189 printf(": %d:%d -> %d:%d",
190 (int)p->fts_statp->st_uid,
191 (int)p->fts_statp->st_gid,
193 (int)p->fts_statp->st_uid :
196 (int) p->fts_statp->st_gid :
200 (int)p->fts_statp->st_gid,
202 (int)p->fts_statp->st_gid :
221 if (*s == '\0') /* Argument was "uid[:.]". */
224 gid = ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid;
232 if (*s == '\0') /* Argument was "[:.]gid". */
234 uid = ((pw = getpwnam(s)) == NULL) ? id(s, "user") : pw->pw_uid;
238 id(const char *name, const char *type)
245 * We know that uid_t's and gid_t's are unsigned longs.
248 val = strtoul(name, &ep, 10);
252 errx(1, "%s: illegal %s name", name, type);
257 chownerr(const char *file)
259 static uid_t euid = -1;
260 static int ngroups = -1;
261 gid_t groups[NGROUPS];
263 /* Check for chown without being root. */
264 if (errno != EPERM ||
265 (uid != (uid_t)(-1) && euid == (uid_t)(-1) && (euid = geteuid()) != 0))
268 /* Check group membership; kernel just returns EPERM. */
269 if (gid != (gid_t)(-1) && ngroups == -1 &&
270 euid == (uid_t)(-1) && (euid = geteuid()) != 0) {
271 ngroups = getgroups(NGROUPS, groups);
272 while (--ngroups >= 0 && gid != groups[ngroups]);
274 errx(1, "you are not a member of group %s", gname);
282 fprintf(stderr, "%s\n%s\n%s\n",
283 "usage: chown [-R [-H | -L | -P]] [-f] [-h] [-v] owner[:group] file ...",
284 " chown [-R [-H | -L | -P]] [-f] [-h] [-v] :group file ...",
285 " chgrp [-R [-H | -L | -P]] [-f] [-h] [-v] group file ...");