3 * David L. Nugent. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 static const char rcsid[] =
36 #include <sys/param.h>
40 static FILE * pwd_fp = NULL;
57 static struct passwd *
58 vnextpwent(char const * nam, uid_t uid, int doclose)
60 struct passwd * pw = NULL;
61 static char pwtmp[1024];
63 strncpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof pwtmp);
64 pwtmp[sizeof pwtmp - 1] = '\0';
66 if (pwd_fp != NULL || (pwd_fp = fopen(pwtmp, "r")) != NULL) {
69 static struct passwd pwd;
71 while (!done && fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL)
75 char * p = strchr(pwtmp, '\n');
78 while (fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL && strchr(pwtmp, '\n')==NULL)
79 ; /* Skip long lines */
83 /* skip comments & empty lines */
84 if (*pwtmp =='\n' || *pwtmp == '#')
89 bzero(&pwd, sizeof pwd);
90 while (!quickout && (p = strsep(&q, ":\n")) != NULL) {
93 case 0: /* username */
96 if (strcmp(nam, p) == 0)
102 case 1: /* password */
106 pwd.pw_uid = atoi(p);
107 if (uid != (uid_t)-1) {
108 if (uid == pwd.pw_uid)
115 pwd.pw_gid = atoi(p);
118 if (nam == NULL && uid == (uid_t)-1)
123 pwd.pw_change = (time_t)atol(p);
126 pwd.pw_expire = (time_t)atol(p);
131 case 8: /* directory */
142 if (done && pwd.pw_name) {
145 #define CKNULL(s) s = s ? s : ""
146 CKNULL(pwd.pw_passwd);
147 CKNULL(pwd.pw_class);
148 CKNULL(pwd.pw_gecos);
150 CKNULL(pwd.pw_shell);
159 return vnextpwent(NULL, -1, 0);
165 return vnextpwent(NULL, uid, 1);
169 vgetpwnam(const char * nam)
171 return vnextpwent(nam, -1, 1);
174 int vpwdb(char *arg, ...)
182 static FILE * grp_fp = NULL;
187 if (grp_fp != NULL) {
197 #if defined(__FreeBSD__)
202 static struct group *
203 vnextgrent(char const * nam, gid_t gid, int doclose)
205 struct group * gr = NULL;
207 static char * grtmp = NULL;
208 static int grlen = 0;
209 static char ** mems = NULL;
210 static int memlen = 0;
212 extendline(&grtmp, &grlen, MAXPATHLEN);
213 strncpy(grtmp, getgrpath(_GROUP), MAXPATHLEN);
214 grtmp[MAXPATHLEN - 1] = '\0';
216 if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) {
219 static struct group grp;
221 while (!done && fgets(grtmp, grlen, grp_fp) != NULL)
228 if ((p = strchr(grtmp, '\n')) == NULL) {
230 extendline(&grtmp, &grlen, grlen + PWBUFSZ);
232 if (fgets(grtmp + l, grlen - l, grp_fp) == NULL)
233 break; /* No newline terminator on last line */
235 /* Skip comments and empty lines */
236 if (*grtmp == '\n' || *grtmp == '#')
240 bzero(&grp, sizeof grp);
241 extendarray(&mems, &memlen, 200);
242 while (!quickout && (p = strsep(&q, sep)) != NULL) {
245 case 0: /* groupname */
248 if (strcmp(nam, p) == 0)
254 case 1: /* password */
258 grp.gr_gid = atoi(p);
259 if (gid != (gid_t)-1) {
260 if (gid == (gid_t)grp.gr_gid)
264 } else if (nam == NULL)
273 extendarray(&mems, &memlen, mno + 2);
284 if (done && grp.gr_name) {
287 CKNULL(grp.gr_passwd);
296 return vnextgrent(NULL, -1, 0);
303 return vnextgrent(NULL, gid, 1);
307 vgetgrnam(const char * nam)
309 return vnextgrent(nam, -1, 1);
313 vgrdb(char *arg, ...)