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