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