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