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;
774 struct nwrap_vector lists;
780 static struct nwrap_cache __nwrap_cache_he;
781 static struct nwrap_he nwrap_he_global;
784 /*********************************************************
786 *********************************************************/
788 static void nwrap_init(void);
789 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
790 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
791 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
793 /*********************************************************
794 * NWRAP LIBC LOADER FUNCTIONS
795 *********************************************************/
804 static const char *nwrap_str_lib(enum nwrap_lib lib)
811 case NWRAP_LIBSOCKET:
815 /* Compiler would warn us about unhandled enum value if we get here */
820 static void *nwrap_load_lib_handle(enum nwrap_lib lib)
822 int flags = RTLD_LAZY;
827 flags |= RTLD_DEEPBIND;
833 handle = nwrap_main_global->libc->nsl_handle;
834 if (handle == NULL) {
835 for (i = 10; i >= 0; i--) {
836 char soname[256] = {0};
838 snprintf(soname, sizeof(soname), "libnsl.so.%d", i);
839 handle = dlopen(soname, flags);
840 if (handle != NULL) {
845 nwrap_main_global->libc->nsl_handle = handle;
850 case NWRAP_LIBSOCKET:
851 #ifdef HAVE_LIBSOCKET
852 handle = nwrap_main_global->libc->sock_handle;
853 if (handle == NULL) {
854 for (i = 10; i >= 0; i--) {
855 char soname[256] = {0};
857 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
858 handle = dlopen(soname, flags);
859 if (handle != NULL) {
864 nwrap_main_global->libc->sock_handle = handle;
870 handle = nwrap_main_global->libc->handle;
871 if (handle == NULL) {
872 for (i = 10; i >= 0; i--) {
873 char soname[256] = {0};
875 snprintf(soname, sizeof(soname), "libc.so.%d", i);
876 handle = dlopen(soname, flags);
877 if (handle != NULL) {
882 nwrap_main_global->libc->handle = handle;
887 if (handle == NULL) {
889 handle = nwrap_main_global->libc->handle
890 = nwrap_main_global->libc->sock_handle
891 = nwrap_main_global->libc->nsl_handle
894 NWRAP_LOG(NWRAP_LOG_ERROR,
895 "Failed to dlopen library: %s\n",
904 static void *_nwrap_load_lib_function(enum nwrap_lib lib, const char *fn_name)
911 handle = nwrap_load_lib_handle(lib);
913 func = dlsym(handle, fn_name);
915 NWRAP_LOG(NWRAP_LOG_ERROR,
916 "Failed to find %s: %s\n",
921 NWRAP_LOG(NWRAP_LOG_TRACE,
923 fn_name, nwrap_str_lib(lib));
927 #define nwrap_load_lib_function(lib, fn_name) \
928 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
929 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
930 _nwrap_load_lib_function(lib, #fn_name); \
933 /* INTERNAL HELPER FUNCTIONS */
934 static void nwrap_lines_unload(struct nwrap_cache *const nwrap)
938 nwrap_vector_foreach(item, nwrap->lines, p) {
939 /* Maybe some vectors were merged ... */
942 SAFE_FREE(nwrap->lines.items);
943 ZERO_STRUCTP(&nwrap->lines);
949 * Functions expeciall from libc need to be loaded individually, you can't load
950 * all at once or gdb will segfault at startup. The same applies to valgrind and
951 * has probably something todo with with the linker.
952 * So we need load each function at the point it is called the first time.
954 static struct passwd *libc_getpwnam(const char *name)
956 nwrap_load_lib_function(NWRAP_LIBC, getpwnam);
958 return nwrap_main_global->libc->fns->_libc_getpwnam(name);
961 #ifdef HAVE_GETPWNAM_R
962 static int libc_getpwnam_r(const char *name,
966 struct passwd **result)
968 #ifdef HAVE___POSIX_GETPWNAM_R
969 if (nwrap_main_global->libc->fns->_libc_getpwnam_r == NULL) {
970 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwnam_r) =
971 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwnam_r");
974 nwrap_load_lib_function(NWRAP_LIBC, getpwnam_r);
977 return nwrap_main_global->libc->fns->_libc_getpwnam_r(name,
985 static struct passwd *libc_getpwuid(uid_t uid)
987 nwrap_load_lib_function(NWRAP_LIBC, getpwuid);
989 return nwrap_main_global->libc->fns->_libc_getpwuid(uid);
992 #ifdef HAVE_GETPWUID_R
993 static int libc_getpwuid_r(uid_t uid,
997 struct passwd **result)
999 #ifdef HAVE___POSIX_GETPWUID_R
1000 if (nwrap_main_global->libc->fns->_libc_getpwuid_r == NULL) {
1001 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwuid_r) =
1002 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwuid_r");
1005 nwrap_load_lib_function(NWRAP_LIBC, getpwuid_r);
1008 return nwrap_main_global->libc->fns->_libc_getpwuid_r(uid,
1016 static inline void str_tolower(char *dst, char *src)
1018 register char *src_tmp = src;
1019 register char *dst_tmp = dst;
1021 while (*src_tmp != '\0') {
1022 *dst_tmp = tolower(*src_tmp);
1028 static bool str_tolower_copy(char **dst_name, const char *const src_name)
1032 if ((dst_name == NULL) || (src_name == NULL)) {
1036 h_name_lower = strdup(src_name);
1037 if (h_name_lower == NULL) {
1038 NWRAP_LOG(NWRAP_LOG_DEBUG, "Out of memory while strdup");
1042 str_tolower(h_name_lower, h_name_lower);
1043 *dst_name = h_name_lower;
1047 static void libc_setpwent(void)
1049 nwrap_load_lib_function(NWRAP_LIBC, setpwent);
1051 nwrap_main_global->libc->fns->_libc_setpwent();
1054 static struct passwd *libc_getpwent(void)
1056 nwrap_load_lib_function(NWRAP_LIBC, getpwent);
1058 return nwrap_main_global->libc->fns->_libc_getpwent();
1061 #ifdef HAVE_SOLARIS_GETPWENT_R
1062 static struct passwd *libc_getpwent_r(struct passwd *pwdst,
1066 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1068 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1072 #else /* HAVE_SOLARIS_GETPWENT_R */
1073 static int libc_getpwent_r(struct passwd *pwdst,
1076 struct passwd **pwdstp)
1078 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1080 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1085 #endif /* HAVE_SOLARIS_GETPWENT_R */
1087 static void libc_endpwent(void)
1089 nwrap_load_lib_function(NWRAP_LIBC, endpwent);
1091 nwrap_main_global->libc->fns->_libc_endpwent();
1094 static int libc_initgroups(const char *user, gid_t gid)
1096 nwrap_load_lib_function(NWRAP_LIBC, initgroups);
1098 return nwrap_main_global->libc->fns->_libc_initgroups(user, gid);
1101 static struct group *libc_getgrnam(const char *name)
1103 nwrap_load_lib_function(NWRAP_LIBC, getgrnam);
1105 return nwrap_main_global->libc->fns->_libc_getgrnam(name);
1108 #ifdef HAVE_GETGRNAM_R
1109 static int libc_getgrnam_r(const char *name,
1113 struct group **result)
1115 #ifdef HAVE___POSIX_GETGRNAM_R
1116 if (nwrap_main_global->libc->fns->_libc_getgrnam_r == NULL) {
1117 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrnam_r) =
1118 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrnam_r");
1121 nwrap_load_lib_function(NWRAP_LIBC, getgrnam_r);
1124 return nwrap_main_global->libc->fns->_libc_getgrnam_r(name,
1132 static struct group *libc_getgrgid(gid_t gid)
1134 nwrap_load_lib_function(NWRAP_LIBC, getgrgid);
1136 return nwrap_main_global->libc->fns->_libc_getgrgid(gid);
1139 #ifdef HAVE_GETGRGID_R
1140 static int libc_getgrgid_r(gid_t gid,
1144 struct group **result)
1146 #ifdef HAVE___POSIX_GETGRGID_R
1147 if (nwrap_main_global->libc->fns->_libc_getgrgid_r == NULL) {
1148 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrgid_r) =
1149 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrgid_r");
1152 nwrap_load_lib_function(NWRAP_LIBC, getgrgid_r);
1155 return nwrap_main_global->libc->fns->_libc_getgrgid_r(gid,
1163 static void libc_setgrent(void)
1165 nwrap_load_lib_function(NWRAP_LIBC, setgrent);
1167 nwrap_main_global->libc->fns->_libc_setgrent();
1170 static struct group *libc_getgrent(void)
1172 nwrap_load_lib_function(NWRAP_LIBC, getgrent);
1174 return nwrap_main_global->libc->fns->_libc_getgrent();
1177 #ifdef HAVE_GETGRENT_R
1178 #ifdef HAVE_SOLARIS_GETGRENT_R
1179 static struct group *libc_getgrent_r(struct group *group,
1183 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1185 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1189 #else /* !HAVE_SOLARIS_GETGRENT_R */
1190 static int libc_getgrent_r(struct group *group,
1193 struct group **result)
1195 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1197 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1202 #endif /* HAVE_SOLARIS_GETGRENT_R */
1203 #endif /* HAVE_GETGRENT_R */
1205 static void libc_endgrent(void)
1207 nwrap_load_lib_function(NWRAP_LIBC, endgrent);
1209 nwrap_main_global->libc->fns->_libc_endgrent();
1212 #ifdef HAVE_GETGROUPLIST
1213 static int libc_getgrouplist(const char *user,
1218 nwrap_load_lib_function(NWRAP_LIBC, getgrouplist);
1220 return nwrap_main_global->libc->fns->_libc_getgrouplist(user,
1227 static void libc_sethostent(int stayopen)
1229 nwrap_load_lib_function(NWRAP_LIBNSL, sethostent);
1231 nwrap_main_global->libc->fns->_libc_sethostent(stayopen);
1234 static struct hostent *libc_gethostent(void)
1236 nwrap_load_lib_function(NWRAP_LIBNSL, gethostent);
1238 return nwrap_main_global->libc->fns->_libc_gethostent();
1241 static void libc_endhostent(void)
1243 nwrap_load_lib_function(NWRAP_LIBNSL, endhostent);
1245 nwrap_main_global->libc->fns->_libc_endhostent();
1248 static struct hostent *libc_gethostbyname(const char *name)
1250 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname);
1252 return nwrap_main_global->libc->fns->_libc_gethostbyname(name);
1255 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
1256 static struct hostent *libc_gethostbyname2(const char *name, int af)
1258 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname2);
1260 return nwrap_main_global->libc->fns->_libc_gethostbyname2(name, af);
1264 static struct hostent *libc_gethostbyaddr(const void *addr,
1268 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr);
1270 return nwrap_main_global->libc->fns->_libc_gethostbyaddr(addr,
1275 static int libc_gethostname(char *name, size_t len)
1277 nwrap_load_lib_function(NWRAP_LIBNSL, gethostname);
1279 return nwrap_main_global->libc->fns->_libc_gethostname(name, len);
1282 #ifdef HAVE_GETHOSTBYNAME_R
1283 static int libc_gethostbyname_r(const char *name,
1284 struct hostent *ret,
1287 struct hostent **result,
1290 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname_r);
1292 return nwrap_main_global->libc->fns->_libc_gethostbyname_r(name,
1301 #ifdef HAVE_GETHOSTBYADDR_R
1302 static int libc_gethostbyaddr_r(const void *addr,
1305 struct hostent *ret,
1308 struct hostent **result,
1311 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr_r);
1313 return nwrap_main_global->libc->fns->_libc_gethostbyaddr_r(addr,
1324 static int libc_getaddrinfo(const char *node,
1325 const char *service,
1326 const struct addrinfo *hints,
1327 struct addrinfo **res)
1329 nwrap_load_lib_function(NWRAP_LIBSOCKET, getaddrinfo);
1331 return nwrap_main_global->libc->fns->_libc_getaddrinfo(node,
1337 static int libc_getnameinfo(const struct sockaddr *sa,
1345 nwrap_load_lib_function(NWRAP_LIBSOCKET, getnameinfo);
1347 return nwrap_main_global->libc->fns->_libc_getnameinfo(sa,
1356 /*********************************************************
1357 * NWRAP NSS MODULE LOADER FUNCTIONS
1358 *********************************************************/
1360 static void *nwrap_load_module_fn(struct nwrap_backend *b,
1361 const char *fn_name)
1366 if (!b->so_handle) {
1367 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1371 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
1372 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1376 res = dlsym(b->so_handle, s);
1378 NWRAP_LOG(NWRAP_LOG_ERROR,
1379 "Cannot find function %s in %s",
1386 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
1388 struct nwrap_module_nss_fns *fns;
1390 if (!b->so_handle) {
1394 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
1399 *(void **)(&fns->_nss_getpwnam_r) =
1400 nwrap_load_module_fn(b, "getpwnam_r");
1401 *(void **)(&fns->_nss_getpwuid_r) =
1402 nwrap_load_module_fn(b, "getpwuid_r");
1403 *(void **)(&fns->_nss_setpwent) =
1404 nwrap_load_module_fn(b, "setpwent");
1405 *(void **)(&fns->_nss_getpwent_r) =
1406 nwrap_load_module_fn(b, "getpwent_r");
1407 *(void **)(&fns->_nss_endpwent) =
1408 nwrap_load_module_fn(b, "endpwent");
1409 *(void **)(&fns->_nss_initgroups) =
1410 nwrap_load_module_fn(b, "initgroups_dyn");
1411 *(void **)(&fns->_nss_getgrnam_r) =
1412 nwrap_load_module_fn(b, "getgrnam_r");
1413 *(void **)(&fns->_nss_getgrgid_r)=
1414 nwrap_load_module_fn(b, "getgrgid_r");
1415 *(void **)(&fns->_nss_setgrent) =
1416 nwrap_load_module_fn(b, "setgrent");
1417 *(void **)(&fns->_nss_getgrent_r) =
1418 nwrap_load_module_fn(b, "getgrent_r");
1419 *(void **)(&fns->_nss_endgrent) =
1420 nwrap_load_module_fn(b, "endgrent");
1425 static void *nwrap_load_module(const char *so_path)
1429 if (!so_path || !strlen(so_path)) {
1433 h = dlopen(so_path, RTLD_LAZY);
1435 NWRAP_LOG(NWRAP_LOG_ERROR,
1436 "Cannot open shared library %s",
1444 static bool nwrap_module_init(const char *name,
1445 struct nwrap_ops *ops,
1446 const char *so_path,
1448 struct nwrap_backend **backends)
1450 struct nwrap_backend *b;
1452 *backends = (struct nwrap_backend *)realloc(*backends,
1453 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
1455 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1459 b = &((*backends)[*num_backends]);
1463 b->so_path = so_path;
1465 if (so_path != NULL) {
1466 b->so_handle = nwrap_load_module(so_path);
1467 b->fns = nwrap_load_module_fns(b);
1468 if (b->fns == NULL) {
1472 b->so_handle = NULL;
1481 static void nwrap_libc_init(struct nwrap_main *r)
1483 r->libc = malloc(sizeof(struct nwrap_libc));
1484 if (r->libc == NULL) {
1485 printf("Failed to allocate memory for libc");
1488 ZERO_STRUCTP(r->libc);
1490 r->libc->fns = malloc(sizeof(struct nwrap_libc_fns));
1491 if (r->libc->fns == NULL) {
1492 printf("Failed to allocate memory for libc functions");
1495 ZERO_STRUCTP(r->libc->fns);
1498 static void nwrap_backend_init(struct nwrap_main *r)
1500 const char *module_so_path = getenv("NSS_WRAPPER_MODULE_SO_PATH");
1501 const char *module_fn_name = getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1503 r->num_backends = 0;
1506 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1509 NWRAP_LOG(NWRAP_LOG_ERROR,
1510 "Failed to initialize 'files' backend");
1514 if (module_so_path != NULL &&
1515 module_so_path[0] != '\0' &&
1516 module_fn_name != NULL &&
1517 module_fn_name[0] != '\0') {
1518 if (!nwrap_module_init(module_fn_name,
1523 NWRAP_LOG(NWRAP_LOG_ERROR,
1524 "Failed to initialize '%s' backend",
1531 static void nwrap_init(void)
1535 size_t max_hostents_tmp;
1537 NWRAP_LOCK(nwrap_initialized);
1538 if (nwrap_initialized) {
1539 NWRAP_UNLOCK(nwrap_initialized);
1544 * Still holding nwrap_initialized lock here.
1545 * We don't use NWRAP_(UN)LOCK_ALL macros here because we
1546 * want to avoid overhead when other threads do their job.
1548 NWRAP_LOCK(nwrap_global);
1549 NWRAP_LOCK(nwrap_gr_global);
1550 NWRAP_LOCK(nwrap_he_global);
1551 NWRAP_LOCK(nwrap_pw_global);
1552 NWRAP_LOCK(nwrap_sp_global);
1554 nwrap_initialized = true;
1556 /* Initialize pthread_atfork handlers */
1557 pthread_atfork(&nwrap_thread_prepare, &nwrap_thread_parent,
1558 &nwrap_thread_child);
1560 env = getenv("NSS_WRAPPER_MAX_HOSTENTS");
1562 max_hostents_tmp = (size_t)strtol(env, &endptr, 10);
1563 if (((env != '\0') && (endptr == '\0')) ||
1564 (max_hostents_tmp == 0)) {
1565 NWRAP_LOG(NWRAP_LOG_DEBUG,
1566 "Error parsing NSS_WRAPPER_MAX_HOSTENTS "
1567 "value or value is too small. "
1568 "Using default value: %lu.",
1569 (unsigned long)max_hostents);
1571 max_hostents = max_hostents_tmp;
1574 /* Initialize hash table */
1575 NWRAP_LOG(NWRAP_LOG_DEBUG,
1576 "Initializing hash table of size %lu items.",
1577 (unsigned long)max_hostents);
1578 if (hcreate(max_hostents) == 0) {
1579 NWRAP_LOG(NWRAP_LOG_ERROR,
1580 "Failed to initialize hash table");
1584 nwrap_main_global = &__nwrap_main_global;
1586 nwrap_libc_init(nwrap_main_global);
1588 nwrap_backend_init(nwrap_main_global);
1591 nwrap_pw_global.cache = &__nwrap_cache_pw;
1593 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
1594 nwrap_pw_global.cache->fp = NULL;
1595 nwrap_pw_global.cache->fd = -1;
1596 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
1597 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
1598 nwrap_pw_global.cache->unload = nwrap_pw_unload;
1601 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1602 nwrap_sp_global.cache = &__nwrap_cache_sp;
1604 nwrap_sp_global.cache->path = getenv("NSS_WRAPPER_SHADOW");
1605 nwrap_sp_global.cache->fp = NULL;
1606 nwrap_sp_global.cache->fd = -1;
1607 nwrap_sp_global.cache->private_data = &nwrap_sp_global;
1608 nwrap_sp_global.cache->parse_line = nwrap_sp_parse_line;
1609 nwrap_sp_global.cache->unload = nwrap_sp_unload;
1610 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1613 nwrap_gr_global.cache = &__nwrap_cache_gr;
1615 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
1616 nwrap_gr_global.cache->fp = NULL;
1617 nwrap_gr_global.cache->fd = -1;
1618 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
1619 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
1620 nwrap_gr_global.cache->unload = nwrap_gr_unload;
1623 nwrap_he_global.cache = &__nwrap_cache_he;
1625 nwrap_he_global.cache->path = getenv("NSS_WRAPPER_HOSTS");
1626 nwrap_he_global.cache->fp = NULL;
1627 nwrap_he_global.cache->fd = -1;
1628 nwrap_he_global.cache->private_data = &nwrap_he_global;
1629 nwrap_he_global.cache->parse_line = nwrap_he_parse_line;
1630 nwrap_he_global.cache->unload = nwrap_he_unload;
1633 /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1637 bool nss_wrapper_enabled(void)
1641 if (nwrap_pw_global.cache->path == NULL ||
1642 nwrap_pw_global.cache->path[0] == '\0') {
1645 if (nwrap_gr_global.cache->path == NULL ||
1646 nwrap_gr_global.cache->path[0] == '\0') {
1653 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1654 bool nss_wrapper_shadow_enabled(void)
1658 if (nwrap_sp_global.cache->path == NULL ||
1659 nwrap_sp_global.cache->path[0] == '\0') {
1665 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1667 bool nss_wrapper_hosts_enabled(void)
1671 if (nwrap_he_global.cache->path == NULL ||
1672 nwrap_he_global.cache->path[0] == '\0') {
1679 static bool nwrap_hostname_enabled(void)
1683 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
1690 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
1694 /* Unused but getline needs it */
1698 if (nwrap->st.st_size == 0) {
1699 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
1703 /* Support for 32-bit system I guess */
1704 if (nwrap->st.st_size > INT32_MAX) {
1705 NWRAP_LOG(NWRAP_LOG_ERROR,
1706 "Size[%u] larger than INT32_MAX",
1707 (unsigned)nwrap->st.st_size);
1714 n = getline(&line, &len, nwrap->fp);
1717 if (feof(nwrap->fp)) {
1721 NWRAP_LOG(NWRAP_LOG_ERROR,
1722 "Unable to read line from file: %s",
1727 if (line[n - 1] == '\n') {
1731 if (line[0] == '\0') {
1736 ok = nwrap->parse_line(nwrap, line);
1738 NWRAP_LOG(NWRAP_LOG_ERROR,
1739 "Unable to parse line file: %s",
1745 /* Line is parsed without issues so add it to list */
1746 ok = nwrap_vector_add_item(&(nwrap->lines), (void *const) line);
1748 NWRAP_LOG(NWRAP_LOG_ERROR,
1749 "Unable to add line to vector");
1753 /* This forces getline to allocate new memory for line. */
1755 } while (!feof(nwrap->fp));
1760 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
1762 nwrap->unload(nwrap);
1764 nwrap_lines_unload(nwrap);
1767 static bool nwrap_files_cache_reload(struct nwrap_cache *nwrap)
1772 bool retried = false;
1774 assert(nwrap != NULL);
1777 if (nwrap->fd < 0) {
1778 nwrap->fp = fopen(nwrap->path, "re");
1779 if (nwrap->fp == NULL) {
1781 NWRAP_LOG(NWRAP_LOG_ERROR,
1782 "Unable to open '%s' readonly %d:%s",
1783 nwrap->path, nwrap->fd,
1788 nwrap->fd = fileno(nwrap->fp);
1789 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
1792 ret = fstat(nwrap->fd, &st);
1794 NWRAP_LOG(NWRAP_LOG_ERROR,
1795 "fstat(%s) - %d:%s",
1805 if (retried == false && st.st_nlink == 0) {
1806 /* maybe someone has replaced the file... */
1807 NWRAP_LOG(NWRAP_LOG_TRACE,
1808 "st_nlink == 0, reopen %s",
1811 memset(&nwrap->st, 0, sizeof(nwrap->st));
1818 if (st.st_mtime == nwrap->st.st_mtime) {
1819 NWRAP_LOG(NWRAP_LOG_TRACE,
1820 "st_mtime[%u] hasn't changed, skip reload",
1821 (unsigned)st.st_mtime);
1825 NWRAP_LOG(NWRAP_LOG_TRACE,
1826 "st_mtime has changed [%u] => [%u], start reload",
1827 (unsigned)st.st_mtime,
1828 (unsigned)nwrap->st.st_mtime);
1832 nwrap_files_cache_unload(nwrap);
1834 ok = nwrap_parse_file(nwrap);
1836 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
1837 nwrap_files_cache_unload(nwrap);
1841 NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
1846 * the caller has to call nwrap_unload() on failure
1848 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
1850 struct nwrap_pw *nwrap_pw;
1857 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1859 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
1860 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
1862 NWRAP_LOG(NWRAP_LOG_ERROR,
1863 "realloc(%u) failed",
1864 (unsigned)list_size);
1867 nwrap_pw->list = pw;
1869 pw = &nwrap_pw->list[nwrap_pw->num];
1876 NWRAP_LOG(NWRAP_LOG_ERROR,
1877 "Invalid line[%s]: '%s'",
1887 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
1892 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1900 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
1905 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1911 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
1913 NWRAP_LOG(NWRAP_LOG_ERROR,
1914 "Invalid line[%s]: '%s' - %s",
1915 line, c, strerror(errno));
1919 NWRAP_LOG(NWRAP_LOG_ERROR,
1920 "Invalid line[%s]: '%s' - %s",
1921 line, c, strerror(errno));
1925 NWRAP_LOG(NWRAP_LOG_ERROR,
1926 "Invalid line[%s]: '%s' - %s",
1927 line, c, strerror(errno));
1932 NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
1937 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1943 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
1945 NWRAP_LOG(NWRAP_LOG_ERROR,
1946 "Invalid line[%s]: '%s' - %s",
1947 line, c, strerror(errno));
1951 NWRAP_LOG(NWRAP_LOG_ERROR,
1952 "Invalid line[%s]: '%s' - %s",
1953 line, c, strerror(errno));
1957 NWRAP_LOG(NWRAP_LOG_ERROR,
1958 "Invalid line[%s]: '%s' - %s",
1959 line, c, strerror(errno));
1964 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]\n", pw->pw_gid);
1969 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
1977 NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
1982 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
1990 NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
1994 NWRAP_LOG(NWRAP_LOG_TRACE, "shell[%s]", pw->pw_shell);
1996 NWRAP_LOG(NWRAP_LOG_DEBUG,
1997 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1998 pw->pw_name, pw->pw_passwd,
1999 pw->pw_uid, pw->pw_gid,
2000 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
2006 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
2008 struct nwrap_pw *nwrap_pw;
2009 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
2011 SAFE_FREE(nwrap_pw->list);
2016 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
2017 char *buf, size_t buflen, struct passwd **dstp)
2023 first = src->pw_name;
2025 last = src->pw_shell;
2026 while (*last) last++;
2028 ofs = PTR_DIFF(last + 1, first);
2030 if (ofs > (off_t) buflen) {
2034 memcpy(buf, first, ofs);
2036 ofs = PTR_DIFF(src->pw_name, first);
2037 dst->pw_name = buf + ofs;
2038 ofs = PTR_DIFF(src->pw_passwd, first);
2039 dst->pw_passwd = buf + ofs;
2040 dst->pw_uid = src->pw_uid;
2041 dst->pw_gid = src->pw_gid;
2042 ofs = PTR_DIFF(src->pw_gecos, first);
2043 dst->pw_gecos = buf + ofs;
2044 ofs = PTR_DIFF(src->pw_dir, first);
2045 dst->pw_dir = buf + ofs;
2046 ofs = PTR_DIFF(src->pw_shell, first);
2047 dst->pw_shell = buf + ofs;
2056 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2057 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line)
2059 struct nwrap_sp *nwrap_sp;
2066 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2068 list_size = sizeof(*nwrap_sp->list) * (nwrap_sp->num+1);
2069 sp = (struct spwd *)realloc(nwrap_sp->list, list_size);
2071 NWRAP_LOG(NWRAP_LOG_ERROR,
2072 "realloc(%u) failed",
2073 (unsigned)list_size);
2076 nwrap_sp->list = sp;
2078 sp = &nwrap_sp->list[nwrap_sp->num];
2085 NWRAP_LOG(NWRAP_LOG_ERROR,
2086 "name -- Invalid line[%s]: '%s'",
2096 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", sp->sp_namp);
2101 NWRAP_LOG(NWRAP_LOG_ERROR,
2102 "pwd -- Invalid line[%s]: '%s'",
2119 NWRAP_LOG(NWRAP_LOG_ERROR,
2120 "lstchg -- Invalid line[%s]: '%s'",
2127 sp->sp_lstchg = strtol(c, &e, 10);
2129 NWRAP_LOG(NWRAP_LOG_ERROR,
2130 "lstchg -- Invalid line[%s]: '%s' - %s",
2131 line, c, strerror(errno));
2135 NWRAP_LOG(NWRAP_LOG_ERROR,
2136 "lstchg -- Invalid line[%s]: '%s' - %s",
2137 line, c, strerror(errno));
2141 NWRAP_LOG(NWRAP_LOG_ERROR,
2142 "lstchg -- Invalid line[%s]: '%s' - %s",
2143 line, c, strerror(errno));
2156 NWRAP_LOG(NWRAP_LOG_ERROR,
2157 "min -- Invalid line[%s]: '%s'",
2164 sp->sp_min = strtol(c, &e, 10);
2166 NWRAP_LOG(NWRAP_LOG_ERROR,
2167 "min -- Invalid line[%s]: '%s' - %s",
2168 line, c, strerror(errno));
2172 NWRAP_LOG(NWRAP_LOG_ERROR,
2173 "min -- Invalid line[%s]: '%s' - %s",
2174 line, c, strerror(errno));
2178 NWRAP_LOG(NWRAP_LOG_ERROR,
2179 "min -- Invalid line[%s]: '%s' - %s",
2180 line, c, strerror(errno));
2193 NWRAP_LOG(NWRAP_LOG_ERROR,
2194 "max -- Invalid line[%s]: '%s'",
2201 sp->sp_max = strtol(c, &e, 10);
2203 NWRAP_LOG(NWRAP_LOG_ERROR,
2204 "max -- Invalid line[%s]: '%s' - %s",
2205 line, c, strerror(errno));
2209 NWRAP_LOG(NWRAP_LOG_ERROR,
2210 "max -- Invalid line[%s]: '%s' - %s",
2211 line, c, strerror(errno));
2215 NWRAP_LOG(NWRAP_LOG_ERROR,
2216 "max -- Invalid line[%s]: '%s' - %s",
2217 line, c, strerror(errno));
2230 NWRAP_LOG(NWRAP_LOG_ERROR,
2231 "warn -- Invalid line[%s]: '%s'",
2238 sp->sp_warn = strtol(c, &e, 10);
2240 NWRAP_LOG(NWRAP_LOG_ERROR,
2241 "warn -- Invalid line[%s]: '%s' - %s",
2242 line, c, strerror(errno));
2246 NWRAP_LOG(NWRAP_LOG_ERROR,
2247 "warn -- Invalid line[%s]: '%s' - %s",
2248 line, c, strerror(errno));
2252 NWRAP_LOG(NWRAP_LOG_ERROR,
2253 "warn -- Invalid line[%s]: '%s' - %s",
2254 line, c, strerror(errno));
2267 NWRAP_LOG(NWRAP_LOG_ERROR,
2268 "inact -- Invalid line[%s]: '%s'",
2275 sp->sp_inact = strtol(c, &e, 10);
2277 NWRAP_LOG(NWRAP_LOG_ERROR,
2278 "inact -- Invalid line[%s]: '%s' - %s",
2279 line, c, strerror(errno));
2283 NWRAP_LOG(NWRAP_LOG_ERROR,
2284 "inact -- Invalid line[%s]: '%s' - %s",
2285 line, c, strerror(errno));
2289 NWRAP_LOG(NWRAP_LOG_ERROR,
2290 "inact -- Invalid line[%s]: '%s' - %s",
2291 line, c, strerror(errno));
2304 NWRAP_LOG(NWRAP_LOG_ERROR,
2305 "expire -- Invalid line[%s]: '%s'",
2312 sp->sp_expire = strtol(c, &e, 10);
2314 NWRAP_LOG(NWRAP_LOG_ERROR,
2315 "expire -- Invalid line[%s]: '%s' - %s",
2316 line, c, strerror(errno));
2320 NWRAP_LOG(NWRAP_LOG_ERROR,
2321 "expire -- Invalid line[%s]: '%s' - %s",
2322 line, c, strerror(errno));
2326 NWRAP_LOG(NWRAP_LOG_ERROR,
2327 "expire -- Invalid line[%s]: '%s' - %s",
2328 line, c, strerror(errno));
2338 static void nwrap_sp_unload(struct nwrap_cache *nwrap)
2340 struct nwrap_sp *nwrap_sp;
2341 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2343 SAFE_FREE(nwrap_sp->list);
2347 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2350 * the caller has to call nwrap_unload() on failure
2352 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
2354 struct nwrap_gr *nwrap_gr;
2362 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2364 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
2365 gr = (struct group *)realloc(nwrap_gr->list, list_size);
2367 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
2370 nwrap_gr->list = gr;
2372 gr = &nwrap_gr->list[nwrap_gr->num];
2379 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2387 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
2392 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2400 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
2405 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2411 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
2413 NWRAP_LOG(NWRAP_LOG_ERROR,
2414 "Invalid line[%s]: '%s' - %s",
2415 line, c, strerror(errno));
2419 NWRAP_LOG(NWRAP_LOG_ERROR,
2420 "Invalid line[%s]: '%s' - %s",
2421 line, c, strerror(errno));
2425 NWRAP_LOG(NWRAP_LOG_ERROR,
2426 "Invalid line[%s]: '%s' - %s",
2427 line, c, strerror(errno));
2432 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
2435 gr->gr_mem = (char **)malloc(sizeof(char *));
2437 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
2440 gr->gr_mem[0] = NULL;
2442 for(nummem=0; p; nummem++) {
2452 if (strlen(c) == 0) {
2456 m_size = sizeof(char *) * (nummem+2);
2457 m = (char **)realloc(gr->gr_mem, m_size);
2459 NWRAP_LOG(NWRAP_LOG_ERROR,
2460 "realloc(%zd) failed",
2465 gr->gr_mem[nummem] = c;
2466 gr->gr_mem[nummem+1] = NULL;
2468 NWRAP_LOG(NWRAP_LOG_TRACE,
2470 nummem, gr->gr_mem[nummem]);
2473 NWRAP_LOG(NWRAP_LOG_DEBUG,
2474 "Added group[%s:%s:%u:] with %u members",
2475 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem);
2481 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
2484 struct nwrap_gr *nwrap_gr;
2485 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2487 if (nwrap_gr->list) {
2488 for (i=0; i < nwrap_gr->num; i++) {
2489 SAFE_FREE(nwrap_gr->list[i].gr_mem);
2491 SAFE_FREE(nwrap_gr->list);
2498 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
2499 char *buf, size_t buflen, struct group **dstp)
2509 first = src->gr_name;
2511 lastm = src->gr_mem;
2518 last = src->gr_passwd;
2520 while (*last) last++;
2522 ofsb = PTR_DIFF(last + 1, first);
2523 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
2525 if ((ofsb + ofsm) > (off_t) buflen) {
2529 memcpy(buf, first, ofsb);
2530 memcpy(buf + ofsb, src->gr_mem, ofsm);
2532 ofs = PTR_DIFF(src->gr_name, first);
2533 dst->gr_name = buf + ofs;
2534 ofs = PTR_DIFF(src->gr_passwd, first);
2535 dst->gr_passwd = buf + ofs;
2536 dst->gr_gid = src->gr_gid;
2538 dst->gr_mem = (char **)(buf + ofsb);
2539 for (i=0; src->gr_mem[i]; i++) {
2540 ofs = PTR_DIFF(src->gr_mem[i], first);
2541 dst->gr_mem[i] = buf + ofs;
2551 static struct nwrap_entlist *nwrap_entlist_init(struct nwrap_entdata *ed)
2553 struct nwrap_entlist *el;
2556 NWRAP_LOG(NWRAP_LOG_ERROR,
2557 "entry is NULL, can't create list item");
2561 el = (struct nwrap_entlist *)malloc(sizeof(struct nwrap_entlist));
2563 NWRAP_LOG(NWRAP_LOG_ERROR, "malloc failed");
2573 static bool nwrap_ed_inventarize_add_new(char *const h_name,
2574 struct nwrap_entdata *const ed)
2578 struct nwrap_entlist *el;
2581 if (h_name == NULL) {
2582 NWRAP_LOG(NWRAP_LOG_ERROR, "h_name NULL - can't add");
2586 el = nwrap_entlist_init(ed);
2592 e.data = (void *)el;
2594 p = hsearch(e, ENTER);
2596 NWRAP_LOG(NWRAP_LOG_ERROR, "Hash table is full!");
2600 ok = nwrap_vector_add_item(&(nwrap_he_global.lists), (void *)el);
2602 NWRAP_LOG(NWRAP_LOG_ERROR,
2603 "Failed to add list entry to vector.");
2610 static bool nwrap_ed_inventarize_add_to_existing(struct nwrap_entdata *const ed,
2611 struct nwrap_entlist *const el)
2613 struct nwrap_entlist *cursor;
2614 struct nwrap_entlist *el_new;
2617 NWRAP_LOG(NWRAP_LOG_ERROR, "list is NULL, can not add");
2622 for (cursor = el; cursor->next != NULL; cursor = cursor->next)
2624 if (cursor->ed == ed) {
2629 if (cursor->ed == ed) {
2633 el_new = nwrap_entlist_init(ed);
2634 if (el_new == NULL) {
2638 cursor->next = el_new;
2642 static bool nwrap_ed_inventarize(char *const name,
2643 struct nwrap_entdata *const ed)
2652 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
2654 p = hsearch(e, FIND);
2656 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", name);
2657 ok = nwrap_ed_inventarize_add_new(name, ed);
2659 struct nwrap_entlist *el = (struct nwrap_entlist *)p->data;
2661 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s found. Add record to list.", name);
2662 ok = nwrap_ed_inventarize_add_to_existing(ed, el);
2668 static bool nwrap_add_hname(struct nwrap_entdata *const ed)
2670 char *const h_name = (char *const)(ed->ht.h_name);
2674 ok = nwrap_ed_inventarize(h_name, ed);
2679 if (ed->ht.h_aliases == NULL) {
2683 /* Itemize aliases */
2684 for (i = 0; ed->ht.h_aliases[i] != NULL; ++i) {
2687 h_name_alias = ed->ht.h_aliases[i];
2689 NWRAP_LOG(NWRAP_LOG_DEBUG, "Add alias: %s", h_name_alias);
2691 if (!nwrap_ed_inventarize(h_name_alias, ed)) {
2692 NWRAP_LOG(NWRAP_LOG_ERROR,
2693 "Unable to add alias: %s", h_name_alias);
2701 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line)
2703 struct nwrap_he *nwrap_he = (struct nwrap_he *)nwrap->private_data;
2704 bool do_aliases = true;
2705 ssize_t aliases_count = 0;
2713 struct nwrap_entdata *ed = (struct nwrap_entdata *)
2714 malloc(sizeof(struct nwrap_entdata));
2716 NWRAP_LOG(NWRAP_LOG_ERROR,
2717 "Unable to allocate memory for nwrap_entdata");
2728 /* Walk to first char */
2729 for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
2731 NWRAP_LOG(NWRAP_LOG_ERROR,
2732 "Invalid line[%s]: '%s'",
2739 for (i = p; !isspace((int)*p); p++) {
2741 NWRAP_LOG(NWRAP_LOG_ERROR,
2742 "Invalid line[%s]: '%s'",
2751 if (inet_pton(AF_INET, i, ed->addr.host_addr)) {
2752 ed->ht.h_addrtype = AF_INET;
2753 ed->ht.h_length = 4;
2755 } else if (inet_pton(AF_INET6, i, ed->addr.host_addr)) {
2756 ed->ht.h_addrtype = AF_INET6;
2757 ed->ht.h_length = 16;
2760 NWRAP_LOG(NWRAP_LOG_ERROR,
2761 "Invalid line[%s]: '%s'",
2769 ok = nwrap_vector_add_item(&(ed->nwrap_addrdata),
2770 (void *const)ed->addr.host_addr);
2772 NWRAP_LOG(NWRAP_LOG_ERROR, "Unable to add addrdata to vector");
2776 ed->ht.h_addr_list = nwrap_vector_head(&ed->nwrap_addrdata);
2784 /* Walk to first char */
2785 for (n = p; *p != '_' && !isalnum((int) *p); p++) {
2787 NWRAP_LOG(NWRAP_LOG_ERROR,
2788 "Invalid line[%s]: '%s'",
2796 for (n = p; !isspace((int)*p); p++) {
2805 /* Convert to lowercase. This operate on same memory region */
2809 /* glib's getent always dereferences he->h_aliases */
2810 ed->ht.h_aliases = malloc(sizeof(char *));
2811 if (ed->ht.h_aliases == NULL) {
2815 ed->ht.h_aliases[0] = NULL;
2820 while (do_aliases) {
2826 /* Walk to first char */
2827 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
2833 /* Only trailing spaces are left */
2838 for (a = p; !isspace((int)*p); p++) {
2847 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
2848 if (aliases == NULL) {
2852 ed->ht.h_aliases = aliases;
2855 aliases[aliases_count] = a;
2856 aliases[aliases_count + 1] = NULL;
2861 ok = nwrap_vector_add_item(&(nwrap_he->entries), (void *const)ed);
2863 NWRAP_LOG(NWRAP_LOG_ERROR, "Unable to add entry to vector");
2868 ed->aliases_count = aliases_count;
2869 /* Inventarize item */
2870 ok = nwrap_add_hname(ed);
2875 ok = nwrap_ed_inventarize(ip, ed);
2884 static void nwrap_he_unload(struct nwrap_cache *nwrap)
2886 struct nwrap_he *nwrap_he =
2887 (struct nwrap_he *)nwrap->private_data;
2888 struct nwrap_entdata *ed;
2889 struct nwrap_entlist *el;
2892 nwrap_vector_foreach (ed, nwrap_he->entries, i)
2894 SAFE_FREE(ed->nwrap_addrdata.items);
2895 SAFE_FREE(ed->ht.h_aliases);
2898 SAFE_FREE(nwrap_he->entries.items);
2899 nwrap_he->entries.count = nwrap_he->entries.capacity = 0;
2901 nwrap_vector_foreach(el, nwrap_he->lists, i)
2903 while (el != NULL) {
2904 struct nwrap_entlist *el_next;
2911 SAFE_FREE(nwrap_he->lists.items);
2912 nwrap_he->lists.count = nwrap_he->lists.capacity = 0;
2919 /* user functions */
2920 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
2926 (void) b; /* unused */
2928 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
2930 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
2932 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
2936 for (i=0; i<nwrap_pw_global.num; i++) {
2937 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
2938 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
2939 return &nwrap_pw_global.list[i];
2941 NWRAP_LOG(NWRAP_LOG_DEBUG,
2942 "user[%s] does not match [%s]",
2944 nwrap_pw_global.list[i].pw_name);
2947 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
2953 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
2954 const char *name, struct passwd *pwdst,
2955 char *buf, size_t buflen, struct passwd **pwdstp)
2959 pw = nwrap_files_getpwnam(b, name);
2967 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2970 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
2976 (void) b; /* unused */
2978 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
2980 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
2984 for (i=0; i<nwrap_pw_global.num; i++) {
2985 if (nwrap_pw_global.list[i].pw_uid == uid) {
2986 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] found", uid);
2987 return &nwrap_pw_global.list[i];
2989 NWRAP_LOG(NWRAP_LOG_DEBUG,
2990 "uid[%u] does not match [%u]",
2992 nwrap_pw_global.list[i].pw_uid);
2995 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
3001 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
3002 uid_t uid, struct passwd *pwdst,
3003 char *buf, size_t buflen, struct passwd **pwdstp)
3007 pw = nwrap_files_getpwuid(b, uid);
3015 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3018 /* user enum functions */
3019 static void nwrap_files_setpwent(struct nwrap_backend *b)
3021 (void) b; /* unused */
3023 nwrap_pw_global.idx = 0;
3026 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
3030 (void) b; /* unused */
3032 if (nwrap_pw_global.idx == 0) {
3034 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3036 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3041 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
3046 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
3048 NWRAP_LOG(NWRAP_LOG_DEBUG,
3049 "return user[%s] uid[%u]",
3050 pw->pw_name, pw->pw_uid);
3055 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
3056 struct passwd *pwdst, char *buf,
3057 size_t buflen, struct passwd **pwdstp)
3061 pw = nwrap_files_getpwent(b);
3069 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3072 static void nwrap_files_endpwent(struct nwrap_backend *b)
3074 (void) b; /* unused */
3076 nwrap_pw_global.idx = 0;
3081 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
3083 #ifdef HAVE_SETSPENT
3084 static void nwrap_files_setspent(void)
3086 nwrap_sp_global.idx = 0;
3089 static struct spwd *nwrap_files_getspent(void)
3093 if (nwrap_sp_global.idx == 0) {
3096 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3098 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3103 if (nwrap_sp_global.idx >= nwrap_sp_global.num) {
3108 sp = &nwrap_sp_global.list[nwrap_sp_global.idx++];
3110 NWRAP_LOG(NWRAP_LOG_DEBUG,
3117 static void nwrap_files_endspent(void)
3119 nwrap_sp_global.idx = 0;
3121 #endif /* HAVE_SETSPENT */
3123 static struct spwd *nwrap_files_getspnam(const char *name)
3128 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
3130 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3132 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3136 for (i=0; i<nwrap_sp_global.num; i++) {
3137 if (strcmp(nwrap_sp_global.list[i].sp_namp, name) == 0) {
3138 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
3139 return &nwrap_sp_global.list[i];
3141 NWRAP_LOG(NWRAP_LOG_DEBUG,
3142 "user[%s] does not match [%s]",
3144 nwrap_sp_global.list[i].sp_namp);
3147 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3152 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3154 /* misc functions */
3155 static int nwrap_files_initgroups(struct nwrap_backend *b,
3164 groups = (gid_t *)malloc(size * sizeof(gid_t));
3165 if (groups == NULL) {
3166 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3172 nwrap_files_setgrent(b);
3173 while ((grp = nwrap_files_getgrent(b)) != NULL) {
3176 NWRAP_LOG(NWRAP_LOG_DEBUG,
3177 "Inspecting %s for group membership",
3180 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
3181 if (group != grp->gr_gid &&
3182 (strcmp(user, grp->gr_mem[i]) == 0)) {
3183 NWRAP_LOG(NWRAP_LOG_DEBUG,
3184 "%s is member of %s",
3188 groups = (gid_t *)realloc(groups,
3189 (size + 1) * sizeof(gid_t));
3190 if (groups == NULL) {
3191 NWRAP_LOG(NWRAP_LOG_ERROR,
3197 groups[size] = grp->gr_gid;
3203 nwrap_files_endgrent(b);
3205 NWRAP_LOG(NWRAP_LOG_DEBUG,
3206 "%s is member of %d groups",
3209 /* This really only works if uid_wrapper is loaded */
3210 rc = setgroups(size, groups);
3217 /* group functions */
3218 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
3224 (void) b; /* unused */
3226 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3228 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3232 for (i=0; i<nwrap_gr_global.num; i++) {
3233 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
3234 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] found", name);
3235 return &nwrap_gr_global.list[i];
3237 NWRAP_LOG(NWRAP_LOG_DEBUG,
3238 "group[%s] does not match [%s]",
3240 nwrap_gr_global.list[i].gr_name);
3243 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
3249 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
3250 const char *name, struct group *grdst,
3251 char *buf, size_t buflen, struct group **grdstp)
3255 gr = nwrap_files_getgrnam(b, name);
3263 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3266 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
3272 (void) b; /* unused */
3274 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3276 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3280 for (i=0; i<nwrap_gr_global.num; i++) {
3281 if (nwrap_gr_global.list[i].gr_gid == gid) {
3282 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] found", gid);
3283 return &nwrap_gr_global.list[i];
3285 NWRAP_LOG(NWRAP_LOG_DEBUG,
3286 "gid[%u] does not match [%u]",
3288 nwrap_gr_global.list[i].gr_gid);
3291 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
3297 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
3298 gid_t gid, struct group *grdst,
3299 char *buf, size_t buflen, struct group **grdstp)
3303 gr = nwrap_files_getgrgid(b, gid);
3311 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3314 /* group enum functions */
3315 static void nwrap_files_setgrent(struct nwrap_backend *b)
3317 (void) b; /* unused */
3319 nwrap_gr_global.idx = 0;
3322 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
3326 (void) b; /* unused */
3328 if (nwrap_gr_global.idx == 0) {
3331 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3333 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3338 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
3343 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
3345 NWRAP_LOG(NWRAP_LOG_DEBUG,
3346 "return group[%s] gid[%u]",
3347 gr->gr_name, gr->gr_gid);
3352 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
3353 struct group *grdst, char *buf,
3354 size_t buflen, struct group **grdstp)
3358 gr = nwrap_files_getgrent(b);
3366 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3369 static void nwrap_files_endgrent(struct nwrap_backend *b)
3371 (void) b; /* unused */
3373 nwrap_gr_global.idx = 0;
3376 /* hosts functions */
3377 static int nwrap_files_gethostbyname(const char *name, int af,
3378 struct hostent *result,
3379 struct nwrap_vector *addr_list)
3381 struct nwrap_entlist *el;
3386 char canon_name[DNS_NAME_MAX] = { 0 };
3388 bool he_found = false;
3391 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3393 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3397 name_len = strlen(name);
3398 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
3399 strncpy(canon_name, name, name_len - 1);
3403 if (!str_tolower_copy(&h_name_lower, name)) {
3404 NWRAP_LOG(NWRAP_LOG_DEBUG,
3405 "Out of memory while converting to lower case");
3409 /* Look at hash table for element */
3410 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3411 e.key = h_name_lower;
3413 e_p = hsearch(e, FIND);
3415 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3416 SAFE_FREE(h_name_lower);
3419 SAFE_FREE(h_name_lower);
3421 /* Always cleanup vector and results */
3422 if (!nwrap_vector_is_initialized(addr_list)) {
3423 if (!nwrap_vector_init(addr_list)) {
3424 NWRAP_LOG(NWRAP_LOG_DEBUG,
3425 "Unable to initialize memory for addr_list vector");
3429 /* When vector is initialized data are valid no more.
3430 * Quick way how to free vector is: */
3431 addr_list->count = 0;
3434 /* Iterate through results */
3435 for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3439 /* Filter by address familiy if provided */
3440 if (af != AF_UNSPEC && he->h_addrtype != af) {
3446 * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3448 if (af == AF_UNSPEC && he->h_addrtype != AF_INET) {
3453 memcpy(result, he, sizeof(struct hostent));
3454 NWRAP_LOG(NWRAP_LOG_DEBUG,
3455 "Name found. Returning record for %s",
3459 nwrap_vector_merge(addr_list, &el->ed->nwrap_addrdata);
3460 result->h_addr_list = nwrap_vector_head(addr_list);
3466 NWRAP_LOG(NWRAP_LOG_DEBUG,
3467 "Name found in database. No records matches type.");
3474 #ifdef HAVE_GETHOSTBYNAME_R
3475 static int nwrap_gethostbyname_r(const char *name,
3476 struct hostent *ret,
3477 char *buf, size_t buflen,
3478 struct hostent **result, int *h_errnop)
3480 struct nwrap_vector *addr_list = malloc(sizeof(struct nwrap_vector));
3483 if (addr_list == NULL) {
3484 NWRAP_LOG(NWRAP_LOG_ERROR,
3485 "Unable to allocate memory for address list");
3490 ZERO_STRUCTP(addr_list);
3492 rc = nwrap_files_gethostbyname(name, AF_UNSPEC, ret, addr_list);
3494 *h_errnop = h_errno;
3495 if (addr_list->items != NULL) {
3496 free(addr_list->items);
3498 SAFE_FREE(addr_list);
3503 if (buflen < (addr_list->count * sizeof(void *))) {
3504 SAFE_FREE(addr_list->items);
3505 SAFE_FREE(addr_list);
3509 /* Copy all to user provided buffer and change
3510 * pointers in returned structure.
3511 * +1 is for ending NULL pointer. */
3512 memcpy(buf, addr_list->items, (addr_list->count + 1) * sizeof(void *));
3514 free(addr_list->items);
3517 ret->h_addr_list = (char **)buf;
3522 int gethostbyname_r(const char *name,
3523 struct hostent *ret,
3524 char *buf, size_t buflen,
3525 struct hostent **result, int *h_errnop)
3527 if (!nss_wrapper_hosts_enabled()) {
3528 return libc_gethostbyname_r(name,
3536 return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
3540 static int nwrap_files_getaddrinfo(const char *name,
3541 unsigned short port,
3542 const struct addrinfo *hints,
3543 struct addrinfo **ai)
3545 struct nwrap_entlist *el;
3547 struct addrinfo *ai_head = NULL;
3548 struct addrinfo *ai_cur = NULL;
3551 char canon_name[DNS_NAME_MAX] = { 0 };
3552 bool skip_canonname = false;
3560 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3562 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3566 name_len = strlen(name);
3567 if (name_len < DNS_NAME_MAX && name[name_len - 1] == '.') {
3568 strncpy(canon_name, name, name_len - 1);
3572 if (!str_tolower_copy(&h_name_lower, name)) {
3573 NWRAP_LOG(NWRAP_LOG_DEBUG,
3574 "Out of memory while converting to lower case");
3578 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3579 e.key = h_name_lower;
3581 e_p = hsearch(e, FIND);
3583 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3584 SAFE_FREE(h_name_lower);
3588 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name: %s found.", h_name_lower);
3589 SAFE_FREE(h_name_lower);
3592 for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3595 struct addrinfo *ai_new = NULL;
3599 if (hints->ai_family != AF_UNSPEC &&
3600 he->h_addrtype != hints->ai_family)
3602 NWRAP_LOG(NWRAP_LOG_DEBUG,
3603 "Entry found but with wrong AF - "
3604 "remembering EAI_ADDRINFO.");
3605 rc = EAI_ADDRFAMILY;
3609 /* Function allocates memory and returns it in ai. */
3610 rc2 = nwrap_convert_he_ai(he,
3616 NWRAP_LOG(NWRAP_LOG_ERROR, "Error converting he to ai");
3617 if (ai_head != NULL) {
3618 freeaddrinfo(ai_head);
3622 skip_canonname = true;
3624 if (ai_head == NULL) {
3627 if (ai_cur != NULL) {
3628 ai_cur->ai_next = ai_new;
3633 if (ai_head != NULL) {
3642 static struct hostent *nwrap_files_gethostbyaddr(const void *addr,
3643 socklen_t len, int type)
3646 char ip[NWRAP_INET_ADDRSTRLEN] = {0};
3647 struct nwrap_entdata *ed;
3652 (void) len; /* unused */
3654 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3656 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3660 a = inet_ntop(type, addr, ip, sizeof(ip));
3666 nwrap_vector_foreach(ed, nwrap_he_global.entries, i)
3669 if (he->h_addrtype != type) {
3673 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
3682 #ifdef HAVE_GETHOSTBYADDR_R
3683 static int nwrap_gethostbyaddr_r(const void *addr, socklen_t len, int type,
3684 struct hostent *ret,
3685 char *buf, size_t buflen,
3686 struct hostent **result, int *h_errnop)
3688 *result = nwrap_files_gethostbyaddr(addr, len, type);
3689 if (*result != NULL) {
3690 memset(buf, '\0', buflen);
3694 *h_errnop = h_errno;
3699 int gethostbyaddr_r(const void *addr, socklen_t len, int type,
3700 struct hostent *ret,
3701 char *buf, size_t buflen,
3702 struct hostent **result, int *h_errnop)
3704 if (!nss_wrapper_hosts_enabled()) {
3705 return libc_gethostbyaddr_r(addr,
3715 return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
3719 /* hosts enum functions */
3720 static void nwrap_files_sethostent(void)
3722 nwrap_he_global.idx = 0;
3725 static struct hostent *nwrap_files_gethostent(void)
3729 if (nwrap_he_global.idx == 0) {
3732 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3734 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading hosts file");
3739 if (nwrap_he_global.idx >= nwrap_he_global.num) {
3744 he = &((struct nwrap_entdata *)nwrap_he_global.entries.items[nwrap_he_global.idx++])->ht;
3746 NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
3751 static void nwrap_files_endhostent(void)
3753 nwrap_he_global.idx = 0;
3761 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
3764 static struct passwd pwd;
3765 static char buf[1000];
3768 if (!b->fns->_nss_getpwnam_r) {
3772 status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
3773 if (status == NSS_STATUS_NOTFOUND) {
3776 if (status != NSS_STATUS_SUCCESS) {
3783 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
3784 const char *name, struct passwd *pwdst,
3785 char *buf, size_t buflen, struct passwd **pwdstp)
3789 (void) b; /* unused */
3790 (void) pwdst; /* unused */
3791 (void) pwdstp; /* unused */
3793 if (!b->fns->_nss_getpwnam_r) {
3794 return NSS_STATUS_NOTFOUND;
3797 ret = b->fns->_nss_getpwnam_r(name, pwdst, buf, buflen, &errno);
3799 case NSS_STATUS_SUCCESS:
3801 case NSS_STATUS_NOTFOUND:
3806 case NSS_STATUS_TRYAGAIN:
3819 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
3822 static struct passwd pwd;
3823 static char buf[1000];
3826 if (!b->fns->_nss_getpwuid_r) {
3830 status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
3831 if (status == NSS_STATUS_NOTFOUND) {
3834 if (status != NSS_STATUS_SUCCESS) {
3840 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
3841 uid_t uid, struct passwd *pwdst,
3842 char *buf, size_t buflen, struct passwd **pwdstp)
3846 (void) pwdstp; /* unused */
3848 if (!b->fns->_nss_getpwuid_r) {
3852 ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
3854 case NSS_STATUS_SUCCESS:
3856 case NSS_STATUS_NOTFOUND:
3861 case NSS_STATUS_TRYAGAIN:
3874 static void nwrap_module_setpwent(struct nwrap_backend *b)
3876 if (!b->fns->_nss_setpwent) {
3880 b->fns->_nss_setpwent();
3883 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
3885 static struct passwd pwd;
3886 static char buf[1000];
3889 if (!b->fns->_nss_getpwent_r) {
3893 status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
3894 if (status == NSS_STATUS_NOTFOUND) {
3897 if (status != NSS_STATUS_SUCCESS) {
3903 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
3904 struct passwd *pwdst, char *buf,
3905 size_t buflen, struct passwd **pwdstp)
3909 (void) pwdstp; /* unused */
3911 if (!b->fns->_nss_getpwent_r) {
3915 ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
3917 case NSS_STATUS_SUCCESS:
3919 case NSS_STATUS_NOTFOUND:
3924 case NSS_STATUS_TRYAGAIN:
3937 static void nwrap_module_endpwent(struct nwrap_backend *b)
3939 if (!b->fns->_nss_endpwent) {
3943 b->fns->_nss_endpwent();
3946 static int nwrap_module_initgroups(struct nwrap_backend *b,
3947 const char *user, gid_t group)
3953 if (!b->fns->_nss_initgroups) {
3954 return NSS_STATUS_UNAVAIL;
3957 return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
3960 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
3963 static struct group grp;
3965 static int buflen = 1000;
3968 if (!b->fns->_nss_getgrnam_r) {
3973 buf = (char *)malloc(buflen);
3976 status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
3977 if (status == NSS_STATUS_TRYAGAIN) {
3979 buf = (char *)realloc(buf, buflen);
3985 if (status == NSS_STATUS_NOTFOUND) {
3989 if (status != NSS_STATUS_SUCCESS) {
3996 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
3997 const char *name, struct group *grdst,
3998 char *buf, size_t buflen, struct group **grdstp)
4002 (void) grdstp; /* unused */
4004 if (!b->fns->_nss_getgrnam_r) {
4008 ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
4010 case NSS_STATUS_SUCCESS:
4012 case NSS_STATUS_NOTFOUND:
4017 case NSS_STATUS_TRYAGAIN:
4030 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
4033 static struct group grp;
4035 static int buflen = 1000;
4038 if (!b->fns->_nss_getgrgid_r) {
4043 buf = (char *)malloc(buflen);
4047 status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
4048 if (status == NSS_STATUS_TRYAGAIN) {
4050 buf = (char *)realloc(buf, buflen);
4056 if (status == NSS_STATUS_NOTFOUND) {
4060 if (status != NSS_STATUS_SUCCESS) {
4067 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
4068 gid_t gid, struct group *grdst,
4069 char *buf, size_t buflen, struct group **grdstp)
4073 (void) grdstp; /* unused */
4075 if (!b->fns->_nss_getgrgid_r) {
4079 ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
4081 case NSS_STATUS_SUCCESS:
4083 case NSS_STATUS_NOTFOUND:
4088 case NSS_STATUS_TRYAGAIN:
4101 static void nwrap_module_setgrent(struct nwrap_backend *b)
4103 if (!b->fns->_nss_setgrent) {
4107 b->fns->_nss_setgrent();
4110 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
4112 static struct group grp;
4114 static int buflen = 1024;
4117 if (!b->fns->_nss_getgrent_r) {
4122 buf = (char *)malloc(buflen);
4126 status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
4127 if (status == NSS_STATUS_TRYAGAIN) {
4129 buf = (char *)realloc(buf, buflen);
4135 if (status == NSS_STATUS_NOTFOUND) {
4139 if (status != NSS_STATUS_SUCCESS) {
4146 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
4147 struct group *grdst, char *buf,
4148 size_t buflen, struct group **grdstp)
4152 (void) grdstp; /* unused */
4154 if (!b->fns->_nss_getgrent_r) {
4158 ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
4160 case NSS_STATUS_SUCCESS:
4162 case NSS_STATUS_NOTFOUND:
4167 case NSS_STATUS_TRYAGAIN:
4180 static void nwrap_module_endgrent(struct nwrap_backend *b)
4182 if (!b->fns->_nss_endgrent) {
4186 b->fns->_nss_endgrent();
4189 /****************************************************************************
4191 ***************************************************************************/
4193 static struct passwd *nwrap_getpwnam(const char *name)
4198 for (i=0; i < nwrap_main_global->num_backends; i++) {
4199 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4200 pwd = b->ops->nw_getpwnam(b, name);
4209 struct passwd *getpwnam(const char *name)
4211 if (!nss_wrapper_enabled()) {
4212 return libc_getpwnam(name);
4215 return nwrap_getpwnam(name);
4218 /****************************************************************************
4220 ***************************************************************************/
4222 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
4223 char *buf, size_t buflen, struct passwd **pwdstp)
4227 for (i=0; i < nwrap_main_global->num_backends; i++) {
4228 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4229 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
4230 if (ret == ENOENT) {
4239 #ifdef HAVE_GETPWNAM_R
4240 # ifdef HAVE_SOLARIS_GETPWNAM_R
4241 int getpwnam_r(const char *name, struct passwd *pwdst,
4242 char *buf, int buflen, struct passwd **pwdstp)
4243 # else /* HAVE_SOLARIS_GETPWNAM_R */
4244 int getpwnam_r(const char *name, struct passwd *pwdst,
4245 char *buf, size_t buflen, struct passwd **pwdstp)
4246 # endif /* HAVE_SOLARIS_GETPWNAM_R */
4248 if (!nss_wrapper_enabled()) {
4249 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4252 return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4256 /****************************************************************************
4258 ***************************************************************************/
4260 static struct passwd *nwrap_getpwuid(uid_t uid)
4265 for (i=0; i < nwrap_main_global->num_backends; i++) {
4266 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4267 pwd = b->ops->nw_getpwuid(b, uid);
4276 struct passwd *getpwuid(uid_t uid)
4278 if (!nss_wrapper_enabled()) {
4279 return libc_getpwuid(uid);
4282 return nwrap_getpwuid(uid);
4285 /****************************************************************************
4287 ***************************************************************************/
4289 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
4290 char *buf, size_t buflen, struct passwd **pwdstp)
4294 for (i=0; i < nwrap_main_global->num_backends; i++) {
4295 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4296 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
4297 if (ret == ENOENT) {
4306 #ifdef HAVE_SOLARIS_GETPWUID_R
4307 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4308 char *buf, int buflen, struct passwd **pwdstp)
4310 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4311 char *buf, size_t buflen, struct passwd **pwdstp)
4314 if (!nss_wrapper_enabled()) {
4315 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4318 return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4321 /****************************************************************************
4323 ***************************************************************************/
4325 static void nwrap_setpwent(void)
4329 for (i=0; i < nwrap_main_global->num_backends; i++) {
4330 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4331 b->ops->nw_setpwent(b);
4337 if (!nss_wrapper_enabled()) {
4345 /****************************************************************************
4347 ***************************************************************************/
4349 static struct passwd *nwrap_getpwent(void)
4354 for (i=0; i < nwrap_main_global->num_backends; i++) {
4355 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4356 pwd = b->ops->nw_getpwent(b);
4365 struct passwd *getpwent(void)
4367 if (!nss_wrapper_enabled()) {
4368 return libc_getpwent();
4371 return nwrap_getpwent();
4374 /****************************************************************************
4376 ***************************************************************************/
4378 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
4379 size_t buflen, struct passwd **pwdstp)
4383 for (i=0; i < nwrap_main_global->num_backends; i++) {
4384 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4385 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
4386 if (ret == ENOENT) {
4395 #ifdef HAVE_SOLARIS_GETPWENT_R
4396 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
4398 struct passwd *pwdstp = NULL;
4401 if (!nss_wrapper_enabled()) {
4402 return libc_getpwent_r(pwdst, buf, buflen);
4404 rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
4411 #else /* HAVE_SOLARIS_GETPWENT_R */
4412 int getpwent_r(struct passwd *pwdst, char *buf,
4413 size_t buflen, struct passwd **pwdstp)
4415 if (!nss_wrapper_enabled()) {
4416 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
4419 return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
4421 #endif /* HAVE_SOLARIS_GETPWENT_R */
4423 /****************************************************************************
4425 ***************************************************************************/
4427 static void nwrap_endpwent(void)
4431 for (i=0; i < nwrap_main_global->num_backends; i++) {
4432 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4433 b->ops->nw_endpwent(b);
4439 if (!nss_wrapper_enabled()) {
4447 /****************************************************************************
4449 ***************************************************************************/
4451 static int nwrap_initgroups(const char *user, gid_t group)
4455 for (i=0; i < nwrap_main_global->num_backends; i++) {
4456 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4459 rc = b->ops->nw_initgroups(b, user, group);
4469 int initgroups(const char *user, gid_t group)
4471 if (!nss_wrapper_enabled()) {
4472 return libc_initgroups(user, group);
4475 return nwrap_initgroups(user, group);
4478 /****************************************************************************
4480 ***************************************************************************/
4482 static struct group *nwrap_getgrnam(const char *name)
4487 for (i=0; i < nwrap_main_global->num_backends; i++) {
4488 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4489 grp = b->ops->nw_getgrnam(b, name);
4498 struct group *getgrnam(const char *name)
4500 if (!nss_wrapper_enabled()) {
4501 return libc_getgrnam(name);
4504 return nwrap_getgrnam(name);
4507 /****************************************************************************
4509 ***************************************************************************/
4511 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
4512 char *buf, size_t buflen, struct group **grdstp)
4516 for (i=0; i < nwrap_main_global->num_backends; i++) {
4517 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4518 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
4519 if (ret == ENOENT) {
4528 #ifdef HAVE_GETGRNAM_R
4529 # ifdef HAVE_SOLARIS_GETGRNAM_R
4530 int getgrnam_r(const char *name, struct group *grp,
4531 char *buf, int buflen, struct group **pgrp)
4532 # else /* HAVE_SOLARIS_GETGRNAM_R */
4533 int getgrnam_r(const char *name, struct group *grp,
4534 char *buf, size_t buflen, struct group **pgrp)
4535 # endif /* HAVE_SOLARIS_GETGRNAM_R */
4537 if (!nss_wrapper_enabled()) {
4538 return libc_getgrnam_r(name,
4545 return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
4547 #endif /* HAVE_GETGRNAM_R */
4549 /****************************************************************************
4551 ***************************************************************************/
4553 static struct group *nwrap_getgrgid(gid_t gid)
4558 for (i=0; i < nwrap_main_global->num_backends; i++) {
4559 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4560 grp = b->ops->nw_getgrgid(b, gid);
4569 struct group *getgrgid(gid_t gid)
4571 if (!nss_wrapper_enabled()) {
4572 return libc_getgrgid(gid);
4575 return nwrap_getgrgid(gid);
4578 /****************************************************************************
4580 ***************************************************************************/
4582 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
4583 char *buf, size_t buflen, struct group **grdstp)
4587 for (i=0; i < nwrap_main_global->num_backends; i++) {
4588 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4589 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
4590 if (ret == ENOENT) {
4599 #ifdef HAVE_GETGRGID_R
4600 # ifdef HAVE_SOLARIS_GETGRGID_R
4601 int getgrgid_r(gid_t gid, struct group *grdst,
4602 char *buf, int buflen, struct group **grdstp)
4603 # else /* HAVE_SOLARIS_GETGRGID_R */
4604 int getgrgid_r(gid_t gid, struct group *grdst,
4605 char *buf, size_t buflen, struct group **grdstp)
4606 # endif /* HAVE_SOLARIS_GETGRGID_R */
4608 if (!nss_wrapper_enabled()) {
4609 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4612 return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4616 /****************************************************************************
4618 ***************************************************************************/
4620 static void nwrap_setgrent(void)
4624 for (i=0; i < nwrap_main_global->num_backends; i++) {
4625 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4626 b->ops->nw_setgrent(b);
4630 #ifdef HAVE_BSD_SETGRENT
4636 if (!nss_wrapper_enabled()) {
4644 #ifdef HAVE_BSD_SETGRENT
4651 /****************************************************************************
4653 ***************************************************************************/
4655 static struct group *nwrap_getgrent(void)
4660 for (i=0; i < nwrap_main_global->num_backends; i++) {
4661 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4662 grp = b->ops->nw_getgrent(b);
4671 struct group *getgrent(void)
4673 if (!nss_wrapper_enabled()) {
4674 return libc_getgrent();
4677 return nwrap_getgrent();
4680 /****************************************************************************
4682 ***************************************************************************/
4684 static int nwrap_getgrent_r(struct group *grdst, char *buf,
4685 size_t buflen, struct group **grdstp)
4689 for (i=0; i < nwrap_main_global->num_backends; i++) {
4690 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4691 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
4692 if (ret == ENOENT) {
4701 #ifdef HAVE_SOLARIS_GETGRENT_R
4702 struct group *getgrent_r(struct group *src, char *buf, int buflen)
4704 struct group *grdstp = NULL;
4707 if (!nss_wrapper_enabled()) {
4708 return libc_getgrent_r(src, buf, buflen);
4711 rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
4718 #else /* HAVE_SOLARIS_GETGRENT_R */
4719 int getgrent_r(struct group *src, char *buf,
4720 size_t buflen, struct group **grdstp)
4722 if (!nss_wrapper_enabled()) {
4723 return libc_getgrent_r(src, buf, buflen, grdstp);
4726 return nwrap_getgrent_r(src, buf, buflen, grdstp);
4728 #endif /* HAVE_SOLARIS_GETGRENT_R */
4730 /****************************************************************************
4732 ***************************************************************************/
4734 static void nwrap_endgrent(void)
4738 for (i=0; i < nwrap_main_global->num_backends; i++) {
4739 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4740 b->ops->nw_endgrent(b);
4746 if (!nss_wrapper_enabled()) {
4754 /****************************************************************************
4756 ***************************************************************************/
4758 #ifdef HAVE_GETGROUPLIST
4759 static int nwrap_getgrouplist(const char *user, gid_t group,
4760 gid_t *groups, int *ngroups)
4766 NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
4768 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
4770 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
4774 groups_tmp[0] = group;
4777 while ((grp = nwrap_getgrent()) != NULL) {
4780 NWRAP_LOG(NWRAP_LOG_DEBUG,
4781 "Inspecting %s for group membership",
4784 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
4786 if (group != grp->gr_gid &&
4787 (strcmp(user, grp->gr_mem[i]) == 0)) {
4789 NWRAP_LOG(NWRAP_LOG_DEBUG,
4790 "%s is member of %s",
4794 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
4796 NWRAP_LOG(NWRAP_LOG_ERROR,
4801 groups_tmp[count] = grp->gr_gid;
4810 NWRAP_LOG(NWRAP_LOG_DEBUG,
4811 "%s is member of %d groups",
4814 if (*ngroups < count) {
4821 memcpy(groups, groups_tmp, count * sizeof(gid_t));
4827 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
4829 if (!nss_wrapper_enabled()) {
4830 return libc_getgrouplist(user, group, groups, ngroups);
4833 return nwrap_getgrouplist(user, group, groups, ngroups);
4837 /**********************************************************
4839 **********************************************************/
4841 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
4843 #ifdef HAVE_SETSPENT
4844 static void nwrap_setspent(void)
4846 nwrap_files_setspent();
4851 if (!nss_wrapper_shadow_enabled()) {
4858 static struct spwd *nwrap_getspent(void)
4860 return nwrap_files_getspent();
4863 struct spwd *getspent(void)
4865 if (!nss_wrapper_shadow_enabled()) {
4869 return nwrap_getspent();
4872 static void nwrap_endspent(void)
4874 nwrap_files_endspent();
4879 if (!nss_wrapper_shadow_enabled()) {
4885 #endif /* HAVE_SETSPENT */
4887 static struct spwd *nwrap_getspnam(const char *name)
4889 return nwrap_files_getspnam(name);
4892 struct spwd *getspnam(const char *name)
4894 if (!nss_wrapper_shadow_enabled()) {
4898 return nwrap_getspnam(name);
4901 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
4903 /**********************************************************
4905 **********************************************************/
4907 static void nwrap_sethostent(int stayopen) {
4908 (void) stayopen; /* ignored */
4910 nwrap_files_sethostent();
4913 #ifdef HAVE_SOLARIS_SETHOSTENT
4914 int sethostent(int stayopen)
4916 if (!nss_wrapper_hosts_enabled()) {
4917 libc_sethostent(stayopen);
4921 nwrap_sethostent(stayopen);
4925 #else /* HAVE_SOLARIS_SETHOSTENT */
4926 void sethostent(int stayopen)
4928 if (!nss_wrapper_hosts_enabled()) {
4929 libc_sethostent(stayopen);
4933 nwrap_sethostent(stayopen);
4935 #endif /* HAVE_SOLARIS_SETHOSTENT */
4937 static struct hostent *nwrap_gethostent(void)
4939 return nwrap_files_gethostent();
4942 struct hostent *gethostent(void) {
4943 if (!nss_wrapper_hosts_enabled()) {
4944 return libc_gethostent();
4947 return nwrap_gethostent();
4950 static void nwrap_endhostent(void) {
4951 nwrap_files_endhostent();
4954 #ifdef HAVE_SOLARIS_ENDHOSTENT
4955 int endhostent(void)
4957 if (!nss_wrapper_hosts_enabled()) {
4966 #else /* HAVE_SOLARIS_ENDHOSTENT */
4967 void endhostent(void)
4969 if (!nss_wrapper_hosts_enabled()) {
4976 #endif /* HAVE_SOLARIS_ENDHOSTENT */
4979 /* BSD implementation stores data in thread local storage but GLIBC does not */
4980 static __thread struct hostent user_he;
4981 static __thread struct nwrap_vector user_addrlist;
4983 static struct hostent user_he;
4984 static struct nwrap_vector user_addrlist;
4986 static struct hostent *nwrap_gethostbyname(const char *name)
4988 if (nwrap_files_gethostbyname(name, AF_UNSPEC, &user_he, &user_addrlist) == -1) {
4994 struct hostent *gethostbyname(const char *name)
4996 if (!nss_wrapper_hosts_enabled()) {
4997 return libc_gethostbyname(name);
5000 return nwrap_gethostbyname(name);
5003 /* This is a GNU extension - Also can be found on BSD systems */
5004 #ifdef HAVE_GETHOSTBYNAME2
5006 /* BSD implementation stores data in thread local storage but GLIBC not */
5007 static __thread struct hostent user_he2;
5008 static __thread struct nwrap_vector user_addrlist2;
5010 static struct hostent user_he2;
5011 static struct nwrap_vector user_addrlist2;
5013 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
5015 if (nwrap_files_gethostbyname(name, af, &user_he2, &user_addrlist2) == -1) {
5021 struct hostent *gethostbyname2(const char *name, int af)
5023 if (!nss_wrapper_hosts_enabled()) {
5024 return libc_gethostbyname2(name, af);
5027 return nwrap_gethostbyname2(name, af);
5031 static struct hostent *nwrap_gethostbyaddr(const void *addr,
5032 socklen_t len, int type)
5034 return nwrap_files_gethostbyaddr(addr, len, type);
5037 struct hostent *gethostbyaddr(const void *addr,
5038 socklen_t len, int type)
5040 if (!nss_wrapper_hosts_enabled()) {
5041 return libc_gethostbyaddr(addr, len, type);
5044 return nwrap_gethostbyaddr(addr, len, type);
5047 static const struct addrinfo default_hints =
5049 .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
5050 .ai_family = AF_UNSPEC,
5055 .ai_canonname = NULL,
5059 static int nwrap_convert_he_ai(const struct hostent *he,
5060 unsigned short port,
5061 const struct addrinfo *hints,
5062 struct addrinfo **pai,
5063 bool skip_canonname)
5065 struct addrinfo *ai;
5072 switch (he->h_addrtype) {
5074 socklen = sizeof(struct sockaddr_in);
5078 socklen = sizeof(struct sockaddr_in6);
5085 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
5090 ai->ai_flags = hints->ai_flags;
5091 ai->ai_family = he->h_addrtype;
5092 ai->ai_socktype = hints->ai_socktype;
5093 ai->ai_protocol = hints->ai_protocol;
5094 ai->ai_canonname = NULL;
5096 if (ai->ai_socktype == 0) {
5097 ai->ai_socktype = SOCK_DGRAM;
5099 if (ai->ai_protocol == 0) {
5100 if (ai->ai_socktype == SOCK_DGRAM) {
5101 ai->ai_protocol = IPPROTO_UDP;
5102 } else if (ai->ai_socktype == SOCK_STREAM) {
5103 ai->ai_protocol = IPPROTO_TCP;
5107 ai->ai_addrlen = socklen;
5108 ai->ai_addr = (void *)(ai + 1);
5110 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
5111 ai->ai_addr->sa_len = socklen;
5113 ai->ai_addr->sa_family = he->h_addrtype;
5115 switch (he->h_addrtype) {
5118 struct sockaddr_in *sinp =
5119 (struct sockaddr_in *) ai->ai_addr;
5121 memset(sinp, 0, sizeof(struct sockaddr_in));
5123 sinp->sin_port = htons(port);
5124 sinp->sin_family = AF_INET;
5126 memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
5127 memcpy(&sinp->sin_addr, he->h_addr_list[0], he->h_length);
5134 struct sockaddr_in6 *sin6p =
5135 (struct sockaddr_in6 *) ai->ai_addr;
5137 memset(sin6p, 0, sizeof(struct sockaddr_in6));
5139 sin6p->sin6_port = htons(port);
5140 sin6p->sin6_family = AF_INET6;
5142 memcpy(&sin6p->sin6_addr,
5152 if (he->h_name && !skip_canonname) {
5153 ai->ai_canonname = strdup(he->h_name);
5154 if (ai->ai_canonname == NULL) {
5164 static int nwrap_getaddrinfo(const char *node,
5165 const char *service,
5166 const struct addrinfo *hints,
5167 struct addrinfo **res)
5169 struct addrinfo *ai = NULL;
5170 unsigned short port = 0;
5180 .family = AF_UNSPEC,
5184 if (node == NULL && service == NULL) {
5188 if (hints == NULL) {
5189 hints = &default_hints;
5193 hints.ai_flags contains invalid flags; or, hints.ai_flags
5194 included AI_CANONNAME and name was NULL.
5196 if ((hints->ai_flags & AI_CANONNAME) && (node == NULL)) {
5197 return EAI_BADFLAGS;
5200 /* If no node has been specified, let glibc deal with it */
5203 struct addrinfo *p = NULL;
5205 ret = libc_getaddrinfo(node, service, hints, &p);
5213 if (service != NULL && service[0] != '\0') {
5214 const char *proto = NULL;
5220 sl = strtol(service, &end_ptr, 10);
5222 if (*end_ptr == '\0') {
5225 } else if (hints->ai_flags & AI_NUMERICSERV) {
5229 if (hints->ai_protocol != 0) {
5230 struct protoent *pent;
5232 pent = getprotobynumber(hints->ai_protocol);
5234 proto = pent->p_name;
5238 s = getservbyname(service, proto);
5242 port = ntohs(s->s_port);
5247 rc = inet_pton(AF_INET, node, &addr.in.v4);
5249 addr.family = AF_INET;
5252 if (addr.family == AF_UNSPEC) {
5253 rc = inet_pton(AF_INET6, node, &addr.in.v6);
5255 addr.family = AF_INET6;
5260 if (addr.family == AF_UNSPEC) {
5261 if (hints->ai_flags & AI_NUMERICHOST) {
5264 } else if ((hints->ai_family != AF_UNSPEC) &&
5265 (hints->ai_family != addr.family))
5267 return EAI_ADDRFAMILY;
5270 rc = nwrap_files_getaddrinfo(node, port, hints, &ai);
5273 struct addrinfo *p = NULL;
5275 ret = libc_getaddrinfo(node, service, hints, &p);
5279 * nwrap_files_getaddrinfo failed, but libc was
5280 * successful -- use the result from libc.
5290 * If the socktype was not specified, duplicate
5291 * each ai returned, so that we have variants for
5294 if (hints->ai_socktype == 0) {
5295 struct addrinfo *ai_cur;
5297 /* freeaddrinfo() frees ai_canonname and ai so allocate them */
5298 for (ai_cur = ai; ai_cur != NULL; ai_cur = ai_cur->ai_next) {
5299 struct addrinfo *ai_new;
5301 /* duplicate the current entry */
5303 ai_new = malloc(sizeof(struct addrinfo));
5304 if (ai_new == NULL) {
5309 memcpy(ai_new, ai_cur, sizeof(struct addrinfo));
5310 ai_new->ai_next = NULL;
5312 /* We need a deep copy or freeaddrinfo() will blow up */
5313 if (ai_cur->ai_canonname != NULL) {
5314 ai_new->ai_canonname =
5315 strdup(ai_cur->ai_canonname);
5318 if (ai_cur->ai_socktype == SOCK_DGRAM) {
5319 ai_new->ai_socktype = SOCK_STREAM;
5320 } else if (ai_cur->ai_socktype == SOCK_STREAM) {
5321 ai_new->ai_socktype = SOCK_DGRAM;
5323 if (ai_cur->ai_protocol == IPPROTO_TCP) {
5324 ai_new->ai_protocol = IPPROTO_UDP;
5325 } else if (ai_cur->ai_protocol == IPPROTO_UDP) {
5326 ai_new->ai_protocol = IPPROTO_TCP;
5329 /* now insert the new entry */
5331 ai_new->ai_next = ai_cur->ai_next;
5332 ai_cur->ai_next = ai_new;
5334 /* and move on (don't duplicate the new entry) */
5345 int getaddrinfo(const char *node, const char *service,
5346 const struct addrinfo *hints,
5347 struct addrinfo **res)
5349 if (!nss_wrapper_hosts_enabled()) {
5350 return libc_getaddrinfo(node, service, hints, res);
5353 return nwrap_getaddrinfo(node, service, hints, res);
5356 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
5357 char *host, size_t hostlen,
5358 char *serv, size_t servlen,
5362 struct servent *service;
5369 if (sa == NULL || salen < sizeof(sa_family_t)) {
5373 if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
5377 type = sa->sa_family;
5380 if (salen < sizeof(struct sockaddr_in))
5382 addr = &((const struct sockaddr_in *)sa)->sin_addr;
5383 addrlen = sizeof(((const struct sockaddr_in *)sa)->sin_addr);
5384 port = ntohs(((const struct sockaddr_in *)sa)->sin_port);
5388 if (salen < sizeof(struct sockaddr_in6))
5390 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
5391 addrlen = sizeof(((const struct sockaddr_in6 *)sa)->sin6_addr);
5392 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port);
5401 if ((flags & NI_NUMERICHOST) == 0) {
5402 he = nwrap_files_gethostbyaddr(addr, addrlen, type);
5403 if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
5406 if (he != NULL && he->h_name != NULL) {
5407 if (strlen(he->h_name) >= hostlen)
5408 return EAI_OVERFLOW;
5409 strcpy(host, he->h_name);
5410 if (flags & NI_NOFQDN)
5411 host[strcspn(host, ".")] = '\0';
5413 if (inet_ntop(type, addr, host, hostlen) == NULL)
5414 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
5420 if ((flags & NI_NUMERICSERV) == 0) {
5421 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
5422 service = getservbyport(htons(port), proto);
5424 if (service != NULL) {
5425 if (strlen(service->s_name) >= servlen)
5426 return EAI_OVERFLOW;
5427 strcpy(serv, service->s_name);
5429 if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
5430 return EAI_OVERFLOW;
5437 #ifdef HAVE_LINUX_GETNAMEINFO
5438 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5439 char *host, socklen_t hostlen,
5440 char *serv, socklen_t servlen,
5442 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
5443 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5444 char *host, socklen_t hostlen,
5445 char *serv, socklen_t servlen,
5448 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5449 char *host, size_t hostlen,
5450 char *serv, size_t servlen,
5454 if (!nss_wrapper_hosts_enabled()) {
5455 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5458 return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5461 static int nwrap_gethostname(char *name, size_t len)
5463 const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
5465 if (strlen(hostname) >= len) {
5466 errno = ENAMETOOLONG;
5469 snprintf(name, len, "%s", hostname);
5474 #ifdef HAVE_SOLARIS_GETHOSTNAME
5475 int gethostname(char *name, int len)
5476 #else /* HAVE_SOLARIS_GETHOSTNAME */
5477 int gethostname(char *name, size_t len)
5478 #endif /* HAVE_SOLARIS_GETHOSTNAME */
5480 if (!nwrap_hostname_enabled()) {
5481 return libc_gethostname(name, len);
5484 return nwrap_gethostname(name, len);
5487 /****************************
5489 ***************************/
5492 * This function is called when the library is unloaded and makes sure that
5493 * sockets get closed and the unix file for the socket are unlinked.
5495 void nwrap_destructor(void)
5500 if (nwrap_main_global != NULL) {
5501 struct nwrap_main *m = nwrap_main_global;
5504 SAFE_FREE(m->libc->fns);
5505 if (m->libc->handle != NULL) {
5506 dlclose(m->libc->handle);
5508 if (m->libc->nsl_handle != NULL) {
5509 dlclose(m->libc->nsl_handle);
5511 if (m->libc->sock_handle != NULL) {
5512 dlclose(m->libc->sock_handle);
5517 for (i = 0; i < m->num_backends; i++) {
5518 struct nwrap_backend *b = &(m->backends[i]);
5520 if (b->so_handle != NULL) {
5521 dlclose(b->so_handle);
5525 SAFE_FREE(m->backends);
5528 if (nwrap_pw_global.cache != NULL) {
5529 struct nwrap_cache *c = nwrap_pw_global.cache;
5531 nwrap_files_cache_unload(c);
5537 SAFE_FREE(nwrap_pw_global.list);
5538 nwrap_pw_global.num = 0;
5541 if (nwrap_gr_global.cache != NULL) {
5542 struct nwrap_cache *c = nwrap_gr_global.cache;
5544 nwrap_files_cache_unload(c);
5550 SAFE_FREE(nwrap_gr_global.list);
5551 nwrap_pw_global.num = 0;
5554 if (nwrap_sp_global.cache != NULL) {
5555 struct nwrap_cache *c = nwrap_sp_global.cache;
5557 nwrap_files_cache_unload(c);
5563 nwrap_sp_global.num = 0;
5566 if (nwrap_he_global.cache != NULL) {
5567 struct nwrap_cache *c = nwrap_he_global.cache;
5569 nwrap_files_cache_unload(c);
5575 nwrap_he_global.num = 0;