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