2 * Copyright (c) 2005,2008 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)
42 # ifdef HAVE_STRINGS_H
45 #endif /* HAVE_STRING_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 ts_setgrfile __P((const char *));
68 void ts_setgrent __P((void));
69 void ts_endgrent __P((void));
70 struct group *ts_getgrent __P((void));
71 struct group *ts_getgrnam __P((const char *));
72 struct group *ts_getgrgid __P((gid_t));
74 void ts_setpwfile __P((const char *));
75 void ts_setpwent __P((void));
76 void ts_endpwent __P((void));
77 struct passwd *ts_getpwent __P((void));
78 struct passwd *ts_getpwnam __P((const char *));
79 struct passwd *ts_getpwuid __P((uid_t));
94 pwf = fopen(pwfile, "r");
113 static struct passwd pw;
114 static char pwbuf[LINE_MAX];
118 if ((colon = fgets(pwbuf, sizeof(pwbuf), pwf)) == NULL)
121 zero_bytes(&pw, sizeof(pw));
122 if ((colon = strchr(cp = colon, ':')) == NULL)
126 if ((colon = strchr(cp = colon, ':')) == NULL)
130 if ((colon = strchr(cp = colon, ':')) == NULL)
133 pw.pw_uid = atoi(cp);
134 if ((colon = strchr(cp = colon, ':')) == NULL)
137 pw.pw_gid = atoi(cp);
138 if ((colon = strchr(cp = colon, ':')) == NULL)
142 if ((colon = strchr(cp = colon, ':')) == NULL)
148 if (len > 0 && colon[len - 1] == '\n')
149 colon[len - 1] = '\0';
161 else if ((pwf = fopen(pwfile, "r")) == NULL)
163 while ((pw = ts_getpwent()) != NULL) {
164 if (strcmp(pw->pw_name, name) == 0)
182 else if ((pwf = fopen(pwfile, "r")) == NULL)
184 while ((pw = ts_getpwent()) != NULL) {
185 if (pw->pw_uid == uid)
208 grf = fopen(grfile, "r");
227 static struct group gr;
228 static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
233 if ((colon = fgets(grbuf, sizeof(grbuf), grf)) == NULL)
236 zero_bytes(&gr, sizeof(gr));
237 if ((colon = strchr(cp = colon, ':')) == NULL)
241 if ((colon = strchr(cp = colon, ':')) == NULL)
245 if ((colon = strchr(cp = colon, ':')) == NULL)
248 gr.gr_gid = atoi(cp);
250 if (len > 0 && colon[len - 1] == '\n')
251 colon[len - 1] = '\0';
252 if (*colon != '\0') {
254 cp = strtok(colon, ",");
255 for (n = 0; cp != NULL && n < GRMEM_MAX; n++) {
257 cp = strtok(NULL, ",");
259 gr.gr_mem[n++] = NULL;
273 else if ((grf = fopen(grfile, "r")) == NULL)
275 while ((gr = ts_getgrent()) != NULL) {
276 if (strcmp(gr->gr_name, name) == 0)
294 else if ((grf = fopen(grfile, "r")) == NULL)
296 while ((gr = ts_getgrent()) != NULL) {
297 if (gr->gr_gid == gid)