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>
55 #include <netinet/in.h>
61 * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h gives us
62 * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
65 #ifndef _POSIX_PTHREAD_SEMANTICS
66 #define _POSIX_PTHREAD_SEMANTICS
73 #endif /* HAVE_SHADOW_H */
76 #include <arpa/inet.h>
77 #include <netinet/in.h>
81 #if defined(HAVE_NSS_H)
85 typedef enum nss_status NSS_STATUS;
86 #elif defined(HAVE_NSS_COMMON_H)
88 #include <nss_common.h>
89 #include <nss_dbdefs.h>
92 typedef nss_status_t NSS_STATUS;
94 # define NSS_STATUS_SUCCESS NSS_SUCCESS
95 # define NSS_STATUS_NOTFOUND NSS_NOTFOUND
96 # define NSS_STATUS_UNAVAIL NSS_UNAVAIL
97 # define NSS_STATUS_TRYAGAIN NSS_TRYAGAIN
99 # error "No nsswitch support detected"
103 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
111 #define EAI_NODATA EAI_NONAME
114 #ifndef EAI_ADDRFAMILY
115 #define EAI_ADDRFAMILY EAI_FAMILY
119 #define __STRING(x) #x
122 #ifndef __STRINGSTRING
123 #define __STRINGSTRING(x) __STRING(x)
127 #define __LINESTR__ __STRINGSTRING(__LINE__)
131 #define __location__ __FILE__ ":" __LINESTR__
135 #define DNS_NAME_MAX 255
138 /* GCC have printf type attribute check. */
139 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
140 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
142 #define PRINTF_ATTRIBUTE(a,b)
143 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
145 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
146 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
148 #define DESTRUCTOR_ATTRIBUTE
149 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
151 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
154 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
158 #define NWRAP_INET_ADDRSTRLEN INET6_ADDRSTRLEN
160 #define NWRAP_INET_ADDRSTRLEN INET_ADDRSTRLEN
163 #define NWRAP_LOCK(m) do { \
164 pthread_mutex_lock(&( m ## _mutex)); \
167 #define NWRAP_UNLOCK(m) do { \
168 pthread_mutex_unlock(&( m ## _mutex)); \
172 static bool nwrap_initialized = false;
173 static pthread_mutex_t nwrap_initialized_mutex = PTHREAD_MUTEX_INITIALIZER;
175 /* The mutex or accessing the id */
176 static pthread_mutex_t nwrap_global_mutex = PTHREAD_MUTEX_INITIALIZER;
177 static pthread_mutex_t nwrap_gr_global_mutex = PTHREAD_MUTEX_INITIALIZER;
178 static pthread_mutex_t nwrap_he_global_mutex = PTHREAD_MUTEX_INITIALIZER;
179 static pthread_mutex_t nwrap_pw_global_mutex = PTHREAD_MUTEX_INITIALIZER;
180 static pthread_mutex_t nwrap_sp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
182 /* Add new global locks here please */
183 /* Also don't forget to add locks to
184 * nwrap_init() function.
186 # define NWRAP_LOCK_ALL do { \
187 NWRAP_LOCK(nwrap_initialized); \
188 NWRAP_LOCK(nwrap_global); \
189 NWRAP_LOCK(nwrap_gr_global); \
190 NWRAP_LOCK(nwrap_he_global); \
191 NWRAP_LOCK(nwrap_pw_global); \
192 NWRAP_LOCK(nwrap_sp_global); \
195 # define NWRAP_UNLOCK_ALL do {\
196 NWRAP_UNLOCK(nwrap_sp_global); \
197 NWRAP_UNLOCK(nwrap_pw_global); \
198 NWRAP_UNLOCK(nwrap_he_global); \
199 NWRAP_UNLOCK(nwrap_gr_global); \
200 NWRAP_UNLOCK(nwrap_global); \
201 NWRAP_UNLOCK(nwrap_initialized); \
204 static void nwrap_thread_prepare(void)
209 static void nwrap_thread_parent(void)
214 static void nwrap_thread_child(void)
219 enum nwrap_dbglvl_e {
227 # define NWRAP_LOG(...)
230 static void nwrap_log(enum nwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
231 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
233 static void nwrap_log(enum nwrap_dbglvl_e dbglvl,
235 const char *format, ...)
240 unsigned int lvl = 0;
243 d = getenv("NSS_WRAPPER_DEBUGLEVEL");
248 va_start(va, format);
249 vsnprintf(buffer, sizeof(buffer), format, va);
254 case NWRAP_LOG_ERROR:
256 "NWRAP_ERROR(%d) - %s: %s\n",
261 "NWRAP_WARN(%d) - %s: %s\n",
264 case NWRAP_LOG_DEBUG:
266 "NWRAP_DEBUG(%d) - %s: %s\n",
269 case NWRAP_LOG_TRACE:
271 "NWRAP_TRACE(%d) - %s: %s\n",
277 #endif /* NDEBUG NWRAP_LOG */
279 struct nwrap_libc_fns {
280 struct passwd *(*_libc_getpwnam)(const char *name);
281 int (*_libc_getpwnam_r)(const char *name, struct passwd *pwd,
282 char *buf, size_t buflen, struct passwd **result);
283 struct passwd *(*_libc_getpwuid)(uid_t uid);
284 int (*_libc_getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);
285 void (*_libc_setpwent)(void);
286 struct passwd *(*_libc_getpwent)(void);
287 #ifdef HAVE_SOLARIS_GETPWENT_R
288 struct passwd *(*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen);
290 int (*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp);
292 void (*_libc_endpwent)(void);
293 int (*_libc_initgroups)(const char *user, gid_t gid);
294 struct group *(*_libc_getgrnam)(const char *name);
295 int (*_libc_getgrnam_r)(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result);
296 struct group *(*_libc_getgrgid)(gid_t gid);
297 int (*_libc_getgrgid_r)(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result);
298 void (*_libc_setgrent)(void);
299 struct group *(*_libc_getgrent)(void);
300 #ifdef HAVE_SOLARIS_GETGRENT_R
301 struct group *(*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen);
303 int (*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen, struct group **result);
305 void (*_libc_endgrent)(void);
306 int (*_libc_getgrouplist)(const char *user, gid_t group, gid_t *groups, int *ngroups);
308 void (*_libc_sethostent)(int stayopen);
309 struct hostent *(*_libc_gethostent)(void);
310 void (*_libc_endhostent)(void);
312 struct hostent *(*_libc_gethostbyname)(const char *name);
313 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
314 struct hostent *(*_libc_gethostbyname2)(const char *name, int af);
316 struct hostent *(*_libc_gethostbyaddr)(const void *addr, socklen_t len, int type);
318 int (*_libc_getaddrinfo)(const char *node, const char *service,
319 const struct addrinfo *hints,
320 struct addrinfo **res);
321 int (*_libc_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
322 char *host, size_t hostlen,
323 char *serv, size_t servlen,
325 int (*_libc_gethostname)(char *name, size_t len);
326 #ifdef HAVE_GETHOSTBYNAME_R
327 int (*_libc_gethostbyname_r)(const char *name,
329 char *buf, size_t buflen,
330 struct hostent **result, int *h_errnop);
332 #ifdef HAVE_GETHOSTBYADDR_R
333 int (*_libc_gethostbyaddr_r)(const void *addr, socklen_t len, int type,
335 char *buf, size_t buflen,
336 struct hostent **result, int *h_errnop);
340 struct nwrap_module_nss_fns {
341 NSS_STATUS (*_nss_getpwnam_r)(const char *name, struct passwd *result, char *buffer,
342 size_t buflen, int *errnop);
343 NSS_STATUS (*_nss_getpwuid_r)(uid_t uid, struct passwd *result, char *buffer,
344 size_t buflen, int *errnop);
345 NSS_STATUS (*_nss_setpwent)(void);
346 NSS_STATUS (*_nss_getpwent_r)(struct passwd *result, char *buffer,
347 size_t buflen, int *errnop);
348 NSS_STATUS (*_nss_endpwent)(void);
349 NSS_STATUS (*_nss_initgroups)(const char *user, gid_t group, long int *start,
350 long int *size, gid_t **groups, long int limit, int *errnop);
351 NSS_STATUS (*_nss_getgrnam_r)(const char *name, struct group *result, char *buffer,
352 size_t buflen, int *errnop);
353 NSS_STATUS (*_nss_getgrgid_r)(gid_t gid, struct group *result, char *buffer,
354 size_t buflen, int *errnop);
355 NSS_STATUS (*_nss_setgrent)(void);
356 NSS_STATUS (*_nss_getgrent_r)(struct group *result, char *buffer,
357 size_t buflen, int *errnop);
358 NSS_STATUS (*_nss_endgrent)(void);
361 struct nwrap_backend {
365 struct nwrap_ops *ops;
366 struct nwrap_module_nss_fns *fns;
370 struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
372 int (*nw_getpwnam_r)(struct nwrap_backend *b,
373 const char *name, struct passwd *pwdst,
374 char *buf, size_t buflen, struct passwd **pwdstp);
375 struct passwd * (*nw_getpwuid)(struct nwrap_backend *b,
377 int (*nw_getpwuid_r)(struct nwrap_backend *b,
378 uid_t uid, struct passwd *pwdst,
379 char *buf, size_t buflen, struct passwd **pwdstp);
380 void (*nw_setpwent)(struct nwrap_backend *b);
381 struct passwd * (*nw_getpwent)(struct nwrap_backend *b);
382 int (*nw_getpwent_r)(struct nwrap_backend *b,
383 struct passwd *pwdst, char *buf,
384 size_t buflen, struct passwd **pwdstp);
385 void (*nw_endpwent)(struct nwrap_backend *b);
386 int (*nw_initgroups)(struct nwrap_backend *b,
387 const char *user, gid_t group);
388 struct group * (*nw_getgrnam)(struct nwrap_backend *b,
390 int (*nw_getgrnam_r)(struct nwrap_backend *b,
391 const char *name, struct group *grdst,
392 char *buf, size_t buflen, struct group **grdstp);
393 struct group * (*nw_getgrgid)(struct nwrap_backend *b,
395 int (*nw_getgrgid_r)(struct nwrap_backend *b,
396 gid_t gid, struct group *grdst,
397 char *buf, size_t buflen, struct group **grdstp);
398 void (*nw_setgrent)(struct nwrap_backend *b);
399 struct group * (*nw_getgrent)(struct nwrap_backend *b);
400 int (*nw_getgrent_r)(struct nwrap_backend *b,
401 struct group *grdst, char *buf,
402 size_t buflen, struct group **grdstp);
403 void (*nw_endgrent)(struct nwrap_backend *b);
406 /* Public prototypes */
408 bool nss_wrapper_enabled(void);
409 bool nss_wrapper_shadow_enabled(void);
410 bool nss_wrapper_hosts_enabled(void);
412 /* prototypes for files backend */
415 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
417 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
418 const char *name, struct passwd *pwdst,
419 char *buf, size_t buflen, struct passwd **pwdstp);
420 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
422 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
423 uid_t uid, struct passwd *pwdst,
424 char *buf, size_t buflen, struct passwd **pwdstp);
425 static void nwrap_files_setpwent(struct nwrap_backend *b);
426 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b);
427 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
428 struct passwd *pwdst, char *buf,
429 size_t buflen, struct passwd **pwdstp);
430 static void nwrap_files_endpwent(struct nwrap_backend *b);
431 static int nwrap_files_initgroups(struct nwrap_backend *b,
432 const char *user, gid_t group);
433 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
435 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
436 const char *name, struct group *grdst,
437 char *buf, size_t buflen, struct group **grdstp);
438 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
440 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
441 gid_t gid, struct group *grdst,
442 char *buf, size_t buflen, struct group **grdstp);
443 static void nwrap_files_setgrent(struct nwrap_backend *b);
444 static struct group *nwrap_files_getgrent(struct nwrap_backend *b);
445 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
446 struct group *grdst, char *buf,
447 size_t buflen, struct group **grdstp);
448 static void nwrap_files_endgrent(struct nwrap_backend *b);
450 /* prototypes for module backend */
452 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b);
453 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
454 struct passwd *pwdst, char *buf,
455 size_t buflen, struct passwd **pwdstp);
456 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
458 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
459 const char *name, struct passwd *pwdst,
460 char *buf, size_t buflen, struct passwd **pwdstp);
461 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
463 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
464 uid_t uid, struct passwd *pwdst,
465 char *buf, size_t buflen, struct passwd **pwdstp);
466 static void nwrap_module_setpwent(struct nwrap_backend *b);
467 static void nwrap_module_endpwent(struct nwrap_backend *b);
468 static struct group *nwrap_module_getgrent(struct nwrap_backend *b);
469 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
470 struct group *grdst, char *buf,
471 size_t buflen, struct group **grdstp);
472 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
474 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
475 const char *name, struct group *grdst,
476 char *buf, size_t buflen, struct group **grdstp);
477 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
479 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
480 gid_t gid, struct group *grdst,
481 char *buf, size_t buflen, struct group **grdstp);
482 static void nwrap_module_setgrent(struct nwrap_backend *b);
483 static void nwrap_module_endgrent(struct nwrap_backend *b);
484 static int nwrap_module_initgroups(struct nwrap_backend *b,
485 const char *user, gid_t group);
487 struct nwrap_ops nwrap_files_ops = {
488 .nw_getpwnam = nwrap_files_getpwnam,
489 .nw_getpwnam_r = nwrap_files_getpwnam_r,
490 .nw_getpwuid = nwrap_files_getpwuid,
491 .nw_getpwuid_r = nwrap_files_getpwuid_r,
492 .nw_setpwent = nwrap_files_setpwent,
493 .nw_getpwent = nwrap_files_getpwent,
494 .nw_getpwent_r = nwrap_files_getpwent_r,
495 .nw_endpwent = nwrap_files_endpwent,
496 .nw_initgroups = nwrap_files_initgroups,
497 .nw_getgrnam = nwrap_files_getgrnam,
498 .nw_getgrnam_r = nwrap_files_getgrnam_r,
499 .nw_getgrgid = nwrap_files_getgrgid,
500 .nw_getgrgid_r = nwrap_files_getgrgid_r,
501 .nw_setgrent = nwrap_files_setgrent,
502 .nw_getgrent = nwrap_files_getgrent,
503 .nw_getgrent_r = nwrap_files_getgrent_r,
504 .nw_endgrent = nwrap_files_endgrent,
507 struct nwrap_ops nwrap_module_ops = {
508 .nw_getpwnam = nwrap_module_getpwnam,
509 .nw_getpwnam_r = nwrap_module_getpwnam_r,
510 .nw_getpwuid = nwrap_module_getpwuid,
511 .nw_getpwuid_r = nwrap_module_getpwuid_r,
512 .nw_setpwent = nwrap_module_setpwent,
513 .nw_getpwent = nwrap_module_getpwent,
514 .nw_getpwent_r = nwrap_module_getpwent_r,
515 .nw_endpwent = nwrap_module_endpwent,
516 .nw_initgroups = nwrap_module_initgroups,
517 .nw_getgrnam = nwrap_module_getgrnam,
518 .nw_getgrnam_r = nwrap_module_getgrnam_r,
519 .nw_getgrgid = nwrap_module_getgrgid,
520 .nw_getgrgid_r = nwrap_module_getgrgid_r,
521 .nw_setgrent = nwrap_module_setgrent,
522 .nw_getgrent = nwrap_module_getgrent,
523 .nw_getgrent_r = nwrap_module_getgrent_r,
524 .nw_endgrent = nwrap_module_endgrent,
531 struct nwrap_libc_fns *fns;
536 struct nwrap_backend *backends;
537 struct nwrap_libc *libc;
540 static struct nwrap_main *nwrap_main_global;
541 static struct nwrap_main __nwrap_main_global;
546 static int nwrap_convert_he_ai(const struct hostent *he,
548 const struct addrinfo *hints,
549 struct addrinfo **pai,
550 bool skip_canonname);
556 #define DEFAULT_VECTOR_CAPACITY 16
558 struct nwrap_vector {
564 /* Macro returns pointer to first element of vector->items array.
566 * nwrap_vector is used as a memory backend which take care of
567 * memory allocations and other stuff like memory growing.
568 * nwrap_vectors should not be considered as some abstract structures.
569 * On this level, vectors are more handy than direct realloc/malloc
572 * nwrap_vector->items is array inside nwrap_vector which can be
573 * directly pointed by libc structure assembled by cwrap itself.
577 * 1) struct hostent contains char **h_addr_list element.
578 * 2) nwrap_vector holds array of pointers to addresses.
579 * It's easier to use vector to store results of
582 * Now, pretend that cwrap assembled struct hostent and
583 * we need to set h_addr_list to point to nwrap_vector.
584 * Idea behind is to shield users from internal nwrap_vector
586 * (Yes, not fully - array terminated by NULL is needed because
587 * it's result expected by libc function caller.)
593 * struct nwrap_vector *vector = malloc(sizeof(struct nwrap_vector));
594 * ... don't care about failed allocation now ...
596 * ... fill nwrap vector ...
599 * he.h_addr_list = nwrap_vector_head(vector);
602 #define nwrap_vector_head(vect) ((void *)((vect)->items))
604 #define nwrap_vector_foreach(item, vect, iter) \
605 for (iter = 0, (item) = (vect).items == NULL ? NULL : (vect).items[0]; \
607 (item) = (vect).items[++iter])
609 #define nwrap_vector_is_initialized(vector) ((vector)->items != NULL)
611 static inline bool nwrap_vector_init(struct nwrap_vector *const vector)
613 if (vector == NULL) {
617 /* count is initialized by ZERO_STRUCTP */
618 ZERO_STRUCTP(vector);
619 vector->items = malloc(sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
620 if (vector->items == NULL) {
623 vector->capacity = DEFAULT_VECTOR_CAPACITY;
624 memset(vector->items, '\0', sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
629 static bool nwrap_vector_add_item(struct nwrap_vector *vector, void *const item)
631 assert (vector != NULL);
633 if (vector->items == NULL) {
634 nwrap_vector_init(vector);
637 if (vector->count == vector->capacity) {
638 /* Items array _MUST_ be NULL terminated because it's passed
639 * as result to caller which expect NULL terminated array from libc.
641 void **items = realloc(vector->items, sizeof(void *) * ((vector->capacity * 2) + 1));
645 vector->items = items;
647 /* Don't count ending NULL to capacity */
648 vector->capacity *= 2;
651 vector->items[vector->count] = item;
654 vector->items[vector->count] = NULL;
659 static bool nwrap_vector_merge(struct nwrap_vector *dst,
660 struct nwrap_vector *src)
662 void **dst_items = NULL;
665 if (src->count == 0) {
669 count = dst->count + src->count;
671 /* We don't need reallocation if we have enough capacity. */
672 if (src->count > (dst->capacity - dst->count)) {
673 dst_items = (void **)realloc(dst->items, (count + 1) * sizeof(void *));
674 if (dst_items == NULL) {
677 dst->items = dst_items;
678 dst->capacity = count;
681 memcpy((void *)(((long *)dst->items) + dst->count),
683 src->count * sizeof(void *));
696 struct nwrap_vector lines;
698 bool (*parse_line)(struct nwrap_cache *, char *line);
699 void (*unload)(struct nwrap_cache *);
704 struct nwrap_cache *cache;
711 struct nwrap_cache __nwrap_cache_pw;
712 struct nwrap_pw nwrap_pw_global;
714 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
715 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
718 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
720 struct nwrap_cache *cache;
727 struct nwrap_cache __nwrap_cache_sp;
728 struct nwrap_sp nwrap_sp_global;
730 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line);
731 static void nwrap_sp_unload(struct nwrap_cache *nwrap);
732 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
736 struct nwrap_cache *cache;
743 struct nwrap_cache __nwrap_cache_gr;
744 struct nwrap_gr nwrap_gr_global;
747 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line);
748 static void nwrap_he_unload(struct nwrap_cache *nwrap);
750 struct nwrap_addrdata {
751 unsigned char host_addr[16]; /* IPv4 or IPv6 address */
754 static size_t max_hostents = 100;
756 struct nwrap_entdata {
757 struct nwrap_addrdata addr;
760 struct nwrap_vector nwrap_addrdata;
762 ssize_t aliases_count;
765 struct nwrap_entlist {
766 struct nwrap_entlist *next;
767 struct nwrap_entdata *ed;
771 struct nwrap_cache *cache;
773 struct nwrap_vector entries;
779 static struct nwrap_cache __nwrap_cache_he;
780 static struct nwrap_he nwrap_he_global;
783 /*********************************************************
785 *********************************************************/
787 static void nwrap_init(void);
788 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
789 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
790 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
792 /*********************************************************
793 * NWRAP LIBC LOADER FUNCTIONS
794 *********************************************************/
803 static const char *nwrap_str_lib(enum nwrap_lib lib)
810 case NWRAP_LIBSOCKET:
814 /* Compiler would warn us about unhandled enum value if we get here */
819 static void *nwrap_load_lib_handle(enum nwrap_lib lib)
821 int flags = RTLD_LAZY;
826 flags |= RTLD_DEEPBIND;
832 handle = nwrap_main_global->libc->nsl_handle;
833 if (handle == NULL) {
834 for (i = 10; i >= 0; i--) {
835 char soname[256] = {0};
837 snprintf(soname, sizeof(soname), "libnsl.so.%d", i);
838 handle = dlopen(soname, flags);
839 if (handle != NULL) {
844 nwrap_main_global->libc->nsl_handle = handle;
849 case NWRAP_LIBSOCKET:
850 #ifdef HAVE_LIBSOCKET
851 handle = nwrap_main_global->libc->sock_handle;
852 if (handle == NULL) {
853 for (i = 10; i >= 0; i--) {
854 char soname[256] = {0};
856 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
857 handle = dlopen(soname, flags);
858 if (handle != NULL) {
863 nwrap_main_global->libc->sock_handle = handle;
869 handle = nwrap_main_global->libc->handle;
870 if (handle == NULL) {
871 for (i = 10; i >= 0; i--) {
872 char soname[256] = {0};
874 snprintf(soname, sizeof(soname), "libc.so.%d", i);
875 handle = dlopen(soname, flags);
876 if (handle != NULL) {
881 nwrap_main_global->libc->handle = handle;
886 if (handle == NULL) {
888 handle = nwrap_main_global->libc->handle
889 = nwrap_main_global->libc->sock_handle
890 = nwrap_main_global->libc->nsl_handle
893 NWRAP_LOG(NWRAP_LOG_ERROR,
894 "Failed to dlopen library: %s\n",
903 static void *_nwrap_load_lib_function(enum nwrap_lib lib, const char *fn_name)
910 handle = nwrap_load_lib_handle(lib);
912 func = dlsym(handle, fn_name);
914 NWRAP_LOG(NWRAP_LOG_ERROR,
915 "Failed to find %s: %s\n",
920 NWRAP_LOG(NWRAP_LOG_TRACE,
922 fn_name, nwrap_str_lib(lib));
926 #define nwrap_load_lib_function(lib, fn_name) \
927 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
928 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
929 _nwrap_load_lib_function(lib, #fn_name); \
932 /* INTERNAL HELPER FUNCTIONS */
933 static void nwrap_lines_unload(struct nwrap_cache *const nwrap)
937 nwrap_vector_foreach(item, nwrap->lines, p) {
938 /* Maybe some vectors were merged ... */
941 SAFE_FREE(nwrap->lines.items);
942 ZERO_STRUCTP(&nwrap->lines);
948 * Functions expeciall from libc need to be loaded individually, you can't load
949 * all at once or gdb will segfault at startup. The same applies to valgrind and
950 * has probably something todo with with the linker.
951 * So we need load each function at the point it is called the first time.
953 static struct passwd *libc_getpwnam(const char *name)
955 nwrap_load_lib_function(NWRAP_LIBC, getpwnam);
957 return nwrap_main_global->libc->fns->_libc_getpwnam(name);
960 #ifdef HAVE_GETPWNAM_R
961 static int libc_getpwnam_r(const char *name,
965 struct passwd **result)
967 #ifdef HAVE___POSIX_GETPWNAM_R
968 if (nwrap_main_global->libc->fns->_libc_getpwnam_r == NULL) {
969 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwnam_r) =
970 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwnam_r");
973 nwrap_load_lib_function(NWRAP_LIBC, getpwnam_r);
976 return nwrap_main_global->libc->fns->_libc_getpwnam_r(name,
984 static struct passwd *libc_getpwuid(uid_t uid)
986 nwrap_load_lib_function(NWRAP_LIBC, getpwuid);
988 return nwrap_main_global->libc->fns->_libc_getpwuid(uid);
991 #ifdef HAVE_GETPWUID_R
992 static int libc_getpwuid_r(uid_t uid,
996 struct passwd **result)
998 #ifdef HAVE___POSIX_GETPWUID_R
999 if (nwrap_main_global->libc->fns->_libc_getpwuid_r == NULL) {
1000 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwuid_r) =
1001 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwuid_r");
1004 nwrap_load_lib_function(NWRAP_LIBC, getpwuid_r);
1007 return nwrap_main_global->libc->fns->_libc_getpwuid_r(uid,
1015 static inline void str_tolower(char *dst, char *src)
1017 register char *src_tmp = src;
1018 register char *dst_tmp = dst;
1020 while (*src_tmp != '\0') {
1021 *dst_tmp = tolower(*src_tmp);
1027 static bool str_tolower_copy(char **dst_name, const char *const src_name)
1031 if ((dst_name == NULL) || (src_name == NULL)) {
1035 h_name_lower = strdup(src_name);
1036 if (h_name_lower == NULL) {
1037 NWRAP_LOG(NWRAP_LOG_DEBUG, "Out of memory while strdup");
1041 str_tolower(h_name_lower, h_name_lower);
1042 *dst_name = h_name_lower;
1046 static void libc_setpwent(void)
1048 nwrap_load_lib_function(NWRAP_LIBC, setpwent);
1050 nwrap_main_global->libc->fns->_libc_setpwent();
1053 static struct passwd *libc_getpwent(void)
1055 nwrap_load_lib_function(NWRAP_LIBC, getpwent);
1057 return nwrap_main_global->libc->fns->_libc_getpwent();
1060 #ifdef HAVE_SOLARIS_GETPWENT_R
1061 static struct passwd *libc_getpwent_r(struct passwd *pwdst,
1065 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1067 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1071 #else /* HAVE_SOLARIS_GETPWENT_R */
1072 static int libc_getpwent_r(struct passwd *pwdst,
1075 struct passwd **pwdstp)
1077 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1079 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1084 #endif /* HAVE_SOLARIS_GETPWENT_R */
1086 static void libc_endpwent(void)
1088 nwrap_load_lib_function(NWRAP_LIBC, endpwent);
1090 nwrap_main_global->libc->fns->_libc_endpwent();
1093 static int libc_initgroups(const char *user, gid_t gid)
1095 nwrap_load_lib_function(NWRAP_LIBC, initgroups);
1097 return nwrap_main_global->libc->fns->_libc_initgroups(user, gid);
1100 static struct group *libc_getgrnam(const char *name)
1102 nwrap_load_lib_function(NWRAP_LIBC, getgrnam);
1104 return nwrap_main_global->libc->fns->_libc_getgrnam(name);
1107 #ifdef HAVE_GETGRNAM_R
1108 static int libc_getgrnam_r(const char *name,
1112 struct group **result)
1114 #ifdef HAVE___POSIX_GETGRNAM_R
1115 if (nwrap_main_global->libc->fns->_libc_getgrnam_r == NULL) {
1116 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrnam_r) =
1117 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrnam_r");
1120 nwrap_load_lib_function(NWRAP_LIBC, getgrnam_r);
1123 return nwrap_main_global->libc->fns->_libc_getgrnam_r(name,
1131 static struct group *libc_getgrgid(gid_t gid)
1133 nwrap_load_lib_function(NWRAP_LIBC, getgrgid);
1135 return nwrap_main_global->libc->fns->_libc_getgrgid(gid);
1138 #ifdef HAVE_GETGRGID_R
1139 static int libc_getgrgid_r(gid_t gid,
1143 struct group **result)
1145 #ifdef HAVE___POSIX_GETGRGID_R
1146 if (nwrap_main_global->libc->fns->_libc_getgrgid_r == NULL) {
1147 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrgid_r) =
1148 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrgid_r");
1151 nwrap_load_lib_function(NWRAP_LIBC, getgrgid_r);
1154 return nwrap_main_global->libc->fns->_libc_getgrgid_r(gid,
1162 static void libc_setgrent(void)
1164 nwrap_load_lib_function(NWRAP_LIBC, setgrent);
1166 nwrap_main_global->libc->fns->_libc_setgrent();
1169 static struct group *libc_getgrent(void)
1171 nwrap_load_lib_function(NWRAP_LIBC, getgrent);
1173 return nwrap_main_global->libc->fns->_libc_getgrent();
1176 #ifdef HAVE_GETGRENT_R
1177 #ifdef HAVE_SOLARIS_GETGRENT_R
1178 static struct group *libc_getgrent_r(struct group *group,
1182 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1184 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1188 #else /* !HAVE_SOLARIS_GETGRENT_R */
1189 static int libc_getgrent_r(struct group *group,
1192 struct group **result)
1194 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1196 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1201 #endif /* HAVE_SOLARIS_GETGRENT_R */
1202 #endif /* HAVE_GETGRENT_R */
1204 static void libc_endgrent(void)
1206 nwrap_load_lib_function(NWRAP_LIBC, endgrent);
1208 nwrap_main_global->libc->fns->_libc_endgrent();
1211 #ifdef HAVE_GETGROUPLIST
1212 static int libc_getgrouplist(const char *user,
1217 nwrap_load_lib_function(NWRAP_LIBC, getgrouplist);
1219 return nwrap_main_global->libc->fns->_libc_getgrouplist(user,
1226 static void libc_sethostent(int stayopen)
1228 nwrap_load_lib_function(NWRAP_LIBNSL, sethostent);
1230 nwrap_main_global->libc->fns->_libc_sethostent(stayopen);
1233 static struct hostent *libc_gethostent(void)
1235 nwrap_load_lib_function(NWRAP_LIBNSL, gethostent);
1237 return nwrap_main_global->libc->fns->_libc_gethostent();
1240 static void libc_endhostent(void)
1242 nwrap_load_lib_function(NWRAP_LIBNSL, endhostent);
1244 nwrap_main_global->libc->fns->_libc_endhostent();
1247 static struct hostent *libc_gethostbyname(const char *name)
1249 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname);
1251 return nwrap_main_global->libc->fns->_libc_gethostbyname(name);
1254 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
1255 static struct hostent *libc_gethostbyname2(const char *name, int af)
1257 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname2);
1259 return nwrap_main_global->libc->fns->_libc_gethostbyname2(name, af);
1263 static struct hostent *libc_gethostbyaddr(const void *addr,
1267 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr);
1269 return nwrap_main_global->libc->fns->_libc_gethostbyaddr(addr,
1274 static int libc_gethostname(char *name, size_t len)
1276 nwrap_load_lib_function(NWRAP_LIBNSL, gethostname);
1278 return nwrap_main_global->libc->fns->_libc_gethostname(name, len);
1281 #ifdef HAVE_GETHOSTBYNAME_R
1282 static int libc_gethostbyname_r(const char *name,
1283 struct hostent *ret,
1286 struct hostent **result,
1289 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname_r);
1291 return nwrap_main_global->libc->fns->_libc_gethostbyname_r(name,
1300 #ifdef HAVE_GETHOSTBYADDR_R
1301 static int libc_gethostbyaddr_r(const void *addr,
1304 struct hostent *ret,
1307 struct hostent **result,
1310 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr_r);
1312 return nwrap_main_global->libc->fns->_libc_gethostbyaddr_r(addr,
1323 static int libc_getaddrinfo(const char *node,
1324 const char *service,
1325 const struct addrinfo *hints,
1326 struct addrinfo **res)
1328 nwrap_load_lib_function(NWRAP_LIBSOCKET, getaddrinfo);
1330 return nwrap_main_global->libc->fns->_libc_getaddrinfo(node,
1336 static int libc_getnameinfo(const struct sockaddr *sa,
1344 nwrap_load_lib_function(NWRAP_LIBSOCKET, getnameinfo);
1346 return nwrap_main_global->libc->fns->_libc_getnameinfo(sa,
1355 /*********************************************************
1356 * NWRAP NSS MODULE LOADER FUNCTIONS
1357 *********************************************************/
1359 static void *nwrap_load_module_fn(struct nwrap_backend *b,
1360 const char *fn_name)
1365 if (!b->so_handle) {
1366 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1370 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
1371 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1375 res = dlsym(b->so_handle, s);
1377 NWRAP_LOG(NWRAP_LOG_ERROR,
1378 "Cannot find function %s in %s",
1385 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
1387 struct nwrap_module_nss_fns *fns;
1389 if (!b->so_handle) {
1393 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
1398 *(void **)(&fns->_nss_getpwnam_r) =
1399 nwrap_load_module_fn(b, "getpwnam_r");
1400 *(void **)(&fns->_nss_getpwuid_r) =
1401 nwrap_load_module_fn(b, "getpwuid_r");
1402 *(void **)(&fns->_nss_setpwent) =
1403 nwrap_load_module_fn(b, "setpwent");
1404 *(void **)(&fns->_nss_getpwent_r) =
1405 nwrap_load_module_fn(b, "getpwent_r");
1406 *(void **)(&fns->_nss_endpwent) =
1407 nwrap_load_module_fn(b, "endpwent");
1408 *(void **)(&fns->_nss_initgroups) =
1409 nwrap_load_module_fn(b, "initgroups_dyn");
1410 *(void **)(&fns->_nss_getgrnam_r) =
1411 nwrap_load_module_fn(b, "getgrnam_r");
1412 *(void **)(&fns->_nss_getgrgid_r)=
1413 nwrap_load_module_fn(b, "getgrgid_r");
1414 *(void **)(&fns->_nss_setgrent) =
1415 nwrap_load_module_fn(b, "setgrent");
1416 *(void **)(&fns->_nss_getgrent_r) =
1417 nwrap_load_module_fn(b, "getgrent_r");
1418 *(void **)(&fns->_nss_endgrent) =
1419 nwrap_load_module_fn(b, "endgrent");
1424 static void *nwrap_load_module(const char *so_path)
1428 if (!so_path || !strlen(so_path)) {
1432 h = dlopen(so_path, RTLD_LAZY);
1434 NWRAP_LOG(NWRAP_LOG_ERROR,
1435 "Cannot open shared library %s",
1443 static bool nwrap_module_init(const char *name,
1444 struct nwrap_ops *ops,
1445 const char *so_path,
1447 struct nwrap_backend **backends)
1449 struct nwrap_backend *b;
1451 *backends = (struct nwrap_backend *)realloc(*backends,
1452 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
1454 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1458 b = &((*backends)[*num_backends]);
1462 b->so_path = so_path;
1464 if (so_path != NULL) {
1465 b->so_handle = nwrap_load_module(so_path);
1466 b->fns = nwrap_load_module_fns(b);
1467 if (b->fns == NULL) {
1471 b->so_handle = NULL;
1480 static void nwrap_libc_init(struct nwrap_main *r)
1482 r->libc = malloc(sizeof(struct nwrap_libc));
1483 if (r->libc == NULL) {
1484 printf("Failed to allocate memory for libc");
1487 ZERO_STRUCTP(r->libc);
1489 r->libc->fns = malloc(sizeof(struct nwrap_libc_fns));
1490 if (r->libc->fns == NULL) {
1491 printf("Failed to allocate memory for libc functions");
1494 ZERO_STRUCTP(r->libc->fns);
1497 static void nwrap_backend_init(struct nwrap_main *r)
1499 const char *module_so_path = getenv("NSS_WRAPPER_MODULE_SO_PATH");
1500 const char *module_fn_name = getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1502 r->num_backends = 0;
1505 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1508 NWRAP_LOG(NWRAP_LOG_ERROR,
1509 "Failed to initialize 'files' backend");
1513 if (module_so_path != NULL &&
1514 module_so_path[0] != '\0' &&
1515 module_fn_name != NULL &&
1516 module_fn_name[0] != '\0') {
1517 if (!nwrap_module_init(module_fn_name,
1522 NWRAP_LOG(NWRAP_LOG_ERROR,
1523 "Failed to initialize '%s' backend",
1530 static void nwrap_init(void)
1534 size_t max_hostents_tmp;
1536 NWRAP_LOCK(nwrap_initialized);
1537 if (nwrap_initialized) {
1538 NWRAP_UNLOCK(nwrap_initialized);
1543 * Still holding nwrap_initialized lock here.
1544 * We don't use NWRAP_(UN)LOCK_ALL macros here because we
1545 * want to avoid overhead when other threads do their job.
1547 NWRAP_LOCK(nwrap_global);
1548 NWRAP_LOCK(nwrap_gr_global);
1549 NWRAP_LOCK(nwrap_he_global);
1550 NWRAP_LOCK(nwrap_pw_global);
1551 NWRAP_LOCK(nwrap_sp_global);
1553 nwrap_initialized = true;
1555 /* Initialize pthread_atfork handlers */
1556 pthread_atfork(&nwrap_thread_prepare, &nwrap_thread_parent,
1557 &nwrap_thread_child);
1559 env = getenv("NSS_WRAPPER_MAX_HOSTENTS");
1561 max_hostents_tmp = (size_t)strtol(env, &endptr, 10);
1562 if (((env != '\0') && (endptr == '\0')) ||
1563 (max_hostents_tmp == 0)) {
1564 NWRAP_LOG(NWRAP_LOG_DEBUG,
1565 "Error parsing NSS_WRAPPER_MAX_HOSTENTS "
1566 "value or value is too small. "
1567 "Using default value: %lu.",
1568 (unsigned long)max_hostents);
1570 max_hostents = max_hostents_tmp;
1573 /* Initialize hash table */
1574 NWRAP_LOG(NWRAP_LOG_DEBUG,
1575 "Initializing hash table of size %lu items.",
1576 (unsigned long)max_hostents);
1577 if (hcreate(max_hostents) == 0) {
1578 NWRAP_LOG(NWRAP_LOG_ERROR,
1579 "Failed to initialize hash table");
1583 nwrap_main_global = &__nwrap_main_global;
1585 nwrap_libc_init(nwrap_main_global);
1587 nwrap_backend_init(nwrap_main_global);
1590 nwrap_pw_global.cache = &__nwrap_cache_pw;
1592 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
1593 nwrap_pw_global.cache->fp = NULL;
1594 nwrap_pw_global.cache->fd = -1;
1595 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
1596 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
1597 nwrap_pw_global.cache->unload = nwrap_pw_unload;
1600 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1601 nwrap_sp_global.cache = &__nwrap_cache_sp;
1603 nwrap_sp_global.cache->path = getenv("NSS_WRAPPER_SHADOW");
1604 nwrap_sp_global.cache->fp = NULL;
1605 nwrap_sp_global.cache->fd = -1;
1606 nwrap_sp_global.cache->private_data = &nwrap_sp_global;
1607 nwrap_sp_global.cache->parse_line = nwrap_sp_parse_line;
1608 nwrap_sp_global.cache->unload = nwrap_sp_unload;
1609 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1612 nwrap_gr_global.cache = &__nwrap_cache_gr;
1614 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
1615 nwrap_gr_global.cache->fp = NULL;
1616 nwrap_gr_global.cache->fd = -1;
1617 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
1618 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
1619 nwrap_gr_global.cache->unload = nwrap_gr_unload;
1622 nwrap_he_global.cache = &__nwrap_cache_he;
1624 nwrap_he_global.cache->path = getenv("NSS_WRAPPER_HOSTS");
1625 nwrap_he_global.cache->fp = NULL;
1626 nwrap_he_global.cache->fd = -1;
1627 nwrap_he_global.cache->private_data = &nwrap_he_global;
1628 nwrap_he_global.cache->parse_line = nwrap_he_parse_line;
1629 nwrap_he_global.cache->unload = nwrap_he_unload;
1632 /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1636 bool nss_wrapper_enabled(void)
1640 if (nwrap_pw_global.cache->path == NULL ||
1641 nwrap_pw_global.cache->path[0] == '\0') {
1644 if (nwrap_gr_global.cache->path == NULL ||
1645 nwrap_gr_global.cache->path[0] == '\0') {
1652 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1653 bool nss_wrapper_shadow_enabled(void)
1657 if (nwrap_sp_global.cache->path == NULL ||
1658 nwrap_sp_global.cache->path[0] == '\0') {
1664 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1666 bool nss_wrapper_hosts_enabled(void)
1670 if (nwrap_he_global.cache->path == NULL ||
1671 nwrap_he_global.cache->path[0] == '\0') {
1678 static bool nwrap_hostname_enabled(void)
1682 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
1689 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
1693 /* Unused but getline needs it */
1697 if (nwrap->st.st_size == 0) {
1698 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
1702 /* Support for 32-bit system I guess */
1703 if (nwrap->st.st_size > INT32_MAX) {
1704 NWRAP_LOG(NWRAP_LOG_ERROR,
1705 "Size[%u] larger than INT32_MAX",
1706 (unsigned)nwrap->st.st_size);
1713 n = getline(&line, &len, nwrap->fp);
1716 if (feof(nwrap->fp)) {
1720 NWRAP_LOG(NWRAP_LOG_ERROR,
1721 "Unable to read line from file: %s",
1726 if (line[n - 1] == '\n') {
1730 if (line[0] == '\0') {
1735 ok = nwrap->parse_line(nwrap, line);
1737 NWRAP_LOG(NWRAP_LOG_ERROR,
1738 "Unable to parse line file: %s",
1744 /* Line is parsed without issues so add it to list */
1745 ok = nwrap_vector_add_item(&(nwrap->lines), (void *const) line);
1747 NWRAP_LOG(NWRAP_LOG_ERROR,
1748 "Unable to add line to vector");
1752 /* This forces getline to allocate new memory for line. */
1754 } while (!feof(nwrap->fp));
1759 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
1761 nwrap->unload(nwrap);
1763 nwrap_lines_unload(nwrap);
1766 static bool nwrap_files_cache_reload(struct nwrap_cache *nwrap)
1771 bool retried = false;
1773 assert(nwrap != NULL);
1776 if (nwrap->fd < 0) {
1777 nwrap->fp = fopen(nwrap->path, "re");
1778 if (nwrap->fp == NULL) {
1780 NWRAP_LOG(NWRAP_LOG_ERROR,
1781 "Unable to open '%s' readonly %d:%s",
1782 nwrap->path, nwrap->fd,
1787 nwrap->fd = fileno(nwrap->fp);
1788 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
1791 ret = fstat(nwrap->fd, &st);
1793 NWRAP_LOG(NWRAP_LOG_ERROR,
1794 "fstat(%s) - %d:%s",
1804 if (retried == false && st.st_nlink == 0) {
1805 /* maybe someone has replaced the file... */
1806 NWRAP_LOG(NWRAP_LOG_TRACE,
1807 "st_nlink == 0, reopen %s",
1810 memset(&nwrap->st, 0, sizeof(nwrap->st));
1817 if (st.st_mtime == nwrap->st.st_mtime) {
1818 NWRAP_LOG(NWRAP_LOG_TRACE,
1819 "st_mtime[%u] hasn't changed, skip reload",
1820 (unsigned)st.st_mtime);
1824 NWRAP_LOG(NWRAP_LOG_TRACE,
1825 "st_mtime has changed [%u] => [%u], start reload",
1826 (unsigned)st.st_mtime,
1827 (unsigned)nwrap->st.st_mtime);
1831 nwrap_files_cache_unload(nwrap);
1833 ok = nwrap_parse_file(nwrap);
1835 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
1836 nwrap_files_cache_unload(nwrap);
1840 NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
1845 * the caller has to call nwrap_unload() on failure
1847 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
1849 struct nwrap_pw *nwrap_pw;
1856 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1858 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
1859 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
1861 NWRAP_LOG(NWRAP_LOG_ERROR,
1862 "realloc(%u) failed",
1863 (unsigned)list_size);
1866 nwrap_pw->list = pw;
1868 pw = &nwrap_pw->list[nwrap_pw->num];
1875 NWRAP_LOG(NWRAP_LOG_ERROR,
1876 "Invalid line[%s]: '%s'",
1886 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
1891 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1899 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
1904 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1910 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
1912 NWRAP_LOG(NWRAP_LOG_ERROR,
1913 "Invalid line[%s]: '%s' - %s",
1914 line, c, strerror(errno));
1918 NWRAP_LOG(NWRAP_LOG_ERROR,
1919 "Invalid line[%s]: '%s' - %s",
1920 line, c, strerror(errno));
1924 NWRAP_LOG(NWRAP_LOG_ERROR,
1925 "Invalid line[%s]: '%s' - %s",
1926 line, c, strerror(errno));
1931 NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
1936 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1942 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
1944 NWRAP_LOG(NWRAP_LOG_ERROR,
1945 "Invalid line[%s]: '%s' - %s",
1946 line, c, strerror(errno));
1950 NWRAP_LOG(NWRAP_LOG_ERROR,
1951 "Invalid line[%s]: '%s' - %s",
1952 line, c, strerror(errno));
1956 NWRAP_LOG(NWRAP_LOG_ERROR,
1957 "Invalid line[%s]: '%s' - %s",
1958 line, c, strerror(errno));
1963 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]\n", pw->pw_gid);
1968 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
1976 NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
1981 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
1989 NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
1993 NWRAP_LOG(NWRAP_LOG_TRACE, "shell[%s]", pw->pw_shell);
1995 NWRAP_LOG(NWRAP_LOG_DEBUG,
1996 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1997 pw->pw_name, pw->pw_passwd,
1998 pw->pw_uid, pw->pw_gid,
1999 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
2005 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
2007 struct nwrap_pw *nwrap_pw;
2008 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
2010 SAFE_FREE(nwrap_pw->list);
2015 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
2016 char *buf, size_t buflen, struct passwd **dstp)
2022 first = src->pw_name;
2024 last = src->pw_shell;
2025 while (*last) last++;
2027 ofs = PTR_DIFF(last + 1, first);
2029 if (ofs > (off_t) buflen) {
2033 memcpy(buf, first, ofs);
2035 ofs = PTR_DIFF(src->pw_name, first);
2036 dst->pw_name = buf + ofs;
2037 ofs = PTR_DIFF(src->pw_passwd, first);
2038 dst->pw_passwd = buf + ofs;
2039 dst->pw_uid = src->pw_uid;
2040 dst->pw_gid = src->pw_gid;
2041 ofs = PTR_DIFF(src->pw_gecos, first);
2042 dst->pw_gecos = buf + ofs;
2043 ofs = PTR_DIFF(src->pw_dir, first);
2044 dst->pw_dir = buf + ofs;
2045 ofs = PTR_DIFF(src->pw_shell, first);
2046 dst->pw_shell = buf + ofs;
2055 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2056 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line)
2058 struct nwrap_sp *nwrap_sp;
2065 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2067 list_size = sizeof(*nwrap_sp->list) * (nwrap_sp->num+1);
2068 sp = (struct spwd *)realloc(nwrap_sp->list, list_size);
2070 NWRAP_LOG(NWRAP_LOG_ERROR,
2071 "realloc(%u) failed",
2072 (unsigned)list_size);
2075 nwrap_sp->list = sp;
2077 sp = &nwrap_sp->list[nwrap_sp->num];
2084 NWRAP_LOG(NWRAP_LOG_ERROR,
2085 "name -- Invalid line[%s]: '%s'",
2095 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", sp->sp_namp);
2100 NWRAP_LOG(NWRAP_LOG_ERROR,
2101 "pwd -- Invalid line[%s]: '%s'",
2118 NWRAP_LOG(NWRAP_LOG_ERROR,
2119 "lstchg -- Invalid line[%s]: '%s'",
2126 sp->sp_lstchg = strtol(c, &e, 10);
2128 NWRAP_LOG(NWRAP_LOG_ERROR,
2129 "lstchg -- Invalid line[%s]: '%s' - %s",
2130 line, c, strerror(errno));
2134 NWRAP_LOG(NWRAP_LOG_ERROR,
2135 "lstchg -- Invalid line[%s]: '%s' - %s",
2136 line, c, strerror(errno));
2140 NWRAP_LOG(NWRAP_LOG_ERROR,
2141 "lstchg -- Invalid line[%s]: '%s' - %s",
2142 line, c, strerror(errno));
2155 NWRAP_LOG(NWRAP_LOG_ERROR,
2156 "min -- Invalid line[%s]: '%s'",
2163 sp->sp_min = strtol(c, &e, 10);
2165 NWRAP_LOG(NWRAP_LOG_ERROR,
2166 "min -- Invalid line[%s]: '%s' - %s",
2167 line, c, strerror(errno));
2171 NWRAP_LOG(NWRAP_LOG_ERROR,
2172 "min -- Invalid line[%s]: '%s' - %s",
2173 line, c, strerror(errno));
2177 NWRAP_LOG(NWRAP_LOG_ERROR,
2178 "min -- Invalid line[%s]: '%s' - %s",
2179 line, c, strerror(errno));
2192 NWRAP_LOG(NWRAP_LOG_ERROR,
2193 "max -- Invalid line[%s]: '%s'",
2200 sp->sp_max = strtol(c, &e, 10);
2202 NWRAP_LOG(NWRAP_LOG_ERROR,
2203 "max -- Invalid line[%s]: '%s' - %s",
2204 line, c, strerror(errno));
2208 NWRAP_LOG(NWRAP_LOG_ERROR,
2209 "max -- Invalid line[%s]: '%s' - %s",
2210 line, c, strerror(errno));
2214 NWRAP_LOG(NWRAP_LOG_ERROR,
2215 "max -- Invalid line[%s]: '%s' - %s",
2216 line, c, strerror(errno));
2229 NWRAP_LOG(NWRAP_LOG_ERROR,
2230 "warn -- Invalid line[%s]: '%s'",
2237 sp->sp_warn = strtol(c, &e, 10);
2239 NWRAP_LOG(NWRAP_LOG_ERROR,
2240 "warn -- Invalid line[%s]: '%s' - %s",
2241 line, c, strerror(errno));
2245 NWRAP_LOG(NWRAP_LOG_ERROR,
2246 "warn -- Invalid line[%s]: '%s' - %s",
2247 line, c, strerror(errno));
2251 NWRAP_LOG(NWRAP_LOG_ERROR,
2252 "warn -- Invalid line[%s]: '%s' - %s",
2253 line, c, strerror(errno));
2266 NWRAP_LOG(NWRAP_LOG_ERROR,
2267 "inact -- Invalid line[%s]: '%s'",
2274 sp->sp_inact = strtol(c, &e, 10);
2276 NWRAP_LOG(NWRAP_LOG_ERROR,
2277 "inact -- Invalid line[%s]: '%s' - %s",
2278 line, c, strerror(errno));
2282 NWRAP_LOG(NWRAP_LOG_ERROR,
2283 "inact -- Invalid line[%s]: '%s' - %s",
2284 line, c, strerror(errno));
2288 NWRAP_LOG(NWRAP_LOG_ERROR,
2289 "inact -- Invalid line[%s]: '%s' - %s",
2290 line, c, strerror(errno));
2303 NWRAP_LOG(NWRAP_LOG_ERROR,
2304 "expire -- Invalid line[%s]: '%s'",
2311 sp->sp_expire = strtol(c, &e, 10);
2313 NWRAP_LOG(NWRAP_LOG_ERROR,
2314 "expire -- Invalid line[%s]: '%s' - %s",
2315 line, c, strerror(errno));
2319 NWRAP_LOG(NWRAP_LOG_ERROR,
2320 "expire -- Invalid line[%s]: '%s' - %s",
2321 line, c, strerror(errno));
2325 NWRAP_LOG(NWRAP_LOG_ERROR,
2326 "expire -- Invalid line[%s]: '%s' - %s",
2327 line, c, strerror(errno));
2337 static void nwrap_sp_unload(struct nwrap_cache *nwrap)
2339 struct nwrap_sp *nwrap_sp;
2340 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2342 SAFE_FREE(nwrap_sp->list);
2346 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2349 * the caller has to call nwrap_unload() on failure
2351 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
2353 struct nwrap_gr *nwrap_gr;
2361 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2363 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
2364 gr = (struct group *)realloc(nwrap_gr->list, list_size);
2366 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
2369 nwrap_gr->list = gr;
2371 gr = &nwrap_gr->list[nwrap_gr->num];
2378 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2386 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
2391 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2399 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
2404 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2410 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
2412 NWRAP_LOG(NWRAP_LOG_ERROR,
2413 "Invalid line[%s]: '%s' - %s",
2414 line, c, strerror(errno));
2418 NWRAP_LOG(NWRAP_LOG_ERROR,
2419 "Invalid line[%s]: '%s' - %s",
2420 line, c, strerror(errno));
2424 NWRAP_LOG(NWRAP_LOG_ERROR,
2425 "Invalid line[%s]: '%s' - %s",
2426 line, c, strerror(errno));
2431 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
2434 gr->gr_mem = (char **)malloc(sizeof(char *));
2436 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
2439 gr->gr_mem[0] = NULL;
2441 for(nummem=0; p; nummem++) {
2451 if (strlen(c) == 0) {
2455 m_size = sizeof(char *) * (nummem+2);
2456 m = (char **)realloc(gr->gr_mem, m_size);
2458 NWRAP_LOG(NWRAP_LOG_ERROR,
2459 "realloc(%zd) failed",
2464 gr->gr_mem[nummem] = c;
2465 gr->gr_mem[nummem+1] = NULL;
2467 NWRAP_LOG(NWRAP_LOG_TRACE,
2469 nummem, gr->gr_mem[nummem]);
2472 NWRAP_LOG(NWRAP_LOG_DEBUG,
2473 "Added group[%s:%s:%u:] with %u members",
2474 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem);
2480 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
2483 struct nwrap_gr *nwrap_gr;
2484 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2486 if (nwrap_gr->list) {
2487 for (i=0; i < nwrap_gr->num; i++) {
2488 SAFE_FREE(nwrap_gr->list[i].gr_mem);
2490 SAFE_FREE(nwrap_gr->list);
2497 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
2498 char *buf, size_t buflen, struct group **dstp)
2508 first = src->gr_name;
2510 lastm = src->gr_mem;
2517 last = src->gr_passwd;
2519 while (*last) last++;
2521 ofsb = PTR_DIFF(last + 1, first);
2522 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
2524 if ((ofsb + ofsm) > (off_t) buflen) {
2528 memcpy(buf, first, ofsb);
2529 memcpy(buf + ofsb, src->gr_mem, ofsm);
2531 ofs = PTR_DIFF(src->gr_name, first);
2532 dst->gr_name = buf + ofs;
2533 ofs = PTR_DIFF(src->gr_passwd, first);
2534 dst->gr_passwd = buf + ofs;
2535 dst->gr_gid = src->gr_gid;
2537 dst->gr_mem = (char **)(buf + ofsb);
2538 for (i=0; src->gr_mem[i]; i++) {
2539 ofs = PTR_DIFF(src->gr_mem[i], first);
2540 dst->gr_mem[i] = buf + ofs;
2550 static struct nwrap_entlist *nwrap_entlist_init(struct nwrap_entdata *ed)
2552 struct nwrap_entlist *el;
2555 NWRAP_LOG(NWRAP_LOG_ERROR,
2556 "entry is NULL, can't create list item");
2560 el = (struct nwrap_entlist *)malloc(sizeof(struct nwrap_entlist));
2562 NWRAP_LOG(NWRAP_LOG_ERROR, "malloc failed");
2572 static bool nwrap_ed_inventarize_add_new(char *const h_name,
2573 struct nwrap_entdata *const ed)
2577 struct nwrap_entlist *el;
2579 if (h_name == NULL) {
2580 NWRAP_LOG(NWRAP_LOG_ERROR, "h_name NULL - can't add");
2584 el = nwrap_entlist_init(ed);
2590 e.data = (void *)el;
2592 p = hsearch(e, ENTER);
2594 NWRAP_LOG(NWRAP_LOG_ERROR, "Hash table is full!");
2601 static bool nwrap_ed_inventarize_add_to_existing(struct nwrap_entdata *const ed,
2602 struct nwrap_entlist *const el)
2604 struct nwrap_entlist *cursor;
2605 struct nwrap_entlist *el_new;
2608 NWRAP_LOG(NWRAP_LOG_ERROR, "list is NULL, can not add");
2613 for (cursor = el; cursor->next != NULL; cursor = cursor->next)
2615 if (cursor->ed == ed) {
2620 if (cursor->ed == ed) {
2624 el_new = nwrap_entlist_init(ed);
2625 if (el_new == NULL) {
2629 cursor->next = el_new;
2633 static bool nwrap_ed_inventarize(char *const name,
2634 struct nwrap_entdata *const ed)
2643 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
2645 p = hsearch(e, FIND);
2647 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", name);
2648 ok = nwrap_ed_inventarize_add_new(name, ed);
2650 struct nwrap_entlist *el = (struct nwrap_entlist *)p->data;
2652 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s found. Add record to list.", name);
2653 ok = nwrap_ed_inventarize_add_to_existing(ed, el);
2659 static bool nwrap_add_hname(struct nwrap_entdata *const ed)
2661 char *const h_name = (char *const)(ed->ht.h_name);
2665 ok = nwrap_ed_inventarize(h_name, ed);
2670 if (ed->ht.h_aliases == NULL) {
2674 /* Itemize aliases */
2675 for (i = 0; ed->ht.h_aliases[i] != NULL; ++i) {
2678 h_name_alias = ed->ht.h_aliases[i];
2680 NWRAP_LOG(NWRAP_LOG_DEBUG, "Add alias: %s", h_name_alias);
2682 if (!nwrap_ed_inventarize(h_name_alias, ed)) {
2683 NWRAP_LOG(NWRAP_LOG_ERROR,
2684 "Unable to add alias: %s", h_name_alias);
2692 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line)
2694 struct nwrap_he *nwrap_he = (struct nwrap_he *)nwrap->private_data;
2695 bool do_aliases = true;
2696 ssize_t aliases_count = 0;
2704 struct nwrap_entdata *ed = (struct nwrap_entdata *)
2705 malloc(sizeof(struct nwrap_entdata));
2707 NWRAP_LOG(NWRAP_LOG_ERROR,
2708 "Unable to allocate memory for nwrap_entdata");
2719 /* Walk to first char */
2720 for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
2722 NWRAP_LOG(NWRAP_LOG_ERROR,
2723 "Invalid line[%s]: '%s'",
2730 for (i = p; !isspace((int)*p); p++) {
2732 NWRAP_LOG(NWRAP_LOG_ERROR,
2733 "Invalid line[%s]: '%s'",
2742 if (inet_pton(AF_INET, i, ed->addr.host_addr)) {
2743 ed->ht.h_addrtype = AF_INET;
2744 ed->ht.h_length = 4;
2746 } else if (inet_pton(AF_INET6, i, ed->addr.host_addr)) {
2747 ed->ht.h_addrtype = AF_INET6;
2748 ed->ht.h_length = 16;
2751 NWRAP_LOG(NWRAP_LOG_ERROR,
2752 "Invalid line[%s]: '%s'",
2760 nwrap_vector_add_item(&(ed->nwrap_addrdata),
2761 (void *const)ed->addr.host_addr);
2762 ed->ht.h_addr_list = nwrap_vector_head(&ed->nwrap_addrdata);
2770 /* Walk to first char */
2771 for (n = p; *p != '_' && !isalnum((int) *p); p++) {
2773 NWRAP_LOG(NWRAP_LOG_ERROR,
2774 "Invalid line[%s]: '%s'",
2782 for (n = p; !isspace((int)*p); p++) {
2791 /* Convert to lowercase. This operate on same memory region */
2795 /* glib's getent always dereferences he->h_aliases */
2796 ed->ht.h_aliases = malloc(sizeof(char *));
2797 if (ed->ht.h_aliases == NULL) {
2801 ed->ht.h_aliases[0] = NULL;
2806 while (do_aliases) {
2812 /* Walk to first char */
2813 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
2819 /* Only trailing spaces are left */
2824 for (a = p; !isspace((int)*p); p++) {
2833 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
2834 if (aliases == NULL) {
2838 ed->ht.h_aliases = aliases;
2841 aliases[aliases_count] = a;
2842 aliases[aliases_count + 1] = NULL;
2847 nwrap_vector_add_item(&(nwrap_he->entries), (void *const)ed);
2849 ed->aliases_count = aliases_count;
2850 /* Inventarize item */
2851 ok = nwrap_add_hname(ed);
2856 ok = nwrap_ed_inventarize(ip, ed);
2865 static void nwrap_he_unload(struct nwrap_cache *nwrap)
2867 struct nwrap_he *nwrap_he =
2868 (struct nwrap_he *)nwrap->private_data;
2869 struct nwrap_entdata *ed;
2872 nwrap_vector_foreach (ed, nwrap_he->entries, i)
2874 SAFE_FREE(ed->nwrap_addrdata.items);
2875 SAFE_FREE(ed->ht.h_aliases);
2878 SAFE_FREE(nwrap_he->entries.items);
2879 nwrap_he->entries.count = nwrap_he->entries.capacity = 0;
2886 /* user functions */
2887 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
2893 (void) b; /* unused */
2895 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
2897 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
2899 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
2903 for (i=0; i<nwrap_pw_global.num; i++) {
2904 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
2905 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
2906 return &nwrap_pw_global.list[i];
2908 NWRAP_LOG(NWRAP_LOG_DEBUG,
2909 "user[%s] does not match [%s]",
2911 nwrap_pw_global.list[i].pw_name);
2914 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
2920 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
2921 const char *name, struct passwd *pwdst,
2922 char *buf, size_t buflen, struct passwd **pwdstp)
2926 pw = nwrap_files_getpwnam(b, name);
2934 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2937 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
2943 (void) b; /* unused */
2945 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
2947 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
2951 for (i=0; i<nwrap_pw_global.num; i++) {
2952 if (nwrap_pw_global.list[i].pw_uid == uid) {
2953 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] found", uid);
2954 return &nwrap_pw_global.list[i];
2956 NWRAP_LOG(NWRAP_LOG_DEBUG,
2957 "uid[%u] does not match [%u]",
2959 nwrap_pw_global.list[i].pw_uid);
2962 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
2968 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
2969 uid_t uid, struct passwd *pwdst,
2970 char *buf, size_t buflen, struct passwd **pwdstp)
2974 pw = nwrap_files_getpwuid(b, uid);
2982 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2985 /* user enum functions */
2986 static void nwrap_files_setpwent(struct nwrap_backend *b)
2988 (void) b; /* unused */
2990 nwrap_pw_global.idx = 0;
2993 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
2997 (void) b; /* unused */
2999 if (nwrap_pw_global.idx == 0) {
3001 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3003 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3008 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
3013 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
3015 NWRAP_LOG(NWRAP_LOG_DEBUG,
3016 "return user[%s] uid[%u]",
3017 pw->pw_name, pw->pw_uid);
3022 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
3023 struct passwd *pwdst, char *buf,
3024 size_t buflen, struct passwd **pwdstp)
3028 pw = nwrap_files_getpwent(b);
3036 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3039 static void nwrap_files_endpwent(struct nwrap_backend *b)
3041 (void) b; /* unused */
3043 nwrap_pw_global.idx = 0;
3048 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
3050 #ifdef HAVE_SETSPENT
3051 static void nwrap_files_setspent(void)
3053 nwrap_sp_global.idx = 0;
3056 static struct spwd *nwrap_files_getspent(void)
3060 if (nwrap_sp_global.idx == 0) {
3063 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3065 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3070 if (nwrap_sp_global.idx >= nwrap_sp_global.num) {
3075 sp = &nwrap_sp_global.list[nwrap_sp_global.idx++];
3077 NWRAP_LOG(NWRAP_LOG_DEBUG,
3084 static void nwrap_files_endspent(void)
3086 nwrap_sp_global.idx = 0;
3088 #endif /* HAVE_SETSPENT */
3090 static struct spwd *nwrap_files_getspnam(const char *name)
3095 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
3097 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3099 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3103 for (i=0; i<nwrap_sp_global.num; i++) {
3104 if (strcmp(nwrap_sp_global.list[i].sp_namp, name) == 0) {
3105 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
3106 return &nwrap_sp_global.list[i];
3108 NWRAP_LOG(NWRAP_LOG_DEBUG,
3109 "user[%s] does not match [%s]",
3111 nwrap_sp_global.list[i].sp_namp);
3114 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3119 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3121 /* misc functions */
3122 static int nwrap_files_initgroups(struct nwrap_backend *b,
3131 groups = (gid_t *)malloc(size * sizeof(gid_t));
3132 if (groups == NULL) {
3133 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3139 nwrap_files_setgrent(b);
3140 while ((grp = nwrap_files_getgrent(b)) != NULL) {
3143 NWRAP_LOG(NWRAP_LOG_DEBUG,
3144 "Inspecting %s for group membership",
3147 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
3148 if (group != grp->gr_gid &&
3149 (strcmp(user, grp->gr_mem[i]) == 0)) {
3150 NWRAP_LOG(NWRAP_LOG_DEBUG,
3151 "%s is member of %s",
3155 groups = (gid_t *)realloc(groups,
3156 (size + 1) * sizeof(gid_t));
3157 if (groups == NULL) {
3158 NWRAP_LOG(NWRAP_LOG_ERROR,
3164 groups[size] = grp->gr_gid;
3170 nwrap_files_endgrent(b);
3172 NWRAP_LOG(NWRAP_LOG_DEBUG,
3173 "%s is member of %d groups",
3176 /* This really only works if uid_wrapper is loaded */
3177 rc = setgroups(size, groups);
3184 /* group functions */
3185 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
3191 (void) b; /* unused */
3193 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3195 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3199 for (i=0; i<nwrap_gr_global.num; i++) {
3200 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
3201 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] found", name);
3202 return &nwrap_gr_global.list[i];
3204 NWRAP_LOG(NWRAP_LOG_DEBUG,
3205 "group[%s] does not match [%s]",
3207 nwrap_gr_global.list[i].gr_name);
3210 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
3216 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
3217 const char *name, struct group *grdst,
3218 char *buf, size_t buflen, struct group **grdstp)
3222 gr = nwrap_files_getgrnam(b, name);
3230 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3233 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
3239 (void) b; /* unused */
3241 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3243 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3247 for (i=0; i<nwrap_gr_global.num; i++) {
3248 if (nwrap_gr_global.list[i].gr_gid == gid) {
3249 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] found", gid);
3250 return &nwrap_gr_global.list[i];
3252 NWRAP_LOG(NWRAP_LOG_DEBUG,
3253 "gid[%u] does not match [%u]",
3255 nwrap_gr_global.list[i].gr_gid);
3258 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
3264 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
3265 gid_t gid, struct group *grdst,
3266 char *buf, size_t buflen, struct group **grdstp)
3270 gr = nwrap_files_getgrgid(b, gid);
3278 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3281 /* group enum functions */
3282 static void nwrap_files_setgrent(struct nwrap_backend *b)
3284 (void) b; /* unused */
3286 nwrap_gr_global.idx = 0;
3289 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
3293 (void) b; /* unused */
3295 if (nwrap_gr_global.idx == 0) {
3298 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3300 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3305 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
3310 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
3312 NWRAP_LOG(NWRAP_LOG_DEBUG,
3313 "return group[%s] gid[%u]",
3314 gr->gr_name, gr->gr_gid);
3319 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
3320 struct group *grdst, char *buf,
3321 size_t buflen, struct group **grdstp)
3325 gr = nwrap_files_getgrent(b);
3333 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3336 static void nwrap_files_endgrent(struct nwrap_backend *b)
3338 (void) b; /* unused */
3340 nwrap_gr_global.idx = 0;
3343 /* hosts functions */
3344 static int nwrap_files_gethostbyname(const char *name, int af,
3345 struct hostent *result,
3346 struct nwrap_vector *addr_list)
3348 struct nwrap_entlist *el;
3353 char canon_name[DNS_NAME_MAX] = { 0 };
3355 bool he_found = false;
3358 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3360 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3364 name_len = strlen(name);
3365 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
3366 strncpy(canon_name, name, name_len - 1);
3370 if (!str_tolower_copy(&h_name_lower, name)) {
3371 NWRAP_LOG(NWRAP_LOG_DEBUG,
3372 "Out of memory while converting to lower case");
3376 /* Look at hash table for element */
3377 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3378 e.key = h_name_lower;
3380 e_p = hsearch(e, FIND);
3382 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3383 SAFE_FREE(h_name_lower);
3386 SAFE_FREE(h_name_lower);
3388 /* Always cleanup vector and results */
3389 if (!nwrap_vector_is_initialized(addr_list)) {
3390 if (!nwrap_vector_init(addr_list)) {
3391 NWRAP_LOG(NWRAP_LOG_DEBUG,
3392 "Unable to initialize memory for addr_list vector");
3396 /* When vector is initialized data are valid no more.
3397 * Quick way how to free vector is: */
3398 addr_list->count = 0;
3401 /* Iterate through results */
3402 for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3406 /* Filter by address familiy if provided */
3407 if (af != AF_UNSPEC && he->h_addrtype != af) {
3413 * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3415 if (af == AF_UNSPEC && he->h_addrtype != AF_INET) {
3420 memcpy(result, he, sizeof(struct hostent));
3421 NWRAP_LOG(NWRAP_LOG_DEBUG,
3422 "Name found. Returning record for %s",
3426 nwrap_vector_merge(addr_list, &el->ed->nwrap_addrdata);
3427 result->h_addr_list = nwrap_vector_head(addr_list);
3433 NWRAP_LOG(NWRAP_LOG_DEBUG,
3434 "Name found in database. No records matches type.");
3441 #ifdef HAVE_GETHOSTBYNAME_R
3442 static int nwrap_gethostbyname_r(const char *name,
3443 struct hostent *ret,
3444 char *buf, size_t buflen,
3445 struct hostent **result, int *h_errnop)
3447 struct nwrap_vector *addr_list = malloc(sizeof(struct nwrap_vector));
3450 if (addr_list == NULL) {
3451 NWRAP_LOG(NWRAP_LOG_ERROR,
3452 "Unable to allocate memory for address list");
3457 ZERO_STRUCTP(addr_list);
3459 rc = nwrap_files_gethostbyname(name, AF_UNSPEC, ret, addr_list);
3461 *h_errnop = h_errno;
3462 if (addr_list->items != NULL) {
3463 free(addr_list->items);
3465 SAFE_FREE(addr_list);
3470 if (buflen < (addr_list->count * sizeof(void *))) {
3471 SAFE_FREE(addr_list->items);
3472 SAFE_FREE(addr_list);
3476 /* Copy all to user provided buffer and change
3477 * pointers in returned structure.
3478 * +1 is for ending NULL pointer. */
3479 memcpy(buf, addr_list->items, (addr_list->count + 1) * sizeof(void *));
3481 free(addr_list->items);
3484 ret->h_addr_list = (char **)buf;
3489 int gethostbyname_r(const char *name,
3490 struct hostent *ret,
3491 char *buf, size_t buflen,
3492 struct hostent **result, int *h_errnop)
3494 if (!nss_wrapper_hosts_enabled()) {
3495 return libc_gethostbyname_r(name,
3503 return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
3507 static int nwrap_files_getaddrinfo(const char *name,
3508 unsigned short port,
3509 const struct addrinfo *hints,
3510 struct addrinfo **ai)
3512 struct nwrap_entlist *el;
3514 struct addrinfo *ai_head = NULL;
3515 struct addrinfo *ai_cur = NULL;
3518 char canon_name[DNS_NAME_MAX] = { 0 };
3519 bool skip_canonname = false;
3527 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3529 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3533 name_len = strlen(name);
3534 if (name_len < DNS_NAME_MAX && name[name_len - 1] == '.') {
3535 strncpy(canon_name, name, name_len - 1);
3539 if (!str_tolower_copy(&h_name_lower, name)) {
3540 NWRAP_LOG(NWRAP_LOG_DEBUG,
3541 "Out of memory while converting to lower case");
3545 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3546 e.key = h_name_lower;
3548 e_p = hsearch(e, FIND);
3550 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3551 SAFE_FREE(h_name_lower);
3555 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name: %s found.", h_name_lower);
3556 SAFE_FREE(h_name_lower);
3559 for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3562 struct addrinfo *ai_new = NULL;
3566 if (hints->ai_family != AF_UNSPEC &&
3567 he->h_addrtype != hints->ai_family)
3569 NWRAP_LOG(NWRAP_LOG_DEBUG,
3570 "Entry found but with wrong AF - "
3571 "remembering EAI_ADDRINFO.");
3572 rc = EAI_ADDRFAMILY;
3576 /* Function allocates memory and returns it in ai. */
3577 rc2 = nwrap_convert_he_ai(he,
3583 NWRAP_LOG(NWRAP_LOG_ERROR, "Error converting he to ai");
3584 if (ai_head != NULL) {
3585 freeaddrinfo(ai_head);
3589 skip_canonname = true;
3591 if (ai_head == NULL) {
3594 if (ai_cur != NULL) {
3595 ai_cur->ai_next = ai_new;
3600 if (ai_head != NULL) {
3609 static struct hostent *nwrap_files_gethostbyaddr(const void *addr,
3610 socklen_t len, int type)
3613 char ip[NWRAP_INET_ADDRSTRLEN] = {0};
3614 struct nwrap_entdata *ed;
3619 (void) len; /* unused */
3621 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3623 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3627 a = inet_ntop(type, addr, ip, sizeof(ip));
3633 nwrap_vector_foreach(ed, nwrap_he_global.entries, i)
3636 if (he->h_addrtype != type) {
3640 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
3649 #ifdef HAVE_GETHOSTBYADDR_R
3650 static int nwrap_gethostbyaddr_r(const void *addr, socklen_t len, int type,
3651 struct hostent *ret,
3652 char *buf, size_t buflen,
3653 struct hostent **result, int *h_errnop)
3655 *result = nwrap_files_gethostbyaddr(addr, len, type);
3656 if (*result != NULL) {
3657 memset(buf, '\0', buflen);
3661 *h_errnop = h_errno;
3666 int gethostbyaddr_r(const void *addr, socklen_t len, int type,
3667 struct hostent *ret,
3668 char *buf, size_t buflen,
3669 struct hostent **result, int *h_errnop)
3671 if (!nss_wrapper_hosts_enabled()) {
3672 return libc_gethostbyaddr_r(addr,
3682 return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
3686 /* hosts enum functions */
3687 static void nwrap_files_sethostent(void)
3689 nwrap_he_global.idx = 0;
3692 static struct hostent *nwrap_files_gethostent(void)
3696 if (nwrap_he_global.idx == 0) {
3699 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3701 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading hosts file");
3706 if (nwrap_he_global.idx >= nwrap_he_global.num) {
3711 he = &((struct nwrap_entdata *)nwrap_he_global.entries.items[nwrap_he_global.idx++])->ht;
3713 NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
3718 static void nwrap_files_endhostent(void)
3720 nwrap_he_global.idx = 0;
3728 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
3731 static struct passwd pwd;
3732 static char buf[1000];
3735 if (!b->fns->_nss_getpwnam_r) {
3739 status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
3740 if (status == NSS_STATUS_NOTFOUND) {
3743 if (status != NSS_STATUS_SUCCESS) {
3750 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
3751 const char *name, struct passwd *pwdst,
3752 char *buf, size_t buflen, struct passwd **pwdstp)
3756 (void) b; /* unused */
3757 (void) pwdst; /* unused */
3758 (void) pwdstp; /* unused */
3760 if (!b->fns->_nss_getpwnam_r) {
3761 return NSS_STATUS_NOTFOUND;
3764 ret = b->fns->_nss_getpwnam_r(name, pwdst, buf, buflen, &errno);
3766 case NSS_STATUS_SUCCESS:
3768 case NSS_STATUS_NOTFOUND:
3773 case NSS_STATUS_TRYAGAIN:
3786 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
3789 static struct passwd pwd;
3790 static char buf[1000];
3793 if (!b->fns->_nss_getpwuid_r) {
3797 status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
3798 if (status == NSS_STATUS_NOTFOUND) {
3801 if (status != NSS_STATUS_SUCCESS) {
3807 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
3808 uid_t uid, struct passwd *pwdst,
3809 char *buf, size_t buflen, struct passwd **pwdstp)
3813 (void) pwdstp; /* unused */
3815 if (!b->fns->_nss_getpwuid_r) {
3819 ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
3821 case NSS_STATUS_SUCCESS:
3823 case NSS_STATUS_NOTFOUND:
3828 case NSS_STATUS_TRYAGAIN:
3841 static void nwrap_module_setpwent(struct nwrap_backend *b)
3843 if (!b->fns->_nss_setpwent) {
3847 b->fns->_nss_setpwent();
3850 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
3852 static struct passwd pwd;
3853 static char buf[1000];
3856 if (!b->fns->_nss_getpwent_r) {
3860 status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
3861 if (status == NSS_STATUS_NOTFOUND) {
3864 if (status != NSS_STATUS_SUCCESS) {
3870 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
3871 struct passwd *pwdst, char *buf,
3872 size_t buflen, struct passwd **pwdstp)
3876 (void) pwdstp; /* unused */
3878 if (!b->fns->_nss_getpwent_r) {
3882 ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
3884 case NSS_STATUS_SUCCESS:
3886 case NSS_STATUS_NOTFOUND:
3891 case NSS_STATUS_TRYAGAIN:
3904 static void nwrap_module_endpwent(struct nwrap_backend *b)
3906 if (!b->fns->_nss_endpwent) {
3910 b->fns->_nss_endpwent();
3913 static int nwrap_module_initgroups(struct nwrap_backend *b,
3914 const char *user, gid_t group)
3920 if (!b->fns->_nss_initgroups) {
3921 return NSS_STATUS_UNAVAIL;
3924 return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
3927 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
3930 static struct group grp;
3932 static int buflen = 1000;
3935 if (!b->fns->_nss_getgrnam_r) {
3940 buf = (char *)malloc(buflen);
3943 status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
3944 if (status == NSS_STATUS_TRYAGAIN) {
3946 buf = (char *)realloc(buf, buflen);
3952 if (status == NSS_STATUS_NOTFOUND) {
3956 if (status != NSS_STATUS_SUCCESS) {
3963 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
3964 const char *name, struct group *grdst,
3965 char *buf, size_t buflen, struct group **grdstp)
3969 (void) grdstp; /* unused */
3971 if (!b->fns->_nss_getgrnam_r) {
3975 ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
3977 case NSS_STATUS_SUCCESS:
3979 case NSS_STATUS_NOTFOUND:
3984 case NSS_STATUS_TRYAGAIN:
3997 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
4000 static struct group grp;
4002 static int buflen = 1000;
4005 if (!b->fns->_nss_getgrgid_r) {
4010 buf = (char *)malloc(buflen);
4014 status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
4015 if (status == NSS_STATUS_TRYAGAIN) {
4017 buf = (char *)realloc(buf, buflen);
4023 if (status == NSS_STATUS_NOTFOUND) {
4027 if (status != NSS_STATUS_SUCCESS) {
4034 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
4035 gid_t gid, struct group *grdst,
4036 char *buf, size_t buflen, struct group **grdstp)
4040 (void) grdstp; /* unused */
4042 if (!b->fns->_nss_getgrgid_r) {
4046 ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
4048 case NSS_STATUS_SUCCESS:
4050 case NSS_STATUS_NOTFOUND:
4055 case NSS_STATUS_TRYAGAIN:
4068 static void nwrap_module_setgrent(struct nwrap_backend *b)
4070 if (!b->fns->_nss_setgrent) {
4074 b->fns->_nss_setgrent();
4077 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
4079 static struct group grp;
4081 static int buflen = 1024;
4084 if (!b->fns->_nss_getgrent_r) {
4089 buf = (char *)malloc(buflen);
4093 status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
4094 if (status == NSS_STATUS_TRYAGAIN) {
4096 buf = (char *)realloc(buf, buflen);
4102 if (status == NSS_STATUS_NOTFOUND) {
4106 if (status != NSS_STATUS_SUCCESS) {
4113 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
4114 struct group *grdst, char *buf,
4115 size_t buflen, struct group **grdstp)
4119 (void) grdstp; /* unused */
4121 if (!b->fns->_nss_getgrent_r) {
4125 ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
4127 case NSS_STATUS_SUCCESS:
4129 case NSS_STATUS_NOTFOUND:
4134 case NSS_STATUS_TRYAGAIN:
4147 static void nwrap_module_endgrent(struct nwrap_backend *b)
4149 if (!b->fns->_nss_endgrent) {
4153 b->fns->_nss_endgrent();
4156 /****************************************************************************
4158 ***************************************************************************/
4160 static struct passwd *nwrap_getpwnam(const char *name)
4165 for (i=0; i < nwrap_main_global->num_backends; i++) {
4166 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4167 pwd = b->ops->nw_getpwnam(b, name);
4176 struct passwd *getpwnam(const char *name)
4178 if (!nss_wrapper_enabled()) {
4179 return libc_getpwnam(name);
4182 return nwrap_getpwnam(name);
4185 /****************************************************************************
4187 ***************************************************************************/
4189 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
4190 char *buf, size_t buflen, struct passwd **pwdstp)
4194 for (i=0; i < nwrap_main_global->num_backends; i++) {
4195 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4196 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
4197 if (ret == ENOENT) {
4206 #ifdef HAVE_GETPWNAM_R
4207 # ifdef HAVE_SOLARIS_GETPWNAM_R
4208 int getpwnam_r(const char *name, struct passwd *pwdst,
4209 char *buf, int buflen, struct passwd **pwdstp)
4210 # else /* HAVE_SOLARIS_GETPWNAM_R */
4211 int getpwnam_r(const char *name, struct passwd *pwdst,
4212 char *buf, size_t buflen, struct passwd **pwdstp)
4213 # endif /* HAVE_SOLARIS_GETPWNAM_R */
4215 if (!nss_wrapper_enabled()) {
4216 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4219 return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4223 /****************************************************************************
4225 ***************************************************************************/
4227 static struct passwd *nwrap_getpwuid(uid_t uid)
4232 for (i=0; i < nwrap_main_global->num_backends; i++) {
4233 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4234 pwd = b->ops->nw_getpwuid(b, uid);
4243 struct passwd *getpwuid(uid_t uid)
4245 if (!nss_wrapper_enabled()) {
4246 return libc_getpwuid(uid);
4249 return nwrap_getpwuid(uid);
4252 /****************************************************************************
4254 ***************************************************************************/
4256 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
4257 char *buf, size_t buflen, struct passwd **pwdstp)
4261 for (i=0; i < nwrap_main_global->num_backends; i++) {
4262 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4263 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
4264 if (ret == ENOENT) {
4273 #ifdef HAVE_SOLARIS_GETPWUID_R
4274 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4275 char *buf, int buflen, struct passwd **pwdstp)
4277 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4278 char *buf, size_t buflen, struct passwd **pwdstp)
4281 if (!nss_wrapper_enabled()) {
4282 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4285 return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4288 /****************************************************************************
4290 ***************************************************************************/
4292 static void nwrap_setpwent(void)
4296 for (i=0; i < nwrap_main_global->num_backends; i++) {
4297 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4298 b->ops->nw_setpwent(b);
4304 if (!nss_wrapper_enabled()) {
4312 /****************************************************************************
4314 ***************************************************************************/
4316 static struct passwd *nwrap_getpwent(void)
4321 for (i=0; i < nwrap_main_global->num_backends; i++) {
4322 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4323 pwd = b->ops->nw_getpwent(b);
4332 struct passwd *getpwent(void)
4334 if (!nss_wrapper_enabled()) {
4335 return libc_getpwent();
4338 return nwrap_getpwent();
4341 /****************************************************************************
4343 ***************************************************************************/
4345 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
4346 size_t buflen, struct passwd **pwdstp)
4350 for (i=0; i < nwrap_main_global->num_backends; i++) {
4351 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4352 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
4353 if (ret == ENOENT) {
4362 #ifdef HAVE_SOLARIS_GETPWENT_R
4363 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
4365 struct passwd *pwdstp = NULL;
4368 if (!nss_wrapper_enabled()) {
4369 return libc_getpwent_r(pwdst, buf, buflen);
4371 rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
4378 #else /* HAVE_SOLARIS_GETPWENT_R */
4379 int getpwent_r(struct passwd *pwdst, char *buf,
4380 size_t buflen, struct passwd **pwdstp)
4382 if (!nss_wrapper_enabled()) {
4383 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
4386 return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
4388 #endif /* HAVE_SOLARIS_GETPWENT_R */
4390 /****************************************************************************
4392 ***************************************************************************/
4394 static void nwrap_endpwent(void)
4398 for (i=0; i < nwrap_main_global->num_backends; i++) {
4399 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4400 b->ops->nw_endpwent(b);
4406 if (!nss_wrapper_enabled()) {
4414 /****************************************************************************
4416 ***************************************************************************/
4418 static int nwrap_initgroups(const char *user, gid_t group)
4422 for (i=0; i < nwrap_main_global->num_backends; i++) {
4423 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4426 rc = b->ops->nw_initgroups(b, user, group);
4436 int initgroups(const char *user, gid_t group)
4438 if (!nss_wrapper_enabled()) {
4439 return libc_initgroups(user, group);
4442 return nwrap_initgroups(user, group);
4445 /****************************************************************************
4447 ***************************************************************************/
4449 static struct group *nwrap_getgrnam(const char *name)
4454 for (i=0; i < nwrap_main_global->num_backends; i++) {
4455 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4456 grp = b->ops->nw_getgrnam(b, name);
4465 struct group *getgrnam(const char *name)
4467 if (!nss_wrapper_enabled()) {
4468 return libc_getgrnam(name);
4471 return nwrap_getgrnam(name);
4474 /****************************************************************************
4476 ***************************************************************************/
4478 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
4479 char *buf, size_t buflen, struct group **grdstp)
4483 for (i=0; i < nwrap_main_global->num_backends; i++) {
4484 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4485 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
4486 if (ret == ENOENT) {
4495 #ifdef HAVE_GETGRNAM_R
4496 # ifdef HAVE_SOLARIS_GETGRNAM_R
4497 int getgrnam_r(const char *name, struct group *grp,
4498 char *buf, int buflen, struct group **pgrp)
4499 # else /* HAVE_SOLARIS_GETGRNAM_R */
4500 int getgrnam_r(const char *name, struct group *grp,
4501 char *buf, size_t buflen, struct group **pgrp)
4502 # endif /* HAVE_SOLARIS_GETGRNAM_R */
4504 if (!nss_wrapper_enabled()) {
4505 return libc_getgrnam_r(name,
4512 return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
4514 #endif /* HAVE_GETGRNAM_R */
4516 /****************************************************************************
4518 ***************************************************************************/
4520 static struct group *nwrap_getgrgid(gid_t gid)
4525 for (i=0; i < nwrap_main_global->num_backends; i++) {
4526 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4527 grp = b->ops->nw_getgrgid(b, gid);
4536 struct group *getgrgid(gid_t gid)
4538 if (!nss_wrapper_enabled()) {
4539 return libc_getgrgid(gid);
4542 return nwrap_getgrgid(gid);
4545 /****************************************************************************
4547 ***************************************************************************/
4549 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
4550 char *buf, size_t buflen, struct group **grdstp)
4554 for (i=0; i < nwrap_main_global->num_backends; i++) {
4555 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4556 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
4557 if (ret == ENOENT) {
4566 #ifdef HAVE_GETGRGID_R
4567 # ifdef HAVE_SOLARIS_GETGRGID_R
4568 int getgrgid_r(gid_t gid, struct group *grdst,
4569 char *buf, int buflen, struct group **grdstp)
4570 # else /* HAVE_SOLARIS_GETGRGID_R */
4571 int getgrgid_r(gid_t gid, struct group *grdst,
4572 char *buf, size_t buflen, struct group **grdstp)
4573 # endif /* HAVE_SOLARIS_GETGRGID_R */
4575 if (!nss_wrapper_enabled()) {
4576 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4579 return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4583 /****************************************************************************
4585 ***************************************************************************/
4587 static void nwrap_setgrent(void)
4591 for (i=0; i < nwrap_main_global->num_backends; i++) {
4592 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4593 b->ops->nw_setgrent(b);
4597 #ifdef HAVE_BSD_SETGRENT
4603 if (!nss_wrapper_enabled()) {
4611 #ifdef HAVE_BSD_SETGRENT
4618 /****************************************************************************
4620 ***************************************************************************/
4622 static struct group *nwrap_getgrent(void)
4627 for (i=0; i < nwrap_main_global->num_backends; i++) {
4628 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4629 grp = b->ops->nw_getgrent(b);
4638 struct group *getgrent(void)
4640 if (!nss_wrapper_enabled()) {
4641 return libc_getgrent();
4644 return nwrap_getgrent();
4647 /****************************************************************************
4649 ***************************************************************************/
4651 static int nwrap_getgrent_r(struct group *grdst, char *buf,
4652 size_t buflen, struct group **grdstp)
4656 for (i=0; i < nwrap_main_global->num_backends; i++) {
4657 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4658 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
4659 if (ret == ENOENT) {
4668 #ifdef HAVE_SOLARIS_GETGRENT_R
4669 struct group *getgrent_r(struct group *src, char *buf, int buflen)
4671 struct group *grdstp = NULL;
4674 if (!nss_wrapper_enabled()) {
4675 return libc_getgrent_r(src, buf, buflen);
4678 rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
4685 #else /* HAVE_SOLARIS_GETGRENT_R */
4686 int getgrent_r(struct group *src, char *buf,
4687 size_t buflen, struct group **grdstp)
4689 if (!nss_wrapper_enabled()) {
4690 return libc_getgrent_r(src, buf, buflen, grdstp);
4693 return nwrap_getgrent_r(src, buf, buflen, grdstp);
4695 #endif /* HAVE_SOLARIS_GETGRENT_R */
4697 /****************************************************************************
4699 ***************************************************************************/
4701 static void nwrap_endgrent(void)
4705 for (i=0; i < nwrap_main_global->num_backends; i++) {
4706 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4707 b->ops->nw_endgrent(b);
4713 if (!nss_wrapper_enabled()) {
4721 /****************************************************************************
4723 ***************************************************************************/
4725 #ifdef HAVE_GETGROUPLIST
4726 static int nwrap_getgrouplist(const char *user, gid_t group,
4727 gid_t *groups, int *ngroups)
4733 NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
4735 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
4737 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
4741 groups_tmp[0] = group;
4744 while ((grp = nwrap_getgrent()) != NULL) {
4747 NWRAP_LOG(NWRAP_LOG_DEBUG,
4748 "Inspecting %s for group membership",
4751 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
4753 if (group != grp->gr_gid &&
4754 (strcmp(user, grp->gr_mem[i]) == 0)) {
4756 NWRAP_LOG(NWRAP_LOG_DEBUG,
4757 "%s is member of %s",
4761 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
4763 NWRAP_LOG(NWRAP_LOG_ERROR,
4768 groups_tmp[count] = grp->gr_gid;
4777 NWRAP_LOG(NWRAP_LOG_DEBUG,
4778 "%s is member of %d groups",
4781 if (*ngroups < count) {
4788 memcpy(groups, groups_tmp, count * sizeof(gid_t));
4794 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
4796 if (!nss_wrapper_enabled()) {
4797 return libc_getgrouplist(user, group, groups, ngroups);
4800 return nwrap_getgrouplist(user, group, groups, ngroups);
4804 /**********************************************************
4806 **********************************************************/
4808 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
4810 #ifdef HAVE_SETSPENT
4811 static void nwrap_setspent(void)
4813 nwrap_files_setspent();
4818 if (!nss_wrapper_shadow_enabled()) {
4825 static struct spwd *nwrap_getspent(void)
4827 return nwrap_files_getspent();
4830 struct spwd *getspent(void)
4832 if (!nss_wrapper_shadow_enabled()) {
4836 return nwrap_getspent();
4839 static void nwrap_endspent(void)
4841 nwrap_files_endspent();
4846 if (!nss_wrapper_shadow_enabled()) {
4852 #endif /* HAVE_SETSPENT */
4854 static struct spwd *nwrap_getspnam(const char *name)
4856 return nwrap_files_getspnam(name);
4859 struct spwd *getspnam(const char *name)
4861 if (!nss_wrapper_shadow_enabled()) {
4865 return nwrap_getspnam(name);
4868 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
4870 /**********************************************************
4872 **********************************************************/
4874 static void nwrap_sethostent(int stayopen) {
4875 (void) stayopen; /* ignored */
4877 nwrap_files_sethostent();
4880 #ifdef HAVE_SOLARIS_SETHOSTENT
4881 int sethostent(int stayopen)
4883 if (!nss_wrapper_hosts_enabled()) {
4884 libc_sethostent(stayopen);
4888 nwrap_sethostent(stayopen);
4892 #else /* HAVE_SOLARIS_SETHOSTENT */
4893 void sethostent(int stayopen)
4895 if (!nss_wrapper_hosts_enabled()) {
4896 libc_sethostent(stayopen);
4900 nwrap_sethostent(stayopen);
4902 #endif /* HAVE_SOLARIS_SETHOSTENT */
4904 static struct hostent *nwrap_gethostent(void)
4906 return nwrap_files_gethostent();
4909 struct hostent *gethostent(void) {
4910 if (!nss_wrapper_hosts_enabled()) {
4911 return libc_gethostent();
4914 return nwrap_gethostent();
4917 static void nwrap_endhostent(void) {
4918 nwrap_files_endhostent();
4921 #ifdef HAVE_SOLARIS_ENDHOSTENT
4922 int endhostent(void)
4924 if (!nss_wrapper_hosts_enabled()) {
4933 #else /* HAVE_SOLARIS_ENDHOSTENT */
4934 void endhostent(void)
4936 if (!nss_wrapper_hosts_enabled()) {
4943 #endif /* HAVE_SOLARIS_ENDHOSTENT */
4946 /* BSD implementation stores data in thread local storage but GLIBC does not */
4947 static __thread struct hostent user_he;
4948 static __thread struct nwrap_vector user_addrlist;
4950 static struct hostent user_he;
4951 static struct nwrap_vector user_addrlist;
4953 static struct hostent *nwrap_gethostbyname(const char *name)
4955 if (nwrap_files_gethostbyname(name, AF_UNSPEC, &user_he, &user_addrlist) == -1) {
4961 struct hostent *gethostbyname(const char *name)
4963 if (!nss_wrapper_hosts_enabled()) {
4964 return libc_gethostbyname(name);
4967 return nwrap_gethostbyname(name);
4970 /* This is a GNU extension - Also can be found on BSD systems */
4971 #ifdef HAVE_GETHOSTBYNAME2
4973 /* BSD implementation stores data in thread local storage but GLIBC not */
4974 static __thread struct hostent user_he2;
4975 static __thread struct nwrap_vector user_addrlist2;
4977 static struct hostent user_he2;
4978 static struct nwrap_vector user_addrlist2;
4980 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
4982 if (nwrap_files_gethostbyname(name, af, &user_he2, &user_addrlist2) == -1) {
4988 struct hostent *gethostbyname2(const char *name, int af)
4990 if (!nss_wrapper_hosts_enabled()) {
4991 return libc_gethostbyname2(name, af);
4994 return nwrap_gethostbyname2(name, af);
4998 static struct hostent *nwrap_gethostbyaddr(const void *addr,
4999 socklen_t len, int type)
5001 return nwrap_files_gethostbyaddr(addr, len, type);
5004 struct hostent *gethostbyaddr(const void *addr,
5005 socklen_t len, int type)
5007 if (!nss_wrapper_hosts_enabled()) {
5008 return libc_gethostbyaddr(addr, len, type);
5011 return nwrap_gethostbyaddr(addr, len, type);
5014 static const struct addrinfo default_hints =
5016 .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
5017 .ai_family = AF_UNSPEC,
5022 .ai_canonname = NULL,
5026 static int nwrap_convert_he_ai(const struct hostent *he,
5027 unsigned short port,
5028 const struct addrinfo *hints,
5029 struct addrinfo **pai,
5030 bool skip_canonname)
5032 struct addrinfo *ai;
5039 switch (he->h_addrtype) {
5041 socklen = sizeof(struct sockaddr_in);
5045 socklen = sizeof(struct sockaddr_in6);
5052 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
5057 ai->ai_flags = hints->ai_flags;
5058 ai->ai_family = he->h_addrtype;
5059 ai->ai_socktype = hints->ai_socktype;
5060 ai->ai_protocol = hints->ai_protocol;
5061 ai->ai_canonname = NULL;
5063 if (ai->ai_socktype == 0) {
5064 ai->ai_socktype = SOCK_DGRAM;
5066 if (ai->ai_protocol == 0) {
5067 if (ai->ai_socktype == SOCK_DGRAM) {
5068 ai->ai_protocol = IPPROTO_UDP;
5069 } else if (ai->ai_socktype == SOCK_STREAM) {
5070 ai->ai_protocol = IPPROTO_TCP;
5074 ai->ai_addrlen = socklen;
5075 ai->ai_addr = (void *)(ai + 1);
5077 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
5078 ai->ai_addr->sa_len = socklen;
5080 ai->ai_addr->sa_family = he->h_addrtype;
5082 switch (he->h_addrtype) {
5085 struct sockaddr_in *sinp =
5086 (struct sockaddr_in *) ai->ai_addr;
5088 memset(sinp, 0, sizeof(struct sockaddr_in));
5090 sinp->sin_port = htons(port);
5091 sinp->sin_family = AF_INET;
5093 memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
5094 memcpy(&sinp->sin_addr, he->h_addr_list[0], he->h_length);
5101 struct sockaddr_in6 *sin6p =
5102 (struct sockaddr_in6 *) ai->ai_addr;
5104 memset(sin6p, 0, sizeof(struct sockaddr_in6));
5106 sin6p->sin6_port = htons(port);
5107 sin6p->sin6_family = AF_INET6;
5109 memcpy(&sin6p->sin6_addr,
5119 if (he->h_name && !skip_canonname) {
5120 ai->ai_canonname = strdup(he->h_name);
5121 if (ai->ai_canonname == NULL) {
5131 static int nwrap_getaddrinfo(const char *node,
5132 const char *service,
5133 const struct addrinfo *hints,
5134 struct addrinfo **res)
5136 struct addrinfo *ai = NULL;
5137 unsigned short port = 0;
5147 .family = AF_UNSPEC,
5151 if (node == NULL && service == NULL) {
5155 if (hints == NULL) {
5156 hints = &default_hints;
5160 hints.ai_flags contains invalid flags; or, hints.ai_flags
5161 included AI_CANONNAME and name was NULL.
5163 if ((hints->ai_flags & AI_CANONNAME) && (node == NULL)) {
5164 return EAI_BADFLAGS;
5167 /* If no node has been specified, let glibc deal with it */
5170 struct addrinfo *p = NULL;
5172 ret = libc_getaddrinfo(node, service, hints, &p);
5180 if (service != NULL && service[0] != '\0') {
5181 const char *proto = NULL;
5187 sl = strtol(service, &end_ptr, 10);
5189 if (*end_ptr == '\0') {
5192 } else if (hints->ai_flags & AI_NUMERICSERV) {
5196 if (hints->ai_protocol != 0) {
5197 struct protoent *pent;
5199 pent = getprotobynumber(hints->ai_protocol);
5201 proto = pent->p_name;
5205 s = getservbyname(service, proto);
5209 port = ntohs(s->s_port);
5214 rc = inet_pton(AF_INET, node, &addr.in.v4);
5216 addr.family = AF_INET;
5219 if (addr.family == AF_UNSPEC) {
5220 rc = inet_pton(AF_INET6, node, &addr.in.v6);
5222 addr.family = AF_INET6;
5227 if (addr.family == AF_UNSPEC) {
5228 if (hints->ai_flags & AI_NUMERICHOST) {
5231 } else if ((hints->ai_family != AF_UNSPEC) &&
5232 (hints->ai_family != addr.family))
5234 return EAI_ADDRFAMILY;
5237 rc = nwrap_files_getaddrinfo(node, port, hints, &ai);
5240 struct addrinfo *p = NULL;
5242 ret = libc_getaddrinfo(node, service, hints, &p);
5246 * nwrap_files_getaddrinfo failed, but libc was
5247 * successful -- use the result from libc.
5257 * If the socktype was not specified, duplicate
5258 * each ai returned, so that we have variants for
5261 if (hints->ai_socktype == 0) {
5262 struct addrinfo *ai_cur;
5264 /* freeaddrinfo() frees ai_canonname and ai so allocate them */
5265 for (ai_cur = ai; ai_cur != NULL; ai_cur = ai_cur->ai_next) {
5266 struct addrinfo *ai_new;
5268 /* duplicate the current entry */
5270 ai_new = malloc(sizeof(struct addrinfo));
5271 if (ai_new == NULL) {
5276 memcpy(ai_new, ai_cur, sizeof(struct addrinfo));
5277 ai_new->ai_next = NULL;
5279 /* We need a deep copy or freeaddrinfo() will blow up */
5280 if (ai_cur->ai_canonname != NULL) {
5281 ai_new->ai_canonname =
5282 strdup(ai_cur->ai_canonname);
5285 if (ai_cur->ai_socktype == SOCK_DGRAM) {
5286 ai_new->ai_socktype = SOCK_STREAM;
5287 } else if (ai_cur->ai_socktype == SOCK_STREAM) {
5288 ai_new->ai_socktype = SOCK_DGRAM;
5290 if (ai_cur->ai_protocol == IPPROTO_TCP) {
5291 ai_new->ai_protocol = IPPROTO_UDP;
5292 } else if (ai_cur->ai_protocol == IPPROTO_UDP) {
5293 ai_new->ai_protocol = IPPROTO_TCP;
5296 /* now insert the new entry */
5298 ai_new->ai_next = ai_cur->ai_next;
5299 ai_cur->ai_next = ai_new;
5301 /* and move on (don't duplicate the new entry) */
5312 int getaddrinfo(const char *node, const char *service,
5313 const struct addrinfo *hints,
5314 struct addrinfo **res)
5316 if (!nss_wrapper_hosts_enabled()) {
5317 return libc_getaddrinfo(node, service, hints, res);
5320 return nwrap_getaddrinfo(node, service, hints, res);
5323 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
5324 char *host, size_t hostlen,
5325 char *serv, size_t servlen,
5329 struct servent *service;
5336 if (sa == NULL || salen < sizeof(sa_family_t)) {
5340 if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
5344 type = sa->sa_family;
5347 if (salen < sizeof(struct sockaddr_in))
5349 addr = &((const struct sockaddr_in *)sa)->sin_addr;
5350 addrlen = sizeof(((const struct sockaddr_in *)sa)->sin_addr);
5351 port = ntohs(((const struct sockaddr_in *)sa)->sin_port);
5355 if (salen < sizeof(struct sockaddr_in6))
5357 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
5358 addrlen = sizeof(((const struct sockaddr_in6 *)sa)->sin6_addr);
5359 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port);
5368 if ((flags & NI_NUMERICHOST) == 0) {
5369 he = nwrap_files_gethostbyaddr(addr, addrlen, type);
5370 if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
5373 if (he != NULL && he->h_name != NULL) {
5374 if (strlen(he->h_name) >= hostlen)
5375 return EAI_OVERFLOW;
5376 strcpy(host, he->h_name);
5377 if (flags & NI_NOFQDN)
5378 host[strcspn(host, ".")] = '\0';
5380 if (inet_ntop(type, addr, host, hostlen) == NULL)
5381 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
5387 if ((flags & NI_NUMERICSERV) == 0) {
5388 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
5389 service = getservbyport(htons(port), proto);
5391 if (service != NULL) {
5392 if (strlen(service->s_name) >= servlen)
5393 return EAI_OVERFLOW;
5394 strcpy(serv, service->s_name);
5396 if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
5397 return EAI_OVERFLOW;
5404 #ifdef HAVE_LINUX_GETNAMEINFO
5405 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5406 char *host, socklen_t hostlen,
5407 char *serv, socklen_t servlen,
5409 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
5410 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5411 char *host, socklen_t hostlen,
5412 char *serv, socklen_t servlen,
5415 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5416 char *host, size_t hostlen,
5417 char *serv, size_t servlen,
5421 if (!nss_wrapper_hosts_enabled()) {
5422 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5425 return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5428 static int nwrap_gethostname(char *name, size_t len)
5430 const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
5432 if (strlen(hostname) >= len) {
5433 errno = ENAMETOOLONG;
5436 snprintf(name, len, "%s", hostname);
5441 #ifdef HAVE_SOLARIS_GETHOSTNAME
5442 int gethostname(char *name, int len)
5443 #else /* HAVE_SOLARIS_GETHOSTNAME */
5444 int gethostname(char *name, size_t len)
5445 #endif /* HAVE_SOLARIS_GETHOSTNAME */
5447 if (!nwrap_hostname_enabled()) {
5448 return libc_gethostname(name, len);
5451 return nwrap_gethostname(name, len);
5454 /****************************
5456 ***************************/
5459 * This function is called when the library is unloaded and makes sure that
5460 * sockets get closed and the unix file for the socket are unlinked.
5462 void nwrap_destructor(void)
5467 if (nwrap_main_global != NULL) {
5468 struct nwrap_main *m = nwrap_main_global;
5471 SAFE_FREE(m->libc->fns);
5472 if (m->libc->handle != NULL) {
5473 dlclose(m->libc->handle);
5475 if (m->libc->nsl_handle != NULL) {
5476 dlclose(m->libc->nsl_handle);
5478 if (m->libc->sock_handle != NULL) {
5479 dlclose(m->libc->sock_handle);
5484 for (i = 0; i < m->num_backends; i++) {
5485 struct nwrap_backend *b = &(m->backends[i]);
5487 if (b->so_handle != NULL) {
5488 dlclose(b->so_handle);
5492 SAFE_FREE(m->backends);
5495 if (nwrap_pw_global.cache != NULL) {
5496 struct nwrap_cache *c = nwrap_pw_global.cache;
5498 nwrap_files_cache_unload(c);
5504 SAFE_FREE(nwrap_pw_global.list);
5505 nwrap_pw_global.num = 0;
5508 if (nwrap_gr_global.cache != NULL) {
5509 struct nwrap_cache *c = nwrap_gr_global.cache;
5511 nwrap_files_cache_unload(c);
5517 SAFE_FREE(nwrap_gr_global.list);
5518 nwrap_pw_global.num = 0;
5521 if (nwrap_he_global.cache != NULL) {
5522 struct nwrap_cache *c = nwrap_he_global.cache;
5524 nwrap_files_cache_unload(c);
5530 nwrap_he_global.num = 0;