1 /* vi: set sw=4 ts=4: */
3 * Mini chown implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "chown (7.2 kb)"
13 //config: chown is used to change the user and/or group ownership
16 //config:config FEATURE_CHOWN_LONG_OPTIONS
17 //config: bool "Enable long options"
19 //config: depends on CHOWN && LONG_OPTS
21 //applet:IF_CHOWN(APPLET_NOEXEC(chown, chown, BB_DIR_BIN, BB_SUID_DROP, chown))
23 //kbuild:lib-$(CONFIG_CHOWN) += chown.o
25 /* BB_AUDIT SUSv3 defects - none? */
26 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
28 //usage:#define chown_trivial_usage
29 //usage: "[-Rh"IF_DESKTOP("LHPcvf")"]... USER[:[GRP]] FILE..."
30 //usage:#define chown_full_usage "\n\n"
31 //usage: "Change the owner and/or group of each FILE to USER and/or GRP\n"
32 //usage: "\n -R Recurse"
33 //usage: "\n -h Affect symlinks instead of symlink targets"
35 //usage: "\n -L Traverse all symlinks to directories"
36 //usage: "\n -H Traverse symlinks on command line only"
37 //usage: "\n -P Don't traverse symlinks (default)"
38 //usage: "\n -c List changed files"
39 //usage: "\n -v List all files"
40 //usage: "\n -f Hide errors"
43 //usage:#define chown_example_usage
44 //usage: "$ ls -l /tmp/foo\n"
45 //usage: "-r--r--r-- 1 andersen andersen 0 Apr 12 18:25 /tmp/foo\n"
46 //usage: "$ chown root /tmp/foo\n"
47 //usage: "$ ls -l /tmp/foo\n"
48 //usage: "-r--r--r-- 1 root andersen 0 Apr 12 18:25 /tmp/foo\n"
49 //usage: "$ chown root.root /tmp/foo\n"
50 //usage: "ls -l /tmp/foo\n"
51 //usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
55 /* This is a NOEXEC applet. Be very careful! */
58 #define OPT_STR "Rh" IF_DESKTOP("vcfLHP")
60 #define OPT_RECURSE (opt & 1)
61 #define OPT_NODEREF (opt & 2)
62 #define OPT_VERBOSE (IF_DESKTOP(opt & 0x04) IF_NOT_DESKTOP(0))
63 #define OPT_CHANGED (IF_DESKTOP(opt & 0x08) IF_NOT_DESKTOP(0))
64 #define OPT_QUIET (IF_DESKTOP(opt & 0x10) IF_NOT_DESKTOP(0))
66 * -L traverse every symbolic link to a directory encountered
67 * -H if a command line argument is a symbolic link to a directory, traverse it
68 * -P do not traverse any symbolic links (default)
69 * We do not conform to the following:
70 * "Specifying more than one of -H, -L, and -P is not an error.
71 * The last option specified shall determine the behavior of the utility." */
73 #define BIT_TRAVERSE 0x20
74 #define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_DESKTOP(0))
76 #define BIT_TRAVERSE_TOP (0x20|0x40)
77 #define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_DESKTOP(0))
79 #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
80 static const char chown_longopts[] ALIGN1 =
81 "recursive\0" No_argument "R"
82 "dereference\0" No_argument "\xff"
83 "no-dereference\0" No_argument "h"
85 "changes\0" No_argument "c"
86 "silent\0" No_argument "f"
87 "quiet\0" No_argument "f"
88 "verbose\0" No_argument "v"
93 typedef int (*chown_fptr)(const char *, uid_t, gid_t);
96 struct bb_uidgid_t ugid;
97 chown_fptr chown_func;
100 static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
101 void *vparam, int depth UNUSED_PARAM)
103 #define param (*(struct param_t*)vparam)
104 #define opt option_mask32
105 uid_t u = (param.ugid.uid == (uid_t)-1L) ? statbuf->st_uid : param.ugid.uid;
106 gid_t g = (param.ugid.gid == (gid_t)-1L) ? statbuf->st_gid : param.ugid.gid;
108 if (param.chown_func(fileName, u, g) == 0) {
110 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
112 printf("changed ownership of '%s' to %u:%u\n",
113 fileName, (unsigned)u, (unsigned)g);
118 bb_simple_perror_msg(fileName);
124 int chown_main(int argc UNUSED_PARAM, char **argv)
126 int retval = EXIT_SUCCESS;
128 struct param_t param;
130 #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
131 opt = getopt32long(argv, "^" OPT_STR "\0" "-2", chown_longopts);
133 opt = getopt32(argv, "^" OPT_STR "\0" "-2");
137 /* This matches coreutils behavior (almost - see below) */
138 param.chown_func = chown;
140 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
141 IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
143 param.chown_func = lchown;
146 flags = ACTION_DEPTHFIRST; /* match coreutils order */
148 flags |= ACTION_RECURSE;
149 if (OPT_TRAVERSE_TOP)
150 flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
152 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
154 parse_chown_usergroup_or_die(¶m.ugid, argv[0]);
156 /* Ok, ready to do the deed now */
158 if (!recursive_action(*argv,
160 fileAction, /* file action */
161 fileAction, /* dir action */
162 ¶m, /* user data */
165 retval = EXIT_FAILURE;
173 Testcase. Run in empty directory.
176 t1="/tmp/busybox chown"
189 ln -s ../up dir/linkup
190 ln -s ../dir2 dir/linkupdir2
197 echo "[$1]" >>test1.out
198 echo "[$1]" >>test2.out
199 (cd test1; $t1 $1) >>test1.out 2>&1
200 (cd test2; $t2 $1) >>test2.out 2>&1
201 (cd test1; ls -lnR) >out1
202 (cd test2; ls -lnR) >out2
203 echo "chown $1" >out.diff
204 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
211 tst "$1 1:1 linkfile"
213 echo "If script produced 'out.diff' file, then at least one testcase failed"
216 # These match coreutils 6.8:
227 # Fix `name' in coreutils output
228 sed 's/`/'"'"'/g' -i test2.out
229 # Compare us with coreutils output
230 diff -u test1.out test2.out