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 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */ |
| 11 | /* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */ |
| 12 | /* BB_AUDIT Note: gnu chown does not support -H, -L, or -P. */ |
| 13 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */ |
| 14 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 17 | #include <string.h> |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 18 | #include "busybox.h" |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 19 | |
Rob Landley | fb8c498 | 2006-01-09 03:45:43 +0000 | [diff] [blame] | 20 | static uid_t uid = -1; |
| 21 | static gid_t gid = -1; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 22 | |
Glenn L McGrath | a88cb50 | 2003-02-08 23:36:16 +0000 | [diff] [blame] | 23 | static int (*chown_func)(const char *, uid_t, gid_t) = chown; |
Eric Andersen | 2b02ab9 | 2001-05-11 15:55:41 +0000 | [diff] [blame] | 24 | |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 25 | static int fileAction(const char *fileName, struct stat *statbuf, |
| 26 | void ATTRIBUTE_UNUSED *junk) |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 27 | { |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 28 | if (!chown_func(fileName, |
"Vladimir N. Oleynik" | 57545c8 | 2006-01-31 12:06:57 +0000 | [diff] [blame] | 29 | (uid == (uid_t)-1) ? statbuf->st_uid : uid, |
| 30 | (gid == (gid_t)-1) ? statbuf->st_gid : gid)) { |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 31 | return TRUE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 32 | } |
Rob Landley | f8ec1b5 | 2006-01-06 18:22:05 +0000 | [diff] [blame] | 33 | bb_perror_msg("%s", fileName); /* A filename could have % in it... */ |
| 34 | return FALSE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | #define FLAG_R 1 |
| 38 | #define FLAG_h 2 |
| 39 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 40 | int chown_main(int argc, char **argv) |
| 41 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | int flags; |
| 43 | int retval = EXIT_SUCCESS; |
| 44 | char *groupName; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 45 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | flags = bb_getopt_ulflags(argc, argv, "Rh"); |
| 47 | |
| 48 | if (flags & FLAG_h) chown_func = lchown; |
| 49 | |
| 50 | if (argc - optind < 2) { |
| 51 | bb_show_usage(); |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | argv += optind; |
Eric Andersen | 2b02ab9 | 2001-05-11 15:55:41 +0000 | [diff] [blame] | 55 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | /* First, check if there is a group name here */ |
| 57 | if ((groupName = strchr(*argv, '.')) == NULL) { |
| 58 | groupName = strchr(*argv, ':'); |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Rob Landley | fb8c498 | 2006-01-09 03:45:43 +0000 | [diff] [blame] | 61 | /* Check for the username and groupname */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | if (groupName) { |
| 63 | *groupName++ = '\0'; |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 64 | gid = get_ug_id(groupName, bb_xgetgrnam); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | } |
Rob Landley | fb8c498 | 2006-01-09 03:45:43 +0000 | [diff] [blame] | 66 | if (--groupName != *argv) uid = get_ug_id(*argv, bb_xgetpwnam); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | ++argv; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 68 | |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 69 | /* Ok, ready to do the deed now */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | do { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 71 | if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE, |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | fileAction, fileAction, NULL)) { |
| 73 | retval = EXIT_FAILURE; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 74 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | } while (*++argv); |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 76 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | return retval; |
Eric Andersen | 9f0fedb | 2001-04-24 18:07:19 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* |
| 81 | Local Variables: |
| 82 | c-file-style: "linux" |
| 83 | c-basic-offset: 4 |
| 84 | tab-width: 4 |
| 85 | End: |
| 86 | */ |