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__
124 /* GCC have printf type attribute check. */
125 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
126 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
128 #define PRINTF_ATTRIBUTE(a,b)
129 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
131 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
132 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
134 #define DESTRUCTOR_ATTRIBUTE
135 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
137 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
139 enum nwrap_dbglvl_e
{
147 # define NWRAP_LOG(...)
150 static void nwrap_log(enum nwrap_dbglvl_e dbglvl
, const char *func
, const char *format
, ...) PRINTF_ATTRIBUTE(3, 4);
151 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
153 static void nwrap_log(enum nwrap_dbglvl_e dbglvl
,
155 const char *format
, ...)
160 unsigned int lvl
= 0;
163 d
= getenv("NSS_WRAPPER_DEBUGLEVEL");
168 va_start(va
, format
);
169 vsnprintf(buffer
, sizeof(buffer
), format
, va
);
174 case NWRAP_LOG_ERROR
:
176 "NWRAP_ERROR(%d) - %s: %s\n",
181 "NWRAP_WARN(%d) - %s: %s\n",
184 case NWRAP_LOG_DEBUG
:
186 "NWRAP_DEBUG(%d) - %s: %s\n",
189 case NWRAP_LOG_TRACE
:
191 "NWRAP_TRACE(%d) - %s: %s\n",
197 #endif /* NDEBUG NWRAP_LOG */
199 struct nwrap_libc_fns
{
200 struct passwd
*(*_libc_getpwnam
)(const char *name
);
201 int (*_libc_getpwnam_r
)(const char *name
, struct passwd
*pwd
,
202 char *buf
, size_t buflen
, struct passwd
**result
);
203 struct passwd
*(*_libc_getpwuid
)(uid_t uid
);
204 int (*_libc_getpwuid_r
)(uid_t uid
, struct passwd
*pwd
, char *buf
, size_t buflen
, struct passwd
**result
);
205 void (*_libc_setpwent
)(void);
206 struct passwd
*(*_libc_getpwent
)(void);
207 #ifdef HAVE_SOLARIS_GETPWENT_R
208 struct passwd
*(*_libc_getpwent_r
)(struct passwd
*pwbuf
, char *buf
, size_t buflen
);
210 int (*_libc_getpwent_r
)(struct passwd
*pwbuf
, char *buf
, size_t buflen
, struct passwd
**pwbufp
);
212 void (*_libc_endpwent
)(void);
213 int (*_libc_initgroups
)(const char *user
, gid_t gid
);
214 struct group
*(*_libc_getgrnam
)(const char *name
);
215 int (*_libc_getgrnam_r
)(const char *name
, struct group
*grp
, char *buf
, size_t buflen
, struct group
**result
);
216 struct group
*(*_libc_getgrgid
)(gid_t gid
);
217 int (*_libc_getgrgid_r
)(gid_t gid
, struct group
*grp
, char *buf
, size_t buflen
, struct group
**result
);
218 void (*_libc_setgrent
)(void);
219 struct group
*(*_libc_getgrent
)(void);
220 #ifdef HAVE_SOLARIS_GETGRENT_R
221 struct group
*(*_libc_getgrent_r
)(struct group
*group
, char *buf
, size_t buflen
);
223 int (*_libc_getgrent_r
)(struct group
*group
, char *buf
, size_t buflen
, struct group
**result
);
225 void (*_libc_endgrent
)(void);
226 int (*_libc_getgrouplist
)(const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
);
228 void (*_libc_sethostent
)(int stayopen
);
229 struct hostent
*(*_libc_gethostent
)(void);
230 void (*_libc_endhostent
)(void);
232 struct hostent
*(*_libc_gethostbyname
)(const char *name
);
233 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
234 struct hostent
*(*_libc_gethostbyname2
)(const char *name
, int af
);
236 struct hostent
*(*_libc_gethostbyaddr
)(const void *addr
, socklen_t len
, int type
);
238 int (*_libc_getaddrinfo
)(const char *node
, const char *service
,
239 const struct addrinfo
*hints
,
240 struct addrinfo
**res
);
241 int (*_libc_getnameinfo
)(const struct sockaddr
*sa
, socklen_t salen
,
242 char *host
, size_t hostlen
,
243 char *serv
, size_t servlen
,
245 int (*_libc_gethostname
)(char *name
, size_t len
);
246 #ifdef HAVE_GETHOSTBYNAME_R
247 int (*_libc_gethostbyname_r
)(const char *name
,
249 char *buf
, size_t buflen
,
250 struct hostent
**result
, int *h_errnop
);
252 #ifdef HAVE_GETHOSTBYADDR_R
253 int (*_libc_gethostbyaddr_r
)(const void *addr
, socklen_t len
, int type
,
255 char *buf
, size_t buflen
,
256 struct hostent
**result
, int *h_errnop
);
260 struct nwrap_module_nss_fns
{
261 NSS_STATUS (*_nss_getpwnam_r
)(const char *name
, struct passwd
*result
, char *buffer
,
262 size_t buflen
, int *errnop
);
263 NSS_STATUS (*_nss_getpwuid_r
)(uid_t uid
, struct passwd
*result
, char *buffer
,
264 size_t buflen
, int *errnop
);
265 NSS_STATUS (*_nss_setpwent
)(void);
266 NSS_STATUS (*_nss_getpwent_r
)(struct passwd
*result
, char *buffer
,
267 size_t buflen
, int *errnop
);
268 NSS_STATUS (*_nss_endpwent
)(void);
269 NSS_STATUS (*_nss_initgroups
)(const char *user
, gid_t group
, long int *start
,
270 long int *size
, gid_t
**groups
, long int limit
, int *errnop
);
271 NSS_STATUS (*_nss_getgrnam_r
)(const char *name
, struct group
*result
, char *buffer
,
272 size_t buflen
, int *errnop
);
273 NSS_STATUS (*_nss_getgrgid_r
)(gid_t gid
, struct group
*result
, char *buffer
,
274 size_t buflen
, int *errnop
);
275 NSS_STATUS (*_nss_setgrent
)(void);
276 NSS_STATUS (*_nss_getgrent_r
)(struct group
*result
, char *buffer
,
277 size_t buflen
, int *errnop
);
278 NSS_STATUS (*_nss_endgrent
)(void);
281 struct nwrap_backend
{
285 struct nwrap_ops
*ops
;
286 struct nwrap_module_nss_fns
*fns
;
290 struct passwd
* (*nw_getpwnam
)(struct nwrap_backend
*b
,
292 int (*nw_getpwnam_r
)(struct nwrap_backend
*b
,
293 const char *name
, struct passwd
*pwdst
,
294 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
295 struct passwd
* (*nw_getpwuid
)(struct nwrap_backend
*b
,
297 int (*nw_getpwuid_r
)(struct nwrap_backend
*b
,
298 uid_t uid
, struct passwd
*pwdst
,
299 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
300 void (*nw_setpwent
)(struct nwrap_backend
*b
);
301 struct passwd
* (*nw_getpwent
)(struct nwrap_backend
*b
);
302 int (*nw_getpwent_r
)(struct nwrap_backend
*b
,
303 struct passwd
*pwdst
, char *buf
,
304 size_t buflen
, struct passwd
**pwdstp
);
305 void (*nw_endpwent
)(struct nwrap_backend
*b
);
306 int (*nw_initgroups
)(struct nwrap_backend
*b
,
307 const char *user
, gid_t group
);
308 struct group
* (*nw_getgrnam
)(struct nwrap_backend
*b
,
310 int (*nw_getgrnam_r
)(struct nwrap_backend
*b
,
311 const char *name
, struct group
*grdst
,
312 char *buf
, size_t buflen
, struct group
**grdstp
);
313 struct group
* (*nw_getgrgid
)(struct nwrap_backend
*b
,
315 int (*nw_getgrgid_r
)(struct nwrap_backend
*b
,
316 gid_t gid
, struct group
*grdst
,
317 char *buf
, size_t buflen
, struct group
**grdstp
);
318 void (*nw_setgrent
)(struct nwrap_backend
*b
);
319 struct group
* (*nw_getgrent
)(struct nwrap_backend
*b
);
320 int (*nw_getgrent_r
)(struct nwrap_backend
*b
,
321 struct group
*grdst
, char *buf
,
322 size_t buflen
, struct group
**grdstp
);
323 void (*nw_endgrent
)(struct nwrap_backend
*b
);
326 /* Public prototypes */
328 bool nss_wrapper_enabled(void);
329 bool nss_wrapper_hosts_enabled(void);
331 /* prototypes for files backend */
334 static struct passwd
*nwrap_files_getpwnam(struct nwrap_backend
*b
,
336 static int nwrap_files_getpwnam_r(struct nwrap_backend
*b
,
337 const char *name
, struct passwd
*pwdst
,
338 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
339 static struct passwd
*nwrap_files_getpwuid(struct nwrap_backend
*b
,
341 static int nwrap_files_getpwuid_r(struct nwrap_backend
*b
,
342 uid_t uid
, struct passwd
*pwdst
,
343 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
344 static void nwrap_files_setpwent(struct nwrap_backend
*b
);
345 static struct passwd
*nwrap_files_getpwent(struct nwrap_backend
*b
);
346 static int nwrap_files_getpwent_r(struct nwrap_backend
*b
,
347 struct passwd
*pwdst
, char *buf
,
348 size_t buflen
, struct passwd
**pwdstp
);
349 static void nwrap_files_endpwent(struct nwrap_backend
*b
);
350 static int nwrap_files_initgroups(struct nwrap_backend
*b
,
351 const char *user
, gid_t group
);
352 static struct group
*nwrap_files_getgrnam(struct nwrap_backend
*b
,
354 static int nwrap_files_getgrnam_r(struct nwrap_backend
*b
,
355 const char *name
, struct group
*grdst
,
356 char *buf
, size_t buflen
, struct group
**grdstp
);
357 static struct group
*nwrap_files_getgrgid(struct nwrap_backend
*b
,
359 static int nwrap_files_getgrgid_r(struct nwrap_backend
*b
,
360 gid_t gid
, struct group
*grdst
,
361 char *buf
, size_t buflen
, struct group
**grdstp
);
362 static void nwrap_files_setgrent(struct nwrap_backend
*b
);
363 static struct group
*nwrap_files_getgrent(struct nwrap_backend
*b
);
364 static int nwrap_files_getgrent_r(struct nwrap_backend
*b
,
365 struct group
*grdst
, char *buf
,
366 size_t buflen
, struct group
**grdstp
);
367 static void nwrap_files_endgrent(struct nwrap_backend
*b
);
369 /* prototypes for module backend */
371 static struct passwd
*nwrap_module_getpwent(struct nwrap_backend
*b
);
372 static int nwrap_module_getpwent_r(struct nwrap_backend
*b
,
373 struct passwd
*pwdst
, char *buf
,
374 size_t buflen
, struct passwd
**pwdstp
);
375 static struct passwd
*nwrap_module_getpwnam(struct nwrap_backend
*b
,
377 static int nwrap_module_getpwnam_r(struct nwrap_backend
*b
,
378 const char *name
, struct passwd
*pwdst
,
379 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
380 static struct passwd
*nwrap_module_getpwuid(struct nwrap_backend
*b
,
382 static int nwrap_module_getpwuid_r(struct nwrap_backend
*b
,
383 uid_t uid
, struct passwd
*pwdst
,
384 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
385 static void nwrap_module_setpwent(struct nwrap_backend
*b
);
386 static void nwrap_module_endpwent(struct nwrap_backend
*b
);
387 static struct group
*nwrap_module_getgrent(struct nwrap_backend
*b
);
388 static int nwrap_module_getgrent_r(struct nwrap_backend
*b
,
389 struct group
*grdst
, char *buf
,
390 size_t buflen
, struct group
**grdstp
);
391 static struct group
*nwrap_module_getgrnam(struct nwrap_backend
*b
,
393 static int nwrap_module_getgrnam_r(struct nwrap_backend
*b
,
394 const char *name
, struct group
*grdst
,
395 char *buf
, size_t buflen
, struct group
**grdstp
);
396 static struct group
*nwrap_module_getgrgid(struct nwrap_backend
*b
,
398 static int nwrap_module_getgrgid_r(struct nwrap_backend
*b
,
399 gid_t gid
, struct group
*grdst
,
400 char *buf
, size_t buflen
, struct group
**grdstp
);
401 static void nwrap_module_setgrent(struct nwrap_backend
*b
);
402 static void nwrap_module_endgrent(struct nwrap_backend
*b
);
403 static int nwrap_module_initgroups(struct nwrap_backend
*b
,
404 const char *user
, gid_t group
);
406 struct nwrap_ops nwrap_files_ops
= {
407 .nw_getpwnam
= nwrap_files_getpwnam
,
408 .nw_getpwnam_r
= nwrap_files_getpwnam_r
,
409 .nw_getpwuid
= nwrap_files_getpwuid
,
410 .nw_getpwuid_r
= nwrap_files_getpwuid_r
,
411 .nw_setpwent
= nwrap_files_setpwent
,
412 .nw_getpwent
= nwrap_files_getpwent
,
413 .nw_getpwent_r
= nwrap_files_getpwent_r
,
414 .nw_endpwent
= nwrap_files_endpwent
,
415 .nw_initgroups
= nwrap_files_initgroups
,
416 .nw_getgrnam
= nwrap_files_getgrnam
,
417 .nw_getgrnam_r
= nwrap_files_getgrnam_r
,
418 .nw_getgrgid
= nwrap_files_getgrgid
,
419 .nw_getgrgid_r
= nwrap_files_getgrgid_r
,
420 .nw_setgrent
= nwrap_files_setgrent
,
421 .nw_getgrent
= nwrap_files_getgrent
,
422 .nw_getgrent_r
= nwrap_files_getgrent_r
,
423 .nw_endgrent
= nwrap_files_endgrent
,
426 struct nwrap_ops nwrap_module_ops
= {
427 .nw_getpwnam
= nwrap_module_getpwnam
,
428 .nw_getpwnam_r
= nwrap_module_getpwnam_r
,
429 .nw_getpwuid
= nwrap_module_getpwuid
,
430 .nw_getpwuid_r
= nwrap_module_getpwuid_r
,
431 .nw_setpwent
= nwrap_module_setpwent
,
432 .nw_getpwent
= nwrap_module_getpwent
,
433 .nw_getpwent_r
= nwrap_module_getpwent_r
,
434 .nw_endpwent
= nwrap_module_endpwent
,
435 .nw_initgroups
= nwrap_module_initgroups
,
436 .nw_getgrnam
= nwrap_module_getgrnam
,
437 .nw_getgrnam_r
= nwrap_module_getgrnam_r
,
438 .nw_getgrgid
= nwrap_module_getgrgid
,
439 .nw_getgrgid_r
= nwrap_module_getgrgid_r
,
440 .nw_setgrent
= nwrap_module_setgrent
,
441 .nw_getgrent
= nwrap_module_getgrent
,
442 .nw_getgrent_r
= nwrap_module_getgrent_r
,
443 .nw_endgrent
= nwrap_module_endgrent
,
450 struct nwrap_libc_fns
*fns
;
454 const char *nwrap_switch
;
456 struct nwrap_backend
*backends
;
457 struct nwrap_libc
*libc
;
460 struct nwrap_main
*nwrap_main_global
;
461 struct nwrap_main __nwrap_main_global
;
469 bool (*parse_line
)(struct nwrap_cache
*, char *line
);
470 void (*unload
)(struct nwrap_cache
*);
474 struct nwrap_cache
*cache
;
481 struct nwrap_cache __nwrap_cache_pw
;
482 struct nwrap_pw nwrap_pw_global
;
484 static bool nwrap_pw_parse_line(struct nwrap_cache
*nwrap
, char *line
);
485 static void nwrap_pw_unload(struct nwrap_cache
*nwrap
);
488 struct nwrap_cache
*cache
;
495 struct nwrap_cache __nwrap_cache_gr
;
496 struct nwrap_gr nwrap_gr_global
;
498 static bool nwrap_he_parse_line(struct nwrap_cache
*nwrap
, char *line
);
499 static void nwrap_he_unload(struct nwrap_cache
*nwrap
);
501 struct nwrap_addrdata
{
502 unsigned char host_addr
[16]; /* IPv4 or IPv6 address */
503 char *h_addr_ptrs
[2]; /* host_addr pointer + NULL */
506 struct nwrap_entdata
{
507 struct nwrap_addrdata
*addr
;
512 struct nwrap_cache
*cache
;
514 struct nwrap_entdata
*list
;
519 struct nwrap_cache __nwrap_cache_he
;
520 struct nwrap_he nwrap_he_global
;
523 /*********************************************************
525 *********************************************************/
527 static void nwrap_init(void);
528 static bool nwrap_gr_parse_line(struct nwrap_cache
*nwrap
, char *line
);
529 static void nwrap_gr_unload(struct nwrap_cache
*nwrap
);
530 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE
;
532 /*********************************************************
533 * NWRAP LIBC LOADER FUNCTIONS
534 *********************************************************/
543 static const char *nwrap_str_lib(enum nwrap_lib lib
)
550 case NWRAP_LIBSOCKET
:
554 /* Compiler would warn us about unhandled enum value if we get here */
559 static void *nwrap_load_lib_handle(enum nwrap_lib lib
)
561 int flags
= RTLD_LAZY
;
566 flags
|= RTLD_DEEPBIND
;
572 handle
= nwrap_main_global
->libc
->nsl_handle
;
573 if (handle
== NULL
) {
574 for (handle
= NULL
, i
= 10; handle
== NULL
&& i
>= 0; i
--) {
575 char soname
[256] = {0};
577 snprintf(soname
, sizeof(soname
), "libnsl.so.%d", i
);
578 handle
= dlopen(soname
, flags
);
581 nwrap_main_global
->libc
->nsl_handle
= handle
;
586 case NWRAP_LIBSOCKET
:
587 #ifdef HAVE_LIBSOCKET
588 handle
= nwrap_main_global
->libc
->sock_handle
;
589 if (handle
== NULL
) {
590 for (handle
= NULL
, i
= 10; handle
== NULL
&& i
>= 0; i
--) {
591 char soname
[256] = {0};
593 snprintf(soname
, sizeof(soname
), "libsocket.so.%d", i
);
594 handle
= dlopen(soname
, flags
);
597 nwrap_main_global
->libc
->sock_handle
= handle
;
603 handle
= nwrap_main_global
->libc
->handle
;
604 if (handle
== NULL
) {
605 for (handle
= NULL
, i
= 10; handle
== NULL
&& i
>= 0; i
--) {
606 char soname
[256] = {0};
608 snprintf(soname
, sizeof(soname
), "libc.so.%d", i
);
609 handle
= dlopen(soname
, flags
);
612 nwrap_main_global
->libc
->handle
= handle
;
617 if (handle
== NULL
) {
619 handle
= nwrap_main_global
->libc
->handle
620 = nwrap_main_global
->libc
->sock_handle
621 = nwrap_main_global
->libc
->nsl_handle
624 NWRAP_LOG(NWRAP_LOG_ERROR
,
625 "Failed to dlopen library: %s\n",
634 static void *_nwrap_load_lib_function(enum nwrap_lib lib
, const char *fn_name
)
641 handle
= nwrap_load_lib_handle(lib
);
643 func
= dlsym(handle
, fn_name
);
645 NWRAP_LOG(NWRAP_LOG_ERROR
,
646 "Failed to find %s: %s\n",
651 NWRAP_LOG(NWRAP_LOG_TRACE
,
653 fn_name
, nwrap_str_lib(lib
));
657 #define nwrap_load_lib_function(lib, fn_name) \
658 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
659 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
660 _nwrap_load_lib_function(lib, #fn_name); \
666 * Functions expeciall from libc need to be loaded individually, you can't load
667 * all at once or gdb will segfault at startup. The same applies to valgrind and
668 * has probably something todo with with the linker.
669 * So we need load each function at the point it is called the first time.
671 static struct passwd
*libc_getpwnam(const char *name
)
673 nwrap_load_lib_function(NWRAP_LIBC
, getpwnam
);
675 return nwrap_main_global
->libc
->fns
->_libc_getpwnam(name
);
678 #ifdef HAVE_GETPWNAM_R
679 static int libc_getpwnam_r(const char *name
,
683 struct passwd
**result
)
685 #ifdef HAVE___POSIX_GETPWNAM_R
686 if (nwrap_main_global
->libc
->fns
->_libc_getpwnam_r
== NULL
) {
687 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getpwnam_r
) =
688 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getpwnam_r");
691 nwrap_load_lib_function(NWRAP_LIBC
, getpwnam_r
);
694 return nwrap_main_global
->libc
->fns
->_libc_getpwnam_r(name
,
702 static struct passwd
*libc_getpwuid(uid_t uid
)
704 nwrap_load_lib_function(NWRAP_LIBC
, getpwuid
);
706 return nwrap_main_global
->libc
->fns
->_libc_getpwuid(uid
);
709 #ifdef HAVE_GETPWUID_R
710 static int libc_getpwuid_r(uid_t uid
,
714 struct passwd
**result
)
716 #ifdef HAVE___POSIX_GETPWUID_R
717 if (nwrap_main_global
->libc
->fns
->_libc_getpwuid_r
== NULL
) {
718 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getpwuid_r
) =
719 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getpwuid_r");
722 nwrap_load_lib_function(NWRAP_LIBC
, getpwuid_r
);
725 return nwrap_main_global
->libc
->fns
->_libc_getpwuid_r(uid
,
733 static void libc_setpwent(void)
735 nwrap_load_lib_function(NWRAP_LIBC
, setpwent
);
737 nwrap_main_global
->libc
->fns
->_libc_setpwent();
740 static struct passwd
*libc_getpwent(void)
742 nwrap_load_lib_function(NWRAP_LIBC
, getpwent
);
744 return nwrap_main_global
->libc
->fns
->_libc_getpwent();
747 #ifdef HAVE_SOLARIS_GETPWENT_R
748 static struct passwd
*libc_getpwent_r(struct passwd
*pwdst
,
752 nwrap_load_lib_function(NWRAP_LIBC
, getpwent_r
);
754 return nwrap_main_global
->libc
->fns
->_libc_getpwent_r(pwdst
,
758 #else /* HAVE_SOLARIS_GETPWENT_R */
759 static int libc_getpwent_r(struct passwd
*pwdst
,
762 struct passwd
**pwdstp
)
764 nwrap_load_lib_function(NWRAP_LIBC
, getpwent_r
);
766 return nwrap_main_global
->libc
->fns
->_libc_getpwent_r(pwdst
,
771 #endif /* HAVE_SOLARIS_GETPWENT_R */
773 static void libc_endpwent(void)
775 nwrap_load_lib_function(NWRAP_LIBC
, endpwent
);
777 nwrap_main_global
->libc
->fns
->_libc_endpwent();
780 static int libc_initgroups(const char *user
, gid_t gid
)
782 nwrap_load_lib_function(NWRAP_LIBC
, initgroups
);
784 return nwrap_main_global
->libc
->fns
->_libc_initgroups(user
, gid
);
787 static struct group
*libc_getgrnam(const char *name
)
789 nwrap_load_lib_function(NWRAP_LIBC
, getgrnam
);
791 return nwrap_main_global
->libc
->fns
->_libc_getgrnam(name
);
794 #ifdef HAVE_GETGRNAM_R
795 static int libc_getgrnam_r(const char *name
,
799 struct group
**result
)
801 #ifdef HAVE___POSIX_GETGRNAM_R
802 if (nwrap_main_global
->libc
->fns
->_libc_getgrnam_r
== NULL
) {
803 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getgrnam_r
) =
804 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getgrnam_r");
807 nwrap_load_lib_function(NWRAP_LIBC
, getgrnam_r
);
810 return nwrap_main_global
->libc
->fns
->_libc_getgrnam_r(name
,
818 static struct group
*libc_getgrgid(gid_t gid
)
820 nwrap_load_lib_function(NWRAP_LIBC
, getgrgid
);
822 return nwrap_main_global
->libc
->fns
->_libc_getgrgid(gid
);
825 #ifdef HAVE_GETGRGID_R
826 static int libc_getgrgid_r(gid_t gid
,
830 struct group
**result
)
832 #ifdef HAVE___POSIX_GETGRGID_R
833 if (nwrap_main_global
->libc
->fns
->_libc_getgrgid_r
== NULL
) {
834 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getgrgid_r
) =
835 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getgrgid_r");
838 nwrap_load_lib_function(NWRAP_LIBC
, getgrgid_r
);
841 return nwrap_main_global
->libc
->fns
->_libc_getgrgid_r(gid
,
849 static void libc_setgrent(void)
851 nwrap_load_lib_function(NWRAP_LIBC
, setgrent
);
853 nwrap_main_global
->libc
->fns
->_libc_setgrent();
856 static struct group
*libc_getgrent(void)
858 nwrap_load_lib_function(NWRAP_LIBC
, getgrent
);
860 return nwrap_main_global
->libc
->fns
->_libc_getgrent();
863 #ifdef HAVE_GETGRENT_R
864 #ifdef HAVE_SOLARIS_GETGRENT_R
865 static struct group
*libc_getgrent_r(struct group
*group
,
869 nwrap_load_lib_function(NWRAP_LIBC
, getgrent_r
);
871 return nwrap_main_global
->libc
->fns
->_libc_getgrent_r(group
,
875 #else /* !HAVE_SOLARIS_GETGRENT_R */
876 static int libc_getgrent_r(struct group
*group
,
879 struct group
**result
)
881 nwrap_load_lib_function(NWRAP_LIBC
, getgrent_r
);
883 return nwrap_main_global
->libc
->fns
->_libc_getgrent_r(group
,
888 #endif /* HAVE_SOLARIS_GETGRENT_R */
889 #endif /* HAVE_GETGRENT_R */
891 static void libc_endgrent(void)
893 nwrap_load_lib_function(NWRAP_LIBC
, endgrent
);
895 nwrap_main_global
->libc
->fns
->_libc_endgrent();
898 #ifdef HAVE_GETGROUPLIST
899 static int libc_getgrouplist(const char *user
,
904 nwrap_load_lib_function(NWRAP_LIBC
, getgrouplist
);
906 return nwrap_main_global
->libc
->fns
->_libc_getgrouplist(user
,
913 static void libc_sethostent(int stayopen
)
915 nwrap_load_lib_function(NWRAP_LIBNSL
, sethostent
);
917 nwrap_main_global
->libc
->fns
->_libc_sethostent(stayopen
);
920 static struct hostent
*libc_gethostent(void)
922 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostent
);
924 return nwrap_main_global
->libc
->fns
->_libc_gethostent();
927 static void libc_endhostent(void)
929 nwrap_load_lib_function(NWRAP_LIBNSL
, endhostent
);
931 nwrap_main_global
->libc
->fns
->_libc_endhostent();
934 static struct hostent
*libc_gethostbyname(const char *name
)
936 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname
);
938 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname(name
);
941 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
942 static struct hostent
*libc_gethostbyname2(const char *name
, int af
)
944 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname2
);
946 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname2(name
, af
);
950 static struct hostent
*libc_gethostbyaddr(const void *addr
,
954 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyaddr
);
956 return nwrap_main_global
->libc
->fns
->_libc_gethostbyaddr(addr
,
961 static int libc_gethostname(char *name
, size_t len
)
963 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostname
);
965 return nwrap_main_global
->libc
->fns
->_libc_gethostname(name
, len
);
968 #ifdef HAVE_GETHOSTBYNAME_R
969 static int libc_gethostbyname_r(const char *name
,
973 struct hostent
**result
,
976 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname_r
);
978 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname_r(name
,
987 #ifdef HAVE_GETHOSTBYADDR_R
988 static int libc_gethostbyaddr_r(const void *addr
,
994 struct hostent
**result
,
997 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyaddr_r
);
999 return nwrap_main_global
->libc
->fns
->_libc_gethostbyaddr_r(addr
,
1010 static int libc_getaddrinfo(const char *node
,
1011 const char *service
,
1012 const struct addrinfo
*hints
,
1013 struct addrinfo
**res
)
1015 nwrap_load_lib_function(NWRAP_LIBSOCKET
, getaddrinfo
);
1017 return nwrap_main_global
->libc
->fns
->_libc_getaddrinfo(node
,
1023 static int libc_getnameinfo(const struct sockaddr
*sa
,
1031 nwrap_load_lib_function(NWRAP_LIBSOCKET
, getnameinfo
);
1033 return nwrap_main_global
->libc
->fns
->_libc_getnameinfo(sa
,
1042 /*********************************************************
1043 * NWRAP NSS MODULE LOADER FUNCTIONS
1044 *********************************************************/
1046 static void *nwrap_load_module_fn(struct nwrap_backend
*b
,
1047 const char *fn_name
)
1052 if (!b
->so_handle
) {
1053 NWRAP_LOG(NWRAP_LOG_ERROR
, "No handle");
1057 if (asprintf(&s
, "_nss_%s_%s", b
->name
, fn_name
) == -1) {
1058 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1062 res
= dlsym(b
->so_handle
, s
);
1064 NWRAP_LOG(NWRAP_LOG_ERROR
,
1065 "Cannot find function %s in %s",
1073 static struct nwrap_module_nss_fns
*nwrap_load_module_fns(struct nwrap_backend
*b
)
1075 struct nwrap_module_nss_fns
*fns
;
1077 if (!b
->so_handle
) {
1081 fns
= (struct nwrap_module_nss_fns
*)malloc(sizeof(struct nwrap_module_nss_fns
));
1086 *(void **)(&fns
->_nss_getpwnam_r
) =
1087 nwrap_load_module_fn(b
, "getpwnam_r");
1088 *(void **)(&fns
->_nss_getpwuid_r
) =
1089 nwrap_load_module_fn(b
, "getpwuid_r");
1090 *(void **)(&fns
->_nss_setpwent
) =
1091 nwrap_load_module_fn(b
, "setpwent");
1092 *(void **)(&fns
->_nss_getpwent_r
) =
1093 nwrap_load_module_fn(b
, "getpwent_r");
1094 *(void **)(&fns
->_nss_endpwent
) =
1095 nwrap_load_module_fn(b
, "endpwent");
1096 *(void **)(&fns
->_nss_initgroups
) =
1097 nwrap_load_module_fn(b
, "initgroups_dyn");
1098 *(void **)(&fns
->_nss_getgrnam_r
) =
1099 nwrap_load_module_fn(b
, "getgrnam_r");
1100 *(void **)(&fns
->_nss_getgrgid_r
)=
1101 nwrap_load_module_fn(b
, "getgrgid_r");
1102 *(void **)(&fns
->_nss_setgrent
) =
1103 nwrap_load_module_fn(b
, "setgrent");
1104 *(void **)(&fns
->_nss_getgrent_r
) =
1105 nwrap_load_module_fn(b
, "getgrent_r");
1106 *(void **)(&fns
->_nss_endgrent
) =
1107 nwrap_load_module_fn(b
, "endgrent");
1112 static void *nwrap_load_module(const char *so_path
)
1116 if (!so_path
|| !strlen(so_path
)) {
1120 h
= dlopen(so_path
, RTLD_LAZY
);
1122 NWRAP_LOG(NWRAP_LOG_ERROR
,
1123 "Cannot open shared library %s",
1131 static bool nwrap_module_init(const char *name
,
1132 struct nwrap_ops
*ops
,
1133 const char *so_path
,
1135 struct nwrap_backend
**backends
)
1137 struct nwrap_backend
*b
;
1139 *backends
= (struct nwrap_backend
*)realloc(*backends
,
1140 sizeof(struct nwrap_backend
) * ((*num_backends
) + 1));
1142 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1146 b
= &((*backends
)[*num_backends
]);
1150 b
->so_path
= so_path
;
1152 if (so_path
!= NULL
) {
1153 b
->so_handle
= nwrap_load_module(so_path
);
1154 b
->fns
= nwrap_load_module_fns(b
);
1155 if (b
->fns
== NULL
) {
1159 b
->so_handle
= NULL
;
1168 static void nwrap_libc_init(struct nwrap_main
*r
)
1170 r
->libc
= malloc(sizeof(struct nwrap_libc
));
1171 if (r
->libc
== NULL
) {
1172 printf("Failed to allocate memory for libc");
1175 ZERO_STRUCTP(r
->libc
);
1177 r
->libc
->fns
= malloc(sizeof(struct nwrap_libc_fns
));
1178 if (r
->libc
->fns
== NULL
) {
1179 printf("Failed to allocate memory for libc functions");
1182 ZERO_STRUCTP(r
->libc
->fns
);
1185 static void nwrap_backend_init(struct nwrap_main
*r
)
1187 const char *module_so_path
= getenv("NSS_WRAPPER_MODULE_SO_PATH");
1188 const char *module_fn_name
= getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1190 r
->num_backends
= 0;
1193 if (!nwrap_module_init("files", &nwrap_files_ops
, NULL
,
1196 NWRAP_LOG(NWRAP_LOG_ERROR
,
1197 "Failed to initialize 'files' backend");
1201 if (module_so_path
!= NULL
&&
1202 module_so_path
[0] != '\0' &&
1203 module_fn_name
!= NULL
&&
1204 module_fn_name
[0] != '\0') {
1205 if (!nwrap_module_init(module_fn_name
,
1210 NWRAP_LOG(NWRAP_LOG_ERROR
,
1211 "Failed to initialize '%s' backend",
1218 static void nwrap_init(void)
1220 static bool initialized
;
1222 if (initialized
) return;
1225 nwrap_main_global
= &__nwrap_main_global
;
1227 nwrap_libc_init(nwrap_main_global
);
1229 nwrap_backend_init(nwrap_main_global
);
1231 nwrap_pw_global
.cache
= &__nwrap_cache_pw
;
1233 nwrap_pw_global
.cache
->path
= getenv("NSS_WRAPPER_PASSWD");
1234 nwrap_pw_global
.cache
->fd
= -1;
1235 nwrap_pw_global
.cache
->private_data
= &nwrap_pw_global
;
1236 nwrap_pw_global
.cache
->parse_line
= nwrap_pw_parse_line
;
1237 nwrap_pw_global
.cache
->unload
= nwrap_pw_unload
;
1239 nwrap_gr_global
.cache
= &__nwrap_cache_gr
;
1241 nwrap_gr_global
.cache
->path
= getenv("NSS_WRAPPER_GROUP");
1242 nwrap_gr_global
.cache
->fd
= -1;
1243 nwrap_gr_global
.cache
->private_data
= &nwrap_gr_global
;
1244 nwrap_gr_global
.cache
->parse_line
= nwrap_gr_parse_line
;
1245 nwrap_gr_global
.cache
->unload
= nwrap_gr_unload
;
1247 nwrap_he_global
.cache
= &__nwrap_cache_he
;
1249 nwrap_he_global
.cache
->path
= getenv("NSS_WRAPPER_HOSTS");
1250 nwrap_he_global
.cache
->fd
= -1;
1251 nwrap_he_global
.cache
->private_data
= &nwrap_he_global
;
1252 nwrap_he_global
.cache
->parse_line
= nwrap_he_parse_line
;
1253 nwrap_he_global
.cache
->unload
= nwrap_he_unload
;
1256 bool nss_wrapper_enabled(void)
1260 if (nwrap_pw_global
.cache
->path
== NULL
||
1261 nwrap_pw_global
.cache
->path
[0] == '\0') {
1264 if (nwrap_gr_global
.cache
->path
== NULL
||
1265 nwrap_gr_global
.cache
->path
[0] == '\0') {
1272 bool nss_wrapper_hosts_enabled(void)
1276 if (nwrap_he_global
.cache
->path
== NULL
||
1277 nwrap_he_global
.cache
->path
[0] == '\0') {
1284 static bool nwrap_hostname_enabled(void)
1288 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL
) {
1295 static bool nwrap_parse_file(struct nwrap_cache
*nwrap
)
1298 uint8_t *buf
= NULL
;
1301 if (nwrap
->st
.st_size
== 0) {
1302 NWRAP_LOG(NWRAP_LOG_DEBUG
, "size == 0");
1306 if (nwrap
->st
.st_size
> INT32_MAX
) {
1307 NWRAP_LOG(NWRAP_LOG_ERROR
,
1308 "Size[%u] larger than INT32_MAX",
1309 (unsigned)nwrap
->st
.st_size
);
1313 ret
= lseek(nwrap
->fd
, 0, SEEK_SET
);
1315 NWRAP_LOG(NWRAP_LOG_ERROR
, "lseek - rc=%d\n", ret
);
1319 buf
= (uint8_t *)malloc(nwrap
->st
.st_size
+ 1);
1321 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1325 ret
= read(nwrap
->fd
, buf
, nwrap
->st
.st_size
);
1326 if (ret
!= nwrap
->st
.st_size
) {
1327 NWRAP_LOG(NWRAP_LOG_ERROR
,
1329 (unsigned)nwrap
->st
.st_size
, ret
);
1333 buf
[nwrap
->st
.st_size
] = '\0';
1335 nline
= (char *)buf
;
1336 while (nline
!= NULL
&& nline
[0] != '\0') {
1344 e
= strchr(line
, '\n');
1355 if (strlen(line
) == 0) {
1359 ok
= nwrap
->parse_line(nwrap
, line
);
1374 static void nwrap_files_cache_unload(struct nwrap_cache
*nwrap
)
1376 nwrap
->unload(nwrap
);
1378 if (nwrap
->buf
) free(nwrap
->buf
);
1383 static void nwrap_files_cache_reload(struct nwrap_cache
*nwrap
)
1388 bool retried
= false;
1391 if (nwrap
->fd
< 0) {
1392 nwrap
->fd
= open(nwrap
->path
, O_RDONLY
);
1393 if (nwrap
->fd
< 0) {
1394 NWRAP_LOG(NWRAP_LOG_ERROR
,
1395 "Unable to open '%s' readonly %d:%s",
1396 nwrap
->path
, nwrap
->fd
,
1400 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Open '%s'", nwrap
->path
);
1403 ret
= fstat(nwrap
->fd
, &st
);
1405 NWRAP_LOG(NWRAP_LOG_ERROR
,
1406 "fstat(%s) - %d:%s",
1413 if (retried
== false && st
.st_nlink
== 0) {
1414 /* maybe someone has replaced the file... */
1415 NWRAP_LOG(NWRAP_LOG_TRACE
,
1416 "st_nlink == 0, reopen %s",
1419 memset(&nwrap
->st
, 0, sizeof(nwrap
->st
));
1425 if (st
.st_mtime
== nwrap
->st
.st_mtime
) {
1426 NWRAP_LOG(NWRAP_LOG_TRACE
,
1427 "st_mtime[%u] hasn't changed, skip reload",
1428 (unsigned)st
.st_mtime
);
1432 NWRAP_LOG(NWRAP_LOG_TRACE
,
1433 "st_mtime has changed [%u] => [%u], start reload",
1434 (unsigned)st
.st_mtime
,
1435 (unsigned)nwrap
->st
.st_mtime
);
1439 nwrap_files_cache_unload(nwrap
);
1441 ok
= nwrap_parse_file(nwrap
);
1443 NWRAP_LOG(NWRAP_LOG_ERROR
, "Failed to reload %s", nwrap
->path
);
1444 nwrap_files_cache_unload(nwrap
);
1447 NWRAP_LOG(NWRAP_LOG_TRACE
, "Reloaded %s", nwrap
->path
);
1451 * the caller has to call nwrap_unload() on failure
1453 static bool nwrap_pw_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1455 struct nwrap_pw
*nwrap_pw
;
1462 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
1464 list_size
= sizeof(*nwrap_pw
->list
) * (nwrap_pw
->num
+1);
1465 pw
= (struct passwd
*)realloc(nwrap_pw
->list
, list_size
);
1467 NWRAP_LOG(NWRAP_LOG_ERROR
,
1468 "realloc(%u) failed",
1469 (unsigned)list_size
);
1472 nwrap_pw
->list
= pw
;
1474 pw
= &nwrap_pw
->list
[nwrap_pw
->num
];
1481 NWRAP_LOG(NWRAP_LOG_ERROR
,
1482 "Invalid line[%s]: '%s'",
1492 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]\n", pw
->pw_name
);
1497 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1505 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]\n", pw
->pw_passwd
);
1510 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1516 pw
->pw_uid
= (uid_t
)strtoul(c
, &e
, 10);
1518 NWRAP_LOG(NWRAP_LOG_ERROR
,
1519 "Invalid line[%s]: '%s' - %s",
1520 line
, c
, strerror(errno
));
1524 NWRAP_LOG(NWRAP_LOG_ERROR
,
1525 "Invalid line[%s]: '%s' - %s",
1526 line
, c
, strerror(errno
));
1530 NWRAP_LOG(NWRAP_LOG_ERROR
,
1531 "Invalid line[%s]: '%s' - %s",
1532 line
, c
, strerror(errno
));
1537 NWRAP_LOG(NWRAP_LOG_TRACE
, "uid[%u]", pw
->pw_uid
);
1542 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1548 pw
->pw_gid
= (gid_t
)strtoul(c
, &e
, 10);
1550 NWRAP_LOG(NWRAP_LOG_ERROR
,
1551 "Invalid line[%s]: '%s' - %s",
1552 line
, c
, strerror(errno
));
1556 NWRAP_LOG(NWRAP_LOG_ERROR
,
1557 "Invalid line[%s]: '%s' - %s",
1558 line
, c
, strerror(errno
));
1562 NWRAP_LOG(NWRAP_LOG_ERROR
,
1563 "Invalid line[%s]: '%s' - %s",
1564 line
, c
, strerror(errno
));
1569 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]\n", pw
->pw_gid
);
1574 NWRAP_LOG(NWRAP_LOG_ERROR
, "invalid line[%s]: '%s'", line
, c
);
1582 NWRAP_LOG(NWRAP_LOG_TRACE
, "gecos[%s]", pw
->pw_gecos
);
1587 NWRAP_LOG(NWRAP_LOG_ERROR
, "'%s'", c
);
1595 NWRAP_LOG(NWRAP_LOG_TRACE
, "dir[%s]", pw
->pw_dir
);
1599 NWRAP_LOG(NWRAP_LOG_TRACE
, "shell[%s]", pw
->pw_shell
);
1601 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1602 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1603 pw
->pw_name
, pw
->pw_passwd
,
1604 pw
->pw_uid
, pw
->pw_gid
,
1605 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
1611 static void nwrap_pw_unload(struct nwrap_cache
*nwrap
)
1613 struct nwrap_pw
*nwrap_pw
;
1614 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
1616 if (nwrap_pw
->list
) free(nwrap_pw
->list
);
1618 nwrap_pw
->list
= NULL
;
1623 static int nwrap_pw_copy_r(const struct passwd
*src
, struct passwd
*dst
,
1624 char *buf
, size_t buflen
, struct passwd
**dstp
)
1630 first
= src
->pw_name
;
1632 last
= src
->pw_shell
;
1633 while (*last
) last
++;
1635 ofs
= PTR_DIFF(last
+ 1, first
);
1637 if (ofs
> (off_t
) buflen
) {
1641 memcpy(buf
, first
, ofs
);
1643 ofs
= PTR_DIFF(src
->pw_name
, first
);
1644 dst
->pw_name
= buf
+ ofs
;
1645 ofs
= PTR_DIFF(src
->pw_passwd
, first
);
1646 dst
->pw_passwd
= buf
+ ofs
;
1647 dst
->pw_uid
= src
->pw_uid
;
1648 dst
->pw_gid
= src
->pw_gid
;
1649 ofs
= PTR_DIFF(src
->pw_gecos
, first
);
1650 dst
->pw_gecos
= buf
+ ofs
;
1651 ofs
= PTR_DIFF(src
->pw_dir
, first
);
1652 dst
->pw_dir
= buf
+ ofs
;
1653 ofs
= PTR_DIFF(src
->pw_shell
, first
);
1654 dst
->pw_shell
= buf
+ ofs
;
1664 * the caller has to call nwrap_unload() on failure
1666 static bool nwrap_gr_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1668 struct nwrap_gr
*nwrap_gr
;
1676 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
1678 list_size
= sizeof(*nwrap_gr
->list
) * (nwrap_gr
->num
+1);
1679 gr
= (struct group
*)realloc(nwrap_gr
->list
, list_size
);
1681 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc failed");
1684 nwrap_gr
->list
= gr
;
1686 gr
= &nwrap_gr
->list
[nwrap_gr
->num
];
1693 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1701 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]", gr
->gr_name
);
1706 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1714 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]", gr
->gr_passwd
);
1719 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1725 gr
->gr_gid
= (gid_t
)strtoul(c
, &e
, 10);
1727 NWRAP_LOG(NWRAP_LOG_ERROR
,
1728 "Invalid line[%s]: '%s' - %s",
1729 line
, c
, strerror(errno
));
1733 NWRAP_LOG(NWRAP_LOG_ERROR
,
1734 "Invalid line[%s]: '%s' - %s",
1735 line
, c
, strerror(errno
));
1739 NWRAP_LOG(NWRAP_LOG_ERROR
,
1740 "Invalid line[%s]: '%s' - %s",
1741 line
, c
, strerror(errno
));
1746 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]", gr
->gr_gid
);
1749 gr
->gr_mem
= (char **)malloc(sizeof(char *));
1751 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1754 gr
->gr_mem
[0] = NULL
;
1756 for(nummem
=0; p
; nummem
++) {
1766 if (strlen(c
) == 0) {
1770 m_size
= sizeof(char *) * (nummem
+2);
1771 m
= (char **)realloc(gr
->gr_mem
, m_size
);
1773 NWRAP_LOG(NWRAP_LOG_ERROR
,
1774 "realloc(%zd) failed",
1779 gr
->gr_mem
[nummem
] = c
;
1780 gr
->gr_mem
[nummem
+1] = NULL
;
1782 NWRAP_LOG(NWRAP_LOG_TRACE
,
1784 nummem
, gr
->gr_mem
[nummem
]);
1787 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1788 "Added group[%s:%s:%u:] with %u members",
1789 gr
->gr_name
, gr
->gr_passwd
, gr
->gr_gid
, nummem
);
1795 static void nwrap_gr_unload(struct nwrap_cache
*nwrap
)
1798 struct nwrap_gr
*nwrap_gr
;
1799 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
1801 if (nwrap_gr
->list
) {
1802 for (i
=0; i
< nwrap_gr
->num
; i
++) {
1803 if (nwrap_gr
->list
[i
].gr_mem
) {
1804 free(nwrap_gr
->list
[i
].gr_mem
);
1807 free(nwrap_gr
->list
);
1810 nwrap_gr
->list
= NULL
;
1815 static int nwrap_gr_copy_r(const struct group
*src
, struct group
*dst
,
1816 char *buf
, size_t buflen
, struct group
**dstp
)
1826 first
= src
->gr_name
;
1828 lastm
= src
->gr_mem
;
1835 last
= src
->gr_passwd
;
1837 while (*last
) last
++;
1839 ofsb
= PTR_DIFF(last
+ 1, first
);
1840 ofsm
= PTR_DIFF(lastm
+ 1, src
->gr_mem
);
1842 if ((ofsb
+ ofsm
) > (off_t
) buflen
) {
1846 memcpy(buf
, first
, ofsb
);
1847 memcpy(buf
+ ofsb
, src
->gr_mem
, ofsm
);
1849 ofs
= PTR_DIFF(src
->gr_name
, first
);
1850 dst
->gr_name
= buf
+ ofs
;
1851 ofs
= PTR_DIFF(src
->gr_passwd
, first
);
1852 dst
->gr_passwd
= buf
+ ofs
;
1853 dst
->gr_gid
= src
->gr_gid
;
1855 dst
->gr_mem
= (char **)(buf
+ ofsb
);
1856 for (i
=0; src
->gr_mem
[i
]; i
++) {
1857 ofs
= PTR_DIFF(src
->gr_mem
[i
], first
);
1858 dst
->gr_mem
[i
] = buf
+ ofs
;
1868 static bool nwrap_he_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1870 struct nwrap_he
*nwrap_he
= (struct nwrap_he
*)nwrap
->private_data
;
1871 struct nwrap_entdata
*ed
;
1873 bool do_aliases
= true;
1874 int aliases_count
= 0;
1879 list_size
= sizeof(struct nwrap_entdata
) * (nwrap_he
->num
+ 1);
1881 ed
= (struct nwrap_entdata
*)realloc(nwrap_he
->list
, list_size
);
1883 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc[%zd] failed", list_size
);
1886 nwrap_he
->list
= ed
;
1888 /* set it to the last element */
1889 ed
= &(nwrap_he
->list
[nwrap_he
->num
]);
1891 ed
->addr
= malloc(sizeof(struct nwrap_addrdata
));
1892 if (ed
->addr
== NULL
) {
1893 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc[%zd] failed", list_size
);
1903 /* Walk to first char */
1904 for (p
= i
; *p
!= '.' && *p
!= ':' && !isxdigit((int) *p
); p
++) {
1906 NWRAP_LOG(NWRAP_LOG_ERROR
,
1907 "Invalid line[%s]: '%s'",
1913 for (i
= p
; !isspace((int)*p
); p
++) {
1915 NWRAP_LOG(NWRAP_LOG_ERROR
,
1916 "Invalid line[%s]: '%s'",
1924 if (inet_pton(AF_INET
, i
, ed
->addr
->host_addr
)) {
1925 ed
->ht
.h_addrtype
= AF_INET
;
1926 ed
->ht
.h_length
= 4;
1928 } else if (inet_pton(AF_INET6
, i
, ed
->addr
->host_addr
)) {
1929 ed
->ht
.h_addrtype
= AF_INET6
;
1930 ed
->ht
.h_length
= 16;
1933 NWRAP_LOG(NWRAP_LOG_ERROR
,
1934 "Invalid line[%s]: '%s'",
1940 ed
->addr
->h_addr_ptrs
[0] = (char *)ed
->addr
->host_addr
;
1941 ed
->addr
->h_addr_ptrs
[1] = NULL
;
1943 ed
->ht
.h_addr_list
= ed
->addr
->h_addr_ptrs
;
1951 /* Walk to first char */
1952 for (n
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
1954 NWRAP_LOG(NWRAP_LOG_ERROR
,
1955 "Invalid line[%s]: '%s'",
1962 for (n
= p
; !isspace((int)*p
); p
++) {
1973 /* glib's getent always dereferences he->h_aliases */
1974 ed
->ht
.h_aliases
= malloc(sizeof(char *));
1975 if (ed
->ht
.h_aliases
== NULL
) {
1978 ed
->ht
.h_aliases
[0] = NULL
;
1983 while (do_aliases
) {
1989 /* Walk to first char */
1990 for (a
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
1996 /* Only trailing spaces are left */
2001 for (a
= p
; !isspace((int)*p
); p
++) {
2010 aliases
= realloc(ed
->ht
.h_aliases
, sizeof(char *) * (aliases_count
+ 2));
2011 if (aliases
== NULL
) {
2014 ed
->ht
.h_aliases
= aliases
;
2016 aliases
[aliases_count
] = a
;
2017 aliases
[aliases_count
+ 1] = NULL
;
2026 static void nwrap_he_unload(struct nwrap_cache
*nwrap
)
2028 struct nwrap_he
*nwrap_he
=
2029 (struct nwrap_he
*)nwrap
->private_data
;
2032 if (nwrap_he
->list
!= NULL
) {
2033 for (i
= 0; i
< nwrap_he
->num
; i
++) {
2034 if (nwrap_he
->list
[i
].ht
.h_aliases
!= NULL
) {
2035 free(nwrap_he
->list
[i
].ht
.h_aliases
);
2037 if (nwrap_he
->list
[i
].addr
!= NULL
) {
2038 free(nwrap_he
->list
[i
].addr
);
2041 free(nwrap_he
->list
);
2044 nwrap_he
->list
= NULL
;
2050 /* user functions */
2051 static struct passwd
*nwrap_files_getpwnam(struct nwrap_backend
*b
,
2056 (void) b
; /* unused */
2058 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Lookup user %s in files", name
);
2060 nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2062 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
2063 if (strcmp(nwrap_pw_global
.list
[i
].pw_name
, name
) == 0) {
2064 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] found", name
);
2065 return &nwrap_pw_global
.list
[i
];
2067 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2068 "user[%s] does not match [%s]",
2070 nwrap_pw_global
.list
[i
].pw_name
);
2073 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] not found\n", name
);
2079 static int nwrap_files_getpwnam_r(struct nwrap_backend
*b
,
2080 const char *name
, struct passwd
*pwdst
,
2081 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2085 pw
= nwrap_files_getpwnam(b
, name
);
2093 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
2096 static struct passwd
*nwrap_files_getpwuid(struct nwrap_backend
*b
,
2101 (void) b
; /* unused */
2103 nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2105 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
2106 if (nwrap_pw_global
.list
[i
].pw_uid
== uid
) {
2107 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] found", uid
);
2108 return &nwrap_pw_global
.list
[i
];
2110 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2111 "uid[%u] does not match [%u]",
2113 nwrap_pw_global
.list
[i
].pw_uid
);
2116 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] not found\n", uid
);
2122 static int nwrap_files_getpwuid_r(struct nwrap_backend
*b
,
2123 uid_t uid
, struct passwd
*pwdst
,
2124 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2128 pw
= nwrap_files_getpwuid(b
, uid
);
2136 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
2139 /* user enum functions */
2140 static void nwrap_files_setpwent(struct nwrap_backend
*b
)
2142 (void) b
; /* unused */
2144 nwrap_pw_global
.idx
= 0;
2147 static struct passwd
*nwrap_files_getpwent(struct nwrap_backend
*b
)
2151 (void) b
; /* unused */
2153 if (nwrap_pw_global
.idx
== 0) {
2154 nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2157 if (nwrap_pw_global
.idx
>= nwrap_pw_global
.num
) {
2162 pw
= &nwrap_pw_global
.list
[nwrap_pw_global
.idx
++];
2164 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2165 "return user[%s] uid[%u]",
2166 pw
->pw_name
, pw
->pw_uid
);
2171 static int nwrap_files_getpwent_r(struct nwrap_backend
*b
,
2172 struct passwd
*pwdst
, char *buf
,
2173 size_t buflen
, struct passwd
**pwdstp
)
2177 pw
= nwrap_files_getpwent(b
);
2185 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
2188 static void nwrap_files_endpwent(struct nwrap_backend
*b
)
2190 (void) b
; /* unused */
2192 nwrap_pw_global
.idx
= 0;
2195 /* misc functions */
2196 static int nwrap_files_initgroups(struct nwrap_backend
*b
,
2197 const char *user
, gid_t group
)
2199 (void) b
; /* unused */
2200 (void) user
; /* unused */
2201 (void) group
; /* used */
2203 /* TODO: maybe we should also fake this... */
2207 /* group functions */
2208 static struct group
*nwrap_files_getgrnam(struct nwrap_backend
*b
,
2213 (void) b
; /* unused */
2215 nwrap_files_cache_reload(nwrap_gr_global
.cache
);
2217 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
2218 if (strcmp(nwrap_gr_global
.list
[i
].gr_name
, name
) == 0) {
2219 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] found", name
);
2220 return &nwrap_gr_global
.list
[i
];
2222 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2223 "group[%s] does not match [%s]",
2225 nwrap_gr_global
.list
[i
].gr_name
);
2228 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] not found", name
);
2234 static int nwrap_files_getgrnam_r(struct nwrap_backend
*b
,
2235 const char *name
, struct group
*grdst
,
2236 char *buf
, size_t buflen
, struct group
**grdstp
)
2240 gr
= nwrap_files_getgrnam(b
, name
);
2248 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
2251 static struct group
*nwrap_files_getgrgid(struct nwrap_backend
*b
,
2256 (void) b
; /* unused */
2258 nwrap_files_cache_reload(nwrap_gr_global
.cache
);
2260 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
2261 if (nwrap_gr_global
.list
[i
].gr_gid
== gid
) {
2262 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] found", gid
);
2263 return &nwrap_gr_global
.list
[i
];
2265 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2266 "gid[%u] does not match [%u]",
2268 nwrap_gr_global
.list
[i
].gr_gid
);
2271 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] not found", gid
);
2277 static int nwrap_files_getgrgid_r(struct nwrap_backend
*b
,
2278 gid_t gid
, struct group
*grdst
,
2279 char *buf
, size_t buflen
, struct group
**grdstp
)
2283 gr
= nwrap_files_getgrgid(b
, gid
);
2291 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
2294 /* group enum functions */
2295 static void nwrap_files_setgrent(struct nwrap_backend
*b
)
2297 (void) b
; /* unused */
2299 nwrap_gr_global
.idx
= 0;
2302 static struct group
*nwrap_files_getgrent(struct nwrap_backend
*b
)
2306 (void) b
; /* unused */
2308 if (nwrap_gr_global
.idx
== 0) {
2309 nwrap_files_cache_reload(nwrap_gr_global
.cache
);
2312 if (nwrap_gr_global
.idx
>= nwrap_gr_global
.num
) {
2317 gr
= &nwrap_gr_global
.list
[nwrap_gr_global
.idx
++];
2319 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2320 "return group[%s] gid[%u]",
2321 gr
->gr_name
, gr
->gr_gid
);
2326 static int nwrap_files_getgrent_r(struct nwrap_backend
*b
,
2327 struct group
*grdst
, char *buf
,
2328 size_t buflen
, struct group
**grdstp
)
2332 gr
= nwrap_files_getgrent(b
);
2340 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
2343 static void nwrap_files_endgrent(struct nwrap_backend
*b
)
2345 (void) b
; /* unused */
2347 nwrap_gr_global
.idx
= 0;
2350 /* hosts functions */
2351 static struct hostent
*nwrap_files_gethostbyname(const char *name
, int af
)
2356 nwrap_files_cache_reload(nwrap_he_global
.cache
);
2358 for (i
= 0; i
< nwrap_he_global
.num
; i
++) {
2361 he
= &nwrap_he_global
.list
[i
].ht
;
2363 /* Filter by address familiy if provided */
2364 if (af
!= AF_UNSPEC
&& he
->h_addrtype
!= af
) {
2368 if (strcasecmp(he
->h_name
, name
) == 0) {
2369 NWRAP_LOG(NWRAP_LOG_DEBUG
, "name[%s] found", name
);
2373 if (he
->h_aliases
== NULL
) {
2377 for (j
= 0; he
->h_aliases
[j
] != NULL
; j
++) {
2378 if (strcasecmp(he
->h_aliases
[j
], name
) == 0) {
2379 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2391 #ifdef HAVE_GETHOSTBYNAME_R
2392 static int nwrap_gethostbyname_r(const char *name
,
2393 struct hostent
*ret
,
2394 char *buf
, size_t buflen
,
2395 struct hostent
**result
, int *h_errnop
)
2397 *result
= nwrap_files_gethostbyname(name
, AF_UNSPEC
);
2398 if (*result
!= NULL
) {
2399 memset(buf
, '\0', buflen
);
2403 *h_errnop
= h_errno
;
2408 int gethostbyname_r(const char *name
,
2409 struct hostent
*ret
,
2410 char *buf
, size_t buflen
,
2411 struct hostent
**result
, int *h_errnop
)
2413 if (!nss_wrapper_hosts_enabled()) {
2414 return libc_gethostbyname_r(name
,
2422 return nwrap_gethostbyname_r(name
, ret
, buf
, buflen
, result
, h_errnop
);
2426 static struct hostent
*nwrap_files_gethostbyaddr(const void *addr
,
2427 socklen_t len
, int type
)
2430 char ip
[INET6_ADDRSTRLEN
] = {0};
2434 (void) len
; /* unused */
2436 nwrap_files_cache_reload(nwrap_he_global
.cache
);
2438 a
= inet_ntop(type
, addr
, ip
, sizeof(ip
));
2444 for (i
= 0; i
< nwrap_he_global
.num
; i
++) {
2445 he
= &nwrap_he_global
.list
[i
].ht
;
2447 if (he
->h_addrtype
!= type
) {
2451 if (memcmp(addr
, he
->h_addr_list
[0], he
->h_length
) == 0) {
2460 #ifdef HAVE_GETHOSTBYADDR_R
2461 static int nwrap_gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
2462 struct hostent
*ret
,
2463 char *buf
, size_t buflen
,
2464 struct hostent
**result
, int *h_errnop
)
2466 *result
= nwrap_files_gethostbyaddr(addr
, len
, type
);
2467 if (*result
!= NULL
) {
2468 memset(buf
, '\0', buflen
);
2472 *h_errnop
= h_errno
;
2477 int gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
2478 struct hostent
*ret
,
2479 char *buf
, size_t buflen
,
2480 struct hostent
**result
, int *h_errnop
)
2482 if (!nss_wrapper_hosts_enabled()) {
2483 return libc_gethostbyaddr_r(addr
,
2493 return nwrap_gethostbyaddr_r(addr
, len
, type
, ret
, buf
, buflen
, result
, h_errnop
);
2497 /* hosts enum functions */
2498 static void nwrap_files_sethostent(void)
2500 nwrap_he_global
.idx
= 0;
2503 static struct hostent
*nwrap_files_gethostent(void)
2507 if (nwrap_he_global
.idx
== 0) {
2508 nwrap_files_cache_reload(nwrap_he_global
.cache
);
2511 if (nwrap_he_global
.idx
>= nwrap_he_global
.num
) {
2516 he
= &nwrap_he_global
.list
[nwrap_he_global
.idx
++].ht
;
2518 NWRAP_LOG(NWRAP_LOG_DEBUG
, "return hosts[%s]", he
->h_name
);
2523 static void nwrap_files_endhostent(void)
2525 nwrap_he_global
.idx
= 0;
2533 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
2536 static struct passwd
*nwrap_module_getpwnam(struct nwrap_backend
*b
,
2539 static struct passwd pwd
;
2540 static char buf
[1000];
2543 if (!b
->fns
->_nss_getpwnam_r
) {
2547 status
= b
->fns
->_nss_getpwnam_r(name
, &pwd
, buf
, sizeof(buf
), &errno
);
2548 if (status
== NSS_STATUS_NOTFOUND
) {
2551 if (status
!= NSS_STATUS_SUCCESS
) {
2558 static int nwrap_module_getpwnam_r(struct nwrap_backend
*b
,
2559 const char *name
, struct passwd
*pwdst
,
2560 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2564 (void) b
; /* unused */
2565 (void) pwdst
; /* unused */
2566 (void) pwdstp
; /* unused */
2568 if (!b
->fns
->_nss_getpwnam_r
) {
2569 return NSS_STATUS_NOTFOUND
;
2572 ret
= b
->fns
->_nss_getpwnam_r(name
, pwdst
, buf
, buflen
, &errno
);
2574 case NSS_STATUS_SUCCESS
:
2576 case NSS_STATUS_NOTFOUND
:
2581 case NSS_STATUS_TRYAGAIN
:
2594 static struct passwd
*nwrap_module_getpwuid(struct nwrap_backend
*b
,
2597 static struct passwd pwd
;
2598 static char buf
[1000];
2601 if (!b
->fns
->_nss_getpwuid_r
) {
2605 status
= b
->fns
->_nss_getpwuid_r(uid
, &pwd
, buf
, sizeof(buf
), &errno
);
2606 if (status
== NSS_STATUS_NOTFOUND
) {
2609 if (status
!= NSS_STATUS_SUCCESS
) {
2615 static int nwrap_module_getpwuid_r(struct nwrap_backend
*b
,
2616 uid_t uid
, struct passwd
*pwdst
,
2617 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
2621 (void) pwdstp
; /* unused */
2623 if (!b
->fns
->_nss_getpwuid_r
) {
2627 ret
= b
->fns
->_nss_getpwuid_r(uid
, pwdst
, buf
, buflen
, &errno
);
2629 case NSS_STATUS_SUCCESS
:
2631 case NSS_STATUS_NOTFOUND
:
2636 case NSS_STATUS_TRYAGAIN
:
2649 static void nwrap_module_setpwent(struct nwrap_backend
*b
)
2651 if (!b
->fns
->_nss_setpwent
) {
2655 b
->fns
->_nss_setpwent();
2658 static struct passwd
*nwrap_module_getpwent(struct nwrap_backend
*b
)
2660 static struct passwd pwd
;
2661 static char buf
[1000];
2664 if (!b
->fns
->_nss_getpwent_r
) {
2668 status
= b
->fns
->_nss_getpwent_r(&pwd
, buf
, sizeof(buf
), &errno
);
2669 if (status
== NSS_STATUS_NOTFOUND
) {
2672 if (status
!= NSS_STATUS_SUCCESS
) {
2678 static int nwrap_module_getpwent_r(struct nwrap_backend
*b
,
2679 struct passwd
*pwdst
, char *buf
,
2680 size_t buflen
, struct passwd
**pwdstp
)
2684 (void) pwdstp
; /* unused */
2686 if (!b
->fns
->_nss_getpwent_r
) {
2690 ret
= b
->fns
->_nss_getpwent_r(pwdst
, buf
, buflen
, &errno
);
2692 case NSS_STATUS_SUCCESS
:
2694 case NSS_STATUS_NOTFOUND
:
2699 case NSS_STATUS_TRYAGAIN
:
2712 static void nwrap_module_endpwent(struct nwrap_backend
*b
)
2714 if (!b
->fns
->_nss_endpwent
) {
2718 b
->fns
->_nss_endpwent();
2721 static int nwrap_module_initgroups(struct nwrap_backend
*b
,
2722 const char *user
, gid_t group
)
2728 if (!b
->fns
->_nss_initgroups
) {
2729 return NSS_STATUS_UNAVAIL
;
2732 return b
->fns
->_nss_initgroups(user
, group
, &start
, &size
, &groups
, 0, &errno
);
2735 static struct group
*nwrap_module_getgrnam(struct nwrap_backend
*b
,
2738 static struct group grp
;
2740 static int buflen
= 1000;
2743 if (!b
->fns
->_nss_getgrnam_r
) {
2748 buf
= (char *)malloc(buflen
);
2751 status
= b
->fns
->_nss_getgrnam_r(name
, &grp
, buf
, buflen
, &errno
);
2752 if (status
== NSS_STATUS_TRYAGAIN
) {
2754 buf
= (char *)realloc(buf
, buflen
);
2760 if (status
== NSS_STATUS_NOTFOUND
) {
2764 if (status
!= NSS_STATUS_SUCCESS
) {
2771 static int nwrap_module_getgrnam_r(struct nwrap_backend
*b
,
2772 const char *name
, struct group
*grdst
,
2773 char *buf
, size_t buflen
, struct group
**grdstp
)
2777 (void) grdstp
; /* unused */
2779 if (!b
->fns
->_nss_getgrnam_r
) {
2783 ret
= b
->fns
->_nss_getgrnam_r(name
, grdst
, buf
, buflen
, &errno
);
2785 case NSS_STATUS_SUCCESS
:
2787 case NSS_STATUS_NOTFOUND
:
2792 case NSS_STATUS_TRYAGAIN
:
2805 static struct group
*nwrap_module_getgrgid(struct nwrap_backend
*b
,
2808 static struct group grp
;
2810 static int buflen
= 1000;
2813 if (!b
->fns
->_nss_getgrgid_r
) {
2818 buf
= (char *)malloc(buflen
);
2822 status
= b
->fns
->_nss_getgrgid_r(gid
, &grp
, buf
, buflen
, &errno
);
2823 if (status
== NSS_STATUS_TRYAGAIN
) {
2825 buf
= (char *)realloc(buf
, buflen
);
2831 if (status
== NSS_STATUS_NOTFOUND
) {
2835 if (status
!= NSS_STATUS_SUCCESS
) {
2842 static int nwrap_module_getgrgid_r(struct nwrap_backend
*b
,
2843 gid_t gid
, struct group
*grdst
,
2844 char *buf
, size_t buflen
, struct group
**grdstp
)
2848 (void) grdstp
; /* unused */
2850 if (!b
->fns
->_nss_getgrgid_r
) {
2854 ret
= b
->fns
->_nss_getgrgid_r(gid
, grdst
, buf
, buflen
, &errno
);
2856 case NSS_STATUS_SUCCESS
:
2858 case NSS_STATUS_NOTFOUND
:
2863 case NSS_STATUS_TRYAGAIN
:
2876 static void nwrap_module_setgrent(struct nwrap_backend
*b
)
2878 if (!b
->fns
->_nss_setgrent
) {
2882 b
->fns
->_nss_setgrent();
2885 static struct group
*nwrap_module_getgrent(struct nwrap_backend
*b
)
2887 static struct group grp
;
2889 static int buflen
= 1024;
2892 if (!b
->fns
->_nss_getgrent_r
) {
2897 buf
= (char *)malloc(buflen
);
2901 status
= b
->fns
->_nss_getgrent_r(&grp
, buf
, buflen
, &errno
);
2902 if (status
== NSS_STATUS_TRYAGAIN
) {
2904 buf
= (char *)realloc(buf
, buflen
);
2910 if (status
== NSS_STATUS_NOTFOUND
) {
2914 if (status
!= NSS_STATUS_SUCCESS
) {
2921 static int nwrap_module_getgrent_r(struct nwrap_backend
*b
,
2922 struct group
*grdst
, char *buf
,
2923 size_t buflen
, struct group
**grdstp
)
2927 (void) grdstp
; /* unused */
2929 if (!b
->fns
->_nss_getgrent_r
) {
2933 ret
= b
->fns
->_nss_getgrent_r(grdst
, buf
, buflen
, &errno
);
2935 case NSS_STATUS_SUCCESS
:
2937 case NSS_STATUS_NOTFOUND
:
2942 case NSS_STATUS_TRYAGAIN
:
2955 static void nwrap_module_endgrent(struct nwrap_backend
*b
)
2957 if (!b
->fns
->_nss_endgrent
) {
2961 b
->fns
->_nss_endgrent();
2964 /****************************************************************************
2966 ***************************************************************************/
2968 static struct passwd
*nwrap_getpwnam(const char *name
)
2973 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
2974 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
2975 pwd
= b
->ops
->nw_getpwnam(b
, name
);
2984 struct passwd
*getpwnam(const char *name
)
2986 if (!nss_wrapper_enabled()) {
2987 return libc_getpwnam(name
);
2990 return nwrap_getpwnam(name
);
2993 /****************************************************************************
2995 ***************************************************************************/
2997 static int nwrap_getpwnam_r(const char *name
, struct passwd
*pwdst
,
2998 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3002 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3003 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3004 ret
= b
->ops
->nw_getpwnam_r(b
, name
, pwdst
, buf
, buflen
, pwdstp
);
3005 if (ret
== ENOENT
) {
3014 #ifdef HAVE_GETPWNAM_R
3015 # ifdef HAVE_SOLARIS_GETPWNAM_R
3016 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
3017 char *buf
, int buflen
, struct passwd
**pwdstp
)
3018 # else /* HAVE_SOLARIS_GETPWNAM_R */
3019 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
3020 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3021 # endif /* HAVE_SOLARIS_GETPWNAM_R */
3023 if (!nss_wrapper_enabled()) {
3024 return libc_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
3027 return nwrap_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
3031 /****************************************************************************
3033 ***************************************************************************/
3035 static struct passwd
*nwrap_getpwuid(uid_t uid
)
3040 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3041 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3042 pwd
= b
->ops
->nw_getpwuid(b
, uid
);
3051 struct passwd
*getpwuid(uid_t uid
)
3053 if (!nss_wrapper_enabled()) {
3054 return libc_getpwuid(uid
);
3057 return nwrap_getpwuid(uid
);
3060 /****************************************************************************
3062 ***************************************************************************/
3064 static int nwrap_getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
3065 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3069 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3070 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3071 ret
= b
->ops
->nw_getpwuid_r(b
, uid
, pwdst
, buf
, buflen
, pwdstp
);
3072 if (ret
== ENOENT
) {
3081 #ifdef HAVE_SOLARIS_GETPWUID_R
3082 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
3083 char *buf
, int buflen
, struct passwd
**pwdstp
)
3085 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
3086 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3089 if (!nss_wrapper_enabled()) {
3090 return libc_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
3093 return nwrap_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
3096 /****************************************************************************
3098 ***************************************************************************/
3100 static void nwrap_setpwent(void)
3104 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3105 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3106 b
->ops
->nw_setpwent(b
);
3112 if (!nss_wrapper_enabled()) {
3120 /****************************************************************************
3122 ***************************************************************************/
3124 static struct passwd
*nwrap_getpwent(void)
3129 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3130 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3131 pwd
= b
->ops
->nw_getpwent(b
);
3140 struct passwd
*getpwent(void)
3142 if (!nss_wrapper_enabled()) {
3143 return libc_getpwent();
3146 return nwrap_getpwent();
3149 /****************************************************************************
3151 ***************************************************************************/
3153 static int nwrap_getpwent_r(struct passwd
*pwdst
, char *buf
,
3154 size_t buflen
, struct passwd
**pwdstp
)
3158 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3159 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3160 ret
= b
->ops
->nw_getpwent_r(b
, pwdst
, buf
, buflen
, pwdstp
);
3161 if (ret
== ENOENT
) {
3170 #ifdef HAVE_SOLARIS_GETPWENT_R
3171 struct passwd
*getpwent_r(struct passwd
*pwdst
, char *buf
, int buflen
)
3173 struct passwd
*pwdstp
= NULL
;
3176 if (!nss_wrapper_enabled()) {
3177 return libc_getpwent_r(pwdst
, buf
, buflen
);
3179 rc
= nwrap_getpwent_r(pwdst
, buf
, buflen
, &pwdstp
);
3186 #else /* HAVE_SOLARIS_GETPWENT_R */
3187 int getpwent_r(struct passwd
*pwdst
, char *buf
,
3188 size_t buflen
, struct passwd
**pwdstp
)
3190 if (!nss_wrapper_enabled()) {
3191 return libc_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
3194 return nwrap_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
3196 #endif /* HAVE_SOLARIS_GETPWENT_R */
3198 /****************************************************************************
3200 ***************************************************************************/
3202 static void nwrap_endpwent(void)
3206 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3207 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3208 b
->ops
->nw_endpwent(b
);
3214 if (!nss_wrapper_enabled()) {
3222 /****************************************************************************
3224 ***************************************************************************/
3226 static int nwrap_initgroups(const char *user
, gid_t group
)
3230 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3231 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3234 rc
= b
->ops
->nw_initgroups(b
, user
, group
);
3244 int initgroups(const char *user
, gid_t group
)
3246 if (!nss_wrapper_enabled()) {
3247 return libc_initgroups(user
, group
);
3250 return nwrap_initgroups(user
, group
);
3253 /****************************************************************************
3255 ***************************************************************************/
3257 static struct group
*nwrap_getgrnam(const char *name
)
3262 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3263 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3264 grp
= b
->ops
->nw_getgrnam(b
, name
);
3273 struct group
*getgrnam(const char *name
)
3275 if (!nss_wrapper_enabled()) {
3276 return libc_getgrnam(name
);
3279 return nwrap_getgrnam(name
);
3282 /****************************************************************************
3284 ***************************************************************************/
3286 static int nwrap_getgrnam_r(const char *name
, struct group
*grdst
,
3287 char *buf
, size_t buflen
, struct group
**grdstp
)
3291 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3292 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3293 ret
= b
->ops
->nw_getgrnam_r(b
, name
, grdst
, buf
, buflen
, grdstp
);
3294 if (ret
== ENOENT
) {
3303 #ifdef HAVE_GETGRNAM_R
3304 # ifdef HAVE_SOLARIS_GETGRNAM_R
3305 int getgrnam_r(const char *name
, struct group
*grp
,
3306 char *buf
, int buflen
, struct group
**pgrp
)
3307 # else /* HAVE_SOLARIS_GETGRNAM_R */
3308 int getgrnam_r(const char *name
, struct group
*grp
,
3309 char *buf
, size_t buflen
, struct group
**pgrp
)
3310 # endif /* HAVE_SOLARIS_GETGRNAM_R */
3312 if (!nss_wrapper_enabled()) {
3313 return libc_getgrnam_r(name
,
3320 return nwrap_getgrnam_r(name
, grp
, buf
, buflen
, pgrp
);
3322 #endif /* HAVE_GETGRNAM_R */
3324 /****************************************************************************
3326 ***************************************************************************/
3328 static struct group
*nwrap_getgrgid(gid_t gid
)
3333 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3334 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3335 grp
= b
->ops
->nw_getgrgid(b
, gid
);
3344 struct group
*getgrgid(gid_t gid
)
3346 if (!nss_wrapper_enabled()) {
3347 return libc_getgrgid(gid
);
3350 return nwrap_getgrgid(gid
);
3353 /****************************************************************************
3355 ***************************************************************************/
3357 static int nwrap_getgrgid_r(gid_t gid
, struct group
*grdst
,
3358 char *buf
, size_t buflen
, struct group
**grdstp
)
3362 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3363 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3364 ret
= b
->ops
->nw_getgrgid_r(b
, gid
, grdst
, buf
, buflen
, grdstp
);
3365 if (ret
== ENOENT
) {
3374 #ifdef HAVE_GETGRGID_R
3375 # ifdef HAVE_SOLARIS_GETGRGID_R
3376 int getgrgid_r(gid_t gid
, struct group
*grdst
,
3377 char *buf
, int buflen
, struct group
**grdstp
)
3378 # else /* HAVE_SOLARIS_GETGRGID_R */
3379 int getgrgid_r(gid_t gid
, struct group
*grdst
,
3380 char *buf
, size_t buflen
, struct group
**grdstp
)
3381 # endif /* HAVE_SOLARIS_GETGRGID_R */
3383 if (!nss_wrapper_enabled()) {
3384 return libc_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
3387 return nwrap_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
3391 /****************************************************************************
3393 ***************************************************************************/
3395 static void nwrap_setgrent(void)
3399 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3400 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3401 b
->ops
->nw_setgrent(b
);
3405 #ifdef HAVE_BSD_SETGRENT
3411 if (!nss_wrapper_enabled()) {
3419 #ifdef HAVE_BSD_SETGRENT
3426 /****************************************************************************
3428 ***************************************************************************/
3430 static struct group
*nwrap_getgrent(void)
3435 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3436 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3437 grp
= b
->ops
->nw_getgrent(b
);
3446 struct group
*getgrent(void)
3448 if (!nss_wrapper_enabled()) {
3449 return libc_getgrent();
3452 return nwrap_getgrent();
3455 /****************************************************************************
3457 ***************************************************************************/
3459 static int nwrap_getgrent_r(struct group
*grdst
, char *buf
,
3460 size_t buflen
, struct group
**grdstp
)
3464 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3465 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3466 ret
= b
->ops
->nw_getgrent_r(b
, grdst
, buf
, buflen
, grdstp
);
3467 if (ret
== ENOENT
) {
3476 #ifdef HAVE_SOLARIS_GETGRENT_R
3477 struct group
*getgrent_r(struct group
*src
, char *buf
, int buflen
)
3479 struct group
*grdstp
= NULL
;
3482 if (!nss_wrapper_enabled()) {
3483 return libc_getgrent_r(src
, buf
, buflen
);
3486 rc
= nwrap_getgrent_r(src
, buf
, buflen
, &grdstp
);
3493 #else /* HAVE_SOLARIS_GETGRENT_R */
3494 int getgrent_r(struct group
*src
, char *buf
,
3495 size_t buflen
, struct group
**grdstp
)
3497 if (!nss_wrapper_enabled()) {
3498 return libc_getgrent_r(src
, buf
, buflen
, grdstp
);
3501 return nwrap_getgrent_r(src
, buf
, buflen
, grdstp
);
3503 #endif /* HAVE_SOLARIS_GETGRENT_R */
3505 /****************************************************************************
3507 ***************************************************************************/
3509 static void nwrap_endgrent(void)
3513 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
3514 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
3515 b
->ops
->nw_endgrent(b
);
3521 if (!nss_wrapper_enabled()) {
3529 /****************************************************************************
3531 ***************************************************************************/
3533 #ifdef HAVE_GETGROUPLIST
3534 static int nwrap_getgrouplist(const char *user
, gid_t group
,
3535 gid_t
*groups
, int *ngroups
)
3540 const char *name_of_group
= "";
3542 NWRAP_LOG(NWRAP_LOG_DEBUG
, "getgrouplist called for %s", user
);
3544 groups_tmp
= (gid_t
*)malloc(count
* sizeof(gid_t
));
3546 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
3551 memcpy(groups_tmp
, &group
, sizeof(gid_t
));
3553 grp
= nwrap_getgrgid(group
);
3555 name_of_group
= grp
->gr_name
;
3559 while ((grp
= nwrap_getgrent()) != NULL
) {
3562 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3563 "Inspecting %s for group membership",
3566 for (i
=0; grp
->gr_mem
&& grp
->gr_mem
[i
] != NULL
; i
++) {
3568 if ((strcmp(user
, grp
->gr_mem
[i
]) == 0) &&
3569 (strcmp(name_of_group
, grp
->gr_name
) != 0)) {
3571 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3572 "%s is member of %s",
3576 groups_tmp
= (gid_t
*)realloc(groups_tmp
, (count
+ 1) * sizeof(gid_t
));
3578 NWRAP_LOG(NWRAP_LOG_ERROR
,
3584 memcpy(&groups_tmp
[count
], &grp
->gr_gid
, sizeof(gid_t
));
3592 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3593 "%s is member of %d groups",
3596 if (*ngroups
< count
) {
3603 memcpy(groups
, groups_tmp
, count
* sizeof(gid_t
));
3609 int getgrouplist(const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
)
3611 if (!nss_wrapper_enabled()) {
3612 return libc_getgrouplist(user
, group
, groups
, ngroups
);
3615 return nwrap_getgrouplist(user
, group
, groups
, ngroups
);
3619 /**********************************************************
3621 **********************************************************/
3623 static void nwrap_sethostent(int stayopen
) {
3624 (void) stayopen
; /* ignored */
3626 nwrap_files_sethostent();
3629 #ifdef HAVE_SOLARIS_SETHOSTENT
3630 int sethostent(int stayopen
)
3632 if (!nss_wrapper_hosts_enabled()) {
3633 libc_sethostent(stayopen
);
3637 nwrap_sethostent(stayopen
);
3641 #else /* HAVE_SOLARIS_SETHOSTENT */
3642 void sethostent(int stayopen
)
3644 if (!nss_wrapper_hosts_enabled()) {
3645 libc_sethostent(stayopen
);
3649 nwrap_sethostent(stayopen
);
3651 #endif /* HAVE_SOLARIS_SETHOSTENT */
3653 static struct hostent
*nwrap_gethostent(void)
3655 return nwrap_files_gethostent();
3658 struct hostent
*gethostent(void) {
3659 if (!nss_wrapper_hosts_enabled()) {
3660 return libc_gethostent();
3663 return nwrap_gethostent();
3666 static void nwrap_endhostent(void) {
3667 nwrap_files_endhostent();
3670 #ifdef HAVE_SOLARIS_ENDHOSTENT
3671 int endhostent(void)
3673 if (!nss_wrapper_hosts_enabled()) {
3682 #else /* HAVE_SOLARIS_ENDHOSTENT */
3683 void endhostent(void)
3685 if (!nss_wrapper_hosts_enabled()) {
3692 #endif /* HAVE_SOLARIS_ENDHOSTENT */
3694 static struct hostent
*nwrap_gethostbyname(const char *name
)
3696 return nwrap_files_gethostbyname(name
, AF_UNSPEC
);
3699 struct hostent
*gethostbyname(const char *name
)
3701 if (!nss_wrapper_hosts_enabled()) {
3702 return libc_gethostbyname(name
);
3705 return nwrap_gethostbyname(name
);
3708 /* This is a GNU extension */
3709 #ifdef HAVE_GETHOSTBYNAME2
3710 static struct hostent
*nwrap_gethostbyname2(const char *name
, int af
)
3712 return nwrap_files_gethostbyname(name
, af
);
3715 struct hostent
*gethostbyname2(const char *name
, int af
)
3717 if (!nss_wrapper_hosts_enabled()) {
3718 return libc_gethostbyname2(name
, af
);
3721 return nwrap_gethostbyname2(name
, af
);
3725 static struct hostent
*nwrap_gethostbyaddr(const void *addr
,
3726 socklen_t len
, int type
)
3728 return nwrap_files_gethostbyaddr(addr
, len
, type
);
3731 struct hostent
*gethostbyaddr(const void *addr
,
3732 socklen_t len
, int type
)
3734 if (!nss_wrapper_hosts_enabled()) {
3735 return libc_gethostbyaddr(addr
, len
, type
);
3738 return nwrap_gethostbyaddr(addr
, len
, type
);
3741 static const struct addrinfo default_hints
=
3743 .ai_flags
= AI_ADDRCONFIG
|AI_V4MAPPED
,
3744 .ai_family
= AF_UNSPEC
,
3749 .ai_canonname
= NULL
,
3753 static int nwrap_convert_he_ai(const struct hostent
*he
,
3754 unsigned short port
,
3755 const struct addrinfo
*hints
,
3756 struct addrinfo
**pai
)
3758 struct addrinfo
*ai
;
3761 switch (he
->h_addrtype
) {
3763 socklen
= sizeof(struct sockaddr_in
);
3767 socklen
= sizeof(struct sockaddr_in6
);
3774 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
) + socklen
);
3780 ai
->ai_family
= he
->h_addrtype
;
3781 ai
->ai_socktype
= hints
->ai_socktype
;
3782 ai
->ai_protocol
= hints
->ai_protocol
;
3784 ai
->ai_addrlen
= socklen
;
3785 ai
->ai_addr
= (void *)(ai
+ 1);
3787 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
3788 ai
->ai_addr
->sa_len
= socklen
;
3790 ai
->ai_addr
->sa_family
= he
->h_addrtype
;
3792 switch (he
->h_addrtype
) {
3795 struct sockaddr_in
*sinp
=
3796 (struct sockaddr_in
*) ai
->ai_addr
;
3798 memset(sinp
, 0, sizeof(struct sockaddr_in
));
3800 sinp
->sin_port
= htons(port
);
3801 sinp
->sin_family
= AF_INET
;
3803 memset (sinp
->sin_zero
, '\0', sizeof (sinp
->sin_zero
));
3804 memcpy(&sinp
->sin_addr
, he
->h_addr_list
[0], he
->h_length
);
3811 struct sockaddr_in6
*sin6p
=
3812 (struct sockaddr_in6
*) ai
->ai_addr
;
3814 memset(sin6p
, 0, sizeof(struct sockaddr_in6
));
3816 sin6p
->sin6_port
= htons(port
);
3817 sin6p
->sin6_family
= AF_INET6
;
3819 memcpy(&sin6p
->sin6_addr
, he
->h_addr_list
[0], he
->h_length
);
3828 ai
->ai_canonname
= strdup(he
->h_name
);
3829 if (ai
->ai_canonname
== NULL
) {
3839 static int nwrap_getaddrinfo(const char *node
,
3840 const char *service
,
3841 const struct addrinfo
*hints
,
3842 struct addrinfo
**res
)
3844 struct addrinfo
*ai
= NULL
;
3845 struct addrinfo
*p
= NULL
;
3846 unsigned short port
= 0;
3849 bool is_addr_ipv4
= false;
3850 bool is_addr_ipv6
= false;
3851 int eai
= EAI_SYSTEM
;
3856 if (node
== NULL
&& service
== NULL
) {
3860 ret
= libc_getaddrinfo(node
, service
, hints
, &p
);
3865 /* If no node has been specified, let glibc deal with it */
3870 if (hints
== NULL
) {
3871 hints
= &default_hints
;
3874 if ((hints
->ai_flags
& AI_CANONNAME
) && node
== NULL
) {
3875 return EAI_BADFLAGS
;
3878 if (service
!= NULL
&& service
[0] != '\0') {
3879 if (isdigit((int)service
[0])) {
3880 port
= (unsigned short)atoi(service
);
3882 const char *proto
= NULL
;
3885 if (hints
->ai_protocol
!= 0) {
3886 struct protoent
*pent
;
3888 pent
= getprotobynumber(hints
->ai_protocol
);
3890 proto
= pent
->p_name
;
3894 s
= getservbyname(service
, proto
);
3896 port
= ntohs(s
->s_port
);
3906 af
= hints
->ai_family
;
3907 if (af
== AF_UNSPEC
) {
3911 rc
= inet_pton(af
, node
, &in
);
3913 is_addr_ipv4
= true;
3914 if (af
== AF_UNSPEC
) {
3919 struct in6_addr in6
;
3923 rc
= inet_pton(af
, node
, &in6
);
3925 is_addr_ipv6
= true;
3931 he
= nwrap_files_gethostbyaddr(&in
, sizeof(struct in_addr
), af
);
3933 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
);
3939 } else if (is_addr_ipv6
) {
3940 struct in6_addr in6
;
3942 rc
= inet_pton(af
, node
, &in6
);
3944 eai
= EAI_ADDRFAMILY
;
3945 return ret
== 0 ? 0 : eai
;
3948 he
= nwrap_files_gethostbyaddr(&in6
,
3949 sizeof(struct in6_addr
),
3952 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
);
3960 he
= nwrap_files_gethostbyname(node
, hints
->ai_family
);
3962 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
);
3971 return ret
== 0 ? 0 : eai
;
3978 if (ai
->ai_flags
== 0) {
3979 ai
->ai_flags
= hints
->ai_flags
;
3981 if (ai
->ai_socktype
== 0) {
3982 ai
->ai_socktype
= SOCK_DGRAM
;
3984 if (ai
->ai_protocol
== 0 && ai
->ai_socktype
== SOCK_DGRAM
) {
3985 ai
->ai_protocol
= 17; /* UDP */
3986 } else if (ai
->ai_protocol
== 0 && ai
->ai_socktype
== SOCK_STREAM
) {
3987 ai
->ai_protocol
= 6; /* TCP */
3990 if (hints
->ai_socktype
== 0) {
3992 rc
= nwrap_convert_he_ai(he
, port
, hints
, &ai
->ai_next
);
3998 if (ai
->ai_next
->ai_flags
== 0) {
3999 ai
->ai_next
->ai_flags
= hints
->ai_flags
;
4001 if (ai
->ai_socktype
== SOCK_DGRAM
) {
4002 ai
->ai_next
->ai_socktype
= SOCK_STREAM
;
4003 } else if (ai
->ai_socktype
== SOCK_STREAM
) {
4004 ai
->ai_next
->ai_socktype
= SOCK_DGRAM
;
4006 if (ai
->ai_next
->ai_socktype
== SOCK_DGRAM
) {
4007 ai
->ai_next
->ai_protocol
= 17; /* UDP */
4008 } else if (ai
->ai_next
->ai_socktype
== SOCK_STREAM
) {
4009 ai
->ai_next
->ai_protocol
= 6; /* TCP */
4018 int getaddrinfo(const char *node
, const char *service
,
4019 const struct addrinfo
*hints
,
4020 struct addrinfo
**res
)
4022 if (!nss_wrapper_hosts_enabled()) {
4023 return libc_getaddrinfo(node
, service
, hints
, res
);
4026 return nwrap_getaddrinfo(node
, service
, hints
, res
);
4029 static int nwrap_getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4030 char *host
, size_t hostlen
,
4031 char *serv
, size_t servlen
,
4035 struct servent
*service
;
4042 if (sa
== NULL
|| salen
< sizeof(sa_family_t
)) {
4046 if ((flags
& NI_NAMEREQD
) && host
== NULL
&& serv
== NULL
) {
4050 type
= sa
->sa_family
;
4053 if (salen
< sizeof(struct sockaddr_in
))
4055 addr
= &((const struct sockaddr_in
*)sa
)->sin_addr
;
4056 addrlen
= sizeof(((const struct sockaddr_in
*)sa
)->sin_addr
);
4057 port
= ntohs(((const struct sockaddr_in
*)sa
)->sin_port
);
4061 if (salen
< sizeof(struct sockaddr_in6
))
4063 addr
= &((const struct sockaddr_in6
*)sa
)->sin6_addr
;
4064 addrlen
= sizeof(((const struct sockaddr_in6
*)sa
)->sin6_addr
);
4065 port
= ntohs(((const struct sockaddr_in6
*)sa
)->sin6_port
);
4074 if ((flags
& NI_NUMERICHOST
) == 0) {
4075 he
= nwrap_files_gethostbyaddr(addr
, addrlen
, type
);
4076 if ((flags
& NI_NAMEREQD
) && (he
== NULL
|| he
->h_name
== NULL
))
4079 if (he
!= NULL
&& he
->h_name
!= NULL
) {
4080 if (strlen(he
->h_name
) >= hostlen
)
4081 return EAI_OVERFLOW
;
4082 strcpy(host
, he
->h_name
);
4083 if (flags
& NI_NOFQDN
)
4084 host
[strcspn(host
, ".")] = '\0';
4086 if (inet_ntop(type
, addr
, host
, hostlen
) == NULL
)
4087 return (errno
== ENOSPC
) ? EAI_OVERFLOW
: EAI_FAIL
;
4093 if ((flags
& NI_NUMERICSERV
) == 0) {
4094 proto
= (flags
& NI_DGRAM
) ? "udp" : "tcp";
4095 service
= getservbyport(htons(port
), proto
);
4097 if (service
!= NULL
) {
4098 if (strlen(service
->s_name
) >= servlen
)
4099 return EAI_OVERFLOW
;
4100 strcpy(serv
, service
->s_name
);
4102 if (snprintf(serv
, servlen
, "%u", port
) >= (int) servlen
)
4103 return EAI_OVERFLOW
;
4110 #ifdef HAVE_LINUX_GETNAMEINFO
4111 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4112 char *host
, socklen_t hostlen
,
4113 char *serv
, socklen_t servlen
,
4115 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
4116 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4117 char *host
, socklen_t hostlen
,
4118 char *serv
, socklen_t servlen
,
4121 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
4122 char *host
, size_t hostlen
,
4123 char *serv
, size_t servlen
,
4127 if (!nss_wrapper_hosts_enabled()) {
4128 return libc_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
4131 return nwrap_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
4134 static int nwrap_gethostname(char *name
, size_t len
)
4136 const char *hostname
= getenv("NSS_WRAPPER_HOSTNAME");
4138 if (strlen(hostname
) >= len
) {
4139 errno
= ENAMETOOLONG
;
4142 snprintf(name
, len
, "%s", hostname
);
4147 #ifdef HAVE_SOLARIS_GETHOSTNAME
4148 int gethostname(char *name
, int len
)
4149 #else /* HAVE_SOLARIS_GETHOSTNAME */
4150 int gethostname(char *name
, size_t len
)
4151 #endif /* HAVE_SOLARIS_GETHOSTNAME */
4153 if (!nwrap_hostname_enabled()) {
4154 return libc_gethostname(name
, len
);
4157 return nwrap_gethostname(name
, len
);
4160 /****************************
4162 ***************************/
4165 * This function is called when the library is unloaded and makes sure that
4166 * sockets get closed and the unix file for the socket are unlinked.
4168 void nwrap_destructor(void)
4172 if (nwrap_main_global
!= NULL
) {
4173 struct nwrap_main
*m
= nwrap_main_global
;
4176 SAFE_FREE(m
->libc
->fns
);
4177 if (m
->libc
->handle
!= NULL
) {
4178 dlclose(m
->libc
->handle
);
4180 if (m
->libc
->nsl_handle
!= NULL
) {
4181 dlclose(m
->libc
->nsl_handle
);
4183 if (m
->libc
->sock_handle
!= NULL
) {
4184 dlclose(m
->libc
->sock_handle
);
4189 for (i
= 0; i
< m
->num_backends
; i
++) {
4190 struct nwrap_backend
*b
= &(m
->backends
[i
]);
4192 if (b
->so_handle
!= NULL
) {
4193 dlclose(b
->so_handle
);
4197 SAFE_FREE(m
->backends
);
4200 if (nwrap_pw_global
.cache
!= NULL
) {
4201 struct nwrap_cache
*c
= nwrap_pw_global
.cache
;
4203 nwrap_files_cache_unload(c
);
4208 SAFE_FREE(nwrap_pw_global
.list
);
4209 nwrap_pw_global
.num
= 0;
4212 if (nwrap_gr_global
.cache
!= NULL
) {
4213 struct nwrap_cache
*c
= nwrap_gr_global
.cache
;
4215 nwrap_files_cache_unload(c
);
4220 SAFE_FREE(nwrap_gr_global
.list
);
4221 nwrap_pw_global
.num
= 0;
4224 if (nwrap_he_global
.cache
!= NULL
) {
4225 struct nwrap_cache
*c
= nwrap_he_global
.cache
;
4227 nwrap_files_cache_unload(c
);
4232 SAFE_FREE(nwrap_he_global
.list
);
4233 nwrap_he_global
.num
= 0;