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
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.
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
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 $
36 * $DragonFly: src/usr.sbin/chown/chown.c,v 1.8 2004/12/18 22:48:02 swildner Exp $
39 #include <sys/param.h>
54 static void a_gid(char *);
55 static void a_uid(char *);
56 static void chownerr(const char *);
57 static uid_t
id(const char *, const char *);
58 static void usage(void);
62 static int Rflag
, ischown
, fflag
, vflag
;
63 static char *gname
, *myname
;
66 main(int argc
, char **argv
)
70 int Hflag
, Lflag
, Pflag
, ch
, fts_options
, hflag
, rval
;
73 myname
= (cp
= strrchr(*argv
, '/')) ? cp
+ 1 : *argv
;
74 ischown
= myname
[2] == 'o';
76 Hflag
= Lflag
= Pflag
= hflag
= vflag
= 0;
77 while ((ch
= getopt(argc
, argv
, "HLPRfhv")) != -1)
112 fts_options
= FTS_PHYSICAL
;
114 if (hflag
&& (Lflag
|| Hflag
))
115 errx(1, "the -R and -h options may not be specified together");
117 fts_options
|= FTS_COMFOLLOW
;
119 fts_options
&= ~FTS_PHYSICAL
;
120 fts_options
|= FTS_LOGICAL
;
126 if ((cp
= strchr(*argv
, ':')) != NULL
) {
131 else if ((cp
= strchr(*argv
, '.')) != NULL
) {
132 warnx("separation of user and group with a period is deprecated");
141 if ((ftsp
= fts_open(++argv
, fts_options
, 0)) == NULL
)
144 for (rval
= 0; (p
= fts_read(ftsp
)) != NULL
;) {
145 switch (p
->fts_info
) {
146 case FTS_D
: /* Change it at FTS_DP. */
148 fts_set(ftsp
, p
, FTS_SKIP
);
150 case FTS_DNR
: /* Warn, chown, continue. */
151 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
154 case FTS_ERR
: /* Warn, continue. */
156 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
159 case FTS_SL
: /* Ignore. */
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.
173 if ((uid
== (uid_t
)(-1) || uid
== p
->fts_statp
->st_uid
) &&
174 (gid
== (gid_t
)(-1) || gid
== p
->fts_statp
->st_gid
))
177 if (lchown(p
->fts_accpath
, uid
, gid
) && !fflag
) {
178 chownerr(p
->fts_path
);
182 printf("%s\n", p
->fts_accpath
);
185 if (chown(p
->fts_accpath
, uid
, gid
) && !fflag
) {
186 chownerr(p
->fts_path
);
190 printf("%s", p
->fts_accpath
);
193 printf(": %d:%d -> %d:%d",
194 (int)p
->fts_statp
->st_uid
,
195 (int)p
->fts_statp
->st_gid
,
197 (int)p
->fts_statp
->st_uid
:
200 (int) p
->fts_statp
->st_gid
:
204 (int)p
->fts_statp
->st_gid
,
206 (int)p
->fts_statp
->st_gid
:
225 if (*s
== '\0') /* Argument was "uid[:.]". */
228 gid
= ((gr
= getgrnam(s
)) == NULL
) ? id(s
, "group") : gr
->gr_gid
;
236 if (*s
== '\0') /* Argument was "[:.]gid". */
238 uid
= ((pw
= getpwnam(s
)) == NULL
) ? id(s
, "user") : pw
->pw_uid
;
242 id(const char *name
, const char *type
)
249 * We know that uid_t's and gid_t's are unsigned longs.
252 val
= strtoul(name
, &ep
, 10);
256 errx(1, "%s: illegal %s name", name
, type
);
261 chownerr(const char *file
)
263 static uid_t euid
= -1;
264 static int ngroups
= -1;
265 gid_t groups
[NGROUPS
];
267 /* Check for chown without being root. */
268 if (errno
!= EPERM
||
269 (uid
!= (uid_t
)(-1) && euid
== (uid_t
)(-1) && (euid
= geteuid()) != 0))
272 /* Check group membership; kernel just returns EPERM. */
273 if (gid
!= (gid_t
)(-1) && ngroups
== -1 &&
274 euid
== (uid_t
)(-1) && (euid
= geteuid()) != 0) {
275 ngroups
= getgroups(NGROUPS
, groups
);
276 while (--ngroups
>= 0 && gid
!= groups
[ngroups
]);
278 errx(1, "you are not a member of group %s", gname
);
286 fprintf(stderr
, "%s\n%s\n%s\n",
287 "usage: chown [-R [-H | -L | -P]] [-f] [-h] [-v] owner[:group] file ...",
288 " chown [-R [-H | -L | -P]] [-f] [-h] [-v] :group file ...",
289 " chgrp [-R [-H | -L | -P]] [-f] [-h] [-v] group file ...");