Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3 | * Mini chown implementation for busybox |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 6 | * |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */ |
| 11 | /* BB_AUDIT GNU defects - unsupported long options. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */ |
| 13 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 14 | #include "busybox.h" |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 15 | |
Rob Landley | fb8c498 | 2006-01-09 03:45:43 +0000 | [diff] [blame] | 16 | static uid_t uid = -1; |
| 17 | static gid_t gid = -1; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 18 | |
Glenn L McGrath | a88cb50 | 2003-02-08 23:36:16 +0000 | [diff] [blame] | 19 | static int (*chown_func)(const char *, uid_t, gid_t) = chown; |
Eric Andersen | 2b02ab9 | 2001-05-11 15:55:41 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 21 | #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-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 34 | static int fileAction(const char *fileName, struct stat *statbuf, |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 35 | void ATTRIBUTE_UNUSED *junk, int depth) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 36 | { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 37 | // TODO: -H/-L/-P |
| 38 | // if (depth ... && S_ISLNK(statbuf->st_mode)) .... |
| 39 | |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 40 | if (!chown_func(fileName, |
"Vladimir N. Oleynik" | 57545c8 | 2006-01-31 12:06:57 +0000 | [diff] [blame] | 41 | (uid == (uid_t)-1) ? statbuf->st_uid : uid, |
| 42 | (gid == (gid_t)-1) ? statbuf->st_gid : gid)) { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 43 | 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 Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 48 | return TRUE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 49 | } |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 50 | if (!OPT_QUIET) |
| 51 | bb_perror_msg("%s", fileName); /* A filename can have % in it... */ |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 52 | return FALSE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | int chown_main(int argc, char **argv) |
| 56 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | int retval = EXIT_SUCCESS; |
| 58 | char *groupName; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 60 | opt_complementary = "-2"; |
| 61 | getopt32(argc, argv, OPT_STR); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 63 | if (OPT_NODEREF) chown_func = lchown; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 64 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | argv += optind; |
Eric Andersen | 2b02ab9 | 2001-05-11 15:55:41 +0000 | [diff] [blame] | 66 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | /* First, check if there is a group name here */ |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 68 | groupName = strchr(*argv, '.'); |
| 69 | if (!groupName) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | groupName = strchr(*argv, ':'); |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Rob Landley | fb8c498 | 2006-01-09 03:45:43 +0000 | [diff] [blame] | 73 | /* Check for the username and groupname */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | if (groupName) { |
| 75 | *groupName++ = '\0'; |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 76 | gid = get_ug_id(groupName, bb_xgetgrnam); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | } |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 78 | if (--groupName != *argv) |
| 79 | uid = get_ug_id(*argv, bb_xgetpwnam); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | ++argv; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 81 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 82 | /* Ok, ready to do the deed now */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | do { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 84 | 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 Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 90 | NULL, // user data |
| 91 | 0) // depth |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 92 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | retval = EXIT_FAILURE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 94 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | } while (*++argv); |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 96 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | return retval; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 98 | } |