blob: 8388be0dadc97332f831a247d67b8eb52a85de52 [file] [log] [blame]
Denis Vlasenkocb04ff52006-12-30 21:11:57 +00001/*
2Copyright (c) 2001-2006, Gerrit Pape
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
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.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000029
Denis Vlasenkocce38582007-02-26 22:47:42 +000030/* Always sets uid and gid */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000031int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok)
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000032{
33 struct passwd *pwd;
34 struct group *gr;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000035 char *user, *group;
36 unsigned n;
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000037
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000038 user = (char*)ug;
39 group = strchr(ug, ':');
40 if (group) {
41 int sz = (++group) - ug;
42 user = alloca(sz);
43 /* copies sz-1 bytes, stores terminating '\0' */
44 safe_strncpy(user, ug, sz);
45 }
46 if (numeric_ok) {
47 n = bb_strtou(user, NULL, 10);
48 if (!errno) {
49 u->uid = n;
50 pwd = getpwuid(n);
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;
54 goto skip;
55 }
56 }
Denis Vlasenkocce38582007-02-26 22:47:42 +000057 /* Either it is not numeric, or caller disallows numeric username */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000058 pwd = getpwnam(user);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000059 if (!pwd)
60 return 0;
61 u->uid = pwd->pw_uid;
62 u->gid = pwd->pw_gid;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000063
64 skip:
65 if (group) {
66 if (numeric_ok) {
67 n = bb_strtou(group, NULL, 10);
68 if (!errno) {
69 u->gid = n;
70 return 1;
71 }
72 }
73 gr = getgrnam(group);
Denys Vlasenko4566e172011-05-16 00:01:08 +020074 if (!gr)
75 return 0;
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000076 u->gid = gr->gr_gid;
77 }
78 return 1;
79}
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +000080void FAST_FUNC xget_uidgid(struct bb_uidgid_t *u, const char *ug)
81{
Denis Vlasenkob74a2db2008-07-21 21:34:51 +000082 if (!get_uidgid(u, ug, 1))
83 bb_error_msg_and_die("unknown user/group %s", ug);
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +000084}
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000085
Denis Vlasenkocce38582007-02-26 22:47:42 +000086/* chown-like:
87 * "user" sets uid only,
88 * ":group" sets gid only
89 * "user:" sets uid and gid (to user's primary group id)
90 * "user:group" sets uid and gid
Denis Vlasenko8d89bed2008-09-07 23:22:08 +000091 * ('unset' uid or gid retains the value it has on entry)
Denis Vlasenkocce38582007-02-26 22:47:42 +000092 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000093void FAST_FUNC parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group)
Denis Vlasenkocce38582007-02-26 22:47:42 +000094{
95 char *group;
96
Denis Vlasenkocce38582007-02-26 22:47:42 +000097 /* Check if there is a group name */
98 group = strchr(user_group, '.'); /* deprecated? */
99 if (!group)
100 group = strchr(user_group, ':');
101 else
102 *group = ':'; /* replace '.' with ':' */
103
104 /* Parse "user[:[group]]" */
105 if (!group) { /* "user" */
106 u->uid = get_ug_id(user_group, xuname2uid);
107 } else if (group == user_group) { /* ":group" */
108 u->gid = get_ug_id(group + 1, xgroup2gid);
109 } else {
110 if (!group[1]) /* "user:" */
111 *group = '\0';
Bernhard Reutner-Fischerd73cbd32008-07-21 14:41:33 +0000112 xget_uidgid(u, user_group);
Denis Vlasenkocce38582007-02-26 22:47:42 +0000113 }
114}
115
Denis Vlasenkode59c0f2006-10-05 22:50:22 +0000116#if 0
117#include <stdio.h>
118int main()
119{
120 unsigned u;
121 struct bb_uidgid_t ug;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000122 u = get_uidgid(&ug, "apache", 0);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +0000123 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
124 ug.uid = ug.gid = 1111;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000125 u = get_uidgid(&ug, "apache", 0);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +0000126 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
127 ug.uid = ug.gid = 1111;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000128 u = get_uidgid(&ug, "apache:users", 0);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +0000129 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
130 ug.uid = ug.gid = 1111;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000131 u = get_uidgid(&ug, "apache:users", 0);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +0000132 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
133 return 0;
134}
135#endif