2 * Copyright (C) Stefan Metzmacher 2007 <metze@samba.org>
3 * Copyright (C) Guenther Deschner 2009 <gd@samba.org>
4 * Copyright (C) Andreas Schneider 2013 <asn@samba.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the author nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/types.h>
42 #include <sys/socket.h>
59 * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h gives us
60 * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
63 #ifndef _POSIX_PTHREAD_SEMANTICS
64 #define _POSIX_PTHREAD_SEMANTICS
71 #endif /* HAVE_SHADOW_H */
74 #include <arpa/inet.h>
75 #include <netinet/in.h>
79 #if defined(HAVE_NSS_H)
83 typedef enum nss_status NSS_STATUS;
84 #elif defined(HAVE_NSS_COMMON_H)
86 #include <nss_common.h>
87 #include <nss_dbdefs.h>
90 typedef nss_status_t NSS_STATUS;
92 # define NSS_STATUS_SUCCESS NSS_SUCCESS
93 # define NSS_STATUS_NOTFOUND NSS_NOTFOUND
94 # define NSS_STATUS_UNAVAIL NSS_UNAVAIL
95 # define NSS_STATUS_TRYAGAIN NSS_TRYAGAIN
97 # error "No nsswitch support detected"
101 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
109 #define EAI_NODATA EAI_NONAME
112 #ifndef EAI_ADDRFAMILY
113 #define EAI_ADDRFAMILY EAI_FAMILY
117 #define __STRING(x) #x
120 #ifndef __STRINGSTRING
121 #define __STRINGSTRING(x) __STRING(x)
125 #define __LINESTR__ __STRINGSTRING(__LINE__)
129 #define __location__ __FILE__ ":" __LINESTR__
133 #define DNS_NAME_MAX 255
136 /* GCC have printf type attribute check. */
137 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
138 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
140 #define PRINTF_ATTRIBUTE(a,b)
141 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
143 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
144 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
146 #define DESTRUCTOR_ATTRIBUTE
147 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
149 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
152 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
156 #define NWRAP_INET_ADDRSTRLEN INET6_ADDRSTRLEN
158 #define NWRAP_INET_ADDRSTRLEN INET_ADDRSTRLEN
161 #define NWRAP_LOCK(m) do { \
162 pthread_mutex_lock(&( m ## _mutex)); \
165 #define NWRAP_UNLOCK(m) do { \
166 pthread_mutex_unlock(&( m ## _mutex)); \
170 static bool nwrap_initialized = false;
171 static pthread_mutex_t nwrap_initialized_mutex = PTHREAD_MUTEX_INITIALIZER;
173 /* The mutex or accessing the id */
174 static pthread_mutex_t nwrap_global_mutex = PTHREAD_MUTEX_INITIALIZER;
175 static pthread_mutex_t nwrap_gr_global_mutex = PTHREAD_MUTEX_INITIALIZER;
176 static pthread_mutex_t nwrap_he_global_mutex = PTHREAD_MUTEX_INITIALIZER;
177 static pthread_mutex_t nwrap_pw_global_mutex = PTHREAD_MUTEX_INITIALIZER;
178 static pthread_mutex_t nwrap_sp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
180 /* Add new global locks here please */
181 /* Also don't forget to add locks to
182 * nwrap_init() function.
184 # define NWRAP_LOCK_ALL do { \
185 NWRAP_LOCK(nwrap_initialized); \
186 NWRAP_LOCK(nwrap_global); \
187 NWRAP_LOCK(nwrap_gr_global); \
188 NWRAP_LOCK(nwrap_he_global); \
189 NWRAP_LOCK(nwrap_pw_global); \
190 NWRAP_LOCK(nwrap_sp_global); \
193 # define NWRAP_UNLOCK_ALL do {\
194 NWRAP_UNLOCK(nwrap_sp_global); \
195 NWRAP_UNLOCK(nwrap_pw_global); \
196 NWRAP_UNLOCK(nwrap_he_global); \
197 NWRAP_UNLOCK(nwrap_gr_global); \
198 NWRAP_UNLOCK(nwrap_global); \
199 NWRAP_UNLOCK(nwrap_initialized); \
202 static void nwrap_thread_prepare(void)
207 static void nwrap_thread_parent(void)
212 static void nwrap_thread_child(void)
217 enum nwrap_dbglvl_e {
225 # define NWRAP_LOG(...)
228 static void nwrap_log(enum nwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
229 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
231 static void nwrap_log(enum nwrap_dbglvl_e dbglvl,
233 const char *format, ...)
238 unsigned int lvl = 0;
241 d = getenv("NSS_WRAPPER_DEBUGLEVEL");
246 va_start(va, format);
247 vsnprintf(buffer, sizeof(buffer), format, va);
252 case NWRAP_LOG_ERROR:
254 "NWRAP_ERROR(%d) - %s: %s\n",
259 "NWRAP_WARN(%d) - %s: %s\n",
262 case NWRAP_LOG_DEBUG:
264 "NWRAP_DEBUG(%d) - %s: %s\n",
267 case NWRAP_LOG_TRACE:
269 "NWRAP_TRACE(%d) - %s: %s\n",
275 #endif /* NDEBUG NWRAP_LOG */
277 struct nwrap_libc_fns {
278 struct passwd *(*_libc_getpwnam)(const char *name);
279 int (*_libc_getpwnam_r)(const char *name, struct passwd *pwd,
280 char *buf, size_t buflen, struct passwd **result);
281 struct passwd *(*_libc_getpwuid)(uid_t uid);
282 int (*_libc_getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);
283 void (*_libc_setpwent)(void);
284 struct passwd *(*_libc_getpwent)(void);
285 #ifdef HAVE_SOLARIS_GETPWENT_R
286 struct passwd *(*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen);
288 int (*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp);
290 void (*_libc_endpwent)(void);
291 int (*_libc_initgroups)(const char *user, gid_t gid);
292 struct group *(*_libc_getgrnam)(const char *name);
293 int (*_libc_getgrnam_r)(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result);
294 struct group *(*_libc_getgrgid)(gid_t gid);
295 int (*_libc_getgrgid_r)(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result);
296 void (*_libc_setgrent)(void);
297 struct group *(*_libc_getgrent)(void);
298 #ifdef HAVE_SOLARIS_GETGRENT_R
299 struct group *(*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen);
301 int (*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen, struct group **result);
303 void (*_libc_endgrent)(void);
304 int (*_libc_getgrouplist)(const char *user, gid_t group, gid_t *groups, int *ngroups);
306 void (*_libc_sethostent)(int stayopen);
307 struct hostent *(*_libc_gethostent)(void);
308 void (*_libc_endhostent)(void);
310 struct hostent *(*_libc_gethostbyname)(const char *name);
311 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
312 struct hostent *(*_libc_gethostbyname2)(const char *name, int af);
314 struct hostent *(*_libc_gethostbyaddr)(const void *addr, socklen_t len, int type);
316 int (*_libc_getaddrinfo)(const char *node, const char *service,
317 const struct addrinfo *hints,
318 struct addrinfo **res);
319 int (*_libc_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
320 char *host, size_t hostlen,
321 char *serv, size_t servlen,
323 int (*_libc_gethostname)(char *name, size_t len);
324 #ifdef HAVE_GETHOSTBYNAME_R
325 int (*_libc_gethostbyname_r)(const char *name,
327 char *buf, size_t buflen,
328 struct hostent **result, int *h_errnop);
330 #ifdef HAVE_GETHOSTBYADDR_R
331 int (*_libc_gethostbyaddr_r)(const void *addr, socklen_t len, int type,
333 char *buf, size_t buflen,
334 struct hostent **result, int *h_errnop);
338 struct nwrap_module_nss_fns {
339 NSS_STATUS (*_nss_getpwnam_r)(const char *name, struct passwd *result, char *buffer,
340 size_t buflen, int *errnop);
341 NSS_STATUS (*_nss_getpwuid_r)(uid_t uid, struct passwd *result, char *buffer,
342 size_t buflen, int *errnop);
343 NSS_STATUS (*_nss_setpwent)(void);
344 NSS_STATUS (*_nss_getpwent_r)(struct passwd *result, char *buffer,
345 size_t buflen, int *errnop);
346 NSS_STATUS (*_nss_endpwent)(void);
347 NSS_STATUS (*_nss_initgroups)(const char *user, gid_t group, long int *start,
348 long int *size, gid_t **groups, long int limit, int *errnop);
349 NSS_STATUS (*_nss_getgrnam_r)(const char *name, struct group *result, char *buffer,
350 size_t buflen, int *errnop);
351 NSS_STATUS (*_nss_getgrgid_r)(gid_t gid, struct group *result, char *buffer,
352 size_t buflen, int *errnop);
353 NSS_STATUS (*_nss_setgrent)(void);
354 NSS_STATUS (*_nss_getgrent_r)(struct group *result, char *buffer,
355 size_t buflen, int *errnop);
356 NSS_STATUS (*_nss_endgrent)(void);
359 struct nwrap_backend {
363 struct nwrap_ops *ops;
364 struct nwrap_module_nss_fns *fns;
368 struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
370 int (*nw_getpwnam_r)(struct nwrap_backend *b,
371 const char *name, struct passwd *pwdst,
372 char *buf, size_t buflen, struct passwd **pwdstp);
373 struct passwd * (*nw_getpwuid)(struct nwrap_backend *b,
375 int (*nw_getpwuid_r)(struct nwrap_backend *b,
376 uid_t uid, struct passwd *pwdst,
377 char *buf, size_t buflen, struct passwd **pwdstp);
378 void (*nw_setpwent)(struct nwrap_backend *b);
379 struct passwd * (*nw_getpwent)(struct nwrap_backend *b);
380 int (*nw_getpwent_r)(struct nwrap_backend *b,
381 struct passwd *pwdst, char *buf,
382 size_t buflen, struct passwd **pwdstp);
383 void (*nw_endpwent)(struct nwrap_backend *b);
384 int (*nw_initgroups)(struct nwrap_backend *b,
385 const char *user, gid_t group);
386 struct group * (*nw_getgrnam)(struct nwrap_backend *b,
388 int (*nw_getgrnam_r)(struct nwrap_backend *b,
389 const char *name, struct group *grdst,
390 char *buf, size_t buflen, struct group **grdstp);
391 struct group * (*nw_getgrgid)(struct nwrap_backend *b,
393 int (*nw_getgrgid_r)(struct nwrap_backend *b,
394 gid_t gid, struct group *grdst,
395 char *buf, size_t buflen, struct group **grdstp);
396 void (*nw_setgrent)(struct nwrap_backend *b);
397 struct group * (*nw_getgrent)(struct nwrap_backend *b);
398 int (*nw_getgrent_r)(struct nwrap_backend *b,
399 struct group *grdst, char *buf,
400 size_t buflen, struct group **grdstp);
401 void (*nw_endgrent)(struct nwrap_backend *b);
404 /* Public prototypes */
406 bool nss_wrapper_enabled(void);
407 bool nss_wrapper_shadow_enabled(void);
408 bool nss_wrapper_hosts_enabled(void);
410 /* prototypes for files backend */
413 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
415 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
416 const char *name, struct passwd *pwdst,
417 char *buf, size_t buflen, struct passwd **pwdstp);
418 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
420 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
421 uid_t uid, struct passwd *pwdst,
422 char *buf, size_t buflen, struct passwd **pwdstp);
423 static void nwrap_files_setpwent(struct nwrap_backend *b);
424 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b);
425 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
426 struct passwd *pwdst, char *buf,
427 size_t buflen, struct passwd **pwdstp);
428 static void nwrap_files_endpwent(struct nwrap_backend *b);
429 static int nwrap_files_initgroups(struct nwrap_backend *b,
430 const char *user, gid_t group);
431 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
433 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
434 const char *name, struct group *grdst,
435 char *buf, size_t buflen, struct group **grdstp);
436 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
438 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
439 gid_t gid, struct group *grdst,
440 char *buf, size_t buflen, struct group **grdstp);
441 static void nwrap_files_setgrent(struct nwrap_backend *b);
442 static struct group *nwrap_files_getgrent(struct nwrap_backend *b);
443 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
444 struct group *grdst, char *buf,
445 size_t buflen, struct group **grdstp);
446 static void nwrap_files_endgrent(struct nwrap_backend *b);
448 /* prototypes for module backend */
450 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b);
451 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
452 struct passwd *pwdst, char *buf,
453 size_t buflen, struct passwd **pwdstp);
454 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
456 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
457 const char *name, struct passwd *pwdst,
458 char *buf, size_t buflen, struct passwd **pwdstp);
459 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
461 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
462 uid_t uid, struct passwd *pwdst,
463 char *buf, size_t buflen, struct passwd **pwdstp);
464 static void nwrap_module_setpwent(struct nwrap_backend *b);
465 static void nwrap_module_endpwent(struct nwrap_backend *b);
466 static struct group *nwrap_module_getgrent(struct nwrap_backend *b);
467 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
468 struct group *grdst, char *buf,
469 size_t buflen, struct group **grdstp);
470 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
472 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
473 const char *name, struct group *grdst,
474 char *buf, size_t buflen, struct group **grdstp);
475 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
477 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
478 gid_t gid, struct group *grdst,
479 char *buf, size_t buflen, struct group **grdstp);
480 static void nwrap_module_setgrent(struct nwrap_backend *b);
481 static void nwrap_module_endgrent(struct nwrap_backend *b);
482 static int nwrap_module_initgroups(struct nwrap_backend *b,
483 const char *user, gid_t group);
485 struct nwrap_ops nwrap_files_ops = {
486 .nw_getpwnam = nwrap_files_getpwnam,
487 .nw_getpwnam_r = nwrap_files_getpwnam_r,
488 .nw_getpwuid = nwrap_files_getpwuid,
489 .nw_getpwuid_r = nwrap_files_getpwuid_r,
490 .nw_setpwent = nwrap_files_setpwent,
491 .nw_getpwent = nwrap_files_getpwent,
492 .nw_getpwent_r = nwrap_files_getpwent_r,
493 .nw_endpwent = nwrap_files_endpwent,
494 .nw_initgroups = nwrap_files_initgroups,
495 .nw_getgrnam = nwrap_files_getgrnam,
496 .nw_getgrnam_r = nwrap_files_getgrnam_r,
497 .nw_getgrgid = nwrap_files_getgrgid,
498 .nw_getgrgid_r = nwrap_files_getgrgid_r,
499 .nw_setgrent = nwrap_files_setgrent,
500 .nw_getgrent = nwrap_files_getgrent,
501 .nw_getgrent_r = nwrap_files_getgrent_r,
502 .nw_endgrent = nwrap_files_endgrent,
505 struct nwrap_ops nwrap_module_ops = {
506 .nw_getpwnam = nwrap_module_getpwnam,
507 .nw_getpwnam_r = nwrap_module_getpwnam_r,
508 .nw_getpwuid = nwrap_module_getpwuid,
509 .nw_getpwuid_r = nwrap_module_getpwuid_r,
510 .nw_setpwent = nwrap_module_setpwent,
511 .nw_getpwent = nwrap_module_getpwent,
512 .nw_getpwent_r = nwrap_module_getpwent_r,
513 .nw_endpwent = nwrap_module_endpwent,
514 .nw_initgroups = nwrap_module_initgroups,
515 .nw_getgrnam = nwrap_module_getgrnam,
516 .nw_getgrnam_r = nwrap_module_getgrnam_r,
517 .nw_getgrgid = nwrap_module_getgrgid,
518 .nw_getgrgid_r = nwrap_module_getgrgid_r,
519 .nw_setgrent = nwrap_module_setgrent,
520 .nw_getgrent = nwrap_module_getgrent,
521 .nw_getgrent_r = nwrap_module_getgrent_r,
522 .nw_endgrent = nwrap_module_endgrent,
529 struct nwrap_libc_fns *fns;
534 struct nwrap_backend *backends;
535 struct nwrap_libc *libc;
538 static struct nwrap_main *nwrap_main_global;
539 static struct nwrap_main __nwrap_main_global;
544 static int nwrap_convert_he_ai(const struct hostent *he,
546 const struct addrinfo *hints,
547 struct addrinfo **pai,
548 bool skip_canonname);
554 #define DEFAULT_VECTOR_CAPACITY 16
556 struct nwrap_vector {
562 /* Macro returns pointer to first element of vector->items array.
564 * nwrap_vector is used as a memory backend which take care of
565 * memory allocations and other stuff like memory growing.
566 * nwrap_vectors should not be considered as some abstract structures.
567 * On this level, vectors are more handy than direct realloc/malloc
570 * nwrap_vector->items is array inside nwrap_vector which can be
571 * directly pointed by libc structure assembled by cwrap itself.
575 * 1) struct hostent contains char **h_addr_list element.
576 * 2) nwrap_vector holds array of pointers to addresses.
577 * It's easier to use vector to store results of
580 * Now, pretend that cwrap assembled struct hostent and
581 * we need to set h_addr_list to point to nwrap_vector.
582 * Idea behind is to shield users from internal nwrap_vector
584 * (Yes, not fully - array terminated by NULL is needed because
585 * it's result expected by libc function caller.)
591 * struct nwrap_vector *vector = malloc(sizeof(struct nwrap_vector));
592 * ... don't care about failed allocation now ...
594 * ... fill nwrap vector ...
597 * he.h_addr_list = nwrap_vector_head(vector);
600 #define nwrap_vector_head(vect) ((void *)((vect)->items))
602 #define nwrap_vector_foreach(item, vect, iter) \
603 for (iter = 0, (item) = (vect).items == NULL ? NULL : (vect).items[0]; \
605 (item) = (vect).items[++iter])
607 static inline bool nwrap_vector_init(struct nwrap_vector *const vector)
609 if (vector == NULL) {
613 /* count is initialized by ZERO_STRUCTP */
614 ZERO_STRUCTP(vector);
615 vector->items = malloc(sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
616 if (vector->items == NULL) {
619 vector->capacity = DEFAULT_VECTOR_CAPACITY;
620 memset(vector->items, '\0', sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
625 static bool nwrap_vector_add_item(struct nwrap_vector *cont, void *const item)
627 assert (cont != NULL);
629 if (cont->items == NULL) {
630 nwrap_vector_init(cont);
633 if (cont->count == cont->capacity) {
634 /* Items array _MUST_ be NULL terminated because it's passed
635 * as result to caller which expect NULL terminated array from libc.
637 void **items = realloc(cont->items, sizeof(void *) * ((cont->capacity * 2) + 1));
643 /* Don't count ending NULL to capacity */
647 cont->items[cont->count] = item;
650 cont->items[cont->count] = NULL;
655 static bool nwrap_vector_merge(struct nwrap_vector *dst,
656 struct nwrap_vector *src)
658 void **dst_items = NULL;
661 if (src->count == 0) {
665 count = dst->count + src->count;
667 /* We don't need reallocation if we have enough capacity. */
668 if (src->count > (dst->capacity - dst->count)) {
669 dst_items = (void **)realloc(dst->items, (count + 1) * sizeof(void *));
670 if (dst_items == NULL) {
673 dst->items = dst_items;
674 dst->capacity = count;
677 memcpy((void *)(((long *)dst->items) + dst->count),
679 src->count * sizeof(void *));
692 struct nwrap_vector lines;
694 bool (*parse_line)(struct nwrap_cache *, char *line);
695 void (*unload)(struct nwrap_cache *);
700 struct nwrap_cache *cache;
707 struct nwrap_cache __nwrap_cache_pw;
708 struct nwrap_pw nwrap_pw_global;
710 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
711 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
714 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
716 struct nwrap_cache *cache;
723 struct nwrap_cache __nwrap_cache_sp;
724 struct nwrap_sp nwrap_sp_global;
726 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line);
727 static void nwrap_sp_unload(struct nwrap_cache *nwrap);
728 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
732 struct nwrap_cache *cache;
739 struct nwrap_cache __nwrap_cache_gr;
740 struct nwrap_gr nwrap_gr_global;
743 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line);
744 static void nwrap_he_unload(struct nwrap_cache *nwrap);
746 struct nwrap_addrdata {
747 unsigned char host_addr[16]; /* IPv4 or IPv6 address */
750 static size_t max_hostents = 100;
752 struct nwrap_entdata {
753 struct nwrap_addrdata addr;
756 struct nwrap_vector nwrap_addrdata;
758 ssize_t aliases_count;
760 struct nwrap_entdata *ed_next;
761 struct nwrap_entdata *ed_tail;
765 struct nwrap_cache *cache;
767 struct nwrap_entdata *list;
768 struct nwrap_vector entdata;
774 static struct nwrap_cache __nwrap_cache_he;
775 static struct nwrap_he nwrap_he_global;
778 /*********************************************************
780 *********************************************************/
782 static void nwrap_init(void);
783 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
784 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
785 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
787 /*********************************************************
788 * NWRAP LIBC LOADER FUNCTIONS
789 *********************************************************/
798 static const char *nwrap_str_lib(enum nwrap_lib lib)
805 case NWRAP_LIBSOCKET:
809 /* Compiler would warn us about unhandled enum value if we get here */
814 static void *nwrap_load_lib_handle(enum nwrap_lib lib)
816 int flags = RTLD_LAZY;
821 flags |= RTLD_DEEPBIND;
827 handle = nwrap_main_global->libc->nsl_handle;
828 if (handle == NULL) {
829 for (i = 10; i >= 0; i--) {
830 char soname[256] = {0};
832 snprintf(soname, sizeof(soname), "libnsl.so.%d", i);
833 handle = dlopen(soname, flags);
834 if (handle != NULL) {
839 nwrap_main_global->libc->nsl_handle = handle;
844 case NWRAP_LIBSOCKET:
845 #ifdef HAVE_LIBSOCKET
846 handle = nwrap_main_global->libc->sock_handle;
847 if (handle == NULL) {
848 for (i = 10; i >= 0; i--) {
849 char soname[256] = {0};
851 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
852 handle = dlopen(soname, flags);
853 if (handle != NULL) {
858 nwrap_main_global->libc->sock_handle = handle;
864 handle = nwrap_main_global->libc->handle;
865 if (handle == NULL) {
866 for (i = 10; i >= 0; i--) {
867 char soname[256] = {0};
869 snprintf(soname, sizeof(soname), "libc.so.%d", i);
870 handle = dlopen(soname, flags);
871 if (handle != NULL) {
876 nwrap_main_global->libc->handle = handle;
881 if (handle == NULL) {
883 handle = nwrap_main_global->libc->handle
884 = nwrap_main_global->libc->sock_handle
885 = nwrap_main_global->libc->nsl_handle
888 NWRAP_LOG(NWRAP_LOG_ERROR,
889 "Failed to dlopen library: %s\n",
898 static void *_nwrap_load_lib_function(enum nwrap_lib lib, const char *fn_name)
905 handle = nwrap_load_lib_handle(lib);
907 func = dlsym(handle, fn_name);
909 NWRAP_LOG(NWRAP_LOG_ERROR,
910 "Failed to find %s: %s\n",
915 NWRAP_LOG(NWRAP_LOG_TRACE,
917 fn_name, nwrap_str_lib(lib));
921 #define nwrap_load_lib_function(lib, fn_name) \
922 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
923 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
924 _nwrap_load_lib_function(lib, #fn_name); \
927 /* INTERNAL HELPER FUNCTIONS */
928 static void nwrap_lines_unload(struct nwrap_cache *const nwrap)
932 nwrap_vector_foreach(item, nwrap->lines, p) {
933 /* Maybe some vectors were merged ... */
936 SAFE_FREE(nwrap->lines.items);
937 ZERO_STRUCTP(&nwrap->lines);
943 * Functions expeciall from libc need to be loaded individually, you can't load
944 * all at once or gdb will segfault at startup. The same applies to valgrind and
945 * has probably something todo with with the linker.
946 * So we need load each function at the point it is called the first time.
948 static struct passwd *libc_getpwnam(const char *name)
950 nwrap_load_lib_function(NWRAP_LIBC, getpwnam);
952 return nwrap_main_global->libc->fns->_libc_getpwnam(name);
955 #ifdef HAVE_GETPWNAM_R
956 static int libc_getpwnam_r(const char *name,
960 struct passwd **result)
962 #ifdef HAVE___POSIX_GETPWNAM_R
963 if (nwrap_main_global->libc->fns->_libc_getpwnam_r == NULL) {
964 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwnam_r) =
965 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwnam_r");
968 nwrap_load_lib_function(NWRAP_LIBC, getpwnam_r);
971 return nwrap_main_global->libc->fns->_libc_getpwnam_r(name,
979 static struct passwd *libc_getpwuid(uid_t uid)
981 nwrap_load_lib_function(NWRAP_LIBC, getpwuid);
983 return nwrap_main_global->libc->fns->_libc_getpwuid(uid);
986 #ifdef HAVE_GETPWUID_R
987 static int libc_getpwuid_r(uid_t uid,
991 struct passwd **result)
993 #ifdef HAVE___POSIX_GETPWUID_R
994 if (nwrap_main_global->libc->fns->_libc_getpwuid_r == NULL) {
995 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwuid_r) =
996 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwuid_r");
999 nwrap_load_lib_function(NWRAP_LIBC, getpwuid_r);
1002 return nwrap_main_global->libc->fns->_libc_getpwuid_r(uid,
1010 static inline void str_tolower(char *dst, char *src)
1012 register char *src_tmp = src;
1013 register char *dst_tmp = dst;
1015 while (*src_tmp != '\0') {
1016 *dst_tmp = tolower(*src_tmp);
1022 static bool str_tolower_copy(char **dst_name, const char *const src_name)
1026 if ((dst_name == NULL) || (src_name == NULL)) {
1030 h_name_lower = strdup(src_name);
1031 if (h_name_lower == NULL) {
1032 NWRAP_LOG(NWRAP_LOG_DEBUG, "Out of memory while strdup");
1036 str_tolower(h_name_lower, h_name_lower);
1037 *dst_name = h_name_lower;
1041 static void libc_setpwent(void)
1043 nwrap_load_lib_function(NWRAP_LIBC, setpwent);
1045 nwrap_main_global->libc->fns->_libc_setpwent();
1048 static struct passwd *libc_getpwent(void)
1050 nwrap_load_lib_function(NWRAP_LIBC, getpwent);
1052 return nwrap_main_global->libc->fns->_libc_getpwent();
1055 #ifdef HAVE_SOLARIS_GETPWENT_R
1056 static struct passwd *libc_getpwent_r(struct passwd *pwdst,
1060 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1062 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1066 #else /* HAVE_SOLARIS_GETPWENT_R */
1067 static int libc_getpwent_r(struct passwd *pwdst,
1070 struct passwd **pwdstp)
1072 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1074 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1079 #endif /* HAVE_SOLARIS_GETPWENT_R */
1081 static void libc_endpwent(void)
1083 nwrap_load_lib_function(NWRAP_LIBC, endpwent);
1085 nwrap_main_global->libc->fns->_libc_endpwent();
1088 static int libc_initgroups(const char *user, gid_t gid)
1090 nwrap_load_lib_function(NWRAP_LIBC, initgroups);
1092 return nwrap_main_global->libc->fns->_libc_initgroups(user, gid);
1095 static struct group *libc_getgrnam(const char *name)
1097 nwrap_load_lib_function(NWRAP_LIBC, getgrnam);
1099 return nwrap_main_global->libc->fns->_libc_getgrnam(name);
1102 #ifdef HAVE_GETGRNAM_R
1103 static int libc_getgrnam_r(const char *name,
1107 struct group **result)
1109 #ifdef HAVE___POSIX_GETGRNAM_R
1110 if (nwrap_main_global->libc->fns->_libc_getgrnam_r == NULL) {
1111 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrnam_r) =
1112 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrnam_r");
1115 nwrap_load_lib_function(NWRAP_LIBC, getgrnam_r);
1118 return nwrap_main_global->libc->fns->_libc_getgrnam_r(name,
1126 static struct group *libc_getgrgid(gid_t gid)
1128 nwrap_load_lib_function(NWRAP_LIBC, getgrgid);
1130 return nwrap_main_global->libc->fns->_libc_getgrgid(gid);
1133 #ifdef HAVE_GETGRGID_R
1134 static int libc_getgrgid_r(gid_t gid,
1138 struct group **result)
1140 #ifdef HAVE___POSIX_GETGRGID_R
1141 if (nwrap_main_global->libc->fns->_libc_getgrgid_r == NULL) {
1142 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrgid_r) =
1143 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrgid_r");
1146 nwrap_load_lib_function(NWRAP_LIBC, getgrgid_r);
1149 return nwrap_main_global->libc->fns->_libc_getgrgid_r(gid,
1157 static void libc_setgrent(void)
1159 nwrap_load_lib_function(NWRAP_LIBC, setgrent);
1161 nwrap_main_global->libc->fns->_libc_setgrent();
1164 static struct group *libc_getgrent(void)
1166 nwrap_load_lib_function(NWRAP_LIBC, getgrent);
1168 return nwrap_main_global->libc->fns->_libc_getgrent();
1171 #ifdef HAVE_GETGRENT_R
1172 #ifdef HAVE_SOLARIS_GETGRENT_R
1173 static struct group *libc_getgrent_r(struct group *group,
1177 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1179 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1183 #else /* !HAVE_SOLARIS_GETGRENT_R */
1184 static int libc_getgrent_r(struct group *group,
1187 struct group **result)
1189 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1191 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1196 #endif /* HAVE_SOLARIS_GETGRENT_R */
1197 #endif /* HAVE_GETGRENT_R */
1199 static void libc_endgrent(void)
1201 nwrap_load_lib_function(NWRAP_LIBC, endgrent);
1203 nwrap_main_global->libc->fns->_libc_endgrent();
1206 #ifdef HAVE_GETGROUPLIST
1207 static int libc_getgrouplist(const char *user,
1212 nwrap_load_lib_function(NWRAP_LIBC, getgrouplist);
1214 return nwrap_main_global->libc->fns->_libc_getgrouplist(user,
1221 static void libc_sethostent(int stayopen)
1223 nwrap_load_lib_function(NWRAP_LIBNSL, sethostent);
1225 nwrap_main_global->libc->fns->_libc_sethostent(stayopen);
1228 static struct hostent *libc_gethostent(void)
1230 nwrap_load_lib_function(NWRAP_LIBNSL, gethostent);
1232 return nwrap_main_global->libc->fns->_libc_gethostent();
1235 static void libc_endhostent(void)
1237 nwrap_load_lib_function(NWRAP_LIBNSL, endhostent);
1239 nwrap_main_global->libc->fns->_libc_endhostent();
1242 static struct hostent *libc_gethostbyname(const char *name)
1244 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname);
1246 return nwrap_main_global->libc->fns->_libc_gethostbyname(name);
1249 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
1250 static struct hostent *libc_gethostbyname2(const char *name, int af)
1252 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname2);
1254 return nwrap_main_global->libc->fns->_libc_gethostbyname2(name, af);
1258 static struct hostent *libc_gethostbyaddr(const void *addr,
1262 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr);
1264 return nwrap_main_global->libc->fns->_libc_gethostbyaddr(addr,
1269 static int libc_gethostname(char *name, size_t len)
1271 nwrap_load_lib_function(NWRAP_LIBNSL, gethostname);
1273 return nwrap_main_global->libc->fns->_libc_gethostname(name, len);
1276 #ifdef HAVE_GETHOSTBYNAME_R
1277 static int libc_gethostbyname_r(const char *name,
1278 struct hostent *ret,
1281 struct hostent **result,
1284 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname_r);
1286 return nwrap_main_global->libc->fns->_libc_gethostbyname_r(name,
1295 #ifdef HAVE_GETHOSTBYADDR_R
1296 static int libc_gethostbyaddr_r(const void *addr,
1299 struct hostent *ret,
1302 struct hostent **result,
1305 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr_r);
1307 return nwrap_main_global->libc->fns->_libc_gethostbyaddr_r(addr,
1318 static int libc_getaddrinfo(const char *node,
1319 const char *service,
1320 const struct addrinfo *hints,
1321 struct addrinfo **res)
1323 nwrap_load_lib_function(NWRAP_LIBSOCKET, getaddrinfo);
1325 return nwrap_main_global->libc->fns->_libc_getaddrinfo(node,
1331 static int libc_getnameinfo(const struct sockaddr *sa,
1339 nwrap_load_lib_function(NWRAP_LIBSOCKET, getnameinfo);
1341 return nwrap_main_global->libc->fns->_libc_getnameinfo(sa,
1350 /*********************************************************
1351 * NWRAP NSS MODULE LOADER FUNCTIONS
1352 *********************************************************/
1354 static void *nwrap_load_module_fn(struct nwrap_backend *b,
1355 const char *fn_name)
1360 if (!b->so_handle) {
1361 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1365 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
1366 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1370 res = dlsym(b->so_handle, s);
1372 NWRAP_LOG(NWRAP_LOG_ERROR,
1373 "Cannot find function %s in %s",
1380 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
1382 struct nwrap_module_nss_fns *fns;
1384 if (!b->so_handle) {
1388 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
1393 *(void **)(&fns->_nss_getpwnam_r) =
1394 nwrap_load_module_fn(b, "getpwnam_r");
1395 *(void **)(&fns->_nss_getpwuid_r) =
1396 nwrap_load_module_fn(b, "getpwuid_r");
1397 *(void **)(&fns->_nss_setpwent) =
1398 nwrap_load_module_fn(b, "setpwent");
1399 *(void **)(&fns->_nss_getpwent_r) =
1400 nwrap_load_module_fn(b, "getpwent_r");
1401 *(void **)(&fns->_nss_endpwent) =
1402 nwrap_load_module_fn(b, "endpwent");
1403 *(void **)(&fns->_nss_initgroups) =
1404 nwrap_load_module_fn(b, "initgroups_dyn");
1405 *(void **)(&fns->_nss_getgrnam_r) =
1406 nwrap_load_module_fn(b, "getgrnam_r");
1407 *(void **)(&fns->_nss_getgrgid_r)=
1408 nwrap_load_module_fn(b, "getgrgid_r");
1409 *(void **)(&fns->_nss_setgrent) =
1410 nwrap_load_module_fn(b, "setgrent");
1411 *(void **)(&fns->_nss_getgrent_r) =
1412 nwrap_load_module_fn(b, "getgrent_r");
1413 *(void **)(&fns->_nss_endgrent) =
1414 nwrap_load_module_fn(b, "endgrent");
1419 static void *nwrap_load_module(const char *so_path)
1423 if (!so_path || !strlen(so_path)) {
1427 h = dlopen(so_path, RTLD_LAZY);
1429 NWRAP_LOG(NWRAP_LOG_ERROR,
1430 "Cannot open shared library %s",
1438 static bool nwrap_module_init(const char *name,
1439 struct nwrap_ops *ops,
1440 const char *so_path,
1442 struct nwrap_backend **backends)
1444 struct nwrap_backend *b;
1446 *backends = (struct nwrap_backend *)realloc(*backends,
1447 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
1449 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1453 b = &((*backends)[*num_backends]);
1457 b->so_path = so_path;
1459 if (so_path != NULL) {
1460 b->so_handle = nwrap_load_module(so_path);
1461 b->fns = nwrap_load_module_fns(b);
1462 if (b->fns == NULL) {
1466 b->so_handle = NULL;
1475 static void nwrap_libc_init(struct nwrap_main *r)
1477 r->libc = malloc(sizeof(struct nwrap_libc));
1478 if (r->libc == NULL) {
1479 printf("Failed to allocate memory for libc");
1482 ZERO_STRUCTP(r->libc);
1484 r->libc->fns = malloc(sizeof(struct nwrap_libc_fns));
1485 if (r->libc->fns == NULL) {
1486 printf("Failed to allocate memory for libc functions");
1489 ZERO_STRUCTP(r->libc->fns);
1492 static void nwrap_backend_init(struct nwrap_main *r)
1494 const char *module_so_path = getenv("NSS_WRAPPER_MODULE_SO_PATH");
1495 const char *module_fn_name = getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1497 r->num_backends = 0;
1500 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1503 NWRAP_LOG(NWRAP_LOG_ERROR,
1504 "Failed to initialize 'files' backend");
1508 if (module_so_path != NULL &&
1509 module_so_path[0] != '\0' &&
1510 module_fn_name != NULL &&
1511 module_fn_name[0] != '\0') {
1512 if (!nwrap_module_init(module_fn_name,
1517 NWRAP_LOG(NWRAP_LOG_ERROR,
1518 "Failed to initialize '%s' backend",
1525 static void nwrap_init(void)
1529 size_t max_hostents_tmp;
1531 NWRAP_LOCK(nwrap_initialized);
1532 if (nwrap_initialized) {
1533 NWRAP_UNLOCK(nwrap_initialized);
1538 * Still holding nwrap_initialized lock here.
1539 * We don't use NWRAP_(UN)LOCK_ALL macros here because we
1540 * want to avoid overhead when other threads do their job.
1542 NWRAP_LOCK(nwrap_global);
1543 NWRAP_LOCK(nwrap_gr_global);
1544 NWRAP_LOCK(nwrap_he_global);
1545 NWRAP_LOCK(nwrap_pw_global);
1546 NWRAP_LOCK(nwrap_sp_global);
1548 nwrap_initialized = true;
1550 /* Initialize pthread_atfork handlers */
1551 pthread_atfork(&nwrap_thread_prepare, &nwrap_thread_parent,
1552 &nwrap_thread_child);
1554 env = getenv("NSS_WRAPPER_MAX_HOSTENTS");
1556 max_hostents_tmp = (size_t)strtol(env, &endptr, 10);
1557 if (((env != '\0') && (endptr == '\0')) ||
1558 (max_hostents_tmp == 0)) {
1559 NWRAP_LOG(NWRAP_LOG_DEBUG,
1560 "Error parsing NSS_WRAPPER_MAX_HOSTENTS "
1561 "value or value is too small. "
1562 "Using default value: %lu.",
1565 max_hostents = max_hostents_tmp;
1568 /* Initialize hash table */
1569 NWRAP_LOG(NWRAP_LOG_DEBUG,
1570 "Initializing hash table of size %lu items.", max_hostents);
1571 if (hcreate(max_hostents) == 0) {
1572 NWRAP_LOG(NWRAP_LOG_ERROR,
1573 "Failed to initialize hash table");
1577 nwrap_main_global = &__nwrap_main_global;
1579 nwrap_libc_init(nwrap_main_global);
1581 nwrap_backend_init(nwrap_main_global);
1584 nwrap_pw_global.cache = &__nwrap_cache_pw;
1586 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
1587 nwrap_pw_global.cache->fp = NULL;
1588 nwrap_pw_global.cache->fd = -1;
1589 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
1590 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
1591 nwrap_pw_global.cache->unload = nwrap_pw_unload;
1594 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1595 nwrap_sp_global.cache = &__nwrap_cache_sp;
1597 nwrap_sp_global.cache->path = getenv("NSS_WRAPPER_SHADOW");
1598 nwrap_sp_global.cache->fp = NULL;
1599 nwrap_sp_global.cache->fd = -1;
1600 nwrap_sp_global.cache->private_data = &nwrap_sp_global;
1601 nwrap_sp_global.cache->parse_line = nwrap_sp_parse_line;
1602 nwrap_sp_global.cache->unload = nwrap_sp_unload;
1603 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1606 nwrap_gr_global.cache = &__nwrap_cache_gr;
1608 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
1609 nwrap_gr_global.cache->fp = NULL;
1610 nwrap_gr_global.cache->fd = -1;
1611 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
1612 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
1613 nwrap_gr_global.cache->unload = nwrap_gr_unload;
1616 nwrap_he_global.cache = &__nwrap_cache_he;
1618 nwrap_he_global.cache->path = getenv("NSS_WRAPPER_HOSTS");
1619 nwrap_he_global.cache->fp = NULL;
1620 nwrap_he_global.cache->fd = -1;
1621 nwrap_he_global.cache->private_data = &nwrap_he_global;
1622 nwrap_he_global.cache->parse_line = nwrap_he_parse_line;
1623 nwrap_he_global.cache->unload = nwrap_he_unload;
1626 /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1630 bool nss_wrapper_enabled(void)
1634 if (nwrap_pw_global.cache->path == NULL ||
1635 nwrap_pw_global.cache->path[0] == '\0') {
1638 if (nwrap_gr_global.cache->path == NULL ||
1639 nwrap_gr_global.cache->path[0] == '\0') {
1646 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1647 bool nss_wrapper_shadow_enabled(void)
1651 if (nwrap_sp_global.cache->path == NULL ||
1652 nwrap_sp_global.cache->path[0] == '\0') {
1658 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1660 bool nss_wrapper_hosts_enabled(void)
1664 if (nwrap_he_global.cache->path == NULL ||
1665 nwrap_he_global.cache->path[0] == '\0') {
1672 static bool nwrap_hostname_enabled(void)
1676 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
1683 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
1687 /* Unused but getline needs it */
1691 if (nwrap->st.st_size == 0) {
1692 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
1696 /* Support for 32-bit system I guess */
1697 if (nwrap->st.st_size > INT32_MAX) {
1698 NWRAP_LOG(NWRAP_LOG_ERROR,
1699 "Size[%u] larger than INT32_MAX",
1700 (unsigned)nwrap->st.st_size);
1707 n = getline(&line, &len, nwrap->fp);
1710 if (feof(nwrap->fp)) {
1714 NWRAP_LOG(NWRAP_LOG_ERROR,
1715 "Unable to read line from file: %s",
1720 if (line[n - 1] == '\n') {
1724 if (line[0] == '\0') {
1729 ok = nwrap->parse_line(nwrap, line);
1731 NWRAP_LOG(NWRAP_LOG_ERROR,
1732 "Unable to parse line file: %s",
1738 /* Line is parsed without issues so add it to list */
1739 ok = nwrap_vector_add_item(&(nwrap->lines), (void *const) line);
1741 NWRAP_LOG(NWRAP_LOG_ERROR,
1742 "Unable to add line to vector");
1746 /* This forces getline to allocate new memory for line. */
1748 } while (!feof(nwrap->fp));
1753 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
1755 nwrap->unload(nwrap);
1757 nwrap_lines_unload(nwrap);
1760 static void nwrap_files_cache_reload(struct nwrap_cache *nwrap)
1765 bool retried = false;
1767 assert(nwrap != NULL);
1770 if (nwrap->fd < 0) {
1771 nwrap->fp = fopen(nwrap->path, "re");
1772 if (nwrap->fp == NULL) {
1774 NWRAP_LOG(NWRAP_LOG_ERROR,
1775 "Unable to open '%s' readonly %d:%s",
1776 nwrap->path, nwrap->fd,
1781 nwrap->fd = fileno(nwrap->fp);
1782 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
1785 ret = fstat(nwrap->fd, &st);
1787 NWRAP_LOG(NWRAP_LOG_ERROR,
1788 "fstat(%s) - %d:%s",
1798 if (retried == false && st.st_nlink == 0) {
1799 /* maybe someone has replaced the file... */
1800 NWRAP_LOG(NWRAP_LOG_TRACE,
1801 "st_nlink == 0, reopen %s",
1804 memset(&nwrap->st, 0, sizeof(nwrap->st));
1811 if (st.st_mtime == nwrap->st.st_mtime) {
1812 NWRAP_LOG(NWRAP_LOG_TRACE,
1813 "st_mtime[%u] hasn't changed, skip reload",
1814 (unsigned)st.st_mtime);
1818 NWRAP_LOG(NWRAP_LOG_TRACE,
1819 "st_mtime has changed [%u] => [%u], start reload",
1820 (unsigned)st.st_mtime,
1821 (unsigned)nwrap->st.st_mtime);
1825 nwrap_files_cache_unload(nwrap);
1827 ok = nwrap_parse_file(nwrap);
1829 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
1830 nwrap_files_cache_unload(nwrap);
1833 NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
1837 * the caller has to call nwrap_unload() on failure
1839 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
1841 struct nwrap_pw *nwrap_pw;
1848 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1850 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
1851 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
1853 NWRAP_LOG(NWRAP_LOG_ERROR,
1854 "realloc(%u) failed",
1855 (unsigned)list_size);
1858 nwrap_pw->list = pw;
1860 pw = &nwrap_pw->list[nwrap_pw->num];
1867 NWRAP_LOG(NWRAP_LOG_ERROR,
1868 "Invalid line[%s]: '%s'",
1878 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
1883 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1891 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
1896 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1902 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
1904 NWRAP_LOG(NWRAP_LOG_ERROR,
1905 "Invalid line[%s]: '%s' - %s",
1906 line, c, strerror(errno));
1910 NWRAP_LOG(NWRAP_LOG_ERROR,
1911 "Invalid line[%s]: '%s' - %s",
1912 line, c, strerror(errno));
1916 NWRAP_LOG(NWRAP_LOG_ERROR,
1917 "Invalid line[%s]: '%s' - %s",
1918 line, c, strerror(errno));
1923 NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
1928 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1934 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
1936 NWRAP_LOG(NWRAP_LOG_ERROR,
1937 "Invalid line[%s]: '%s' - %s",
1938 line, c, strerror(errno));
1942 NWRAP_LOG(NWRAP_LOG_ERROR,
1943 "Invalid line[%s]: '%s' - %s",
1944 line, c, strerror(errno));
1948 NWRAP_LOG(NWRAP_LOG_ERROR,
1949 "Invalid line[%s]: '%s' - %s",
1950 line, c, strerror(errno));
1955 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]\n", pw->pw_gid);
1960 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
1968 NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
1973 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
1981 NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
1985 NWRAP_LOG(NWRAP_LOG_TRACE, "shell[%s]", pw->pw_shell);
1987 NWRAP_LOG(NWRAP_LOG_DEBUG,
1988 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1989 pw->pw_name, pw->pw_passwd,
1990 pw->pw_uid, pw->pw_gid,
1991 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
1997 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
1999 struct nwrap_pw *nwrap_pw;
2000 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
2002 SAFE_FREE(nwrap_pw->list);
2007 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
2008 char *buf, size_t buflen, struct passwd **dstp)
2014 first = src->pw_name;
2016 last = src->pw_shell;
2017 while (*last) last++;
2019 ofs = PTR_DIFF(last + 1, first);
2021 if (ofs > (off_t) buflen) {
2025 memcpy(buf, first, ofs);
2027 ofs = PTR_DIFF(src->pw_name, first);
2028 dst->pw_name = buf + ofs;
2029 ofs = PTR_DIFF(src->pw_passwd, first);
2030 dst->pw_passwd = buf + ofs;
2031 dst->pw_uid = src->pw_uid;
2032 dst->pw_gid = src->pw_gid;
2033 ofs = PTR_DIFF(src->pw_gecos, first);
2034 dst->pw_gecos = buf + ofs;
2035 ofs = PTR_DIFF(src->pw_dir, first);
2036 dst->pw_dir = buf + ofs;
2037 ofs = PTR_DIFF(src->pw_shell, first);
2038 dst->pw_shell = buf + ofs;
2047 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2048 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line)
2050 struct nwrap_sp *nwrap_sp;
2057 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2059 list_size = sizeof(*nwrap_sp->list) * (nwrap_sp->num+1);
2060 sp = (struct spwd *)realloc(nwrap_sp->list, list_size);
2062 NWRAP_LOG(NWRAP_LOG_ERROR,
2063 "realloc(%u) failed",
2064 (unsigned)list_size);
2067 nwrap_sp->list = sp;
2069 sp = &nwrap_sp->list[nwrap_sp->num];
2076 NWRAP_LOG(NWRAP_LOG_ERROR,
2077 "name -- Invalid line[%s]: '%s'",
2087 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", sp->sp_namp);
2092 NWRAP_LOG(NWRAP_LOG_ERROR,
2093 "pwd -- Invalid line[%s]: '%s'",
2110 NWRAP_LOG(NWRAP_LOG_ERROR,
2111 "lstchg -- Invalid line[%s]: '%s'",
2118 sp->sp_lstchg = strtol(c, &e, 10);
2120 NWRAP_LOG(NWRAP_LOG_ERROR,
2121 "lstchg -- Invalid line[%s]: '%s' - %s",
2122 line, c, strerror(errno));
2126 NWRAP_LOG(NWRAP_LOG_ERROR,
2127 "lstchg -- Invalid line[%s]: '%s' - %s",
2128 line, c, strerror(errno));
2132 NWRAP_LOG(NWRAP_LOG_ERROR,
2133 "lstchg -- Invalid line[%s]: '%s' - %s",
2134 line, c, strerror(errno));
2147 NWRAP_LOG(NWRAP_LOG_ERROR,
2148 "min -- Invalid line[%s]: '%s'",
2155 sp->sp_min = strtol(c, &e, 10);
2157 NWRAP_LOG(NWRAP_LOG_ERROR,
2158 "min -- Invalid line[%s]: '%s' - %s",
2159 line, c, strerror(errno));
2163 NWRAP_LOG(NWRAP_LOG_ERROR,
2164 "min -- Invalid line[%s]: '%s' - %s",
2165 line, c, strerror(errno));
2169 NWRAP_LOG(NWRAP_LOG_ERROR,
2170 "min -- Invalid line[%s]: '%s' - %s",
2171 line, c, strerror(errno));
2184 NWRAP_LOG(NWRAP_LOG_ERROR,
2185 "max -- Invalid line[%s]: '%s'",
2192 sp->sp_max = strtol(c, &e, 10);
2194 NWRAP_LOG(NWRAP_LOG_ERROR,
2195 "max -- Invalid line[%s]: '%s' - %s",
2196 line, c, strerror(errno));
2200 NWRAP_LOG(NWRAP_LOG_ERROR,
2201 "max -- Invalid line[%s]: '%s' - %s",
2202 line, c, strerror(errno));
2206 NWRAP_LOG(NWRAP_LOG_ERROR,
2207 "max -- Invalid line[%s]: '%s' - %s",
2208 line, c, strerror(errno));
2221 NWRAP_LOG(NWRAP_LOG_ERROR,
2222 "warn -- Invalid line[%s]: '%s'",
2229 sp->sp_warn = strtol(c, &e, 10);
2231 NWRAP_LOG(NWRAP_LOG_ERROR,
2232 "warn -- Invalid line[%s]: '%s' - %s",
2233 line, c, strerror(errno));
2237 NWRAP_LOG(NWRAP_LOG_ERROR,
2238 "warn -- Invalid line[%s]: '%s' - %s",
2239 line, c, strerror(errno));
2243 NWRAP_LOG(NWRAP_LOG_ERROR,
2244 "warn -- Invalid line[%s]: '%s' - %s",
2245 line, c, strerror(errno));
2258 NWRAP_LOG(NWRAP_LOG_ERROR,
2259 "inact -- Invalid line[%s]: '%s'",
2266 sp->sp_inact = strtol(c, &e, 10);
2268 NWRAP_LOG(NWRAP_LOG_ERROR,
2269 "inact -- Invalid line[%s]: '%s' - %s",
2270 line, c, strerror(errno));
2274 NWRAP_LOG(NWRAP_LOG_ERROR,
2275 "inact -- Invalid line[%s]: '%s' - %s",
2276 line, c, strerror(errno));
2280 NWRAP_LOG(NWRAP_LOG_ERROR,
2281 "inact -- Invalid line[%s]: '%s' - %s",
2282 line, c, strerror(errno));
2295 NWRAP_LOG(NWRAP_LOG_ERROR,
2296 "expire -- Invalid line[%s]: '%s'",
2303 sp->sp_expire = strtol(c, &e, 10);
2305 NWRAP_LOG(NWRAP_LOG_ERROR,
2306 "expire -- Invalid line[%s]: '%s' - %s",
2307 line, c, strerror(errno));
2311 NWRAP_LOG(NWRAP_LOG_ERROR,
2312 "expire -- Invalid line[%s]: '%s' - %s",
2313 line, c, strerror(errno));
2317 NWRAP_LOG(NWRAP_LOG_ERROR,
2318 "expire -- Invalid line[%s]: '%s' - %s",
2319 line, c, strerror(errno));
2329 static void nwrap_sp_unload(struct nwrap_cache *nwrap)
2331 struct nwrap_sp *nwrap_sp;
2332 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2334 SAFE_FREE(nwrap_sp->list);
2338 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2341 * the caller has to call nwrap_unload() on failure
2343 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
2345 struct nwrap_gr *nwrap_gr;
2353 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2355 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
2356 gr = (struct group *)realloc(nwrap_gr->list, list_size);
2358 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
2361 nwrap_gr->list = gr;
2363 gr = &nwrap_gr->list[nwrap_gr->num];
2370 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2378 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
2383 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2391 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
2396 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2402 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
2404 NWRAP_LOG(NWRAP_LOG_ERROR,
2405 "Invalid line[%s]: '%s' - %s",
2406 line, c, strerror(errno));
2410 NWRAP_LOG(NWRAP_LOG_ERROR,
2411 "Invalid line[%s]: '%s' - %s",
2412 line, c, strerror(errno));
2416 NWRAP_LOG(NWRAP_LOG_ERROR,
2417 "Invalid line[%s]: '%s' - %s",
2418 line, c, strerror(errno));
2423 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
2426 gr->gr_mem = (char **)malloc(sizeof(char *));
2428 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
2431 gr->gr_mem[0] = NULL;
2433 for(nummem=0; p; nummem++) {
2443 if (strlen(c) == 0) {
2447 m_size = sizeof(char *) * (nummem+2);
2448 m = (char **)realloc(gr->gr_mem, m_size);
2450 NWRAP_LOG(NWRAP_LOG_ERROR,
2451 "realloc(%zd) failed",
2456 gr->gr_mem[nummem] = c;
2457 gr->gr_mem[nummem+1] = NULL;
2459 NWRAP_LOG(NWRAP_LOG_TRACE,
2461 nummem, gr->gr_mem[nummem]);
2464 NWRAP_LOG(NWRAP_LOG_DEBUG,
2465 "Added group[%s:%s:%u:] with %u members",
2466 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem);
2472 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
2475 struct nwrap_gr *nwrap_gr;
2476 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2478 if (nwrap_gr->list) {
2479 for (i=0; i < nwrap_gr->num; i++) {
2480 SAFE_FREE(nwrap_gr->list[i].gr_mem);
2482 SAFE_FREE(nwrap_gr->list);
2489 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
2490 char *buf, size_t buflen, struct group **dstp)
2500 first = src->gr_name;
2502 lastm = src->gr_mem;
2509 last = src->gr_passwd;
2511 while (*last) last++;
2513 ofsb = PTR_DIFF(last + 1, first);
2514 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
2516 if ((ofsb + ofsm) > (off_t) buflen) {
2520 memcpy(buf, first, ofsb);
2521 memcpy(buf + ofsb, src->gr_mem, ofsm);
2523 ofs = PTR_DIFF(src->gr_name, first);
2524 dst->gr_name = buf + ofs;
2525 ofs = PTR_DIFF(src->gr_passwd, first);
2526 dst->gr_passwd = buf + ofs;
2527 dst->gr_gid = src->gr_gid;
2529 dst->gr_mem = (char **)(buf + ofsb);
2530 for (i=0; src->gr_mem[i]; i++) {
2531 ofs = PTR_DIFF(src->gr_mem[i], first);
2532 dst->gr_mem[i] = buf + ofs;
2542 static bool nwrap_add_ai(char *const ip_addr, struct nwrap_entdata *const ed)
2550 p = hsearch(e, ENTER);
2552 NWRAP_LOG(NWRAP_LOG_DEBUG, "Hash table is full");
2560 static bool nwrap_add_hname_add_new(char *const h_name,
2561 struct nwrap_entdata *const ed)
2563 /* No element found.. inventarize new item */
2568 e.data = (void *)ed;
2572 p = hsearch(e, ENTER);
2574 NWRAP_LOG(NWRAP_LOG_DEBUG, "Hash table is full!");
2581 static void nwrap_add_hname_add_to_existing(struct nwrap_entdata *const ed,
2582 struct nwrap_entdata *const ed_dst)
2584 if (ed_dst->ed_tail != NULL) {
2585 ed_dst->ed_tail->ed_next = ed;
2586 if (ed_dst->ed_tail != ed) {
2587 ed_dst->ed_tail = ed;
2591 ed_dst->ed_tail = ed;
2595 static bool nwrap_add_hname_alias(const char *const h_name_a,
2596 struct nwrap_entdata *const ed)
2598 /* One of argument 'h_hame_a' are "optional" */
2599 char *const h_name = (char *const) ((h_name_a == NULL) ? ed->ht.h_name : h_name_a);
2603 /* Maybe it's little bit late ... */
2605 assert(h_name != NULL);
2609 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
2610 p = hsearch(e, FIND);
2612 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", h_name);
2613 /* Just add alias and don't mess with metadata */
2614 nwrap_add_hname_add_new(h_name, ed);
2616 /* Element found. Add them to end of list */
2617 struct nwrap_entdata *ed_dst = (struct nwrap_entdata *)p->data;
2619 assert(p->data != NULL);
2620 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s found. Add record to list.", h_name);
2621 nwrap_add_hname_add_to_existing(ed, ed_dst);
2627 static bool nwrap_add_hname(const char *const h_name_a,
2628 struct nwrap_entdata *const ed)
2630 /* One of argument 'h_hame_a' are "optional" */
2631 char *const h_name = (char *const) ((h_name_a == NULL) ? ed->ht.h_name : h_name_a);
2637 /* Maybe it's little bit late ... */
2639 assert(h_name != NULL);
2643 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
2644 p = hsearch(e, FIND);
2646 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", h_name);
2647 /* Just add alias and don't mess with metadata */
2648 nwrap_add_hname_add_new(h_name, ed);
2650 if (ed->ed_tail == NULL) {
2654 /* Element found. Add them to end of list */
2655 struct nwrap_entdata *ed_dst = (struct nwrap_entdata *)p->data;
2657 assert(p->data != NULL);
2658 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s found. Add record to list.", h_name);
2659 nwrap_add_hname_add_to_existing(ed, ed_dst);
2662 /* Return true when list of aliases is empty */
2663 if (ed->ht.h_aliases == NULL) {
2667 /* Itemize aliases */
2668 for (i = 0; ed->ht.h_aliases[i] != NULL; ++i) {
2669 h_name_alias = ed->ht.h_aliases[i];
2670 assert(h_name_alias != NULL);
2672 NWRAP_LOG(NWRAP_LOG_DEBUG, "Add alias: %s", h_name_alias);
2674 if (!nwrap_add_hname_alias(h_name_alias, ed)) {
2675 NWRAP_LOG(NWRAP_LOG_DEBUG,
2676 "Unable to add alias: %s", h_name_alias);
2683 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line)
2685 struct nwrap_he *nwrap_he = (struct nwrap_he *)nwrap->private_data;
2686 bool do_aliases = true;
2687 ssize_t aliases_count = 0;
2694 struct nwrap_entdata *ed = (struct nwrap_entdata *)
2695 malloc(sizeof(struct nwrap_entdata));
2697 NWRAP_LOG(NWRAP_LOG_ERROR,
2698 "Unable to allocate memory for nwrap_entdata");
2709 /* Walk to first char */
2710 for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
2712 NWRAP_LOG(NWRAP_LOG_ERROR,
2713 "Invalid line[%s]: '%s'",
2720 for (i = p; !isspace((int)*p); p++) {
2722 NWRAP_LOG(NWRAP_LOG_ERROR,
2723 "Invalid line[%s]: '%s'",
2732 if (inet_pton(AF_INET, i, ed->addr.host_addr)) {
2733 ed->ht.h_addrtype = AF_INET;
2734 ed->ht.h_length = 4;
2736 } else if (inet_pton(AF_INET6, i, ed->addr.host_addr)) {
2737 ed->ht.h_addrtype = AF_INET6;
2738 ed->ht.h_length = 16;
2741 NWRAP_LOG(NWRAP_LOG_ERROR,
2742 "Invalid line[%s]: '%s'",
2750 nwrap_vector_add_item(&(ed->nwrap_addrdata),
2751 (void *const)ed->addr.host_addr);
2752 ed->ht.h_addr_list = nwrap_vector_head(&ed->nwrap_addrdata);
2760 /* Walk to first char */
2761 for (n = p; *p != '_' && !isalnum((int) *p); p++) {
2763 NWRAP_LOG(NWRAP_LOG_ERROR,
2764 "Invalid line[%s]: '%s'",
2772 for (n = p; !isspace((int)*p); p++) {
2781 /* Convert to lowercase. This operate on same memory region */
2785 /* glib's getent always dereferences he->h_aliases */
2786 ed->ht.h_aliases = malloc(sizeof(char *));
2787 if (ed->ht.h_aliases == NULL) {
2791 ed->ht.h_aliases[0] = NULL;
2796 while (do_aliases) {
2802 /* Walk to first char */
2803 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
2809 /* Only trailing spaces are left */
2814 for (a = p; !isspace((int)*p); p++) {
2823 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
2824 if (aliases == NULL) {
2828 ed->ht.h_aliases = aliases;
2831 aliases[aliases_count] = a;
2832 aliases[aliases_count + 1] = NULL;
2837 nwrap_vector_add_item(&(nwrap_he->entdata), (void *const)ed);
2839 ed->aliases_count = aliases_count;
2840 /* Inventarize item */
2841 nwrap_add_hname(NULL, ed);
2842 nwrap_add_ai(ip, ed);
2848 static void nwrap_he_unload(struct nwrap_cache *nwrap)
2850 struct nwrap_he *nwrap_he =
2851 (struct nwrap_he *)nwrap->private_data;
2852 struct nwrap_entdata *ed;
2855 nwrap_vector_foreach (ed, nwrap_he->entdata, i)
2857 SAFE_FREE(ed->ht.h_aliases);
2860 SAFE_FREE(nwrap_he->entdata.items);
2861 nwrap_he->entdata.count = nwrap_he->entdata.capacity = 0;
2868 /* user functions */
2869 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
2874 (void) b; /* unused */
2876 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
2878 nwrap_files_cache_reload(nwrap_pw_global.cache);
2880 for (i=0; i<nwrap_pw_global.num; i++) {
2881 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
2882 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
2883 return &nwrap_pw_global.list[i];
2885 NWRAP_LOG(NWRAP_LOG_DEBUG,
2886 "user[%s] does not match [%s]",
2888 nwrap_pw_global.list[i].pw_name);
2891 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
2897 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
2898 const char *name, struct passwd *pwdst,
2899 char *buf, size_t buflen, struct passwd **pwdstp)
2903 pw = nwrap_files_getpwnam(b, name);
2911 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2914 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
2919 (void) b; /* unused */
2921 nwrap_files_cache_reload(nwrap_pw_global.cache);
2923 for (i=0; i<nwrap_pw_global.num; i++) {
2924 if (nwrap_pw_global.list[i].pw_uid == uid) {
2925 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] found", uid);
2926 return &nwrap_pw_global.list[i];
2928 NWRAP_LOG(NWRAP_LOG_DEBUG,
2929 "uid[%u] does not match [%u]",
2931 nwrap_pw_global.list[i].pw_uid);
2934 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
2940 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
2941 uid_t uid, struct passwd *pwdst,
2942 char *buf, size_t buflen, struct passwd **pwdstp)
2946 pw = nwrap_files_getpwuid(b, uid);
2954 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2957 /* user enum functions */
2958 static void nwrap_files_setpwent(struct nwrap_backend *b)
2960 (void) b; /* unused */
2962 nwrap_pw_global.idx = 0;
2965 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
2969 (void) b; /* unused */
2971 if (nwrap_pw_global.idx == 0) {
2972 nwrap_files_cache_reload(nwrap_pw_global.cache);
2975 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
2980 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
2982 NWRAP_LOG(NWRAP_LOG_DEBUG,
2983 "return user[%s] uid[%u]",
2984 pw->pw_name, pw->pw_uid);
2989 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
2990 struct passwd *pwdst, char *buf,
2991 size_t buflen, struct passwd **pwdstp)
2995 pw = nwrap_files_getpwent(b);
3003 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3006 static void nwrap_files_endpwent(struct nwrap_backend *b)
3008 (void) b; /* unused */
3010 nwrap_pw_global.idx = 0;
3015 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
3017 #ifdef HAVE_SETSPENT
3018 static void nwrap_files_setspent(void)
3020 nwrap_sp_global.idx = 0;
3023 static struct spwd *nwrap_files_getspent(void)
3027 if (nwrap_sp_global.idx == 0) {
3028 nwrap_files_cache_reload(nwrap_sp_global.cache);
3031 if (nwrap_sp_global.idx >= nwrap_sp_global.num) {
3036 sp = &nwrap_sp_global.list[nwrap_sp_global.idx++];
3038 NWRAP_LOG(NWRAP_LOG_DEBUG,
3045 static void nwrap_files_endspent(void)
3047 nwrap_sp_global.idx = 0;
3049 #endif /* HAVE_SETSPENT */
3051 static struct spwd *nwrap_files_getspnam(const char *name)
3055 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
3057 nwrap_files_cache_reload(nwrap_sp_global.cache);
3059 for (i=0; i<nwrap_sp_global.num; i++) {
3060 if (strcmp(nwrap_sp_global.list[i].sp_namp, name) == 0) {
3061 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
3062 return &nwrap_sp_global.list[i];
3064 NWRAP_LOG(NWRAP_LOG_DEBUG,
3065 "user[%s] does not match [%s]",
3067 nwrap_sp_global.list[i].sp_namp);
3070 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3075 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3077 /* misc functions */
3078 static int nwrap_files_initgroups(struct nwrap_backend *b,
3087 groups = (gid_t *)malloc(size * sizeof(gid_t));
3088 if (groups == NULL) {
3089 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3095 nwrap_files_setgrent(b);
3096 while ((grp = nwrap_files_getgrent(b)) != NULL) {
3099 NWRAP_LOG(NWRAP_LOG_DEBUG,
3100 "Inspecting %s for group membership",
3103 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
3104 if (group != grp->gr_gid &&
3105 (strcmp(user, grp->gr_mem[i]) == 0)) {
3106 NWRAP_LOG(NWRAP_LOG_DEBUG,
3107 "%s is member of %s",
3111 groups = (gid_t *)realloc(groups,
3112 (size + 1) * sizeof(gid_t));
3113 if (groups == NULL) {
3114 NWRAP_LOG(NWRAP_LOG_ERROR,
3120 groups[size] = grp->gr_gid;
3126 nwrap_files_endgrent(b);
3128 NWRAP_LOG(NWRAP_LOG_DEBUG,
3129 "%s is member of %d groups",
3132 /* This really only works if uid_wrapper is loaded */
3133 rc = setgroups(size, groups);
3140 /* group functions */
3141 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
3146 (void) b; /* unused */
3148 nwrap_files_cache_reload(nwrap_gr_global.cache);
3150 for (i=0; i<nwrap_gr_global.num; i++) {
3151 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
3152 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] found", name);
3153 return &nwrap_gr_global.list[i];
3155 NWRAP_LOG(NWRAP_LOG_DEBUG,
3156 "group[%s] does not match [%s]",
3158 nwrap_gr_global.list[i].gr_name);
3161 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
3167 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
3168 const char *name, struct group *grdst,
3169 char *buf, size_t buflen, struct group **grdstp)
3173 gr = nwrap_files_getgrnam(b, name);
3181 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3184 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
3189 (void) b; /* unused */
3191 nwrap_files_cache_reload(nwrap_gr_global.cache);
3193 for (i=0; i<nwrap_gr_global.num; i++) {
3194 if (nwrap_gr_global.list[i].gr_gid == gid) {
3195 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] found", gid);
3196 return &nwrap_gr_global.list[i];
3198 NWRAP_LOG(NWRAP_LOG_DEBUG,
3199 "gid[%u] does not match [%u]",
3201 nwrap_gr_global.list[i].gr_gid);
3204 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
3210 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
3211 gid_t gid, struct group *grdst,
3212 char *buf, size_t buflen, struct group **grdstp)
3216 gr = nwrap_files_getgrgid(b, gid);
3224 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3227 /* group enum functions */
3228 static void nwrap_files_setgrent(struct nwrap_backend *b)
3230 (void) b; /* unused */
3232 nwrap_gr_global.idx = 0;
3235 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
3239 (void) b; /* unused */
3241 if (nwrap_gr_global.idx == 0) {
3242 nwrap_files_cache_reload(nwrap_gr_global.cache);
3245 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
3250 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
3252 NWRAP_LOG(NWRAP_LOG_DEBUG,
3253 "return group[%s] gid[%u]",
3254 gr->gr_name, gr->gr_gid);
3259 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
3260 struct group *grdst, char *buf,
3261 size_t buflen, struct group **grdstp)
3265 gr = nwrap_files_getgrent(b);
3273 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3276 static void nwrap_files_endgrent(struct nwrap_backend *b)
3278 (void) b; /* unused */
3280 nwrap_gr_global.idx = 0;
3283 /* hosts functions */
3284 static int nwrap_files_gethostbyname(const char *name, int af,
3285 struct hostent *result,
3286 struct nwrap_vector *addr_list)
3288 struct nwrap_entdata *ed_head;
3289 struct nwrap_entdata *ed_cur;
3294 char canon_name[DNS_NAME_MAX] = { 0 };
3296 bool he_found = false;
3298 nwrap_files_cache_reload(nwrap_he_global.cache);
3300 name_len = strlen(name);
3301 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
3302 strncpy(canon_name, name, name_len - 1);
3306 if (!str_tolower_copy(&h_name_lower, name)) {
3307 NWRAP_LOG(NWRAP_LOG_DEBUG,
3308 "Out of memory while converting to lower case");
3312 /* Look at hash table for element */
3313 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3314 e.key = h_name_lower;
3316 e_p = hsearch(e, FIND);
3318 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3319 SAFE_FREE(h_name_lower);
3322 SAFE_FREE(h_name_lower);
3324 /* Always cleanup vector and results */
3325 if (!nwrap_vector_init(addr_list)) {
3326 NWRAP_LOG(NWRAP_LOG_DEBUG,
3327 "Unable to initialize memory for addr_list vector");
3331 /* Iterate through results */
3332 ed_head = (struct nwrap_entdata *)e_p->data;
3333 for (ed_cur = ed_head; ed_cur != NULL; ed_cur = ed_cur->ed_next) {
3336 /* Filter by address familiy if provided */
3337 if (af != AF_UNSPEC && he->h_addrtype != af) {
3343 * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3345 if (af == AF_UNSPEC && he->h_addrtype != AF_INET) {
3350 memcpy(result, he, sizeof(struct hostent));
3351 NWRAP_LOG(NWRAP_LOG_DEBUG,
3352 "Name found. Returning record for %s",
3356 nwrap_vector_merge(addr_list, &ed_cur->nwrap_addrdata);
3357 result->h_addr_list = nwrap_vector_head(addr_list);
3363 NWRAP_LOG(NWRAP_LOG_DEBUG,
3364 "Name found in database. No records matches type.");
3371 #ifdef HAVE_GETHOSTBYNAME_R
3372 static int nwrap_gethostbyname_r(const char *name,
3373 struct hostent *ret,
3374 char *buf, size_t buflen,
3375 struct hostent **result, int *h_errnop)
3377 struct nwrap_vector *addr_list = malloc(sizeof(struct nwrap_vector));
3380 if (addr_list == NULL) {
3381 NWRAP_LOG(NWRAP_LOG_ERROR,
3382 "Unable to allocate memory for address list");
3387 ZERO_STRUCTP(addr_list);
3389 rc = nwrap_files_gethostbyname(name, AF_UNSPEC, ret, addr_list);
3391 *h_errnop = h_errno;
3392 if (addr_list->items != NULL) {
3393 free(addr_list->items);
3395 SAFE_FREE(addr_list);
3400 memset(buf, '\0', buflen);
3405 int gethostbyname_r(const char *name,
3406 struct hostent *ret,
3407 char *buf, size_t buflen,
3408 struct hostent **result, int *h_errnop)
3410 if (!nss_wrapper_hosts_enabled()) {
3411 return libc_gethostbyname_r(name,
3419 return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
3423 static struct addrinfo *nwrap_files_getaddrinfo(const char *name,
3424 unsigned short port,
3425 const struct addrinfo *hints,
3426 struct addrinfo **ai_tail)
3428 struct nwrap_entdata *ed_head;
3429 struct nwrap_entdata *ed_cur;
3431 struct addrinfo *ai = NULL;
3432 struct addrinfo *ai_head = NULL;
3433 struct addrinfo *ai_prev = NULL;
3436 char canon_name[DNS_NAME_MAX] = { 0 };
3437 bool skip_canonname = false;
3441 nwrap_files_cache_reload(nwrap_he_global.cache);
3443 name_len = strlen(name);
3444 if (name_len < DNS_NAME_MAX && name[name_len - 1] == '.') {
3445 strncpy(canon_name, name, name_len - 1);
3449 if (!str_tolower_copy(&h_name_lower, name)) {
3450 NWRAP_LOG(NWRAP_LOG_DEBUG,
3451 "Out of memory while converting to lower case");
3455 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3456 e.key = h_name_lower;
3458 e_p = hsearch(e, FIND);
3460 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3461 SAFE_FREE(h_name_lower);
3465 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name: %s found.", h_name_lower);
3466 SAFE_FREE(h_name_lower);
3468 ed_head = (struct nwrap_entdata *)e_p->data;
3470 for (ed_cur = ed_head; ed_cur != NULL; ed_cur = ed_cur->ed_next) {
3475 if (hints->ai_family != AF_UNSPEC &&
3476 he->h_addrtype != hints->ai_family) {
3480 /* Function allocates memory and returns it in ai. */
3481 rc = nwrap_convert_he_ai(he,
3487 /* FIXME: Investigate if this is nice to do... */
3488 NWRAP_LOG(NWRAP_LOG_ERROR,
3489 "Error in converting he to ai! Skipping.");
3492 skip_canonname = true;
3494 if (ai_head == NULL) {
3497 if (ai_prev != NULL) {
3498 ai_prev->ai_next = ai;
3507 static struct hostent *nwrap_files_gethostbyaddr(const void *addr,
3508 socklen_t len, int type)
3511 char ip[NWRAP_INET_ADDRSTRLEN] = {0};
3512 struct nwrap_entdata *ed;
3516 (void) len; /* unused */
3518 nwrap_files_cache_reload(nwrap_he_global.cache);
3520 a = inet_ntop(type, addr, ip, sizeof(ip));
3526 nwrap_vector_foreach(ed, nwrap_he_global.entdata, i)
3529 if (he->h_addrtype != type) {
3533 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
3542 #ifdef HAVE_GETHOSTBYADDR_R
3543 static int nwrap_gethostbyaddr_r(const void *addr, socklen_t len, int type,
3544 struct hostent *ret,
3545 char *buf, size_t buflen,
3546 struct hostent **result, int *h_errnop)
3548 *result = nwrap_files_gethostbyaddr(addr, len, type);
3549 if (*result != NULL) {
3550 memset(buf, '\0', buflen);
3554 *h_errnop = h_errno;
3559 int gethostbyaddr_r(const void *addr, socklen_t len, int type,
3560 struct hostent *ret,
3561 char *buf, size_t buflen,
3562 struct hostent **result, int *h_errnop)
3564 if (!nss_wrapper_hosts_enabled()) {
3565 return libc_gethostbyaddr_r(addr,
3575 return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
3579 /* hosts enum functions */
3580 static void nwrap_files_sethostent(void)
3582 nwrap_he_global.idx = 0;
3585 static struct hostent *nwrap_files_gethostent(void)
3589 if (nwrap_he_global.idx == 0) {
3590 nwrap_files_cache_reload(nwrap_he_global.cache);
3593 if (nwrap_he_global.idx >= nwrap_he_global.num) {
3598 he = &((struct nwrap_entdata *)nwrap_he_global.entdata.items[nwrap_he_global.idx++])->ht;
3600 NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
3605 static void nwrap_files_endhostent(void)
3607 nwrap_he_global.idx = 0;
3615 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
3618 static struct passwd pwd;
3619 static char buf[1000];
3622 if (!b->fns->_nss_getpwnam_r) {
3626 status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
3627 if (status == NSS_STATUS_NOTFOUND) {
3630 if (status != NSS_STATUS_SUCCESS) {
3637 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
3638 const char *name, struct passwd *pwdst,
3639 char *buf, size_t buflen, struct passwd **pwdstp)
3643 (void) b; /* unused */
3644 (void) pwdst; /* unused */
3645 (void) pwdstp; /* unused */
3647 if (!b->fns->_nss_getpwnam_r) {
3648 return NSS_STATUS_NOTFOUND;
3651 ret = b->fns->_nss_getpwnam_r(name, pwdst, buf, buflen, &errno);
3653 case NSS_STATUS_SUCCESS:
3655 case NSS_STATUS_NOTFOUND:
3660 case NSS_STATUS_TRYAGAIN:
3673 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
3676 static struct passwd pwd;
3677 static char buf[1000];
3680 if (!b->fns->_nss_getpwuid_r) {
3684 status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
3685 if (status == NSS_STATUS_NOTFOUND) {
3688 if (status != NSS_STATUS_SUCCESS) {
3694 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
3695 uid_t uid, struct passwd *pwdst,
3696 char *buf, size_t buflen, struct passwd **pwdstp)
3700 (void) pwdstp; /* unused */
3702 if (!b->fns->_nss_getpwuid_r) {
3706 ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
3708 case NSS_STATUS_SUCCESS:
3710 case NSS_STATUS_NOTFOUND:
3715 case NSS_STATUS_TRYAGAIN:
3728 static void nwrap_module_setpwent(struct nwrap_backend *b)
3730 if (!b->fns->_nss_setpwent) {
3734 b->fns->_nss_setpwent();
3737 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
3739 static struct passwd pwd;
3740 static char buf[1000];
3743 if (!b->fns->_nss_getpwent_r) {
3747 status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
3748 if (status == NSS_STATUS_NOTFOUND) {
3751 if (status != NSS_STATUS_SUCCESS) {
3757 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
3758 struct passwd *pwdst, char *buf,
3759 size_t buflen, struct passwd **pwdstp)
3763 (void) pwdstp; /* unused */
3765 if (!b->fns->_nss_getpwent_r) {
3769 ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
3771 case NSS_STATUS_SUCCESS:
3773 case NSS_STATUS_NOTFOUND:
3778 case NSS_STATUS_TRYAGAIN:
3791 static void nwrap_module_endpwent(struct nwrap_backend *b)
3793 if (!b->fns->_nss_endpwent) {
3797 b->fns->_nss_endpwent();
3800 static int nwrap_module_initgroups(struct nwrap_backend *b,
3801 const char *user, gid_t group)
3807 if (!b->fns->_nss_initgroups) {
3808 return NSS_STATUS_UNAVAIL;
3811 return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
3814 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
3817 static struct group grp;
3819 static int buflen = 1000;
3822 if (!b->fns->_nss_getgrnam_r) {
3827 buf = (char *)malloc(buflen);
3830 status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
3831 if (status == NSS_STATUS_TRYAGAIN) {
3833 buf = (char *)realloc(buf, buflen);
3839 if (status == NSS_STATUS_NOTFOUND) {
3843 if (status != NSS_STATUS_SUCCESS) {
3850 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
3851 const char *name, struct group *grdst,
3852 char *buf, size_t buflen, struct group **grdstp)
3856 (void) grdstp; /* unused */
3858 if (!b->fns->_nss_getgrnam_r) {
3862 ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
3864 case NSS_STATUS_SUCCESS:
3866 case NSS_STATUS_NOTFOUND:
3871 case NSS_STATUS_TRYAGAIN:
3884 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
3887 static struct group grp;
3889 static int buflen = 1000;
3892 if (!b->fns->_nss_getgrgid_r) {
3897 buf = (char *)malloc(buflen);
3901 status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
3902 if (status == NSS_STATUS_TRYAGAIN) {
3904 buf = (char *)realloc(buf, buflen);
3910 if (status == NSS_STATUS_NOTFOUND) {
3914 if (status != NSS_STATUS_SUCCESS) {
3921 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
3922 gid_t gid, struct group *grdst,
3923 char *buf, size_t buflen, struct group **grdstp)
3927 (void) grdstp; /* unused */
3929 if (!b->fns->_nss_getgrgid_r) {
3933 ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
3935 case NSS_STATUS_SUCCESS:
3937 case NSS_STATUS_NOTFOUND:
3942 case NSS_STATUS_TRYAGAIN:
3955 static void nwrap_module_setgrent(struct nwrap_backend *b)
3957 if (!b->fns->_nss_setgrent) {
3961 b->fns->_nss_setgrent();
3964 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
3966 static struct group grp;
3968 static int buflen = 1024;
3971 if (!b->fns->_nss_getgrent_r) {
3976 buf = (char *)malloc(buflen);
3980 status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
3981 if (status == NSS_STATUS_TRYAGAIN) {
3983 buf = (char *)realloc(buf, buflen);
3989 if (status == NSS_STATUS_NOTFOUND) {
3993 if (status != NSS_STATUS_SUCCESS) {
4000 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
4001 struct group *grdst, char *buf,
4002 size_t buflen, struct group **grdstp)
4006 (void) grdstp; /* unused */
4008 if (!b->fns->_nss_getgrent_r) {
4012 ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
4014 case NSS_STATUS_SUCCESS:
4016 case NSS_STATUS_NOTFOUND:
4021 case NSS_STATUS_TRYAGAIN:
4034 static void nwrap_module_endgrent(struct nwrap_backend *b)
4036 if (!b->fns->_nss_endgrent) {
4040 b->fns->_nss_endgrent();
4043 /****************************************************************************
4045 ***************************************************************************/
4047 static struct passwd *nwrap_getpwnam(const char *name)
4052 for (i=0; i < nwrap_main_global->num_backends; i++) {
4053 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4054 pwd = b->ops->nw_getpwnam(b, name);
4063 struct passwd *getpwnam(const char *name)
4065 if (!nss_wrapper_enabled()) {
4066 return libc_getpwnam(name);
4069 return nwrap_getpwnam(name);
4072 /****************************************************************************
4074 ***************************************************************************/
4076 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
4077 char *buf, size_t buflen, struct passwd **pwdstp)
4081 for (i=0; i < nwrap_main_global->num_backends; i++) {
4082 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4083 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
4084 if (ret == ENOENT) {
4093 #ifdef HAVE_GETPWNAM_R
4094 # ifdef HAVE_SOLARIS_GETPWNAM_R
4095 int getpwnam_r(const char *name, struct passwd *pwdst,
4096 char *buf, int buflen, struct passwd **pwdstp)
4097 # else /* HAVE_SOLARIS_GETPWNAM_R */
4098 int getpwnam_r(const char *name, struct passwd *pwdst,
4099 char *buf, size_t buflen, struct passwd **pwdstp)
4100 # endif /* HAVE_SOLARIS_GETPWNAM_R */
4102 if (!nss_wrapper_enabled()) {
4103 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4106 return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4110 /****************************************************************************
4112 ***************************************************************************/
4114 static struct passwd *nwrap_getpwuid(uid_t uid)
4119 for (i=0; i < nwrap_main_global->num_backends; i++) {
4120 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4121 pwd = b->ops->nw_getpwuid(b, uid);
4130 struct passwd *getpwuid(uid_t uid)
4132 if (!nss_wrapper_enabled()) {
4133 return libc_getpwuid(uid);
4136 return nwrap_getpwuid(uid);
4139 /****************************************************************************
4141 ***************************************************************************/
4143 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
4144 char *buf, size_t buflen, struct passwd **pwdstp)
4148 for (i=0; i < nwrap_main_global->num_backends; i++) {
4149 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4150 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
4151 if (ret == ENOENT) {
4160 #ifdef HAVE_SOLARIS_GETPWUID_R
4161 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4162 char *buf, int buflen, struct passwd **pwdstp)
4164 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4165 char *buf, size_t buflen, struct passwd **pwdstp)
4168 if (!nss_wrapper_enabled()) {
4169 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4172 return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4175 /****************************************************************************
4177 ***************************************************************************/
4179 static void nwrap_setpwent(void)
4183 for (i=0; i < nwrap_main_global->num_backends; i++) {
4184 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4185 b->ops->nw_setpwent(b);
4191 if (!nss_wrapper_enabled()) {
4199 /****************************************************************************
4201 ***************************************************************************/
4203 static struct passwd *nwrap_getpwent(void)
4208 for (i=0; i < nwrap_main_global->num_backends; i++) {
4209 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4210 pwd = b->ops->nw_getpwent(b);
4219 struct passwd *getpwent(void)
4221 if (!nss_wrapper_enabled()) {
4222 return libc_getpwent();
4225 return nwrap_getpwent();
4228 /****************************************************************************
4230 ***************************************************************************/
4232 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
4233 size_t buflen, struct passwd **pwdstp)
4237 for (i=0; i < nwrap_main_global->num_backends; i++) {
4238 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4239 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
4240 if (ret == ENOENT) {
4249 #ifdef HAVE_SOLARIS_GETPWENT_R
4250 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
4252 struct passwd *pwdstp = NULL;
4255 if (!nss_wrapper_enabled()) {
4256 return libc_getpwent_r(pwdst, buf, buflen);
4258 rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
4265 #else /* HAVE_SOLARIS_GETPWENT_R */
4266 int getpwent_r(struct passwd *pwdst, char *buf,
4267 size_t buflen, struct passwd **pwdstp)
4269 if (!nss_wrapper_enabled()) {
4270 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
4273 return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
4275 #endif /* HAVE_SOLARIS_GETPWENT_R */
4277 /****************************************************************************
4279 ***************************************************************************/
4281 static void nwrap_endpwent(void)
4285 for (i=0; i < nwrap_main_global->num_backends; i++) {
4286 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4287 b->ops->nw_endpwent(b);
4293 if (!nss_wrapper_enabled()) {
4301 /****************************************************************************
4303 ***************************************************************************/
4305 static int nwrap_initgroups(const char *user, gid_t group)
4309 for (i=0; i < nwrap_main_global->num_backends; i++) {
4310 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4313 rc = b->ops->nw_initgroups(b, user, group);
4323 int initgroups(const char *user, gid_t group)
4325 if (!nss_wrapper_enabled()) {
4326 return libc_initgroups(user, group);
4329 return nwrap_initgroups(user, group);
4332 /****************************************************************************
4334 ***************************************************************************/
4336 static struct group *nwrap_getgrnam(const char *name)
4341 for (i=0; i < nwrap_main_global->num_backends; i++) {
4342 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4343 grp = b->ops->nw_getgrnam(b, name);
4352 struct group *getgrnam(const char *name)
4354 if (!nss_wrapper_enabled()) {
4355 return libc_getgrnam(name);
4358 return nwrap_getgrnam(name);
4361 /****************************************************************************
4363 ***************************************************************************/
4365 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
4366 char *buf, size_t buflen, struct group **grdstp)
4370 for (i=0; i < nwrap_main_global->num_backends; i++) {
4371 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4372 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
4373 if (ret == ENOENT) {
4382 #ifdef HAVE_GETGRNAM_R
4383 # ifdef HAVE_SOLARIS_GETGRNAM_R
4384 int getgrnam_r(const char *name, struct group *grp,
4385 char *buf, int buflen, struct group **pgrp)
4386 # else /* HAVE_SOLARIS_GETGRNAM_R */
4387 int getgrnam_r(const char *name, struct group *grp,
4388 char *buf, size_t buflen, struct group **pgrp)
4389 # endif /* HAVE_SOLARIS_GETGRNAM_R */
4391 if (!nss_wrapper_enabled()) {
4392 return libc_getgrnam_r(name,
4399 return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
4401 #endif /* HAVE_GETGRNAM_R */
4403 /****************************************************************************
4405 ***************************************************************************/
4407 static struct group *nwrap_getgrgid(gid_t gid)
4412 for (i=0; i < nwrap_main_global->num_backends; i++) {
4413 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4414 grp = b->ops->nw_getgrgid(b, gid);
4423 struct group *getgrgid(gid_t gid)
4425 if (!nss_wrapper_enabled()) {
4426 return libc_getgrgid(gid);
4429 return nwrap_getgrgid(gid);
4432 /****************************************************************************
4434 ***************************************************************************/
4436 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
4437 char *buf, size_t buflen, struct group **grdstp)
4441 for (i=0; i < nwrap_main_global->num_backends; i++) {
4442 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4443 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
4444 if (ret == ENOENT) {
4453 #ifdef HAVE_GETGRGID_R
4454 # ifdef HAVE_SOLARIS_GETGRGID_R
4455 int getgrgid_r(gid_t gid, struct group *grdst,
4456 char *buf, int buflen, struct group **grdstp)
4457 # else /* HAVE_SOLARIS_GETGRGID_R */
4458 int getgrgid_r(gid_t gid, struct group *grdst,
4459 char *buf, size_t buflen, struct group **grdstp)
4460 # endif /* HAVE_SOLARIS_GETGRGID_R */
4462 if (!nss_wrapper_enabled()) {
4463 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4466 return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4470 /****************************************************************************
4472 ***************************************************************************/
4474 static void nwrap_setgrent(void)
4478 for (i=0; i < nwrap_main_global->num_backends; i++) {
4479 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4480 b->ops->nw_setgrent(b);
4484 #ifdef HAVE_BSD_SETGRENT
4490 if (!nss_wrapper_enabled()) {
4498 #ifdef HAVE_BSD_SETGRENT
4505 /****************************************************************************
4507 ***************************************************************************/
4509 static struct group *nwrap_getgrent(void)
4514 for (i=0; i < nwrap_main_global->num_backends; i++) {
4515 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4516 grp = b->ops->nw_getgrent(b);
4525 struct group *getgrent(void)
4527 if (!nss_wrapper_enabled()) {
4528 return libc_getgrent();
4531 return nwrap_getgrent();
4534 /****************************************************************************
4536 ***************************************************************************/
4538 static int nwrap_getgrent_r(struct group *grdst, char *buf,
4539 size_t buflen, struct group **grdstp)
4543 for (i=0; i < nwrap_main_global->num_backends; i++) {
4544 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4545 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
4546 if (ret == ENOENT) {
4555 #ifdef HAVE_SOLARIS_GETGRENT_R
4556 struct group *getgrent_r(struct group *src, char *buf, int buflen)
4558 struct group *grdstp = NULL;
4561 if (!nss_wrapper_enabled()) {
4562 return libc_getgrent_r(src, buf, buflen);
4565 rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
4572 #else /* HAVE_SOLARIS_GETGRENT_R */
4573 int getgrent_r(struct group *src, char *buf,
4574 size_t buflen, struct group **grdstp)
4576 if (!nss_wrapper_enabled()) {
4577 return libc_getgrent_r(src, buf, buflen, grdstp);
4580 return nwrap_getgrent_r(src, buf, buflen, grdstp);
4582 #endif /* HAVE_SOLARIS_GETGRENT_R */
4584 /****************************************************************************
4586 ***************************************************************************/
4588 static void nwrap_endgrent(void)
4592 for (i=0; i < nwrap_main_global->num_backends; i++) {
4593 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4594 b->ops->nw_endgrent(b);
4600 if (!nss_wrapper_enabled()) {
4608 /****************************************************************************
4610 ***************************************************************************/
4612 #ifdef HAVE_GETGROUPLIST
4613 static int nwrap_getgrouplist(const char *user, gid_t group,
4614 gid_t *groups, int *ngroups)
4620 NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
4622 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
4624 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
4628 groups_tmp[0] = group;
4631 while ((grp = nwrap_getgrent()) != NULL) {
4634 NWRAP_LOG(NWRAP_LOG_DEBUG,
4635 "Inspecting %s for group membership",
4638 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
4640 if (group != grp->gr_gid &&
4641 (strcmp(user, grp->gr_mem[i]) == 0)) {
4643 NWRAP_LOG(NWRAP_LOG_DEBUG,
4644 "%s is member of %s",
4648 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
4650 NWRAP_LOG(NWRAP_LOG_ERROR,
4655 groups_tmp[count] = grp->gr_gid;
4664 NWRAP_LOG(NWRAP_LOG_DEBUG,
4665 "%s is member of %d groups",
4668 if (*ngroups < count) {
4675 memcpy(groups, groups_tmp, count * sizeof(gid_t));
4681 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
4683 if (!nss_wrapper_enabled()) {
4684 return libc_getgrouplist(user, group, groups, ngroups);
4687 return nwrap_getgrouplist(user, group, groups, ngroups);
4691 /**********************************************************
4693 **********************************************************/
4695 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
4697 #ifdef HAVE_SETSPENT
4698 static void nwrap_setspent(void)
4700 nwrap_files_setspent();
4705 if (!nss_wrapper_shadow_enabled()) {
4712 static struct spwd *nwrap_getspent(void)
4714 return nwrap_files_getspent();
4717 struct spwd *getspent(void)
4719 if (!nss_wrapper_shadow_enabled()) {
4723 return nwrap_getspent();
4726 static void nwrap_endspent(void)
4728 nwrap_files_endspent();
4733 if (!nss_wrapper_shadow_enabled()) {
4739 #endif /* HAVE_SETSPENT */
4741 static struct spwd *nwrap_getspnam(const char *name)
4743 return nwrap_files_getspnam(name);
4746 struct spwd *getspnam(const char *name)
4748 if (!nss_wrapper_shadow_enabled()) {
4752 return nwrap_getspnam(name);
4755 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
4757 /**********************************************************
4759 **********************************************************/
4761 static void nwrap_sethostent(int stayopen) {
4762 (void) stayopen; /* ignored */
4764 nwrap_files_sethostent();
4767 #ifdef HAVE_SOLARIS_SETHOSTENT
4768 int sethostent(int stayopen)
4770 if (!nss_wrapper_hosts_enabled()) {
4771 libc_sethostent(stayopen);
4775 nwrap_sethostent(stayopen);
4779 #else /* HAVE_SOLARIS_SETHOSTENT */
4780 void sethostent(int stayopen)
4782 if (!nss_wrapper_hosts_enabled()) {
4783 libc_sethostent(stayopen);
4787 nwrap_sethostent(stayopen);
4789 #endif /* HAVE_SOLARIS_SETHOSTENT */
4791 static struct hostent *nwrap_gethostent(void)
4793 return nwrap_files_gethostent();
4796 struct hostent *gethostent(void) {
4797 if (!nss_wrapper_hosts_enabled()) {
4798 return libc_gethostent();
4801 return nwrap_gethostent();
4804 static void nwrap_endhostent(void) {
4805 nwrap_files_endhostent();
4808 #ifdef HAVE_SOLARIS_ENDHOSTENT
4809 int endhostent(void)
4811 if (!nss_wrapper_hosts_enabled()) {
4820 #else /* HAVE_SOLARIS_ENDHOSTENT */
4821 void endhostent(void)
4823 if (!nss_wrapper_hosts_enabled()) {
4830 #endif /* HAVE_SOLARIS_ENDHOSTENT */
4833 /* BSD implementation stores data in thread local storage but GLIBC does not */
4834 static __thread struct hostent user_he;
4835 static __thread struct nwrap_vector user_addrlist;
4837 static struct hostent user_he;
4838 static struct nwrap_vector user_addrlist;
4840 static struct hostent *nwrap_gethostbyname(const char *name)
4842 if (nwrap_files_gethostbyname(name, AF_UNSPEC, &user_he, &user_addrlist) == -1) {
4848 struct hostent *gethostbyname(const char *name)
4850 if (!nss_wrapper_hosts_enabled()) {
4851 return libc_gethostbyname(name);
4854 return nwrap_gethostbyname(name);
4857 /* This is a GNU extension - Also can be found on BSD systems */
4858 #ifdef HAVE_GETHOSTBYNAME2
4860 /* BSD implementation stores data in thread local storage but GLIBC not */
4861 static __thread struct hostent user_he2;
4862 static __thread struct nwrap_vector user_addrlist2;
4864 static struct hostent user_he2;
4865 static struct nwrap_vector user_addrlist2;
4867 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
4869 if (nwrap_files_gethostbyname(name, af, &user_he2, &user_addrlist2) == -1) {
4875 struct hostent *gethostbyname2(const char *name, int af)
4877 if (!nss_wrapper_hosts_enabled()) {
4878 return libc_gethostbyname2(name, af);
4881 return nwrap_gethostbyname2(name, af);
4885 static struct hostent *nwrap_gethostbyaddr(const void *addr,
4886 socklen_t len, int type)
4888 return nwrap_files_gethostbyaddr(addr, len, type);
4891 struct hostent *gethostbyaddr(const void *addr,
4892 socklen_t len, int type)
4894 if (!nss_wrapper_hosts_enabled()) {
4895 return libc_gethostbyaddr(addr, len, type);
4898 return nwrap_gethostbyaddr(addr, len, type);
4901 static const struct addrinfo default_hints =
4903 .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
4904 .ai_family = AF_UNSPEC,
4909 .ai_canonname = NULL,
4913 static int nwrap_convert_he_ai(const struct hostent *he,
4914 unsigned short port,
4915 const struct addrinfo *hints,
4916 struct addrinfo **pai,
4917 bool skip_canonname)
4919 struct addrinfo *ai;
4926 switch (he->h_addrtype) {
4928 socklen = sizeof(struct sockaddr_in);
4932 socklen = sizeof(struct sockaddr_in6);
4939 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
4945 ai->ai_family = he->h_addrtype;
4946 ai->ai_socktype = hints->ai_socktype;
4947 ai->ai_protocol = hints->ai_protocol;
4948 ai->ai_canonname = NULL;
4950 ai->ai_addrlen = socklen;
4951 ai->ai_addr = (void *)(ai + 1);
4953 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
4954 ai->ai_addr->sa_len = socklen;
4956 ai->ai_addr->sa_family = he->h_addrtype;
4958 switch (he->h_addrtype) {
4961 struct sockaddr_in *sinp =
4962 (struct sockaddr_in *) ai->ai_addr;
4964 memset(sinp, 0, sizeof(struct sockaddr_in));
4966 sinp->sin_port = htons(port);
4967 sinp->sin_family = AF_INET;
4969 memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
4970 memcpy(&sinp->sin_addr, he->h_addr_list[0], he->h_length);
4977 struct sockaddr_in6 *sin6p =
4978 (struct sockaddr_in6 *) ai->ai_addr;
4980 memset(sin6p, 0, sizeof(struct sockaddr_in6));
4982 sin6p->sin6_port = htons(port);
4983 sin6p->sin6_family = AF_INET6;
4985 memcpy(&sin6p->sin6_addr,
4995 if (he->h_name && !skip_canonname) {
4996 ai->ai_canonname = strdup(he->h_name);
4997 if (ai->ai_canonname == NULL) {
5007 static int nwrap_getaddrinfo(const char *node,
5008 const char *service,
5009 const struct addrinfo *hints,
5010 struct addrinfo **res)
5012 struct addrinfo *ai = NULL;
5013 struct addrinfo *p = NULL;
5014 struct addrinfo *ai_tail;
5015 unsigned short port = 0;
5025 .family = AF_UNSPEC,
5027 int eai = EAI_SYSTEM;
5031 if (node == NULL && service == NULL) {
5035 if (hints == NULL) {
5036 hints = &default_hints;
5040 hints.ai_flags contains invalid flags; or, hints.ai_flags
5041 included AI_CANONNAME and name was NULL.
5043 if ((hints->ai_flags & AI_CANONNAME) && (node == NULL)) {
5044 return EAI_BADFLAGS;
5047 ret = libc_getaddrinfo(node, service, hints, &p);
5051 /* If no node has been specified, let glibc deal with it */
5056 if (service != NULL && service[0] != '\0') {
5057 if (isdigit((int)service[0])) {
5058 port = (unsigned short)atoi(service);
5060 const char *proto = NULL;
5063 if (hints->ai_protocol != 0) {
5064 struct protoent *pent;
5066 pent = getprotobynumber(hints->ai_protocol);
5068 proto = pent->p_name;
5072 s = getservbyname(service, proto);
5074 port = ntohs(s->s_port);
5085 if (hints->ai_family == AF_UNSPEC || hints->ai_family == AF_INET) {
5086 rc = inet_pton(AF_INET, node, &addr.in.v4);
5089 addr.family = AF_INET;
5092 rc = inet_pton(AF_INET6, node, &addr.in.v6);
5094 addr.family = AF_INET6;
5100 if (addr.family == AF_INET) {
5101 ai = nwrap_files_getaddrinfo(node, port, hints, &ai_tail);
5106 } else if (addr.family == AF_INET6) {
5107 ai = nwrap_files_getaddrinfo(node, port, hints, &ai_tail);
5113 ai = nwrap_files_getaddrinfo(node, port, hints, &ai_tail);
5120 return ret == 0 ? 0 : eai;
5127 if (ai->ai_flags == 0) {
5128 ai->ai_flags = hints->ai_flags;
5130 if (ai->ai_socktype == 0) {
5131 ai->ai_socktype = SOCK_DGRAM;
5133 if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_DGRAM) {
5134 ai->ai_protocol = 17; /* UDP */
5135 } else if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_STREAM) {
5136 ai->ai_protocol = 6; /* TCP */
5139 if (hints->ai_socktype == 0) {
5141 struct addrinfo *ai_head = ai;
5142 struct addrinfo *ai_tmp;
5143 struct addrinfo *ai_new_tail = ai_tail;
5145 /* Add at least one more struct */
5148 ai_tmp = malloc(sizeof(struct addrinfo));
5149 memcpy(ai_tmp, ai_head, sizeof(struct addrinfo));
5150 ai_tmp->ai_next = NULL;
5152 /* We need a deep copy or freeaddrinfo() will blow up */
5153 if (ai_head->ai_canonname != NULL) {
5154 ai_tmp->ai_canonname =
5155 strdup(ai_head->ai_canonname);
5157 /* ai_head should point inside hints. */
5158 ai_tmp->ai_addr = ai_head->ai_addr;
5160 if (ai_head->ai_flags == 0) {
5161 ai_tmp->ai_flags = hints->ai_flags;
5163 if (ai_head->ai_socktype == SOCK_DGRAM) {
5164 ai_tmp->ai_socktype = SOCK_STREAM;
5165 } else if (ai_head->ai_socktype == SOCK_STREAM) {
5166 ai_tmp->ai_socktype = SOCK_DGRAM;
5168 if (ai_head->ai_socktype == SOCK_DGRAM) {
5169 ai_tmp->ai_protocol = 17; /* UDP */
5170 } else if (ai_head->ai_socktype == SOCK_STREAM) {
5171 ai_tmp->ai_protocol = 6; /* TCP */
5173 ai_new_tail->ai_next = ai_tmp;
5174 ai_new_tail = ai_tmp;
5176 if (ai_head == ai_tail) {
5179 ai_head = ai_head->ai_next;
5188 int getaddrinfo(const char *node, const char *service,
5189 const struct addrinfo *hints,
5190 struct addrinfo **res)
5192 if (!nss_wrapper_hosts_enabled()) {
5193 return libc_getaddrinfo(node, service, hints, res);
5196 return nwrap_getaddrinfo(node, service, hints, res);
5199 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
5200 char *host, size_t hostlen,
5201 char *serv, size_t servlen,
5205 struct servent *service;
5212 if (sa == NULL || salen < sizeof(sa_family_t)) {
5216 if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
5220 type = sa->sa_family;
5223 if (salen < sizeof(struct sockaddr_in))
5225 addr = &((const struct sockaddr_in *)sa)->sin_addr;
5226 addrlen = sizeof(((const struct sockaddr_in *)sa)->sin_addr);
5227 port = ntohs(((const struct sockaddr_in *)sa)->sin_port);
5231 if (salen < sizeof(struct sockaddr_in6))
5233 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
5234 addrlen = sizeof(((const struct sockaddr_in6 *)sa)->sin6_addr);
5235 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port);
5244 if ((flags & NI_NUMERICHOST) == 0) {
5245 he = nwrap_files_gethostbyaddr(addr, addrlen, type);
5246 if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
5249 if (he != NULL && he->h_name != NULL) {
5250 if (strlen(he->h_name) >= hostlen)
5251 return EAI_OVERFLOW;
5252 strcpy(host, he->h_name);
5253 if (flags & NI_NOFQDN)
5254 host[strcspn(host, ".")] = '\0';
5256 if (inet_ntop(type, addr, host, hostlen) == NULL)
5257 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
5263 if ((flags & NI_NUMERICSERV) == 0) {
5264 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
5265 service = getservbyport(htons(port), proto);
5267 if (service != NULL) {
5268 if (strlen(service->s_name) >= servlen)
5269 return EAI_OVERFLOW;
5270 strcpy(serv, service->s_name);
5272 if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
5273 return EAI_OVERFLOW;
5280 #ifdef HAVE_LINUX_GETNAMEINFO
5281 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5282 char *host, socklen_t hostlen,
5283 char *serv, socklen_t servlen,
5285 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
5286 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5287 char *host, socklen_t hostlen,
5288 char *serv, socklen_t servlen,
5291 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5292 char *host, size_t hostlen,
5293 char *serv, size_t servlen,
5297 if (!nss_wrapper_hosts_enabled()) {
5298 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5301 return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5304 static int nwrap_gethostname(char *name, size_t len)
5306 const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
5308 if (strlen(hostname) >= len) {
5309 errno = ENAMETOOLONG;
5312 snprintf(name, len, "%s", hostname);
5317 #ifdef HAVE_SOLARIS_GETHOSTNAME
5318 int gethostname(char *name, int len)
5319 #else /* HAVE_SOLARIS_GETHOSTNAME */
5320 int gethostname(char *name, size_t len)
5321 #endif /* HAVE_SOLARIS_GETHOSTNAME */
5323 if (!nwrap_hostname_enabled()) {
5324 return libc_gethostname(name, len);
5327 return nwrap_gethostname(name, len);
5330 /****************************
5332 ***************************/
5335 * This function is called when the library is unloaded and makes sure that
5336 * sockets get closed and the unix file for the socket are unlinked.
5338 void nwrap_destructor(void)
5343 if (nwrap_main_global != NULL) {
5344 struct nwrap_main *m = nwrap_main_global;
5347 SAFE_FREE(m->libc->fns);
5348 if (m->libc->handle != NULL) {
5349 dlclose(m->libc->handle);
5351 if (m->libc->nsl_handle != NULL) {
5352 dlclose(m->libc->nsl_handle);
5354 if (m->libc->sock_handle != NULL) {
5355 dlclose(m->libc->sock_handle);
5360 for (i = 0; i < m->num_backends; i++) {
5361 struct nwrap_backend *b = &(m->backends[i]);
5363 if (b->so_handle != NULL) {
5364 dlclose(b->so_handle);
5368 SAFE_FREE(m->backends);
5371 if (nwrap_pw_global.cache != NULL) {
5372 struct nwrap_cache *c = nwrap_pw_global.cache;
5374 nwrap_files_cache_unload(c);
5380 SAFE_FREE(nwrap_pw_global.list);
5381 nwrap_pw_global.num = 0;
5384 if (nwrap_gr_global.cache != NULL) {
5385 struct nwrap_cache *c = nwrap_gr_global.cache;
5387 nwrap_files_cache_unload(c);
5393 SAFE_FREE(nwrap_gr_global.list);
5394 nwrap_pw_global.num = 0;
5397 if (nwrap_he_global.cache != NULL) {
5398 struct nwrap_cache *c = nwrap_he_global.cache;
5400 nwrap_files_cache_unload(c);
5406 nwrap_he_global.num = 0;