blob: fddce7cf13d4b96cd567d189509d242a8b6b911a [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
Rob Landleyfb8c4982006-01-09 03:45:43 +000016static uid_t uid = -1;
17static gid_t gid = -1;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000018
Glenn L McGratha88cb502003-02-08 23:36:16 +000019static int (*chown_func)(const char *, uid_t, gid_t) = chown;
Eric Andersen2b02ab92001-05-11 15:55:41 +000020
Denis Vlasenko51b4c922006-10-27 16:07:20 +000021#define OPT_RECURSE (option_mask32 & 1)
22#define OPT_NODEREF (option_mask32 & 2)
23#define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0))
24#define OPT_CHANGED (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0))
25#define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
26#define OPT_STR ("Rh" USE_DESKTOP("vcf"))
27
28/* TODO:
29 * -H if a command line argument is a symbolic link to a directory, traverse it
30 * -L traverse every symbolic link to a directory encountered
31 * -P do not traverse any symbolic links (default)
32 */
33
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000034static int fileAction(const char *fileName, struct stat *statbuf,
Denis Vlasenko8c35d652006-10-27 23:42:25 +000035 void ATTRIBUTE_UNUSED *junk, int depth)
Eric Andersen9f0fedb2001-04-24 18:07:19 +000036{
Denis Vlasenko51b4c922006-10-27 16:07:20 +000037 // TODO: -H/-L/-P
38 // if (depth ... && S_ISLNK(statbuf->st_mode)) ....
39
Rob Landleyf8ec1b52006-01-06 18:22:05 +000040 if (!chown_func(fileName,
"Vladimir N. Oleynik"57545c82006-01-31 12:06:57 +000041 (uid == (uid_t)-1) ? statbuf->st_uid : uid,
42 (gid == (gid_t)-1) ? statbuf->st_gid : gid)) {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000043 if (OPT_VERBOSE
44 || (OPT_CHANGED && (statbuf->st_uid != uid || statbuf->st_gid != gid))
45 ) {
46 printf("changed ownership of '%s' to %u:%u\n", fileName, uid, gid);
47 }
Rob Landleyf8ec1b52006-01-06 18:22:05 +000048 return TRUE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000049 }
Denis Vlasenko51b4c922006-10-27 16:07:20 +000050 if (!OPT_QUIET)
51 bb_perror_msg("%s", fileName); /* A filename can have % in it... */
Rob Landleyf8ec1b52006-01-06 18:22:05 +000052 return FALSE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000053}
54
55int chown_main(int argc, char **argv)
56{
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 int retval = EXIT_SUCCESS;
58 char *groupName;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000059
Denis Vlasenko51b4c922006-10-27 16:07:20 +000060 opt_complementary = "-2";
61 getopt32(argc, argv, OPT_STR);
Manuel Novoa III cad53642003-03-19 09:13:01 +000062
Denis Vlasenko51b4c922006-10-27 16:07:20 +000063 if (OPT_NODEREF) chown_func = lchown;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000064
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 argv += optind;
Eric Andersen2b02ab92001-05-11 15:55:41 +000066
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 /* First, check if there is a group name here */
Denis Vlasenko51b4c922006-10-27 16:07:20 +000068 groupName = strchr(*argv, '.');
69 if (!groupName) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 groupName = strchr(*argv, ':');
Eric Andersen9f0fedb2001-04-24 18:07:19 +000071 }
72
Rob Landleyfb8c4982006-01-09 03:45:43 +000073 /* Check for the username and groupname */
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 if (groupName) {
75 *groupName++ = '\0';
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000076 gid = get_ug_id(groupName, bb_xgetgrnam);
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 }
Denis Vlasenko8c35d652006-10-27 23:42:25 +000078 if (--groupName != *argv)
79 uid = get_ug_id(*argv, bb_xgetpwnam);
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 ++argv;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000081
Eric Andersen9f0fedb2001-04-24 18:07:19 +000082 /* Ok, ready to do the deed now */
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 do {
Denis Vlasenko51b4c922006-10-27 16:07:20 +000084 if (!recursive_action(*argv,
85 OPT_RECURSE, // recurse
86 FALSE, // follow links: TODO: -H/-L/-P
87 FALSE, // depth first
88 fileAction, // file action
89 fileAction, // dir action
Denis Vlasenko8c35d652006-10-27 23:42:25 +000090 NULL, // user data
91 0) // depth
Denis Vlasenko51b4c922006-10-27 16:07:20 +000092 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 retval = EXIT_FAILURE;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000094 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 } while (*++argv);
Eric Andersen9f0fedb2001-04-24 18:07:19 +000096
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 return retval;
Eric Andersen9f0fedb2001-04-24 18:07:19 +000098}