4 * Copyright (c) 2007, Stefan Metzmacher <metze@samba.org>
5 * Copyright (c) 2009, Guenther Deschner <gd@samba.org>
6 * Copyright (c) 2014-2015, Michael Adam <obnox@samba.org>
7 * Copyright (c) 2015, Robin Hack <hack.robin@gmail.com>
8 * Copyright (c) 2013-2018, Andreas Schneider <asn@samba.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the author nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #include <sys/types.h>
45 #include <sys/socket.h>
58 #include <netinet/in.h>
64 * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h gives us
65 * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
68 #ifndef _POSIX_PTHREAD_SEMANTICS
69 #define _POSIX_PTHREAD_SEMANTICS
76 #endif /* HAVE_SHADOW_H */
79 #include <arpa/inet.h>
80 #include <netinet/in.h>
84 #if defined(HAVE_NSS_H)
88 typedef enum nss_status NSS_STATUS
;
89 #elif defined(HAVE_NSS_COMMON_H)
91 #include <nss_common.h>
92 #include <nss_dbdefs.h>
95 typedef nss_status_t NSS_STATUS
;
97 # define NSS_STATUS_SUCCESS NSS_SUCCESS
98 # define NSS_STATUS_NOTFOUND NSS_NOTFOUND
99 # define NSS_STATUS_UNAVAIL NSS_UNAVAIL
100 # define NSS_STATUS_TRYAGAIN NSS_TRYAGAIN
102 # error "No nsswitch support detected"
106 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
114 #define EAI_NODATA EAI_NONAME
117 #ifndef EAI_ADDRFAMILY
118 #define EAI_ADDRFAMILY EAI_FAMILY
122 #define __STRING(x) #x
125 #ifndef __STRINGSTRING
126 #define __STRINGSTRING(x) __STRING(x)
130 #define __LINESTR__ __STRINGSTRING(__LINE__)
134 #define __location__ __FILE__ ":" __LINESTR__
138 #define DNS_NAME_MAX 255
141 /* GCC have printf type attribute check. */
142 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
143 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
145 #define PRINTF_ATTRIBUTE(a,b)
146 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
148 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
149 #define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
151 #define CONSTRUCTOR_ATTRIBUTE
152 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
154 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
155 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
157 #define DESTRUCTOR_ATTRIBUTE
158 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
160 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
163 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
166 #ifndef discard_const
167 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
170 #ifndef discard_const_p
171 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
175 #define NWRAP_INET_ADDRSTRLEN INET6_ADDRSTRLEN
177 #define NWRAP_INET_ADDRSTRLEN INET_ADDRSTRLEN
180 #define NWRAP_LOCK(m) do { \
181 pthread_mutex_lock(&( m ## _mutex)); \
184 #define NWRAP_UNLOCK(m) do { \
185 pthread_mutex_unlock(&( m ## _mutex)); \
188 static pthread_mutex_t libc_symbol_binding_mutex
= PTHREAD_MUTEX_INITIALIZER
;
189 static pthread_mutex_t nss_module_symbol_binding_mutex
= PTHREAD_MUTEX_INITIALIZER
;
191 static bool nwrap_initialized
= false;
192 static pthread_mutex_t nwrap_initialized_mutex
= PTHREAD_MUTEX_INITIALIZER
;
194 /* The mutex or accessing the id */
195 static pthread_mutex_t nwrap_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
196 static pthread_mutex_t nwrap_gr_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
197 static pthread_mutex_t nwrap_he_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
198 static pthread_mutex_t nwrap_pw_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
199 static pthread_mutex_t nwrap_sp_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
201 /* Add new global locks here please */
202 /* Also don't forget to add locks to
203 * nwrap_init() function.
205 # define NWRAP_LOCK_ALL do { \
206 NWRAP_LOCK(libc_symbol_binding); \
207 NWRAP_LOCK(nss_module_symbol_binding); \
208 NWRAP_LOCK(nwrap_initialized); \
209 NWRAP_LOCK(nwrap_global); \
210 NWRAP_LOCK(nwrap_gr_global); \
211 NWRAP_LOCK(nwrap_he_global); \
212 NWRAP_LOCK(nwrap_pw_global); \
213 NWRAP_LOCK(nwrap_sp_global); \
216 # define NWRAP_UNLOCK_ALL do {\
217 NWRAP_UNLOCK(nwrap_sp_global); \
218 NWRAP_UNLOCK(nwrap_pw_global); \
219 NWRAP_UNLOCK(nwrap_he_global); \
220 NWRAP_UNLOCK(nwrap_gr_global); \
221 NWRAP_UNLOCK(nwrap_global); \
222 NWRAP_UNLOCK(nwrap_initialized); \
223 NWRAP_UNLOCK(nss_module_symbol_binding); \
224 NWRAP_UNLOCK(libc_symbol_binding); \
227 static void nwrap_init(void);
229 static void nwrap_thread_prepare(void)
235 static void nwrap_thread_parent(void)
240 static void nwrap_thread_child(void)
245 enum nwrap_dbglvl_e
{
252 #ifndef HAVE_GETPROGNAME
253 static const char *getprogname(void)
255 #if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
256 return program_invocation_short_name
;
257 #elif defined(HAVE_GETEXECNAME)
258 return getexecname();
261 #endif /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
263 #endif /* HAVE_GETPROGNAME */
265 static void nwrap_log(enum nwrap_dbglvl_e dbglvl
, const char *func
, const char *format
, ...) PRINTF_ATTRIBUTE(3, 4);
266 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
268 static void nwrap_log(enum nwrap_dbglvl_e dbglvl
,
270 const char *format
, ...)
275 unsigned int lvl
= 0;
276 const char *prefix
= "NWRAP";
277 const char *progname
= getprogname();
279 d
= getenv("NSS_WRAPPER_DEBUGLEVEL");
288 va_start(va
, format
);
289 vsnprintf(buffer
, sizeof(buffer
), format
, va
);
293 case NWRAP_LOG_ERROR
:
294 prefix
= "NWRAP_ERROR";
297 prefix
= "NWRAP_WARN";
299 case NWRAP_LOG_DEBUG
:
300 prefix
= "NWRAP_DEBUG";
302 case NWRAP_LOG_TRACE
:
303 prefix
= "NWRAP_TRACE";
307 if (progname
== NULL
) {
308 progname
= "<unknown>";
312 "%s[%s (%u)] - %s: %s\n",
315 (unsigned int)getpid(),
324 #define LIBC_NAME "libc.so"
326 typedef struct passwd
*(*__libc_getpwnam
)(const char *name
);
328 typedef int (*__libc_getpwnam_r
)(const char *name
,
332 struct passwd
**result
);
334 typedef struct passwd
*(*__libc_getpwuid
)(uid_t uid
);
336 typedef int (*__libc_getpwuid_r
)(uid_t uid
,
340 struct passwd
**result
);
342 typedef void (*__libc_setpwent
)(void);
344 typedef struct passwd
*(*__libc_getpwent
)(void);
346 #ifdef HAVE_GETPWENT_R
347 # ifdef HAVE_SOLARIS_GETPWENT_R
348 typedef struct passwd
*(*__libc_getpwent_r
)(struct passwd
*pwbuf
,
351 # else /* HAVE_SOLARIS_GETPWENT_R */
352 typedef int (*__libc_getpwent_r
)(struct passwd
*pwbuf
,
355 struct passwd
**pwbufp
);
356 # endif /* HAVE_SOLARIS_GETPWENT_R */
357 #endif /* HAVE_GETPWENT_R */
359 typedef void (*__libc_endpwent
)(void);
361 typedef int (*__libc_initgroups
)(const char *user
, gid_t gid
);
363 typedef struct group
*(*__libc_getgrnam
)(const char *name
);
365 typedef int (*__libc_getgrnam_r
)(const char *name
,
369 struct group
**result
);
371 typedef struct group
*(*__libc_getgrgid
)(gid_t gid
);
373 typedef int (*__libc_getgrgid_r
)(gid_t gid
,
377 struct group
**result
);
379 typedef void (*__libc_setgrent
)(void);
381 typedef struct group
*(*__libc_getgrent
)(void);
383 #ifdef HAVE_GETGRENT_R
384 # ifdef HAVE_SOLARIS_GETGRENT_R
385 typedef struct group
*(*__libc_getgrent_r
)(struct group
*group
,
388 # else /* HAVE_SOLARIS_GETGRENT_R */
389 typedef int (*__libc_getgrent_r
)(struct group
*group
,
392 struct group
**result
);
393 # endif /* HAVE_SOLARIS_GETGRENT_R */
394 #endif /* HAVE_GETGRENT_R */
396 typedef void (*__libc_endgrent
)(void);
398 typedef int (*__libc_getgrouplist
)(const char *user
,
403 typedef void (*__libc_sethostent
)(int stayopen
);
405 typedef struct hostent
*(*__libc_gethostent
)(void);
407 typedef void (*__libc_endhostent
)(void);
409 typedef struct hostent
*(*__libc_gethostbyname
)(const char *name
);
411 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
412 typedef struct hostent
*(*__libc_gethostbyname2
)(const char *name
, int af
);
415 #ifdef HAVE_GETHOSTBYNAME2_R /* GNU extension */
416 typedef int (*__libc_gethostbyname2_r
)(const char *name
,
421 struct hostent
**result
,
425 typedef struct hostent
*(*__libc_gethostbyaddr
)(const void *addr
,
429 typedef int (*__libc_getaddrinfo
)(const char *node
,
431 const struct addrinfo
*hints
,
432 struct addrinfo
**res
);
433 typedef int (*__libc_getnameinfo
)(const struct sockaddr
*sa
,
441 typedef int (*__libc_gethostname
)(char *name
, size_t len
);
443 #ifdef HAVE_GETHOSTBYNAME_R
444 typedef int (*__libc_gethostbyname_r
)(const char *name
,
446 char *buf
, size_t buflen
,
447 struct hostent
**result
, int *h_errnop
);
450 #ifdef HAVE_GETHOSTBYADDR_R
451 typedef int (*__libc_gethostbyaddr_r
)(const void *addr
,
457 struct hostent
**result
,
461 #define NWRAP_SYMBOL_ENTRY(i) \
467 struct nwrap_libc_symbols
{
468 NWRAP_SYMBOL_ENTRY(getpwnam
);
469 NWRAP_SYMBOL_ENTRY(getpwnam_r
);
470 NWRAP_SYMBOL_ENTRY(getpwuid
);
471 NWRAP_SYMBOL_ENTRY(getpwuid_r
);
472 NWRAP_SYMBOL_ENTRY(setpwent
);
473 NWRAP_SYMBOL_ENTRY(getpwent
);
474 #ifdef HAVE_GETPWENT_R
475 NWRAP_SYMBOL_ENTRY(getpwent_r
);
477 NWRAP_SYMBOL_ENTRY(endpwent
);
479 NWRAP_SYMBOL_ENTRY(initgroups
);
480 NWRAP_SYMBOL_ENTRY(getgrnam
);
481 NWRAP_SYMBOL_ENTRY(getgrnam_r
);
482 NWRAP_SYMBOL_ENTRY(getgrgid
);
483 NWRAP_SYMBOL_ENTRY(getgrgid_r
);
484 NWRAP_SYMBOL_ENTRY(setgrent
);
485 NWRAP_SYMBOL_ENTRY(getgrent
);
486 #ifdef HAVE_GETGRENT_R
487 NWRAP_SYMBOL_ENTRY(getgrent_r
);
489 NWRAP_SYMBOL_ENTRY(endgrent
);
490 NWRAP_SYMBOL_ENTRY(getgrouplist
);
492 NWRAP_SYMBOL_ENTRY(sethostent
);
493 NWRAP_SYMBOL_ENTRY(gethostent
);
494 NWRAP_SYMBOL_ENTRY(endhostent
);
495 NWRAP_SYMBOL_ENTRY(gethostbyname
);
496 #ifdef HAVE_GETHOSTBYNAME_R
497 NWRAP_SYMBOL_ENTRY(gethostbyname_r
);
499 #ifdef HAVE_GETHOSTBYNAME2
500 NWRAP_SYMBOL_ENTRY(gethostbyname2
);
502 #ifdef HAVE_GETHOSTBYNAME2_R
503 NWRAP_SYMBOL_ENTRY(gethostbyname2_r
);
505 NWRAP_SYMBOL_ENTRY(gethostbyaddr
);
506 #ifdef HAVE_GETHOSTBYADDR_R
507 NWRAP_SYMBOL_ENTRY(gethostbyaddr_r
);
509 NWRAP_SYMBOL_ENTRY(getaddrinfo
);
510 NWRAP_SYMBOL_ENTRY(getnameinfo
);
511 NWRAP_SYMBOL_ENTRY(gethostname
);
513 #undef NWRAP_SYMBOL_ENTRY
515 typedef NSS_STATUS (*__nss_getpwnam_r
)(const char *name
,
516 struct passwd
*result
,
520 typedef NSS_STATUS (*__nss_getpwuid_r
)(uid_t uid
,
521 struct passwd
*result
,
525 typedef NSS_STATUS (*__nss_setpwent
)(void);
526 typedef NSS_STATUS (*__nss_getpwent_r
)(struct passwd
*result
,
530 typedef NSS_STATUS (*__nss_endpwent
)(void);
531 typedef NSS_STATUS (*__nss_initgroups
)(const char *user
,
538 typedef NSS_STATUS (*__nss_getgrnam_r
)(const char *name
,
539 struct group
*result
,
543 typedef NSS_STATUS (*__nss_getgrgid_r
)(gid_t gid
,
544 struct group
*result
,
548 typedef NSS_STATUS (*__nss_setgrent
)(void);
549 typedef NSS_STATUS (*__nss_getgrent_r
)(struct group
*result
,
553 typedef NSS_STATUS (*__nss_endgrent
)(void);
554 typedef NSS_STATUS (*__nss_gethostbyaddr_r
)(const void *addr
,
557 struct hostent
*result
,
562 typedef NSS_STATUS (*__nss_gethostbyname2_r
)(const char *name
,
564 struct hostent
*result
,
570 #define NWRAP_NSS_MODULE_SYMBOL_ENTRY(i) \
576 struct nwrap_nss_module_symbols
{
577 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getpwnam_r
);
578 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getpwuid_r
);
579 NWRAP_NSS_MODULE_SYMBOL_ENTRY(setpwent
);
580 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getpwent_r
);
581 NWRAP_NSS_MODULE_SYMBOL_ENTRY(endpwent
);
583 NWRAP_NSS_MODULE_SYMBOL_ENTRY(initgroups
);
584 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getgrnam_r
);
585 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getgrgid_r
);
586 NWRAP_NSS_MODULE_SYMBOL_ENTRY(setgrent
);
587 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getgrent_r
);
588 NWRAP_NSS_MODULE_SYMBOL_ENTRY(endgrent
);
590 NWRAP_NSS_MODULE_SYMBOL_ENTRY(gethostbyaddr_r
);
591 NWRAP_NSS_MODULE_SYMBOL_ENTRY(gethostbyname2_r
);
594 struct nwrap_backend
{
598 struct nwrap_ops
*ops
;
599 struct nwrap_nss_module_symbols
*symbols
;
605 struct passwd
* (*nw_getpwnam
)(struct nwrap_backend
*b
,
607 int (*nw_getpwnam_r
)(struct nwrap_backend
*b
,
608 const char *name
, struct passwd
*pwdst
,
609 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
610 struct passwd
* (*nw_getpwuid
)(struct nwrap_backend
*b
,
612 int (*nw_getpwuid_r
)(struct nwrap_backend
*b
,
613 uid_t uid
, struct passwd
*pwdst
,
614 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
615 void (*nw_setpwent
)(struct nwrap_backend
*b
);
616 struct passwd
* (*nw_getpwent
)(struct nwrap_backend
*b
);
617 int (*nw_getpwent_r
)(struct nwrap_backend
*b
,
618 struct passwd
*pwdst
, char *buf
,
619 size_t buflen
, struct passwd
**pwdstp
);
620 void (*nw_endpwent
)(struct nwrap_backend
*b
);
621 int (*nw_initgroups
)(struct nwrap_backend
*b
,
622 const char *user
, gid_t group
);
623 struct group
* (*nw_getgrnam
)(struct nwrap_backend
*b
,
625 int (*nw_getgrnam_r
)(struct nwrap_backend
*b
,
626 const char *name
, struct group
*grdst
,
627 char *buf
, size_t buflen
, struct group
**grdstp
);
628 struct group
* (*nw_getgrgid
)(struct nwrap_backend
*b
,
630 int (*nw_getgrgid_r
)(struct nwrap_backend
*b
,
631 gid_t gid
, struct group
*grdst
,
632 char *buf
, size_t buflen
, struct group
**grdstp
);
633 void (*nw_setgrent
)(struct nwrap_backend
*b
);
634 struct group
* (*nw_getgrent
)(struct nwrap_backend
*b
);
635 int (*nw_getgrent_r
)(struct nwrap_backend
*b
,
636 struct group
*grdst
, char *buf
,
637 size_t buflen
, struct group
**grdstp
);
638 void (*nw_endgrent
)(struct nwrap_backend
*b
);
639 struct hostent
*(*nw_gethostbyaddr
)(struct nwrap_backend
*b
,
641 socklen_t len
, int type
);
642 struct hostent
*(*nw_gethostbyname
)(struct nwrap_backend
*b
,
644 struct hostent
*(*nw_gethostbyname2
)(struct nwrap_backend
*b
,
645 const char *name
, int af
);
646 int (*nw_gethostbyname2_r
)(struct nwrap_backend
*b
,
647 const char *name
, int af
,
648 struct hostent
*hedst
,
649 char *buf
, size_t buflen
,
650 struct hostent
**hedstp
);
653 /* Public prototypes */
655 bool nss_wrapper_enabled(void);
656 bool nss_wrapper_shadow_enabled(void);
657 bool nss_wrapper_hosts_enabled(void);
659 /* prototypes for files backend */
662 static struct passwd
*nwrap_files_getpwnam(struct nwrap_backend
*b
,
664 static int nwrap_files_getpwnam_r(struct nwrap_backend
*b
,
665 const char *name
, struct passwd
*pwdst
,
666 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
667 static struct passwd
*nwrap_files_getpwuid(struct nwrap_backend
*b
,
669 static int nwrap_files_getpwuid_r(struct nwrap_backend
*b
,
670 uid_t uid
, struct passwd
*pwdst
,
671 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
672 static void nwrap_files_setpwent(struct nwrap_backend
*b
);
673 static struct passwd
*nwrap_files_getpwent(struct nwrap_backend
*b
);
674 static int nwrap_files_getpwent_r(struct nwrap_backend
*b
,
675 struct passwd
*pwdst
, char *buf
,
676 size_t buflen
, struct passwd
**pwdstp
);
677 static void nwrap_files_endpwent(struct nwrap_backend
*b
);
678 static int nwrap_files_initgroups(struct nwrap_backend
*b
,
679 const char *user
, gid_t group
);
680 static struct group
*nwrap_files_getgrnam(struct nwrap_backend
*b
,
682 static int nwrap_files_getgrnam_r(struct nwrap_backend
*b
,
683 const char *name
, struct group
*grdst
,
684 char *buf
, size_t buflen
, struct group
**grdstp
);
685 static struct group
*nwrap_files_getgrgid(struct nwrap_backend
*b
,
687 static int nwrap_files_getgrgid_r(struct nwrap_backend
*b
,
688 gid_t gid
, struct group
*grdst
,
689 char *buf
, size_t buflen
, struct group
**grdstp
);
690 static void nwrap_files_setgrent(struct nwrap_backend
*b
);
691 static struct group
*nwrap_files_getgrent(struct nwrap_backend
*b
);
692 static int nwrap_files_getgrent_r(struct nwrap_backend
*b
,
693 struct group
*grdst
, char *buf
,
694 size_t buflen
, struct group
**grdstp
);
695 static void nwrap_files_endgrent(struct nwrap_backend
*b
);
696 static struct hostent
*nwrap_files_gethostbyaddr(struct nwrap_backend
*b
,
698 socklen_t len
, int type
);
699 static struct hostent
*nwrap_files_gethostbyname(struct nwrap_backend
*b
,
701 #ifdef HAVE_GETHOSTBYNAME2
702 static struct hostent
*nwrap_files_gethostbyname2(struct nwrap_backend
*b
,
703 const char *name
, int af
);
704 #endif /* HAVE_GETHOSTBYNAME2 */
705 static int nwrap_files_gethostbyname2_r(struct nwrap_backend
*b
,
706 const char *name
, int af
,
707 struct hostent
*hedst
,
708 char *buf
, size_t buflen
,
709 struct hostent
**hedstp
);
711 /* prototypes for module backend */
713 static struct passwd
*nwrap_module_getpwent(struct nwrap_backend
*b
);
714 static int nwrap_module_getpwent_r(struct nwrap_backend
*b
,
715 struct passwd
*pwdst
, char *buf
,
716 size_t buflen
, struct passwd
**pwdstp
);
717 static struct passwd
*nwrap_module_getpwnam(struct nwrap_backend
*b
,
719 static int nwrap_module_getpwnam_r(struct nwrap_backend
*b
,
720 const char *name
, struct passwd
*pwdst
,
721 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
722 static struct passwd
*nwrap_module_getpwuid(struct nwrap_backend
*b
,
724 static int nwrap_module_getpwuid_r(struct nwrap_backend
*b
,
725 uid_t uid
, struct passwd
*pwdst
,
726 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
727 static void nwrap_module_setpwent(struct nwrap_backend
*b
);
728 static void nwrap_module_endpwent(struct nwrap_backend
*b
);
729 static struct group
*nwrap_module_getgrent(struct nwrap_backend
*b
);
730 static int nwrap_module_getgrent_r(struct nwrap_backend
*b
,
731 struct group
*grdst
, char *buf
,
732 size_t buflen
, struct group
**grdstp
);
733 static struct group
*nwrap_module_getgrnam(struct nwrap_backend
*b
,
735 static int nwrap_module_getgrnam_r(struct nwrap_backend
*b
,
736 const char *name
, struct group
*grdst
,
737 char *buf
, size_t buflen
, struct group
**grdstp
);
738 static struct group
*nwrap_module_getgrgid(struct nwrap_backend
*b
,
740 static int nwrap_module_getgrgid_r(struct nwrap_backend
*b
,
741 gid_t gid
, struct group
*grdst
,
742 char *buf
, size_t buflen
, struct group
**grdstp
);
743 static void nwrap_module_setgrent(struct nwrap_backend
*b
);
744 static void nwrap_module_endgrent(struct nwrap_backend
*b
);
745 static int nwrap_module_initgroups(struct nwrap_backend
*b
,
746 const char *user
, gid_t group
);
747 static struct hostent
*nwrap_module_gethostbyaddr(struct nwrap_backend
*b
,
749 socklen_t len
, int type
);
750 static struct hostent
*nwrap_module_gethostbyname(struct nwrap_backend
*b
,
752 static struct hostent
*nwrap_module_gethostbyname2(struct nwrap_backend
*b
,
753 const char *name
, int af
);
754 static int nwrap_module_gethostbyname2_r(struct nwrap_backend
*b
,
755 const char *name
, int af
,
756 struct hostent
*hedst
,
757 char *buf
, size_t buflen
,
758 struct hostent
**hedstp
);
760 struct nwrap_ops nwrap_files_ops
= {
761 .nw_getpwnam
= nwrap_files_getpwnam
,
762 .nw_getpwnam_r
= nwrap_files_getpwnam_r
,
763 .nw_getpwuid
= nwrap_files_getpwuid
,
764 .nw_getpwuid_r
= nwrap_files_getpwuid_r
,
765 .nw_setpwent
= nwrap_files_setpwent
,
766 .nw_getpwent
= nwrap_files_getpwent
,
767 .nw_getpwent_r
= nwrap_files_getpwent_r
,
768 .nw_endpwent
= nwrap_files_endpwent
,
769 .nw_initgroups
= nwrap_files_initgroups
,
770 .nw_getgrnam
= nwrap_files_getgrnam
,
771 .nw_getgrnam_r
= nwrap_files_getgrnam_r
,
772 .nw_getgrgid
= nwrap_files_getgrgid
,
773 .nw_getgrgid_r
= nwrap_files_getgrgid_r
,
774 .nw_setgrent
= nwrap_files_setgrent
,
775 .nw_getgrent
= nwrap_files_getgrent
,
776 .nw_getgrent_r
= nwrap_files_getgrent_r
,
777 .nw_endgrent
= nwrap_files_endgrent
,
778 .nw_gethostbyaddr
= nwrap_files_gethostbyaddr
,
779 .nw_gethostbyname
= nwrap_files_gethostbyname
,
780 #ifdef HAVE_GETHOSTBYNAME2
781 .nw_gethostbyname2
= nwrap_files_gethostbyname2
,
782 #endif /* HAVE_GETHOSTBYNAME2 */
783 .nw_gethostbyname2_r
= nwrap_files_gethostbyname2_r
,
786 struct nwrap_ops nwrap_module_ops
= {
787 .nw_getpwnam
= nwrap_module_getpwnam
,
788 .nw_getpwnam_r
= nwrap_module_getpwnam_r
,
789 .nw_getpwuid
= nwrap_module_getpwuid
,
790 .nw_getpwuid_r
= nwrap_module_getpwuid_r
,
791 .nw_setpwent
= nwrap_module_setpwent
,
792 .nw_getpwent
= nwrap_module_getpwent
,
793 .nw_getpwent_r
= nwrap_module_getpwent_r
,
794 .nw_endpwent
= nwrap_module_endpwent
,
795 .nw_initgroups
= nwrap_module_initgroups
,
796 .nw_getgrnam
= nwrap_module_getgrnam
,
797 .nw_getgrnam_r
= nwrap_module_getgrnam_r
,
798 .nw_getgrgid
= nwrap_module_getgrgid
,
799 .nw_getgrgid_r
= nwrap_module_getgrgid_r
,
800 .nw_setgrent
= nwrap_module_setgrent
,
801 .nw_getgrent
= nwrap_module_getgrent
,
802 .nw_getgrent_r
= nwrap_module_getgrent_r
,
803 .nw_endgrent
= nwrap_module_endgrent
,
804 .nw_gethostbyaddr
= nwrap_module_gethostbyaddr
,
805 .nw_gethostbyname
= nwrap_module_gethostbyname
,
806 .nw_gethostbyname2
= nwrap_module_gethostbyname2
,
807 .nw_gethostbyname2_r
= nwrap_module_gethostbyname2_r
,
814 struct nwrap_libc_symbols symbols
;
819 struct nwrap_backend
*backends
;
820 struct nwrap_libc
*libc
;
823 static struct nwrap_main
*nwrap_main_global
;
824 static struct nwrap_main __nwrap_main_global
;
829 static int nwrap_convert_he_ai(const struct hostent
*he
,
831 const struct addrinfo
*hints
,
832 struct addrinfo
**pai
,
833 bool skip_canonname
);
839 #define DEFAULT_VECTOR_CAPACITY 16
841 struct nwrap_vector
{
847 /* Macro returns pointer to first element of vector->items array.
849 * nwrap_vector is used as a memory backend which take care of
850 * memory allocations and other stuff like memory growing.
851 * nwrap_vectors should not be considered as some abstract structures.
852 * On this level, vectors are more handy than direct realloc/malloc
855 * nwrap_vector->items is array inside nwrap_vector which can be
856 * directly pointed by libc structure assembled by cwrap itself.
860 * 1) struct hostent contains char **h_addr_list element.
861 * 2) nwrap_vector holds array of pointers to addresses.
862 * It's easier to use vector to store results of
865 * Now, pretend that cwrap assembled struct hostent and
866 * we need to set h_addr_list to point to nwrap_vector.
867 * Idea behind is to shield users from internal nwrap_vector
869 * (Yes, not fully - array terminated by NULL is needed because
870 * it's result expected by libc function caller.)
876 * struct nwrap_vector *vector = malloc(sizeof(struct nwrap_vector));
877 * ... don't care about failed allocation now ...
879 * ... fill nwrap vector ...
882 * he.h_addr_list = nwrap_vector_head(vector);
885 #define nwrap_vector_head(vect) ((void *)((vect)->items))
887 #define nwrap_vector_foreach(item, vect, iter) \
888 for (iter = 0, (item) = (vect).items == NULL ? NULL : (vect).items[0]; \
890 (item) = (vect).items[++iter])
892 #define nwrap_vector_is_initialized(vector) ((vector)->items != NULL)
894 static inline bool nwrap_vector_init(struct nwrap_vector
*const vector
)
896 if (vector
== NULL
) {
900 /* count is initialized by ZERO_STRUCTP */
901 ZERO_STRUCTP(vector
);
902 vector
->items
= malloc(sizeof(void *) * (DEFAULT_VECTOR_CAPACITY
+ 1));
903 if (vector
->items
== NULL
) {
906 vector
->capacity
= DEFAULT_VECTOR_CAPACITY
;
907 memset(vector
->items
, '\0', sizeof(void *) * (DEFAULT_VECTOR_CAPACITY
+ 1));
912 static bool nwrap_vector_add_item(struct nwrap_vector
*vector
, void *const item
)
914 assert (vector
!= NULL
);
916 if (vector
->items
== NULL
) {
917 nwrap_vector_init(vector
);
920 if (vector
->count
== vector
->capacity
) {
921 /* Items array _MUST_ be NULL terminated because it's passed
922 * as result to caller which expect NULL terminated array from libc.
924 void **items
= realloc(vector
->items
, sizeof(void *) * ((vector
->capacity
* 2) + 1));
928 vector
->items
= items
;
930 /* Don't count ending NULL to capacity */
931 vector
->capacity
*= 2;
934 vector
->items
[vector
->count
] = item
;
937 vector
->items
[vector
->count
] = NULL
;
942 static bool nwrap_vector_merge(struct nwrap_vector
*dst
,
943 struct nwrap_vector
*src
)
945 void **dst_items
= NULL
;
948 if (src
->count
== 0) {
952 count
= dst
->count
+ src
->count
;
954 /* We don't need reallocation if we have enough capacity. */
955 if (src
->count
> (dst
->capacity
- dst
->count
)) {
956 dst_items
= (void **)realloc(dst
->items
, (count
+ 1) * sizeof(void *));
957 if (dst_items
== NULL
) {
960 dst
->items
= dst_items
;
961 dst
->capacity
= count
;
964 memcpy((void *)(((long *)dst
->items
) + dst
->count
),
966 src
->count
* sizeof(void *));
979 struct nwrap_vector lines
;
981 bool (*parse_line
)(struct nwrap_cache
*, char *line
);
982 void (*unload
)(struct nwrap_cache
*);
987 struct nwrap_cache
*cache
;
994 struct nwrap_cache __nwrap_cache_pw
;
995 struct nwrap_pw nwrap_pw_global
;
997 static bool nwrap_pw_parse_line(struct nwrap_cache
*nwrap
, char *line
);
998 static void nwrap_pw_unload(struct nwrap_cache
*nwrap
);
1001 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1003 struct nwrap_cache
*cache
;
1010 struct nwrap_cache __nwrap_cache_sp
;
1011 struct nwrap_sp nwrap_sp_global
;
1013 static bool nwrap_sp_parse_line(struct nwrap_cache
*nwrap
, char *line
);
1014 static void nwrap_sp_unload(struct nwrap_cache
*nwrap
);
1015 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1019 struct nwrap_cache
*cache
;
1026 struct nwrap_cache __nwrap_cache_gr
;
1027 struct nwrap_gr nwrap_gr_global
;
1030 static bool nwrap_he_parse_line(struct nwrap_cache
*nwrap
, char *line
);
1031 static void nwrap_he_unload(struct nwrap_cache
*nwrap
);
1033 struct nwrap_addrdata
{
1034 unsigned char host_addr
[16]; /* IPv4 or IPv6 address */
1037 static size_t max_hostents
= 100;
1039 struct nwrap_entdata
{
1040 struct nwrap_addrdata addr
;
1043 struct nwrap_vector nwrap_addrdata
;
1045 ssize_t aliases_count
;
1048 struct nwrap_entlist
{
1049 struct nwrap_entlist
*next
;
1050 struct nwrap_entdata
*ed
;
1054 struct nwrap_cache
*cache
;
1056 struct nwrap_vector entries
;
1057 struct nwrap_vector lists
;
1063 static struct nwrap_cache __nwrap_cache_he
;
1064 static struct nwrap_he nwrap_he_global
;
1067 /*********************************************************
1069 *********************************************************/
1071 static bool nwrap_gr_parse_line(struct nwrap_cache
*nwrap
, char *line
);
1072 static void nwrap_gr_unload(struct nwrap_cache
*nwrap
);
1073 #if ! defined(HAVE_CONSTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_INIT)
1074 /* xlC and other oldschool compilers support (only) this */
1075 #pragma init (nwrap_constructor)
1077 void nwrap_constructor(void) CONSTRUCTOR_ATTRIBUTE
;
1078 #if ! defined(HAVE_DESTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_FINI)
1079 #pragma fini (nwrap_destructor)
1081 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE
;
1083 /*********************************************************
1084 * NWRAP LIBC LOADER FUNCTIONS
1085 *********************************************************/
1093 static const char *nwrap_str_lib(enum nwrap_lib lib
)
1100 case NWRAP_LIBSOCKET
:
1104 /* Compiler would warn us about unhandled enum value if we get here */
1108 static void *nwrap_load_lib_handle(enum nwrap_lib lib
)
1110 int flags
= RTLD_LAZY
;
1111 void *handle
= NULL
;
1114 #ifdef RTLD_DEEPBIND
1115 const char *env_preload
= getenv("LD_PRELOAD");
1116 const char *env_deepbind
= getenv("NSS_WRAPPER_DISABLE_DEEPBIND");
1117 bool enable_deepbind
= true;
1119 /* Don't do a deepbind if we run with libasan */
1120 if (env_preload
!= NULL
&& strlen(env_preload
) < 1024) {
1121 const char *p
= strstr(env_preload
, "libasan.so");
1123 enable_deepbind
= false;
1127 if (env_deepbind
!= NULL
&& strlen(env_deepbind
) >= 1) {
1128 enable_deepbind
= false;
1131 if (enable_deepbind
) {
1132 flags
|= RTLD_DEEPBIND
;
1139 handle
= nwrap_main_global
->libc
->nsl_handle
;
1140 if (handle
== NULL
) {
1141 for (i
= 10; i
>= 0; i
--) {
1142 char soname
[256] = {0};
1144 snprintf(soname
, sizeof(soname
), "libnsl.so.%d", i
);
1145 handle
= dlopen(soname
, flags
);
1146 if (handle
!= NULL
) {
1151 nwrap_main_global
->libc
->nsl_handle
= handle
;
1156 case NWRAP_LIBSOCKET
:
1157 #ifdef HAVE_LIBSOCKET
1158 handle
= nwrap_main_global
->libc
->sock_handle
;
1159 if (handle
== NULL
) {
1160 for (i
= 10; i
>= 0; i
--) {
1161 char soname
[256] = {0};
1163 snprintf(soname
, sizeof(soname
), "libsocket.so.%d", i
);
1164 handle
= dlopen(soname
, flags
);
1165 if (handle
!= NULL
) {
1170 nwrap_main_global
->libc
->sock_handle
= handle
;
1176 handle
= nwrap_main_global
->libc
->handle
;
1177 if (handle
== NULL
) {
1178 for (i
= 10; i
>= 0; i
--) {
1179 char soname
[256] = {0};
1181 snprintf(soname
, sizeof(soname
), "libc.so.%d", i
);
1182 handle
= dlopen(soname
, flags
);
1183 if (handle
!= NULL
) {
1188 nwrap_main_global
->libc
->handle
= handle
;
1193 if (handle
== NULL
) {
1195 handle
= nwrap_main_global
->libc
->handle
1196 = nwrap_main_global
->libc
->sock_handle
1197 = nwrap_main_global
->libc
->nsl_handle
1200 NWRAP_LOG(NWRAP_LOG_ERROR
,
1201 "Failed to dlopen library: %s\n",
1210 static void *_nwrap_bind_symbol(enum nwrap_lib lib
, const char *fn_name
)
1217 handle
= nwrap_load_lib_handle(lib
);
1219 func
= dlsym(handle
, fn_name
);
1221 NWRAP_LOG(NWRAP_LOG_ERROR
,
1222 "Failed to find %s: %s\n",
1223 fn_name
, dlerror());
1227 NWRAP_LOG(NWRAP_LOG_TRACE
,
1228 "Loaded %s from %s",
1229 fn_name
, nwrap_str_lib(lib
));
1233 #define nwrap_bind_symbol_libc(sym_name) \
1234 NWRAP_LOCK(libc_symbol_binding); \
1235 if (nwrap_main_global->libc->symbols._libc_##sym_name.obj == NULL) { \
1236 nwrap_main_global->libc->symbols._libc_##sym_name.obj = \
1237 _nwrap_bind_symbol(NWRAP_LIBC, #sym_name); \
1239 NWRAP_UNLOCK(libc_symbol_binding)
1241 #define nwrap_bind_symbol_libc_posix(sym_name) \
1242 NWRAP_LOCK(libc_symbol_binding); \
1243 if (nwrap_main_global->libc->symbols._libc_##sym_name.obj == NULL) { \
1244 nwrap_main_global->libc->symbols._libc_##sym_name.obj = \
1245 _nwrap_bind_symbol(NWRAP_LIBC, "__posix_" #sym_name); \
1247 NWRAP_UNLOCK(libc_symbol_binding)
1249 #define nwrap_bind_symbol_libnsl(sym_name) \
1250 NWRAP_LOCK(libc_symbol_binding); \
1251 if (nwrap_main_global->libc->symbols._libc_##sym_name.obj == NULL) { \
1252 nwrap_main_global->libc->symbols._libc_##sym_name.obj = \
1253 _nwrap_bind_symbol(NWRAP_LIBNSL, #sym_name); \
1255 NWRAP_UNLOCK(libc_symbol_binding)
1257 #define nwrap_bind_symbol_libsocket(sym_name) \
1258 NWRAP_LOCK(libc_symbol_binding); \
1259 if (nwrap_main_global->libc->symbols._libc_##sym_name.obj == NULL) { \
1260 nwrap_main_global->libc->symbols._libc_##sym_name.obj = \
1261 _nwrap_bind_symbol(NWRAP_LIBSOCKET, #sym_name); \
1263 NWRAP_UNLOCK(libc_symbol_binding)
1265 /* INTERNAL HELPER FUNCTIONS */
1266 static void nwrap_lines_unload(struct nwrap_cache
*const nwrap
)
1270 nwrap_vector_foreach(item
, nwrap
->lines
, p
) {
1271 /* Maybe some vectors were merged ... */
1274 SAFE_FREE(nwrap
->lines
.items
);
1275 ZERO_STRUCTP(&nwrap
->lines
);
1281 * Functions expeciall from libc need to be loaded individually, you can't load
1282 * all at once or gdb will segfault at startup. The same applies to valgrind and
1283 * has probably something todo with with the linker.
1284 * So we need load each function at the point it is called the first time.
1286 static struct passwd
*libc_getpwnam(const char *name
)
1288 nwrap_bind_symbol_libc(getpwnam
);
1290 return nwrap_main_global
->libc
->symbols
._libc_getpwnam
.f(name
);
1293 #ifdef HAVE_GETPWNAM_R
1294 static int libc_getpwnam_r(const char *name
,
1298 struct passwd
**result
)
1300 #ifdef HAVE___POSIX_GETPWNAM_R
1301 nwrap_bind_symbol_libc_posix(getpwnam_r
);
1303 nwrap_bind_symbol_libc(getpwnam_r
);
1306 return nwrap_main_global
->libc
->symbols
._libc_getpwnam_r
.f(name
,
1314 static struct passwd
*libc_getpwuid(uid_t uid
)
1316 nwrap_bind_symbol_libc(getpwuid
);
1318 return nwrap_main_global
->libc
->symbols
._libc_getpwuid
.f(uid
);
1321 #ifdef HAVE_GETPWUID_R
1322 static int libc_getpwuid_r(uid_t uid
,
1326 struct passwd
**result
)
1328 #ifdef HAVE___POSIX_GETPWUID_R
1329 nwrap_bind_symbol_libc_posix(getpwuid_r
);
1331 nwrap_bind_symbol_libc(getpwuid_r
);
1334 return nwrap_main_global
->libc
->symbols
._libc_getpwuid_r
.f(uid
,
1342 static inline void str_tolower(char *dst
, char *src
)
1344 register char *src_tmp
= src
;
1345 register char *dst_tmp
= dst
;
1347 while (*src_tmp
!= '\0') {
1348 *dst_tmp
= tolower(*src_tmp
);
1354 static bool str_tolower_copy(char **dst_name
, const char *const src_name
)
1358 if ((dst_name
== NULL
) || (src_name
== NULL
)) {
1362 h_name_lower
= strdup(src_name
);
1363 if (h_name_lower
== NULL
) {
1364 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Out of memory while strdup");
1368 str_tolower(h_name_lower
, h_name_lower
);
1369 *dst_name
= h_name_lower
;
1373 static void libc_setpwent(void)
1375 nwrap_bind_symbol_libc(setpwent
);
1377 nwrap_main_global
->libc
->symbols
._libc_setpwent
.f();
1380 static struct passwd
*libc_getpwent(void)
1382 nwrap_bind_symbol_libc(getpwent
);
1384 return nwrap_main_global
->libc
->symbols
._libc_getpwent
.f();
1387 #ifdef HAVE_GETPWENT_R
1388 # ifdef HAVE_SOLARIS_GETPWENT_R
1389 static struct passwd
*libc_getpwent_r(struct passwd
*pwdst
,
1393 nwrap_bind_symbol_libc(getpwent_r
);
1395 return nwrap_main_global
->libc
->symbols
._libc_getpwent_r
.f(pwdst
,
1399 # else /* HAVE_SOLARIS_GETPWENT_R */
1400 static int libc_getpwent_r(struct passwd
*pwdst
,
1403 struct passwd
**pwdstp
)
1405 nwrap_bind_symbol_libc(getpwent_r
);
1407 return nwrap_main_global
->libc
->symbols
._libc_getpwent_r
.f(pwdst
,
1412 # endif /* HAVE_SOLARIS_GETPWENT_R */
1413 #endif /* HAVE_GETPWENT_R */
1415 static void libc_endpwent(void)
1417 nwrap_bind_symbol_libc(endpwent
);
1419 nwrap_main_global
->libc
->symbols
._libc_endpwent
.f();
1422 static int libc_initgroups(const char *user
, gid_t gid
)
1424 nwrap_bind_symbol_libc(initgroups
);
1426 return nwrap_main_global
->libc
->symbols
._libc_initgroups
.f(user
, gid
);
1429 static struct group
*libc_getgrnam(const char *name
)
1431 nwrap_bind_symbol_libc(getgrnam
);
1433 return nwrap_main_global
->libc
->symbols
._libc_getgrnam
.f(name
);
1436 #ifdef HAVE_GETGRNAM_R
1437 static int libc_getgrnam_r(const char *name
,
1441 struct group
**result
)
1443 #ifdef HAVE___POSIX_GETGRNAM_R
1444 nwrap_bind_symbol_libc_posix(getgrnam_r
);
1446 nwrap_bind_symbol_libc(getgrnam_r
);
1449 return nwrap_main_global
->libc
->symbols
._libc_getgrnam_r
.f(name
,
1457 static struct group
*libc_getgrgid(gid_t gid
)
1459 nwrap_bind_symbol_libc(getgrgid
);
1461 return nwrap_main_global
->libc
->symbols
._libc_getgrgid
.f(gid
);
1464 #ifdef HAVE_GETGRGID_R
1465 static int libc_getgrgid_r(gid_t gid
,
1469 struct group
**result
)
1471 #ifdef HAVE___POSIX_GETGRGID_R
1472 if (nwrap_main_global
->libc
->symbols
._libc_getgrgid_r
== NULL
) {
1473 *(void **) (&nwrap_main_global
->libc
->symbols
._libc_getgrgid_r
) =
1474 _nwrap_bind_symbol_libc("__posix_getgrgid_r");
1477 nwrap_bind_symbol_libc(getgrgid_r
);
1480 return nwrap_main_global
->libc
->symbols
._libc_getgrgid_r
.f(gid
,
1488 static void libc_setgrent(void)
1490 nwrap_bind_symbol_libc(setgrent
);
1492 nwrap_main_global
->libc
->symbols
._libc_setgrent
.f();
1495 static struct group
*libc_getgrent(void)
1497 nwrap_bind_symbol_libc(getgrent
);
1499 return nwrap_main_global
->libc
->symbols
._libc_getgrent
.f();
1502 #ifdef HAVE_GETGRENT_R
1503 # ifdef HAVE_SOLARIS_GETGRENT_R
1504 static struct group
*libc_getgrent_r(struct group
*group
,
1508 nwrap_bind_symbol_libc(getgrent_r
);
1510 return nwrap_main_global
->libc
->symbols
._libc_getgrent_r
.f(group
,
1514 # else /* HAVE_SOLARIS_GETGRENT_R */
1515 static int libc_getgrent_r(struct group
*group
,
1518 struct group
**result
)
1520 nwrap_bind_symbol_libc(getgrent_r
);
1522 return nwrap_main_global
->libc
->symbols
._libc_getgrent_r
.f(group
,
1527 # endif /* HAVE_SOLARIS_GETGRENT_R */
1528 #endif /* HAVE_GETGRENT_R */
1530 static void libc_endgrent(void)
1532 nwrap_bind_symbol_libc(endgrent
);
1534 nwrap_main_global
->libc
->symbols
._libc_endgrent
.f();
1537 #ifdef HAVE_GETGROUPLIST
1538 static int libc_getgrouplist(const char *user
,
1543 nwrap_bind_symbol_libc(getgrouplist
);
1545 return nwrap_main_global
->libc
->symbols
._libc_getgrouplist
.f(user
,
1552 static void libc_sethostent(int stayopen
)
1554 nwrap_bind_symbol_libnsl(sethostent
);
1556 nwrap_main_global
->libc
->symbols
._libc_sethostent
.f(stayopen
);
1559 static struct hostent
*libc_gethostent(void)
1561 nwrap_bind_symbol_libnsl(gethostent
);
1563 return nwrap_main_global
->libc
->symbols
._libc_gethostent
.f();
1566 static void libc_endhostent(void)
1568 nwrap_bind_symbol_libnsl(endhostent
);
1570 nwrap_main_global
->libc
->symbols
._libc_endhostent
.f();
1573 static struct hostent
*libc_gethostbyname(const char *name
)
1575 nwrap_bind_symbol_libnsl(gethostbyname
);
1577 return nwrap_main_global
->libc
->symbols
._libc_gethostbyname
.f(name
);
1580 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
1581 static struct hostent
*libc_gethostbyname2(const char *name
, int af
)
1583 nwrap_bind_symbol_libnsl(gethostbyname2
);
1585 return nwrap_main_global
->libc
->symbols
._libc_gethostbyname2
.f(name
, af
);
1589 #ifdef HAVE_GETHOSTBYNAME2_R /* GNU extension */
1590 static int libc_gethostbyname2_r(const char *name
,
1592 struct hostent
*ret
,
1595 struct hostent
**result
,
1598 nwrap_bind_symbol_libnsl(gethostbyname2_r
);
1600 return nwrap_main_global
->libc
->symbols
._libc_gethostbyname2_r
.f(name
,
1610 static struct hostent
*libc_gethostbyaddr(const void *addr
,
1614 nwrap_bind_symbol_libnsl(gethostbyaddr
);
1616 return nwrap_main_global
->libc
->symbols
._libc_gethostbyaddr
.f(addr
,
1621 static int libc_gethostname(char *name
, size_t len
)
1623 nwrap_bind_symbol_libnsl(gethostname
);
1625 return nwrap_main_global
->libc
->symbols
._libc_gethostname
.f(name
, len
);
1628 #ifdef HAVE_GETHOSTBYNAME_R
1629 static int libc_gethostbyname_r(const char *name
,
1630 struct hostent
*ret
,
1633 struct hostent
**result
,
1636 nwrap_bind_symbol_libnsl(gethostbyname_r
);
1638 return nwrap_main_global
->libc
->symbols
._libc_gethostbyname_r
.f(name
,
1647 #ifdef HAVE_GETHOSTBYADDR_R
1648 static int libc_gethostbyaddr_r(const void *addr
,
1651 struct hostent
*ret
,
1654 struct hostent
**result
,
1657 nwrap_bind_symbol_libnsl(gethostbyaddr_r
);
1659 return nwrap_main_global
->libc
->symbols
._libc_gethostbyaddr_r
.f(addr
,
1670 static int libc_getaddrinfo(const char *node
,
1671 const char *service
,
1672 const struct addrinfo
*hints
,
1673 struct addrinfo
**res
)
1675 nwrap_bind_symbol_libsocket(getaddrinfo
);
1677 return nwrap_main_global
->libc
->symbols
._libc_getaddrinfo
.f(node
,
1683 static int libc_getnameinfo(const struct sockaddr
*sa
,
1691 nwrap_bind_symbol_libsocket(getnameinfo
);
1693 return nwrap_main_global
->libc
->symbols
._libc_getnameinfo
.f(sa
,
1702 /*********************************************************
1703 * NWRAP NSS MODULE LOADER FUNCTIONS
1704 *********************************************************/
1706 static void *_nwrap_bind_nss_module_symbol(struct nwrap_backend
*b
,
1707 const char *fn_name
)
1713 if (b
->so_handle
== NULL
) {
1714 NWRAP_LOG(NWRAP_LOG_ERROR
, "No handle");
1718 rc
= asprintf(&s
, "_nss_%s_%s", b
->name
, fn_name
);
1720 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1724 res
= dlsym(b
->so_handle
, s
);
1726 NWRAP_LOG(NWRAP_LOG_WARN
,
1727 "Cannot find function %s in %s",
1734 #define nwrap_nss_module_bind_symbol(sym_name) \
1735 NWRAP_LOCK(nss_module_symbol_binding); \
1736 if (symbols->_nss_##sym_name.obj == NULL) { \
1737 symbols->_nss_##sym_name.obj = \
1738 _nwrap_bind_nss_module_symbol(b, #sym_name); \
1740 NWRAP_UNLOCK(nss_module_symbol_binding)
1742 #define nwrap_nss_module_bind_symbol2(sym_name, alt_name) \
1743 NWRAP_LOCK(nss_module_symbol_binding); \
1744 if (symbols->_nss_##sym_name.obj == NULL) { \
1745 symbols->_nss_##sym_name.obj = \
1746 _nwrap_bind_nss_module_symbol(b, #alt_name); \
1748 NWRAP_UNLOCK(nss_module_symbol_binding)
1750 static struct nwrap_nss_module_symbols
*
1751 nwrap_bind_nss_module_symbols(struct nwrap_backend
*b
)
1753 struct nwrap_nss_module_symbols
*symbols
;
1755 if (!b
->so_handle
) {
1759 symbols
= calloc(1, sizeof(struct nwrap_nss_module_symbols
));
1760 if (symbols
== NULL
) {
1764 nwrap_nss_module_bind_symbol(getpwnam_r
);
1765 nwrap_nss_module_bind_symbol(getpwuid_r
);
1766 nwrap_nss_module_bind_symbol(setpwent
);
1767 nwrap_nss_module_bind_symbol(getpwent_r
);
1768 nwrap_nss_module_bind_symbol(endpwent
);
1769 nwrap_nss_module_bind_symbol2(initgroups
, initgroups_dyn
);
1770 nwrap_nss_module_bind_symbol(getgrnam_r
);
1771 nwrap_nss_module_bind_symbol(getgrgid_r
);
1772 nwrap_nss_module_bind_symbol(setgrent
);
1773 nwrap_nss_module_bind_symbol(getgrent_r
);
1774 nwrap_nss_module_bind_symbol(endgrent
);
1775 nwrap_nss_module_bind_symbol(gethostbyaddr_r
);
1776 nwrap_nss_module_bind_symbol(gethostbyname2_r
);
1781 static void *nwrap_load_module(const char *so_path
)
1785 if (!so_path
|| !strlen(so_path
)) {
1789 h
= dlopen(so_path
, RTLD_LAZY
);
1791 NWRAP_LOG(NWRAP_LOG_ERROR
,
1792 "Cannot open shared library %s",
1800 static bool nwrap_module_init(const char *name
,
1801 struct nwrap_ops
*ops
,
1802 const char *so_path
,
1803 size_t *num_backends
,
1804 struct nwrap_backend
**backends
)
1806 struct nwrap_backend
*b
= NULL
;
1807 size_t n
= *num_backends
+ 1;
1809 b
= realloc(*backends
, sizeof(struct nwrap_backend
) * n
);
1811 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1816 b
= &((*backends
)[*num_backends
]);
1818 *b
= (struct nwrap_backend
) {
1824 if (so_path
!= NULL
) {
1825 b
->so_handle
= nwrap_load_module(so_path
);
1826 b
->symbols
= nwrap_bind_nss_module_symbols(b
);
1827 if (b
->symbols
== NULL
) {
1837 static void nwrap_libc_init(struct nwrap_main
*r
)
1839 r
->libc
= calloc(1, sizeof(struct nwrap_libc
));
1840 if (r
->libc
== NULL
) {
1841 printf("Failed to allocate memory for libc");
1846 static void nwrap_backend_init(struct nwrap_main
*r
)
1848 const char *module_so_path
= getenv("NSS_WRAPPER_MODULE_SO_PATH");
1849 const char *module_fn_name
= getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1851 r
->num_backends
= 0;
1854 if (!nwrap_module_init("files", &nwrap_files_ops
, NULL
,
1857 NWRAP_LOG(NWRAP_LOG_ERROR
,
1858 "Failed to initialize 'files' backend");
1862 if (module_so_path
!= NULL
&&
1863 module_so_path
[0] != '\0' &&
1864 module_fn_name
!= NULL
&&
1865 module_fn_name
[0] != '\0') {
1866 if (!nwrap_module_init(module_fn_name
,
1871 NWRAP_LOG(NWRAP_LOG_ERROR
,
1872 "Failed to initialize '%s' backend",
1879 static void nwrap_init(void)
1883 size_t max_hostents_tmp
;
1886 NWRAP_LOCK(nwrap_initialized
);
1887 if (nwrap_initialized
) {
1888 NWRAP_UNLOCK(nwrap_initialized
);
1893 * Still holding nwrap_initialized lock here.
1894 * We don't use NWRAP_(UN)LOCK_ALL macros here because we
1895 * want to avoid overhead when other threads do their job.
1897 NWRAP_LOCK(nwrap_global
);
1898 NWRAP_LOCK(nwrap_gr_global
);
1899 NWRAP_LOCK(nwrap_he_global
);
1900 NWRAP_LOCK(nwrap_pw_global
);
1901 NWRAP_LOCK(nwrap_sp_global
);
1903 nwrap_initialized
= true;
1905 env
= getenv("NSS_WRAPPER_MAX_HOSTENTS");
1907 max_hostents_tmp
= (size_t)strtoul(env
, &endptr
, 10);
1908 if ((*env
== '\0') ||
1909 (*endptr
!= '\0') ||
1910 (max_hostents_tmp
== 0)) {
1911 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1912 "Error parsing NSS_WRAPPER_MAX_HOSTENTS "
1913 "value or value is too small. "
1914 "Using default value: %lu.",
1915 (unsigned long)max_hostents
);
1917 max_hostents
= max_hostents_tmp
;
1920 /* Initialize hash table */
1921 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1922 "Initializing hash table of size %lu items.",
1923 (unsigned long)max_hostents
);
1924 ok
= hcreate(max_hostents
);
1926 NWRAP_LOG(NWRAP_LOG_ERROR
,
1927 "Failed to initialize hash table");
1931 nwrap_main_global
= &__nwrap_main_global
;
1933 nwrap_libc_init(nwrap_main_global
);
1935 nwrap_backend_init(nwrap_main_global
);
1938 nwrap_pw_global
.cache
= &__nwrap_cache_pw
;
1940 nwrap_pw_global
.cache
->path
= getenv("NSS_WRAPPER_PASSWD");
1941 nwrap_pw_global
.cache
->fp
= NULL
;
1942 nwrap_pw_global
.cache
->fd
= -1;
1943 nwrap_pw_global
.cache
->private_data
= &nwrap_pw_global
;
1944 nwrap_pw_global
.cache
->parse_line
= nwrap_pw_parse_line
;
1945 nwrap_pw_global
.cache
->unload
= nwrap_pw_unload
;
1948 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1949 nwrap_sp_global
.cache
= &__nwrap_cache_sp
;
1951 nwrap_sp_global
.cache
->path
= getenv("NSS_WRAPPER_SHADOW");
1952 nwrap_sp_global
.cache
->fp
= NULL
;
1953 nwrap_sp_global
.cache
->fd
= -1;
1954 nwrap_sp_global
.cache
->private_data
= &nwrap_sp_global
;
1955 nwrap_sp_global
.cache
->parse_line
= nwrap_sp_parse_line
;
1956 nwrap_sp_global
.cache
->unload
= nwrap_sp_unload
;
1957 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1960 nwrap_gr_global
.cache
= &__nwrap_cache_gr
;
1962 nwrap_gr_global
.cache
->path
= getenv("NSS_WRAPPER_GROUP");
1963 nwrap_gr_global
.cache
->fp
= NULL
;
1964 nwrap_gr_global
.cache
->fd
= -1;
1965 nwrap_gr_global
.cache
->private_data
= &nwrap_gr_global
;
1966 nwrap_gr_global
.cache
->parse_line
= nwrap_gr_parse_line
;
1967 nwrap_gr_global
.cache
->unload
= nwrap_gr_unload
;
1970 nwrap_he_global
.cache
= &__nwrap_cache_he
;
1972 nwrap_he_global
.cache
->path
= getenv("NSS_WRAPPER_HOSTS");
1973 nwrap_he_global
.cache
->fp
= NULL
;
1974 nwrap_he_global
.cache
->fd
= -1;
1975 nwrap_he_global
.cache
->private_data
= &nwrap_he_global
;
1976 nwrap_he_global
.cache
->parse_line
= nwrap_he_parse_line
;
1977 nwrap_he_global
.cache
->unload
= nwrap_he_unload
;
1979 /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1983 bool nss_wrapper_enabled(void)
1987 if (nwrap_pw_global
.cache
->path
== NULL
||
1988 nwrap_pw_global
.cache
->path
[0] == '\0') {
1991 if (nwrap_gr_global
.cache
->path
== NULL
||
1992 nwrap_gr_global
.cache
->path
[0] == '\0') {
1999 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2000 bool nss_wrapper_shadow_enabled(void)
2004 if (nwrap_sp_global
.cache
->path
== NULL
||
2005 nwrap_sp_global
.cache
->path
[0] == '\0') {
2011 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2013 bool nss_wrapper_hosts_enabled(void)
2017 if (nwrap_he_global
.cache
->path
== NULL
||
2018 nwrap_he_global
.cache
->path
[0] == '\0') {
2025 static bool nwrap_hostname_enabled(void)
2029 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL
) {
2036 static bool nwrap_parse_file(struct nwrap_cache
*nwrap
)
2040 /* Unused but getline needs it */
2044 if (nwrap
->st
.st_size
== 0) {
2045 NWRAP_LOG(NWRAP_LOG_DEBUG
, "size == 0");
2049 /* Support for 32-bit system I guess */
2050 if (nwrap
->st
.st_size
> INT32_MAX
) {
2051 NWRAP_LOG(NWRAP_LOG_ERROR
,
2052 "Size[%u] larger than INT32_MAX",
2053 (unsigned)nwrap
->st
.st_size
);
2060 n
= getline(&line
, &len
, nwrap
->fp
);
2063 if (feof(nwrap
->fp
)) {
2067 NWRAP_LOG(NWRAP_LOG_ERROR
,
2068 "Unable to read line from file: %s",
2073 if (line
[n
- 1] == '\n') {
2077 if (line
[0] == '\0') {
2082 ok
= nwrap
->parse_line(nwrap
, line
);
2084 NWRAP_LOG(NWRAP_LOG_ERROR
,
2085 "Unable to parse line file: %s",
2091 /* Line is parsed without issues so add it to list */
2092 ok
= nwrap_vector_add_item(&(nwrap
->lines
), (void *const) line
);
2094 NWRAP_LOG(NWRAP_LOG_ERROR
,
2095 "Unable to add line to vector");
2099 /* This forces getline to allocate new memory for line. */
2101 } while (!feof(nwrap
->fp
));
2106 static void nwrap_files_cache_unload(struct nwrap_cache
*nwrap
)
2108 nwrap
->unload(nwrap
);
2110 nwrap_lines_unload(nwrap
);
2113 static bool nwrap_files_cache_reload(struct nwrap_cache
*nwrap
)
2118 bool retried
= false;
2120 assert(nwrap
!= NULL
);
2123 if (nwrap
->fd
< 0) {
2124 nwrap
->fp
= fopen(nwrap
->path
, "re");
2125 if (nwrap
->fp
== NULL
) {
2127 NWRAP_LOG(NWRAP_LOG_ERROR
,
2128 "Unable to open '%s' readonly %d:%s",
2129 nwrap
->path
, nwrap
->fd
,
2134 nwrap
->fd
= fileno(nwrap
->fp
);
2135 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Open '%s'", nwrap
->path
);
2138 ret
= fstat(nwrap
->fd
, &st
);
2139 if (ret
!= 0 && errno
== EBADF
&& retried
== false) {
2140 /* maybe something closed the fd on our behalf */
2141 NWRAP_LOG(NWRAP_LOG_TRACE
,
2142 "fstat(%s) - %d:%s - reopen",
2147 memset(&nwrap
->st
, 0, sizeof(nwrap
->st
));
2153 else if (ret
!= 0) {
2154 NWRAP_LOG(NWRAP_LOG_ERROR
,
2155 "fstat(%s) - %d:%s",
2165 if (retried
== false && st
.st_nlink
== 0) {
2166 /* maybe someone has replaced the file... */
2167 NWRAP_LOG(NWRAP_LOG_TRACE
,
2168 "st_nlink == 0, reopen %s",
2171 memset(&nwrap
->st
, 0, sizeof(nwrap
->st
));
2178 if (st
.st_mtime
== nwrap
->st
.st_mtime
) {
2179 NWRAP_LOG(NWRAP_LOG_TRACE
,
2180 "st_mtime[%u] hasn't changed, skip reload",
2181 (unsigned)st
.st_mtime
);
2185 NWRAP_LOG(NWRAP_LOG_TRACE
,
2186 "st_mtime has changed [%u] => [%u], start reload",
2187 (unsigned)st
.st_mtime
,
2188 (unsigned)nwrap
->st
.st_mtime
);
2192 nwrap_files_cache_unload(nwrap
);
2194 ok
= nwrap_parse_file(nwrap
);
2196 NWRAP_LOG(NWRAP_LOG_ERROR
, "Failed to reload %s", nwrap
->path
);
2197 nwrap_files_cache_unload(nwrap
);
2201 NWRAP_LOG(NWRAP_LOG_TRACE
, "Reloaded %s", nwrap
->path
);
2206 * the caller has to call nwrap_unload() on failure
2208 static bool nwrap_pw_parse_line(struct nwrap_cache
*nwrap
, char *line
)
2210 struct nwrap_pw
*nwrap_pw
;
2217 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
2219 list_size
= sizeof(*nwrap_pw
->list
) * (nwrap_pw
->num
+1);
2220 pw
= (struct passwd
*)realloc(nwrap_pw
->list
, list_size
);
2222 NWRAP_LOG(NWRAP_LOG_ERROR
,
2223 "realloc(%u) failed",
2224 (unsigned)list_size
);
2227 nwrap_pw
->list
= pw
;
2229 pw
= &nwrap_pw
->list
[nwrap_pw
->num
];
2236 NWRAP_LOG(NWRAP_LOG_ERROR
,
2237 "Invalid line[%s]: '%s'",
2247 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]\n", pw
->pw_name
);
2252 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
2260 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]\n", pw
->pw_passwd
);
2265 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
2271 pw
->pw_uid
= (uid_t
)strtoul(c
, &e
, 10);
2273 NWRAP_LOG(NWRAP_LOG_ERROR
,
2274 "Invalid line[%s]: '%s' - %s",
2275 line
, c
, strerror(errno
));
2279 NWRAP_LOG(NWRAP_LOG_ERROR
,
2280 "Invalid line[%s]: '%s' - %s",
2281 line
, c
, strerror(errno
));
2285 NWRAP_LOG(NWRAP_LOG_ERROR
,
2286 "Invalid line[%s]: '%s' - %s",
2287 line
, c
, strerror(errno
));
2292 NWRAP_LOG(NWRAP_LOG_TRACE
, "uid[%u]", pw
->pw_uid
);
2297 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
2303 pw
->pw_gid
= (gid_t
)strtoul(c
, &e
, 10);
2305 NWRAP_LOG(NWRAP_LOG_ERROR
,
2306 "Invalid line[%s]: '%s' - %s",
2307 line
, c
, strerror(errno
));
2311 NWRAP_LOG(NWRAP_LOG_ERROR
,
2312 "Invalid line[%s]: '%s' - %s",
2313 line
, c
, strerror(errno
));
2317 NWRAP_LOG(NWRAP_LOG_ERROR
,
2318 "Invalid line[%s]: '%s' - %s",
2319 line
, c
, strerror(errno
));
2324 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]\n", pw
->pw_gid
);
2326 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
2327 pw
->pw_class
= discard_const_p(char, "");
2329 NWRAP_LOG(NWRAP_LOG_TRACE
, "class[%s]", pw
->pw_class
);
2330 #endif /* HAVE_STRUCT_PASSWD_PW_CLASS */
2332 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
2335 NWRAP_LOG(NWRAP_LOG_TRACE
,
2337 (unsigned long)pw
->pw_change
);
2338 #endif /* HAVE_STRUCT_PASSWD_PW_CHANGE */
2340 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
2343 NWRAP_LOG(NWRAP_LOG_TRACE
,
2345 (unsigned long)pw
->pw_expire
);
2346 #endif /* HAVE_STRUCT_PASSWD_PW_EXPIRE */
2351 NWRAP_LOG(NWRAP_LOG_ERROR
, "invalid line[%s]: '%s'", line
, c
);
2359 NWRAP_LOG(NWRAP_LOG_TRACE
, "gecos[%s]", pw
->pw_gecos
);
2364 NWRAP_LOG(NWRAP_LOG_ERROR
, "'%s'", c
);
2372 NWRAP_LOG(NWRAP_LOG_TRACE
, "dir[%s]", pw
->pw_dir
);
2376 NWRAP_LOG(NWRAP_LOG_TRACE
, "shell[%s]", pw
->pw_shell
);
2378 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2379 "Added user[%s:%s:%u:%u:%s:%s:%s]",
2380 pw
->pw_name
, pw
->pw_passwd
,
2381 pw
->pw_uid
, pw
->pw_gid
,
2382 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
2388 static void nwrap_pw_unload(struct nwrap_cache
*nwrap
)
2390 struct nwrap_pw
*nwrap_pw
;
2391 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
2393 SAFE_FREE(nwrap_pw
->list
);
2398 static int nwrap_pw_copy_r(const struct passwd
*src
, struct passwd
*dst
,
2399 char *buf
, size_t buflen
, struct passwd
**dstp
)
2405 first
= src
->pw_name
;
2407 last
= src
->pw_shell
;
2408 while (*last
) last
++;
2410 ofs
= PTR_DIFF(last
+ 1, first
);
2412 if (ofs
> (off_t
) buflen
) {
2416 memcpy(buf
, first
, ofs
);
2418 ofs
= PTR_DIFF(src
->pw_name
, first
);
2419 dst
->pw_name
= buf
+ ofs
;
2420 ofs
= PTR_DIFF(src
->pw_passwd
, first
);
2421 dst
->pw_passwd
= buf
+ ofs
;
2422 dst
->pw_uid
= src
->pw_uid
;
2423 dst
->pw_gid
= src
->pw_gid
;
2424 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
2425 ofs
= PTR_DIFF(src
->pw_class
, first
);
2426 dst
->pw_class
= buf
+ ofs
;
2427 #endif /* HAVE_STRUCT_PASSWD_PW_CLASS */
2429 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
2431 #endif /* HAVE_STRUCT_PASSWD_PW_CHANGE */
2433 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
2435 #endif /* HAVE_STRUCT_PASSWD_PW_EXPIRE */
2437 ofs
= PTR_DIFF(src
->pw_gecos
, first
);
2438 dst
->pw_gecos
= buf
+ ofs
;
2439 ofs
= PTR_DIFF(src
->pw_dir
, first
);
2440 dst
->pw_dir
= buf
+ ofs
;
2441 ofs
= PTR_DIFF(src
->pw_shell
, first
);
2442 dst
->pw_shell
= buf
+ ofs
;
2451 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2452 static bool nwrap_sp_parse_line(struct nwrap_cache
*nwrap
, char *line
)
2454 struct nwrap_sp
*nwrap_sp
;
2461 nwrap_sp
= (struct nwrap_sp
*)nwrap
->private_data
;
2463 list_size
= sizeof(*nwrap_sp
->list
) * (nwrap_sp
->num
+1);
2464 sp
= (struct spwd
*)realloc(nwrap_sp
->list
, list_size
);
2466 NWRAP_LOG(NWRAP_LOG_ERROR
,
2467 "realloc(%u) failed",
2468 (unsigned)list_size
);
2471 nwrap_sp
->list
= sp
;
2473 sp
= &nwrap_sp
->list
[nwrap_sp
->num
];
2480 NWRAP_LOG(NWRAP_LOG_ERROR
,
2481 "name -- Invalid line[%s]: '%s'",
2491 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]\n", sp
->sp_namp
);
2496 NWRAP_LOG(NWRAP_LOG_ERROR
,
2497 "pwd -- Invalid line[%s]: '%s'",
2514 NWRAP_LOG(NWRAP_LOG_ERROR
,
2515 "lstchg -- Invalid line[%s]: '%s'",
2522 sp
->sp_lstchg
= strtol(c
, &e
, 10);
2524 NWRAP_LOG(NWRAP_LOG_ERROR
,
2525 "lstchg -- Invalid line[%s]: '%s' - %s",
2526 line
, c
, strerror(errno
));
2530 NWRAP_LOG(NWRAP_LOG_ERROR
,
2531 "lstchg -- Invalid line[%s]: '%s' - %s",
2532 line
, c
, strerror(errno
));
2536 NWRAP_LOG(NWRAP_LOG_ERROR
,
2537 "lstchg -- Invalid line[%s]: '%s' - %s",
2538 line
, c
, strerror(errno
));
2551 NWRAP_LOG(NWRAP_LOG_ERROR
,
2552 "min -- Invalid line[%s]: '%s'",
2559 sp
->sp_min
= strtol(c
, &e
, 10);
2561 NWRAP_LOG(NWRAP_LOG_ERROR
,
2562 "min -- Invalid line[%s]: '%s' - %s",
2563 line
, c
, strerror(errno
));
2567 NWRAP_LOG(NWRAP_LOG_ERROR
,
2568 "min -- Invalid line[%s]: '%s' - %s",
2569 line
, c
, strerror(errno
));
2573 NWRAP_LOG(NWRAP_LOG_ERROR
,
2574 "min -- Invalid line[%s]: '%s' - %s",
2575 line
, c
, strerror(errno
));
2588 NWRAP_LOG(NWRAP_LOG_ERROR
,
2589 "max -- Invalid line[%s]: '%s'",
2596 sp
->sp_max
= strtol(c
, &e
, 10);
2598 NWRAP_LOG(NWRAP_LOG_ERROR
,
2599 "max -- Invalid line[%s]: '%s' - %s",
2600 line
, c
, strerror(errno
));
2604 NWRAP_LOG(NWRAP_LOG_ERROR
,
2605 "max -- Invalid line[%s]: '%s' - %s",
2606 line
, c
, strerror(errno
));
2610 NWRAP_LOG(NWRAP_LOG_ERROR
,
2611 "max -- Invalid line[%s]: '%s' - %s",
2612 line
, c
, strerror(errno
));
2625 NWRAP_LOG(NWRAP_LOG_ERROR
,
2626 "warn -- Invalid line[%s]: '%s'",
2633 sp
->sp_warn
= strtol(c
, &e
, 10);
2635 NWRAP_LOG(NWRAP_LOG_ERROR
,
2636 "warn -- Invalid line[%s]: '%s' - %s",
2637 line
, c
, strerror(errno
));
2641 NWRAP_LOG(NWRAP_LOG_ERROR
,
2642 "warn -- Invalid line[%s]: '%s' - %s",
2643 line
, c
, strerror(errno
));
2647 NWRAP_LOG(NWRAP_LOG_ERROR
,
2648 "warn -- Invalid line[%s]: '%s' - %s",
2649 line
, c
, strerror(errno
));
2662 NWRAP_LOG(NWRAP_LOG_ERROR
,
2663 "inact -- Invalid line[%s]: '%s'",
2670 sp
->sp_inact
= strtol(c
, &e
, 10);
2672 NWRAP_LOG(NWRAP_LOG_ERROR
,
2673 "inact -- Invalid line[%s]: '%s' - %s",
2674 line
, c
, strerror(errno
));
2678 NWRAP_LOG(NWRAP_LOG_ERROR
,
2679 "inact -- Invalid line[%s]: '%s' - %s",
2680 line
, c
, strerror(errno
));
2684 NWRAP_LOG(NWRAP_LOG_ERROR
,
2685 "inact -- Invalid line[%s]: '%s' - %s",
2686 line
, c
, strerror(errno
));
2699 NWRAP_LOG(NWRAP_LOG_ERROR
,
2700 "expire -- Invalid line[%s]: '%s'",
2707 sp
->sp_expire
= strtol(c
, &e
, 10);
2709 NWRAP_LOG(NWRAP_LOG_ERROR
,
2710 "expire -- Invalid line[%s]: '%s' - %s",
2711 line
, c
, strerror(errno
));
2715 NWRAP_LOG(NWRAP_LOG_ERROR
,
2716 "expire -- Invalid line[%s]: '%s' - %s",
2717 line
, c
, strerror(errno
));
2721 NWRAP_LOG(NWRAP_LOG_ERROR
,
2722 "expire -- Invalid line[%s]: '%s' - %s",
2723 line
, c
, strerror(errno
));
2733 static void nwrap_sp_unload(struct nwrap_cache
*nwrap
)
2735 struct nwrap_sp
*nwrap_sp
;
2736 nwrap_sp
= (struct nwrap_sp
*)nwrap
->private_data
;
2738 SAFE_FREE(nwrap_sp
->list
);
2742 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2745 * the caller has to call nwrap_unload() on failure
2747 static bool nwrap_gr_parse_line(struct nwrap_cache
*nwrap
, char *line
)
2749 struct nwrap_gr
*nwrap_gr
;
2757 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
2759 list_size
= sizeof(*nwrap_gr
->list
) * (nwrap_gr
->num
+1);
2760 gr
= (struct group
*)realloc(nwrap_gr
->list
, list_size
);
2762 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc failed");
2765 nwrap_gr
->list
= gr
;
2767 gr
= &nwrap_gr
->list
[nwrap_gr
->num
];
2774 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
2782 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]", gr
->gr_name
);
2787 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
2795 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]", gr
->gr_passwd
);
2800 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
2806 gr
->gr_gid
= (gid_t
)strtoul(c
, &e
, 10);
2808 NWRAP_LOG(NWRAP_LOG_ERROR
,
2809 "Invalid line[%s]: '%s' - %s",
2810 line
, c
, strerror(errno
));
2814 NWRAP_LOG(NWRAP_LOG_ERROR
,
2815 "Invalid line[%s]: '%s' - %s",
2816 line
, c
, strerror(errno
));
2820 NWRAP_LOG(NWRAP_LOG_ERROR
,
2821 "Invalid line[%s]: '%s' - %s",
2822 line
, c
, strerror(errno
));
2827 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]", gr
->gr_gid
);
2830 gr
->gr_mem
= (char **)malloc(sizeof(char *));
2832 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
2835 gr
->gr_mem
[0] = NULL
;
2837 for(nummem
= 0; p
!= NULL
&& p
[0] != '\0'; nummem
++) {
2847 if (strlen(c
) == 0) {
2851 m_size
= sizeof(char *) * (nummem
+2);
2852 m
= (char **)realloc(gr
->gr_mem
, m_size
);
2854 NWRAP_LOG(NWRAP_LOG_ERROR
,
2855 "realloc(%zd) failed",
2860 gr
->gr_mem
[nummem
] = c
;
2861 gr
->gr_mem
[nummem
+1] = NULL
;
2863 NWRAP_LOG(NWRAP_LOG_TRACE
,
2865 nummem
, gr
->gr_mem
[nummem
]);
2868 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2869 "Added group[%s:%s:%u:] with %u members",
2870 gr
->gr_name
, gr
->gr_passwd
, gr
->gr_gid
, nummem
);
2876 static void nwrap_gr_unload(struct nwrap_cache
*nwrap
)
2879 struct nwrap_gr
*nwrap_gr
;
2880 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
2882 if (nwrap_gr
->list
) {
2883 for (i
=0; i
< nwrap_gr
->num
; i
++) {
2884 SAFE_FREE(nwrap_gr
->list
[i
].gr_mem
);
2886 SAFE_FREE(nwrap_gr
->list
);
2893 static int nwrap_gr_copy_r(const struct group
*src
, struct group
*dst
,
2894 char *buf
, size_t buflen
, struct group
**dstp
)
2897 uintptr_t align
= 0;
2898 unsigned int gr_mem_cnt
= 0;
2901 size_t gr_name_len
= strlen(src
->gr_name
) + 1;
2902 size_t gr_passwd_len
= strlen(src
->gr_passwd
) + 1;
2908 for (i
= 0; src
->gr_mem
[i
] != NULL
; i
++) {
2912 /* Align the memory for storing pointers */
2913 align
= __alignof__(char *) - ((p
- (char *)0) % __alignof__(char *));
2915 (1 + gr_mem_cnt
) * sizeof(char *) +
2916 gr_name_len
+ gr_passwd_len
;
2918 if (total_len
> buflen
) {
2922 buflen
-= total_len
;
2927 dst
->gr_mem
= g_mem
.data
;
2930 p
+= (1 + gr_mem_cnt
) * sizeof(char *);
2941 dst
->gr_gid
= src
->gr_gid
;
2943 memcpy(dst
->gr_name
, src
->gr_name
, gr_name_len
);
2945 memcpy(dst
->gr_passwd
, src
->gr_passwd
, gr_passwd_len
);
2947 /* Set the terminating entry */
2948 dst
->gr_mem
[gr_mem_cnt
] = NULL
;
2950 /* Now add the group members content */
2952 for (i
= 0; i
< gr_mem_cnt
; i
++) {
2953 size_t len
= strlen(src
->gr_mem
[i
]) + 1;
2960 if (total_len
> buflen
) {
2965 for (i
= 0; i
< gr_mem_cnt
; i
++) {
2966 size_t len
= strlen(src
->gr_mem
[i
]) + 1;
2968 memcpy(dst
->gr_mem
[i
],
2980 static struct nwrap_entlist
*nwrap_entlist_init(struct nwrap_entdata
*ed
)
2982 struct nwrap_entlist
*el
;
2985 NWRAP_LOG(NWRAP_LOG_ERROR
,
2986 "entry is NULL, can't create list item");
2990 el
= (struct nwrap_entlist
*)malloc(sizeof(struct nwrap_entlist
));
2992 NWRAP_LOG(NWRAP_LOG_ERROR
, "malloc failed");
3002 static bool nwrap_ed_inventarize_add_new(char *const h_name
,
3003 struct nwrap_entdata
*const ed
)
3007 struct nwrap_entlist
*el
;
3010 if (h_name
== NULL
) {
3011 NWRAP_LOG(NWRAP_LOG_ERROR
, "h_name NULL - can't add");
3015 el
= nwrap_entlist_init(ed
);
3021 e
.data
= (void *)el
;
3023 p
= hsearch(e
, ENTER
);
3025 NWRAP_LOG(NWRAP_LOG_ERROR
,
3026 "Hash table is full (%s)!",
3031 ok
= nwrap_vector_add_item(&(nwrap_he_global
.lists
), (void *)el
);
3033 NWRAP_LOG(NWRAP_LOG_ERROR
,
3034 "Failed to add list entry to vector.");
3041 static bool nwrap_ed_inventarize_add_to_existing(struct nwrap_entdata
*const ed
,
3042 struct nwrap_entlist
*const el
)
3044 struct nwrap_entlist
*cursor
;
3045 struct nwrap_entlist
*el_new
;
3048 NWRAP_LOG(NWRAP_LOG_ERROR
, "list is NULL, can not add");
3053 for (cursor
= el
; cursor
->next
!= NULL
; cursor
= cursor
->next
)
3055 if (cursor
->ed
== ed
) {
3056 /* The entry already exists in this list. */
3061 if (cursor
->ed
== ed
) {
3062 /* The entry already exists in this list. */
3066 el_new
= nwrap_entlist_init(ed
);
3067 if (el_new
== NULL
) {
3071 cursor
->next
= el_new
;
3075 static bool nwrap_ed_inventarize(char *const name
,
3076 struct nwrap_entdata
*const ed
)
3085 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Searching name: %s", e
.key
);
3087 p
= hsearch(e
, FIND
);
3089 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name %s not found. Adding...", name
);
3090 ok
= nwrap_ed_inventarize_add_new(name
, ed
);
3092 struct nwrap_entlist
*el
= (struct nwrap_entlist
*)p
->data
;
3094 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name %s found. Add record to list.", name
);
3095 ok
= nwrap_ed_inventarize_add_to_existing(ed
, el
);
3101 static bool nwrap_add_hname(struct nwrap_entdata
*const ed
)
3103 char *const h_name
= (char *const)(ed
->ht
.h_name
);
3107 ok
= nwrap_ed_inventarize(h_name
, ed
);
3112 if (ed
->ht
.h_aliases
== NULL
) {
3116 /* Itemize aliases */
3117 for (i
= 0; ed
->ht
.h_aliases
[i
] != NULL
; ++i
) {
3120 h_name_alias
= ed
->ht
.h_aliases
[i
];
3122 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Add alias: %s", h_name_alias
);
3124 if (!nwrap_ed_inventarize(h_name_alias
, ed
)) {
3125 NWRAP_LOG(NWRAP_LOG_ERROR
,
3126 "Unable to add alias: %s", h_name_alias
);
3134 static bool nwrap_he_parse_line(struct nwrap_cache
*nwrap
, char *line
)
3136 struct nwrap_he
*nwrap_he
= (struct nwrap_he
*)nwrap
->private_data
;
3137 bool do_aliases
= true;
3138 ssize_t aliases_count
= 0;
3146 struct nwrap_entdata
*ed
= (struct nwrap_entdata
*)
3147 malloc(sizeof(struct nwrap_entdata
));
3149 NWRAP_LOG(NWRAP_LOG_ERROR
,
3150 "Unable to allocate memory for nwrap_entdata");
3161 /* Walk to first char */
3162 for (p
= i
; *p
!= '.' && *p
!= ':' && !isxdigit((int) *p
); p
++) {
3164 NWRAP_LOG(NWRAP_LOG_ERROR
,
3165 "Invalid line[%s]: '%s'",
3172 for (i
= p
; !isspace((int)*p
); p
++) {
3174 NWRAP_LOG(NWRAP_LOG_ERROR
,
3175 "Invalid line[%s]: '%s'",
3184 if (inet_pton(AF_INET
, i
, ed
->addr
.host_addr
)) {
3185 ed
->ht
.h_addrtype
= AF_INET
;
3186 ed
->ht
.h_length
= 4;
3188 } else if (inet_pton(AF_INET6
, i
, ed
->addr
.host_addr
)) {
3189 ed
->ht
.h_addrtype
= AF_INET6
;
3190 ed
->ht
.h_length
= 16;
3193 NWRAP_LOG(NWRAP_LOG_ERROR
,
3194 "Invalid line[%s]: '%s'",
3202 ok
= nwrap_vector_add_item(&(ed
->nwrap_addrdata
),
3203 (void *const)ed
->addr
.host_addr
);
3205 NWRAP_LOG(NWRAP_LOG_ERROR
, "Unable to add addrdata to vector");
3209 ed
->ht
.h_addr_list
= nwrap_vector_head(&ed
->nwrap_addrdata
);
3217 /* Walk to first char */
3218 for (n
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
3220 NWRAP_LOG(NWRAP_LOG_ERROR
,
3221 "Invalid line[%s]: '%s'",
3229 for (n
= p
; !isspace((int)*p
); p
++) {
3238 /* Convert to lowercase. This operate on same memory region */
3242 /* glib's getent always dereferences he->h_aliases */
3243 ed
->ht
.h_aliases
= malloc(sizeof(char *));
3244 if (ed
->ht
.h_aliases
== NULL
) {
3248 ed
->ht
.h_aliases
[0] = NULL
;
3253 while (do_aliases
) {
3259 /* Walk to first char */
3260 for (a
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
3266 /* Only trailing spaces are left */
3271 for (a
= p
; !isspace((int)*p
); p
++) {
3280 aliases
= realloc(ed
->ht
.h_aliases
, sizeof(char *) * (aliases_count
+ 2));
3281 if (aliases
== NULL
) {
3285 ed
->ht
.h_aliases
= aliases
;
3288 aliases
[aliases_count
] = a
;
3289 aliases
[aliases_count
+ 1] = NULL
;
3294 ok
= nwrap_vector_add_item(&(nwrap_he
->entries
), (void *const)ed
);
3296 NWRAP_LOG(NWRAP_LOG_ERROR
, "Unable to add entry to vector");
3301 ed
->aliases_count
= aliases_count
;
3302 /* Inventarize item */
3303 ok
= nwrap_add_hname(ed
);
3308 ok
= nwrap_ed_inventarize(ip
, ed
);
3317 static void nwrap_he_unload(struct nwrap_cache
*nwrap
)
3319 struct nwrap_he
*nwrap_he
=
3320 (struct nwrap_he
*)nwrap
->private_data
;
3321 struct nwrap_entdata
*ed
;
3322 struct nwrap_entlist
*el
;
3326 nwrap_vector_foreach (ed
, nwrap_he
->entries
, i
)
3328 SAFE_FREE(ed
->nwrap_addrdata
.items
);
3329 SAFE_FREE(ed
->ht
.h_aliases
);
3332 SAFE_FREE(nwrap_he
->entries
.items
);
3333 nwrap_he
->entries
.count
= nwrap_he
->entries
.capacity
= 0;
3335 nwrap_vector_foreach(el
, nwrap_he
->lists
, i
)
3337 while (el
!= NULL
) {
3338 struct nwrap_entlist
*el_next
;
3345 SAFE_FREE(nwrap_he
->lists
.items
);
3346 nwrap_he
->lists
.count
= nwrap_he
->lists
.capacity
= 0;
3352 * If we unload the file, the pointers in the hash table point to
3353 * invalid memory. So we need to destroy the hash table and recreate
3357 rc
= hcreate(max_hostents
);
3359 NWRAP_LOG(NWRAP_LOG_ERROR
, "Failed to initialize hash table");
3365 /* user functions */
3366 static struct passwd
*nwrap_files_getpwnam(struct nwrap_backend
*b
,
3372 (void) b
; /* unused */
3374 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Lookup user %s in files", name
);
3376 ok
= nwrap_files_cache_reload(nwrap_pw_global
.cache
);
3378 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading passwd file");
3382 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
3383 if (strcmp(nwrap_pw_global
.list
[i
].pw_name
, name
) == 0) {
3384 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] found", name
);
3385 return &nwrap_pw_global
.list
[i
];
3387 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3388 "user[%s] does not match [%s]",
3390 nwrap_pw_global
.list
[i
].pw_name
);
3393 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] not found\n", name
);
3399 static int nwrap_files_getpwnam_r(struct nwrap_backend
*b
,
3400 const char *name
, struct passwd
*pwdst
,
3401 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3405 pw
= nwrap_files_getpwnam(b
, name
);
3413 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
3416 static struct passwd
*nwrap_files_getpwuid(struct nwrap_backend
*b
,
3422 (void) b
; /* unused */
3424 ok
= nwrap_files_cache_reload(nwrap_pw_global
.cache
);
3426 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading passwd file");
3430 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
3431 if (nwrap_pw_global
.list
[i
].pw_uid
== uid
) {
3432 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] found", uid
);
3433 return &nwrap_pw_global
.list
[i
];
3435 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3436 "uid[%u] does not match [%u]",
3438 nwrap_pw_global
.list
[i
].pw_uid
);
3441 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] not found\n", uid
);
3447 static int nwrap_files_getpwuid_r(struct nwrap_backend
*b
,
3448 uid_t uid
, struct passwd
*pwdst
,
3449 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3453 pw
= nwrap_files_getpwuid(b
, uid
);
3461 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
3464 /* user enum functions */
3465 static void nwrap_files_setpwent(struct nwrap_backend
*b
)
3467 (void) b
; /* unused */
3469 nwrap_pw_global
.idx
= 0;
3472 static struct passwd
*nwrap_files_getpwent(struct nwrap_backend
*b
)
3476 (void) b
; /* unused */
3478 if (nwrap_pw_global
.idx
== 0) {
3480 ok
= nwrap_files_cache_reload(nwrap_pw_global
.cache
);
3482 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading passwd file");
3487 if (nwrap_pw_global
.idx
>= nwrap_pw_global
.num
) {
3492 pw
= &nwrap_pw_global
.list
[nwrap_pw_global
.idx
++];
3494 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3495 "return user[%s] uid[%u]",
3496 pw
->pw_name
, pw
->pw_uid
);
3501 static int nwrap_files_getpwent_r(struct nwrap_backend
*b
,
3502 struct passwd
*pwdst
, char *buf
,
3503 size_t buflen
, struct passwd
**pwdstp
)
3507 pw
= nwrap_files_getpwent(b
);
3515 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
3518 static void nwrap_files_endpwent(struct nwrap_backend
*b
)
3520 (void) b
; /* unused */
3522 nwrap_pw_global
.idx
= 0;
3527 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
3529 #ifdef HAVE_SETSPENT
3530 static void nwrap_files_setspent(void)
3532 nwrap_sp_global
.idx
= 0;
3535 static struct spwd
*nwrap_files_getspent(void)
3539 if (nwrap_sp_global
.idx
== 0) {
3542 ok
= nwrap_files_cache_reload(nwrap_sp_global
.cache
);
3544 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading shadow file");
3549 if (nwrap_sp_global
.idx
>= nwrap_sp_global
.num
) {
3554 sp
= &nwrap_sp_global
.list
[nwrap_sp_global
.idx
++];
3556 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3563 static void nwrap_files_endspent(void)
3565 nwrap_sp_global
.idx
= 0;
3567 #endif /* HAVE_SETSPENT */
3569 static struct spwd
*nwrap_files_getspnam(const char *name
)
3574 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Lookup user %s in files", name
);
3576 ok
= nwrap_files_cache_reload(nwrap_sp_global
.cache
);
3578 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading shadow file");
3582 for (i
=0; i
<nwrap_sp_global
.num
; i
++) {
3583 if (strcmp(nwrap_sp_global
.list
[i
].sp_namp
, name
) == 0) {
3584 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] found", name
);
3585 return &nwrap_sp_global
.list
[i
];
3587 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3588 "user[%s] does not match [%s]",
3590 nwrap_sp_global
.list
[i
].sp_namp
);
3593 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] not found\n", name
);
3598 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3600 /* misc functions */
3601 static int nwrap_files_initgroups(struct nwrap_backend
*b
,
3610 groups
= (gid_t
*)malloc(size
* sizeof(gid_t
));
3611 if (groups
== NULL
) {
3612 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
3618 nwrap_files_setgrent(b
);
3619 while ((grp
= nwrap_files_getgrent(b
)) != NULL
) {
3622 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3623 "Inspecting %s for group membership",
3626 for (i
=0; grp
->gr_mem
&& grp
->gr_mem
[i
] != NULL
; i
++) {
3627 if (group
!= grp
->gr_gid
&&
3628 (strcmp(user
, grp
->gr_mem
[i
]) == 0)) {
3629 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3630 "%s is member of %s",
3634 groups
= (gid_t
*)realloc(groups
,
3635 (size
+ 1) * sizeof(gid_t
));
3636 if (groups
== NULL
) {
3637 NWRAP_LOG(NWRAP_LOG_ERROR
,
3643 groups
[size
] = grp
->gr_gid
;
3649 nwrap_files_endgrent(b
);
3651 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3652 "%s is member of %d groups",
3655 /* This really only works if uid_wrapper is loaded */
3656 rc
= setgroups(size
, groups
);
3663 /* group functions */
3664 static struct group
*nwrap_files_getgrnam(struct nwrap_backend
*b
,
3670 (void) b
; /* unused */
3672 ok
= nwrap_files_cache_reload(nwrap_gr_global
.cache
);
3674 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading group file");
3678 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
3679 if (strcmp(nwrap_gr_global
.list
[i
].gr_name
, name
) == 0) {
3680 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] found", name
);
3681 return &nwrap_gr_global
.list
[i
];
3683 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3684 "group[%s] does not match [%s]",
3686 nwrap_gr_global
.list
[i
].gr_name
);
3689 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] not found", name
);
3695 static int nwrap_files_getgrnam_r(struct nwrap_backend
*b
,
3696 const char *name
, struct group
*grdst
,
3697 char *buf
, size_t buflen
, struct group
**grdstp
)
3701 gr
= nwrap_files_getgrnam(b
, name
);
3709 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
3712 static struct group
*nwrap_files_getgrgid(struct nwrap_backend
*b
,
3718 (void) b
; /* unused */
3720 ok
= nwrap_files_cache_reload(nwrap_gr_global
.cache
);
3722 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading group file");
3726 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
3727 if (nwrap_gr_global
.list
[i
].gr_gid
== gid
) {
3728 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] found", gid
);
3729 return &nwrap_gr_global
.list
[i
];
3731 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3732 "gid[%u] does not match [%u]",
3734 nwrap_gr_global
.list
[i
].gr_gid
);
3737 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] not found", gid
);
3743 static int nwrap_files_getgrgid_r(struct nwrap_backend
*b
,
3744 gid_t gid
, struct group
*grdst
,
3745 char *buf
, size_t buflen
, struct group
**grdstp
)
3749 gr
= nwrap_files_getgrgid(b
, gid
);
3757 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
3760 /* group enum functions */
3761 static void nwrap_files_setgrent(struct nwrap_backend
*b
)
3763 (void) b
; /* unused */
3765 nwrap_gr_global
.idx
= 0;
3768 static struct group
*nwrap_files_getgrent(struct nwrap_backend
*b
)
3772 (void) b
; /* unused */
3774 if (nwrap_gr_global
.idx
== 0) {
3777 ok
= nwrap_files_cache_reload(nwrap_gr_global
.cache
);
3779 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading group file");
3784 if (nwrap_gr_global
.idx
>= nwrap_gr_global
.num
) {
3789 gr
= &nwrap_gr_global
.list
[nwrap_gr_global
.idx
++];
3791 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3792 "return group[%s] gid[%u]",
3793 gr
->gr_name
, gr
->gr_gid
);
3798 static int nwrap_files_getgrent_r(struct nwrap_backend
*b
,
3799 struct group
*grdst
, char *buf
,
3800 size_t buflen
, struct group
**grdstp
)
3804 gr
= nwrap_files_getgrent(b
);
3812 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
3815 static void nwrap_files_endgrent(struct nwrap_backend
*b
)
3817 (void) b
; /* unused */
3819 nwrap_gr_global
.idx
= 0;
3822 /* hosts functions */
3823 static int nwrap_files_internal_gethostbyname(const char *name
, int af
,
3824 struct hostent
*result
,
3825 struct nwrap_vector
*addr_list
)
3827 struct nwrap_entlist
*el
;
3832 char canon_name
[DNS_NAME_MAX
] = { 0 };
3834 bool he_found
= false;
3838 * We need to make sure we have zeroed return pointer for consumers
3839 * which don't check return values, e.g. OpenLDAP.
3841 ZERO_STRUCTP(result
);
3843 ok
= nwrap_files_cache_reload(nwrap_he_global
.cache
);
3845 NWRAP_LOG(NWRAP_LOG_ERROR
, "error loading hosts file");
3849 name_len
= strlen(name
);
3850 if (name_len
< sizeof(canon_name
) && name
[name_len
- 1] == '.') {
3851 memcpy(canon_name
, name
, name_len
- 1);
3852 canon_name
[name_len
] = '\0';
3856 if (!str_tolower_copy(&h_name_lower
, name
)) {
3857 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3858 "Out of memory while converting to lower case");
3862 /* Look at hash table for element */
3863 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Searching for name: %s", h_name_lower
);
3864 e
.key
= h_name_lower
;
3866 e_p
= hsearch(e
, FIND
);
3868 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name %s not found.", h_name_lower
);
3869 SAFE_FREE(h_name_lower
);
3872 SAFE_FREE(h_name_lower
);
3874 /* Always cleanup vector and results */
3875 if (!nwrap_vector_is_initialized(addr_list
)) {
3876 if (!nwrap_vector_init(addr_list
)) {
3877 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3878 "Unable to initialize memory for addr_list vector");
3882 /* When vector is initialized data are valid no more.
3883 * Quick way how to free vector is: */
3884 addr_list
->count
= 0;
3887 /* Iterate through results */
3888 for (el
= (struct nwrap_entlist
*)e_p
->data
; el
!= NULL
; el
= el
->next
)
3892 /* Filter by address familiy if provided */
3893 if (af
!= AF_UNSPEC
&& he
->h_addrtype
!= af
) {
3899 * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3901 if (af
== AF_UNSPEC
&& he
->h_addrtype
!= AF_INET
) {
3906 memcpy(result
, he
, sizeof(struct hostent
));
3907 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3908 "Name found. Returning record for %s",
3912 nwrap_vector_merge(addr_list
, &el
->ed
->nwrap_addrdata
);
3913 result
->h_addr_list
= nwrap_vector_head(addr_list
);
3919 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3920 "Name found in database. No records matches type.");
3927 static int nwrap_files_gethostbyname2_r(struct nwrap_backend
*b
,
3928 const char *name
, int af
,
3929 struct hostent
*hedst
,
3930 char *buf
, size_t buflen
,
3931 struct hostent
**hedstp
)
3933 struct nwrap_vector
*addr_list
= NULL
;
3940 (void) b
; /* unused */
3941 (void) af
; /* unused */
3943 if (name
== NULL
|| hedst
== NULL
|| buf
== NULL
|| buflen
== 0) {
3950 addr_list
= calloc(1, sizeof(struct nwrap_vector
));
3951 if (addr_list
== NULL
) {
3952 NWRAP_LOG(NWRAP_LOG_ERROR
,
3953 "Unable to allocate memory for address list");
3958 rc
= nwrap_files_internal_gethostbyname(name
, af
, hedst
,
3961 SAFE_FREE(addr_list
->items
);
3962 SAFE_FREE(addr_list
);
3967 /* +1 i for ending NULL pointer */
3968 if (buflen
< ((addr_list
->count
+ 1) * sizeof(void *))) {
3969 SAFE_FREE(addr_list
->items
);
3970 SAFE_FREE(addr_list
);
3974 /* Copy all to user provided buffer and change
3975 * pointers in returned structure.
3976 * +1 is for ending NULL pointer. */
3977 memcpy(buf
, addr_list
->items
, (addr_list
->count
+ 1) * sizeof(void *));
3979 SAFE_FREE(addr_list
->items
);
3980 SAFE_FREE(addr_list
);
3983 hedst
->h_addr_list
= g
.list
;
3988 #ifdef HAVE_GETHOSTBYNAME_R
3989 static int nwrap_gethostbyname_r(const char *name
,
3990 struct hostent
*ret
,
3991 char *buf
, size_t buflen
,
3992 struct hostent
**result
, int *h_errnop
)
3997 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3998 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3999 rc
= b
->ops
->nw_gethostbyname2_r(b
, name
, AF_UNSPEC
, ret
,
4000 buf
, buflen
, result
);
4003 } else if (rc
== ERANGE
) {
4007 *h_errnop
= h_errno
;
4011 int gethostbyname_r(const char *name
,
4012 struct hostent
*ret
,
4013 char *buf
, size_t buflen
,
4014 struct hostent
**result
, int *h_errnop
)
4016 if (!nss_wrapper_hosts_enabled()) {
4017 return libc_gethostbyname_r(name
,
4025 return nwrap_gethostbyname_r(name
, ret
, buf
, buflen
, result
, h_errnop
);
4029 #ifdef HAVE_GETHOSTBYNAME2_R
4030 static int nwrap_gethostbyname2_r(const char *name
, int af
,
4031 struct hostent
*ret
,
4032 char *buf
, size_t buflen
,
4033 struct hostent
**result
, int *h_errnop
)
4038 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4039 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4040 rc
= b
->ops
->nw_gethostbyname2_r(b
, name
, af
, ret
,
4041 buf
, buflen
, result
);
4044 } else if (rc
== ERANGE
) {
4048 *h_errnop
= h_errno
;
4052 int gethostbyname2_r(const char *name
, int af
,
4053 struct hostent
*ret
,
4054 char *buf
, size_t buflen
,
4055 struct hostent
**result
, int *h_errnop
)
4057 if (!nss_wrapper_hosts_enabled()) {
4058 return libc_gethostbyname2_r(name
, af
, ret
, buf
, buflen
,
4062 return nwrap_gethostbyname2_r(name
, af
, ret
, buf
, buflen
, result
,
4067 static int nwrap_files_getaddrinfo(const char *name
,
4068 unsigned short port
,
4069 const struct addrinfo
*hints
,
4070 struct addrinfo
**ai
)
4072 struct nwrap_entlist
*el
;
4074 struct addrinfo
*ai_head
= NULL
;
4075 struct addrinfo
*ai_cur
= NULL
;
4078 char canon_name
[DNS_NAME_MAX
] = { 0 };
4079 bool skip_canonname
= false;
4087 ok
= nwrap_files_cache_reload(nwrap_he_global
.cache
);
4089 NWRAP_LOG(NWRAP_LOG_ERROR
, "error loading hosts file");
4093 name_len
= strlen(name
);
4094 if (name_len
== 0) {
4098 if (name_len
< sizeof(canon_name
) && name
[name_len
- 1] == '.') {
4099 memcpy(canon_name
, name
, name_len
- 1);
4100 canon_name
[name_len
] = '\0';
4104 if (!str_tolower_copy(&h_name_lower
, name
)) {
4105 NWRAP_LOG(NWRAP_LOG_DEBUG
,
4106 "Out of memory while converting to lower case");
4110 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Searching for name: %s", h_name_lower
);
4111 e
.key
= h_name_lower
;
4113 e_p
= hsearch(e
, FIND
);
4115 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name %s not found.", h_name_lower
);
4116 SAFE_FREE(h_name_lower
);
4120 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name: %s found.", h_name_lower
);
4121 SAFE_FREE(h_name_lower
);
4124 for (el
= (struct nwrap_entlist
*)e_p
->data
; el
!= NULL
; el
= el
->next
)
4127 struct addrinfo
*ai_new
= NULL
;
4131 if (hints
->ai_family
!= AF_UNSPEC
&&
4132 he
->h_addrtype
!= hints
->ai_family
)
4134 NWRAP_LOG(NWRAP_LOG_DEBUG
,
4135 "Entry found but with wrong AF - "
4136 "remembering EAI_ADDRINFO.");
4137 rc
= EAI_ADDRFAMILY
;
4141 /* Function allocates memory and returns it in ai. */
4142 rc2
= nwrap_convert_he_ai(he
,
4148 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error converting he to ai");
4149 if (ai_head
!= NULL
) {
4150 freeaddrinfo(ai_head
);
4154 skip_canonname
= true;
4156 if (ai_head
== NULL
) {
4159 if (ai_cur
!= NULL
) {
4160 ai_cur
->ai_next
= ai_new
;
4165 if (ai_head
!= NULL
) {
4174 static struct hostent
*nwrap_files_gethostbyaddr(struct nwrap_backend
*b
,
4176 socklen_t len
, int type
)
4179 char ip
[NWRAP_INET_ADDRSTRLEN
] = {0};
4180 struct nwrap_entdata
*ed
;
4185 (void) b
; /* unused */
4186 (void) len
; /* unused */
4188 ok
= nwrap_files_cache_reload(nwrap_he_global
.cache
);
4190 NWRAP_LOG(NWRAP_LOG_ERROR
, "error loading hosts file");
4194 a
= inet_ntop(type
, addr
, ip
, sizeof(ip
));
4200 nwrap_vector_foreach(ed
, nwrap_he_global
.entries
, i
)
4203 if (he
->h_addrtype
!= type
) {
4207 if (memcmp(addr
, he
->h_addr_list
[0], he
->h_length
) == 0) {
4216 #ifdef HAVE_GETHOSTBYADDR_R
4217 static int nwrap_gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
4218 struct hostent
*ret
,
4219 char *buf
, size_t buflen
,
4220 struct hostent
**result
, int *h_errnop
)
4223 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4224 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4225 *result
= b
->ops
->nw_gethostbyaddr(b
, addr
, len
, type
);
4226 if (*result
!= NULL
) {
4231 if (*result
!= NULL
) {
4232 memset(buf
, '\0', buflen
);
4237 *h_errnop
= h_errno
;
4241 int gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
4242 struct hostent
*ret
,
4243 char *buf
, size_t buflen
,
4244 struct hostent
**result
, int *h_errnop
)
4246 if (!nss_wrapper_hosts_enabled()) {
4247 return libc_gethostbyaddr_r(addr
,
4257 return nwrap_gethostbyaddr_r(addr
, len
, type
, ret
, buf
, buflen
, result
, h_errnop
);
4261 /* hosts enum functions */
4262 static void nwrap_files_sethostent(void)
4264 nwrap_he_global
.idx
= 0;
4267 static struct hostent
*nwrap_files_gethostent(void)
4271 if (nwrap_he_global
.idx
== 0) {
4274 ok
= nwrap_files_cache_reload(nwrap_he_global
.cache
);
4276 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading hosts file");
4281 if (nwrap_he_global
.idx
>= nwrap_he_global
.num
) {
4286 he
= &((struct nwrap_entdata
*)nwrap_he_global
.entries
.items
[nwrap_he_global
.idx
++])->ht
;
4288 NWRAP_LOG(NWRAP_LOG_DEBUG
, "return hosts[%s]", he
->h_name
);
4293 static void nwrap_files_endhostent(void)
4295 nwrap_he_global
.idx
= 0;
4303 static struct passwd
*nwrap_module_getpwnam(struct nwrap_backend
*b
,
4306 static struct passwd pwd
;
4307 static char buf
[1000];
4310 if (b
->symbols
->_nss_getpwnam_r
.f
== NULL
) {
4314 status
= b
->symbols
->_nss_getpwnam_r
.f(name
,
4319 if (status
== NSS_STATUS_NOTFOUND
) {
4322 if (status
!= NSS_STATUS_SUCCESS
) {
4329 static int nwrap_module_getpwnam_r(struct nwrap_backend
*b
,
4330 const char *name
, struct passwd
*pwdst
,
4331 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
4337 if (b
->symbols
->_nss_getpwnam_r
.f
== NULL
) {
4338 return NSS_STATUS_NOTFOUND
;
4341 ret
= b
->symbols
->_nss_getpwnam_r
.f(name
, pwdst
, buf
, buflen
, &errno
);
4343 case NSS_STATUS_SUCCESS
:
4346 case NSS_STATUS_NOTFOUND
:
4351 case NSS_STATUS_TRYAGAIN
:
4364 static struct passwd
*nwrap_module_getpwuid(struct nwrap_backend
*b
,
4367 static struct passwd pwd
;
4368 static char buf
[1000];
4371 if (b
->symbols
->_nss_getpwuid_r
.f
== NULL
) {
4375 status
= b
->symbols
->_nss_getpwuid_r
.f(uid
,
4380 if (status
== NSS_STATUS_NOTFOUND
) {
4383 if (status
!= NSS_STATUS_SUCCESS
) {
4389 static int nwrap_module_getpwuid_r(struct nwrap_backend
*b
,
4390 uid_t uid
, struct passwd
*pwdst
,
4391 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
4397 if (b
->symbols
->_nss_getpwuid_r
.f
== NULL
) {
4401 ret
= b
->symbols
->_nss_getpwuid_r
.f(uid
, pwdst
, buf
, buflen
, &errno
);
4403 case NSS_STATUS_SUCCESS
:
4406 case NSS_STATUS_NOTFOUND
:
4411 case NSS_STATUS_TRYAGAIN
:
4424 static void nwrap_module_setpwent(struct nwrap_backend
*b
)
4426 if (b
->symbols
->_nss_setpwent
.f
== NULL
) {
4430 b
->symbols
->_nss_setpwent
.f();
4433 static struct passwd
*nwrap_module_getpwent(struct nwrap_backend
*b
)
4435 static struct passwd pwd
;
4436 static char buf
[1000];
4439 if (b
->symbols
->_nss_getpwent_r
.f
== NULL
) {
4443 status
= b
->symbols
->_nss_getpwent_r
.f(&pwd
, buf
, sizeof(buf
), &errno
);
4444 if (status
== NSS_STATUS_NOTFOUND
) {
4447 if (status
!= NSS_STATUS_SUCCESS
) {
4453 static int nwrap_module_getpwent_r(struct nwrap_backend
*b
,
4454 struct passwd
*pwdst
, char *buf
,
4455 size_t buflen
, struct passwd
**pwdstp
)
4461 if (b
->symbols
->_nss_getpwent_r
.f
== NULL
) {
4465 ret
= b
->symbols
->_nss_getpwent_r
.f(pwdst
, buf
, buflen
, &errno
);
4467 case NSS_STATUS_SUCCESS
:
4470 case NSS_STATUS_NOTFOUND
:
4475 case NSS_STATUS_TRYAGAIN
:
4488 static void nwrap_module_endpwent(struct nwrap_backend
*b
)
4490 if (b
->symbols
->_nss_endpwent
.f
== NULL
) {
4494 b
->symbols
->_nss_endpwent
.f();
4497 static int nwrap_module_initgroups(struct nwrap_backend
*b
,
4498 const char *user
, gid_t group
)
4504 if (b
->symbols
->_nss_initgroups
.f
== NULL
) {
4505 return NSS_STATUS_UNAVAIL
;
4508 return b
->symbols
->_nss_initgroups
.f(user
,
4517 static struct group
*nwrap_module_getgrnam(struct nwrap_backend
*b
,
4520 static struct group grp
;
4522 static int buflen
= 1000;
4525 if (b
->symbols
->_nss_getgrnam_r
.f
== NULL
) {
4530 buf
= (char *)malloc(buflen
);
4533 status
= b
->symbols
->_nss_getgrnam_r
.f(name
, &grp
, buf
, buflen
, &errno
);
4534 if (status
== NSS_STATUS_TRYAGAIN
) {
4536 buf
= (char *)realloc(buf
, buflen
);
4542 if (status
== NSS_STATUS_NOTFOUND
) {
4546 if (status
!= NSS_STATUS_SUCCESS
) {
4553 static int nwrap_module_getgrnam_r(struct nwrap_backend
*b
,
4554 const char *name
, struct group
*grdst
,
4555 char *buf
, size_t buflen
, struct group
**grdstp
)
4561 if (b
->symbols
->_nss_getgrnam_r
.f
== NULL
) {
4565 ret
= b
->symbols
->_nss_getgrnam_r
.f(name
, grdst
, buf
, buflen
, &errno
);
4567 case NSS_STATUS_SUCCESS
:
4570 case NSS_STATUS_NOTFOUND
:
4575 case NSS_STATUS_TRYAGAIN
:
4588 static struct group
*nwrap_module_getgrgid(struct nwrap_backend
*b
,
4591 static struct group grp
;
4593 static int buflen
= 1000;
4596 if (b
->symbols
->_nss_getgrgid_r
.f
== NULL
) {
4601 buf
= (char *)malloc(buflen
);
4605 status
= b
->symbols
->_nss_getgrgid_r
.f(gid
, &grp
, buf
, buflen
, &errno
);
4606 if (status
== NSS_STATUS_TRYAGAIN
) {
4608 buf
= (char *)realloc(buf
, buflen
);
4614 if (status
== NSS_STATUS_NOTFOUND
) {
4618 if (status
!= NSS_STATUS_SUCCESS
) {
4625 static int nwrap_module_getgrgid_r(struct nwrap_backend
*b
,
4626 gid_t gid
, struct group
*grdst
,
4627 char *buf
, size_t buflen
, struct group
**grdstp
)
4633 if (b
->symbols
->_nss_getgrgid_r
.f
== NULL
) {
4637 ret
= b
->symbols
->_nss_getgrgid_r
.f(gid
, grdst
, buf
, buflen
, &errno
);
4639 case NSS_STATUS_SUCCESS
:
4642 case NSS_STATUS_NOTFOUND
:
4647 case NSS_STATUS_TRYAGAIN
:
4660 static void nwrap_module_setgrent(struct nwrap_backend
*b
)
4662 if (b
->symbols
->_nss_setgrent
.f
== NULL
) {
4666 b
->symbols
->_nss_setgrent
.f();
4669 static struct group
*nwrap_module_getgrent(struct nwrap_backend
*b
)
4671 static struct group grp
;
4673 static int buflen
= 1024;
4676 if (b
->symbols
->_nss_getgrent_r
.f
== NULL
) {
4681 buf
= (char *)malloc(buflen
);
4685 status
= b
->symbols
->_nss_getgrent_r
.f(&grp
, buf
, buflen
, &errno
);
4686 if (status
== NSS_STATUS_TRYAGAIN
) {
4688 buf
= (char *)realloc(buf
, buflen
);
4694 if (status
== NSS_STATUS_NOTFOUND
) {
4698 if (status
!= NSS_STATUS_SUCCESS
) {
4705 static int nwrap_module_getgrent_r(struct nwrap_backend
*b
,
4706 struct group
*grdst
, char *buf
,
4707 size_t buflen
, struct group
**grdstp
)
4713 if (b
->symbols
->_nss_getgrent_r
.f
== NULL
) {
4717 ret
= b
->symbols
->_nss_getgrent_r
.f(grdst
, buf
, buflen
, &errno
);
4719 case NSS_STATUS_SUCCESS
:
4722 case NSS_STATUS_NOTFOUND
:
4727 case NSS_STATUS_TRYAGAIN
:
4740 static void nwrap_module_endgrent(struct nwrap_backend
*b
)
4742 if (b
->symbols
->_nss_endgrent
.f
== NULL
) {
4746 b
->symbols
->_nss_endgrent
.f();
4749 static struct hostent
*nwrap_module_gethostbyaddr(struct nwrap_backend
*b
,
4751 socklen_t len
, int type
)
4753 static struct hostent he
;
4754 static char *buf
= NULL
;
4755 static size_t buflen
= 1000;
4758 if (b
->symbols
->_nss_gethostbyaddr_r
.f
== NULL
) {
4763 buf
= (char *)malloc(buflen
);
4769 status
= b
->symbols
->_nss_gethostbyaddr_r
.f(addr
,
4777 if (status
== NSS_STATUS_TRYAGAIN
) {
4781 p
= (char *)realloc(buf
, buflen
);
4789 if (status
== NSS_STATUS_NOTFOUND
) {
4793 if (status
!= NSS_STATUS_SUCCESS
) {
4801 static int nwrap_module_gethostbyname2_r(struct nwrap_backend
*b
,
4802 const char *name
, int af
,
4803 struct hostent
*hedst
,
4804 char *buf
, size_t buflen
,
4805 struct hostent
**hedstp
)
4811 if (b
->symbols
->_nss_gethostbyname2_r
.f
== NULL
) {
4815 status
= b
->symbols
->_nss_gethostbyname2_r
.f(name
,
4823 case NSS_STATUS_SUCCESS
:
4826 case NSS_STATUS_NOTFOUND
:
4831 case NSS_STATUS_TRYAGAIN
:
4844 static struct hostent
*nwrap_module_gethostbyname(struct nwrap_backend
*b
,
4847 static struct hostent he
;
4848 static char *buf
= NULL
;
4849 static size_t buflen
= 1000;
4852 if (b
->symbols
->_nss_gethostbyname2_r
.f
== NULL
) {
4857 buf
= (char *)malloc(buflen
);
4864 status
= b
->symbols
->_nss_gethostbyname2_r
.f(name
,
4871 if (status
== NSS_STATUS_TRYAGAIN
) {
4875 p
= (char *)realloc(buf
, buflen
);
4883 if (status
== NSS_STATUS_NOTFOUND
) {
4887 if (status
!= NSS_STATUS_SUCCESS
) {
4895 static struct hostent
*nwrap_module_gethostbyname2(struct nwrap_backend
*b
,
4896 const char *name
, int af
)
4898 static struct hostent he
;
4899 static char *buf
= NULL
;
4900 static size_t buflen
= 1000;
4903 if (b
->symbols
->_nss_gethostbyname2_r
.f
== NULL
) {
4908 buf
= (char *)malloc(buflen
);
4915 status
= b
->symbols
->_nss_gethostbyname2_r
.f(name
,
4922 if (status
== NSS_STATUS_TRYAGAIN
) {
4926 p
= (char *)realloc(buf
, buflen
);
4934 if (status
== NSS_STATUS_NOTFOUND
) {
4938 if (status
!= NSS_STATUS_SUCCESS
) {
4946 /****************************************************************************
4948 ***************************************************************************/
4950 static struct passwd
*nwrap_getpwnam(const char *name
)
4955 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4956 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4957 pwd
= b
->ops
->nw_getpwnam(b
, name
);
4966 struct passwd
*getpwnam(const char *name
)
4968 if (!nss_wrapper_enabled()) {
4969 return libc_getpwnam(name
);
4972 return nwrap_getpwnam(name
);
4975 /****************************************************************************
4977 ***************************************************************************/
4979 static int nwrap_getpwnam_r(const char *name
, struct passwd
*pwdst
,
4980 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
4985 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4986 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4987 ret
= b
->ops
->nw_getpwnam_r(b
, name
, pwdst
, buf
, buflen
, pwdstp
);
4988 if (ret
== ENOENT
) {
4997 #ifdef HAVE_GETPWNAM_R
4998 # ifdef HAVE_SOLARIS_GETPWNAM_R
4999 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
5000 char *buf
, int buflen
, struct passwd
**pwdstp
)
5001 # else /* HAVE_SOLARIS_GETPWNAM_R */
5002 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
5003 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
5004 # endif /* HAVE_SOLARIS_GETPWNAM_R */
5006 if (!nss_wrapper_enabled()) {
5007 return libc_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
5010 return nwrap_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
5014 /****************************************************************************
5016 ***************************************************************************/
5018 static struct passwd
*nwrap_getpwuid(uid_t uid
)
5023 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5024 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5025 pwd
= b
->ops
->nw_getpwuid(b
, uid
);
5034 struct passwd
*getpwuid(uid_t uid
)
5036 if (!nss_wrapper_enabled()) {
5037 return libc_getpwuid(uid
);
5040 return nwrap_getpwuid(uid
);
5043 /****************************************************************************
5045 ***************************************************************************/
5047 static int nwrap_getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
5048 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
5053 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5054 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5055 ret
= b
->ops
->nw_getpwuid_r(b
, uid
, pwdst
, buf
, buflen
, pwdstp
);
5056 if (ret
== ENOENT
) {
5065 #ifdef HAVE_SOLARIS_GETPWUID_R
5066 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
5067 char *buf
, int buflen
, struct passwd
**pwdstp
)
5069 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
5070 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
5073 if (!nss_wrapper_enabled()) {
5074 return libc_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
5077 return nwrap_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
5080 /****************************************************************************
5082 ***************************************************************************/
5084 static void nwrap_setpwent(void)
5088 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5089 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5090 b
->ops
->nw_setpwent(b
);
5096 if (!nss_wrapper_enabled()) {
5104 /****************************************************************************
5106 ***************************************************************************/
5108 static struct passwd
*nwrap_getpwent(void)
5113 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5114 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5115 pwd
= b
->ops
->nw_getpwent(b
);
5124 struct passwd
*getpwent(void)
5126 if (!nss_wrapper_enabled()) {
5127 return libc_getpwent();
5130 return nwrap_getpwent();
5133 /****************************************************************************
5135 ***************************************************************************/
5137 #ifdef HAVE_GETPWENT_R
5138 static int nwrap_getpwent_r(struct passwd
*pwdst
, char *buf
,
5139 size_t buflen
, struct passwd
**pwdstp
)
5144 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5145 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5146 ret
= b
->ops
->nw_getpwent_r(b
, pwdst
, buf
, buflen
, pwdstp
);
5147 if (ret
== ENOENT
) {
5156 # ifdef HAVE_SOLARIS_GETPWENT_R
5157 struct passwd
*getpwent_r(struct passwd
*pwdst
, char *buf
, int buflen
)
5159 struct passwd
*pwdstp
= NULL
;
5162 if (!nss_wrapper_enabled()) {
5163 return libc_getpwent_r(pwdst
, buf
, buflen
);
5165 rc
= nwrap_getpwent_r(pwdst
, buf
, buflen
, &pwdstp
);
5172 # else /* HAVE_SOLARIS_GETPWENT_R */
5173 int getpwent_r(struct passwd
*pwdst
, char *buf
,
5174 size_t buflen
, struct passwd
**pwdstp
)
5176 if (!nss_wrapper_enabled()) {
5177 return libc_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
5180 return nwrap_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
5182 # endif /* HAVE_SOLARIS_GETPWENT_R */
5183 #endif /* HAVE_GETPWENT_R */
5185 /****************************************************************************
5187 ***************************************************************************/
5189 static void nwrap_endpwent(void)
5193 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5194 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5195 b
->ops
->nw_endpwent(b
);
5201 if (!nss_wrapper_enabled()) {
5209 /****************************************************************************
5211 ***************************************************************************/
5213 static int nwrap_initgroups(const char *user
, gid_t group
)
5217 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5218 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5221 rc
= b
->ops
->nw_initgroups(b
, user
, group
);
5231 int initgroups(const char *user
, gid_t group
)
5233 if (!nss_wrapper_enabled()) {
5234 return libc_initgroups(user
, group
);
5237 return nwrap_initgroups(user
, group
);
5240 /****************************************************************************
5242 ***************************************************************************/
5244 static struct group
*nwrap_getgrnam(const char *name
)
5249 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5250 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5251 grp
= b
->ops
->nw_getgrnam(b
, name
);
5260 struct group
*getgrnam(const char *name
)
5262 if (!nss_wrapper_enabled()) {
5263 return libc_getgrnam(name
);
5266 return nwrap_getgrnam(name
);
5269 /****************************************************************************
5271 ***************************************************************************/
5273 static int nwrap_getgrnam_r(const char *name
, struct group
*grdst
,
5274 char *buf
, size_t buflen
, struct group
**grdstp
)
5279 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5280 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5281 ret
= b
->ops
->nw_getgrnam_r(b
, name
, grdst
, buf
, buflen
, grdstp
);
5282 if (ret
== ENOENT
) {
5291 #ifdef HAVE_GETGRNAM_R
5292 # ifdef HAVE_SOLARIS_GETGRNAM_R
5293 int getgrnam_r(const char *name
, struct group
*grp
,
5294 char *buf
, int buflen
, struct group
**pgrp
)
5295 # else /* HAVE_SOLARIS_GETGRNAM_R */
5296 int getgrnam_r(const char *name
, struct group
*grp
,
5297 char *buf
, size_t buflen
, struct group
**pgrp
)
5298 # endif /* HAVE_SOLARIS_GETGRNAM_R */
5300 if (!nss_wrapper_enabled()) {
5301 return libc_getgrnam_r(name
,
5308 return nwrap_getgrnam_r(name
, grp
, buf
, buflen
, pgrp
);
5310 #endif /* HAVE_GETGRNAM_R */
5312 /****************************************************************************
5314 ***************************************************************************/
5316 static struct group
*nwrap_getgrgid(gid_t gid
)
5321 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5322 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5323 grp
= b
->ops
->nw_getgrgid(b
, gid
);
5332 struct group
*getgrgid(gid_t gid
)
5334 if (!nss_wrapper_enabled()) {
5335 return libc_getgrgid(gid
);
5338 return nwrap_getgrgid(gid
);
5341 /****************************************************************************
5343 ***************************************************************************/
5345 static int nwrap_getgrgid_r(gid_t gid
, struct group
*grdst
,
5346 char *buf
, size_t buflen
, struct group
**grdstp
)
5351 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5352 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5353 ret
= b
->ops
->nw_getgrgid_r(b
, gid
, grdst
, buf
, buflen
, grdstp
);
5354 if (ret
== ENOENT
) {
5363 #ifdef HAVE_GETGRGID_R
5364 # ifdef HAVE_SOLARIS_GETGRGID_R
5365 int getgrgid_r(gid_t gid
, struct group
*grdst
,
5366 char *buf
, int buflen
, struct group
**grdstp
)
5367 # else /* HAVE_SOLARIS_GETGRGID_R */
5368 int getgrgid_r(gid_t gid
, struct group
*grdst
,
5369 char *buf
, size_t buflen
, struct group
**grdstp
)
5370 # endif /* HAVE_SOLARIS_GETGRGID_R */
5372 if (!nss_wrapper_enabled()) {
5373 return libc_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
5376 return nwrap_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
5380 /****************************************************************************
5382 ***************************************************************************/
5384 static void nwrap_setgrent(void)
5388 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5389 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5390 b
->ops
->nw_setgrent(b
);
5394 #ifdef HAVE_BSD_SETGRENT
5400 if (!nss_wrapper_enabled()) {
5408 #ifdef HAVE_BSD_SETGRENT
5415 /****************************************************************************
5417 ***************************************************************************/
5419 static struct group
*nwrap_getgrent(void)
5424 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5425 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5426 grp
= b
->ops
->nw_getgrent(b
);
5435 struct group
*getgrent(void)
5437 if (!nss_wrapper_enabled()) {
5438 return libc_getgrent();
5441 return nwrap_getgrent();
5444 /****************************************************************************
5446 ***************************************************************************/
5448 #ifdef HAVE_GETGRENT_R
5449 static int nwrap_getgrent_r(struct group
*grdst
, char *buf
,
5450 size_t buflen
, struct group
**grdstp
)
5455 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5456 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5457 ret
= b
->ops
->nw_getgrent_r(b
, grdst
, buf
, buflen
, grdstp
);
5458 if (ret
== ENOENT
) {
5467 # ifdef HAVE_SOLARIS_GETGRENT_R
5468 struct group
*getgrent_r(struct group
*src
, char *buf
, int buflen
)
5470 struct group
*grdstp
= NULL
;
5473 if (!nss_wrapper_enabled()) {
5474 return libc_getgrent_r(src
, buf
, buflen
);
5477 rc
= nwrap_getgrent_r(src
, buf
, buflen
, &grdstp
);
5484 # else /* HAVE_SOLARIS_GETGRENT_R */
5485 int getgrent_r(struct group
*src
, char *buf
,
5486 size_t buflen
, struct group
**grdstp
)
5488 if (!nss_wrapper_enabled()) {
5489 return libc_getgrent_r(src
, buf
, buflen
, grdstp
);
5492 return nwrap_getgrent_r(src
, buf
, buflen
, grdstp
);
5494 # endif /* HAVE_SOLARIS_GETGRENT_R */
5495 #endif /* HAVE_GETGRENT_R */
5497 /****************************************************************************
5499 ***************************************************************************/
5501 static void nwrap_endgrent(void)
5505 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5506 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5507 b
->ops
->nw_endgrent(b
);
5513 if (!nss_wrapper_enabled()) {
5521 /****************************************************************************
5523 ***************************************************************************/
5525 #ifdef HAVE_GETGROUPLIST
5526 static int nwrap_getgrouplist(const char *user
, gid_t group
,
5527 gid_t
*groups
, int *ngroups
)
5533 NWRAP_LOG(NWRAP_LOG_DEBUG
, "getgrouplist called for %s", user
);
5535 groups_tmp
= (gid_t
*)malloc(count
* sizeof(gid_t
));
5537 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
5541 groups_tmp
[0] = group
;
5544 while ((grp
= nwrap_getgrent()) != NULL
) {
5547 NWRAP_LOG(NWRAP_LOG_DEBUG
,
5548 "Inspecting %s for group membership",
5551 for (i
=0; grp
->gr_mem
&& grp
->gr_mem
[i
] != NULL
; i
++) {
5553 if (group
!= grp
->gr_gid
&&
5554 (strcmp(user
, grp
->gr_mem
[i
]) == 0)) {
5556 NWRAP_LOG(NWRAP_LOG_DEBUG
,
5557 "%s is member of %s",
5561 groups_tmp
= (gid_t
*)realloc(groups_tmp
, (count
+ 1) * sizeof(gid_t
));
5563 NWRAP_LOG(NWRAP_LOG_ERROR
,
5568 groups_tmp
[count
] = grp
->gr_gid
;
5577 NWRAP_LOG(NWRAP_LOG_DEBUG
,
5578 "%s is member of %d groups",
5581 if (*ngroups
< count
) {
5588 memcpy(groups
, groups_tmp
, count
* sizeof(gid_t
));
5594 int getgrouplist(const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
)
5596 if (!nss_wrapper_enabled()) {
5597 return libc_getgrouplist(user
, group
, groups
, ngroups
);
5600 return nwrap_getgrouplist(user
, group
, groups
, ngroups
);
5604 /**********************************************************
5606 **********************************************************/
5608 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
5610 #ifdef HAVE_SETSPENT
5611 static void nwrap_setspent(void)
5613 nwrap_files_setspent();
5618 if (!nss_wrapper_shadow_enabled()) {
5625 static struct spwd
*nwrap_getspent(void)
5627 return nwrap_files_getspent();
5630 struct spwd
*getspent(void)
5632 if (!nss_wrapper_shadow_enabled()) {
5636 return nwrap_getspent();
5639 static void nwrap_endspent(void)
5641 nwrap_files_endspent();
5646 if (!nss_wrapper_shadow_enabled()) {
5652 #endif /* HAVE_SETSPENT */
5654 static struct spwd
*nwrap_getspnam(const char *name
)
5656 return nwrap_files_getspnam(name
);
5659 struct spwd
*getspnam(const char *name
)
5661 if (!nss_wrapper_shadow_enabled()) {
5665 return nwrap_getspnam(name
);
5668 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
5670 /**********************************************************
5672 **********************************************************/
5674 static void nwrap_sethostent(int stayopen
) {
5675 (void) stayopen
; /* ignored */
5677 nwrap_files_sethostent();
5680 #ifdef HAVE_SOLARIS_SETHOSTENT
5681 int sethostent(int stayopen
)
5683 if (!nss_wrapper_hosts_enabled()) {
5684 libc_sethostent(stayopen
);
5688 nwrap_sethostent(stayopen
);
5692 #else /* HAVE_SOLARIS_SETHOSTENT */
5693 void sethostent(int stayopen
)
5695 if (!nss_wrapper_hosts_enabled()) {
5696 libc_sethostent(stayopen
);
5700 nwrap_sethostent(stayopen
);
5702 #endif /* HAVE_SOLARIS_SETHOSTENT */
5704 static struct hostent
*nwrap_gethostent(void)
5706 return nwrap_files_gethostent();
5709 struct hostent
*gethostent(void) {
5710 if (!nss_wrapper_hosts_enabled()) {
5711 return libc_gethostent();
5714 return nwrap_gethostent();
5717 static void nwrap_endhostent(void) {
5718 nwrap_files_endhostent();
5721 #ifdef HAVE_SOLARIS_ENDHOSTENT
5722 int endhostent(void)
5724 if (!nss_wrapper_hosts_enabled()) {
5733 #else /* HAVE_SOLARIS_ENDHOSTENT */
5734 void endhostent(void)
5736 if (!nss_wrapper_hosts_enabled()) {
5743 #endif /* HAVE_SOLARIS_ENDHOSTENT */
5747 /* BSD implementation stores data in thread local storage but GLIBC does not */
5748 static __thread
struct hostent user_he
;
5749 static __thread
struct nwrap_vector user_addrlist
;
5751 static struct hostent user_he
;
5752 static struct nwrap_vector user_addrlist
;
5755 static struct hostent
*nwrap_files_gethostbyname(struct nwrap_backend
*b
,
5760 (void) b
; /* unused */
5762 ret
= nwrap_files_internal_gethostbyname(name
, AF_UNSPEC
, &user_he
,
5771 static struct hostent
*nwrap_gethostbyname(const char *name
)
5774 struct hostent
*he
= NULL
;
5776 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5777 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5778 he
= b
->ops
->nw_gethostbyname(b
, name
);
5787 struct hostent
*gethostbyname(const char *name
)
5789 if (!nss_wrapper_hosts_enabled()) {
5790 return libc_gethostbyname(name
);
5793 return nwrap_gethostbyname(name
);
5796 /* This is a GNU extension - Also can be found on BSD systems */
5797 #ifdef HAVE_GETHOSTBYNAME2
5799 /* BSD implementation stores data in thread local storage but GLIBC not */
5800 static __thread
struct hostent user_he2
;
5801 static __thread
struct nwrap_vector user_addrlist2
;
5803 static struct hostent user_he2
;
5804 static struct nwrap_vector user_addrlist2
;
5807 static struct hostent
*nwrap_files_gethostbyname2(struct nwrap_backend
*b
,
5808 const char *name
, int af
)
5812 (void) b
; /* unused */
5814 ret
= nwrap_files_internal_gethostbyname(name
, af
, &user_he2
,
5823 static struct hostent
*nwrap_gethostbyname2(const char *name
, int af
)
5826 struct hostent
*he
= NULL
;
5828 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5829 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5830 he
= b
->ops
->nw_gethostbyname2(b
, name
, af
);
5839 struct hostent
*gethostbyname2(const char *name
, int af
)
5841 if (!nss_wrapper_hosts_enabled()) {
5842 return libc_gethostbyname2(name
, af
);
5845 return nwrap_gethostbyname2(name
, af
);
5849 static struct hostent
*nwrap_gethostbyaddr(const void *addr
,
5850 socklen_t len
, int type
)
5853 struct hostent
*he
= NULL
;
5855 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
5856 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
5857 he
= b
->ops
->nw_gethostbyaddr(b
, addr
, len
, type
);
5866 struct hostent
*gethostbyaddr(const void *addr
,
5867 socklen_t len
, int type
)
5869 if (!nss_wrapper_hosts_enabled()) {
5870 return libc_gethostbyaddr(addr
, len
, type
);
5873 return nwrap_gethostbyaddr(addr
, len
, type
);
5876 static const struct addrinfo default_hints
=
5878 .ai_flags
= AI_ADDRCONFIG
|AI_V4MAPPED
,
5879 .ai_family
= AF_UNSPEC
,
5884 .ai_canonname
= NULL
,
5888 static int nwrap_convert_he_ai(const struct hostent
*he
,
5889 unsigned short port
,
5890 const struct addrinfo
*hints
,
5891 struct addrinfo
**pai
,
5892 bool skip_canonname
)
5894 struct addrinfo
*ai
;
5901 switch (he
->h_addrtype
) {
5903 socklen
= sizeof(struct sockaddr_in
);
5907 socklen
= sizeof(struct sockaddr_in6
);
5914 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
) + socklen
);
5919 ai
->ai_flags
= hints
->ai_flags
;
5920 ai
->ai_family
= he
->h_addrtype
;
5921 ai
->ai_socktype
= hints
->ai_socktype
;
5922 ai
->ai_protocol
= hints
->ai_protocol
;
5923 ai
->ai_canonname
= NULL
;
5925 if (ai
->ai_socktype
== 0) {
5926 ai
->ai_socktype
= SOCK_DGRAM
;
5928 if (ai
->ai_protocol
== 0) {
5929 if (ai
->ai_socktype
== SOCK_DGRAM
) {
5930 ai
->ai_protocol
= IPPROTO_UDP
;
5931 } else if (ai
->ai_socktype
== SOCK_STREAM
) {
5932 ai
->ai_protocol
= IPPROTO_TCP
;
5936 ai
->ai_addrlen
= socklen
;
5937 ai
->ai_addr
= (void *)(ai
+ 1);
5939 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
5940 ai
->ai_addr
->sa_len
= socklen
;
5942 ai
->ai_addr
->sa_family
= he
->h_addrtype
;
5944 switch (he
->h_addrtype
) {
5948 struct sockaddr
*sa
;
5949 struct sockaddr_in
*in
;
5952 addr
.sa
= ai
->ai_addr
;
5954 memset(addr
.in
, 0, sizeof(struct sockaddr_in
));
5956 addr
.in
->sin_port
= htons(port
);
5957 addr
.in
->sin_family
= AF_INET
;
5959 memset(addr
.in
->sin_zero
,
5961 sizeof (addr
.in
->sin_zero
));
5962 memcpy(&(addr
.in
->sin_addr
),
5972 struct sockaddr
*sa
;
5973 struct sockaddr_in6
*in6
;
5976 addr
.sa
= ai
->ai_addr
;
5978 memset(addr
.in6
, 0, sizeof(struct sockaddr_in6
));
5980 addr
.in6
->sin6_port
= htons(port
);
5981 addr
.in6
->sin6_family
= AF_INET6
;
5983 memcpy(&addr
.in6
->sin6_addr
,
5993 if (he
->h_name
&& !skip_canonname
) {
5994 ai
->ai_canonname
= strdup(he
->h_name
);
5995 if (ai
->ai_canonname
== NULL
) {
6005 static int nwrap_getaddrinfo(const char *node
,
6006 const char *service
,
6007 const struct addrinfo
*hints
,
6008 struct addrinfo
**res
)
6010 struct addrinfo
*ai
= NULL
;
6011 unsigned short port
= 0;
6021 .family
= AF_UNSPEC
,
6025 if (node
== NULL
&& service
== NULL
) {
6029 if (hints
== NULL
) {
6030 hints
= &default_hints
;
6034 hints.ai_flags contains invalid flags; or, hints.ai_flags
6035 included AI_CANONNAME and name was NULL.
6037 if ((hints
->ai_flags
& AI_CANONNAME
) && (node
== NULL
)) {
6038 return EAI_BADFLAGS
;
6041 /* If no node has been specified, let glibc deal with it */
6044 struct addrinfo
*p
= NULL
;
6046 ret
= libc_getaddrinfo(node
, service
, hints
, &p
);
6054 if (service
!= NULL
&& service
[0] != '\0') {
6055 const char *proto
= NULL
;
6061 sl
= strtol(service
, &end_ptr
, 10);
6063 if (*end_ptr
== '\0') {
6066 } else if (hints
->ai_flags
& AI_NUMERICSERV
) {
6070 if (hints
->ai_protocol
!= 0) {
6071 struct protoent
*pent
;
6073 pent
= getprotobynumber(hints
->ai_protocol
);
6075 proto
= pent
->p_name
;
6079 s
= getservbyname(service
, proto
);
6083 port
= ntohs(s
->s_port
);
6088 rc
= inet_pton(AF_INET
, node
, &addr
.in
.v4
);
6090 addr
.family
= AF_INET
;
6093 if (addr
.family
== AF_UNSPEC
) {
6094 rc
= inet_pton(AF_INET6
, node
, &addr
.in
.v6
);
6096 addr
.family
= AF_INET6
;
6101 if (addr
.family
== AF_UNSPEC
) {
6102 if (hints
->ai_flags
& AI_NUMERICHOST
) {
6105 } else if ((hints
->ai_family
!= AF_UNSPEC
) &&
6106 (hints
->ai_family
!= addr
.family
))
6108 return EAI_ADDRFAMILY
;
6111 rc
= nwrap_files_getaddrinfo(node
, port
, hints
, &ai
);
6114 struct addrinfo
*p
= NULL
;
6116 ret
= libc_getaddrinfo(node
, service
, hints
, &p
);
6120 * nwrap_files_getaddrinfo failed, but libc was
6121 * successful -- use the result from libc.
6131 * If the socktype was not specified, duplicate
6132 * each ai returned, so that we have variants for
6135 if (hints
->ai_socktype
== 0) {
6136 struct addrinfo
*ai_cur
;
6138 /* freeaddrinfo() frees ai_canonname and ai so allocate them */
6139 for (ai_cur
= ai
; ai_cur
!= NULL
; ai_cur
= ai_cur
->ai_next
) {
6140 struct addrinfo
*ai_new
;
6142 /* duplicate the current entry */
6144 ai_new
= malloc(sizeof(struct addrinfo
));
6145 if (ai_new
== NULL
) {
6150 memcpy(ai_new
, ai_cur
, sizeof(struct addrinfo
));
6151 ai_new
->ai_next
= NULL
;
6153 /* We need a deep copy or freeaddrinfo() will blow up */
6154 if (ai_cur
->ai_canonname
!= NULL
) {
6155 ai_new
->ai_canonname
=
6156 strdup(ai_cur
->ai_canonname
);
6159 if (ai_cur
->ai_socktype
== SOCK_DGRAM
) {
6160 ai_new
->ai_socktype
= SOCK_STREAM
;
6161 } else if (ai_cur
->ai_socktype
== SOCK_STREAM
) {
6162 ai_new
->ai_socktype
= SOCK_DGRAM
;
6164 if (ai_cur
->ai_protocol
== IPPROTO_TCP
) {
6165 ai_new
->ai_protocol
= IPPROTO_UDP
;
6166 } else if (ai_cur
->ai_protocol
== IPPROTO_UDP
) {
6167 ai_new
->ai_protocol
= IPPROTO_TCP
;
6170 /* now insert the new entry */
6172 ai_new
->ai_next
= ai_cur
->ai_next
;
6173 ai_cur
->ai_next
= ai_new
;
6175 /* and move on (don't duplicate the new entry) */
6186 int getaddrinfo(const char *node
, const char *service
,
6187 const struct addrinfo
*hints
,
6188 struct addrinfo
**res
)
6190 if (!nss_wrapper_hosts_enabled()) {
6191 return libc_getaddrinfo(node
, service
, hints
, res
);
6194 return nwrap_getaddrinfo(node
, service
, hints
, res
);
6197 static int nwrap_getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
6198 char *host
, size_t hostlen
,
6199 char *serv
, size_t servlen
,
6203 struct servent
*service
;
6211 if (sa
== NULL
|| salen
< sizeof(sa_family_t
)) {
6215 if ((flags
& NI_NAMEREQD
) && host
== NULL
&& serv
== NULL
) {
6219 type
= sa
->sa_family
;
6223 const struct sockaddr
*sa
;
6224 const struct sockaddr_in
*in
;
6227 if (salen
< sizeof(struct sockaddr_in
)) {
6233 addr
= &(a
.in
->sin_addr
);
6234 addrlen
= sizeof(a
.in
->sin_addr
);
6235 port
= ntohs(a
.in
->sin_port
);
6241 const struct sockaddr
*sa
;
6242 const struct sockaddr_in6
*in6
;
6245 if (salen
< sizeof(struct sockaddr_in6
)) {
6251 addr
= &(a
.in6
->sin6_addr
);
6252 addrlen
= sizeof(a
.in6
->sin6_addr
);
6253 port
= ntohs(a
.in6
->sin6_port
);
6263 if ((flags
& NI_NUMERICHOST
) == 0) {
6264 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
6265 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
6266 he
= b
->ops
->nw_gethostbyaddr(b
, addr
, addrlen
, type
);
6271 if ((flags
& NI_NAMEREQD
) && (he
== NULL
|| he
->h_name
== NULL
))
6274 if (he
!= NULL
&& he
->h_name
!= NULL
) {
6275 if (strlen(he
->h_name
) >= hostlen
)
6276 return EAI_OVERFLOW
;
6277 snprintf(host
, hostlen
, "%s", he
->h_name
);
6278 if (flags
& NI_NOFQDN
)
6279 host
[strcspn(host
, ".")] = '\0';
6281 if (inet_ntop(type
, addr
, host
, hostlen
) == NULL
)
6282 return (errno
== ENOSPC
) ? EAI_OVERFLOW
: EAI_FAIL
;
6288 if ((flags
& NI_NUMERICSERV
) == 0) {
6289 proto
= (flags
& NI_DGRAM
) ? "udp" : "tcp";
6290 service
= getservbyport(htons(port
), proto
);
6292 if (service
!= NULL
) {
6293 if (strlen(service
->s_name
) >= servlen
)
6294 return EAI_OVERFLOW
;
6295 snprintf(serv
, servlen
, "%s", service
->s_name
);
6297 if (snprintf(serv
, servlen
, "%u", port
) >= (int) servlen
)
6298 return EAI_OVERFLOW
;
6305 #ifdef HAVE_LINUX_GETNAMEINFO
6306 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
6307 char *host
, socklen_t hostlen
,
6308 char *serv
, socklen_t servlen
,
6310 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
6311 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
6312 char *host
, socklen_t hostlen
,
6313 char *serv
, socklen_t servlen
,
6316 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
6317 char *host
, size_t hostlen
,
6318 char *serv
, size_t servlen
,
6322 if (!nss_wrapper_hosts_enabled()) {
6323 return libc_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
6326 return nwrap_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
6329 static int nwrap_gethostname(char *name
, size_t len
)
6331 const char *hostname
= getenv("NSS_WRAPPER_HOSTNAME");
6333 if (strlen(hostname
) >= len
) {
6334 errno
= ENAMETOOLONG
;
6337 snprintf(name
, len
, "%s", hostname
);
6342 #ifdef HAVE_SOLARIS_GETHOSTNAME
6343 int gethostname(char *name
, int len
)
6344 #else /* HAVE_SOLARIS_GETHOSTNAME */
6345 int gethostname(char *name
, size_t len
)
6346 #endif /* HAVE_SOLARIS_GETHOSTNAME */
6348 if (!nwrap_hostname_enabled()) {
6349 return libc_gethostname(name
, len
);
6352 return nwrap_gethostname(name
, len
);
6355 /****************************
6357 ***************************/
6358 void nwrap_constructor(void)
6361 * If we hold a lock and the application forks, then the child
6362 * is not able to unlock the mutex and we are in a deadlock.
6364 * Setting these handlers should prevent such deadlocks.
6366 pthread_atfork(&nwrap_thread_prepare
,
6367 &nwrap_thread_parent
,
6368 &nwrap_thread_child
);
6370 /* Do not call nwrap_init() here. */
6373 /****************************
6375 ***************************/
6378 * This function is called when the library is unloaded and makes sure that
6379 * sockets get closed and the unix file for the socket are unlinked.
6381 void nwrap_destructor(void)
6386 if (nwrap_main_global
!= NULL
) {
6387 struct nwrap_main
*m
= nwrap_main_global
;
6390 if (m
->libc
!= NULL
) {
6391 if (m
->libc
->handle
!= NULL
) {
6392 dlclose(m
->libc
->handle
);
6394 if (m
->libc
->nsl_handle
!= NULL
) {
6395 dlclose(m
->libc
->nsl_handle
);
6397 if (m
->libc
->sock_handle
!= NULL
) {
6398 dlclose(m
->libc
->sock_handle
);
6404 if (m
->backends
!= NULL
) {
6405 for (i
= 0; i
< m
->num_backends
; i
++) {
6406 struct nwrap_backend
*b
= &(m
->backends
[i
]);
6408 if (b
->so_handle
!= NULL
) {
6409 dlclose(b
->so_handle
);
6411 SAFE_FREE(b
->symbols
);
6413 SAFE_FREE(m
->backends
);
6417 if (nwrap_pw_global
.cache
!= NULL
) {
6418 struct nwrap_cache
*c
= nwrap_pw_global
.cache
;
6420 nwrap_files_cache_unload(c
);
6426 SAFE_FREE(nwrap_pw_global
.list
);
6427 nwrap_pw_global
.num
= 0;
6430 if (nwrap_gr_global
.cache
!= NULL
) {
6431 struct nwrap_cache
*c
= nwrap_gr_global
.cache
;
6433 nwrap_files_cache_unload(c
);
6439 SAFE_FREE(nwrap_gr_global
.list
);
6440 nwrap_pw_global
.num
= 0;
6443 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
6444 if (nwrap_sp_global
.cache
!= NULL
) {
6445 struct nwrap_cache
*c
= nwrap_sp_global
.cache
;
6447 nwrap_files_cache_unload(c
);
6453 nwrap_sp_global
.num
= 0;
6455 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
6457 if (nwrap_he_global
.cache
!= NULL
) {
6458 struct nwrap_cache
*c
= nwrap_he_global
.cache
;
6460 nwrap_files_cache_unload(c
);
6466 nwrap_he_global
.num
= 0;
6469 free(user_addrlist
.items
);
6470 #ifdef HAVE_GETHOSTBYNAME2
6471 free(user_addrlist2
.items
);