vfs_glusterfs: Use glfs_renameat() for SMB_VFS_RENAMEAT
[samba4-gss.git] / third_party / nss_wrapper / nss_wrapper.c
blob88e81d9c652bb87d02daa87ba73e6f27f8894adc
1 /*
2 * BSD 3-Clause License
4 * Copyright (c) 2007, Stefan Metzmacher <metze@samba.org>
5 * Copyright (c) 2009, Guenther Deschner <gd@samba.org>
6 * Copyright (c) 2014-2015, Michael Adam <obnox@samba.org>
7 * Copyright (c) 2015, Robin Hack <hack.robin@gmail.com>
8 * Copyright (c) 2013-2018, Andreas Schneider <asn@samba.org>
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the author nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #include "config.h"
41 #include <pthread.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/socket.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdarg.h>
49 #include <stdbool.h>
50 #include <stddef.h>
51 #include <stdio.h>
52 #include <stdint.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <ctype.h>
58 #include <netinet/in.h>
60 #include <search.h>
61 #include <assert.h>
64 * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h gives us
65 * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
66 * Solaris
68 #ifndef _POSIX_PTHREAD_SEMANTICS
69 #define _POSIX_PTHREAD_SEMANTICS
70 #endif
72 #include <pwd.h>
73 #include <grp.h>
74 #ifdef HAVE_SHADOW_H
75 #include <shadow.h>
76 #endif /* HAVE_SHADOW_H */
78 #include <netdb.h>
79 #include <arpa/inet.h>
80 #include <netinet/in.h>
82 #include <dlfcn.h>
84 #if defined(HAVE_NSS_H)
85 /* Linux and BSD */
86 #include <nss.h>
88 typedef enum nss_status NSS_STATUS;
89 #elif defined(HAVE_NSS_COMMON_H)
90 /* Solaris */
91 #include <nss_common.h>
92 #include <nss_dbdefs.h>
93 #include <nsswitch.h>
95 typedef nss_status_t NSS_STATUS;
97 # define NSS_STATUS_SUCCESS NSS_SUCCESS
98 # define NSS_STATUS_NOTFOUND NSS_NOTFOUND
99 # define NSS_STATUS_UNAVAIL NSS_UNAVAIL
100 # define NSS_STATUS_TRYAGAIN NSS_TRYAGAIN
101 #else
102 # error "No nsswitch support detected"
103 #endif
105 #ifndef PTR_DIFF
106 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
107 #endif
109 #ifndef _PUBLIC_
110 #define _PUBLIC_
111 #endif
113 #ifndef EAI_NODATA
114 #define EAI_NODATA EAI_NONAME
115 #endif
117 #ifndef EAI_ADDRFAMILY
118 #define EAI_ADDRFAMILY EAI_FAMILY
119 #endif
121 #ifndef __STRING
122 #define __STRING(x) #x
123 #endif
125 #ifndef __STRINGSTRING
126 #define __STRINGSTRING(x) __STRING(x)
127 #endif
129 #ifndef __LINESTR__
130 #define __LINESTR__ __STRINGSTRING(__LINE__)
131 #endif
133 #ifndef __location__
134 #define __location__ __FILE__ ":" __LINESTR__
135 #endif
137 #ifndef DNS_NAME_MAX
138 #define DNS_NAME_MAX 255
139 #endif
141 /* GCC have printf type attribute check. */
142 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
143 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
144 #else
145 #define PRINTF_ATTRIBUTE(a,b)
146 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
148 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
149 #define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
150 #else
151 #define CONSTRUCTOR_ATTRIBUTE
152 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
154 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
155 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
156 #else
157 #define DESTRUCTOR_ATTRIBUTE
158 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
160 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
162 #ifndef SAFE_FREE
163 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
164 #endif
166 #ifndef discard_const
167 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
168 #endif
170 #ifndef discard_const_p
171 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
172 #endif
174 #ifdef HAVE_IPV6
175 #define NWRAP_INET_ADDRSTRLEN INET6_ADDRSTRLEN
176 #else
177 #define NWRAP_INET_ADDRSTRLEN INET_ADDRSTRLEN
178 #endif
180 #define NWRAP_LOCK(m) do { \
181 pthread_mutex_lock(&( m ## _mutex)); \
182 } while(0)
184 #define NWRAP_UNLOCK(m) do { \
185 pthread_mutex_unlock(&( m ## _mutex)); \
186 } while(0)
188 static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
189 static pthread_mutex_t nss_module_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
191 static bool nwrap_initialized = false;
192 static pthread_mutex_t nwrap_initialized_mutex = PTHREAD_MUTEX_INITIALIZER;
194 /* The mutex or accessing the id */
195 static pthread_mutex_t nwrap_global_mutex = PTHREAD_MUTEX_INITIALIZER;
196 static pthread_mutex_t nwrap_gr_global_mutex = PTHREAD_MUTEX_INITIALIZER;
197 static pthread_mutex_t nwrap_he_global_mutex = PTHREAD_MUTEX_INITIALIZER;
198 static pthread_mutex_t nwrap_pw_global_mutex = PTHREAD_MUTEX_INITIALIZER;
199 static pthread_mutex_t nwrap_sp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
201 /* Add new global locks here please */
202 /* Also don't forget to add locks to
203 * nwrap_init() function.
205 # define NWRAP_LOCK_ALL do { \
206 NWRAP_LOCK(libc_symbol_binding); \
207 NWRAP_LOCK(nss_module_symbol_binding); \
208 NWRAP_LOCK(nwrap_initialized); \
209 NWRAP_LOCK(nwrap_global); \
210 NWRAP_LOCK(nwrap_gr_global); \
211 NWRAP_LOCK(nwrap_he_global); \
212 NWRAP_LOCK(nwrap_pw_global); \
213 NWRAP_LOCK(nwrap_sp_global); \
214 } while (0);
216 # define NWRAP_UNLOCK_ALL do {\
217 NWRAP_UNLOCK(nwrap_sp_global); \
218 NWRAP_UNLOCK(nwrap_pw_global); \
219 NWRAP_UNLOCK(nwrap_he_global); \
220 NWRAP_UNLOCK(nwrap_gr_global); \
221 NWRAP_UNLOCK(nwrap_global); \
222 NWRAP_UNLOCK(nwrap_initialized); \
223 NWRAP_UNLOCK(nss_module_symbol_binding); \
224 NWRAP_UNLOCK(libc_symbol_binding); \
225 } while (0);
227 static void nwrap_init(void);
229 static void nwrap_thread_prepare(void)
231 nwrap_init();
232 NWRAP_LOCK_ALL;
235 static void nwrap_thread_parent(void)
237 NWRAP_UNLOCK_ALL;
240 static void nwrap_thread_child(void)
242 NWRAP_UNLOCK_ALL;
245 enum nwrap_dbglvl_e {
246 NWRAP_LOG_ERROR = 0,
247 NWRAP_LOG_WARN,
248 NWRAP_LOG_DEBUG,
249 NWRAP_LOG_TRACE
252 #ifndef HAVE_GETPROGNAME
253 static const char *getprogname(void)
255 #if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
256 return program_invocation_short_name;
257 #elif defined(HAVE_GETEXECNAME)
258 return getexecname();
259 #else
260 return NULL;
261 #endif /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
263 #endif /* HAVE_GETPROGNAME */
265 static void nwrap_log(enum nwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
266 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
268 static void nwrap_log(enum nwrap_dbglvl_e dbglvl,
269 const char *func,
270 const char *format, ...)
272 char buffer[1024];
273 va_list va;
274 const char *d;
275 unsigned int lvl = 0;
276 const char *prefix = "NWRAP";
277 const char *progname = getprogname();
279 d = getenv("NSS_WRAPPER_DEBUGLEVEL");
280 if (d != NULL) {
281 lvl = atoi(d);
284 if (lvl < dbglvl) {
285 return;
288 va_start(va, format);
289 vsnprintf(buffer, sizeof(buffer), format, va);
290 va_end(va);
292 switch (dbglvl) {
293 case NWRAP_LOG_ERROR:
294 prefix = "NWRAP_ERROR";
295 break;
296 case NWRAP_LOG_WARN:
297 prefix = "NWRAP_WARN";
298 break;
299 case NWRAP_LOG_DEBUG:
300 prefix = "NWRAP_DEBUG";
301 break;
302 case NWRAP_LOG_TRACE:
303 prefix = "NWRAP_TRACE";
304 break;
307 if (progname == NULL) {
308 progname = "<unknown>";
311 fprintf(stderr,
312 "%s[%s (%u)] - %s: %s\n",
313 prefix,
314 progname,
315 (unsigned int)getpid(),
316 func,
317 buffer);
320 /*****************
321 * LIBC
322 *****************/
324 #define LIBC_NAME "libc.so"
326 typedef struct passwd *(*__libc_getpwnam)(const char *name);
328 typedef int (*__libc_getpwnam_r)(const char *name,
329 struct passwd *pwd,
330 char *buf,
331 size_t buflen,
332 struct passwd **result);
334 typedef struct passwd *(*__libc_getpwuid)(uid_t uid);
336 typedef int (*__libc_getpwuid_r)(uid_t uid,
337 struct passwd *pwd,
338 char *buf,
339 size_t buflen,
340 struct passwd **result);
342 typedef void (*__libc_setpwent)(void);
344 typedef struct passwd *(*__libc_getpwent)(void);
346 #ifdef HAVE_GETPWENT_R
347 # ifdef HAVE_SOLARIS_GETPWENT_R
348 typedef struct passwd *(*__libc_getpwent_r)(struct passwd *pwbuf,
349 char *buf,
350 size_t buflen);
351 # else /* HAVE_SOLARIS_GETPWENT_R */
352 typedef int (*__libc_getpwent_r)(struct passwd *pwbuf,
353 char *buf,
354 size_t buflen,
355 struct passwd **pwbufp);
356 # endif /* HAVE_SOLARIS_GETPWENT_R */
357 #endif /* HAVE_GETPWENT_R */
359 typedef void (*__libc_endpwent)(void);
361 typedef int (*__libc_initgroups)(const char *user, gid_t gid);
363 typedef struct group *(*__libc_getgrnam)(const char *name);
365 typedef int (*__libc_getgrnam_r)(const char *name,
366 struct group *grp,
367 char *buf,
368 size_t buflen,
369 struct group **result);
371 typedef struct group *(*__libc_getgrgid)(gid_t gid);
373 typedef int (*__libc_getgrgid_r)(gid_t gid,
374 struct group *grp,
375 char *buf,
376 size_t buflen,
377 struct group **result);
379 typedef void (*__libc_setgrent)(void);
381 typedef struct group *(*__libc_getgrent)(void);
383 #ifdef HAVE_GETGRENT_R
384 # ifdef HAVE_SOLARIS_GETGRENT_R
385 typedef struct group *(*__libc_getgrent_r)(struct group *group,
386 char *buf,
387 size_t buflen);
388 # else /* HAVE_SOLARIS_GETGRENT_R */
389 typedef int (*__libc_getgrent_r)(struct group *group,
390 char *buf,
391 size_t buflen,
392 struct group **result);
393 # endif /* HAVE_SOLARIS_GETGRENT_R */
394 #endif /* HAVE_GETGRENT_R */
396 typedef void (*__libc_endgrent)(void);
398 typedef int (*__libc_getgrouplist)(const char *user,
399 gid_t group,
400 gid_t *groups,
401 int *ngroups);
403 typedef void (*__libc_sethostent)(int stayopen);
405 typedef struct hostent *(*__libc_gethostent)(void);
407 typedef void (*__libc_endhostent)(void);
409 typedef struct hostent *(*__libc_gethostbyname)(const char *name);
411 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
412 typedef struct hostent *(*__libc_gethostbyname2)(const char *name, int af);
413 #endif
415 #ifdef HAVE_GETHOSTBYNAME2_R /* GNU extension */
416 typedef int (*__libc_gethostbyname2_r)(const char *name,
417 int af,
418 struct hostent *ret,
419 char *buf,
420 size_t buflen,
421 struct hostent **result,
422 int *h_errnop);
423 #endif
425 typedef struct hostent *(*__libc_gethostbyaddr)(const void *addr,
426 socklen_t len,
427 int type);
429 typedef int (*__libc_getaddrinfo)(const char *node,
430 const char *service,
431 const struct addrinfo *hints,
432 struct addrinfo **res);
433 typedef int (*__libc_getnameinfo)(const struct sockaddr *sa,
434 socklen_t salen,
435 char *host,
436 size_t hostlen,
437 char *serv,
438 size_t servlen,
439 int flags);
441 typedef int (*__libc_gethostname)(char *name, size_t len);
443 #ifdef HAVE_GETHOSTBYNAME_R
444 typedef int (*__libc_gethostbyname_r)(const char *name,
445 struct hostent *ret,
446 char *buf, size_t buflen,
447 struct hostent **result, int *h_errnop);
448 #endif
450 #ifdef HAVE_GETHOSTBYADDR_R
451 typedef int (*__libc_gethostbyaddr_r)(const void *addr,
452 socklen_t len,
453 int type,
454 struct hostent *ret,
455 char *buf,
456 size_t buflen,
457 struct hostent **result,
458 int *h_errnop);
459 #endif
461 #define NWRAP_SYMBOL_ENTRY(i) \
462 union { \
463 __libc_##i f; \
464 void *obj; \
465 } _libc_##i
467 struct nwrap_libc_symbols {
468 NWRAP_SYMBOL_ENTRY(getpwnam);
469 NWRAP_SYMBOL_ENTRY(getpwnam_r);
470 NWRAP_SYMBOL_ENTRY(getpwuid);
471 NWRAP_SYMBOL_ENTRY(getpwuid_r);
472 NWRAP_SYMBOL_ENTRY(setpwent);
473 NWRAP_SYMBOL_ENTRY(getpwent);
474 #ifdef HAVE_GETPWENT_R
475 NWRAP_SYMBOL_ENTRY(getpwent_r);
476 #endif
477 NWRAP_SYMBOL_ENTRY(endpwent);
479 NWRAP_SYMBOL_ENTRY(initgroups);
480 NWRAP_SYMBOL_ENTRY(getgrnam);
481 NWRAP_SYMBOL_ENTRY(getgrnam_r);
482 NWRAP_SYMBOL_ENTRY(getgrgid);
483 NWRAP_SYMBOL_ENTRY(getgrgid_r);
484 NWRAP_SYMBOL_ENTRY(setgrent);
485 NWRAP_SYMBOL_ENTRY(getgrent);
486 #ifdef HAVE_GETGRENT_R
487 NWRAP_SYMBOL_ENTRY(getgrent_r);
488 #endif
489 NWRAP_SYMBOL_ENTRY(endgrent);
490 NWRAP_SYMBOL_ENTRY(getgrouplist);
492 NWRAP_SYMBOL_ENTRY(sethostent);
493 NWRAP_SYMBOL_ENTRY(gethostent);
494 NWRAP_SYMBOL_ENTRY(endhostent);
495 NWRAP_SYMBOL_ENTRY(gethostbyname);
496 #ifdef HAVE_GETHOSTBYNAME_R
497 NWRAP_SYMBOL_ENTRY(gethostbyname_r);
498 #endif
499 #ifdef HAVE_GETHOSTBYNAME2
500 NWRAP_SYMBOL_ENTRY(gethostbyname2);
501 #endif
502 #ifdef HAVE_GETHOSTBYNAME2_R
503 NWRAP_SYMBOL_ENTRY(gethostbyname2_r);
504 #endif
505 NWRAP_SYMBOL_ENTRY(gethostbyaddr);
506 #ifdef HAVE_GETHOSTBYADDR_R
507 NWRAP_SYMBOL_ENTRY(gethostbyaddr_r);
508 #endif
509 NWRAP_SYMBOL_ENTRY(getaddrinfo);
510 NWRAP_SYMBOL_ENTRY(getnameinfo);
511 NWRAP_SYMBOL_ENTRY(gethostname);
513 #undef NWRAP_SYMBOL_ENTRY
515 typedef NSS_STATUS (*__nss_getpwnam_r)(const char *name,
516 struct passwd *result,
517 char *buffer,
518 size_t buflen,
519 int *errnop);
520 typedef NSS_STATUS (*__nss_getpwuid_r)(uid_t uid,
521 struct passwd *result,
522 char *buffer,
523 size_t buflen,
524 int *errnop);
525 typedef NSS_STATUS (*__nss_setpwent)(void);
526 typedef NSS_STATUS (*__nss_getpwent_r)(struct passwd *result,
527 char *buffer,
528 size_t buflen,
529 int *errnop);
530 typedef NSS_STATUS (*__nss_endpwent)(void);
531 typedef NSS_STATUS (*__nss_initgroups)(const char *user,
532 gid_t group,
533 long int *start,
534 long int *size,
535 gid_t **groups,
536 long int limit,
537 int *errnop);
538 typedef NSS_STATUS (*__nss_getgrnam_r)(const char *name,
539 struct group *result,
540 char *buffer,
541 size_t buflen,
542 int *errnop);
543 typedef NSS_STATUS (*__nss_getgrgid_r)(gid_t gid,
544 struct group *result,
545 char *buffer,
546 size_t buflen,
547 int *errnop);
548 typedef NSS_STATUS (*__nss_setgrent)(void);
549 typedef NSS_STATUS (*__nss_getgrent_r)(struct group *result,
550 char *buffer,
551 size_t buflen,
552 int *errnop);
553 typedef NSS_STATUS (*__nss_endgrent)(void);
554 typedef NSS_STATUS (*__nss_gethostbyaddr_r)(const void *addr,
555 socklen_t addrlen,
556 int af,
557 struct hostent *result,
558 char *buffer,
559 size_t buflen,
560 int *errnop,
561 int *h_errnop);
562 typedef NSS_STATUS (*__nss_gethostbyname2_r)(const char *name,
563 int af,
564 struct hostent *result,
565 char *buffer,
566 size_t buflen,
567 int *errnop,
568 int *h_errnop);
570 #define NWRAP_NSS_MODULE_SYMBOL_ENTRY(i) \
571 union { \
572 __nss_##i f; \
573 void *obj; \
574 } _nss_##i
576 struct nwrap_nss_module_symbols {
577 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getpwnam_r);
578 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getpwuid_r);
579 NWRAP_NSS_MODULE_SYMBOL_ENTRY(setpwent);
580 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getpwent_r);
581 NWRAP_NSS_MODULE_SYMBOL_ENTRY(endpwent);
583 NWRAP_NSS_MODULE_SYMBOL_ENTRY(initgroups);
584 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getgrnam_r);
585 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getgrgid_r);
586 NWRAP_NSS_MODULE_SYMBOL_ENTRY(setgrent);
587 NWRAP_NSS_MODULE_SYMBOL_ENTRY(getgrent_r);
588 NWRAP_NSS_MODULE_SYMBOL_ENTRY(endgrent);
590 NWRAP_NSS_MODULE_SYMBOL_ENTRY(gethostbyaddr_r);
591 NWRAP_NSS_MODULE_SYMBOL_ENTRY(gethostbyname2_r);
594 struct nwrap_backend {
595 const char *name;
596 const char *so_path;
597 void *so_handle;
598 struct nwrap_ops *ops;
599 struct nwrap_nss_module_symbols *symbols;
602 struct nwrap_vector;
604 struct nwrap_ops {
605 struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
606 const char *name);
607 int (*nw_getpwnam_r)(struct nwrap_backend *b,
608 const char *name, struct passwd *pwdst,
609 char *buf, size_t buflen, struct passwd **pwdstp);
610 struct passwd * (*nw_getpwuid)(struct nwrap_backend *b,
611 uid_t uid);
612 int (*nw_getpwuid_r)(struct nwrap_backend *b,
613 uid_t uid, struct passwd *pwdst,
614 char *buf, size_t buflen, struct passwd **pwdstp);
615 void (*nw_setpwent)(struct nwrap_backend *b);
616 struct passwd * (*nw_getpwent)(struct nwrap_backend *b);
617 int (*nw_getpwent_r)(struct nwrap_backend *b,
618 struct passwd *pwdst, char *buf,
619 size_t buflen, struct passwd **pwdstp);
620 void (*nw_endpwent)(struct nwrap_backend *b);
621 int (*nw_initgroups)(struct nwrap_backend *b,
622 const char *user, gid_t group);
623 struct group * (*nw_getgrnam)(struct nwrap_backend *b,
624 const char *name);
625 int (*nw_getgrnam_r)(struct nwrap_backend *b,
626 const char *name, struct group *grdst,
627 char *buf, size_t buflen, struct group **grdstp);
628 struct group * (*nw_getgrgid)(struct nwrap_backend *b,
629 gid_t gid);
630 int (*nw_getgrgid_r)(struct nwrap_backend *b,
631 gid_t gid, struct group *grdst,
632 char *buf, size_t buflen, struct group **grdstp);
633 void (*nw_setgrent)(struct nwrap_backend *b);
634 struct group * (*nw_getgrent)(struct nwrap_backend *b);
635 int (*nw_getgrent_r)(struct nwrap_backend *b,
636 struct group *grdst, char *buf,
637 size_t buflen, struct group **grdstp);
638 void (*nw_endgrent)(struct nwrap_backend *b);
639 struct hostent *(*nw_gethostbyaddr)(struct nwrap_backend *b,
640 const void *addr,
641 socklen_t len, int type);
642 struct hostent *(*nw_gethostbyname)(struct nwrap_backend *b,
643 const char *name);
644 struct hostent *(*nw_gethostbyname2)(struct nwrap_backend *b,
645 const char *name, int af);
646 int (*nw_gethostbyname2_r)(struct nwrap_backend *b,
647 const char *name, int af,
648 struct hostent *hedst,
649 char *buf, size_t buflen,
650 struct hostent **hedstp);
653 /* Public prototypes */
655 bool nss_wrapper_enabled(void);
656 bool nss_wrapper_shadow_enabled(void);
657 bool nss_wrapper_hosts_enabled(void);
659 /* prototypes for files backend */
662 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
663 const char *name);
664 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
665 const char *name, struct passwd *pwdst,
666 char *buf, size_t buflen, struct passwd **pwdstp);
667 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
668 uid_t uid);
669 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
670 uid_t uid, struct passwd *pwdst,
671 char *buf, size_t buflen, struct passwd **pwdstp);
672 static void nwrap_files_setpwent(struct nwrap_backend *b);
673 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b);
674 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
675 struct passwd *pwdst, char *buf,
676 size_t buflen, struct passwd **pwdstp);
677 static void nwrap_files_endpwent(struct nwrap_backend *b);
678 static int nwrap_files_initgroups(struct nwrap_backend *b,
679 const char *user, gid_t group);
680 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
681 const char *name);
682 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
683 const char *name, struct group *grdst,
684 char *buf, size_t buflen, struct group **grdstp);
685 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
686 gid_t gid);
687 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
688 gid_t gid, struct group *grdst,
689 char *buf, size_t buflen, struct group **grdstp);
690 static void nwrap_files_setgrent(struct nwrap_backend *b);
691 static struct group *nwrap_files_getgrent(struct nwrap_backend *b);
692 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
693 struct group *grdst, char *buf,
694 size_t buflen, struct group **grdstp);
695 static void nwrap_files_endgrent(struct nwrap_backend *b);
696 static struct hostent *nwrap_files_gethostbyaddr(struct nwrap_backend *b,
697 const void *addr,
698 socklen_t len, int type);
699 static struct hostent *nwrap_files_gethostbyname(struct nwrap_backend *b,
700 const char *name);
701 #ifdef HAVE_GETHOSTBYNAME2
702 static struct hostent *nwrap_files_gethostbyname2(struct nwrap_backend *b,
703 const char *name, int af);
704 #endif /* HAVE_GETHOSTBYNAME2 */
705 static int nwrap_files_gethostbyname2_r(struct nwrap_backend *b,
706 const char *name, int af,
707 struct hostent *hedst,
708 char *buf, size_t buflen,
709 struct hostent **hedstp);
711 /* prototypes for module backend */
713 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b);
714 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
715 struct passwd *pwdst, char *buf,
716 size_t buflen, struct passwd **pwdstp);
717 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
718 const char *name);
719 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
720 const char *name, struct passwd *pwdst,
721 char *buf, size_t buflen, struct passwd **pwdstp);
722 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
723 uid_t uid);
724 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
725 uid_t uid, struct passwd *pwdst,
726 char *buf, size_t buflen, struct passwd **pwdstp);
727 static void nwrap_module_setpwent(struct nwrap_backend *b);
728 static void nwrap_module_endpwent(struct nwrap_backend *b);
729 static struct group *nwrap_module_getgrent(struct nwrap_backend *b);
730 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
731 struct group *grdst, char *buf,
732 size_t buflen, struct group **grdstp);
733 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
734 const char *name);
735 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
736 const char *name, struct group *grdst,
737 char *buf, size_t buflen, struct group **grdstp);
738 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
739 gid_t gid);
740 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
741 gid_t gid, struct group *grdst,
742 char *buf, size_t buflen, struct group **grdstp);
743 static void nwrap_module_setgrent(struct nwrap_backend *b);
744 static void nwrap_module_endgrent(struct nwrap_backend *b);
745 static int nwrap_module_initgroups(struct nwrap_backend *b,
746 const char *user, gid_t group);
747 static struct hostent *nwrap_module_gethostbyaddr(struct nwrap_backend *b,
748 const void *addr,
749 socklen_t len, int type);
750 static struct hostent *nwrap_module_gethostbyname(struct nwrap_backend *b,
751 const char *name);
752 static struct hostent *nwrap_module_gethostbyname2(struct nwrap_backend *b,
753 const char *name, int af);
754 static int nwrap_module_gethostbyname2_r(struct nwrap_backend *b,
755 const char *name, int af,
756 struct hostent *hedst,
757 char *buf, size_t buflen,
758 struct hostent **hedstp);
760 struct nwrap_ops nwrap_files_ops = {
761 .nw_getpwnam = nwrap_files_getpwnam,
762 .nw_getpwnam_r = nwrap_files_getpwnam_r,
763 .nw_getpwuid = nwrap_files_getpwuid,
764 .nw_getpwuid_r = nwrap_files_getpwuid_r,
765 .nw_setpwent = nwrap_files_setpwent,
766 .nw_getpwent = nwrap_files_getpwent,
767 .nw_getpwent_r = nwrap_files_getpwent_r,
768 .nw_endpwent = nwrap_files_endpwent,
769 .nw_initgroups = nwrap_files_initgroups,
770 .nw_getgrnam = nwrap_files_getgrnam,
771 .nw_getgrnam_r = nwrap_files_getgrnam_r,
772 .nw_getgrgid = nwrap_files_getgrgid,
773 .nw_getgrgid_r = nwrap_files_getgrgid_r,
774 .nw_setgrent = nwrap_files_setgrent,
775 .nw_getgrent = nwrap_files_getgrent,
776 .nw_getgrent_r = nwrap_files_getgrent_r,
777 .nw_endgrent = nwrap_files_endgrent,
778 .nw_gethostbyaddr = nwrap_files_gethostbyaddr,
779 .nw_gethostbyname = nwrap_files_gethostbyname,
780 #ifdef HAVE_GETHOSTBYNAME2
781 .nw_gethostbyname2 = nwrap_files_gethostbyname2,
782 #endif /* HAVE_GETHOSTBYNAME2 */
783 .nw_gethostbyname2_r = nwrap_files_gethostbyname2_r,
786 struct nwrap_ops nwrap_module_ops = {
787 .nw_getpwnam = nwrap_module_getpwnam,
788 .nw_getpwnam_r = nwrap_module_getpwnam_r,
789 .nw_getpwuid = nwrap_module_getpwuid,
790 .nw_getpwuid_r = nwrap_module_getpwuid_r,
791 .nw_setpwent = nwrap_module_setpwent,
792 .nw_getpwent = nwrap_module_getpwent,
793 .nw_getpwent_r = nwrap_module_getpwent_r,
794 .nw_endpwent = nwrap_module_endpwent,
795 .nw_initgroups = nwrap_module_initgroups,
796 .nw_getgrnam = nwrap_module_getgrnam,
797 .nw_getgrnam_r = nwrap_module_getgrnam_r,
798 .nw_getgrgid = nwrap_module_getgrgid,
799 .nw_getgrgid_r = nwrap_module_getgrgid_r,
800 .nw_setgrent = nwrap_module_setgrent,
801 .nw_getgrent = nwrap_module_getgrent,
802 .nw_getgrent_r = nwrap_module_getgrent_r,
803 .nw_endgrent = nwrap_module_endgrent,
804 .nw_gethostbyaddr = nwrap_module_gethostbyaddr,
805 .nw_gethostbyname = nwrap_module_gethostbyname,
806 .nw_gethostbyname2 = nwrap_module_gethostbyname2,
807 .nw_gethostbyname2_r = nwrap_module_gethostbyname2_r,
810 struct nwrap_libc {
811 void *handle;
812 void *nsl_handle;
813 void *sock_handle;
814 struct nwrap_libc_symbols symbols;
817 struct nwrap_main {
818 size_t num_backends;
819 struct nwrap_backend *backends;
820 struct nwrap_libc *libc;
823 static struct nwrap_main *nwrap_main_global;
824 static struct nwrap_main __nwrap_main_global;
827 * PROTOTYPES
829 static int nwrap_convert_he_ai(const struct hostent *he,
830 unsigned short port,
831 const struct addrinfo *hints,
832 struct addrinfo **pai,
833 bool skip_canonname);
836 * VECTORS
839 #define DEFAULT_VECTOR_CAPACITY 16
841 struct nwrap_vector {
842 void **items;
843 size_t count;
844 size_t capacity;
847 /* Macro returns pointer to first element of vector->items array.
849 * nwrap_vector is used as a memory backend which take care of
850 * memory allocations and other stuff like memory growing.
851 * nwrap_vectors should not be considered as some abstract structures.
852 * On this level, vectors are more handy than direct realloc/malloc
853 * calls.
855 * nwrap_vector->items is array inside nwrap_vector which can be
856 * directly pointed by libc structure assembled by cwrap itself.
858 * EXAMPLE:
860 * 1) struct hostent contains char **h_addr_list element.
861 * 2) nwrap_vector holds array of pointers to addresses.
862 * It's easier to use vector to store results of
863 * file parsing etc.
865 * Now, pretend that cwrap assembled struct hostent and
866 * we need to set h_addr_list to point to nwrap_vector.
867 * Idea behind is to shield users from internal nwrap_vector
868 * implementation.
869 * (Yes, not fully - array terminated by NULL is needed because
870 * it's result expected by libc function caller.)
873 * CODE EXAMPLE:
875 * struct hostent he;
876 * struct nwrap_vector *vector = malloc(sizeof(struct nwrap_vector));
877 * ... don't care about failed allocation now ...
879 * ... fill nwrap vector ...
881 * struct hostent he;
882 * he.h_addr_list = nwrap_vector_head(vector);
885 #define nwrap_vector_head(vect) ((void *)((vect)->items))
887 #define nwrap_vector_foreach(item, vect, iter) \
888 for (iter = 0, (item) = (vect).items == NULL ? NULL : (vect).items[0]; \
889 item != NULL; \
890 (item) = (vect).items[++iter])
892 #define nwrap_vector_is_initialized(vector) ((vector)->items != NULL)
894 static inline bool nwrap_vector_init(struct nwrap_vector *const vector)
896 if (vector == NULL) {
897 return false;
900 /* count is initialized by ZERO_STRUCTP */
901 ZERO_STRUCTP(vector);
902 vector->items = malloc(sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
903 if (vector->items == NULL) {
904 return false;
906 vector->capacity = DEFAULT_VECTOR_CAPACITY;
907 memset(vector->items, '\0', sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
909 return true;
912 static bool nwrap_vector_add_item(struct nwrap_vector *vector, void *const item)
914 assert (vector != NULL);
916 if (vector->items == NULL) {
917 nwrap_vector_init(vector);
920 if (vector->count == vector->capacity) {
921 /* Items array _MUST_ be NULL terminated because it's passed
922 * as result to caller which expect NULL terminated array from libc.
924 void **items = realloc(vector->items, sizeof(void *) * ((vector->capacity * 2) + 1));
925 if (items == NULL) {
926 return false;
928 vector->items = items;
930 /* Don't count ending NULL to capacity */
931 vector->capacity *= 2;
934 vector->items[vector->count] = item;
936 vector->count += 1;
937 vector->items[vector->count] = NULL;
939 return true;
942 static bool nwrap_vector_merge(struct nwrap_vector *dst,
943 struct nwrap_vector *src)
945 void **dst_items = NULL;
946 size_t count;
948 if (src->count == 0) {
949 return true;
952 count = dst->count + src->count;
954 /* We don't need reallocation if we have enough capacity. */
955 if (src->count > (dst->capacity - dst->count)) {
956 dst_items = (void **)realloc(dst->items, (count + 1) * sizeof(void *));
957 if (dst_items == NULL) {
958 return false;
960 dst->items = dst_items;
961 dst->capacity = count;
964 memcpy((void *)(((long *)dst->items) + dst->count),
965 src->items,
966 src->count * sizeof(void *));
967 dst->count = count;
969 return true;
972 struct nwrap_cache {
973 const char *path;
974 int fd;
975 FILE *fp;
976 struct stat st;
977 void *private_data;
979 struct nwrap_vector lines;
981 bool (*parse_line)(struct nwrap_cache *, char *line);
982 void (*unload)(struct nwrap_cache *);
985 /* passwd */
986 struct nwrap_pw {
987 struct nwrap_cache *cache;
989 struct passwd *list;
990 int num;
991 int idx;
994 struct nwrap_cache __nwrap_cache_pw;
995 struct nwrap_pw nwrap_pw_global;
997 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
998 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
1000 /* shadow */
1001 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1002 struct nwrap_sp {
1003 struct nwrap_cache *cache;
1005 struct spwd *list;
1006 int num;
1007 int idx;
1010 struct nwrap_cache __nwrap_cache_sp;
1011 struct nwrap_sp nwrap_sp_global;
1013 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line);
1014 static void nwrap_sp_unload(struct nwrap_cache *nwrap);
1015 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1017 /* group */
1018 struct nwrap_gr {
1019 struct nwrap_cache *cache;
1021 struct group *list;
1022 int num;
1023 int idx;
1026 struct nwrap_cache __nwrap_cache_gr;
1027 struct nwrap_gr nwrap_gr_global;
1029 /* hosts */
1030 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line);
1031 static void nwrap_he_unload(struct nwrap_cache *nwrap);
1033 struct nwrap_addrdata {
1034 unsigned char host_addr[16]; /* IPv4 or IPv6 address */
1037 static size_t max_hostents = 100;
1039 struct nwrap_entdata {
1040 struct nwrap_addrdata addr;
1041 struct hostent ht;
1043 struct nwrap_vector nwrap_addrdata;
1045 ssize_t aliases_count;
1048 struct nwrap_entlist {
1049 struct nwrap_entlist *next;
1050 struct nwrap_entdata *ed;
1053 struct nwrap_he {
1054 struct nwrap_cache *cache;
1056 struct nwrap_vector entries;
1057 struct nwrap_vector lists;
1059 int num;
1060 int idx;
1063 static struct nwrap_cache __nwrap_cache_he;
1064 static struct nwrap_he nwrap_he_global;
1067 /*********************************************************
1068 * NWRAP PROTOTYPES
1069 *********************************************************/
1071 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
1072 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
1073 #if ! defined(HAVE_CONSTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_INIT)
1074 /* xlC and other oldschool compilers support (only) this */
1075 #pragma init (nwrap_constructor)
1076 #endif
1077 void nwrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
1078 #if ! defined(HAVE_DESTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_FINI)
1079 #pragma fini (nwrap_destructor)
1080 #endif
1081 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
1083 /*********************************************************
1084 * NWRAP LIBC LOADER FUNCTIONS
1085 *********************************************************/
1087 enum nwrap_lib {
1088 NWRAP_LIBC,
1089 NWRAP_LIBNSL,
1090 NWRAP_LIBSOCKET,
1093 static const char *nwrap_str_lib(enum nwrap_lib lib)
1095 switch (lib) {
1096 case NWRAP_LIBC:
1097 return "libc";
1098 case NWRAP_LIBNSL:
1099 return "libnsl";
1100 case NWRAP_LIBSOCKET:
1101 return "libsocket";
1104 /* Compiler would warn us about unhandled enum value if we get here */
1105 return "unknown";
1108 static void *nwrap_load_lib_handle(enum nwrap_lib lib)
1110 int flags = RTLD_LAZY;
1111 void *handle = NULL;
1112 int i;
1114 #ifdef RTLD_DEEPBIND
1115 const char *env_preload = getenv("LD_PRELOAD");
1116 const char *env_deepbind = getenv("NSS_WRAPPER_DISABLE_DEEPBIND");
1117 bool enable_deepbind = true;
1119 /* Don't do a deepbind if we run with libasan */
1120 if (env_preload != NULL && strlen(env_preload) < 1024) {
1121 const char *p = strstr(env_preload, "libasan.so");
1122 if (p != NULL) {
1123 enable_deepbind = false;
1127 if (env_deepbind != NULL && strlen(env_deepbind) >= 1) {
1128 enable_deepbind = false;
1131 if (enable_deepbind) {
1132 flags |= RTLD_DEEPBIND;
1134 #endif
1136 switch (lib) {
1137 case NWRAP_LIBNSL:
1138 #ifdef HAVE_LIBNSL
1139 handle = nwrap_main_global->libc->nsl_handle;
1140 if (handle == NULL) {
1141 for (i = 10; i >= 0; i--) {
1142 char soname[256] = {0};
1144 snprintf(soname, sizeof(soname), "libnsl.so.%d", i);
1145 handle = dlopen(soname, flags);
1146 if (handle != NULL) {
1147 break;
1151 nwrap_main_global->libc->nsl_handle = handle;
1153 break;
1154 #endif
1155 /* FALL TROUGH */
1156 case NWRAP_LIBSOCKET:
1157 #ifdef HAVE_LIBSOCKET
1158 handle = nwrap_main_global->libc->sock_handle;
1159 if (handle == NULL) {
1160 for (i = 10; i >= 0; i--) {
1161 char soname[256] = {0};
1163 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
1164 handle = dlopen(soname, flags);
1165 if (handle != NULL) {
1166 break;
1170 nwrap_main_global->libc->sock_handle = handle;
1172 break;
1173 #endif
1174 /* FALL TROUGH */
1175 case NWRAP_LIBC:
1176 handle = nwrap_main_global->libc->handle;
1177 if (handle == NULL) {
1178 for (i = 10; i >= 0; i--) {
1179 char soname[256] = {0};
1181 snprintf(soname, sizeof(soname), "libc.so.%d", i);
1182 handle = dlopen(soname, flags);
1183 if (handle != NULL) {
1184 break;
1188 nwrap_main_global->libc->handle = handle;
1190 break;
1193 if (handle == NULL) {
1194 #ifdef RTLD_NEXT
1195 handle = nwrap_main_global->libc->handle
1196 = nwrap_main_global->libc->sock_handle
1197 = nwrap_main_global->libc->nsl_handle
1198 = RTLD_NEXT;
1199 #else
1200 NWRAP_LOG(NWRAP_LOG_ERROR,
1201 "Failed to dlopen library: %s\n",
1202 dlerror());
1203 exit(-1);
1204 #endif
1207 return handle;
1210 static void *_nwrap_bind_symbol(enum nwrap_lib lib, const char *fn_name)
1212 void *handle;
1213 void *func;
1215 nwrap_init();
1217 handle = nwrap_load_lib_handle(lib);
1219 func = dlsym(handle, fn_name);
1220 if (func == NULL) {
1221 NWRAP_LOG(NWRAP_LOG_ERROR,
1222 "Failed to find %s: %s\n",
1223 fn_name, dlerror());
1224 exit(-1);
1227 NWRAP_LOG(NWRAP_LOG_TRACE,
1228 "Loaded %s from %s",
1229 fn_name, nwrap_str_lib(lib));
1230 return func;
1233 #define nwrap_bind_symbol_libc(sym_name) \
1234 NWRAP_LOCK(libc_symbol_binding); \
1235 if (nwrap_main_global->libc->symbols._libc_##sym_name.obj == NULL) { \
1236 nwrap_main_global->libc->symbols._libc_##sym_name.obj = \
1237 _nwrap_bind_symbol(NWRAP_LIBC, #sym_name); \
1239 NWRAP_UNLOCK(libc_symbol_binding)
1241 #define nwrap_bind_symbol_libc_posix(sym_name) \
1242 NWRAP_LOCK(libc_symbol_binding); \
1243 if (nwrap_main_global->libc->symbols._libc_##sym_name.obj == NULL) { \
1244 nwrap_main_global->libc->symbols._libc_##sym_name.obj = \
1245 _nwrap_bind_symbol(NWRAP_LIBC, "__posix_" #sym_name); \
1247 NWRAP_UNLOCK(libc_symbol_binding)
1249 #define nwrap_bind_symbol_libnsl(sym_name) \
1250 NWRAP_LOCK(libc_symbol_binding); \
1251 if (nwrap_main_global->libc->symbols._libc_##sym_name.obj == NULL) { \
1252 nwrap_main_global->libc->symbols._libc_##sym_name.obj = \
1253 _nwrap_bind_symbol(NWRAP_LIBNSL, #sym_name); \
1255 NWRAP_UNLOCK(libc_symbol_binding)
1257 #define nwrap_bind_symbol_libsocket(sym_name) \
1258 NWRAP_LOCK(libc_symbol_binding); \
1259 if (nwrap_main_global->libc->symbols._libc_##sym_name.obj == NULL) { \
1260 nwrap_main_global->libc->symbols._libc_##sym_name.obj = \
1261 _nwrap_bind_symbol(NWRAP_LIBSOCKET, #sym_name); \
1263 NWRAP_UNLOCK(libc_symbol_binding)
1265 /* INTERNAL HELPER FUNCTIONS */
1266 static void nwrap_lines_unload(struct nwrap_cache *const nwrap)
1268 size_t p;
1269 void *item;
1270 nwrap_vector_foreach(item, nwrap->lines, p) {
1271 /* Maybe some vectors were merged ... */
1272 SAFE_FREE(item);
1274 SAFE_FREE(nwrap->lines.items);
1275 ZERO_STRUCTP(&nwrap->lines);
1279 * IMPORTANT
1281 * Functions expeciall from libc need to be loaded individually, you can't load
1282 * all at once or gdb will segfault at startup. The same applies to valgrind and
1283 * has probably something todo with with the linker.
1284 * So we need load each function at the point it is called the first time.
1286 static struct passwd *libc_getpwnam(const char *name)
1288 nwrap_bind_symbol_libc(getpwnam);
1290 return nwrap_main_global->libc->symbols._libc_getpwnam.f(name);
1293 #ifdef HAVE_GETPWNAM_R
1294 static int libc_getpwnam_r(const char *name,
1295 struct passwd *pwd,
1296 char *buf,
1297 size_t buflen,
1298 struct passwd **result)
1300 #ifdef HAVE___POSIX_GETPWNAM_R
1301 nwrap_bind_symbol_libc_posix(getpwnam_r);
1302 #else
1303 nwrap_bind_symbol_libc(getpwnam_r);
1304 #endif
1306 return nwrap_main_global->libc->symbols._libc_getpwnam_r.f(name,
1307 pwd,
1308 buf,
1309 buflen,
1310 result);
1312 #endif
1314 static struct passwd *libc_getpwuid(uid_t uid)
1316 nwrap_bind_symbol_libc(getpwuid);
1318 return nwrap_main_global->libc->symbols._libc_getpwuid.f(uid);
1321 #ifdef HAVE_GETPWUID_R
1322 static int libc_getpwuid_r(uid_t uid,
1323 struct passwd *pwd,
1324 char *buf,
1325 size_t buflen,
1326 struct passwd **result)
1328 #ifdef HAVE___POSIX_GETPWUID_R
1329 nwrap_bind_symbol_libc_posix(getpwuid_r);
1330 #else
1331 nwrap_bind_symbol_libc(getpwuid_r);
1332 #endif
1334 return nwrap_main_global->libc->symbols._libc_getpwuid_r.f(uid,
1335 pwd,
1336 buf,
1337 buflen,
1338 result);
1340 #endif
1342 static inline void str_tolower(char *dst, char *src)
1344 register char *src_tmp = src;
1345 register char *dst_tmp = dst;
1347 while (*src_tmp != '\0') {
1348 *dst_tmp = tolower(*src_tmp);
1349 ++src_tmp;
1350 ++dst_tmp;
1354 static bool str_tolower_copy(char **dst_name, const char *const src_name)
1356 char *h_name_lower;
1358 if ((dst_name == NULL) || (src_name == NULL)) {
1359 return false;
1362 h_name_lower = strdup(src_name);
1363 if (h_name_lower == NULL) {
1364 NWRAP_LOG(NWRAP_LOG_DEBUG, "Out of memory while strdup");
1365 return false;
1368 str_tolower(h_name_lower, h_name_lower);
1369 *dst_name = h_name_lower;
1370 return true;
1373 static void libc_setpwent(void)
1375 nwrap_bind_symbol_libc(setpwent);
1377 nwrap_main_global->libc->symbols._libc_setpwent.f();
1380 static struct passwd *libc_getpwent(void)
1382 nwrap_bind_symbol_libc(getpwent);
1384 return nwrap_main_global->libc->symbols._libc_getpwent.f();
1387 #ifdef HAVE_GETPWENT_R
1388 # ifdef HAVE_SOLARIS_GETPWENT_R
1389 static struct passwd *libc_getpwent_r(struct passwd *pwdst,
1390 char *buf,
1391 int buflen)
1393 nwrap_bind_symbol_libc(getpwent_r);
1395 return nwrap_main_global->libc->symbols._libc_getpwent_r.f(pwdst,
1396 buf,
1397 buflen);
1399 # else /* HAVE_SOLARIS_GETPWENT_R */
1400 static int libc_getpwent_r(struct passwd *pwdst,
1401 char *buf,
1402 size_t buflen,
1403 struct passwd **pwdstp)
1405 nwrap_bind_symbol_libc(getpwent_r);
1407 return nwrap_main_global->libc->symbols._libc_getpwent_r.f(pwdst,
1408 buf,
1409 buflen,
1410 pwdstp);
1412 # endif /* HAVE_SOLARIS_GETPWENT_R */
1413 #endif /* HAVE_GETPWENT_R */
1415 static void libc_endpwent(void)
1417 nwrap_bind_symbol_libc(endpwent);
1419 nwrap_main_global->libc->symbols._libc_endpwent.f();
1422 static int libc_initgroups(const char *user, gid_t gid)
1424 nwrap_bind_symbol_libc(initgroups);
1426 return nwrap_main_global->libc->symbols._libc_initgroups.f(user, gid);
1429 static struct group *libc_getgrnam(const char *name)
1431 nwrap_bind_symbol_libc(getgrnam);
1433 return nwrap_main_global->libc->symbols._libc_getgrnam.f(name);
1436 #ifdef HAVE_GETGRNAM_R
1437 static int libc_getgrnam_r(const char *name,
1438 struct group *grp,
1439 char *buf,
1440 size_t buflen,
1441 struct group **result)
1443 #ifdef HAVE___POSIX_GETGRNAM_R
1444 nwrap_bind_symbol_libc_posix(getgrnam_r);
1445 #else
1446 nwrap_bind_symbol_libc(getgrnam_r);
1447 #endif
1449 return nwrap_main_global->libc->symbols._libc_getgrnam_r.f(name,
1450 grp,
1451 buf,
1452 buflen,
1453 result);
1455 #endif
1457 static struct group *libc_getgrgid(gid_t gid)
1459 nwrap_bind_symbol_libc(getgrgid);
1461 return nwrap_main_global->libc->symbols._libc_getgrgid.f(gid);
1464 #ifdef HAVE_GETGRGID_R
1465 static int libc_getgrgid_r(gid_t gid,
1466 struct group *grp,
1467 char *buf,
1468 size_t buflen,
1469 struct group **result)
1471 #ifdef HAVE___POSIX_GETGRGID_R
1472 if (nwrap_main_global->libc->symbols._libc_getgrgid_r == NULL) {
1473 *(void **) (&nwrap_main_global->libc->symbols._libc_getgrgid_r) =
1474 _nwrap_bind_symbol_libc("__posix_getgrgid_r");
1476 #else
1477 nwrap_bind_symbol_libc(getgrgid_r);
1478 #endif
1480 return nwrap_main_global->libc->symbols._libc_getgrgid_r.f(gid,
1481 grp,
1482 buf,
1483 buflen,
1484 result);
1486 #endif
1488 static void libc_setgrent(void)
1490 nwrap_bind_symbol_libc(setgrent);
1492 nwrap_main_global->libc->symbols._libc_setgrent.f();
1495 static struct group *libc_getgrent(void)
1497 nwrap_bind_symbol_libc(getgrent);
1499 return nwrap_main_global->libc->symbols._libc_getgrent.f();
1502 #ifdef HAVE_GETGRENT_R
1503 # ifdef HAVE_SOLARIS_GETGRENT_R
1504 static struct group *libc_getgrent_r(struct group *group,
1505 char *buf,
1506 size_t buflen)
1508 nwrap_bind_symbol_libc(getgrent_r);
1510 return nwrap_main_global->libc->symbols._libc_getgrent_r.f(group,
1511 buf,
1512 buflen);
1514 # else /* HAVE_SOLARIS_GETGRENT_R */
1515 static int libc_getgrent_r(struct group *group,
1516 char *buf,
1517 size_t buflen,
1518 struct group **result)
1520 nwrap_bind_symbol_libc(getgrent_r);
1522 return nwrap_main_global->libc->symbols._libc_getgrent_r.f(group,
1523 buf,
1524 buflen,
1525 result);
1527 # endif /* HAVE_SOLARIS_GETGRENT_R */
1528 #endif /* HAVE_GETGRENT_R */
1530 static void libc_endgrent(void)
1532 nwrap_bind_symbol_libc(endgrent);
1534 nwrap_main_global->libc->symbols._libc_endgrent.f();
1537 #ifdef HAVE_GETGROUPLIST
1538 static int libc_getgrouplist(const char *user,
1539 gid_t group,
1540 gid_t *groups,
1541 int *ngroups)
1543 nwrap_bind_symbol_libc(getgrouplist);
1545 return nwrap_main_global->libc->symbols._libc_getgrouplist.f(user,
1546 group,
1547 groups,
1548 ngroups);
1550 #endif
1552 static void libc_sethostent(int stayopen)
1554 nwrap_bind_symbol_libnsl(sethostent);
1556 nwrap_main_global->libc->symbols._libc_sethostent.f(stayopen);
1559 static struct hostent *libc_gethostent(void)
1561 nwrap_bind_symbol_libnsl(gethostent);
1563 return nwrap_main_global->libc->symbols._libc_gethostent.f();
1566 static void libc_endhostent(void)
1568 nwrap_bind_symbol_libnsl(endhostent);
1570 nwrap_main_global->libc->symbols._libc_endhostent.f();
1573 static struct hostent *libc_gethostbyname(const char *name)
1575 nwrap_bind_symbol_libnsl(gethostbyname);
1577 return nwrap_main_global->libc->symbols._libc_gethostbyname.f(name);
1580 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
1581 static struct hostent *libc_gethostbyname2(const char *name, int af)
1583 nwrap_bind_symbol_libnsl(gethostbyname2);
1585 return nwrap_main_global->libc->symbols._libc_gethostbyname2.f(name, af);
1587 #endif
1589 #ifdef HAVE_GETHOSTBYNAME2_R /* GNU extension */
1590 static int libc_gethostbyname2_r(const char *name,
1591 int af,
1592 struct hostent *ret,
1593 char *buf,
1594 size_t buflen,
1595 struct hostent **result,
1596 int *h_errnop)
1598 nwrap_bind_symbol_libnsl(gethostbyname2_r);
1600 return nwrap_main_global->libc->symbols._libc_gethostbyname2_r.f(name,
1602 ret,
1603 buf,
1604 buflen,
1605 result,
1606 h_errnop);
1608 #endif
1610 static struct hostent *libc_gethostbyaddr(const void *addr,
1611 socklen_t len,
1612 int type)
1614 nwrap_bind_symbol_libnsl(gethostbyaddr);
1616 return nwrap_main_global->libc->symbols._libc_gethostbyaddr.f(addr,
1617 len,
1618 type);
1621 static int libc_gethostname(char *name, size_t len)
1623 nwrap_bind_symbol_libnsl(gethostname);
1625 return nwrap_main_global->libc->symbols._libc_gethostname.f(name, len);
1628 #ifdef HAVE_GETHOSTBYNAME_R
1629 static int libc_gethostbyname_r(const char *name,
1630 struct hostent *ret,
1631 char *buf,
1632 size_t buflen,
1633 struct hostent **result,
1634 int *h_errnop)
1636 nwrap_bind_symbol_libnsl(gethostbyname_r);
1638 return nwrap_main_global->libc->symbols._libc_gethostbyname_r.f(name,
1639 ret,
1640 buf,
1641 buflen,
1642 result,
1643 h_errnop);
1645 #endif
1647 #ifdef HAVE_GETHOSTBYADDR_R
1648 static int libc_gethostbyaddr_r(const void *addr,
1649 socklen_t len,
1650 int type,
1651 struct hostent *ret,
1652 char *buf,
1653 size_t buflen,
1654 struct hostent **result,
1655 int *h_errnop)
1657 nwrap_bind_symbol_libnsl(gethostbyaddr_r);
1659 return nwrap_main_global->libc->symbols._libc_gethostbyaddr_r.f(addr,
1660 len,
1661 type,
1662 ret,
1663 buf,
1664 buflen,
1665 result,
1666 h_errnop);
1668 #endif
1670 static int libc_getaddrinfo(const char *node,
1671 const char *service,
1672 const struct addrinfo *hints,
1673 struct addrinfo **res)
1675 nwrap_bind_symbol_libsocket(getaddrinfo);
1677 return nwrap_main_global->libc->symbols._libc_getaddrinfo.f(node,
1678 service,
1679 hints,
1680 res);
1683 static int libc_getnameinfo(const struct sockaddr *sa,
1684 socklen_t salen,
1685 char *host,
1686 size_t hostlen,
1687 char *serv,
1688 size_t servlen,
1689 int flags)
1691 nwrap_bind_symbol_libsocket(getnameinfo);
1693 return nwrap_main_global->libc->symbols._libc_getnameinfo.f(sa,
1694 salen,
1695 host,
1696 hostlen,
1697 serv,
1698 servlen,
1699 flags);
1702 /*********************************************************
1703 * NWRAP NSS MODULE LOADER FUNCTIONS
1704 *********************************************************/
1706 static void *_nwrap_bind_nss_module_symbol(struct nwrap_backend *b,
1707 const char *fn_name)
1709 void *res = NULL;
1710 char *s = NULL;
1711 int rc;
1713 if (b->so_handle == NULL) {
1714 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1715 return NULL;
1718 rc = asprintf(&s, "_nss_%s_%s", b->name, fn_name);
1719 if (rc == -1) {
1720 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1721 return NULL;
1724 res = dlsym(b->so_handle, s);
1725 if (res == NULL) {
1726 NWRAP_LOG(NWRAP_LOG_WARN,
1727 "Cannot find function %s in %s",
1728 s, b->so_path);
1730 SAFE_FREE(s);
1731 return res;
1734 #define nwrap_nss_module_bind_symbol(sym_name) \
1735 NWRAP_LOCK(nss_module_symbol_binding); \
1736 if (symbols->_nss_##sym_name.obj == NULL) { \
1737 symbols->_nss_##sym_name.obj = \
1738 _nwrap_bind_nss_module_symbol(b, #sym_name); \
1740 NWRAP_UNLOCK(nss_module_symbol_binding)
1742 #define nwrap_nss_module_bind_symbol2(sym_name, alt_name) \
1743 NWRAP_LOCK(nss_module_symbol_binding); \
1744 if (symbols->_nss_##sym_name.obj == NULL) { \
1745 symbols->_nss_##sym_name.obj = \
1746 _nwrap_bind_nss_module_symbol(b, #alt_name); \
1748 NWRAP_UNLOCK(nss_module_symbol_binding)
1750 static struct nwrap_nss_module_symbols *
1751 nwrap_bind_nss_module_symbols(struct nwrap_backend *b)
1753 struct nwrap_nss_module_symbols *symbols;
1755 if (!b->so_handle) {
1756 return NULL;
1759 symbols = calloc(1, sizeof(struct nwrap_nss_module_symbols));
1760 if (symbols == NULL) {
1761 return NULL;
1764 nwrap_nss_module_bind_symbol(getpwnam_r);
1765 nwrap_nss_module_bind_symbol(getpwuid_r);
1766 nwrap_nss_module_bind_symbol(setpwent);
1767 nwrap_nss_module_bind_symbol(getpwent_r);
1768 nwrap_nss_module_bind_symbol(endpwent);
1769 nwrap_nss_module_bind_symbol2(initgroups, initgroups_dyn);
1770 nwrap_nss_module_bind_symbol(getgrnam_r);
1771 nwrap_nss_module_bind_symbol(getgrgid_r);
1772 nwrap_nss_module_bind_symbol(setgrent);
1773 nwrap_nss_module_bind_symbol(getgrent_r);
1774 nwrap_nss_module_bind_symbol(endgrent);
1775 nwrap_nss_module_bind_symbol(gethostbyaddr_r);
1776 nwrap_nss_module_bind_symbol(gethostbyname2_r);
1778 return symbols;
1781 static void *nwrap_load_module(const char *so_path)
1783 void *h;
1785 if (!so_path || !strlen(so_path)) {
1786 return NULL;
1789 h = dlopen(so_path, RTLD_LAZY);
1790 if (!h) {
1791 NWRAP_LOG(NWRAP_LOG_ERROR,
1792 "Cannot open shared library %s",
1793 so_path);
1794 return NULL;
1797 return h;
1800 static bool nwrap_module_init(const char *name,
1801 struct nwrap_ops *ops,
1802 const char *so_path,
1803 size_t *num_backends,
1804 struct nwrap_backend **backends)
1806 struct nwrap_backend *b = NULL;
1807 size_t n = *num_backends + 1;
1809 b = realloc(*backends, sizeof(struct nwrap_backend) * n);
1810 if (b == NULL) {
1811 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1812 return false;
1814 *backends = b;
1816 b = &((*backends)[*num_backends]);
1818 *b = (struct nwrap_backend) {
1819 .name = name,
1820 .ops = ops,
1821 .so_path = so_path,
1824 if (so_path != NULL) {
1825 b->so_handle = nwrap_load_module(so_path);
1826 b->symbols = nwrap_bind_nss_module_symbols(b);
1827 if (b->symbols == NULL) {
1828 return false;
1832 *num_backends = n;
1834 return true;
1837 static void nwrap_libc_init(struct nwrap_main *r)
1839 r->libc = calloc(1, sizeof(struct nwrap_libc));
1840 if (r->libc == NULL) {
1841 printf("Failed to allocate memory for libc");
1842 exit(-1);
1846 static void nwrap_backend_init(struct nwrap_main *r)
1848 const char *module_so_path = getenv("NSS_WRAPPER_MODULE_SO_PATH");
1849 const char *module_fn_name = getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1851 r->num_backends = 0;
1852 r->backends = NULL;
1854 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1855 &r->num_backends,
1856 &r->backends)) {
1857 NWRAP_LOG(NWRAP_LOG_ERROR,
1858 "Failed to initialize 'files' backend");
1859 return;
1862 if (module_so_path != NULL &&
1863 module_so_path[0] != '\0' &&
1864 module_fn_name != NULL &&
1865 module_fn_name[0] != '\0') {
1866 if (!nwrap_module_init(module_fn_name,
1867 &nwrap_module_ops,
1868 module_so_path,
1869 &r->num_backends,
1870 &r->backends)) {
1871 NWRAP_LOG(NWRAP_LOG_ERROR,
1872 "Failed to initialize '%s' backend",
1873 module_fn_name);
1874 return;
1879 static void nwrap_init(void)
1881 const char *env;
1882 char *endptr;
1883 size_t max_hostents_tmp;
1884 int ok;
1886 NWRAP_LOCK(nwrap_initialized);
1887 if (nwrap_initialized) {
1888 NWRAP_UNLOCK(nwrap_initialized);
1889 return;
1893 * Still holding nwrap_initialized lock here.
1894 * We don't use NWRAP_(UN)LOCK_ALL macros here because we
1895 * want to avoid overhead when other threads do their job.
1897 NWRAP_LOCK(nwrap_global);
1898 NWRAP_LOCK(nwrap_gr_global);
1899 NWRAP_LOCK(nwrap_he_global);
1900 NWRAP_LOCK(nwrap_pw_global);
1901 NWRAP_LOCK(nwrap_sp_global);
1903 nwrap_initialized = true;
1905 env = getenv("NSS_WRAPPER_MAX_HOSTENTS");
1906 if (env != NULL) {
1907 max_hostents_tmp = (size_t)strtoul(env, &endptr, 10);
1908 if ((*env == '\0') ||
1909 (*endptr != '\0') ||
1910 (max_hostents_tmp == 0)) {
1911 NWRAP_LOG(NWRAP_LOG_DEBUG,
1912 "Error parsing NSS_WRAPPER_MAX_HOSTENTS "
1913 "value or value is too small. "
1914 "Using default value: %lu.",
1915 (unsigned long)max_hostents);
1916 } else {
1917 max_hostents = max_hostents_tmp;
1920 /* Initialize hash table */
1921 NWRAP_LOG(NWRAP_LOG_DEBUG,
1922 "Initializing hash table of size %lu items.",
1923 (unsigned long)max_hostents);
1924 ok = hcreate(max_hostents);
1925 if (!ok) {
1926 NWRAP_LOG(NWRAP_LOG_ERROR,
1927 "Failed to initialize hash table");
1928 exit(-1);
1931 nwrap_main_global = &__nwrap_main_global;
1933 nwrap_libc_init(nwrap_main_global);
1935 nwrap_backend_init(nwrap_main_global);
1937 /* passwd */
1938 nwrap_pw_global.cache = &__nwrap_cache_pw;
1940 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
1941 nwrap_pw_global.cache->fp = NULL;
1942 nwrap_pw_global.cache->fd = -1;
1943 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
1944 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
1945 nwrap_pw_global.cache->unload = nwrap_pw_unload;
1947 /* shadow */
1948 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1949 nwrap_sp_global.cache = &__nwrap_cache_sp;
1951 nwrap_sp_global.cache->path = getenv("NSS_WRAPPER_SHADOW");
1952 nwrap_sp_global.cache->fp = NULL;
1953 nwrap_sp_global.cache->fd = -1;
1954 nwrap_sp_global.cache->private_data = &nwrap_sp_global;
1955 nwrap_sp_global.cache->parse_line = nwrap_sp_parse_line;
1956 nwrap_sp_global.cache->unload = nwrap_sp_unload;
1957 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1959 /* group */
1960 nwrap_gr_global.cache = &__nwrap_cache_gr;
1962 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
1963 nwrap_gr_global.cache->fp = NULL;
1964 nwrap_gr_global.cache->fd = -1;
1965 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
1966 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
1967 nwrap_gr_global.cache->unload = nwrap_gr_unload;
1969 /* hosts */
1970 nwrap_he_global.cache = &__nwrap_cache_he;
1972 nwrap_he_global.cache->path = getenv("NSS_WRAPPER_HOSTS");
1973 nwrap_he_global.cache->fp = NULL;
1974 nwrap_he_global.cache->fd = -1;
1975 nwrap_he_global.cache->private_data = &nwrap_he_global;
1976 nwrap_he_global.cache->parse_line = nwrap_he_parse_line;
1977 nwrap_he_global.cache->unload = nwrap_he_unload;
1979 /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1980 NWRAP_UNLOCK_ALL;
1983 bool nss_wrapper_enabled(void)
1985 nwrap_init();
1987 if (nwrap_pw_global.cache->path == NULL ||
1988 nwrap_pw_global.cache->path[0] == '\0') {
1989 return false;
1991 if (nwrap_gr_global.cache->path == NULL ||
1992 nwrap_gr_global.cache->path[0] == '\0') {
1993 return false;
1996 return true;
1999 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2000 bool nss_wrapper_shadow_enabled(void)
2002 nwrap_init();
2004 if (nwrap_sp_global.cache->path == NULL ||
2005 nwrap_sp_global.cache->path[0] == '\0') {
2006 return false;
2009 return true;
2011 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2013 bool nss_wrapper_hosts_enabled(void)
2015 nwrap_init();
2017 if (nwrap_he_global.cache->path == NULL ||
2018 nwrap_he_global.cache->path[0] == '\0') {
2019 return false;
2022 return true;
2025 static bool nwrap_hostname_enabled(void)
2027 nwrap_init();
2029 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
2030 return false;
2033 return true;
2036 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
2038 char *line = NULL;
2039 ssize_t n;
2040 /* Unused but getline needs it */
2041 size_t len;
2042 bool ok;
2044 if (nwrap->st.st_size == 0) {
2045 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
2046 return true;
2049 /* Support for 32-bit system I guess */
2050 if (nwrap->st.st_size > INT32_MAX) {
2051 NWRAP_LOG(NWRAP_LOG_ERROR,
2052 "Size[%u] larger than INT32_MAX",
2053 (unsigned)nwrap->st.st_size);
2054 return false;
2057 rewind(nwrap->fp);
2059 do {
2060 n = getline(&line, &len, nwrap->fp);
2061 if (n < 0) {
2062 SAFE_FREE(line);
2063 if (feof(nwrap->fp)) {
2064 break;
2067 NWRAP_LOG(NWRAP_LOG_ERROR,
2068 "Unable to read line from file: %s",
2069 nwrap->path);
2070 return false;
2073 if (line[n - 1] == '\n') {
2074 line[n - 1] = '\0';
2077 if (line[0] == '\0') {
2078 SAFE_FREE(line);
2079 continue;
2082 ok = nwrap->parse_line(nwrap, line);
2083 if (!ok) {
2084 NWRAP_LOG(NWRAP_LOG_ERROR,
2085 "Unable to parse line file: %s",
2086 line);
2087 SAFE_FREE(line);
2088 return false;
2091 /* Line is parsed without issues so add it to list */
2092 ok = nwrap_vector_add_item(&(nwrap->lines), (void *const) line);
2093 if (!ok) {
2094 NWRAP_LOG(NWRAP_LOG_ERROR,
2095 "Unable to add line to vector");
2096 return false;
2099 /* This forces getline to allocate new memory for line. */
2100 line = NULL;
2101 } while (!feof(nwrap->fp));
2103 return true;
2106 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
2108 nwrap->unload(nwrap);
2110 nwrap_lines_unload(nwrap);
2113 static bool nwrap_files_cache_reload(struct nwrap_cache *nwrap)
2115 struct stat st;
2116 int ret;
2117 bool ok;
2118 bool retried = false;
2120 assert(nwrap != NULL);
2122 reopen:
2123 if (nwrap->fd < 0) {
2124 nwrap->fp = fopen(nwrap->path, "re");
2125 if (nwrap->fp == NULL) {
2126 nwrap->fd = -1;
2127 NWRAP_LOG(NWRAP_LOG_ERROR,
2128 "Unable to open '%s' readonly %d:%s",
2129 nwrap->path, nwrap->fd,
2130 strerror(errno));
2131 return false;
2134 nwrap->fd = fileno(nwrap->fp);
2135 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
2138 ret = fstat(nwrap->fd, &st);
2139 if (ret != 0 && errno == EBADF && retried == false) {
2140 /* maybe something closed the fd on our behalf */
2141 NWRAP_LOG(NWRAP_LOG_TRACE,
2142 "fstat(%s) - %d:%s - reopen",
2143 nwrap->path,
2144 ret,
2145 strerror(errno));
2146 retried = true;
2147 memset(&nwrap->st, 0, sizeof(nwrap->st));
2148 fclose(nwrap->fp);
2149 nwrap->fp = NULL;
2150 nwrap->fd = -1;
2151 goto reopen;
2153 else if (ret != 0) {
2154 NWRAP_LOG(NWRAP_LOG_ERROR,
2155 "fstat(%s) - %d:%s",
2156 nwrap->path,
2157 ret,
2158 strerror(errno));
2159 fclose(nwrap->fp);
2160 nwrap->fp = NULL;
2161 nwrap->fd = -1;
2162 return false;
2165 if (retried == false && st.st_nlink == 0) {
2166 /* maybe someone has replaced the file... */
2167 NWRAP_LOG(NWRAP_LOG_TRACE,
2168 "st_nlink == 0, reopen %s",
2169 nwrap->path);
2170 retried = true;
2171 memset(&nwrap->st, 0, sizeof(nwrap->st));
2172 fclose(nwrap->fp);
2173 nwrap->fp = NULL;
2174 nwrap->fd = -1;
2175 goto reopen;
2178 if (st.st_mtime == nwrap->st.st_mtime) {
2179 NWRAP_LOG(NWRAP_LOG_TRACE,
2180 "st_mtime[%u] hasn't changed, skip reload",
2181 (unsigned)st.st_mtime);
2182 return true;
2185 NWRAP_LOG(NWRAP_LOG_TRACE,
2186 "st_mtime has changed [%u] => [%u], start reload",
2187 (unsigned)st.st_mtime,
2188 (unsigned)nwrap->st.st_mtime);
2190 nwrap->st = st;
2192 nwrap_files_cache_unload(nwrap);
2194 ok = nwrap_parse_file(nwrap);
2195 if (!ok) {
2196 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
2197 nwrap_files_cache_unload(nwrap);
2198 return false;
2201 NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
2202 return true;
2206 * the caller has to call nwrap_unload() on failure
2208 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
2210 struct nwrap_pw *nwrap_pw;
2211 char *c;
2212 char *p;
2213 char *e;
2214 struct passwd *pw;
2215 size_t list_size;
2217 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
2219 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
2220 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
2221 if (!pw) {
2222 NWRAP_LOG(NWRAP_LOG_ERROR,
2223 "realloc(%u) failed",
2224 (unsigned)list_size);
2225 return false;
2227 nwrap_pw->list = pw;
2229 pw = &nwrap_pw->list[nwrap_pw->num];
2231 c = line;
2233 /* name */
2234 p = strchr(c, ':');
2235 if (!p) {
2236 NWRAP_LOG(NWRAP_LOG_ERROR,
2237 "Invalid line[%s]: '%s'",
2238 line,
2240 return false;
2242 *p = '\0';
2243 p++;
2244 pw->pw_name = c;
2245 c = p;
2247 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
2249 /* password */
2250 p = strchr(c, ':');
2251 if (!p) {
2252 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2253 return false;
2255 *p = '\0';
2256 p++;
2257 pw->pw_passwd = c;
2258 c = p;
2260 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
2262 /* uid */
2263 p = strchr(c, ':');
2264 if (!p) {
2265 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2266 return false;
2268 *p = '\0';
2269 p++;
2270 e = NULL;
2271 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
2272 if (c == e) {
2273 NWRAP_LOG(NWRAP_LOG_ERROR,
2274 "Invalid line[%s]: '%s' - %s",
2275 line, c, strerror(errno));
2276 return false;
2278 if (e == NULL) {
2279 NWRAP_LOG(NWRAP_LOG_ERROR,
2280 "Invalid line[%s]: '%s' - %s",
2281 line, c, strerror(errno));
2282 return false;
2284 if (e[0] != '\0') {
2285 NWRAP_LOG(NWRAP_LOG_ERROR,
2286 "Invalid line[%s]: '%s' - %s",
2287 line, c, strerror(errno));
2288 return false;
2290 c = p;
2292 NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
2294 /* gid */
2295 p = strchr(c, ':');
2296 if (!p) {
2297 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2298 return false;
2300 *p = '\0';
2301 p++;
2302 e = NULL;
2303 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
2304 if (c == e) {
2305 NWRAP_LOG(NWRAP_LOG_ERROR,
2306 "Invalid line[%s]: '%s' - %s",
2307 line, c, strerror(errno));
2308 return false;
2310 if (e == NULL) {
2311 NWRAP_LOG(NWRAP_LOG_ERROR,
2312 "Invalid line[%s]: '%s' - %s",
2313 line, c, strerror(errno));
2314 return false;
2316 if (e[0] != '\0') {
2317 NWRAP_LOG(NWRAP_LOG_ERROR,
2318 "Invalid line[%s]: '%s' - %s",
2319 line, c, strerror(errno));
2320 return false;
2322 c = p;
2324 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]\n", pw->pw_gid);
2326 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
2327 pw->pw_class = discard_const_p(char, "");
2329 NWRAP_LOG(NWRAP_LOG_TRACE, "class[%s]", pw->pw_class);
2330 #endif /* HAVE_STRUCT_PASSWD_PW_CLASS */
2332 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
2333 pw->pw_change = 0;
2335 NWRAP_LOG(NWRAP_LOG_TRACE,
2336 "change[%lu]",
2337 (unsigned long)pw->pw_change);
2338 #endif /* HAVE_STRUCT_PASSWD_PW_CHANGE */
2340 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
2341 pw->pw_expire = 0;
2343 NWRAP_LOG(NWRAP_LOG_TRACE,
2344 "expire[%lu]",
2345 (unsigned long)pw->pw_expire);
2346 #endif /* HAVE_STRUCT_PASSWD_PW_EXPIRE */
2348 /* gecos */
2349 p = strchr(c, ':');
2350 if (!p) {
2351 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
2352 return false;
2354 *p = '\0';
2355 p++;
2356 pw->pw_gecos = c;
2357 c = p;
2359 NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
2361 /* dir */
2362 p = strchr(c, ':');
2363 if (!p) {
2364 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
2365 return false;
2367 *p = '\0';
2368 p++;
2369 pw->pw_dir = c;
2370 c = p;
2372 NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
2374 /* shell */
2375 pw->pw_shell = c;
2376 NWRAP_LOG(NWRAP_LOG_TRACE, "shell[%s]", pw->pw_shell);
2378 NWRAP_LOG(NWRAP_LOG_DEBUG,
2379 "Added user[%s:%s:%u:%u:%s:%s:%s]",
2380 pw->pw_name, pw->pw_passwd,
2381 pw->pw_uid, pw->pw_gid,
2382 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
2384 nwrap_pw->num++;
2385 return true;
2388 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
2390 struct nwrap_pw *nwrap_pw;
2391 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
2393 SAFE_FREE(nwrap_pw->list);
2394 nwrap_pw->num = 0;
2395 nwrap_pw->idx = 0;
2398 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
2399 char *buf, size_t buflen, struct passwd **dstp)
2401 char *first;
2402 char *last;
2403 off_t ofs;
2405 first = src->pw_name;
2407 last = src->pw_shell;
2408 while (*last) last++;
2410 ofs = PTR_DIFF(last + 1, first);
2412 if (ofs > (off_t) buflen) {
2413 return ERANGE;
2416 memcpy(buf, first, ofs);
2418 ofs = PTR_DIFF(src->pw_name, first);
2419 dst->pw_name = buf + ofs;
2420 ofs = PTR_DIFF(src->pw_passwd, first);
2421 dst->pw_passwd = buf + ofs;
2422 dst->pw_uid = src->pw_uid;
2423 dst->pw_gid = src->pw_gid;
2424 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
2425 ofs = PTR_DIFF(src->pw_class, first);
2426 dst->pw_class = buf + ofs;
2427 #endif /* HAVE_STRUCT_PASSWD_PW_CLASS */
2429 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
2430 dst->pw_change = 0;
2431 #endif /* HAVE_STRUCT_PASSWD_PW_CHANGE */
2433 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
2434 dst->pw_expire = 0;
2435 #endif /* HAVE_STRUCT_PASSWD_PW_EXPIRE */
2437 ofs = PTR_DIFF(src->pw_gecos, first);
2438 dst->pw_gecos = buf + ofs;
2439 ofs = PTR_DIFF(src->pw_dir, first);
2440 dst->pw_dir = buf + ofs;
2441 ofs = PTR_DIFF(src->pw_shell, first);
2442 dst->pw_shell = buf + ofs;
2444 if (dstp) {
2445 *dstp = dst;
2448 return 0;
2451 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2452 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line)
2454 struct nwrap_sp *nwrap_sp;
2455 struct spwd *sp;
2456 size_t list_size;
2457 char *c;
2458 char *e;
2459 char *p;
2461 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2463 list_size = sizeof(*nwrap_sp->list) * (nwrap_sp->num+1);
2464 sp = (struct spwd *)realloc(nwrap_sp->list, list_size);
2465 if (sp == NULL) {
2466 NWRAP_LOG(NWRAP_LOG_ERROR,
2467 "realloc(%u) failed",
2468 (unsigned)list_size);
2469 return false;
2471 nwrap_sp->list = sp;
2473 sp = &nwrap_sp->list[nwrap_sp->num];
2475 c = line;
2477 /* name */
2478 p = strchr(c, ':');
2479 if (p == NULL) {
2480 NWRAP_LOG(NWRAP_LOG_ERROR,
2481 "name -- Invalid line[%s]: '%s'",
2482 line,
2484 return false;
2486 *p = '\0';
2487 p++;
2488 sp->sp_namp = c;
2489 c = p;
2491 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", sp->sp_namp);
2493 /* pwd */
2494 p = strchr(c, ':');
2495 if (p == NULL) {
2496 NWRAP_LOG(NWRAP_LOG_ERROR,
2497 "pwd -- Invalid line[%s]: '%s'",
2498 line,
2500 return false;
2502 *p = '\0';
2503 p++;
2504 sp->sp_pwdp = c;
2505 c = p;
2507 /* lstchg (long) */
2508 if (c[0] == ':') {
2509 sp->sp_lstchg = -1;
2510 p++;
2511 } else {
2512 p = strchr(c, ':');
2513 if (p == NULL) {
2514 NWRAP_LOG(NWRAP_LOG_ERROR,
2515 "lstchg -- Invalid line[%s]: '%s'",
2516 line,
2518 return false;
2520 *p = '\0';
2521 p++;
2522 sp->sp_lstchg = strtol(c, &e, 10);
2523 if (c == e) {
2524 NWRAP_LOG(NWRAP_LOG_ERROR,
2525 "lstchg -- Invalid line[%s]: '%s' - %s",
2526 line, c, strerror(errno));
2527 return false;
2529 if (e == NULL) {
2530 NWRAP_LOG(NWRAP_LOG_ERROR,
2531 "lstchg -- Invalid line[%s]: '%s' - %s",
2532 line, c, strerror(errno));
2533 return false;
2535 if (e[0] != '\0') {
2536 NWRAP_LOG(NWRAP_LOG_ERROR,
2537 "lstchg -- Invalid line[%s]: '%s' - %s",
2538 line, c, strerror(errno));
2539 return false;
2542 c = p;
2544 /* min (long) */
2545 if (c[0] == ':') {
2546 sp->sp_min = -1;
2547 p++;
2548 } else {
2549 p = strchr(c, ':');
2550 if (p == NULL) {
2551 NWRAP_LOG(NWRAP_LOG_ERROR,
2552 "min -- Invalid line[%s]: '%s'",
2553 line,
2555 return false;
2557 *p = '\0';
2558 p++;
2559 sp->sp_min = strtol(c, &e, 10);
2560 if (c == e) {
2561 NWRAP_LOG(NWRAP_LOG_ERROR,
2562 "min -- Invalid line[%s]: '%s' - %s",
2563 line, c, strerror(errno));
2564 return false;
2566 if (e == NULL) {
2567 NWRAP_LOG(NWRAP_LOG_ERROR,
2568 "min -- Invalid line[%s]: '%s' - %s",
2569 line, c, strerror(errno));
2570 return false;
2572 if (e[0] != '\0') {
2573 NWRAP_LOG(NWRAP_LOG_ERROR,
2574 "min -- Invalid line[%s]: '%s' - %s",
2575 line, c, strerror(errno));
2576 return false;
2579 c = p;
2581 /* max (long) */
2582 if (c[0] == ':') {
2583 sp->sp_max = -1;
2584 p++;
2585 } else {
2586 p = strchr(c, ':');
2587 if (p == NULL) {
2588 NWRAP_LOG(NWRAP_LOG_ERROR,
2589 "max -- Invalid line[%s]: '%s'",
2590 line,
2592 return false;
2594 *p = '\0';
2595 p++;
2596 sp->sp_max = strtol(c, &e, 10);
2597 if (c == e) {
2598 NWRAP_LOG(NWRAP_LOG_ERROR,
2599 "max -- Invalid line[%s]: '%s' - %s",
2600 line, c, strerror(errno));
2601 return false;
2603 if (e == NULL) {
2604 NWRAP_LOG(NWRAP_LOG_ERROR,
2605 "max -- Invalid line[%s]: '%s' - %s",
2606 line, c, strerror(errno));
2607 return false;
2609 if (e[0] != '\0') {
2610 NWRAP_LOG(NWRAP_LOG_ERROR,
2611 "max -- Invalid line[%s]: '%s' - %s",
2612 line, c, strerror(errno));
2613 return false;
2616 c = p;
2618 /* warn (long) */
2619 if (c[0] == ':') {
2620 sp->sp_warn = -1;
2621 p++;
2622 } else {
2623 p = strchr(c, ':');
2624 if (p == NULL) {
2625 NWRAP_LOG(NWRAP_LOG_ERROR,
2626 "warn -- Invalid line[%s]: '%s'",
2627 line,
2629 return false;
2631 *p = '\0';
2632 p++;
2633 sp->sp_warn = strtol(c, &e, 10);
2634 if (c == e) {
2635 NWRAP_LOG(NWRAP_LOG_ERROR,
2636 "warn -- Invalid line[%s]: '%s' - %s",
2637 line, c, strerror(errno));
2638 return false;
2640 if (e == NULL) {
2641 NWRAP_LOG(NWRAP_LOG_ERROR,
2642 "warn -- Invalid line[%s]: '%s' - %s",
2643 line, c, strerror(errno));
2644 return false;
2646 if (e[0] != '\0') {
2647 NWRAP_LOG(NWRAP_LOG_ERROR,
2648 "warn -- Invalid line[%s]: '%s' - %s",
2649 line, c, strerror(errno));
2650 return false;
2653 c = p;
2655 /* inact (long) */
2656 if (c[0] == ':') {
2657 sp->sp_inact = -1;
2658 p++;
2659 } else {
2660 p = strchr(c, ':');
2661 if (p == NULL) {
2662 NWRAP_LOG(NWRAP_LOG_ERROR,
2663 "inact -- Invalid line[%s]: '%s'",
2664 line,
2666 return false;
2668 *p = '\0';
2669 p++;
2670 sp->sp_inact = strtol(c, &e, 10);
2671 if (c == e) {
2672 NWRAP_LOG(NWRAP_LOG_ERROR,
2673 "inact -- Invalid line[%s]: '%s' - %s",
2674 line, c, strerror(errno));
2675 return false;
2677 if (e == NULL) {
2678 NWRAP_LOG(NWRAP_LOG_ERROR,
2679 "inact -- Invalid line[%s]: '%s' - %s",
2680 line, c, strerror(errno));
2681 return false;
2683 if (e[0] != '\0') {
2684 NWRAP_LOG(NWRAP_LOG_ERROR,
2685 "inact -- Invalid line[%s]: '%s' - %s",
2686 line, c, strerror(errno));
2687 return false;
2690 c = p;
2692 /* expire (long) */
2693 if (c[0] == ':') {
2694 sp->sp_expire = -1;
2695 p++;
2696 } else {
2697 p = strchr(c, ':');
2698 if (p == NULL) {
2699 NWRAP_LOG(NWRAP_LOG_ERROR,
2700 "expire -- Invalid line[%s]: '%s'",
2701 line,
2703 return false;
2705 *p = '\0';
2706 p++;
2707 sp->sp_expire = strtol(c, &e, 10);
2708 if (c == e) {
2709 NWRAP_LOG(NWRAP_LOG_ERROR,
2710 "expire -- Invalid line[%s]: '%s' - %s",
2711 line, c, strerror(errno));
2712 return false;
2714 if (e == NULL) {
2715 NWRAP_LOG(NWRAP_LOG_ERROR,
2716 "expire -- Invalid line[%s]: '%s' - %s",
2717 line, c, strerror(errno));
2718 return false;
2720 if (e[0] != '\0') {
2721 NWRAP_LOG(NWRAP_LOG_ERROR,
2722 "expire -- Invalid line[%s]: '%s' - %s",
2723 line, c, strerror(errno));
2724 return false;
2727 c = p;
2729 nwrap_sp->num++;
2730 return true;
2733 static void nwrap_sp_unload(struct nwrap_cache *nwrap)
2735 struct nwrap_sp *nwrap_sp;
2736 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2738 SAFE_FREE(nwrap_sp->list);
2739 nwrap_sp->num = 0;
2740 nwrap_sp->idx = 0;
2742 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2745 * the caller has to call nwrap_unload() on failure
2747 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
2749 struct nwrap_gr *nwrap_gr;
2750 char *c;
2751 char *p;
2752 char *e;
2753 struct group *gr;
2754 size_t list_size;
2755 unsigned nummem;
2757 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2759 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
2760 gr = (struct group *)realloc(nwrap_gr->list, list_size);
2761 if (!gr) {
2762 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
2763 return false;
2765 nwrap_gr->list = gr;
2767 gr = &nwrap_gr->list[nwrap_gr->num];
2769 c = line;
2771 /* name */
2772 p = strchr(c, ':');
2773 if (!p) {
2774 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2775 return false;
2777 *p = '\0';
2778 p++;
2779 gr->gr_name = c;
2780 c = p;
2782 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
2784 /* password */
2785 p = strchr(c, ':');
2786 if (!p) {
2787 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2788 return false;
2790 *p = '\0';
2791 p++;
2792 gr->gr_passwd = c;
2793 c = p;
2795 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
2797 /* gid */
2798 p = strchr(c, ':');
2799 if (!p) {
2800 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2801 return false;
2803 *p = '\0';
2804 p++;
2805 e = NULL;
2806 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
2807 if (c == e) {
2808 NWRAP_LOG(NWRAP_LOG_ERROR,
2809 "Invalid line[%s]: '%s' - %s",
2810 line, c, strerror(errno));
2811 return false;
2813 if (e == NULL) {
2814 NWRAP_LOG(NWRAP_LOG_ERROR,
2815 "Invalid line[%s]: '%s' - %s",
2816 line, c, strerror(errno));
2817 return false;
2819 if (e[0] != '\0') {
2820 NWRAP_LOG(NWRAP_LOG_ERROR,
2821 "Invalid line[%s]: '%s' - %s",
2822 line, c, strerror(errno));
2823 return false;
2825 c = p;
2827 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
2829 /* members */
2830 gr->gr_mem = (char **)malloc(sizeof(char *));
2831 if (!gr->gr_mem) {
2832 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
2833 return false;
2835 gr->gr_mem[0] = NULL;
2837 for(nummem = 0; p != NULL && p[0] != '\0'; nummem++) {
2838 char **m;
2839 size_t m_size;
2840 c = p;
2841 p = strchr(c, ',');
2842 if (p) {
2843 *p = '\0';
2844 p++;
2847 if (strlen(c) == 0) {
2848 break;
2851 m_size = sizeof(char *) * (nummem+2);
2852 m = (char **)realloc(gr->gr_mem, m_size);
2853 if (!m) {
2854 NWRAP_LOG(NWRAP_LOG_ERROR,
2855 "realloc(%zd) failed",
2856 m_size);
2857 return false;
2859 gr->gr_mem = m;
2860 gr->gr_mem[nummem] = c;
2861 gr->gr_mem[nummem+1] = NULL;
2863 NWRAP_LOG(NWRAP_LOG_TRACE,
2864 "member[%u]: '%s'",
2865 nummem, gr->gr_mem[nummem]);
2868 NWRAP_LOG(NWRAP_LOG_DEBUG,
2869 "Added group[%s:%s:%u:] with %u members",
2870 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem);
2872 nwrap_gr->num++;
2873 return true;
2876 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
2878 int i;
2879 struct nwrap_gr *nwrap_gr;
2880 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2882 if (nwrap_gr->list) {
2883 for (i=0; i < nwrap_gr->num; i++) {
2884 SAFE_FREE(nwrap_gr->list[i].gr_mem);
2886 SAFE_FREE(nwrap_gr->list);
2889 nwrap_gr->num = 0;
2890 nwrap_gr->idx = 0;
2893 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
2894 char *buf, size_t buflen, struct group **dstp)
2896 char *p = NULL;
2897 uintptr_t align = 0;
2898 unsigned int gr_mem_cnt = 0;
2899 unsigned i;
2900 size_t total_len;
2901 size_t gr_name_len = strlen(src->gr_name) + 1;
2902 size_t gr_passwd_len = strlen(src->gr_passwd) + 1;
2903 union {
2904 char *ptr;
2905 char **data;
2906 } g_mem;
2908 for (i = 0; src->gr_mem[i] != NULL; i++) {
2909 gr_mem_cnt++;
2912 /* Align the memory for storing pointers */
2913 align = __alignof__(char *) - ((p - (char *)0) % __alignof__(char *));
2914 total_len = align +
2915 (1 + gr_mem_cnt) * sizeof(char *) +
2916 gr_name_len + gr_passwd_len;
2918 if (total_len > buflen) {
2919 errno = ERANGE;
2920 return -1;
2922 buflen -= total_len;
2924 /* gr_mem */
2925 p = buf + align;
2926 g_mem.ptr = p;
2927 dst->gr_mem = g_mem.data;
2929 /* gr_name */
2930 p += (1 + gr_mem_cnt) * sizeof(char *);
2931 dst->gr_name = p;
2933 /* gr_passwd */
2934 p += gr_name_len;
2935 dst->gr_passwd = p;
2937 /* gr_mem[x] */
2938 p += gr_passwd_len;
2940 /* gr_gid */
2941 dst->gr_gid = src->gr_gid;
2943 memcpy(dst->gr_name, src->gr_name, gr_name_len);
2945 memcpy(dst->gr_passwd, src->gr_passwd, gr_passwd_len);
2947 /* Set the terminating entry */
2948 dst->gr_mem[gr_mem_cnt] = NULL;
2950 /* Now add the group members content */
2951 total_len = 0;
2952 for (i = 0; i < gr_mem_cnt; i++) {
2953 size_t len = strlen(src->gr_mem[i]) + 1;
2955 dst->gr_mem[i] = p;
2956 total_len += len;
2957 p += len;
2960 if (total_len > buflen) {
2961 errno = ERANGE;
2962 return -1;
2965 for (i = 0; i < gr_mem_cnt; i++) {
2966 size_t len = strlen(src->gr_mem[i]) + 1;
2968 memcpy(dst->gr_mem[i],
2969 src->gr_mem[i],
2970 len);
2973 if (dstp != NULL) {
2974 *dstp = dst;
2977 return 0;
2980 static struct nwrap_entlist *nwrap_entlist_init(struct nwrap_entdata *ed)
2982 struct nwrap_entlist *el;
2984 if (ed == NULL) {
2985 NWRAP_LOG(NWRAP_LOG_ERROR,
2986 "entry is NULL, can't create list item");
2987 return NULL;
2990 el = (struct nwrap_entlist *)malloc(sizeof(struct nwrap_entlist));
2991 if (el == NULL) {
2992 NWRAP_LOG(NWRAP_LOG_ERROR, "malloc failed");
2993 return NULL;
2996 el->next = NULL;
2997 el->ed = ed;
2999 return el;
3002 static bool nwrap_ed_inventarize_add_new(char *const h_name,
3003 struct nwrap_entdata *const ed)
3005 ENTRY e;
3006 ENTRY *p;
3007 struct nwrap_entlist *el;
3008 bool ok;
3010 if (h_name == NULL) {
3011 NWRAP_LOG(NWRAP_LOG_ERROR, "h_name NULL - can't add");
3012 return false;
3015 el = nwrap_entlist_init(ed);
3016 if (el == NULL) {
3017 return false;
3020 e.key = h_name;
3021 e.data = (void *)el;
3023 p = hsearch(e, ENTER);
3024 if (p == NULL) {
3025 NWRAP_LOG(NWRAP_LOG_ERROR,
3026 "Hash table is full (%s)!",
3027 strerror(errno));
3028 return false;
3031 ok = nwrap_vector_add_item(&(nwrap_he_global.lists), (void *)el);
3032 if (!ok) {
3033 NWRAP_LOG(NWRAP_LOG_ERROR,
3034 "Failed to add list entry to vector.");
3035 return false;
3038 return true;
3041 static bool nwrap_ed_inventarize_add_to_existing(struct nwrap_entdata *const ed,
3042 struct nwrap_entlist *const el)
3044 struct nwrap_entlist *cursor;
3045 struct nwrap_entlist *el_new;
3047 if (el == NULL) {
3048 NWRAP_LOG(NWRAP_LOG_ERROR, "list is NULL, can not add");
3049 return false;
3053 for (cursor = el; cursor->next != NULL; cursor = cursor->next)
3055 if (cursor->ed == ed) {
3056 /* The entry already exists in this list. */
3057 return true;
3061 if (cursor->ed == ed) {
3062 /* The entry already exists in this list. */
3063 return true;
3066 el_new = nwrap_entlist_init(ed);
3067 if (el_new == NULL) {
3068 return false;
3071 cursor->next = el_new;
3072 return true;
3075 static bool nwrap_ed_inventarize(char *const name,
3076 struct nwrap_entdata *const ed)
3078 ENTRY e;
3079 ENTRY *p;
3080 bool ok;
3082 e.key = name;
3083 e.data = NULL;
3085 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
3087 p = hsearch(e, FIND);
3088 if (p == NULL) {
3089 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", name);
3090 ok = nwrap_ed_inventarize_add_new(name, ed);
3091 } else {
3092 struct nwrap_entlist *el = (struct nwrap_entlist *)p->data;
3094 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s found. Add record to list.", name);
3095 ok = nwrap_ed_inventarize_add_to_existing(ed, el);
3098 return ok;
3101 static bool nwrap_add_hname(struct nwrap_entdata *const ed)
3103 char *const h_name = (char *const)(ed->ht.h_name);
3104 unsigned i;
3105 bool ok;
3107 ok = nwrap_ed_inventarize(h_name, ed);
3108 if (!ok) {
3109 return false;
3112 if (ed->ht.h_aliases == NULL) {
3113 return true;
3116 /* Itemize aliases */
3117 for (i = 0; ed->ht.h_aliases[i] != NULL; ++i) {
3118 char *h_name_alias;
3120 h_name_alias = ed->ht.h_aliases[i];
3122 NWRAP_LOG(NWRAP_LOG_DEBUG, "Add alias: %s", h_name_alias);
3124 if (!nwrap_ed_inventarize(h_name_alias, ed)) {
3125 NWRAP_LOG(NWRAP_LOG_ERROR,
3126 "Unable to add alias: %s", h_name_alias);
3127 return false;
3131 return true;
3134 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line)
3136 struct nwrap_he *nwrap_he = (struct nwrap_he *)nwrap->private_data;
3137 bool do_aliases = true;
3138 ssize_t aliases_count = 0;
3139 char *p;
3140 char *i;
3141 char *n;
3143 char *ip;
3144 bool ok;
3146 struct nwrap_entdata *ed = (struct nwrap_entdata *)
3147 malloc(sizeof(struct nwrap_entdata));
3148 if (ed == NULL) {
3149 NWRAP_LOG(NWRAP_LOG_ERROR,
3150 "Unable to allocate memory for nwrap_entdata");
3151 return false;
3153 ZERO_STRUCTP(ed);
3155 i = line;
3158 * IP
3161 /* Walk to first char */
3162 for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
3163 if (*p == '\0') {
3164 NWRAP_LOG(NWRAP_LOG_ERROR,
3165 "Invalid line[%s]: '%s'",
3166 line, i);
3167 free(ed);
3168 return false;
3172 for (i = p; !isspace((int)*p); p++) {
3173 if (*p == '\0') {
3174 NWRAP_LOG(NWRAP_LOG_ERROR,
3175 "Invalid line[%s]: '%s'",
3176 line, i);
3177 free(ed);
3178 return false;
3182 *p = '\0';
3184 if (inet_pton(AF_INET, i, ed->addr.host_addr)) {
3185 ed->ht.h_addrtype = AF_INET;
3186 ed->ht.h_length = 4;
3187 #ifdef HAVE_IPV6
3188 } else if (inet_pton(AF_INET6, i, ed->addr.host_addr)) {
3189 ed->ht.h_addrtype = AF_INET6;
3190 ed->ht.h_length = 16;
3191 #endif
3192 } else {
3193 NWRAP_LOG(NWRAP_LOG_ERROR,
3194 "Invalid line[%s]: '%s'",
3195 line, i);
3197 free(ed);
3198 return false;
3200 ip = i;
3202 ok = nwrap_vector_add_item(&(ed->nwrap_addrdata),
3203 (void *const)ed->addr.host_addr);
3204 if (!ok) {
3205 NWRAP_LOG(NWRAP_LOG_ERROR, "Unable to add addrdata to vector");
3206 free(ed);
3207 return false;
3209 ed->ht.h_addr_list = nwrap_vector_head(&ed->nwrap_addrdata);
3211 p++;
3214 * FQDN
3217 /* Walk to first char */
3218 for (n = p; *p != '_' && !isalnum((int) *p); p++) {
3219 if (*p == '\0') {
3220 NWRAP_LOG(NWRAP_LOG_ERROR,
3221 "Invalid line[%s]: '%s'",
3222 line, n);
3224 free(ed);
3225 return false;
3229 for (n = p; !isspace((int)*p); p++) {
3230 if (*p == '\0') {
3231 do_aliases = false;
3232 break;
3236 *p = '\0';
3238 /* Convert to lowercase. This operate on same memory region */
3239 str_tolower(n, n);
3240 ed->ht.h_name = n;
3242 /* glib's getent always dereferences he->h_aliases */
3243 ed->ht.h_aliases = malloc(sizeof(char *));
3244 if (ed->ht.h_aliases == NULL) {
3245 free(ed);
3246 return false;
3248 ed->ht.h_aliases[0] = NULL;
3251 * Aliases
3253 while (do_aliases) {
3254 char **aliases;
3255 char *a;
3257 p++;
3259 /* Walk to first char */
3260 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
3261 if (*p == '\0') {
3262 do_aliases = false;
3263 break;
3266 /* Only trailing spaces are left */
3267 if (!do_aliases) {
3268 break;
3271 for (a = p; !isspace((int)*p); p++) {
3272 if (*p == '\0') {
3273 do_aliases = false;
3274 break;
3278 *p = '\0';
3280 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
3281 if (aliases == NULL) {
3282 free(ed);
3283 return false;
3285 ed->ht.h_aliases = aliases;
3287 str_tolower(a, a);
3288 aliases[aliases_count] = a;
3289 aliases[aliases_count + 1] = NULL;
3291 aliases_count += 1;
3294 ok = nwrap_vector_add_item(&(nwrap_he->entries), (void *const)ed);
3295 if (!ok) {
3296 NWRAP_LOG(NWRAP_LOG_ERROR, "Unable to add entry to vector");
3297 free(ed);
3298 return false;
3301 ed->aliases_count = aliases_count;
3302 /* Inventarize item */
3303 ok = nwrap_add_hname(ed);
3304 if (!ok) {
3305 return false;
3308 ok = nwrap_ed_inventarize(ip, ed);
3309 if (!ok) {
3310 return false;
3313 nwrap_he->num++;
3314 return true;
3317 static void nwrap_he_unload(struct nwrap_cache *nwrap)
3319 struct nwrap_he *nwrap_he =
3320 (struct nwrap_he *)nwrap->private_data;
3321 struct nwrap_entdata *ed;
3322 struct nwrap_entlist *el;
3323 size_t i;
3324 int rc;
3326 nwrap_vector_foreach (ed, nwrap_he->entries, i)
3328 SAFE_FREE(ed->nwrap_addrdata.items);
3329 SAFE_FREE(ed->ht.h_aliases);
3330 SAFE_FREE(ed);
3332 SAFE_FREE(nwrap_he->entries.items);
3333 nwrap_he->entries.count = nwrap_he->entries.capacity = 0;
3335 nwrap_vector_foreach(el, nwrap_he->lists, i)
3337 while (el != NULL) {
3338 struct nwrap_entlist *el_next;
3340 el_next = el->next;
3341 SAFE_FREE(el);
3342 el = el_next;
3345 SAFE_FREE(nwrap_he->lists.items);
3346 nwrap_he->lists.count = nwrap_he->lists.capacity = 0;
3348 nwrap_he->num = 0;
3349 nwrap_he->idx = 0;
3352 * If we unload the file, the pointers in the hash table point to
3353 * invalid memory. So we need to destroy the hash table and recreate
3354 * it.
3356 hdestroy();
3357 rc = hcreate(max_hostents);
3358 if (rc == 0) {
3359 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to initialize hash table");
3360 exit(-1);
3365 /* user functions */
3366 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
3367 const char *name)
3369 int i;
3370 bool ok;
3372 (void) b; /* unused */
3374 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
3376 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3377 if (!ok) {
3378 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3379 return NULL;
3382 for (i=0; i<nwrap_pw_global.num; i++) {
3383 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
3384 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
3385 return &nwrap_pw_global.list[i];
3387 NWRAP_LOG(NWRAP_LOG_DEBUG,
3388 "user[%s] does not match [%s]",
3389 name,
3390 nwrap_pw_global.list[i].pw_name);
3393 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3395 errno = ENOENT;
3396 return NULL;
3399 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
3400 const char *name, struct passwd *pwdst,
3401 char *buf, size_t buflen, struct passwd **pwdstp)
3403 struct passwd *pw;
3405 pw = nwrap_files_getpwnam(b, name);
3406 if (!pw) {
3407 if (errno == 0) {
3408 return ENOENT;
3410 return errno;
3413 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3416 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
3417 uid_t uid)
3419 int i;
3420 bool ok;
3422 (void) b; /* unused */
3424 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3425 if (!ok) {
3426 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3427 return NULL;
3430 for (i=0; i<nwrap_pw_global.num; i++) {
3431 if (nwrap_pw_global.list[i].pw_uid == uid) {
3432 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] found", uid);
3433 return &nwrap_pw_global.list[i];
3435 NWRAP_LOG(NWRAP_LOG_DEBUG,
3436 "uid[%u] does not match [%u]",
3437 uid,
3438 nwrap_pw_global.list[i].pw_uid);
3441 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
3443 errno = ENOENT;
3444 return NULL;
3447 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
3448 uid_t uid, struct passwd *pwdst,
3449 char *buf, size_t buflen, struct passwd **pwdstp)
3451 struct passwd *pw;
3453 pw = nwrap_files_getpwuid(b, uid);
3454 if (!pw) {
3455 if (errno == 0) {
3456 return ENOENT;
3458 return errno;
3461 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3464 /* user enum functions */
3465 static void nwrap_files_setpwent(struct nwrap_backend *b)
3467 (void) b; /* unused */
3469 nwrap_pw_global.idx = 0;
3472 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
3474 struct passwd *pw;
3476 (void) b; /* unused */
3478 if (nwrap_pw_global.idx == 0) {
3479 bool ok;
3480 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3481 if (!ok) {
3482 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3483 return NULL;
3487 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
3488 errno = ENOENT;
3489 return NULL;
3492 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
3494 NWRAP_LOG(NWRAP_LOG_DEBUG,
3495 "return user[%s] uid[%u]",
3496 pw->pw_name, pw->pw_uid);
3498 return pw;
3501 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
3502 struct passwd *pwdst, char *buf,
3503 size_t buflen, struct passwd **pwdstp)
3505 struct passwd *pw;
3507 pw = nwrap_files_getpwent(b);
3508 if (!pw) {
3509 if (errno == 0) {
3510 return ENOENT;
3512 return errno;
3515 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3518 static void nwrap_files_endpwent(struct nwrap_backend *b)
3520 (void) b; /* unused */
3522 nwrap_pw_global.idx = 0;
3525 /* shadow */
3527 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
3529 #ifdef HAVE_SETSPENT
3530 static void nwrap_files_setspent(void)
3532 nwrap_sp_global.idx = 0;
3535 static struct spwd *nwrap_files_getspent(void)
3537 struct spwd *sp;
3539 if (nwrap_sp_global.idx == 0) {
3540 bool ok;
3542 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3543 if (!ok) {
3544 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3545 return NULL;
3549 if (nwrap_sp_global.idx >= nwrap_sp_global.num) {
3550 errno = ENOENT;
3551 return NULL;
3554 sp = &nwrap_sp_global.list[nwrap_sp_global.idx++];
3556 NWRAP_LOG(NWRAP_LOG_DEBUG,
3557 "return user[%s]",
3558 sp->sp_namp);
3560 return sp;
3563 static void nwrap_files_endspent(void)
3565 nwrap_sp_global.idx = 0;
3567 #endif /* HAVE_SETSPENT */
3569 static struct spwd *nwrap_files_getspnam(const char *name)
3571 int i;
3572 bool ok;
3574 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
3576 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3577 if (!ok) {
3578 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3579 return NULL;
3582 for (i=0; i<nwrap_sp_global.num; i++) {
3583 if (strcmp(nwrap_sp_global.list[i].sp_namp, name) == 0) {
3584 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
3585 return &nwrap_sp_global.list[i];
3587 NWRAP_LOG(NWRAP_LOG_DEBUG,
3588 "user[%s] does not match [%s]",
3589 name,
3590 nwrap_sp_global.list[i].sp_namp);
3593 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3595 errno = ENOENT;
3596 return NULL;
3598 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3600 /* misc functions */
3601 static int nwrap_files_initgroups(struct nwrap_backend *b,
3602 const char *user,
3603 gid_t group)
3605 struct group *grp;
3606 gid_t *groups;
3607 int size = 1;
3608 int rc;
3610 groups = (gid_t *)malloc(size * sizeof(gid_t));
3611 if (groups == NULL) {
3612 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3613 errno = ENOMEM;
3614 return -1;
3616 groups[0] = group;
3618 nwrap_files_setgrent(b);
3619 while ((grp = nwrap_files_getgrent(b)) != NULL) {
3620 int i = 0;
3622 NWRAP_LOG(NWRAP_LOG_DEBUG,
3623 "Inspecting %s for group membership",
3624 grp->gr_name);
3626 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
3627 if (group != grp->gr_gid &&
3628 (strcmp(user, grp->gr_mem[i]) == 0)) {
3629 NWRAP_LOG(NWRAP_LOG_DEBUG,
3630 "%s is member of %s",
3631 user,
3632 grp->gr_name);
3634 groups = (gid_t *)realloc(groups,
3635 (size + 1) * sizeof(gid_t));
3636 if (groups == NULL) {
3637 NWRAP_LOG(NWRAP_LOG_ERROR,
3638 "Out of memory");
3639 errno = ENOMEM;
3640 return -1;
3643 groups[size] = grp->gr_gid;
3644 size++;
3649 nwrap_files_endgrent(b);
3651 NWRAP_LOG(NWRAP_LOG_DEBUG,
3652 "%s is member of %d groups",
3653 user, size);
3655 /* This really only works if uid_wrapper is loaded */
3656 rc = setgroups(size, groups);
3658 free(groups);
3660 return rc;
3663 /* group functions */
3664 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
3665 const char *name)
3667 int i;
3668 bool ok;
3670 (void) b; /* unused */
3672 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3673 if (!ok) {
3674 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3675 return NULL;
3678 for (i=0; i<nwrap_gr_global.num; i++) {
3679 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
3680 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] found", name);
3681 return &nwrap_gr_global.list[i];
3683 NWRAP_LOG(NWRAP_LOG_DEBUG,
3684 "group[%s] does not match [%s]",
3685 name,
3686 nwrap_gr_global.list[i].gr_name);
3689 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
3691 errno = ENOENT;
3692 return NULL;
3695 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
3696 const char *name, struct group *grdst,
3697 char *buf, size_t buflen, struct group **grdstp)
3699 struct group *gr;
3701 gr = nwrap_files_getgrnam(b, name);
3702 if (!gr) {
3703 if (errno == 0) {
3704 return ENOENT;
3706 return errno;
3709 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3712 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
3713 gid_t gid)
3715 int i;
3716 bool ok;
3718 (void) b; /* unused */
3720 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3721 if (!ok) {
3722 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3723 return NULL;
3726 for (i=0; i<nwrap_gr_global.num; i++) {
3727 if (nwrap_gr_global.list[i].gr_gid == gid) {
3728 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] found", gid);
3729 return &nwrap_gr_global.list[i];
3731 NWRAP_LOG(NWRAP_LOG_DEBUG,
3732 "gid[%u] does not match [%u]",
3733 gid,
3734 nwrap_gr_global.list[i].gr_gid);
3737 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
3739 errno = ENOENT;
3740 return NULL;
3743 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
3744 gid_t gid, struct group *grdst,
3745 char *buf, size_t buflen, struct group **grdstp)
3747 struct group *gr;
3749 gr = nwrap_files_getgrgid(b, gid);
3750 if (!gr) {
3751 if (errno == 0) {
3752 return ENOENT;
3754 return errno;
3757 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3760 /* group enum functions */
3761 static void nwrap_files_setgrent(struct nwrap_backend *b)
3763 (void) b; /* unused */
3765 nwrap_gr_global.idx = 0;
3768 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
3770 struct group *gr;
3772 (void) b; /* unused */
3774 if (nwrap_gr_global.idx == 0) {
3775 bool ok;
3777 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3778 if (!ok) {
3779 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3780 return NULL;
3784 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
3785 errno = ENOENT;
3786 return NULL;
3789 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
3791 NWRAP_LOG(NWRAP_LOG_DEBUG,
3792 "return group[%s] gid[%u]",
3793 gr->gr_name, gr->gr_gid);
3795 return gr;
3798 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
3799 struct group *grdst, char *buf,
3800 size_t buflen, struct group **grdstp)
3802 struct group *gr;
3804 gr = nwrap_files_getgrent(b);
3805 if (!gr) {
3806 if (errno == 0) {
3807 return ENOENT;
3809 return errno;
3812 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3815 static void nwrap_files_endgrent(struct nwrap_backend *b)
3817 (void) b; /* unused */
3819 nwrap_gr_global.idx = 0;
3822 /* hosts functions */
3823 static int nwrap_files_internal_gethostbyname(const char *name, int af,
3824 struct hostent *result,
3825 struct nwrap_vector *addr_list)
3827 struct nwrap_entlist *el;
3828 struct hostent *he;
3829 char *h_name_lower;
3830 ENTRY e;
3831 ENTRY *e_p;
3832 char canon_name[DNS_NAME_MAX] = { 0 };
3833 size_t name_len;
3834 bool he_found = false;
3835 bool ok;
3838 * We need to make sure we have zeroed return pointer for consumers
3839 * which don't check return values, e.g. OpenLDAP.
3841 ZERO_STRUCTP(result);
3843 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3844 if (!ok) {
3845 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3846 goto no_ent;
3849 name_len = strlen(name);
3850 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
3851 memcpy(canon_name, name, name_len - 1);
3852 canon_name[name_len] = '\0';
3853 name = canon_name;
3856 if (!str_tolower_copy(&h_name_lower, name)) {
3857 NWRAP_LOG(NWRAP_LOG_DEBUG,
3858 "Out of memory while converting to lower case");
3859 goto no_ent;
3862 /* Look at hash table for element */
3863 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3864 e.key = h_name_lower;
3865 e.data = NULL;
3866 e_p = hsearch(e, FIND);
3867 if (e_p == NULL) {
3868 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3869 SAFE_FREE(h_name_lower);
3870 goto no_ent;
3872 SAFE_FREE(h_name_lower);
3874 /* Always cleanup vector and results */
3875 if (!nwrap_vector_is_initialized(addr_list)) {
3876 if (!nwrap_vector_init(addr_list)) {
3877 NWRAP_LOG(NWRAP_LOG_DEBUG,
3878 "Unable to initialize memory for addr_list vector");
3879 goto no_ent;
3881 } else {
3882 /* When vector is initialized data are valid no more.
3883 * Quick way how to free vector is: */
3884 addr_list->count = 0;
3887 /* Iterate through results */
3888 for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3890 he = &(el->ed->ht);
3892 /* Filter by address familiy if provided */
3893 if (af != AF_UNSPEC && he->h_addrtype != af) {
3894 continue;
3898 * GLIBC HACK?
3899 * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3901 if (af == AF_UNSPEC && he->h_addrtype != AF_INET) {
3902 continue;
3905 if (!he_found) {
3906 memcpy(result, he, sizeof(struct hostent));
3907 NWRAP_LOG(NWRAP_LOG_DEBUG,
3908 "Name found. Returning record for %s",
3909 he->h_name);
3910 he_found = true;
3912 nwrap_vector_merge(addr_list, &el->ed->nwrap_addrdata);
3913 result->h_addr_list = nwrap_vector_head(addr_list);
3916 if (he_found) {
3917 return 0;
3919 NWRAP_LOG(NWRAP_LOG_DEBUG,
3920 "Name found in database. No records matches type.");
3922 no_ent:
3923 errno = ENOENT;
3924 return -1;
3927 static int nwrap_files_gethostbyname2_r(struct nwrap_backend *b,
3928 const char *name, int af,
3929 struct hostent *hedst,
3930 char *buf, size_t buflen,
3931 struct hostent **hedstp)
3933 struct nwrap_vector *addr_list = NULL;
3934 union {
3935 char *ptr;
3936 char **list;
3937 } g;
3938 int rc;
3940 (void) b; /* unused */
3941 (void) af; /* unused */
3943 if (name == NULL || hedst == NULL || buf == NULL || buflen == 0) {
3944 errno = EINVAL;
3945 return -1;
3947 *hedstp = NULL;
3948 buf[0] = '\0';
3950 addr_list = calloc(1, sizeof(struct nwrap_vector));
3951 if (addr_list == NULL) {
3952 NWRAP_LOG(NWRAP_LOG_ERROR,
3953 "Unable to allocate memory for address list");
3954 errno = ENOENT;
3955 return -1;
3958 rc = nwrap_files_internal_gethostbyname(name, af, hedst,
3959 addr_list);
3960 if (rc == -1) {
3961 SAFE_FREE(addr_list->items);
3962 SAFE_FREE(addr_list);
3963 errno = ENOENT;
3964 return -1;
3967 /* +1 i for ending NULL pointer */
3968 if (buflen < ((addr_list->count + 1) * sizeof(void *))) {
3969 SAFE_FREE(addr_list->items);
3970 SAFE_FREE(addr_list);
3971 return ERANGE;
3974 /* Copy all to user provided buffer and change
3975 * pointers in returned structure.
3976 * +1 is for ending NULL pointer. */
3977 memcpy(buf, addr_list->items, (addr_list->count + 1) * sizeof(void *));
3979 SAFE_FREE(addr_list->items);
3980 SAFE_FREE(addr_list);
3982 g.ptr = buf;
3983 hedst->h_addr_list = g.list;
3984 *hedstp = hedst;
3985 return 0;
3988 #ifdef HAVE_GETHOSTBYNAME_R
3989 static int nwrap_gethostbyname_r(const char *name,
3990 struct hostent *ret,
3991 char *buf, size_t buflen,
3992 struct hostent **result, int *h_errnop)
3994 int rc;
3995 size_t i;
3997 for (i=0; i < nwrap_main_global->num_backends; i++) {
3998 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3999 rc = b->ops->nw_gethostbyname2_r(b, name, AF_UNSPEC, ret,
4000 buf, buflen, result);
4001 if (rc == 0) {
4002 return 0;
4003 } else if (rc == ERANGE) {
4004 return ERANGE;
4007 *h_errnop = h_errno;
4008 return ENOENT;
4011 int gethostbyname_r(const char *name,
4012 struct hostent *ret,
4013 char *buf, size_t buflen,
4014 struct hostent **result, int *h_errnop)
4016 if (!nss_wrapper_hosts_enabled()) {
4017 return libc_gethostbyname_r(name,
4018 ret,
4019 buf,
4020 buflen,
4021 result,
4022 h_errnop);
4025 return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
4027 #endif
4029 #ifdef HAVE_GETHOSTBYNAME2_R
4030 static int nwrap_gethostbyname2_r(const char *name, int af,
4031 struct hostent *ret,
4032 char *buf, size_t buflen,
4033 struct hostent **result, int *h_errnop)
4035 int rc;
4036 size_t i;
4038 for (i=0; i < nwrap_main_global->num_backends; i++) {
4039 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4040 rc = b->ops->nw_gethostbyname2_r(b, name, af, ret,
4041 buf, buflen, result);
4042 if (rc == 0) {
4043 return 0;
4044 } else if (rc == ERANGE) {
4045 return ERANGE;
4048 *h_errnop = h_errno;
4049 return ENOENT;
4052 int gethostbyname2_r(const char *name, int af,
4053 struct hostent *ret,
4054 char *buf, size_t buflen,
4055 struct hostent **result, int *h_errnop)
4057 if (!nss_wrapper_hosts_enabled()) {
4058 return libc_gethostbyname2_r(name, af, ret, buf, buflen,
4059 result, h_errnop);
4062 return nwrap_gethostbyname2_r(name, af, ret, buf, buflen, result,
4063 h_errnop);
4065 #endif
4067 static int nwrap_files_getaddrinfo(const char *name,
4068 unsigned short port,
4069 const struct addrinfo *hints,
4070 struct addrinfo **ai)
4072 struct nwrap_entlist *el;
4073 struct hostent *he;
4074 struct addrinfo *ai_head = NULL;
4075 struct addrinfo *ai_cur = NULL;
4076 char *h_name_lower;
4077 size_t name_len;
4078 char canon_name[DNS_NAME_MAX] = { 0 };
4079 bool skip_canonname = false;
4080 ENTRY e = {
4081 .key = NULL,
4083 ENTRY *e_p = NULL;
4084 int rc;
4085 bool ok;
4087 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
4088 if (!ok) {
4089 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
4090 return EAI_SYSTEM;
4093 name_len = strlen(name);
4094 if (name_len == 0) {
4095 return EAI_NONAME;
4098 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
4099 memcpy(canon_name, name, name_len - 1);
4100 canon_name[name_len] = '\0';
4101 name = canon_name;
4104 if (!str_tolower_copy(&h_name_lower, name)) {
4105 NWRAP_LOG(NWRAP_LOG_DEBUG,
4106 "Out of memory while converting to lower case");
4107 return EAI_MEMORY;
4110 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
4111 e.key = h_name_lower;
4112 e.data = NULL;
4113 e_p = hsearch(e, FIND);
4114 if (e_p == NULL) {
4115 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
4116 SAFE_FREE(h_name_lower);
4117 errno = ENOENT;
4118 return EAI_NONAME;
4120 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name: %s found.", h_name_lower);
4121 SAFE_FREE(h_name_lower);
4123 rc = EAI_NONAME;
4124 for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
4126 int rc2;
4127 struct addrinfo *ai_new = NULL;
4129 he = &(el->ed->ht);
4131 if (hints->ai_family != AF_UNSPEC &&
4132 he->h_addrtype != hints->ai_family)
4134 NWRAP_LOG(NWRAP_LOG_DEBUG,
4135 "Entry found but with wrong AF - "
4136 "remembering EAI_ADDRINFO.");
4137 rc = EAI_ADDRFAMILY;
4138 continue;
4141 /* Function allocates memory and returns it in ai. */
4142 rc2 = nwrap_convert_he_ai(he,
4143 port,
4144 hints,
4145 &ai_new,
4146 skip_canonname);
4147 if (rc2 != 0) {
4148 NWRAP_LOG(NWRAP_LOG_ERROR, "Error converting he to ai");
4149 if (ai_head != NULL) {
4150 freeaddrinfo(ai_head);
4152 return rc2;
4154 skip_canonname = true;
4156 if (ai_head == NULL) {
4157 ai_head = ai_new;
4159 if (ai_cur != NULL) {
4160 ai_cur->ai_next = ai_new;
4162 ai_cur = ai_new;
4165 if (ai_head != NULL) {
4166 rc = 0;
4169 *ai = ai_head;
4171 return rc;
4174 static struct hostent *nwrap_files_gethostbyaddr(struct nwrap_backend *b,
4175 const void *addr,
4176 socklen_t len, int type)
4178 struct hostent *he;
4179 char ip[NWRAP_INET_ADDRSTRLEN] = {0};
4180 struct nwrap_entdata *ed;
4181 const char *a;
4182 size_t i;
4183 bool ok;
4185 (void) b; /* unused */
4186 (void) len; /* unused */
4188 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
4189 if (!ok) {
4190 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
4191 return NULL;
4194 a = inet_ntop(type, addr, ip, sizeof(ip));
4195 if (a == NULL) {
4196 errno = EINVAL;
4197 return NULL;
4200 nwrap_vector_foreach(ed, nwrap_he_global.entries, i)
4202 he = &(ed->ht);
4203 if (he->h_addrtype != type) {
4204 continue;
4207 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
4208 return he;
4212 errno = ENOENT;
4213 return NULL;
4216 #ifdef HAVE_GETHOSTBYADDR_R
4217 static int nwrap_gethostbyaddr_r(const void *addr, socklen_t len, int type,
4218 struct hostent *ret,
4219 char *buf, size_t buflen,
4220 struct hostent **result, int *h_errnop)
4222 size_t i;
4223 for (i=0; i < nwrap_main_global->num_backends; i++) {
4224 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4225 *result = b->ops->nw_gethostbyaddr(b, addr, len, type);
4226 if (*result != NULL) {
4227 break;
4231 if (*result != NULL) {
4232 memset(buf, '\0', buflen);
4233 *ret = **result;
4234 return 0;
4237 *h_errnop = h_errno;
4238 return -1;
4241 int gethostbyaddr_r(const void *addr, socklen_t len, int type,
4242 struct hostent *ret,
4243 char *buf, size_t buflen,
4244 struct hostent **result, int *h_errnop)
4246 if (!nss_wrapper_hosts_enabled()) {
4247 return libc_gethostbyaddr_r(addr,
4248 len,
4249 type,
4250 ret,
4251 buf,
4252 buflen,
4253 result,
4254 h_errnop);
4257 return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
4259 #endif
4261 /* hosts enum functions */
4262 static void nwrap_files_sethostent(void)
4264 nwrap_he_global.idx = 0;
4267 static struct hostent *nwrap_files_gethostent(void)
4269 struct hostent *he;
4271 if (nwrap_he_global.idx == 0) {
4272 bool ok;
4274 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
4275 if (!ok) {
4276 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading hosts file");
4277 return NULL;
4281 if (nwrap_he_global.idx >= nwrap_he_global.num) {
4282 errno = ENOENT;
4283 return NULL;
4286 he = &((struct nwrap_entdata *)nwrap_he_global.entries.items[nwrap_he_global.idx++])->ht;
4288 NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
4290 return he;
4293 static void nwrap_files_endhostent(void)
4295 nwrap_he_global.idx = 0;
4299 * module backend
4303 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
4304 const char *name)
4306 static struct passwd pwd;
4307 static char buf[1000];
4308 NSS_STATUS status;
4310 if (b->symbols->_nss_getpwnam_r.f == NULL) {
4311 return NULL;
4314 status = b->symbols->_nss_getpwnam_r.f(name,
4315 &pwd,
4316 buf,
4317 sizeof(buf),
4318 &errno);
4319 if (status == NSS_STATUS_NOTFOUND) {
4320 return NULL;
4322 if (status != NSS_STATUS_SUCCESS) {
4323 return NULL;
4326 return &pwd;
4329 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
4330 const char *name, struct passwd *pwdst,
4331 char *buf, size_t buflen, struct passwd **pwdstp)
4333 int ret;
4335 *pwdstp = NULL;
4337 if (b->symbols->_nss_getpwnam_r.f == NULL) {
4338 return NSS_STATUS_NOTFOUND;
4341 ret = b->symbols->_nss_getpwnam_r.f(name, pwdst, buf, buflen, &errno);
4342 switch (ret) {
4343 case NSS_STATUS_SUCCESS:
4344 *pwdstp = pwdst;
4345 return 0;
4346 case NSS_STATUS_NOTFOUND:
4347 if (errno != 0) {
4348 return errno;
4350 return ENOENT;
4351 case NSS_STATUS_TRYAGAIN:
4352 if (errno != 0) {
4353 return errno;
4355 return ERANGE;
4356 default:
4357 if (errno != 0) {
4358 return errno;
4360 return ret;
4364 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
4365 uid_t uid)
4367 static struct passwd pwd;
4368 static char buf[1000];
4369 NSS_STATUS status;
4371 if (b->symbols->_nss_getpwuid_r.f == NULL) {
4372 return NULL;
4375 status = b->symbols->_nss_getpwuid_r.f(uid,
4376 &pwd,
4377 buf,
4378 sizeof(buf),
4379 &errno);
4380 if (status == NSS_STATUS_NOTFOUND) {
4381 return NULL;
4383 if (status != NSS_STATUS_SUCCESS) {
4384 return NULL;
4386 return &pwd;
4389 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
4390 uid_t uid, struct passwd *pwdst,
4391 char *buf, size_t buflen, struct passwd **pwdstp)
4393 int ret;
4395 *pwdstp = NULL;
4397 if (b->symbols->_nss_getpwuid_r.f == NULL) {
4398 return ENOENT;
4401 ret = b->symbols->_nss_getpwuid_r.f(uid, pwdst, buf, buflen, &errno);
4402 switch (ret) {
4403 case NSS_STATUS_SUCCESS:
4404 *pwdstp = pwdst;
4405 return 0;
4406 case NSS_STATUS_NOTFOUND:
4407 if (errno != 0) {
4408 return errno;
4410 return ENOENT;
4411 case NSS_STATUS_TRYAGAIN:
4412 if (errno != 0) {
4413 return errno;
4415 return ERANGE;
4416 default:
4417 if (errno != 0) {
4418 return errno;
4420 return ret;
4424 static void nwrap_module_setpwent(struct nwrap_backend *b)
4426 if (b->symbols->_nss_setpwent.f == NULL) {
4427 return;
4430 b->symbols->_nss_setpwent.f();
4433 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
4435 static struct passwd pwd;
4436 static char buf[1000];
4437 NSS_STATUS status;
4439 if (b->symbols->_nss_getpwent_r.f == NULL) {
4440 return NULL;
4443 status = b->symbols->_nss_getpwent_r.f(&pwd, buf, sizeof(buf), &errno);
4444 if (status == NSS_STATUS_NOTFOUND) {
4445 return NULL;
4447 if (status != NSS_STATUS_SUCCESS) {
4448 return NULL;
4450 return &pwd;
4453 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
4454 struct passwd *pwdst, char *buf,
4455 size_t buflen, struct passwd **pwdstp)
4457 int ret;
4459 *pwdstp = NULL;
4461 if (b->symbols->_nss_getpwent_r.f == NULL) {
4462 return ENOENT;
4465 ret = b->symbols->_nss_getpwent_r.f(pwdst, buf, buflen, &errno);
4466 switch (ret) {
4467 case NSS_STATUS_SUCCESS:
4468 *pwdstp = pwdst;
4469 return 0;
4470 case NSS_STATUS_NOTFOUND:
4471 if (errno != 0) {
4472 return errno;
4474 return ENOENT;
4475 case NSS_STATUS_TRYAGAIN:
4476 if (errno != 0) {
4477 return errno;
4479 return ERANGE;
4480 default:
4481 if (errno != 0) {
4482 return errno;
4484 return ret;
4488 static void nwrap_module_endpwent(struct nwrap_backend *b)
4490 if (b->symbols->_nss_endpwent.f == NULL) {
4491 return;
4494 b->symbols->_nss_endpwent.f();
4497 static int nwrap_module_initgroups(struct nwrap_backend *b,
4498 const char *user, gid_t group)
4500 gid_t *groups;
4501 long int start;
4502 long int size;
4504 if (b->symbols->_nss_initgroups.f == NULL) {
4505 return NSS_STATUS_UNAVAIL;
4508 return b->symbols->_nss_initgroups.f(user,
4509 group,
4510 &start,
4511 &size,
4512 &groups,
4514 &errno);
4517 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
4518 const char *name)
4520 static struct group grp;
4521 static char *buf;
4522 static int buflen = 1000;
4523 NSS_STATUS status;
4525 if (b->symbols->_nss_getgrnam_r.f == NULL) {
4526 return NULL;
4529 if (!buf) {
4530 buf = (char *)malloc(buflen);
4532 again:
4533 status = b->symbols->_nss_getgrnam_r.f(name, &grp, buf, buflen, &errno);
4534 if (status == NSS_STATUS_TRYAGAIN) {
4535 buflen *= 2;
4536 buf = (char *)realloc(buf, buflen);
4537 if (!buf) {
4538 return NULL;
4540 goto again;
4542 if (status == NSS_STATUS_NOTFOUND) {
4543 SAFE_FREE(buf);
4544 return NULL;
4546 if (status != NSS_STATUS_SUCCESS) {
4547 SAFE_FREE(buf);
4548 return NULL;
4550 return &grp;
4553 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
4554 const char *name, struct group *grdst,
4555 char *buf, size_t buflen, struct group **grdstp)
4557 int ret;
4559 *grdstp = NULL;
4561 if (b->symbols->_nss_getgrnam_r.f == NULL) {
4562 return ENOENT;
4565 ret = b->symbols->_nss_getgrnam_r.f(name, grdst, buf, buflen, &errno);
4566 switch (ret) {
4567 case NSS_STATUS_SUCCESS:
4568 *grdstp = grdst;
4569 return 0;
4570 case NSS_STATUS_NOTFOUND:
4571 if (errno != 0) {
4572 return errno;
4574 return ENOENT;
4575 case NSS_STATUS_TRYAGAIN:
4576 if (errno != 0) {
4577 return errno;
4579 return ERANGE;
4580 default:
4581 if (errno != 0) {
4582 return errno;
4584 return ret;
4588 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
4589 gid_t gid)
4591 static struct group grp;
4592 static char *buf;
4593 static int buflen = 1000;
4594 NSS_STATUS status;
4596 if (b->symbols->_nss_getgrgid_r.f == NULL) {
4597 return NULL;
4600 if (!buf) {
4601 buf = (char *)malloc(buflen);
4604 again:
4605 status = b->symbols->_nss_getgrgid_r.f(gid, &grp, buf, buflen, &errno);
4606 if (status == NSS_STATUS_TRYAGAIN) {
4607 buflen *= 2;
4608 buf = (char *)realloc(buf, buflen);
4609 if (!buf) {
4610 return NULL;
4612 goto again;
4614 if (status == NSS_STATUS_NOTFOUND) {
4615 SAFE_FREE(buf);
4616 return NULL;
4618 if (status != NSS_STATUS_SUCCESS) {
4619 SAFE_FREE(buf);
4620 return NULL;
4622 return &grp;
4625 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
4626 gid_t gid, struct group *grdst,
4627 char *buf, size_t buflen, struct group **grdstp)
4629 int ret;
4631 *grdstp = NULL;
4633 if (b->symbols->_nss_getgrgid_r.f == NULL) {
4634 return ENOENT;
4637 ret = b->symbols->_nss_getgrgid_r.f(gid, grdst, buf, buflen, &errno);
4638 switch (ret) {
4639 case NSS_STATUS_SUCCESS:
4640 *grdstp = grdst;
4641 return 0;
4642 case NSS_STATUS_NOTFOUND:
4643 if (errno != 0) {
4644 return errno;
4646 return ENOENT;
4647 case NSS_STATUS_TRYAGAIN:
4648 if (errno != 0) {
4649 return errno;
4651 return ERANGE;
4652 default:
4653 if (errno != 0) {
4654 return errno;
4656 return ret;
4660 static void nwrap_module_setgrent(struct nwrap_backend *b)
4662 if (b->symbols->_nss_setgrent.f == NULL) {
4663 return;
4666 b->symbols->_nss_setgrent.f();
4669 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
4671 static struct group grp;
4672 static char *buf;
4673 static int buflen = 1024;
4674 NSS_STATUS status;
4676 if (b->symbols->_nss_getgrent_r.f == NULL) {
4677 return NULL;
4680 if (!buf) {
4681 buf = (char *)malloc(buflen);
4684 again:
4685 status = b->symbols->_nss_getgrent_r.f(&grp, buf, buflen, &errno);
4686 if (status == NSS_STATUS_TRYAGAIN) {
4687 buflen *= 2;
4688 buf = (char *)realloc(buf, buflen);
4689 if (!buf) {
4690 return NULL;
4692 goto again;
4694 if (status == NSS_STATUS_NOTFOUND) {
4695 SAFE_FREE(buf);
4696 return NULL;
4698 if (status != NSS_STATUS_SUCCESS) {
4699 SAFE_FREE(buf);
4700 return NULL;
4702 return &grp;
4705 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
4706 struct group *grdst, char *buf,
4707 size_t buflen, struct group **grdstp)
4709 int ret;
4711 *grdstp = NULL;
4713 if (b->symbols->_nss_getgrent_r.f == NULL) {
4714 return ENOENT;
4717 ret = b->symbols->_nss_getgrent_r.f(grdst, buf, buflen, &errno);
4718 switch (ret) {
4719 case NSS_STATUS_SUCCESS:
4720 *grdstp = grdst;
4721 return 0;
4722 case NSS_STATUS_NOTFOUND:
4723 if (errno != 0) {
4724 return errno;
4726 return ENOENT;
4727 case NSS_STATUS_TRYAGAIN:
4728 if (errno != 0) {
4729 return errno;
4731 return ERANGE;
4732 default:
4733 if (errno != 0) {
4734 return errno;
4736 return ret;
4740 static void nwrap_module_endgrent(struct nwrap_backend *b)
4742 if (b->symbols->_nss_endgrent.f == NULL) {
4743 return;
4746 b->symbols->_nss_endgrent.f();
4749 static struct hostent *nwrap_module_gethostbyaddr(struct nwrap_backend *b,
4750 const void *addr,
4751 socklen_t len, int type)
4753 static struct hostent he;
4754 static char *buf = NULL;
4755 static size_t buflen = 1000;
4756 NSS_STATUS status;
4758 if (b->symbols->_nss_gethostbyaddr_r.f == NULL) {
4759 return NULL;
4762 if (buf == NULL) {
4763 buf = (char *)malloc(buflen);
4764 if (buf == NULL) {
4765 return NULL;
4768 again:
4769 status = b->symbols->_nss_gethostbyaddr_r.f(addr,
4770 len,
4771 type,
4772 &he,
4773 buf,
4774 buflen,
4775 &errno,
4776 &h_errno);
4777 if (status == NSS_STATUS_TRYAGAIN) {
4778 char *p = NULL;
4780 buflen *= 2;
4781 p = (char *)realloc(buf, buflen);
4782 if (p == NULL) {
4783 SAFE_FREE(buf);
4784 return NULL;
4786 buf = p;
4787 goto again;
4789 if (status == NSS_STATUS_NOTFOUND) {
4790 SAFE_FREE(buf);
4791 return NULL;
4793 if (status != NSS_STATUS_SUCCESS) {
4794 SAFE_FREE(buf);
4795 return NULL;
4798 return &he;
4801 static int nwrap_module_gethostbyname2_r(struct nwrap_backend *b,
4802 const char *name, int af,
4803 struct hostent *hedst,
4804 char *buf, size_t buflen,
4805 struct hostent **hedstp)
4807 NSS_STATUS status;
4809 *hedstp = NULL;
4811 if (b->symbols->_nss_gethostbyname2_r.f == NULL) {
4812 return ENOENT;
4815 status = b->symbols->_nss_gethostbyname2_r.f(name,
4817 hedst,
4818 buf,
4819 buflen,
4820 &errno,
4821 &h_errno);
4822 switch (status) {
4823 case NSS_STATUS_SUCCESS:
4824 *hedstp = hedst;
4825 return 0;
4826 case NSS_STATUS_NOTFOUND:
4827 if (errno != 0) {
4828 return errno;
4830 return ENOENT;
4831 case NSS_STATUS_TRYAGAIN:
4832 if (errno != 0) {
4833 return errno;
4835 return ERANGE;
4836 default:
4837 if (errno != 0) {
4838 return errno;
4840 return status;
4844 static struct hostent *nwrap_module_gethostbyname(struct nwrap_backend *b,
4845 const char *name)
4847 static struct hostent he;
4848 static char *buf = NULL;
4849 static size_t buflen = 1000;
4850 NSS_STATUS status;
4852 if (b->symbols->_nss_gethostbyname2_r.f == NULL) {
4853 return NULL;
4856 if (buf == NULL) {
4857 buf = (char *)malloc(buflen);
4858 if (buf == NULL) {
4859 return NULL;
4863 again:
4864 status = b->symbols->_nss_gethostbyname2_r.f(name,
4865 AF_UNSPEC,
4866 &he,
4867 buf,
4868 buflen,
4869 &errno,
4870 &h_errno);
4871 if (status == NSS_STATUS_TRYAGAIN) {
4872 char *p = NULL;
4874 buflen *= 2;
4875 p = (char *)realloc(buf, buflen);
4876 if (p == NULL) {
4877 SAFE_FREE(buf);
4878 return NULL;
4880 buf = p;
4881 goto again;
4883 if (status == NSS_STATUS_NOTFOUND) {
4884 SAFE_FREE(buf);
4885 return NULL;
4887 if (status != NSS_STATUS_SUCCESS) {
4888 SAFE_FREE(buf);
4889 return NULL;
4892 return &he;
4895 static struct hostent *nwrap_module_gethostbyname2(struct nwrap_backend *b,
4896 const char *name, int af)
4898 static struct hostent he;
4899 static char *buf = NULL;
4900 static size_t buflen = 1000;
4901 NSS_STATUS status;
4903 if (b->symbols->_nss_gethostbyname2_r.f == NULL) {
4904 return NULL;
4907 if (buf == NULL) {
4908 buf = (char *)malloc(buflen);
4909 if (buf == NULL) {
4910 return NULL;
4914 again:
4915 status = b->symbols->_nss_gethostbyname2_r.f(name,
4917 &he,
4918 buf,
4919 buflen,
4920 &errno,
4921 &h_errno);
4922 if (status == NSS_STATUS_TRYAGAIN) {
4923 char *p = NULL;
4925 buflen *= 2;
4926 p = (char *)realloc(buf, buflen);
4927 if (p == NULL) {
4928 SAFE_FREE(buf);
4929 return NULL;
4931 buf = p;
4932 goto again;
4934 if (status == NSS_STATUS_NOTFOUND) {
4935 SAFE_FREE(buf);
4936 return NULL;
4938 if (status != NSS_STATUS_SUCCESS) {
4939 SAFE_FREE(buf);
4940 return NULL;
4943 return &he;
4946 /****************************************************************************
4947 * GETPWNAM
4948 ***************************************************************************/
4950 static struct passwd *nwrap_getpwnam(const char *name)
4952 size_t i;
4953 struct passwd *pwd;
4955 for (i=0; i < nwrap_main_global->num_backends; i++) {
4956 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4957 pwd = b->ops->nw_getpwnam(b, name);
4958 if (pwd) {
4959 return pwd;
4963 return NULL;
4966 struct passwd *getpwnam(const char *name)
4968 if (!nss_wrapper_enabled()) {
4969 return libc_getpwnam(name);
4972 return nwrap_getpwnam(name);
4975 /****************************************************************************
4976 * GETPWNAM_R
4977 ***************************************************************************/
4979 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
4980 char *buf, size_t buflen, struct passwd **pwdstp)
4982 size_t i;
4983 int ret;
4985 for (i=0; i < nwrap_main_global->num_backends; i++) {
4986 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4987 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
4988 if (ret == ENOENT) {
4989 continue;
4991 return ret;
4994 return ENOENT;
4997 #ifdef HAVE_GETPWNAM_R
4998 # ifdef HAVE_SOLARIS_GETPWNAM_R
4999 int getpwnam_r(const char *name, struct passwd *pwdst,
5000 char *buf, int buflen, struct passwd **pwdstp)
5001 # else /* HAVE_SOLARIS_GETPWNAM_R */
5002 int getpwnam_r(const char *name, struct passwd *pwdst,
5003 char *buf, size_t buflen, struct passwd **pwdstp)
5004 # endif /* HAVE_SOLARIS_GETPWNAM_R */
5006 if (!nss_wrapper_enabled()) {
5007 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
5010 return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
5012 #endif
5014 /****************************************************************************
5015 * GETPWUID
5016 ***************************************************************************/
5018 static struct passwd *nwrap_getpwuid(uid_t uid)
5020 size_t i;
5021 struct passwd *pwd;
5023 for (i=0; i < nwrap_main_global->num_backends; i++) {
5024 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5025 pwd = b->ops->nw_getpwuid(b, uid);
5026 if (pwd) {
5027 return pwd;
5031 return NULL;
5034 struct passwd *getpwuid(uid_t uid)
5036 if (!nss_wrapper_enabled()) {
5037 return libc_getpwuid(uid);
5040 return nwrap_getpwuid(uid);
5043 /****************************************************************************
5044 * GETPWUID_R
5045 ***************************************************************************/
5047 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
5048 char *buf, size_t buflen, struct passwd **pwdstp)
5050 size_t i;
5051 int ret;
5053 for (i=0; i < nwrap_main_global->num_backends; i++) {
5054 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5055 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
5056 if (ret == ENOENT) {
5057 continue;
5059 return ret;
5062 return ENOENT;
5065 #ifdef HAVE_SOLARIS_GETPWUID_R
5066 int getpwuid_r(uid_t uid, struct passwd *pwdst,
5067 char *buf, int buflen, struct passwd **pwdstp)
5068 #else
5069 int getpwuid_r(uid_t uid, struct passwd *pwdst,
5070 char *buf, size_t buflen, struct passwd **pwdstp)
5071 #endif
5073 if (!nss_wrapper_enabled()) {
5074 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
5077 return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
5080 /****************************************************************************
5081 * SETPWENT
5082 ***************************************************************************/
5084 static void nwrap_setpwent(void)
5086 size_t i;
5088 for (i=0; i < nwrap_main_global->num_backends; i++) {
5089 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5090 b->ops->nw_setpwent(b);
5094 void setpwent(void)
5096 if (!nss_wrapper_enabled()) {
5097 libc_setpwent();
5098 return;
5101 nwrap_setpwent();
5104 /****************************************************************************
5105 * GETPWENT
5106 ***************************************************************************/
5108 static struct passwd *nwrap_getpwent(void)
5110 size_t i;
5111 struct passwd *pwd;
5113 for (i=0; i < nwrap_main_global->num_backends; i++) {
5114 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5115 pwd = b->ops->nw_getpwent(b);
5116 if (pwd) {
5117 return pwd;
5121 return NULL;
5124 struct passwd *getpwent(void)
5126 if (!nss_wrapper_enabled()) {
5127 return libc_getpwent();
5130 return nwrap_getpwent();
5133 /****************************************************************************
5134 * GETPWENT_R
5135 ***************************************************************************/
5137 #ifdef HAVE_GETPWENT_R
5138 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
5139 size_t buflen, struct passwd **pwdstp)
5141 size_t i;
5142 int ret;
5144 for (i=0; i < nwrap_main_global->num_backends; i++) {
5145 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5146 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
5147 if (ret == ENOENT) {
5148 continue;
5150 return ret;
5153 return ENOENT;
5156 # ifdef HAVE_SOLARIS_GETPWENT_R
5157 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
5159 struct passwd *pwdstp = NULL;
5160 int rc;
5162 if (!nss_wrapper_enabled()) {
5163 return libc_getpwent_r(pwdst, buf, buflen);
5165 rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
5166 if (rc < 0) {
5167 return NULL;
5170 return pwdstp;
5172 # else /* HAVE_SOLARIS_GETPWENT_R */
5173 int getpwent_r(struct passwd *pwdst, char *buf,
5174 size_t buflen, struct passwd **pwdstp)
5176 if (!nss_wrapper_enabled()) {
5177 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
5180 return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
5182 # endif /* HAVE_SOLARIS_GETPWENT_R */
5183 #endif /* HAVE_GETPWENT_R */
5185 /****************************************************************************
5186 * ENDPWENT
5187 ***************************************************************************/
5189 static void nwrap_endpwent(void)
5191 size_t i;
5193 for (i=0; i < nwrap_main_global->num_backends; i++) {
5194 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5195 b->ops->nw_endpwent(b);
5199 void endpwent(void)
5201 if (!nss_wrapper_enabled()) {
5202 libc_endpwent();
5203 return;
5206 nwrap_endpwent();
5209 /****************************************************************************
5210 * INITGROUPS
5211 ***************************************************************************/
5213 static int nwrap_initgroups(const char *user, gid_t group)
5215 size_t i;
5217 for (i=0; i < nwrap_main_global->num_backends; i++) {
5218 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5219 int rc;
5221 rc = b->ops->nw_initgroups(b, user, group);
5222 if (rc == 0) {
5223 return 0;
5227 errno = ENOENT;
5228 return -1;
5231 int initgroups(const char *user, gid_t group)
5233 if (!nss_wrapper_enabled()) {
5234 return libc_initgroups(user, group);
5237 return nwrap_initgroups(user, group);
5240 /****************************************************************************
5241 * GETGRNAM
5242 ***************************************************************************/
5244 static struct group *nwrap_getgrnam(const char *name)
5246 size_t i;
5247 struct group *grp;
5249 for (i=0; i < nwrap_main_global->num_backends; i++) {
5250 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5251 grp = b->ops->nw_getgrnam(b, name);
5252 if (grp) {
5253 return grp;
5257 return NULL;
5260 struct group *getgrnam(const char *name)
5262 if (!nss_wrapper_enabled()) {
5263 return libc_getgrnam(name);
5266 return nwrap_getgrnam(name);
5269 /****************************************************************************
5270 * GETGRNAM_R
5271 ***************************************************************************/
5273 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
5274 char *buf, size_t buflen, struct group **grdstp)
5276 size_t i;
5277 int ret;
5279 for (i=0; i < nwrap_main_global->num_backends; i++) {
5280 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5281 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
5282 if (ret == ENOENT) {
5283 continue;
5285 return ret;
5288 return ENOENT;
5291 #ifdef HAVE_GETGRNAM_R
5292 # ifdef HAVE_SOLARIS_GETGRNAM_R
5293 int getgrnam_r(const char *name, struct group *grp,
5294 char *buf, int buflen, struct group **pgrp)
5295 # else /* HAVE_SOLARIS_GETGRNAM_R */
5296 int getgrnam_r(const char *name, struct group *grp,
5297 char *buf, size_t buflen, struct group **pgrp)
5298 # endif /* HAVE_SOLARIS_GETGRNAM_R */
5300 if (!nss_wrapper_enabled()) {
5301 return libc_getgrnam_r(name,
5302 grp,
5303 buf,
5304 buflen,
5305 pgrp);
5308 return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
5310 #endif /* HAVE_GETGRNAM_R */
5312 /****************************************************************************
5313 * GETGRGID
5314 ***************************************************************************/
5316 static struct group *nwrap_getgrgid(gid_t gid)
5318 size_t i;
5319 struct group *grp;
5321 for (i=0; i < nwrap_main_global->num_backends; i++) {
5322 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5323 grp = b->ops->nw_getgrgid(b, gid);
5324 if (grp) {
5325 return grp;
5329 return NULL;
5332 struct group *getgrgid(gid_t gid)
5334 if (!nss_wrapper_enabled()) {
5335 return libc_getgrgid(gid);
5338 return nwrap_getgrgid(gid);
5341 /****************************************************************************
5342 * GETGRGID_R
5343 ***************************************************************************/
5345 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
5346 char *buf, size_t buflen, struct group **grdstp)
5348 size_t i;
5349 int ret;
5351 for (i=0; i < nwrap_main_global->num_backends; i++) {
5352 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5353 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
5354 if (ret == ENOENT) {
5355 continue;
5357 return ret;
5360 return ENOENT;
5363 #ifdef HAVE_GETGRGID_R
5364 # ifdef HAVE_SOLARIS_GETGRGID_R
5365 int getgrgid_r(gid_t gid, struct group *grdst,
5366 char *buf, int buflen, struct group **grdstp)
5367 # else /* HAVE_SOLARIS_GETGRGID_R */
5368 int getgrgid_r(gid_t gid, struct group *grdst,
5369 char *buf, size_t buflen, struct group **grdstp)
5370 # endif /* HAVE_SOLARIS_GETGRGID_R */
5372 if (!nss_wrapper_enabled()) {
5373 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
5376 return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
5378 #endif
5380 /****************************************************************************
5381 * SETGRENT
5382 ***************************************************************************/
5384 static void nwrap_setgrent(void)
5386 size_t i;
5388 for (i=0; i < nwrap_main_global->num_backends; i++) {
5389 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5390 b->ops->nw_setgrent(b);
5394 #ifdef HAVE_BSD_SETGRENT
5395 int setgrent(void)
5396 #else
5397 void setgrent(void)
5398 #endif
5400 if (!nss_wrapper_enabled()) {
5401 libc_setgrent();
5402 goto out;
5405 nwrap_setgrent();
5407 out:
5408 #ifdef HAVE_BSD_SETGRENT
5409 return 0;
5410 #else
5411 return;
5412 #endif
5415 /****************************************************************************
5416 * GETGRENT
5417 ***************************************************************************/
5419 static struct group *nwrap_getgrent(void)
5421 size_t i;
5422 struct group *grp;
5424 for (i=0; i < nwrap_main_global->num_backends; i++) {
5425 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5426 grp = b->ops->nw_getgrent(b);
5427 if (grp) {
5428 return grp;
5432 return NULL;
5435 struct group *getgrent(void)
5437 if (!nss_wrapper_enabled()) {
5438 return libc_getgrent();
5441 return nwrap_getgrent();
5444 /****************************************************************************
5445 * GETGRENT_R
5446 ***************************************************************************/
5448 #ifdef HAVE_GETGRENT_R
5449 static int nwrap_getgrent_r(struct group *grdst, char *buf,
5450 size_t buflen, struct group **grdstp)
5452 size_t i;
5453 int ret;
5455 for (i=0; i < nwrap_main_global->num_backends; i++) {
5456 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5457 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
5458 if (ret == ENOENT) {
5459 continue;
5461 return ret;
5464 return ENOENT;
5467 # ifdef HAVE_SOLARIS_GETGRENT_R
5468 struct group *getgrent_r(struct group *src, char *buf, int buflen)
5470 struct group *grdstp = NULL;
5471 int rc;
5473 if (!nss_wrapper_enabled()) {
5474 return libc_getgrent_r(src, buf, buflen);
5477 rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
5478 if (rc < 0) {
5479 return NULL;
5482 return grdstp;
5484 # else /* HAVE_SOLARIS_GETGRENT_R */
5485 int getgrent_r(struct group *src, char *buf,
5486 size_t buflen, struct group **grdstp)
5488 if (!nss_wrapper_enabled()) {
5489 return libc_getgrent_r(src, buf, buflen, grdstp);
5492 return nwrap_getgrent_r(src, buf, buflen, grdstp);
5494 # endif /* HAVE_SOLARIS_GETGRENT_R */
5495 #endif /* HAVE_GETGRENT_R */
5497 /****************************************************************************
5498 * ENDGRENT
5499 ***************************************************************************/
5501 static void nwrap_endgrent(void)
5503 size_t i;
5505 for (i=0; i < nwrap_main_global->num_backends; i++) {
5506 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5507 b->ops->nw_endgrent(b);
5511 void endgrent(void)
5513 if (!nss_wrapper_enabled()) {
5514 libc_endgrent();
5515 return;
5518 nwrap_endgrent();
5521 /****************************************************************************
5522 * GETGROUPLIST
5523 ***************************************************************************/
5525 #ifdef HAVE_GETGROUPLIST
5526 static int nwrap_getgrouplist(const char *user, gid_t group,
5527 gid_t *groups, int *ngroups)
5529 struct group *grp;
5530 gid_t *groups_tmp;
5531 int count = 1;
5533 NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
5535 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
5536 if (!groups_tmp) {
5537 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
5538 errno = ENOMEM;
5539 return -1;
5541 groups_tmp[0] = group;
5543 nwrap_setgrent();
5544 while ((grp = nwrap_getgrent()) != NULL) {
5545 int i = 0;
5547 NWRAP_LOG(NWRAP_LOG_DEBUG,
5548 "Inspecting %s for group membership",
5549 grp->gr_name);
5551 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
5553 if (group != grp->gr_gid &&
5554 (strcmp(user, grp->gr_mem[i]) == 0)) {
5556 NWRAP_LOG(NWRAP_LOG_DEBUG,
5557 "%s is member of %s",
5558 user,
5559 grp->gr_name);
5561 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
5562 if (!groups_tmp) {
5563 NWRAP_LOG(NWRAP_LOG_ERROR,
5564 "Out of memory");
5565 errno = ENOMEM;
5566 return -1;
5568 groups_tmp[count] = grp->gr_gid;
5570 count++;
5575 nwrap_endgrent();
5577 NWRAP_LOG(NWRAP_LOG_DEBUG,
5578 "%s is member of %d groups",
5579 user, *ngroups);
5581 if (*ngroups < count) {
5582 *ngroups = count;
5583 free(groups_tmp);
5584 return -1;
5587 *ngroups = count;
5588 memcpy(groups, groups_tmp, count * sizeof(gid_t));
5589 free(groups_tmp);
5591 return count;
5594 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
5596 if (!nss_wrapper_enabled()) {
5597 return libc_getgrouplist(user, group, groups, ngroups);
5600 return nwrap_getgrouplist(user, group, groups, ngroups);
5602 #endif
5604 /**********************************************************
5605 * SHADOW
5606 **********************************************************/
5608 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
5610 #ifdef HAVE_SETSPENT
5611 static void nwrap_setspent(void)
5613 nwrap_files_setspent();
5616 void setspent(void)
5618 if (!nss_wrapper_shadow_enabled()) {
5619 return;
5622 nwrap_setspent();
5625 static struct spwd *nwrap_getspent(void)
5627 return nwrap_files_getspent();
5630 struct spwd *getspent(void)
5632 if (!nss_wrapper_shadow_enabled()) {
5633 return NULL;
5636 return nwrap_getspent();
5639 static void nwrap_endspent(void)
5641 nwrap_files_endspent();
5644 void endspent(void)
5646 if (!nss_wrapper_shadow_enabled()) {
5647 return;
5650 nwrap_endspent();
5652 #endif /* HAVE_SETSPENT */
5654 static struct spwd *nwrap_getspnam(const char *name)
5656 return nwrap_files_getspnam(name);
5659 struct spwd *getspnam(const char *name)
5661 if (!nss_wrapper_shadow_enabled()) {
5662 return NULL;
5665 return nwrap_getspnam(name);
5668 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
5670 /**********************************************************
5671 * NETDB
5672 **********************************************************/
5674 static void nwrap_sethostent(int stayopen) {
5675 (void) stayopen; /* ignored */
5677 nwrap_files_sethostent();
5680 #ifdef HAVE_SOLARIS_SETHOSTENT
5681 int sethostent(int stayopen)
5683 if (!nss_wrapper_hosts_enabled()) {
5684 libc_sethostent(stayopen);
5685 return 0;
5688 nwrap_sethostent(stayopen);
5690 return 0;
5692 #else /* HAVE_SOLARIS_SETHOSTENT */
5693 void sethostent(int stayopen)
5695 if (!nss_wrapper_hosts_enabled()) {
5696 libc_sethostent(stayopen);
5697 return;
5700 nwrap_sethostent(stayopen);
5702 #endif /* HAVE_SOLARIS_SETHOSTENT */
5704 static struct hostent *nwrap_gethostent(void)
5706 return nwrap_files_gethostent();
5709 struct hostent *gethostent(void) {
5710 if (!nss_wrapper_hosts_enabled()) {
5711 return libc_gethostent();
5714 return nwrap_gethostent();
5717 static void nwrap_endhostent(void) {
5718 nwrap_files_endhostent();
5721 #ifdef HAVE_SOLARIS_ENDHOSTENT
5722 int endhostent(void)
5724 if (!nss_wrapper_hosts_enabled()) {
5725 libc_endhostent();
5726 return 0;
5729 nwrap_endhostent();
5731 return 0;
5733 #else /* HAVE_SOLARIS_ENDHOSTENT */
5734 void endhostent(void)
5736 if (!nss_wrapper_hosts_enabled()) {
5737 libc_endhostent();
5738 return;
5741 nwrap_endhostent();
5743 #endif /* HAVE_SOLARIS_ENDHOSTENT */
5746 #ifdef BSD
5747 /* BSD implementation stores data in thread local storage but GLIBC does not */
5748 static __thread struct hostent user_he;
5749 static __thread struct nwrap_vector user_addrlist;
5750 #else
5751 static struct hostent user_he;
5752 static struct nwrap_vector user_addrlist;
5753 #endif /* BSD */
5755 static struct hostent *nwrap_files_gethostbyname(struct nwrap_backend *b,
5756 const char *name)
5758 int ret;
5760 (void) b; /* unused */
5762 ret = nwrap_files_internal_gethostbyname(name, AF_UNSPEC, &user_he,
5763 &user_addrlist);
5764 if (ret == 0) {
5765 return &user_he;
5768 return NULL;
5771 static struct hostent *nwrap_gethostbyname(const char *name)
5773 size_t i;
5774 struct hostent *he = NULL;
5776 for (i=0; i < nwrap_main_global->num_backends; i++) {
5777 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5778 he = b->ops->nw_gethostbyname(b, name);
5779 if (he != NULL) {
5780 return he;
5784 return NULL;
5787 struct hostent *gethostbyname(const char *name)
5789 if (!nss_wrapper_hosts_enabled()) {
5790 return libc_gethostbyname(name);
5793 return nwrap_gethostbyname(name);
5796 /* This is a GNU extension - Also can be found on BSD systems */
5797 #ifdef HAVE_GETHOSTBYNAME2
5798 #ifdef BSD
5799 /* BSD implementation stores data in thread local storage but GLIBC not */
5800 static __thread struct hostent user_he2;
5801 static __thread struct nwrap_vector user_addrlist2;
5802 #else
5803 static struct hostent user_he2;
5804 static struct nwrap_vector user_addrlist2;
5805 #endif /* BSD */
5807 static struct hostent *nwrap_files_gethostbyname2(struct nwrap_backend *b,
5808 const char *name, int af)
5810 int ret;
5812 (void) b; /* unused */
5814 ret = nwrap_files_internal_gethostbyname(name, af, &user_he2,
5815 &user_addrlist2);
5816 if (ret == 0) {
5817 return &user_he2;
5820 return NULL;
5823 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
5825 size_t i;
5826 struct hostent *he = NULL;
5828 for (i=0; i < nwrap_main_global->num_backends; i++) {
5829 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5830 he = b->ops->nw_gethostbyname2(b, name, af);
5831 if (he != NULL) {
5832 return he;
5836 return NULL;
5839 struct hostent *gethostbyname2(const char *name, int af)
5841 if (!nss_wrapper_hosts_enabled()) {
5842 return libc_gethostbyname2(name, af);
5845 return nwrap_gethostbyname2(name, af);
5847 #endif
5849 static struct hostent *nwrap_gethostbyaddr(const void *addr,
5850 socklen_t len, int type)
5852 size_t i;
5853 struct hostent *he = NULL;
5855 for (i=0; i < nwrap_main_global->num_backends; i++) {
5856 struct nwrap_backend *b = &nwrap_main_global->backends[i];
5857 he = b->ops->nw_gethostbyaddr(b, addr, len, type);
5858 if (he != NULL) {
5859 return he;
5863 return NULL;
5866 struct hostent *gethostbyaddr(const void *addr,
5867 socklen_t len, int type)
5869 if (!nss_wrapper_hosts_enabled()) {
5870 return libc_gethostbyaddr(addr, len, type);
5873 return nwrap_gethostbyaddr(addr, len, type);
5876 static const struct addrinfo default_hints =
5878 .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
5879 .ai_family = AF_UNSPEC,
5880 .ai_socktype = 0,
5881 .ai_protocol = 0,
5882 .ai_addrlen = 0,
5883 .ai_addr = NULL,
5884 .ai_canonname = NULL,
5885 .ai_next = NULL
5888 static int nwrap_convert_he_ai(const struct hostent *he,
5889 unsigned short port,
5890 const struct addrinfo *hints,
5891 struct addrinfo **pai,
5892 bool skip_canonname)
5894 struct addrinfo *ai;
5895 socklen_t socklen;
5897 if (he == NULL) {
5898 return EAI_MEMORY;
5901 switch (he->h_addrtype) {
5902 case AF_INET:
5903 socklen = sizeof(struct sockaddr_in);
5904 break;
5905 #ifdef HAVE_IPV6
5906 case AF_INET6:
5907 socklen = sizeof(struct sockaddr_in6);
5908 break;
5909 #endif
5910 default:
5911 return EAI_FAMILY;
5914 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
5915 if (ai == NULL) {
5916 return EAI_MEMORY;
5919 ai->ai_flags = hints->ai_flags;
5920 ai->ai_family = he->h_addrtype;
5921 ai->ai_socktype = hints->ai_socktype;
5922 ai->ai_protocol = hints->ai_protocol;
5923 ai->ai_canonname = NULL;
5925 if (ai->ai_socktype == 0) {
5926 ai->ai_socktype = SOCK_DGRAM;
5928 if (ai->ai_protocol == 0) {
5929 if (ai->ai_socktype == SOCK_DGRAM) {
5930 ai->ai_protocol = IPPROTO_UDP;
5931 } else if (ai->ai_socktype == SOCK_STREAM) {
5932 ai->ai_protocol = IPPROTO_TCP;
5936 ai->ai_addrlen = socklen;
5937 ai->ai_addr = (void *)(ai + 1);
5939 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
5940 ai->ai_addr->sa_len = socklen;
5941 #endif
5942 ai->ai_addr->sa_family = he->h_addrtype;
5944 switch (he->h_addrtype) {
5945 case AF_INET:
5947 union {
5948 struct sockaddr *sa;
5949 struct sockaddr_in *in;
5950 } addr;
5952 addr.sa = ai->ai_addr;
5954 memset(addr.in, 0, sizeof(struct sockaddr_in));
5956 addr.in->sin_port = htons(port);
5957 addr.in->sin_family = AF_INET;
5959 memset(addr.in->sin_zero,
5960 '\0',
5961 sizeof (addr.in->sin_zero));
5962 memcpy(&(addr.in->sin_addr),
5963 he->h_addr_list[0],
5964 he->h_length);
5967 break;
5968 #ifdef HAVE_IPV6
5969 case AF_INET6:
5971 union {
5972 struct sockaddr *sa;
5973 struct sockaddr_in6 *in6;
5974 } addr;
5976 addr.sa = ai->ai_addr;
5978 memset(addr.in6, 0, sizeof(struct sockaddr_in6));
5980 addr.in6->sin6_port = htons(port);
5981 addr.in6->sin6_family = AF_INET6;
5983 memcpy(&addr.in6->sin6_addr,
5984 he->h_addr_list[0],
5985 he->h_length);
5987 break;
5988 #endif
5991 ai->ai_next = NULL;
5993 if (he->h_name && !skip_canonname) {
5994 ai->ai_canonname = strdup(he->h_name);
5995 if (ai->ai_canonname == NULL) {
5996 freeaddrinfo(ai);
5997 return EAI_MEMORY;
6001 *pai = ai;
6002 return 0;
6005 static int nwrap_getaddrinfo(const char *node,
6006 const char *service,
6007 const struct addrinfo *hints,
6008 struct addrinfo **res)
6010 struct addrinfo *ai = NULL;
6011 unsigned short port = 0;
6012 struct {
6013 int family;
6014 union {
6015 struct in_addr v4;
6016 #ifdef HAVE_IPV6
6017 struct in6_addr v6;
6018 } in;
6019 #endif
6020 } addr = {
6021 .family = AF_UNSPEC,
6023 int rc;
6025 if (node == NULL && service == NULL) {
6026 return EAI_NONAME;
6029 if (hints == NULL) {
6030 hints = &default_hints;
6033 /* EAI_BADFLAGS
6034 hints.ai_flags contains invalid flags; or, hints.ai_flags
6035 included AI_CANONNAME and name was NULL.
6037 if ((hints->ai_flags & AI_CANONNAME) && (node == NULL)) {
6038 return EAI_BADFLAGS;
6041 /* If no node has been specified, let glibc deal with it */
6042 if (node == NULL) {
6043 int ret;
6044 struct addrinfo *p = NULL;
6046 ret = libc_getaddrinfo(node, service, hints, &p);
6048 if (ret == 0) {
6049 *res = p;
6051 return ret;
6054 if (service != NULL && service[0] != '\0') {
6055 const char *proto = NULL;
6056 struct servent *s;
6057 char *end_ptr;
6058 long sl;
6060 errno = 0;
6061 sl = strtol(service, &end_ptr, 10);
6063 if (*end_ptr == '\0') {
6064 port = sl;
6065 goto valid_port;
6066 } else if (hints->ai_flags & AI_NUMERICSERV) {
6067 return EAI_NONAME;
6070 if (hints->ai_protocol != 0) {
6071 struct protoent *pent;
6073 pent = getprotobynumber(hints->ai_protocol);
6074 if (pent != NULL) {
6075 proto = pent->p_name;
6079 s = getservbyname(service, proto);
6080 if (s == NULL) {
6081 return EAI_NONAME;
6083 port = ntohs(s->s_port);
6086 valid_port:
6088 rc = inet_pton(AF_INET, node, &addr.in.v4);
6089 if (rc == 1) {
6090 addr.family = AF_INET;
6092 #ifdef HAVE_IPV6
6093 if (addr.family == AF_UNSPEC) {
6094 rc = inet_pton(AF_INET6, node, &addr.in.v6);
6095 if (rc == 1) {
6096 addr.family = AF_INET6;
6099 #endif
6101 if (addr.family == AF_UNSPEC) {
6102 if (hints->ai_flags & AI_NUMERICHOST) {
6103 return EAI_NONAME;
6105 } else if ((hints->ai_family != AF_UNSPEC) &&
6106 (hints->ai_family != addr.family))
6108 return EAI_ADDRFAMILY;
6111 rc = nwrap_files_getaddrinfo(node, port, hints, &ai);
6112 if (rc != 0) {
6113 int ret;
6114 struct addrinfo *p = NULL;
6116 ret = libc_getaddrinfo(node, service, hints, &p);
6118 if (ret == 0) {
6120 * nwrap_files_getaddrinfo failed, but libc was
6121 * successful -- use the result from libc.
6123 *res = p;
6124 return 0;
6127 return rc;
6131 * If the socktype was not specified, duplicate
6132 * each ai returned, so that we have variants for
6133 * both UDP and TCP.
6135 if (hints->ai_socktype == 0) {
6136 struct addrinfo *ai_cur;
6138 /* freeaddrinfo() frees ai_canonname and ai so allocate them */
6139 for (ai_cur = ai; ai_cur != NULL; ai_cur = ai_cur->ai_next) {
6140 struct addrinfo *ai_new;
6142 /* duplicate the current entry */
6144 ai_new = malloc(sizeof(struct addrinfo));
6145 if (ai_new == NULL) {
6146 freeaddrinfo(ai);
6147 return EAI_MEMORY;
6150 memcpy(ai_new, ai_cur, sizeof(struct addrinfo));
6151 ai_new->ai_next = NULL;
6153 /* We need a deep copy or freeaddrinfo() will blow up */
6154 if (ai_cur->ai_canonname != NULL) {
6155 ai_new->ai_canonname =
6156 strdup(ai_cur->ai_canonname);
6159 if (ai_cur->ai_socktype == SOCK_DGRAM) {
6160 ai_new->ai_socktype = SOCK_STREAM;
6161 } else if (ai_cur->ai_socktype == SOCK_STREAM) {
6162 ai_new->ai_socktype = SOCK_DGRAM;
6164 if (ai_cur->ai_protocol == IPPROTO_TCP) {
6165 ai_new->ai_protocol = IPPROTO_UDP;
6166 } else if (ai_cur->ai_protocol == IPPROTO_UDP) {
6167 ai_new->ai_protocol = IPPROTO_TCP;
6170 /* now insert the new entry */
6172 ai_new->ai_next = ai_cur->ai_next;
6173 ai_cur->ai_next = ai_new;
6175 /* and move on (don't duplicate the new entry) */
6177 ai_cur = ai_new;
6181 *res = ai;
6183 return 0;
6186 int getaddrinfo(const char *node, const char *service,
6187 const struct addrinfo *hints,
6188 struct addrinfo **res)
6190 if (!nss_wrapper_hosts_enabled()) {
6191 return libc_getaddrinfo(node, service, hints, res);
6194 return nwrap_getaddrinfo(node, service, hints, res);
6197 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
6198 char *host, size_t hostlen,
6199 char *serv, size_t servlen,
6200 int flags)
6202 struct hostent *he;
6203 struct servent *service;
6204 const char *proto;
6205 const void *addr;
6206 socklen_t addrlen;
6207 uint16_t port;
6208 sa_family_t type;
6209 size_t i;
6211 if (sa == NULL || salen < sizeof(sa_family_t)) {
6212 return EAI_FAMILY;
6215 if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
6216 return EAI_NONAME;
6219 type = sa->sa_family;
6220 switch (type) {
6221 case AF_INET: {
6222 union {
6223 const struct sockaddr *sa;
6224 const struct sockaddr_in *in;
6225 } a;
6227 if (salen < sizeof(struct sockaddr_in)) {
6228 return EAI_FAMILY;
6231 a.sa = sa;
6233 addr = &(a.in->sin_addr);
6234 addrlen = sizeof(a.in->sin_addr);
6235 port = ntohs(a.in->sin_port);
6236 break;
6238 #ifdef HAVE_IPV6
6239 case AF_INET6: {
6240 union {
6241 const struct sockaddr *sa;
6242 const struct sockaddr_in6 *in6;
6243 } a;
6245 if (salen < sizeof(struct sockaddr_in6)) {
6246 return EAI_FAMILY;
6249 a.sa = sa;
6251 addr = &(a.in6->sin6_addr);
6252 addrlen = sizeof(a.in6->sin6_addr);
6253 port = ntohs(a.in6->sin6_port);
6254 break;
6256 #endif
6257 default:
6258 return EAI_FAMILY;
6261 if (host != NULL) {
6262 he = NULL;
6263 if ((flags & NI_NUMERICHOST) == 0) {
6264 for (i=0; i < nwrap_main_global->num_backends; i++) {
6265 struct nwrap_backend *b = &nwrap_main_global->backends[i];
6266 he = b->ops->nw_gethostbyaddr(b, addr, addrlen, type);
6267 if (he != NULL) {
6268 break;
6271 if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
6272 return EAI_NONAME;
6274 if (he != NULL && he->h_name != NULL) {
6275 if (strlen(he->h_name) >= hostlen)
6276 return EAI_OVERFLOW;
6277 snprintf(host, hostlen, "%s", he->h_name);
6278 if (flags & NI_NOFQDN)
6279 host[strcspn(host, ".")] = '\0';
6280 } else {
6281 if (inet_ntop(type, addr, host, hostlen) == NULL)
6282 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
6286 if (serv != NULL) {
6287 service = NULL;
6288 if ((flags & NI_NUMERICSERV) == 0) {
6289 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
6290 service = getservbyport(htons(port), proto);
6292 if (service != NULL) {
6293 if (strlen(service->s_name) >= servlen)
6294 return EAI_OVERFLOW;
6295 snprintf(serv, servlen, "%s", service->s_name);
6296 } else {
6297 if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
6298 return EAI_OVERFLOW;
6302 return 0;
6305 #ifdef HAVE_LINUX_GETNAMEINFO
6306 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
6307 char *host, socklen_t hostlen,
6308 char *serv, socklen_t servlen,
6309 int flags)
6310 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
6311 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
6312 char *host, socklen_t hostlen,
6313 char *serv, socklen_t servlen,
6314 unsigned int flags)
6315 #else
6316 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
6317 char *host, size_t hostlen,
6318 char *serv, size_t servlen,
6319 int flags)
6320 #endif
6322 if (!nss_wrapper_hosts_enabled()) {
6323 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
6326 return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
6329 static int nwrap_gethostname(char *name, size_t len)
6331 const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
6333 if (strlen(hostname) >= len) {
6334 errno = ENAMETOOLONG;
6335 return -1;
6337 snprintf(name, len, "%s", hostname);
6339 return 0;
6342 #ifdef HAVE_SOLARIS_GETHOSTNAME
6343 int gethostname(char *name, int len)
6344 #else /* HAVE_SOLARIS_GETHOSTNAME */
6345 int gethostname(char *name, size_t len)
6346 #endif /* HAVE_SOLARIS_GETHOSTNAME */
6348 if (!nwrap_hostname_enabled()) {
6349 return libc_gethostname(name, len);
6352 return nwrap_gethostname(name, len);
6355 /****************************
6356 * CONSTRUCTOR
6357 ***************************/
6358 void nwrap_constructor(void)
6361 * If we hold a lock and the application forks, then the child
6362 * is not able to unlock the mutex and we are in a deadlock.
6364 * Setting these handlers should prevent such deadlocks.
6366 pthread_atfork(&nwrap_thread_prepare,
6367 &nwrap_thread_parent,
6368 &nwrap_thread_child);
6370 /* Do not call nwrap_init() here. */
6373 /****************************
6374 * DESTRUCTOR
6375 ***************************/
6378 * This function is called when the library is unloaded and makes sure that
6379 * sockets get closed and the unix file for the socket are unlinked.
6381 void nwrap_destructor(void)
6383 size_t i;
6385 NWRAP_LOCK_ALL;
6386 if (nwrap_main_global != NULL) {
6387 struct nwrap_main *m = nwrap_main_global;
6389 /* libc */
6390 if (m->libc != NULL) {
6391 if (m->libc->handle != NULL) {
6392 dlclose(m->libc->handle);
6394 if (m->libc->nsl_handle != NULL) {
6395 dlclose(m->libc->nsl_handle);
6397 if (m->libc->sock_handle != NULL) {
6398 dlclose(m->libc->sock_handle);
6400 SAFE_FREE(m->libc);
6403 /* backends */
6404 if (m->backends != NULL) {
6405 for (i = 0; i < m->num_backends; i++) {
6406 struct nwrap_backend *b = &(m->backends[i]);
6408 if (b->so_handle != NULL) {
6409 dlclose(b->so_handle);
6411 SAFE_FREE(b->symbols);
6413 SAFE_FREE(m->backends);
6417 if (nwrap_pw_global.cache != NULL) {
6418 struct nwrap_cache *c = nwrap_pw_global.cache;
6420 nwrap_files_cache_unload(c);
6421 if (c->fd >= 0) {
6422 fclose(c->fp);
6423 c->fd = -1;
6426 SAFE_FREE(nwrap_pw_global.list);
6427 nwrap_pw_global.num = 0;
6430 if (nwrap_gr_global.cache != NULL) {
6431 struct nwrap_cache *c = nwrap_gr_global.cache;
6433 nwrap_files_cache_unload(c);
6434 if (c->fd >= 0) {
6435 fclose(c->fp);
6436 c->fd = -1;
6439 SAFE_FREE(nwrap_gr_global.list);
6440 nwrap_pw_global.num = 0;
6443 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
6444 if (nwrap_sp_global.cache != NULL) {
6445 struct nwrap_cache *c = nwrap_sp_global.cache;
6447 nwrap_files_cache_unload(c);
6448 if (c->fd >= 0) {
6449 fclose(c->fp);
6450 c->fd = -1;
6453 nwrap_sp_global.num = 0;
6455 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
6457 if (nwrap_he_global.cache != NULL) {
6458 struct nwrap_cache *c = nwrap_he_global.cache;
6460 nwrap_files_cache_unload(c);
6461 if (c->fd >= 0) {
6462 fclose(c->fp);
6463 c->fd = -1;
6466 nwrap_he_global.num = 0;
6469 free(user_addrlist.items);
6470 #ifdef HAVE_GETHOSTBYNAME2
6471 free(user_addrlist2.items);
6472 #endif
6474 hdestroy();
6475 NWRAP_UNLOCK_ALL;