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
26 * $FreeBSD: src/usr.sbin/pw/pw_vpw.c,v 1.4 2004/06/17 14:07:16 robert Exp $
32 #include <sys/param.h>
36 static FILE * pwd_fp = NULL;
53 static struct passwd *
54 vnextpwent(char const * nam, uid_t uid, int doclose)
56 struct passwd * pw = NULL;
57 static char pwtmp[1024];
59 strlcpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof(pwtmp));
61 if (pwd_fp != NULL || (pwd_fp = fopen(pwtmp, "r")) != NULL) {
64 static struct passwd pwd;
66 while (!done && fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL)
70 char * p = strchr(pwtmp, '\n');
73 while (fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL && strchr(pwtmp, '\n')==NULL)
74 ; /* Skip long lines */
78 /* skip comments & empty lines */
79 if (*pwtmp =='\n' || *pwtmp == '#')
84 bzero(&pwd, sizeof pwd);
85 while (!quickout && (p = strsep(&q, ":\n")) != NULL) {
88 case 0: /* username */
91 if (strcmp(nam, p) == 0)
97 case 1: /* password */
101 pwd.pw_uid = atoi(p);
102 if (uid != (uid_t)-1) {
103 if (uid == pwd.pw_uid)
110 pwd.pw_gid = atoi(p);
113 if (nam == NULL && uid == (uid_t)-1)
118 pwd.pw_change = (time_t)atol(p);
121 pwd.pw_expire = (time_t)atol(p);
126 case 8: /* directory */
137 if (done && pwd.pw_name) {
140 #define CKNULL(s) s = s ? s : ""
141 CKNULL(pwd.pw_passwd);
142 CKNULL(pwd.pw_class);
143 CKNULL(pwd.pw_gecos);
145 CKNULL(pwd.pw_shell);
154 return vnextpwent(NULL, -1, 0);
160 return vnextpwent(NULL, uid, 1);
164 vgetpwnam(const char * nam)
166 return vnextpwent(nam, -1, 1);
170 vpwdb(char *arg __unused, ...)
177 static FILE * grp_fp = NULL;
182 if (grp_fp != NULL) {
194 static struct group *
195 vnextgrent(char const * nam, gid_t gid, int doclose)
197 struct group * gr = NULL;
199 static char * grtmp = NULL;
200 static int grlen = 0;
201 static char ** mems = NULL;
202 static int memlen = 0;
204 extendline(&grtmp, &grlen, MAXPATHLEN);
205 strlcpy(grtmp, getgrpath(_GROUP), MAXPATHLEN);
207 if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) {
210 static struct group grp;
212 while (!done && fgets(grtmp, grlen, grp_fp) != NULL)
219 if ((p = strchr(grtmp, '\n')) == NULL) {
221 extendline(&grtmp, &grlen, grlen + PWBUFSZ);
223 if (fgets(grtmp + l, grlen - l, grp_fp) == NULL)
224 break; /* No newline terminator on last line */
226 /* Skip comments and empty lines */
227 if (*grtmp == '\n' || *grtmp == '#')
231 bzero(&grp, sizeof grp);
232 extendarray(&mems, &memlen, 200);
233 while (!quickout && (p = strsep(&q, sep)) != NULL) {
236 case 0: /* groupname */
239 if (strcmp(nam, p) == 0)
245 case 1: /* password */
249 grp.gr_gid = atoi(p);
250 if (gid != (gid_t)-1) {
251 if (gid == (gid_t)grp.gr_gid)
255 } else if (nam == NULL)
264 extendarray(&mems, &memlen, mno + 2);
275 if (done && grp.gr_name) {
278 CKNULL(grp.gr_passwd);
287 return vnextgrent(NULL, -1, 0);
294 return vnextgrent(NULL, gid, 1);
298 vgetgrnam(const char * nam)
300 return vnextgrent(nam, -1, 1);
304 vgrdb(char *arg __unused, ...)