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