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