Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3 | * Mini chown implementation for busybox |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //config:config CHOWN |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "chown (7.2 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: chown is used to change the user and/or group ownership |
| 14 | //config: of files. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 15 | //config: |
| 16 | //config:config FEATURE_CHOWN_LONG_OPTIONS |
| 17 | //config: bool "Enable long options" |
| 18 | //config: default y |
| 19 | //config: depends on CHOWN && LONG_OPTS |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 20 | |
| 21 | //applet:IF_CHOWN(APPLET_NOEXEC(chown, chown, BB_DIR_BIN, BB_SUID_DROP, chown)) |
| 22 | |
| 23 | //kbuild:lib-$(CONFIG_CHOWN) += chown.o |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 25 | /* BB_AUDIT SUSv3 defects - none? */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */ |
| 27 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 28 | //usage:#define chown_trivial_usage |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 29 | //usage: "[-Rh"IF_DESKTOP("LHPcvf")"]... USER[:[GRP]] FILE..." |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 30 | //usage:#define chown_full_usage "\n\n" |
Aaro Koskinen | 2735bc0 | 2015-10-16 17:24:46 +0200 | [diff] [blame] | 31 | //usage: "Change the owner and/or group of each FILE to USER and/or GRP\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 32 | //usage: "\n -R Recurse" |
| 33 | //usage: "\n -h Affect symlinks instead of symlink targets" |
Denys Vlasenko | 16ca379 | 2014-01-09 17:52:13 +0100 | [diff] [blame] | 34 | //usage: IF_DESKTOP( |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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)" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 38 | //usage: "\n -c List changed files" |
| 39 | //usage: "\n -v List all files" |
| 40 | //usage: "\n -f Hide errors" |
| 41 | //usage: ) |
| 42 | //usage: |
| 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" |
| 52 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 53 | #include "libbb.h" |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 55 | /* This is a NOEXEC applet. Be very careful! */ |
| 56 | |
| 57 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 58 | #define OPT_STR "Rh" IF_DESKTOP("vcfLHP") |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 59 | #define BIT_RECURSE 1 |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 60 | #define OPT_RECURSE (opt & 1) |
| 61 | #define OPT_NODEREF (opt & 2) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 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)) |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 65 | /* POSIX options |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 66 | * -L traverse every symbolic link to a directory encountered |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 67 | * -H if a command line argument is a symbolic link to a directory, traverse it |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 68 | * -P do not traverse any symbolic links (default) |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 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." */ |
| 72 | /* -L */ |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 73 | #define BIT_TRAVERSE 0x20 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 74 | #define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_DESKTOP(0)) |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 75 | /* -H or -L */ |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 76 | #define BIT_TRAVERSE_TOP (0x20|0x40) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 77 | #define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_DESKTOP(0)) |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 78 | |
Matheus Izvekov | 31416d5 | 2010-01-21 19:30:25 -0200 | [diff] [blame] | 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" |
| 84 | # if ENABLE_DESKTOP |
| 85 | "changes\0" No_argument "c" |
| 86 | "silent\0" No_argument "f" |
| 87 | "quiet\0" No_argument "f" |
| 88 | "verbose\0" No_argument "v" |
| 89 | # endif |
| 90 | ; |
| 91 | #endif |
| 92 | |
Denis Vlasenko | 16c7fb7 | 2007-03-14 22:08:04 +0000 | [diff] [blame] | 93 | typedef int (*chown_fptr)(const char *, uid_t, gid_t); |
| 94 | |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 95 | struct param_t { |
| 96 | struct bb_uidgid_t ugid; |
| 97 | chown_fptr chown_func; |
| 98 | }; |
Denis Vlasenko | 16c7fb7 | 2007-03-14 22:08:04 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 100 | static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 101 | void *vparam, int depth UNUSED_PARAM) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 102 | { |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 103 | #define param (*(struct param_t*)vparam) |
| 104 | #define opt option_mask32 |
Denys Vlasenko | 0cd445f | 2010-02-06 21:11:49 +0100 | [diff] [blame] | 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; |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 108 | if (param.chown_func(fileName, u, g) == 0) { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 109 | if (OPT_VERBOSE |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 110 | || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g)) |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 111 | ) { |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 112 | printf("changed ownership of '%s' to %u:%u\n", |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 113 | fileName, (unsigned)u, (unsigned)g); |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 114 | } |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 115 | return TRUE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 116 | } |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 117 | if (!OPT_QUIET) |
Denys Vlasenko | 0cd445f | 2010-02-06 21:11:49 +0100 | [diff] [blame] | 118 | bb_simple_perror_msg(fileName); |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 119 | return FALSE; |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 120 | #undef opt |
| 121 | #undef param |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 124 | int chown_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 125 | { |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 126 | int retval = EXIT_SUCCESS; |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 127 | int opt, flags; |
| 128 | struct param_t param; |
| 129 | |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 130 | #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS |
Denys Vlasenko | b013bec | 2017-12-31 17:59:16 +0100 | [diff] [blame^] | 131 | opt = getopt32long(argv, "^" OPT_STR "\0" "-2", chown_longopts); |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 132 | #else |
Denys Vlasenko | b013bec | 2017-12-31 17:59:16 +0100 | [diff] [blame^] | 133 | opt = getopt32(argv, "^" OPT_STR "\0" "-2"); |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 134 | #endif |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 135 | argv += optind; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 137 | /* This matches coreutils behavior (almost - see below) */ |
Denys Vlasenko | 9882b34 | 2010-01-23 00:13:32 +0100 | [diff] [blame] | 138 | param.chown_func = chown; |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 139 | if (OPT_NODEREF |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 140 | /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */ |
| 141 | IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE) |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 142 | ) { |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 143 | param.chown_func = lchown; |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 144 | } |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 145 | |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 146 | flags = ACTION_DEPTHFIRST; /* match coreutils order */ |
| 147 | if (OPT_RECURSE) |
| 148 | flags |= ACTION_RECURSE; |
| 149 | if (OPT_TRAVERSE_TOP) |
| 150 | flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */ |
| 151 | if (OPT_TRAVERSE) |
| 152 | flags |= ACTION_FOLLOWLINKS; /* follow links if -L */ |
| 153 | |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 154 | parse_chown_usergroup_or_die(¶m.ugid, argv[0]); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 155 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 156 | /* Ok, ready to do the deed now */ |
Denys Vlasenko | 9882b34 | 2010-01-23 00:13:32 +0100 | [diff] [blame] | 157 | while (*++argv) { |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 158 | if (!recursive_action(*argv, |
| 159 | flags, /* flags */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 160 | fileAction, /* file action */ |
| 161 | fileAction, /* dir action */ |
Denis Vlasenko | 08d120e | 2008-07-22 11:37:23 +0000 | [diff] [blame] | 162 | ¶m, /* user data */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 163 | 0) /* depth */ |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 164 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 165 | retval = EXIT_FAILURE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 166 | } |
Denys Vlasenko | 9882b34 | 2010-01-23 00:13:32 +0100 | [diff] [blame] | 167 | } |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 168 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 169 | return retval; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 170 | } |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | Testcase. Run in empty directory. |
| 174 | |
| 175 | #!/bin/sh |
| 176 | t1="/tmp/busybox chown" |
| 177 | t2="/usr/bin/chown" |
| 178 | create() { |
| 179 | rm -rf $1; mkdir $1 |
| 180 | ( |
| 181 | cd $1 || exit 1 |
| 182 | mkdir dir dir2 |
| 183 | >up |
| 184 | >file |
| 185 | >dir/file |
| 186 | >dir2/file |
| 187 | ln -s dir linkdir |
| 188 | ln -s file linkfile |
| 189 | ln -s ../up dir/linkup |
| 190 | ln -s ../dir2 dir/linkupdir2 |
| 191 | ) |
| 192 | chown -R 0:0 $1 |
| 193 | } |
| 194 | tst() { |
| 195 | create test1 |
| 196 | create test2 |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 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 |
| 205 | rm out.diff |
| 206 | } |
| 207 | tst_for_each() { |
| 208 | tst "$1 1:1 file" |
| 209 | tst "$1 1:1 dir" |
| 210 | tst "$1 1:1 linkdir" |
| 211 | tst "$1 1:1 linkfile" |
| 212 | } |
| 213 | echo "If script produced 'out.diff' file, then at least one testcase failed" |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 214 | >test1.out |
| 215 | >test2.out |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 216 | # These match coreutils 6.8: |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 217 | tst_for_each "-v" |
| 218 | tst_for_each "-vR" |
| 219 | tst_for_each "-vRP" |
| 220 | tst_for_each "-vRL" |
| 221 | tst_for_each "-vRH" |
| 222 | tst_for_each "-vh" |
| 223 | tst_for_each "-vhR" |
| 224 | tst_for_each "-vhRP" |
| 225 | tst_for_each "-vhRL" |
| 226 | tst_for_each "-vhRH" |
| 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 |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 231 | |
| 232 | */ |