1*28604916Sjruoho /* $NetBSD: pwgr.c,v 1.1 2012/03/17 16:33:14 jruoho Exp $ */
2*28604916Sjruoho /*
3*28604916Sjruoho * Copyright (c) 2007 The NetBSD Foundation, Inc.
4*28604916Sjruoho * All rights reserved.
5*28604916Sjruoho *
6*28604916Sjruoho * Redistribution and use in source and binary forms, with or without
7*28604916Sjruoho * modification, are permitted provided that the following conditions
8*28604916Sjruoho * are met:
9*28604916Sjruoho * 1. Redistributions of source code must retain the above copyright
10*28604916Sjruoho * notice, this list of conditions and the following disclaimer.
11*28604916Sjruoho * 2. Redistributions in binary form must reproduce the above copyright
12*28604916Sjruoho * notice, this list of conditions and the following disclaimer in the
13*28604916Sjruoho * documentation and/or other materials provided with the distribution.
14*28604916Sjruoho *
15*28604916Sjruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16*28604916Sjruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17*28604916Sjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18*28604916Sjruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19*28604916Sjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*28604916Sjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*28604916Sjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*28604916Sjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*28604916Sjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*28604916Sjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*28604916Sjruoho * POSSIBILITY OF SUCH DAMAGE.
26*28604916Sjruoho */
27*28604916Sjruoho
28*28604916Sjruoho /*
29*28604916Sjruoho * This file implements replacements for all user/group-related functions
30*28604916Sjruoho * called by id(1). It provides fake but deterministic user and group
31*28604916Sjruoho * information. The details are as such:
32*28604916Sjruoho * User root, uid 0, primary group 0 (wheel).
33*28604916Sjruoho * User test, uid 100, primary group 100 (users), secondary group 0 (wheel).
34*28604916Sjruoho */
35*28604916Sjruoho
36*28604916Sjruoho #include <sys/types.h>
37*28604916Sjruoho
38*28604916Sjruoho #include <errno.h>
39*28604916Sjruoho #include <grp.h>
40*28604916Sjruoho #include <pwd.h>
41*28604916Sjruoho #include <stdlib.h>
42*28604916Sjruoho #include <unistd.h>
43*28604916Sjruoho #include <string.h>
44*28604916Sjruoho
45*28604916Sjruoho char Login[16];
46*28604916Sjruoho struct group GrEntry;
47*28604916Sjruoho struct passwd PwEntry;
48*28604916Sjruoho
49*28604916Sjruoho gid_t
getgid(void)50*28604916Sjruoho getgid(void)
51*28604916Sjruoho {
52*28604916Sjruoho return 100;
53*28604916Sjruoho }
54*28604916Sjruoho
55*28604916Sjruoho gid_t
getegid(void)56*28604916Sjruoho getegid(void)
57*28604916Sjruoho {
58*28604916Sjruoho if (getenv("LIBFAKE_EGID_ROOT") != NULL)
59*28604916Sjruoho return 0;
60*28604916Sjruoho else
61*28604916Sjruoho return 100;
62*28604916Sjruoho }
63*28604916Sjruoho
64*28604916Sjruoho uid_t
getuid(void)65*28604916Sjruoho getuid(void)
66*28604916Sjruoho {
67*28604916Sjruoho return 100;
68*28604916Sjruoho }
69*28604916Sjruoho
70*28604916Sjruoho uid_t
geteuid(void)71*28604916Sjruoho geteuid(void)
72*28604916Sjruoho {
73*28604916Sjruoho if (getenv("LIBFAKE_EUID_ROOT") != NULL)
74*28604916Sjruoho return 0;
75*28604916Sjruoho else
76*28604916Sjruoho return 100;
77*28604916Sjruoho }
78*28604916Sjruoho
79*28604916Sjruoho char *
getlogin(void)80*28604916Sjruoho getlogin(void)
81*28604916Sjruoho {
82*28604916Sjruoho strcpy(Login, "test");
83*28604916Sjruoho return Login;
84*28604916Sjruoho }
85*28604916Sjruoho
86*28604916Sjruoho struct group *
getgrgid(gid_t gid)87*28604916Sjruoho getgrgid(gid_t gid)
88*28604916Sjruoho {
89*28604916Sjruoho struct group *g = &GrEntry;
90*28604916Sjruoho
91*28604916Sjruoho memset(g, 0, sizeof(*g));
92*28604916Sjruoho if (gid == 0) {
93*28604916Sjruoho g->gr_name = __UNCONST("wheel");
94*28604916Sjruoho g->gr_gid = 0;
95*28604916Sjruoho } else if (gid == 100) {
96*28604916Sjruoho g->gr_name = __UNCONST("users");
97*28604916Sjruoho g->gr_gid = 100;
98*28604916Sjruoho } else
99*28604916Sjruoho g = NULL;
100*28604916Sjruoho
101*28604916Sjruoho return g;
102*28604916Sjruoho }
103*28604916Sjruoho
104*28604916Sjruoho int
getgrouplist(const char * name,gid_t basegid,gid_t * groups,int * ngroups)105*28604916Sjruoho getgrouplist(const char *name, gid_t basegid, gid_t *groups, int *ngroups)
106*28604916Sjruoho {
107*28604916Sjruoho int cnt, ret;
108*28604916Sjruoho
109*28604916Sjruoho if (strcmp(name, "root") == 0) {
110*28604916Sjruoho if (*ngroups >= 1) {
111*28604916Sjruoho groups[0] = basegid;
112*28604916Sjruoho cnt = 1;
113*28604916Sjruoho }
114*28604916Sjruoho
115*28604916Sjruoho ret = (*ngroups >= cnt) ? 0 : -1;
116*28604916Sjruoho *ngroups = cnt;
117*28604916Sjruoho } else if (strcmp(name, "test") == 0) {
118*28604916Sjruoho if (*ngroups >= 1) {
119*28604916Sjruoho groups[0] = basegid;
120*28604916Sjruoho cnt = 1;
121*28604916Sjruoho }
122*28604916Sjruoho
123*28604916Sjruoho if (*ngroups >= 2) {
124*28604916Sjruoho groups[1] = 0;
125*28604916Sjruoho cnt = 2;
126*28604916Sjruoho }
127*28604916Sjruoho
128*28604916Sjruoho ret = (*ngroups >= cnt) ? 0 : -1;
129*28604916Sjruoho *ngroups = cnt;
130*28604916Sjruoho } else
131*28604916Sjruoho ret = -1;
132*28604916Sjruoho
133*28604916Sjruoho return ret;
134*28604916Sjruoho }
135*28604916Sjruoho
136*28604916Sjruoho int
getgroups(int gidsetlen,gid_t * gidset)137*28604916Sjruoho getgroups(int gidsetlen, gid_t *gidset)
138*28604916Sjruoho {
139*28604916Sjruoho if (gidsetlen < 2) {
140*28604916Sjruoho errno = EINVAL;
141*28604916Sjruoho return -1;
142*28604916Sjruoho }
143*28604916Sjruoho
144*28604916Sjruoho gidset[0] = 100;
145*28604916Sjruoho gidset[1] = 0;
146*28604916Sjruoho return 2;
147*28604916Sjruoho }
148*28604916Sjruoho
149*28604916Sjruoho struct passwd *
getpwnam(const char * login)150*28604916Sjruoho getpwnam(const char *login)
151*28604916Sjruoho {
152*28604916Sjruoho struct passwd *p = &PwEntry;
153*28604916Sjruoho
154*28604916Sjruoho memset(p, 0, sizeof(*p));
155*28604916Sjruoho if (strcmp(login, "root") == 0) {
156*28604916Sjruoho p->pw_name = __UNCONST("root");
157*28604916Sjruoho p->pw_uid = 0;
158*28604916Sjruoho p->pw_gid = 0;
159*28604916Sjruoho } else if (strcmp(login, "test") == 0) {
160*28604916Sjruoho p->pw_name = __UNCONST("test");
161*28604916Sjruoho p->pw_uid = 100;
162*28604916Sjruoho p->pw_gid = 100;
163*28604916Sjruoho } else
164*28604916Sjruoho p = NULL;
165*28604916Sjruoho
166*28604916Sjruoho return p;
167*28604916Sjruoho }
168*28604916Sjruoho
169*28604916Sjruoho struct passwd *
getpwuid(uid_t uid)170*28604916Sjruoho getpwuid(uid_t uid)
171*28604916Sjruoho {
172*28604916Sjruoho struct passwd *p = &PwEntry;
173*28604916Sjruoho
174*28604916Sjruoho memset(p, 0, sizeof(*p));
175*28604916Sjruoho if (uid == 0) {
176*28604916Sjruoho p->pw_name = __UNCONST("root");
177*28604916Sjruoho p->pw_uid = 0;
178*28604916Sjruoho p->pw_gid = 0;
179*28604916Sjruoho } else if (uid == 100) {
180*28604916Sjruoho p->pw_name = __UNCONST("test");
181*28604916Sjruoho p->pw_uid = 100;
182*28604916Sjruoho p->pw_gid = 100;
183*28604916Sjruoho } else
184*28604916Sjruoho p = NULL;
185*28604916Sjruoho
186*28604916Sjruoho return p;
187*28604916Sjruoho }
188