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