file_cmds-90.tar.gz
[apple/file_cmds.git] / chown / chown.c
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.
32 */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)chown.c 8.8 (Berkeley) 4/4/94";
43 #endif
44 #endif /* not lint */
45
46 #include <sys/cdefs.h>
47 __RCSID("$FreeBSD: src/usr.sbin/chown/chown.c,v 1.24 2002/07/17 16:22:24 dwmalone Exp $");
48
49 #include <sys/param.h>
50 #include <sys/stat.h>
51
52 #include <err.h>
53 #include <errno.h>
54 #include <fts.h>
55 #include <grp.h>
56 #include <pwd.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 void a_gid(const char *);
63 void a_uid(const char *);
64 void chownerr(const char *);
65 u_long id(const char *, const char *);
66 void usage(void);
67
68 uid_t uid;
69 gid_t gid;
70 int ischown;
71 const char *gname;
72
73 int
74 main(int argc, char **argv)
75 {
76 FTS *ftsp;
77 FTSENT *p;
78 int Hflag, Lflag, Rflag, fflag, hflag, vflag;
79 int ch, fts_options, rval;
80 char *cp;
81
82 cp = strrchr(argv[0], '/');
83 cp = (cp != NULL) ? cp + 1 : argv[0];
84 ischown = (strcmp(cp, "chown") == 0);
85
86 Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
87 #ifndef __APPLE__
88 while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
89 #else
90 while ((ch = getopt(argc, argv, "HLPRfv")) != -1)
91 #endif
92 switch (ch) {
93 case 'H':
94 Hflag = 1;
95 Lflag = 0;
96 break;
97 case 'L':
98 Lflag = 1;
99 Hflag = 0;
100 break;
101 case 'P':
102 Hflag = Lflag = 0;
103 break;
104 case 'R':
105 Rflag = 1;
106 break;
107 case 'f':
108 fflag = 1;
109 break;
110 #ifndef __APPLE__
111 case 'h':
112 hflag = 1;
113 break;
114 #endif
115 case 'v':
116 vflag = 1;
117 break;
118 case '?':
119 default:
120 usage();
121 }
122 argv += optind;
123 argc -= optind;
124
125 if (argc < 2)
126 usage();
127
128 if (Rflag) {
129 fts_options = FTS_PHYSICAL;
130 if (hflag && (Hflag || Lflag))
131 errx(1, "the -R%c and -h options may not be "
132 "specified together", Hflag ? 'H' : 'L');
133 if (Hflag)
134 fts_options |= FTS_COMFOLLOW;
135 else if (Lflag) {
136 fts_options &= ~FTS_PHYSICAL;
137 fts_options |= FTS_LOGICAL;
138 }
139 } else
140 fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
141
142 uid = (uid_t)-1;
143 gid = (gid_t)-1;
144 if (ischown) {
145 if ((cp = strchr(*argv, ':')) != NULL) {
146 *cp++ = '\0';
147 a_gid(cp);
148 }
149 #ifdef SUPPORT_DOT
150 else if ((cp = strchr(*argv, '.')) != NULL) {
151 warnx("separation of user and group with a period is deprecated");
152 *cp++ = '\0';
153 a_gid(cp);
154 }
155 #endif
156 a_uid(*argv);
157 } else
158 a_gid(*argv);
159
160 if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
161 err(1, NULL);
162
163 for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
164 switch (p->fts_info) {
165 case FTS_D: /* Change it at FTS_DP. */
166 if (!Rflag)
167 fts_set(ftsp, p, FTS_SKIP);
168 continue;
169 case FTS_DNR: /* Warn, chown. */
170 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
171 rval = 1;
172 break;
173 case FTS_ERR: /* Warn, continue. */
174 case FTS_NS:
175 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
176 rval = 1;
177 continue;
178 case FTS_SL:
179 case FTS_SLNONE:
180 /*
181 * The only symlinks that end up here are ones that
182 * don't point to anything and ones that we found
183 * doing a physical walk.
184 */
185 if (hflag)
186 break;
187 else
188 continue;
189 default:
190 break;
191 }
192 if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
193 (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
194 continue;
195 #ifndef __APPLE__
196 if ((hflag ? lchown : chown)(p->fts_accpath, uid, gid) == -1) {
197 #else
198 if (chown(p->fts_accpath, uid, gid) == -1) {
199 #endif /* __APPLE__ */
200 if (!fflag) {
201 chownerr(p->fts_path);
202 rval = 1;
203 }
204 } else {
205 if (vflag)
206 printf("%s\n", p->fts_path);
207 }
208 }
209 if (errno)
210 err(1, "fts_read");
211 exit(rval);
212 }
213
214 void
215 a_gid(const char *s)
216 {
217 struct group *gr;
218
219 if (*s == '\0') /* Argument was "uid[:.]". */
220 return;
221 gname = s;
222 gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
223 }
224
225 void
226 a_uid(const char *s)
227 {
228 struct passwd *pw;
229
230 if (*s == '\0') /* Argument was "[:.]gid". */
231 return;
232 uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
233 }
234
235 u_long
236 id(const char *name, const char *type)
237 {
238 u_long val;
239 char *ep;
240
241 /*
242 * XXX
243 * We know that uid_t's and gid_t's are unsigned longs.
244 */
245 errno = 0;
246 val = strtoul(name, &ep, 10);
247 if (errno)
248 err(1, "%s", name);
249 if (*ep != '\0')
250 errx(1, "%s: illegal %s name", name, type);
251 return (val);
252 }
253
254 void
255 chownerr(const char *file)
256 {
257 static uid_t euid = -1;
258 static int ngroups = -1;
259 gid_t groups[NGROUPS_MAX];
260
261 /* Check for chown without being root. */
262 if (errno != EPERM || (uid != (uid_t)-1 &&
263 euid == (uid_t)-1 && (euid = geteuid()) != 0)) {
264 warn("%s", file);
265 return;
266 }
267
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_MAX, groups);
272 while (--ngroups >= 0 && gid != groups[ngroups]);
273 if (ngroups < 0) {
274 warnx("you are not a member of group %s", gname);
275 return;
276 }
277 }
278 warn("%s", file);
279 }
280
281 void
282 usage(void)
283 {
284
285 if (ischown)
286 (void)fprintf(stderr, "%s\n%s\n",
287 "usage: chown [-fhv] [-R [-H | -L | -P]] owner[:group]"
288 " file ...",
289 " chown [-fhv] [-R [-H | -L | -P]] :group file ...");
290 else
291 (void)fprintf(stderr, "%s\n",
292 #ifndef __APPLE__
293 "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ...");
294 #else
295 "usage: chgrp [-fv] [-R [-H | -L | -P]] group file ...");
296 #endif
297 exit(1);
298 }