blob: cb07bbcbb91d42e76b014acc60fa1fef52585b89 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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
Pere Orga34425382011-03-31 14:43:25 +020013//usage:#define chown_trivial_usage
14//usage: "[-RhLHP"IF_DESKTOP("cvf")"]... OWNER[<.|:>[GROUP]] FILE..."
15//usage:#define chown_full_usage "\n\n"
16//usage: "Change the owner and/or group of each FILE to OWNER and/or GROUP\n"
Pere Orga34425382011-03-31 14:43:25 +020017//usage: "\n -R Recurse"
18//usage: "\n -h Affect symlinks instead of symlink targets"
Denys Vlasenko16ca3792014-01-09 17:52:13 +010019//usage: IF_DESKTOP(
Pere Orga34425382011-03-31 14:43:25 +020020//usage: "\n -L Traverse all symlinks to directories"
21//usage: "\n -H Traverse symlinks on command line only"
22//usage: "\n -P Don't traverse symlinks (default)"
Pere Orga34425382011-03-31 14:43:25 +020023//usage: "\n -c List changed files"
24//usage: "\n -v List all files"
25//usage: "\n -f Hide errors"
26//usage: )
27//usage:
28//usage:#define chown_example_usage
29//usage: "$ ls -l /tmp/foo\n"
30//usage: "-r--r--r-- 1 andersen andersen 0 Apr 12 18:25 /tmp/foo\n"
31//usage: "$ chown root /tmp/foo\n"
32//usage: "$ ls -l /tmp/foo\n"
33//usage: "-r--r--r-- 1 root andersen 0 Apr 12 18:25 /tmp/foo\n"
34//usage: "$ chown root.root /tmp/foo\n"
35//usage: "ls -l /tmp/foo\n"
36//usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
37
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000038#include "libbb.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000039
Denis Vlasenko99912ca2007-04-10 15:43:37 +000040/* This is a NOEXEC applet. Be very careful! */
41
42
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000043#define OPT_STR ("Rh" IF_DESKTOP("vcfLHP"))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000044#define BIT_RECURSE 1
Denis Vlasenko08d120e2008-07-22 11:37:23 +000045#define OPT_RECURSE (opt & 1)
46#define OPT_NODEREF (opt & 2)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000047#define OPT_VERBOSE (IF_DESKTOP(opt & 0x04) IF_NOT_DESKTOP(0))
48#define OPT_CHANGED (IF_DESKTOP(opt & 0x08) IF_NOT_DESKTOP(0))
49#define OPT_QUIET (IF_DESKTOP(opt & 0x10) IF_NOT_DESKTOP(0))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000050/* POSIX options
Denis Vlasenko51b4c922006-10-27 16:07:20 +000051 * -L traverse every symbolic link to a directory encountered
Denis Vlasenkocd27c422007-03-08 13:37:43 +000052 * -H if a command line argument is a symbolic link to a directory, traverse it
Denis Vlasenko51b4c922006-10-27 16:07:20 +000053 * -P do not traverse any symbolic links (default)
Denis Vlasenkocd27c422007-03-08 13:37:43 +000054 * We do not conform to the following:
55 * "Specifying more than one of -H, -L, and -P is not an error.
56 * The last option specified shall determine the behavior of the utility." */
57/* -L */
Denis Vlasenko8a910812007-03-08 16:14:46 +000058#define BIT_TRAVERSE 0x20
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000059#define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_DESKTOP(0))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000060/* -H or -L */
Denis Vlasenko8a910812007-03-08 16:14:46 +000061#define BIT_TRAVERSE_TOP (0x20|0x40)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000062#define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_DESKTOP(0))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000063
Matheus Izvekov31416d52010-01-21 19:30:25 -020064#if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
65static const char chown_longopts[] ALIGN1 =
66 "recursive\0" No_argument "R"
67 "dereference\0" No_argument "\xff"
68 "no-dereference\0" No_argument "h"
69# if ENABLE_DESKTOP
70 "changes\0" No_argument "c"
71 "silent\0" No_argument "f"
72 "quiet\0" No_argument "f"
73 "verbose\0" No_argument "v"
74# endif
75 ;
76#endif
77
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000078typedef int (*chown_fptr)(const char *, uid_t, gid_t);
79
Denis Vlasenko08d120e2008-07-22 11:37:23 +000080struct param_t {
81 struct bb_uidgid_t ugid;
82 chown_fptr chown_func;
83};
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000084
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000085static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
Denis Vlasenko08d120e2008-07-22 11:37:23 +000086 void *vparam, int depth UNUSED_PARAM)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000087{
Denis Vlasenko08d120e2008-07-22 11:37:23 +000088#define param (*(struct param_t*)vparam)
89#define opt option_mask32
Denys Vlasenko0cd445f2010-02-06 21:11:49 +010090 uid_t u = (param.ugid.uid == (uid_t)-1L) ? statbuf->st_uid : param.ugid.uid;
91 gid_t g = (param.ugid.gid == (gid_t)-1L) ? statbuf->st_gid : param.ugid.gid;
Denis Vlasenko8a910812007-03-08 16:14:46 +000092
Denis Vlasenko08d120e2008-07-22 11:37:23 +000093 if (param.chown_func(fileName, u, g) == 0) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000094 if (OPT_VERBOSE
Denis Vlasenko8a910812007-03-08 16:14:46 +000095 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000096 ) {
Denis Vlasenko6eebed52007-01-03 20:07:06 +000097 printf("changed ownership of '%s' to %u:%u\n",
Denis Vlasenko8a910812007-03-08 16:14:46 +000098 fileName, (unsigned)u, (unsigned)g);
Denis Vlasenko51b4c922006-10-27 16:07:20 +000099 }
Rob Landleyf8ec1b52006-01-06 18:22:05 +0000100 return TRUE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000101 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000102 if (!OPT_QUIET)
Denys Vlasenko0cd445f2010-02-06 21:11:49 +0100103 bb_simple_perror_msg(fileName);
Rob Landleyf8ec1b52006-01-06 18:22:05 +0000104 return FALSE;
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000105#undef opt
106#undef param
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000107}
108
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000109int chown_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000110{
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000111 int retval = EXIT_SUCCESS;
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000112 int opt, flags;
113 struct param_t param;
114
Denys Vlasenko0cd445f2010-02-06 21:11:49 +0100115 /* Just -1 might not work: uid_t may be unsigned long */
116 param.ugid.uid = -1L;
117 param.ugid.gid = -1L;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000118
Matheus Izvekov31416d52010-01-21 19:30:25 -0200119#if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
120 applet_long_options = chown_longopts;
121#endif
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000122 opt_complementary = "-2";
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000123 opt = getopt32(argv, OPT_STR);
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000124 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000126 /* This matches coreutils behavior (almost - see below) */
Denys Vlasenko9882b342010-01-23 00:13:32 +0100127 param.chown_func = chown;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000128 if (OPT_NODEREF
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100129 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
130 IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000131 ) {
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000132 param.chown_func = lchown;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000133 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000134
Denis Vlasenkod166f832007-07-05 00:12:55 +0000135 flags = ACTION_DEPTHFIRST; /* match coreutils order */
136 if (OPT_RECURSE)
137 flags |= ACTION_RECURSE;
138 if (OPT_TRAVERSE_TOP)
139 flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
140 if (OPT_TRAVERSE)
141 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
142
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000143 parse_chown_usergroup_or_die(&param.ugid, argv[0]);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000144
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000145 /* Ok, ready to do the deed now */
Denys Vlasenko9882b342010-01-23 00:13:32 +0100146 while (*++argv) {
Denis Vlasenkod166f832007-07-05 00:12:55 +0000147 if (!recursive_action(*argv,
148 flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000149 fileAction, /* file action */
150 fileAction, /* dir action */
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000151 &param, /* user data */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000152 0) /* depth */
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000153 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000155 }
Denys Vlasenko9882b342010-01-23 00:13:32 +0100156 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000157
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000159}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000160
161/*
162Testcase. Run in empty directory.
163
164#!/bin/sh
165t1="/tmp/busybox chown"
166t2="/usr/bin/chown"
167create() {
168 rm -rf $1; mkdir $1
169 (
170 cd $1 || exit 1
171 mkdir dir dir2
172 >up
173 >file
174 >dir/file
175 >dir2/file
176 ln -s dir linkdir
177 ln -s file linkfile
178 ln -s ../up dir/linkup
179 ln -s ../dir2 dir/linkupdir2
180 )
181 chown -R 0:0 $1
182}
183tst() {
184 create test1
185 create test2
Denis Vlasenkod166f832007-07-05 00:12:55 +0000186 echo "[$1]" >>test1.out
187 echo "[$1]" >>test2.out
188 (cd test1; $t1 $1) >>test1.out 2>&1
189 (cd test2; $t2 $1) >>test2.out 2>&1
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000190 (cd test1; ls -lnR) >out1
191 (cd test2; ls -lnR) >out2
192 echo "chown $1" >out.diff
193 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
194 rm out.diff
195}
196tst_for_each() {
197 tst "$1 1:1 file"
198 tst "$1 1:1 dir"
199 tst "$1 1:1 linkdir"
200 tst "$1 1:1 linkfile"
201}
202echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenkod166f832007-07-05 00:12:55 +0000203>test1.out
204>test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000205# These match coreutils 6.8:
Denis Vlasenkod166f832007-07-05 00:12:55 +0000206tst_for_each "-v"
207tst_for_each "-vR"
208tst_for_each "-vRP"
209tst_for_each "-vRL"
210tst_for_each "-vRH"
211tst_for_each "-vh"
212tst_for_each "-vhR"
213tst_for_each "-vhRP"
214tst_for_each "-vhRL"
215tst_for_each "-vhRH"
216# Fix `name' in coreutils output
217sed 's/`/'"'"'/g' -i test2.out
218# Compare us with coreutils output
219diff -u test1.out test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000220
221*/