15d2faeae90f5691c951cdbb7f66690a5aebf054
[oweals/busybox.git] / coreutils / chown.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini chown implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 defects - none? */
11 /* BB_AUDIT GNU defects - unsupported long options. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
13
14 #include "busybox.h"
15
16 static struct bb_uidgid_t ugid = { -1, -1 };
17
18 static int (*chown_func)(const char *, uid_t, gid_t) = chown;
19
20 #define OPT_STR     ("Rh" USE_DESKTOP("vcfLHP"))
21 #define BIT_RECURSE 1
22 #define OPT_RECURSE (option_mask32 & 1)
23 #define OPT_NODEREF (option_mask32 & 2)
24 #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0))
25 #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0))
26 #define OPT_QUIET   (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
27 /* POSIX options
28  * -L traverse every symbolic link to a directory encountered
29  * -H if a command line argument is a symbolic link to a directory, traverse it
30  * -P do not traverse any symbolic links (default)
31  * We do not conform to the following:
32  * "Specifying more than one of -H, -L, and -P is not an error.
33  * The last option specified shall determine the behavior of the utility." */
34 /* -L */
35 #define BIT_TRAVERSE 0x20
36 #define OPT_TRAVERSE (USE_DESKTOP(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0))
37 /* -H or -L */
38 #define BIT_TRAVERSE_TOP (0x20|0x40)
39 #define OPT_TRAVERSE_TOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0))
40
41 static int fileAction(const char *fileName, struct stat *statbuf,
42                 void ATTRIBUTE_UNUSED *junk, int depth)
43 {
44         uid_t u = (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid;
45         gid_t g = (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid;
46
47         if (!chown_func(fileName, u, g)) {
48                 if (OPT_VERBOSE
49                  || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
50                 ) {
51                         printf("changed ownership of '%s' to %u:%u\n",
52                                         fileName, (unsigned)u, (unsigned)g);
53                 }
54                 return TRUE;
55         }
56         if (!OPT_QUIET)
57                 bb_perror_msg("%s", fileName);  /* A filename can have % in it... */
58         return FALSE;
59 }
60
61 int chown_main(int argc, char **argv);
62 int chown_main(int argc, char **argv)
63 {
64         int retval = EXIT_SUCCESS;
65
66         opt_complementary = "-2";
67         getopt32(argc, argv, OPT_STR);
68         argv += optind;
69
70         /* This matches coreutils behavior (almost - see below) */
71         if (OPT_NODEREF
72             /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
73             USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
74         ) {
75                 chown_func = lchown;
76         }
77
78         parse_chown_usergroup_or_die(&ugid, argv[0]);
79
80         /* Ok, ready to do the deed now */
81         argv++;
82         do {
83                 char *arg = *argv;
84
85                 if (OPT_TRAVERSE_TOP) {
86                         /* resolves symlink (even recursive) */
87                         arg = xmalloc_realpath(arg);
88                         if (!arg)
89                                 continue;
90                 }
91
92                 if (!recursive_action(arg,
93                                 OPT_RECURSE,    // recurse
94                                 OPT_TRAVERSE,   // follow links if -L
95                                 FALSE,          // depth first
96                                 fileAction,     // file action
97                                 fileAction,     // dir action
98                                 NULL,           // user data
99                                 0)              // depth
100                 ) {
101                         retval = EXIT_FAILURE;
102                 }
103
104                 if (OPT_TRAVERSE_TOP)
105                         free(arg);
106         } while (*++argv);
107
108         return retval;
109 }
110
111 /*
112 Testcase. Run in empty directory.
113
114 #!/bin/sh
115 t1="/tmp/busybox chown"
116 t2="/usr/bin/chown"
117 create() {
118     rm -rf $1; mkdir $1
119     (
120     cd $1 || exit 1
121     mkdir dir dir2
122     >up
123     >file
124     >dir/file
125     >dir2/file
126     ln -s dir linkdir
127     ln -s file linkfile
128     ln -s ../up dir/linkup
129     ln -s ../dir2 dir/linkupdir2
130     )
131     chown -R 0:0 $1
132 }
133 tst() {
134     create test1
135     create test2
136     (cd test1; $t1 $1)
137     (cd test2; $t2 $1)
138     (cd test1; ls -lnR) >out1
139     (cd test2; ls -lnR) >out2
140     echo "chown $1" >out.diff
141     if ! diff -u out1 out2 >>out.diff; then exit 1; fi
142     rm out.diff
143 }
144 tst_for_each() {
145     tst "$1 1:1 file"
146     tst "$1 1:1 dir"
147     tst "$1 1:1 linkdir"
148     tst "$1 1:1 linkfile"
149 }
150 echo "If script produced 'out.diff' file, then at least one testcase failed"
151 # These match coreutils 6.8:
152 tst_for_each ""
153 tst_for_each "-R"
154 tst_for_each "-RP"
155 tst_for_each "-RL"
156 tst_for_each "-RH"
157 tst_for_each "-h"
158 tst_for_each "-hR"
159 tst_for_each "-hRP"
160 # Below: with "chown linkdir" coreutils 6.8 will chown linkdir _target_,
161 # we lchown _the link_. I believe we are "more correct".
162 #tst_for_each "-hRL"
163 #tst_for_each "-hRH"
164
165 */