2 * Copyright (c) 2003 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by
6 * Jacques A. Vidrine, Safeport Network Services, and Network
7 * Associates Laboratories, the Security Research Division of Network
8 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
9 * ("CBOSS"), as part of the DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/lib/libc/gen/getpwent.c,v 1.90 2006/04/28 12:03:35 ume Exp $
33 * $DragonFly: src/lib/libc/gen/getpwent.c,v 1.7 2005/11/19 22:32:53 swildner Exp $
36 #include "namespace.h"
37 #include <sys/param.h>
40 #include <rpcsvc/yp_prot.h>
41 #include <rpcsvc/ypclnt.h>
43 #include <arpa/inet.h>
52 #include <pthread_np.h>
59 #include "un-namespace.h"
61 #include "libc_private.h"
69 #define CTASSERT(x) _CTASSERT(x, __LINE__)
70 #define _CTASSERT(x, y) __CTASSERT(x, y)
71 #define __CTASSERT(x, y) typedef char __assert_ ## y [(x) ? 1 : -1]
74 /* Counter as stored in /etc/pwd.db */
77 CTASSERT(MAXLOGNAME
> sizeof(uid_t
));
78 CTASSERT(MAXLOGNAME
> sizeof(pwkeynum
));
81 PWD_STORAGE_INITIAL
= 1 << 10, /* 1 KByte */
82 PWD_STORAGE_MAX
= 1 << 20, /* 1 MByte */
88 static const ns_src defaultsrc
[] = {
89 { NSSRC_COMPAT
, NS_SUCCESS
},
93 int __pw_match_entry(const char *, size_t, enum nss_lookup_type
,
95 int __pw_parse_entry(char *, size_t, struct passwd
*, int, int *errnop
);
97 static void pwd_init(struct passwd
*);
104 static struct passwd
*getpw(int (*fn
)(union key
, struct passwd
*, char *,
105 size_t, struct passwd
**), union key
);
106 static int wrap_getpwnam_r(union key
, struct passwd
*, char *,
107 size_t, struct passwd
**);
108 static int wrap_getpwuid_r(union key
, struct passwd
*, char *, size_t,
110 static int wrap_getpwent_r(union key
, struct passwd
*, char *, size_t,
113 static int pwdb_match_entry_v3(char *, size_t, enum nss_lookup_type
,
114 const char *, uid_t
);
115 static int pwdb_parse_entry_v3(char *, size_t, struct passwd
*, int *);
116 static int pwdb_match_entry_v4(char *, size_t, enum nss_lookup_type
,
117 const char *, uid_t
);
118 static int pwdb_parse_entry_v4(char *, size_t, struct passwd
*, int *);
122 int (*match
)(char *, size_t, enum nss_lookup_type
, const char *,
124 int (*parse
)(char *, size_t, struct passwd
*, int *);
125 } pwdb_versions
[] = {
126 { NULL
, NULL
}, /* version 0 */
127 { NULL
, NULL
}, /* version 1 */
128 { NULL
, NULL
}, /* version 2 */
129 { pwdb_match_entry_v3
, pwdb_parse_entry_v3
}, /* version 3 */
130 { pwdb_match_entry_v4
, pwdb_parse_entry_v4
}, /* version 4 */
140 static void files_endstate(void *);
141 NSS_TLS_HANDLING(files
);
142 static DB
*pwdbopen(int *);
143 static void files_endstate(void *);
144 static int files_setpwent(void *, void *, va_list);
145 static int files_passwd(void *, void *, va_list);
152 static void dns_endstate(void *);
153 NSS_TLS_HANDLING(dns
);
154 static int dns_setpwent(void *, void *, va_list);
155 static int dns_passwd(void *, void *, va_list);
161 char domain
[MAXHOSTNAMELEN
];
166 static void nis_endstate(void *);
167 NSS_TLS_HANDLING(nis
);
168 static int nis_setpwent(void *, void *, va_list);
169 static int nis_passwd(void *, void *, va_list);
170 static int nis_map(char *, enum nss_lookup_type
, char *, size_t, int *);
171 static int nis_adjunct(char *, const char *, char *, size_t);
175 struct compat_state
{
181 struct passwd
template;
190 static void compat_endstate(void *);
191 NSS_TLS_HANDLING(compat
);
192 static int compat_setpwent(void *, void *, va_list);
193 static int compat_passwd(void *, void *, va_list);
194 static void compat_clear_template(struct passwd
*);
195 static int compat_set_template(struct passwd
*, struct passwd
*);
196 static int compat_use_template(struct passwd
*, struct passwd
*, char *,
198 static int compat_redispatch(struct compat_state
*, enum nss_lookup_type
,
199 enum nss_lookup_type
, const char *, const char *, uid_t
,
200 struct passwd
*, char *, size_t, int *);
203 static int pwd_id_func(char *, size_t *, va_list ap
, void *);
204 static int pwd_marshal_func(char *, size_t *, void *, va_list, void *);
205 static int pwd_unmarshal_func(char *, size_t, void *, va_list, void *);
208 pwd_id_func(char *buffer
, size_t *buffer_size
, va_list ap
, void *cache_mdata
)
212 size_t size
, desired_size
;
213 int res
= NS_UNAVAIL
;
214 enum nss_lookup_type lookup_type
;
216 lookup_type
= (enum nss_lookup_type
)cache_mdata
;
217 switch (lookup_type
) {
219 name
= va_arg(ap
, char *);
221 desired_size
= sizeof(enum nss_lookup_type
) + size
+ 1;
222 if (desired_size
> *buffer_size
) {
227 memcpy(buffer
, &lookup_type
, sizeof(enum nss_lookup_type
));
228 memcpy(buffer
+ sizeof(enum nss_lookup_type
), name
, size
+ 1);
233 uid
= va_arg(ap
, uid_t
);
234 desired_size
= sizeof(enum nss_lookup_type
) + sizeof(uid_t
);
235 if (desired_size
> *buffer_size
) {
240 memcpy(buffer
, &lookup_type
, sizeof(enum nss_lookup_type
));
241 memcpy(buffer
+ sizeof(enum nss_lookup_type
), &uid
,
247 /* should be unreachable */
252 *buffer_size
= desired_size
;
257 pwd_marshal_func(char *buffer
, size_t *buffer_size
, void *retval
, va_list ap
,
264 size_t orig_buf_size
;
266 struct passwd new_pwd
;
267 size_t desired_size
, size
;
270 switch ((enum nss_lookup_type
)cache_mdata
) {
272 name
= va_arg(ap
, char *);
275 uid
= va_arg(ap
, uid_t
);
280 /* should be unreachable */
284 pwd
= va_arg(ap
, struct passwd
*);
285 orig_buf
= va_arg(ap
, char *);
286 orig_buf_size
= va_arg(ap
, size_t);
288 desired_size
= sizeof(struct passwd
) + sizeof(char *) +
289 strlen(pwd
->pw_name
) + 1;
290 if (pwd
->pw_passwd
!= NULL
)
291 desired_size
+= strlen(pwd
->pw_passwd
) + 1;
292 if (pwd
->pw_class
!= NULL
)
293 desired_size
+= strlen(pwd
->pw_class
) + 1;
294 if (pwd
->pw_gecos
!= NULL
)
295 desired_size
+= strlen(pwd
->pw_gecos
) + 1;
296 if (pwd
->pw_dir
!= NULL
)
297 desired_size
+= strlen(pwd
->pw_dir
) + 1;
298 if (pwd
->pw_shell
!= NULL
)
299 desired_size
+= strlen(pwd
->pw_shell
) + 1;
301 if (*buffer_size
< desired_size
) {
302 /* this assignment is here for future use */
303 *buffer_size
= desired_size
;
307 memcpy(&new_pwd
, pwd
, sizeof(struct passwd
));
308 memset(buffer
, 0, desired_size
);
310 *buffer_size
= desired_size
;
311 p
= buffer
+ sizeof(struct passwd
) + sizeof(char *);
312 memcpy(buffer
+ sizeof(struct passwd
), &p
, sizeof(char *));
314 if (new_pwd
.pw_name
!= NULL
) {
315 size
= strlen(new_pwd
.pw_name
);
316 memcpy(p
, new_pwd
.pw_name
, size
);
321 if (new_pwd
.pw_passwd
!= NULL
) {
322 size
= strlen(new_pwd
.pw_passwd
);
323 memcpy(p
, new_pwd
.pw_passwd
, size
);
324 new_pwd
.pw_passwd
= p
;
328 if (new_pwd
.pw_class
!= NULL
) {
329 size
= strlen(new_pwd
.pw_class
);
330 memcpy(p
, new_pwd
.pw_class
, size
);
331 new_pwd
.pw_class
= p
;
335 if (new_pwd
.pw_gecos
!= NULL
) {
336 size
= strlen(new_pwd
.pw_gecos
);
337 memcpy(p
, new_pwd
.pw_gecos
, size
);
338 new_pwd
.pw_gecos
= p
;
342 if (new_pwd
.pw_dir
!= NULL
) {
343 size
= strlen(new_pwd
.pw_dir
);
344 memcpy(p
, new_pwd
.pw_dir
, size
);
349 if (new_pwd
.pw_shell
!= NULL
) {
350 size
= strlen(new_pwd
.pw_shell
);
351 memcpy(p
, new_pwd
.pw_shell
, size
);
352 new_pwd
.pw_shell
= p
;
356 memcpy(buffer
, &new_pwd
, sizeof(struct passwd
));
361 pwd_unmarshal_func(char *buffer
, size_t buffer_size
, void *retval
, va_list ap
,
368 size_t orig_buf_size
;
373 switch ((enum nss_lookup_type
)cache_mdata
) {
375 name
= va_arg(ap
, char *);
378 uid
= va_arg(ap
, uid_t
);
383 /* should be unreachable */
387 pwd
= va_arg(ap
, struct passwd
*);
388 orig_buf
= va_arg(ap
, char *);
389 orig_buf_size
= va_arg(ap
, size_t);
390 ret_errno
= va_arg(ap
, int *);
393 buffer_size
- sizeof(struct passwd
) - sizeof(char *)) {
398 memcpy(pwd
, buffer
, sizeof(struct passwd
));
399 memcpy(&p
, buffer
+ sizeof(struct passwd
), sizeof(char *));
400 memcpy(orig_buf
, buffer
+ sizeof(struct passwd
) + sizeof(char *),
401 buffer_size
- sizeof(struct passwd
) - sizeof(char *));
403 NS_APPLY_OFFSET(pwd
->pw_name
, orig_buf
, p
, char *);
404 NS_APPLY_OFFSET(pwd
->pw_passwd
, orig_buf
, p
, char *);
405 NS_APPLY_OFFSET(pwd
->pw_class
, orig_buf
, p
, char *);
406 NS_APPLY_OFFSET(pwd
->pw_gecos
, orig_buf
, p
, char *);
407 NS_APPLY_OFFSET(pwd
->pw_dir
, orig_buf
, p
, char *);
408 NS_APPLY_OFFSET(pwd
->pw_shell
, orig_buf
, p
, char *);
411 *((struct passwd
**)retval
) = pwd
;
416 NSS_MP_CACHE_HANDLING(passwd
);
417 #endif /* NS_CACHING */
423 static const nss_cache_info cache_info
= NS_MP_CACHE_INFO_INITIALIZER(
424 passwd
, (void *)nss_lt_all
,
428 static const ns_dtab dtab
[] = {
429 { NSSRC_FILES
, files_setpwent
, (void *)SETPWENT
},
431 { NSSRC_DNS
, dns_setpwent
, (void *)SETPWENT
},
434 { NSSRC_NIS
, nis_setpwent
, (void *)SETPWENT
},
436 { NSSRC_COMPAT
, compat_setpwent
, (void *)SETPWENT
},
438 NS_CACHE_CB(&cache_info
)
442 _nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "setpwent", defaultsrc
, 0);
447 setpassent(int stayopen
)
450 static const nss_cache_info cache_info
= NS_MP_CACHE_INFO_INITIALIZER(
451 passwd
, (void *)nss_lt_all
,
455 static const ns_dtab dtab
[] = {
456 { NSSRC_FILES
, files_setpwent
, (void *)SETPWENT
},
458 { NSSRC_DNS
, dns_setpwent
, (void *)SETPWENT
},
461 { NSSRC_NIS
, nis_setpwent
, (void *)SETPWENT
},
463 { NSSRC_COMPAT
, compat_setpwent
, (void *)SETPWENT
},
465 NS_CACHE_CB(&cache_info
)
469 _nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "setpwent", defaultsrc
, stayopen
);
478 static const nss_cache_info cache_info
= NS_MP_CACHE_INFO_INITIALIZER(
479 passwd
, (void *)nss_lt_all
,
483 static const ns_dtab dtab
[] = {
484 { NSSRC_FILES
, files_setpwent
, (void *)ENDPWENT
},
486 { NSSRC_DNS
, dns_setpwent
, (void *)ENDPWENT
},
489 { NSSRC_NIS
, nis_setpwent
, (void *)ENDPWENT
},
491 { NSSRC_COMPAT
, compat_setpwent
, (void *)ENDPWENT
},
493 NS_CACHE_CB(&cache_info
)
497 _nsdispatch(NULL
, dtab
, NSDB_PASSWD
, "endpwent", defaultsrc
);
502 getpwent_r(struct passwd
*pwd
, char *buffer
, size_t bufsize
,
503 struct passwd
**result
)
506 static const nss_cache_info cache_info
= NS_MP_CACHE_INFO_INITIALIZER(
507 passwd
, (void *)nss_lt_all
,
508 pwd_marshal_func
, pwd_unmarshal_func
);
511 static const ns_dtab dtab
[] = {
512 { NSSRC_FILES
, files_passwd
, (void *)nss_lt_all
},
514 { NSSRC_DNS
, dns_passwd
, (void *)nss_lt_all
},
517 { NSSRC_NIS
, nis_passwd
, (void *)nss_lt_all
},
519 { NSSRC_COMPAT
, compat_passwd
, (void *)nss_lt_all
},
521 NS_CACHE_CB(&cache_info
)
530 rv
= _nsdispatch(result
, dtab
, NSDB_PASSWD
, "getpwent_r", defaultsrc
,
531 pwd
, buffer
, bufsize
, &ret_errno
);
532 if (rv
== NS_SUCCESS
)
540 getpwnam_r(const char *name
, struct passwd
*pwd
, char *buffer
, size_t bufsize
,
541 struct passwd
**result
)
544 static const nss_cache_info cache_info
=
545 NS_COMMON_CACHE_INFO_INITIALIZER(
546 passwd
, (void *)nss_lt_name
,
547 pwd_id_func
, pwd_marshal_func
, pwd_unmarshal_func
);
550 static const ns_dtab dtab
[] = {
551 { NSSRC_FILES
, files_passwd
, (void *)nss_lt_name
},
553 { NSSRC_DNS
, dns_passwd
, (void *)nss_lt_name
},
556 { NSSRC_NIS
, nis_passwd
, (void *)nss_lt_name
},
558 { NSSRC_COMPAT
, compat_passwd
, (void *)nss_lt_name
},
560 NS_CACHE_CB(&cache_info
)
569 rv
= _nsdispatch(result
, dtab
, NSDB_PASSWD
, "getpwnam_r", defaultsrc
,
570 name
, pwd
, buffer
, bufsize
, &ret_errno
);
571 if (rv
== NS_SUCCESS
)
579 getpwuid_r(uid_t uid
, struct passwd
*pwd
, char *buffer
, size_t bufsize
,
580 struct passwd
**result
)
583 static const nss_cache_info cache_info
=
584 NS_COMMON_CACHE_INFO_INITIALIZER(
585 passwd
, (void *)nss_lt_id
,
586 pwd_id_func
, pwd_marshal_func
, pwd_unmarshal_func
);
589 static const ns_dtab dtab
[] = {
590 { NSSRC_FILES
, files_passwd
, (void *)nss_lt_id
},
592 { NSSRC_DNS
, dns_passwd
, (void *)nss_lt_id
},
595 { NSSRC_NIS
, nis_passwd
, (void *)nss_lt_id
},
597 { NSSRC_COMPAT
, compat_passwd
, (void *)nss_lt_id
},
599 NS_CACHE_CB(&cache_info
)
608 rv
= _nsdispatch(result
, dtab
, NSDB_PASSWD
, "getpwuid_r", defaultsrc
,
609 uid
, pwd
, buffer
, bufsize
, &ret_errno
);
610 if (rv
== NS_SUCCESS
)
618 pwd_init(struct passwd
*pwd
)
620 static char nul
[] = "";
622 memset(pwd
, 0, sizeof(*pwd
));
623 pwd
->pw_uid
= (uid_t
)-1; /* Considered least likely to lead to */
624 pwd
->pw_gid
= (gid_t
)-1; /* a security issue. */
626 pwd
->pw_passwd
= nul
;
634 static struct passwd pwd
;
635 static char *pwd_storage
;
636 static size_t pwd_storage_size
;
639 static struct passwd
*
640 getpw(int (*fn
)(union key
, struct passwd
*, char *, size_t, struct passwd
**),
646 if (pwd_storage
== NULL
) {
647 pwd_storage
= malloc(PWD_STORAGE_INITIAL
);
648 if (pwd_storage
== NULL
)
650 pwd_storage_size
= PWD_STORAGE_INITIAL
;
653 rv
= fn(key
, &pwd
, pwd_storage
, pwd_storage_size
, &res
);
654 if (res
== NULL
&& rv
== ERANGE
) {
656 if ((pwd_storage_size
<< 1) > PWD_STORAGE_MAX
) {
661 pwd_storage_size
<<= 1;
662 pwd_storage
= malloc(pwd_storage_size
);
663 if (pwd_storage
== NULL
)
666 } while (res
== NULL
&& rv
== ERANGE
);
674 wrap_getpwnam_r(union key key
, struct passwd
*pwd
, char *buffer
,
675 size_t bufsize
, struct passwd
**res
)
677 return (getpwnam_r(key
.name
, pwd
, buffer
, bufsize
, res
));
682 wrap_getpwuid_r(union key key
, struct passwd
*pwd
, char *buffer
,
683 size_t bufsize
, struct passwd
**res
)
685 return (getpwuid_r(key
.uid
, pwd
, buffer
, bufsize
, res
));
690 wrap_getpwent_r(union key key __unused
, struct passwd
*pwd
, char *buffer
,
691 size_t bufsize
, struct passwd
**res
)
693 return (getpwent_r(pwd
, buffer
, bufsize
, res
));
698 getpwnam(const char *name
)
703 return (getpw(wrap_getpwnam_r
, key
));
713 return (getpw(wrap_getpwuid_r
, key
));
722 key
.uid
= 0; /* not used */
723 return (getpw(wrap_getpwent_r
, key
));
731 pwdbopen(int *version
)
737 if (geteuid() != 0 ||
738 (res
= dbopen(_PATH_SMP_DB
, O_RDONLY
, 0, DB_HASH
, NULL
)) == NULL
)
739 res
= dbopen(_PATH_MP_DB
, O_RDONLY
, 0, DB_HASH
, NULL
);
742 key
.data
= _PWD_VERSION_KEY
;
743 key
.size
= strlen(_PWD_VERSION_KEY
);
744 rv
= res
->get(res
, &key
, &entry
, 0);
746 *version
= *(unsigned char *)entry
.data
;
750 *version
>= sizeof(pwdb_versions
)/sizeof(pwdb_versions
[0])) {
751 syslog(LOG_CRIT
, "Unsupported password database version %d",
761 files_endstate(void *p
)
767 db
= ((struct files_state
*)p
)->db
;
775 files_setpwent(void *retval
, void *mdata
, va_list ap
)
777 struct files_state
*st
;
780 rv
= files_getstate(&st
);
783 switch ((enum constants
)mdata
) {
785 stayopen
= va_arg(ap
, int);
788 st
->db
= pwdbopen(&st
->version
);
789 st
->stayopen
= stayopen
;
792 if (st
->db
!= NULL
) {
793 st
->db
->close(st
->db
);
805 files_passwd(void *retval
, void *mdata
, va_list ap
)
807 char keybuf
[MAXLOGNAME
+ 1];
809 struct files_state
*st
;
810 enum nss_lookup_type how
;
814 size_t bufsize
, namesize
;
817 int rv
, stayopen
, *errnop
;
821 how
= (enum nss_lookup_type
)mdata
;
824 name
= va_arg(ap
, const char *);
825 keybuf
[0] = _PW_KEYBYNAME
;
828 uid
= va_arg(ap
, uid_t
);
829 keybuf
[0] = _PW_KEYBYUID
;
832 keybuf
[0] = _PW_KEYBYNUM
;
838 pwd
= va_arg(ap
, struct passwd
*);
839 buffer
= va_arg(ap
, char *);
840 bufsize
= va_arg(ap
, size_t);
841 errnop
= va_arg(ap
, int *);
842 *errnop
= files_getstate(&st
);
845 if (how
== nss_lt_all
&& st
->keynum
< 0) {
849 if (st
->db
== NULL
&&
850 (st
->db
= pwdbopen(&st
->version
)) == NULL
) {
855 if (how
== nss_lt_all
)
858 stayopen
= st
->stayopen
;
863 /* MAXLOGNAME includes NUL byte, but we do not
864 * include the NUL byte in the key.
866 namesize
= strlcpy(&keybuf
[1], name
, sizeof(keybuf
)-1);
867 if (namesize
>= sizeof(keybuf
)-1) {
872 key
.size
= namesize
+ 1;
875 if (st
->version
< _PWD_CURRENT_VERSION
) {
876 memcpy(&keybuf
[1], &uid
, sizeof(uid
));
877 key
.size
= sizeof(uid
) + 1;
880 memcpy(&keybuf
[1], &store
, sizeof(store
));
881 key
.size
= sizeof(store
) + 1;
886 if (st
->version
< _PWD_CURRENT_VERSION
) {
887 memcpy(&keybuf
[1], &st
->keynum
,
889 key
.size
= sizeof(st
->keynum
) + 1;
891 store
= htonl(st
->keynum
);
892 memcpy(&keybuf
[1], &store
, sizeof(store
));
893 key
.size
= sizeof(store
) + 1;
897 keybuf
[0] = _PW_VERSIONED(keybuf
[0], st
->version
);
898 rv
= st
->db
->get(st
->db
, &key
, &entry
, 0);
899 if (rv
< 0 || rv
> 1) { /* should never return > 1 */
903 } else if (rv
== 1) {
904 if (how
== nss_lt_all
)
909 rv
= pwdb_versions
[st
->version
].match(entry
.data
, entry
.size
,
911 if (rv
!= NS_SUCCESS
)
913 if (entry
.size
> bufsize
) {
918 memcpy(buffer
, entry
.data
, entry
.size
);
919 rv
= pwdb_versions
[st
->version
].parse(buffer
, entry
.size
, pwd
,
921 } while (how
== nss_lt_all
&& !(rv
& NS_TERMINATE
));
923 if (!stayopen
&& st
->db
!= NULL
) {
924 st
->db
->close(st
->db
);
927 if (rv
== NS_SUCCESS
) {
928 pwd
->pw_fields
&= ~_PWF_SOURCE
;
929 pwd
->pw_fields
|= _PWF_FILES
;
931 *(struct passwd
**)retval
= pwd
;
938 pwdb_match_entry_v3(char *entry
, size_t entrysize
, enum nss_lookup_type how
,
939 const char *name
, uid_t uid
)
944 eom
= &entry
[entrysize
];
945 for (p
= entry
; p
< eom
; p
++)
949 return (NS_NOTFOUND
);
950 if (how
== nss_lt_all
)
952 if (how
== nss_lt_name
)
953 return (strcmp(name
, entry
) == 0 ? NS_SUCCESS
: NS_NOTFOUND
);
954 for (p
++; p
< eom
; p
++)
957 if (*p
!= '\0' || (++p
) + sizeof(uid
) >= eom
)
958 return (NS_NOTFOUND
);
959 memcpy(&uid2
, p
, sizeof(uid2
));
960 return (uid
== uid2
? NS_SUCCESS
: NS_NOTFOUND
);
965 pwdb_parse_entry_v3(char *buffer
, size_t bufsize
, struct passwd
*pwd
,
969 int32_t pw_change
, pw_expire
;
971 /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
973 eom
= &buffer
[bufsize
];
974 #define STRING(field) do { \
976 while (p < eom && *p != '\0') \
979 return (NS_NOTFOUND); \
982 #define SCALAR(field) do { \
983 if (p + sizeof(field) > eom) \
984 return (NS_NOTFOUND); \
985 memcpy(&(field), p, sizeof(field)); \
986 p += sizeof(field); \
988 STRING(pwd
->pw_name
);
989 STRING(pwd
->pw_passwd
);
993 STRING(pwd
->pw_class
);
994 STRING(pwd
->pw_gecos
);
996 STRING(pwd
->pw_shell
);
998 SCALAR(pwd
->pw_fields
);
1001 pwd
->pw_change
= pw_change
;
1002 pwd
->pw_expire
= pw_expire
;
1003 return (NS_SUCCESS
);
1008 pwdb_match_entry_v4(char *entry
, size_t entrysize
, enum nss_lookup_type how
,
1009 const char *name
, uid_t uid
)
1011 const char *p
, *eom
;
1014 eom
= &entry
[entrysize
];
1015 for (p
= entry
; p
< eom
; p
++)
1019 return (NS_NOTFOUND
);
1020 if (how
== nss_lt_all
)
1021 return (NS_SUCCESS
);
1022 if (how
== nss_lt_name
)
1023 return (strcmp(name
, entry
) == 0 ? NS_SUCCESS
: NS_NOTFOUND
);
1024 for (p
++; p
< eom
; p
++)
1027 if (*p
!= '\0' || (++p
) + sizeof(uid
) >= eom
)
1028 return (NS_NOTFOUND
);
1029 memcpy(&uid2
, p
, sizeof(uid2
));
1031 return (uid
== (uid_t
)uid2
? NS_SUCCESS
: NS_NOTFOUND
);
1036 pwdb_parse_entry_v4(char *buffer
, size_t bufsize
, struct passwd
*pwd
,
1042 /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
1044 eom
= &buffer
[bufsize
];
1045 #define STRING(field) do { \
1047 while (p < eom && *p != '\0') \
1050 return (NS_NOTFOUND); \
1053 #define SCALAR(field) do { \
1054 if (p + sizeof(n) > eom) \
1055 return (NS_NOTFOUND); \
1056 memcpy(&n, p, sizeof(n)); \
1057 (field) = ntohl(n); \
1060 STRING(pwd
->pw_name
);
1061 STRING(pwd
->pw_passwd
);
1062 SCALAR(pwd
->pw_uid
);
1063 SCALAR(pwd
->pw_gid
);
1064 SCALAR(pwd
->pw_change
);
1065 STRING(pwd
->pw_class
);
1066 STRING(pwd
->pw_gecos
);
1067 STRING(pwd
->pw_dir
);
1068 STRING(pwd
->pw_shell
);
1069 SCALAR(pwd
->pw_expire
);
1070 SCALAR(pwd
->pw_fields
);
1073 return (NS_SUCCESS
);
1082 dns_endstate(void *p
)
1089 dns_setpwent(void *retval
, void *mdata
, va_list ap
)
1091 struct dns_state
*st
;
1094 rv
= dns_getstate(&st
);
1096 return (NS_UNAVAIL
);
1098 return (NS_UNAVAIL
);
1103 dns_passwd(void *retval
, void *mdata
, va_list ap
)
1105 char buf
[HESIOD_NAME_MAX
];
1106 struct dns_state
*st
;
1108 const char *name
, *label
;
1110 char *buffer
, **hes
;
1111 size_t bufsize
, linesize
;
1113 enum nss_lookup_type how
;
1120 how
= (enum nss_lookup_type
)mdata
;
1123 name
= va_arg(ap
, const char *);
1126 uid
= va_arg(ap
, uid_t
);
1131 pwd
= va_arg(ap
, struct passwd
*);
1132 buffer
= va_arg(ap
, char *);
1133 bufsize
= va_arg(ap
, size_t);
1134 errnop
= va_arg(ap
, int *);
1135 *errnop
= dns_getstate(&st
);
1137 return (NS_UNAVAIL
);
1138 if (hesiod_init(&ctx
) != 0) {
1150 if (snprintf(buf
, sizeof(buf
), "%lu",
1151 (unsigned long)uid
) >= sizeof(buf
))
1156 if (st
->counter
< 0)
1158 if (snprintf(buf
, sizeof(buf
), "passwd-%ld",
1159 st
->counter
++) >= sizeof(buf
))
1164 hes
= hesiod_resolve(ctx
, label
,
1165 how
== nss_lt_id
? "uid" : "passwd");
1167 if (how
== nss_lt_all
)
1169 if (errno
!= ENOENT
)
1173 rv
= __pw_match_entry(hes
[0], strlen(hes
[0]), how
, name
, uid
);
1174 if (rv
!= NS_SUCCESS
) {
1175 hesiod_free_list(ctx
, hes
);
1179 linesize
= strlcpy(buffer
, hes
[0], bufsize
);
1180 if (linesize
>= bufsize
) {
1185 hesiod_free_list(ctx
, hes
);
1187 rv
= __pw_parse_entry(buffer
, bufsize
, pwd
, 0, errnop
);
1188 } while (how
== nss_lt_all
&& !(rv
& NS_TERMINATE
));
1191 hesiod_free_list(ctx
, hes
);
1194 if (rv
== NS_SUCCESS
) {
1195 pwd
->pw_fields
&= ~_PWF_SOURCE
;
1196 pwd
->pw_fields
|= _PWF_HESIOD
;
1198 *(struct passwd
**)retval
= pwd
;
1210 nis_endstate(void *p
)
1212 free(((struct nis_state
*)p
)->key
);
1217 * Test for the presence of special FreeBSD-specific master.passwd.by*
1218 * maps. We do this using yp_order(). If it fails, then either the server
1219 * doesn't have the map, or the YPPROC_ORDER procedure isn't supported by
1220 * the server (Sun NIS+ servers in YP compat mode behave this way). If
1221 * the master.passwd.by* maps don't exist, then let the lookup routine try
1222 * the regular passwd.by* maps instead. If the lookup routine fails, it
1223 * can return an error as needed.
1226 nis_map(char *domain
, enum nss_lookup_type how
, char *buffer
, size_t bufsize
,
1232 if (geteuid() == 0) {
1233 if (snprintf(buffer
, bufsize
, "master.passwd.by%s",
1234 (how
== nss_lt_id
) ? "uid" : "name") >= bufsize
)
1235 return (NS_UNAVAIL
);
1236 rv
= yp_order(domain
, buffer
, &order
);
1239 return (NS_SUCCESS
);
1243 if (snprintf(buffer
, bufsize
, "passwd.by%s",
1244 (how
== nss_lt_id
) ? "uid" : "name") >= bufsize
)
1245 return (NS_UNAVAIL
);
1247 return (NS_SUCCESS
);
1252 nis_adjunct(char *domain
, const char *name
, char *buffer
, size_t bufsize
)
1255 char *result
, *p
, *q
, *eor
;
1259 rv
= yp_match(domain
, "passwd.adjunct.byname", name
, strlen(name
),
1260 &result
, &resultlen
);
1264 eor
= &result
[resultlen
];
1265 p
= memchr(result
, ':', eor
- result
);
1266 if (p
!= NULL
&& ++p
< eor
&&
1267 (q
= memchr(p
, ':', eor
- p
)) != NULL
) {
1268 if (q
- p
>= bufsize
)
1271 memcpy(buffer
, p
, q
- p
);
1272 buffer
[q
- p
] ='\0';
1283 nis_setpwent(void *retval
, void *mdata
, va_list ap
)
1285 struct nis_state
*st
;
1288 rv
= nis_getstate(&st
);
1290 return (NS_UNAVAIL
);
1294 return (NS_UNAVAIL
);
1299 nis_passwd(void *retval
, void *mdata
, va_list ap
)
1302 struct nis_state
*st
;
1305 char *buffer
, *key
, *result
;
1308 enum nss_lookup_type how
;
1309 int *errnop
, keylen
, resultlen
, rv
, master
;
1313 how
= (enum nss_lookup_type
)mdata
;
1316 name
= va_arg(ap
, const char *);
1319 uid
= va_arg(ap
, uid_t
);
1324 pwd
= va_arg(ap
, struct passwd
*);
1325 buffer
= va_arg(ap
, char *);
1326 bufsize
= va_arg(ap
, size_t);
1327 errnop
= va_arg(ap
, int *);
1328 *errnop
= nis_getstate(&st
);
1330 return (NS_UNAVAIL
);
1331 if (st
->domain
[0] == '\0') {
1332 if (getdomainname(st
->domain
, sizeof(st
->domain
)) != 0) {
1334 return (NS_UNAVAIL
);
1337 rv
= nis_map(st
->domain
, how
, map
, sizeof(map
), &master
);
1338 if (rv
!= NS_SUCCESS
)
1345 if (strlcpy(buffer
, name
, bufsize
) >= bufsize
)
1349 if (snprintf(buffer
, bufsize
, "%lu",
1350 (unsigned long)uid
) >= bufsize
)
1359 if (how
== nss_lt_all
) {
1360 if (st
->key
== NULL
)
1361 rv
= yp_first(st
->domain
, map
, &st
->key
,
1362 &st
->keylen
, &result
, &resultlen
);
1365 keylen
= st
->keylen
;
1367 rv
= yp_next(st
->domain
, map
, key
, keylen
,
1368 &st
->key
, &st
->keylen
, &result
,
1376 if (rv
== YPERR_NOMORE
)
1383 rv
= yp_match(st
->domain
, map
, buffer
, strlen(buffer
),
1384 &result
, &resultlen
);
1385 if (rv
== YPERR_KEY
) {
1388 } else if (rv
!= 0) {
1394 if (resultlen
>= bufsize
)
1396 memcpy(buffer
, result
, resultlen
);
1397 buffer
[resultlen
] = '\0';
1399 rv
= __pw_match_entry(buffer
, resultlen
, how
, name
, uid
);
1400 if (rv
== NS_SUCCESS
)
1401 rv
= __pw_parse_entry(buffer
, resultlen
, pwd
, master
,
1403 } while (how
== nss_lt_all
&& !(rv
& NS_TERMINATE
));
1405 if (rv
== NS_SUCCESS
) {
1406 if (strstr(pwd
->pw_passwd
, "##") != NULL
) {
1407 rv
= nis_adjunct(st
->domain
, pwd
->pw_name
,
1408 &buffer
[resultlen
+1], bufsize
-resultlen
-1);
1412 pwd
->pw_passwd
= &buffer
[resultlen
+1];
1414 pwd
->pw_fields
&= ~_PWF_SOURCE
;
1415 pwd
->pw_fields
|= _PWF_NIS
;
1417 *(struct passwd
**)retval
= pwd
;
1432 compat_clear_template(struct passwd
*template)
1435 free(template->pw_passwd
);
1436 free(template->pw_gecos
);
1437 free(template->pw_dir
);
1438 free(template->pw_shell
);
1439 memset(template, 0, sizeof(*template));
1444 compat_set_template(struct passwd
*src
, struct passwd
*template)
1447 compat_clear_template(template);
1448 #ifdef PW_OVERRIDE_PASSWD
1449 if ((src
->pw_fields
& _PWF_PASSWD
) &&
1450 (template->pw_passwd
= strdup(src
->pw_passwd
)) == NULL
)
1453 if (src
->pw_fields
& _PWF_UID
)
1454 template->pw_uid
= src
->pw_uid
;
1455 if (src
->pw_fields
& _PWF_GID
)
1456 template->pw_gid
= src
->pw_gid
;
1457 if ((src
->pw_fields
& _PWF_GECOS
) &&
1458 (template->pw_gecos
= strdup(src
->pw_gecos
)) == NULL
)
1460 if ((src
->pw_fields
& _PWF_DIR
) &&
1461 (template->pw_dir
= strdup(src
->pw_dir
)) == NULL
)
1463 if ((src
->pw_fields
& _PWF_SHELL
) &&
1464 (template->pw_shell
= strdup(src
->pw_shell
)) == NULL
)
1466 template->pw_fields
= src
->pw_fields
;
1469 syslog(LOG_ERR
, "getpwent memory allocation failure");
1475 compat_use_template(struct passwd
*pwd
, struct passwd
*template, char *buffer
,
1479 char *copy
, *p
, *q
, *eob
;
1482 /* We cannot know the layout of the password fields in `buffer',
1483 * so we have to copy everything.
1485 if (template->pw_fields
== 0) /* nothing to fill-in */
1488 n
+= pwd
->pw_name
!= NULL
? strlen(pwd
->pw_name
) + 1 : 0;
1489 n
+= pwd
->pw_passwd
!= NULL
? strlen(pwd
->pw_passwd
) + 1 : 0;
1490 n
+= pwd
->pw_class
!= NULL
? strlen(pwd
->pw_class
) + 1 : 0;
1491 n
+= pwd
->pw_gecos
!= NULL
? strlen(pwd
->pw_gecos
) + 1 : 0;
1492 n
+= pwd
->pw_dir
!= NULL
? strlen(pwd
->pw_dir
) + 1 : 0;
1493 n
+= pwd
->pw_shell
!= NULL
? strlen(pwd
->pw_shell
) + 1 : 0;
1496 syslog(LOG_ERR
, "getpwent memory allocation failure");
1501 #define COPY(field) do { \
1502 if (pwd->field == NULL) \
1503 hold.field = NULL; \
1506 p += strlcpy(p, pwd->field, eob-p) + 1; \
1517 eob
= &buffer
[bufsize
];
1518 #define COPY(field, flag) do { \
1519 q = (template->pw_fields & flag) ? template->field : hold.field; \
1521 pwd->field = NULL; \
1524 if ((n = strlcpy(p, q, eob-p)) >= eob-p) { \
1532 #ifdef PW_OVERRIDE_PASSWD
1533 COPY(pw_passwd
, _PWF_PASSWD
);
1538 COPY(pw_gecos
, _PWF_GECOS
);
1539 COPY(pw_dir
, _PWF_DIR
);
1540 COPY(pw_shell
, _PWF_SHELL
);
1542 #define COPY(field, flag) do { \
1543 if (template->pw_fields & flag) \
1544 pwd->field = template->field; \
1546 COPY(pw_uid
, _PWF_UID
);
1547 COPY(pw_gid
, _PWF_GID
);
1555 compat_exclude(const char *name
, DB
**db
)
1560 (*db
= dbopen(NULL
, O_RDWR
, 600, DB_HASH
, 0)) == NULL
)
1562 key
.size
= strlen(name
);
1563 key
.data
= (char *)name
;
1567 if ((*db
)->put(*db
, &key
, &data
, 0) == -1)
1574 compat_is_excluded(const char *name
, DB
*db
)
1580 key
.size
= strlen(name
);
1581 key
.data
= (char *)name
;
1582 return (db
->get(db
, &key
, &data
, 0) == 0);
1587 compat_redispatch(struct compat_state
*st
, enum nss_lookup_type how
,
1588 enum nss_lookup_type lookup_how
, const char *name
, const char *lookup_name
,
1589 uid_t uid
, struct passwd
*pwd
, char *buffer
, size_t bufsize
, int *errnop
)
1591 static const ns_src compatsrc
[] = {
1593 { NSSRC_NIS
, NS_SUCCESS
},
1599 { NSSRC_NIS
, nis_passwd
, NULL
},
1602 { NSSRC_DNS
, dns_passwd
, NULL
},
1604 { NULL
, NULL
, NULL
}
1609 for (i
= 0; i
< sizeof(dtab
)/sizeof(dtab
[0]) - 1; i
++)
1610 dtab
[i
].mdata
= (void *)lookup_how
;
1613 switch (lookup_how
) {
1615 rv
= _nsdispatch(&discard
, dtab
, NSDB_PASSWD_COMPAT
,
1616 "getpwent_r", compatsrc
, pwd
, buffer
, bufsize
,
1620 rv
= _nsdispatch(&discard
, dtab
, NSDB_PASSWD_COMPAT
,
1621 "getpwuid_r", compatsrc
, uid
, pwd
, buffer
,
1625 rv
= _nsdispatch(&discard
, dtab
, NSDB_PASSWD_COMPAT
,
1626 "getpwnam_r", compatsrc
, lookup_name
, pwd
, buffer
,
1630 return (NS_UNAVAIL
);
1632 if (rv
!= NS_SUCCESS
)
1634 if (compat_is_excluded(pwd
->pw_name
, st
->exclude
)) {
1635 if (how
== nss_lt_all
)
1637 return (NS_NOTFOUND
);
1639 e
= compat_use_template(pwd
, &st
->template, buffer
, bufsize
);
1645 return (NS_UNAVAIL
);
1649 if (strcmp(name
, pwd
->pw_name
) != 0)
1650 return (NS_NOTFOUND
);
1653 if (uid
!= pwd
->pw_uid
)
1654 return (NS_NOTFOUND
);
1659 return (NS_SUCCESS
);
1664 compat_endstate(void *p
)
1666 struct compat_state
*st
;
1670 st
= (struct compat_state
*)p
;
1672 st
->db
->close(st
->db
);
1673 if (st
->exclude
!= NULL
)
1674 st
->exclude
->close(st
->exclude
);
1675 compat_clear_template(&st
->template);
1681 compat_setpwent(void *retval
, void *mdata
, va_list ap
)
1683 static const ns_src compatsrc
[] = {
1685 { NSSRC_NIS
, NS_SUCCESS
},
1691 { NSSRC_NIS
, nis_setpwent
, NULL
},
1694 { NSSRC_DNS
, dns_setpwent
, NULL
},
1696 { NULL
, NULL
, NULL
}
1698 struct compat_state
*st
;
1701 #define set_setent(x, y) do { \
1704 for (i = 0; i < (sizeof(x)/sizeof(x[0])) - 1; i++) \
1705 x[i].mdata = (void *)y; \
1708 rv
= compat_getstate(&st
);
1710 return (NS_UNAVAIL
);
1711 switch ((enum constants
)mdata
) {
1713 stayopen
= va_arg(ap
, int);
1716 st
->db
= pwdbopen(&st
->version
);
1717 st
->stayopen
= stayopen
;
1718 set_setent(dtab
, mdata
);
1719 _nsdispatch(NULL
, dtab
, NSDB_PASSWD_COMPAT
, "setpwent",
1723 if (st
->db
!= NULL
) {
1724 st
->db
->close(st
->db
);
1727 set_setent(dtab
, mdata
);
1728 _nsdispatch(NULL
, dtab
, NSDB_PASSWD_COMPAT
, "endpwent",
1734 return (NS_UNAVAIL
);
1740 compat_passwd(void *retval
, void *mdata
, va_list ap
)
1742 char keybuf
[MAXLOGNAME
+ 1];
1744 struct compat_state
*st
;
1745 enum nss_lookup_type how
;
1748 char *buffer
, *pw_name
;
1749 char *host
, *user
, *domain
;
1753 int rv
, from_compat
, stayopen
, *errnop
;
1758 how
= (enum nss_lookup_type
)mdata
;
1761 name
= va_arg(ap
, const char *);
1764 uid
= va_arg(ap
, uid_t
);
1772 pwd
= va_arg(ap
, struct passwd
*);
1773 buffer
= va_arg(ap
, char *);
1774 bufsize
= va_arg(ap
, size_t);
1775 errnop
= va_arg(ap
, int *);
1776 *errnop
= compat_getstate(&st
);
1778 return (NS_UNAVAIL
);
1779 if (how
== nss_lt_all
&& st
->keynum
< 0) {
1783 if (st
->db
== NULL
&&
1784 (st
->db
= pwdbopen(&st
->version
)) == NULL
) {
1789 if (how
== nss_lt_all
) {
1790 if (st
->keynum
< 0) {
1797 stayopen
= st
->stayopen
;
1801 switch (st
->compat
) {
1802 case COMPAT_MODE_ALL
:
1803 rv
= compat_redispatch(st
, how
, how
, name
, name
, uid
, pwd
,
1804 buffer
, bufsize
, errnop
);
1805 if (rv
!= NS_SUCCESS
)
1806 st
->compat
= COMPAT_MODE_OFF
;
1808 case COMPAT_MODE_NETGROUP
:
1809 /* XXX getnetgrent is not thread-safe. */
1811 rv
= getnetgrent(&host
, &user
, &domain
);
1814 st
->compat
= COMPAT_MODE_OFF
;
1817 } else if (user
== NULL
|| user
[0] == '\0')
1819 rv
= compat_redispatch(st
, how
, nss_lt_name
, name
,
1820 user
, uid
, pwd
, buffer
, bufsize
, errnop
);
1821 } while (st
->compat
== COMPAT_MODE_NETGROUP
&&
1822 !(rv
& NS_TERMINATE
));
1824 case COMPAT_MODE_NAME
:
1825 rv
= compat_redispatch(st
, how
, nss_lt_name
, name
, st
->name
,
1826 uid
, pwd
, buffer
, bufsize
, errnop
);
1829 st
->compat
= COMPAT_MODE_OFF
;
1834 if (rv
& NS_TERMINATE
) {
1840 while (st
->keynum
>= 0) {
1842 if (st
->version
< _PWD_CURRENT_VERSION
) {
1843 memcpy(&keybuf
[1], &st
->keynum
, sizeof(st
->keynum
));
1844 key
.size
= sizeof(st
->keynum
) + 1;
1846 store
= htonl(st
->keynum
);
1847 memcpy(&keybuf
[1], &store
, sizeof(store
));
1848 key
.size
= sizeof(store
) + 1;
1850 keybuf
[0] = _PW_VERSIONED(_PW_KEYBYNUM
, st
->version
);
1851 rv
= st
->db
->get(st
->db
, &key
, &entry
, 0);
1852 if (rv
< 0 || rv
> 1) { /* should never return > 1 */
1856 } else if (rv
== 1) {
1861 pw_name
= (char *)entry
.data
;
1862 switch (pw_name
[0]) {
1864 switch (pw_name
[1]) {
1866 st
->compat
= COMPAT_MODE_ALL
;
1869 setnetgrent(&pw_name
[2]);
1870 st
->compat
= COMPAT_MODE_NETGROUP
;
1873 st
->name
= strdup(&pw_name
[1]);
1874 if (st
->name
== NULL
) {
1876 "getpwent memory allocation failure");
1881 st
->compat
= COMPAT_MODE_NAME
;
1883 if (entry
.size
> bufsize
) {
1888 memcpy(buffer
, entry
.data
, entry
.size
);
1889 rv
= pwdb_versions
[st
->version
].parse(buffer
,
1890 entry
.size
, pwd
, errnop
);
1891 if (rv
!= NS_SUCCESS
)
1893 else if (compat_set_template(pwd
, &st
->template) < 0) {
1900 switch (pw_name
[1]) {
1902 /* XXX Maybe syslog warning */
1905 setnetgrent(&pw_name
[2]);
1906 while (getnetgrent(&host
, &user
, &domain
) !=
1908 if (user
!= NULL
&& user
[0] != '\0')
1909 compat_exclude(user
,
1915 compat_exclude(&pw_name
[1], &st
->exclude
);
1922 if (compat_is_excluded((char *)entry
.data
, st
->exclude
))
1924 rv
= pwdb_versions
[st
->version
].match(entry
.data
, entry
.size
,
1926 if (rv
== NS_RETURN
)
1928 else if (rv
!= NS_SUCCESS
)
1930 if (entry
.size
> bufsize
) {
1935 memcpy(buffer
, entry
.data
, entry
.size
);
1936 rv
= pwdb_versions
[st
->version
].parse(buffer
, entry
.size
, pwd
,
1938 if (rv
& NS_TERMINATE
)
1942 if (!stayopen
&& st
->db
!= NULL
) {
1943 st
->db
->close(st
->db
);
1946 if (rv
== NS_SUCCESS
) {
1948 pwd
->pw_fields
&= ~_PWF_SOURCE
;
1949 pwd
->pw_fields
|= _PWF_FILES
;
1952 *(struct passwd
**)retval
= pwd
;
1959 * common passwd line matching and parsing
1962 __pw_match_entry(const char *entry
, size_t entrysize
, enum nss_lookup_type how
,
1963 const char *name
, uid_t uid
)
1965 const char *p
, *eom
;
1970 eom
= entry
+ entrysize
;
1971 for (p
= entry
; p
< eom
; p
++)
1975 return (NS_NOTFOUND
);
1976 if (how
== nss_lt_all
)
1977 return (NS_SUCCESS
);
1978 if (how
== nss_lt_name
) {
1980 if (len
== (p
- entry
) && memcmp(name
, entry
, len
) == 0)
1981 return (NS_SUCCESS
);
1983 return (NS_NOTFOUND
);
1985 for (p
++; p
< eom
; p
++)
1989 return (NS_NOTFOUND
);
1990 m
= strtoul(++p
, &q
, 10);
1991 if (q
[0] != ':' || (uid_t
)m
!= uid
)
1992 return (NS_NOTFOUND
);
1994 return (NS_SUCCESS
);
1998 /* XXX buffer must be NUL-terminated. errnop is not set correctly. */
2000 __pw_parse_entry(char *buffer
, size_t bufsize __unused
, struct passwd
*pwd
,
2001 int master
, int *errnop __unused
)
2004 if (__pw_scan(buffer
, pwd
, master
? _PWSCAN_MASTER
: 0) == 0)
2005 return (NS_NOTFOUND
);
2007 return (NS_SUCCESS
);