Expand PMF_FN_* macros.
[netbsd-mini2440.git] / tests / util / id / pwgr.c
blob48b649a83a8b4d3c098e2a5a7f8b9fc261a6eb22
1 /* $NetBSD: pwgr.c,v 1.1 2007/11/19 14:17:45 jmmv Exp $ */
2 /*
3 * Copyright (c) 2007 The NetBSD Foundation, Inc.
4 * All rights reserved.
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.
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.
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).
36 #include <sys/types.h>
38 #include <errno.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #include <stdlib.h>
42 #include <string.h>
44 char Login[16];
45 struct group GrEntry;
46 struct passwd PwEntry;
48 gid_t
49 getgid(void)
51 return 100;
54 gid_t
55 getegid(void)
57 if (getenv("LIBFAKE_EGID_ROOT") != NULL)
58 return 0;
59 else
60 return 100;
63 uid_t
64 getuid(void)
66 return 100;
69 uid_t
70 geteuid(void)
72 if (getenv("LIBFAKE_EUID_ROOT") != NULL)
73 return 0;
74 else
75 return 100;
78 char *
79 getlogin(void)
81 strcpy(Login, "test");
82 return Login;
85 struct group *
86 getgrgid(gid_t gid)
88 struct group *g = &GrEntry;
90 memset(g, 0, sizeof(*g));
91 if (gid == 0) {
92 g->gr_name = "wheel";
93 g->gr_gid = 0;
94 } else if (gid == 100) {
95 g->gr_name = "users";
96 g->gr_gid = 100;
97 } else
98 g = NULL;
100 return g;
104 getgrouplist(const char *name, int basegid, int *groups, int *ngroups)
106 int cnt, ret;
108 if (name == "root") {
109 if (*ngroups >= 1) {
110 groups[0] = basegid;
111 cnt = 1;
114 ret = (*ngroups >= cnt) ? 0 : -1;
115 *ngroups = cnt;
116 } else if (name == "test") {
117 if (*ngroups >= 1) {
118 groups[0] = basegid;
119 cnt = 1;
122 if (*ngroups >= 2) {
123 groups[1] = 0;
124 cnt = 2;
127 ret = (*ngroups >= cnt) ? 0 : -1;
128 *ngroups = cnt;
129 } else
130 ret = -1;
132 return ret;
136 getgroups(int gidsetlen, gid_t *gidset)
138 if (gidsetlen < 2) {
139 errno = EINVAL;
140 return -1;
143 gidset[0] = 100;
144 gidset[1] = 0;
145 return 2;
148 struct passwd *
149 getpwnam(const char *login)
151 struct passwd *p = &PwEntry;
153 memset(p, 0, sizeof(*p));
154 if (strcmp(login, "root") == 0) {
155 p->pw_name = "root";
156 p->pw_uid = 0;
157 p->pw_gid = 0;
158 } else if (strcmp(login, "test") == 0) {
159 p->pw_name = "test";
160 p->pw_uid = 100;
161 p->pw_gid = 100;
162 } else
163 p = NULL;
165 return p;
168 struct passwd *
169 getpwuid(uid_t uid)
171 struct passwd *p = &PwEntry;
173 memset(p, 0, sizeof(*p));
174 if (uid == 0) {
175 p->pw_name = "root";
176 p->pw_uid = 0;
177 p->pw_gid = 0;
178 } else if (uid == 100) {
179 p->pw_name = "test";
180 p->pw_uid = 100;
181 p->pw_gid = 100;
182 } else
183 p = NULL;
185 return p;