1 /* $NetBSD: getpwent.c,v 1.77 2010/03/23 20:28:59 drochner Exp $ */
4 * Copyright (c) 1997-2000, 2004-2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1988, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * Portions Copyright (c) 1994, 1995, Jason Downs. All rights reserved.
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
73 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
74 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
75 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
76 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
77 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
79 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
80 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86 #include <sys/cdefs.h>
87 #if defined(LIBC_SCCS) && !defined(lint)
89 static char sccsid
[] = "@(#)getpwent.c 8.2 (Berkeley) 4/27/95";
91 __RCSID("$NetBSD: getpwent.c,v 1.77 2010/03/23 20:28:59 drochner Exp $");
93 #endif /* LIBC_SCCS and not lint */
95 #include "namespace.h"
96 #include "reentrant.h"
98 #include <sys/param.h>
105 #include <netgroup.h>
106 #include <nsswitch.h>
120 #include <machine/param.h>
122 #include <rpcsvc/yp_prot.h>
123 #include <rpcsvc/ypclnt.h>
126 #include "pw_private.h"
128 #define _PASSWD_COMPAT /* "passwd" defaults to compat, so always provide it */
131 __weak_alias(endpwent
,_endpwent
)
132 __weak_alias(setpassent
,_setpassent
)
133 __weak_alias(setpwent
,_setpwent
)
137 static mutex_t _pwmutex
= MUTEX_INITIALIZER
;
140 const char __yp_token
[] = "__YP!"; /* Let pwd_mkdb pull this in. */
144 * The pwd.db lookup techniques and data extraction code here must be kept
145 * in sync with that in `pwd_mkdb'.
148 #if defined(YP) || defined(HESIOD)
151 * Parses entry using pw_scan(3) (without the trailing \n)
152 * after copying to buf, and fills in pw with corresponding values.
153 * If old is non-zero, entry is in _PASSWORD_OLDFMT.
154 * Returns 1 if parsed successfully, 0 on parse failure.
157 _pw_parse(const char *entry
, struct passwd
*pw
, char *buf
, size_t buflen
,
162 _DIAGASSERT(entry
!= NULL
);
163 _DIAGASSERT(pw
!= NULL
);
164 _DIAGASSERT(buf
!= NULL
);
166 if (strlcpy(buf
, entry
, buflen
) >= buflen
)
168 flags
= _PASSWORD_NOWARN
;
170 flags
|= _PASSWORD_OLDFMT
;
171 return __pw_scan(buf
, pw
, &flags
);
173 #endif /* YP || HESIOD */
177 * if *db is NULL, dbopen(3) /etc/spwd.db or /etc/pwd.db (depending
178 * upon permissions, etc)
181 _pw_opendb(DB
**db
, int *version
)
187 const char *dbfile
= NULL
;
189 _DIAGASSERT(db
!= NULL
);
190 _DIAGASSERT(version
!= NULL
);
191 if (*db
!= NULL
) /* open *db */
194 if (geteuid() == 0) {
195 dbfile
= _PATH_SMP_DB
;
196 *db
= dbopen(dbfile
, O_RDONLY
, 0, DB_HASH
, NULL
);
199 dbfile
= _PATH_MP_DB
;
200 *db
= dbopen(dbfile
, O_RDONLY
, 0, DB_HASH
, NULL
);
205 syslog(LOG_ERR
, "%s: %m", dbfile
);
211 key
.data
= __UNCONST("VERSION");
212 key
.size
= strlen((char *)key
.data
) + 1;
213 switch ((*(*db
)->get
)(*db
, &key
, &value
, 0)) {
215 if (sizeof(*version
) != value
.size
)
217 (void)memcpy(version
, value
.data
, value
.size
);
220 *version
= 0; /* not found */
223 return NS_UNAVAIL
; /* error in db routines */
232 * Lookup key in *db, filling in pw
233 * with the result, allocating memory from buffer (size buflen).
234 * (The caller may point key.data to buffer on entry; the contents
235 * of key.data will be invalid on exit.)
238 _pw_getkey(DB
*db
, DBT
*key
,
239 struct passwd
*pw
, char *buffer
, size_t buflen
, int *pwflags
,
245 _DIAGASSERT(db
!= NULL
);
246 _DIAGASSERT(key
!= NULL
);
247 _DIAGASSERT(pw
!= NULL
);
248 _DIAGASSERT(buffer
!= NULL
);
249 /* pwflags may be NULL (if we don't care about them */
251 if (db
== NULL
) /* this shouldn't happen */
254 switch ((db
->get
)(db
, key
, &data
, 0)) {
258 return NS_NOTFOUND
; /* not found */
260 return NS_UNAVAIL
; /* error in db routines */
265 p
= (char *)data
.data
;
266 if (data
.size
> buflen
) {
272 * THE DECODING BELOW MUST MATCH THAT IN pwd_mkdb.
275 #define MACRO(a) do { a } while (/*CONSTCOND*/0)
276 #define EXPAND(e) MACRO(e = t; while ((*t++ = *p++));)
277 #define SCALAR(v) MACRO(memmove(&(v), p, sizeof v); p += sizeof v;)
279 EXPAND(pw
->pw_passwd
);
287 SCALAR(pw
->pw_change
);
288 EXPAND(pw
->pw_class
);
289 EXPAND(pw
->pw_gecos
);
291 EXPAND(pw
->pw_shell
);
297 SCALAR(pw
->pw_expire
);
299 /* See if there's any data left. If so, read in flags. */
300 if (data
.size
> (size_t) (p
- (char *)data
.data
)) {
302 } else { /* default */
303 *pwflags
= _PASSWORD_NOUID
|_PASSWORD_NOGID
;
312 * Obtain want bytes from buffer (of size buflen) and return a pointer
313 * to the available memory after adjusting buffer/buflen.
314 * Returns NULL if there is insufficient space.
317 _pw_memfrombuf(size_t want
, char **buffer
, size_t *buflen
)
321 if (want
> *buflen
) {
333 * Copy the contents of frompw to pw; memory for strings
334 * and arrays will be allocated from buf (of size buflen).
335 * If proto != NULL, use various fields in proto in preference to frompw.
336 * Returns 1 if copied successfully, 0 on copy failure.
337 * NOTE: frompw must not use buf for its own pointers.
340 _pw_copy(const struct passwd
*frompw
, struct passwd
*pw
,
341 char *buf
, size_t buflen
, const struct passwd
*protopw
, int protoflags
)
346 _DIAGASSERT(frompw
!= NULL
);
347 _DIAGASSERT(pw
!= NULL
);
348 _DIAGASSERT(buf
!= NULL
);
349 /* protopw may be NULL */
351 useproto
= protopw
&& protopw
->pw_name
;
353 #define COPYSTR(to, from) \
355 count = strlen((from)); \
356 (to) = _pw_memfrombuf(count+1, &buf, &buflen); \
359 memmove((to), (from), count); \
361 } while (0) /* LINTED */
363 #define COPYFIELD(field) COPYSTR(pw->field, frompw->field)
365 #define COPYPROTOFIELD(field) COPYSTR(pw->field, \
366 (useproto && *protopw->field ? protopw->field : frompw->field))
370 #ifdef PW_OVERRIDE_PASSWD
371 COPYPROTOFIELD(pw_passwd
);
373 COPYFIELD(pw_passwd
);
376 if (useproto
&& !(protoflags
& _PASSWORD_NOUID
))
377 pw
->pw_uid
= protopw
->pw_uid
;
379 pw
->pw_uid
= frompw
->pw_uid
;
381 if (useproto
&& !(protoflags
& _PASSWORD_NOGID
))
382 pw
->pw_gid
= protopw
->pw_gid
;
384 pw
->pw_gid
= frompw
->pw_gid
;
386 pw
->pw_change
= frompw
->pw_change
;
388 COPYPROTOFIELD(pw_gecos
);
389 COPYPROTOFIELD(pw_dir
);
390 COPYPROTOFIELD(pw_shell
);
394 #undef COPYPROTOFIELD
404 /* state shared between files methods */
406 int stayopen
; /* see getpassent(3) */
407 DB
*db
; /* passwd file handle */
408 int keynum
; /* key counter, -1 if no more */
412 static struct files_state _files_state
;
413 /* storage for non _r functions */
414 static struct passwd _files_passwd
;
415 static char _files_passwdbuf
[_GETPW_R_SIZE_MAX
];
418 _files_start(struct files_state
*state
)
422 _DIAGASSERT(state
!= NULL
);
425 rv
= _pw_opendb(&state
->db
, &state
->version
);
426 if (rv
!= NS_SUCCESS
)
432 _files_end(struct files_state
*state
)
435 _DIAGASSERT(state
!= NULL
);
439 (void)(state
->db
->close
)(state
->db
);
447 * Search state->db for the next desired entry.
448 * If search is _PW_KEYBYNUM, look for state->keynum.
449 * If search is _PW_KEYBYNAME, look for name.
450 * If search is _PW_KEYBYUID, look for uid.
451 * Sets *retval to the errno if the result is not NS_SUCCESS
455 _files_pwscan(int *retval
, struct passwd
*pw
, char *buffer
, size_t buflen
,
456 struct files_state
*state
, int search
, const char *name
, uid_t uid
)
463 _DIAGASSERT(retval
!= NULL
);
464 _DIAGASSERT(pw
!= NULL
);
465 _DIAGASSERT(buffer
!= NULL
);
466 _DIAGASSERT(state
!= NULL
);
467 /* name is NULL to indicate searching for uid */
471 if (state
->db
== NULL
) { /* only start if file not open yet */
472 rv
= _files_start(state
);
473 if (rv
!= NS_SUCCESS
)
474 goto filespwscan_out
;
477 for (;;) { /* search for a match */
480 if (state
->keynum
== -1)
481 return NS_NOTFOUND
; /* no more records */
483 from
= &state
->keynum
;
484 fromlen
= sizeof(state
->keynum
);
488 fromlen
= strlen(name
);
492 fromlen
= sizeof(uid
);
498 if (buflen
<= fromlen
) { /* buffer too small */
502 buffer
[0] = search
; /* setup key */
503 memmove(buffer
+ 1, from
, fromlen
);
504 key
.size
= fromlen
+ 1;
505 key
.data
= (u_char
*)buffer
;
508 rv
= _pw_getkey(state
->db
, &key
, pw
, buffer
, buflen
, NULL
,
510 if (rv
!= NS_SUCCESS
) /* no match */
512 if (pw
->pw_name
[0] == '+' || pw
->pw_name
[0] == '-') {
513 /* if a compat line */
514 if (search
== _PW_KEYBYNUM
)
515 continue; /* read next if pwent */
516 rv
= NS_NOTFOUND
; /* don't match if pw{nam,uid} */
522 if (rv
== NS_NOTFOUND
&& search
== _PW_KEYBYNUM
)
523 state
->keynum
= -1; /* flag `no more records' */
525 if (rv
== NS_SUCCESS
) {
526 if ((search
== _PW_KEYBYUID
&& pw
->pw_uid
!= uid
) ||
527 (search
== _PW_KEYBYNAME
&& strcmp(pw
->pw_name
, name
) != 0))
532 if (rv
!= NS_SUCCESS
&& rv
!= NS_NOTFOUND
)
539 _files_setpwent(void *nsrv
, void *nscb
, va_list ap
)
542 _files_state
.stayopen
= 0;
543 return _files_start(&_files_state
);
548 _files_setpassent(void *nsrv
, void *nscb
, va_list ap
)
550 int *retval
= va_arg(ap
, int *);
551 int stayopen
= va_arg(ap
, int);
555 _files_state
.stayopen
= stayopen
;
556 rv
= _files_start(&_files_state
);
557 *retval
= (rv
== NS_SUCCESS
);
563 _files_endpwent(void *nsrv
, void *nscb
, va_list ap
)
566 _files_state
.stayopen
= 0;
567 return _files_end(&_files_state
);
572 _files_getpwent(void *nsrv
, void *nscb
, va_list ap
)
574 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
578 _DIAGASSERT(retval
!= NULL
);
581 rv
= _files_pwscan(&rerror
, &_files_passwd
,
582 _files_passwdbuf
, sizeof(_files_passwdbuf
),
583 &_files_state
, _PW_KEYBYNUM
, NULL
, 0);
584 if (rv
== NS_SUCCESS
)
585 *retval
= &_files_passwd
;
591 _files_getpwent_r(void *nsrv
, void *nscb
, va_list ap
)
593 int *retval
= va_arg(ap
, int *);
594 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
595 char *buffer
= va_arg(ap
, char *);
596 size_t buflen
= va_arg(ap
, size_t);
597 struct passwd
**result
= va_arg(ap
, struct passwd
**);
601 _DIAGASSERT(retval
!= NULL
);
602 _DIAGASSERT(pw
!= NULL
);
603 _DIAGASSERT(buffer
!= NULL
);
604 _DIAGASSERT(result
!= NULL
);
606 rv
= _files_pwscan(retval
, pw
, buffer
, buflen
, &_files_state
,
607 _PW_KEYBYNUM
, NULL
, 0);
608 if (rv
== NS_SUCCESS
)
617 _files_getpwnam(void *nsrv
, void *nscb
, va_list ap
)
619 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
620 const char *name
= va_arg(ap
, const char *);
624 _DIAGASSERT(retval
!= NULL
);
627 rv
= _files_start(&_files_state
);
628 if (rv
!= NS_SUCCESS
)
630 rv
= _files_pwscan(&rerror
, &_files_passwd
,
631 _files_passwdbuf
, sizeof(_files_passwdbuf
),
632 &_files_state
, _PW_KEYBYNAME
, name
, 0);
633 if (!_files_state
.stayopen
)
634 _files_end(&_files_state
);
635 if (rv
== NS_SUCCESS
)
636 *retval
= &_files_passwd
;
642 _files_getpwnam_r(void *nsrv
, void *nscb
, va_list ap
)
644 int *retval
= va_arg(ap
, int *);
645 const char *name
= va_arg(ap
, const char *);
646 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
647 char *buffer
= va_arg(ap
, char *);
648 size_t buflen
= va_arg(ap
, size_t);
649 struct passwd
**result
= va_arg(ap
, struct passwd
**);
651 struct files_state state
;
654 _DIAGASSERT(retval
!= NULL
);
655 _DIAGASSERT(pw
!= NULL
);
656 _DIAGASSERT(buffer
!= NULL
);
657 _DIAGASSERT(result
!= NULL
);
660 memset(&state
, 0, sizeof(state
));
661 rv
= _files_pwscan(retval
, pw
, buffer
, buflen
, &state
,
662 _PW_KEYBYNAME
, name
, 0);
664 if (rv
== NS_SUCCESS
)
671 _files_getpwuid(void *nsrv
, void *nscb
, va_list ap
)
673 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
674 uid_t uid
= va_arg(ap
, uid_t
);
678 _DIAGASSERT(retval
!= NULL
);
681 rv
= _files_start(&_files_state
);
682 if (rv
!= NS_SUCCESS
)
684 rv
= _files_pwscan(&rerror
, &_files_passwd
,
685 _files_passwdbuf
, sizeof(_files_passwdbuf
),
686 &_files_state
, _PW_KEYBYUID
, NULL
, uid
);
687 if (!_files_state
.stayopen
)
688 _files_end(&_files_state
);
689 if (rv
== NS_SUCCESS
)
690 *retval
= &_files_passwd
;
696 _files_getpwuid_r(void *nsrv
, void *nscb
, va_list ap
)
698 int *retval
= va_arg(ap
, int *);
699 uid_t uid
= va_arg(ap
, uid_t
);
700 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
701 char *buffer
= va_arg(ap
, char *);
702 size_t buflen
= va_arg(ap
, size_t);
703 struct passwd
**result
= va_arg(ap
, struct passwd
**);
705 struct files_state state
;
708 _DIAGASSERT(retval
!= NULL
);
709 _DIAGASSERT(pw
!= NULL
);
710 _DIAGASSERT(buffer
!= NULL
);
711 _DIAGASSERT(result
!= NULL
);
714 memset(&state
, 0, sizeof(state
));
715 rv
= _files_pwscan(retval
, pw
, buffer
, buflen
, &state
,
716 _PW_KEYBYUID
, NULL
, uid
);
718 if (rv
== NS_SUCCESS
)
729 /* state shared between dns methods */
731 int stayopen
; /* see getpassent(3) */
732 void *context
; /* Hesiod context */
733 int num
; /* passwd index, -1 if no more */
736 static struct dns_state _dns_state
;
737 /* storage for non _r functions */
738 static struct passwd _dns_passwd
;
739 static char _dns_passwdbuf
[_GETPW_R_SIZE_MAX
];
742 _dns_start(struct dns_state
*state
)
745 _DIAGASSERT(state
!= NULL
);
748 if (state
->context
== NULL
) { /* setup Hesiod */
749 if (hesiod_init(&state
->context
) == -1)
757 _dns_end(struct dns_state
*state
)
760 _DIAGASSERT(state
!= NULL
);
763 if (state
->context
) {
764 hesiod_end(state
->context
);
765 state
->context
= NULL
;
772 * Look for the Hesiod name provided in buffer in the NULL-terminated
774 * and decode into pw/buffer/buflen.
777 _dns_pwscan(int *retval
, struct passwd
*pw
, char *buffer
, size_t buflen
,
778 struct dns_state
*state
, const char **zones
)
780 const char **curzone
;
784 _DIAGASSERT(retval
!= NULL
);
785 _DIAGASSERT(pw
!= NULL
);
786 _DIAGASSERT(buffer
!= NULL
);
787 _DIAGASSERT(state
!= NULL
);
788 _DIAGASSERT(zones
!= NULL
);
792 if (state
->context
== NULL
) { /* only start if Hesiod not setup */
793 rv
= _dns_start(state
);
794 if (rv
!= NS_SUCCESS
)
801 for (curzone
= zones
; *curzone
; curzone
++) { /* search zones */
802 hp
= hesiod_resolve(state
->context
, buffer
, *curzone
);
805 if (errno
!= ENOENT
) {
810 if (*curzone
== NULL
)
813 if ((ep
= strchr(hp
[0], '\n')) != NULL
)
814 *ep
= '\0'; /* clear trailing \n */
815 if (_pw_parse(hp
[0], pw
, buffer
, buflen
, 1)) /* validate line */
821 if (rv
!= NS_SUCCESS
&& rv
!= NS_NOTFOUND
)
824 hesiod_free_list(state
->context
, hp
);
830 _dns_setpwent(void *nsrv
, void *nscb
, va_list ap
)
833 _dns_state
.stayopen
= 0;
834 return _dns_start(&_dns_state
);
839 _dns_setpassent(void *nsrv
, void *nscb
, va_list ap
)
841 int *retval
= va_arg(ap
, int *);
842 int stayopen
= va_arg(ap
, int);
846 _dns_state
.stayopen
= stayopen
;
847 rv
= _dns_start(&_dns_state
);
848 *retval
= (rv
== NS_SUCCESS
);
854 _dns_endpwent(void *nsrv
, void *nscb
, va_list ap
)
857 _dns_state
.stayopen
= 0;
858 return _dns_end(&_dns_state
);
863 _dns_getpwent(void *nsrv
, void *nscb
, va_list ap
)
865 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
870 _DIAGASSERT(retval
!= NULL
);
874 if (_dns_state
.num
== -1) /* exhausted search */
877 if (_dns_state
.context
== NULL
) {
878 /* only start if Hesiod not setup */
879 rv
= _dns_start(&_dns_state
);
880 if (rv
!= NS_SUCCESS
)
888 /* find passwd-NNN */
889 snprintf(_dns_passwdbuf
, sizeof(_dns_passwdbuf
),
890 "passwd-%u", _dns_state
.num
);
893 hp
= hesiod_resolve(_dns_state
.context
, _dns_passwdbuf
, "passwd");
900 if ((ep
= strchr(hp
[0], '\n')) != NULL
)
901 *ep
= '\0'; /* clear trailing \n */
903 if (_pw_parse(hp
[0], &_dns_passwd
,
904 _dns_passwdbuf
, sizeof(_dns_passwdbuf
), 1))
906 else { /* dodgy entry, try again */
907 hesiod_free_list(_dns_state
.context
, hp
);
913 hesiod_free_list(_dns_state
.context
, hp
);
914 if (rv
== NS_SUCCESS
)
915 *retval
= &_dns_passwd
;
921 _dns_getpwent_r(void *nsrv
, void *nscb
, va_list ap
)
923 int *retval
= va_arg(ap
, int *);
924 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
925 char *buffer
= va_arg(ap
, char *);
926 size_t buflen
= va_arg(ap
, size_t);
927 struct passwd
**result
= va_arg(ap
, struct passwd
**);
932 _DIAGASSERT(retval
!= NULL
);
933 _DIAGASSERT(pw
!= NULL
);
934 _DIAGASSERT(buffer
!= NULL
);
935 _DIAGASSERT(result
!= NULL
);
939 if (_dns_state
.num
== -1) /* exhausted search */
942 if (_dns_state
.context
== NULL
) {
943 /* only start if Hesiod not setup */
944 rv
= _dns_start(&_dns_state
);
945 if (rv
!= NS_SUCCESS
)
953 /* find passwd-NNN */
954 snprintf(buffer
, buflen
, "passwd-%u", _dns_state
.num
);
957 hp
= hesiod_resolve(_dns_state
.context
, buffer
, "passwd");
964 if ((ep
= strchr(hp
[0], '\n')) != NULL
)
965 *ep
= '\0'; /* clear trailing \n */
967 if (_pw_parse(hp
[0], pw
, buffer
, buflen
, 1))
969 else { /* dodgy entry, try again */
970 hesiod_free_list(_dns_state
.context
, hp
);
976 hesiod_free_list(_dns_state
.context
, hp
);
977 if (rv
== NS_SUCCESS
)
984 static const char *_dns_uid_zones
[] = {
992 _dns_getpwuid(void *nsrv
, void *nscb
, va_list ap
)
994 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
995 uid_t uid
= va_arg(ap
, uid_t
);
999 _DIAGASSERT(retval
!= NULL
);
1002 rv
= _dns_start(&_dns_state
);
1003 if (rv
!= NS_SUCCESS
)
1005 snprintf(_dns_passwdbuf
, sizeof(_dns_passwdbuf
),
1006 "%u", (unsigned int)uid
);
1007 rv
= _dns_pwscan(&rerror
, &_dns_passwd
,
1008 _dns_passwdbuf
, sizeof(_dns_passwdbuf
),
1009 &_dns_state
, _dns_uid_zones
);
1010 if (!_dns_state
.stayopen
)
1011 _dns_end(&_dns_state
);
1012 if (rv
== NS_SUCCESS
&& uid
== _dns_passwd
.pw_uid
)
1013 *retval
= &_dns_passwd
;
1019 _dns_getpwuid_r(void *nsrv
, void *nscb
, va_list ap
)
1021 int *retval
= va_arg(ap
, int *);
1022 uid_t uid
= va_arg(ap
, uid_t
);
1023 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
1024 char *buffer
= va_arg(ap
, char *);
1025 size_t buflen
= va_arg(ap
, size_t);
1026 struct passwd
**result
= va_arg(ap
, struct passwd
**);
1028 struct dns_state state
;
1031 _DIAGASSERT(retval
!= NULL
);
1032 _DIAGASSERT(pw
!= NULL
);
1033 _DIAGASSERT(buffer
!= NULL
);
1034 _DIAGASSERT(result
!= NULL
);
1037 memset(&state
, 0, sizeof(state
));
1038 snprintf(buffer
, buflen
, "%u", (unsigned int)uid
);
1039 rv
= _dns_pwscan(retval
, pw
, buffer
, buflen
, &state
, _dns_uid_zones
);
1041 if (rv
!= NS_SUCCESS
)
1043 if (uid
== pw
->pw_uid
) {
1050 static const char *_dns_nam_zones
[] = {
1057 _dns_getpwnam(void *nsrv
, void *nscb
, va_list ap
)
1059 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
1060 const char *name
= va_arg(ap
, const char *);
1064 _DIAGASSERT(retval
!= NULL
);
1067 rv
= _dns_start(&_dns_state
);
1068 if (rv
!= NS_SUCCESS
)
1070 snprintf(_dns_passwdbuf
, sizeof(_dns_passwdbuf
), "%s", name
);
1071 rv
= _dns_pwscan(&rerror
, &_dns_passwd
,
1072 _dns_passwdbuf
, sizeof(_dns_passwdbuf
),
1073 &_dns_state
, _dns_nam_zones
);
1074 if (!_dns_state
.stayopen
)
1075 _dns_end(&_dns_state
);
1076 if (rv
== NS_SUCCESS
&& strcmp(name
, _dns_passwd
.pw_name
) == 0)
1077 *retval
= &_dns_passwd
;
1083 _dns_getpwnam_r(void *nsrv
, void *nscb
, va_list ap
)
1085 int *retval
= va_arg(ap
, int *);
1086 const char *name
= va_arg(ap
, const char *);
1087 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
1088 char *buffer
= va_arg(ap
, char *);
1089 size_t buflen
= va_arg(ap
, size_t);
1090 struct passwd
**result
= va_arg(ap
, struct passwd
**);
1092 struct dns_state state
;
1095 _DIAGASSERT(retval
!= NULL
);
1096 _DIAGASSERT(pw
!= NULL
);
1097 _DIAGASSERT(buffer
!= NULL
);
1098 _DIAGASSERT(result
!= NULL
);
1101 memset(&state
, 0, sizeof(state
));
1102 snprintf(buffer
, buflen
, "%s", name
);
1103 rv
= _dns_pwscan(retval
, pw
, buffer
, buflen
, &state
, _dns_nam_zones
);
1105 if (rv
!= NS_SUCCESS
)
1107 if (strcmp(name
, pw
->pw_name
) == 0) {
1121 /* state shared between nis methods */
1123 int stayopen
; /* see getpassent(3) */
1124 char *domain
; /* NIS domain */
1125 int done
; /* non-zero if search exhausted */
1126 char *current
; /* current first/next match */
1127 int currentlen
; /* length of _nis_current */
1128 enum { /* shadow map type */
1129 NISMAP_UNKNOWN
, /* unknown ... */
1130 NISMAP_NONE
, /* none: use "passwd.by*" */
1131 NISMAP_ADJUNCT
, /* pw_passwd from "passwd.adjunct.*" */
1132 NISMAP_MASTER
/* all from "master.passwd.by*" */
1136 static struct nis_state _nis_state
;
1137 /* storage for non _r functions */
1138 static struct passwd _nis_passwd
;
1139 static char _nis_passwdbuf
[_GETPW_R_SIZE_MAX
];
1141 /* macros for deciding which NIS maps to use. */
1142 #define PASSWD_BYNAME(x) ((x)->maptype == NISMAP_MASTER \
1143 ? "master.passwd.byname" : "passwd.byname")
1144 #define PASSWD_BYUID(x) ((x)->maptype == NISMAP_MASTER \
1145 ? "master.passwd.byuid" : "passwd.byuid")
1148 _nis_start(struct nis_state
*state
)
1151 _DIAGASSERT(state
!= NULL
);
1154 if (state
->current
) {
1155 free(state
->current
);
1156 state
->current
= NULL
;
1158 if (state
->domain
== NULL
) { /* setup NIS */
1159 switch (yp_get_default_domain(&state
->domain
)) {
1169 /* determine where to get pw_passwd from */
1170 if (state
->maptype
== NISMAP_UNKNOWN
) {
1173 state
->maptype
= NISMAP_NONE
; /* default to no adjunct */
1174 if (geteuid() != 0) /* non-root can't use adjunct */
1177 /* look for "master.passwd.*" */
1178 r
= yp_order(state
->domain
, "master.passwd.byname", &order
);
1180 state
->maptype
= NISMAP_MASTER
;
1184 /* master.passwd doesn't exist, try passwd.adjunct */
1185 if (r
== YPERR_MAP
) {
1186 r
= yp_order(state
->domain
, "passwd.adjunct.byname",
1189 state
->maptype
= NISMAP_ADJUNCT
;
1196 _nis_end(struct nis_state
*state
)
1199 _DIAGASSERT(state
!= NULL
);
1202 state
->domain
= NULL
;
1205 free(state
->current
);
1206 state
->current
= NULL
;
1207 state
->maptype
= NISMAP_UNKNOWN
;
1213 * wrapper to _pw_parse that obtains the real password from the
1214 * "passwd.adjunct.byname" NIS map if the maptype is NISMAP_ADJUNCT.
1217 _nis_parse(const char *entry
, struct passwd
*pw
, char *buf
, size_t buflen
,
1218 struct nis_state
*state
)
1222 _DIAGASSERT(entry
!= NULL
);
1223 _DIAGASSERT(pw
!= NULL
);
1224 _DIAGASSERT(buf
!= NULL
);
1225 _DIAGASSERT(state
!= NULL
);
1227 elen
= strlen(entry
);
1230 if (! _pw_parse(entry
, pw
, buf
, buflen
,
1231 !(state
->maptype
== NISMAP_MASTER
)))
1234 if ((state
->maptype
== NISMAP_ADJUNCT
) &&
1235 (strstr(pw
->pw_passwd
, "##") != NULL
)) {
1239 if (yp_match(state
->domain
, "passwd.adjunct.byname",
1240 pw
->pw_name
, (int)strlen(pw
->pw_name
),
1241 &data
, &datalen
) == 0) {
1243 /* skip name to get password */
1245 if ((bp
= strsep(&ep
, ":")) != NULL
&&
1246 (bp
= strsep(&ep
, ":")) != NULL
) {
1247 /* store new pw_passwd after entry */
1248 strlcpy(buf
+ elen
, bp
, buflen
- elen
);
1249 pw
->pw_passwd
= &buf
[elen
];
1261 * Look for the yp key provided in buffer from map,
1262 * and decode into pw/buffer/buflen.
1265 _nis_pwscan(int *retval
, struct passwd
*pw
, char *buffer
, size_t buflen
,
1266 struct nis_state
*state
, const char *map
)
1269 int nisr
, rv
, datalen
;
1271 _DIAGASSERT(retval
!= NULL
);
1272 _DIAGASSERT(pw
!= NULL
);
1273 _DIAGASSERT(buffer
!= NULL
);
1274 _DIAGASSERT(state
!= NULL
);
1275 _DIAGASSERT(map
!= NULL
);
1279 if (state
->domain
== NULL
) { /* only start if NIS not setup */
1280 rv
= _nis_start(state
);
1281 if (rv
!= NS_SUCCESS
)
1289 nisr
= yp_match(state
->domain
, map
, buffer
, (int)strlen(buffer
),
1293 data
[datalen
] = '\0'; /* clear trailing \n */
1294 if (_nis_parse(data
, pw
, buffer
, buflen
, state
))
1295 rv
= NS_SUCCESS
; /* validate line */
1306 if (rv
!= NS_SUCCESS
&& rv
!= NS_NOTFOUND
)
1315 _nis_setpwent(void *nsrv
, void *nscb
, va_list ap
)
1318 _nis_state
.stayopen
= 0;
1319 return _nis_start(&_nis_state
);
1324 _nis_setpassent(void *nsrv
, void *nscb
, va_list ap
)
1326 int *retval
= va_arg(ap
, int *);
1327 int stayopen
= va_arg(ap
, int);
1331 _nis_state
.stayopen
= stayopen
;
1332 rv
= _nis_start(&_nis_state
);
1333 *retval
= (rv
== NS_SUCCESS
);
1339 _nis_endpwent(void *nsrv
, void *nscb
, va_list ap
)
1342 return _nis_end(&_nis_state
);
1348 _nis_getpwent(void *nsrv
, void *nscb
, va_list ap
)
1350 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
1353 int keylen
, datalen
, rv
, nisr
;
1355 _DIAGASSERT(retval
!= NULL
);
1359 if (_nis_state
.done
) /* exhausted search */
1361 if (_nis_state
.domain
== NULL
) {
1362 /* only start if NIS not setup */
1363 rv
= _nis_start(&_nis_state
);
1364 if (rv
!= NS_SUCCESS
)
1373 if (_nis_state
.current
) { /* already searching */
1374 nisr
= yp_next(_nis_state
.domain
, PASSWD_BYNAME(&_nis_state
),
1375 _nis_state
.current
, _nis_state
.currentlen
,
1376 &key
, &keylen
, &data
, &datalen
);
1377 free(_nis_state
.current
);
1378 _nis_state
.current
= NULL
;
1381 _nis_state
.current
= key
;
1382 _nis_state
.currentlen
= keylen
;
1386 _nis_state
.done
= 1;
1392 } else { /* new search */
1393 if (yp_first(_nis_state
.domain
, PASSWD_BYNAME(&_nis_state
),
1394 &_nis_state
.current
, &_nis_state
.currentlen
,
1401 data
[datalen
] = '\0'; /* clear trailing \n */
1403 if (_nis_parse(data
, &_nis_passwd
,
1404 _nis_passwdbuf
, sizeof(_nis_passwdbuf
), &_nis_state
))
1406 else { /* dodgy entry, try again */
1408 goto next_nis_entry
;
1416 if (rv
== NS_SUCCESS
)
1417 *retval
= &_nis_passwd
;
1423 _nis_getpwent_r(void *nsrv
, void *nscb
, va_list ap
)
1425 int *retval
= va_arg(ap
, int *);
1426 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
1427 char *buffer
= va_arg(ap
, char *);
1428 size_t buflen
= va_arg(ap
, size_t);
1429 struct passwd
**result
= va_arg(ap
, struct passwd
**);
1432 int keylen
, datalen
, rv
, nisr
;
1434 _DIAGASSERT(retval
!= NULL
);
1435 _DIAGASSERT(pw
!= NULL
);
1436 _DIAGASSERT(buffer
!= NULL
);
1437 _DIAGASSERT(result
!= NULL
);
1441 if (_nis_state
.done
) /* exhausted search */
1443 if (_nis_state
.domain
== NULL
) {
1444 /* only start if NIS not setup */
1445 rv
= _nis_start(&_nis_state
);
1446 if (rv
!= NS_SUCCESS
)
1455 if (_nis_state
.current
) { /* already searching */
1456 nisr
= yp_next(_nis_state
.domain
, PASSWD_BYNAME(&_nis_state
),
1457 _nis_state
.current
, _nis_state
.currentlen
,
1458 &key
, &keylen
, &data
, &datalen
);
1459 free(_nis_state
.current
);
1460 _nis_state
.current
= NULL
;
1463 _nis_state
.current
= key
;
1464 _nis_state
.currentlen
= keylen
;
1468 _nis_state
.done
= 1;
1474 } else { /* new search */
1475 if (yp_first(_nis_state
.domain
, PASSWD_BYNAME(&_nis_state
),
1476 &_nis_state
.current
, &_nis_state
.currentlen
,
1483 data
[datalen
] = '\0'; /* clear trailing \n */
1485 if (_nis_parse(data
, pw
, buffer
, buflen
, &_nis_state
))
1487 else { /* dodgy entry, try again */
1491 goto next_nis_entry
;
1499 if (rv
== NS_SUCCESS
)
1508 _nis_getpwuid(void *nsrv
, void *nscb
, va_list ap
)
1510 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
1511 uid_t uid
= va_arg(ap
, uid_t
);
1515 _DIAGASSERT(retval
!= NULL
);
1518 rv
= _nis_start(&_nis_state
);
1519 if (rv
!= NS_SUCCESS
)
1521 snprintf(_nis_passwdbuf
, sizeof(_nis_passwdbuf
), "%u", (unsigned int)uid
);
1522 rv
= _nis_pwscan(&rerror
, &_nis_passwd
,
1523 _nis_passwdbuf
, sizeof(_nis_passwdbuf
),
1524 &_nis_state
, PASSWD_BYUID(&_nis_state
));
1525 if (!_nis_state
.stayopen
)
1526 _nis_end(&_nis_state
);
1527 if (rv
== NS_SUCCESS
&& uid
== _nis_passwd
.pw_uid
)
1528 *retval
= &_nis_passwd
;
1534 _nis_getpwuid_r(void *nsrv
, void *nscb
, va_list ap
)
1536 int *retval
= va_arg(ap
, int *);
1537 uid_t uid
= va_arg(ap
, uid_t
);
1538 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
1539 char *buffer
= va_arg(ap
, char *);
1540 size_t buflen
= va_arg(ap
, size_t);
1541 struct passwd
**result
= va_arg(ap
, struct passwd
**);
1543 struct nis_state state
;
1546 _DIAGASSERT(retval
!= NULL
);
1547 _DIAGASSERT(pw
!= NULL
);
1548 _DIAGASSERT(buffer
!= NULL
);
1549 _DIAGASSERT(result
!= NULL
);
1552 memset(&state
, 0, sizeof(state
));
1553 rv
= _nis_start(&state
);
1554 if (rv
!= NS_SUCCESS
)
1556 snprintf(buffer
, buflen
, "%u", (unsigned int)uid
);
1557 rv
= _nis_pwscan(retval
, pw
, buffer
, buflen
,
1558 &state
, PASSWD_BYUID(&state
));
1560 if (rv
!= NS_SUCCESS
)
1562 if (uid
== pw
->pw_uid
) {
1571 _nis_getpwnam(void *nsrv
, void *nscb
, va_list ap
)
1573 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
1574 const char *name
= va_arg(ap
, const char *);
1578 _DIAGASSERT(retval
!= NULL
);
1581 rv
= _nis_start(&_nis_state
);
1582 if (rv
!= NS_SUCCESS
)
1584 snprintf(_nis_passwdbuf
, sizeof(_nis_passwdbuf
), "%s", name
);
1585 rv
= _nis_pwscan(&rerror
, &_nis_passwd
,
1586 _nis_passwdbuf
, sizeof(_nis_passwdbuf
),
1587 &_nis_state
, PASSWD_BYNAME(&_nis_state
));
1588 if (!_nis_state
.stayopen
)
1589 _nis_end(&_nis_state
);
1590 if (rv
== NS_SUCCESS
&& strcmp(name
, _nis_passwd
.pw_name
) == 0)
1591 *retval
= &_nis_passwd
;
1597 _nis_getpwnam_r(void *nsrv
, void *nscb
, va_list ap
)
1599 int *retval
= va_arg(ap
, int *);
1600 const char *name
= va_arg(ap
, const char *);
1601 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
1602 char *buffer
= va_arg(ap
, char *);
1603 size_t buflen
= va_arg(ap
, size_t);
1604 struct passwd
**result
= va_arg(ap
, struct passwd
**);
1606 struct nis_state state
;
1609 _DIAGASSERT(retval
!= NULL
);
1610 _DIAGASSERT(pw
!= NULL
);
1611 _DIAGASSERT(buffer
!= NULL
);
1612 _DIAGASSERT(result
!= NULL
);
1615 snprintf(buffer
, buflen
, "%s", name
);
1616 memset(&state
, 0, sizeof(state
));
1617 rv
= _nis_start(&state
);
1618 if (rv
!= NS_SUCCESS
)
1620 rv
= _nis_pwscan(retval
, pw
, buffer
, buflen
,
1621 &state
, PASSWD_BYNAME(&state
));
1623 if (rv
!= NS_SUCCESS
)
1625 if (strcmp(name
, pw
->pw_name
) == 0) {
1635 #ifdef _PASSWD_COMPAT
1640 /* state shared between compat methods */
1642 struct compat_state
{
1643 int stayopen
; /* see getpassent(3) */
1644 DB
*db
; /* passwd DB */
1645 int keynum
; /* key counter, -1 if no more */
1646 enum { /* current compat mode */
1647 COMPAT_NOTOKEN
= 0, /* no compat token present */
1648 COMPAT_NONE
, /* parsing normal pwd.db line */
1649 COMPAT_FULL
, /* parsing `+' entries */
1650 COMPAT_USER
, /* parsing `+name' entries */
1651 COMPAT_NETGROUP
/* parsing `+@netgroup' entries */
1653 char *user
; /* COMPAT_USER "+name" */
1654 DB
*exclude
; /* compat exclude DB */
1655 struct passwd proto
; /* proto passwd entry */
1656 char protobuf
[_GETPW_R_SIZE_MAX
];
1657 /* buffer for proto ptrs */
1658 int protoflags
; /* proto passwd flags */
1662 static struct compat_state _compat_state
;
1663 /* storage for non _r functions */
1664 static struct passwd _compat_passwd
;
1665 static char _compat_passwdbuf
[_GETPW_R_SIZE_MAX
];
1668 _compat_start(struct compat_state
*state
)
1672 _DIAGASSERT(state
!= NULL
);
1675 if (state
->db
== NULL
) { /* not open yet */
1678 char bf
[MAXLOGNAME
];
1680 rv
= _pw_opendb(&state
->db
, &state
->version
);
1681 if (rv
!= NS_SUCCESS
)
1684 state
->mode
= COMPAT_NOTOKEN
;
1687 * Determine if the "compat" token is present in pwd.db;
1688 * either "__YP!" or PW_KEYBYNAME+"+".
1689 * Only works if pwd_mkdb installs the token.
1691 key
.data
= (u_char
*)__UNCONST(__yp_token
);
1692 key
.size
= strlen(__yp_token
);
1694 bf
[0] = _PW_KEYBYNAME
; /* Pre-token database support. */
1696 pkey
.data
= (u_char
*)bf
;
1699 if ((state
->db
->get
)(state
->db
, &key
, &data
, 0) == 0
1700 || (state
->db
->get
)(state
->db
, &pkey
, &pdata
, 0) == 0)
1701 state
->mode
= COMPAT_NONE
;
1707 _compat_end(struct compat_state
*state
)
1710 _DIAGASSERT(state
!= NULL
);
1714 (void)(state
->db
->close
)(state
->db
);
1717 state
->mode
= COMPAT_NOTOKEN
;
1721 if (state
->exclude
!= NULL
)
1722 (void)(state
->exclude
->close
)(state
->exclude
);
1723 state
->exclude
= NULL
;
1724 state
->proto
.pw_name
= NULL
;
1725 state
->protoflags
= 0;
1730 * _compat_add_exclude
1731 * add the name to the exclude list in state->exclude.
1734 _compat_add_exclude(struct compat_state
*state
, const char *name
)
1738 _DIAGASSERT(state
!= NULL
);
1739 _DIAGASSERT(name
!= NULL
);
1741 /* initialize the exclusion table if needed */
1742 if (state
->exclude
== NULL
) {
1743 state
->exclude
= dbopen(NULL
, O_RDWR
, 600, DB_HASH
, NULL
);
1744 if (state
->exclude
== NULL
)
1748 key
.size
= strlen(name
); /* set up the key */
1749 key
.data
= (u_char
*)__UNCONST(name
);
1751 data
.data
= NULL
; /* data is nothing */
1755 if ((state
->exclude
->put
)(state
->exclude
, &key
, &data
, 0) == -1)
1762 * _compat_is_excluded
1763 * test if a name is on the compat mode exclude list
1766 _compat_is_excluded(struct compat_state
*state
, const char *name
)
1770 _DIAGASSERT(state
!= NULL
);
1771 _DIAGASSERT(name
!= NULL
);
1773 if (state
->exclude
== NULL
)
1774 return 0; /* nothing excluded */
1776 key
.size
= strlen(name
); /* set up the key */
1777 key
.data
= (u_char
*)__UNCONST(name
);
1779 if ((state
->exclude
->get
)(state
->exclude
, &key
, &data
, 0) == 0)
1780 return 1; /* is excluded */
1788 * log an error if "files" or "compat" is specified in
1789 * passwd_compat database
1793 _passwdcompat_bad(void *nsrv
, void *nscb
, va_list ap
)
1797 _DIAGASSERT(nsrv
!= NULL
);
1798 _DIAGASSERT(nscb
!= NULL
);
1802 "nsswitch.conf passwd_compat database can't use '%s'",
1810 * _passwdcompat_setpassent
1811 * Call setpassent for all passwd_compat sources.
1814 _passwdcompat_setpassent(int stayopen
)
1816 static const ns_dtab dtab
[] = {
1817 NS_FILES_CB(_passwdcompat_bad
, "files")
1818 NS_DNS_CB(_dns_setpassent
, NULL
)
1819 NS_NIS_CB(_nis_setpassent
, NULL
)
1820 NS_COMPAT_CB(_passwdcompat_bad
, "compat")
1826 rv
= nsdispatch(NULL
, dtab
, NSDB_PASSWD_COMPAT
, "setpassent",
1827 __nsdefaultnis_forceall
, &result
, stayopen
);
1832 * _passwdcompat_endpwent
1833 * Call endpwent for all passwd_compat sources.
1836 _passwdcompat_endpwent(void)
1838 static const ns_dtab dtab
[] = {
1839 NS_FILES_CB(_passwdcompat_bad
, "files")
1840 NS_DNS_CB(_dns_endpwent
, NULL
)
1841 NS_NIS_CB(_nis_endpwent
, NULL
)
1842 NS_COMPAT_CB(_passwdcompat_bad
, "compat")
1846 return nsdispatch(NULL
, dtab
, NSDB_PASSWD_COMPAT
, "endpwent",
1847 __nsdefaultnis_forceall
);
1851 * _passwdcompat_pwscan
1852 * When a name lookup in compat mode is required (e.g., `+name', or a
1853 * name in `+@netgroup'), look it up in the 'passwd_compat' nsswitch
1855 * Fail if passwd_compat contains files or compat.
1858 _passwdcompat_pwscan(struct passwd
*pw
, char *buffer
, size_t buflen
,
1859 int search
, const char *name
, uid_t uid
)
1861 static const ns_dtab compatentdtab
[] = {
1862 NS_FILES_CB(_passwdcompat_bad
, "files")
1863 NS_DNS_CB(_dns_getpwent_r
, NULL
)
1864 NS_NIS_CB(_nis_getpwent_r
, NULL
)
1865 NS_COMPAT_CB(_passwdcompat_bad
, "compat")
1868 static const ns_dtab compatuiddtab
[] = {
1869 NS_FILES_CB(_passwdcompat_bad
, "files")
1870 NS_DNS_CB(_dns_getpwuid_r
, NULL
)
1871 NS_NIS_CB(_nis_getpwuid_r
, NULL
)
1872 NS_COMPAT_CB(_passwdcompat_bad
, "compat")
1875 static const ns_dtab compatnamdtab
[] = {
1876 NS_FILES_CB(_passwdcompat_bad
, "files")
1877 NS_DNS_CB(_dns_getpwnam_r
, NULL
)
1878 NS_NIS_CB(_nis_getpwnam_r
, NULL
)
1879 NS_COMPAT_CB(_passwdcompat_bad
, "compat")
1888 rv
= nsdispatch(NULL
, compatentdtab
,
1889 NSDB_PASSWD_COMPAT
, "getpwent_r", __nsdefaultnis
,
1890 &crv
, pw
, buffer
, buflen
, &cpw
);
1893 _DIAGASSERT(name
!= NULL
);
1894 rv
= nsdispatch(NULL
, compatnamdtab
,
1895 NSDB_PASSWD_COMPAT
, "getpwnam_r", __nsdefaultnis
,
1896 &crv
, name
, pw
, buffer
, buflen
, &cpw
);
1899 rv
= nsdispatch(NULL
, compatuiddtab
,
1900 NSDB_PASSWD_COMPAT
, "getpwuid_r", __nsdefaultnis
,
1901 &crv
, uid
, pw
, buffer
, buflen
, &cpw
);
1912 * Search state->db for the next desired entry.
1913 * If search is _PW_KEYBYNUM, look for state->keynum.
1914 * If search is _PW_KEYBYNAME, look for name.
1915 * If search is _PW_KEYBYUID, look for uid.
1916 * Sets *retval to the errno if the result is not NS_SUCCESS
1920 _compat_pwscan(int *retval
, struct passwd
*pw
, char *buffer
, size_t buflen
,
1921 struct compat_state
*state
, int search
, const char *name
, uid_t uid
)
1925 const char *user
, *host
, *dom
;
1929 _DIAGASSERT(retval
!= NULL
);
1930 _DIAGASSERT(pw
!= NULL
);
1931 _DIAGASSERT(buffer
!= NULL
);
1932 _DIAGASSERT(state
!= NULL
);
1933 /* name may be NULL */
1937 if (state
->db
== NULL
) {
1938 rv
= _compat_start(state
);
1939 if (rv
!= NS_SUCCESS
)
1942 if (buflen
<= 1) { /* buffer too small */
1947 for (;;) { /* loop over pwd.db */
1949 if (state
->mode
!= COMPAT_NOTOKEN
&&
1950 state
->mode
!= COMPAT_NONE
) {
1951 /* doing a compat lookup */
1953 char cbuf
[_GETPW_R_SIZE_MAX
];
1955 switch (state
->mode
) {
1958 /* get next user or lookup by key */
1959 rv
= _passwdcompat_pwscan(&cpw
,
1960 cbuf
, sizeof(cbuf
), search
, name
, uid
);
1961 if (rv
!= NS_SUCCESS
)
1962 state
->mode
= COMPAT_NONE
;
1965 case COMPAT_NETGROUP
:
1966 /* XXXREENTRANT: getnetgrent is not thread safe */
1967 /* get next user from netgroup */
1968 r
= getnetgrent(&host
, &user
, &dom
);
1969 if (r
== 0) { /* end of group */
1971 state
->mode
= COMPAT_NONE
;
1974 if (!user
|| !*user
)
1976 rv
= _passwdcompat_pwscan(&cpw
,
1978 _PW_KEYBYNAME
, user
, 0);
1982 /* get specific user */
1983 if (state
->user
== NULL
) {
1984 state
->mode
= COMPAT_NONE
;
1987 rv
= _passwdcompat_pwscan(&cpw
,
1989 _PW_KEYBYNAME
, state
->user
, 0);
1992 state
->mode
= COMPAT_NONE
;
1995 case COMPAT_NOTOKEN
:
2000 if (rv
!= NS_SUCCESS
) /* if not matched, next loop */
2003 /* copy cpw to pw, applying prototype */
2004 if (! _pw_copy(&cpw
, pw
, buffer
, buflen
,
2005 &state
->proto
, state
->protoflags
)) {
2010 if (_compat_is_excluded(state
, pw
->pw_name
))
2011 continue; /* excluded; next loop */
2013 if ((search
== _PW_KEYBYNAME
2014 && strcmp(pw
->pw_name
, name
) != 0)
2015 || (search
== _PW_KEYBYUID
&& pw
->pw_uid
!= uid
)) {
2016 continue; /* not specific; next loop */
2019 break; /* exit loop if found */
2020 } else { /* not a compat line */
2021 state
->proto
.pw_name
= NULL
;
2022 /* clear prototype */
2025 if (state
->mode
== COMPAT_NOTOKEN
) {
2026 /* no compat token; do direct lookup */
2029 if (state
->keynum
== -1) /* no more records */
2032 from
= &state
->keynum
;
2033 fromlen
= sizeof(state
->keynum
);
2037 fromlen
= strlen(name
);
2041 fromlen
= sizeof(uid
);
2048 /* compat token; do line by line */
2049 if (state
->keynum
== -1) /* no more records */
2052 from
= &state
->keynum
;
2053 fromlen
= sizeof(state
->keynum
);
2054 buffer
[0] = _PW_KEYBYNUM
;
2057 if (buflen
<= fromlen
) { /* buffer too small */
2061 memmove(buffer
+ 1, from
, fromlen
); /* setup key */
2062 key
.size
= fromlen
+ 1;
2063 key
.data
= (u_char
*)buffer
;
2065 rv
= _pw_getkey(state
->db
, &key
, pw
, buffer
, buflen
, &pwflags
,
2067 if (rv
!= NS_SUCCESS
) /* stop on error */
2070 if (state
->mode
== COMPAT_NOTOKEN
)
2071 break; /* stop if no compat token */
2073 if (pw
->pw_name
[0] == '+') {
2074 /* compat inclusion */
2075 switch(pw
->pw_name
[1]) {
2076 case '\0': /* `+' */
2077 state
->mode
= COMPAT_FULL
;
2078 /* reset passwd_compat search */
2079 /* XXXREENTRANT: setpassent is not thread safe ? */
2080 (void) _passwdcompat_setpassent(0);
2082 case '@': /* `+@netgroup' */
2083 state
->mode
= COMPAT_NETGROUP
;
2084 /* reset netgroup search */
2085 /* XXXREENTRANT: setnetgrent is not thread safe */
2086 setnetgrent(pw
->pw_name
+ 2);
2088 default: /* `+name' */
2089 state
->mode
= COMPAT_USER
;
2092 state
->user
= strdup(pw
->pw_name
+ 1);
2095 /* save the prototype */
2096 state
->protoflags
= pwflags
;
2097 if (! _pw_copy(pw
, &state
->proto
, state
->protobuf
,
2098 sizeof(state
->protobuf
), NULL
, 0)) {
2102 continue; /* loop again after inclusion */
2103 } else if (pw
->pw_name
[0] == '-') {
2104 /* compat exclusion */
2106 switch(pw
->pw_name
[1]) {
2107 case '\0': /* `-' */
2109 case '@': /* `-@netgroup' */
2110 /* XXXREENTRANT: {set,get,end}netgrent is not thread safe */
2111 setnetgrent(pw
->pw_name
+ 2);
2112 while (getnetgrent(&host
, &user
, &dom
)) {
2113 if (!user
|| !*user
)
2115 if (! _compat_add_exclude(state
,user
)) {
2122 default: /* `-name' */
2123 if (! _compat_add_exclude(state
,
2129 if (rv
!= NS_SUCCESS
) /* exclusion failure */
2131 continue; /* loop again after exclusion */
2133 if (search
== _PW_KEYBYNUM
||
2134 (search
== _PW_KEYBYUID
&& pw
->pw_uid
== uid
) ||
2135 (search
== _PW_KEYBYNAME
&& strcmp(pw
->pw_name
, name
) == 0))
2136 break; /* token mode match found */
2139 if (rv
== NS_NOTFOUND
&&
2140 (search
== _PW_KEYBYNUM
|| state
->mode
!= COMPAT_NOTOKEN
))
2141 state
->keynum
= -1; /* flag `no more records' */
2143 if (rv
== NS_SUCCESS
) {
2144 if ((search
== _PW_KEYBYNAME
&& strcmp(pw
->pw_name
, name
) != 0)
2145 || (search
== _PW_KEYBYUID
&& pw
->pw_uid
!= uid
))
2149 if (rv
!= NS_SUCCESS
&& rv
!= NS_NOTFOUND
)
2156 _compat_setpwent(void *nsrv
, void *nscb
, va_list ap
)
2159 /* force passwd_compat setpwent() */
2160 (void) _passwdcompat_setpassent(0);
2162 /* reset state, keep db open */
2163 _compat_state
.stayopen
= 0;
2164 return _compat_start(&_compat_state
);
2169 _compat_setpassent(void *nsrv
, void *nscb
, va_list ap
)
2171 int *retval
= va_arg(ap
, int *);
2172 int stayopen
= va_arg(ap
, int);
2176 /* force passwd_compat setpassent() */
2177 (void) _passwdcompat_setpassent(stayopen
);
2179 _compat_state
.stayopen
= stayopen
;
2180 rv
= _compat_start(&_compat_state
);
2181 *retval
= (rv
== NS_SUCCESS
);
2187 _compat_endpwent(void *nsrv
, void *nscb
, va_list ap
)
2190 /* force passwd_compat endpwent() */
2191 (void) _passwdcompat_endpwent();
2193 /* reset state, close db */
2194 _compat_state
.stayopen
= 0;
2195 return _compat_end(&_compat_state
);
2201 _compat_getpwent(void *nsrv
, void *nscb
, va_list ap
)
2203 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
2207 _DIAGASSERT(retval
!= NULL
);
2210 rv
= _compat_pwscan(&rerror
, &_compat_passwd
,
2211 _compat_passwdbuf
, sizeof(_compat_passwdbuf
),
2212 &_compat_state
, _PW_KEYBYNUM
, NULL
, 0);
2213 if (rv
== NS_SUCCESS
)
2214 *retval
= &_compat_passwd
;
2220 _compat_getpwent_r(void *nsrv
, void *nscb
, va_list ap
)
2222 int *retval
= va_arg(ap
, int *);
2223 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
2224 char *buffer
= va_arg(ap
, char *);
2225 size_t buflen
= va_arg(ap
, size_t);
2226 struct passwd
**result
= va_arg(ap
, struct passwd
**);
2230 _DIAGASSERT(retval
!= NULL
);
2231 _DIAGASSERT(pw
!= NULL
);
2232 _DIAGASSERT(buffer
!= NULL
);
2233 _DIAGASSERT(result
!= NULL
);
2235 rv
= _compat_pwscan(retval
, pw
, buffer
, buflen
, &_compat_state
,
2236 _PW_KEYBYNUM
, NULL
, 0);
2237 if (rv
== NS_SUCCESS
)
2247 _compat_getpwnam(void *nsrv
, void *nscb
, va_list ap
)
2249 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
2250 const char *name
= va_arg(ap
, const char *);
2254 _DIAGASSERT(retval
!= NULL
);
2257 rv
= _compat_start(&_compat_state
);
2258 if (rv
!= NS_SUCCESS
)
2260 rv
= _compat_pwscan(&rerror
, &_compat_passwd
,
2261 _compat_passwdbuf
, sizeof(_compat_passwdbuf
),
2262 &_compat_state
, _PW_KEYBYNAME
, name
, 0);
2263 if (!_compat_state
.stayopen
)
2264 _compat_end(&_compat_state
);
2265 if (rv
== NS_SUCCESS
)
2266 *retval
= &_compat_passwd
;
2272 _compat_getpwnam_r(void *nsrv
, void *nscb
, va_list ap
)
2274 int *retval
= va_arg(ap
, int *);
2275 const char *name
= va_arg(ap
, const char *);
2276 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
2277 char *buffer
= va_arg(ap
, char *);
2278 size_t buflen
= va_arg(ap
, size_t);
2279 struct passwd
**result
= va_arg(ap
, struct passwd
**);
2281 struct compat_state state
;
2284 _DIAGASSERT(retval
!= NULL
);
2285 _DIAGASSERT(pw
!= NULL
);
2286 _DIAGASSERT(buffer
!= NULL
);
2287 _DIAGASSERT(result
!= NULL
);
2290 memset(&state
, 0, sizeof(state
));
2291 rv
= _compat_pwscan(retval
, pw
, buffer
, buflen
, &state
,
2292 _PW_KEYBYNAME
, name
, 0);
2293 _compat_end(&state
);
2294 if (rv
== NS_SUCCESS
)
2301 _compat_getpwuid(void *nsrv
, void *nscb
, va_list ap
)
2303 struct passwd
**retval
= va_arg(ap
, struct passwd
**);
2304 uid_t uid
= va_arg(ap
, uid_t
);
2308 _DIAGASSERT(retval
!= NULL
);
2311 rv
= _compat_start(&_compat_state
);
2312 if (rv
!= NS_SUCCESS
)
2314 rv
= _compat_pwscan(&rerror
, &_compat_passwd
,
2315 _compat_passwdbuf
, sizeof(_compat_passwdbuf
),
2316 &_compat_state
, _PW_KEYBYUID
, NULL
, uid
);
2317 if (!_compat_state
.stayopen
)
2318 _compat_end(&_compat_state
);
2319 if (rv
== NS_SUCCESS
)
2320 *retval
= &_compat_passwd
;
2326 _compat_getpwuid_r(void *nsrv
, void *nscb
, va_list ap
)
2328 int *retval
= va_arg(ap
, int *);
2329 uid_t uid
= va_arg(ap
, uid_t
);
2330 struct passwd
*pw
= va_arg(ap
, struct passwd
*);
2331 char *buffer
= va_arg(ap
, char *);
2332 size_t buflen
= va_arg(ap
, size_t);
2333 struct passwd
**result
= va_arg(ap
, struct passwd
**);
2335 struct compat_state state
;
2338 _DIAGASSERT(retval
!= NULL
);
2339 _DIAGASSERT(pw
!= NULL
);
2340 _DIAGASSERT(buffer
!= NULL
);
2341 _DIAGASSERT(result
!= NULL
);
2344 memset(&state
, 0, sizeof(state
));
2345 rv
= _compat_pwscan(retval
, pw
, buffer
, buflen
, &state
,
2346 _PW_KEYBYUID
, NULL
, uid
);
2347 _compat_end(&state
);
2348 if (rv
== NS_SUCCESS
)
2353 #endif /* _PASSWD_COMPAT */
2364 struct passwd
*retval
;
2366 static const ns_dtab dtab
[] = {
2367 NS_FILES_CB(_files_getpwent
, NULL
)
2368 NS_DNS_CB(_dns_getpwent
, NULL
)
2369 NS_NIS_CB(_nis_getpwent
, NULL
)
2370 NS_COMPAT_CB(_compat_getpwent
, NULL
)
2374 mutex_lock(&_pwmutex
);
2375 r
= nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "getpwent", __nsdefaultcompat
,
2377 mutex_unlock(&_pwmutex
);
2378 return (r
== NS_SUCCESS
) ? retval
: NULL
;
2382 getpwent_r(struct passwd
*pwd
, char *buffer
, size_t buflen
,
2383 struct passwd
**result
)
2387 static const ns_dtab dtab
[] = {
2388 NS_FILES_CB(_files_getpwent_r
, NULL
)
2389 NS_DNS_CB(_dns_getpwent_r
, NULL
)
2390 NS_NIS_CB(_nis_getpwent_r
, NULL
)
2391 NS_COMPAT_CB(_compat_getpwent_r
, NULL
)
2395 _DIAGASSERT(pwd
!= NULL
);
2396 _DIAGASSERT(buffer
!= NULL
);
2397 _DIAGASSERT(result
!= NULL
);
2401 mutex_lock(&_pwmutex
);
2402 r
= nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "getpwent_r", __nsdefaultcompat
,
2403 &retval
, pwd
, buffer
, buflen
, result
);
2404 mutex_unlock(&_pwmutex
);
2416 getpwnam(const char *name
)
2419 struct passwd
*retval
;
2421 static const ns_dtab dtab
[] = {
2422 NS_FILES_CB(_files_getpwnam
, NULL
)
2423 NS_DNS_CB(_dns_getpwnam
, NULL
)
2424 NS_NIS_CB(_nis_getpwnam
, NULL
)
2425 NS_COMPAT_CB(_compat_getpwnam
, NULL
)
2429 mutex_lock(&_pwmutex
);
2430 rv
= nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "getpwnam", __nsdefaultcompat
,
2432 mutex_unlock(&_pwmutex
);
2433 return (rv
== NS_SUCCESS
) ? retval
: NULL
;
2437 getpwnam_r(const char *name
, struct passwd
*pwd
, char *buffer
, size_t buflen
,
2438 struct passwd
**result
)
2442 static const ns_dtab dtab
[] = {
2443 NS_FILES_CB(_files_getpwnam_r
, NULL
)
2444 NS_DNS_CB(_dns_getpwnam_r
, NULL
)
2445 NS_NIS_CB(_nis_getpwnam_r
, NULL
)
2446 NS_COMPAT_CB(_compat_getpwnam_r
, NULL
)
2450 _DIAGASSERT(name
!= NULL
);
2451 _DIAGASSERT(pwd
!= NULL
);
2452 _DIAGASSERT(buffer
!= NULL
);
2453 _DIAGASSERT(result
!= NULL
);
2457 mutex_lock(&_pwmutex
);
2458 r
= nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "getpwnam_r", __nsdefaultcompat
,
2459 &retval
, name
, pwd
, buffer
, buflen
, result
);
2460 mutex_unlock(&_pwmutex
);
2474 struct passwd
*retval
;
2476 static const ns_dtab dtab
[] = {
2477 NS_FILES_CB(_files_getpwuid
, NULL
)
2478 NS_DNS_CB(_dns_getpwuid
, NULL
)
2479 NS_NIS_CB(_nis_getpwuid
, NULL
)
2480 NS_COMPAT_CB(_compat_getpwuid
, NULL
)
2484 mutex_lock(&_pwmutex
);
2485 rv
= nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "getpwuid", __nsdefaultcompat
,
2487 mutex_unlock(&_pwmutex
);
2488 return (rv
== NS_SUCCESS
) ? retval
: NULL
;
2492 getpwuid_r(uid_t uid
, struct passwd
*pwd
, char *buffer
, size_t buflen
,
2493 struct passwd
**result
)
2497 static const ns_dtab dtab
[] = {
2498 NS_FILES_CB(_files_getpwuid_r
, NULL
)
2499 NS_DNS_CB(_dns_getpwuid_r
, NULL
)
2500 NS_NIS_CB(_nis_getpwuid_r
, NULL
)
2501 NS_COMPAT_CB(_compat_getpwuid_r
, NULL
)
2505 _DIAGASSERT(pwd
!= NULL
);
2506 _DIAGASSERT(buffer
!= NULL
);
2507 _DIAGASSERT(result
!= NULL
);
2511 mutex_lock(&_pwmutex
);
2512 r
= nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "getpwuid_r", __nsdefaultcompat
,
2513 &retval
, uid
, pwd
, buffer
, buflen
, result
);
2515 mutex_unlock(&_pwmutex
);
2528 static const ns_dtab dtab
[] = {
2529 NS_FILES_CB(_files_endpwent
, NULL
)
2530 NS_DNS_CB(_dns_endpwent
, NULL
)
2531 NS_NIS_CB(_nis_endpwent
, NULL
)
2532 NS_COMPAT_CB(_compat_endpwent
, NULL
)
2536 mutex_lock(&_pwmutex
);
2537 /* force all endpwent() methods */
2538 (void) nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "endpwent",
2539 __nsdefaultcompat_forceall
);
2540 mutex_unlock(&_pwmutex
);
2545 setpassent(int stayopen
)
2547 static const ns_dtab dtab
[] = {
2548 NS_FILES_CB(_files_setpassent
, NULL
)
2549 NS_DNS_CB(_dns_setpassent
, NULL
)
2550 NS_NIS_CB(_nis_setpassent
, NULL
)
2551 NS_COMPAT_CB(_compat_setpassent
, NULL
)
2556 mutex_lock(&_pwmutex
);
2557 /* force all setpassent() methods */
2558 rv
= nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "setpassent",
2559 __nsdefaultcompat_forceall
, &retval
, stayopen
);
2560 mutex_unlock(&_pwmutex
);
2561 return (rv
== NS_SUCCESS
) ? retval
: 0;
2567 static const ns_dtab dtab
[] = {
2568 NS_FILES_CB(_files_setpwent
, NULL
)
2569 NS_DNS_CB(_dns_setpwent
, NULL
)
2570 NS_NIS_CB(_nis_setpwent
, NULL
)
2571 NS_COMPAT_CB(_compat_setpwent
, NULL
)
2575 mutex_lock(&_pwmutex
);
2576 /* force all setpwent() methods */
2577 (void) nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "setpwent",
2578 __nsdefaultcompat_forceall
);
2579 mutex_unlock(&_pwmutex
);