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>
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 *);
72 main(int argc, char **argv)
76 int Hflag, Lflag, Rflag, fflag, hflag, vflag;
77 int ch, fts_options, rval;
80 ischown = (strcmp(basename(argv[0]), "chown") == 0);
82 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
83 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
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');
124 fts_options |= FTS_COMFOLLOW;
126 fts_options &= ~FTS_PHYSICAL;
127 fts_options |= FTS_LOGICAL;
130 fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
135 if ((cp = strchr(*argv, ':')) != NULL) {
140 else if ((cp = strchr(*argv, '.')) != NULL) {
141 warnx("separation of user and group with a period is deprecated");
150 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
153 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
154 switch (p->fts_info) {
155 case FTS_D: /* Change it at FTS_DP. */
157 fts_set(ftsp, p, FTS_SKIP);
159 case FTS_DNR: /* Warn, chown. */
160 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
163 case FTS_ERR: /* Warn, continue. */
165 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
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.
182 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
183 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
185 if ((hflag ? lchown : chown)(p->fts_accpath, uid, gid) == -1) {
187 chownerr(p->fts_path);
192 printf("%s", p->fts_path);
195 printf(": %ju:%ju -> %ju:%ju",
197 p->fts_statp->st_uid,
199 p->fts_statp->st_gid,
202 p->fts_statp->st_uid :
206 p->fts_statp->st_gid :
209 printf(": %ju -> %ju",
211 p->fts_statp->st_gid,
214 p->fts_statp->st_gid :
232 if (*s == '\0') /* Argument was "uid[:.]". */
235 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
243 if (*s == '\0') /* Argument was "[:.]gid". */
245 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
249 id(const char *name, const char *type)
256 * We know that uid_t's and gid_t's are unsigned longs.
259 val = strtoul(name, &ep, 10);
263 errx(1, "%s: illegal %s name", name, type);
268 chownerr(const char *file)
270 static uid_t euid = -1;
271 static int ngroups = -1;
272 gid_t groups[NGROUPS_MAX];
274 /* Check for chown without being root. */
275 if (errno != EPERM || (uid != (uid_t)-1 &&
276 euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
281 /* Check group membership; kernel just returns EPERM. */
282 if (gid != (gid_t)-1 && ngroups == -1 &&
283 euid == (uid_t)-1 && (euid = geteuid()) != 0) {
284 ngroups = getgroups(NGROUPS_MAX, groups);
285 while (--ngroups >= 0 && gid != groups[ngroups]);
287 warnx("you are not a member of group %s", gname);
299 (void)fprintf(stderr, "%s\n%s\n",
300 "usage: chown [-fhv] [-R [-H | -L | -P]] owner[:group]"
302 " chown [-fhv] [-R [-H | -L | -P]] :group file ...");
304 (void)fprintf(stderr, "%s\n",
305 "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ...");