blob: 717e4b17aade55c849796de32e861e08840b661d [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? */
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000014
Denis Vlasenko99912ca2007-04-10 15:43:37 +000015/* This is a NOEXEC applet. Be very careful! */
16
17
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000018#define OPT_STR ("Rh" IF_DESKTOP("vcfLHP"))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000019#define BIT_RECURSE 1
Denis Vlasenko08d120e2008-07-22 11:37:23 +000020#define OPT_RECURSE (opt & 1)
21#define OPT_NODEREF (opt & 2)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000022#define OPT_VERBOSE (IF_DESKTOP(opt & 0x04) IF_NOT_DESKTOP(0))
23#define OPT_CHANGED (IF_DESKTOP(opt & 0x08) IF_NOT_DESKTOP(0))
24#define OPT_QUIET (IF_DESKTOP(opt & 0x10) IF_NOT_DESKTOP(0))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000025/* POSIX options
Denis Vlasenko51b4c922006-10-27 16:07:20 +000026 * -L traverse every symbolic link to a directory encountered
Denis Vlasenkocd27c422007-03-08 13:37:43 +000027 * -H if a command line argument is a symbolic link to a directory, traverse it
Denis Vlasenko51b4c922006-10-27 16:07:20 +000028 * -P do not traverse any symbolic links (default)
Denis Vlasenkocd27c422007-03-08 13:37:43 +000029 * We do not conform to the following:
30 * "Specifying more than one of -H, -L, and -P is not an error.
31 * The last option specified shall determine the behavior of the utility." */
32/* -L */
Denis Vlasenko8a910812007-03-08 16:14:46 +000033#define BIT_TRAVERSE 0x20
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000034#define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_DESKTOP(0))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000035/* -H or -L */
Denis Vlasenko8a910812007-03-08 16:14:46 +000036#define BIT_TRAVERSE_TOP (0x20|0x40)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000037#define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_DESKTOP(0))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000038
Matheus Izvekov31416d52010-01-21 19:30:25 -020039#if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
40static const char chown_longopts[] ALIGN1 =
41 "recursive\0" No_argument "R"
42 "dereference\0" No_argument "\xff"
43 "no-dereference\0" No_argument "h"
44# if ENABLE_DESKTOP
45 "changes\0" No_argument "c"
46 "silent\0" No_argument "f"
47 "quiet\0" No_argument "f"
48 "verbose\0" No_argument "v"
49# endif
50 ;
51#endif
52
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000053typedef int (*chown_fptr)(const char *, uid_t, gid_t);
54
Denis Vlasenko08d120e2008-07-22 11:37:23 +000055struct param_t {
56 struct bb_uidgid_t ugid;
57 chown_fptr chown_func;
58};
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000059
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000060static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
Denis Vlasenko08d120e2008-07-22 11:37:23 +000061 void *vparam, int depth UNUSED_PARAM)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000062{
Denis Vlasenko08d120e2008-07-22 11:37:23 +000063#define param (*(struct param_t*)vparam)
64#define opt option_mask32
Denys Vlasenko0cd445f2010-02-06 21:11:49 +010065 uid_t u = (param.ugid.uid == (uid_t)-1L) ? statbuf->st_uid : param.ugid.uid;
66 gid_t g = (param.ugid.gid == (gid_t)-1L) ? statbuf->st_gid : param.ugid.gid;
Denis Vlasenko8a910812007-03-08 16:14:46 +000067
Denis Vlasenko08d120e2008-07-22 11:37:23 +000068 if (param.chown_func(fileName, u, g) == 0) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000069 if (OPT_VERBOSE
Denis Vlasenko8a910812007-03-08 16:14:46 +000070 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000071 ) {
Denis Vlasenko6eebed52007-01-03 20:07:06 +000072 printf("changed ownership of '%s' to %u:%u\n",
Denis Vlasenko8a910812007-03-08 16:14:46 +000073 fileName, (unsigned)u, (unsigned)g);
Denis Vlasenko51b4c922006-10-27 16:07:20 +000074 }
Rob Landleyf8ec1b52006-01-06 18:22:05 +000075 return TRUE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000076 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +000077 if (!OPT_QUIET)
Denys Vlasenko0cd445f2010-02-06 21:11:49 +010078 bb_simple_perror_msg(fileName);
Rob Landleyf8ec1b52006-01-06 18:22:05 +000079 return FALSE;
Denis Vlasenko08d120e2008-07-22 11:37:23 +000080#undef opt
81#undef param
Eric Andersen9f0fedb2001-04-24 18:07:19 +000082}
83
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000084int chown_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000085{
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000086 int retval = EXIT_SUCCESS;
Denis Vlasenko08d120e2008-07-22 11:37:23 +000087 int opt, flags;
88 struct param_t param;
89
Denys Vlasenko0cd445f2010-02-06 21:11:49 +010090 /* Just -1 might not work: uid_t may be unsigned long */
91 param.ugid.uid = -1L;
92 param.ugid.gid = -1L;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000093
Matheus Izvekov31416d52010-01-21 19:30:25 -020094#if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
95 applet_long_options = chown_longopts;
96#endif
Denis Vlasenko51b4c922006-10-27 16:07:20 +000097 opt_complementary = "-2";
Denis Vlasenko08d120e2008-07-22 11:37:23 +000098 opt = getopt32(argv, OPT_STR);
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000099 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000101 /* This matches coreutils behavior (almost - see below) */
Denys Vlasenko9882b342010-01-23 00:13:32 +0100102 param.chown_func = chown;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000103 if (OPT_NODEREF
Denis Vlasenko8a910812007-03-08 16:14:46 +0000104 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000105 IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000106 ) {
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000107 param.chown_func = lchown;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000108 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000109
Denis Vlasenkod166f832007-07-05 00:12:55 +0000110 flags = ACTION_DEPTHFIRST; /* match coreutils order */
111 if (OPT_RECURSE)
112 flags |= ACTION_RECURSE;
113 if (OPT_TRAVERSE_TOP)
114 flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
115 if (OPT_TRAVERSE)
116 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
117
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000118 parse_chown_usergroup_or_die(&param.ugid, argv[0]);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000119
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000120 /* Ok, ready to do the deed now */
Denys Vlasenko9882b342010-01-23 00:13:32 +0100121 while (*++argv) {
Denis Vlasenkod166f832007-07-05 00:12:55 +0000122 if (!recursive_action(*argv,
123 flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000124 fileAction, /* file action */
125 fileAction, /* dir action */
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000126 &param, /* user data */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000127 0) /* depth */
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000128 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000130 }
Denys Vlasenko9882b342010-01-23 00:13:32 +0100131 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000132
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000134}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000135
136/*
137Testcase. Run in empty directory.
138
139#!/bin/sh
140t1="/tmp/busybox chown"
141t2="/usr/bin/chown"
142create() {
143 rm -rf $1; mkdir $1
144 (
145 cd $1 || exit 1
146 mkdir dir dir2
147 >up
148 >file
149 >dir/file
150 >dir2/file
151 ln -s dir linkdir
152 ln -s file linkfile
153 ln -s ../up dir/linkup
154 ln -s ../dir2 dir/linkupdir2
155 )
156 chown -R 0:0 $1
157}
158tst() {
159 create test1
160 create test2
Denis Vlasenkod166f832007-07-05 00:12:55 +0000161 echo "[$1]" >>test1.out
162 echo "[$1]" >>test2.out
163 (cd test1; $t1 $1) >>test1.out 2>&1
164 (cd test2; $t2 $1) >>test2.out 2>&1
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000165 (cd test1; ls -lnR) >out1
166 (cd test2; ls -lnR) >out2
167 echo "chown $1" >out.diff
168 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
169 rm out.diff
170}
171tst_for_each() {
172 tst "$1 1:1 file"
173 tst "$1 1:1 dir"
174 tst "$1 1:1 linkdir"
175 tst "$1 1:1 linkfile"
176}
177echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenkod166f832007-07-05 00:12:55 +0000178>test1.out
179>test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000180# These match coreutils 6.8:
Denis Vlasenkod166f832007-07-05 00:12:55 +0000181tst_for_each "-v"
182tst_for_each "-vR"
183tst_for_each "-vRP"
184tst_for_each "-vRL"
185tst_for_each "-vRH"
186tst_for_each "-vh"
187tst_for_each "-vhR"
188tst_for_each "-vhRP"
189tst_for_each "-vhRL"
190tst_for_each "-vhRH"
191# Fix `name' in coreutils output
192sed 's/`/'"'"'/g' -i test2.out
193# Compare us with coreutils output
194diff -u test1.out test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000195
196*/