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 (i
= 10; i
>= 0; i
--) {
579 char soname
[256] = {0};
581 snprintf(soname
, sizeof(soname
), "libnsl.so.%d", i
);
582 handle
= dlopen(soname
, flags
);
583 if (handle
!= NULL
) {
588 nwrap_main_global
->libc
->nsl_handle
= handle
;
593 case NWRAP_LIBSOCKET
:
594 #ifdef HAVE_LIBSOCKET
595 handle
= nwrap_main_global
->libc
->sock_handle
;
596 if (handle
== NULL
) {
597 for (i
= 10; i
>= 0; i
--) {
598 char soname
[256] = {0};
600 snprintf(soname
, sizeof(soname
), "libsocket.so.%d", i
);
601 handle
= dlopen(soname
, flags
);
602 if (handle
!= NULL
) {
607 nwrap_main_global
->libc
->sock_handle
= handle
;
613 handle
= nwrap_main_global
->libc
->handle
;
614 if (handle
== NULL
) {
615 for (i
= 10; i
>= 0; i
--) {
616 char soname
[256] = {0};
618 snprintf(soname
, sizeof(soname
), "libc.so.%d", i
);
619 handle
= dlopen(soname
, flags
);
620 if (handle
!= NULL
) {
625 nwrap_main_global
->libc
->handle
= handle
;
630 if (handle
== NULL
) {
632 handle
= nwrap_main_global
->libc
->handle
633 = nwrap_main_global
->libc
->sock_handle
634 = nwrap_main_global
->libc
->nsl_handle
637 NWRAP_LOG(NWRAP_LOG_ERROR
,
638 "Failed to dlopen library: %s\n",
647 static void *_nwrap_load_lib_function(enum nwrap_lib lib
, const char *fn_name
)
654 handle
= nwrap_load_lib_handle(lib
);
656 func
= dlsym(handle
, fn_name
);
658 NWRAP_LOG(NWRAP_LOG_ERROR
,
659 "Failed to find %s: %s\n",
664 NWRAP_LOG(NWRAP_LOG_TRACE
,
666 fn_name
, nwrap_str_lib(lib
));
670 #define nwrap_load_lib_function(lib, fn_name) \
671 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
672 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
673 _nwrap_load_lib_function(lib, #fn_name); \
679 * Functions expeciall from libc need to be loaded individually, you can't load
680 * all at once or gdb will segfault at startup. The same applies to valgrind and
681 * has probably something todo with with the linker.
682 * So we need load each function at the point it is called the first time.
684 static struct passwd
*libc_getpwnam(const char *name
)
686 nwrap_load_lib_function(NWRAP_LIBC
, getpwnam
);
688 return nwrap_main_global
->libc
->fns
->_libc_getpwnam(name
);
691 #ifdef HAVE_GETPWNAM_R
692 static int libc_getpwnam_r(const char *name
,
696 struct passwd
**result
)
698 #ifdef HAVE___POSIX_GETPWNAM_R
699 if (nwrap_main_global
->libc
->fns
->_libc_getpwnam_r
== NULL
) {
700 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getpwnam_r
) =
701 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getpwnam_r");
704 nwrap_load_lib_function(NWRAP_LIBC
, getpwnam_r
);
707 return nwrap_main_global
->libc
->fns
->_libc_getpwnam_r(name
,
715 static struct passwd
*libc_getpwuid(uid_t uid
)
717 nwrap_load_lib_function(NWRAP_LIBC
, getpwuid
);
719 return nwrap_main_global
->libc
->fns
->_libc_getpwuid(uid
);
722 #ifdef HAVE_GETPWUID_R
723 static int libc_getpwuid_r(uid_t uid
,
727 struct passwd
**result
)
729 #ifdef HAVE___POSIX_GETPWUID_R
730 if (nwrap_main_global
->libc
->fns
->_libc_getpwuid_r
== NULL
) {
731 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getpwuid_r
) =
732 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getpwuid_r");
735 nwrap_load_lib_function(NWRAP_LIBC
, getpwuid_r
);
738 return nwrap_main_global
->libc
->fns
->_libc_getpwuid_r(uid
,
746 static void libc_setpwent(void)
748 nwrap_load_lib_function(NWRAP_LIBC
, setpwent
);
750 nwrap_main_global
->libc
->fns
->_libc_setpwent();
753 static struct passwd
*libc_getpwent(void)
755 nwrap_load_lib_function(NWRAP_LIBC
, getpwent
);
757 return nwrap_main_global
->libc
->fns
->_libc_getpwent();
760 #ifdef HAVE_SOLARIS_GETPWENT_R
761 static struct passwd
*libc_getpwent_r(struct passwd
*pwdst
,
765 nwrap_load_lib_function(NWRAP_LIBC
, getpwent_r
);
767 return nwrap_main_global
->libc
->fns
->_libc_getpwent_r(pwdst
,
771 #else /* HAVE_SOLARIS_GETPWENT_R */
772 static int libc_getpwent_r(struct passwd
*pwdst
,
775 struct passwd
**pwdstp
)
777 nwrap_load_lib_function(NWRAP_LIBC
, getpwent_r
);
779 return nwrap_main_global
->libc
->fns
->_libc_getpwent_r(pwdst
,
784 #endif /* HAVE_SOLARIS_GETPWENT_R */
786 static void libc_endpwent(void)
788 nwrap_load_lib_function(NWRAP_LIBC
, endpwent
);
790 nwrap_main_global
->libc
->fns
->_libc_endpwent();
793 static int libc_initgroups(const char *user
, gid_t gid
)
795 nwrap_load_lib_function(NWRAP_LIBC
, initgroups
);
797 return nwrap_main_global
->libc
->fns
->_libc_initgroups(user
, gid
);
800 static struct group
*libc_getgrnam(const char *name
)
802 nwrap_load_lib_function(NWRAP_LIBC
, getgrnam
);
804 return nwrap_main_global
->libc
->fns
->_libc_getgrnam(name
);
807 #ifdef HAVE_GETGRNAM_R
808 static int libc_getgrnam_r(const char *name
,
812 struct group
**result
)
814 #ifdef HAVE___POSIX_GETGRNAM_R
815 if (nwrap_main_global
->libc
->fns
->_libc_getgrnam_r
== NULL
) {
816 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getgrnam_r
) =
817 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getgrnam_r");
820 nwrap_load_lib_function(NWRAP_LIBC
, getgrnam_r
);
823 return nwrap_main_global
->libc
->fns
->_libc_getgrnam_r(name
,
831 static struct group
*libc_getgrgid(gid_t gid
)
833 nwrap_load_lib_function(NWRAP_LIBC
, getgrgid
);
835 return nwrap_main_global
->libc
->fns
->_libc_getgrgid(gid
);
838 #ifdef HAVE_GETGRGID_R
839 static int libc_getgrgid_r(gid_t gid
,
843 struct group
**result
)
845 #ifdef HAVE___POSIX_GETGRGID_R
846 if (nwrap_main_global
->libc
->fns
->_libc_getgrgid_r
== NULL
) {
847 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getgrgid_r
) =
848 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getgrgid_r");
851 nwrap_load_lib_function(NWRAP_LIBC
, getgrgid_r
);
854 return nwrap_main_global
->libc
->fns
->_libc_getgrgid_r(gid
,
862 static void libc_setgrent(void)
864 nwrap_load_lib_function(NWRAP_LIBC
, setgrent
);
866 nwrap_main_global
->libc
->fns
->_libc_setgrent();
869 static struct group
*libc_getgrent(void)
871 nwrap_load_lib_function(NWRAP_LIBC
, getgrent
);
873 return nwrap_main_global
->libc
->fns
->_libc_getgrent();
876 #ifdef HAVE_GETGRENT_R
877 #ifdef HAVE_SOLARIS_GETGRENT_R
878 static struct group
*libc_getgrent_r(struct group
*group
,
882 nwrap_load_lib_function(NWRAP_LIBC
, getgrent_r
);
884 return nwrap_main_global
->libc
->fns
->_libc_getgrent_r(group
,
888 #else /* !HAVE_SOLARIS_GETGRENT_R */
889 static int libc_getgrent_r(struct group
*group
,
892 struct group
**result
)
894 nwrap_load_lib_function(NWRAP_LIBC
, getgrent_r
);
896 return nwrap_main_global
->libc
->fns
->_libc_getgrent_r(group
,
901 #endif /* HAVE_SOLARIS_GETGRENT_R */
902 #endif /* HAVE_GETGRENT_R */
904 static void libc_endgrent(void)
906 nwrap_load_lib_function(NWRAP_LIBC
, endgrent
);
908 nwrap_main_global
->libc
->fns
->_libc_endgrent();
911 #ifdef HAVE_GETGROUPLIST
912 static int libc_getgrouplist(const char *user
,
917 nwrap_load_lib_function(NWRAP_LIBC
, getgrouplist
);
919 return nwrap_main_global
->libc
->fns
->_libc_getgrouplist(user
,
926 static void libc_sethostent(int stayopen
)
928 nwrap_load_lib_function(NWRAP_LIBNSL
, sethostent
);
930 nwrap_main_global
->libc
->fns
->_libc_sethostent(stayopen
);
933 static struct hostent
*libc_gethostent(void)
935 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostent
);
937 return nwrap_main_global
->libc
->fns
->_libc_gethostent();
940 static void libc_endhostent(void)
942 nwrap_load_lib_function(NWRAP_LIBNSL
, endhostent
);
944 nwrap_main_global
->libc
->fns
->_libc_endhostent();
947 static struct hostent
*libc_gethostbyname(const char *name
)
949 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname
);
951 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname(name
);
954 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
955 static struct hostent
*libc_gethostbyname2(const char *name
, int af
)
957 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname2
);
959 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname2(name
, af
);
963 static struct hostent
*libc_gethostbyaddr(const void *addr
,
967 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyaddr
);
969 return nwrap_main_global
->libc
->fns
->_libc_gethostbyaddr(addr
,
974 static int libc_gethostname(char *name
, size_t len
)
976 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostname
);
978 return nwrap_main_global
->libc
->fns
->_libc_gethostname(name
, len
);
981 #ifdef HAVE_GETHOSTBYNAME_R
982 static int libc_gethostbyname_r(const char *name
,
986 struct hostent
**result
,
989 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname_r
);
991 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname_r(name
,
1000 #ifdef HAVE_GETHOSTBYADDR_R
1001 static int libc_gethostbyaddr_r(const void *addr
,
1004 struct hostent
*ret
,
1007 struct hostent
**result
,
1010 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyaddr_r
);
1012 return nwrap_main_global
->libc
->fns
->_libc_gethostbyaddr_r(addr
,
1023 static int libc_getaddrinfo(const char *node
,
1024 const char *service
,
1025 const struct addrinfo
*hints
,
1026 struct addrinfo
**res
)
1028 nwrap_load_lib_function(NWRAP_LIBSOCKET
, getaddrinfo
);
1030 return nwrap_main_global
->libc
->fns
->_libc_getaddrinfo(node
,
1036 static int libc_getnameinfo(const struct sockaddr
*sa
,
1044 nwrap_load_lib_function(NWRAP_LIBSOCKET
, getnameinfo
);
1046 return nwrap_main_global
->libc
->fns
->_libc_getnameinfo(sa
,
1055 /*********************************************************
1056 * NWRAP NSS MODULE LOADER FUNCTIONS
1057 *********************************************************/
1059 static void *nwrap_load_module_fn(struct nwrap_backend
*b
,
1060 const char *fn_name
)
1065 if (!b
->so_handle
) {
1066 NWRAP_LOG(NWRAP_LOG_ERROR
, "No handle");
1070 if (asprintf(&s
, "_nss_%s_%s", b
->name
, fn_name
) == -1) {
1071 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1075 res
= dlsym(b
->so_handle
, s
);
1077 NWRAP_LOG(NWRAP_LOG_ERROR
,
1078 "Cannot find function %s in %s",
1086 static struct nwrap_module_nss_fns
*nwrap_load_module_fns(struct nwrap_backend
*b
)
1088 struct nwrap_module_nss_fns
*fns
;
1090 if (!b
->so_handle
) {
1094 fns
= (struct nwrap_module_nss_fns
*)malloc(sizeof(struct nwrap_module_nss_fns
));
1099 *(void **)(&fns
->_nss_getpwnam_r
) =
1100 nwrap_load_module_fn(b
, "getpwnam_r");
1101 *(void **)(&fns
->_nss_getpwuid_r
) =
1102 nwrap_load_module_fn(b
, "getpwuid_r");
1103 *(void **)(&fns
->_nss_setpwent
) =
1104 nwrap_load_module_fn(b
, "setpwent");
1105 *(void **)(&fns
->_nss_getpwent_r
) =
1106 nwrap_load_module_fn(b
, "getpwent_r");
1107 *(void **)(&fns
->_nss_endpwent
) =
1108 nwrap_load_module_fn(b
, "endpwent");
1109 *(void **)(&fns
->_nss_initgroups
) =
1110 nwrap_load_module_fn(b
, "initgroups_dyn");
1111 *(void **)(&fns
->_nss_getgrnam_r
) =
1112 nwrap_load_module_fn(b
, "getgrnam_r");
1113 *(void **)(&fns
->_nss_getgrgid_r
)=
1114 nwrap_load_module_fn(b
, "getgrgid_r");
1115 *(void **)(&fns
->_nss_setgrent
) =
1116 nwrap_load_module_fn(b
, "setgrent");
1117 *(void **)(&fns
->_nss_getgrent_r
) =
1118 nwrap_load_module_fn(b
, "getgrent_r");
1119 *(void **)(&fns
->_nss_endgrent
) =
1120 nwrap_load_module_fn(b
, "endgrent");
1125 static void *nwrap_load_module(const char *so_path
)
1129 if (!so_path
|| !strlen(so_path
)) {
1133 h
= dlopen(so_path
, RTLD_LAZY
);
1135 NWRAP_LOG(NWRAP_LOG_ERROR
,
1136 "Cannot open shared library %s",
1144 static bool nwrap_module_init(const char *name
,
1145 struct nwrap_ops
*ops
,
1146 const char *so_path
,
1148 struct nwrap_backend
**backends
)
1150 struct nwrap_backend
*b
;
1152 *backends
= (struct nwrap_backend
*)realloc(*backends
,
1153 sizeof(struct nwrap_backend
) * ((*num_backends
) + 1));
1155 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1159 b
= &((*backends
)[*num_backends
]);
1163 b
->so_path
= so_path
;
1165 if (so_path
!= NULL
) {
1166 b
->so_handle
= nwrap_load_module(so_path
);
1167 b
->fns
= nwrap_load_module_fns(b
);
1168 if (b
->fns
== NULL
) {
1172 b
->so_handle
= NULL
;
1181 static void nwrap_libc_init(struct nwrap_main
*r
)
1183 r
->libc
= malloc(sizeof(struct nwrap_libc
));
1184 if (r
->libc
== NULL
) {
1185 printf("Failed to allocate memory for libc");
1188 ZERO_STRUCTP(r
->libc
);
1190 r
->libc
->fns
= malloc(sizeof(struct nwrap_libc_fns
));
1191 if (r
->libc
->fns
== NULL
) {
1192 printf("Failed to allocate memory for libc functions");
1195 ZERO_STRUCTP(r
->libc
->fns
);
1198 static void nwrap_backend_init(struct nwrap_main
*r
)
1200 const char *module_so_path
= getenv("NSS_WRAPPER_MODULE_SO_PATH");
1201 const char *module_fn_name
= getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1203 r
->num_backends
= 0;
1206 if (!nwrap_module_init("files", &nwrap_files_ops
, NULL
,
1209 NWRAP_LOG(NWRAP_LOG_ERROR
,
1210 "Failed to initialize 'files' backend");
1214 if (module_so_path
!= NULL
&&
1215 module_so_path
[0] != '\0' &&
1216 module_fn_name
!= NULL
&&
1217 module_fn_name
[0] != '\0') {
1218 if (!nwrap_module_init(module_fn_name
,
1223 NWRAP_LOG(NWRAP_LOG_ERROR
,
1224 "Failed to initialize '%s' backend",
1231 static void nwrap_init(void)
1233 static bool initialized
;
1235 if (initialized
) return;
1238 nwrap_main_global
= &__nwrap_main_global
;
1240 nwrap_libc_init(nwrap_main_global
);
1242 nwrap_backend_init(nwrap_main_global
);
1244 nwrap_pw_global
.cache
= &__nwrap_cache_pw
;
1246 nwrap_pw_global
.cache
->path
= getenv("NSS_WRAPPER_PASSWD");
1247 nwrap_pw_global
.cache
->fd
= -1;
1248 nwrap_pw_global
.cache
->private_data
= &nwrap_pw_global
;
1249 nwrap_pw_global
.cache
->parse_line
= nwrap_pw_parse_line
;
1250 nwrap_pw_global
.cache
->unload
= nwrap_pw_unload
;
1252 nwrap_gr_global
.cache
= &__nwrap_cache_gr
;
1254 nwrap_gr_global
.cache
->path
= getenv("NSS_WRAPPER_GROUP");
1255 nwrap_gr_global
.cache
->fd
= -1;
1256 nwrap_gr_global
.cache
->private_data
= &nwrap_gr_global
;
1257 nwrap_gr_global
.cache
->parse_line
= nwrap_gr_parse_line
;
1258 nwrap_gr_global
.cache
->unload
= nwrap_gr_unload
;
1260 nwrap_he_global
.cache
= &__nwrap_cache_he
;
1262 nwrap_he_global
.cache
->path
= getenv("NSS_WRAPPER_HOSTS");
1263 nwrap_he_global
.cache
->fd
= -1;
1264 nwrap_he_global
.cache
->private_data
= &nwrap_he_global
;
1265 nwrap_he_global
.cache
->parse_line
= nwrap_he_parse_line
;
1266 nwrap_he_global
.cache
->unload
= nwrap_he_unload
;
1269 bool nss_wrapper_enabled(void)
1273 if (nwrap_pw_global
.cache
->path
== NULL
||
1274 nwrap_pw_global
.cache
->path
[0] == '\0') {
1277 if (nwrap_gr_global
.cache
->path
== NULL
||
1278 nwrap_gr_global
.cache
->path
[0] == '\0') {
1285 bool nss_wrapper_hosts_enabled(void)
1289 if (nwrap_he_global
.cache
->path
== NULL
||
1290 nwrap_he_global
.cache
->path
[0] == '\0') {
1297 static bool nwrap_hostname_enabled(void)
1301 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL
) {
1308 static bool nwrap_parse_file(struct nwrap_cache
*nwrap
)
1311 uint8_t *buf
= NULL
;
1314 if (nwrap
->st
.st_size
== 0) {
1315 NWRAP_LOG(NWRAP_LOG_DEBUG
, "size == 0");
1319 if (nwrap
->st
.st_size
> INT32_MAX
) {
1320 NWRAP_LOG(NWRAP_LOG_ERROR
,
1321 "Size[%u] larger than INT32_MAX",
1322 (unsigned)nwrap
->st
.st_size
);
1326 ret
= lseek(nwrap
->fd
, 0, SEEK_SET
);
1328 NWRAP_LOG(NWRAP_LOG_ERROR
, "lseek - rc=%d\n", ret
);
1332 buf
= (uint8_t *)malloc(nwrap
->st
.st_size
+ 1);
1334 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1338 ret
= read(nwrap
->fd
, buf
, nwrap
->st
.st_size
);
1339 if (ret
!= nwrap
->st
.st_size
) {
1340 NWRAP_LOG(NWRAP_LOG_ERROR
,
1342 (unsigned)nwrap
->st
.st_size
, ret
);
1346 buf
[nwrap
->st
.st_size
] = '\0';
1348 nline
= (char *)buf
;
1349 while (nline
!= NULL
&& nline
[0] != '\0') {
1357 e
= strchr(line
, '\n');
1368 if (strlen(line
) == 0) {
1372 ok
= nwrap
->parse_line(nwrap
, line
);
1387 static void nwrap_files_cache_unload(struct nwrap_cache
*nwrap
)
1389 nwrap
->unload(nwrap
);
1391 if (nwrap
->buf
) free(nwrap
->buf
);
1396 static void nwrap_files_cache_reload(struct nwrap_cache
*nwrap
)
1401 bool retried
= false;
1404 if (nwrap
->fd
< 0) {
1405 nwrap
->fd
= open(nwrap
->path
, O_RDONLY
);
1406 if (nwrap
->fd
< 0) {
1407 NWRAP_LOG(NWRAP_LOG_ERROR
,
1408 "Unable to open '%s' readonly %d:%s",
1409 nwrap
->path
, nwrap
->fd
,
1413 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Open '%s'", nwrap
->path
);
1416 ret
= fstat(nwrap
->fd
, &st
);
1418 NWRAP_LOG(NWRAP_LOG_ERROR
,
1419 "fstat(%s) - %d:%s",
1426 if (retried
== false && st
.st_nlink
== 0) {
1427 /* maybe someone has replaced the file... */
1428 NWRAP_LOG(NWRAP_LOG_TRACE
,
1429 "st_nlink == 0, reopen %s",
1432 memset(&nwrap
->st
, 0, sizeof(nwrap
->st
));
1438 if (st
.st_mtime
== nwrap
->st
.st_mtime
) {
1439 NWRAP_LOG(NWRAP_LOG_TRACE
,
1440 "st_mtime[%u] hasn't changed, skip reload",
1441 (unsigned)st
.st_mtime
);
1445 NWRAP_LOG(NWRAP_LOG_TRACE
,
1446 "st_mtime has changed [%u] => [%u], start reload",
1447 (unsigned)st
.st_mtime
,
1448 (unsigned)nwrap
->st
.st_mtime
);
1452 nwrap_files_cache_unload(nwrap
);
1454 ok
= nwrap_parse_file(nwrap
);
1456 NWRAP_LOG(NWRAP_LOG_ERROR
, "Failed to reload %s", nwrap
->path
);
1457 nwrap_files_cache_unload(nwrap
);
1460 NWRAP_LOG(NWRAP_LOG_TRACE
, "Reloaded %s", nwrap
->path
);
1464 * the caller has to call nwrap_unload() on failure
1466 static bool nwrap_pw_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1468 struct nwrap_pw
*nwrap_pw
;
1475 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
1477 list_size
= sizeof(*nwrap_pw
->list
) * (nwrap_pw
->num
+1);
1478 pw
= (struct passwd
*)realloc(nwrap_pw
->list
, list_size
);
1480 NWRAP_LOG(NWRAP_LOG_ERROR
,
1481 "realloc(%u) failed",
1482 (unsigned)list_size
);
1485 nwrap_pw
->list
= pw
;
1487 pw
= &nwrap_pw
->list
[nwrap_pw
->num
];
1494 NWRAP_LOG(NWRAP_LOG_ERROR
,
1495 "Invalid line[%s]: '%s'",
1505 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]\n", pw
->pw_name
);
1510 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1518 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]\n", pw
->pw_passwd
);
1523 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1529 pw
->pw_uid
= (uid_t
)strtoul(c
, &e
, 10);
1531 NWRAP_LOG(NWRAP_LOG_ERROR
,
1532 "Invalid line[%s]: '%s' - %s",
1533 line
, c
, strerror(errno
));
1537 NWRAP_LOG(NWRAP_LOG_ERROR
,
1538 "Invalid line[%s]: '%s' - %s",
1539 line
, c
, strerror(errno
));
1543 NWRAP_LOG(NWRAP_LOG_ERROR
,
1544 "Invalid line[%s]: '%s' - %s",
1545 line
, c
, strerror(errno
));
1550 NWRAP_LOG(NWRAP_LOG_TRACE
, "uid[%u]", pw
->pw_uid
);
1555 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1561 pw
->pw_gid
= (gid_t
)strtoul(c
, &e
, 10);
1563 NWRAP_LOG(NWRAP_LOG_ERROR
,
1564 "Invalid line[%s]: '%s' - %s",
1565 line
, c
, strerror(errno
));
1569 NWRAP_LOG(NWRAP_LOG_ERROR
,
1570 "Invalid line[%s]: '%s' - %s",
1571 line
, c
, strerror(errno
));
1575 NWRAP_LOG(NWRAP_LOG_ERROR
,
1576 "Invalid line[%s]: '%s' - %s",
1577 line
, c
, strerror(errno
));
1582 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]\n", pw
->pw_gid
);
1587 NWRAP_LOG(NWRAP_LOG_ERROR
, "invalid line[%s]: '%s'", line
, c
);
1595 NWRAP_LOG(NWRAP_LOG_TRACE
, "gecos[%s]", pw
->pw_gecos
);
1600 NWRAP_LOG(NWRAP_LOG_ERROR
, "'%s'", c
);
1608 NWRAP_LOG(NWRAP_LOG_TRACE
, "dir[%s]", pw
->pw_dir
);
1612 NWRAP_LOG(NWRAP_LOG_TRACE
, "shell[%s]", pw
->pw_shell
);
1614 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1615 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1616 pw
->pw_name
, pw
->pw_passwd
,
1617 pw
->pw_uid
, pw
->pw_gid
,
1618 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
1624 static void nwrap_pw_unload(struct nwrap_cache
*nwrap
)
1626 struct nwrap_pw
*nwrap_pw
;
1627 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
1629 if (nwrap_pw
->list
) free(nwrap_pw
->list
);
1631 nwrap_pw
->list
= NULL
;
1636 static int nwrap_pw_copy_r(const struct passwd
*src
, struct passwd
*dst
,
1637 char *buf
, size_t buflen
, struct passwd
**dstp
)
1643 first
= src
->pw_name
;
1645 last
= src
->pw_shell
;
1646 while (*last
) last
++;
1648 ofs
= PTR_DIFF(last
+ 1, first
);
1650 if (ofs
> (off_t
) buflen
) {
1654 memcpy(buf
, first
, ofs
);
1656 ofs
= PTR_DIFF(src
->pw_name
, first
);
1657 dst
->pw_name
= buf
+ ofs
;
1658 ofs
= PTR_DIFF(src
->pw_passwd
, first
);
1659 dst
->pw_passwd
= buf
+ ofs
;
1660 dst
->pw_uid
= src
->pw_uid
;
1661 dst
->pw_gid
= src
->pw_gid
;
1662 ofs
= PTR_DIFF(src
->pw_gecos
, first
);
1663 dst
->pw_gecos
= buf
+ ofs
;
1664 ofs
= PTR_DIFF(src
->pw_dir
, first
);
1665 dst
->pw_dir
= buf
+ ofs
;
1666 ofs
= PTR_DIFF(src
->pw_shell
, first
);
1667 dst
->pw_shell
= buf
+ ofs
;
1677 * the caller has to call nwrap_unload() on failure
1679 static bool nwrap_gr_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1681 struct nwrap_gr
*nwrap_gr
;
1689 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
1691 list_size
= sizeof(*nwrap_gr
->list
) * (nwrap_gr
->num
+1);
1692 gr
= (struct group
*)realloc(nwrap_gr
->list
, list_size
);
1694 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc failed");
1697 nwrap_gr
->list
= gr
;
1699 gr
= &nwrap_gr
->list
[nwrap_gr
->num
];
1706 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1714 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]", gr
->gr_name
);
1719 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1727 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]", gr
->gr_passwd
);
1732 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1738 gr
->gr_gid
= (gid_t
)strtoul(c
, &e
, 10);
1740 NWRAP_LOG(NWRAP_LOG_ERROR
,
1741 "Invalid line[%s]: '%s' - %s",
1742 line
, c
, strerror(errno
));
1746 NWRAP_LOG(NWRAP_LOG_ERROR
,
1747 "Invalid line[%s]: '%s' - %s",
1748 line
, c
, strerror(errno
));
1752 NWRAP_LOG(NWRAP_LOG_ERROR
,
1753 "Invalid line[%s]: '%s' - %s",
1754 line
, c
, strerror(errno
));
1759 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]", gr
->gr_gid
);
1762 gr
->gr_mem
= (char **)malloc(sizeof(char *));
1764 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1767 gr
->gr_mem
[0] = NULL
;
1769 for(nummem
=0; p
; nummem
++) {
1779 if (strlen(c
) == 0) {
1783 m_size
= sizeof(char *) * (nummem
+2);
1784 m
= (char **)realloc(gr
->gr_mem
, m_size
);
1786 NWRAP_LOG(NWRAP_LOG_ERROR
,
1787 "realloc(%zd) failed",
1792 gr
->gr_mem
[nummem
] = c
;
1793 gr
->gr_mem
[nummem
+1] = NULL
;
1795 NWRAP_LOG(NWRAP_LOG_TRACE
,
1797 nummem
, gr
->gr_mem
[nummem
]);
1800 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1801 "Added group[%s:%s:%u:] with %u members",
1802 gr
->gr_name
, gr
->gr_passwd
, gr
->gr_gid
, nummem
);
1808 static void nwrap_gr_unload(struct nwrap_cache
*nwrap
)
1811 struct nwrap_gr
*nwrap_gr
;
1812 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
1814 if (nwrap_gr
->list
) {
1815 for (i
=0; i
< nwrap_gr
->num
; i
++) {
1816 if (nwrap_gr
->list
[i
].gr_mem
) {
1817 free(nwrap_gr
->list
[i
].gr_mem
);
1820 free(nwrap_gr
->list
);
1823 nwrap_gr
->list
= NULL
;
1828 static int nwrap_gr_copy_r(const struct group
*src
, struct group
*dst
,
1829 char *buf
, size_t buflen
, struct group
**dstp
)
1839 first
= src
->gr_name
;
1841 lastm
= src
->gr_mem
;
1848 last
= src
->gr_passwd
;
1850 while (*last
) last
++;
1852 ofsb
= PTR_DIFF(last
+ 1, first
);
1853 ofsm
= PTR_DIFF(lastm
+ 1, src
->gr_mem
);
1855 if ((ofsb
+ ofsm
) > (off_t
) buflen
) {
1859 memcpy(buf
, first
, ofsb
);
1860 memcpy(buf
+ ofsb
, src
->gr_mem
, ofsm
);
1862 ofs
= PTR_DIFF(src
->gr_name
, first
);
1863 dst
->gr_name
= buf
+ ofs
;
1864 ofs
= PTR_DIFF(src
->gr_passwd
, first
);
1865 dst
->gr_passwd
= buf
+ ofs
;
1866 dst
->gr_gid
= src
->gr_gid
;
1868 dst
->gr_mem
= (char **)(buf
+ ofsb
);
1869 for (i
=0; src
->gr_mem
[i
]; i
++) {
1870 ofs
= PTR_DIFF(src
->gr_mem
[i
], first
);
1871 dst
->gr_mem
[i
] = buf
+ ofs
;
1881 static bool nwrap_he_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1883 struct nwrap_he
*nwrap_he
= (struct nwrap_he
*)nwrap
->private_data
;
1884 struct nwrap_entdata
*ed
;
1886 bool do_aliases
= true;
1887 int aliases_count
= 0;
1892 list_size
= sizeof(struct nwrap_entdata
) * (nwrap_he
->num
+ 1);
1894 ed
= (struct nwrap_entdata
*)realloc(nwrap_he
->list
, list_size
);
1896 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc[%zd] failed", list_size
);
1899 nwrap_he
->list
= ed
;
1901 /* set it to the last element */
1902 ed
= &(nwrap_he
->list
[nwrap_he
->num
]);
1904 ed
->addr
= malloc(sizeof(struct nwrap_addrdata
));
1905 if (ed
->addr
== NULL
) {
1906 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc[%zd] failed", list_size
);
1916 /* Walk to first char */
1917 for (p
= i
; *p
!= '.' && *p
!= ':' && !isxdigit((int) *p
); p
++) {
1919 NWRAP_LOG(NWRAP_LOG_ERROR
,
1920 "Invalid line[%s]: '%s'",
1926 for (i
= p
; !isspace((int)*p
); p
++) {
1928 NWRAP_LOG(NWRAP_LOG_ERROR
,
1929 "Invalid line[%s]: '%s'",
1937 if (inet_pton(AF_INET
, i
, ed
->addr
->host_addr
)) {
1938 ed
->ht
.h_addrtype
= AF_INET
;
1939 ed
->ht
.h_length
= 4;
1941 } else if (inet_pton(AF_INET6
, i
, ed
->addr
->host_addr
)) {
1942 ed
->ht
.h_addrtype
= AF_INET6
;
1943 ed
->ht
.h_length
= 16;
1946 NWRAP_LOG(NWRAP_LOG_ERROR
,
1947 "Invalid line[%s]: '%s'",
1953 ed
->addr
->h_addr_ptrs
[0] = (char *)ed
->addr
->host_addr
;
1954 ed
->addr
->h_addr_ptrs
[1] = NULL
;
1956 ed
->ht
.h_addr_list
= ed
->addr
->h_addr_ptrs
;
1964 /* Walk to first char */
1965 for (n
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
1967 NWRAP_LOG(NWRAP_LOG_ERROR
,
1968 "Invalid line[%s]: '%s'",
1975 for (n
= p
; !isspace((int)*p
); p
++) {
1986 /* glib's getent always dereferences he->h_aliases */
1987 ed
->ht
.h_aliases
= malloc(sizeof(char *));
1988 if (ed
->ht
.h_aliases
== NULL
) {
1991 ed
->ht
.h_aliases
[0] = NULL
;
1996 while (do_aliases
) {
2002 /* Walk to first char */
2003 for (a
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
2009 /* Only trailing spaces are left */
2014 for (a
= p
; !isspace((int)*p
); p
++) {
2023 aliases
= realloc(ed
->ht
.h_aliases
, sizeof(char *) * (aliases_count
+ 2));
2024 if (aliases
== NULL
) {
2027 ed
->ht
.h_aliases
= aliases
;
2029 aliases
[aliases_count
] = a
;
2030 aliases
[aliases_count
+ 1] = NULL
;
2039 static void nwrap_he_unload(struct nwrap_cache
*nwrap
)
2041 struct nwrap_he
*nwrap_he
=
2042 (struct nwrap_he
*)nwrap
->private_data
;
2045 if (nwrap_he
->list
!= NULL
) {
2046 for (i
= 0; i
< nwrap_he
->num
; i
++) {
2047 if (nwrap_he
->list
[i
].ht
.h_aliases
!= NULL
) {
2048 free(nwrap_he
->list
[i
].ht
.h_aliases
);
2050 if (nwrap_he
->list
[i
].addr
!= NULL
) {
2051 free(nwrap_he
->list
[i
].addr
);
2054 free(nwrap_he
->list
);
2057 nwrap_he
->list
= NULL
;
2063 /* user functions */
2064 static struct passwd
*nwrap_files_getpwnam(struct nwrap_backend
*b
,
2069 (void) b
; /* unused */
2071 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Lookup user %s in files", name
);
2073 nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2075 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
2076 if (strcmp(nwrap_pw_global
.list
[i
].pw_name
, name
) == 0) {
2077 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] found", name
);
2078 return &nwrap_pw_global
.list
[i
];
2080 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2081 "user[%s] does not match [%s]",
2083 nwrap_pw_global
.list
[i
].pw_name
);
2086 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] not found\n", name
);
2092 static int nwrap_files_getpwnam_r(struct nwrap_backend
*b
,
2093 const char *name
, struct passwd
*pwdst
,
2094 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2098 pw
= nwrap_files_getpwnam(b
, name
);
2106 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
2109 static struct passwd
*nwrap_files_getpwuid(struct nwrap_backend
*b
,
2114 (void) b
; /* unused */
2116 nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2118 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
2119 if (nwrap_pw_global
.list
[i
].pw_uid
== uid
) {
2120 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] found", uid
);
2121 return &nwrap_pw_global
.list
[i
];
2123 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2124 "uid[%u] does not match [%u]",
2126 nwrap_pw_global
.list
[i
].pw_uid
);
2129 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] not found\n", uid
);
2135 static int nwrap_files_getpwuid_r(struct nwrap_backend
*b
,
2136 uid_t uid
, struct passwd
*pwdst
,
2137 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2141 pw
= nwrap_files_getpwuid(b
, uid
);
2149 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
2152 /* user enum functions */
2153 static void nwrap_files_setpwent(struct nwrap_backend
*b
)
2155 (void) b
; /* unused */
2157 nwrap_pw_global
.idx
= 0;
2160 static struct passwd
*nwrap_files_getpwent(struct nwrap_backend
*b
)
2164 (void) b
; /* unused */
2166 if (nwrap_pw_global
.idx
== 0) {
2167 nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2170 if (nwrap_pw_global
.idx
>= nwrap_pw_global
.num
) {
2175 pw
= &nwrap_pw_global
.list
[nwrap_pw_global
.idx
++];
2177 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2178 "return user[%s] uid[%u]",
2179 pw
->pw_name
, pw
->pw_uid
);
2184 static int nwrap_files_getpwent_r(struct nwrap_backend
*b
,
2185 struct passwd
*pwdst
, char *buf
,
2186 size_t buflen
, struct passwd
**pwdstp
)
2190 pw
= nwrap_files_getpwent(b
);
2198 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
2201 static void nwrap_files_endpwent(struct nwrap_backend
*b
)
2203 (void) b
; /* unused */
2205 nwrap_pw_global
.idx
= 0;
2208 /* misc functions */
2209 static int nwrap_files_initgroups(struct nwrap_backend
*b
,
2210 const char *user
, gid_t group
)
2212 (void) b
; /* unused */
2213 (void) user
; /* unused */
2214 (void) group
; /* used */
2216 /* TODO: maybe we should also fake this... */
2220 /* group functions */
2221 static struct group
*nwrap_files_getgrnam(struct nwrap_backend
*b
,
2226 (void) b
; /* unused */
2228 nwrap_files_cache_reload(nwrap_gr_global
.cache
);
2230 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
2231 if (strcmp(nwrap_gr_global
.list
[i
].gr_name
, name
) == 0) {
2232 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] found", name
);
2233 return &nwrap_gr_global
.list
[i
];
2235 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2236 "group[%s] does not match [%s]",
2238 nwrap_gr_global
.list
[i
].gr_name
);
2241 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] not found", name
);
2247 static int nwrap_files_getgrnam_r(struct nwrap_backend
*b
,
2248 const char *name
, struct group
*grdst
,
2249 char *buf
, size_t buflen
, struct group
**grdstp
)
2253 gr
= nwrap_files_getgrnam(b
, name
);
2261 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
2264 static struct group
*nwrap_files_getgrgid(struct nwrap_backend
*b
,
2269 (void) b
; /* unused */
2271 nwrap_files_cache_reload(nwrap_gr_global
.cache
);
2273 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
2274 if (nwrap_gr_global
.list
[i
].gr_gid
== gid
) {
2275 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] found", gid
);
2276 return &nwrap_gr_global
.list
[i
];
2278 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2279 "gid[%u] does not match [%u]",
2281 nwrap_gr_global
.list
[i
].gr_gid
);
2284 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] not found", gid
);
2290 static int nwrap_files_getgrgid_r(struct nwrap_backend
*b
,
2291 gid_t gid
, struct group
*grdst
,
2292 char *buf
, size_t buflen
, struct group
**grdstp
)
2296 gr
= nwrap_files_getgrgid(b
, gid
);
2304 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
2307 /* group enum functions */
2308 static void nwrap_files_setgrent(struct nwrap_backend
*b
)
2310 (void) b
; /* unused */
2312 nwrap_gr_global
.idx
= 0;
2315 static struct group
*nwrap_files_getgrent(struct nwrap_backend
*b
)
2319 (void) b
; /* unused */
2321 if (nwrap_gr_global
.idx
== 0) {
2322 nwrap_files_cache_reload(nwrap_gr_global
.cache
);
2325 if (nwrap_gr_global
.idx
>= nwrap_gr_global
.num
) {
2330 gr
= &nwrap_gr_global
.list
[nwrap_gr_global
.idx
++];
2332 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2333 "return group[%s] gid[%u]",
2334 gr
->gr_name
, gr
->gr_gid
);
2339 static int nwrap_files_getgrent_r(struct nwrap_backend
*b
,
2340 struct group
*grdst
, char *buf
,
2341 size_t buflen
, struct group
**grdstp
)
2345 gr
= nwrap_files_getgrent(b
);
2353 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
2356 static void nwrap_files_endgrent(struct nwrap_backend
*b
)
2358 (void) b
; /* unused */
2360 nwrap_gr_global
.idx
= 0;
2363 /* hosts functions */
2364 static struct hostent
*nwrap_files_gethostbyname(const char *name
, int af
)
2367 char canon_name
[DNS_NAME_MAX
] = { 0 };
2371 nwrap_files_cache_reload(nwrap_he_global
.cache
);
2373 name_len
= strlen(name
);
2374 if (name_len
< sizeof(canon_name
) && name
[name_len
- 1] == '.') {
2375 strncpy(canon_name
, name
, name_len
- 1);
2379 for (i
= 0; i
< nwrap_he_global
.num
; i
++) {
2382 he
= &nwrap_he_global
.list
[i
].ht
;
2384 /* Filter by address familiy if provided */
2385 if (af
!= AF_UNSPEC
&& he
->h_addrtype
!= af
) {
2389 if (strcasecmp(he
->h_name
, name
) == 0) {
2390 NWRAP_LOG(NWRAP_LOG_DEBUG
, "name[%s] found", name
);
2394 if (he
->h_aliases
== NULL
) {
2398 for (j
= 0; he
->h_aliases
[j
] != NULL
; j
++) {
2399 if (strcasecmp(he
->h_aliases
[j
], name
) == 0) {
2400 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2412 #ifdef HAVE_GETHOSTBYNAME_R
2413 static int nwrap_gethostbyname_r(const char *name
,
2414 struct hostent
*ret
,
2415 char *buf
, size_t buflen
,
2416 struct hostent
**result
, int *h_errnop
)
2418 *result
= nwrap_files_gethostbyname(name
, AF_UNSPEC
);
2419 if (*result
!= NULL
) {
2420 memset(buf
, '\0', buflen
);
2424 *h_errnop
= h_errno
;
2429 int gethostbyname_r(const char *name
,
2430 struct hostent
*ret
,
2431 char *buf
, size_t buflen
,
2432 struct hostent
**result
, int *h_errnop
)
2434 if (!nss_wrapper_hosts_enabled()) {
2435 return libc_gethostbyname_r(name
,
2443 return nwrap_gethostbyname_r(name
, ret
, buf
, buflen
, result
, h_errnop
);
2447 static struct hostent
*nwrap_files_gethostbyaddr(const void *addr
,
2448 socklen_t len
, int type
)
2451 char ip
[INET6_ADDRSTRLEN
] = {0};
2455 (void) len
; /* unused */
2457 nwrap_files_cache_reload(nwrap_he_global
.cache
);
2459 a
= inet_ntop(type
, addr
, ip
, sizeof(ip
));
2465 for (i
= 0; i
< nwrap_he_global
.num
; i
++) {
2466 he
= &nwrap_he_global
.list
[i
].ht
;
2468 if (he
->h_addrtype
!= type
) {
2472 if (memcmp(addr
, he
->h_addr_list
[0], he
->h_length
) == 0) {
2481 #ifdef HAVE_GETHOSTBYADDR_R
2482 static int nwrap_gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
2483 struct hostent
*ret
,
2484 char *buf
, size_t buflen
,
2485 struct hostent
**result
, int *h_errnop
)
2487 *result
= nwrap_files_gethostbyaddr(addr
, len
, type
);
2488 if (*result
!= NULL
) {
2489 memset(buf
, '\0', buflen
);
2493 *h_errnop
= h_errno
;
2498 int gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
2499 struct hostent
*ret
,
2500 char *buf
, size_t buflen
,
2501 struct hostent
**result
, int *h_errnop
)
2503 if (!nss_wrapper_hosts_enabled()) {
2504 return libc_gethostbyaddr_r(addr
,
2514 return nwrap_gethostbyaddr_r(addr
, len
, type
, ret
, buf
, buflen
, result
, h_errnop
);
2518 /* hosts enum functions */
2519 static void nwrap_files_sethostent(void)
2521 nwrap_he_global
.idx
= 0;
2524 static struct hostent
*nwrap_files_gethostent(void)
2528 if (nwrap_he_global
.idx
== 0) {
2529 nwrap_files_cache_reload(nwrap_he_global
.cache
);
2532 if (nwrap_he_global
.idx
>= nwrap_he_global
.num
) {
2537 he
= &nwrap_he_global
.list
[nwrap_he_global
.idx
++].ht
;
2539 NWRAP_LOG(NWRAP_LOG_DEBUG
, "return hosts[%s]", he
->h_name
);
2544 static void nwrap_files_endhostent(void)
2546 nwrap_he_global
.idx
= 0;
2554 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
2557 static struct passwd
*nwrap_module_getpwnam(struct nwrap_backend
*b
,
2560 static struct passwd pwd
;
2561 static char buf
[1000];
2564 if (!b
->fns
->_nss_getpwnam_r
) {
2568 status
= b
->fns
->_nss_getpwnam_r(name
, &pwd
, buf
, sizeof(buf
), &errno
);
2569 if (status
== NSS_STATUS_NOTFOUND
) {
2572 if (status
!= NSS_STATUS_SUCCESS
) {
2579 static int nwrap_module_getpwnam_r(struct nwrap_backend
*b
,
2580 const char *name
, struct passwd
*pwdst
,
2581 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2585 (void) b
; /* unused */
2586 (void) pwdst
; /* unused */
2587 (void) pwdstp
; /* unused */
2589 if (!b
->fns
->_nss_getpwnam_r
) {
2590 return NSS_STATUS_NOTFOUND
;
2593 ret
= b
->fns
->_nss_getpwnam_r(name
, pwdst
, buf
, buflen
, &errno
);
2595 case NSS_STATUS_SUCCESS
:
2597 case NSS_STATUS_NOTFOUND
:
2602 case NSS_STATUS_TRYAGAIN
:
2615 static struct passwd
*nwrap_module_getpwuid(struct nwrap_backend
*b
,
2618 static struct passwd pwd
;
2619 static char buf
[1000];
2622 if (!b
->fns
->_nss_getpwuid_r
) {
2626 status
= b
->fns
->_nss_getpwuid_r(uid
, &pwd
, buf
, sizeof(buf
), &errno
);
2627 if (status
== NSS_STATUS_NOTFOUND
) {
2630 if (status
!= NSS_STATUS_SUCCESS
) {
2636 static int nwrap_module_getpwuid_r(struct nwrap_backend
*b
,
2637 uid_t uid
, struct passwd
*pwdst
,
2638 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2642 (void) pwdstp
; /* unused */
2644 if (!b
->fns
->_nss_getpwuid_r
) {
2648 ret
= b
->fns
->_nss_getpwuid_r(uid
, pwdst
, buf
, buflen
, &errno
);
2650 case NSS_STATUS_SUCCESS
:
2652 case NSS_STATUS_NOTFOUND
:
2657 case NSS_STATUS_TRYAGAIN
:
2670 static void nwrap_module_setpwent(struct nwrap_backend
*b
)
2672 if (!b
->fns
->_nss_setpwent
) {
2676 b
->fns
->_nss_setpwent();
2679 static struct passwd
*nwrap_module_getpwent(struct nwrap_backend
*b
)
2681 static struct passwd pwd
;
2682 static char buf
[1000];
2685 if (!b
->fns
->_nss_getpwent_r
) {
2689 status
= b
->fns
->_nss_getpwent_r(&pwd
, buf
, sizeof(buf
), &errno
);
2690 if (status
== NSS_STATUS_NOTFOUND
) {
2693 if (status
!= NSS_STATUS_SUCCESS
) {
2699 static int nwrap_module_getpwent_r(struct nwrap_backend
*b
,
2700 struct passwd
*pwdst
, char *buf
,
2701 size_t buflen
, struct passwd
**pwdstp
)
2705 (void) pwdstp
; /* unused */
2707 if (!b
->fns
->_nss_getpwent_r
) {
2711 ret
= b
->fns
->_nss_getpwent_r(pwdst
, buf
, buflen
, &errno
);
2713 case NSS_STATUS_SUCCESS
:
2715 case NSS_STATUS_NOTFOUND
:
2720 case NSS_STATUS_TRYAGAIN
:
2733 static void nwrap_module_endpwent(struct nwrap_backend
*b
)
2735 if (!b
->fns
->_nss_endpwent
) {
2739 b
->fns
->_nss_endpwent();
2742 static int nwrap_module_initgroups(struct nwrap_backend
*b
,
2743 const char *user
, gid_t group
)
2749 if (!b
->fns
->_nss_initgroups
) {
2750 return NSS_STATUS_UNAVAIL
;
2753 return b
->fns
->_nss_initgroups(user
, group
, &start
, &size
, &groups
, 0, &errno
);
2756 static struct group
*nwrap_module_getgrnam(struct nwrap_backend
*b
,
2759 static struct group grp
;
2761 static int buflen
= 1000;
2764 if (!b
->fns
->_nss_getgrnam_r
) {
2769 buf
= (char *)malloc(buflen
);
2772 status
= b
->fns
->_nss_getgrnam_r(name
, &grp
, buf
, buflen
, &errno
);
2773 if (status
== NSS_STATUS_TRYAGAIN
) {
2775 buf
= (char *)realloc(buf
, buflen
);
2781 if (status
== NSS_STATUS_NOTFOUND
) {
2785 if (status
!= NSS_STATUS_SUCCESS
) {
2792 static int nwrap_module_getgrnam_r(struct nwrap_backend
*b
,
2793 const char *name
, struct group
*grdst
,
2794 char *buf
, size_t buflen
, struct group
**grdstp
)
2798 (void) grdstp
; /* unused */
2800 if (!b
->fns
->_nss_getgrnam_r
) {
2804 ret
= b
->fns
->_nss_getgrnam_r(name
, grdst
, buf
, buflen
, &errno
);
2806 case NSS_STATUS_SUCCESS
:
2808 case NSS_STATUS_NOTFOUND
:
2813 case NSS_STATUS_TRYAGAIN
:
2826 static struct group
*nwrap_module_getgrgid(struct nwrap_backend
*b
,
2829 static struct group grp
;
2831 static int buflen
= 1000;
2834 if (!b
->fns
->_nss_getgrgid_r
) {
2839 buf
= (char *)malloc(buflen
);
2843 status
= b
->fns
->_nss_getgrgid_r(gid
, &grp
, buf
, buflen
, &errno
);
2844 if (status
== NSS_STATUS_TRYAGAIN
) {
2846 buf
= (char *)realloc(buf
, buflen
);
2852 if (status
== NSS_STATUS_NOTFOUND
) {
2856 if (status
!= NSS_STATUS_SUCCESS
) {
2863 static int nwrap_module_getgrgid_r(struct nwrap_backend
*b
,
2864 gid_t gid
, struct group
*grdst
,
2865 char *buf
, size_t buflen
, struct group
**grdstp
)
2869 (void) grdstp
; /* unused */
2871 if (!b
->fns
->_nss_getgrgid_r
) {
2875 ret
= b
->fns
->_nss_getgrgid_r(gid
, grdst
, buf
, buflen
, &errno
);
2877 case NSS_STATUS_SUCCESS
:
2879 case NSS_STATUS_NOTFOUND
:
2884 case NSS_STATUS_TRYAGAIN
:
2897 static void nwrap_module_setgrent(struct nwrap_backend
*b
)
2899 if (!b
->fns
->_nss_setgrent
) {
2903 b
->fns
->_nss_setgrent();
2906 static struct group
*nwrap_module_getgrent(struct nwrap_backend
*b
)
2908 static struct group grp
;
2910 static int buflen
= 1024;
2913 if (!b
->fns
->_nss_getgrent_r
) {
2918 buf
= (char *)malloc(buflen
);
2922 status
= b
->fns
->_nss_getgrent_r(&grp
, buf
, buflen
, &errno
);
2923 if (status
== NSS_STATUS_TRYAGAIN
) {
2925 buf
= (char *)realloc(buf
, buflen
);
2931 if (status
== NSS_STATUS_NOTFOUND
) {
2935 if (status
!= NSS_STATUS_SUCCESS
) {
2942 static int nwrap_module_getgrent_r(struct nwrap_backend
*b
,
2943 struct group
*grdst
, char *buf
,
2944 size_t buflen
, struct group
**grdstp
)
2948 (void) grdstp
; /* unused */
2950 if (!b
->fns
->_nss_getgrent_r
) {
2954 ret
= b
->fns
->_nss_getgrent_r(grdst
, buf
, buflen
, &errno
);
2956 case NSS_STATUS_SUCCESS
:
2958 case NSS_STATUS_NOTFOUND
:
2963 case NSS_STATUS_TRYAGAIN
:
2976 static void nwrap_module_endgrent(struct nwrap_backend
*b
)
2978 if (!b
->fns
->_nss_endgrent
) {
2982 b
->fns
->_nss_endgrent();
2985 /****************************************************************************
2987 ***************************************************************************/
2989 static struct passwd
*nwrap_getpwnam(const char *name
)
2994 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
2995 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
2996 pwd
= b
->ops
->nw_getpwnam(b
, name
);
3005 struct passwd
*getpwnam(const char *name
)
3007 if (!nss_wrapper_enabled()) {
3008 return libc_getpwnam(name
);
3011 return nwrap_getpwnam(name
);
3014 /****************************************************************************
3016 ***************************************************************************/
3018 static int nwrap_getpwnam_r(const char *name
, struct passwd
*pwdst
,
3019 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3023 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3024 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3025 ret
= b
->ops
->nw_getpwnam_r(b
, name
, pwdst
, buf
, buflen
, pwdstp
);
3026 if (ret
== ENOENT
) {
3035 #ifdef HAVE_GETPWNAM_R
3036 # ifdef HAVE_SOLARIS_GETPWNAM_R
3037 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
3038 char *buf
, int buflen
, struct passwd
**pwdstp
)
3039 # else /* HAVE_SOLARIS_GETPWNAM_R */
3040 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
3041 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3042 # endif /* HAVE_SOLARIS_GETPWNAM_R */
3044 if (!nss_wrapper_enabled()) {
3045 return libc_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
3048 return nwrap_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
3052 /****************************************************************************
3054 ***************************************************************************/
3056 static struct passwd
*nwrap_getpwuid(uid_t uid
)
3061 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3062 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3063 pwd
= b
->ops
->nw_getpwuid(b
, uid
);
3072 struct passwd
*getpwuid(uid_t uid
)
3074 if (!nss_wrapper_enabled()) {
3075 return libc_getpwuid(uid
);
3078 return nwrap_getpwuid(uid
);
3081 /****************************************************************************
3083 ***************************************************************************/
3085 static int nwrap_getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
3086 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3090 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3091 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3092 ret
= b
->ops
->nw_getpwuid_r(b
, uid
, pwdst
, buf
, buflen
, pwdstp
);
3093 if (ret
== ENOENT
) {
3102 #ifdef HAVE_SOLARIS_GETPWUID_R
3103 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
3104 char *buf
, int buflen
, struct passwd
**pwdstp
)
3106 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
3107 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3110 if (!nss_wrapper_enabled()) {
3111 return libc_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
3114 return nwrap_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
3117 /****************************************************************************
3119 ***************************************************************************/
3121 static void nwrap_setpwent(void)
3125 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3126 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3127 b
->ops
->nw_setpwent(b
);
3133 if (!nss_wrapper_enabled()) {
3141 /****************************************************************************
3143 ***************************************************************************/
3145 static struct passwd
*nwrap_getpwent(void)
3150 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3151 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3152 pwd
= b
->ops
->nw_getpwent(b
);
3161 struct passwd
*getpwent(void)
3163 if (!nss_wrapper_enabled()) {
3164 return libc_getpwent();
3167 return nwrap_getpwent();
3170 /****************************************************************************
3172 ***************************************************************************/
3174 static int nwrap_getpwent_r(struct passwd
*pwdst
, char *buf
,
3175 size_t buflen
, struct passwd
**pwdstp
)
3179 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3180 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3181 ret
= b
->ops
->nw_getpwent_r(b
, pwdst
, buf
, buflen
, pwdstp
);
3182 if (ret
== ENOENT
) {
3191 #ifdef HAVE_SOLARIS_GETPWENT_R
3192 struct passwd
*getpwent_r(struct passwd
*pwdst
, char *buf
, int buflen
)
3194 struct passwd
*pwdstp
= NULL
;
3197 if (!nss_wrapper_enabled()) {
3198 return libc_getpwent_r(pwdst
, buf
, buflen
);
3200 rc
= nwrap_getpwent_r(pwdst
, buf
, buflen
, &pwdstp
);
3207 #else /* HAVE_SOLARIS_GETPWENT_R */
3208 int getpwent_r(struct passwd
*pwdst
, char *buf
,
3209 size_t buflen
, struct passwd
**pwdstp
)
3211 if (!nss_wrapper_enabled()) {
3212 return libc_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
3215 return nwrap_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
3217 #endif /* HAVE_SOLARIS_GETPWENT_R */
3219 /****************************************************************************
3221 ***************************************************************************/
3223 static void nwrap_endpwent(void)
3227 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3228 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3229 b
->ops
->nw_endpwent(b
);
3235 if (!nss_wrapper_enabled()) {
3243 /****************************************************************************
3245 ***************************************************************************/
3247 static int nwrap_initgroups(const char *user
, gid_t group
)
3251 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3252 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3255 rc
= b
->ops
->nw_initgroups(b
, user
, group
);
3265 int initgroups(const char *user
, gid_t group
)
3267 if (!nss_wrapper_enabled()) {
3268 return libc_initgroups(user
, group
);
3271 return nwrap_initgroups(user
, group
);
3274 /****************************************************************************
3276 ***************************************************************************/
3278 static struct group
*nwrap_getgrnam(const char *name
)
3283 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3284 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3285 grp
= b
->ops
->nw_getgrnam(b
, name
);
3294 struct group
*getgrnam(const char *name
)
3296 if (!nss_wrapper_enabled()) {
3297 return libc_getgrnam(name
);
3300 return nwrap_getgrnam(name
);
3303 /****************************************************************************
3305 ***************************************************************************/
3307 static int nwrap_getgrnam_r(const char *name
, struct group
*grdst
,
3308 char *buf
, size_t buflen
, struct group
**grdstp
)
3312 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3313 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3314 ret
= b
->ops
->nw_getgrnam_r(b
, name
, grdst
, buf
, buflen
, grdstp
);
3315 if (ret
== ENOENT
) {
3324 #ifdef HAVE_GETGRNAM_R
3325 # ifdef HAVE_SOLARIS_GETGRNAM_R
3326 int getgrnam_r(const char *name
, struct group
*grp
,
3327 char *buf
, int buflen
, struct group
**pgrp
)
3328 # else /* HAVE_SOLARIS_GETGRNAM_R */
3329 int getgrnam_r(const char *name
, struct group
*grp
,
3330 char *buf
, size_t buflen
, struct group
**pgrp
)
3331 # endif /* HAVE_SOLARIS_GETGRNAM_R */
3333 if (!nss_wrapper_enabled()) {
3334 return libc_getgrnam_r(name
,
3341 return nwrap_getgrnam_r(name
, grp
, buf
, buflen
, pgrp
);
3343 #endif /* HAVE_GETGRNAM_R */
3345 /****************************************************************************
3347 ***************************************************************************/
3349 static struct group
*nwrap_getgrgid(gid_t gid
)
3354 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3355 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3356 grp
= b
->ops
->nw_getgrgid(b
, gid
);
3365 struct group
*getgrgid(gid_t gid
)
3367 if (!nss_wrapper_enabled()) {
3368 return libc_getgrgid(gid
);
3371 return nwrap_getgrgid(gid
);
3374 /****************************************************************************
3376 ***************************************************************************/
3378 static int nwrap_getgrgid_r(gid_t gid
, struct group
*grdst
,
3379 char *buf
, size_t buflen
, struct group
**grdstp
)
3383 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3384 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3385 ret
= b
->ops
->nw_getgrgid_r(b
, gid
, grdst
, buf
, buflen
, grdstp
);
3386 if (ret
== ENOENT
) {
3395 #ifdef HAVE_GETGRGID_R
3396 # ifdef HAVE_SOLARIS_GETGRGID_R
3397 int getgrgid_r(gid_t gid
, struct group
*grdst
,
3398 char *buf
, int buflen
, struct group
**grdstp
)
3399 # else /* HAVE_SOLARIS_GETGRGID_R */
3400 int getgrgid_r(gid_t gid
, struct group
*grdst
,
3401 char *buf
, size_t buflen
, struct group
**grdstp
)
3402 # endif /* HAVE_SOLARIS_GETGRGID_R */
3404 if (!nss_wrapper_enabled()) {
3405 return libc_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
3408 return nwrap_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
3412 /****************************************************************************
3414 ***************************************************************************/
3416 static void nwrap_setgrent(void)
3420 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3421 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3422 b
->ops
->nw_setgrent(b
);
3426 #ifdef HAVE_BSD_SETGRENT
3432 if (!nss_wrapper_enabled()) {
3440 #ifdef HAVE_BSD_SETGRENT
3447 /****************************************************************************
3449 ***************************************************************************/
3451 static struct group
*nwrap_getgrent(void)
3456 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3457 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3458 grp
= b
->ops
->nw_getgrent(b
);
3467 struct group
*getgrent(void)
3469 if (!nss_wrapper_enabled()) {
3470 return libc_getgrent();
3473 return nwrap_getgrent();
3476 /****************************************************************************
3478 ***************************************************************************/
3480 static int nwrap_getgrent_r(struct group
*grdst
, char *buf
,
3481 size_t buflen
, struct group
**grdstp
)
3485 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3486 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3487 ret
= b
->ops
->nw_getgrent_r(b
, grdst
, buf
, buflen
, grdstp
);
3488 if (ret
== ENOENT
) {
3497 #ifdef HAVE_SOLARIS_GETGRENT_R
3498 struct group
*getgrent_r(struct group
*src
, char *buf
, int buflen
)
3500 struct group
*grdstp
= NULL
;
3503 if (!nss_wrapper_enabled()) {
3504 return libc_getgrent_r(src
, buf
, buflen
);
3507 rc
= nwrap_getgrent_r(src
, buf
, buflen
, &grdstp
);
3514 #else /* HAVE_SOLARIS_GETGRENT_R */
3515 int getgrent_r(struct group
*src
, char *buf
,
3516 size_t buflen
, struct group
**grdstp
)
3518 if (!nss_wrapper_enabled()) {
3519 return libc_getgrent_r(src
, buf
, buflen
, grdstp
);
3522 return nwrap_getgrent_r(src
, buf
, buflen
, grdstp
);
3524 #endif /* HAVE_SOLARIS_GETGRENT_R */
3526 /****************************************************************************
3528 ***************************************************************************/
3530 static void nwrap_endgrent(void)
3534 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3535 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3536 b
->ops
->nw_endgrent(b
);
3542 if (!nss_wrapper_enabled()) {
3550 /****************************************************************************
3552 ***************************************************************************/
3554 #ifdef HAVE_GETGROUPLIST
3555 static int nwrap_getgrouplist(const char *user
, gid_t group
,
3556 gid_t
*groups
, int *ngroups
)
3561 const char *name_of_group
= "";
3563 NWRAP_LOG(NWRAP_LOG_DEBUG
, "getgrouplist called for %s", user
);
3565 groups_tmp
= (gid_t
*)malloc(count
* sizeof(gid_t
));
3567 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
3572 memcpy(groups_tmp
, &group
, sizeof(gid_t
));
3574 grp
= nwrap_getgrgid(group
);
3576 name_of_group
= grp
->gr_name
;
3580 while ((grp
= nwrap_getgrent()) != NULL
) {
3583 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3584 "Inspecting %s for group membership",
3587 for (i
=0; grp
->gr_mem
&& grp
->gr_mem
[i
] != NULL
; i
++) {
3589 if ((strcmp(user
, grp
->gr_mem
[i
]) == 0) &&
3590 (strcmp(name_of_group
, grp
->gr_name
) != 0)) {
3592 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3593 "%s is member of %s",
3597 groups_tmp
= (gid_t
*)realloc(groups_tmp
, (count
+ 1) * sizeof(gid_t
));
3599 NWRAP_LOG(NWRAP_LOG_ERROR
,
3605 memcpy(&groups_tmp
[count
], &grp
->gr_gid
, sizeof(gid_t
));
3613 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3614 "%s is member of %d groups",
3617 if (*ngroups
< count
) {
3624 memcpy(groups
, groups_tmp
, count
* sizeof(gid_t
));
3630 int getgrouplist(const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
)
3632 if (!nss_wrapper_enabled()) {
3633 return libc_getgrouplist(user
, group
, groups
, ngroups
);
3636 return nwrap_getgrouplist(user
, group
, groups
, ngroups
);
3640 /**********************************************************
3642 **********************************************************/
3644 static void nwrap_sethostent(int stayopen
) {
3645 (void) stayopen
; /* ignored */
3647 nwrap_files_sethostent();
3650 #ifdef HAVE_SOLARIS_SETHOSTENT
3651 int sethostent(int stayopen
)
3653 if (!nss_wrapper_hosts_enabled()) {
3654 libc_sethostent(stayopen
);
3658 nwrap_sethostent(stayopen
);
3662 #else /* HAVE_SOLARIS_SETHOSTENT */
3663 void sethostent(int stayopen
)
3665 if (!nss_wrapper_hosts_enabled()) {
3666 libc_sethostent(stayopen
);
3670 nwrap_sethostent(stayopen
);
3672 #endif /* HAVE_SOLARIS_SETHOSTENT */
3674 static struct hostent
*nwrap_gethostent(void)
3676 return nwrap_files_gethostent();
3679 struct hostent
*gethostent(void) {
3680 if (!nss_wrapper_hosts_enabled()) {
3681 return libc_gethostent();
3684 return nwrap_gethostent();
3687 static void nwrap_endhostent(void) {
3688 nwrap_files_endhostent();
3691 #ifdef HAVE_SOLARIS_ENDHOSTENT
3692 int endhostent(void)
3694 if (!nss_wrapper_hosts_enabled()) {
3703 #else /* HAVE_SOLARIS_ENDHOSTENT */
3704 void endhostent(void)
3706 if (!nss_wrapper_hosts_enabled()) {
3713 #endif /* HAVE_SOLARIS_ENDHOSTENT */
3715 static struct hostent
*nwrap_gethostbyname(const char *name
)
3717 return nwrap_files_gethostbyname(name
, AF_UNSPEC
);
3720 struct hostent
*gethostbyname(const char *name
)
3722 if (!nss_wrapper_hosts_enabled()) {
3723 return libc_gethostbyname(name
);
3726 return nwrap_gethostbyname(name
);
3729 /* This is a GNU extension */
3730 #ifdef HAVE_GETHOSTBYNAME2
3731 static struct hostent
*nwrap_gethostbyname2(const char *name
, int af
)
3733 return nwrap_files_gethostbyname(name
, af
);
3736 struct hostent
*gethostbyname2(const char *name
, int af
)
3738 if (!nss_wrapper_hosts_enabled()) {
3739 return libc_gethostbyname2(name
, af
);
3742 return nwrap_gethostbyname2(name
, af
);
3746 static struct hostent
*nwrap_gethostbyaddr(const void *addr
,
3747 socklen_t len
, int type
)
3749 return nwrap_files_gethostbyaddr(addr
, len
, type
);
3752 struct hostent
*gethostbyaddr(const void *addr
,
3753 socklen_t len
, int type
)
3755 if (!nss_wrapper_hosts_enabled()) {
3756 return libc_gethostbyaddr(addr
, len
, type
);
3759 return nwrap_gethostbyaddr(addr
, len
, type
);
3762 static const struct addrinfo default_hints
=
3764 .ai_flags
= AI_ADDRCONFIG
|AI_V4MAPPED
,
3765 .ai_family
= AF_UNSPEC
,
3770 .ai_canonname
= NULL
,
3774 static int nwrap_convert_he_ai(const struct hostent
*he
,
3775 unsigned short port
,
3776 const struct addrinfo
*hints
,
3777 struct addrinfo
**pai
)
3779 struct addrinfo
*ai
;
3782 switch (he
->h_addrtype
) {
3784 socklen
= sizeof(struct sockaddr_in
);
3788 socklen
= sizeof(struct sockaddr_in6
);
3795 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
) + socklen
);
3801 ai
->ai_family
= he
->h_addrtype
;
3802 ai
->ai_socktype
= hints
->ai_socktype
;
3803 ai
->ai_protocol
= hints
->ai_protocol
;
3805 ai
->ai_addrlen
= socklen
;
3806 ai
->ai_addr
= (void *)(ai
+ 1);
3808 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
3809 ai
->ai_addr
->sa_len
= socklen
;
3811 ai
->ai_addr
->sa_family
= he
->h_addrtype
;
3813 switch (he
->h_addrtype
) {
3816 struct sockaddr_in
*sinp
=
3817 (struct sockaddr_in
*) ai
->ai_addr
;
3819 memset(sinp
, 0, sizeof(struct sockaddr_in
));
3821 sinp
->sin_port
= htons(port
);
3822 sinp
->sin_family
= AF_INET
;
3824 memset (sinp
->sin_zero
, '\0', sizeof (sinp
->sin_zero
));
3825 memcpy(&sinp
->sin_addr
, he
->h_addr_list
[0], he
->h_length
);
3832 struct sockaddr_in6
*sin6p
=
3833 (struct sockaddr_in6
*) ai
->ai_addr
;
3835 memset(sin6p
, 0, sizeof(struct sockaddr_in6
));
3837 sin6p
->sin6_port
= htons(port
);
3838 sin6p
->sin6_family
= AF_INET6
;
3840 memcpy(&sin6p
->sin6_addr
, he
->h_addr_list
[0], he
->h_length
);
3849 ai
->ai_canonname
= strdup(he
->h_name
);
3850 if (ai
->ai_canonname
== NULL
) {
3860 static int nwrap_getaddrinfo(const char *node
,
3861 const char *service
,
3862 const struct addrinfo
*hints
,
3863 struct addrinfo
**res
)
3865 struct addrinfo
*ai
= NULL
;
3866 struct addrinfo
*p
= NULL
;
3867 unsigned short port
= 0;
3878 .family
= AF_UNSPEC
,
3880 int eai
= EAI_SYSTEM
;
3884 if (node
== NULL
&& service
== NULL
) {
3888 ret
= libc_getaddrinfo(node
, service
, hints
, &p
);
3893 /* If no node has been specified, let glibc deal with it */
3898 if (hints
== NULL
) {
3899 hints
= &default_hints
;
3902 if ((hints
->ai_flags
& AI_CANONNAME
) && node
== NULL
) {
3903 return EAI_BADFLAGS
;
3906 if (service
!= NULL
&& service
[0] != '\0') {
3907 if (isdigit((int)service
[0])) {
3908 port
= (unsigned short)atoi(service
);
3910 const char *proto
= NULL
;
3913 if (hints
->ai_protocol
!= 0) {
3914 struct protoent
*pent
;
3916 pent
= getprotobynumber(hints
->ai_protocol
);
3918 proto
= pent
->p_name
;
3922 s
= getservbyname(service
, proto
);
3924 port
= ntohs(s
->s_port
);
3935 if (hints
->ai_family
== AF_UNSPEC
|| hints
->ai_family
== AF_INET
) {
3936 rc
= inet_pton(AF_INET
, node
, &addr
.in
.v4
);
3939 addr
.family
= AF_INET
;
3942 rc
= inet_pton(AF_INET6
, node
, &addr
.in
.v6
);
3944 addr
.family
= AF_INET6
;
3949 if (addr
.family
== AF_INET
) {
3950 he
= nwrap_files_gethostbyaddr(&addr
.in
.v4
,
3951 sizeof(struct in_addr
),
3954 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
);
3960 } else if (addr
.family
== AF_INET6
) {
3961 he
= nwrap_files_gethostbyaddr(&addr
.in
.v6
,
3962 sizeof(struct in6_addr
),
3965 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
);
3973 he
= nwrap_files_gethostbyname(node
, hints
->ai_family
);
3975 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
);
3984 return ret
== 0 ? 0 : eai
;
3991 if (ai
->ai_flags
== 0) {
3992 ai
->ai_flags
= hints
->ai_flags
;
3994 if (ai
->ai_socktype
== 0) {
3995 ai
->ai_socktype
= SOCK_DGRAM
;
3997 if (ai
->ai_protocol
== 0 && ai
->ai_socktype
== SOCK_DGRAM
) {
3998 ai
->ai_protocol
= 17; /* UDP */
3999 } else if (ai
->ai_protocol
== 0 && ai
->ai_socktype
== SOCK_STREAM
) {
4000 ai
->ai_protocol
= 6; /* TCP */
4003 if (hints
->ai_socktype
== 0) {
4005 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
->ai_next
);
4011 if (ai
->ai_next
->ai_flags
== 0) {
4012 ai
->ai_next
->ai_flags
= hints
->ai_flags
;
4014 if (ai
->ai_socktype
== SOCK_DGRAM
) {
4015 ai
->ai_next
->ai_socktype
= SOCK_STREAM
;
4016 } else if (ai
->ai_socktype
== SOCK_STREAM
) {
4017 ai
->ai_next
->ai_socktype
= SOCK_DGRAM
;
4019 if (ai
->ai_next
->ai_socktype
== SOCK_DGRAM
) {
4020 ai
->ai_next
->ai_protocol
= 17; /* UDP */
4021 } else if (ai
->ai_next
->ai_socktype
== SOCK_STREAM
) {
4022 ai
->ai_next
->ai_protocol
= 6; /* TCP */
4031 int getaddrinfo(const char *node
, const char *service
,
4032 const struct addrinfo
*hints
,
4033 struct addrinfo
**res
)
4035 if (!nss_wrapper_hosts_enabled()) {
4036 return libc_getaddrinfo(node
, service
, hints
, res
);
4039 return nwrap_getaddrinfo(node
, service
, hints
, res
);
4042 static int nwrap_getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4043 char *host
, size_t hostlen
,
4044 char *serv
, size_t servlen
,
4048 struct servent
*service
;
4055 if (sa
== NULL
|| salen
< sizeof(sa_family_t
)) {
4059 if ((flags
& NI_NAMEREQD
) && host
== NULL
&& serv
== NULL
) {
4063 type
= sa
->sa_family
;
4066 if (salen
< sizeof(struct sockaddr_in
))
4068 addr
= &((const struct sockaddr_in
*)sa
)->sin_addr
;
4069 addrlen
= sizeof(((const struct sockaddr_in
*)sa
)->sin_addr
);
4070 port
= ntohs(((const struct sockaddr_in
*)sa
)->sin_port
);
4074 if (salen
< sizeof(struct sockaddr_in6
))
4076 addr
= &((const struct sockaddr_in6
*)sa
)->sin6_addr
;
4077 addrlen
= sizeof(((const struct sockaddr_in6
*)sa
)->sin6_addr
);
4078 port
= ntohs(((const struct sockaddr_in6
*)sa
)->sin6_port
);
4087 if ((flags
& NI_NUMERICHOST
) == 0) {
4088 he
= nwrap_files_gethostbyaddr(addr
, addrlen
, type
);
4089 if ((flags
& NI_NAMEREQD
) && (he
== NULL
|| he
->h_name
== NULL
))
4092 if (he
!= NULL
&& he
->h_name
!= NULL
) {
4093 if (strlen(he
->h_name
) >= hostlen
)
4094 return EAI_OVERFLOW
;
4095 strcpy(host
, he
->h_name
);
4096 if (flags
& NI_NOFQDN
)
4097 host
[strcspn(host
, ".")] = '\0';
4099 if (inet_ntop(type
, addr
, host
, hostlen
) == NULL
)
4100 return (errno
== ENOSPC
) ? EAI_OVERFLOW
: EAI_FAIL
;
4106 if ((flags
& NI_NUMERICSERV
) == 0) {
4107 proto
= (flags
& NI_DGRAM
) ? "udp" : "tcp";
4108 service
= getservbyport(htons(port
), proto
);
4110 if (service
!= NULL
) {
4111 if (strlen(service
->s_name
) >= servlen
)
4112 return EAI_OVERFLOW
;
4113 strcpy(serv
, service
->s_name
);
4115 if (snprintf(serv
, servlen
, "%u", port
) >= (int) servlen
)
4116 return EAI_OVERFLOW
;
4123 #ifdef HAVE_LINUX_GETNAMEINFO
4124 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4125 char *host
, socklen_t hostlen
,
4126 char *serv
, socklen_t servlen
,
4128 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
4129 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4130 char *host
, socklen_t hostlen
,
4131 char *serv
, socklen_t servlen
,
4134 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4135 char *host
, size_t hostlen
,
4136 char *serv
, size_t servlen
,
4140 if (!nss_wrapper_hosts_enabled()) {
4141 return libc_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
4144 return nwrap_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
4147 static int nwrap_gethostname(char *name
, size_t len
)
4149 const char *hostname
= getenv("NSS_WRAPPER_HOSTNAME");
4151 if (strlen(hostname
) >= len
) {
4152 errno
= ENAMETOOLONG
;
4155 snprintf(name
, len
, "%s", hostname
);
4160 #ifdef HAVE_SOLARIS_GETHOSTNAME
4161 int gethostname(char *name
, int len
)
4162 #else /* HAVE_SOLARIS_GETHOSTNAME */
4163 int gethostname(char *name
, size_t len
)
4164 #endif /* HAVE_SOLARIS_GETHOSTNAME */
4166 if (!nwrap_hostname_enabled()) {
4167 return libc_gethostname(name
, len
);
4170 return nwrap_gethostname(name
, len
);
4173 /****************************
4175 ***************************/
4178 * This function is called when the library is unloaded and makes sure that
4179 * sockets get closed and the unix file for the socket are unlinked.
4181 void nwrap_destructor(void)
4185 if (nwrap_main_global
!= NULL
) {
4186 struct nwrap_main
*m
= nwrap_main_global
;
4189 SAFE_FREE(m
->libc
->fns
);
4190 if (m
->libc
->handle
!= NULL
) {
4191 dlclose(m
->libc
->handle
);
4193 if (m
->libc
->nsl_handle
!= NULL
) {
4194 dlclose(m
->libc
->nsl_handle
);
4196 if (m
->libc
->sock_handle
!= NULL
) {
4197 dlclose(m
->libc
->sock_handle
);
4202 for (i
= 0; i
< m
->num_backends
; i
++) {
4203 struct nwrap_backend
*b
= &(m
->backends
[i
]);
4205 if (b
->so_handle
!= NULL
) {
4206 dlclose(b
->so_handle
);
4210 SAFE_FREE(m
->backends
);
4213 if (nwrap_pw_global
.cache
!= NULL
) {
4214 struct nwrap_cache
*c
= nwrap_pw_global
.cache
;
4216 nwrap_files_cache_unload(c
);
4221 SAFE_FREE(nwrap_pw_global
.list
);
4222 nwrap_pw_global
.num
= 0;
4225 if (nwrap_gr_global
.cache
!= NULL
) {
4226 struct nwrap_cache
*c
= nwrap_gr_global
.cache
;
4228 nwrap_files_cache_unload(c
);
4233 SAFE_FREE(nwrap_gr_global
.list
);
4234 nwrap_pw_global
.num
= 0;
4237 if (nwrap_he_global
.cache
!= NULL
) {
4238 struct nwrap_cache
*c
= nwrap_he_global
.cache
;
4240 nwrap_files_cache_unload(c
);
4245 SAFE_FREE(nwrap_he_global
.list
);
4246 nwrap_he_global
.num
= 0;