2 * Copyright (c) 2005, 2008, 2010-2011 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;
66 void setgrfile(const char *);
69 struct group *getgrent(void);
70 struct group *getgrnam(const char *);
71 struct group *getgrgid(gid_t);
73 void setpwfile(const char *);
76 struct passwd *getpwent(void);
77 struct passwd *getpwnam(const char *);
78 struct passwd *getpwuid(uid_t);
81 setpwfile(const char *file)
92 pwf = fopen(pwfile, "r");
94 fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
114 static struct passwd pw;
115 static char pwbuf[LINE_MAX];
119 if ((colon = fgets(pwbuf, sizeof(pwbuf), pwf)) == NULL)
122 zero_bytes(&pw, sizeof(pw));
123 if ((colon = strchr(cp = colon, ':')) == NULL)
127 if ((colon = strchr(cp = colon, ':')) == NULL)
131 if ((colon = strchr(cp = colon, ':')) == NULL)
134 pw.pw_uid = atoi(cp);
135 if ((colon = strchr(cp = colon, ':')) == NULL)
138 pw.pw_gid = atoi(cp);
139 if ((colon = strchr(cp = colon, ':')) == NULL)
143 if ((colon = strchr(cp = colon, ':')) == NULL)
149 if (len > 0 && colon[len - 1] == '\n')
150 colon[len - 1] = '\0';
155 getpwnam(const char *name)
160 if ((pwf = fopen(pwfile, "r")) == NULL)
162 fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
166 while ((pw = getpwent()) != NULL) {
167 if (strcmp(pw->pw_name, name) == 0)
183 if ((pwf = fopen(pwfile, "r")) == NULL)
185 fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
189 while ((pw = getpwent()) != NULL) {
190 if (pw->pw_uid == uid)
201 setgrfile(const char *file)
212 grf = fopen(grfile, "r");
214 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
234 static struct group gr;
235 static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
240 if ((colon = fgets(grbuf, sizeof(grbuf), grf)) == NULL)
243 zero_bytes(&gr, sizeof(gr));
244 if ((colon = strchr(cp = colon, ':')) == NULL)
248 if ((colon = strchr(cp = colon, ':')) == NULL)
252 if ((colon = strchr(cp = colon, ':')) == NULL)
255 gr.gr_gid = atoi(cp);
257 if (len > 0 && colon[len - 1] == '\n')
258 colon[len - 1] = '\0';
259 if (*colon != '\0') {
261 cp = strtok(colon, ",");
262 for (n = 0; cp != NULL && n < GRMEM_MAX; n++) {
264 cp = strtok(NULL, ",");
266 gr.gr_mem[n++] = NULL;
273 getgrnam(const char *name)
278 if ((grf = fopen(grfile, "r")) == NULL)
280 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
284 while ((gr = getgrent()) != NULL) {
285 if (strcmp(gr->gr_name, name) == 0)
301 if ((grf = fopen(grfile, "r")) == NULL)
303 fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
307 while ((gr = getgrent()) != NULL) {
308 if (gr->gr_gid == gid)