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