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 * 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.
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
32 static const char copyright[] =
33 "@(#) Copyright (c) 1988, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
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);
70 static const char *gname;
73 main(int argc, char **argv)
77 int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
78 int ch, fts_options, rval;
81 ischown = (strcmp(basename(argv[0]), "chown") == 0);
83 Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
84 while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
123 if (hflag && (Hflag || Lflag))
124 errx(1, "the -R%c and -h options may not be "
125 "specified together", Hflag ? 'H' : 'L');
127 fts_options = FTS_LOGICAL;
129 fts_options = FTS_PHYSICAL;
132 fts_options |= FTS_COMFOLLOW;
136 fts_options = FTS_PHYSICAL;
138 fts_options = FTS_LOGICAL;
142 fts_options |= FTS_XDEV;
147 if ((cp = strchr(*argv, ':')) != NULL) {
152 else if ((cp = strchr(*argv, '.')) != NULL) {
153 warnx("separation of user and group with a period is deprecated");
162 if ((ftsp = fts_open(++argv, fts_options, NULL)) == NULL)
165 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
168 if ((fts_options & FTS_LOGICAL) ||
169 ((fts_options & FTS_COMFOLLOW) &&
170 p->fts_level == FTS_ROOTLEVEL))
173 atflag = AT_SYMLINK_NOFOLLOW;
175 switch (p->fts_info) {
176 case FTS_D: /* Change it at FTS_DP. */
178 fts_set(ftsp, p, FTS_SKIP);
180 case FTS_DNR: /* Warn, chown. */
181 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
184 case FTS_ERR: /* Warn, continue. */
186 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
192 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
193 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
195 if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
197 chownerr(p->fts_path);
200 printf("%s", p->fts_path);
203 printf(": %ju:%ju -> %ju:%ju",
205 p->fts_statp->st_uid,
207 p->fts_statp->st_gid,
210 p->fts_statp->st_uid :
214 p->fts_statp->st_gid :
217 printf(": %ju -> %ju",
219 p->fts_statp->st_gid,
222 p->fts_statp->st_gid :
239 if (*s == '\0') /* Argument was "uid[:.]". */
242 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
250 if (*s == '\0') /* Argument was "[:.]gid". */
252 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
256 id(const char *name, const char *type)
263 * We know that uid_t's and gid_t's are unsigned longs.
266 val = strtoul(name, &ep, 10);
267 if (errno || *ep != '\0')
268 errx(1, "%s: illegal %s name", name, type);
273 chownerr(const char *file)
275 static uid_t euid = -1;
276 static int ngroups = -1;
277 static long ngroups_max;
280 /* Check for chown without being root. */
281 if (errno != EPERM || (uid != (uid_t)-1 &&
282 euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
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)
293 ngroups = getgroups(ngroups_max, groups);
294 while (--ngroups >= 0 && gid != groups[ngroups]);
297 warnx("you are not a member of group %s", gname);
309 (void)fprintf(stderr, "%s\n%s\n",
310 "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
312 " chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
314 (void)fprintf(stderr, "%s\n",
315 "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");