2 * Copyright (C) Stefan Metzmacher 2007 <metze@samba.org>
3 * Copyright (C) Guenther Deschner 2009 <gd@samba.org>
4 * Copyright (C) Andreas Schneider 2013 <asn@samba.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the author nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/types.h>
42 #include <sys/socket.h>
59 * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h gives us
60 * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
63 #ifndef _POSIX_PTHREAD_SEMANTICS
64 #define _POSIX_PTHREAD_SEMANTICS
71 #endif /* HAVE_SHADOW_H */
74 #include <arpa/inet.h>
75 #include <netinet/in.h>
79 #if defined(HAVE_NSS_H)
83 typedef enum nss_status NSS_STATUS;
84 #elif defined(HAVE_NSS_COMMON_H)
86 #include <nss_common.h>
87 #include <nss_dbdefs.h>
90 typedef nss_status_t NSS_STATUS;
92 # define NSS_STATUS_SUCCESS NSS_SUCCESS
93 # define NSS_STATUS_NOTFOUND NSS_NOTFOUND
94 # define NSS_STATUS_UNAVAIL NSS_UNAVAIL
95 # define NSS_STATUS_TRYAGAIN NSS_TRYAGAIN
97 # error "No nsswitch support detected"
101 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
109 #define EAI_NODATA EAI_NONAME
112 #ifndef EAI_ADDRFAMILY
113 #define EAI_ADDRFAMILY EAI_FAMILY
117 #define __STRING(x) #x
120 #ifndef __STRINGSTRING
121 #define __STRINGSTRING(x) __STRING(x)
125 #define __LINESTR__ __STRINGSTRING(__LINE__)
129 #define __location__ __FILE__ ":" __LINESTR__
133 #define DNS_NAME_MAX 255
136 /* GCC have printf type attribute check. */
137 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
138 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
140 #define PRINTF_ATTRIBUTE(a,b)
141 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
143 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
144 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
146 #define DESTRUCTOR_ATTRIBUTE
147 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
149 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
152 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
156 #define NWRAP_INET_ADDRSTRLEN INET6_ADDRSTRLEN
158 #define NWRAP_INET_ADDRSTRLEN INET_ADDRSTRLEN
161 #define NWRAP_LOCK(m) do { \
162 pthread_mutex_lock(&( m ## _mutex)); \
165 #define NWRAP_UNLOCK(m) do { \
166 pthread_mutex_unlock(&( m ## _mutex)); \
170 static bool nwrap_initialized = false;
171 static pthread_mutex_t nwrap_initialized_mutex = PTHREAD_MUTEX_INITIALIZER;
173 /* The mutex or accessing the id */
174 static pthread_mutex_t nwrap_global_mutex = PTHREAD_MUTEX_INITIALIZER;
175 static pthread_mutex_t nwrap_gr_global_mutex = PTHREAD_MUTEX_INITIALIZER;
176 static pthread_mutex_t nwrap_he_global_mutex = PTHREAD_MUTEX_INITIALIZER;
177 static pthread_mutex_t nwrap_pw_global_mutex = PTHREAD_MUTEX_INITIALIZER;
178 static pthread_mutex_t nwrap_sp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
180 /* Add new global locks here please */
181 /* Also don't forget to add locks to
182 * nwrap_init() function.
184 # define NWRAP_LOCK_ALL do { \
185 NWRAP_LOCK(nwrap_initialized); \
186 NWRAP_LOCK(nwrap_global); \
187 NWRAP_LOCK(nwrap_gr_global); \
188 NWRAP_LOCK(nwrap_he_global); \
189 NWRAP_LOCK(nwrap_pw_global); \
190 NWRAP_LOCK(nwrap_sp_global); \
193 # define NWRAP_UNLOCK_ALL do {\
194 NWRAP_UNLOCK(nwrap_sp_global); \
195 NWRAP_UNLOCK(nwrap_pw_global); \
196 NWRAP_UNLOCK(nwrap_he_global); \
197 NWRAP_UNLOCK(nwrap_gr_global); \
198 NWRAP_UNLOCK(nwrap_global); \
199 NWRAP_UNLOCK(nwrap_initialized); \
202 static void nwrap_thread_prepare(void)
207 static void nwrap_thread_parent(void)
212 static void nwrap_thread_child(void)
217 enum nwrap_dbglvl_e {
225 # define NWRAP_LOG(...)
228 static void nwrap_log(enum nwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
229 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
231 static void nwrap_log(enum nwrap_dbglvl_e dbglvl,
233 const char *format, ...)
238 unsigned int lvl = 0;
241 d = getenv("NSS_WRAPPER_DEBUGLEVEL");
246 va_start(va, format);
247 vsnprintf(buffer, sizeof(buffer), format, va);
252 case NWRAP_LOG_ERROR:
254 "NWRAP_ERROR(%d) - %s: %s\n",
259 "NWRAP_WARN(%d) - %s: %s\n",
262 case NWRAP_LOG_DEBUG:
264 "NWRAP_DEBUG(%d) - %s: %s\n",
267 case NWRAP_LOG_TRACE:
269 "NWRAP_TRACE(%d) - %s: %s\n",
275 #endif /* NDEBUG NWRAP_LOG */
277 struct nwrap_libc_fns {
278 struct passwd *(*_libc_getpwnam)(const char *name);
279 int (*_libc_getpwnam_r)(const char *name, struct passwd *pwd,
280 char *buf, size_t buflen, struct passwd **result);
281 struct passwd *(*_libc_getpwuid)(uid_t uid);
282 int (*_libc_getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);
283 void (*_libc_setpwent)(void);
284 struct passwd *(*_libc_getpwent)(void);
285 #ifdef HAVE_SOLARIS_GETPWENT_R
286 struct passwd *(*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen);
288 int (*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp);
290 void (*_libc_endpwent)(void);
291 int (*_libc_initgroups)(const char *user, gid_t gid);
292 struct group *(*_libc_getgrnam)(const char *name);
293 int (*_libc_getgrnam_r)(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result);
294 struct group *(*_libc_getgrgid)(gid_t gid);
295 int (*_libc_getgrgid_r)(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result);
296 void (*_libc_setgrent)(void);
297 struct group *(*_libc_getgrent)(void);
298 #ifdef HAVE_SOLARIS_GETGRENT_R
299 struct group *(*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen);
301 int (*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen, struct group **result);
303 void (*_libc_endgrent)(void);
304 int (*_libc_getgrouplist)(const char *user, gid_t group, gid_t *groups, int *ngroups);
306 void (*_libc_sethostent)(int stayopen);
307 struct hostent *(*_libc_gethostent)(void);
308 void (*_libc_endhostent)(void);
310 struct hostent *(*_libc_gethostbyname)(const char *name);
311 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
312 struct hostent *(*_libc_gethostbyname2)(const char *name, int af);
314 struct hostent *(*_libc_gethostbyaddr)(const void *addr, socklen_t len, int type);
316 int (*_libc_getaddrinfo)(const char *node, const char *service,
317 const struct addrinfo *hints,
318 struct addrinfo **res);
319 int (*_libc_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
320 char *host, size_t hostlen,
321 char *serv, size_t servlen,
323 int (*_libc_gethostname)(char *name, size_t len);
324 #ifdef HAVE_GETHOSTBYNAME_R
325 int (*_libc_gethostbyname_r)(const char *name,
327 char *buf, size_t buflen,
328 struct hostent **result, int *h_errnop);
330 #ifdef HAVE_GETHOSTBYADDR_R
331 int (*_libc_gethostbyaddr_r)(const void *addr, socklen_t len, int type,
333 char *buf, size_t buflen,
334 struct hostent **result, int *h_errnop);
338 struct nwrap_module_nss_fns {
339 NSS_STATUS (*_nss_getpwnam_r)(const char *name, struct passwd *result, char *buffer,
340 size_t buflen, int *errnop);
341 NSS_STATUS (*_nss_getpwuid_r)(uid_t uid, struct passwd *result, char *buffer,
342 size_t buflen, int *errnop);
343 NSS_STATUS (*_nss_setpwent)(void);
344 NSS_STATUS (*_nss_getpwent_r)(struct passwd *result, char *buffer,
345 size_t buflen, int *errnop);
346 NSS_STATUS (*_nss_endpwent)(void);
347 NSS_STATUS (*_nss_initgroups)(const char *user, gid_t group, long int *start,
348 long int *size, gid_t **groups, long int limit, int *errnop);
349 NSS_STATUS (*_nss_getgrnam_r)(const char *name, struct group *result, char *buffer,
350 size_t buflen, int *errnop);
351 NSS_STATUS (*_nss_getgrgid_r)(gid_t gid, struct group *result, char *buffer,
352 size_t buflen, int *errnop);
353 NSS_STATUS (*_nss_setgrent)(void);
354 NSS_STATUS (*_nss_getgrent_r)(struct group *result, char *buffer,
355 size_t buflen, int *errnop);
356 NSS_STATUS (*_nss_endgrent)(void);
359 struct nwrap_backend {
363 struct nwrap_ops *ops;
364 struct nwrap_module_nss_fns *fns;
368 struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
370 int (*nw_getpwnam_r)(struct nwrap_backend *b,
371 const char *name, struct passwd *pwdst,
372 char *buf, size_t buflen, struct passwd **pwdstp);
373 struct passwd * (*nw_getpwuid)(struct nwrap_backend *b,
375 int (*nw_getpwuid_r)(struct nwrap_backend *b,
376 uid_t uid, struct passwd *pwdst,
377 char *buf, size_t buflen, struct passwd **pwdstp);
378 void (*nw_setpwent)(struct nwrap_backend *b);
379 struct passwd * (*nw_getpwent)(struct nwrap_backend *b);
380 int (*nw_getpwent_r)(struct nwrap_backend *b,
381 struct passwd *pwdst, char *buf,
382 size_t buflen, struct passwd **pwdstp);
383 void (*nw_endpwent)(struct nwrap_backend *b);
384 int (*nw_initgroups)(struct nwrap_backend *b,
385 const char *user, gid_t group);
386 struct group * (*nw_getgrnam)(struct nwrap_backend *b,
388 int (*nw_getgrnam_r)(struct nwrap_backend *b,
389 const char *name, struct group *grdst,
390 char *buf, size_t buflen, struct group **grdstp);
391 struct group * (*nw_getgrgid)(struct nwrap_backend *b,
393 int (*nw_getgrgid_r)(struct nwrap_backend *b,
394 gid_t gid, struct group *grdst,
395 char *buf, size_t buflen, struct group **grdstp);
396 void (*nw_setgrent)(struct nwrap_backend *b);
397 struct group * (*nw_getgrent)(struct nwrap_backend *b);
398 int (*nw_getgrent_r)(struct nwrap_backend *b,
399 struct group *grdst, char *buf,
400 size_t buflen, struct group **grdstp);
401 void (*nw_endgrent)(struct nwrap_backend *b);
404 /* Public prototypes */
406 bool nss_wrapper_enabled(void);
407 bool nss_wrapper_shadow_enabled(void);
408 bool nss_wrapper_hosts_enabled(void);
410 /* prototypes for files backend */
413 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
415 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
416 const char *name, struct passwd *pwdst,
417 char *buf, size_t buflen, struct passwd **pwdstp);
418 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
420 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
421 uid_t uid, struct passwd *pwdst,
422 char *buf, size_t buflen, struct passwd **pwdstp);
423 static void nwrap_files_setpwent(struct nwrap_backend *b);
424 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b);
425 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
426 struct passwd *pwdst, char *buf,
427 size_t buflen, struct passwd **pwdstp);
428 static void nwrap_files_endpwent(struct nwrap_backend *b);
429 static int nwrap_files_initgroups(struct nwrap_backend *b,
430 const char *user, gid_t group);
431 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
433 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
434 const char *name, struct group *grdst,
435 char *buf, size_t buflen, struct group **grdstp);
436 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
438 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
439 gid_t gid, struct group *grdst,
440 char *buf, size_t buflen, struct group **grdstp);
441 static void nwrap_files_setgrent(struct nwrap_backend *b);
442 static struct group *nwrap_files_getgrent(struct nwrap_backend *b);
443 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
444 struct group *grdst, char *buf,
445 size_t buflen, struct group **grdstp);
446 static void nwrap_files_endgrent(struct nwrap_backend *b);
448 /* prototypes for module backend */
450 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b);
451 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
452 struct passwd *pwdst, char *buf,
453 size_t buflen, struct passwd **pwdstp);
454 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
456 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
457 const char *name, struct passwd *pwdst,
458 char *buf, size_t buflen, struct passwd **pwdstp);
459 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
461 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
462 uid_t uid, struct passwd *pwdst,
463 char *buf, size_t buflen, struct passwd **pwdstp);
464 static void nwrap_module_setpwent(struct nwrap_backend *b);
465 static void nwrap_module_endpwent(struct nwrap_backend *b);
466 static struct group *nwrap_module_getgrent(struct nwrap_backend *b);
467 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
468 struct group *grdst, char *buf,
469 size_t buflen, struct group **grdstp);
470 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
472 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
473 const char *name, struct group *grdst,
474 char *buf, size_t buflen, struct group **grdstp);
475 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
477 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
478 gid_t gid, struct group *grdst,
479 char *buf, size_t buflen, struct group **grdstp);
480 static void nwrap_module_setgrent(struct nwrap_backend *b);
481 static void nwrap_module_endgrent(struct nwrap_backend *b);
482 static int nwrap_module_initgroups(struct nwrap_backend *b,
483 const char *user, gid_t group);
485 struct nwrap_ops nwrap_files_ops = {
486 .nw_getpwnam = nwrap_files_getpwnam,
487 .nw_getpwnam_r = nwrap_files_getpwnam_r,
488 .nw_getpwuid = nwrap_files_getpwuid,
489 .nw_getpwuid_r = nwrap_files_getpwuid_r,
490 .nw_setpwent = nwrap_files_setpwent,
491 .nw_getpwent = nwrap_files_getpwent,
492 .nw_getpwent_r = nwrap_files_getpwent_r,
493 .nw_endpwent = nwrap_files_endpwent,
494 .nw_initgroups = nwrap_files_initgroups,
495 .nw_getgrnam = nwrap_files_getgrnam,
496 .nw_getgrnam_r = nwrap_files_getgrnam_r,
497 .nw_getgrgid = nwrap_files_getgrgid,
498 .nw_getgrgid_r = nwrap_files_getgrgid_r,
499 .nw_setgrent = nwrap_files_setgrent,
500 .nw_getgrent = nwrap_files_getgrent,
501 .nw_getgrent_r = nwrap_files_getgrent_r,
502 .nw_endgrent = nwrap_files_endgrent,
505 struct nwrap_ops nwrap_module_ops = {
506 .nw_getpwnam = nwrap_module_getpwnam,
507 .nw_getpwnam_r = nwrap_module_getpwnam_r,
508 .nw_getpwuid = nwrap_module_getpwuid,
509 .nw_getpwuid_r = nwrap_module_getpwuid_r,
510 .nw_setpwent = nwrap_module_setpwent,
511 .nw_getpwent = nwrap_module_getpwent,
512 .nw_getpwent_r = nwrap_module_getpwent_r,
513 .nw_endpwent = nwrap_module_endpwent,
514 .nw_initgroups = nwrap_module_initgroups,
515 .nw_getgrnam = nwrap_module_getgrnam,
516 .nw_getgrnam_r = nwrap_module_getgrnam_r,
517 .nw_getgrgid = nwrap_module_getgrgid,
518 .nw_getgrgid_r = nwrap_module_getgrgid_r,
519 .nw_setgrent = nwrap_module_setgrent,
520 .nw_getgrent = nwrap_module_getgrent,
521 .nw_getgrent_r = nwrap_module_getgrent_r,
522 .nw_endgrent = nwrap_module_endgrent,
529 struct nwrap_libc_fns *fns;
534 struct nwrap_backend *backends;
535 struct nwrap_libc *libc;
538 static struct nwrap_main *nwrap_main_global;
539 static struct nwrap_main __nwrap_main_global;
544 static int nwrap_convert_he_ai(const struct hostent *he,
546 const struct addrinfo *hints,
547 struct addrinfo **pai,
548 bool skip_canonname);
554 #define DEFAULT_VECTOR_CAPACITY 16
556 struct nwrap_vector {
562 /* Macro returns pointer to first element of vector->items array.
564 * nwrap_vector is used as a memory backend which take care of
565 * memory allocations and other stuff like memory growing.
566 * nwrap_vectors should not be considered as some abstract structures.
567 * On this level, vectors are more handy than direct realloc/malloc
570 * nwrap_vector->items is array inside nwrap_vector which can be
571 * directly pointed by libc structure assembled by cwrap itself.
575 * 1) struct hostent contains char **h_addr_list element.
576 * 2) nwrap_vector holds array of pointers to addresses.
577 * It's easier to use vector to store results of
580 * Now, pretend that cwrap assembled struct hostent and
581 * we need to set h_addr_list to point to nwrap_vector.
582 * Idea behind is to shield users from internal nwrap_vector
584 * (Yes, not fully - array terminated by NULL is needed because
585 * it's result expected by libc function caller.)
591 * struct nwrap_vector *vector = malloc(sizeof(struct nwrap_vector));
592 * ... don't care about failed allocation now ...
594 * ... fill nwrap vector ...
597 * he.h_addr_list = nwrap_vector_head(vector);
600 #define nwrap_vector_head(vect) ((void *)((vect)->items))
602 #define nwrap_vector_foreach(item, vect, iter) \
603 for (iter = 0, (item) = (vect).items == NULL ? NULL : (vect).items[0]; \
605 (item) = (vect).items[++iter])
607 #define nwrap_vector_is_initialized(vector) ((vector)->items != NULL)
609 static inline bool nwrap_vector_init(struct nwrap_vector *const vector)
611 if (vector == NULL) {
615 /* count is initialized by ZERO_STRUCTP */
616 ZERO_STRUCTP(vector);
617 vector->items = malloc(sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
618 if (vector->items == NULL) {
621 vector->capacity = DEFAULT_VECTOR_CAPACITY;
622 memset(vector->items, '\0', sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
627 static bool nwrap_vector_add_item(struct nwrap_vector *vector, void *const item)
629 assert (vector != NULL);
631 if (vector->items == NULL) {
632 nwrap_vector_init(vector);
635 if (vector->count == vector->capacity) {
636 /* Items array _MUST_ be NULL terminated because it's passed
637 * as result to caller which expect NULL terminated array from libc.
639 void **items = realloc(vector->items, sizeof(void *) * ((vector->capacity * 2) + 1));
643 vector->items = items;
645 /* Don't count ending NULL to capacity */
646 vector->capacity *= 2;
649 vector->items[vector->count] = item;
652 vector->items[vector->count] = NULL;
657 static bool nwrap_vector_merge(struct nwrap_vector *dst,
658 struct nwrap_vector *src)
660 void **dst_items = NULL;
663 if (src->count == 0) {
667 count = dst->count + src->count;
669 /* We don't need reallocation if we have enough capacity. */
670 if (src->count > (dst->capacity - dst->count)) {
671 dst_items = (void **)realloc(dst->items, (count + 1) * sizeof(void *));
672 if (dst_items == NULL) {
675 dst->items = dst_items;
676 dst->capacity = count;
679 memcpy((void *)(((long *)dst->items) + dst->count),
681 src->count * sizeof(void *));
694 struct nwrap_vector lines;
696 bool (*parse_line)(struct nwrap_cache *, char *line);
697 void (*unload)(struct nwrap_cache *);
702 struct nwrap_cache *cache;
709 struct nwrap_cache __nwrap_cache_pw;
710 struct nwrap_pw nwrap_pw_global;
712 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
713 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
716 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
718 struct nwrap_cache *cache;
725 struct nwrap_cache __nwrap_cache_sp;
726 struct nwrap_sp nwrap_sp_global;
728 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line);
729 static void nwrap_sp_unload(struct nwrap_cache *nwrap);
730 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
734 struct nwrap_cache *cache;
741 struct nwrap_cache __nwrap_cache_gr;
742 struct nwrap_gr nwrap_gr_global;
745 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line);
746 static void nwrap_he_unload(struct nwrap_cache *nwrap);
748 struct nwrap_addrdata {
749 unsigned char host_addr[16]; /* IPv4 or IPv6 address */
752 static size_t max_hostents = 100;
754 struct nwrap_entdata {
755 struct nwrap_addrdata addr;
758 struct nwrap_vector nwrap_addrdata;
760 ssize_t aliases_count;
762 struct nwrap_entdata *ed_next;
763 struct nwrap_entdata *ed_tail;
767 struct nwrap_cache *cache;
769 struct nwrap_entdata *list;
770 struct nwrap_vector entdata;
776 static struct nwrap_cache __nwrap_cache_he;
777 static struct nwrap_he nwrap_he_global;
780 /*********************************************************
782 *********************************************************/
784 static void nwrap_init(void);
785 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
786 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
787 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
789 /*********************************************************
790 * NWRAP LIBC LOADER FUNCTIONS
791 *********************************************************/
800 static const char *nwrap_str_lib(enum nwrap_lib lib)
807 case NWRAP_LIBSOCKET:
811 /* Compiler would warn us about unhandled enum value if we get here */
816 static void *nwrap_load_lib_handle(enum nwrap_lib lib)
818 int flags = RTLD_LAZY;
823 flags |= RTLD_DEEPBIND;
829 handle = nwrap_main_global->libc->nsl_handle;
830 if (handle == NULL) {
831 for (i = 10; i >= 0; i--) {
832 char soname[256] = {0};
834 snprintf(soname, sizeof(soname), "libnsl.so.%d", i);
835 handle = dlopen(soname, flags);
836 if (handle != NULL) {
841 nwrap_main_global->libc->nsl_handle = handle;
846 case NWRAP_LIBSOCKET:
847 #ifdef HAVE_LIBSOCKET
848 handle = nwrap_main_global->libc->sock_handle;
849 if (handle == NULL) {
850 for (i = 10; i >= 0; i--) {
851 char soname[256] = {0};
853 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
854 handle = dlopen(soname, flags);
855 if (handle != NULL) {
860 nwrap_main_global->libc->sock_handle = handle;
866 handle = nwrap_main_global->libc->handle;
867 if (handle == NULL) {
868 for (i = 10; i >= 0; i--) {
869 char soname[256] = {0};
871 snprintf(soname, sizeof(soname), "libc.so.%d", i);
872 handle = dlopen(soname, flags);
873 if (handle != NULL) {
878 nwrap_main_global->libc->handle = handle;
883 if (handle == NULL) {
885 handle = nwrap_main_global->libc->handle
886 = nwrap_main_global->libc->sock_handle
887 = nwrap_main_global->libc->nsl_handle
890 NWRAP_LOG(NWRAP_LOG_ERROR,
891 "Failed to dlopen library: %s\n",
900 static void *_nwrap_load_lib_function(enum nwrap_lib lib, const char *fn_name)
907 handle = nwrap_load_lib_handle(lib);
909 func = dlsym(handle, fn_name);
911 NWRAP_LOG(NWRAP_LOG_ERROR,
912 "Failed to find %s: %s\n",
917 NWRAP_LOG(NWRAP_LOG_TRACE,
919 fn_name, nwrap_str_lib(lib));
923 #define nwrap_load_lib_function(lib, fn_name) \
924 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
925 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
926 _nwrap_load_lib_function(lib, #fn_name); \
929 /* INTERNAL HELPER FUNCTIONS */
930 static void nwrap_lines_unload(struct nwrap_cache *const nwrap)
934 nwrap_vector_foreach(item, nwrap->lines, p) {
935 /* Maybe some vectors were merged ... */
938 SAFE_FREE(nwrap->lines.items);
939 ZERO_STRUCTP(&nwrap->lines);
945 * Functions expeciall from libc need to be loaded individually, you can't load
946 * all at once or gdb will segfault at startup. The same applies to valgrind and
947 * has probably something todo with with the linker.
948 * So we need load each function at the point it is called the first time.
950 static struct passwd *libc_getpwnam(const char *name)
952 nwrap_load_lib_function(NWRAP_LIBC, getpwnam);
954 return nwrap_main_global->libc->fns->_libc_getpwnam(name);
957 #ifdef HAVE_GETPWNAM_R
958 static int libc_getpwnam_r(const char *name,
962 struct passwd **result)
964 #ifdef HAVE___POSIX_GETPWNAM_R
965 if (nwrap_main_global->libc->fns->_libc_getpwnam_r == NULL) {
966 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwnam_r) =
967 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwnam_r");
970 nwrap_load_lib_function(NWRAP_LIBC, getpwnam_r);
973 return nwrap_main_global->libc->fns->_libc_getpwnam_r(name,
981 static struct passwd *libc_getpwuid(uid_t uid)
983 nwrap_load_lib_function(NWRAP_LIBC, getpwuid);
985 return nwrap_main_global->libc->fns->_libc_getpwuid(uid);
988 #ifdef HAVE_GETPWUID_R
989 static int libc_getpwuid_r(uid_t uid,
993 struct passwd **result)
995 #ifdef HAVE___POSIX_GETPWUID_R
996 if (nwrap_main_global->libc->fns->_libc_getpwuid_r == NULL) {
997 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwuid_r) =
998 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwuid_r");
1001 nwrap_load_lib_function(NWRAP_LIBC, getpwuid_r);
1004 return nwrap_main_global->libc->fns->_libc_getpwuid_r(uid,
1012 static inline void str_tolower(char *dst, char *src)
1014 register char *src_tmp = src;
1015 register char *dst_tmp = dst;
1017 while (*src_tmp != '\0') {
1018 *dst_tmp = tolower(*src_tmp);
1024 static bool str_tolower_copy(char **dst_name, const char *const src_name)
1028 if ((dst_name == NULL) || (src_name == NULL)) {
1032 h_name_lower = strdup(src_name);
1033 if (h_name_lower == NULL) {
1034 NWRAP_LOG(NWRAP_LOG_DEBUG, "Out of memory while strdup");
1038 str_tolower(h_name_lower, h_name_lower);
1039 *dst_name = h_name_lower;
1043 static void libc_setpwent(void)
1045 nwrap_load_lib_function(NWRAP_LIBC, setpwent);
1047 nwrap_main_global->libc->fns->_libc_setpwent();
1050 static struct passwd *libc_getpwent(void)
1052 nwrap_load_lib_function(NWRAP_LIBC, getpwent);
1054 return nwrap_main_global->libc->fns->_libc_getpwent();
1057 #ifdef HAVE_SOLARIS_GETPWENT_R
1058 static struct passwd *libc_getpwent_r(struct passwd *pwdst,
1062 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1064 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1068 #else /* HAVE_SOLARIS_GETPWENT_R */
1069 static int libc_getpwent_r(struct passwd *pwdst,
1072 struct passwd **pwdstp)
1074 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1076 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1081 #endif /* HAVE_SOLARIS_GETPWENT_R */
1083 static void libc_endpwent(void)
1085 nwrap_load_lib_function(NWRAP_LIBC, endpwent);
1087 nwrap_main_global->libc->fns->_libc_endpwent();
1090 static int libc_initgroups(const char *user, gid_t gid)
1092 nwrap_load_lib_function(NWRAP_LIBC, initgroups);
1094 return nwrap_main_global->libc->fns->_libc_initgroups(user, gid);
1097 static struct group *libc_getgrnam(const char *name)
1099 nwrap_load_lib_function(NWRAP_LIBC, getgrnam);
1101 return nwrap_main_global->libc->fns->_libc_getgrnam(name);
1104 #ifdef HAVE_GETGRNAM_R
1105 static int libc_getgrnam_r(const char *name,
1109 struct group **result)
1111 #ifdef HAVE___POSIX_GETGRNAM_R
1112 if (nwrap_main_global->libc->fns->_libc_getgrnam_r == NULL) {
1113 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrnam_r) =
1114 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrnam_r");
1117 nwrap_load_lib_function(NWRAP_LIBC, getgrnam_r);
1120 return nwrap_main_global->libc->fns->_libc_getgrnam_r(name,
1128 static struct group *libc_getgrgid(gid_t gid)
1130 nwrap_load_lib_function(NWRAP_LIBC, getgrgid);
1132 return nwrap_main_global->libc->fns->_libc_getgrgid(gid);
1135 #ifdef HAVE_GETGRGID_R
1136 static int libc_getgrgid_r(gid_t gid,
1140 struct group **result)
1142 #ifdef HAVE___POSIX_GETGRGID_R
1143 if (nwrap_main_global->libc->fns->_libc_getgrgid_r == NULL) {
1144 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrgid_r) =
1145 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrgid_r");
1148 nwrap_load_lib_function(NWRAP_LIBC, getgrgid_r);
1151 return nwrap_main_global->libc->fns->_libc_getgrgid_r(gid,
1159 static void libc_setgrent(void)
1161 nwrap_load_lib_function(NWRAP_LIBC, setgrent);
1163 nwrap_main_global->libc->fns->_libc_setgrent();
1166 static struct group *libc_getgrent(void)
1168 nwrap_load_lib_function(NWRAP_LIBC, getgrent);
1170 return nwrap_main_global->libc->fns->_libc_getgrent();
1173 #ifdef HAVE_GETGRENT_R
1174 #ifdef HAVE_SOLARIS_GETGRENT_R
1175 static struct group *libc_getgrent_r(struct group *group,
1179 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1181 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1185 #else /* !HAVE_SOLARIS_GETGRENT_R */
1186 static int libc_getgrent_r(struct group *group,
1189 struct group **result)
1191 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1193 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1198 #endif /* HAVE_SOLARIS_GETGRENT_R */
1199 #endif /* HAVE_GETGRENT_R */
1201 static void libc_endgrent(void)
1203 nwrap_load_lib_function(NWRAP_LIBC, endgrent);
1205 nwrap_main_global->libc->fns->_libc_endgrent();
1208 #ifdef HAVE_GETGROUPLIST
1209 static int libc_getgrouplist(const char *user,
1214 nwrap_load_lib_function(NWRAP_LIBC, getgrouplist);
1216 return nwrap_main_global->libc->fns->_libc_getgrouplist(user,
1223 static void libc_sethostent(int stayopen)
1225 nwrap_load_lib_function(NWRAP_LIBNSL, sethostent);
1227 nwrap_main_global->libc->fns->_libc_sethostent(stayopen);
1230 static struct hostent *libc_gethostent(void)
1232 nwrap_load_lib_function(NWRAP_LIBNSL, gethostent);
1234 return nwrap_main_global->libc->fns->_libc_gethostent();
1237 static void libc_endhostent(void)
1239 nwrap_load_lib_function(NWRAP_LIBNSL, endhostent);
1241 nwrap_main_global->libc->fns->_libc_endhostent();
1244 static struct hostent *libc_gethostbyname(const char *name)
1246 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname);
1248 return nwrap_main_global->libc->fns->_libc_gethostbyname(name);
1251 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
1252 static struct hostent *libc_gethostbyname2(const char *name, int af)
1254 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname2);
1256 return nwrap_main_global->libc->fns->_libc_gethostbyname2(name, af);
1260 static struct hostent *libc_gethostbyaddr(const void *addr,
1264 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr);
1266 return nwrap_main_global->libc->fns->_libc_gethostbyaddr(addr,
1271 static int libc_gethostname(char *name, size_t len)
1273 nwrap_load_lib_function(NWRAP_LIBNSL, gethostname);
1275 return nwrap_main_global->libc->fns->_libc_gethostname(name, len);
1278 #ifdef HAVE_GETHOSTBYNAME_R
1279 static int libc_gethostbyname_r(const char *name,
1280 struct hostent *ret,
1283 struct hostent **result,
1286 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname_r);
1288 return nwrap_main_global->libc->fns->_libc_gethostbyname_r(name,
1297 #ifdef HAVE_GETHOSTBYADDR_R
1298 static int libc_gethostbyaddr_r(const void *addr,
1301 struct hostent *ret,
1304 struct hostent **result,
1307 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr_r);
1309 return nwrap_main_global->libc->fns->_libc_gethostbyaddr_r(addr,
1320 static int libc_getaddrinfo(const char *node,
1321 const char *service,
1322 const struct addrinfo *hints,
1323 struct addrinfo **res)
1325 nwrap_load_lib_function(NWRAP_LIBSOCKET, getaddrinfo);
1327 return nwrap_main_global->libc->fns->_libc_getaddrinfo(node,
1333 static int libc_getnameinfo(const struct sockaddr *sa,
1341 nwrap_load_lib_function(NWRAP_LIBSOCKET, getnameinfo);
1343 return nwrap_main_global->libc->fns->_libc_getnameinfo(sa,
1352 /*********************************************************
1353 * NWRAP NSS MODULE LOADER FUNCTIONS
1354 *********************************************************/
1356 static void *nwrap_load_module_fn(struct nwrap_backend *b,
1357 const char *fn_name)
1362 if (!b->so_handle) {
1363 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1367 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
1368 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1372 res = dlsym(b->so_handle, s);
1374 NWRAP_LOG(NWRAP_LOG_ERROR,
1375 "Cannot find function %s in %s",
1382 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
1384 struct nwrap_module_nss_fns *fns;
1386 if (!b->so_handle) {
1390 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
1395 *(void **)(&fns->_nss_getpwnam_r) =
1396 nwrap_load_module_fn(b, "getpwnam_r");
1397 *(void **)(&fns->_nss_getpwuid_r) =
1398 nwrap_load_module_fn(b, "getpwuid_r");
1399 *(void **)(&fns->_nss_setpwent) =
1400 nwrap_load_module_fn(b, "setpwent");
1401 *(void **)(&fns->_nss_getpwent_r) =
1402 nwrap_load_module_fn(b, "getpwent_r");
1403 *(void **)(&fns->_nss_endpwent) =
1404 nwrap_load_module_fn(b, "endpwent");
1405 *(void **)(&fns->_nss_initgroups) =
1406 nwrap_load_module_fn(b, "initgroups_dyn");
1407 *(void **)(&fns->_nss_getgrnam_r) =
1408 nwrap_load_module_fn(b, "getgrnam_r");
1409 *(void **)(&fns->_nss_getgrgid_r)=
1410 nwrap_load_module_fn(b, "getgrgid_r");
1411 *(void **)(&fns->_nss_setgrent) =
1412 nwrap_load_module_fn(b, "setgrent");
1413 *(void **)(&fns->_nss_getgrent_r) =
1414 nwrap_load_module_fn(b, "getgrent_r");
1415 *(void **)(&fns->_nss_endgrent) =
1416 nwrap_load_module_fn(b, "endgrent");
1421 static void *nwrap_load_module(const char *so_path)
1425 if (!so_path || !strlen(so_path)) {
1429 h = dlopen(so_path, RTLD_LAZY);
1431 NWRAP_LOG(NWRAP_LOG_ERROR,
1432 "Cannot open shared library %s",
1440 static bool nwrap_module_init(const char *name,
1441 struct nwrap_ops *ops,
1442 const char *so_path,
1444 struct nwrap_backend **backends)
1446 struct nwrap_backend *b;
1448 *backends = (struct nwrap_backend *)realloc(*backends,
1449 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
1451 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1455 b = &((*backends)[*num_backends]);
1459 b->so_path = so_path;
1461 if (so_path != NULL) {
1462 b->so_handle = nwrap_load_module(so_path);
1463 b->fns = nwrap_load_module_fns(b);
1464 if (b->fns == NULL) {
1468 b->so_handle = NULL;
1477 static void nwrap_libc_init(struct nwrap_main *r)
1479 r->libc = malloc(sizeof(struct nwrap_libc));
1480 if (r->libc == NULL) {
1481 printf("Failed to allocate memory for libc");
1484 ZERO_STRUCTP(r->libc);
1486 r->libc->fns = malloc(sizeof(struct nwrap_libc_fns));
1487 if (r->libc->fns == NULL) {
1488 printf("Failed to allocate memory for libc functions");
1491 ZERO_STRUCTP(r->libc->fns);
1494 static void nwrap_backend_init(struct nwrap_main *r)
1496 const char *module_so_path = getenv("NSS_WRAPPER_MODULE_SO_PATH");
1497 const char *module_fn_name = getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1499 r->num_backends = 0;
1502 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1505 NWRAP_LOG(NWRAP_LOG_ERROR,
1506 "Failed to initialize 'files' backend");
1510 if (module_so_path != NULL &&
1511 module_so_path[0] != '\0' &&
1512 module_fn_name != NULL &&
1513 module_fn_name[0] != '\0') {
1514 if (!nwrap_module_init(module_fn_name,
1519 NWRAP_LOG(NWRAP_LOG_ERROR,
1520 "Failed to initialize '%s' backend",
1527 static void nwrap_init(void)
1531 size_t max_hostents_tmp;
1533 NWRAP_LOCK(nwrap_initialized);
1534 if (nwrap_initialized) {
1535 NWRAP_UNLOCK(nwrap_initialized);
1540 * Still holding nwrap_initialized lock here.
1541 * We don't use NWRAP_(UN)LOCK_ALL macros here because we
1542 * want to avoid overhead when other threads do their job.
1544 NWRAP_LOCK(nwrap_global);
1545 NWRAP_LOCK(nwrap_gr_global);
1546 NWRAP_LOCK(nwrap_he_global);
1547 NWRAP_LOCK(nwrap_pw_global);
1548 NWRAP_LOCK(nwrap_sp_global);
1550 nwrap_initialized = true;
1552 /* Initialize pthread_atfork handlers */
1553 pthread_atfork(&nwrap_thread_prepare, &nwrap_thread_parent,
1554 &nwrap_thread_child);
1556 env = getenv("NSS_WRAPPER_MAX_HOSTENTS");
1558 max_hostents_tmp = (size_t)strtol(env, &endptr, 10);
1559 if (((env != '\0') && (endptr == '\0')) ||
1560 (max_hostents_tmp == 0)) {
1561 NWRAP_LOG(NWRAP_LOG_DEBUG,
1562 "Error parsing NSS_WRAPPER_MAX_HOSTENTS "
1563 "value or value is too small. "
1564 "Using default value: %lu.",
1567 max_hostents = max_hostents_tmp;
1570 /* Initialize hash table */
1571 NWRAP_LOG(NWRAP_LOG_DEBUG,
1572 "Initializing hash table of size %lu items.", max_hostents);
1573 if (hcreate(max_hostents) == 0) {
1574 NWRAP_LOG(NWRAP_LOG_ERROR,
1575 "Failed to initialize hash table");
1579 nwrap_main_global = &__nwrap_main_global;
1581 nwrap_libc_init(nwrap_main_global);
1583 nwrap_backend_init(nwrap_main_global);
1586 nwrap_pw_global.cache = &__nwrap_cache_pw;
1588 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
1589 nwrap_pw_global.cache->fp = NULL;
1590 nwrap_pw_global.cache->fd = -1;
1591 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
1592 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
1593 nwrap_pw_global.cache->unload = nwrap_pw_unload;
1596 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1597 nwrap_sp_global.cache = &__nwrap_cache_sp;
1599 nwrap_sp_global.cache->path = getenv("NSS_WRAPPER_SHADOW");
1600 nwrap_sp_global.cache->fp = NULL;
1601 nwrap_sp_global.cache->fd = -1;
1602 nwrap_sp_global.cache->private_data = &nwrap_sp_global;
1603 nwrap_sp_global.cache->parse_line = nwrap_sp_parse_line;
1604 nwrap_sp_global.cache->unload = nwrap_sp_unload;
1605 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1608 nwrap_gr_global.cache = &__nwrap_cache_gr;
1610 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
1611 nwrap_gr_global.cache->fp = NULL;
1612 nwrap_gr_global.cache->fd = -1;
1613 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
1614 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
1615 nwrap_gr_global.cache->unload = nwrap_gr_unload;
1618 nwrap_he_global.cache = &__nwrap_cache_he;
1620 nwrap_he_global.cache->path = getenv("NSS_WRAPPER_HOSTS");
1621 nwrap_he_global.cache->fp = NULL;
1622 nwrap_he_global.cache->fd = -1;
1623 nwrap_he_global.cache->private_data = &nwrap_he_global;
1624 nwrap_he_global.cache->parse_line = nwrap_he_parse_line;
1625 nwrap_he_global.cache->unload = nwrap_he_unload;
1628 /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1632 bool nss_wrapper_enabled(void)
1636 if (nwrap_pw_global.cache->path == NULL ||
1637 nwrap_pw_global.cache->path[0] == '\0') {
1640 if (nwrap_gr_global.cache->path == NULL ||
1641 nwrap_gr_global.cache->path[0] == '\0') {
1648 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1649 bool nss_wrapper_shadow_enabled(void)
1653 if (nwrap_sp_global.cache->path == NULL ||
1654 nwrap_sp_global.cache->path[0] == '\0') {
1660 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1662 bool nss_wrapper_hosts_enabled(void)
1666 if (nwrap_he_global.cache->path == NULL ||
1667 nwrap_he_global.cache->path[0] == '\0') {
1674 static bool nwrap_hostname_enabled(void)
1678 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
1685 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
1689 /* Unused but getline needs it */
1693 if (nwrap->st.st_size == 0) {
1694 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
1698 /* Support for 32-bit system I guess */
1699 if (nwrap->st.st_size > INT32_MAX) {
1700 NWRAP_LOG(NWRAP_LOG_ERROR,
1701 "Size[%u] larger than INT32_MAX",
1702 (unsigned)nwrap->st.st_size);
1709 n = getline(&line, &len, nwrap->fp);
1712 if (feof(nwrap->fp)) {
1716 NWRAP_LOG(NWRAP_LOG_ERROR,
1717 "Unable to read line from file: %s",
1722 if (line[n - 1] == '\n') {
1726 if (line[0] == '\0') {
1731 ok = nwrap->parse_line(nwrap, line);
1733 NWRAP_LOG(NWRAP_LOG_ERROR,
1734 "Unable to parse line file: %s",
1740 /* Line is parsed without issues so add it to list */
1741 ok = nwrap_vector_add_item(&(nwrap->lines), (void *const) line);
1743 NWRAP_LOG(NWRAP_LOG_ERROR,
1744 "Unable to add line to vector");
1748 /* This forces getline to allocate new memory for line. */
1750 } while (!feof(nwrap->fp));
1755 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
1757 nwrap->unload(nwrap);
1759 nwrap_lines_unload(nwrap);
1762 static void nwrap_files_cache_reload(struct nwrap_cache *nwrap)
1767 bool retried = false;
1769 assert(nwrap != NULL);
1772 if (nwrap->fd < 0) {
1773 nwrap->fp = fopen(nwrap->path, "re");
1774 if (nwrap->fp == NULL) {
1776 NWRAP_LOG(NWRAP_LOG_ERROR,
1777 "Unable to open '%s' readonly %d:%s",
1778 nwrap->path, nwrap->fd,
1783 nwrap->fd = fileno(nwrap->fp);
1784 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
1787 ret = fstat(nwrap->fd, &st);
1789 NWRAP_LOG(NWRAP_LOG_ERROR,
1790 "fstat(%s) - %d:%s",
1800 if (retried == false && st.st_nlink == 0) {
1801 /* maybe someone has replaced the file... */
1802 NWRAP_LOG(NWRAP_LOG_TRACE,
1803 "st_nlink == 0, reopen %s",
1806 memset(&nwrap->st, 0, sizeof(nwrap->st));
1813 if (st.st_mtime == nwrap->st.st_mtime) {
1814 NWRAP_LOG(NWRAP_LOG_TRACE,
1815 "st_mtime[%u] hasn't changed, skip reload",
1816 (unsigned)st.st_mtime);
1820 NWRAP_LOG(NWRAP_LOG_TRACE,
1821 "st_mtime has changed [%u] => [%u], start reload",
1822 (unsigned)st.st_mtime,
1823 (unsigned)nwrap->st.st_mtime);
1827 nwrap_files_cache_unload(nwrap);
1829 ok = nwrap_parse_file(nwrap);
1831 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
1832 nwrap_files_cache_unload(nwrap);
1835 NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
1839 * the caller has to call nwrap_unload() on failure
1841 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
1843 struct nwrap_pw *nwrap_pw;
1850 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1852 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
1853 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
1855 NWRAP_LOG(NWRAP_LOG_ERROR,
1856 "realloc(%u) failed",
1857 (unsigned)list_size);
1860 nwrap_pw->list = pw;
1862 pw = &nwrap_pw->list[nwrap_pw->num];
1869 NWRAP_LOG(NWRAP_LOG_ERROR,
1870 "Invalid line[%s]: '%s'",
1880 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
1885 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1893 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
1898 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1904 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
1906 NWRAP_LOG(NWRAP_LOG_ERROR,
1907 "Invalid line[%s]: '%s' - %s",
1908 line, c, strerror(errno));
1912 NWRAP_LOG(NWRAP_LOG_ERROR,
1913 "Invalid line[%s]: '%s' - %s",
1914 line, c, strerror(errno));
1918 NWRAP_LOG(NWRAP_LOG_ERROR,
1919 "Invalid line[%s]: '%s' - %s",
1920 line, c, strerror(errno));
1925 NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
1930 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1936 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
1938 NWRAP_LOG(NWRAP_LOG_ERROR,
1939 "Invalid line[%s]: '%s' - %s",
1940 line, c, strerror(errno));
1944 NWRAP_LOG(NWRAP_LOG_ERROR,
1945 "Invalid line[%s]: '%s' - %s",
1946 line, c, strerror(errno));
1950 NWRAP_LOG(NWRAP_LOG_ERROR,
1951 "Invalid line[%s]: '%s' - %s",
1952 line, c, strerror(errno));
1957 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]\n", pw->pw_gid);
1962 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
1970 NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
1975 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
1983 NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
1987 NWRAP_LOG(NWRAP_LOG_TRACE, "shell[%s]", pw->pw_shell);
1989 NWRAP_LOG(NWRAP_LOG_DEBUG,
1990 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1991 pw->pw_name, pw->pw_passwd,
1992 pw->pw_uid, pw->pw_gid,
1993 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
1999 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
2001 struct nwrap_pw *nwrap_pw;
2002 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
2004 SAFE_FREE(nwrap_pw->list);
2009 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
2010 char *buf, size_t buflen, struct passwd **dstp)
2016 first = src->pw_name;
2018 last = src->pw_shell;
2019 while (*last) last++;
2021 ofs = PTR_DIFF(last + 1, first);
2023 if (ofs > (off_t) buflen) {
2027 memcpy(buf, first, ofs);
2029 ofs = PTR_DIFF(src->pw_name, first);
2030 dst->pw_name = buf + ofs;
2031 ofs = PTR_DIFF(src->pw_passwd, first);
2032 dst->pw_passwd = buf + ofs;
2033 dst->pw_uid = src->pw_uid;
2034 dst->pw_gid = src->pw_gid;
2035 ofs = PTR_DIFF(src->pw_gecos, first);
2036 dst->pw_gecos = buf + ofs;
2037 ofs = PTR_DIFF(src->pw_dir, first);
2038 dst->pw_dir = buf + ofs;
2039 ofs = PTR_DIFF(src->pw_shell, first);
2040 dst->pw_shell = buf + ofs;
2049 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2050 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line)
2052 struct nwrap_sp *nwrap_sp;
2059 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2061 list_size = sizeof(*nwrap_sp->list) * (nwrap_sp->num+1);
2062 sp = (struct spwd *)realloc(nwrap_sp->list, list_size);
2064 NWRAP_LOG(NWRAP_LOG_ERROR,
2065 "realloc(%u) failed",
2066 (unsigned)list_size);
2069 nwrap_sp->list = sp;
2071 sp = &nwrap_sp->list[nwrap_sp->num];
2078 NWRAP_LOG(NWRAP_LOG_ERROR,
2079 "name -- Invalid line[%s]: '%s'",
2089 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", sp->sp_namp);
2094 NWRAP_LOG(NWRAP_LOG_ERROR,
2095 "pwd -- Invalid line[%s]: '%s'",
2112 NWRAP_LOG(NWRAP_LOG_ERROR,
2113 "lstchg -- Invalid line[%s]: '%s'",
2120 sp->sp_lstchg = strtol(c, &e, 10);
2122 NWRAP_LOG(NWRAP_LOG_ERROR,
2123 "lstchg -- Invalid line[%s]: '%s' - %s",
2124 line, c, strerror(errno));
2128 NWRAP_LOG(NWRAP_LOG_ERROR,
2129 "lstchg -- Invalid line[%s]: '%s' - %s",
2130 line, c, strerror(errno));
2134 NWRAP_LOG(NWRAP_LOG_ERROR,
2135 "lstchg -- Invalid line[%s]: '%s' - %s",
2136 line, c, strerror(errno));
2149 NWRAP_LOG(NWRAP_LOG_ERROR,
2150 "min -- Invalid line[%s]: '%s'",
2157 sp->sp_min = strtol(c, &e, 10);
2159 NWRAP_LOG(NWRAP_LOG_ERROR,
2160 "min -- Invalid line[%s]: '%s' - %s",
2161 line, c, strerror(errno));
2165 NWRAP_LOG(NWRAP_LOG_ERROR,
2166 "min -- Invalid line[%s]: '%s' - %s",
2167 line, c, strerror(errno));
2171 NWRAP_LOG(NWRAP_LOG_ERROR,
2172 "min -- Invalid line[%s]: '%s' - %s",
2173 line, c, strerror(errno));
2186 NWRAP_LOG(NWRAP_LOG_ERROR,
2187 "max -- Invalid line[%s]: '%s'",
2194 sp->sp_max = strtol(c, &e, 10);
2196 NWRAP_LOG(NWRAP_LOG_ERROR,
2197 "max -- Invalid line[%s]: '%s' - %s",
2198 line, c, strerror(errno));
2202 NWRAP_LOG(NWRAP_LOG_ERROR,
2203 "max -- Invalid line[%s]: '%s' - %s",
2204 line, c, strerror(errno));
2208 NWRAP_LOG(NWRAP_LOG_ERROR,
2209 "max -- Invalid line[%s]: '%s' - %s",
2210 line, c, strerror(errno));
2223 NWRAP_LOG(NWRAP_LOG_ERROR,
2224 "warn -- Invalid line[%s]: '%s'",
2231 sp->sp_warn = strtol(c, &e, 10);
2233 NWRAP_LOG(NWRAP_LOG_ERROR,
2234 "warn -- Invalid line[%s]: '%s' - %s",
2235 line, c, strerror(errno));
2239 NWRAP_LOG(NWRAP_LOG_ERROR,
2240 "warn -- Invalid line[%s]: '%s' - %s",
2241 line, c, strerror(errno));
2245 NWRAP_LOG(NWRAP_LOG_ERROR,
2246 "warn -- Invalid line[%s]: '%s' - %s",
2247 line, c, strerror(errno));
2260 NWRAP_LOG(NWRAP_LOG_ERROR,
2261 "inact -- Invalid line[%s]: '%s'",
2268 sp->sp_inact = strtol(c, &e, 10);
2270 NWRAP_LOG(NWRAP_LOG_ERROR,
2271 "inact -- Invalid line[%s]: '%s' - %s",
2272 line, c, strerror(errno));
2276 NWRAP_LOG(NWRAP_LOG_ERROR,
2277 "inact -- Invalid line[%s]: '%s' - %s",
2278 line, c, strerror(errno));
2282 NWRAP_LOG(NWRAP_LOG_ERROR,
2283 "inact -- Invalid line[%s]: '%s' - %s",
2284 line, c, strerror(errno));
2297 NWRAP_LOG(NWRAP_LOG_ERROR,
2298 "expire -- Invalid line[%s]: '%s'",
2305 sp->sp_expire = strtol(c, &e, 10);
2307 NWRAP_LOG(NWRAP_LOG_ERROR,
2308 "expire -- Invalid line[%s]: '%s' - %s",
2309 line, c, strerror(errno));
2313 NWRAP_LOG(NWRAP_LOG_ERROR,
2314 "expire -- Invalid line[%s]: '%s' - %s",
2315 line, c, strerror(errno));
2319 NWRAP_LOG(NWRAP_LOG_ERROR,
2320 "expire -- Invalid line[%s]: '%s' - %s",
2321 line, c, strerror(errno));
2331 static void nwrap_sp_unload(struct nwrap_cache *nwrap)
2333 struct nwrap_sp *nwrap_sp;
2334 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2336 SAFE_FREE(nwrap_sp->list);
2340 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2343 * the caller has to call nwrap_unload() on failure
2345 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
2347 struct nwrap_gr *nwrap_gr;
2355 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2357 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
2358 gr = (struct group *)realloc(nwrap_gr->list, list_size);
2360 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
2363 nwrap_gr->list = gr;
2365 gr = &nwrap_gr->list[nwrap_gr->num];
2372 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2380 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
2385 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2393 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
2398 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2404 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
2406 NWRAP_LOG(NWRAP_LOG_ERROR,
2407 "Invalid line[%s]: '%s' - %s",
2408 line, c, strerror(errno));
2412 NWRAP_LOG(NWRAP_LOG_ERROR,
2413 "Invalid line[%s]: '%s' - %s",
2414 line, c, strerror(errno));
2418 NWRAP_LOG(NWRAP_LOG_ERROR,
2419 "Invalid line[%s]: '%s' - %s",
2420 line, c, strerror(errno));
2425 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
2428 gr->gr_mem = (char **)malloc(sizeof(char *));
2430 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
2433 gr->gr_mem[0] = NULL;
2435 for(nummem=0; p; nummem++) {
2445 if (strlen(c) == 0) {
2449 m_size = sizeof(char *) * (nummem+2);
2450 m = (char **)realloc(gr->gr_mem, m_size);
2452 NWRAP_LOG(NWRAP_LOG_ERROR,
2453 "realloc(%zd) failed",
2458 gr->gr_mem[nummem] = c;
2459 gr->gr_mem[nummem+1] = NULL;
2461 NWRAP_LOG(NWRAP_LOG_TRACE,
2463 nummem, gr->gr_mem[nummem]);
2466 NWRAP_LOG(NWRAP_LOG_DEBUG,
2467 "Added group[%s:%s:%u:] with %u members",
2468 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem);
2474 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
2477 struct nwrap_gr *nwrap_gr;
2478 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2480 if (nwrap_gr->list) {
2481 for (i=0; i < nwrap_gr->num; i++) {
2482 SAFE_FREE(nwrap_gr->list[i].gr_mem);
2484 SAFE_FREE(nwrap_gr->list);
2491 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
2492 char *buf, size_t buflen, struct group **dstp)
2502 first = src->gr_name;
2504 lastm = src->gr_mem;
2511 last = src->gr_passwd;
2513 while (*last) last++;
2515 ofsb = PTR_DIFF(last + 1, first);
2516 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
2518 if ((ofsb + ofsm) > (off_t) buflen) {
2522 memcpy(buf, first, ofsb);
2523 memcpy(buf + ofsb, src->gr_mem, ofsm);
2525 ofs = PTR_DIFF(src->gr_name, first);
2526 dst->gr_name = buf + ofs;
2527 ofs = PTR_DIFF(src->gr_passwd, first);
2528 dst->gr_passwd = buf + ofs;
2529 dst->gr_gid = src->gr_gid;
2531 dst->gr_mem = (char **)(buf + ofsb);
2532 for (i=0; src->gr_mem[i]; i++) {
2533 ofs = PTR_DIFF(src->gr_mem[i], first);
2534 dst->gr_mem[i] = buf + ofs;
2544 static bool nwrap_add_ai(char *const ip_addr, struct nwrap_entdata *const ed)
2552 p = hsearch(e, ENTER);
2554 NWRAP_LOG(NWRAP_LOG_DEBUG, "Hash table is full");
2562 static bool nwrap_add_hname_add_new(char *const h_name,
2563 struct nwrap_entdata *const ed)
2565 /* No element found.. inventarize new item */
2570 e.data = (void *)ed;
2574 p = hsearch(e, ENTER);
2576 NWRAP_LOG(NWRAP_LOG_DEBUG, "Hash table is full!");
2583 static void nwrap_add_hname_add_to_existing(struct nwrap_entdata *const ed,
2584 struct nwrap_entdata *const ed_dst)
2586 if (ed_dst->ed_tail != NULL) {
2587 ed_dst->ed_tail->ed_next = ed;
2588 if (ed_dst->ed_tail != ed) {
2589 ed_dst->ed_tail = ed;
2593 ed_dst->ed_tail = ed;
2597 static bool nwrap_add_hname_alias(const char *const h_name_a,
2598 struct nwrap_entdata *const ed)
2600 /* One of argument 'h_hame_a' are "optional" */
2601 char *const h_name = (char *const) ((h_name_a == NULL) ? ed->ht.h_name : h_name_a);
2605 /* Maybe it's little bit late ... */
2607 assert(h_name != NULL);
2611 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
2612 p = hsearch(e, FIND);
2614 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", h_name);
2615 /* Just add alias and don't mess with metadata */
2616 nwrap_add_hname_add_new(h_name, ed);
2618 /* Element found. Add them to end of list */
2619 struct nwrap_entdata *ed_dst = (struct nwrap_entdata *)p->data;
2621 assert(p->data != NULL);
2622 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s found. Add record to list.", h_name);
2623 nwrap_add_hname_add_to_existing(ed, ed_dst);
2629 static bool nwrap_add_hname(const char *const h_name_a,
2630 struct nwrap_entdata *const ed)
2632 /* One of argument 'h_hame_a' are "optional" */
2633 char *const h_name = (char *const) ((h_name_a == NULL) ? ed->ht.h_name : h_name_a);
2639 /* Maybe it's little bit late ... */
2641 assert(h_name != NULL);
2645 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
2646 p = hsearch(e, FIND);
2648 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", h_name);
2649 /* Just add alias and don't mess with metadata */
2650 nwrap_add_hname_add_new(h_name, ed);
2652 if (ed->ed_tail == NULL) {
2656 /* Element found. Add them to end of list */
2657 struct nwrap_entdata *ed_dst = (struct nwrap_entdata *)p->data;
2659 assert(p->data != NULL);
2660 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s found. Add record to list.", h_name);
2661 nwrap_add_hname_add_to_existing(ed, ed_dst);
2664 /* Return true when list of aliases is empty */
2665 if (ed->ht.h_aliases == NULL) {
2669 /* Itemize aliases */
2670 for (i = 0; ed->ht.h_aliases[i] != NULL; ++i) {
2671 h_name_alias = ed->ht.h_aliases[i];
2672 assert(h_name_alias != NULL);
2674 NWRAP_LOG(NWRAP_LOG_DEBUG, "Add alias: %s", h_name_alias);
2676 if (!nwrap_add_hname_alias(h_name_alias, ed)) {
2677 NWRAP_LOG(NWRAP_LOG_DEBUG,
2678 "Unable to add alias: %s", h_name_alias);
2685 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line)
2687 struct nwrap_he *nwrap_he = (struct nwrap_he *)nwrap->private_data;
2688 bool do_aliases = true;
2689 ssize_t aliases_count = 0;
2696 struct nwrap_entdata *ed = (struct nwrap_entdata *)
2697 malloc(sizeof(struct nwrap_entdata));
2699 NWRAP_LOG(NWRAP_LOG_ERROR,
2700 "Unable to allocate memory for nwrap_entdata");
2711 /* Walk to first char */
2712 for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
2714 NWRAP_LOG(NWRAP_LOG_ERROR,
2715 "Invalid line[%s]: '%s'",
2722 for (i = p; !isspace((int)*p); p++) {
2724 NWRAP_LOG(NWRAP_LOG_ERROR,
2725 "Invalid line[%s]: '%s'",
2734 if (inet_pton(AF_INET, i, ed->addr.host_addr)) {
2735 ed->ht.h_addrtype = AF_INET;
2736 ed->ht.h_length = 4;
2738 } else if (inet_pton(AF_INET6, i, ed->addr.host_addr)) {
2739 ed->ht.h_addrtype = AF_INET6;
2740 ed->ht.h_length = 16;
2743 NWRAP_LOG(NWRAP_LOG_ERROR,
2744 "Invalid line[%s]: '%s'",
2752 nwrap_vector_add_item(&(ed->nwrap_addrdata),
2753 (void *const)ed->addr.host_addr);
2754 ed->ht.h_addr_list = nwrap_vector_head(&ed->nwrap_addrdata);
2762 /* Walk to first char */
2763 for (n = p; *p != '_' && !isalnum((int) *p); p++) {
2765 NWRAP_LOG(NWRAP_LOG_ERROR,
2766 "Invalid line[%s]: '%s'",
2774 for (n = p; !isspace((int)*p); p++) {
2783 /* Convert to lowercase. This operate on same memory region */
2787 /* glib's getent always dereferences he->h_aliases */
2788 ed->ht.h_aliases = malloc(sizeof(char *));
2789 if (ed->ht.h_aliases == NULL) {
2793 ed->ht.h_aliases[0] = NULL;
2798 while (do_aliases) {
2804 /* Walk to first char */
2805 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
2811 /* Only trailing spaces are left */
2816 for (a = p; !isspace((int)*p); p++) {
2825 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
2826 if (aliases == NULL) {
2830 ed->ht.h_aliases = aliases;
2833 aliases[aliases_count] = a;
2834 aliases[aliases_count + 1] = NULL;
2839 nwrap_vector_add_item(&(nwrap_he->entdata), (void *const)ed);
2841 ed->aliases_count = aliases_count;
2842 /* Inventarize item */
2843 nwrap_add_hname(NULL, ed);
2844 nwrap_add_ai(ip, ed);
2850 static void nwrap_he_unload(struct nwrap_cache *nwrap)
2852 struct nwrap_he *nwrap_he =
2853 (struct nwrap_he *)nwrap->private_data;
2854 struct nwrap_entdata *ed;
2857 nwrap_vector_foreach (ed, nwrap_he->entdata, i)
2859 SAFE_FREE(ed->nwrap_addrdata.items);
2860 SAFE_FREE(ed->ht.h_aliases);
2863 SAFE_FREE(nwrap_he->entdata.items);
2864 nwrap_he->entdata.count = nwrap_he->entdata.capacity = 0;
2871 /* user functions */
2872 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
2877 (void) b; /* unused */
2879 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
2881 nwrap_files_cache_reload(nwrap_pw_global.cache);
2883 for (i=0; i<nwrap_pw_global.num; i++) {
2884 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
2885 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
2886 return &nwrap_pw_global.list[i];
2888 NWRAP_LOG(NWRAP_LOG_DEBUG,
2889 "user[%s] does not match [%s]",
2891 nwrap_pw_global.list[i].pw_name);
2894 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
2900 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
2901 const char *name, struct passwd *pwdst,
2902 char *buf, size_t buflen, struct passwd **pwdstp)
2906 pw = nwrap_files_getpwnam(b, name);
2914 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2917 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
2922 (void) b; /* unused */
2924 nwrap_files_cache_reload(nwrap_pw_global.cache);
2926 for (i=0; i<nwrap_pw_global.num; i++) {
2927 if (nwrap_pw_global.list[i].pw_uid == uid) {
2928 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] found", uid);
2929 return &nwrap_pw_global.list[i];
2931 NWRAP_LOG(NWRAP_LOG_DEBUG,
2932 "uid[%u] does not match [%u]",
2934 nwrap_pw_global.list[i].pw_uid);
2937 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
2943 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
2944 uid_t uid, struct passwd *pwdst,
2945 char *buf, size_t buflen, struct passwd **pwdstp)
2949 pw = nwrap_files_getpwuid(b, uid);
2957 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2960 /* user enum functions */
2961 static void nwrap_files_setpwent(struct nwrap_backend *b)
2963 (void) b; /* unused */
2965 nwrap_pw_global.idx = 0;
2968 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
2972 (void) b; /* unused */
2974 if (nwrap_pw_global.idx == 0) {
2975 nwrap_files_cache_reload(nwrap_pw_global.cache);
2978 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
2983 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
2985 NWRAP_LOG(NWRAP_LOG_DEBUG,
2986 "return user[%s] uid[%u]",
2987 pw->pw_name, pw->pw_uid);
2992 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
2993 struct passwd *pwdst, char *buf,
2994 size_t buflen, struct passwd **pwdstp)
2998 pw = nwrap_files_getpwent(b);
3006 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3009 static void nwrap_files_endpwent(struct nwrap_backend *b)
3011 (void) b; /* unused */
3013 nwrap_pw_global.idx = 0;
3018 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
3020 #ifdef HAVE_SETSPENT
3021 static void nwrap_files_setspent(void)
3023 nwrap_sp_global.idx = 0;
3026 static struct spwd *nwrap_files_getspent(void)
3030 if (nwrap_sp_global.idx == 0) {
3031 nwrap_files_cache_reload(nwrap_sp_global.cache);
3034 if (nwrap_sp_global.idx >= nwrap_sp_global.num) {
3039 sp = &nwrap_sp_global.list[nwrap_sp_global.idx++];
3041 NWRAP_LOG(NWRAP_LOG_DEBUG,
3048 static void nwrap_files_endspent(void)
3050 nwrap_sp_global.idx = 0;
3052 #endif /* HAVE_SETSPENT */
3054 static struct spwd *nwrap_files_getspnam(const char *name)
3058 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
3060 nwrap_files_cache_reload(nwrap_sp_global.cache);
3062 for (i=0; i<nwrap_sp_global.num; i++) {
3063 if (strcmp(nwrap_sp_global.list[i].sp_namp, name) == 0) {
3064 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
3065 return &nwrap_sp_global.list[i];
3067 NWRAP_LOG(NWRAP_LOG_DEBUG,
3068 "user[%s] does not match [%s]",
3070 nwrap_sp_global.list[i].sp_namp);
3073 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3078 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3080 /* misc functions */
3081 static int nwrap_files_initgroups(struct nwrap_backend *b,
3090 groups = (gid_t *)malloc(size * sizeof(gid_t));
3091 if (groups == NULL) {
3092 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3098 nwrap_files_setgrent(b);
3099 while ((grp = nwrap_files_getgrent(b)) != NULL) {
3102 NWRAP_LOG(NWRAP_LOG_DEBUG,
3103 "Inspecting %s for group membership",
3106 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
3107 if (group != grp->gr_gid &&
3108 (strcmp(user, grp->gr_mem[i]) == 0)) {
3109 NWRAP_LOG(NWRAP_LOG_DEBUG,
3110 "%s is member of %s",
3114 groups = (gid_t *)realloc(groups,
3115 (size + 1) * sizeof(gid_t));
3116 if (groups == NULL) {
3117 NWRAP_LOG(NWRAP_LOG_ERROR,
3123 groups[size] = grp->gr_gid;
3129 nwrap_files_endgrent(b);
3131 NWRAP_LOG(NWRAP_LOG_DEBUG,
3132 "%s is member of %d groups",
3135 /* This really only works if uid_wrapper is loaded */
3136 rc = setgroups(size, groups);
3143 /* group functions */
3144 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
3149 (void) b; /* unused */
3151 nwrap_files_cache_reload(nwrap_gr_global.cache);
3153 for (i=0; i<nwrap_gr_global.num; i++) {
3154 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
3155 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] found", name);
3156 return &nwrap_gr_global.list[i];
3158 NWRAP_LOG(NWRAP_LOG_DEBUG,
3159 "group[%s] does not match [%s]",
3161 nwrap_gr_global.list[i].gr_name);
3164 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
3170 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
3171 const char *name, struct group *grdst,
3172 char *buf, size_t buflen, struct group **grdstp)
3176 gr = nwrap_files_getgrnam(b, name);
3184 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3187 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
3192 (void) b; /* unused */
3194 nwrap_files_cache_reload(nwrap_gr_global.cache);
3196 for (i=0; i<nwrap_gr_global.num; i++) {
3197 if (nwrap_gr_global.list[i].gr_gid == gid) {
3198 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] found", gid);
3199 return &nwrap_gr_global.list[i];
3201 NWRAP_LOG(NWRAP_LOG_DEBUG,
3202 "gid[%u] does not match [%u]",
3204 nwrap_gr_global.list[i].gr_gid);
3207 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
3213 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
3214 gid_t gid, struct group *grdst,
3215 char *buf, size_t buflen, struct group **grdstp)
3219 gr = nwrap_files_getgrgid(b, gid);
3227 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3230 /* group enum functions */
3231 static void nwrap_files_setgrent(struct nwrap_backend *b)
3233 (void) b; /* unused */
3235 nwrap_gr_global.idx = 0;
3238 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
3242 (void) b; /* unused */
3244 if (nwrap_gr_global.idx == 0) {
3245 nwrap_files_cache_reload(nwrap_gr_global.cache);
3248 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
3253 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
3255 NWRAP_LOG(NWRAP_LOG_DEBUG,
3256 "return group[%s] gid[%u]",
3257 gr->gr_name, gr->gr_gid);
3262 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
3263 struct group *grdst, char *buf,
3264 size_t buflen, struct group **grdstp)
3268 gr = nwrap_files_getgrent(b);
3276 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3279 static void nwrap_files_endgrent(struct nwrap_backend *b)
3281 (void) b; /* unused */
3283 nwrap_gr_global.idx = 0;
3286 /* hosts functions */
3287 static int nwrap_files_gethostbyname(const char *name, int af,
3288 struct hostent *result,
3289 struct nwrap_vector *addr_list)
3291 struct nwrap_entdata *ed_head;
3292 struct nwrap_entdata *ed_cur;
3297 char canon_name[DNS_NAME_MAX] = { 0 };
3299 bool he_found = false;
3301 nwrap_files_cache_reload(nwrap_he_global.cache);
3303 name_len = strlen(name);
3304 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
3305 strncpy(canon_name, name, name_len - 1);
3309 if (!str_tolower_copy(&h_name_lower, name)) {
3310 NWRAP_LOG(NWRAP_LOG_DEBUG,
3311 "Out of memory while converting to lower case");
3315 /* Look at hash table for element */
3316 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3317 e.key = h_name_lower;
3319 e_p = hsearch(e, FIND);
3321 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3322 SAFE_FREE(h_name_lower);
3325 SAFE_FREE(h_name_lower);
3327 /* Always cleanup vector and results */
3328 if (!nwrap_vector_is_initialized(addr_list)) {
3329 if (!nwrap_vector_init(addr_list)) {
3330 NWRAP_LOG(NWRAP_LOG_DEBUG,
3331 "Unable to initialize memory for addr_list vector");
3335 /* When vector is initialized data are valid no more.
3336 * Quick way how to free vector is: */
3337 addr_list->count = 0;
3340 /* Iterate through results */
3341 ed_head = (struct nwrap_entdata *)e_p->data;
3342 for (ed_cur = ed_head; ed_cur != NULL; ed_cur = ed_cur->ed_next) {
3345 /* Filter by address familiy if provided */
3346 if (af != AF_UNSPEC && he->h_addrtype != af) {
3352 * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3354 if (af == AF_UNSPEC && he->h_addrtype != AF_INET) {
3359 memcpy(result, he, sizeof(struct hostent));
3360 NWRAP_LOG(NWRAP_LOG_DEBUG,
3361 "Name found. Returning record for %s",
3365 nwrap_vector_merge(addr_list, &ed_cur->nwrap_addrdata);
3366 result->h_addr_list = nwrap_vector_head(addr_list);
3372 NWRAP_LOG(NWRAP_LOG_DEBUG,
3373 "Name found in database. No records matches type.");
3380 #ifdef HAVE_GETHOSTBYNAME_R
3381 static int nwrap_gethostbyname_r(const char *name,
3382 struct hostent *ret,
3383 char *buf, size_t buflen,
3384 struct hostent **result, int *h_errnop)
3386 struct nwrap_vector *addr_list = malloc(sizeof(struct nwrap_vector));
3389 if (addr_list == NULL) {
3390 NWRAP_LOG(NWRAP_LOG_ERROR,
3391 "Unable to allocate memory for address list");
3396 ZERO_STRUCTP(addr_list);
3398 rc = nwrap_files_gethostbyname(name, AF_UNSPEC, ret, addr_list);
3400 *h_errnop = h_errno;
3401 if (addr_list->items != NULL) {
3402 free(addr_list->items);
3404 SAFE_FREE(addr_list);
3409 if (buflen < (addr_list->count * sizeof(void *))) {
3410 SAFE_FREE(addr_list->items);
3411 SAFE_FREE(addr_list);
3415 /* Copy all to user provided buffer and change
3416 * pointers in returned structure.
3417 * +1 is for ending NULL pointer. */
3418 memcpy(buf, addr_list->items, (addr_list->count + 1) * sizeof(void *));
3420 free(addr_list->items);
3423 ret->h_addr_list = (char **)buf;
3428 int gethostbyname_r(const char *name,
3429 struct hostent *ret,
3430 char *buf, size_t buflen,
3431 struct hostent **result, int *h_errnop)
3433 if (!nss_wrapper_hosts_enabled()) {
3434 return libc_gethostbyname_r(name,
3442 return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
3446 static struct addrinfo *nwrap_files_getaddrinfo(const char *name,
3447 unsigned short port,
3448 const struct addrinfo *hints,
3449 struct addrinfo **ai_tail)
3451 struct nwrap_entdata *ed_head;
3452 struct nwrap_entdata *ed_cur;
3454 struct addrinfo *ai = NULL;
3455 struct addrinfo *ai_head = NULL;
3456 struct addrinfo *ai_prev = NULL;
3459 char canon_name[DNS_NAME_MAX] = { 0 };
3460 bool skip_canonname = false;
3464 nwrap_files_cache_reload(nwrap_he_global.cache);
3466 name_len = strlen(name);
3467 if (name_len < DNS_NAME_MAX && name[name_len - 1] == '.') {
3468 strncpy(canon_name, name, name_len - 1);
3472 if (!str_tolower_copy(&h_name_lower, name)) {
3473 NWRAP_LOG(NWRAP_LOG_DEBUG,
3474 "Out of memory while converting to lower case");
3478 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3479 e.key = h_name_lower;
3481 e_p = hsearch(e, FIND);
3483 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3484 SAFE_FREE(h_name_lower);
3488 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name: %s found.", h_name_lower);
3489 SAFE_FREE(h_name_lower);
3491 ed_head = (struct nwrap_entdata *)e_p->data;
3493 for (ed_cur = ed_head; ed_cur != NULL; ed_cur = ed_cur->ed_next) {
3498 if (hints->ai_family != AF_UNSPEC &&
3499 he->h_addrtype != hints->ai_family) {
3503 /* Function allocates memory and returns it in ai. */
3504 rc = nwrap_convert_he_ai(he,
3510 /* FIXME: Investigate if this is nice to do... */
3511 NWRAP_LOG(NWRAP_LOG_ERROR,
3512 "Error in converting he to ai! Skipping.");
3515 skip_canonname = true;
3517 if (ai_head == NULL) {
3520 if (ai_prev != NULL) {
3521 ai_prev->ai_next = ai;
3530 static struct hostent *nwrap_files_gethostbyaddr(const void *addr,
3531 socklen_t len, int type)
3534 char ip[NWRAP_INET_ADDRSTRLEN] = {0};
3535 struct nwrap_entdata *ed;
3539 (void) len; /* unused */
3541 nwrap_files_cache_reload(nwrap_he_global.cache);
3543 a = inet_ntop(type, addr, ip, sizeof(ip));
3549 nwrap_vector_foreach(ed, nwrap_he_global.entdata, i)
3552 if (he->h_addrtype != type) {
3556 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
3565 #ifdef HAVE_GETHOSTBYADDR_R
3566 static int nwrap_gethostbyaddr_r(const void *addr, socklen_t len, int type,
3567 struct hostent *ret,
3568 char *buf, size_t buflen,
3569 struct hostent **result, int *h_errnop)
3571 *result = nwrap_files_gethostbyaddr(addr, len, type);
3572 if (*result != NULL) {
3573 memset(buf, '\0', buflen);
3577 *h_errnop = h_errno;
3582 int gethostbyaddr_r(const void *addr, socklen_t len, int type,
3583 struct hostent *ret,
3584 char *buf, size_t buflen,
3585 struct hostent **result, int *h_errnop)
3587 if (!nss_wrapper_hosts_enabled()) {
3588 return libc_gethostbyaddr_r(addr,
3598 return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
3602 /* hosts enum functions */
3603 static void nwrap_files_sethostent(void)
3605 nwrap_he_global.idx = 0;
3608 static struct hostent *nwrap_files_gethostent(void)
3612 if (nwrap_he_global.idx == 0) {
3613 nwrap_files_cache_reload(nwrap_he_global.cache);
3616 if (nwrap_he_global.idx >= nwrap_he_global.num) {
3621 he = &((struct nwrap_entdata *)nwrap_he_global.entdata.items[nwrap_he_global.idx++])->ht;
3623 NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
3628 static void nwrap_files_endhostent(void)
3630 nwrap_he_global.idx = 0;
3638 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
3641 static struct passwd pwd;
3642 static char buf[1000];
3645 if (!b->fns->_nss_getpwnam_r) {
3649 status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
3650 if (status == NSS_STATUS_NOTFOUND) {
3653 if (status != NSS_STATUS_SUCCESS) {
3660 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
3661 const char *name, struct passwd *pwdst,
3662 char *buf, size_t buflen, struct passwd **pwdstp)
3666 (void) b; /* unused */
3667 (void) pwdst; /* unused */
3668 (void) pwdstp; /* unused */
3670 if (!b->fns->_nss_getpwnam_r) {
3671 return NSS_STATUS_NOTFOUND;
3674 ret = b->fns->_nss_getpwnam_r(name, pwdst, buf, buflen, &errno);
3676 case NSS_STATUS_SUCCESS:
3678 case NSS_STATUS_NOTFOUND:
3683 case NSS_STATUS_TRYAGAIN:
3696 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
3699 static struct passwd pwd;
3700 static char buf[1000];
3703 if (!b->fns->_nss_getpwuid_r) {
3707 status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
3708 if (status == NSS_STATUS_NOTFOUND) {
3711 if (status != NSS_STATUS_SUCCESS) {
3717 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
3718 uid_t uid, struct passwd *pwdst,
3719 char *buf, size_t buflen, struct passwd **pwdstp)
3723 (void) pwdstp; /* unused */
3725 if (!b->fns->_nss_getpwuid_r) {
3729 ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
3731 case NSS_STATUS_SUCCESS:
3733 case NSS_STATUS_NOTFOUND:
3738 case NSS_STATUS_TRYAGAIN:
3751 static void nwrap_module_setpwent(struct nwrap_backend *b)
3753 if (!b->fns->_nss_setpwent) {
3757 b->fns->_nss_setpwent();
3760 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
3762 static struct passwd pwd;
3763 static char buf[1000];
3766 if (!b->fns->_nss_getpwent_r) {
3770 status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
3771 if (status == NSS_STATUS_NOTFOUND) {
3774 if (status != NSS_STATUS_SUCCESS) {
3780 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
3781 struct passwd *pwdst, char *buf,
3782 size_t buflen, struct passwd **pwdstp)
3786 (void) pwdstp; /* unused */
3788 if (!b->fns->_nss_getpwent_r) {
3792 ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
3794 case NSS_STATUS_SUCCESS:
3796 case NSS_STATUS_NOTFOUND:
3801 case NSS_STATUS_TRYAGAIN:
3814 static void nwrap_module_endpwent(struct nwrap_backend *b)
3816 if (!b->fns->_nss_endpwent) {
3820 b->fns->_nss_endpwent();
3823 static int nwrap_module_initgroups(struct nwrap_backend *b,
3824 const char *user, gid_t group)
3830 if (!b->fns->_nss_initgroups) {
3831 return NSS_STATUS_UNAVAIL;
3834 return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
3837 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
3840 static struct group grp;
3842 static int buflen = 1000;
3845 if (!b->fns->_nss_getgrnam_r) {
3850 buf = (char *)malloc(buflen);
3853 status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
3854 if (status == NSS_STATUS_TRYAGAIN) {
3856 buf = (char *)realloc(buf, buflen);
3862 if (status == NSS_STATUS_NOTFOUND) {
3866 if (status != NSS_STATUS_SUCCESS) {
3873 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
3874 const char *name, struct group *grdst,
3875 char *buf, size_t buflen, struct group **grdstp)
3879 (void) grdstp; /* unused */
3881 if (!b->fns->_nss_getgrnam_r) {
3885 ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
3887 case NSS_STATUS_SUCCESS:
3889 case NSS_STATUS_NOTFOUND:
3894 case NSS_STATUS_TRYAGAIN:
3907 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
3910 static struct group grp;
3912 static int buflen = 1000;
3915 if (!b->fns->_nss_getgrgid_r) {
3920 buf = (char *)malloc(buflen);
3924 status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
3925 if (status == NSS_STATUS_TRYAGAIN) {
3927 buf = (char *)realloc(buf, buflen);
3933 if (status == NSS_STATUS_NOTFOUND) {
3937 if (status != NSS_STATUS_SUCCESS) {
3944 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
3945 gid_t gid, struct group *grdst,
3946 char *buf, size_t buflen, struct group **grdstp)
3950 (void) grdstp; /* unused */
3952 if (!b->fns->_nss_getgrgid_r) {
3956 ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
3958 case NSS_STATUS_SUCCESS:
3960 case NSS_STATUS_NOTFOUND:
3965 case NSS_STATUS_TRYAGAIN:
3978 static void nwrap_module_setgrent(struct nwrap_backend *b)
3980 if (!b->fns->_nss_setgrent) {
3984 b->fns->_nss_setgrent();
3987 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
3989 static struct group grp;
3991 static int buflen = 1024;
3994 if (!b->fns->_nss_getgrent_r) {
3999 buf = (char *)malloc(buflen);
4003 status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
4004 if (status == NSS_STATUS_TRYAGAIN) {
4006 buf = (char *)realloc(buf, buflen);
4012 if (status == NSS_STATUS_NOTFOUND) {
4016 if (status != NSS_STATUS_SUCCESS) {
4023 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
4024 struct group *grdst, char *buf,
4025 size_t buflen, struct group **grdstp)
4029 (void) grdstp; /* unused */
4031 if (!b->fns->_nss_getgrent_r) {
4035 ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
4037 case NSS_STATUS_SUCCESS:
4039 case NSS_STATUS_NOTFOUND:
4044 case NSS_STATUS_TRYAGAIN:
4057 static void nwrap_module_endgrent(struct nwrap_backend *b)
4059 if (!b->fns->_nss_endgrent) {
4063 b->fns->_nss_endgrent();
4066 /****************************************************************************
4068 ***************************************************************************/
4070 static struct passwd *nwrap_getpwnam(const char *name)
4075 for (i=0; i < nwrap_main_global->num_backends; i++) {
4076 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4077 pwd = b->ops->nw_getpwnam(b, name);
4086 struct passwd *getpwnam(const char *name)
4088 if (!nss_wrapper_enabled()) {
4089 return libc_getpwnam(name);
4092 return nwrap_getpwnam(name);
4095 /****************************************************************************
4097 ***************************************************************************/
4099 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
4100 char *buf, size_t buflen, struct passwd **pwdstp)
4104 for (i=0; i < nwrap_main_global->num_backends; i++) {
4105 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4106 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
4107 if (ret == ENOENT) {
4116 #ifdef HAVE_GETPWNAM_R
4117 # ifdef HAVE_SOLARIS_GETPWNAM_R
4118 int getpwnam_r(const char *name, struct passwd *pwdst,
4119 char *buf, int buflen, struct passwd **pwdstp)
4120 # else /* HAVE_SOLARIS_GETPWNAM_R */
4121 int getpwnam_r(const char *name, struct passwd *pwdst,
4122 char *buf, size_t buflen, struct passwd **pwdstp)
4123 # endif /* HAVE_SOLARIS_GETPWNAM_R */
4125 if (!nss_wrapper_enabled()) {
4126 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4129 return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4133 /****************************************************************************
4135 ***************************************************************************/
4137 static struct passwd *nwrap_getpwuid(uid_t uid)
4142 for (i=0; i < nwrap_main_global->num_backends; i++) {
4143 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4144 pwd = b->ops->nw_getpwuid(b, uid);
4153 struct passwd *getpwuid(uid_t uid)
4155 if (!nss_wrapper_enabled()) {
4156 return libc_getpwuid(uid);
4159 return nwrap_getpwuid(uid);
4162 /****************************************************************************
4164 ***************************************************************************/
4166 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
4167 char *buf, size_t buflen, struct passwd **pwdstp)
4171 for (i=0; i < nwrap_main_global->num_backends; i++) {
4172 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4173 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
4174 if (ret == ENOENT) {
4183 #ifdef HAVE_SOLARIS_GETPWUID_R
4184 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4185 char *buf, int buflen, struct passwd **pwdstp)
4187 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4188 char *buf, size_t buflen, struct passwd **pwdstp)
4191 if (!nss_wrapper_enabled()) {
4192 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4195 return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4198 /****************************************************************************
4200 ***************************************************************************/
4202 static void nwrap_setpwent(void)
4206 for (i=0; i < nwrap_main_global->num_backends; i++) {
4207 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4208 b->ops->nw_setpwent(b);
4214 if (!nss_wrapper_enabled()) {
4222 /****************************************************************************
4224 ***************************************************************************/
4226 static struct passwd *nwrap_getpwent(void)
4231 for (i=0; i < nwrap_main_global->num_backends; i++) {
4232 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4233 pwd = b->ops->nw_getpwent(b);
4242 struct passwd *getpwent(void)
4244 if (!nss_wrapper_enabled()) {
4245 return libc_getpwent();
4248 return nwrap_getpwent();
4251 /****************************************************************************
4253 ***************************************************************************/
4255 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
4256 size_t buflen, struct passwd **pwdstp)
4260 for (i=0; i < nwrap_main_global->num_backends; i++) {
4261 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4262 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
4263 if (ret == ENOENT) {
4272 #ifdef HAVE_SOLARIS_GETPWENT_R
4273 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
4275 struct passwd *pwdstp = NULL;
4278 if (!nss_wrapper_enabled()) {
4279 return libc_getpwent_r(pwdst, buf, buflen);
4281 rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
4288 #else /* HAVE_SOLARIS_GETPWENT_R */
4289 int getpwent_r(struct passwd *pwdst, char *buf,
4290 size_t buflen, struct passwd **pwdstp)
4292 if (!nss_wrapper_enabled()) {
4293 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
4296 return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
4298 #endif /* HAVE_SOLARIS_GETPWENT_R */
4300 /****************************************************************************
4302 ***************************************************************************/
4304 static void nwrap_endpwent(void)
4308 for (i=0; i < nwrap_main_global->num_backends; i++) {
4309 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4310 b->ops->nw_endpwent(b);
4316 if (!nss_wrapper_enabled()) {
4324 /****************************************************************************
4326 ***************************************************************************/
4328 static int nwrap_initgroups(const char *user, gid_t group)
4332 for (i=0; i < nwrap_main_global->num_backends; i++) {
4333 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4336 rc = b->ops->nw_initgroups(b, user, group);
4346 int initgroups(const char *user, gid_t group)
4348 if (!nss_wrapper_enabled()) {
4349 return libc_initgroups(user, group);
4352 return nwrap_initgroups(user, group);
4355 /****************************************************************************
4357 ***************************************************************************/
4359 static struct group *nwrap_getgrnam(const char *name)
4364 for (i=0; i < nwrap_main_global->num_backends; i++) {
4365 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4366 grp = b->ops->nw_getgrnam(b, name);
4375 struct group *getgrnam(const char *name)
4377 if (!nss_wrapper_enabled()) {
4378 return libc_getgrnam(name);
4381 return nwrap_getgrnam(name);
4384 /****************************************************************************
4386 ***************************************************************************/
4388 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
4389 char *buf, size_t buflen, struct group **grdstp)
4393 for (i=0; i < nwrap_main_global->num_backends; i++) {
4394 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4395 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
4396 if (ret == ENOENT) {
4405 #ifdef HAVE_GETGRNAM_R
4406 # ifdef HAVE_SOLARIS_GETGRNAM_R
4407 int getgrnam_r(const char *name, struct group *grp,
4408 char *buf, int buflen, struct group **pgrp)
4409 # else /* HAVE_SOLARIS_GETGRNAM_R */
4410 int getgrnam_r(const char *name, struct group *grp,
4411 char *buf, size_t buflen, struct group **pgrp)
4412 # endif /* HAVE_SOLARIS_GETGRNAM_R */
4414 if (!nss_wrapper_enabled()) {
4415 return libc_getgrnam_r(name,
4422 return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
4424 #endif /* HAVE_GETGRNAM_R */
4426 /****************************************************************************
4428 ***************************************************************************/
4430 static struct group *nwrap_getgrgid(gid_t gid)
4435 for (i=0; i < nwrap_main_global->num_backends; i++) {
4436 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4437 grp = b->ops->nw_getgrgid(b, gid);
4446 struct group *getgrgid(gid_t gid)
4448 if (!nss_wrapper_enabled()) {
4449 return libc_getgrgid(gid);
4452 return nwrap_getgrgid(gid);
4455 /****************************************************************************
4457 ***************************************************************************/
4459 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
4460 char *buf, size_t buflen, struct group **grdstp)
4464 for (i=0; i < nwrap_main_global->num_backends; i++) {
4465 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4466 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
4467 if (ret == ENOENT) {
4476 #ifdef HAVE_GETGRGID_R
4477 # ifdef HAVE_SOLARIS_GETGRGID_R
4478 int getgrgid_r(gid_t gid, struct group *grdst,
4479 char *buf, int buflen, struct group **grdstp)
4480 # else /* HAVE_SOLARIS_GETGRGID_R */
4481 int getgrgid_r(gid_t gid, struct group *grdst,
4482 char *buf, size_t buflen, struct group **grdstp)
4483 # endif /* HAVE_SOLARIS_GETGRGID_R */
4485 if (!nss_wrapper_enabled()) {
4486 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4489 return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4493 /****************************************************************************
4495 ***************************************************************************/
4497 static void nwrap_setgrent(void)
4501 for (i=0; i < nwrap_main_global->num_backends; i++) {
4502 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4503 b->ops->nw_setgrent(b);
4507 #ifdef HAVE_BSD_SETGRENT
4513 if (!nss_wrapper_enabled()) {
4521 #ifdef HAVE_BSD_SETGRENT
4528 /****************************************************************************
4530 ***************************************************************************/
4532 static struct group *nwrap_getgrent(void)
4537 for (i=0; i < nwrap_main_global->num_backends; i++) {
4538 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4539 grp = b->ops->nw_getgrent(b);
4548 struct group *getgrent(void)
4550 if (!nss_wrapper_enabled()) {
4551 return libc_getgrent();
4554 return nwrap_getgrent();
4557 /****************************************************************************
4559 ***************************************************************************/
4561 static int nwrap_getgrent_r(struct group *grdst, char *buf,
4562 size_t buflen, struct group **grdstp)
4566 for (i=0; i < nwrap_main_global->num_backends; i++) {
4567 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4568 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
4569 if (ret == ENOENT) {
4578 #ifdef HAVE_SOLARIS_GETGRENT_R
4579 struct group *getgrent_r(struct group *src, char *buf, int buflen)
4581 struct group *grdstp = NULL;
4584 if (!nss_wrapper_enabled()) {
4585 return libc_getgrent_r(src, buf, buflen);
4588 rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
4595 #else /* HAVE_SOLARIS_GETGRENT_R */
4596 int getgrent_r(struct group *src, char *buf,
4597 size_t buflen, struct group **grdstp)
4599 if (!nss_wrapper_enabled()) {
4600 return libc_getgrent_r(src, buf, buflen, grdstp);
4603 return nwrap_getgrent_r(src, buf, buflen, grdstp);
4605 #endif /* HAVE_SOLARIS_GETGRENT_R */
4607 /****************************************************************************
4609 ***************************************************************************/
4611 static void nwrap_endgrent(void)
4615 for (i=0; i < nwrap_main_global->num_backends; i++) {
4616 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4617 b->ops->nw_endgrent(b);
4623 if (!nss_wrapper_enabled()) {
4631 /****************************************************************************
4633 ***************************************************************************/
4635 #ifdef HAVE_GETGROUPLIST
4636 static int nwrap_getgrouplist(const char *user, gid_t group,
4637 gid_t *groups, int *ngroups)
4643 NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
4645 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
4647 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
4651 groups_tmp[0] = group;
4654 while ((grp = nwrap_getgrent()) != NULL) {
4657 NWRAP_LOG(NWRAP_LOG_DEBUG,
4658 "Inspecting %s for group membership",
4661 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
4663 if (group != grp->gr_gid &&
4664 (strcmp(user, grp->gr_mem[i]) == 0)) {
4666 NWRAP_LOG(NWRAP_LOG_DEBUG,
4667 "%s is member of %s",
4671 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
4673 NWRAP_LOG(NWRAP_LOG_ERROR,
4678 groups_tmp[count] = grp->gr_gid;
4687 NWRAP_LOG(NWRAP_LOG_DEBUG,
4688 "%s is member of %d groups",
4691 if (*ngroups < count) {
4698 memcpy(groups, groups_tmp, count * sizeof(gid_t));
4704 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
4706 if (!nss_wrapper_enabled()) {
4707 return libc_getgrouplist(user, group, groups, ngroups);
4710 return nwrap_getgrouplist(user, group, groups, ngroups);
4714 /**********************************************************
4716 **********************************************************/
4718 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
4720 #ifdef HAVE_SETSPENT
4721 static void nwrap_setspent(void)
4723 nwrap_files_setspent();
4728 if (!nss_wrapper_shadow_enabled()) {
4735 static struct spwd *nwrap_getspent(void)
4737 return nwrap_files_getspent();
4740 struct spwd *getspent(void)
4742 if (!nss_wrapper_shadow_enabled()) {
4746 return nwrap_getspent();
4749 static void nwrap_endspent(void)
4751 nwrap_files_endspent();
4756 if (!nss_wrapper_shadow_enabled()) {
4762 #endif /* HAVE_SETSPENT */
4764 static struct spwd *nwrap_getspnam(const char *name)
4766 return nwrap_files_getspnam(name);
4769 struct spwd *getspnam(const char *name)
4771 if (!nss_wrapper_shadow_enabled()) {
4775 return nwrap_getspnam(name);
4778 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
4780 /**********************************************************
4782 **********************************************************/
4784 static void nwrap_sethostent(int stayopen) {
4785 (void) stayopen; /* ignored */
4787 nwrap_files_sethostent();
4790 #ifdef HAVE_SOLARIS_SETHOSTENT
4791 int sethostent(int stayopen)
4793 if (!nss_wrapper_hosts_enabled()) {
4794 libc_sethostent(stayopen);
4798 nwrap_sethostent(stayopen);
4802 #else /* HAVE_SOLARIS_SETHOSTENT */
4803 void sethostent(int stayopen)
4805 if (!nss_wrapper_hosts_enabled()) {
4806 libc_sethostent(stayopen);
4810 nwrap_sethostent(stayopen);
4812 #endif /* HAVE_SOLARIS_SETHOSTENT */
4814 static struct hostent *nwrap_gethostent(void)
4816 return nwrap_files_gethostent();
4819 struct hostent *gethostent(void) {
4820 if (!nss_wrapper_hosts_enabled()) {
4821 return libc_gethostent();
4824 return nwrap_gethostent();
4827 static void nwrap_endhostent(void) {
4828 nwrap_files_endhostent();
4831 #ifdef HAVE_SOLARIS_ENDHOSTENT
4832 int endhostent(void)
4834 if (!nss_wrapper_hosts_enabled()) {
4843 #else /* HAVE_SOLARIS_ENDHOSTENT */
4844 void endhostent(void)
4846 if (!nss_wrapper_hosts_enabled()) {
4853 #endif /* HAVE_SOLARIS_ENDHOSTENT */
4856 /* BSD implementation stores data in thread local storage but GLIBC does not */
4857 static __thread struct hostent user_he;
4858 static __thread struct nwrap_vector user_addrlist;
4860 static struct hostent user_he;
4861 static struct nwrap_vector user_addrlist;
4863 static struct hostent *nwrap_gethostbyname(const char *name)
4865 if (nwrap_files_gethostbyname(name, AF_UNSPEC, &user_he, &user_addrlist) == -1) {
4871 struct hostent *gethostbyname(const char *name)
4873 if (!nss_wrapper_hosts_enabled()) {
4874 return libc_gethostbyname(name);
4877 return nwrap_gethostbyname(name);
4880 /* This is a GNU extension - Also can be found on BSD systems */
4881 #ifdef HAVE_GETHOSTBYNAME2
4883 /* BSD implementation stores data in thread local storage but GLIBC not */
4884 static __thread struct hostent user_he2;
4885 static __thread struct nwrap_vector user_addrlist2;
4887 static struct hostent user_he2;
4888 static struct nwrap_vector user_addrlist2;
4890 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
4892 if (nwrap_files_gethostbyname(name, af, &user_he2, &user_addrlist2) == -1) {
4898 struct hostent *gethostbyname2(const char *name, int af)
4900 if (!nss_wrapper_hosts_enabled()) {
4901 return libc_gethostbyname2(name, af);
4904 return nwrap_gethostbyname2(name, af);
4908 static struct hostent *nwrap_gethostbyaddr(const void *addr,
4909 socklen_t len, int type)
4911 return nwrap_files_gethostbyaddr(addr, len, type);
4914 struct hostent *gethostbyaddr(const void *addr,
4915 socklen_t len, int type)
4917 if (!nss_wrapper_hosts_enabled()) {
4918 return libc_gethostbyaddr(addr, len, type);
4921 return nwrap_gethostbyaddr(addr, len, type);
4924 static const struct addrinfo default_hints =
4926 .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
4927 .ai_family = AF_UNSPEC,
4932 .ai_canonname = NULL,
4936 static int nwrap_convert_he_ai(const struct hostent *he,
4937 unsigned short port,
4938 const struct addrinfo *hints,
4939 struct addrinfo **pai,
4940 bool skip_canonname)
4942 struct addrinfo *ai;
4949 switch (he->h_addrtype) {
4951 socklen = sizeof(struct sockaddr_in);
4955 socklen = sizeof(struct sockaddr_in6);
4962 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
4968 ai->ai_family = he->h_addrtype;
4969 ai->ai_socktype = hints->ai_socktype;
4970 ai->ai_protocol = hints->ai_protocol;
4971 ai->ai_canonname = NULL;
4973 ai->ai_addrlen = socklen;
4974 ai->ai_addr = (void *)(ai + 1);
4976 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
4977 ai->ai_addr->sa_len = socklen;
4979 ai->ai_addr->sa_family = he->h_addrtype;
4981 switch (he->h_addrtype) {
4984 struct sockaddr_in *sinp =
4985 (struct sockaddr_in *) ai->ai_addr;
4987 memset(sinp, 0, sizeof(struct sockaddr_in));
4989 sinp->sin_port = htons(port);
4990 sinp->sin_family = AF_INET;
4992 memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
4993 memcpy(&sinp->sin_addr, he->h_addr_list[0], he->h_length);
5000 struct sockaddr_in6 *sin6p =
5001 (struct sockaddr_in6 *) ai->ai_addr;
5003 memset(sin6p, 0, sizeof(struct sockaddr_in6));
5005 sin6p->sin6_port = htons(port);
5006 sin6p->sin6_family = AF_INET6;
5008 memcpy(&sin6p->sin6_addr,
5018 if (he->h_name && !skip_canonname) {
5019 ai->ai_canonname = strdup(he->h_name);
5020 if (ai->ai_canonname == NULL) {
5030 static int nwrap_getaddrinfo(const char *node,
5031 const char *service,
5032 const struct addrinfo *hints,
5033 struct addrinfo **res)
5035 struct addrinfo *ai = NULL;
5036 struct addrinfo *p = NULL;
5037 struct addrinfo *ai_tail;
5038 unsigned short port = 0;
5048 .family = AF_UNSPEC,
5052 if (node == NULL && service == NULL) {
5056 if (hints == NULL) {
5057 hints = &default_hints;
5061 hints.ai_flags contains invalid flags; or, hints.ai_flags
5062 included AI_CANONNAME and name was NULL.
5064 if ((hints->ai_flags & AI_CANONNAME) && (node == NULL)) {
5065 return EAI_BADFLAGS;
5068 ret = libc_getaddrinfo(node, service, hints, &p);
5072 /* If no node has been specified, let glibc deal with it */
5077 if (service != NULL && service[0] != '\0') {
5078 const char *proto = NULL;
5084 sl = strtol(service, &end_ptr, 10);
5086 if (*end_ptr == '\0') {
5089 } else if (hints->ai_flags & AI_NUMERICSERV) {
5093 if (hints->ai_protocol != 0) {
5094 struct protoent *pent;
5096 pent = getprotobynumber(hints->ai_protocol);
5098 proto = pent->p_name;
5102 s = getservbyname(service, proto);
5104 port = ntohs(s->s_port);
5114 if (hints->ai_family == AF_UNSPEC || hints->ai_family == AF_INET) {
5115 int rc = inet_pton(AF_INET, node, &addr.in.v4);
5117 addr.family = AF_INET;
5121 if (addr.family == AF_UNSPEC) {
5122 int rc = inet_pton(AF_INET6, node, &addr.in.v6);
5124 addr.family = AF_INET6;
5129 ai = nwrap_files_getaddrinfo(node, port, hints, &ai_tail);
5133 * nwrap_files_getaddrinfo failed, but libc was
5134 * successful -- use the result from libc.
5146 if (ai->ai_flags == 0) {
5147 ai->ai_flags = hints->ai_flags;
5149 if (ai->ai_socktype == 0) {
5150 ai->ai_socktype = SOCK_DGRAM;
5152 if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_DGRAM) {
5153 ai->ai_protocol = 17; /* UDP */
5154 } else if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_STREAM) {
5155 ai->ai_protocol = 6; /* TCP */
5158 if (hints->ai_socktype == 0) {
5160 struct addrinfo *ai_head = ai;
5161 struct addrinfo *ai_tmp;
5162 struct addrinfo *ai_new_tail = ai_tail;
5164 /* Add at least one more struct */
5167 ai_tmp = malloc(sizeof(struct addrinfo));
5168 memcpy(ai_tmp, ai_head, sizeof(struct addrinfo));
5169 ai_tmp->ai_next = NULL;
5171 /* We need a deep copy or freeaddrinfo() will blow up */
5172 if (ai_head->ai_canonname != NULL) {
5173 ai_tmp->ai_canonname =
5174 strdup(ai_head->ai_canonname);
5176 /* ai_head should point inside hints. */
5177 ai_tmp->ai_addr = ai_head->ai_addr;
5179 if (ai_head->ai_flags == 0) {
5180 ai_tmp->ai_flags = hints->ai_flags;
5182 if (ai_head->ai_socktype == SOCK_DGRAM) {
5183 ai_tmp->ai_socktype = SOCK_STREAM;
5184 } else if (ai_head->ai_socktype == SOCK_STREAM) {
5185 ai_tmp->ai_socktype = SOCK_DGRAM;
5187 if (ai_head->ai_socktype == SOCK_DGRAM) {
5188 ai_tmp->ai_protocol = 17; /* UDP */
5189 } else if (ai_head->ai_socktype == SOCK_STREAM) {
5190 ai_tmp->ai_protocol = 6; /* TCP */
5192 ai_new_tail->ai_next = ai_tmp;
5193 ai_new_tail = ai_tmp;
5195 if (ai_head == ai_tail) {
5198 ai_head = ai_head->ai_next;
5207 int getaddrinfo(const char *node, const char *service,
5208 const struct addrinfo *hints,
5209 struct addrinfo **res)
5211 if (!nss_wrapper_hosts_enabled()) {
5212 return libc_getaddrinfo(node, service, hints, res);
5215 return nwrap_getaddrinfo(node, service, hints, res);
5218 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
5219 char *host, size_t hostlen,
5220 char *serv, size_t servlen,
5224 struct servent *service;
5231 if (sa == NULL || salen < sizeof(sa_family_t)) {
5235 if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
5239 type = sa->sa_family;
5242 if (salen < sizeof(struct sockaddr_in))
5244 addr = &((const struct sockaddr_in *)sa)->sin_addr;
5245 addrlen = sizeof(((const struct sockaddr_in *)sa)->sin_addr);
5246 port = ntohs(((const struct sockaddr_in *)sa)->sin_port);
5250 if (salen < sizeof(struct sockaddr_in6))
5252 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
5253 addrlen = sizeof(((const struct sockaddr_in6 *)sa)->sin6_addr);
5254 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port);
5263 if ((flags & NI_NUMERICHOST) == 0) {
5264 he = nwrap_files_gethostbyaddr(addr, addrlen, type);
5265 if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
5268 if (he != NULL && he->h_name != NULL) {
5269 if (strlen(he->h_name) >= hostlen)
5270 return EAI_OVERFLOW;
5271 strcpy(host, he->h_name);
5272 if (flags & NI_NOFQDN)
5273 host[strcspn(host, ".")] = '\0';
5275 if (inet_ntop(type, addr, host, hostlen) == NULL)
5276 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
5282 if ((flags & NI_NUMERICSERV) == 0) {
5283 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
5284 service = getservbyport(htons(port), proto);
5286 if (service != NULL) {
5287 if (strlen(service->s_name) >= servlen)
5288 return EAI_OVERFLOW;
5289 strcpy(serv, service->s_name);
5291 if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
5292 return EAI_OVERFLOW;
5299 #ifdef HAVE_LINUX_GETNAMEINFO
5300 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5301 char *host, socklen_t hostlen,
5302 char *serv, socklen_t servlen,
5304 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
5305 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5306 char *host, socklen_t hostlen,
5307 char *serv, socklen_t servlen,
5310 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5311 char *host, size_t hostlen,
5312 char *serv, size_t servlen,
5316 if (!nss_wrapper_hosts_enabled()) {
5317 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5320 return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5323 static int nwrap_gethostname(char *name, size_t len)
5325 const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
5327 if (strlen(hostname) >= len) {
5328 errno = ENAMETOOLONG;
5331 snprintf(name, len, "%s", hostname);
5336 #ifdef HAVE_SOLARIS_GETHOSTNAME
5337 int gethostname(char *name, int len)
5338 #else /* HAVE_SOLARIS_GETHOSTNAME */
5339 int gethostname(char *name, size_t len)
5340 #endif /* HAVE_SOLARIS_GETHOSTNAME */
5342 if (!nwrap_hostname_enabled()) {
5343 return libc_gethostname(name, len);
5346 return nwrap_gethostname(name, len);
5349 /****************************
5351 ***************************/
5354 * This function is called when the library is unloaded and makes sure that
5355 * sockets get closed and the unix file for the socket are unlinked.
5357 void nwrap_destructor(void)
5362 if (nwrap_main_global != NULL) {
5363 struct nwrap_main *m = nwrap_main_global;
5366 SAFE_FREE(m->libc->fns);
5367 if (m->libc->handle != NULL) {
5368 dlclose(m->libc->handle);
5370 if (m->libc->nsl_handle != NULL) {
5371 dlclose(m->libc->nsl_handle);
5373 if (m->libc->sock_handle != NULL) {
5374 dlclose(m->libc->sock_handle);
5379 for (i = 0; i < m->num_backends; i++) {
5380 struct nwrap_backend *b = &(m->backends[i]);
5382 if (b->so_handle != NULL) {
5383 dlclose(b->so_handle);
5387 SAFE_FREE(m->backends);
5390 if (nwrap_pw_global.cache != NULL) {
5391 struct nwrap_cache *c = nwrap_pw_global.cache;
5393 nwrap_files_cache_unload(c);
5399 SAFE_FREE(nwrap_pw_global.list);
5400 nwrap_pw_global.num = 0;
5403 if (nwrap_gr_global.cache != NULL) {
5404 struct nwrap_cache *c = nwrap_gr_global.cache;
5406 nwrap_files_cache_unload(c);
5412 SAFE_FREE(nwrap_gr_global.list);
5413 nwrap_pw_global.num = 0;
5416 if (nwrap_he_global.cache != NULL) {
5417 struct nwrap_cache *c = nwrap_he_global.cache;
5419 nwrap_files_cache_unload(c);
5425 nwrap_he_global.num = 0;