blob: 528a2a05abfb3b6f2d6018bd7d88def5bfae9bae [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 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config CHOWN
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "chown (7.6 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: chown is used to change the user and/or group ownership
14//config: of files.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015//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 Vlasenkoaf3f4202016-11-23 14:46:56 +010020
21//applet:IF_CHOWN(APPLET_NOEXEC(chown, chown, BB_DIR_BIN, BB_SUID_DROP, chown))
22
23//kbuild:lib-$(CONFIG_CHOWN) += chown.o
Eric Andersen9f0fedb2001-04-24 18:07:19 +000024
Denis Vlasenko8a910812007-03-08 16:14:46 +000025/* BB_AUDIT SUSv3 defects - none? */
Manuel Novoa III cad53642003-03-19 09:13:01 +000026/* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
27
Pere Orga34425382011-03-31 14:43:25 +020028//usage:#define chown_trivial_usage
Aaro Koskinen2735bc02015-10-16 17:24:46 +020029//usage: "[-Rh"IF_DESKTOP("LHPcvf")"]... USER[:[GRP]] FILE..."
Pere Orga34425382011-03-31 14:43:25 +020030//usage:#define chown_full_usage "\n\n"
Denys Vlasenkoe2b92152021-06-14 20:47:20 +020031//usage: "Change the owner and/or group of FILEs to USER and/or GRP"
32//usage: "\n"
Pere Orga34425382011-03-31 14:43:25 +020033//usage: "\n -h Affect symlinks instead of symlink targets"
Denys Vlasenko16ca3792014-01-09 17:52:13 +010034//usage: IF_DESKTOP(
Pere Orga34425382011-03-31 14:43:25 +020035//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)"
Denys Vlasenkoe2b92152021-06-14 20:47:20 +020038//usage: )
39//next 4 options are the same for chmod/chown/chgrp:
40//usage: "\n -R Recurse"
41//usage: IF_DESKTOP(
Pere Orga34425382011-03-31 14:43:25 +020042//usage: "\n -c List changed files"
Denys Vlasenkoe2b92152021-06-14 20:47:20 +020043//usage: "\n -v Verbose"
Pere Orga34425382011-03-31 14:43:25 +020044//usage: "\n -f Hide errors"
45//usage: )
46//usage:
47//usage:#define chown_example_usage
48//usage: "$ ls -l /tmp/foo\n"
49//usage: "-r--r--r-- 1 andersen andersen 0 Apr 12 18:25 /tmp/foo\n"
50//usage: "$ chown root /tmp/foo\n"
51//usage: "$ ls -l /tmp/foo\n"
52//usage: "-r--r--r-- 1 root andersen 0 Apr 12 18:25 /tmp/foo\n"
53//usage: "$ chown root.root /tmp/foo\n"
54//usage: "ls -l /tmp/foo\n"
55//usage: "-r--r--r-- 1 root root 0 Apr 12 18:25 /tmp/foo\n"
56
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000057#include "libbb.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000058
Denis Vlasenko99912ca2007-04-10 15:43:37 +000059/* This is a NOEXEC applet. Be very careful! */
60
61
Denys Vlasenko22542ec2017-08-08 21:55:02 +020062#define OPT_STR "Rh" IF_DESKTOP("vcfLHP")
Denis Vlasenkocd27c422007-03-08 13:37:43 +000063#define BIT_RECURSE 1
Denis Vlasenko08d120e2008-07-22 11:37:23 +000064#define OPT_RECURSE (opt & 1)
65#define OPT_NODEREF (opt & 2)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000066#define OPT_VERBOSE (IF_DESKTOP(opt & 0x04) IF_NOT_DESKTOP(0))
67#define OPT_CHANGED (IF_DESKTOP(opt & 0x08) IF_NOT_DESKTOP(0))
68#define OPT_QUIET (IF_DESKTOP(opt & 0x10) IF_NOT_DESKTOP(0))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000069/* POSIX options
Denis Vlasenko51b4c922006-10-27 16:07:20 +000070 * -L traverse every symbolic link to a directory encountered
Denis Vlasenkocd27c422007-03-08 13:37:43 +000071 * -H if a command line argument is a symbolic link to a directory, traverse it
Denis Vlasenko51b4c922006-10-27 16:07:20 +000072 * -P do not traverse any symbolic links (default)
Denis Vlasenkocd27c422007-03-08 13:37:43 +000073 * We do not conform to the following:
74 * "Specifying more than one of -H, -L, and -P is not an error.
75 * The last option specified shall determine the behavior of the utility." */
76/* -L */
Denis Vlasenko8a910812007-03-08 16:14:46 +000077#define BIT_TRAVERSE 0x20
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000078#define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_DESKTOP(0))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000079/* -H or -L */
Denis Vlasenko8a910812007-03-08 16:14:46 +000080#define BIT_TRAVERSE_TOP (0x20|0x40)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000081#define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_DESKTOP(0))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000082
Matheus Izvekov31416d52010-01-21 19:30:25 -020083#if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
84static const char chown_longopts[] ALIGN1 =
85 "recursive\0" No_argument "R"
86 "dereference\0" No_argument "\xff"
87 "no-dereference\0" No_argument "h"
88# if ENABLE_DESKTOP
89 "changes\0" No_argument "c"
90 "silent\0" No_argument "f"
91 "quiet\0" No_argument "f"
92 "verbose\0" No_argument "v"
93# endif
94 ;
95#endif
96
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000097typedef int (*chown_fptr)(const char *, uid_t, gid_t);
98
Denis Vlasenko08d120e2008-07-22 11:37:23 +000099struct param_t {
100 struct bb_uidgid_t ugid;
101 chown_fptr chown_func;
102};
Denis Vlasenko16c7fb72007-03-14 22:08:04 +0000103
Denys Vlasenko689d0652020-10-01 21:52:16 +0200104static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
105 const char *fileName, struct stat *statbuf)
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000106{
Denys Vlasenko689d0652020-10-01 21:52:16 +0200107#define param (*(struct param_t*)state->userData)
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000108#define opt option_mask32
Denys Vlasenko0cd445f2010-02-06 21:11:49 +0100109 uid_t u = (param.ugid.uid == (uid_t)-1L) ? statbuf->st_uid : param.ugid.uid;
110 gid_t g = (param.ugid.gid == (gid_t)-1L) ? statbuf->st_gid : param.ugid.gid;
Denis Vlasenko8a910812007-03-08 16:14:46 +0000111
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000112 if (param.chown_func(fileName, u, g) == 0) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000113 if (OPT_VERBOSE
Denis Vlasenko8a910812007-03-08 16:14:46 +0000114 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000115 ) {
Denis Vlasenko6eebed52007-01-03 20:07:06 +0000116 printf("changed ownership of '%s' to %u:%u\n",
Denis Vlasenko8a910812007-03-08 16:14:46 +0000117 fileName, (unsigned)u, (unsigned)g);
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000118 }
Rob Landleyf8ec1b52006-01-06 18:22:05 +0000119 return TRUE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000120 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000121 if (!OPT_QUIET)
Denys Vlasenko0cd445f2010-02-06 21:11:49 +0100122 bb_simple_perror_msg(fileName);
Rob Landleyf8ec1b52006-01-06 18:22:05 +0000123 return FALSE;
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000124#undef opt
125#undef param
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000126}
127
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000128int chown_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000129{
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000130 int retval = EXIT_SUCCESS;
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000131 int opt, flags;
132 struct param_t param;
133
Denys Vlasenko036585a2017-08-08 16:38:18 +0200134#if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
Denys Vlasenkob013bec2017-12-31 17:59:16 +0100135 opt = getopt32long(argv, "^" OPT_STR "\0" "-2", chown_longopts);
Denys Vlasenko036585a2017-08-08 16:38:18 +0200136#else
Denys Vlasenkob013bec2017-12-31 17:59:16 +0100137 opt = getopt32(argv, "^" OPT_STR "\0" "-2");
Denys Vlasenko036585a2017-08-08 16:38:18 +0200138#endif
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000139 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000141 /* This matches coreutils behavior (almost - see below) */
Denys Vlasenko9882b342010-01-23 00:13:32 +0100142 param.chown_func = chown;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000143 if (OPT_NODEREF
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100144 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
145 IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000146 ) {
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000147 param.chown_func = lchown;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000148 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000149
Denis Vlasenkod166f832007-07-05 00:12:55 +0000150 flags = ACTION_DEPTHFIRST; /* match coreutils order */
151 if (OPT_RECURSE)
152 flags |= ACTION_RECURSE;
153 if (OPT_TRAVERSE_TOP)
154 flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
155 if (OPT_TRAVERSE)
156 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
157
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000158 parse_chown_usergroup_or_die(&param.ugid, argv[0]);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000159
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000160 /* Ok, ready to do the deed now */
Denys Vlasenko9882b342010-01-23 00:13:32 +0100161 while (*++argv) {
Denis Vlasenkod166f832007-07-05 00:12:55 +0000162 if (!recursive_action(*argv,
163 flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000164 fileAction, /* file action */
165 fileAction, /* dir action */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200166 &param) /* user data */
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000167 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000169 }
Denys Vlasenko9882b342010-01-23 00:13:32 +0100170 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000171
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000173}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000174
175/*
176Testcase. Run in empty directory.
177
178#!/bin/sh
179t1="/tmp/busybox chown"
180t2="/usr/bin/chown"
181create() {
182 rm -rf $1; mkdir $1
183 (
184 cd $1 || exit 1
185 mkdir dir dir2
186 >up
187 >file
188 >dir/file
189 >dir2/file
190 ln -s dir linkdir
191 ln -s file linkfile
192 ln -s ../up dir/linkup
193 ln -s ../dir2 dir/linkupdir2
194 )
195 chown -R 0:0 $1
196}
197tst() {
198 create test1
199 create test2
Denis Vlasenkod166f832007-07-05 00:12:55 +0000200 echo "[$1]" >>test1.out
201 echo "[$1]" >>test2.out
202 (cd test1; $t1 $1) >>test1.out 2>&1
203 (cd test2; $t2 $1) >>test2.out 2>&1
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000204 (cd test1; ls -lnR) >out1
205 (cd test2; ls -lnR) >out2
206 echo "chown $1" >out.diff
207 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
208 rm out.diff
209}
210tst_for_each() {
211 tst "$1 1:1 file"
212 tst "$1 1:1 dir"
213 tst "$1 1:1 linkdir"
214 tst "$1 1:1 linkfile"
215}
216echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenkod166f832007-07-05 00:12:55 +0000217>test1.out
218>test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000219# These match coreutils 6.8:
Denis Vlasenkod166f832007-07-05 00:12:55 +0000220tst_for_each "-v"
221tst_for_each "-vR"
222tst_for_each "-vRP"
223tst_for_each "-vRL"
224tst_for_each "-vRH"
225tst_for_each "-vh"
226tst_for_each "-vhR"
227tst_for_each "-vhRP"
228tst_for_each "-vhRL"
229tst_for_each "-vhRH"
230# Fix `name' in coreutils output
231sed 's/`/'"'"'/g' -i test2.out
232# Compare us with coreutils output
233diff -u test1.out test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000234
235*/