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 | |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 16 | static struct bb_uidgid_t ugid = { -1, -1 }; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 17 | |
Glenn L McGrath | a88cb50 | 2003-02-08 23:36:16 +0000 | [diff] [blame] | 18 | static int (*chown_func)(const char *, uid_t, gid_t) = chown; |
Eric Andersen | 2b02ab9 | 2001-05-11 15:55:41 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 20 | #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-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 33 | static int fileAction(const char *fileName, struct stat *statbuf, |
Denis Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 34 | void ATTRIBUTE_UNUSED *junk, int depth) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 35 | { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 36 | // TODO: -H/-L/-P |
| 37 | // if (depth ... && S_ISLNK(statbuf->st_mode)) .... |
| 38 | |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 39 | if (!chown_func(fileName, |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 40 | (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid, |
| 41 | (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid) |
| 42 | ) { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 43 | if (OPT_VERBOSE |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 44 | || (OPT_CHANGED && (statbuf->st_uid != ugid.uid || statbuf->st_gid != ugid.gid)) |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 45 | ) { |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 46 | printf("changed ownership of '%s' to %u:%u\n", |
| 47 | fileName, ugid.uid, ugid.gid); |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 48 | } |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 49 | return TRUE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 50 | } |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 51 | if (!OPT_QUIET) |
| 52 | bb_perror_msg("%s", fileName); /* A filename can have % in it... */ |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 53 | return FALSE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | int chown_main(int argc, char **argv) |
| 57 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | char *groupName; |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 59 | int retval = EXIT_SUCCESS; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 61 | opt_complementary = "-2"; |
| 62 | getopt32(argc, argv, OPT_STR); |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 63 | argv += optind; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 65 | if (OPT_NODEREF) chown_func = lchown; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +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 | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 68 | groupName = strchr(*argv, '.'); /* deprecated? */ |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 69 | if (!groupName) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | groupName = strchr(*argv, ':'); |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 71 | else |
| 72 | *groupName = ':'; /* replace '.' with ':' */ |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 74 | /* First, try parsing "user[:[group]]" */ |
| 75 | if (!groupName) { /* "user" */ |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 76 | ugid.uid = get_ug_id(*argv, xuname2uid); |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 77 | } else if (groupName == *argv) { /* ":group" */ |
Denis Vlasenko | 6eebed5 | 2007-01-03 20:07:06 +0000 | [diff] [blame] | 78 | ugid.gid = get_ug_id(groupName + 1, xgroup2gid); |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 79 | } else { |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 80 | 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 84 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 85 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 86 | /* Ok, ready to do the deed now */ |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 87 | argv++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | do { |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 89 | 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 Vlasenko | 8c35d65 | 2006-10-27 23:42:25 +0000 | [diff] [blame] | 95 | NULL, // user data |
| 96 | 0) // depth |
Denis Vlasenko | 51b4c92 | 2006-10-27 16:07:20 +0000 | [diff] [blame] | 97 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | retval = EXIT_FAILURE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 99 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 100 | } while (*++argv); |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 101 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | return retval; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 103 | } |