Parallelize in_ifaddrhead operation
[dragonfly.git] / usr.sbin / pw / pw_vpw.c
blobb0789e4fa75c16bbfdcc8d8ae23ba88ca5964c1c
1 /*-
2 * Copyright (C) 1996
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
7 * are met:
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
24 * SUCH DAMAGE.
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.4 2005/12/05 02:40:27 swildner Exp $
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <sys/param.h>
35 #include "pwupd.h"
37 static FILE * pwd_fp = NULL;
39 void
40 vendpwent(void)
42 if (pwd_fp != NULL) {
43 fclose(pwd_fp);
44 pwd_fp = NULL;
48 void
49 vsetpwent(void)
51 vendpwent();
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) {
64 int done = 0;
66 static struct passwd pwd;
68 while (!done && fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL)
70 int i, quickout = 0;
71 char * q;
72 char * p = strchr(pwtmp, '\n');
74 if (p == NULL) {
75 while (fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL && strchr(pwtmp, '\n')==NULL)
76 ; /* Skip long lines */
77 continue;
80 /* skip comments & empty lines */
81 if (*pwtmp =='\n' || *pwtmp == '#')
82 continue;
84 i = 0;
85 q = p = pwtmp;
86 bzero(&pwd, sizeof pwd);
87 while (!quickout && (p = strsep(&q, ":\n")) != NULL) {
88 switch (i++)
90 case 0: /* username */
91 pwd.pw_name = p;
92 if (nam) {
93 if (strcmp(nam, p) == 0)
94 done = 1;
95 else
96 quickout = 1;
98 break;
99 case 1: /* password */
100 pwd.pw_passwd = p;
101 break;
102 case 2: /* uid */
103 pwd.pw_uid = atoi(p);
104 if (uid != (uid_t)-1) {
105 if (uid == pwd.pw_uid)
106 done = 1;
107 else
108 quickout = 1;
110 break;
111 case 3: /* gid */
112 pwd.pw_gid = atoi(p);
113 break;
114 case 4: /* class */
115 if (nam == NULL && uid == (uid_t)-1)
116 done = 1;
117 pwd.pw_class = p;
118 break;
119 case 5: /* change */
120 pwd.pw_change = (time_t)atol(p);
121 break;
122 case 6: /* expire */
123 pwd.pw_expire = (time_t)atol(p);
124 break;
125 case 7: /* gecos */
126 pwd.pw_gecos = p;
127 break;
128 case 8: /* directory */
129 pwd.pw_dir = p;
130 break;
131 case 9: /* shell */
132 pwd.pw_shell = p;
133 break;
137 if (doclose)
138 vendpwent();
139 if (done && pwd.pw_name) {
140 pw = &pwd;
142 #define CKNULL(s) s = s ? s : ""
143 CKNULL(pwd.pw_passwd);
144 CKNULL(pwd.pw_class);
145 CKNULL(pwd.pw_gecos);
146 CKNULL(pwd.pw_dir);
147 CKNULL(pwd.pw_shell);
150 return pw;
153 struct passwd *
154 vgetpwent(void)
156 return vnextpwent(NULL, -1, 0);
159 struct passwd *
160 vgetpwuid(uid_t uid)
162 return vnextpwent(NULL, uid, 1);
165 struct passwd *
166 vgetpwnam(const char * nam)
168 return vnextpwent(nam, -1, 1);
172 vpwdb(char *arg, ...)
174 arg=arg;
175 return 0;
180 static FILE * grp_fp = NULL;
182 void
183 vendgrent(void)
185 if (grp_fp != NULL) {
186 fclose(grp_fp);
187 grp_fp = NULL;
191 RET_SETGRENT
192 vsetgrent(void)
194 vendgrent();
195 #if defined(__DragonFly__)
196 return 0;
197 #endif
200 static struct group *
201 vnextgrent(char const * nam, gid_t gid, int doclose)
203 struct group * gr = NULL;
205 static char * grtmp = NULL;
206 static int grlen = 0;
207 static char ** mems = NULL;
208 static int memlen = 0;
210 extendline(&grtmp, &grlen, MAXPATHLEN);
211 strncpy(grtmp, getgrpath(_GROUP), MAXPATHLEN);
212 grtmp[MAXPATHLEN - 1] = '\0';
214 if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) {
215 int done = 0;
217 static struct group grp;
219 while (!done && fgets(grtmp, grlen, grp_fp) != NULL)
221 int i, quickout = 0;
222 int mno = 0;
223 char * q, * p;
224 char * sep = ":\n";
226 if ((p = strchr(grtmp, '\n')) == NULL) {
227 int l;
228 extendline(&grtmp, &grlen, grlen + PWBUFSZ);
229 l = strlen(grtmp);
230 if (fgets(grtmp + l, grlen - l, grp_fp) == NULL)
231 break; /* No newline terminator on last line */
233 /* Skip comments and empty lines */
234 if (*grtmp == '\n' || *grtmp == '#')
235 continue;
236 i = 0;
237 q = p = grtmp;
238 bzero(&grp, sizeof grp);
239 extendarray(&mems, &memlen, 200);
240 while (!quickout && (p = strsep(&q, sep)) != NULL) {
241 switch (i++)
243 case 0: /* groupname */
244 grp.gr_name = p;
245 if (nam) {
246 if (strcmp(nam, p) == 0)
247 done = 1;
248 else
249 quickout = 1;
251 break;
252 case 1: /* password */
253 grp.gr_passwd = p;
254 break;
255 case 2: /* gid */
256 grp.gr_gid = atoi(p);
257 if (gid != (gid_t)-1) {
258 if (gid == (gid_t)grp.gr_gid)
259 done = 1;
260 else
261 quickout = 1;
262 } else if (nam == NULL)
263 done = 1;
264 break;
265 case 3:
266 q = p;
267 sep = ",\n";
268 break;
269 default:
270 if (*p) {
271 extendarray(&mems, &memlen, mno + 2);
272 mems[mno++] = p;
274 break;
277 grp.gr_mem = mems;
278 mems[mno] = NULL;
280 if (doclose)
281 vendgrent();
282 if (done && grp.gr_name) {
283 gr = &grp;
285 CKNULL(grp.gr_passwd);
288 return gr;
291 struct group *
292 vgetgrent(void)
294 return vnextgrent(NULL, -1, 0);
298 struct group *
299 vgetgrgid(gid_t gid)
301 return vnextgrent(NULL, gid, 1);
304 struct group *
305 vgetgrnam(const char * nam)
307 return vnextgrent(nam, -1, 1);
311 vgrdb(char *arg, ...)
313 arg=arg;
314 return 0;