pw_vpw.c revision 225736
1/*-
2 * Copyright (C) 1996
3 *	David L. Nugent.  All rights reserved.
4 *
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.
13 *
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.
25 *
26 */
27
28#ifndef lint
29static const char rcsid[] =
30  "$FreeBSD: stable/9/usr.sbin/pw/pw_vpw.c 130633 2004-06-17 14:07:16Z robert $";
31#endif /* not lint */
32
33#include <stdio.h>
34#include <string.h>
35#include <stdlib.h>
36#include <sys/param.h>
37
38#include "pwupd.h"
39
40static FILE * pwd_fp = NULL;
41
42void
43vendpwent(void)
44{
45	if (pwd_fp != NULL) {
46		fclose(pwd_fp);
47		pwd_fp = NULL;
48	}
49}
50
51void
52vsetpwent(void)
53{
54	vendpwent();
55}
56
57static struct passwd *
58vnextpwent(char const * nam, uid_t uid, int doclose)
59{
60	struct passwd * pw = NULL;
61	static char pwtmp[1024];
62
63        strlcpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof(pwtmp));
64
65        if (pwd_fp != NULL || (pwd_fp = fopen(pwtmp, "r")) != NULL) {
66                int done = 0;
67
68                static struct passwd pwd;
69
70                while (!done && fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL)
71                {
72                        int i, quickout = 0;
73                        char * q;
74                        char * p = strchr(pwtmp, '\n');
75
76                        if (p == NULL) {
77		  		while (fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL && strchr(pwtmp, '\n')==NULL)
78		  			; /* Skip long lines */
79		  		continue;
80                        }
81
82			/* skip comments & empty lines */
83	       		if (*pwtmp =='\n' || *pwtmp == '#')
84				continue;
85
86                        i = 0;
87                        q = p = pwtmp;
88                        bzero(&pwd, sizeof pwd);
89                        while (!quickout && (p = strsep(&q, ":\n")) != NULL) {
90                          	switch (i++)
91                          	{
92                                case 0:   /* username */
93        				pwd.pw_name = p;
94        				if (nam) {
95        					if (strcmp(nam, p) == 0)
96        						done = 1;
97        					else
98        						quickout = 1;
99        				}
100        				break;
101                                case 1:   /* password */
102        				pwd.pw_passwd = p;
103        				break;
104                                case 2:   /* uid */
105        				pwd.pw_uid = atoi(p);
106        				if (uid != (uid_t)-1) {
107        					if (uid == pwd.pw_uid)
108        						done = 1;
109        					else
110        						quickout = 1;
111        				}
112        				break;
113                                case 3:   /* gid */
114        				pwd.pw_gid = atoi(p);
115        				break;
116                                case 4:   /* class */
117					if (nam == NULL && uid == (uid_t)-1)
118						done = 1;
119        				pwd.pw_class = p;
120        				break;
121                                case 5:   /* change */
122        				pwd.pw_change = (time_t)atol(p);
123        				break;
124                                case 6:   /* expire */
125        				pwd.pw_expire = (time_t)atol(p);
126        				break;
127                                case 7:   /* gecos */
128        				pwd.pw_gecos = p;
129        				break;
130                                case 8:   /* directory */
131        				pwd.pw_dir = p;
132        				break;
133                                case 9:   /* shell */
134        				pwd.pw_shell = p;
135        				break;
136                                }
137        		}
138                }
139		if (doclose)
140			vendpwent();
141		if (done && pwd.pw_name) {
142			pw = &pwd;
143
144			#define CKNULL(s)   s = s ? s : ""
145			CKNULL(pwd.pw_passwd);
146			CKNULL(pwd.pw_class);
147			CKNULL(pwd.pw_gecos);
148			CKNULL(pwd.pw_dir);
149			CKNULL(pwd.pw_shell);
150                }
151        }
152        return pw;
153}
154
155struct passwd *
156vgetpwent(void)
157{
158  return vnextpwent(NULL, -1, 0);
159}
160
161struct passwd *
162vgetpwuid(uid_t uid)
163{
164  return vnextpwent(NULL, uid, 1);
165}
166
167struct passwd *
168vgetpwnam(const char * nam)
169{
170  return vnextpwent(nam, -1, 1);
171}
172
173int vpwdb(char *arg, ...)
174{
175  arg=arg;
176  return 0;
177}
178
179
180
181static FILE * grp_fp = NULL;
182
183void
184vendgrent(void)
185{
186	if (grp_fp != NULL) {
187		fclose(grp_fp);
188		grp_fp = NULL;
189	}
190}
191
192RET_SETGRENT
193vsetgrent(void)
194{
195	vendgrent();
196#if defined(__FreeBSD__)
197	return 0;
198#endif
199}
200
201static struct group *
202vnextgrent(char const * nam, gid_t gid, int doclose)
203{
204	struct group * gr = NULL;
205
206	static char * grtmp = NULL;
207	static int grlen = 0;
208	static char ** mems = NULL;
209	static int memlen = 0;
210
211	extendline(&grtmp, &grlen, MAXPATHLEN);
212	strlcpy(grtmp, getgrpath(_GROUP), MAXPATHLEN);
213
214	if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) {
215		int done = 0;
216
217		static struct group grp;
218
219		while (!done && fgets(grtmp, grlen, grp_fp) != NULL)
220		{
221			int i, quickout = 0;
222			int mno = 0;
223			char * q, * p;
224			char * sep = ":\n";
225
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 */
232			}
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++)
242				{
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;
250					}
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;
273					}
274					break;
275				}
276			}
277			grp.gr_mem = mems;
278			mems[mno] = NULL;
279                }
280		if (doclose)
281			vendgrent();
282		if (done && grp.gr_name) {
283			gr = &grp;
284
285			CKNULL(grp.gr_passwd);
286		}
287	}
288	return gr;
289}
290
291struct group *
292vgetgrent(void)
293{
294  return vnextgrent(NULL, -1, 0);
295}
296
297
298struct group *
299vgetgrgid(gid_t gid)
300{
301  return vnextgrent(NULL, gid, 1);
302}
303
304struct group *
305vgetgrnam(const char * nam)
306{
307  return vnextgrent(nam, -1, 1);
308}
309
310int
311vgrdb(char *arg, ...)
312{
313  arg=arg;
314  return 0;
315}
316
317