chown: fix handling of "user.group" notation
[platform/upstream/busybox.git] / coreutils / chown.c
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, -L, and -P. */
11 /* BB_AUDIT GNU defects - unsupported long options. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
13
14 #include "busybox.h"
15
16 static struct bb_uidgid_t ugid = { -1, -1 };
17
18 static int (*chown_func)(const char *, uid_t, gid_t) = chown;
19
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
33 static int fileAction(const char *fileName, struct stat *statbuf,
34                 void ATTRIBUTE_UNUSED *junk, int depth)
35 {
36         // TODO: -H/-L/-P
37         // if (depth ... && S_ISLNK(statbuf->st_mode)) ....
38
39         if (!chown_func(fileName,
40                         (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid,
41                         (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid)
42         ) {
43                 if (OPT_VERBOSE
44                  || (OPT_CHANGED && (statbuf->st_uid != ugid.uid || statbuf->st_gid != ugid.gid))
45                 ) {
46                         printf("changed ownership of '%s' to %u:%u\n",
47                                         fileName, ugid.uid, ugid.gid);
48                 }
49                 return TRUE;
50         }
51         if (!OPT_QUIET)
52                 bb_perror_msg("%s", fileName);  /* A filename can have % in it... */
53         return FALSE;
54 }
55
56 int chown_main(int argc, char **argv)
57 {
58         char *groupName;
59         int retval = EXIT_SUCCESS;
60
61         opt_complementary = "-2";
62         getopt32(argc, argv, OPT_STR);
63         argv += optind;
64
65         if (OPT_NODEREF) chown_func = lchown;
66
67         /* First, check if there is a group name here */
68         groupName = strchr(*argv, '.'); /* deprecated? */
69         if (!groupName)
70                 groupName = strchr(*argv, ':');
71         else
72                 *groupName = ':'; /* replace '.' with ':' */
73
74         /* First, try parsing "user[:[group]]" */
75         if (!groupName) { /* "user" */
76                 ugid.uid = get_ug_id(*argv, xuname2uid);
77         } else if (groupName == *argv) { /* ":group" */
78                 ugid.gid = get_ug_id(groupName + 1, xgroup2gid);
79         } else {
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);
84         }
85
86         /* Ok, ready to do the deed now */
87         argv++;
88         do {
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
95                                 NULL,           // user data
96                                 0)              // depth 
97                 ) {
98                         retval = EXIT_FAILURE;
99                 }
100         } while (*++argv);
101
102         return retval;
103 }