2 * Copyright (C) Stefan Metzmacher 2007 <metze@samba.org>
3 * Copyright (C) Guenther Deschner 2009 <gd@samba.org>
4 * Copyright (C) Andreas Schneider 2013 <asn@samba.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the author nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/types.h>
40 #include <sys/socket.h>
54 * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h gives us
55 * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
58 #ifndef _POSIX_PTHREAD_SEMANTICS
59 #define _POSIX_PTHREAD_SEMANTICS
66 #include <arpa/inet.h>
67 #include <netinet/in.h>
71 #if defined(HAVE_NSS_H)
75 typedef enum nss_status NSS_STATUS
;
76 #elif defined(HAVE_NSS_COMMON_H)
78 #include <nss_common.h>
79 #include <nss_dbdefs.h>
82 typedef nss_status_t NSS_STATUS
;
84 # define NSS_STATUS_SUCCESS NSS_SUCCESS
85 # define NSS_STATUS_NOTFOUND NSS_NOTFOUND
86 # define NSS_STATUS_UNAVAIL NSS_UNAVAIL
87 # define NSS_STATUS_TRYAGAIN NSS_TRYAGAIN
89 # error "No nsswitch support detected"
93 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
101 #define EAI_NODATA EAI_NONAME
104 #ifndef EAI_ADDRFAMILY
105 #define EAI_ADDRFAMILY EAI_FAMILY
109 #define __STRING(x) #x
112 #ifndef __STRINGSTRING
113 #define __STRINGSTRING(x) __STRING(x)
117 #define __LINESTR__ __STRINGSTRING(__LINE__)
121 #define __location__ __FILE__ ":" __LINESTR__
125 #define DNS_NAME_MAX 255
128 /* GCC have printf type attribute check. */
129 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
130 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
132 #define PRINTF_ATTRIBUTE(a,b)
133 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
135 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
136 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
138 #define DESTRUCTOR_ATTRIBUTE
139 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
141 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
143 enum nwrap_dbglvl_e
{
151 # define NWRAP_LOG(...)
154 static void nwrap_log(enum nwrap_dbglvl_e dbglvl
, const char *func
, const char *format
, ...) PRINTF_ATTRIBUTE(3, 4);
155 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
157 static void nwrap_log(enum nwrap_dbglvl_e dbglvl
,
159 const char *format
, ...)
164 unsigned int lvl
= 0;
167 d
= getenv("NSS_WRAPPER_DEBUGLEVEL");
172 va_start(va
, format
);
173 vsnprintf(buffer
, sizeof(buffer
), format
, va
);
178 case NWRAP_LOG_ERROR
:
180 "NWRAP_ERROR(%d) - %s: %s\n",
185 "NWRAP_WARN(%d) - %s: %s\n",
188 case NWRAP_LOG_DEBUG
:
190 "NWRAP_DEBUG(%d) - %s: %s\n",
193 case NWRAP_LOG_TRACE
:
195 "NWRAP_TRACE(%d) - %s: %s\n",
201 #endif /* NDEBUG NWRAP_LOG */
203 struct nwrap_libc_fns
{
204 struct passwd
*(*_libc_getpwnam
)(const char *name
);
205 int (*_libc_getpwnam_r
)(const char *name
, struct passwd
*pwd
,
206 char *buf
, size_t buflen
, struct passwd
**result
);
207 struct passwd
*(*_libc_getpwuid
)(uid_t uid
);
208 int (*_libc_getpwuid_r
)(uid_t uid
, struct passwd
*pwd
, char *buf
, size_t buflen
, struct passwd
**result
);
209 void (*_libc_setpwent
)(void);
210 struct passwd
*(*_libc_getpwent
)(void);
211 #ifdef HAVE_SOLARIS_GETPWENT_R
212 struct passwd
*(*_libc_getpwent_r
)(struct passwd
*pwbuf
, char *buf
, size_t buflen
);
214 int (*_libc_getpwent_r
)(struct passwd
*pwbuf
, char *buf
, size_t buflen
, struct passwd
**pwbufp
);
216 void (*_libc_endpwent
)(void);
217 int (*_libc_initgroups
)(const char *user
, gid_t gid
);
218 struct group
*(*_libc_getgrnam
)(const char *name
);
219 int (*_libc_getgrnam_r
)(const char *name
, struct group
*grp
, char *buf
, size_t buflen
, struct group
**result
);
220 struct group
*(*_libc_getgrgid
)(gid_t gid
);
221 int (*_libc_getgrgid_r
)(gid_t gid
, struct group
*grp
, char *buf
, size_t buflen
, struct group
**result
);
222 void (*_libc_setgrent
)(void);
223 struct group
*(*_libc_getgrent
)(void);
224 #ifdef HAVE_SOLARIS_GETGRENT_R
225 struct group
*(*_libc_getgrent_r
)(struct group
*group
, char *buf
, size_t buflen
);
227 int (*_libc_getgrent_r
)(struct group
*group
, char *buf
, size_t buflen
, struct group
**result
);
229 void (*_libc_endgrent
)(void);
230 int (*_libc_getgrouplist
)(const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
);
232 void (*_libc_sethostent
)(int stayopen
);
233 struct hostent
*(*_libc_gethostent
)(void);
234 void (*_libc_endhostent
)(void);
236 struct hostent
*(*_libc_gethostbyname
)(const char *name
);
237 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
238 struct hostent
*(*_libc_gethostbyname2
)(const char *name
, int af
);
240 struct hostent
*(*_libc_gethostbyaddr
)(const void *addr
, socklen_t len
, int type
);
242 int (*_libc_getaddrinfo
)(const char *node
, const char *service
,
243 const struct addrinfo
*hints
,
244 struct addrinfo
**res
);
245 int (*_libc_getnameinfo
)(const struct sockaddr
*sa
, socklen_t salen
,
246 char *host
, size_t hostlen
,
247 char *serv
, size_t servlen
,
249 int (*_libc_gethostname
)(char *name
, size_t len
);
250 #ifdef HAVE_GETHOSTBYNAME_R
251 int (*_libc_gethostbyname_r
)(const char *name
,
253 char *buf
, size_t buflen
,
254 struct hostent
**result
, int *h_errnop
);
256 #ifdef HAVE_GETHOSTBYADDR_R
257 int (*_libc_gethostbyaddr_r
)(const void *addr
, socklen_t len
, int type
,
259 char *buf
, size_t buflen
,
260 struct hostent
**result
, int *h_errnop
);
264 struct nwrap_module_nss_fns
{
265 NSS_STATUS (*_nss_getpwnam_r
)(const char *name
, struct passwd
*result
, char *buffer
,
266 size_t buflen
, int *errnop
);
267 NSS_STATUS (*_nss_getpwuid_r
)(uid_t uid
, struct passwd
*result
, char *buffer
,
268 size_t buflen
, int *errnop
);
269 NSS_STATUS (*_nss_setpwent
)(void);
270 NSS_STATUS (*_nss_getpwent_r
)(struct passwd
*result
, char *buffer
,
271 size_t buflen
, int *errnop
);
272 NSS_STATUS (*_nss_endpwent
)(void);
273 NSS_STATUS (*_nss_initgroups
)(const char *user
, gid_t group
, long int *start
,
274 long int *size
, gid_t
**groups
, long int limit
, int *errnop
);
275 NSS_STATUS (*_nss_getgrnam_r
)(const char *name
, struct group
*result
, char *buffer
,
276 size_t buflen
, int *errnop
);
277 NSS_STATUS (*_nss_getgrgid_r
)(gid_t gid
, struct group
*result
, char *buffer
,
278 size_t buflen
, int *errnop
);
279 NSS_STATUS (*_nss_setgrent
)(void);
280 NSS_STATUS (*_nss_getgrent_r
)(struct group
*result
, char *buffer
,
281 size_t buflen
, int *errnop
);
282 NSS_STATUS (*_nss_endgrent
)(void);
285 struct nwrap_backend
{
289 struct nwrap_ops
*ops
;
290 struct nwrap_module_nss_fns
*fns
;
294 struct passwd
* (*nw_getpwnam
)(struct nwrap_backend
*b
,
296 int (*nw_getpwnam_r
)(struct nwrap_backend
*b
,
297 const char *name
, struct passwd
*pwdst
,
298 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
299 struct passwd
* (*nw_getpwuid
)(struct nwrap_backend
*b
,
301 int (*nw_getpwuid_r
)(struct nwrap_backend
*b
,
302 uid_t uid
, struct passwd
*pwdst
,
303 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
304 void (*nw_setpwent
)(struct nwrap_backend
*b
);
305 struct passwd
* (*nw_getpwent
)(struct nwrap_backend
*b
);
306 int (*nw_getpwent_r
)(struct nwrap_backend
*b
,
307 struct passwd
*pwdst
, char *buf
,
308 size_t buflen
, struct passwd
**pwdstp
);
309 void (*nw_endpwent
)(struct nwrap_backend
*b
);
310 int (*nw_initgroups
)(struct nwrap_backend
*b
,
311 const char *user
, gid_t group
);
312 struct group
* (*nw_getgrnam
)(struct nwrap_backend
*b
,
314 int (*nw_getgrnam_r
)(struct nwrap_backend
*b
,
315 const char *name
, struct group
*grdst
,
316 char *buf
, size_t buflen
, struct group
**grdstp
);
317 struct group
* (*nw_getgrgid
)(struct nwrap_backend
*b
,
319 int (*nw_getgrgid_r
)(struct nwrap_backend
*b
,
320 gid_t gid
, struct group
*grdst
,
321 char *buf
, size_t buflen
, struct group
**grdstp
);
322 void (*nw_setgrent
)(struct nwrap_backend
*b
);
323 struct group
* (*nw_getgrent
)(struct nwrap_backend
*b
);
324 int (*nw_getgrent_r
)(struct nwrap_backend
*b
,
325 struct group
*grdst
, char *buf
,
326 size_t buflen
, struct group
**grdstp
);
327 void (*nw_endgrent
)(struct nwrap_backend
*b
);
330 /* Public prototypes */
332 bool nss_wrapper_enabled(void);
333 bool nss_wrapper_hosts_enabled(void);
335 /* prototypes for files backend */
338 static struct passwd
*nwrap_files_getpwnam(struct nwrap_backend
*b
,
340 static int nwrap_files_getpwnam_r(struct nwrap_backend
*b
,
341 const char *name
, struct passwd
*pwdst
,
342 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
343 static struct passwd
*nwrap_files_getpwuid(struct nwrap_backend
*b
,
345 static int nwrap_files_getpwuid_r(struct nwrap_backend
*b
,
346 uid_t uid
, struct passwd
*pwdst
,
347 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
348 static void nwrap_files_setpwent(struct nwrap_backend
*b
);
349 static struct passwd
*nwrap_files_getpwent(struct nwrap_backend
*b
);
350 static int nwrap_files_getpwent_r(struct nwrap_backend
*b
,
351 struct passwd
*pwdst
, char *buf
,
352 size_t buflen
, struct passwd
**pwdstp
);
353 static void nwrap_files_endpwent(struct nwrap_backend
*b
);
354 static int nwrap_files_initgroups(struct nwrap_backend
*b
,
355 const char *user
, gid_t group
);
356 static struct group
*nwrap_files_getgrnam(struct nwrap_backend
*b
,
358 static int nwrap_files_getgrnam_r(struct nwrap_backend
*b
,
359 const char *name
, struct group
*grdst
,
360 char *buf
, size_t buflen
, struct group
**grdstp
);
361 static struct group
*nwrap_files_getgrgid(struct nwrap_backend
*b
,
363 static int nwrap_files_getgrgid_r(struct nwrap_backend
*b
,
364 gid_t gid
, struct group
*grdst
,
365 char *buf
, size_t buflen
, struct group
**grdstp
);
366 static void nwrap_files_setgrent(struct nwrap_backend
*b
);
367 static struct group
*nwrap_files_getgrent(struct nwrap_backend
*b
);
368 static int nwrap_files_getgrent_r(struct nwrap_backend
*b
,
369 struct group
*grdst
, char *buf
,
370 size_t buflen
, struct group
**grdstp
);
371 static void nwrap_files_endgrent(struct nwrap_backend
*b
);
373 /* prototypes for module backend */
375 static struct passwd
*nwrap_module_getpwent(struct nwrap_backend
*b
);
376 static int nwrap_module_getpwent_r(struct nwrap_backend
*b
,
377 struct passwd
*pwdst
, char *buf
,
378 size_t buflen
, struct passwd
**pwdstp
);
379 static struct passwd
*nwrap_module_getpwnam(struct nwrap_backend
*b
,
381 static int nwrap_module_getpwnam_r(struct nwrap_backend
*b
,
382 const char *name
, struct passwd
*pwdst
,
383 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
384 static struct passwd
*nwrap_module_getpwuid(struct nwrap_backend
*b
,
386 static int nwrap_module_getpwuid_r(struct nwrap_backend
*b
,
387 uid_t uid
, struct passwd
*pwdst
,
388 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
389 static void nwrap_module_setpwent(struct nwrap_backend
*b
);
390 static void nwrap_module_endpwent(struct nwrap_backend
*b
);
391 static struct group
*nwrap_module_getgrent(struct nwrap_backend
*b
);
392 static int nwrap_module_getgrent_r(struct nwrap_backend
*b
,
393 struct group
*grdst
, char *buf
,
394 size_t buflen
, struct group
**grdstp
);
395 static struct group
*nwrap_module_getgrnam(struct nwrap_backend
*b
,
397 static int nwrap_module_getgrnam_r(struct nwrap_backend
*b
,
398 const char *name
, struct group
*grdst
,
399 char *buf
, size_t buflen
, struct group
**grdstp
);
400 static struct group
*nwrap_module_getgrgid(struct nwrap_backend
*b
,
402 static int nwrap_module_getgrgid_r(struct nwrap_backend
*b
,
403 gid_t gid
, struct group
*grdst
,
404 char *buf
, size_t buflen
, struct group
**grdstp
);
405 static void nwrap_module_setgrent(struct nwrap_backend
*b
);
406 static void nwrap_module_endgrent(struct nwrap_backend
*b
);
407 static int nwrap_module_initgroups(struct nwrap_backend
*b
,
408 const char *user
, gid_t group
);
410 struct nwrap_ops nwrap_files_ops
= {
411 .nw_getpwnam
= nwrap_files_getpwnam
,
412 .nw_getpwnam_r
= nwrap_files_getpwnam_r
,
413 .nw_getpwuid
= nwrap_files_getpwuid
,
414 .nw_getpwuid_r
= nwrap_files_getpwuid_r
,
415 .nw_setpwent
= nwrap_files_setpwent
,
416 .nw_getpwent
= nwrap_files_getpwent
,
417 .nw_getpwent_r
= nwrap_files_getpwent_r
,
418 .nw_endpwent
= nwrap_files_endpwent
,
419 .nw_initgroups
= nwrap_files_initgroups
,
420 .nw_getgrnam
= nwrap_files_getgrnam
,
421 .nw_getgrnam_r
= nwrap_files_getgrnam_r
,
422 .nw_getgrgid
= nwrap_files_getgrgid
,
423 .nw_getgrgid_r
= nwrap_files_getgrgid_r
,
424 .nw_setgrent
= nwrap_files_setgrent
,
425 .nw_getgrent
= nwrap_files_getgrent
,
426 .nw_getgrent_r
= nwrap_files_getgrent_r
,
427 .nw_endgrent
= nwrap_files_endgrent
,
430 struct nwrap_ops nwrap_module_ops
= {
431 .nw_getpwnam
= nwrap_module_getpwnam
,
432 .nw_getpwnam_r
= nwrap_module_getpwnam_r
,
433 .nw_getpwuid
= nwrap_module_getpwuid
,
434 .nw_getpwuid_r
= nwrap_module_getpwuid_r
,
435 .nw_setpwent
= nwrap_module_setpwent
,
436 .nw_getpwent
= nwrap_module_getpwent
,
437 .nw_getpwent_r
= nwrap_module_getpwent_r
,
438 .nw_endpwent
= nwrap_module_endpwent
,
439 .nw_initgroups
= nwrap_module_initgroups
,
440 .nw_getgrnam
= nwrap_module_getgrnam
,
441 .nw_getgrnam_r
= nwrap_module_getgrnam_r
,
442 .nw_getgrgid
= nwrap_module_getgrgid
,
443 .nw_getgrgid_r
= nwrap_module_getgrgid_r
,
444 .nw_setgrent
= nwrap_module_setgrent
,
445 .nw_getgrent
= nwrap_module_getgrent
,
446 .nw_getgrent_r
= nwrap_module_getgrent_r
,
447 .nw_endgrent
= nwrap_module_endgrent
,
454 struct nwrap_libc_fns
*fns
;
458 const char *nwrap_switch
;
460 struct nwrap_backend
*backends
;
461 struct nwrap_libc
*libc
;
464 struct nwrap_main
*nwrap_main_global
;
465 struct nwrap_main __nwrap_main_global
;
473 bool (*parse_line
)(struct nwrap_cache
*, char *line
);
474 void (*unload
)(struct nwrap_cache
*);
478 struct nwrap_cache
*cache
;
485 struct nwrap_cache __nwrap_cache_pw
;
486 struct nwrap_pw nwrap_pw_global
;
488 static bool nwrap_pw_parse_line(struct nwrap_cache
*nwrap
, char *line
);
489 static void nwrap_pw_unload(struct nwrap_cache
*nwrap
);
492 struct nwrap_cache
*cache
;
499 struct nwrap_cache __nwrap_cache_gr
;
500 struct nwrap_gr nwrap_gr_global
;
502 static bool nwrap_he_parse_line(struct nwrap_cache
*nwrap
, char *line
);
503 static void nwrap_he_unload(struct nwrap_cache
*nwrap
);
505 struct nwrap_addrdata
{
506 unsigned char host_addr
[16]; /* IPv4 or IPv6 address */
507 char *h_addr_ptrs
[2]; /* host_addr pointer + NULL */
510 struct nwrap_entdata
{
511 struct nwrap_addrdata
*addr
;
516 struct nwrap_cache
*cache
;
518 struct nwrap_entdata
*list
;
523 struct nwrap_cache __nwrap_cache_he
;
524 struct nwrap_he nwrap_he_global
;
527 /*********************************************************
529 *********************************************************/
531 static void nwrap_init(void);
532 static bool nwrap_gr_parse_line(struct nwrap_cache
*nwrap
, char *line
);
533 static void nwrap_gr_unload(struct nwrap_cache
*nwrap
);
534 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE
;
536 /*********************************************************
537 * NWRAP LIBC LOADER FUNCTIONS
538 *********************************************************/
547 static const char *nwrap_str_lib(enum nwrap_lib lib
)
554 case NWRAP_LIBSOCKET
:
558 /* Compiler would warn us about unhandled enum value if we get here */
563 static void *nwrap_load_lib_handle(enum nwrap_lib lib
)
565 int flags
= RTLD_LAZY
;
570 flags
|= RTLD_DEEPBIND
;
576 handle
= nwrap_main_global
->libc
->nsl_handle
;
577 if (handle
== NULL
) {
578 for (handle
= NULL
, i
= 10; handle
== NULL
&& i
>= 0; i
--) {
579 char soname
[256] = {0};
581 snprintf(soname
, sizeof(soname
), "libnsl.so.%d", i
);
582 handle
= dlopen(soname
, flags
);
585 nwrap_main_global
->libc
->nsl_handle
= handle
;
590 case NWRAP_LIBSOCKET
:
591 #ifdef HAVE_LIBSOCKET
592 handle
= nwrap_main_global
->libc
->sock_handle
;
593 if (handle
== NULL
) {
594 for (handle
= NULL
, i
= 10; handle
== NULL
&& i
>= 0; i
--) {
595 char soname
[256] = {0};
597 snprintf(soname
, sizeof(soname
), "libsocket.so.%d", i
);
598 handle
= dlopen(soname
, flags
);
601 nwrap_main_global
->libc
->sock_handle
= handle
;
607 handle
= nwrap_main_global
->libc
->handle
;
608 if (handle
== NULL
) {
609 for (handle
= NULL
, i
= 10; handle
== NULL
&& i
>= 0; i
--) {
610 char soname
[256] = {0};
612 snprintf(soname
, sizeof(soname
), "libc.so.%d", i
);
613 handle
= dlopen(soname
, flags
);
616 nwrap_main_global
->libc
->handle
= handle
;
621 if (handle
== NULL
) {
623 handle
= nwrap_main_global
->libc
->handle
624 = nwrap_main_global
->libc
->sock_handle
625 = nwrap_main_global
->libc
->nsl_handle
628 NWRAP_LOG(NWRAP_LOG_ERROR
,
629 "Failed to dlopen library: %s\n",
638 static void *_nwrap_load_lib_function(enum nwrap_lib lib
, const char *fn_name
)
645 handle
= nwrap_load_lib_handle(lib
);
647 func
= dlsym(handle
, fn_name
);
649 NWRAP_LOG(NWRAP_LOG_ERROR
,
650 "Failed to find %s: %s\n",
655 NWRAP_LOG(NWRAP_LOG_TRACE
,
657 fn_name
, nwrap_str_lib(lib
));
661 #define nwrap_load_lib_function(lib, fn_name) \
662 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
663 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
664 _nwrap_load_lib_function(lib, #fn_name); \
670 * Functions expeciall from libc need to be loaded individually, you can't load
671 * all at once or gdb will segfault at startup. The same applies to valgrind and
672 * has probably something todo with with the linker.
673 * So we need load each function at the point it is called the first time.
675 static struct passwd
*libc_getpwnam(const char *name
)
677 nwrap_load_lib_function(NWRAP_LIBC
, getpwnam
);
679 return nwrap_main_global
->libc
->fns
->_libc_getpwnam(name
);
682 #ifdef HAVE_GETPWNAM_R
683 static int libc_getpwnam_r(const char *name
,
687 struct passwd
**result
)
689 #ifdef HAVE___POSIX_GETPWNAM_R
690 if (nwrap_main_global
->libc
->fns
->_libc_getpwnam_r
== NULL
) {
691 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getpwnam_r
) =
692 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getpwnam_r");
695 nwrap_load_lib_function(NWRAP_LIBC
, getpwnam_r
);
698 return nwrap_main_global
->libc
->fns
->_libc_getpwnam_r(name
,
706 static struct passwd
*libc_getpwuid(uid_t uid
)
708 nwrap_load_lib_function(NWRAP_LIBC
, getpwuid
);
710 return nwrap_main_global
->libc
->fns
->_libc_getpwuid(uid
);
713 #ifdef HAVE_GETPWUID_R
714 static int libc_getpwuid_r(uid_t uid
,
718 struct passwd
**result
)
720 #ifdef HAVE___POSIX_GETPWUID_R
721 if (nwrap_main_global
->libc
->fns
->_libc_getpwuid_r
== NULL
) {
722 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getpwuid_r
) =
723 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getpwuid_r");
726 nwrap_load_lib_function(NWRAP_LIBC
, getpwuid_r
);
729 return nwrap_main_global
->libc
->fns
->_libc_getpwuid_r(uid
,
737 static void libc_setpwent(void)
739 nwrap_load_lib_function(NWRAP_LIBC
, setpwent
);
741 nwrap_main_global
->libc
->fns
->_libc_setpwent();
744 static struct passwd
*libc_getpwent(void)
746 nwrap_load_lib_function(NWRAP_LIBC
, getpwent
);
748 return nwrap_main_global
->libc
->fns
->_libc_getpwent();
751 #ifdef HAVE_SOLARIS_GETPWENT_R
752 static struct passwd
*libc_getpwent_r(struct passwd
*pwdst
,
756 nwrap_load_lib_function(NWRAP_LIBC
, getpwent_r
);
758 return nwrap_main_global
->libc
->fns
->_libc_getpwent_r(pwdst
,
762 #else /* HAVE_SOLARIS_GETPWENT_R */
763 static int libc_getpwent_r(struct passwd
*pwdst
,
766 struct passwd
**pwdstp
)
768 nwrap_load_lib_function(NWRAP_LIBC
, getpwent_r
);
770 return nwrap_main_global
->libc
->fns
->_libc_getpwent_r(pwdst
,
775 #endif /* HAVE_SOLARIS_GETPWENT_R */
777 static void libc_endpwent(void)
779 nwrap_load_lib_function(NWRAP_LIBC
, endpwent
);
781 nwrap_main_global
->libc
->fns
->_libc_endpwent();
784 static int libc_initgroups(const char *user
, gid_t gid
)
786 nwrap_load_lib_function(NWRAP_LIBC
, initgroups
);
788 return nwrap_main_global
->libc
->fns
->_libc_initgroups(user
, gid
);
791 static struct group
*libc_getgrnam(const char *name
)
793 nwrap_load_lib_function(NWRAP_LIBC
, getgrnam
);
795 return nwrap_main_global
->libc
->fns
->_libc_getgrnam(name
);
798 #ifdef HAVE_GETGRNAM_R
799 static int libc_getgrnam_r(const char *name
,
803 struct group
**result
)
805 #ifdef HAVE___POSIX_GETGRNAM_R
806 if (nwrap_main_global
->libc
->fns
->_libc_getgrnam_r
== NULL
) {
807 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getgrnam_r
) =
808 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getgrnam_r");
811 nwrap_load_lib_function(NWRAP_LIBC
, getgrnam_r
);
814 return nwrap_main_global
->libc
->fns
->_libc_getgrnam_r(name
,
822 static struct group
*libc_getgrgid(gid_t gid
)
824 nwrap_load_lib_function(NWRAP_LIBC
, getgrgid
);
826 return nwrap_main_global
->libc
->fns
->_libc_getgrgid(gid
);
829 #ifdef HAVE_GETGRGID_R
830 static int libc_getgrgid_r(gid_t gid
,
834 struct group
**result
)
836 #ifdef HAVE___POSIX_GETGRGID_R
837 if (nwrap_main_global
->libc
->fns
->_libc_getgrgid_r
== NULL
) {
838 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getgrgid_r
) =
839 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getgrgid_r");
842 nwrap_load_lib_function(NWRAP_LIBC
, getgrgid_r
);
845 return nwrap_main_global
->libc
->fns
->_libc_getgrgid_r(gid
,
853 static void libc_setgrent(void)
855 nwrap_load_lib_function(NWRAP_LIBC
, setgrent
);
857 nwrap_main_global
->libc
->fns
->_libc_setgrent();
860 static struct group
*libc_getgrent(void)
862 nwrap_load_lib_function(NWRAP_LIBC
, getgrent
);
864 return nwrap_main_global
->libc
->fns
->_libc_getgrent();
867 #ifdef HAVE_GETGRENT_R
868 #ifdef HAVE_SOLARIS_GETGRENT_R
869 static struct group
*libc_getgrent_r(struct group
*group
,
873 nwrap_load_lib_function(NWRAP_LIBC
, getgrent_r
);
875 return nwrap_main_global
->libc
->fns
->_libc_getgrent_r(group
,
879 #else /* !HAVE_SOLARIS_GETGRENT_R */
880 static int libc_getgrent_r(struct group
*group
,
883 struct group
**result
)
885 nwrap_load_lib_function(NWRAP_LIBC
, getgrent_r
);
887 return nwrap_main_global
->libc
->fns
->_libc_getgrent_r(group
,
892 #endif /* HAVE_SOLARIS_GETGRENT_R */
893 #endif /* HAVE_GETGRENT_R */
895 static void libc_endgrent(void)
897 nwrap_load_lib_function(NWRAP_LIBC
, endgrent
);
899 nwrap_main_global
->libc
->fns
->_libc_endgrent();
902 #ifdef HAVE_GETGROUPLIST
903 static int libc_getgrouplist(const char *user
,
908 nwrap_load_lib_function(NWRAP_LIBC
, getgrouplist
);
910 return nwrap_main_global
->libc
->fns
->_libc_getgrouplist(user
,
917 static void libc_sethostent(int stayopen
)
919 nwrap_load_lib_function(NWRAP_LIBNSL
, sethostent
);
921 nwrap_main_global
->libc
->fns
->_libc_sethostent(stayopen
);
924 static struct hostent
*libc_gethostent(void)
926 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostent
);
928 return nwrap_main_global
->libc
->fns
->_libc_gethostent();
931 static void libc_endhostent(void)
933 nwrap_load_lib_function(NWRAP_LIBNSL
, endhostent
);
935 nwrap_main_global
->libc
->fns
->_libc_endhostent();
938 static struct hostent
*libc_gethostbyname(const char *name
)
940 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname
);
942 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname(name
);
945 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
946 static struct hostent
*libc_gethostbyname2(const char *name
, int af
)
948 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname2
);
950 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname2(name
, af
);
954 static struct hostent
*libc_gethostbyaddr(const void *addr
,
958 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyaddr
);
960 return nwrap_main_global
->libc
->fns
->_libc_gethostbyaddr(addr
,
965 static int libc_gethostname(char *name
, size_t len
)
967 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostname
);
969 return nwrap_main_global
->libc
->fns
->_libc_gethostname(name
, len
);
972 #ifdef HAVE_GETHOSTBYNAME_R
973 static int libc_gethostbyname_r(const char *name
,
977 struct hostent
**result
,
980 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname_r
);
982 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname_r(name
,
991 #ifdef HAVE_GETHOSTBYADDR_R
992 static int libc_gethostbyaddr_r(const void *addr
,
998 struct hostent
**result
,
1001 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyaddr_r
);
1003 return nwrap_main_global
->libc
->fns
->_libc_gethostbyaddr_r(addr
,
1014 static int libc_getaddrinfo(const char *node
,
1015 const char *service
,
1016 const struct addrinfo
*hints
,
1017 struct addrinfo
**res
)
1019 nwrap_load_lib_function(NWRAP_LIBSOCKET
, getaddrinfo
);
1021 return nwrap_main_global
->libc
->fns
->_libc_getaddrinfo(node
,
1027 static int libc_getnameinfo(const struct sockaddr
*sa
,
1035 nwrap_load_lib_function(NWRAP_LIBSOCKET
, getnameinfo
);
1037 return nwrap_main_global
->libc
->fns
->_libc_getnameinfo(sa
,
1046 /*********************************************************
1047 * NWRAP NSS MODULE LOADER FUNCTIONS
1048 *********************************************************/
1050 static void *nwrap_load_module_fn(struct nwrap_backend
*b
,
1051 const char *fn_name
)
1056 if (!b
->so_handle
) {
1057 NWRAP_LOG(NWRAP_LOG_ERROR
, "No handle");
1061 if (asprintf(&s
, "_nss_%s_%s", b
->name
, fn_name
) == -1) {
1062 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1066 res
= dlsym(b
->so_handle
, s
);
1068 NWRAP_LOG(NWRAP_LOG_ERROR
,
1069 "Cannot find function %s in %s",
1077 static struct nwrap_module_nss_fns
*nwrap_load_module_fns(struct nwrap_backend
*b
)
1079 struct nwrap_module_nss_fns
*fns
;
1081 if (!b
->so_handle
) {
1085 fns
= (struct nwrap_module_nss_fns
*)malloc(sizeof(struct nwrap_module_nss_fns
));
1090 *(void **)(&fns
->_nss_getpwnam_r
) =
1091 nwrap_load_module_fn(b
, "getpwnam_r");
1092 *(void **)(&fns
->_nss_getpwuid_r
) =
1093 nwrap_load_module_fn(b
, "getpwuid_r");
1094 *(void **)(&fns
->_nss_setpwent
) =
1095 nwrap_load_module_fn(b
, "setpwent");
1096 *(void **)(&fns
->_nss_getpwent_r
) =
1097 nwrap_load_module_fn(b
, "getpwent_r");
1098 *(void **)(&fns
->_nss_endpwent
) =
1099 nwrap_load_module_fn(b
, "endpwent");
1100 *(void **)(&fns
->_nss_initgroups
) =
1101 nwrap_load_module_fn(b
, "initgroups_dyn");
1102 *(void **)(&fns
->_nss_getgrnam_r
) =
1103 nwrap_load_module_fn(b
, "getgrnam_r");
1104 *(void **)(&fns
->_nss_getgrgid_r
)=
1105 nwrap_load_module_fn(b
, "getgrgid_r");
1106 *(void **)(&fns
->_nss_setgrent
) =
1107 nwrap_load_module_fn(b
, "setgrent");
1108 *(void **)(&fns
->_nss_getgrent_r
) =
1109 nwrap_load_module_fn(b
, "getgrent_r");
1110 *(void **)(&fns
->_nss_endgrent
) =
1111 nwrap_load_module_fn(b
, "endgrent");
1116 static void *nwrap_load_module(const char *so_path
)
1120 if (!so_path
|| !strlen(so_path
)) {
1124 h
= dlopen(so_path
, RTLD_LAZY
);
1126 NWRAP_LOG(NWRAP_LOG_ERROR
,
1127 "Cannot open shared library %s",
1135 static bool nwrap_module_init(const char *name
,
1136 struct nwrap_ops
*ops
,
1137 const char *so_path
,
1139 struct nwrap_backend
**backends
)
1141 struct nwrap_backend
*b
;
1143 *backends
= (struct nwrap_backend
*)realloc(*backends
,
1144 sizeof(struct nwrap_backend
) * ((*num_backends
) + 1));
1146 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1150 b
= &((*backends
)[*num_backends
]);
1154 b
->so_path
= so_path
;
1156 if (so_path
!= NULL
) {
1157 b
->so_handle
= nwrap_load_module(so_path
);
1158 b
->fns
= nwrap_load_module_fns(b
);
1159 if (b
->fns
== NULL
) {
1163 b
->so_handle
= NULL
;
1172 static void nwrap_libc_init(struct nwrap_main
*r
)
1174 r
->libc
= malloc(sizeof(struct nwrap_libc
));
1175 if (r
->libc
== NULL
) {
1176 printf("Failed to allocate memory for libc");
1179 ZERO_STRUCTP(r
->libc
);
1181 r
->libc
->fns
= malloc(sizeof(struct nwrap_libc_fns
));
1182 if (r
->libc
->fns
== NULL
) {
1183 printf("Failed to allocate memory for libc functions");
1186 ZERO_STRUCTP(r
->libc
->fns
);
1189 static void nwrap_backend_init(struct nwrap_main
*r
)
1191 const char *module_so_path
= getenv("NSS_WRAPPER_MODULE_SO_PATH");
1192 const char *module_fn_name
= getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1194 r
->num_backends
= 0;
1197 if (!nwrap_module_init("files", &nwrap_files_ops
, NULL
,
1200 NWRAP_LOG(NWRAP_LOG_ERROR
,
1201 "Failed to initialize 'files' backend");
1205 if (module_so_path
!= NULL
&&
1206 module_so_path
[0] != '\0' &&
1207 module_fn_name
!= NULL
&&
1208 module_fn_name
[0] != '\0') {
1209 if (!nwrap_module_init(module_fn_name
,
1214 NWRAP_LOG(NWRAP_LOG_ERROR
,
1215 "Failed to initialize '%s' backend",
1222 static void nwrap_init(void)
1224 static bool initialized
;
1226 if (initialized
) return;
1229 nwrap_main_global
= &__nwrap_main_global
;
1231 nwrap_libc_init(nwrap_main_global
);
1233 nwrap_backend_init(nwrap_main_global
);
1235 nwrap_pw_global
.cache
= &__nwrap_cache_pw
;
1237 nwrap_pw_global
.cache
->path
= getenv("NSS_WRAPPER_PASSWD");
1238 nwrap_pw_global
.cache
->fd
= -1;
1239 nwrap_pw_global
.cache
->private_data
= &nwrap_pw_global
;
1240 nwrap_pw_global
.cache
->parse_line
= nwrap_pw_parse_line
;
1241 nwrap_pw_global
.cache
->unload
= nwrap_pw_unload
;
1243 nwrap_gr_global
.cache
= &__nwrap_cache_gr
;
1245 nwrap_gr_global
.cache
->path
= getenv("NSS_WRAPPER_GROUP");
1246 nwrap_gr_global
.cache
->fd
= -1;
1247 nwrap_gr_global
.cache
->private_data
= &nwrap_gr_global
;
1248 nwrap_gr_global
.cache
->parse_line
= nwrap_gr_parse_line
;
1249 nwrap_gr_global
.cache
->unload
= nwrap_gr_unload
;
1251 nwrap_he_global
.cache
= &__nwrap_cache_he
;
1253 nwrap_he_global
.cache
->path
= getenv("NSS_WRAPPER_HOSTS");
1254 nwrap_he_global
.cache
->fd
= -1;
1255 nwrap_he_global
.cache
->private_data
= &nwrap_he_global
;
1256 nwrap_he_global
.cache
->parse_line
= nwrap_he_parse_line
;
1257 nwrap_he_global
.cache
->unload
= nwrap_he_unload
;
1260 bool nss_wrapper_enabled(void)
1264 if (nwrap_pw_global
.cache
->path
== NULL
||
1265 nwrap_pw_global
.cache
->path
[0] == '\0') {
1268 if (nwrap_gr_global
.cache
->path
== NULL
||
1269 nwrap_gr_global
.cache
->path
[0] == '\0') {
1276 bool nss_wrapper_hosts_enabled(void)
1280 if (nwrap_he_global
.cache
->path
== NULL
||
1281 nwrap_he_global
.cache
->path
[0] == '\0') {
1288 static bool nwrap_hostname_enabled(void)
1292 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL
) {
1299 static bool nwrap_parse_file(struct nwrap_cache
*nwrap
)
1302 uint8_t *buf
= NULL
;
1305 if (nwrap
->st
.st_size
== 0) {
1306 NWRAP_LOG(NWRAP_LOG_DEBUG
, "size == 0");
1310 if (nwrap
->st
.st_size
> INT32_MAX
) {
1311 NWRAP_LOG(NWRAP_LOG_ERROR
,
1312 "Size[%u] larger than INT32_MAX",
1313 (unsigned)nwrap
->st
.st_size
);
1317 ret
= lseek(nwrap
->fd
, 0, SEEK_SET
);
1319 NWRAP_LOG(NWRAP_LOG_ERROR
, "lseek - rc=%d\n", ret
);
1323 buf
= (uint8_t *)malloc(nwrap
->st
.st_size
+ 1);
1325 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1329 ret
= read(nwrap
->fd
, buf
, nwrap
->st
.st_size
);
1330 if (ret
!= nwrap
->st
.st_size
) {
1331 NWRAP_LOG(NWRAP_LOG_ERROR
,
1333 (unsigned)nwrap
->st
.st_size
, ret
);
1337 buf
[nwrap
->st
.st_size
] = '\0';
1339 nline
= (char *)buf
;
1340 while (nline
!= NULL
&& nline
[0] != '\0') {
1348 e
= strchr(line
, '\n');
1359 if (strlen(line
) == 0) {
1363 ok
= nwrap
->parse_line(nwrap
, line
);
1378 static void nwrap_files_cache_unload(struct nwrap_cache
*nwrap
)
1380 nwrap
->unload(nwrap
);
1382 if (nwrap
->buf
) free(nwrap
->buf
);
1387 static void nwrap_files_cache_reload(struct nwrap_cache
*nwrap
)
1392 bool retried
= false;
1395 if (nwrap
->fd
< 0) {
1396 nwrap
->fd
= open(nwrap
->path
, O_RDONLY
);
1397 if (nwrap
->fd
< 0) {
1398 NWRAP_LOG(NWRAP_LOG_ERROR
,
1399 "Unable to open '%s' readonly %d:%s",
1400 nwrap
->path
, nwrap
->fd
,
1404 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Open '%s'", nwrap
->path
);
1407 ret
= fstat(nwrap
->fd
, &st
);
1409 NWRAP_LOG(NWRAP_LOG_ERROR
,
1410 "fstat(%s) - %d:%s",
1417 if (retried
== false && st
.st_nlink
== 0) {
1418 /* maybe someone has replaced the file... */
1419 NWRAP_LOG(NWRAP_LOG_TRACE
,
1420 "st_nlink == 0, reopen %s",
1423 memset(&nwrap
->st
, 0, sizeof(nwrap
->st
));
1429 if (st
.st_mtime
== nwrap
->st
.st_mtime
) {
1430 NWRAP_LOG(NWRAP_LOG_TRACE
,
1431 "st_mtime[%u] hasn't changed, skip reload",
1432 (unsigned)st
.st_mtime
);
1436 NWRAP_LOG(NWRAP_LOG_TRACE
,
1437 "st_mtime has changed [%u] => [%u], start reload",
1438 (unsigned)st
.st_mtime
,
1439 (unsigned)nwrap
->st
.st_mtime
);
1443 nwrap_files_cache_unload(nwrap
);
1445 ok
= nwrap_parse_file(nwrap
);
1447 NWRAP_LOG(NWRAP_LOG_ERROR
, "Failed to reload %s", nwrap
->path
);
1448 nwrap_files_cache_unload(nwrap
);
1451 NWRAP_LOG(NWRAP_LOG_TRACE
, "Reloaded %s", nwrap
->path
);
1455 * the caller has to call nwrap_unload() on failure
1457 static bool nwrap_pw_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1459 struct nwrap_pw
*nwrap_pw
;
1466 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
1468 list_size
= sizeof(*nwrap_pw
->list
) * (nwrap_pw
->num
+1);
1469 pw
= (struct passwd
*)realloc(nwrap_pw
->list
, list_size
);
1471 NWRAP_LOG(NWRAP_LOG_ERROR
,
1472 "realloc(%u) failed",
1473 (unsigned)list_size
);
1476 nwrap_pw
->list
= pw
;
1478 pw
= &nwrap_pw
->list
[nwrap_pw
->num
];
1485 NWRAP_LOG(NWRAP_LOG_ERROR
,
1486 "Invalid line[%s]: '%s'",
1496 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]\n", pw
->pw_name
);
1501 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1509 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]\n", pw
->pw_passwd
);
1514 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1520 pw
->pw_uid
= (uid_t
)strtoul(c
, &e
, 10);
1522 NWRAP_LOG(NWRAP_LOG_ERROR
,
1523 "Invalid line[%s]: '%s' - %s",
1524 line
, c
, strerror(errno
));
1528 NWRAP_LOG(NWRAP_LOG_ERROR
,
1529 "Invalid line[%s]: '%s' - %s",
1530 line
, c
, strerror(errno
));
1534 NWRAP_LOG(NWRAP_LOG_ERROR
,
1535 "Invalid line[%s]: '%s' - %s",
1536 line
, c
, strerror(errno
));
1541 NWRAP_LOG(NWRAP_LOG_TRACE
, "uid[%u]", pw
->pw_uid
);
1546 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1552 pw
->pw_gid
= (gid_t
)strtoul(c
, &e
, 10);
1554 NWRAP_LOG(NWRAP_LOG_ERROR
,
1555 "Invalid line[%s]: '%s' - %s",
1556 line
, c
, strerror(errno
));
1560 NWRAP_LOG(NWRAP_LOG_ERROR
,
1561 "Invalid line[%s]: '%s' - %s",
1562 line
, c
, strerror(errno
));
1566 NWRAP_LOG(NWRAP_LOG_ERROR
,
1567 "Invalid line[%s]: '%s' - %s",
1568 line
, c
, strerror(errno
));
1573 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]\n", pw
->pw_gid
);
1578 NWRAP_LOG(NWRAP_LOG_ERROR
, "invalid line[%s]: '%s'", line
, c
);
1586 NWRAP_LOG(NWRAP_LOG_TRACE
, "gecos[%s]", pw
->pw_gecos
);
1591 NWRAP_LOG(NWRAP_LOG_ERROR
, "'%s'", c
);
1599 NWRAP_LOG(NWRAP_LOG_TRACE
, "dir[%s]", pw
->pw_dir
);
1603 NWRAP_LOG(NWRAP_LOG_TRACE
, "shell[%s]", pw
->pw_shell
);
1605 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1606 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1607 pw
->pw_name
, pw
->pw_passwd
,
1608 pw
->pw_uid
, pw
->pw_gid
,
1609 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
1615 static void nwrap_pw_unload(struct nwrap_cache
*nwrap
)
1617 struct nwrap_pw
*nwrap_pw
;
1618 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
1620 if (nwrap_pw
->list
) free(nwrap_pw
->list
);
1622 nwrap_pw
->list
= NULL
;
1627 static int nwrap_pw_copy_r(const struct passwd
*src
, struct passwd
*dst
,
1628 char *buf
, size_t buflen
, struct passwd
**dstp
)
1634 first
= src
->pw_name
;
1636 last
= src
->pw_shell
;
1637 while (*last
) last
++;
1639 ofs
= PTR_DIFF(last
+ 1, first
);
1641 if (ofs
> (off_t
) buflen
) {
1645 memcpy(buf
, first
, ofs
);
1647 ofs
= PTR_DIFF(src
->pw_name
, first
);
1648 dst
->pw_name
= buf
+ ofs
;
1649 ofs
= PTR_DIFF(src
->pw_passwd
, first
);
1650 dst
->pw_passwd
= buf
+ ofs
;
1651 dst
->pw_uid
= src
->pw_uid
;
1652 dst
->pw_gid
= src
->pw_gid
;
1653 ofs
= PTR_DIFF(src
->pw_gecos
, first
);
1654 dst
->pw_gecos
= buf
+ ofs
;
1655 ofs
= PTR_DIFF(src
->pw_dir
, first
);
1656 dst
->pw_dir
= buf
+ ofs
;
1657 ofs
= PTR_DIFF(src
->pw_shell
, first
);
1658 dst
->pw_shell
= buf
+ ofs
;
1668 * the caller has to call nwrap_unload() on failure
1670 static bool nwrap_gr_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1672 struct nwrap_gr
*nwrap_gr
;
1680 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
1682 list_size
= sizeof(*nwrap_gr
->list
) * (nwrap_gr
->num
+1);
1683 gr
= (struct group
*)realloc(nwrap_gr
->list
, list_size
);
1685 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc failed");
1688 nwrap_gr
->list
= gr
;
1690 gr
= &nwrap_gr
->list
[nwrap_gr
->num
];
1697 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1705 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]", gr
->gr_name
);
1710 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1718 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]", gr
->gr_passwd
);
1723 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1729 gr
->gr_gid
= (gid_t
)strtoul(c
, &e
, 10);
1731 NWRAP_LOG(NWRAP_LOG_ERROR
,
1732 "Invalid line[%s]: '%s' - %s",
1733 line
, c
, strerror(errno
));
1737 NWRAP_LOG(NWRAP_LOG_ERROR
,
1738 "Invalid line[%s]: '%s' - %s",
1739 line
, c
, strerror(errno
));
1743 NWRAP_LOG(NWRAP_LOG_ERROR
,
1744 "Invalid line[%s]: '%s' - %s",
1745 line
, c
, strerror(errno
));
1750 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]", gr
->gr_gid
);
1753 gr
->gr_mem
= (char **)malloc(sizeof(char *));
1755 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1758 gr
->gr_mem
[0] = NULL
;
1760 for(nummem
=0; p
; nummem
++) {
1770 if (strlen(c
) == 0) {
1774 m_size
= sizeof(char *) * (nummem
+2);
1775 m
= (char **)realloc(gr
->gr_mem
, m_size
);
1777 NWRAP_LOG(NWRAP_LOG_ERROR
,
1778 "realloc(%zd) failed",
1783 gr
->gr_mem
[nummem
] = c
;
1784 gr
->gr_mem
[nummem
+1] = NULL
;
1786 NWRAP_LOG(NWRAP_LOG_TRACE
,
1788 nummem
, gr
->gr_mem
[nummem
]);
1791 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1792 "Added group[%s:%s:%u:] with %u members",
1793 gr
->gr_name
, gr
->gr_passwd
, gr
->gr_gid
, nummem
);
1799 static void nwrap_gr_unload(struct nwrap_cache
*nwrap
)
1802 struct nwrap_gr
*nwrap_gr
;
1803 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
1805 if (nwrap_gr
->list
) {
1806 for (i
=0; i
< nwrap_gr
->num
; i
++) {
1807 if (nwrap_gr
->list
[i
].gr_mem
) {
1808 free(nwrap_gr
->list
[i
].gr_mem
);
1811 free(nwrap_gr
->list
);
1814 nwrap_gr
->list
= NULL
;
1819 static int nwrap_gr_copy_r(const struct group
*src
, struct group
*dst
,
1820 char *buf
, size_t buflen
, struct group
**dstp
)
1830 first
= src
->gr_name
;
1832 lastm
= src
->gr_mem
;
1839 last
= src
->gr_passwd
;
1841 while (*last
) last
++;
1843 ofsb
= PTR_DIFF(last
+ 1, first
);
1844 ofsm
= PTR_DIFF(lastm
+ 1, src
->gr_mem
);
1846 if ((ofsb
+ ofsm
) > (off_t
) buflen
) {
1850 memcpy(buf
, first
, ofsb
);
1851 memcpy(buf
+ ofsb
, src
->gr_mem
, ofsm
);
1853 ofs
= PTR_DIFF(src
->gr_name
, first
);
1854 dst
->gr_name
= buf
+ ofs
;
1855 ofs
= PTR_DIFF(src
->gr_passwd
, first
);
1856 dst
->gr_passwd
= buf
+ ofs
;
1857 dst
->gr_gid
= src
->gr_gid
;
1859 dst
->gr_mem
= (char **)(buf
+ ofsb
);
1860 for (i
=0; src
->gr_mem
[i
]; i
++) {
1861 ofs
= PTR_DIFF(src
->gr_mem
[i
], first
);
1862 dst
->gr_mem
[i
] = buf
+ ofs
;
1872 static bool nwrap_he_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1874 struct nwrap_he
*nwrap_he
= (struct nwrap_he
*)nwrap
->private_data
;
1875 struct nwrap_entdata
*ed
;
1877 bool do_aliases
= true;
1878 int aliases_count
= 0;
1883 list_size
= sizeof(struct nwrap_entdata
) * (nwrap_he
->num
+ 1);
1885 ed
= (struct nwrap_entdata
*)realloc(nwrap_he
->list
, list_size
);
1887 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc[%zd] failed", list_size
);
1890 nwrap_he
->list
= ed
;
1892 /* set it to the last element */
1893 ed
= &(nwrap_he
->list
[nwrap_he
->num
]);
1895 ed
->addr
= malloc(sizeof(struct nwrap_addrdata
));
1896 if (ed
->addr
== NULL
) {
1897 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc[%zd] failed", list_size
);
1907 /* Walk to first char */
1908 for (p
= i
; *p
!= '.' && *p
!= ':' && !isxdigit((int) *p
); p
++) {
1910 NWRAP_LOG(NWRAP_LOG_ERROR
,
1911 "Invalid line[%s]: '%s'",
1917 for (i
= p
; !isspace((int)*p
); p
++) {
1919 NWRAP_LOG(NWRAP_LOG_ERROR
,
1920 "Invalid line[%s]: '%s'",
1928 if (inet_pton(AF_INET
, i
, ed
->addr
->host_addr
)) {
1929 ed
->ht
.h_addrtype
= AF_INET
;
1930 ed
->ht
.h_length
= 4;
1932 } else if (inet_pton(AF_INET6
, i
, ed
->addr
->host_addr
)) {
1933 ed
->ht
.h_addrtype
= AF_INET6
;
1934 ed
->ht
.h_length
= 16;
1937 NWRAP_LOG(NWRAP_LOG_ERROR
,
1938 "Invalid line[%s]: '%s'",
1944 ed
->addr
->h_addr_ptrs
[0] = (char *)ed
->addr
->host_addr
;
1945 ed
->addr
->h_addr_ptrs
[1] = NULL
;
1947 ed
->ht
.h_addr_list
= ed
->addr
->h_addr_ptrs
;
1955 /* Walk to first char */
1956 for (n
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
1958 NWRAP_LOG(NWRAP_LOG_ERROR
,
1959 "Invalid line[%s]: '%s'",
1966 for (n
= p
; !isspace((int)*p
); p
++) {
1977 /* glib's getent always dereferences he->h_aliases */
1978 ed
->ht
.h_aliases
= malloc(sizeof(char *));
1979 if (ed
->ht
.h_aliases
== NULL
) {
1982 ed
->ht
.h_aliases
[0] = NULL
;
1987 while (do_aliases
) {
1993 /* Walk to first char */
1994 for (a
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
2000 /* Only trailing spaces are left */
2005 for (a
= p
; !isspace((int)*p
); p
++) {
2014 aliases
= realloc(ed
->ht
.h_aliases
, sizeof(char *) * (aliases_count
+ 2));
2015 if (aliases
== NULL
) {
2018 ed
->ht
.h_aliases
= aliases
;
2020 aliases
[aliases_count
] = a
;
2021 aliases
[aliases_count
+ 1] = NULL
;
2030 static void nwrap_he_unload(struct nwrap_cache
*nwrap
)
2032 struct nwrap_he
*nwrap_he
=
2033 (struct nwrap_he
*)nwrap
->private_data
;
2036 if (nwrap_he
->list
!= NULL
) {
2037 for (i
= 0; i
< nwrap_he
->num
; i
++) {
2038 if (nwrap_he
->list
[i
].ht
.h_aliases
!= NULL
) {
2039 free(nwrap_he
->list
[i
].ht
.h_aliases
);
2041 if (nwrap_he
->list
[i
].addr
!= NULL
) {
2042 free(nwrap_he
->list
[i
].addr
);
2045 free(nwrap_he
->list
);
2048 nwrap_he
->list
= NULL
;
2054 /* user functions */
2055 static struct passwd
*nwrap_files_getpwnam(struct nwrap_backend
*b
,
2060 (void) b
; /* unused */
2062 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Lookup user %s in files", name
);
2064 nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2066 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
2067 if (strcmp(nwrap_pw_global
.list
[i
].pw_name
, name
) == 0) {
2068 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] found", name
);
2069 return &nwrap_pw_global
.list
[i
];
2071 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2072 "user[%s] does not match [%s]",
2074 nwrap_pw_global
.list
[i
].pw_name
);
2077 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] not found\n", name
);
2083 static int nwrap_files_getpwnam_r(struct nwrap_backend
*b
,
2084 const char *name
, struct passwd
*pwdst
,
2085 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2089 pw
= nwrap_files_getpwnam(b
, name
);
2097 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
2100 static struct passwd
*nwrap_files_getpwuid(struct nwrap_backend
*b
,
2105 (void) b
; /* unused */
2107 nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2109 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
2110 if (nwrap_pw_global
.list
[i
].pw_uid
== uid
) {
2111 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] found", uid
);
2112 return &nwrap_pw_global
.list
[i
];
2114 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2115 "uid[%u] does not match [%u]",
2117 nwrap_pw_global
.list
[i
].pw_uid
);
2120 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] not found\n", uid
);
2126 static int nwrap_files_getpwuid_r(struct nwrap_backend
*b
,
2127 uid_t uid
, struct passwd
*pwdst
,
2128 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2132 pw
= nwrap_files_getpwuid(b
, uid
);
2140 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
2143 /* user enum functions */
2144 static void nwrap_files_setpwent(struct nwrap_backend
*b
)
2146 (void) b
; /* unused */
2148 nwrap_pw_global
.idx
= 0;
2151 static struct passwd
*nwrap_files_getpwent(struct nwrap_backend
*b
)
2155 (void) b
; /* unused */
2157 if (nwrap_pw_global
.idx
== 0) {
2158 nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2161 if (nwrap_pw_global
.idx
>= nwrap_pw_global
.num
) {
2166 pw
= &nwrap_pw_global
.list
[nwrap_pw_global
.idx
++];
2168 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2169 "return user[%s] uid[%u]",
2170 pw
->pw_name
, pw
->pw_uid
);
2175 static int nwrap_files_getpwent_r(struct nwrap_backend
*b
,
2176 struct passwd
*pwdst
, char *buf
,
2177 size_t buflen
, struct passwd
**pwdstp
)
2181 pw
= nwrap_files_getpwent(b
);
2189 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
2192 static void nwrap_files_endpwent(struct nwrap_backend
*b
)
2194 (void) b
; /* unused */
2196 nwrap_pw_global
.idx
= 0;
2199 /* misc functions */
2200 static int nwrap_files_initgroups(struct nwrap_backend
*b
,
2201 const char *user
, gid_t group
)
2203 (void) b
; /* unused */
2204 (void) user
; /* unused */
2205 (void) group
; /* used */
2207 /* TODO: maybe we should also fake this... */
2211 /* group functions */
2212 static struct group
*nwrap_files_getgrnam(struct nwrap_backend
*b
,
2217 (void) b
; /* unused */
2219 nwrap_files_cache_reload(nwrap_gr_global
.cache
);
2221 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
2222 if (strcmp(nwrap_gr_global
.list
[i
].gr_name
, name
) == 0) {
2223 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] found", name
);
2224 return &nwrap_gr_global
.list
[i
];
2226 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2227 "group[%s] does not match [%s]",
2229 nwrap_gr_global
.list
[i
].gr_name
);
2232 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] not found", name
);
2238 static int nwrap_files_getgrnam_r(struct nwrap_backend
*b
,
2239 const char *name
, struct group
*grdst
,
2240 char *buf
, size_t buflen
, struct group
**grdstp
)
2244 gr
= nwrap_files_getgrnam(b
, name
);
2252 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
2255 static struct group
*nwrap_files_getgrgid(struct nwrap_backend
*b
,
2260 (void) b
; /* unused */
2262 nwrap_files_cache_reload(nwrap_gr_global
.cache
);
2264 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
2265 if (nwrap_gr_global
.list
[i
].gr_gid
== gid
) {
2266 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] found", gid
);
2267 return &nwrap_gr_global
.list
[i
];
2269 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2270 "gid[%u] does not match [%u]",
2272 nwrap_gr_global
.list
[i
].gr_gid
);
2275 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] not found", gid
);
2281 static int nwrap_files_getgrgid_r(struct nwrap_backend
*b
,
2282 gid_t gid
, struct group
*grdst
,
2283 char *buf
, size_t buflen
, struct group
**grdstp
)
2287 gr
= nwrap_files_getgrgid(b
, gid
);
2295 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
2298 /* group enum functions */
2299 static void nwrap_files_setgrent(struct nwrap_backend
*b
)
2301 (void) b
; /* unused */
2303 nwrap_gr_global
.idx
= 0;
2306 static struct group
*nwrap_files_getgrent(struct nwrap_backend
*b
)
2310 (void) b
; /* unused */
2312 if (nwrap_gr_global
.idx
== 0) {
2313 nwrap_files_cache_reload(nwrap_gr_global
.cache
);
2316 if (nwrap_gr_global
.idx
>= nwrap_gr_global
.num
) {
2321 gr
= &nwrap_gr_global
.list
[nwrap_gr_global
.idx
++];
2323 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2324 "return group[%s] gid[%u]",
2325 gr
->gr_name
, gr
->gr_gid
);
2330 static int nwrap_files_getgrent_r(struct nwrap_backend
*b
,
2331 struct group
*grdst
, char *buf
,
2332 size_t buflen
, struct group
**grdstp
)
2336 gr
= nwrap_files_getgrent(b
);
2344 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
2347 static void nwrap_files_endgrent(struct nwrap_backend
*b
)
2349 (void) b
; /* unused */
2351 nwrap_gr_global
.idx
= 0;
2354 /* hosts functions */
2355 static struct hostent
*nwrap_files_gethostbyname(const char *name
, int af
)
2358 char canon_name
[DNS_NAME_MAX
] = { 0 };
2362 nwrap_files_cache_reload(nwrap_he_global
.cache
);
2364 name_len
= strlen(name
);
2365 if (name_len
< sizeof(canon_name
) && name
[name_len
- 1] == '.') {
2366 strncpy(canon_name
, name
, name_len
- 1);
2370 for (i
= 0; i
< nwrap_he_global
.num
; i
++) {
2373 he
= &nwrap_he_global
.list
[i
].ht
;
2375 /* Filter by address familiy if provided */
2376 if (af
!= AF_UNSPEC
&& he
->h_addrtype
!= af
) {
2380 if (strcasecmp(he
->h_name
, name
) == 0) {
2381 NWRAP_LOG(NWRAP_LOG_DEBUG
, "name[%s] found", name
);
2385 if (he
->h_aliases
== NULL
) {
2389 for (j
= 0; he
->h_aliases
[j
] != NULL
; j
++) {
2390 if (strcasecmp(he
->h_aliases
[j
], name
) == 0) {
2391 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2403 #ifdef HAVE_GETHOSTBYNAME_R
2404 static int nwrap_gethostbyname_r(const char *name
,
2405 struct hostent
*ret
,
2406 char *buf
, size_t buflen
,
2407 struct hostent
**result
, int *h_errnop
)
2409 *result
= nwrap_files_gethostbyname(name
, AF_UNSPEC
);
2410 if (*result
!= NULL
) {
2411 memset(buf
, '\0', buflen
);
2415 *h_errnop
= h_errno
;
2420 int gethostbyname_r(const char *name
,
2421 struct hostent
*ret
,
2422 char *buf
, size_t buflen
,
2423 struct hostent
**result
, int *h_errnop
)
2425 if (!nss_wrapper_hosts_enabled()) {
2426 return libc_gethostbyname_r(name
,
2434 return nwrap_gethostbyname_r(name
, ret
, buf
, buflen
, result
, h_errnop
);
2438 static struct hostent
*nwrap_files_gethostbyaddr(const void *addr
,
2439 socklen_t len
, int type
)
2442 char ip
[INET6_ADDRSTRLEN
] = {0};
2446 (void) len
; /* unused */
2448 nwrap_files_cache_reload(nwrap_he_global
.cache
);
2450 a
= inet_ntop(type
, addr
, ip
, sizeof(ip
));
2456 for (i
= 0; i
< nwrap_he_global
.num
; i
++) {
2457 he
= &nwrap_he_global
.list
[i
].ht
;
2459 if (he
->h_addrtype
!= type
) {
2463 if (memcmp(addr
, he
->h_addr_list
[0], he
->h_length
) == 0) {
2472 #ifdef HAVE_GETHOSTBYADDR_R
2473 static int nwrap_gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
2474 struct hostent
*ret
,
2475 char *buf
, size_t buflen
,
2476 struct hostent
**result
, int *h_errnop
)
2478 *result
= nwrap_files_gethostbyaddr(addr
, len
, type
);
2479 if (*result
!= NULL
) {
2480 memset(buf
, '\0', buflen
);
2484 *h_errnop
= h_errno
;
2489 int gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
2490 struct hostent
*ret
,
2491 char *buf
, size_t buflen
,
2492 struct hostent
**result
, int *h_errnop
)
2494 if (!nss_wrapper_hosts_enabled()) {
2495 return libc_gethostbyaddr_r(addr
,
2505 return nwrap_gethostbyaddr_r(addr
, len
, type
, ret
, buf
, buflen
, result
, h_errnop
);
2509 /* hosts enum functions */
2510 static void nwrap_files_sethostent(void)
2512 nwrap_he_global
.idx
= 0;
2515 static struct hostent
*nwrap_files_gethostent(void)
2519 if (nwrap_he_global
.idx
== 0) {
2520 nwrap_files_cache_reload(nwrap_he_global
.cache
);
2523 if (nwrap_he_global
.idx
>= nwrap_he_global
.num
) {
2528 he
= &nwrap_he_global
.list
[nwrap_he_global
.idx
++].ht
;
2530 NWRAP_LOG(NWRAP_LOG_DEBUG
, "return hosts[%s]", he
->h_name
);
2535 static void nwrap_files_endhostent(void)
2537 nwrap_he_global
.idx
= 0;
2545 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
2548 static struct passwd
*nwrap_module_getpwnam(struct nwrap_backend
*b
,
2551 static struct passwd pwd
;
2552 static char buf
[1000];
2555 if (!b
->fns
->_nss_getpwnam_r
) {
2559 status
= b
->fns
->_nss_getpwnam_r(name
, &pwd
, buf
, sizeof(buf
), &errno
);
2560 if (status
== NSS_STATUS_NOTFOUND
) {
2563 if (status
!= NSS_STATUS_SUCCESS
) {
2570 static int nwrap_module_getpwnam_r(struct nwrap_backend
*b
,
2571 const char *name
, struct passwd
*pwdst
,
2572 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2576 (void) b
; /* unused */
2577 (void) pwdst
; /* unused */
2578 (void) pwdstp
; /* unused */
2580 if (!b
->fns
->_nss_getpwnam_r
) {
2581 return NSS_STATUS_NOTFOUND
;
2584 ret
= b
->fns
->_nss_getpwnam_r(name
, pwdst
, buf
, buflen
, &errno
);
2586 case NSS_STATUS_SUCCESS
:
2588 case NSS_STATUS_NOTFOUND
:
2593 case NSS_STATUS_TRYAGAIN
:
2606 static struct passwd
*nwrap_module_getpwuid(struct nwrap_backend
*b
,
2609 static struct passwd pwd
;
2610 static char buf
[1000];
2613 if (!b
->fns
->_nss_getpwuid_r
) {
2617 status
= b
->fns
->_nss_getpwuid_r(uid
, &pwd
, buf
, sizeof(buf
), &errno
);
2618 if (status
== NSS_STATUS_NOTFOUND
) {
2621 if (status
!= NSS_STATUS_SUCCESS
) {
2627 static int nwrap_module_getpwuid_r(struct nwrap_backend
*b
,
2628 uid_t uid
, struct passwd
*pwdst
,
2629 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2633 (void) pwdstp
; /* unused */
2635 if (!b
->fns
->_nss_getpwuid_r
) {
2639 ret
= b
->fns
->_nss_getpwuid_r(uid
, pwdst
, buf
, buflen
, &errno
);
2641 case NSS_STATUS_SUCCESS
:
2643 case NSS_STATUS_NOTFOUND
:
2648 case NSS_STATUS_TRYAGAIN
:
2661 static void nwrap_module_setpwent(struct nwrap_backend
*b
)
2663 if (!b
->fns
->_nss_setpwent
) {
2667 b
->fns
->_nss_setpwent();
2670 static struct passwd
*nwrap_module_getpwent(struct nwrap_backend
*b
)
2672 static struct passwd pwd
;
2673 static char buf
[1000];
2676 if (!b
->fns
->_nss_getpwent_r
) {
2680 status
= b
->fns
->_nss_getpwent_r(&pwd
, buf
, sizeof(buf
), &errno
);
2681 if (status
== NSS_STATUS_NOTFOUND
) {
2684 if (status
!= NSS_STATUS_SUCCESS
) {
2690 static int nwrap_module_getpwent_r(struct nwrap_backend
*b
,
2691 struct passwd
*pwdst
, char *buf
,
2692 size_t buflen
, struct passwd
**pwdstp
)
2696 (void) pwdstp
; /* unused */
2698 if (!b
->fns
->_nss_getpwent_r
) {
2702 ret
= b
->fns
->_nss_getpwent_r(pwdst
, buf
, buflen
, &errno
);
2704 case NSS_STATUS_SUCCESS
:
2706 case NSS_STATUS_NOTFOUND
:
2711 case NSS_STATUS_TRYAGAIN
:
2724 static void nwrap_module_endpwent(struct nwrap_backend
*b
)
2726 if (!b
->fns
->_nss_endpwent
) {
2730 b
->fns
->_nss_endpwent();
2733 static int nwrap_module_initgroups(struct nwrap_backend
*b
,
2734 const char *user
, gid_t group
)
2740 if (!b
->fns
->_nss_initgroups
) {
2741 return NSS_STATUS_UNAVAIL
;
2744 return b
->fns
->_nss_initgroups(user
, group
, &start
, &size
, &groups
, 0, &errno
);
2747 static struct group
*nwrap_module_getgrnam(struct nwrap_backend
*b
,
2750 static struct group grp
;
2752 static int buflen
= 1000;
2755 if (!b
->fns
->_nss_getgrnam_r
) {
2760 buf
= (char *)malloc(buflen
);
2763 status
= b
->fns
->_nss_getgrnam_r(name
, &grp
, buf
, buflen
, &errno
);
2764 if (status
== NSS_STATUS_TRYAGAIN
) {
2766 buf
= (char *)realloc(buf
, buflen
);
2772 if (status
== NSS_STATUS_NOTFOUND
) {
2776 if (status
!= NSS_STATUS_SUCCESS
) {
2783 static int nwrap_module_getgrnam_r(struct nwrap_backend
*b
,
2784 const char *name
, struct group
*grdst
,
2785 char *buf
, size_t buflen
, struct group
**grdstp
)
2789 (void) grdstp
; /* unused */
2791 if (!b
->fns
->_nss_getgrnam_r
) {
2795 ret
= b
->fns
->_nss_getgrnam_r(name
, grdst
, buf
, buflen
, &errno
);
2797 case NSS_STATUS_SUCCESS
:
2799 case NSS_STATUS_NOTFOUND
:
2804 case NSS_STATUS_TRYAGAIN
:
2817 static struct group
*nwrap_module_getgrgid(struct nwrap_backend
*b
,
2820 static struct group grp
;
2822 static int buflen
= 1000;
2825 if (!b
->fns
->_nss_getgrgid_r
) {
2830 buf
= (char *)malloc(buflen
);
2834 status
= b
->fns
->_nss_getgrgid_r(gid
, &grp
, buf
, buflen
, &errno
);
2835 if (status
== NSS_STATUS_TRYAGAIN
) {
2837 buf
= (char *)realloc(buf
, buflen
);
2843 if (status
== NSS_STATUS_NOTFOUND
) {
2847 if (status
!= NSS_STATUS_SUCCESS
) {
2854 static int nwrap_module_getgrgid_r(struct nwrap_backend
*b
,
2855 gid_t gid
, struct group
*grdst
,
2856 char *buf
, size_t buflen
, struct group
**grdstp
)
2860 (void) grdstp
; /* unused */
2862 if (!b
->fns
->_nss_getgrgid_r
) {
2866 ret
= b
->fns
->_nss_getgrgid_r(gid
, grdst
, buf
, buflen
, &errno
);
2868 case NSS_STATUS_SUCCESS
:
2870 case NSS_STATUS_NOTFOUND
:
2875 case NSS_STATUS_TRYAGAIN
:
2888 static void nwrap_module_setgrent(struct nwrap_backend
*b
)
2890 if (!b
->fns
->_nss_setgrent
) {
2894 b
->fns
->_nss_setgrent();
2897 static struct group
*nwrap_module_getgrent(struct nwrap_backend
*b
)
2899 static struct group grp
;
2901 static int buflen
= 1024;
2904 if (!b
->fns
->_nss_getgrent_r
) {
2909 buf
= (char *)malloc(buflen
);
2913 status
= b
->fns
->_nss_getgrent_r(&grp
, buf
, buflen
, &errno
);
2914 if (status
== NSS_STATUS_TRYAGAIN
) {
2916 buf
= (char *)realloc(buf
, buflen
);
2922 if (status
== NSS_STATUS_NOTFOUND
) {
2926 if (status
!= NSS_STATUS_SUCCESS
) {
2933 static int nwrap_module_getgrent_r(struct nwrap_backend
*b
,
2934 struct group
*grdst
, char *buf
,
2935 size_t buflen
, struct group
**grdstp
)
2939 (void) grdstp
; /* unused */
2941 if (!b
->fns
->_nss_getgrent_r
) {
2945 ret
= b
->fns
->_nss_getgrent_r(grdst
, buf
, buflen
, &errno
);
2947 case NSS_STATUS_SUCCESS
:
2949 case NSS_STATUS_NOTFOUND
:
2954 case NSS_STATUS_TRYAGAIN
:
2967 static void nwrap_module_endgrent(struct nwrap_backend
*b
)
2969 if (!b
->fns
->_nss_endgrent
) {
2973 b
->fns
->_nss_endgrent();
2976 /****************************************************************************
2978 ***************************************************************************/
2980 static struct passwd
*nwrap_getpwnam(const char *name
)
2985 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
2986 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
2987 pwd
= b
->ops
->nw_getpwnam(b
, name
);
2996 struct passwd
*getpwnam(const char *name
)
2998 if (!nss_wrapper_enabled()) {
2999 return libc_getpwnam(name
);
3002 return nwrap_getpwnam(name
);
3005 /****************************************************************************
3007 ***************************************************************************/
3009 static int nwrap_getpwnam_r(const char *name
, struct passwd
*pwdst
,
3010 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3014 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3015 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3016 ret
= b
->ops
->nw_getpwnam_r(b
, name
, pwdst
, buf
, buflen
, pwdstp
);
3017 if (ret
== ENOENT
) {
3026 #ifdef HAVE_GETPWNAM_R
3027 # ifdef HAVE_SOLARIS_GETPWNAM_R
3028 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
3029 char *buf
, int buflen
, struct passwd
**pwdstp
)
3030 # else /* HAVE_SOLARIS_GETPWNAM_R */
3031 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
3032 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3033 # endif /* HAVE_SOLARIS_GETPWNAM_R */
3035 if (!nss_wrapper_enabled()) {
3036 return libc_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
3039 return nwrap_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
3043 /****************************************************************************
3045 ***************************************************************************/
3047 static struct passwd
*nwrap_getpwuid(uid_t uid
)
3052 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3053 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3054 pwd
= b
->ops
->nw_getpwuid(b
, uid
);
3063 struct passwd
*getpwuid(uid_t uid
)
3065 if (!nss_wrapper_enabled()) {
3066 return libc_getpwuid(uid
);
3069 return nwrap_getpwuid(uid
);
3072 /****************************************************************************
3074 ***************************************************************************/
3076 static int nwrap_getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
3077 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3081 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3082 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3083 ret
= b
->ops
->nw_getpwuid_r(b
, uid
, pwdst
, buf
, buflen
, pwdstp
);
3084 if (ret
== ENOENT
) {
3093 #ifdef HAVE_SOLARIS_GETPWUID_R
3094 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
3095 char *buf
, int buflen
, struct passwd
**pwdstp
)
3097 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
3098 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3101 if (!nss_wrapper_enabled()) {
3102 return libc_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
3105 return nwrap_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
3108 /****************************************************************************
3110 ***************************************************************************/
3112 static void nwrap_setpwent(void)
3116 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3117 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3118 b
->ops
->nw_setpwent(b
);
3124 if (!nss_wrapper_enabled()) {
3132 /****************************************************************************
3134 ***************************************************************************/
3136 static struct passwd
*nwrap_getpwent(void)
3141 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3142 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3143 pwd
= b
->ops
->nw_getpwent(b
);
3152 struct passwd
*getpwent(void)
3154 if (!nss_wrapper_enabled()) {
3155 return libc_getpwent();
3158 return nwrap_getpwent();
3161 /****************************************************************************
3163 ***************************************************************************/
3165 static int nwrap_getpwent_r(struct passwd
*pwdst
, char *buf
,
3166 size_t buflen
, struct passwd
**pwdstp
)
3170 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3171 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3172 ret
= b
->ops
->nw_getpwent_r(b
, pwdst
, buf
, buflen
, pwdstp
);
3173 if (ret
== ENOENT
) {
3182 #ifdef HAVE_SOLARIS_GETPWENT_R
3183 struct passwd
*getpwent_r(struct passwd
*pwdst
, char *buf
, int buflen
)
3185 struct passwd
*pwdstp
= NULL
;
3188 if (!nss_wrapper_enabled()) {
3189 return libc_getpwent_r(pwdst
, buf
, buflen
);
3191 rc
= nwrap_getpwent_r(pwdst
, buf
, buflen
, &pwdstp
);
3198 #else /* HAVE_SOLARIS_GETPWENT_R */
3199 int getpwent_r(struct passwd
*pwdst
, char *buf
,
3200 size_t buflen
, struct passwd
**pwdstp
)
3202 if (!nss_wrapper_enabled()) {
3203 return libc_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
3206 return nwrap_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
3208 #endif /* HAVE_SOLARIS_GETPWENT_R */
3210 /****************************************************************************
3212 ***************************************************************************/
3214 static void nwrap_endpwent(void)
3218 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3219 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3220 b
->ops
->nw_endpwent(b
);
3226 if (!nss_wrapper_enabled()) {
3234 /****************************************************************************
3236 ***************************************************************************/
3238 static int nwrap_initgroups(const char *user
, gid_t group
)
3242 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3243 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3246 rc
= b
->ops
->nw_initgroups(b
, user
, group
);
3256 int initgroups(const char *user
, gid_t group
)
3258 if (!nss_wrapper_enabled()) {
3259 return libc_initgroups(user
, group
);
3262 return nwrap_initgroups(user
, group
);
3265 /****************************************************************************
3267 ***************************************************************************/
3269 static struct group
*nwrap_getgrnam(const char *name
)
3274 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3275 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3276 grp
= b
->ops
->nw_getgrnam(b
, name
);
3285 struct group
*getgrnam(const char *name
)
3287 if (!nss_wrapper_enabled()) {
3288 return libc_getgrnam(name
);
3291 return nwrap_getgrnam(name
);
3294 /****************************************************************************
3296 ***************************************************************************/
3298 static int nwrap_getgrnam_r(const char *name
, struct group
*grdst
,
3299 char *buf
, size_t buflen
, struct group
**grdstp
)
3303 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3304 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3305 ret
= b
->ops
->nw_getgrnam_r(b
, name
, grdst
, buf
, buflen
, grdstp
);
3306 if (ret
== ENOENT
) {
3315 #ifdef HAVE_GETGRNAM_R
3316 # ifdef HAVE_SOLARIS_GETGRNAM_R
3317 int getgrnam_r(const char *name
, struct group
*grp
,
3318 char *buf
, int buflen
, struct group
**pgrp
)
3319 # else /* HAVE_SOLARIS_GETGRNAM_R */
3320 int getgrnam_r(const char *name
, struct group
*grp
,
3321 char *buf
, size_t buflen
, struct group
**pgrp
)
3322 # endif /* HAVE_SOLARIS_GETGRNAM_R */
3324 if (!nss_wrapper_enabled()) {
3325 return libc_getgrnam_r(name
,
3332 return nwrap_getgrnam_r(name
, grp
, buf
, buflen
, pgrp
);
3334 #endif /* HAVE_GETGRNAM_R */
3336 /****************************************************************************
3338 ***************************************************************************/
3340 static struct group
*nwrap_getgrgid(gid_t gid
)
3345 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3346 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3347 grp
= b
->ops
->nw_getgrgid(b
, gid
);
3356 struct group
*getgrgid(gid_t gid
)
3358 if (!nss_wrapper_enabled()) {
3359 return libc_getgrgid(gid
);
3362 return nwrap_getgrgid(gid
);
3365 /****************************************************************************
3367 ***************************************************************************/
3369 static int nwrap_getgrgid_r(gid_t gid
, struct group
*grdst
,
3370 char *buf
, size_t buflen
, struct group
**grdstp
)
3374 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3375 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3376 ret
= b
->ops
->nw_getgrgid_r(b
, gid
, grdst
, buf
, buflen
, grdstp
);
3377 if (ret
== ENOENT
) {
3386 #ifdef HAVE_GETGRGID_R
3387 # ifdef HAVE_SOLARIS_GETGRGID_R
3388 int getgrgid_r(gid_t gid
, struct group
*grdst
,
3389 char *buf
, int buflen
, struct group
**grdstp
)
3390 # else /* HAVE_SOLARIS_GETGRGID_R */
3391 int getgrgid_r(gid_t gid
, struct group
*grdst
,
3392 char *buf
, size_t buflen
, struct group
**grdstp
)
3393 # endif /* HAVE_SOLARIS_GETGRGID_R */
3395 if (!nss_wrapper_enabled()) {
3396 return libc_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
3399 return nwrap_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
3403 /****************************************************************************
3405 ***************************************************************************/
3407 static void nwrap_setgrent(void)
3411 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3412 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3413 b
->ops
->nw_setgrent(b
);
3417 #ifdef HAVE_BSD_SETGRENT
3423 if (!nss_wrapper_enabled()) {
3431 #ifdef HAVE_BSD_SETGRENT
3438 /****************************************************************************
3440 ***************************************************************************/
3442 static struct group
*nwrap_getgrent(void)
3447 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3448 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3449 grp
= b
->ops
->nw_getgrent(b
);
3458 struct group
*getgrent(void)
3460 if (!nss_wrapper_enabled()) {
3461 return libc_getgrent();
3464 return nwrap_getgrent();
3467 /****************************************************************************
3469 ***************************************************************************/
3471 static int nwrap_getgrent_r(struct group
*grdst
, char *buf
,
3472 size_t buflen
, struct group
**grdstp
)
3476 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3477 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3478 ret
= b
->ops
->nw_getgrent_r(b
, grdst
, buf
, buflen
, grdstp
);
3479 if (ret
== ENOENT
) {
3488 #ifdef HAVE_SOLARIS_GETGRENT_R
3489 struct group
*getgrent_r(struct group
*src
, char *buf
, int buflen
)
3491 struct group
*grdstp
= NULL
;
3494 if (!nss_wrapper_enabled()) {
3495 return libc_getgrent_r(src
, buf
, buflen
);
3498 rc
= nwrap_getgrent_r(src
, buf
, buflen
, &grdstp
);
3505 #else /* HAVE_SOLARIS_GETGRENT_R */
3506 int getgrent_r(struct group
*src
, char *buf
,
3507 size_t buflen
, struct group
**grdstp
)
3509 if (!nss_wrapper_enabled()) {
3510 return libc_getgrent_r(src
, buf
, buflen
, grdstp
);
3513 return nwrap_getgrent_r(src
, buf
, buflen
, grdstp
);
3515 #endif /* HAVE_SOLARIS_GETGRENT_R */
3517 /****************************************************************************
3519 ***************************************************************************/
3521 static void nwrap_endgrent(void)
3525 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3526 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3527 b
->ops
->nw_endgrent(b
);
3533 if (!nss_wrapper_enabled()) {
3541 /****************************************************************************
3543 ***************************************************************************/
3545 #ifdef HAVE_GETGROUPLIST
3546 static int nwrap_getgrouplist(const char *user
, gid_t group
,
3547 gid_t
*groups
, int *ngroups
)
3552 const char *name_of_group
= "";
3554 NWRAP_LOG(NWRAP_LOG_DEBUG
, "getgrouplist called for %s", user
);
3556 groups_tmp
= (gid_t
*)malloc(count
* sizeof(gid_t
));
3558 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
3563 memcpy(groups_tmp
, &group
, sizeof(gid_t
));
3565 grp
= nwrap_getgrgid(group
);
3567 name_of_group
= grp
->gr_name
;
3571 while ((grp
= nwrap_getgrent()) != NULL
) {
3574 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3575 "Inspecting %s for group membership",
3578 for (i
=0; grp
->gr_mem
&& grp
->gr_mem
[i
] != NULL
; i
++) {
3580 if ((strcmp(user
, grp
->gr_mem
[i
]) == 0) &&
3581 (strcmp(name_of_group
, grp
->gr_name
) != 0)) {
3583 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3584 "%s is member of %s",
3588 groups_tmp
= (gid_t
*)realloc(groups_tmp
, (count
+ 1) * sizeof(gid_t
));
3590 NWRAP_LOG(NWRAP_LOG_ERROR
,
3596 memcpy(&groups_tmp
[count
], &grp
->gr_gid
, sizeof(gid_t
));
3604 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3605 "%s is member of %d groups",
3608 if (*ngroups
< count
) {
3615 memcpy(groups
, groups_tmp
, count
* sizeof(gid_t
));
3621 int getgrouplist(const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
)
3623 if (!nss_wrapper_enabled()) {
3624 return libc_getgrouplist(user
, group
, groups
, ngroups
);
3627 return nwrap_getgrouplist(user
, group
, groups
, ngroups
);
3631 /**********************************************************
3633 **********************************************************/
3635 static void nwrap_sethostent(int stayopen
) {
3636 (void) stayopen
; /* ignored */
3638 nwrap_files_sethostent();
3641 #ifdef HAVE_SOLARIS_SETHOSTENT
3642 int sethostent(int stayopen
)
3644 if (!nss_wrapper_hosts_enabled()) {
3645 libc_sethostent(stayopen
);
3649 nwrap_sethostent(stayopen
);
3653 #else /* HAVE_SOLARIS_SETHOSTENT */
3654 void sethostent(int stayopen
)
3656 if (!nss_wrapper_hosts_enabled()) {
3657 libc_sethostent(stayopen
);
3661 nwrap_sethostent(stayopen
);
3663 #endif /* HAVE_SOLARIS_SETHOSTENT */
3665 static struct hostent
*nwrap_gethostent(void)
3667 return nwrap_files_gethostent();
3670 struct hostent
*gethostent(void) {
3671 if (!nss_wrapper_hosts_enabled()) {
3672 return libc_gethostent();
3675 return nwrap_gethostent();
3678 static void nwrap_endhostent(void) {
3679 nwrap_files_endhostent();
3682 #ifdef HAVE_SOLARIS_ENDHOSTENT
3683 int endhostent(void)
3685 if (!nss_wrapper_hosts_enabled()) {
3694 #else /* HAVE_SOLARIS_ENDHOSTENT */
3695 void endhostent(void)
3697 if (!nss_wrapper_hosts_enabled()) {
3704 #endif /* HAVE_SOLARIS_ENDHOSTENT */
3706 static struct hostent
*nwrap_gethostbyname(const char *name
)
3708 return nwrap_files_gethostbyname(name
, AF_UNSPEC
);
3711 struct hostent
*gethostbyname(const char *name
)
3713 if (!nss_wrapper_hosts_enabled()) {
3714 return libc_gethostbyname(name
);
3717 return nwrap_gethostbyname(name
);
3720 /* This is a GNU extension */
3721 #ifdef HAVE_GETHOSTBYNAME2
3722 static struct hostent
*nwrap_gethostbyname2(const char *name
, int af
)
3724 return nwrap_files_gethostbyname(name
, af
);
3727 struct hostent
*gethostbyname2(const char *name
, int af
)
3729 if (!nss_wrapper_hosts_enabled()) {
3730 return libc_gethostbyname2(name
, af
);
3733 return nwrap_gethostbyname2(name
, af
);
3737 static struct hostent
*nwrap_gethostbyaddr(const void *addr
,
3738 socklen_t len
, int type
)
3740 return nwrap_files_gethostbyaddr(addr
, len
, type
);
3743 struct hostent
*gethostbyaddr(const void *addr
,
3744 socklen_t len
, int type
)
3746 if (!nss_wrapper_hosts_enabled()) {
3747 return libc_gethostbyaddr(addr
, len
, type
);
3750 return nwrap_gethostbyaddr(addr
, len
, type
);
3753 static const struct addrinfo default_hints
=
3755 .ai_flags
= AI_ADDRCONFIG
|AI_V4MAPPED
,
3756 .ai_family
= AF_UNSPEC
,
3761 .ai_canonname
= NULL
,
3765 static int nwrap_convert_he_ai(const struct hostent
*he
,
3766 unsigned short port
,
3767 const struct addrinfo
*hints
,
3768 struct addrinfo
**pai
)
3770 struct addrinfo
*ai
;
3773 switch (he
->h_addrtype
) {
3775 socklen
= sizeof(struct sockaddr_in
);
3779 socklen
= sizeof(struct sockaddr_in6
);
3786 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
) + socklen
);
3792 ai
->ai_family
= he
->h_addrtype
;
3793 ai
->ai_socktype
= hints
->ai_socktype
;
3794 ai
->ai_protocol
= hints
->ai_protocol
;
3796 ai
->ai_addrlen
= socklen
;
3797 ai
->ai_addr
= (void *)(ai
+ 1);
3799 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
3800 ai
->ai_addr
->sa_len
= socklen
;
3802 ai
->ai_addr
->sa_family
= he
->h_addrtype
;
3804 switch (he
->h_addrtype
) {
3807 struct sockaddr_in
*sinp
=
3808 (struct sockaddr_in
*) ai
->ai_addr
;
3810 memset(sinp
, 0, sizeof(struct sockaddr_in
));
3812 sinp
->sin_port
= htons(port
);
3813 sinp
->sin_family
= AF_INET
;
3815 memset (sinp
->sin_zero
, '\0', sizeof (sinp
->sin_zero
));
3816 memcpy(&sinp
->sin_addr
, he
->h_addr_list
[0], he
->h_length
);
3823 struct sockaddr_in6
*sin6p
=
3824 (struct sockaddr_in6
*) ai
->ai_addr
;
3826 memset(sin6p
, 0, sizeof(struct sockaddr_in6
));
3828 sin6p
->sin6_port
= htons(port
);
3829 sin6p
->sin6_family
= AF_INET6
;
3831 memcpy(&sin6p
->sin6_addr
, he
->h_addr_list
[0], he
->h_length
);
3840 ai
->ai_canonname
= strdup(he
->h_name
);
3841 if (ai
->ai_canonname
== NULL
) {
3851 static int nwrap_getaddrinfo(const char *node
,
3852 const char *service
,
3853 const struct addrinfo
*hints
,
3854 struct addrinfo
**res
)
3856 struct addrinfo
*ai
= NULL
;
3857 struct addrinfo
*p
= NULL
;
3858 unsigned short port
= 0;
3869 .family
= AF_UNSPEC
,
3871 int eai
= EAI_SYSTEM
;
3875 if (node
== NULL
&& service
== NULL
) {
3879 ret
= libc_getaddrinfo(node
, service
, hints
, &p
);
3884 /* If no node has been specified, let glibc deal with it */
3889 if (hints
== NULL
) {
3890 hints
= &default_hints
;
3893 if ((hints
->ai_flags
& AI_CANONNAME
) && node
== NULL
) {
3894 return EAI_BADFLAGS
;
3897 if (service
!= NULL
&& service
[0] != '\0') {
3898 if (isdigit((int)service
[0])) {
3899 port
= (unsigned short)atoi(service
);
3901 const char *proto
= NULL
;
3904 if (hints
->ai_protocol
!= 0) {
3905 struct protoent
*pent
;
3907 pent
= getprotobynumber(hints
->ai_protocol
);
3909 proto
= pent
->p_name
;
3913 s
= getservbyname(service
, proto
);
3915 port
= ntohs(s
->s_port
);
3926 if (hints
->ai_family
== AF_UNSPEC
|| hints
->ai_family
== AF_INET
) {
3927 rc
= inet_pton(AF_INET
, node
, &addr
.in
.v4
);
3930 addr
.family
= AF_INET
;
3933 rc
= inet_pton(AF_INET6
, node
, &addr
.in
.v6
);
3935 addr
.family
= AF_INET6
;
3940 if (addr
.family
== AF_INET
) {
3941 he
= nwrap_files_gethostbyaddr(&addr
.in
.v4
,
3942 sizeof(struct in_addr
),
3945 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
);
3951 } else if (addr
.family
== AF_INET6
) {
3952 he
= nwrap_files_gethostbyaddr(&addr
.in
.v6
,
3953 sizeof(struct in6_addr
),
3956 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
);
3964 he
= nwrap_files_gethostbyname(node
, hints
->ai_family
);
3966 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
);
3975 return ret
== 0 ? 0 : eai
;
3982 if (ai
->ai_flags
== 0) {
3983 ai
->ai_flags
= hints
->ai_flags
;
3985 if (ai
->ai_socktype
== 0) {
3986 ai
->ai_socktype
= SOCK_DGRAM
;
3988 if (ai
->ai_protocol
== 0 && ai
->ai_socktype
== SOCK_DGRAM
) {
3989 ai
->ai_protocol
= 17; /* UDP */
3990 } else if (ai
->ai_protocol
== 0 && ai
->ai_socktype
== SOCK_STREAM
) {
3991 ai
->ai_protocol
= 6; /* TCP */
3994 if (hints
->ai_socktype
== 0) {
3996 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
->ai_next
);
4002 if (ai
->ai_next
->ai_flags
== 0) {
4003 ai
->ai_next
->ai_flags
= hints
->ai_flags
;
4005 if (ai
->ai_socktype
== SOCK_DGRAM
) {
4006 ai
->ai_next
->ai_socktype
= SOCK_STREAM
;
4007 } else if (ai
->ai_socktype
== SOCK_STREAM
) {
4008 ai
->ai_next
->ai_socktype
= SOCK_DGRAM
;
4010 if (ai
->ai_next
->ai_socktype
== SOCK_DGRAM
) {
4011 ai
->ai_next
->ai_protocol
= 17; /* UDP */
4012 } else if (ai
->ai_next
->ai_socktype
== SOCK_STREAM
) {
4013 ai
->ai_next
->ai_protocol
= 6; /* TCP */
4022 int getaddrinfo(const char *node
, const char *service
,
4023 const struct addrinfo
*hints
,
4024 struct addrinfo
**res
)
4026 if (!nss_wrapper_hosts_enabled()) {
4027 return libc_getaddrinfo(node
, service
, hints
, res
);
4030 return nwrap_getaddrinfo(node
, service
, hints
, res
);
4033 static int nwrap_getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4034 char *host
, size_t hostlen
,
4035 char *serv
, size_t servlen
,
4039 struct servent
*service
;
4046 if (sa
== NULL
|| salen
< sizeof(sa_family_t
)) {
4050 if ((flags
& NI_NAMEREQD
) && host
== NULL
&& serv
== NULL
) {
4054 type
= sa
->sa_family
;
4057 if (salen
< sizeof(struct sockaddr_in
))
4059 addr
= &((const struct sockaddr_in
*)sa
)->sin_addr
;
4060 addrlen
= sizeof(((const struct sockaddr_in
*)sa
)->sin_addr
);
4061 port
= ntohs(((const struct sockaddr_in
*)sa
)->sin_port
);
4065 if (salen
< sizeof(struct sockaddr_in6
))
4067 addr
= &((const struct sockaddr_in6
*)sa
)->sin6_addr
;
4068 addrlen
= sizeof(((const struct sockaddr_in6
*)sa
)->sin6_addr
);
4069 port
= ntohs(((const struct sockaddr_in6
*)sa
)->sin6_port
);
4078 if ((flags
& NI_NUMERICHOST
) == 0) {
4079 he
= nwrap_files_gethostbyaddr(addr
, addrlen
, type
);
4080 if ((flags
& NI_NAMEREQD
) && (he
== NULL
|| he
->h_name
== NULL
))
4083 if (he
!= NULL
&& he
->h_name
!= NULL
) {
4084 if (strlen(he
->h_name
) >= hostlen
)
4085 return EAI_OVERFLOW
;
4086 strcpy(host
, he
->h_name
);
4087 if (flags
& NI_NOFQDN
)
4088 host
[strcspn(host
, ".")] = '\0';
4090 if (inet_ntop(type
, addr
, host
, hostlen
) == NULL
)
4091 return (errno
== ENOSPC
) ? EAI_OVERFLOW
: EAI_FAIL
;
4097 if ((flags
& NI_NUMERICSERV
) == 0) {
4098 proto
= (flags
& NI_DGRAM
) ? "udp" : "tcp";
4099 service
= getservbyport(htons(port
), proto
);
4101 if (service
!= NULL
) {
4102 if (strlen(service
->s_name
) >= servlen
)
4103 return EAI_OVERFLOW
;
4104 strcpy(serv
, service
->s_name
);
4106 if (snprintf(serv
, servlen
, "%u", port
) >= (int) servlen
)
4107 return EAI_OVERFLOW
;
4114 #ifdef HAVE_LINUX_GETNAMEINFO
4115 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4116 char *host
, socklen_t hostlen
,
4117 char *serv
, socklen_t servlen
,
4119 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
4120 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4121 char *host
, socklen_t hostlen
,
4122 char *serv
, socklen_t servlen
,
4125 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4126 char *host
, size_t hostlen
,
4127 char *serv
, size_t servlen
,
4131 if (!nss_wrapper_hosts_enabled()) {
4132 return libc_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
4135 return nwrap_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
4138 static int nwrap_gethostname(char *name
, size_t len
)
4140 const char *hostname
= getenv("NSS_WRAPPER_HOSTNAME");
4142 if (strlen(hostname
) >= len
) {
4143 errno
= ENAMETOOLONG
;
4146 snprintf(name
, len
, "%s", hostname
);
4151 #ifdef HAVE_SOLARIS_GETHOSTNAME
4152 int gethostname(char *name
, int len
)
4153 #else /* HAVE_SOLARIS_GETHOSTNAME */
4154 int gethostname(char *name
, size_t len
)
4155 #endif /* HAVE_SOLARIS_GETHOSTNAME */
4157 if (!nwrap_hostname_enabled()) {
4158 return libc_gethostname(name
, len
);
4161 return nwrap_gethostname(name
, len
);
4164 /****************************
4166 ***************************/
4169 * This function is called when the library is unloaded and makes sure that
4170 * sockets get closed and the unix file for the socket are unlinked.
4172 void nwrap_destructor(void)
4176 if (nwrap_main_global
!= NULL
) {
4177 struct nwrap_main
*m
= nwrap_main_global
;
4180 SAFE_FREE(m
->libc
->fns
);
4181 if (m
->libc
->handle
!= NULL
) {
4182 dlclose(m
->libc
->handle
);
4184 if (m
->libc
->nsl_handle
!= NULL
) {
4185 dlclose(m
->libc
->nsl_handle
);
4187 if (m
->libc
->sock_handle
!= NULL
) {
4188 dlclose(m
->libc
->sock_handle
);
4193 for (i
= 0; i
< m
->num_backends
; i
++) {
4194 struct nwrap_backend
*b
= &(m
->backends
[i
]);
4196 if (b
->so_handle
!= NULL
) {
4197 dlclose(b
->so_handle
);
4201 SAFE_FREE(m
->backends
);
4204 if (nwrap_pw_global
.cache
!= NULL
) {
4205 struct nwrap_cache
*c
= nwrap_pw_global
.cache
;
4207 nwrap_files_cache_unload(c
);
4212 SAFE_FREE(nwrap_pw_global
.list
);
4213 nwrap_pw_global
.num
= 0;
4216 if (nwrap_gr_global
.cache
!= NULL
) {
4217 struct nwrap_cache
*c
= nwrap_gr_global
.cache
;
4219 nwrap_files_cache_unload(c
);
4224 SAFE_FREE(nwrap_gr_global
.list
);
4225 nwrap_pw_global
.num
= 0;
4228 if (nwrap_he_global
.cache
!= NULL
) {
4229 struct nwrap_cache
*c
= nwrap_he_global
.cache
;
4231 nwrap_files_cache_unload(c
);
4236 SAFE_FREE(nwrap_he_global
.list
);
4237 nwrap_he_global
.num
= 0;