Unbreak buildkernel.
[dragonfly.git] / usr.sbin / chown / chown.c
blob6b80a171c65063d0c2d2b636f6ccf38864f0e9ce
1 /*
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
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. 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
27 * SUCH DAMAGE.
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: head/usr.sbin/chown/chown.c 282208 2015-04-29 00:49:00Z smh $
34 #include <sys/param.h>
35 #include <sys/stat.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <fts.h>
41 #include <grp.h>
42 #include <libgen.h>
43 #include <pwd.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
50 static void a_gid(const char *);
51 static void a_uid(const char *);
52 static void chownerr(const char *);
53 static uid_t id(const char *, const char *);
54 static void usage(void);
56 static uid_t uid;
57 static gid_t gid;
58 static int ischown;
59 static const char *gname;
61 int
62 main(int argc, char **argv)
64 FTS *ftsp;
65 FTSENT *p;
66 int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
67 int ch, fts_options, rval;
68 char *cp;
70 ischown = (strcmp(basename(argv[0]), "chown") == 0);
72 Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
73 while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
74 switch (ch) {
75 case 'H':
76 Hflag = 1;
77 Lflag = 0;
78 break;
79 case 'L':
80 Lflag = 1;
81 Hflag = 0;
82 break;
83 case 'P':
84 Hflag = Lflag = 0;
85 break;
86 case 'R':
87 Rflag = 1;
88 break;
89 case 'f':
90 fflag = 1;
91 break;
92 case 'h':
93 hflag = 1;
94 break;
95 case 'v':
96 vflag++;
97 break;
98 case 'x':
99 xflag = 1;
100 break;
101 default:
102 usage();
104 argv += optind;
105 argc -= optind;
107 if (argc < 2)
108 usage();
110 if (Rflag) {
111 if (hflag && (Hflag || Lflag))
112 errx(1, "the -R%c and -h options may not be "
113 "specified together", Hflag ? 'H' : 'L');
114 if (Lflag) {
115 fts_options = FTS_LOGICAL;
116 } else {
117 fts_options = FTS_PHYSICAL;
119 if (Hflag) {
120 fts_options |= FTS_COMFOLLOW;
123 } else if (hflag) {
124 fts_options = FTS_PHYSICAL;
125 } else {
126 fts_options = FTS_LOGICAL;
129 if (xflag)
130 fts_options |= FTS_XDEV;
132 uid = (uid_t)-1;
133 gid = (gid_t)-1;
134 if (ischown) {
135 if ((cp = strchr(*argv, ':')) != NULL) {
136 *cp++ = '\0';
137 a_gid(cp);
139 #ifdef SUPPORT_DOT
140 else if ((cp = strchr(*argv, '.')) != NULL) {
141 warnx("separation of user and group with a period is deprecated");
142 *cp++ = '\0';
143 a_gid(cp);
145 #endif
146 a_uid(*argv);
147 } else
148 a_gid(*argv);
150 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
151 err(1, NULL);
153 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
154 int atflag;
156 if ((fts_options & FTS_LOGICAL) ||
157 ((fts_options & FTS_COMFOLLOW) &&
158 p->fts_level == FTS_ROOTLEVEL))
159 atflag = 0;
160 else
161 atflag = AT_SYMLINK_NOFOLLOW;
163 switch (p->fts_info) {
164 case FTS_D: /* Change it at FTS_DP. */
165 if (!Rflag)
166 fts_set(ftsp, p, FTS_SKIP);
167 continue;
168 case FTS_DNR: /* Warn, chown. */
169 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
170 rval = 1;
171 break;
172 case FTS_ERR: /* Warn, continue. */
173 case FTS_NS:
174 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
175 rval = 1;
176 continue;
177 default:
178 break;
180 if ((uid == (uid_t)(-1) || uid == p->fts_statp->st_uid) &&
181 (gid == (gid_t)(-1) || gid == p->fts_statp->st_gid))
182 continue;
183 if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
184 == -1 && !fflag) {
185 chownerr(p->fts_path);
186 rval = 1;
187 } else if (vflag) {
188 printf("%s", p->fts_path);
189 if (vflag > 1) {
190 if (ischown) {
191 printf(": %ju:%ju -> %ju:%ju",
192 (uintmax_t)
193 p->fts_statp->st_uid,
194 (uintmax_t)
195 p->fts_statp->st_gid,
196 (uid == (uid_t)(-1)) ?
197 (uintmax_t)
198 p->fts_statp->st_uid :
199 (uintmax_t)uid,
200 (gid == (gid_t)(-1)) ?
201 (uintmax_t)
202 p->fts_statp->st_gid :
203 (uintmax_t)gid);
204 } else {
205 printf(": %ju -> %ju",
206 (uintmax_t)
207 p->fts_statp->st_gid,
208 (gid == (gid_t)(-1)) ?
209 (uintmax_t)
210 p->fts_statp->st_gid :
211 (uintmax_t)gid);
214 printf("\n");
217 if (errno)
218 err(1, "fts_read");
219 exit(rval);
222 static void
223 a_gid(const char *s)
225 struct group *gr;
227 if (*s == '\0') /* Argument was "uid[:.]". */
228 return;
229 gname = s;
230 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
233 static void
234 a_uid(const char *s)
236 struct passwd *pw;
238 if (*s == '\0') /* Argument was "[:.]gid". */
239 return;
240 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
243 static uid_t
244 id(const char *name, const char *type)
246 uid_t val;
247 char *ep;
250 * XXX
251 * We know that uid_t's and gid_t's are unsigned longs.
253 errno = 0;
254 val = strtoul(name, &ep, 10);
255 if (errno || *ep != '\0')
256 errx(1, "%s: illegal %s name", name, type);
257 return (val);
260 static void
261 chownerr(const char *file)
263 static uid_t euid = -1;
264 static int ngroups = -1;
265 static long ngroups_max;
266 gid_t *groups;
268 /* Check for chown without being root. */
269 if (errno != EPERM || (uid != (uid_t)(-1) &&
270 euid == (uid_t)(-1) && (euid = geteuid()) != 0)) {
271 warn("%s", file);
272 return;
275 /* Check group membership; kernel just returns EPERM. */
276 if (gid != (gid_t)(-1) && ngroups == -1 &&
277 euid == (uid_t)(-1) && (euid = geteuid()) != 0) {
278 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
279 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
280 err(1, "malloc");
281 ngroups = getgroups(ngroups_max, groups);
282 while (--ngroups >= 0 && gid != groups[ngroups]);
283 free(groups);
284 if (ngroups < 0) {
285 warnx("you are not a member of group %s", gname);
286 return;
289 warn("%s", file);
292 static void
293 usage(void)
296 if (ischown)
297 fprintf(stderr, "%s\n%s\n",
298 "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
299 " file ...",
300 " chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
301 else
302 fprintf(stderr, "%s\n",
303 "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
304 exit(1);