2 * Copyright (c) 2005, 2008, 2010 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Trivial replacements for the libc get{gr,pw}{uid,nam}() routines
19 * for use by testsudoers in the sudo test harness.
20 * We need our own since many platforms don't provide set{pw,gr}file().
25 #include <sys/types.h>
26 #include <sys/param.h>
35 #endif /* STDC_HEADERS */
37 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
41 #endif /* HAVE_STRING_H */
44 #endif /* HAVE_STRINGS_H */
53 # define LINE_MAX 2048
60 static const char *pwfile = "/etc/passwd";
61 static int pw_stayopen;
64 static const char *grfile = "/etc/group";
65 static int gr_stayopen;
67 void setgrfile __P((const char *));
68 void setgrent __P((void));
69 void endgrent __P((void));
70 struct group *getgrent __P((void));
71 struct group *getgrnam __P((const char *));
72 struct group *getgrgid __P((gid_t));
74 void setpwfile __P((const char *));
75 void setpwent __P((void));
76 void endpwent __P((void));
77 struct passwd *getpwent __P((void));
78 struct passwd *getpwnam __P((const char *));
79 struct passwd *getpwuid __P((uid_t));
94 pwf = fopen(pwfile, "r");
96 fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
116 static struct passwd pw;
117 static char pwbuf[LINE_MAX];
121 if ((colon = fgets(pwbuf, sizeof(pwbuf), pwf)) == NULL)
124 zero_bytes(&pw, sizeof(pw));
125 if ((colon = strchr(cp = colon, ':')) == NULL)
129 if ((colon = strchr(cp = colon, ':')) == NULL)
133 if ((colon = strchr(cp = colon, ':')) == NULL)
136 pw.pw_uid = atoi(cp);
137 if ((colon = strchr(cp = colon, ':')) == NULL)
140 pw.pw_gid = atoi(cp);
141 if ((colon = strchr(cp = colon, ':')) == NULL)
145 if ((colon = strchr(cp = colon, ':')) == NULL)
151 if (len > 0 && colon[len - 1] == '\n')
152 colon[len - 1] = '\0';
163 if ((pwf = fopen(pwfile, "r")) == NULL)
165 fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
169 while ((pw = getpwent()) != NULL) {
170 if (strcmp(pw->pw_name, name) == 0)
187 if ((pwf = fopen(pwfile, "r")) == NULL)
189 fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
193 while ((pw = getpwent()) != NULL) {
194 if (pw->pw_uid == uid)
217 grf = fopen(grfile, "r");
219 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
239 static struct group gr;
240 static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
245 if ((colon = fgets(grbuf, sizeof(grbuf), grf)) == NULL)
248 zero_bytes(&gr, sizeof(gr));
249 if ((colon = strchr(cp = colon, ':')) == NULL)
253 if ((colon = strchr(cp = colon, ':')) == NULL)
257 if ((colon = strchr(cp = colon, ':')) == NULL)
260 gr.gr_gid = atoi(cp);
262 if (len > 0 && colon[len - 1] == '\n')
263 colon[len - 1] = '\0';
264 if (*colon != '\0') {
266 cp = strtok(colon, ",");
267 for (n = 0; cp != NULL && n < GRMEM_MAX; n++) {
269 cp = strtok(NULL, ",");
271 gr.gr_mem[n++] = NULL;
284 if ((grf = fopen(grfile, "r")) == NULL)
286 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
290 while ((gr = getgrent()) != NULL) {
291 if (strcmp(gr->gr_name, name) == 0)
308 if ((grf = fopen(grfile, "r")) == NULL)
310 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
314 while ((gr = getgrent()) != NULL) {
315 if (gr->gr_gid == gid)