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;
763 struct nwrap_entlist {
764 struct nwrap_entlist *next;
765 struct nwrap_entdata *ed;
769 struct nwrap_cache *cache;
771 struct nwrap_entdata *list;
772 struct nwrap_vector entdata;
778 static struct nwrap_cache __nwrap_cache_he;
779 static struct nwrap_he nwrap_he_global;
782 /*********************************************************
784 *********************************************************/
786 static void nwrap_init(void);
787 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
788 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
789 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
791 /*********************************************************
792 * NWRAP LIBC LOADER FUNCTIONS
793 *********************************************************/
802 static const char *nwrap_str_lib(enum nwrap_lib lib)
809 case NWRAP_LIBSOCKET:
813 /* Compiler would warn us about unhandled enum value if we get here */
818 static void *nwrap_load_lib_handle(enum nwrap_lib lib)
820 int flags = RTLD_LAZY;
825 flags |= RTLD_DEEPBIND;
831 handle = nwrap_main_global->libc->nsl_handle;
832 if (handle == NULL) {
833 for (i = 10; i >= 0; i--) {
834 char soname[256] = {0};
836 snprintf(soname, sizeof(soname), "libnsl.so.%d", i);
837 handle = dlopen(soname, flags);
838 if (handle != NULL) {
843 nwrap_main_global->libc->nsl_handle = handle;
848 case NWRAP_LIBSOCKET:
849 #ifdef HAVE_LIBSOCKET
850 handle = nwrap_main_global->libc->sock_handle;
851 if (handle == NULL) {
852 for (i = 10; i >= 0; i--) {
853 char soname[256] = {0};
855 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
856 handle = dlopen(soname, flags);
857 if (handle != NULL) {
862 nwrap_main_global->libc->sock_handle = handle;
868 handle = nwrap_main_global->libc->handle;
869 if (handle == NULL) {
870 for (i = 10; i >= 0; i--) {
871 char soname[256] = {0};
873 snprintf(soname, sizeof(soname), "libc.so.%d", i);
874 handle = dlopen(soname, flags);
875 if (handle != NULL) {
880 nwrap_main_global->libc->handle = handle;
885 if (handle == NULL) {
887 handle = nwrap_main_global->libc->handle
888 = nwrap_main_global->libc->sock_handle
889 = nwrap_main_global->libc->nsl_handle
892 NWRAP_LOG(NWRAP_LOG_ERROR,
893 "Failed to dlopen library: %s\n",
902 static void *_nwrap_load_lib_function(enum nwrap_lib lib, const char *fn_name)
909 handle = nwrap_load_lib_handle(lib);
911 func = dlsym(handle, fn_name);
913 NWRAP_LOG(NWRAP_LOG_ERROR,
914 "Failed to find %s: %s\n",
919 NWRAP_LOG(NWRAP_LOG_TRACE,
921 fn_name, nwrap_str_lib(lib));
925 #define nwrap_load_lib_function(lib, fn_name) \
926 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
927 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
928 _nwrap_load_lib_function(lib, #fn_name); \
931 /* INTERNAL HELPER FUNCTIONS */
932 static void nwrap_lines_unload(struct nwrap_cache *const nwrap)
936 nwrap_vector_foreach(item, nwrap->lines, p) {
937 /* Maybe some vectors were merged ... */
940 SAFE_FREE(nwrap->lines.items);
941 ZERO_STRUCTP(&nwrap->lines);
947 * Functions expeciall from libc need to be loaded individually, you can't load
948 * all at once or gdb will segfault at startup. The same applies to valgrind and
949 * has probably something todo with with the linker.
950 * So we need load each function at the point it is called the first time.
952 static struct passwd *libc_getpwnam(const char *name)
954 nwrap_load_lib_function(NWRAP_LIBC, getpwnam);
956 return nwrap_main_global->libc->fns->_libc_getpwnam(name);
959 #ifdef HAVE_GETPWNAM_R
960 static int libc_getpwnam_r(const char *name,
964 struct passwd **result)
966 #ifdef HAVE___POSIX_GETPWNAM_R
967 if (nwrap_main_global->libc->fns->_libc_getpwnam_r == NULL) {
968 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwnam_r) =
969 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwnam_r");
972 nwrap_load_lib_function(NWRAP_LIBC, getpwnam_r);
975 return nwrap_main_global->libc->fns->_libc_getpwnam_r(name,
983 static struct passwd *libc_getpwuid(uid_t uid)
985 nwrap_load_lib_function(NWRAP_LIBC, getpwuid);
987 return nwrap_main_global->libc->fns->_libc_getpwuid(uid);
990 #ifdef HAVE_GETPWUID_R
991 static int libc_getpwuid_r(uid_t uid,
995 struct passwd **result)
997 #ifdef HAVE___POSIX_GETPWUID_R
998 if (nwrap_main_global->libc->fns->_libc_getpwuid_r == NULL) {
999 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwuid_r) =
1000 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwuid_r");
1003 nwrap_load_lib_function(NWRAP_LIBC, getpwuid_r);
1006 return nwrap_main_global->libc->fns->_libc_getpwuid_r(uid,
1014 static inline void str_tolower(char *dst, char *src)
1016 register char *src_tmp = src;
1017 register char *dst_tmp = dst;
1019 while (*src_tmp != '\0') {
1020 *dst_tmp = tolower(*src_tmp);
1026 static bool str_tolower_copy(char **dst_name, const char *const src_name)
1030 if ((dst_name == NULL) || (src_name == NULL)) {
1034 h_name_lower = strdup(src_name);
1035 if (h_name_lower == NULL) {
1036 NWRAP_LOG(NWRAP_LOG_DEBUG, "Out of memory while strdup");
1040 str_tolower(h_name_lower, h_name_lower);
1041 *dst_name = h_name_lower;
1045 static void libc_setpwent(void)
1047 nwrap_load_lib_function(NWRAP_LIBC, setpwent);
1049 nwrap_main_global->libc->fns->_libc_setpwent();
1052 static struct passwd *libc_getpwent(void)
1054 nwrap_load_lib_function(NWRAP_LIBC, getpwent);
1056 return nwrap_main_global->libc->fns->_libc_getpwent();
1059 #ifdef HAVE_SOLARIS_GETPWENT_R
1060 static struct passwd *libc_getpwent_r(struct passwd *pwdst,
1064 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1066 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1070 #else /* HAVE_SOLARIS_GETPWENT_R */
1071 static int libc_getpwent_r(struct passwd *pwdst,
1074 struct passwd **pwdstp)
1076 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1078 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1083 #endif /* HAVE_SOLARIS_GETPWENT_R */
1085 static void libc_endpwent(void)
1087 nwrap_load_lib_function(NWRAP_LIBC, endpwent);
1089 nwrap_main_global->libc->fns->_libc_endpwent();
1092 static int libc_initgroups(const char *user, gid_t gid)
1094 nwrap_load_lib_function(NWRAP_LIBC, initgroups);
1096 return nwrap_main_global->libc->fns->_libc_initgroups(user, gid);
1099 static struct group *libc_getgrnam(const char *name)
1101 nwrap_load_lib_function(NWRAP_LIBC, getgrnam);
1103 return nwrap_main_global->libc->fns->_libc_getgrnam(name);
1106 #ifdef HAVE_GETGRNAM_R
1107 static int libc_getgrnam_r(const char *name,
1111 struct group **result)
1113 #ifdef HAVE___POSIX_GETGRNAM_R
1114 if (nwrap_main_global->libc->fns->_libc_getgrnam_r == NULL) {
1115 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrnam_r) =
1116 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrnam_r");
1119 nwrap_load_lib_function(NWRAP_LIBC, getgrnam_r);
1122 return nwrap_main_global->libc->fns->_libc_getgrnam_r(name,
1130 static struct group *libc_getgrgid(gid_t gid)
1132 nwrap_load_lib_function(NWRAP_LIBC, getgrgid);
1134 return nwrap_main_global->libc->fns->_libc_getgrgid(gid);
1137 #ifdef HAVE_GETGRGID_R
1138 static int libc_getgrgid_r(gid_t gid,
1142 struct group **result)
1144 #ifdef HAVE___POSIX_GETGRGID_R
1145 if (nwrap_main_global->libc->fns->_libc_getgrgid_r == NULL) {
1146 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrgid_r) =
1147 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrgid_r");
1150 nwrap_load_lib_function(NWRAP_LIBC, getgrgid_r);
1153 return nwrap_main_global->libc->fns->_libc_getgrgid_r(gid,
1161 static void libc_setgrent(void)
1163 nwrap_load_lib_function(NWRAP_LIBC, setgrent);
1165 nwrap_main_global->libc->fns->_libc_setgrent();
1168 static struct group *libc_getgrent(void)
1170 nwrap_load_lib_function(NWRAP_LIBC, getgrent);
1172 return nwrap_main_global->libc->fns->_libc_getgrent();
1175 #ifdef HAVE_GETGRENT_R
1176 #ifdef HAVE_SOLARIS_GETGRENT_R
1177 static struct group *libc_getgrent_r(struct group *group,
1181 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1183 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1187 #else /* !HAVE_SOLARIS_GETGRENT_R */
1188 static int libc_getgrent_r(struct group *group,
1191 struct group **result)
1193 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1195 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1200 #endif /* HAVE_SOLARIS_GETGRENT_R */
1201 #endif /* HAVE_GETGRENT_R */
1203 static void libc_endgrent(void)
1205 nwrap_load_lib_function(NWRAP_LIBC, endgrent);
1207 nwrap_main_global->libc->fns->_libc_endgrent();
1210 #ifdef HAVE_GETGROUPLIST
1211 static int libc_getgrouplist(const char *user,
1216 nwrap_load_lib_function(NWRAP_LIBC, getgrouplist);
1218 return nwrap_main_global->libc->fns->_libc_getgrouplist(user,
1225 static void libc_sethostent(int stayopen)
1227 nwrap_load_lib_function(NWRAP_LIBNSL, sethostent);
1229 nwrap_main_global->libc->fns->_libc_sethostent(stayopen);
1232 static struct hostent *libc_gethostent(void)
1234 nwrap_load_lib_function(NWRAP_LIBNSL, gethostent);
1236 return nwrap_main_global->libc->fns->_libc_gethostent();
1239 static void libc_endhostent(void)
1241 nwrap_load_lib_function(NWRAP_LIBNSL, endhostent);
1243 nwrap_main_global->libc->fns->_libc_endhostent();
1246 static struct hostent *libc_gethostbyname(const char *name)
1248 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname);
1250 return nwrap_main_global->libc->fns->_libc_gethostbyname(name);
1253 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
1254 static struct hostent *libc_gethostbyname2(const char *name, int af)
1256 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname2);
1258 return nwrap_main_global->libc->fns->_libc_gethostbyname2(name, af);
1262 static struct hostent *libc_gethostbyaddr(const void *addr,
1266 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr);
1268 return nwrap_main_global->libc->fns->_libc_gethostbyaddr(addr,
1273 static int libc_gethostname(char *name, size_t len)
1275 nwrap_load_lib_function(NWRAP_LIBNSL, gethostname);
1277 return nwrap_main_global->libc->fns->_libc_gethostname(name, len);
1280 #ifdef HAVE_GETHOSTBYNAME_R
1281 static int libc_gethostbyname_r(const char *name,
1282 struct hostent *ret,
1285 struct hostent **result,
1288 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname_r);
1290 return nwrap_main_global->libc->fns->_libc_gethostbyname_r(name,
1299 #ifdef HAVE_GETHOSTBYADDR_R
1300 static int libc_gethostbyaddr_r(const void *addr,
1303 struct hostent *ret,
1306 struct hostent **result,
1309 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr_r);
1311 return nwrap_main_global->libc->fns->_libc_gethostbyaddr_r(addr,
1322 static int libc_getaddrinfo(const char *node,
1323 const char *service,
1324 const struct addrinfo *hints,
1325 struct addrinfo **res)
1327 nwrap_load_lib_function(NWRAP_LIBSOCKET, getaddrinfo);
1329 return nwrap_main_global->libc->fns->_libc_getaddrinfo(node,
1335 static int libc_getnameinfo(const struct sockaddr *sa,
1343 nwrap_load_lib_function(NWRAP_LIBSOCKET, getnameinfo);
1345 return nwrap_main_global->libc->fns->_libc_getnameinfo(sa,
1354 /*********************************************************
1355 * NWRAP NSS MODULE LOADER FUNCTIONS
1356 *********************************************************/
1358 static void *nwrap_load_module_fn(struct nwrap_backend *b,
1359 const char *fn_name)
1364 if (!b->so_handle) {
1365 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1369 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
1370 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1374 res = dlsym(b->so_handle, s);
1376 NWRAP_LOG(NWRAP_LOG_ERROR,
1377 "Cannot find function %s in %s",
1384 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
1386 struct nwrap_module_nss_fns *fns;
1388 if (!b->so_handle) {
1392 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
1397 *(void **)(&fns->_nss_getpwnam_r) =
1398 nwrap_load_module_fn(b, "getpwnam_r");
1399 *(void **)(&fns->_nss_getpwuid_r) =
1400 nwrap_load_module_fn(b, "getpwuid_r");
1401 *(void **)(&fns->_nss_setpwent) =
1402 nwrap_load_module_fn(b, "setpwent");
1403 *(void **)(&fns->_nss_getpwent_r) =
1404 nwrap_load_module_fn(b, "getpwent_r");
1405 *(void **)(&fns->_nss_endpwent) =
1406 nwrap_load_module_fn(b, "endpwent");
1407 *(void **)(&fns->_nss_initgroups) =
1408 nwrap_load_module_fn(b, "initgroups_dyn");
1409 *(void **)(&fns->_nss_getgrnam_r) =
1410 nwrap_load_module_fn(b, "getgrnam_r");
1411 *(void **)(&fns->_nss_getgrgid_r)=
1412 nwrap_load_module_fn(b, "getgrgid_r");
1413 *(void **)(&fns->_nss_setgrent) =
1414 nwrap_load_module_fn(b, "setgrent");
1415 *(void **)(&fns->_nss_getgrent_r) =
1416 nwrap_load_module_fn(b, "getgrent_r");
1417 *(void **)(&fns->_nss_endgrent) =
1418 nwrap_load_module_fn(b, "endgrent");
1423 static void *nwrap_load_module(const char *so_path)
1427 if (!so_path || !strlen(so_path)) {
1431 h = dlopen(so_path, RTLD_LAZY);
1433 NWRAP_LOG(NWRAP_LOG_ERROR,
1434 "Cannot open shared library %s",
1442 static bool nwrap_module_init(const char *name,
1443 struct nwrap_ops *ops,
1444 const char *so_path,
1446 struct nwrap_backend **backends)
1448 struct nwrap_backend *b;
1450 *backends = (struct nwrap_backend *)realloc(*backends,
1451 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
1453 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1457 b = &((*backends)[*num_backends]);
1461 b->so_path = so_path;
1463 if (so_path != NULL) {
1464 b->so_handle = nwrap_load_module(so_path);
1465 b->fns = nwrap_load_module_fns(b);
1466 if (b->fns == NULL) {
1470 b->so_handle = NULL;
1479 static void nwrap_libc_init(struct nwrap_main *r)
1481 r->libc = malloc(sizeof(struct nwrap_libc));
1482 if (r->libc == NULL) {
1483 printf("Failed to allocate memory for libc");
1486 ZERO_STRUCTP(r->libc);
1488 r->libc->fns = malloc(sizeof(struct nwrap_libc_fns));
1489 if (r->libc->fns == NULL) {
1490 printf("Failed to allocate memory for libc functions");
1493 ZERO_STRUCTP(r->libc->fns);
1496 static void nwrap_backend_init(struct nwrap_main *r)
1498 const char *module_so_path = getenv("NSS_WRAPPER_MODULE_SO_PATH");
1499 const char *module_fn_name = getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1501 r->num_backends = 0;
1504 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1507 NWRAP_LOG(NWRAP_LOG_ERROR,
1508 "Failed to initialize 'files' backend");
1512 if (module_so_path != NULL &&
1513 module_so_path[0] != '\0' &&
1514 module_fn_name != NULL &&
1515 module_fn_name[0] != '\0') {
1516 if (!nwrap_module_init(module_fn_name,
1521 NWRAP_LOG(NWRAP_LOG_ERROR,
1522 "Failed to initialize '%s' backend",
1529 static void nwrap_init(void)
1533 size_t max_hostents_tmp;
1535 NWRAP_LOCK(nwrap_initialized);
1536 if (nwrap_initialized) {
1537 NWRAP_UNLOCK(nwrap_initialized);
1542 * Still holding nwrap_initialized lock here.
1543 * We don't use NWRAP_(UN)LOCK_ALL macros here because we
1544 * want to avoid overhead when other threads do their job.
1546 NWRAP_LOCK(nwrap_global);
1547 NWRAP_LOCK(nwrap_gr_global);
1548 NWRAP_LOCK(nwrap_he_global);
1549 NWRAP_LOCK(nwrap_pw_global);
1550 NWRAP_LOCK(nwrap_sp_global);
1552 nwrap_initialized = true;
1554 /* Initialize pthread_atfork handlers */
1555 pthread_atfork(&nwrap_thread_prepare, &nwrap_thread_parent,
1556 &nwrap_thread_child);
1558 env = getenv("NSS_WRAPPER_MAX_HOSTENTS");
1560 max_hostents_tmp = (size_t)strtol(env, &endptr, 10);
1561 if (((env != '\0') && (endptr == '\0')) ||
1562 (max_hostents_tmp == 0)) {
1563 NWRAP_LOG(NWRAP_LOG_DEBUG,
1564 "Error parsing NSS_WRAPPER_MAX_HOSTENTS "
1565 "value or value is too small. "
1566 "Using default value: %lu.",
1569 max_hostents = max_hostents_tmp;
1572 /* Initialize hash table */
1573 NWRAP_LOG(NWRAP_LOG_DEBUG,
1574 "Initializing hash table of size %lu items.", max_hostents);
1575 if (hcreate(max_hostents) == 0) {
1576 NWRAP_LOG(NWRAP_LOG_ERROR,
1577 "Failed to initialize hash table");
1581 nwrap_main_global = &__nwrap_main_global;
1583 nwrap_libc_init(nwrap_main_global);
1585 nwrap_backend_init(nwrap_main_global);
1588 nwrap_pw_global.cache = &__nwrap_cache_pw;
1590 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
1591 nwrap_pw_global.cache->fp = NULL;
1592 nwrap_pw_global.cache->fd = -1;
1593 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
1594 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
1595 nwrap_pw_global.cache->unload = nwrap_pw_unload;
1598 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1599 nwrap_sp_global.cache = &__nwrap_cache_sp;
1601 nwrap_sp_global.cache->path = getenv("NSS_WRAPPER_SHADOW");
1602 nwrap_sp_global.cache->fp = NULL;
1603 nwrap_sp_global.cache->fd = -1;
1604 nwrap_sp_global.cache->private_data = &nwrap_sp_global;
1605 nwrap_sp_global.cache->parse_line = nwrap_sp_parse_line;
1606 nwrap_sp_global.cache->unload = nwrap_sp_unload;
1607 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1610 nwrap_gr_global.cache = &__nwrap_cache_gr;
1612 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
1613 nwrap_gr_global.cache->fp = NULL;
1614 nwrap_gr_global.cache->fd = -1;
1615 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
1616 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
1617 nwrap_gr_global.cache->unload = nwrap_gr_unload;
1620 nwrap_he_global.cache = &__nwrap_cache_he;
1622 nwrap_he_global.cache->path = getenv("NSS_WRAPPER_HOSTS");
1623 nwrap_he_global.cache->fp = NULL;
1624 nwrap_he_global.cache->fd = -1;
1625 nwrap_he_global.cache->private_data = &nwrap_he_global;
1626 nwrap_he_global.cache->parse_line = nwrap_he_parse_line;
1627 nwrap_he_global.cache->unload = nwrap_he_unload;
1630 /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1634 bool nss_wrapper_enabled(void)
1638 if (nwrap_pw_global.cache->path == NULL ||
1639 nwrap_pw_global.cache->path[0] == '\0') {
1642 if (nwrap_gr_global.cache->path == NULL ||
1643 nwrap_gr_global.cache->path[0] == '\0') {
1650 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1651 bool nss_wrapper_shadow_enabled(void)
1655 if (nwrap_sp_global.cache->path == NULL ||
1656 nwrap_sp_global.cache->path[0] == '\0') {
1662 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1664 bool nss_wrapper_hosts_enabled(void)
1668 if (nwrap_he_global.cache->path == NULL ||
1669 nwrap_he_global.cache->path[0] == '\0') {
1676 static bool nwrap_hostname_enabled(void)
1680 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
1687 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
1691 /* Unused but getline needs it */
1695 if (nwrap->st.st_size == 0) {
1696 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
1700 /* Support for 32-bit system I guess */
1701 if (nwrap->st.st_size > INT32_MAX) {
1702 NWRAP_LOG(NWRAP_LOG_ERROR,
1703 "Size[%u] larger than INT32_MAX",
1704 (unsigned)nwrap->st.st_size);
1711 n = getline(&line, &len, nwrap->fp);
1714 if (feof(nwrap->fp)) {
1718 NWRAP_LOG(NWRAP_LOG_ERROR,
1719 "Unable to read line from file: %s",
1724 if (line[n - 1] == '\n') {
1728 if (line[0] == '\0') {
1733 ok = nwrap->parse_line(nwrap, line);
1735 NWRAP_LOG(NWRAP_LOG_ERROR,
1736 "Unable to parse line file: %s",
1742 /* Line is parsed without issues so add it to list */
1743 ok = nwrap_vector_add_item(&(nwrap->lines), (void *const) line);
1745 NWRAP_LOG(NWRAP_LOG_ERROR,
1746 "Unable to add line to vector");
1750 /* This forces getline to allocate new memory for line. */
1752 } while (!feof(nwrap->fp));
1757 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
1759 nwrap->unload(nwrap);
1761 nwrap_lines_unload(nwrap);
1764 static bool nwrap_files_cache_reload(struct nwrap_cache *nwrap)
1769 bool retried = false;
1771 assert(nwrap != NULL);
1774 if (nwrap->fd < 0) {
1775 nwrap->fp = fopen(nwrap->path, "re");
1776 if (nwrap->fp == NULL) {
1778 NWRAP_LOG(NWRAP_LOG_ERROR,
1779 "Unable to open '%s' readonly %d:%s",
1780 nwrap->path, nwrap->fd,
1785 nwrap->fd = fileno(nwrap->fp);
1786 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
1789 ret = fstat(nwrap->fd, &st);
1791 NWRAP_LOG(NWRAP_LOG_ERROR,
1792 "fstat(%s) - %d:%s",
1802 if (retried == false && st.st_nlink == 0) {
1803 /* maybe someone has replaced the file... */
1804 NWRAP_LOG(NWRAP_LOG_TRACE,
1805 "st_nlink == 0, reopen %s",
1808 memset(&nwrap->st, 0, sizeof(nwrap->st));
1815 if (st.st_mtime == nwrap->st.st_mtime) {
1816 NWRAP_LOG(NWRAP_LOG_TRACE,
1817 "st_mtime[%u] hasn't changed, skip reload",
1818 (unsigned)st.st_mtime);
1822 NWRAP_LOG(NWRAP_LOG_TRACE,
1823 "st_mtime has changed [%u] => [%u], start reload",
1824 (unsigned)st.st_mtime,
1825 (unsigned)nwrap->st.st_mtime);
1829 nwrap_files_cache_unload(nwrap);
1831 ok = nwrap_parse_file(nwrap);
1833 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
1834 nwrap_files_cache_unload(nwrap);
1838 NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
1843 * the caller has to call nwrap_unload() on failure
1845 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
1847 struct nwrap_pw *nwrap_pw;
1854 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1856 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
1857 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
1859 NWRAP_LOG(NWRAP_LOG_ERROR,
1860 "realloc(%u) failed",
1861 (unsigned)list_size);
1864 nwrap_pw->list = pw;
1866 pw = &nwrap_pw->list[nwrap_pw->num];
1873 NWRAP_LOG(NWRAP_LOG_ERROR,
1874 "Invalid line[%s]: '%s'",
1884 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
1889 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1897 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
1902 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1908 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
1910 NWRAP_LOG(NWRAP_LOG_ERROR,
1911 "Invalid line[%s]: '%s' - %s",
1912 line, c, strerror(errno));
1916 NWRAP_LOG(NWRAP_LOG_ERROR,
1917 "Invalid line[%s]: '%s' - %s",
1918 line, c, strerror(errno));
1922 NWRAP_LOG(NWRAP_LOG_ERROR,
1923 "Invalid line[%s]: '%s' - %s",
1924 line, c, strerror(errno));
1929 NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
1934 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1940 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
1942 NWRAP_LOG(NWRAP_LOG_ERROR,
1943 "Invalid line[%s]: '%s' - %s",
1944 line, c, strerror(errno));
1948 NWRAP_LOG(NWRAP_LOG_ERROR,
1949 "Invalid line[%s]: '%s' - %s",
1950 line, c, strerror(errno));
1954 NWRAP_LOG(NWRAP_LOG_ERROR,
1955 "Invalid line[%s]: '%s' - %s",
1956 line, c, strerror(errno));
1961 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]\n", pw->pw_gid);
1966 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
1974 NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
1979 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
1987 NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
1991 NWRAP_LOG(NWRAP_LOG_TRACE, "shell[%s]", pw->pw_shell);
1993 NWRAP_LOG(NWRAP_LOG_DEBUG,
1994 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1995 pw->pw_name, pw->pw_passwd,
1996 pw->pw_uid, pw->pw_gid,
1997 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
2003 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
2005 struct nwrap_pw *nwrap_pw;
2006 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
2008 SAFE_FREE(nwrap_pw->list);
2013 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
2014 char *buf, size_t buflen, struct passwd **dstp)
2020 first = src->pw_name;
2022 last = src->pw_shell;
2023 while (*last) last++;
2025 ofs = PTR_DIFF(last + 1, first);
2027 if (ofs > (off_t) buflen) {
2031 memcpy(buf, first, ofs);
2033 ofs = PTR_DIFF(src->pw_name, first);
2034 dst->pw_name = buf + ofs;
2035 ofs = PTR_DIFF(src->pw_passwd, first);
2036 dst->pw_passwd = buf + ofs;
2037 dst->pw_uid = src->pw_uid;
2038 dst->pw_gid = src->pw_gid;
2039 ofs = PTR_DIFF(src->pw_gecos, first);
2040 dst->pw_gecos = buf + ofs;
2041 ofs = PTR_DIFF(src->pw_dir, first);
2042 dst->pw_dir = buf + ofs;
2043 ofs = PTR_DIFF(src->pw_shell, first);
2044 dst->pw_shell = buf + ofs;
2053 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2054 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line)
2056 struct nwrap_sp *nwrap_sp;
2063 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2065 list_size = sizeof(*nwrap_sp->list) * (nwrap_sp->num+1);
2066 sp = (struct spwd *)realloc(nwrap_sp->list, list_size);
2068 NWRAP_LOG(NWRAP_LOG_ERROR,
2069 "realloc(%u) failed",
2070 (unsigned)list_size);
2073 nwrap_sp->list = sp;
2075 sp = &nwrap_sp->list[nwrap_sp->num];
2082 NWRAP_LOG(NWRAP_LOG_ERROR,
2083 "name -- Invalid line[%s]: '%s'",
2093 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", sp->sp_namp);
2098 NWRAP_LOG(NWRAP_LOG_ERROR,
2099 "pwd -- Invalid line[%s]: '%s'",
2116 NWRAP_LOG(NWRAP_LOG_ERROR,
2117 "lstchg -- Invalid line[%s]: '%s'",
2124 sp->sp_lstchg = strtol(c, &e, 10);
2126 NWRAP_LOG(NWRAP_LOG_ERROR,
2127 "lstchg -- Invalid line[%s]: '%s' - %s",
2128 line, c, strerror(errno));
2132 NWRAP_LOG(NWRAP_LOG_ERROR,
2133 "lstchg -- Invalid line[%s]: '%s' - %s",
2134 line, c, strerror(errno));
2138 NWRAP_LOG(NWRAP_LOG_ERROR,
2139 "lstchg -- Invalid line[%s]: '%s' - %s",
2140 line, c, strerror(errno));
2153 NWRAP_LOG(NWRAP_LOG_ERROR,
2154 "min -- Invalid line[%s]: '%s'",
2161 sp->sp_min = strtol(c, &e, 10);
2163 NWRAP_LOG(NWRAP_LOG_ERROR,
2164 "min -- Invalid line[%s]: '%s' - %s",
2165 line, c, strerror(errno));
2169 NWRAP_LOG(NWRAP_LOG_ERROR,
2170 "min -- Invalid line[%s]: '%s' - %s",
2171 line, c, strerror(errno));
2175 NWRAP_LOG(NWRAP_LOG_ERROR,
2176 "min -- Invalid line[%s]: '%s' - %s",
2177 line, c, strerror(errno));
2190 NWRAP_LOG(NWRAP_LOG_ERROR,
2191 "max -- Invalid line[%s]: '%s'",
2198 sp->sp_max = strtol(c, &e, 10);
2200 NWRAP_LOG(NWRAP_LOG_ERROR,
2201 "max -- Invalid line[%s]: '%s' - %s",
2202 line, c, strerror(errno));
2206 NWRAP_LOG(NWRAP_LOG_ERROR,
2207 "max -- Invalid line[%s]: '%s' - %s",
2208 line, c, strerror(errno));
2212 NWRAP_LOG(NWRAP_LOG_ERROR,
2213 "max -- Invalid line[%s]: '%s' - %s",
2214 line, c, strerror(errno));
2227 NWRAP_LOG(NWRAP_LOG_ERROR,
2228 "warn -- Invalid line[%s]: '%s'",
2235 sp->sp_warn = strtol(c, &e, 10);
2237 NWRAP_LOG(NWRAP_LOG_ERROR,
2238 "warn -- Invalid line[%s]: '%s' - %s",
2239 line, c, strerror(errno));
2243 NWRAP_LOG(NWRAP_LOG_ERROR,
2244 "warn -- Invalid line[%s]: '%s' - %s",
2245 line, c, strerror(errno));
2249 NWRAP_LOG(NWRAP_LOG_ERROR,
2250 "warn -- Invalid line[%s]: '%s' - %s",
2251 line, c, strerror(errno));
2264 NWRAP_LOG(NWRAP_LOG_ERROR,
2265 "inact -- Invalid line[%s]: '%s'",
2272 sp->sp_inact = strtol(c, &e, 10);
2274 NWRAP_LOG(NWRAP_LOG_ERROR,
2275 "inact -- Invalid line[%s]: '%s' - %s",
2276 line, c, strerror(errno));
2280 NWRAP_LOG(NWRAP_LOG_ERROR,
2281 "inact -- Invalid line[%s]: '%s' - %s",
2282 line, c, strerror(errno));
2286 NWRAP_LOG(NWRAP_LOG_ERROR,
2287 "inact -- Invalid line[%s]: '%s' - %s",
2288 line, c, strerror(errno));
2301 NWRAP_LOG(NWRAP_LOG_ERROR,
2302 "expire -- Invalid line[%s]: '%s'",
2309 sp->sp_expire = strtol(c, &e, 10);
2311 NWRAP_LOG(NWRAP_LOG_ERROR,
2312 "expire -- Invalid line[%s]: '%s' - %s",
2313 line, c, strerror(errno));
2317 NWRAP_LOG(NWRAP_LOG_ERROR,
2318 "expire -- Invalid line[%s]: '%s' - %s",
2319 line, c, strerror(errno));
2323 NWRAP_LOG(NWRAP_LOG_ERROR,
2324 "expire -- Invalid line[%s]: '%s' - %s",
2325 line, c, strerror(errno));
2335 static void nwrap_sp_unload(struct nwrap_cache *nwrap)
2337 struct nwrap_sp *nwrap_sp;
2338 nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2340 SAFE_FREE(nwrap_sp->list);
2344 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2347 * the caller has to call nwrap_unload() on failure
2349 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
2351 struct nwrap_gr *nwrap_gr;
2359 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2361 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
2362 gr = (struct group *)realloc(nwrap_gr->list, list_size);
2364 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
2367 nwrap_gr->list = gr;
2369 gr = &nwrap_gr->list[nwrap_gr->num];
2376 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2384 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
2389 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2397 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
2402 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2408 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
2410 NWRAP_LOG(NWRAP_LOG_ERROR,
2411 "Invalid line[%s]: '%s' - %s",
2412 line, c, strerror(errno));
2416 NWRAP_LOG(NWRAP_LOG_ERROR,
2417 "Invalid line[%s]: '%s' - %s",
2418 line, c, strerror(errno));
2422 NWRAP_LOG(NWRAP_LOG_ERROR,
2423 "Invalid line[%s]: '%s' - %s",
2424 line, c, strerror(errno));
2429 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
2432 gr->gr_mem = (char **)malloc(sizeof(char *));
2434 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
2437 gr->gr_mem[0] = NULL;
2439 for(nummem=0; p; nummem++) {
2449 if (strlen(c) == 0) {
2453 m_size = sizeof(char *) * (nummem+2);
2454 m = (char **)realloc(gr->gr_mem, m_size);
2456 NWRAP_LOG(NWRAP_LOG_ERROR,
2457 "realloc(%zd) failed",
2462 gr->gr_mem[nummem] = c;
2463 gr->gr_mem[nummem+1] = NULL;
2465 NWRAP_LOG(NWRAP_LOG_TRACE,
2467 nummem, gr->gr_mem[nummem]);
2470 NWRAP_LOG(NWRAP_LOG_DEBUG,
2471 "Added group[%s:%s:%u:] with %u members",
2472 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem);
2478 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
2481 struct nwrap_gr *nwrap_gr;
2482 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2484 if (nwrap_gr->list) {
2485 for (i=0; i < nwrap_gr->num; i++) {
2486 SAFE_FREE(nwrap_gr->list[i].gr_mem);
2488 SAFE_FREE(nwrap_gr->list);
2495 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
2496 char *buf, size_t buflen, struct group **dstp)
2506 first = src->gr_name;
2508 lastm = src->gr_mem;
2515 last = src->gr_passwd;
2517 while (*last) last++;
2519 ofsb = PTR_DIFF(last + 1, first);
2520 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
2522 if ((ofsb + ofsm) > (off_t) buflen) {
2526 memcpy(buf, first, ofsb);
2527 memcpy(buf + ofsb, src->gr_mem, ofsm);
2529 ofs = PTR_DIFF(src->gr_name, first);
2530 dst->gr_name = buf + ofs;
2531 ofs = PTR_DIFF(src->gr_passwd, first);
2532 dst->gr_passwd = buf + ofs;
2533 dst->gr_gid = src->gr_gid;
2535 dst->gr_mem = (char **)(buf + ofsb);
2536 for (i=0; src->gr_mem[i]; i++) {
2537 ofs = PTR_DIFF(src->gr_mem[i], first);
2538 dst->gr_mem[i] = buf + ofs;
2548 static struct nwrap_entlist *nwrap_entlist_init(struct nwrap_entdata *ed)
2550 struct nwrap_entlist *el;
2553 NWRAP_LOG(NWRAP_LOG_ERROR,
2554 "entry is NULL, can't create list item");
2558 el = (struct nwrap_entlist *)malloc(sizeof(struct nwrap_entlist));
2560 NWRAP_LOG(NWRAP_LOG_ERROR, "malloc failed");
2570 static bool nwrap_add_ai(char *const ip_addr, struct nwrap_entdata *const ed)
2574 struct nwrap_entlist *el;
2576 if (ip_addr == NULL) {
2577 NWRAP_LOG(NWRAP_LOG_ERROR, "ip_addr NULL - can't add");
2581 el = nwrap_entlist_init(ed);
2589 p = hsearch(e, ENTER);
2591 NWRAP_LOG(NWRAP_LOG_ERROR, "Hash table is full");
2599 static bool nwrap_ed_inventarize_add_new(char *const h_name,
2600 struct nwrap_entdata *const ed)
2604 struct nwrap_entlist *el;
2606 if (h_name == NULL) {
2607 NWRAP_LOG(NWRAP_LOG_ERROR, "h_name NULL - can't add");
2611 el = nwrap_entlist_init(ed);
2617 e.data = (void *)el;
2619 p = hsearch(e, ENTER);
2621 NWRAP_LOG(NWRAP_LOG_ERROR, "Hash table is full!");
2628 static bool nwrap_ed_inventarize_add_to_existing(struct nwrap_entdata *const ed,
2629 struct nwrap_entlist *const el)
2631 struct nwrap_entlist *cursor;
2632 struct nwrap_entlist *el_new;
2635 NWRAP_LOG(NWRAP_LOG_ERROR, "list is NULL, can not add");
2639 el_new = nwrap_entlist_init(ed);
2640 if (el_new == NULL) {
2644 for (cursor = el; cursor->next != NULL; cursor = cursor->next)
2646 if (cursor->ed == ed) {
2652 if (cursor->ed == ed) {
2657 cursor->next = el_new;
2661 static bool nwrap_ed_inventarize(char *const name,
2662 struct nwrap_entdata *const ed)
2671 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
2673 p = hsearch(e, FIND);
2675 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", name);
2676 ok = nwrap_ed_inventarize_add_new(name, ed);
2678 struct nwrap_entlist *el = (struct nwrap_entlist *)p->data;
2680 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s found. Add record to list.", name);
2681 ok = nwrap_ed_inventarize_add_to_existing(ed, el);
2687 static bool nwrap_add_hname(struct nwrap_entdata *const ed)
2689 char *const h_name = (char *const)(ed->ht.h_name);
2693 ok = nwrap_ed_inventarize(h_name, ed);
2698 if (ed->ht.h_aliases == NULL) {
2702 /* Itemize aliases */
2703 for (i = 0; ed->ht.h_aliases[i] != NULL; ++i) {
2706 h_name_alias = ed->ht.h_aliases[i];
2708 NWRAP_LOG(NWRAP_LOG_DEBUG, "Add alias: %s", h_name_alias);
2710 if (!nwrap_ed_inventarize(h_name_alias, ed)) {
2711 NWRAP_LOG(NWRAP_LOG_ERROR,
2712 "Unable to add alias: %s", h_name_alias);
2720 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line)
2722 struct nwrap_he *nwrap_he = (struct nwrap_he *)nwrap->private_data;
2723 bool do_aliases = true;
2724 ssize_t aliases_count = 0;
2732 struct nwrap_entdata *ed = (struct nwrap_entdata *)
2733 malloc(sizeof(struct nwrap_entdata));
2735 NWRAP_LOG(NWRAP_LOG_ERROR,
2736 "Unable to allocate memory for nwrap_entdata");
2747 /* Walk to first char */
2748 for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
2750 NWRAP_LOG(NWRAP_LOG_ERROR,
2751 "Invalid line[%s]: '%s'",
2758 for (i = p; !isspace((int)*p); p++) {
2760 NWRAP_LOG(NWRAP_LOG_ERROR,
2761 "Invalid line[%s]: '%s'",
2770 if (inet_pton(AF_INET, i, ed->addr.host_addr)) {
2771 ed->ht.h_addrtype = AF_INET;
2772 ed->ht.h_length = 4;
2774 } else if (inet_pton(AF_INET6, i, ed->addr.host_addr)) {
2775 ed->ht.h_addrtype = AF_INET6;
2776 ed->ht.h_length = 16;
2779 NWRAP_LOG(NWRAP_LOG_ERROR,
2780 "Invalid line[%s]: '%s'",
2788 nwrap_vector_add_item(&(ed->nwrap_addrdata),
2789 (void *const)ed->addr.host_addr);
2790 ed->ht.h_addr_list = nwrap_vector_head(&ed->nwrap_addrdata);
2798 /* Walk to first char */
2799 for (n = p; *p != '_' && !isalnum((int) *p); p++) {
2801 NWRAP_LOG(NWRAP_LOG_ERROR,
2802 "Invalid line[%s]: '%s'",
2810 for (n = p; !isspace((int)*p); p++) {
2819 /* Convert to lowercase. This operate on same memory region */
2823 /* glib's getent always dereferences he->h_aliases */
2824 ed->ht.h_aliases = malloc(sizeof(char *));
2825 if (ed->ht.h_aliases == NULL) {
2829 ed->ht.h_aliases[0] = NULL;
2834 while (do_aliases) {
2840 /* Walk to first char */
2841 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
2847 /* Only trailing spaces are left */
2852 for (a = p; !isspace((int)*p); p++) {
2861 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
2862 if (aliases == NULL) {
2866 ed->ht.h_aliases = aliases;
2869 aliases[aliases_count] = a;
2870 aliases[aliases_count + 1] = NULL;
2875 nwrap_vector_add_item(&(nwrap_he->entdata), (void *const)ed);
2877 ed->aliases_count = aliases_count;
2878 /* Inventarize item */
2879 ok = nwrap_add_hname(ed);
2884 ok = nwrap_add_ai(ip, ed);
2893 static void nwrap_he_unload(struct nwrap_cache *nwrap)
2895 struct nwrap_he *nwrap_he =
2896 (struct nwrap_he *)nwrap->private_data;
2897 struct nwrap_entdata *ed;
2900 nwrap_vector_foreach (ed, nwrap_he->entdata, i)
2902 SAFE_FREE(ed->nwrap_addrdata.items);
2903 SAFE_FREE(ed->ht.h_aliases);
2906 SAFE_FREE(nwrap_he->entdata.items);
2907 nwrap_he->entdata.count = nwrap_he->entdata.capacity = 0;
2914 /* user functions */
2915 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
2921 (void) b; /* unused */
2923 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
2925 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
2927 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
2931 for (i=0; i<nwrap_pw_global.num; i++) {
2932 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
2933 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
2934 return &nwrap_pw_global.list[i];
2936 NWRAP_LOG(NWRAP_LOG_DEBUG,
2937 "user[%s] does not match [%s]",
2939 nwrap_pw_global.list[i].pw_name);
2942 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
2948 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
2949 const char *name, struct passwd *pwdst,
2950 char *buf, size_t buflen, struct passwd **pwdstp)
2954 pw = nwrap_files_getpwnam(b, name);
2962 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2965 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
2971 (void) b; /* unused */
2973 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
2975 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
2979 for (i=0; i<nwrap_pw_global.num; i++) {
2980 if (nwrap_pw_global.list[i].pw_uid == uid) {
2981 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] found", uid);
2982 return &nwrap_pw_global.list[i];
2984 NWRAP_LOG(NWRAP_LOG_DEBUG,
2985 "uid[%u] does not match [%u]",
2987 nwrap_pw_global.list[i].pw_uid);
2990 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
2996 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
2997 uid_t uid, struct passwd *pwdst,
2998 char *buf, size_t buflen, struct passwd **pwdstp)
3002 pw = nwrap_files_getpwuid(b, uid);
3010 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3013 /* user enum functions */
3014 static void nwrap_files_setpwent(struct nwrap_backend *b)
3016 (void) b; /* unused */
3018 nwrap_pw_global.idx = 0;
3021 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
3025 (void) b; /* unused */
3027 if (nwrap_pw_global.idx == 0) {
3029 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3031 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3036 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
3041 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
3043 NWRAP_LOG(NWRAP_LOG_DEBUG,
3044 "return user[%s] uid[%u]",
3045 pw->pw_name, pw->pw_uid);
3050 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
3051 struct passwd *pwdst, char *buf,
3052 size_t buflen, struct passwd **pwdstp)
3056 pw = nwrap_files_getpwent(b);
3064 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3067 static void nwrap_files_endpwent(struct nwrap_backend *b)
3069 (void) b; /* unused */
3071 nwrap_pw_global.idx = 0;
3076 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
3078 #ifdef HAVE_SETSPENT
3079 static void nwrap_files_setspent(void)
3081 nwrap_sp_global.idx = 0;
3084 static struct spwd *nwrap_files_getspent(void)
3088 if (nwrap_sp_global.idx == 0) {
3091 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3093 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3098 if (nwrap_sp_global.idx >= nwrap_sp_global.num) {
3103 sp = &nwrap_sp_global.list[nwrap_sp_global.idx++];
3105 NWRAP_LOG(NWRAP_LOG_DEBUG,
3112 static void nwrap_files_endspent(void)
3114 nwrap_sp_global.idx = 0;
3116 #endif /* HAVE_SETSPENT */
3118 static struct spwd *nwrap_files_getspnam(const char *name)
3123 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
3125 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3127 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3131 for (i=0; i<nwrap_sp_global.num; i++) {
3132 if (strcmp(nwrap_sp_global.list[i].sp_namp, name) == 0) {
3133 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
3134 return &nwrap_sp_global.list[i];
3136 NWRAP_LOG(NWRAP_LOG_DEBUG,
3137 "user[%s] does not match [%s]",
3139 nwrap_sp_global.list[i].sp_namp);
3142 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3147 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3149 /* misc functions */
3150 static int nwrap_files_initgroups(struct nwrap_backend *b,
3159 groups = (gid_t *)malloc(size * sizeof(gid_t));
3160 if (groups == NULL) {
3161 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3167 nwrap_files_setgrent(b);
3168 while ((grp = nwrap_files_getgrent(b)) != NULL) {
3171 NWRAP_LOG(NWRAP_LOG_DEBUG,
3172 "Inspecting %s for group membership",
3175 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
3176 if (group != grp->gr_gid &&
3177 (strcmp(user, grp->gr_mem[i]) == 0)) {
3178 NWRAP_LOG(NWRAP_LOG_DEBUG,
3179 "%s is member of %s",
3183 groups = (gid_t *)realloc(groups,
3184 (size + 1) * sizeof(gid_t));
3185 if (groups == NULL) {
3186 NWRAP_LOG(NWRAP_LOG_ERROR,
3192 groups[size] = grp->gr_gid;
3198 nwrap_files_endgrent(b);
3200 NWRAP_LOG(NWRAP_LOG_DEBUG,
3201 "%s is member of %d groups",
3204 /* This really only works if uid_wrapper is loaded */
3205 rc = setgroups(size, groups);
3212 /* group functions */
3213 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
3219 (void) b; /* unused */
3221 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3223 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3227 for (i=0; i<nwrap_gr_global.num; i++) {
3228 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
3229 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] found", name);
3230 return &nwrap_gr_global.list[i];
3232 NWRAP_LOG(NWRAP_LOG_DEBUG,
3233 "group[%s] does not match [%s]",
3235 nwrap_gr_global.list[i].gr_name);
3238 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
3244 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
3245 const char *name, struct group *grdst,
3246 char *buf, size_t buflen, struct group **grdstp)
3250 gr = nwrap_files_getgrnam(b, name);
3258 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3261 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
3267 (void) b; /* unused */
3269 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3271 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3275 for (i=0; i<nwrap_gr_global.num; i++) {
3276 if (nwrap_gr_global.list[i].gr_gid == gid) {
3277 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] found", gid);
3278 return &nwrap_gr_global.list[i];
3280 NWRAP_LOG(NWRAP_LOG_DEBUG,
3281 "gid[%u] does not match [%u]",
3283 nwrap_gr_global.list[i].gr_gid);
3286 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
3292 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
3293 gid_t gid, struct group *grdst,
3294 char *buf, size_t buflen, struct group **grdstp)
3298 gr = nwrap_files_getgrgid(b, gid);
3306 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3309 /* group enum functions */
3310 static void nwrap_files_setgrent(struct nwrap_backend *b)
3312 (void) b; /* unused */
3314 nwrap_gr_global.idx = 0;
3317 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
3321 (void) b; /* unused */
3323 if (nwrap_gr_global.idx == 0) {
3326 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3328 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3333 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
3338 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
3340 NWRAP_LOG(NWRAP_LOG_DEBUG,
3341 "return group[%s] gid[%u]",
3342 gr->gr_name, gr->gr_gid);
3347 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
3348 struct group *grdst, char *buf,
3349 size_t buflen, struct group **grdstp)
3353 gr = nwrap_files_getgrent(b);
3361 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3364 static void nwrap_files_endgrent(struct nwrap_backend *b)
3366 (void) b; /* unused */
3368 nwrap_gr_global.idx = 0;
3371 /* hosts functions */
3372 static int nwrap_files_gethostbyname(const char *name, int af,
3373 struct hostent *result,
3374 struct nwrap_vector *addr_list)
3376 struct nwrap_entlist *el;
3381 char canon_name[DNS_NAME_MAX] = { 0 };
3383 bool he_found = false;
3386 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3388 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3392 name_len = strlen(name);
3393 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
3394 strncpy(canon_name, name, name_len - 1);
3398 if (!str_tolower_copy(&h_name_lower, name)) {
3399 NWRAP_LOG(NWRAP_LOG_DEBUG,
3400 "Out of memory while converting to lower case");
3404 /* Look at hash table for element */
3405 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3406 e.key = h_name_lower;
3408 e_p = hsearch(e, FIND);
3410 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3411 SAFE_FREE(h_name_lower);
3414 SAFE_FREE(h_name_lower);
3416 /* Always cleanup vector and results */
3417 if (!nwrap_vector_is_initialized(addr_list)) {
3418 if (!nwrap_vector_init(addr_list)) {
3419 NWRAP_LOG(NWRAP_LOG_DEBUG,
3420 "Unable to initialize memory for addr_list vector");
3424 /* When vector is initialized data are valid no more.
3425 * Quick way how to free vector is: */
3426 addr_list->count = 0;
3429 /* Iterate through results */
3430 for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3434 /* Filter by address familiy if provided */
3435 if (af != AF_UNSPEC && he->h_addrtype != af) {
3441 * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3443 if (af == AF_UNSPEC && he->h_addrtype != AF_INET) {
3448 memcpy(result, he, sizeof(struct hostent));
3449 NWRAP_LOG(NWRAP_LOG_DEBUG,
3450 "Name found. Returning record for %s",
3454 nwrap_vector_merge(addr_list, &el->ed->nwrap_addrdata);
3455 result->h_addr_list = nwrap_vector_head(addr_list);
3461 NWRAP_LOG(NWRAP_LOG_DEBUG,
3462 "Name found in database. No records matches type.");
3469 #ifdef HAVE_GETHOSTBYNAME_R
3470 static int nwrap_gethostbyname_r(const char *name,
3471 struct hostent *ret,
3472 char *buf, size_t buflen,
3473 struct hostent **result, int *h_errnop)
3475 struct nwrap_vector *addr_list = malloc(sizeof(struct nwrap_vector));
3478 if (addr_list == NULL) {
3479 NWRAP_LOG(NWRAP_LOG_ERROR,
3480 "Unable to allocate memory for address list");
3485 ZERO_STRUCTP(addr_list);
3487 rc = nwrap_files_gethostbyname(name, AF_UNSPEC, ret, addr_list);
3489 *h_errnop = h_errno;
3490 if (addr_list->items != NULL) {
3491 free(addr_list->items);
3493 SAFE_FREE(addr_list);
3498 if (buflen < (addr_list->count * sizeof(void *))) {
3499 SAFE_FREE(addr_list->items);
3500 SAFE_FREE(addr_list);
3504 /* Copy all to user provided buffer and change
3505 * pointers in returned structure.
3506 * +1 is for ending NULL pointer. */
3507 memcpy(buf, addr_list->items, (addr_list->count + 1) * sizeof(void *));
3509 free(addr_list->items);
3512 ret->h_addr_list = (char **)buf;
3517 int gethostbyname_r(const char *name,
3518 struct hostent *ret,
3519 char *buf, size_t buflen,
3520 struct hostent **result, int *h_errnop)
3522 if (!nss_wrapper_hosts_enabled()) {
3523 return libc_gethostbyname_r(name,
3531 return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
3535 static int nwrap_files_getaddrinfo(const char *name,
3536 unsigned short port,
3537 const struct addrinfo *hints,
3538 struct addrinfo **ai,
3539 struct addrinfo **ai_tail)
3541 struct nwrap_entlist *el;
3543 struct addrinfo *_ai = NULL;
3544 struct addrinfo *ai_head = NULL;
3545 struct addrinfo *ai_prev = NULL;
3548 char canon_name[DNS_NAME_MAX] = { 0 };
3549 bool skip_canonname = false;
3555 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3557 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3561 name_len = strlen(name);
3562 if (name_len < DNS_NAME_MAX && name[name_len - 1] == '.') {
3563 strncpy(canon_name, name, name_len - 1);
3567 if (!str_tolower_copy(&h_name_lower, name)) {
3568 NWRAP_LOG(NWRAP_LOG_DEBUG,
3569 "Out of memory while converting to lower case");
3573 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3574 e.key = h_name_lower;
3576 e_p = hsearch(e, FIND);
3578 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3579 SAFE_FREE(h_name_lower);
3583 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name: %s found.", h_name_lower);
3584 SAFE_FREE(h_name_lower);
3587 for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3593 if (hints->ai_family != AF_UNSPEC &&
3594 he->h_addrtype != hints->ai_family)
3596 NWRAP_LOG(NWRAP_LOG_DEBUG,
3597 "Entry found but with wrong AF - "
3598 "remembering EAI_ADDRINFO.");
3599 rc = EAI_ADDRFAMILY;
3603 /* Function allocates memory and returns it in ai. */
3604 rc2 = nwrap_convert_he_ai(he,
3610 NWRAP_LOG(NWRAP_LOG_ERROR, "Error converting he to ai");
3613 skip_canonname = true;
3615 if (ai_head == NULL) {
3618 if (ai_prev != NULL) {
3619 ai_prev->ai_next = _ai;
3624 if (ai_head != NULL) {
3634 static struct hostent *nwrap_files_gethostbyaddr(const void *addr,
3635 socklen_t len, int type)
3638 char ip[NWRAP_INET_ADDRSTRLEN] = {0};
3639 struct nwrap_entdata *ed;
3644 (void) len; /* unused */
3646 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3648 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3652 a = inet_ntop(type, addr, ip, sizeof(ip));
3658 nwrap_vector_foreach(ed, nwrap_he_global.entdata, i)
3661 if (he->h_addrtype != type) {
3665 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
3674 #ifdef HAVE_GETHOSTBYADDR_R
3675 static int nwrap_gethostbyaddr_r(const void *addr, socklen_t len, int type,
3676 struct hostent *ret,
3677 char *buf, size_t buflen,
3678 struct hostent **result, int *h_errnop)
3680 *result = nwrap_files_gethostbyaddr(addr, len, type);
3681 if (*result != NULL) {
3682 memset(buf, '\0', buflen);
3686 *h_errnop = h_errno;
3691 int gethostbyaddr_r(const void *addr, socklen_t len, int type,
3692 struct hostent *ret,
3693 char *buf, size_t buflen,
3694 struct hostent **result, int *h_errnop)
3696 if (!nss_wrapper_hosts_enabled()) {
3697 return libc_gethostbyaddr_r(addr,
3707 return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
3711 /* hosts enum functions */
3712 static void nwrap_files_sethostent(void)
3714 nwrap_he_global.idx = 0;
3717 static struct hostent *nwrap_files_gethostent(void)
3721 if (nwrap_he_global.idx == 0) {
3724 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3726 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading hosts file");
3731 if (nwrap_he_global.idx >= nwrap_he_global.num) {
3736 he = &((struct nwrap_entdata *)nwrap_he_global.entdata.items[nwrap_he_global.idx++])->ht;
3738 NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
3743 static void nwrap_files_endhostent(void)
3745 nwrap_he_global.idx = 0;
3753 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
3756 static struct passwd pwd;
3757 static char buf[1000];
3760 if (!b->fns->_nss_getpwnam_r) {
3764 status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
3765 if (status == NSS_STATUS_NOTFOUND) {
3768 if (status != NSS_STATUS_SUCCESS) {
3775 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
3776 const char *name, struct passwd *pwdst,
3777 char *buf, size_t buflen, struct passwd **pwdstp)
3781 (void) b; /* unused */
3782 (void) pwdst; /* unused */
3783 (void) pwdstp; /* unused */
3785 if (!b->fns->_nss_getpwnam_r) {
3786 return NSS_STATUS_NOTFOUND;
3789 ret = b->fns->_nss_getpwnam_r(name, pwdst, buf, buflen, &errno);
3791 case NSS_STATUS_SUCCESS:
3793 case NSS_STATUS_NOTFOUND:
3798 case NSS_STATUS_TRYAGAIN:
3811 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
3814 static struct passwd pwd;
3815 static char buf[1000];
3818 if (!b->fns->_nss_getpwuid_r) {
3822 status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
3823 if (status == NSS_STATUS_NOTFOUND) {
3826 if (status != NSS_STATUS_SUCCESS) {
3832 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
3833 uid_t uid, struct passwd *pwdst,
3834 char *buf, size_t buflen, struct passwd **pwdstp)
3838 (void) pwdstp; /* unused */
3840 if (!b->fns->_nss_getpwuid_r) {
3844 ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
3846 case NSS_STATUS_SUCCESS:
3848 case NSS_STATUS_NOTFOUND:
3853 case NSS_STATUS_TRYAGAIN:
3866 static void nwrap_module_setpwent(struct nwrap_backend *b)
3868 if (!b->fns->_nss_setpwent) {
3872 b->fns->_nss_setpwent();
3875 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
3877 static struct passwd pwd;
3878 static char buf[1000];
3881 if (!b->fns->_nss_getpwent_r) {
3885 status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
3886 if (status == NSS_STATUS_NOTFOUND) {
3889 if (status != NSS_STATUS_SUCCESS) {
3895 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
3896 struct passwd *pwdst, char *buf,
3897 size_t buflen, struct passwd **pwdstp)
3901 (void) pwdstp; /* unused */
3903 if (!b->fns->_nss_getpwent_r) {
3907 ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
3909 case NSS_STATUS_SUCCESS:
3911 case NSS_STATUS_NOTFOUND:
3916 case NSS_STATUS_TRYAGAIN:
3929 static void nwrap_module_endpwent(struct nwrap_backend *b)
3931 if (!b->fns->_nss_endpwent) {
3935 b->fns->_nss_endpwent();
3938 static int nwrap_module_initgroups(struct nwrap_backend *b,
3939 const char *user, gid_t group)
3945 if (!b->fns->_nss_initgroups) {
3946 return NSS_STATUS_UNAVAIL;
3949 return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
3952 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
3955 static struct group grp;
3957 static int buflen = 1000;
3960 if (!b->fns->_nss_getgrnam_r) {
3965 buf = (char *)malloc(buflen);
3968 status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
3969 if (status == NSS_STATUS_TRYAGAIN) {
3971 buf = (char *)realloc(buf, buflen);
3977 if (status == NSS_STATUS_NOTFOUND) {
3981 if (status != NSS_STATUS_SUCCESS) {
3988 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
3989 const char *name, struct group *grdst,
3990 char *buf, size_t buflen, struct group **grdstp)
3994 (void) grdstp; /* unused */
3996 if (!b->fns->_nss_getgrnam_r) {
4000 ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
4002 case NSS_STATUS_SUCCESS:
4004 case NSS_STATUS_NOTFOUND:
4009 case NSS_STATUS_TRYAGAIN:
4022 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
4025 static struct group grp;
4027 static int buflen = 1000;
4030 if (!b->fns->_nss_getgrgid_r) {
4035 buf = (char *)malloc(buflen);
4039 status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
4040 if (status == NSS_STATUS_TRYAGAIN) {
4042 buf = (char *)realloc(buf, buflen);
4048 if (status == NSS_STATUS_NOTFOUND) {
4052 if (status != NSS_STATUS_SUCCESS) {
4059 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
4060 gid_t gid, struct group *grdst,
4061 char *buf, size_t buflen, struct group **grdstp)
4065 (void) grdstp; /* unused */
4067 if (!b->fns->_nss_getgrgid_r) {
4071 ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
4073 case NSS_STATUS_SUCCESS:
4075 case NSS_STATUS_NOTFOUND:
4080 case NSS_STATUS_TRYAGAIN:
4093 static void nwrap_module_setgrent(struct nwrap_backend *b)
4095 if (!b->fns->_nss_setgrent) {
4099 b->fns->_nss_setgrent();
4102 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
4104 static struct group grp;
4106 static int buflen = 1024;
4109 if (!b->fns->_nss_getgrent_r) {
4114 buf = (char *)malloc(buflen);
4118 status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
4119 if (status == NSS_STATUS_TRYAGAIN) {
4121 buf = (char *)realloc(buf, buflen);
4127 if (status == NSS_STATUS_NOTFOUND) {
4131 if (status != NSS_STATUS_SUCCESS) {
4138 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
4139 struct group *grdst, char *buf,
4140 size_t buflen, struct group **grdstp)
4144 (void) grdstp; /* unused */
4146 if (!b->fns->_nss_getgrent_r) {
4150 ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
4152 case NSS_STATUS_SUCCESS:
4154 case NSS_STATUS_NOTFOUND:
4159 case NSS_STATUS_TRYAGAIN:
4172 static void nwrap_module_endgrent(struct nwrap_backend *b)
4174 if (!b->fns->_nss_endgrent) {
4178 b->fns->_nss_endgrent();
4181 /****************************************************************************
4183 ***************************************************************************/
4185 static struct passwd *nwrap_getpwnam(const char *name)
4190 for (i=0; i < nwrap_main_global->num_backends; i++) {
4191 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4192 pwd = b->ops->nw_getpwnam(b, name);
4201 struct passwd *getpwnam(const char *name)
4203 if (!nss_wrapper_enabled()) {
4204 return libc_getpwnam(name);
4207 return nwrap_getpwnam(name);
4210 /****************************************************************************
4212 ***************************************************************************/
4214 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
4215 char *buf, size_t buflen, struct passwd **pwdstp)
4219 for (i=0; i < nwrap_main_global->num_backends; i++) {
4220 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4221 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
4222 if (ret == ENOENT) {
4231 #ifdef HAVE_GETPWNAM_R
4232 # ifdef HAVE_SOLARIS_GETPWNAM_R
4233 int getpwnam_r(const char *name, struct passwd *pwdst,
4234 char *buf, int buflen, struct passwd **pwdstp)
4235 # else /* HAVE_SOLARIS_GETPWNAM_R */
4236 int getpwnam_r(const char *name, struct passwd *pwdst,
4237 char *buf, size_t buflen, struct passwd **pwdstp)
4238 # endif /* HAVE_SOLARIS_GETPWNAM_R */
4240 if (!nss_wrapper_enabled()) {
4241 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4244 return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4248 /****************************************************************************
4250 ***************************************************************************/
4252 static struct passwd *nwrap_getpwuid(uid_t uid)
4257 for (i=0; i < nwrap_main_global->num_backends; i++) {
4258 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4259 pwd = b->ops->nw_getpwuid(b, uid);
4268 struct passwd *getpwuid(uid_t uid)
4270 if (!nss_wrapper_enabled()) {
4271 return libc_getpwuid(uid);
4274 return nwrap_getpwuid(uid);
4277 /****************************************************************************
4279 ***************************************************************************/
4281 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
4282 char *buf, size_t buflen, struct passwd **pwdstp)
4286 for (i=0; i < nwrap_main_global->num_backends; i++) {
4287 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4288 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
4289 if (ret == ENOENT) {
4298 #ifdef HAVE_SOLARIS_GETPWUID_R
4299 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4300 char *buf, int buflen, struct passwd **pwdstp)
4302 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4303 char *buf, size_t buflen, struct passwd **pwdstp)
4306 if (!nss_wrapper_enabled()) {
4307 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4310 return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4313 /****************************************************************************
4315 ***************************************************************************/
4317 static void nwrap_setpwent(void)
4321 for (i=0; i < nwrap_main_global->num_backends; i++) {
4322 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4323 b->ops->nw_setpwent(b);
4329 if (!nss_wrapper_enabled()) {
4337 /****************************************************************************
4339 ***************************************************************************/
4341 static struct passwd *nwrap_getpwent(void)
4346 for (i=0; i < nwrap_main_global->num_backends; i++) {
4347 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4348 pwd = b->ops->nw_getpwent(b);
4357 struct passwd *getpwent(void)
4359 if (!nss_wrapper_enabled()) {
4360 return libc_getpwent();
4363 return nwrap_getpwent();
4366 /****************************************************************************
4368 ***************************************************************************/
4370 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
4371 size_t buflen, struct passwd **pwdstp)
4375 for (i=0; i < nwrap_main_global->num_backends; i++) {
4376 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4377 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
4378 if (ret == ENOENT) {
4387 #ifdef HAVE_SOLARIS_GETPWENT_R
4388 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
4390 struct passwd *pwdstp = NULL;
4393 if (!nss_wrapper_enabled()) {
4394 return libc_getpwent_r(pwdst, buf, buflen);
4396 rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
4403 #else /* HAVE_SOLARIS_GETPWENT_R */
4404 int getpwent_r(struct passwd *pwdst, char *buf,
4405 size_t buflen, struct passwd **pwdstp)
4407 if (!nss_wrapper_enabled()) {
4408 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
4411 return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
4413 #endif /* HAVE_SOLARIS_GETPWENT_R */
4415 /****************************************************************************
4417 ***************************************************************************/
4419 static void nwrap_endpwent(void)
4423 for (i=0; i < nwrap_main_global->num_backends; i++) {
4424 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4425 b->ops->nw_endpwent(b);
4431 if (!nss_wrapper_enabled()) {
4439 /****************************************************************************
4441 ***************************************************************************/
4443 static int nwrap_initgroups(const char *user, gid_t group)
4447 for (i=0; i < nwrap_main_global->num_backends; i++) {
4448 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4451 rc = b->ops->nw_initgroups(b, user, group);
4461 int initgroups(const char *user, gid_t group)
4463 if (!nss_wrapper_enabled()) {
4464 return libc_initgroups(user, group);
4467 return nwrap_initgroups(user, group);
4470 /****************************************************************************
4472 ***************************************************************************/
4474 static struct group *nwrap_getgrnam(const char *name)
4479 for (i=0; i < nwrap_main_global->num_backends; i++) {
4480 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4481 grp = b->ops->nw_getgrnam(b, name);
4490 struct group *getgrnam(const char *name)
4492 if (!nss_wrapper_enabled()) {
4493 return libc_getgrnam(name);
4496 return nwrap_getgrnam(name);
4499 /****************************************************************************
4501 ***************************************************************************/
4503 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
4504 char *buf, size_t buflen, struct group **grdstp)
4508 for (i=0; i < nwrap_main_global->num_backends; i++) {
4509 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4510 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
4511 if (ret == ENOENT) {
4520 #ifdef HAVE_GETGRNAM_R
4521 # ifdef HAVE_SOLARIS_GETGRNAM_R
4522 int getgrnam_r(const char *name, struct group *grp,
4523 char *buf, int buflen, struct group **pgrp)
4524 # else /* HAVE_SOLARIS_GETGRNAM_R */
4525 int getgrnam_r(const char *name, struct group *grp,
4526 char *buf, size_t buflen, struct group **pgrp)
4527 # endif /* HAVE_SOLARIS_GETGRNAM_R */
4529 if (!nss_wrapper_enabled()) {
4530 return libc_getgrnam_r(name,
4537 return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
4539 #endif /* HAVE_GETGRNAM_R */
4541 /****************************************************************************
4543 ***************************************************************************/
4545 static struct group *nwrap_getgrgid(gid_t gid)
4550 for (i=0; i < nwrap_main_global->num_backends; i++) {
4551 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4552 grp = b->ops->nw_getgrgid(b, gid);
4561 struct group *getgrgid(gid_t gid)
4563 if (!nss_wrapper_enabled()) {
4564 return libc_getgrgid(gid);
4567 return nwrap_getgrgid(gid);
4570 /****************************************************************************
4572 ***************************************************************************/
4574 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
4575 char *buf, size_t buflen, struct group **grdstp)
4579 for (i=0; i < nwrap_main_global->num_backends; i++) {
4580 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4581 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
4582 if (ret == ENOENT) {
4591 #ifdef HAVE_GETGRGID_R
4592 # ifdef HAVE_SOLARIS_GETGRGID_R
4593 int getgrgid_r(gid_t gid, struct group *grdst,
4594 char *buf, int buflen, struct group **grdstp)
4595 # else /* HAVE_SOLARIS_GETGRGID_R */
4596 int getgrgid_r(gid_t gid, struct group *grdst,
4597 char *buf, size_t buflen, struct group **grdstp)
4598 # endif /* HAVE_SOLARIS_GETGRGID_R */
4600 if (!nss_wrapper_enabled()) {
4601 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4604 return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4608 /****************************************************************************
4610 ***************************************************************************/
4612 static void nwrap_setgrent(void)
4616 for (i=0; i < nwrap_main_global->num_backends; i++) {
4617 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4618 b->ops->nw_setgrent(b);
4622 #ifdef HAVE_BSD_SETGRENT
4628 if (!nss_wrapper_enabled()) {
4636 #ifdef HAVE_BSD_SETGRENT
4643 /****************************************************************************
4645 ***************************************************************************/
4647 static struct group *nwrap_getgrent(void)
4652 for (i=0; i < nwrap_main_global->num_backends; i++) {
4653 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4654 grp = b->ops->nw_getgrent(b);
4663 struct group *getgrent(void)
4665 if (!nss_wrapper_enabled()) {
4666 return libc_getgrent();
4669 return nwrap_getgrent();
4672 /****************************************************************************
4674 ***************************************************************************/
4676 static int nwrap_getgrent_r(struct group *grdst, char *buf,
4677 size_t buflen, struct group **grdstp)
4681 for (i=0; i < nwrap_main_global->num_backends; i++) {
4682 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4683 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
4684 if (ret == ENOENT) {
4693 #ifdef HAVE_SOLARIS_GETGRENT_R
4694 struct group *getgrent_r(struct group *src, char *buf, int buflen)
4696 struct group *grdstp = NULL;
4699 if (!nss_wrapper_enabled()) {
4700 return libc_getgrent_r(src, buf, buflen);
4703 rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
4710 #else /* HAVE_SOLARIS_GETGRENT_R */
4711 int getgrent_r(struct group *src, char *buf,
4712 size_t buflen, struct group **grdstp)
4714 if (!nss_wrapper_enabled()) {
4715 return libc_getgrent_r(src, buf, buflen, grdstp);
4718 return nwrap_getgrent_r(src, buf, buflen, grdstp);
4720 #endif /* HAVE_SOLARIS_GETGRENT_R */
4722 /****************************************************************************
4724 ***************************************************************************/
4726 static void nwrap_endgrent(void)
4730 for (i=0; i < nwrap_main_global->num_backends; i++) {
4731 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4732 b->ops->nw_endgrent(b);
4738 if (!nss_wrapper_enabled()) {
4746 /****************************************************************************
4748 ***************************************************************************/
4750 #ifdef HAVE_GETGROUPLIST
4751 static int nwrap_getgrouplist(const char *user, gid_t group,
4752 gid_t *groups, int *ngroups)
4758 NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
4760 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
4762 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
4766 groups_tmp[0] = group;
4769 while ((grp = nwrap_getgrent()) != NULL) {
4772 NWRAP_LOG(NWRAP_LOG_DEBUG,
4773 "Inspecting %s for group membership",
4776 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
4778 if (group != grp->gr_gid &&
4779 (strcmp(user, grp->gr_mem[i]) == 0)) {
4781 NWRAP_LOG(NWRAP_LOG_DEBUG,
4782 "%s is member of %s",
4786 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
4788 NWRAP_LOG(NWRAP_LOG_ERROR,
4793 groups_tmp[count] = grp->gr_gid;
4802 NWRAP_LOG(NWRAP_LOG_DEBUG,
4803 "%s is member of %d groups",
4806 if (*ngroups < count) {
4813 memcpy(groups, groups_tmp, count * sizeof(gid_t));
4819 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
4821 if (!nss_wrapper_enabled()) {
4822 return libc_getgrouplist(user, group, groups, ngroups);
4825 return nwrap_getgrouplist(user, group, groups, ngroups);
4829 /**********************************************************
4831 **********************************************************/
4833 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
4835 #ifdef HAVE_SETSPENT
4836 static void nwrap_setspent(void)
4838 nwrap_files_setspent();
4843 if (!nss_wrapper_shadow_enabled()) {
4850 static struct spwd *nwrap_getspent(void)
4852 return nwrap_files_getspent();
4855 struct spwd *getspent(void)
4857 if (!nss_wrapper_shadow_enabled()) {
4861 return nwrap_getspent();
4864 static void nwrap_endspent(void)
4866 nwrap_files_endspent();
4871 if (!nss_wrapper_shadow_enabled()) {
4877 #endif /* HAVE_SETSPENT */
4879 static struct spwd *nwrap_getspnam(const char *name)
4881 return nwrap_files_getspnam(name);
4884 struct spwd *getspnam(const char *name)
4886 if (!nss_wrapper_shadow_enabled()) {
4890 return nwrap_getspnam(name);
4893 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
4895 /**********************************************************
4897 **********************************************************/
4899 static void nwrap_sethostent(int stayopen) {
4900 (void) stayopen; /* ignored */
4902 nwrap_files_sethostent();
4905 #ifdef HAVE_SOLARIS_SETHOSTENT
4906 int sethostent(int stayopen)
4908 if (!nss_wrapper_hosts_enabled()) {
4909 libc_sethostent(stayopen);
4913 nwrap_sethostent(stayopen);
4917 #else /* HAVE_SOLARIS_SETHOSTENT */
4918 void sethostent(int stayopen)
4920 if (!nss_wrapper_hosts_enabled()) {
4921 libc_sethostent(stayopen);
4925 nwrap_sethostent(stayopen);
4927 #endif /* HAVE_SOLARIS_SETHOSTENT */
4929 static struct hostent *nwrap_gethostent(void)
4931 return nwrap_files_gethostent();
4934 struct hostent *gethostent(void) {
4935 if (!nss_wrapper_hosts_enabled()) {
4936 return libc_gethostent();
4939 return nwrap_gethostent();
4942 static void nwrap_endhostent(void) {
4943 nwrap_files_endhostent();
4946 #ifdef HAVE_SOLARIS_ENDHOSTENT
4947 int endhostent(void)
4949 if (!nss_wrapper_hosts_enabled()) {
4958 #else /* HAVE_SOLARIS_ENDHOSTENT */
4959 void endhostent(void)
4961 if (!nss_wrapper_hosts_enabled()) {
4968 #endif /* HAVE_SOLARIS_ENDHOSTENT */
4971 /* BSD implementation stores data in thread local storage but GLIBC does not */
4972 static __thread struct hostent user_he;
4973 static __thread struct nwrap_vector user_addrlist;
4975 static struct hostent user_he;
4976 static struct nwrap_vector user_addrlist;
4978 static struct hostent *nwrap_gethostbyname(const char *name)
4980 if (nwrap_files_gethostbyname(name, AF_UNSPEC, &user_he, &user_addrlist) == -1) {
4986 struct hostent *gethostbyname(const char *name)
4988 if (!nss_wrapper_hosts_enabled()) {
4989 return libc_gethostbyname(name);
4992 return nwrap_gethostbyname(name);
4995 /* This is a GNU extension - Also can be found on BSD systems */
4996 #ifdef HAVE_GETHOSTBYNAME2
4998 /* BSD implementation stores data in thread local storage but GLIBC not */
4999 static __thread struct hostent user_he2;
5000 static __thread struct nwrap_vector user_addrlist2;
5002 static struct hostent user_he2;
5003 static struct nwrap_vector user_addrlist2;
5005 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
5007 if (nwrap_files_gethostbyname(name, af, &user_he2, &user_addrlist2) == -1) {
5013 struct hostent *gethostbyname2(const char *name, int af)
5015 if (!nss_wrapper_hosts_enabled()) {
5016 return libc_gethostbyname2(name, af);
5019 return nwrap_gethostbyname2(name, af);
5023 static struct hostent *nwrap_gethostbyaddr(const void *addr,
5024 socklen_t len, int type)
5026 return nwrap_files_gethostbyaddr(addr, len, type);
5029 struct hostent *gethostbyaddr(const void *addr,
5030 socklen_t len, int type)
5032 if (!nss_wrapper_hosts_enabled()) {
5033 return libc_gethostbyaddr(addr, len, type);
5036 return nwrap_gethostbyaddr(addr, len, type);
5039 static const struct addrinfo default_hints =
5041 .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
5042 .ai_family = AF_UNSPEC,
5047 .ai_canonname = NULL,
5051 static int nwrap_convert_he_ai(const struct hostent *he,
5052 unsigned short port,
5053 const struct addrinfo *hints,
5054 struct addrinfo **pai,
5055 bool skip_canonname)
5057 struct addrinfo *ai;
5064 switch (he->h_addrtype) {
5066 socklen = sizeof(struct sockaddr_in);
5070 socklen = sizeof(struct sockaddr_in6);
5077 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
5083 ai->ai_family = he->h_addrtype;
5084 ai->ai_socktype = hints->ai_socktype;
5085 ai->ai_protocol = hints->ai_protocol;
5086 ai->ai_canonname = NULL;
5088 ai->ai_addrlen = socklen;
5089 ai->ai_addr = (void *)(ai + 1);
5091 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
5092 ai->ai_addr->sa_len = socklen;
5094 ai->ai_addr->sa_family = he->h_addrtype;
5096 switch (he->h_addrtype) {
5099 struct sockaddr_in *sinp =
5100 (struct sockaddr_in *) ai->ai_addr;
5102 memset(sinp, 0, sizeof(struct sockaddr_in));
5104 sinp->sin_port = htons(port);
5105 sinp->sin_family = AF_INET;
5107 memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
5108 memcpy(&sinp->sin_addr, he->h_addr_list[0], he->h_length);
5115 struct sockaddr_in6 *sin6p =
5116 (struct sockaddr_in6 *) ai->ai_addr;
5118 memset(sin6p, 0, sizeof(struct sockaddr_in6));
5120 sin6p->sin6_port = htons(port);
5121 sin6p->sin6_family = AF_INET6;
5123 memcpy(&sin6p->sin6_addr,
5133 if (he->h_name && !skip_canonname) {
5134 ai->ai_canonname = strdup(he->h_name);
5135 if (ai->ai_canonname == NULL) {
5145 static int nwrap_getaddrinfo(const char *node,
5146 const char *service,
5147 const struct addrinfo *hints,
5148 struct addrinfo **res)
5150 struct addrinfo *ai = NULL;
5151 struct addrinfo *ai_tail;
5152 unsigned short port = 0;
5162 .family = AF_UNSPEC,
5166 if (node == NULL && service == NULL) {
5170 if (hints == NULL) {
5171 hints = &default_hints;
5175 hints.ai_flags contains invalid flags; or, hints.ai_flags
5176 included AI_CANONNAME and name was NULL.
5178 if ((hints->ai_flags & AI_CANONNAME) && (node == NULL)) {
5179 return EAI_BADFLAGS;
5182 /* If no node has been specified, let glibc deal with it */
5185 struct addrinfo *p = NULL;
5187 ret = libc_getaddrinfo(node, service, hints, &p);
5195 if (service != NULL && service[0] != '\0') {
5196 const char *proto = NULL;
5202 sl = strtol(service, &end_ptr, 10);
5204 if (*end_ptr == '\0') {
5207 } else if (hints->ai_flags & AI_NUMERICSERV) {
5211 if (hints->ai_protocol != 0) {
5212 struct protoent *pent;
5214 pent = getprotobynumber(hints->ai_protocol);
5216 proto = pent->p_name;
5220 s = getservbyname(service, proto);
5224 port = ntohs(s->s_port);
5229 rc = inet_pton(AF_INET, node, &addr.in.v4);
5231 addr.family = AF_INET;
5234 if (addr.family == AF_UNSPEC) {
5235 rc = inet_pton(AF_INET6, node, &addr.in.v6);
5237 addr.family = AF_INET6;
5242 if (addr.family == AF_UNSPEC) {
5243 if (hints->ai_flags & AI_NUMERICHOST) {
5246 } else if ((hints->ai_family != AF_UNSPEC) &&
5247 (hints->ai_family != addr.family))
5249 return EAI_ADDRFAMILY;
5252 rc = nwrap_files_getaddrinfo(node, port, hints, &ai, &ai_tail);
5255 struct addrinfo *p = NULL;
5257 ret = libc_getaddrinfo(node, service, hints, &p);
5261 * nwrap_files_getaddrinfo failed, but libc was
5262 * successful -- use the result from libc.
5271 if (ai->ai_flags == 0) {
5272 ai->ai_flags = hints->ai_flags;
5274 if (ai->ai_socktype == 0) {
5275 ai->ai_socktype = SOCK_DGRAM;
5277 if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_DGRAM) {
5278 ai->ai_protocol = 17; /* UDP */
5279 } else if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_STREAM) {
5280 ai->ai_protocol = 6; /* TCP */
5283 if (hints->ai_socktype == 0) {
5285 struct addrinfo *ai_head = ai;
5286 struct addrinfo *ai_tmp;
5287 struct addrinfo *ai_new_tail = ai_tail;
5289 /* Add at least one more struct */
5292 ai_tmp = malloc(sizeof(struct addrinfo));
5293 memcpy(ai_tmp, ai_head, sizeof(struct addrinfo));
5294 ai_tmp->ai_next = NULL;
5296 /* We need a deep copy or freeaddrinfo() will blow up */
5297 if (ai_head->ai_canonname != NULL) {
5298 ai_tmp->ai_canonname =
5299 strdup(ai_head->ai_canonname);
5301 /* ai_head should point inside hints. */
5302 ai_tmp->ai_addr = ai_head->ai_addr;
5304 if (ai_head->ai_flags == 0) {
5305 ai_tmp->ai_flags = hints->ai_flags;
5307 if (ai_head->ai_socktype == SOCK_DGRAM) {
5308 ai_tmp->ai_socktype = SOCK_STREAM;
5309 } else if (ai_head->ai_socktype == SOCK_STREAM) {
5310 ai_tmp->ai_socktype = SOCK_DGRAM;
5312 if (ai_head->ai_socktype == SOCK_DGRAM) {
5313 ai_tmp->ai_protocol = 17; /* UDP */
5314 } else if (ai_head->ai_socktype == SOCK_STREAM) {
5315 ai_tmp->ai_protocol = 6; /* TCP */
5317 ai_new_tail->ai_next = ai_tmp;
5318 ai_new_tail = ai_tmp;
5320 if (ai_head == ai_tail) {
5323 ai_head = ai_head->ai_next;
5332 int getaddrinfo(const char *node, const char *service,
5333 const struct addrinfo *hints,
5334 struct addrinfo **res)
5336 if (!nss_wrapper_hosts_enabled()) {
5337 return libc_getaddrinfo(node, service, hints, res);
5340 return nwrap_getaddrinfo(node, service, hints, res);
5343 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
5344 char *host, size_t hostlen,
5345 char *serv, size_t servlen,
5349 struct servent *service;
5356 if (sa == NULL || salen < sizeof(sa_family_t)) {
5360 if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
5364 type = sa->sa_family;
5367 if (salen < sizeof(struct sockaddr_in))
5369 addr = &((const struct sockaddr_in *)sa)->sin_addr;
5370 addrlen = sizeof(((const struct sockaddr_in *)sa)->sin_addr);
5371 port = ntohs(((const struct sockaddr_in *)sa)->sin_port);
5375 if (salen < sizeof(struct sockaddr_in6))
5377 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
5378 addrlen = sizeof(((const struct sockaddr_in6 *)sa)->sin6_addr);
5379 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port);
5388 if ((flags & NI_NUMERICHOST) == 0) {
5389 he = nwrap_files_gethostbyaddr(addr, addrlen, type);
5390 if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
5393 if (he != NULL && he->h_name != NULL) {
5394 if (strlen(he->h_name) >= hostlen)
5395 return EAI_OVERFLOW;
5396 strcpy(host, he->h_name);
5397 if (flags & NI_NOFQDN)
5398 host[strcspn(host, ".")] = '\0';
5400 if (inet_ntop(type, addr, host, hostlen) == NULL)
5401 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
5407 if ((flags & NI_NUMERICSERV) == 0) {
5408 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
5409 service = getservbyport(htons(port), proto);
5411 if (service != NULL) {
5412 if (strlen(service->s_name) >= servlen)
5413 return EAI_OVERFLOW;
5414 strcpy(serv, service->s_name);
5416 if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
5417 return EAI_OVERFLOW;
5424 #ifdef HAVE_LINUX_GETNAMEINFO
5425 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5426 char *host, socklen_t hostlen,
5427 char *serv, socklen_t servlen,
5429 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
5430 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5431 char *host, socklen_t hostlen,
5432 char *serv, socklen_t servlen,
5435 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5436 char *host, size_t hostlen,
5437 char *serv, size_t servlen,
5441 if (!nss_wrapper_hosts_enabled()) {
5442 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5445 return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5448 static int nwrap_gethostname(char *name, size_t len)
5450 const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
5452 if (strlen(hostname) >= len) {
5453 errno = ENAMETOOLONG;
5456 snprintf(name, len, "%s", hostname);
5461 #ifdef HAVE_SOLARIS_GETHOSTNAME
5462 int gethostname(char *name, int len)
5463 #else /* HAVE_SOLARIS_GETHOSTNAME */
5464 int gethostname(char *name, size_t len)
5465 #endif /* HAVE_SOLARIS_GETHOSTNAME */
5467 if (!nwrap_hostname_enabled()) {
5468 return libc_gethostname(name, len);
5471 return nwrap_gethostname(name, len);
5474 /****************************
5476 ***************************/
5479 * This function is called when the library is unloaded and makes sure that
5480 * sockets get closed and the unix file for the socket are unlinked.
5482 void nwrap_destructor(void)
5487 if (nwrap_main_global != NULL) {
5488 struct nwrap_main *m = nwrap_main_global;
5491 SAFE_FREE(m->libc->fns);
5492 if (m->libc->handle != NULL) {
5493 dlclose(m->libc->handle);
5495 if (m->libc->nsl_handle != NULL) {
5496 dlclose(m->libc->nsl_handle);
5498 if (m->libc->sock_handle != NULL) {
5499 dlclose(m->libc->sock_handle);
5504 for (i = 0; i < m->num_backends; i++) {
5505 struct nwrap_backend *b = &(m->backends[i]);
5507 if (b->so_handle != NULL) {
5508 dlclose(b->so_handle);
5512 SAFE_FREE(m->backends);
5515 if (nwrap_pw_global.cache != NULL) {
5516 struct nwrap_cache *c = nwrap_pw_global.cache;
5518 nwrap_files_cache_unload(c);
5524 SAFE_FREE(nwrap_pw_global.list);
5525 nwrap_pw_global.num = 0;
5528 if (nwrap_gr_global.cache != NULL) {
5529 struct nwrap_cache *c = nwrap_gr_global.cache;
5531 nwrap_files_cache_unload(c);
5537 SAFE_FREE(nwrap_gr_global.list);
5538 nwrap_pw_global.num = 0;
5541 if (nwrap_he_global.cache != NULL) {
5542 struct nwrap_cache *c = nwrap_he_global.cache;
5544 nwrap_files_cache_unload(c);
5550 nwrap_he_global.num = 0;