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 */
48 #include "tsgetgrpw.h"
52 # define LINE_MAX 2048
59 static const char *pwfile = "/etc/passwd";
60 static int pw_stayopen;
63 static const char *grfile = "/etc/group";
64 static int gr_stayopen;
79 pwf = fopen(pwfile, "r");
81 fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
101 static struct passwd pw;
102 static char pwbuf[LINE_MAX];
106 if ((colon = fgets(pwbuf, sizeof(pwbuf), pwf)) == NULL)
109 zero_bytes(&pw, sizeof(pw));
110 if ((colon = strchr(cp = colon, ':')) == NULL)
114 if ((colon = strchr(cp = colon, ':')) == NULL)
118 if ((colon = strchr(cp = colon, ':')) == NULL)
121 pw.pw_uid = atoi(cp);
122 if ((colon = strchr(cp = colon, ':')) == NULL)
125 pw.pw_gid = atoi(cp);
126 if ((colon = strchr(cp = colon, ':')) == NULL)
130 if ((colon = strchr(cp = colon, ':')) == NULL)
136 if (len > 0 && colon[len - 1] == '\n')
137 colon[len - 1] = '\0';
148 if ((pwf = fopen(pwfile, "r")) == NULL)
150 fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
154 while ((pw = getpwent()) != NULL) {
155 if (strcmp(pw->pw_name, name) == 0)
172 if ((pwf = fopen(pwfile, "r")) == NULL)
174 fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
178 while ((pw = getpwent()) != NULL) {
179 if (pw->pw_uid == uid)
202 grf = fopen(grfile, "r");
204 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
224 static struct group gr;
225 static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
230 if ((colon = fgets(grbuf, sizeof(grbuf), grf)) == NULL)
233 zero_bytes(&gr, sizeof(gr));
234 if ((colon = strchr(cp = colon, ':')) == NULL)
238 if ((colon = strchr(cp = colon, ':')) == NULL)
242 if ((colon = strchr(cp = colon, ':')) == NULL)
245 gr.gr_gid = atoi(cp);
247 if (len > 0 && colon[len - 1] == '\n')
248 colon[len - 1] = '\0';
249 if (*colon != '\0') {
251 cp = strtok(colon, ",");
252 for (n = 0; cp != NULL && n < GRMEM_MAX; n++) {
254 cp = strtok(NULL, ",");
256 gr.gr_mem[n++] = NULL;
269 if ((grf = fopen(grfile, "r")) == NULL)
271 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
275 while ((gr = getgrent()) != NULL) {
276 if (strcmp(gr->gr_name, name) == 0)
293 if ((grf = fopen(grfile, "r")) == NULL)
295 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
299 while ((gr = getgrent()) != NULL) {
300 if (gr->gr_gid == gid)