blob: 2d8e556f06029f2ed5f7e2e117db952d8b290a32 [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 Vlasenko5e34ff22009-04-21 11:09:40 +000019#define OPT_STR ("Rh" IF_DESKTOP("vcfLHP"))
Denis Vlasenkocd27c422007-03-08 13:37:43 +000020#define BIT_RECURSE 1
Denis Vlasenko08d120e2008-07-22 11:37:23 +000021#define OPT_RECURSE (opt & 1)
22#define OPT_NODEREF (opt & 2)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000023#define OPT_VERBOSE (IF_DESKTOP(opt & 0x04) IF_NOT_DESKTOP(0))
24#define OPT_CHANGED (IF_DESKTOP(opt & 0x08) IF_NOT_DESKTOP(0))
25#define OPT_QUIET (IF_DESKTOP(opt & 0x10) IF_NOT_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
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000035#define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_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)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000038#define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_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
Denis Vlasenko08d120e2008-07-22 11:37:23 +000042struct param_t {
43 struct bb_uidgid_t ugid;
44 chown_fptr chown_func;
45};
Denis Vlasenko16c7fb72007-03-14 22:08:04 +000046
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000047static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
Denis Vlasenko08d120e2008-07-22 11:37:23 +000048 void *vparam, int depth UNUSED_PARAM)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000049{
Denis Vlasenko08d120e2008-07-22 11:37:23 +000050#define param (*(struct param_t*)vparam)
51#define opt option_mask32
52 uid_t u = (param.ugid.uid == (uid_t)-1) ? statbuf->st_uid : param.ugid.uid;
53 gid_t g = (param.ugid.gid == (gid_t)-1) ? statbuf->st_gid : param.ugid.gid;
Denis Vlasenko8a910812007-03-08 16:14:46 +000054
Denis Vlasenko08d120e2008-07-22 11:37:23 +000055 if (param.chown_func(fileName, u, g) == 0) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000056 if (OPT_VERBOSE
Denis Vlasenko8a910812007-03-08 16:14:46 +000057 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000058 ) {
Denis Vlasenko6eebed52007-01-03 20:07:06 +000059 printf("changed ownership of '%s' to %u:%u\n",
Denis Vlasenko8a910812007-03-08 16:14:46 +000060 fileName, (unsigned)u, (unsigned)g);
Denis Vlasenko51b4c922006-10-27 16:07:20 +000061 }
Rob Landleyf8ec1b52006-01-06 18:22:05 +000062 return TRUE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000063 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +000064 if (!OPT_QUIET)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000065 bb_simple_perror_msg(fileName); /* A filename can have % in it... */
Rob Landleyf8ec1b52006-01-06 18:22:05 +000066 return FALSE;
Denis Vlasenko08d120e2008-07-22 11:37:23 +000067#undef opt
68#undef param
Eric Andersen9f0fedb2001-04-24 18:07:19 +000069}
70
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000071int chown_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000072{
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000073 int retval = EXIT_SUCCESS;
Denis Vlasenko08d120e2008-07-22 11:37:23 +000074 int opt, flags;
75 struct param_t param;
76
77 param.ugid.uid = -1;
78 param.ugid.gid = -1;
79 param.chown_func = chown;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000080
Denis Vlasenko51b4c922006-10-27 16:07:20 +000081 opt_complementary = "-2";
Denis Vlasenko08d120e2008-07-22 11:37:23 +000082 opt = getopt32(argv, OPT_STR);
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000083 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +000084
Denis Vlasenkocd27c422007-03-08 13:37:43 +000085 /* This matches coreutils behavior (almost - see below) */
86 if (OPT_NODEREF
Denis Vlasenko8a910812007-03-08 16:14:46 +000087 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000088 IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
Denis Vlasenkocd27c422007-03-08 13:37:43 +000089 ) {
Denis Vlasenko08d120e2008-07-22 11:37:23 +000090 param.chown_func = lchown;
Denis Vlasenkocd27c422007-03-08 13:37:43 +000091 }
Eric Andersen9f0fedb2001-04-24 18:07:19 +000092
Denis Vlasenkod166f832007-07-05 00:12:55 +000093 flags = ACTION_DEPTHFIRST; /* match coreutils order */
94 if (OPT_RECURSE)
95 flags |= ACTION_RECURSE;
96 if (OPT_TRAVERSE_TOP)
97 flags |= ACTION_FOLLOWLINKS_L0; /* -H/-L: follow links on depth 0 */
98 if (OPT_TRAVERSE)
99 flags |= ACTION_FOLLOWLINKS; /* follow links if -L */
100
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000101 parse_chown_usergroup_or_die(&param.ugid, argv[0]);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000102
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000103 /* Ok, ready to do the deed now */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000104 argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 do {
Denis Vlasenkod166f832007-07-05 00:12:55 +0000106 if (!recursive_action(*argv,
107 flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000108 fileAction, /* file action */
109 fileAction, /* dir action */
Denis Vlasenko08d120e2008-07-22 11:37:23 +0000110 &param, /* user data */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000111 0) /* depth */
Denis Vlasenko51b4c922006-10-27 16:07:20 +0000112 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000114 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 } while (*++argv);
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000116
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000118}
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000119
120/*
121Testcase. Run in empty directory.
122
123#!/bin/sh
124t1="/tmp/busybox chown"
125t2="/usr/bin/chown"
126create() {
127 rm -rf $1; mkdir $1
128 (
129 cd $1 || exit 1
130 mkdir dir dir2
131 >up
132 >file
133 >dir/file
134 >dir2/file
135 ln -s dir linkdir
136 ln -s file linkfile
137 ln -s ../up dir/linkup
138 ln -s ../dir2 dir/linkupdir2
139 )
140 chown -R 0:0 $1
141}
142tst() {
143 create test1
144 create test2
Denis Vlasenkod166f832007-07-05 00:12:55 +0000145 echo "[$1]" >>test1.out
146 echo "[$1]" >>test2.out
147 (cd test1; $t1 $1) >>test1.out 2>&1
148 (cd test2; $t2 $1) >>test2.out 2>&1
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000149 (cd test1; ls -lnR) >out1
150 (cd test2; ls -lnR) >out2
151 echo "chown $1" >out.diff
152 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
153 rm out.diff
154}
155tst_for_each() {
156 tst "$1 1:1 file"
157 tst "$1 1:1 dir"
158 tst "$1 1:1 linkdir"
159 tst "$1 1:1 linkfile"
160}
161echo "If script produced 'out.diff' file, then at least one testcase failed"
Denis Vlasenkod166f832007-07-05 00:12:55 +0000162>test1.out
163>test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000164# These match coreutils 6.8:
Denis Vlasenkod166f832007-07-05 00:12:55 +0000165tst_for_each "-v"
166tst_for_each "-vR"
167tst_for_each "-vRP"
168tst_for_each "-vRL"
169tst_for_each "-vRH"
170tst_for_each "-vh"
171tst_for_each "-vhR"
172tst_for_each "-vhRP"
173tst_for_each "-vhRL"
174tst_for_each "-vhRH"
175# Fix `name' in coreutils output
176sed 's/`/'"'"'/g' -i test2.out
177# Compare us with coreutils output
178diff -u test1.out test2.out
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000179
180*/