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