blob: 69c228e163ab0551b3d938994e136c6769384718 [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 Vlasenkode59c0f2006-10-05 22:50:22 +000028#include "busybox.h"
29
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000030int get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok)
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000031{
32 struct passwd *pwd;
33 struct group *gr;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000034 char *user, *group;
35 unsigned n;
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000036
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000037 user = (char*)ug;
38 group = strchr(ug, ':');
39 if (group) {
40 int sz = (++group) - ug;
41 user = alloca(sz);
42 /* copies sz-1 bytes, stores terminating '\0' */
43 safe_strncpy(user, ug, sz);
44 }
45 if (numeric_ok) {
46 n = bb_strtou(user, NULL, 10);
47 if (!errno) {
48 u->uid = n;
49 pwd = getpwuid(n);
50 /* If we have e.g. "500" string without user */
51 /* with uid 500 in /etc/passwd, we set gid == uid */
52 u->gid = pwd ? pwd->pw_gid : n;
53 goto skip;
54 }
55 }
56 pwd = getpwnam(user);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000057 if (!pwd)
58 return 0;
59 u->uid = pwd->pw_uid;
60 u->gid = pwd->pw_gid;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000061
62 skip:
63 if (group) {
64 if (numeric_ok) {
65 n = bb_strtou(group, NULL, 10);
66 if (!errno) {
67 u->gid = n;
68 return 1;
69 }
70 }
71 gr = getgrnam(group);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000072 if (!gr) return 0;
73 u->gid = gr->gr_gid;
74 }
75 return 1;
76}
77
78#if 0
79#include <stdio.h>
80int main()
81{
82 unsigned u;
83 struct bb_uidgid_t ug;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000084 u = get_uidgid(&ug, "apache", 0);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000085 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
86 ug.uid = ug.gid = 1111;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000087 u = get_uidgid(&ug, "apache", 0);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000088 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
89 ug.uid = ug.gid = 1111;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000090 u = get_uidgid(&ug, "apache:users", 0);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000091 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
92 ug.uid = ug.gid = 1111;
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000093 u = get_uidgid(&ug, "apache:users", 0);
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000094 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
95 return 0;
96}
97#endif