[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Mini chown implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
---|
| 6 | *
|
---|
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 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 |
|
---|
| 15 | #include <stdlib.h>
|
---|
| 16 | #include <unistd.h>
|
---|
| 17 | #include <string.h>
|
---|
| 18 | #include "busybox.h"
|
---|
| 19 |
|
---|
| 20 | static uid_t uid = -1;
|
---|
| 21 | static gid_t gid = -1;
|
---|
| 22 |
|
---|
| 23 | static int (*chown_func)(const char *, uid_t, gid_t) = chown;
|
---|
| 24 |
|
---|
| 25 | static int fileAction(const char *fileName, struct stat *statbuf,
|
---|
| 26 | void ATTRIBUTE_UNUSED *junk)
|
---|
| 27 | {
|
---|
| 28 | if (!chown_func(fileName,
|
---|
| 29 | (uid == (uid_t)-1) ? statbuf->st_uid : uid,
|
---|
| 30 | (gid == (gid_t)-1) ? statbuf->st_gid : gid)) {
|
---|
| 31 | return TRUE;
|
---|
| 32 | }
|
---|
| 33 | bb_perror_msg("%s", fileName); /* A filename could have % in it... */
|
---|
| 34 | return FALSE;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | #define FLAG_R 1
|
---|
| 38 | #define FLAG_h 2
|
---|
| 39 |
|
---|
| 40 | int chown_main(int argc, char **argv)
|
---|
| 41 | {
|
---|
| 42 | int flags;
|
---|
| 43 | int retval = EXIT_SUCCESS;
|
---|
| 44 | char *groupName;
|
---|
| 45 |
|
---|
| 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();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | argv += optind;
|
---|
| 55 |
|
---|
| 56 | /* First, check if there is a group name here */
|
---|
| 57 | if ((groupName = strchr(*argv, '.')) == NULL) {
|
---|
| 58 | groupName = strchr(*argv, ':');
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /* Check for the username and groupname */
|
---|
| 62 | if (groupName) {
|
---|
| 63 | *groupName++ = '\0';
|
---|
| 64 | gid = get_ug_id(groupName, bb_xgetgrnam);
|
---|
| 65 | }
|
---|
| 66 | if (--groupName != *argv) uid = get_ug_id(*argv, bb_xgetpwnam);
|
---|
| 67 | ++argv;
|
---|
| 68 |
|
---|
| 69 | /* Ok, ready to do the deed now */
|
---|
| 70 | do {
|
---|
| 71 | if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
|
---|
| 72 | fileAction, fileAction, NULL)) {
|
---|
| 73 | retval = EXIT_FAILURE;
|
---|
| 74 | }
|
---|
| 75 | } while (*++argv);
|
---|
| 76 |
|
---|
| 77 | return retval;
|
---|
| 78 | }
|
---|