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