2 Copyright (c) 2001-2006, Gerrit Pape
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /* Always sets uid and gid */
31 int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok)
39 group = strchr(ug, ':');
41 int sz = (++group) - ug;
43 /* copies sz-1 bytes, stores terminating '\0' */
44 safe_strncpy(user, ug, sz);
47 n = bb_strtou(user, NULL, 10);
51 /* If we have e.g. "500" string without user */
52 /* with uid 500 in /etc/passwd, we set gid == uid */
53 u->gid = pwd ? pwd->pw_gid : n;
57 /* Either it is not numeric, or caller disallows numeric username */
67 n = bb_strtou(group, NULL, 10);
79 void FAST_FUNC xget_uidgid(struct bb_uidgid_t *u, const char *ug)
81 if (!get_uidgid(u, ug, 1))
82 bb_error_msg_and_die("unknown user/group %s", ug);
86 * "user" sets uid only,
87 * ":group" sets gid only
88 * "user:" sets uid and gid (to user's primary group id)
89 * "user:group" sets uid and gid
90 * ('unset' uid or gid retains the value it has on entry)
92 void FAST_FUNC parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group)
96 /* Check if there is a group name */
97 group = strchr(user_group, '.'); /* deprecated? */
99 group = strchr(user_group, ':');
101 *group = ':'; /* replace '.' with ':' */
103 /* Parse "user[:[group]]" */
104 if (!group) { /* "user" */
105 u->uid = get_ug_id(user_group, xuname2uid);
106 } else if (group == user_group) { /* ":group" */
107 u->gid = get_ug_id(group + 1, xgroup2gid);
109 if (!group[1]) /* "user:" */
111 xget_uidgid(u, user_group);
120 struct bb_uidgid_t ug;
121 u = get_uidgid(&ug, "apache", 0);
122 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
123 ug.uid = ug.gid = 1111;
124 u = get_uidgid(&ug, "apache", 0);
125 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
126 ug.uid = ug.gid = 1111;
127 u = get_uidgid(&ug, "apache:users", 0);
128 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
129 ug.uid = ug.gid = 1111;
130 u = get_uidgid(&ug, "apache:users", 0);
131 printf("%u = %u:%u\n", u, ug.uid, ug.gid);