blob: df3b77dacb16873332e51e1d3990bd90bfed8367 [file] [log] [blame]
Eric Andersen9f0fedb2001-04-24 18:07:19 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenbdfd0d72001-10-24 05:00:29 +00003 * Mini chown implementation for busybox
Eric Andersen9f0fedb2001-04-24 18:07:19 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen9f0fedb2001-04-24 18:07:19 +00006 *
Rob Landleyf8ec1b52006-01-06 18:22:05 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen9f0fedb2001-04-24 18:07:19 +00008 */
9
Denis Vlasenko8a910812007-03-08 16:14:46 +000010/* BB_AUDIT SUSv3 defects - none? */
Denis Vlasenko51b4c922006-10-27 16:07:20 +000011/* BB_AUDIT GNU defects - unsupported long options. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000012/* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
13
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000015
Denis Vlasenko99912ca2007-04-10 15:43:37 +000016/* This is a NOEXEC applet. Be very careful! */
17
18
Denis Vlasenkocd27c422007-03-08 13:37:43 +000019#define OPT_STR ("Rh" USE_DESKTOP("vcfLHP"))
20#define BIT_RECURSE 1
Denis Vlasenko51b4c922006-10-27 16:07:20 +000021#define OPT_RECURSE (option_mask32 & 1)
22#define OPT_NODEREF (option_mask32 & 2)
Denis Vlasenkocd27c422007-03-08 13:37:43 +000023#define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0))
24#define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000025#define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000026/* POSIX options
Denis Vlasenko51b4c922006-10-27 16:07:20 +000027 * -L traverse every symbolic link to a directory encountered
Denis Vlasenkocd27c422007-03-08 13:37:43 +000028 * -H if a command line argument is a symbolic link to a directory, traverse it
Denis Vlasenko51b4c922006-10-27 16:07:20 +000029 * -P do not traverse any symbolic links (default)
Denis Vlasenkocd27c422007-03-08 13:37:43 +000030 * 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 Vlasenko8a910812007-03-08 16:14:46 +000034#define BIT_TRAVERSE 0x20
35#define OPT_TRAVERSE (USE_DESKTOP(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000036/* -H or -L */
Denis Vlasenko8a910812007-03-08 16:14:46 +000037#define BIT_TRAVERSE_TOP (0x20|0x40)
38#define OPT_TRAVERSE_TOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000039
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000040typedef int (*chown_fptr)(const char *, uid_t, gid_t);
41
42static struct bb_uidgid_t ugid = { -1, -1 };
43
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000044static int fileAction(const char *fileName, struct stat *statbuf,
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000045 void *cf, int depth)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000046{
Denis Vlasenko8a910812007-03-08 16:14:46 +000047 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 Vlasenko16c7fb72007-03-14 22:08:04 +000050 if (!((chown_fptr)cf)(fileName, u, g)) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000051 if (OPT_VERBOSE
Denis Vlasenko8a910812007-03-08 16:14:46 +000052 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000053 ) {
Denis Vlasenko6eebed52007-01-03 20:07:06 +000054 printf("changed ownership of '%s' to %u:%u\n",
Denis Vlasenko8a910812007-03-08 16:14:46 +000055 fileName, (unsigned)u, (unsigned)g);
Denis Vlasenko51b4c922006-10-27 16:07:20 +000056 }
Rob Landleyf8ec1b52006-01-06 18:22:05 +000057 return TRUE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000058 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +000059 if (!OPT_QUIET)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000060 bb_simple_perror_msg(fileName); /* A filename can have % in it... */
Rob Landleyf8ec1b52006-01-06 18:22:05 +000061 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000062}
63
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000064int chown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000065int chown_main(int argc, char **argv)
66{
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000067 int retval = EXIT_SUCCESS;
Denis Vlasenkod166f832007-07-05 00:12:55 +000068 int flags;
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000069 chown_fptr chown_func;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000070
Denis Vlasenko51b4c922006-10-27 16:07:20 +000071 opt_complementary = "-2";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000072 getopt32(argv, OPT_STR);
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000073 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +000074
Denis Vlasenkocd27c422007-03-08 13:37:43 +000075 /* This matches coreutils behavior (almost - see below) */
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000076 chown_func = chown;
Denis Vlasenkocd27c422007-03-08 13:37:43 +000077 if (OPT_NODEREF
Denis Vlasenko8a910812007-03-08 16:14:46 +000078 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
79 USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
Denis Vlasenkocd27c422007-03-08 13:37:43 +000080 ) {
81 chown_func = lchown;
82 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +000083
Denis Vlasenkod166f832007-07-05 00:12:55 +000084 flags = ACTION_DEPTHFIRST; /* match coreutils order */
85 if (OPT_RECURSE)
86 flags |= ACTION_RECURSE;
87 if (OPT_TRAVERSE_TOP)
88 flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
89 if (OPT_TRAVERSE)
90 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
91
Denis Vlasenkocce38582007-02-26 22:47:42 +000092 parse_chown_usergroup_or_die(&ugid, argv[0]);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000093
Eric Andersen9f0fedb2001-04-24 18:07:19 +000094 /* Ok, ready to do the deed now */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000095 argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 do {
Denis Vlasenkod166f832007-07-05 00:12:55 +000097 if (!recursive_action(*argv,
98 flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +000099 fileAction, /* file action */
100 fileAction, /* dir action */
101 chown_func, /* user data */
102 0) /* depth */
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000103 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000105 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 } while (*++argv);
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000107
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000109}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000110
111/*
112Testcase. Run in empty directory.
113
114#!/bin/sh
115t1="/tmp/busybox chown"
116t2="/usr/bin/chown"
117create() {
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}
133tst() {
134 create test1
135 create test2
Denis Vlasenkod166f832007-07-05 00:12:55 +0000136 echo "[$1]" >>test1.out
137 echo "[$1]" >>test2.out
138 (cd test1; $t1 $1) >>test1.out 2>&1
139 (cd test2; $t2 $1) >>test2.out 2>&1
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000140 (cd test1; ls -lnR) >out1
141 (cd test2; ls -lnR) >out2
142 echo "chown $1" >out.diff
143 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
144 rm out.diff
145}
146tst_for_each() {
147 tst "$1 1:1 file"
148 tst "$1 1:1 dir"
149 tst "$1 1:1 linkdir"
150 tst "$1 1:1 linkfile"
151}
152echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenkod166f832007-07-05 00:12:55 +0000153>test1.out
154>test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000155# These match coreutils 6.8:
Denis Vlasenkod166f832007-07-05 00:12:55 +0000156tst_for_each "-v"
157tst_for_each "-vR"
158tst_for_each "-vRP"
159tst_for_each "-vRL"
160tst_for_each "-vRH"
161tst_for_each "-vh"
162tst_for_each "-vhR"
163tst_for_each "-vhRP"
164tst_for_each "-vhRL"
165tst_for_each "-vhRH"
166# Fix `name' in coreutils output
167sed 's/`/'"'"'/g' -i test2.out
168# Compare us with coreutils output
169diff -u test1.out test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000170
171*/