blob: 09cf73f04c0bb8ddf8a5584ac8ce6c8525305b64 [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 Vlasenko51b4c922006-10-27 16:07:20 +000010/* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
11/* 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
Eric Andersen9f0fedb2001-04-24 18:07:19 +000014#include "busybox.h"
Eric Andersen9f0fedb2001-04-24 18:07:19 +000015
Denis Vlasenko6eebed52007-01-03 20:07:06 +000016static struct bb_uidgid_t ugid = { -1, -1 };
Eric Andersen9f0fedb2001-04-24 18:07:19 +000017
Glenn L McGratha88cb502003-02-08 23:36:16 +000018static int (*chown_func)(const char *, uid_t, gid_t) = chown;
Eric Andersen2b02ab92001-05-11 15:55:41 +000019
Denis Vlasenko51b4c922006-10-27 16:07:20 +000020#define OPT_RECURSE (option_mask32 & 1)
21#define OPT_NODEREF (option_mask32 & 2)
22#define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0))
23#define OPT_CHANGED (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
24#define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
25#define OPT_STR ("Rh" USE_DESKTOP("vcf"))
26
27/* TODO:
28 * -H if a command line argument is a symbolic link to a directory, traverse it
29 * -L traverse every symbolic link to a directory encountered
30 * -P do not traverse any symbolic links (default)
31 */
32
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000033static int fileAction(const char *fileName, struct stat *statbuf,
Denis Vlasenko8c35d652006-10-27 23:42:25 +000034 void ATTRIBUTE_UNUSED *junk, int depth)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000035{
Denis Vlasenko51b4c922006-10-27 16:07:20 +000036 // TODO: -H/-L/-P
37 // if (depth ... && S_ISLNK(statbuf->st_mode)) ....
38
Rob Landleyf8ec1b52006-01-06 18:22:05 +000039 if (!chown_func(fileName,
Denis Vlasenko6eebed52007-01-03 20:07:06 +000040 (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid,
41 (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid)
42 ) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000043 if (OPT_VERBOSE
Denis Vlasenko6eebed52007-01-03 20:07:06 +000044 || (OPT_CHANGED && (statbuf->st_uid != ugid.uid || statbuf->st_gid != ugid.gid))
Denis Vlasenko51b4c922006-10-27 16:07:20 +000045 ) {
Denis Vlasenko6eebed52007-01-03 20:07:06 +000046 printf("changed ownership of '%s' to %u:%u\n",
47 fileName, ugid.uid, ugid.gid);
Denis Vlasenko51b4c922006-10-27 16:07:20 +000048 }
Rob Landleyf8ec1b52006-01-06 18:22:05 +000049 return TRUE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000050 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +000051 if (!OPT_QUIET)
52 bb_perror_msg("%s", fileName); /* A filename can have % in it... */
Rob Landleyf8ec1b52006-01-06 18:22:05 +000053 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000054}
55
56int chown_main(int argc, char **argv)
57{
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 char *groupName;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000059 int retval = EXIT_SUCCESS;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000060
Denis Vlasenko51b4c922006-10-27 16:07:20 +000061 opt_complementary = "-2";
62 getopt32(argc, argv, OPT_STR);
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000063 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +000064
Denis Vlasenko51b4c922006-10-27 16:07:20 +000065 if (OPT_NODEREF) chown_func = lchown;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000066
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 /* First, check if there is a group name here */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000068 groupName = strchr(*argv, '.'); /* deprecated? */
Denis Vlasenko6eebed52007-01-03 20:07:06 +000069 if (!groupName)
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 groupName = strchr(*argv, ':');
Denis Vlasenko6eebed52007-01-03 20:07:06 +000071 else
72 *groupName = ':'; /* replace '.' with ':' */
Eric Andersen9f0fedb2001-04-24 18:07:19 +000073
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000074 /* First, try parsing "user[:[group]]" */
Denis Vlasenko150f4022007-01-13 21:06:21 +000075 if (!groupName) { /* "user" */
Denis Vlasenko6eebed52007-01-03 20:07:06 +000076 ugid.uid = get_ug_id(*argv, xuname2uid);
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000077 } else if (groupName == *argv) { /* ":group" */
Denis Vlasenko6eebed52007-01-03 20:07:06 +000078 ugid.gid = get_ug_id(groupName + 1, xgroup2gid);
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000079 } else {
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000080 if (!groupName[1]) /* "user:" */
81 *groupName = '\0';
82 if (!get_uidgid(&ugid, *argv, 1))
83 bb_error_msg_and_die("unknown user/group %s", *argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000085
Eric Andersen9f0fedb2001-04-24 18:07:19 +000086 /* Ok, ready to do the deed now */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000087 argv++;
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 do {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000089 if (!recursive_action(*argv,
90 OPT_RECURSE, // recurse
91 FALSE, // follow links: TODO: -H/-L/-P
92 FALSE, // depth first
93 fileAction, // file action
94 fileAction, // dir action
Denis Vlasenko8c35d652006-10-27 23:42:25 +000095 NULL, // user data
Denis Vlasenkof7996f32007-01-11 17:20:00 +000096 0) // depth
Denis Vlasenko51b4c922006-10-27 16:07:20 +000097 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000099 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 } while (*++argv);
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000101
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +0000103}