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.3 2000/01/15 00:20:21 davidn Exp $
27 * $DragonFly: src/usr.sbin/pw/pw_vpw.c,v 1.3 2004/02/10 02:59:43 rob Exp $
33 #include <sys/param.h>
37 static FILE * pwd_fp = NULL;
54 static struct passwd *
55 vnextpwent(char const * nam, uid_t uid, int doclose)
57 struct passwd * pw = NULL;
58 static char pwtmp[1024];
60 strncpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof pwtmp);
61 pwtmp[sizeof pwtmp - 1] = '\0';
63 if (pwd_fp != NULL || (pwd_fp = fopen(pwtmp, "r")) != NULL) {
66 static struct passwd pwd;
68 while (!done && fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL)
72 char * p = strchr(pwtmp, '\n');
75 while (fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL && strchr(pwtmp, '\n')==NULL)
76 ; /* Skip long lines */
80 /* skip comments & empty lines */
81 if (*pwtmp =='\n' || *pwtmp == '#')
86 bzero(&pwd, sizeof pwd);
87 while (!quickout && (p = strsep(&q, ":\n")) != NULL) {
90 case 0: /* username */
93 if (strcmp(nam, p) == 0)
99 case 1: /* password */
103 pwd.pw_uid = atoi(p);
104 if (uid != (uid_t)-1) {
105 if (uid == pwd.pw_uid)
112 pwd.pw_gid = atoi(p);
115 if (nam == NULL && uid == (uid_t)-1)
120 pwd.pw_change = (time_t)atol(p);
123 pwd.pw_expire = (time_t)atol(p);
128 case 8: /* directory */
139 if (done && pwd.pw_name) {
142 #define CKNULL(s) s = s ? s : ""
143 CKNULL(pwd.pw_passwd);
144 CKNULL(pwd.pw_class);
145 CKNULL(pwd.pw_gecos);
147 CKNULL(pwd.pw_shell);
156 return vnextpwent(NULL, -1, 0);
162 return vnextpwent(NULL, uid, 1);
166 vgetpwnam(const char * nam)
168 return vnextpwent(nam, -1, 1);
171 int vpwdb(char *arg, ...)
179 static FILE * grp_fp = NULL;
184 if (grp_fp != NULL) {
194 #if defined(__DragonFly__)
199 static struct group *
200 vnextgrent(char const * nam, gid_t gid, int doclose)
202 struct group * gr = NULL;
204 static char * grtmp = NULL;
205 static int grlen = 0;
206 static char ** mems = NULL;
207 static int memlen = 0;
209 extendline(&grtmp, &grlen, MAXPATHLEN);
210 strncpy(grtmp, getgrpath(_GROUP), MAXPATHLEN);
211 grtmp[MAXPATHLEN - 1] = '\0';
213 if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) {
216 static struct group grp;
218 while (!done && fgets(grtmp, grlen, grp_fp) != NULL)
225 if ((p = strchr(grtmp, '\n')) == NULL) {
227 extendline(&grtmp, &grlen, grlen + PWBUFSZ);
229 if (fgets(grtmp + l, grlen - l, grp_fp) == NULL)
230 break; /* No newline terminator on last line */
232 /* Skip comments and empty lines */
233 if (*grtmp == '\n' || *grtmp == '#')
237 bzero(&grp, sizeof grp);
238 extendarray(&mems, &memlen, 200);
239 while (!quickout && (p = strsep(&q, sep)) != NULL) {
242 case 0: /* groupname */
245 if (strcmp(nam, p) == 0)
251 case 1: /* password */
255 grp.gr_gid = atoi(p);
256 if (gid != (gid_t)-1) {
257 if (gid == (gid_t)grp.gr_gid)
261 } else if (nam == NULL)
270 extendarray(&mems, &memlen, mno + 2);
281 if (done && grp.gr_name) {
284 CKNULL(grp.gr_passwd);
293 return vnextgrent(NULL, -1, 0);
300 return vnextgrent(NULL, gid, 1);
304 vgetgrnam(const char * nam)
306 return vnextgrent(nam, -1, 1);
310 vgrdb(char *arg, ...)