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