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