getpwent.c revision 1.5 1 1.5 lukem /* $NetBSD: getpwent.c,v 1.5 2002/02/02 15:57:54 lukem Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.5 lukem * Copyright (c) 1987, 1988, 1989, 1993, 1994, 1995
5 1.5 lukem * The Regents of the University of California. All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.5 lukem * 3. All advertising materials mentioning features or use of this software
16 1.1 gwr * must display the following acknowledgement:
17 1.5 lukem * This product includes software developed by the University of
18 1.5 lukem * California, Berkeley and its contributors.
19 1.5 lukem * 4. Neither the name of the University nor the names of its contributors
20 1.5 lukem * may be used to endorse or promote products derived from this software
21 1.5 lukem * without specific prior written permission.
22 1.1 gwr *
23 1.5 lukem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.5 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.5 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.5 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.5 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.5 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.5 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.5 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.5 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.5 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.5 lukem * SUCH DAMAGE.
34 1.1 gwr */
35 1.1 gwr
36 1.1 gwr /*
37 1.5 lukem * Copied from: lib/libc/gen/getpwent.c
38 1.5 lukem * NetBSD: getpwent.c,v 1.48 2000/10/03 03:22:26 enami Exp
39 1.5 lukem * and then gutted, leaving only /etc/master.passwd support.
40 1.1 gwr */
41 1.1 gwr
42 1.4 tsutsui #include <sys/cdefs.h>
43 1.4 tsutsui
44 1.4 tsutsui #ifdef __weak_alias
45 1.4 tsutsui #define endpwent _endpwent
46 1.4 tsutsui #define getpwent _getpwent
47 1.5 lukem #define getpwuid _getpwuid
48 1.4 tsutsui #define getpwnam _getpwnam
49 1.5 lukem #define setpwent _setpwent
50 1.4 tsutsui #define setpassent _setpassent
51 1.4 tsutsui
52 1.4 tsutsui __weak_alias(endpwent,_endpwent)
53 1.4 tsutsui __weak_alias(getpwent,_getpwent)
54 1.5 lukem __weak_alias(getpwuid,_getpwuid)
55 1.4 tsutsui __weak_alias(getpwnam,_getpwnam)
56 1.5 lukem __weak_alias(setpwent,_setpwent)
57 1.4 tsutsui __weak_alias(setpassent,_setpassent)
58 1.4 tsutsui #endif
59 1.1 gwr
60 1.5 lukem #include <sys/param.h>
61 1.5 lukem
62 1.5 lukem #include <limits.h>
63 1.5 lukem #include <pwd.h>
64 1.5 lukem #include <stdlib.h>
65 1.5 lukem #include <stdio.h>
66 1.5 lukem #include <string.h>
67 1.5 lukem
68 1.5 lukem static int pwstart(void);
69 1.5 lukem static int pwscan(int, uid_t, const char *);
70 1.5 lukem static int pwmatchline(int, uid_t, const char *);
71 1.5 lukem
72 1.5 lukem static FILE *_pw_fp;
73 1.5 lukem static struct passwd _pw_passwd; /* password structure */
74 1.5 lukem static int _pw_stayopen; /* keep fd's open */
75 1.5 lukem static int _pw_filesdone;
76 1.1 gwr
77 1.5 lukem #define MAXLINELENGTH 1024
78 1.5 lukem
79 1.5 lukem static char pwline[MAXLINELENGTH];
80 1.1 gwr
81 1.1 gwr struct passwd *
82 1.5 lukem getpwent(void)
83 1.1 gwr {
84 1.1 gwr
85 1.5 lukem if ((!_pw_fp && !pwstart()) || !pwscan(0, 0, NULL))
86 1.5 lukem return (NULL);
87 1.5 lukem return (&_pw_passwd);
88 1.5 lukem }
89 1.1 gwr
90 1.5 lukem struct passwd *
91 1.5 lukem getpwnam(const char *name)
92 1.5 lukem {
93 1.5 lukem int rval;
94 1.5 lukem
95 1.5 lukem if (!pwstart())
96 1.5 lukem return NULL;
97 1.5 lukem rval = pwscan(1, 0, name);
98 1.5 lukem if (!_pw_stayopen)
99 1.5 lukem endpwent();
100 1.5 lukem return (rval) ? &_pw_passwd : NULL;
101 1.1 gwr }
102 1.1 gwr
103 1.5 lukem struct passwd *
104 1.5 lukem getpwuid(uid_t uid)
105 1.1 gwr {
106 1.5 lukem int rval;
107 1.5 lukem
108 1.5 lukem if (!pwstart())
109 1.5 lukem return NULL;
110 1.5 lukem rval = pwscan(1, uid, NULL);
111 1.5 lukem if (!_pw_stayopen)
112 1.5 lukem endpwent();
113 1.5 lukem return (rval) ? &_pw_passwd : NULL;
114 1.1 gwr }
115 1.1 gwr
116 1.1 gwr void
117 1.5 lukem setpwent(void)
118 1.1 gwr {
119 1.5 lukem
120 1.1 gwr (void) setpassent(0);
121 1.1 gwr }
122 1.1 gwr
123 1.5 lukem int
124 1.5 lukem setpassent(int stayopen)
125 1.5 lukem {
126 1.5 lukem
127 1.5 lukem if (!pwstart())
128 1.5 lukem return 0;
129 1.5 lukem _pw_stayopen = stayopen;
130 1.5 lukem return 1;
131 1.5 lukem }
132 1.5 lukem
133 1.1 gwr void
134 1.5 lukem endpwent(void)
135 1.1 gwr {
136 1.5 lukem
137 1.5 lukem _pw_filesdone = 0;
138 1.5 lukem if (_pw_fp) {
139 1.5 lukem (void)fclose(_pw_fp);
140 1.5 lukem _pw_fp = NULL;
141 1.2 gwr }
142 1.1 gwr }
143 1.1 gwr
144 1.5 lukem static int
145 1.5 lukem pwstart(void)
146 1.1 gwr {
147 1.1 gwr
148 1.5 lukem _pw_filesdone = 0;
149 1.5 lukem if (_pw_fp) {
150 1.5 lukem rewind(_pw_fp);
151 1.5 lukem return 1;
152 1.5 lukem }
153 1.5 lukem return (_pw_fp = fopen(_PATH_MASTERPASSWD, "r")) ? 1 : 0;
154 1.5 lukem }
155 1.1 gwr
156 1.1 gwr
157 1.5 lukem static int
158 1.5 lukem pwscan(int search, uid_t uid, const char *name)
159 1.1 gwr {
160 1.1 gwr
161 1.5 lukem if (_pw_filesdone)
162 1.5 lukem return 0;
163 1.5 lukem for (;;) {
164 1.5 lukem if (!fgets(pwline, sizeof(pwline), _pw_fp)) {
165 1.5 lukem if (!search)
166 1.5 lukem _pw_filesdone = 1;
167 1.5 lukem return 0;
168 1.5 lukem }
169 1.5 lukem /* skip lines that are too big */
170 1.5 lukem if (!strchr(pwline, '\n')) {
171 1.5 lukem int ch;
172 1.5 lukem
173 1.5 lukem while ((ch = getc(_pw_fp)) != '\n' && ch != EOF)
174 1.5 lukem ;
175 1.5 lukem continue;
176 1.5 lukem }
177 1.5 lukem if (pwmatchline(search, uid, name))
178 1.5 lukem return 1;
179 1.5 lukem }
180 1.5 lukem /* NOTREACHED */
181 1.5 lukem }
182 1.1 gwr
183 1.5 lukem static int
184 1.5 lukem pwmatchline(int search, uid_t uid, const char *name)
185 1.5 lukem {
186 1.5 lukem unsigned long id;
187 1.5 lukem char *cp, *bp, *ep;
188 1.1 gwr
189 1.5 lukem /* name may be NULL if search is nonzero */
190 1.5 lukem
191 1.5 lukem bp = pwline;
192 1.5 lukem memset(&_pw_passwd, 0, sizeof(_pw_passwd));
193 1.5 lukem _pw_passwd.pw_name = strsep(&bp, ":\n"); /* name */
194 1.5 lukem if (search && name && strcmp(_pw_passwd.pw_name, name))
195 1.5 lukem return 0;
196 1.5 lukem
197 1.5 lukem _pw_passwd.pw_passwd = strsep(&bp, ":\n"); /* passwd */
198 1.5 lukem
199 1.5 lukem if (!(cp = strsep(&bp, ":\n"))) /* uid */
200 1.5 lukem return 0;
201 1.5 lukem id = strtoul(cp, &ep, 10);
202 1.5 lukem if (id > UID_MAX || *ep != '\0')
203 1.5 lukem return 0;
204 1.5 lukem _pw_passwd.pw_uid = (uid_t)id;
205 1.5 lukem if (search && name == NULL && _pw_passwd.pw_uid != uid)
206 1.5 lukem return 0;
207 1.5 lukem
208 1.5 lukem if (!(cp = strsep(&bp, ":\n"))) /* gid */
209 1.5 lukem return 0;
210 1.5 lukem id = strtoul(cp, &ep, 10);
211 1.5 lukem if (id > GID_MAX || *ep != '\0')
212 1.5 lukem return 0;
213 1.5 lukem _pw_passwd.pw_gid = (gid_t)id;
214 1.5 lukem
215 1.5 lukem if (!(ep = strsep(&bp, ":"))) /* class */
216 1.5 lukem return 0;
217 1.5 lukem if (!(ep = strsep(&bp, ":"))) /* change */
218 1.5 lukem return 0;
219 1.5 lukem if (!(ep = strsep(&bp, ":"))) /* expire */
220 1.5 lukem return 0;
221 1.5 lukem
222 1.5 lukem if (!(_pw_passwd.pw_gecos = strsep(&bp, ":\n"))) /* gecos */
223 1.5 lukem return 0;
224 1.5 lukem if (!(_pw_passwd.pw_dir = strsep(&bp, ":\n"))) /* directory */
225 1.5 lukem return 0;
226 1.5 lukem if (!(_pw_passwd.pw_shell = strsep(&bp, ":\n"))) /* shell */
227 1.5 lukem return 0;
228 1.1 gwr
229 1.5 lukem if (strchr(bp, ':') != NULL)
230 1.5 lukem return 0;
231 1.1 gwr
232 1.5 lukem return 1;
233 1.1 gwr }
234