getpwent.c revision 1.8 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1988 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
35 1.7 jtc /*static char *sccsid = "from: @(#)getpwent.c 5.21 (Berkeley) 3/14/91";*/
36 1.8 jtc static char *rcsid = "$Id: getpwent.c,v 1.8 1993/10/25 22:23:40 jtc Exp $";
37 1.1 cgd #endif /* LIBC_SCCS and not lint */
38 1.1 cgd
39 1.1 cgd #include <sys/param.h>
40 1.1 cgd #include <fcntl.h>
41 1.1 cgd #include <db.h>
42 1.1 cgd #include <syslog.h>
43 1.1 cgd #include <pwd.h>
44 1.1 cgd #include <utmp.h>
45 1.1 cgd #include <errno.h>
46 1.1 cgd #include <unistd.h>
47 1.1 cgd #include <stdlib.h>
48 1.1 cgd #include <string.h>
49 1.1 cgd #include <limits.h>
50 1.4 deraadt #ifdef YP
51 1.4 deraadt #include <stdio.h>
52 1.4 deraadt #include <rpc/rpc.h>
53 1.4 deraadt #include <rpcsvc/yp_prot.h>
54 1.4 deraadt #include <rpcsvc/ypclnt.h>
55 1.4 deraadt #endif
56 1.1 cgd
57 1.1 cgd static struct passwd _pw_passwd; /* password structure */
58 1.1 cgd static DB *_pw_db; /* password database */
59 1.1 cgd static int _pw_keynum; /* key counter */
60 1.1 cgd static int _pw_stayopen; /* keep fd's open */
61 1.1 cgd static int __hashpw(), __initdb();
62 1.1 cgd
63 1.4 deraadt #ifdef YP
64 1.4 deraadt static char *__ypcurrent, *__ypdomain;
65 1.4 deraadt static int __ypcurrentlen, __ypmode=0;
66 1.4 deraadt static char line[1024];
67 1.4 deraadt
68 1.5 deraadt static int
69 1.5 deraadt __ypparse(pw, s)
70 1.4 deraadt struct passwd *pw;
71 1.4 deraadt char *s;
72 1.4 deraadt {
73 1.4 deraadt char *bp, *cp;
74 1.4 deraadt
75 1.4 deraadt bp = s;
76 1.4 deraadt pw->pw_name = strsep(&bp, ":\n");
77 1.4 deraadt pw->pw_passwd = strsep(&bp, ":\n");
78 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
79 1.4 deraadt return 1;
80 1.4 deraadt pw->pw_uid = atoi(cp);
81 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
82 1.4 deraadt return 0;
83 1.4 deraadt pw->pw_gid = atoi(cp);
84 1.4 deraadt pw->pw_change = 0;
85 1.4 deraadt pw->pw_class = "";
86 1.4 deraadt pw->pw_gecos = strsep(&bp, ":\n");
87 1.4 deraadt pw->pw_dir = strsep(&bp, ":\n");
88 1.4 deraadt pw->pw_shell = strsep(&bp, ":\n");
89 1.4 deraadt pw->pw_expire = 0;
90 1.4 deraadt return 0;
91 1.4 deraadt }
92 1.4 deraadt #endif
93 1.4 deraadt
94 1.1 cgd struct passwd *
95 1.1 cgd getpwent()
96 1.1 cgd {
97 1.1 cgd DBT key;
98 1.1 cgd char bf[sizeof(_pw_keynum) + 1];
99 1.4 deraadt #ifdef YP
100 1.4 deraadt char *bp, *cp;
101 1.4 deraadt #endif
102 1.1 cgd
103 1.1 cgd if (!_pw_db && !__initdb())
104 1.1 cgd return((struct passwd *)NULL);
105 1.1 cgd
106 1.4 deraadt #ifdef YP
107 1.4 deraadt again:
108 1.4 deraadt if(__ypmode) {
109 1.4 deraadt char *key, *data;
110 1.4 deraadt int keylen, datalen;
111 1.4 deraadt int r;
112 1.4 deraadt
113 1.4 deraadt if(!__ypdomain) {
114 1.4 deraadt if( _yp_check(&__ypdomain) == 0) {
115 1.4 deraadt __ypmode = 0;
116 1.4 deraadt goto again;
117 1.4 deraadt }
118 1.4 deraadt }
119 1.4 deraadt if(__ypcurrent) {
120 1.4 deraadt r = yp_next(__ypdomain, "passwd.byname",
121 1.4 deraadt __ypcurrent, __ypcurrentlen,
122 1.4 deraadt &key, &keylen, &data, &datalen);
123 1.4 deraadt free(__ypcurrent);
124 1.4 deraadt __ypcurrent = NULL;
125 1.4 deraadt /*printf("yp_next %d\n", r);*/
126 1.4 deraadt switch(r) {
127 1.4 deraadt case 0:
128 1.4 deraadt break;
129 1.4 deraadt default:
130 1.4 deraadt __ypcurrent = NULL;
131 1.4 deraadt __ypmode = 0;
132 1.4 deraadt free(data);
133 1.4 deraadt data = NULL;
134 1.4 deraadt goto again;
135 1.4 deraadt }
136 1.4 deraadt __ypcurrent = key;
137 1.4 deraadt __ypcurrentlen = keylen;
138 1.4 deraadt bcopy(data, line, datalen);
139 1.4 deraadt free(data);
140 1.4 deraadt data = NULL;
141 1.4 deraadt } else {
142 1.4 deraadt r = yp_first(__ypdomain, "passwd.byname",
143 1.4 deraadt &__ypcurrent, &__ypcurrentlen,
144 1.4 deraadt &data, &datalen);
145 1.4 deraadt /*printf("yp_first %d\n", r);*/
146 1.4 deraadt switch(r) {
147 1.4 deraadt case 0:
148 1.4 deraadt break;
149 1.4 deraadt default:
150 1.4 deraadt __ypmode = 0;
151 1.4 deraadt free(data);
152 1.4 deraadt goto again;
153 1.4 deraadt }
154 1.4 deraadt bcopy(data, line, datalen);
155 1.4 deraadt free(data);
156 1.4 deraadt data = NULL;
157 1.4 deraadt }
158 1.4 deraadt line[datalen] = '\0';
159 1.4 deraadt /*printf("line = %s\n", line);*/
160 1.4 deraadt bp = line;
161 1.4 deraadt goto parse;
162 1.4 deraadt }
163 1.4 deraadt #endif
164 1.4 deraadt
165 1.1 cgd ++_pw_keynum;
166 1.1 cgd bf[0] = _PW_KEYBYNUM;
167 1.1 cgd bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
168 1.1 cgd key.data = (u_char *)bf;
169 1.1 cgd key.size = sizeof(_pw_keynum) + 1;
170 1.4 deraadt if(__hashpw(&key)) {
171 1.4 deraadt #ifdef YP
172 1.4 deraadt if(strcmp(_pw_passwd.pw_name, "+") == 0) {
173 1.4 deraadt __ypmode = 1;
174 1.4 deraadt goto again;
175 1.4 deraadt }
176 1.4 deraadt #endif
177 1.4 deraadt return &_pw_passwd;
178 1.4 deraadt }
179 1.4 deraadt return (struct passwd *)NULL;
180 1.4 deraadt
181 1.4 deraadt #ifdef YP
182 1.4 deraadt parse:
183 1.4 deraadt _pw_passwd.pw_name = strsep(&bp, ":\n");
184 1.4 deraadt _pw_passwd.pw_passwd = strsep(&bp, ":\n");
185 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
186 1.4 deraadt goto again;
187 1.4 deraadt _pw_passwd.pw_uid = atoi(cp);
188 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
189 1.4 deraadt goto again;
190 1.4 deraadt _pw_passwd.pw_gid = atoi(cp);
191 1.4 deraadt _pw_passwd.pw_change = 0;
192 1.4 deraadt _pw_passwd.pw_class = "";
193 1.4 deraadt _pw_passwd.pw_gecos = strsep(&bp, ":\n");
194 1.4 deraadt _pw_passwd.pw_dir = strsep(&bp, ":\n");
195 1.4 deraadt _pw_passwd.pw_shell = strsep(&bp, ":\n");
196 1.4 deraadt _pw_passwd.pw_expire = 0;
197 1.4 deraadt return &_pw_passwd;
198 1.4 deraadt #endif
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd struct passwd *
202 1.1 cgd getpwnam(name)
203 1.1 cgd const char *name;
204 1.1 cgd {
205 1.1 cgd DBT key;
206 1.4 deraadt
207 1.4 deraadt #ifdef YP
208 1.4 deraadt char bf[sizeof(_pw_keynum) + 1];
209 1.4 deraadt int r;
210 1.4 deraadt
211 1.4 deraadt if (!_pw_db && !__initdb())
212 1.4 deraadt return((struct passwd *)NULL);
213 1.4 deraadt
214 1.4 deraadt for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
215 1.4 deraadt bf[0] = _PW_KEYBYNUM;
216 1.4 deraadt bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
217 1.4 deraadt key.data = (u_char *)bf;
218 1.4 deraadt key.size = sizeof(_pw_keynum) + 1;
219 1.4 deraadt if(__hashpw(&key) == 0)
220 1.4 deraadt break;
221 1.4 deraadt if(strcmp(_pw_passwd.pw_name, "+") == 0) {
222 1.4 deraadt if(!__ypdomain) {
223 1.4 deraadt if(_yp_check(&__ypdomain) == 0) {
224 1.4 deraadt continue;
225 1.4 deraadt }
226 1.4 deraadt }
227 1.4 deraadt if(__ypcurrent) {
228 1.4 deraadt free(__ypcurrent);
229 1.4 deraadt __ypcurrent = NULL;
230 1.4 deraadt }
231 1.4 deraadt r = yp_match(__ypdomain, "passwd.byname",
232 1.4 deraadt name, strlen(name),
233 1.4 deraadt &__ypcurrent, &__ypcurrentlen);
234 1.4 deraadt switch(r) {
235 1.4 deraadt case 0:
236 1.4 deraadt break;
237 1.4 deraadt default:
238 1.4 deraadt free(__ypcurrent);
239 1.4 deraadt __ypcurrent = NULL;
240 1.4 deraadt continue;
241 1.4 deraadt }
242 1.5 deraadt bcopy(__ypcurrent, line, __ypcurrentlen);
243 1.5 deraadt line[__ypcurrentlen] = '\0';
244 1.5 deraadt if(__ypparse(&_pw_passwd, line))
245 1.4 deraadt continue;
246 1.4 deraadt }
247 1.4 deraadt if( strcmp(_pw_passwd.pw_name, name) == 0) {
248 1.4 deraadt if (!_pw_stayopen) {
249 1.4 deraadt (void)(_pw_db->close)(_pw_db);
250 1.4 deraadt _pw_db = (DB *)NULL;
251 1.4 deraadt }
252 1.4 deraadt return &_pw_passwd;
253 1.4 deraadt }
254 1.4 deraadt continue;
255 1.4 deraadt }
256 1.4 deraadt if (!_pw_stayopen) {
257 1.4 deraadt (void)(_pw_db->close)(_pw_db);
258 1.4 deraadt _pw_db = (DB *)NULL;
259 1.4 deraadt }
260 1.4 deraadt return (struct passwd *)NULL;
261 1.4 deraadt #else
262 1.1 cgd int len, rval;
263 1.1 cgd char bf[UT_NAMESIZE + 1];
264 1.1 cgd
265 1.1 cgd if (!_pw_db && !__initdb())
266 1.1 cgd return((struct passwd *)NULL);
267 1.1 cgd
268 1.1 cgd bf[0] = _PW_KEYBYNAME;
269 1.1 cgd len = strlen(name);
270 1.1 cgd bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
271 1.1 cgd key.data = (u_char *)bf;
272 1.1 cgd key.size = len + 1;
273 1.1 cgd rval = __hashpw(&key);
274 1.1 cgd
275 1.1 cgd if (!_pw_stayopen) {
276 1.1 cgd (void)(_pw_db->close)(_pw_db);
277 1.1 cgd _pw_db = (DB *)NULL;
278 1.1 cgd }
279 1.1 cgd return(rval ? &_pw_passwd : (struct passwd *)NULL);
280 1.4 deraadt #endif
281 1.1 cgd }
282 1.1 cgd
283 1.1 cgd struct passwd *
284 1.1 cgd #ifdef __STDC__
285 1.1 cgd getpwuid(uid_t uid)
286 1.1 cgd #else
287 1.1 cgd getpwuid(uid)
288 1.1 cgd int uid;
289 1.1 cgd #endif
290 1.1 cgd {
291 1.1 cgd DBT key;
292 1.4 deraadt
293 1.4 deraadt #ifdef YP
294 1.4 deraadt char bf[sizeof(_pw_keynum) + 1], uidbuf[20];
295 1.4 deraadt int r;
296 1.4 deraadt
297 1.4 deraadt if (!_pw_db && !__initdb())
298 1.4 deraadt return((struct passwd *)NULL);
299 1.4 deraadt
300 1.4 deraadt for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
301 1.4 deraadt bf[0] = _PW_KEYBYNUM;
302 1.4 deraadt bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
303 1.4 deraadt key.data = (u_char *)bf;
304 1.4 deraadt key.size = sizeof(_pw_keynum) + 1;
305 1.4 deraadt if(__hashpw(&key) == 0)
306 1.4 deraadt break;
307 1.4 deraadt if(strcmp(_pw_passwd.pw_name, "+") == 0) {
308 1.4 deraadt if(!__ypdomain) {
309 1.4 deraadt if(_yp_check(&__ypdomain) == 0) {
310 1.4 deraadt continue;
311 1.4 deraadt }
312 1.4 deraadt }
313 1.4 deraadt if(__ypcurrent) {
314 1.4 deraadt free(__ypcurrent);
315 1.4 deraadt __ypcurrent = NULL;
316 1.4 deraadt }
317 1.4 deraadt sprintf(uidbuf, "%d", uid);
318 1.4 deraadt r = yp_match(__ypdomain, "passwd.byuid",
319 1.4 deraadt uidbuf, strlen(uidbuf),
320 1.4 deraadt &__ypcurrent, &__ypcurrentlen);
321 1.4 deraadt switch(r) {
322 1.4 deraadt case 0:
323 1.4 deraadt break;
324 1.4 deraadt default:
325 1.4 deraadt free(__ypcurrent);
326 1.4 deraadt __ypcurrent = NULL;
327 1.4 deraadt continue;
328 1.4 deraadt }
329 1.5 deraadt bcopy(__ypcurrent, line, __ypcurrentlen);
330 1.5 deraadt line[__ypcurrentlen] = '\0';
331 1.5 deraadt if(__ypparse(&_pw_passwd, line))
332 1.4 deraadt continue;
333 1.4 deraadt }
334 1.4 deraadt if( _pw_passwd.pw_uid == uid) {
335 1.4 deraadt if (!_pw_stayopen) {
336 1.4 deraadt (void)(_pw_db->close)(_pw_db);
337 1.4 deraadt _pw_db = (DB *)NULL;
338 1.4 deraadt }
339 1.4 deraadt return &_pw_passwd;
340 1.4 deraadt }
341 1.4 deraadt continue;
342 1.4 deraadt }
343 1.4 deraadt if (!_pw_stayopen) {
344 1.4 deraadt (void)(_pw_db->close)(_pw_db);
345 1.4 deraadt _pw_db = (DB *)NULL;
346 1.4 deraadt }
347 1.4 deraadt return (struct passwd *)NULL;
348 1.4 deraadt #else
349 1.1 cgd int keyuid, rval;
350 1.1 cgd char bf[sizeof(keyuid) + 1];
351 1.1 cgd
352 1.1 cgd if (!_pw_db && !__initdb())
353 1.1 cgd return((struct passwd *)NULL);
354 1.1 cgd
355 1.1 cgd bf[0] = _PW_KEYBYUID;
356 1.1 cgd keyuid = uid;
357 1.1 cgd bcopy(&keyuid, bf + 1, sizeof(keyuid));
358 1.1 cgd key.data = (u_char *)bf;
359 1.1 cgd key.size = sizeof(keyuid) + 1;
360 1.1 cgd rval = __hashpw(&key);
361 1.1 cgd
362 1.1 cgd if (!_pw_stayopen) {
363 1.1 cgd (void)(_pw_db->close)(_pw_db);
364 1.1 cgd _pw_db = (DB *)NULL;
365 1.1 cgd }
366 1.1 cgd return(rval ? &_pw_passwd : (struct passwd *)NULL);
367 1.4 deraadt #endif
368 1.1 cgd }
369 1.1 cgd
370 1.1 cgd int
371 1.1 cgd setpassent(stayopen)
372 1.1 cgd int stayopen;
373 1.1 cgd {
374 1.1 cgd _pw_keynum = 0;
375 1.1 cgd _pw_stayopen = stayopen;
376 1.1 cgd return(1);
377 1.1 cgd }
378 1.1 cgd
379 1.8 jtc void
380 1.1 cgd setpwent()
381 1.1 cgd {
382 1.1 cgd _pw_keynum = 0;
383 1.1 cgd _pw_stayopen = 0;
384 1.4 deraadt #ifdef YP
385 1.4 deraadt __ypmode = 0;
386 1.4 deraadt if(__ypcurrent)
387 1.4 deraadt free(__ypcurrent);
388 1.4 deraadt __ypcurrent = NULL;
389 1.4 deraadt #endif
390 1.1 cgd }
391 1.1 cgd
392 1.1 cgd void
393 1.1 cgd endpwent()
394 1.1 cgd {
395 1.1 cgd _pw_keynum = 0;
396 1.1 cgd if (_pw_db) {
397 1.1 cgd (void)(_pw_db->close)(_pw_db);
398 1.1 cgd _pw_db = (DB *)NULL;
399 1.1 cgd }
400 1.4 deraadt #ifdef YP
401 1.4 deraadt __ypmode = 0;
402 1.4 deraadt if(__ypcurrent)
403 1.4 deraadt free(__ypcurrent);
404 1.4 deraadt __ypcurrent = NULL;
405 1.4 deraadt #endif
406 1.1 cgd }
407 1.1 cgd
408 1.4 deraadt static int
409 1.1 cgd __initdb()
410 1.1 cgd {
411 1.1 cgd static int warned;
412 1.1 cgd char *p;
413 1.1 cgd
414 1.1 cgd p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
415 1.3 proven _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
416 1.1 cgd if (_pw_db)
417 1.1 cgd return(1);
418 1.1 cgd if (!warned)
419 1.1 cgd syslog(LOG_ERR, "%s: %m", p);
420 1.1 cgd return(0);
421 1.1 cgd }
422 1.1 cgd
423 1.4 deraadt static int
424 1.1 cgd __hashpw(key)
425 1.1 cgd DBT *key;
426 1.1 cgd {
427 1.1 cgd register char *p, *t;
428 1.1 cgd static u_int max;
429 1.1 cgd static char *line;
430 1.1 cgd DBT data;
431 1.1 cgd
432 1.1 cgd if ((_pw_db->get)(_pw_db, key, &data, 0))
433 1.1 cgd return(0);
434 1.1 cgd p = (char *)data.data;
435 1.1 cgd if (data.size > max && !(line = realloc(line, max += 1024)))
436 1.1 cgd return(0);
437 1.1 cgd
438 1.1 cgd t = line;
439 1.1 cgd #define EXPAND(e) e = t; while (*t++ = *p++);
440 1.1 cgd EXPAND(_pw_passwd.pw_name);
441 1.1 cgd EXPAND(_pw_passwd.pw_passwd);
442 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int));
443 1.1 cgd p += sizeof(int);
444 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int));
445 1.1 cgd p += sizeof(int);
446 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t));
447 1.1 cgd p += sizeof(time_t);
448 1.1 cgd EXPAND(_pw_passwd.pw_class);
449 1.1 cgd EXPAND(_pw_passwd.pw_gecos);
450 1.1 cgd EXPAND(_pw_passwd.pw_dir);
451 1.1 cgd EXPAND(_pw_passwd.pw_shell);
452 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t));
453 1.1 cgd p += sizeof(time_t);
454 1.1 cgd return(1);
455 1.1 cgd }
456