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