getpwent.c revision 1.40 1 1.40 lukem /* $NetBSD: getpwent.c,v 1.40 1999/01/26 01:08:06 lukem Exp $ */
2 1.12 cgd
3 1.1 cgd /*
4 1.12 cgd * Copyright (c) 1988, 1993
5 1.12 cgd * The Regents of the University of California. All rights reserved.
6 1.14 phil * Portions Copyright (c) 1994, 1995, Jason Downs. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.22 christos #include <sys/cdefs.h>
38 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
39 1.12 cgd #if 0
40 1.24 perry static char sccsid[] = "@(#)getpwent.c 8.2 (Berkeley) 4/27/95";
41 1.12 cgd #else
42 1.40 lukem __RCSID("$NetBSD: getpwent.c,v 1.40 1999/01/26 01:08:06 lukem Exp $");
43 1.12 cgd #endif
44 1.1 cgd #endif /* LIBC_SCCS and not lint */
45 1.1 cgd
46 1.23 jtc #include "namespace.h"
47 1.1 cgd #include <sys/param.h>
48 1.1 cgd #include <fcntl.h>
49 1.1 cgd #include <db.h>
50 1.1 cgd #include <syslog.h>
51 1.1 cgd #include <pwd.h>
52 1.1 cgd #include <utmp.h>
53 1.1 cgd #include <errno.h>
54 1.1 cgd #include <unistd.h>
55 1.1 cgd #include <stdlib.h>
56 1.1 cgd #include <string.h>
57 1.1 cgd #include <limits.h>
58 1.14 phil #include <netgroup.h>
59 1.32 lukem #include <nsswitch.h>
60 1.32 lukem #ifdef HESIOD
61 1.32 lukem #include <hesiod.h>
62 1.32 lukem #endif
63 1.4 deraadt #ifdef YP
64 1.14 phil #include <machine/param.h>
65 1.4 deraadt #include <stdio.h>
66 1.4 deraadt #include <rpc/rpc.h>
67 1.4 deraadt #include <rpcsvc/yp_prot.h>
68 1.4 deraadt #include <rpcsvc/ypclnt.h>
69 1.23 jtc #endif
70 1.23 jtc
71 1.27 thorpej #include "pw_private.h"
72 1.27 thorpej
73 1.40 lukem #if defined(YP) || defined(HESIOD)
74 1.40 lukem #define _PASSWD_COMPAT
75 1.40 lukem #endif
76 1.40 lukem
77 1.23 jtc #ifdef __weak_alias
78 1.23 jtc __weak_alias(endpwent,_endpwent);
79 1.23 jtc __weak_alias(getpwent,_getpwent);
80 1.23 jtc __weak_alias(getpwnam,_getpwnam);
81 1.23 jtc __weak_alias(getpwuid,_getpwuid);
82 1.23 jtc __weak_alias(setpassent,_setpassent);
83 1.23 jtc __weak_alias(setpwent,_setpwent);
84 1.4 deraadt #endif
85 1.1 cgd
86 1.24 perry
87 1.24 perry /*
88 1.24 perry * The lookup techniques and data extraction code here must be kept
89 1.24 perry * in sync with that in `pwd_mkdb'.
90 1.24 perry */
91 1.24 perry
92 1.1 cgd static struct passwd _pw_passwd; /* password structure */
93 1.1 cgd static DB *_pw_db; /* password database */
94 1.1 cgd static int _pw_keynum; /* key counter */
95 1.1 cgd static int _pw_stayopen; /* keep fd's open */
96 1.14 phil static int _pw_flags; /* password flags */
97 1.32 lukem static int _pw_none; /* true if getpwent got EOF */
98 1.32 lukem
99 1.14 phil static int __hashpw __P((DBT *));
100 1.14 phil static int __initdb __P((void));
101 1.14 phil
102 1.14 phil const char __yp_token[] = "__YP!"; /* Let pwd_mkdb pull this in. */
103 1.36 lukem static const ns_src compatsrc[] = {
104 1.36 lukem { NSSRC_COMPAT, NS_SUCCESS },
105 1.36 lukem { 0 }
106 1.36 lukem };
107 1.1 cgd
108 1.4 deraadt #ifdef YP
109 1.32 lukem static char *__ypcurrent, *__ypdomain;
110 1.32 lukem static int __ypcurrentlen;
111 1.32 lukem #endif
112 1.32 lukem
113 1.32 lukem #ifdef HESIOD
114 1.32 lukem static int _pw_hesnum;
115 1.32 lukem #endif
116 1.32 lukem
117 1.40 lukem #ifdef _PASSWD_COMPAT
118 1.32 lukem enum _pwmode { PWMODE_NONE, PWMODE_FULL, PWMODE_USER, PWMODE_NETGRP };
119 1.32 lukem static enum _pwmode __pwmode;
120 1.14 phil
121 1.26 lukem enum _ypmap { YPMAP_NONE, YPMAP_ADJUNCT, YPMAP_MASTER };
122 1.26 lukem
123 1.32 lukem static struct passwd *__pwproto = (struct passwd *)NULL;
124 1.32 lukem static int __pwproto_flags;
125 1.32 lukem static char line[1024];
126 1.32 lukem static long prbuf[1024 / sizeof(long)];
127 1.32 lukem static DB *__pwexclude = (DB *)NULL;
128 1.32 lukem
129 1.32 lukem static int __pwexclude_add __P((const char *));
130 1.32 lukem static int __pwexclude_is __P((const char *));
131 1.32 lukem static void __pwproto_set __P((void));
132 1.32 lukem static int __ypmaptype __P((void));
133 1.32 lukem static int __pwparse __P((struct passwd *, char *));
134 1.14 phil
135 1.26 lukem /* macros for deciding which YP maps to use. */
136 1.26 lukem #define PASSWD_BYNAME (__ypmaptype() == YPMAP_MASTER \
137 1.26 lukem ? "master.passwd.byname" : "passwd.byname")
138 1.26 lukem #define PASSWD_BYUID (__ypmaptype() == YPMAP_MASTER \
139 1.26 lukem ? "master.passwd.byuid" : "passwd.byuid")
140 1.26 lukem
141 1.32 lukem /*
142 1.32 lukem * add a name to the compat mode exclude list
143 1.32 lukem */
144 1.14 phil static int
145 1.32 lukem __pwexclude_add(name)
146 1.26 lukem const char *name;
147 1.14 phil {
148 1.30 christos DBT key;
149 1.30 christos DBT data;
150 1.14 phil
151 1.14 phil /* initialize the exclusion table if needed. */
152 1.32 lukem if(__pwexclude == (DB *)NULL) {
153 1.32 lukem __pwexclude = dbopen(NULL, O_RDWR, 600, DB_HASH, NULL);
154 1.32 lukem if(__pwexclude == (DB *)NULL)
155 1.32 lukem return 1;
156 1.14 phil }
157 1.14 phil
158 1.14 phil /* set up the key */
159 1.30 christos key.size = strlen(name);
160 1.30 christos /* LINTED key does not get modified */
161 1.14 phil key.data = (char *)name;
162 1.14 phil
163 1.14 phil /* data is nothing. */
164 1.14 phil data.data = NULL;
165 1.14 phil data.size = 0;
166 1.14 phil
167 1.14 phil /* store it */
168 1.32 lukem if((__pwexclude->put)(__pwexclude, &key, &data, 0) == -1)
169 1.32 lukem return 1;
170 1.14 phil
171 1.32 lukem return 0;
172 1.14 phil }
173 1.14 phil
174 1.32 lukem /*
175 1.32 lukem * test if a name is on the compat mode exclude list
176 1.32 lukem */
177 1.14 phil static int
178 1.32 lukem __pwexclude_is(name)
179 1.26 lukem const char *name;
180 1.14 phil {
181 1.31 christos DBT key;
182 1.30 christos DBT data;
183 1.14 phil
184 1.32 lukem if(__pwexclude == (DB *)NULL)
185 1.32 lukem return 0; /* nothing excluded */
186 1.14 phil
187 1.14 phil /* set up the key */
188 1.30 christos key.size = strlen(name);
189 1.30 christos /* LINTED key does not get modified */
190 1.14 phil key.data = (char *)name;
191 1.14 phil
192 1.32 lukem if((__pwexclude->get)(__pwexclude, &key, &data, 0) == 0)
193 1.32 lukem return 1; /* excluded */
194 1.14 phil
195 1.32 lukem return 0;
196 1.14 phil }
197 1.14 phil
198 1.32 lukem /*
199 1.32 lukem * setup the compat mode prototype template
200 1.32 lukem */
201 1.14 phil static void
202 1.32 lukem __pwproto_set()
203 1.14 phil {
204 1.17 lukem char *ptr;
205 1.17 lukem struct passwd *pw = &_pw_passwd;
206 1.14 phil
207 1.14 phil /* make this the new prototype */
208 1.30 christos ptr = (char *)(void *)prbuf;
209 1.14 phil
210 1.14 phil /* first allocate the struct. */
211 1.37 christos __pwproto = (struct passwd *)(void *)ptr;
212 1.14 phil ptr += sizeof(struct passwd);
213 1.14 phil
214 1.14 phil /* name */
215 1.14 phil if(pw->pw_name && (pw->pw_name)[0]) {
216 1.37 christos ptr = (char *)ALIGN((u_long)ptr);
217 1.29 perry memmove(ptr, pw->pw_name, strlen(pw->pw_name) + 1);
218 1.32 lukem __pwproto->pw_name = ptr;
219 1.14 phil ptr += (strlen(pw->pw_name) + 1);
220 1.14 phil } else
221 1.32 lukem __pwproto->pw_name = (char *)NULL;
222 1.14 phil
223 1.14 phil /* password */
224 1.14 phil if(pw->pw_passwd && (pw->pw_passwd)[0]) {
225 1.37 christos ptr = (char *)ALIGN((u_long)ptr);
226 1.29 perry memmove(ptr, pw->pw_passwd, strlen(pw->pw_passwd) + 1);
227 1.32 lukem __pwproto->pw_passwd = ptr;
228 1.14 phil ptr += (strlen(pw->pw_passwd) + 1);
229 1.14 phil } else
230 1.32 lukem __pwproto->pw_passwd = (char *)NULL;
231 1.14 phil
232 1.14 phil /* uid */
233 1.32 lukem __pwproto->pw_uid = pw->pw_uid;
234 1.14 phil
235 1.14 phil /* gid */
236 1.32 lukem __pwproto->pw_gid = pw->pw_gid;
237 1.14 phil
238 1.14 phil /* change (ignored anyway) */
239 1.32 lukem __pwproto->pw_change = pw->pw_change;
240 1.14 phil
241 1.14 phil /* class (ignored anyway) */
242 1.32 lukem __pwproto->pw_class = "";
243 1.14 phil
244 1.14 phil /* gecos */
245 1.14 phil if(pw->pw_gecos && (pw->pw_gecos)[0]) {
246 1.37 christos ptr = (char *)ALIGN((u_long)ptr);
247 1.29 perry memmove(ptr, pw->pw_gecos, strlen(pw->pw_gecos) + 1);
248 1.32 lukem __pwproto->pw_gecos = ptr;
249 1.14 phil ptr += (strlen(pw->pw_gecos) + 1);
250 1.14 phil } else
251 1.32 lukem __pwproto->pw_gecos = (char *)NULL;
252 1.14 phil
253 1.14 phil /* dir */
254 1.14 phil if(pw->pw_dir && (pw->pw_dir)[0]) {
255 1.37 christos ptr = (char *)ALIGN((u_long)ptr);
256 1.29 perry memmove(ptr, pw->pw_dir, strlen(pw->pw_dir) + 1);
257 1.32 lukem __pwproto->pw_dir = ptr;
258 1.14 phil ptr += (strlen(pw->pw_dir) + 1);
259 1.14 phil } else
260 1.32 lukem __pwproto->pw_dir = (char *)NULL;
261 1.14 phil
262 1.14 phil /* shell */
263 1.14 phil if(pw->pw_shell && (pw->pw_shell)[0]) {
264 1.37 christos ptr = (char *)ALIGN((u_long)ptr);
265 1.29 perry memmove(ptr, pw->pw_shell, strlen(pw->pw_shell) + 1);
266 1.32 lukem __pwproto->pw_shell = ptr;
267 1.14 phil ptr += (strlen(pw->pw_shell) + 1);
268 1.14 phil } else
269 1.32 lukem __pwproto->pw_shell = (char *)NULL;
270 1.14 phil
271 1.14 phil /* expire (ignored anyway) */
272 1.32 lukem __pwproto->pw_expire = pw->pw_expire;
273 1.14 phil
274 1.14 phil /* flags */
275 1.32 lukem __pwproto_flags = _pw_flags;
276 1.14 phil }
277 1.4 deraadt
278 1.5 deraadt static int
279 1.26 lukem __ypmaptype()
280 1.26 lukem {
281 1.26 lukem static int maptype = -1;
282 1.26 lukem int order, r;
283 1.26 lukem
284 1.26 lukem if (maptype != -1)
285 1.26 lukem return (maptype);
286 1.26 lukem
287 1.26 lukem maptype = YPMAP_NONE;
288 1.26 lukem if (geteuid() != 0)
289 1.26 lukem return (maptype);
290 1.26 lukem
291 1.26 lukem if (!__ypdomain) {
292 1.26 lukem if( _yp_check(&__ypdomain) == 0)
293 1.26 lukem return (maptype);
294 1.26 lukem }
295 1.26 lukem
296 1.26 lukem r = yp_order(__ypdomain, "master.passwd.byname", &order);
297 1.26 lukem if (r == 0) {
298 1.26 lukem maptype = YPMAP_MASTER;
299 1.26 lukem return (maptype);
300 1.26 lukem }
301 1.26 lukem
302 1.26 lukem /*
303 1.26 lukem * NIS+ in YP compat mode doesn't support
304 1.26 lukem * YPPROC_ORDER -- no point in continuing.
305 1.26 lukem */
306 1.26 lukem if (r == YPERR_YPERR)
307 1.26 lukem return (maptype);
308 1.26 lukem
309 1.26 lukem /* master.passwd doesn't exist -- try passwd.adjunct */
310 1.26 lukem if (r == YPERR_MAP) {
311 1.26 lukem r = yp_order(__ypdomain, "passwd.adjunct.byname", &order);
312 1.26 lukem if (r == 0)
313 1.26 lukem maptype = YPMAP_ADJUNCT;
314 1.26 lukem return (maptype);
315 1.26 lukem }
316 1.26 lukem
317 1.26 lukem return (maptype);
318 1.26 lukem }
319 1.26 lukem
320 1.32 lukem /*
321 1.32 lukem * parse an old-style passwd file line (from NIS or HESIOD)
322 1.32 lukem */
323 1.26 lukem static int
324 1.32 lukem __pwparse(pw, s)
325 1.26 lukem struct passwd *pw;
326 1.26 lukem char *s;
327 1.4 deraadt {
328 1.26 lukem static char adjunctpw[YPMAXRECORD + 2];
329 1.26 lukem int flags, maptype;
330 1.4 deraadt
331 1.26 lukem maptype = __ypmaptype();
332 1.26 lukem flags = _PASSWORD_NOWARN;
333 1.26 lukem if (maptype != YPMAP_MASTER)
334 1.26 lukem flags |= _PASSWORD_OLDFMT;
335 1.27 thorpej if (! __pw_scan(s, pw, &flags))
336 1.13 mycroft return 1;
337 1.14 phil
338 1.14 phil /* now let the prototype override, if set. */
339 1.32 lukem if(__pwproto != (struct passwd *)NULL) {
340 1.32 lukem #ifdef PW_OVERRIDE_PASSWD
341 1.32 lukem if(__pwproto->pw_passwd != (char *)NULL)
342 1.32 lukem pw->pw_passwd = __pwproto->pw_passwd;
343 1.14 phil #endif
344 1.32 lukem if(!(__pwproto_flags & _PASSWORD_NOUID))
345 1.32 lukem pw->pw_uid = __pwproto->pw_uid;
346 1.32 lukem if(!(__pwproto_flags & _PASSWORD_NOGID))
347 1.32 lukem pw->pw_gid = __pwproto->pw_gid;
348 1.32 lukem if(__pwproto->pw_gecos != (char *)NULL)
349 1.32 lukem pw->pw_gecos = __pwproto->pw_gecos;
350 1.32 lukem if(__pwproto->pw_dir != (char *)NULL)
351 1.32 lukem pw->pw_dir = __pwproto->pw_dir;
352 1.32 lukem if(__pwproto->pw_shell != (char *)NULL)
353 1.32 lukem pw->pw_shell = __pwproto->pw_shell;
354 1.14 phil }
355 1.26 lukem if ((maptype == YPMAP_ADJUNCT) &&
356 1.26 lukem (strstr(pw->pw_passwd, "##") != NULL)) {
357 1.26 lukem char *data, *bp;
358 1.26 lukem int datalen;
359 1.26 lukem
360 1.26 lukem if (yp_match(__ypdomain, "passwd.adjunct.byname", pw->pw_name,
361 1.30 christos (int)strlen(pw->pw_name), &data, &datalen) == 0) {
362 1.26 lukem if (datalen > sizeof(adjunctpw) - 1)
363 1.26 lukem datalen = sizeof(adjunctpw) - 1;
364 1.37 christos strncpy(adjunctpw, data, (size_t)datalen);
365 1.26 lukem
366 1.26 lukem /* skip name to get password */
367 1.26 lukem if ((bp = strsep(&data, ":")) != NULL &&
368 1.26 lukem (bp = strsep(&data, ":")) != NULL)
369 1.26 lukem pw->pw_passwd = bp;
370 1.26 lukem }
371 1.26 lukem }
372 1.4 deraadt return 0;
373 1.4 deraadt }
374 1.40 lukem #endif /* _PASSWD_COMPAT */
375 1.32 lukem
376 1.32 lukem /*
377 1.32 lukem * local files implementation of getpw*()
378 1.32 lukem * varargs: type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
379 1.32 lukem */
380 1.32 lukem static int _local_getpw __P((void *, void *, va_list));
381 1.4 deraadt
382 1.37 christos /*ARGSUSED*/
383 1.32 lukem static int
384 1.32 lukem _local_getpw(rv, cb_data, ap)
385 1.32 lukem void *rv;
386 1.32 lukem void *cb_data;
387 1.32 lukem va_list ap;
388 1.1 cgd {
389 1.32 lukem DBT key;
390 1.37 christos char bf[/*CONSTCOND*/ MAX(MAXLOGNAME, sizeof(_pw_keynum)) + 1];
391 1.32 lukem uid_t uid;
392 1.32 lukem int search, len, rval;
393 1.32 lukem const char *name;
394 1.1 cgd
395 1.1 cgd if (!_pw_db && !__initdb())
396 1.32 lukem return NS_UNAVAIL;
397 1.32 lukem
398 1.32 lukem search = va_arg(ap, int);
399 1.32 lukem bf[0] = search;
400 1.32 lukem switch (search) {
401 1.32 lukem case _PW_KEYBYNUM:
402 1.32 lukem ++_pw_keynum;
403 1.37 christos memmove(bf + 1, &_pw_keynum, sizeof(_pw_keynum));
404 1.32 lukem key.size = sizeof(_pw_keynum) + 1;
405 1.32 lukem break;
406 1.32 lukem case _PW_KEYBYNAME:
407 1.32 lukem name = va_arg(ap, const char *);
408 1.32 lukem len = strlen(name);
409 1.37 christos memmove(bf + 1, name, (size_t)MIN(len, MAXLOGNAME));
410 1.32 lukem key.size = len + 1;
411 1.32 lukem break;
412 1.32 lukem case _PW_KEYBYUID:
413 1.32 lukem uid = va_arg(ap, uid_t);
414 1.37 christos memmove(bf + 1, &uid, sizeof(len));
415 1.32 lukem key.size = sizeof(uid) + 1;
416 1.32 lukem break;
417 1.32 lukem default:
418 1.32 lukem abort();
419 1.32 lukem }
420 1.32 lukem
421 1.32 lukem key.data = (u_char *)bf;
422 1.32 lukem rval = __hashpw(&key);
423 1.32 lukem if (rval == NS_NOTFOUND && search == _PW_KEYBYNUM) {
424 1.32 lukem _pw_none = 1;
425 1.32 lukem rval = NS_SUCCESS;
426 1.32 lukem }
427 1.32 lukem
428 1.32 lukem if (!_pw_stayopen && (search != _PW_KEYBYNUM)) {
429 1.32 lukem (void)(_pw_db->close)(_pw_db);
430 1.32 lukem _pw_db = (DB *)NULL;
431 1.32 lukem }
432 1.32 lukem return (rval);
433 1.32 lukem }
434 1.32 lukem
435 1.32 lukem #ifdef HESIOD
436 1.32 lukem /*
437 1.32 lukem * hesiod implementation of getpw*()
438 1.32 lukem * varargs: type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
439 1.32 lukem */
440 1.32 lukem static int _dns_getpw __P((void *, void *, va_list));
441 1.32 lukem
442 1.37 christos /*ARGSUSED*/
443 1.32 lukem static int
444 1.32 lukem _dns_getpw(rv, cb_data, ap)
445 1.32 lukem void *rv;
446 1.32 lukem void *cb_data;
447 1.32 lukem va_list ap;
448 1.32 lukem {
449 1.32 lukem const char *name;
450 1.32 lukem uid_t uid;
451 1.32 lukem int search;
452 1.39 lukem
453 1.38 mycroft char *map;
454 1.32 lukem char **hp;
455 1.39 lukem void *context;
456 1.39 lukem int r;
457 1.32 lukem
458 1.32 lukem search = va_arg(ap, int);
459 1.32 lukem switch (search) {
460 1.32 lukem case _PW_KEYBYNUM:
461 1.32 lukem snprintf(line, sizeof(line) - 1, "passwd-%u", _pw_hesnum);
462 1.32 lukem _pw_hesnum++;
463 1.38 mycroft map = "passwd";
464 1.32 lukem break;
465 1.32 lukem case _PW_KEYBYNAME:
466 1.32 lukem name = va_arg(ap, const char *);
467 1.32 lukem strncpy(line, name, sizeof(line));
468 1.38 mycroft map = "passwd";
469 1.32 lukem break;
470 1.32 lukem case _PW_KEYBYUID:
471 1.32 lukem uid = va_arg(ap, uid_t);
472 1.33 lukem snprintf(line, sizeof(line), "%u", (unsigned int)uid);
473 1.40 lukem map = "uid"; /* XXX this is `passwd' on ultrix */
474 1.32 lukem break;
475 1.32 lukem default:
476 1.32 lukem abort();
477 1.32 lukem }
478 1.32 lukem line[sizeof(line) - 1] = '\0';
479 1.32 lukem
480 1.39 lukem r = NS_UNAVAIL;
481 1.39 lukem if (hesiod_init(&context) == -1)
482 1.39 lukem return (r);
483 1.39 lukem
484 1.39 lukem hp = hesiod_resolve(context, line, map);
485 1.32 lukem if (hp == NULL) {
486 1.39 lukem if (errno == ENOENT) {
487 1.32 lukem if (search == _PW_KEYBYNUM) {
488 1.32 lukem _pw_hesnum = 0;
489 1.32 lukem _pw_none = 1;
490 1.39 lukem r = NS_SUCCESS;
491 1.39 lukem } else
492 1.39 lukem r = NS_NOTFOUND;
493 1.32 lukem }
494 1.39 lukem goto cleanup_dns_getpw;
495 1.32 lukem }
496 1.32 lukem
497 1.32 lukem strncpy(line, hp[0], sizeof(line)); /* only check first elem */
498 1.32 lukem line[sizeof(line) - 1] = '\0';
499 1.39 lukem hesiod_free_list(context, hp);
500 1.32 lukem if (__pwparse(&_pw_passwd, line))
501 1.39 lukem r = NS_UNAVAIL;
502 1.39 lukem else
503 1.39 lukem r = NS_SUCCESS;
504 1.39 lukem cleanup_dns_getpw:
505 1.39 lukem hesiod_end(context);
506 1.39 lukem return (r);
507 1.32 lukem }
508 1.32 lukem #endif
509 1.1 cgd
510 1.4 deraadt #ifdef YP
511 1.32 lukem /*
512 1.32 lukem * nis implementation of getpw*()
513 1.32 lukem * varargs: type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
514 1.32 lukem */
515 1.32 lukem static int _nis_getpw __P((void *, void *, va_list));
516 1.14 phil
517 1.37 christos /*ARGSUSED*/
518 1.32 lukem static int
519 1.32 lukem _nis_getpw(rv, cb_data, ap)
520 1.32 lukem void *rv;
521 1.32 lukem void *cb_data;
522 1.32 lukem va_list ap;
523 1.32 lukem {
524 1.32 lukem const char *name;
525 1.32 lukem uid_t uid;
526 1.32 lukem int search;
527 1.32 lukem char *key, *data;
528 1.32 lukem char *map = PASSWD_BYNAME;
529 1.32 lukem int keylen, datalen, r;
530 1.32 lukem
531 1.32 lukem if(__ypdomain == NULL) {
532 1.32 lukem if(_yp_check(&__ypdomain) == 0)
533 1.32 lukem return NS_UNAVAIL;
534 1.32 lukem }
535 1.32 lukem
536 1.32 lukem search = va_arg(ap, int);
537 1.32 lukem switch (search) {
538 1.32 lukem case _PW_KEYBYNUM:
539 1.32 lukem break;
540 1.32 lukem case _PW_KEYBYNAME:
541 1.32 lukem name = va_arg(ap, const char *);
542 1.32 lukem strncpy(line, name, sizeof(line));
543 1.32 lukem break;
544 1.32 lukem case _PW_KEYBYUID:
545 1.32 lukem uid = va_arg(ap, uid_t);
546 1.33 lukem snprintf(line, sizeof(line), "%u", (unsigned int)uid);
547 1.32 lukem map = PASSWD_BYUID;
548 1.32 lukem break;
549 1.32 lukem default:
550 1.32 lukem abort();
551 1.32 lukem }
552 1.32 lukem line[sizeof(line) - 1] = '\0';
553 1.32 lukem if (search != _PW_KEYBYNUM) {
554 1.32 lukem data = NULL;
555 1.32 lukem r = yp_match(__ypdomain, map, line, (int)strlen(line),
556 1.32 lukem &data, &datalen);
557 1.32 lukem switch (r) {
558 1.32 lukem case 0:
559 1.32 lukem break;
560 1.32 lukem case YPERR_KEY:
561 1.32 lukem r = NS_NOTFOUND;
562 1.32 lukem break;
563 1.32 lukem default:
564 1.32 lukem r = NS_UNAVAIL;
565 1.32 lukem break;
566 1.32 lukem }
567 1.32 lukem if (r != 0) {
568 1.32 lukem if (data)
569 1.32 lukem free(data);
570 1.32 lukem return r;
571 1.4 deraadt }
572 1.32 lukem data[datalen] = '\0'; /* clear trailing \n */
573 1.32 lukem strncpy(line, data, sizeof(line));
574 1.32 lukem line[sizeof(line) - 1] = '\0';
575 1.32 lukem free(data);
576 1.32 lukem if (__pwparse(&_pw_passwd, line))
577 1.32 lukem return NS_UNAVAIL;
578 1.32 lukem return NS_SUCCESS;
579 1.32 lukem }
580 1.32 lukem
581 1.32 lukem for (;;) {
582 1.32 lukem data = key = NULL;
583 1.32 lukem if (__ypcurrent) {
584 1.32 lukem r = yp_next(__ypdomain, map,
585 1.14 phil __ypcurrent, __ypcurrentlen,
586 1.14 phil &key, &keylen, &data, &datalen);
587 1.32 lukem free(__ypcurrent);
588 1.32 lukem switch (r) {
589 1.32 lukem case 0:
590 1.32 lukem __ypcurrent = key;
591 1.32 lukem __ypcurrentlen = keylen;
592 1.32 lukem break;
593 1.32 lukem case YPERR_NOMORE:
594 1.32 lukem __ypcurrent = NULL;
595 1.32 lukem _pw_none = 1;
596 1.32 lukem if (key)
597 1.32 lukem free(key);
598 1.32 lukem return NS_SUCCESS;
599 1.32 lukem default:
600 1.32 lukem r = NS_UNAVAIL;
601 1.32 lukem break;
602 1.17 lukem }
603 1.32 lukem } else {
604 1.32 lukem r = 0;
605 1.32 lukem if (yp_first(__ypdomain, map, &__ypcurrent,
606 1.32 lukem &__ypcurrentlen, &data, &datalen))
607 1.32 lukem r = NS_UNAVAIL;
608 1.32 lukem }
609 1.32 lukem if (r != 0) {
610 1.32 lukem if (key)
611 1.32 lukem free(key);
612 1.32 lukem if (data)
613 1.32 lukem free(data);
614 1.32 lukem return r;
615 1.32 lukem }
616 1.32 lukem data[datalen] = '\0'; /* clear trailing \n */
617 1.32 lukem strncpy(line, data, sizeof(line));
618 1.32 lukem line[sizeof(line) - 1] = '\0';
619 1.32 lukem free(data);
620 1.32 lukem if (! __pwparse(&_pw_passwd, line))
621 1.32 lukem return NS_SUCCESS;
622 1.32 lukem }
623 1.32 lukem /* NOTREACHED */
624 1.32 lukem } /* _nis_getpw */
625 1.32 lukem #endif
626 1.32 lukem
627 1.40 lukem #ifdef _PASSWD_COMPAT
628 1.32 lukem /*
629 1.32 lukem * See if the compat token is in the database. Only works if pwd_mkdb knows
630 1.32 lukem * about the token.
631 1.32 lukem */
632 1.32 lukem static int __has_compatpw __P((void));
633 1.32 lukem
634 1.32 lukem static int
635 1.32 lukem __has_compatpw()
636 1.32 lukem {
637 1.32 lukem DBT key, data;
638 1.32 lukem DBT pkey, pdata;
639 1.35 lukem char bf[MAXLOGNAME];
640 1.32 lukem
641 1.37 christos /*LINTED*/
642 1.32 lukem key.data = (u_char *)__yp_token;
643 1.32 lukem key.size = strlen(__yp_token);
644 1.32 lukem
645 1.32 lukem /* Pre-token database support. */
646 1.32 lukem bf[0] = _PW_KEYBYNAME;
647 1.35 lukem bf[1] = '+';
648 1.32 lukem pkey.data = (u_char *)bf;
649 1.35 lukem pkey.size = 2;
650 1.32 lukem
651 1.32 lukem if ((_pw_db->get)(_pw_db, &key, &data, 0)
652 1.32 lukem && (_pw_db->get)(_pw_db, &pkey, &pdata, 0))
653 1.32 lukem return 0; /* No compat token */
654 1.32 lukem return 1;
655 1.32 lukem }
656 1.32 lukem
657 1.32 lukem /*
658 1.32 lukem * log an error if "files" or "compat" is specified in passwd_compat database
659 1.32 lukem */
660 1.32 lukem static int _bad_getpw __P((void *, void *, va_list));
661 1.32 lukem
662 1.37 christos /*ARGSUSED*/
663 1.32 lukem static int
664 1.32 lukem _bad_getpw(rv, cb_data, ap)
665 1.32 lukem void *rv;
666 1.32 lukem void *cb_data;
667 1.32 lukem va_list ap;
668 1.32 lukem {
669 1.32 lukem static int warned;
670 1.32 lukem if (!warned) {
671 1.32 lukem syslog(LOG_ERR,
672 1.32 lukem "nsswitch.conf passwd_compat database can't use '%s'",
673 1.32 lukem (char *)cb_data);
674 1.32 lukem }
675 1.32 lukem warned = 1;
676 1.32 lukem return NS_UNAVAIL;
677 1.32 lukem }
678 1.32 lukem
679 1.32 lukem /*
680 1.32 lukem * when a name lookup in compat mode is required (e.g., '+name', or a name in
681 1.32 lukem * '+@netgroup'), look it up in the 'passwd_compat' nsswitch database.
682 1.32 lukem * only Hesiod and NIS is supported - it doesn't make sense to lookup
683 1.32 lukem * compat names from 'files' or 'compat'.
684 1.32 lukem */
685 1.32 lukem static int __getpwcompat __P((int, uid_t, const char *));
686 1.32 lukem
687 1.32 lukem static int
688 1.32 lukem __getpwcompat(type, uid, name)
689 1.32 lukem int type;
690 1.32 lukem uid_t uid;
691 1.32 lukem const char *name;
692 1.32 lukem {
693 1.36 lukem static const ns_dtab dtab[] = {
694 1.35 lukem NS_FILES_CB(_bad_getpw, "files")
695 1.35 lukem NS_DNS_CB(_dns_getpw, NULL)
696 1.35 lukem NS_NIS_CB(_nis_getpw, NULL)
697 1.35 lukem NS_COMPAT_CB(_bad_getpw, "compat")
698 1.35 lukem { 0 }
699 1.32 lukem };
700 1.36 lukem static const ns_src defaultnis[] = {
701 1.36 lukem { NSSRC_NIS, NS_SUCCESS },
702 1.36 lukem { 0 }
703 1.36 lukem };
704 1.32 lukem
705 1.32 lukem switch (type) {
706 1.32 lukem case _PW_KEYBYNUM:
707 1.35 lukem return nsdispatch(NULL, dtab, NSDB_PASSWD_COMPAT, "getpwcompat",
708 1.36 lukem defaultnis, type);
709 1.32 lukem case _PW_KEYBYNAME:
710 1.35 lukem return nsdispatch(NULL, dtab, NSDB_PASSWD_COMPAT, "getpwcompat",
711 1.36 lukem defaultnis, type, name);
712 1.32 lukem case _PW_KEYBYUID:
713 1.35 lukem return nsdispatch(NULL, dtab, NSDB_PASSWD_COMPAT, "getpwcompat",
714 1.36 lukem defaultnis, type, uid);
715 1.32 lukem default:
716 1.32 lukem abort();
717 1.37 christos /*NOTREACHED*/
718 1.32 lukem }
719 1.32 lukem }
720 1.40 lukem #endif /* _PASSWD_COMPAT */
721 1.32 lukem
722 1.32 lukem /*
723 1.32 lukem * compat implementation of getpwent()
724 1.32 lukem * varargs (ignored):
725 1.32 lukem * type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
726 1.32 lukem */
727 1.32 lukem static int _compat_getpwent __P((void *, void *, va_list));
728 1.32 lukem
729 1.37 christos /*ARGSUSED*/
730 1.32 lukem static int
731 1.32 lukem _compat_getpwent(rv, cb_data, ap)
732 1.32 lukem void *rv;
733 1.32 lukem void *cb_data;
734 1.32 lukem va_list ap;
735 1.32 lukem {
736 1.32 lukem DBT key;
737 1.32 lukem char bf[sizeof(_pw_keynum) + 1];
738 1.40 lukem #ifdef _PASSWD_COMPAT
739 1.32 lukem static char *name = NULL;
740 1.32 lukem const char *user, *host, *dom;
741 1.32 lukem int has_compatpw;
742 1.40 lukem #endif
743 1.32 lukem
744 1.32 lukem if (!_pw_db && !__initdb())
745 1.32 lukem return NS_UNAVAIL;
746 1.32 lukem
747 1.40 lukem #ifdef _PASSWD_COMPAT
748 1.32 lukem has_compatpw = __has_compatpw();
749 1.32 lukem
750 1.32 lukem again:
751 1.32 lukem if (has_compatpw && (__pwmode != PWMODE_NONE)) {
752 1.32 lukem int r;
753 1.32 lukem
754 1.32 lukem switch (__pwmode) {
755 1.32 lukem case PWMODE_FULL:
756 1.32 lukem r = __getpwcompat(_PW_KEYBYNUM, 0, NULL);
757 1.32 lukem if (r == NS_SUCCESS)
758 1.32 lukem return r;
759 1.32 lukem __pwmode = PWMODE_NONE;
760 1.14 phil break;
761 1.32 lukem
762 1.32 lukem case PWMODE_NETGRP:
763 1.32 lukem r = getnetgrent(&host, &user, &dom);
764 1.32 lukem if (r == 0) { /* end of group */
765 1.14 phil endnetgrent();
766 1.32 lukem __pwmode = PWMODE_NONE;
767 1.32 lukem break;
768 1.14 phil }
769 1.32 lukem if (!user || !*user)
770 1.32 lukem break;
771 1.32 lukem r = __getpwcompat(_PW_KEYBYNAME, 0, user);
772 1.32 lukem if (r == NS_SUCCESS)
773 1.32 lukem return r;
774 1.14 phil break;
775 1.32 lukem
776 1.32 lukem case PWMODE_USER:
777 1.32 lukem if (name == NULL) {
778 1.32 lukem __pwmode = PWMODE_NONE;
779 1.32 lukem break;
780 1.4 deraadt }
781 1.32 lukem r = __getpwcompat(_PW_KEYBYNAME, 0, name);
782 1.32 lukem free(name);
783 1.32 lukem name = NULL;
784 1.32 lukem if (r == NS_SUCCESS)
785 1.32 lukem return r;
786 1.14 phil break;
787 1.32 lukem
788 1.32 lukem case PWMODE_NONE:
789 1.32 lukem abort();
790 1.4 deraadt }
791 1.32 lukem goto again;
792 1.4 deraadt }
793 1.40 lukem #endif
794 1.4 deraadt
795 1.1 cgd ++_pw_keynum;
796 1.1 cgd bf[0] = _PW_KEYBYNUM;
797 1.37 christos memmove(bf + 1, &_pw_keynum, sizeof(_pw_keynum));
798 1.32 lukem key.data = (u_char *)bf;
799 1.32 lukem key.size = sizeof(_pw_keynum) + 1;
800 1.32 lukem if(__hashpw(&key) == NS_SUCCESS) {
801 1.40 lukem #ifdef _PASSWD_COMPAT
802 1.14 phil /* if we don't have YP at all, don't bother. */
803 1.32 lukem if (has_compatpw) {
804 1.14 phil if(_pw_passwd.pw_name[0] == '+') {
805 1.14 phil /* set the mode */
806 1.14 phil switch(_pw_passwd.pw_name[1]) {
807 1.14 phil case '\0':
808 1.32 lukem __pwmode = PWMODE_FULL;
809 1.14 phil break;
810 1.14 phil case '@':
811 1.32 lukem __pwmode = PWMODE_NETGRP;
812 1.14 phil setnetgrent(_pw_passwd.pw_name + 2);
813 1.14 phil break;
814 1.14 phil default:
815 1.32 lukem __pwmode = PWMODE_USER;
816 1.14 phil name = strdup(_pw_passwd.pw_name + 1);
817 1.14 phil break;
818 1.14 phil }
819 1.14 phil
820 1.14 phil /* save the prototype */
821 1.32 lukem __pwproto_set();
822 1.14 phil goto again;
823 1.14 phil } else if(_pw_passwd.pw_name[0] == '-') {
824 1.14 phil /* an attempted exclusion */
825 1.14 phil switch(_pw_passwd.pw_name[1]) {
826 1.14 phil case '\0':
827 1.14 phil break;
828 1.14 phil case '@':
829 1.14 phil setnetgrent(_pw_passwd.pw_name + 2);
830 1.14 phil while(getnetgrent(&host, &user, &dom)) {
831 1.14 phil if(user && *user)
832 1.32 lukem __pwexclude_add(user);
833 1.14 phil }
834 1.14 phil endnetgrent();
835 1.14 phil break;
836 1.14 phil default:
837 1.32 lukem __pwexclude_add(_pw_passwd.pw_name + 1);
838 1.14 phil break;
839 1.14 phil }
840 1.14 phil goto again;
841 1.14 phil }
842 1.4 deraadt }
843 1.40 lukem #endif
844 1.32 lukem return NS_SUCCESS;
845 1.4 deraadt }
846 1.32 lukem return NS_NOTFOUND;
847 1.1 cgd }
848 1.1 cgd
849 1.14 phil /*
850 1.32 lukem * compat implementation of getpwnam() and getpwuid()
851 1.32 lukem * varargs: type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
852 1.14 phil */
853 1.32 lukem static int _compat_getpw __P((void *, void *, va_list));
854 1.32 lukem
855 1.14 phil static int
856 1.32 lukem _compat_getpw(rv, cb_data, ap)
857 1.32 lukem void *rv;
858 1.32 lukem void *cb_data;
859 1.32 lukem va_list ap;
860 1.14 phil {
861 1.40 lukem #ifdef _PASSWD_COMPAT
862 1.32 lukem DBT key;
863 1.34 lukem int search, rval, r, s;
864 1.32 lukem uid_t uid;
865 1.32 lukem char bf[MAXLOGNAME + 1];
866 1.34 lukem const char *name, *host, *user, *dom;
867 1.40 lukem #endif
868 1.34 lukem
869 1.34 lukem if (!_pw_db && !__initdb())
870 1.34 lukem return NS_UNAVAIL;
871 1.34 lukem
872 1.34 lukem /*
873 1.34 lukem * If there isn't a compat token in the database, use files.
874 1.34 lukem */
875 1.40 lukem #ifdef _PASSWD_COMPAT
876 1.34 lukem if (! __has_compatpw())
877 1.40 lukem #endif
878 1.34 lukem return (_local_getpw(rv, cb_data, ap));
879 1.32 lukem
880 1.40 lukem #ifdef _PASSWD_COMPAT
881 1.32 lukem search = va_arg(ap, int);
882 1.32 lukem uid = 0;
883 1.32 lukem name = NULL;
884 1.32 lukem rval = NS_NOTFOUND;
885 1.32 lukem switch (search) {
886 1.32 lukem case _PW_KEYBYNAME:
887 1.32 lukem name = va_arg(ap, const char *);
888 1.32 lukem break;
889 1.32 lukem case _PW_KEYBYUID:
890 1.32 lukem uid = va_arg(ap, uid_t);
891 1.32 lukem break;
892 1.32 lukem default:
893 1.32 lukem abort();
894 1.32 lukem }
895 1.14 phil
896 1.34 lukem for(s = -1, _pw_keynum=1; _pw_keynum; _pw_keynum++) {
897 1.34 lukem bf[0] = _PW_KEYBYNUM;
898 1.37 christos memmove(bf + 1, &_pw_keynum, sizeof(_pw_keynum));
899 1.34 lukem key.data = (u_char *)bf;
900 1.34 lukem key.size = sizeof(_pw_keynum) + 1;
901 1.34 lukem if(__hashpw(&key) != NS_SUCCESS)
902 1.34 lukem break;
903 1.34 lukem switch(_pw_passwd.pw_name[0]) {
904 1.34 lukem case '+':
905 1.34 lukem /* save the prototype */
906 1.34 lukem __pwproto_set();
907 1.34 lukem
908 1.34 lukem switch(_pw_passwd.pw_name[1]) {
909 1.34 lukem case '\0':
910 1.34 lukem r = __getpwcompat(search, uid, name);
911 1.34 lukem if (r != NS_SUCCESS)
912 1.34 lukem continue;
913 1.10 deraadt break;
914 1.34 lukem case '@':
915 1.14 phil pwnam_netgrp:
916 1.34 lukem if(__ypcurrent) {
917 1.34 lukem free(__ypcurrent);
918 1.34 lukem __ypcurrent = NULL;
919 1.34 lukem }
920 1.34 lukem if (s == -1) /* first time */
921 1.34 lukem setnetgrent(_pw_passwd.pw_name + 2);
922 1.34 lukem s = getnetgrent(&host, &user, &dom);
923 1.34 lukem if (s == 0) { /* end of group */
924 1.34 lukem endnetgrent();
925 1.34 lukem s = -1;
926 1.34 lukem continue;
927 1.34 lukem }
928 1.34 lukem if (!user || !*user)
929 1.34 lukem goto pwnam_netgrp;
930 1.32 lukem
931 1.34 lukem r = __getpwcompat(_PW_KEYBYNAME, 0, user);
932 1.32 lukem
933 1.34 lukem if (r == NS_UNAVAIL)
934 1.34 lukem return r;
935 1.34 lukem if (r == NS_NOTFOUND) {
936 1.34 lukem /*
937 1.34 lukem * just because this user is bad
938 1.34 lukem * it doesn't mean they all are.
939 1.34 lukem */
940 1.34 lukem goto pwnam_netgrp;
941 1.4 deraadt }
942 1.34 lukem break;
943 1.34 lukem default:
944 1.34 lukem user = _pw_passwd.pw_name + 1;
945 1.34 lukem r = __getpwcompat(_PW_KEYBYNAME, 0, user);
946 1.34 lukem
947 1.34 lukem if (r == NS_UNAVAIL)
948 1.34 lukem return r;
949 1.34 lukem if (r == NS_NOTFOUND)
950 1.10 deraadt continue;
951 1.14 phil break;
952 1.34 lukem }
953 1.34 lukem if(__pwexclude_is(_pw_passwd.pw_name)) {
954 1.34 lukem if(s == 1) /* inside netgroup */
955 1.34 lukem goto pwnam_netgrp;
956 1.34 lukem continue;
957 1.34 lukem }
958 1.34 lukem break;
959 1.34 lukem case '-':
960 1.34 lukem /* attempted exclusion */
961 1.34 lukem switch(_pw_passwd.pw_name[1]) {
962 1.34 lukem case '\0':
963 1.34 lukem break;
964 1.34 lukem case '@':
965 1.34 lukem setnetgrent(_pw_passwd.pw_name + 2);
966 1.34 lukem while(getnetgrent(&host, &user, &dom)) {
967 1.34 lukem if(user && *user)
968 1.34 lukem __pwexclude_add(user);
969 1.14 phil }
970 1.34 lukem endnetgrent();
971 1.14 phil break;
972 1.34 lukem default:
973 1.34 lukem __pwexclude_add(_pw_passwd.pw_name + 1);
974 1.32 lukem break;
975 1.4 deraadt }
976 1.34 lukem break;
977 1.34 lukem }
978 1.34 lukem if ((search == _PW_KEYBYNAME &&
979 1.34 lukem strcmp(_pw_passwd.pw_name, name) == 0)
980 1.34 lukem || (search == _PW_KEYBYUID && _pw_passwd.pw_uid == uid)) {
981 1.34 lukem rval = NS_SUCCESS;
982 1.34 lukem break;
983 1.4 deraadt }
984 1.34 lukem if(s == 1) /* inside netgroup */
985 1.34 lukem goto pwnam_netgrp;
986 1.34 lukem continue;
987 1.4 deraadt }
988 1.34 lukem __pwproto = (struct passwd *)NULL;
989 1.1 cgd
990 1.1 cgd if (!_pw_stayopen) {
991 1.1 cgd (void)(_pw_db->close)(_pw_db);
992 1.1 cgd _pw_db = (DB *)NULL;
993 1.1 cgd }
994 1.32 lukem if(__pwexclude != (DB *)NULL) {
995 1.32 lukem (void)(__pwexclude->close)(__pwexclude);
996 1.32 lukem __pwexclude = (DB *)NULL;
997 1.32 lukem }
998 1.32 lukem return rval;
999 1.40 lukem #endif /* _PASSWD_COMPAT */
1000 1.1 cgd }
1001 1.1 cgd
1002 1.1 cgd struct passwd *
1003 1.32 lukem getpwent()
1004 1.1 cgd {
1005 1.32 lukem int r;
1006 1.36 lukem static const ns_dtab dtab[] = {
1007 1.35 lukem NS_FILES_CB(_local_getpw, NULL)
1008 1.35 lukem NS_DNS_CB(_dns_getpw, NULL)
1009 1.35 lukem NS_NIS_CB(_nis_getpw, NULL)
1010 1.35 lukem NS_COMPAT_CB(_compat_getpwent, NULL)
1011 1.35 lukem { 0 }
1012 1.32 lukem };
1013 1.32 lukem
1014 1.32 lukem _pw_none = 0;
1015 1.36 lukem r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwent", compatsrc,
1016 1.35 lukem _PW_KEYBYNUM);
1017 1.32 lukem if (_pw_none || r != NS_SUCCESS)
1018 1.32 lukem return (struct passwd *)NULL;
1019 1.32 lukem return &_pw_passwd;
1020 1.32 lukem }
1021 1.10 deraadt
1022 1.32 lukem struct passwd *
1023 1.32 lukem getpwnam(name)
1024 1.32 lukem const char *name;
1025 1.32 lukem {
1026 1.32 lukem int r;
1027 1.36 lukem static const ns_dtab dtab[] = {
1028 1.35 lukem NS_FILES_CB(_local_getpw, NULL)
1029 1.35 lukem NS_DNS_CB(_dns_getpw, NULL)
1030 1.35 lukem NS_NIS_CB(_nis_getpw, NULL)
1031 1.35 lukem NS_COMPAT_CB(_compat_getpw, NULL)
1032 1.35 lukem { 0 }
1033 1.32 lukem };
1034 1.4 deraadt
1035 1.32 lukem if (name == NULL || name[0] == '\0')
1036 1.32 lukem return (struct passwd *)NULL;
1037 1.4 deraadt
1038 1.36 lukem r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwnam", compatsrc,
1039 1.35 lukem _PW_KEYBYNAME, name);
1040 1.32 lukem return (r == NS_SUCCESS ? &_pw_passwd : (struct passwd *)NULL);
1041 1.32 lukem }
1042 1.14 phil
1043 1.32 lukem struct passwd *
1044 1.32 lukem getpwuid(uid)
1045 1.32 lukem uid_t uid;
1046 1.32 lukem {
1047 1.32 lukem int r;
1048 1.36 lukem static const ns_dtab dtab[] = {
1049 1.35 lukem NS_FILES_CB(_local_getpw, NULL)
1050 1.35 lukem NS_DNS_CB(_dns_getpw, NULL)
1051 1.35 lukem NS_NIS_CB(_nis_getpw, NULL)
1052 1.35 lukem NS_COMPAT_CB(_compat_getpw, NULL)
1053 1.35 lukem { 0 }
1054 1.32 lukem };
1055 1.1 cgd
1056 1.36 lukem r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwuid", compatsrc,
1057 1.35 lukem _PW_KEYBYUID, uid);
1058 1.32 lukem return (r == NS_SUCCESS ? &_pw_passwd : (struct passwd *)NULL);
1059 1.1 cgd }
1060 1.1 cgd
1061 1.1 cgd int
1062 1.1 cgd setpassent(stayopen)
1063 1.1 cgd int stayopen;
1064 1.1 cgd {
1065 1.1 cgd _pw_keynum = 0;
1066 1.1 cgd _pw_stayopen = stayopen;
1067 1.9 jtc #ifdef YP
1068 1.32 lukem __pwmode = PWMODE_NONE;
1069 1.9 jtc if(__ypcurrent)
1070 1.9 jtc free(__ypcurrent);
1071 1.9 jtc __ypcurrent = NULL;
1072 1.32 lukem #endif
1073 1.32 lukem #ifdef HESIOD
1074 1.32 lukem _pw_hesnum = 0;
1075 1.32 lukem #endif
1076 1.40 lukem #ifdef _PASSWD_COMPAT
1077 1.32 lukem if(__pwexclude != (DB *)NULL) {
1078 1.32 lukem (void)(__pwexclude->close)(__pwexclude);
1079 1.32 lukem __pwexclude = (DB *)NULL;
1080 1.14 phil }
1081 1.32 lukem __pwproto = (struct passwd *)NULL;
1082 1.9 jtc #endif
1083 1.32 lukem return 1;
1084 1.1 cgd }
1085 1.1 cgd
1086 1.8 jtc void
1087 1.1 cgd setpwent()
1088 1.1 cgd {
1089 1.9 jtc (void) setpassent(0);
1090 1.1 cgd }
1091 1.1 cgd
1092 1.1 cgd void
1093 1.1 cgd endpwent()
1094 1.1 cgd {
1095 1.1 cgd _pw_keynum = 0;
1096 1.1 cgd if (_pw_db) {
1097 1.1 cgd (void)(_pw_db->close)(_pw_db);
1098 1.1 cgd _pw_db = (DB *)NULL;
1099 1.1 cgd }
1100 1.40 lukem #ifdef _PASSWD_COMPAT
1101 1.32 lukem __pwmode = PWMODE_NONE;
1102 1.32 lukem #endif
1103 1.4 deraadt #ifdef YP
1104 1.4 deraadt if(__ypcurrent)
1105 1.4 deraadt free(__ypcurrent);
1106 1.4 deraadt __ypcurrent = NULL;
1107 1.32 lukem #endif
1108 1.32 lukem #ifdef HESIOD
1109 1.32 lukem _pw_hesnum = 0;
1110 1.32 lukem #endif
1111 1.40 lukem #ifdef _PASSWD_COMPAT
1112 1.32 lukem if(__pwexclude != (DB *)NULL) {
1113 1.32 lukem (void)(__pwexclude->close)(__pwexclude);
1114 1.32 lukem __pwexclude = (DB *)NULL;
1115 1.14 phil }
1116 1.32 lukem __pwproto = (struct passwd *)NULL;
1117 1.4 deraadt #endif
1118 1.1 cgd }
1119 1.1 cgd
1120 1.4 deraadt static int
1121 1.1 cgd __initdb()
1122 1.1 cgd {
1123 1.1 cgd static int warned;
1124 1.1 cgd char *p;
1125 1.1 cgd
1126 1.40 lukem #ifdef _PASSWD_COMPAT
1127 1.32 lukem __pwmode = PWMODE_NONE;
1128 1.14 phil #endif
1129 1.25 mrg if (geteuid() == 0) {
1130 1.25 mrg _pw_db = dbopen((p = _PATH_SMP_DB), O_RDONLY, 0, DB_HASH, NULL);
1131 1.25 mrg if (_pw_db)
1132 1.25 mrg return(1);
1133 1.25 mrg }
1134 1.25 mrg _pw_db = dbopen((p = _PATH_MP_DB), O_RDONLY, 0, DB_HASH, NULL);
1135 1.1 cgd if (_pw_db)
1136 1.32 lukem return 1;
1137 1.1 cgd if (!warned)
1138 1.1 cgd syslog(LOG_ERR, "%s: %m", p);
1139 1.11 deraadt warned = 1;
1140 1.32 lukem return 0;
1141 1.1 cgd }
1142 1.1 cgd
1143 1.4 deraadt static int
1144 1.1 cgd __hashpw(key)
1145 1.1 cgd DBT *key;
1146 1.1 cgd {
1147 1.17 lukem char *p, *t;
1148 1.1 cgd static u_int max;
1149 1.30 christos static char *buf;
1150 1.1 cgd DBT data;
1151 1.1 cgd
1152 1.32 lukem switch ((_pw_db->get)(_pw_db, key, &data, 0)) {
1153 1.32 lukem case 0:
1154 1.32 lukem break; /* found */
1155 1.32 lukem case 1:
1156 1.32 lukem return NS_NOTFOUND;
1157 1.32 lukem case -1:
1158 1.32 lukem return NS_UNAVAIL; /* error in db routines */
1159 1.32 lukem default:
1160 1.32 lukem abort();
1161 1.32 lukem }
1162 1.32 lukem
1163 1.1 cgd p = (char *)data.data;
1164 1.30 christos if (data.size > max && !(buf = realloc(buf, (max += 1024))))
1165 1.32 lukem return NS_UNAVAIL;
1166 1.1 cgd
1167 1.24 perry /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
1168 1.30 christos t = buf;
1169 1.14 phil #define EXPAND(e) e = t; while ((*t++ = *p++));
1170 1.24 perry #define SCALAR(v) memmove(&(v), p, sizeof v); p += sizeof v
1171 1.1 cgd EXPAND(_pw_passwd.pw_name);
1172 1.1 cgd EXPAND(_pw_passwd.pw_passwd);
1173 1.24 perry SCALAR(_pw_passwd.pw_uid);
1174 1.24 perry SCALAR(_pw_passwd.pw_gid);
1175 1.24 perry SCALAR(_pw_passwd.pw_change);
1176 1.1 cgd EXPAND(_pw_passwd.pw_class);
1177 1.1 cgd EXPAND(_pw_passwd.pw_gecos);
1178 1.1 cgd EXPAND(_pw_passwd.pw_dir);
1179 1.1 cgd EXPAND(_pw_passwd.pw_shell);
1180 1.24 perry SCALAR(_pw_passwd.pw_expire);
1181 1.14 phil
1182 1.14 phil /* See if there's any data left. If so, read in flags. */
1183 1.14 phil if (data.size > (p - (char *)data.data)) {
1184 1.24 perry SCALAR(_pw_flags);
1185 1.14 phil } else
1186 1.14 phil _pw_flags = _PASSWORD_NOUID|_PASSWORD_NOGID; /* default */
1187 1.14 phil
1188 1.32 lukem return NS_SUCCESS;
1189 1.1 cgd }
1190