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 | * |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 defects - none? */ |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 11 | /* BB_AUDIT GNU defects - unsupported long options. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */ |
| 13 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 16 | /* This is a NOEXEC applet. Be very careful! */ |
| 17 | |
| 18 | |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 19 | #define OPT_STR ("Rh" USE_DESKTOP("vcfLHP")) |
| 20 | #define BIT_RECURSE 1 |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 21 | #define OPT_RECURSE (option_mask32 & 1) |
| 22 | #define OPT_NODEREF (option_mask32 & 2) |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 23 | #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0)) |
| 24 | #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0)) |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 25 | #define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0)) |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 26 | /* POSIX options |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 27 | * -L traverse every symbolic link to a directory encountered |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 28 | * -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] | 29 | * -P do not traverse any symbolic links (default) |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 30 | * We do not conform to the following: |
| 31 | * "Specifying more than one of -H, -L, and -P is not an error. |
| 32 | * The last option specified shall determine the behavior of the utility." */ |
| 33 | /* -L */ |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 34 | #define BIT_TRAVERSE 0x20 |
| 35 | #define OPT_TRAVERSE (USE_DESKTOP(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0)) |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 36 | /* -H or -L */ |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 37 | #define BIT_TRAVERSE_TOP (0x20|0x40) |
| 38 | #define OPT_TRAVERSE_TOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0)) |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 16c7fb7 | 2007-03-14 22:08:04 +0000 | [diff] [blame] | 40 | typedef int (*chown_fptr)(const char *, uid_t, gid_t); |
| 41 | |
| 42 | static struct bb_uidgid_t ugid = { -1, -1 }; |
| 43 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 44 | static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 45 | void *cf, int depth ATTRIBUTE_UNUSED) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 46 | { |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 47 | uid_t u = (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid; |
| 48 | gid_t g = (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid; |
| 49 | |
Denis Vlasenko | 16c7fb7 | 2007-03-14 22:08:04 +0000 | [diff] [blame] | 50 | if (!((chown_fptr)cf)(fileName, u, g)) { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 51 | if (OPT_VERBOSE |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 52 | || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g)) |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 53 | ) { |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 54 | printf("changed ownership of '%s' to %u:%u\n", |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 55 | fileName, (unsigned)u, (unsigned)g); |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 56 | } |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 57 | return TRUE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 58 | } |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 59 | if (!OPT_QUIET) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 60 | bb_simple_perror_msg(fileName); /* A filename can have % in it... */ |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 61 | return FALSE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 64 | int chown_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 65 | { |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 66 | int retval = EXIT_SUCCESS; |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 67 | int flags; |
Denis Vlasenko | 16c7fb7 | 2007-03-14 22:08:04 +0000 | [diff] [blame] | 68 | chown_fptr chown_func; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 70 | opt_complementary = "-2"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 71 | getopt32(argv, OPT_STR); |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 72 | argv += optind; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 74 | /* This matches coreutils behavior (almost - see below) */ |
Denis Vlasenko | 16c7fb7 | 2007-03-14 22:08:04 +0000 | [diff] [blame] | 75 | chown_func = chown; |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 76 | if (OPT_NODEREF |
Denis Vlasenko | 8a91081 | 2007-03-08 16:14:46 +0000 | [diff] [blame] | 77 | /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */ |
| 78 | USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE) |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 79 | ) { |
| 80 | chown_func = lchown; |
| 81 | } |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 83 | flags = ACTION_DEPTHFIRST; /* match coreutils order */ |
| 84 | if (OPT_RECURSE) |
| 85 | flags |= ACTION_RECURSE; |
| 86 | if (OPT_TRAVERSE_TOP) |
| 87 | flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */ |
| 88 | if (OPT_TRAVERSE) |
| 89 | flags |= ACTION_FOLLOWLINKS; /* follow links if -L */ |
| 90 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 91 | parse_chown_usergroup_or_die(&ugid, argv[0]); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 92 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 93 | /* Ok, ready to do the deed now */ |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 94 | argv++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | do { |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 96 | if (!recursive_action(*argv, |
| 97 | flags, /* flags */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 98 | fileAction, /* file action */ |
| 99 | fileAction, /* dir action */ |
| 100 | chown_func, /* user data */ |
| 101 | 0) /* depth */ |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 102 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 103 | retval = EXIT_FAILURE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 104 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | } while (*++argv); |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 106 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 107 | return retval; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 108 | } |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | Testcase. Run in empty directory. |
| 112 | |
| 113 | #!/bin/sh |
| 114 | t1="/tmp/busybox chown" |
| 115 | t2="/usr/bin/chown" |
| 116 | create() { |
| 117 | rm -rf $1; mkdir $1 |
| 118 | ( |
| 119 | cd $1 || exit 1 |
| 120 | mkdir dir dir2 |
| 121 | >up |
| 122 | >file |
| 123 | >dir/file |
| 124 | >dir2/file |
| 125 | ln -s dir linkdir |
| 126 | ln -s file linkfile |
| 127 | ln -s ../up dir/linkup |
| 128 | ln -s ../dir2 dir/linkupdir2 |
| 129 | ) |
| 130 | chown -R 0:0 $1 |
| 131 | } |
| 132 | tst() { |
| 133 | create test1 |
| 134 | create test2 |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 135 | echo "[$1]" >>test1.out |
| 136 | echo "[$1]" >>test2.out |
| 137 | (cd test1; $t1 $1) >>test1.out 2>&1 |
| 138 | (cd test2; $t2 $1) >>test2.out 2>&1 |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 139 | (cd test1; ls -lnR) >out1 |
| 140 | (cd test2; ls -lnR) >out2 |
| 141 | echo "chown $1" >out.diff |
| 142 | if ! diff -u out1 out2 >>out.diff; then exit 1; fi |
| 143 | rm out.diff |
| 144 | } |
| 145 | tst_for_each() { |
| 146 | tst "$1 1:1 file" |
| 147 | tst "$1 1:1 dir" |
| 148 | tst "$1 1:1 linkdir" |
| 149 | tst "$1 1:1 linkfile" |
| 150 | } |
| 151 | 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] | 152 | >test1.out |
| 153 | >test2.out |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 154 | # These match coreutils 6.8: |
Denis Vlasenko | d166f83 | 2007-07-05 00:12:55 +0000 | [diff] [blame] | 155 | tst_for_each "-v" |
| 156 | tst_for_each "-vR" |
| 157 | tst_for_each "-vRP" |
| 158 | tst_for_each "-vRL" |
| 159 | tst_for_each "-vRH" |
| 160 | tst_for_each "-vh" |
| 161 | tst_for_each "-vhR" |
| 162 | tst_for_each "-vhRP" |
| 163 | tst_for_each "-vhRL" |
| 164 | tst_for_each "-vhRH" |
| 165 | # Fix `name' in coreutils output |
| 166 | sed 's/`/'"'"'/g' -i test2.out |
| 167 | # Compare us with coreutils output |
| 168 | diff -u test1.out test2.out |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 169 | |
| 170 | */ |