2 * Copyright (C) Stefan Metzmacher 2007 <metze@samba.org>
3 * Copyright (C) Guenther Deschner 2009 <gd@samba.org>
4 * Copyright (C) Andreas Schneider 2013 <asn@samba.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the author nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/types.h>
42 #include <sys/socket.h>
55 #include <netinet/in.h>
61 * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h gives us
62 * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
65 #ifndef _POSIX_PTHREAD_SEMANTICS
66 #define _POSIX_PTHREAD_SEMANTICS
73 #endif /* HAVE_SHADOW_H */
76 #include <arpa/inet.h>
77 #include <netinet/in.h>
81 #if defined(HAVE_NSS_H)
85 typedef enum nss_status NSS_STATUS
;
86 #elif defined(HAVE_NSS_COMMON_H)
88 #include <nss_common.h>
89 #include <nss_dbdefs.h>
92 typedef nss_status_t NSS_STATUS
;
94 # define NSS_STATUS_SUCCESS NSS_SUCCESS
95 # define NSS_STATUS_NOTFOUND NSS_NOTFOUND
96 # define NSS_STATUS_UNAVAIL NSS_UNAVAIL
97 # define NSS_STATUS_TRYAGAIN NSS_TRYAGAIN
99 # error "No nsswitch support detected"
103 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
111 #define EAI_NODATA EAI_NONAME
114 #ifndef EAI_ADDRFAMILY
115 #define EAI_ADDRFAMILY EAI_FAMILY
119 #define __STRING(x) #x
122 #ifndef __STRINGSTRING
123 #define __STRINGSTRING(x) __STRING(x)
127 #define __LINESTR__ __STRINGSTRING(__LINE__)
131 #define __location__ __FILE__ ":" __LINESTR__
135 #define DNS_NAME_MAX 255
138 /* GCC have printf type attribute check. */
139 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
140 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
142 #define PRINTF_ATTRIBUTE(a,b)
143 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
145 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
146 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
148 #define DESTRUCTOR_ATTRIBUTE
149 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
151 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
154 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
157 #ifndef discard_const
158 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
161 #ifndef discard_const_p
162 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
166 #define NWRAP_INET_ADDRSTRLEN INET6_ADDRSTRLEN
168 #define NWRAP_INET_ADDRSTRLEN INET_ADDRSTRLEN
171 #define NWRAP_LOCK(m) do { \
172 pthread_mutex_lock(&( m ## _mutex)); \
175 #define NWRAP_UNLOCK(m) do { \
176 pthread_mutex_unlock(&( m ## _mutex)); \
180 static bool nwrap_initialized
= false;
181 static pthread_mutex_t nwrap_initialized_mutex
= PTHREAD_MUTEX_INITIALIZER
;
183 /* The mutex or accessing the id */
184 static pthread_mutex_t nwrap_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
185 static pthread_mutex_t nwrap_gr_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
186 static pthread_mutex_t nwrap_he_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
187 static pthread_mutex_t nwrap_pw_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
188 static pthread_mutex_t nwrap_sp_global_mutex
= PTHREAD_MUTEX_INITIALIZER
;
190 /* Add new global locks here please */
191 /* Also don't forget to add locks to
192 * nwrap_init() function.
194 # define NWRAP_LOCK_ALL do { \
195 NWRAP_LOCK(nwrap_initialized); \
196 NWRAP_LOCK(nwrap_global); \
197 NWRAP_LOCK(nwrap_gr_global); \
198 NWRAP_LOCK(nwrap_he_global); \
199 NWRAP_LOCK(nwrap_pw_global); \
200 NWRAP_LOCK(nwrap_sp_global); \
203 # define NWRAP_UNLOCK_ALL do {\
204 NWRAP_UNLOCK(nwrap_sp_global); \
205 NWRAP_UNLOCK(nwrap_pw_global); \
206 NWRAP_UNLOCK(nwrap_he_global); \
207 NWRAP_UNLOCK(nwrap_gr_global); \
208 NWRAP_UNLOCK(nwrap_global); \
209 NWRAP_UNLOCK(nwrap_initialized); \
212 static void nwrap_thread_prepare(void)
217 static void nwrap_thread_parent(void)
222 static void nwrap_thread_child(void)
227 enum nwrap_dbglvl_e
{
235 # define NWRAP_LOG(...)
238 static void nwrap_log(enum nwrap_dbglvl_e dbglvl
, const char *func
, const char *format
, ...) PRINTF_ATTRIBUTE(3, 4);
239 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
241 static void nwrap_log(enum nwrap_dbglvl_e dbglvl
,
243 const char *format
, ...)
248 unsigned int lvl
= 0;
251 d
= getenv("NSS_WRAPPER_DEBUGLEVEL");
256 va_start(va
, format
);
257 vsnprintf(buffer
, sizeof(buffer
), format
, va
);
262 case NWRAP_LOG_ERROR
:
264 "NWRAP_ERROR(%d) - %s: %s\n",
269 "NWRAP_WARN(%d) - %s: %s\n",
272 case NWRAP_LOG_DEBUG
:
274 "NWRAP_DEBUG(%d) - %s: %s\n",
277 case NWRAP_LOG_TRACE
:
279 "NWRAP_TRACE(%d) - %s: %s\n",
285 #endif /* NDEBUG NWRAP_LOG */
287 struct nwrap_libc_fns
{
288 struct passwd
*(*_libc_getpwnam
)(const char *name
);
289 int (*_libc_getpwnam_r
)(const char *name
, struct passwd
*pwd
,
290 char *buf
, size_t buflen
, struct passwd
**result
);
291 struct passwd
*(*_libc_getpwuid
)(uid_t uid
);
292 int (*_libc_getpwuid_r
)(uid_t uid
, struct passwd
*pwd
, char *buf
, size_t buflen
, struct passwd
**result
);
293 void (*_libc_setpwent
)(void);
294 struct passwd
*(*_libc_getpwent
)(void);
295 #ifdef HAVE_SOLARIS_GETPWENT_R
296 struct passwd
*(*_libc_getpwent_r
)(struct passwd
*pwbuf
, char *buf
, size_t buflen
);
298 int (*_libc_getpwent_r
)(struct passwd
*pwbuf
, char *buf
, size_t buflen
, struct passwd
**pwbufp
);
300 void (*_libc_endpwent
)(void);
301 int (*_libc_initgroups
)(const char *user
, gid_t gid
);
302 struct group
*(*_libc_getgrnam
)(const char *name
);
303 int (*_libc_getgrnam_r
)(const char *name
, struct group
*grp
, char *buf
, size_t buflen
, struct group
**result
);
304 struct group
*(*_libc_getgrgid
)(gid_t gid
);
305 int (*_libc_getgrgid_r
)(gid_t gid
, struct group
*grp
, char *buf
, size_t buflen
, struct group
**result
);
306 void (*_libc_setgrent
)(void);
307 struct group
*(*_libc_getgrent
)(void);
308 #ifdef HAVE_SOLARIS_GETGRENT_R
309 struct group
*(*_libc_getgrent_r
)(struct group
*group
, char *buf
, size_t buflen
);
311 int (*_libc_getgrent_r
)(struct group
*group
, char *buf
, size_t buflen
, struct group
**result
);
313 void (*_libc_endgrent
)(void);
314 int (*_libc_getgrouplist
)(const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
);
316 void (*_libc_sethostent
)(int stayopen
);
317 struct hostent
*(*_libc_gethostent
)(void);
318 void (*_libc_endhostent
)(void);
320 struct hostent
*(*_libc_gethostbyname
)(const char *name
);
321 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
322 struct hostent
*(*_libc_gethostbyname2
)(const char *name
, int af
);
324 struct hostent
*(*_libc_gethostbyaddr
)(const void *addr
, socklen_t len
, int type
);
326 int (*_libc_getaddrinfo
)(const char *node
, const char *service
,
327 const struct addrinfo
*hints
,
328 struct addrinfo
**res
);
329 int (*_libc_getnameinfo
)(const struct sockaddr
*sa
, socklen_t salen
,
330 char *host
, size_t hostlen
,
331 char *serv
, size_t servlen
,
333 int (*_libc_gethostname
)(char *name
, size_t len
);
334 #ifdef HAVE_GETHOSTBYNAME_R
335 int (*_libc_gethostbyname_r
)(const char *name
,
337 char *buf
, size_t buflen
,
338 struct hostent
**result
, int *h_errnop
);
340 #ifdef HAVE_GETHOSTBYADDR_R
341 int (*_libc_gethostbyaddr_r
)(const void *addr
, socklen_t len
, int type
,
343 char *buf
, size_t buflen
,
344 struct hostent
**result
, int *h_errnop
);
348 struct nwrap_module_nss_fns
{
349 NSS_STATUS (*_nss_getpwnam_r
)(const char *name
, struct passwd
*result
, char *buffer
,
350 size_t buflen
, int *errnop
);
351 NSS_STATUS (*_nss_getpwuid_r
)(uid_t uid
, struct passwd
*result
, char *buffer
,
352 size_t buflen
, int *errnop
);
353 NSS_STATUS (*_nss_setpwent
)(void);
354 NSS_STATUS (*_nss_getpwent_r
)(struct passwd
*result
, char *buffer
,
355 size_t buflen
, int *errnop
);
356 NSS_STATUS (*_nss_endpwent
)(void);
357 NSS_STATUS (*_nss_initgroups
)(const char *user
, gid_t group
, long int *start
,
358 long int *size
, gid_t
**groups
, long int limit
, int *errnop
);
359 NSS_STATUS (*_nss_getgrnam_r
)(const char *name
, struct group
*result
, char *buffer
,
360 size_t buflen
, int *errnop
);
361 NSS_STATUS (*_nss_getgrgid_r
)(gid_t gid
, struct group
*result
, char *buffer
,
362 size_t buflen
, int *errnop
);
363 NSS_STATUS (*_nss_setgrent
)(void);
364 NSS_STATUS (*_nss_getgrent_r
)(struct group
*result
, char *buffer
,
365 size_t buflen
, int *errnop
);
366 NSS_STATUS (*_nss_endgrent
)(void);
369 struct nwrap_backend
{
373 struct nwrap_ops
*ops
;
374 struct nwrap_module_nss_fns
*fns
;
378 struct passwd
* (*nw_getpwnam
)(struct nwrap_backend
*b
,
380 int (*nw_getpwnam_r
)(struct nwrap_backend
*b
,
381 const char *name
, struct passwd
*pwdst
,
382 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
383 struct passwd
* (*nw_getpwuid
)(struct nwrap_backend
*b
,
385 int (*nw_getpwuid_r
)(struct nwrap_backend
*b
,
386 uid_t uid
, struct passwd
*pwdst
,
387 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
388 void (*nw_setpwent
)(struct nwrap_backend
*b
);
389 struct passwd
* (*nw_getpwent
)(struct nwrap_backend
*b
);
390 int (*nw_getpwent_r
)(struct nwrap_backend
*b
,
391 struct passwd
*pwdst
, char *buf
,
392 size_t buflen
, struct passwd
**pwdstp
);
393 void (*nw_endpwent
)(struct nwrap_backend
*b
);
394 int (*nw_initgroups
)(struct nwrap_backend
*b
,
395 const char *user
, gid_t group
);
396 struct group
* (*nw_getgrnam
)(struct nwrap_backend
*b
,
398 int (*nw_getgrnam_r
)(struct nwrap_backend
*b
,
399 const char *name
, struct group
*grdst
,
400 char *buf
, size_t buflen
, struct group
**grdstp
);
401 struct group
* (*nw_getgrgid
)(struct nwrap_backend
*b
,
403 int (*nw_getgrgid_r
)(struct nwrap_backend
*b
,
404 gid_t gid
, struct group
*grdst
,
405 char *buf
, size_t buflen
, struct group
**grdstp
);
406 void (*nw_setgrent
)(struct nwrap_backend
*b
);
407 struct group
* (*nw_getgrent
)(struct nwrap_backend
*b
);
408 int (*nw_getgrent_r
)(struct nwrap_backend
*b
,
409 struct group
*grdst
, char *buf
,
410 size_t buflen
, struct group
**grdstp
);
411 void (*nw_endgrent
)(struct nwrap_backend
*b
);
414 /* Public prototypes */
416 bool nss_wrapper_enabled(void);
417 bool nss_wrapper_shadow_enabled(void);
418 bool nss_wrapper_hosts_enabled(void);
420 /* prototypes for files backend */
423 static struct passwd
*nwrap_files_getpwnam(struct nwrap_backend
*b
,
425 static int nwrap_files_getpwnam_r(struct nwrap_backend
*b
,
426 const char *name
, struct passwd
*pwdst
,
427 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
428 static struct passwd
*nwrap_files_getpwuid(struct nwrap_backend
*b
,
430 static int nwrap_files_getpwuid_r(struct nwrap_backend
*b
,
431 uid_t uid
, struct passwd
*pwdst
,
432 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
433 static void nwrap_files_setpwent(struct nwrap_backend
*b
);
434 static struct passwd
*nwrap_files_getpwent(struct nwrap_backend
*b
);
435 static int nwrap_files_getpwent_r(struct nwrap_backend
*b
,
436 struct passwd
*pwdst
, char *buf
,
437 size_t buflen
, struct passwd
**pwdstp
);
438 static void nwrap_files_endpwent(struct nwrap_backend
*b
);
439 static int nwrap_files_initgroups(struct nwrap_backend
*b
,
440 const char *user
, gid_t group
);
441 static struct group
*nwrap_files_getgrnam(struct nwrap_backend
*b
,
443 static int nwrap_files_getgrnam_r(struct nwrap_backend
*b
,
444 const char *name
, struct group
*grdst
,
445 char *buf
, size_t buflen
, struct group
**grdstp
);
446 static struct group
*nwrap_files_getgrgid(struct nwrap_backend
*b
,
448 static int nwrap_files_getgrgid_r(struct nwrap_backend
*b
,
449 gid_t gid
, struct group
*grdst
,
450 char *buf
, size_t buflen
, struct group
**grdstp
);
451 static void nwrap_files_setgrent(struct nwrap_backend
*b
);
452 static struct group
*nwrap_files_getgrent(struct nwrap_backend
*b
);
453 static int nwrap_files_getgrent_r(struct nwrap_backend
*b
,
454 struct group
*grdst
, char *buf
,
455 size_t buflen
, struct group
**grdstp
);
456 static void nwrap_files_endgrent(struct nwrap_backend
*b
);
458 /* prototypes for module backend */
460 static struct passwd
*nwrap_module_getpwent(struct nwrap_backend
*b
);
461 static int nwrap_module_getpwent_r(struct nwrap_backend
*b
,
462 struct passwd
*pwdst
, char *buf
,
463 size_t buflen
, struct passwd
**pwdstp
);
464 static struct passwd
*nwrap_module_getpwnam(struct nwrap_backend
*b
,
466 static int nwrap_module_getpwnam_r(struct nwrap_backend
*b
,
467 const char *name
, struct passwd
*pwdst
,
468 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
469 static struct passwd
*nwrap_module_getpwuid(struct nwrap_backend
*b
,
471 static int nwrap_module_getpwuid_r(struct nwrap_backend
*b
,
472 uid_t uid
, struct passwd
*pwdst
,
473 char *buf
, size_t buflen
, struct passwd
**pwdstp
);
474 static void nwrap_module_setpwent(struct nwrap_backend
*b
);
475 static void nwrap_module_endpwent(struct nwrap_backend
*b
);
476 static struct group
*nwrap_module_getgrent(struct nwrap_backend
*b
);
477 static int nwrap_module_getgrent_r(struct nwrap_backend
*b
,
478 struct group
*grdst
, char *buf
,
479 size_t buflen
, struct group
**grdstp
);
480 static struct group
*nwrap_module_getgrnam(struct nwrap_backend
*b
,
482 static int nwrap_module_getgrnam_r(struct nwrap_backend
*b
,
483 const char *name
, struct group
*grdst
,
484 char *buf
, size_t buflen
, struct group
**grdstp
);
485 static struct group
*nwrap_module_getgrgid(struct nwrap_backend
*b
,
487 static int nwrap_module_getgrgid_r(struct nwrap_backend
*b
,
488 gid_t gid
, struct group
*grdst
,
489 char *buf
, size_t buflen
, struct group
**grdstp
);
490 static void nwrap_module_setgrent(struct nwrap_backend
*b
);
491 static void nwrap_module_endgrent(struct nwrap_backend
*b
);
492 static int nwrap_module_initgroups(struct nwrap_backend
*b
,
493 const char *user
, gid_t group
);
495 struct nwrap_ops nwrap_files_ops
= {
496 .nw_getpwnam
= nwrap_files_getpwnam
,
497 .nw_getpwnam_r
= nwrap_files_getpwnam_r
,
498 .nw_getpwuid
= nwrap_files_getpwuid
,
499 .nw_getpwuid_r
= nwrap_files_getpwuid_r
,
500 .nw_setpwent
= nwrap_files_setpwent
,
501 .nw_getpwent
= nwrap_files_getpwent
,
502 .nw_getpwent_r
= nwrap_files_getpwent_r
,
503 .nw_endpwent
= nwrap_files_endpwent
,
504 .nw_initgroups
= nwrap_files_initgroups
,
505 .nw_getgrnam
= nwrap_files_getgrnam
,
506 .nw_getgrnam_r
= nwrap_files_getgrnam_r
,
507 .nw_getgrgid
= nwrap_files_getgrgid
,
508 .nw_getgrgid_r
= nwrap_files_getgrgid_r
,
509 .nw_setgrent
= nwrap_files_setgrent
,
510 .nw_getgrent
= nwrap_files_getgrent
,
511 .nw_getgrent_r
= nwrap_files_getgrent_r
,
512 .nw_endgrent
= nwrap_files_endgrent
,
515 struct nwrap_ops nwrap_module_ops
= {
516 .nw_getpwnam
= nwrap_module_getpwnam
,
517 .nw_getpwnam_r
= nwrap_module_getpwnam_r
,
518 .nw_getpwuid
= nwrap_module_getpwuid
,
519 .nw_getpwuid_r
= nwrap_module_getpwuid_r
,
520 .nw_setpwent
= nwrap_module_setpwent
,
521 .nw_getpwent
= nwrap_module_getpwent
,
522 .nw_getpwent_r
= nwrap_module_getpwent_r
,
523 .nw_endpwent
= nwrap_module_endpwent
,
524 .nw_initgroups
= nwrap_module_initgroups
,
525 .nw_getgrnam
= nwrap_module_getgrnam
,
526 .nw_getgrnam_r
= nwrap_module_getgrnam_r
,
527 .nw_getgrgid
= nwrap_module_getgrgid
,
528 .nw_getgrgid_r
= nwrap_module_getgrgid_r
,
529 .nw_setgrent
= nwrap_module_setgrent
,
530 .nw_getgrent
= nwrap_module_getgrent
,
531 .nw_getgrent_r
= nwrap_module_getgrent_r
,
532 .nw_endgrent
= nwrap_module_endgrent
,
539 struct nwrap_libc_fns
*fns
;
544 struct nwrap_backend
*backends
;
545 struct nwrap_libc
*libc
;
548 static struct nwrap_main
*nwrap_main_global
;
549 static struct nwrap_main __nwrap_main_global
;
554 static int nwrap_convert_he_ai(const struct hostent
*he
,
556 const struct addrinfo
*hints
,
557 struct addrinfo
**pai
,
558 bool skip_canonname
);
564 #define DEFAULT_VECTOR_CAPACITY 16
566 struct nwrap_vector
{
572 /* Macro returns pointer to first element of vector->items array.
574 * nwrap_vector is used as a memory backend which take care of
575 * memory allocations and other stuff like memory growing.
576 * nwrap_vectors should not be considered as some abstract structures.
577 * On this level, vectors are more handy than direct realloc/malloc
580 * nwrap_vector->items is array inside nwrap_vector which can be
581 * directly pointed by libc structure assembled by cwrap itself.
585 * 1) struct hostent contains char **h_addr_list element.
586 * 2) nwrap_vector holds array of pointers to addresses.
587 * It's easier to use vector to store results of
590 * Now, pretend that cwrap assembled struct hostent and
591 * we need to set h_addr_list to point to nwrap_vector.
592 * Idea behind is to shield users from internal nwrap_vector
594 * (Yes, not fully - array terminated by NULL is needed because
595 * it's result expected by libc function caller.)
601 * struct nwrap_vector *vector = malloc(sizeof(struct nwrap_vector));
602 * ... don't care about failed allocation now ...
604 * ... fill nwrap vector ...
607 * he.h_addr_list = nwrap_vector_head(vector);
610 #define nwrap_vector_head(vect) ((void *)((vect)->items))
612 #define nwrap_vector_foreach(item, vect, iter) \
613 for (iter = 0, (item) = (vect).items == NULL ? NULL : (vect).items[0]; \
615 (item) = (vect).items[++iter])
617 #define nwrap_vector_is_initialized(vector) ((vector)->items != NULL)
619 static inline bool nwrap_vector_init(struct nwrap_vector
*const vector
)
621 if (vector
== NULL
) {
625 /* count is initialized by ZERO_STRUCTP */
626 ZERO_STRUCTP(vector
);
627 vector
->items
= malloc(sizeof(void *) * (DEFAULT_VECTOR_CAPACITY
+ 1));
628 if (vector
->items
== NULL
) {
631 vector
->capacity
= DEFAULT_VECTOR_CAPACITY
;
632 memset(vector
->items
, '\0', sizeof(void *) * (DEFAULT_VECTOR_CAPACITY
+ 1));
637 static bool nwrap_vector_add_item(struct nwrap_vector
*vector
, void *const item
)
639 assert (vector
!= NULL
);
641 if (vector
->items
== NULL
) {
642 nwrap_vector_init(vector
);
645 if (vector
->count
== vector
->capacity
) {
646 /* Items array _MUST_ be NULL terminated because it's passed
647 * as result to caller which expect NULL terminated array from libc.
649 void **items
= realloc(vector
->items
, sizeof(void *) * ((vector
->capacity
* 2) + 1));
653 vector
->items
= items
;
655 /* Don't count ending NULL to capacity */
656 vector
->capacity
*= 2;
659 vector
->items
[vector
->count
] = item
;
662 vector
->items
[vector
->count
] = NULL
;
667 static bool nwrap_vector_merge(struct nwrap_vector
*dst
,
668 struct nwrap_vector
*src
)
670 void **dst_items
= NULL
;
673 if (src
->count
== 0) {
677 count
= dst
->count
+ src
->count
;
679 /* We don't need reallocation if we have enough capacity. */
680 if (src
->count
> (dst
->capacity
- dst
->count
)) {
681 dst_items
= (void **)realloc(dst
->items
, (count
+ 1) * sizeof(void *));
682 if (dst_items
== NULL
) {
685 dst
->items
= dst_items
;
686 dst
->capacity
= count
;
689 memcpy((void *)(((long *)dst
->items
) + dst
->count
),
691 src
->count
* sizeof(void *));
704 struct nwrap_vector lines
;
706 bool (*parse_line
)(struct nwrap_cache
*, char *line
);
707 void (*unload
)(struct nwrap_cache
*);
712 struct nwrap_cache
*cache
;
719 struct nwrap_cache __nwrap_cache_pw
;
720 struct nwrap_pw nwrap_pw_global
;
722 static bool nwrap_pw_parse_line(struct nwrap_cache
*nwrap
, char *line
);
723 static void nwrap_pw_unload(struct nwrap_cache
*nwrap
);
726 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
728 struct nwrap_cache
*cache
;
735 struct nwrap_cache __nwrap_cache_sp
;
736 struct nwrap_sp nwrap_sp_global
;
738 static bool nwrap_sp_parse_line(struct nwrap_cache
*nwrap
, char *line
);
739 static void nwrap_sp_unload(struct nwrap_cache
*nwrap
);
740 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
744 struct nwrap_cache
*cache
;
751 struct nwrap_cache __nwrap_cache_gr
;
752 struct nwrap_gr nwrap_gr_global
;
755 static bool nwrap_he_parse_line(struct nwrap_cache
*nwrap
, char *line
);
756 static void nwrap_he_unload(struct nwrap_cache
*nwrap
);
758 struct nwrap_addrdata
{
759 unsigned char host_addr
[16]; /* IPv4 or IPv6 address */
762 static size_t max_hostents
= 100;
764 struct nwrap_entdata
{
765 struct nwrap_addrdata addr
;
768 struct nwrap_vector nwrap_addrdata
;
770 ssize_t aliases_count
;
773 struct nwrap_entlist
{
774 struct nwrap_entlist
*next
;
775 struct nwrap_entdata
*ed
;
779 struct nwrap_cache
*cache
;
781 struct nwrap_vector entries
;
782 struct nwrap_vector lists
;
788 static struct nwrap_cache __nwrap_cache_he
;
789 static struct nwrap_he nwrap_he_global
;
792 /*********************************************************
794 *********************************************************/
796 static void nwrap_init(void);
797 static bool nwrap_gr_parse_line(struct nwrap_cache
*nwrap
, char *line
);
798 static void nwrap_gr_unload(struct nwrap_cache
*nwrap
);
799 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE
;
801 /*********************************************************
802 * NWRAP LIBC LOADER FUNCTIONS
803 *********************************************************/
812 static const char *nwrap_str_lib(enum nwrap_lib lib
)
819 case NWRAP_LIBSOCKET
:
823 /* Compiler would warn us about unhandled enum value if we get here */
828 static void *nwrap_load_lib_handle(enum nwrap_lib lib
)
830 int flags
= RTLD_LAZY
;
835 flags
|= RTLD_DEEPBIND
;
841 handle
= nwrap_main_global
->libc
->nsl_handle
;
842 if (handle
== NULL
) {
843 for (i
= 10; i
>= 0; i
--) {
844 char soname
[256] = {0};
846 snprintf(soname
, sizeof(soname
), "libnsl.so.%d", i
);
847 handle
= dlopen(soname
, flags
);
848 if (handle
!= NULL
) {
853 nwrap_main_global
->libc
->nsl_handle
= handle
;
858 case NWRAP_LIBSOCKET
:
859 #ifdef HAVE_LIBSOCKET
860 handle
= nwrap_main_global
->libc
->sock_handle
;
861 if (handle
== NULL
) {
862 for (i
= 10; i
>= 0; i
--) {
863 char soname
[256] = {0};
865 snprintf(soname
, sizeof(soname
), "libsocket.so.%d", i
);
866 handle
= dlopen(soname
, flags
);
867 if (handle
!= NULL
) {
872 nwrap_main_global
->libc
->sock_handle
= handle
;
878 handle
= nwrap_main_global
->libc
->handle
;
879 if (handle
== NULL
) {
880 for (i
= 10; i
>= 0; i
--) {
881 char soname
[256] = {0};
883 snprintf(soname
, sizeof(soname
), "libc.so.%d", i
);
884 handle
= dlopen(soname
, flags
);
885 if (handle
!= NULL
) {
890 nwrap_main_global
->libc
->handle
= handle
;
895 if (handle
== NULL
) {
897 handle
= nwrap_main_global
->libc
->handle
898 = nwrap_main_global
->libc
->sock_handle
899 = nwrap_main_global
->libc
->nsl_handle
902 NWRAP_LOG(NWRAP_LOG_ERROR
,
903 "Failed to dlopen library: %s\n",
912 static void *_nwrap_load_lib_function(enum nwrap_lib lib
, const char *fn_name
)
919 handle
= nwrap_load_lib_handle(lib
);
921 func
= dlsym(handle
, fn_name
);
923 NWRAP_LOG(NWRAP_LOG_ERROR
,
924 "Failed to find %s: %s\n",
929 NWRAP_LOG(NWRAP_LOG_TRACE
,
931 fn_name
, nwrap_str_lib(lib
));
935 #define nwrap_load_lib_function(lib, fn_name) \
936 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
937 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
938 _nwrap_load_lib_function(lib, #fn_name); \
941 /* INTERNAL HELPER FUNCTIONS */
942 static void nwrap_lines_unload(struct nwrap_cache
*const nwrap
)
946 nwrap_vector_foreach(item
, nwrap
->lines
, p
) {
947 /* Maybe some vectors were merged ... */
950 SAFE_FREE(nwrap
->lines
.items
);
951 ZERO_STRUCTP(&nwrap
->lines
);
957 * Functions expeciall from libc need to be loaded individually, you can't load
958 * all at once or gdb will segfault at startup. The same applies to valgrind and
959 * has probably something todo with with the linker.
960 * So we need load each function at the point it is called the first time.
962 static struct passwd
*libc_getpwnam(const char *name
)
964 nwrap_load_lib_function(NWRAP_LIBC
, getpwnam
);
966 return nwrap_main_global
->libc
->fns
->_libc_getpwnam(name
);
969 #ifdef HAVE_GETPWNAM_R
970 static int libc_getpwnam_r(const char *name
,
974 struct passwd
**result
)
976 #ifdef HAVE___POSIX_GETPWNAM_R
977 if (nwrap_main_global
->libc
->fns
->_libc_getpwnam_r
== NULL
) {
978 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getpwnam_r
) =
979 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getpwnam_r");
982 nwrap_load_lib_function(NWRAP_LIBC
, getpwnam_r
);
985 return nwrap_main_global
->libc
->fns
->_libc_getpwnam_r(name
,
993 static struct passwd
*libc_getpwuid(uid_t uid
)
995 nwrap_load_lib_function(NWRAP_LIBC
, getpwuid
);
997 return nwrap_main_global
->libc
->fns
->_libc_getpwuid(uid
);
1000 #ifdef HAVE_GETPWUID_R
1001 static int libc_getpwuid_r(uid_t uid
,
1005 struct passwd
**result
)
1007 #ifdef HAVE___POSIX_GETPWUID_R
1008 if (nwrap_main_global
->libc
->fns
->_libc_getpwuid_r
== NULL
) {
1009 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getpwuid_r
) =
1010 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getpwuid_r");
1013 nwrap_load_lib_function(NWRAP_LIBC
, getpwuid_r
);
1016 return nwrap_main_global
->libc
->fns
->_libc_getpwuid_r(uid
,
1024 static inline void str_tolower(char *dst
, char *src
)
1026 register char *src_tmp
= src
;
1027 register char *dst_tmp
= dst
;
1029 while (*src_tmp
!= '\0') {
1030 *dst_tmp
= tolower(*src_tmp
);
1036 static bool str_tolower_copy(char **dst_name
, const char *const src_name
)
1040 if ((dst_name
== NULL
) || (src_name
== NULL
)) {
1044 h_name_lower
= strdup(src_name
);
1045 if (h_name_lower
== NULL
) {
1046 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Out of memory while strdup");
1050 str_tolower(h_name_lower
, h_name_lower
);
1051 *dst_name
= h_name_lower
;
1055 static void libc_setpwent(void)
1057 nwrap_load_lib_function(NWRAP_LIBC
, setpwent
);
1059 nwrap_main_global
->libc
->fns
->_libc_setpwent();
1062 static struct passwd
*libc_getpwent(void)
1064 nwrap_load_lib_function(NWRAP_LIBC
, getpwent
);
1066 return nwrap_main_global
->libc
->fns
->_libc_getpwent();
1069 #ifdef HAVE_SOLARIS_GETPWENT_R
1070 static struct passwd
*libc_getpwent_r(struct passwd
*pwdst
,
1074 nwrap_load_lib_function(NWRAP_LIBC
, getpwent_r
);
1076 return nwrap_main_global
->libc
->fns
->_libc_getpwent_r(pwdst
,
1080 #else /* HAVE_SOLARIS_GETPWENT_R */
1081 static int libc_getpwent_r(struct passwd
*pwdst
,
1084 struct passwd
**pwdstp
)
1086 nwrap_load_lib_function(NWRAP_LIBC
, getpwent_r
);
1088 return nwrap_main_global
->libc
->fns
->_libc_getpwent_r(pwdst
,
1093 #endif /* HAVE_SOLARIS_GETPWENT_R */
1095 static void libc_endpwent(void)
1097 nwrap_load_lib_function(NWRAP_LIBC
, endpwent
);
1099 nwrap_main_global
->libc
->fns
->_libc_endpwent();
1102 static int libc_initgroups(const char *user
, gid_t gid
)
1104 nwrap_load_lib_function(NWRAP_LIBC
, initgroups
);
1106 return nwrap_main_global
->libc
->fns
->_libc_initgroups(user
, gid
);
1109 static struct group
*libc_getgrnam(const char *name
)
1111 nwrap_load_lib_function(NWRAP_LIBC
, getgrnam
);
1113 return nwrap_main_global
->libc
->fns
->_libc_getgrnam(name
);
1116 #ifdef HAVE_GETGRNAM_R
1117 static int libc_getgrnam_r(const char *name
,
1121 struct group
**result
)
1123 #ifdef HAVE___POSIX_GETGRNAM_R
1124 if (nwrap_main_global
->libc
->fns
->_libc_getgrnam_r
== NULL
) {
1125 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getgrnam_r
) =
1126 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getgrnam_r");
1129 nwrap_load_lib_function(NWRAP_LIBC
, getgrnam_r
);
1132 return nwrap_main_global
->libc
->fns
->_libc_getgrnam_r(name
,
1140 static struct group
*libc_getgrgid(gid_t gid
)
1142 nwrap_load_lib_function(NWRAP_LIBC
, getgrgid
);
1144 return nwrap_main_global
->libc
->fns
->_libc_getgrgid(gid
);
1147 #ifdef HAVE_GETGRGID_R
1148 static int libc_getgrgid_r(gid_t gid
,
1152 struct group
**result
)
1154 #ifdef HAVE___POSIX_GETGRGID_R
1155 if (nwrap_main_global
->libc
->fns
->_libc_getgrgid_r
== NULL
) {
1156 *(void **) (&nwrap_main_global
->libc
->fns
->_libc_getgrgid_r
) =
1157 _nwrap_load_lib_function(NWRAP_LIBC
, "__posix_getgrgid_r");
1160 nwrap_load_lib_function(NWRAP_LIBC
, getgrgid_r
);
1163 return nwrap_main_global
->libc
->fns
->_libc_getgrgid_r(gid
,
1171 static void libc_setgrent(void)
1173 nwrap_load_lib_function(NWRAP_LIBC
, setgrent
);
1175 nwrap_main_global
->libc
->fns
->_libc_setgrent();
1178 static struct group
*libc_getgrent(void)
1180 nwrap_load_lib_function(NWRAP_LIBC
, getgrent
);
1182 return nwrap_main_global
->libc
->fns
->_libc_getgrent();
1185 #ifdef HAVE_GETGRENT_R
1186 #ifdef HAVE_SOLARIS_GETGRENT_R
1187 static struct group
*libc_getgrent_r(struct group
*group
,
1191 nwrap_load_lib_function(NWRAP_LIBC
, getgrent_r
);
1193 return nwrap_main_global
->libc
->fns
->_libc_getgrent_r(group
,
1197 #else /* !HAVE_SOLARIS_GETGRENT_R */
1198 static int libc_getgrent_r(struct group
*group
,
1201 struct group
**result
)
1203 nwrap_load_lib_function(NWRAP_LIBC
, getgrent_r
);
1205 return nwrap_main_global
->libc
->fns
->_libc_getgrent_r(group
,
1210 #endif /* HAVE_SOLARIS_GETGRENT_R */
1211 #endif /* HAVE_GETGRENT_R */
1213 static void libc_endgrent(void)
1215 nwrap_load_lib_function(NWRAP_LIBC
, endgrent
);
1217 nwrap_main_global
->libc
->fns
->_libc_endgrent();
1220 #ifdef HAVE_GETGROUPLIST
1221 static int libc_getgrouplist(const char *user
,
1226 nwrap_load_lib_function(NWRAP_LIBC
, getgrouplist
);
1228 return nwrap_main_global
->libc
->fns
->_libc_getgrouplist(user
,
1235 static void libc_sethostent(int stayopen
)
1237 nwrap_load_lib_function(NWRAP_LIBNSL
, sethostent
);
1239 nwrap_main_global
->libc
->fns
->_libc_sethostent(stayopen
);
1242 static struct hostent
*libc_gethostent(void)
1244 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostent
);
1246 return nwrap_main_global
->libc
->fns
->_libc_gethostent();
1249 static void libc_endhostent(void)
1251 nwrap_load_lib_function(NWRAP_LIBNSL
, endhostent
);
1253 nwrap_main_global
->libc
->fns
->_libc_endhostent();
1256 static struct hostent
*libc_gethostbyname(const char *name
)
1258 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname
);
1260 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname(name
);
1263 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
1264 static struct hostent
*libc_gethostbyname2(const char *name
, int af
)
1266 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname2
);
1268 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname2(name
, af
);
1272 static struct hostent
*libc_gethostbyaddr(const void *addr
,
1276 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyaddr
);
1278 return nwrap_main_global
->libc
->fns
->_libc_gethostbyaddr(addr
,
1283 static int libc_gethostname(char *name
, size_t len
)
1285 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostname
);
1287 return nwrap_main_global
->libc
->fns
->_libc_gethostname(name
, len
);
1290 #ifdef HAVE_GETHOSTBYNAME_R
1291 static int libc_gethostbyname_r(const char *name
,
1292 struct hostent
*ret
,
1295 struct hostent
**result
,
1298 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyname_r
);
1300 return nwrap_main_global
->libc
->fns
->_libc_gethostbyname_r(name
,
1309 #ifdef HAVE_GETHOSTBYADDR_R
1310 static int libc_gethostbyaddr_r(const void *addr
,
1313 struct hostent
*ret
,
1316 struct hostent
**result
,
1319 nwrap_load_lib_function(NWRAP_LIBNSL
, gethostbyaddr_r
);
1321 return nwrap_main_global
->libc
->fns
->_libc_gethostbyaddr_r(addr
,
1332 static int libc_getaddrinfo(const char *node
,
1333 const char *service
,
1334 const struct addrinfo
*hints
,
1335 struct addrinfo
**res
)
1337 nwrap_load_lib_function(NWRAP_LIBSOCKET
, getaddrinfo
);
1339 return nwrap_main_global
->libc
->fns
->_libc_getaddrinfo(node
,
1345 static int libc_getnameinfo(const struct sockaddr
*sa
,
1353 nwrap_load_lib_function(NWRAP_LIBSOCKET
, getnameinfo
);
1355 return nwrap_main_global
->libc
->fns
->_libc_getnameinfo(sa
,
1364 /*********************************************************
1365 * NWRAP NSS MODULE LOADER FUNCTIONS
1366 *********************************************************/
1368 static void *nwrap_load_module_fn(struct nwrap_backend
*b
,
1369 const char *fn_name
)
1374 if (!b
->so_handle
) {
1375 NWRAP_LOG(NWRAP_LOG_ERROR
, "No handle");
1379 if (asprintf(&s
, "_nss_%s_%s", b
->name
, fn_name
) == -1) {
1380 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1384 res
= dlsym(b
->so_handle
, s
);
1386 NWRAP_LOG(NWRAP_LOG_ERROR
,
1387 "Cannot find function %s in %s",
1394 static struct nwrap_module_nss_fns
*nwrap_load_module_fns(struct nwrap_backend
*b
)
1396 struct nwrap_module_nss_fns
*fns
;
1398 if (!b
->so_handle
) {
1402 fns
= (struct nwrap_module_nss_fns
*)malloc(sizeof(struct nwrap_module_nss_fns
));
1407 *(void **)(&fns
->_nss_getpwnam_r
) =
1408 nwrap_load_module_fn(b
, "getpwnam_r");
1409 *(void **)(&fns
->_nss_getpwuid_r
) =
1410 nwrap_load_module_fn(b
, "getpwuid_r");
1411 *(void **)(&fns
->_nss_setpwent
) =
1412 nwrap_load_module_fn(b
, "setpwent");
1413 *(void **)(&fns
->_nss_getpwent_r
) =
1414 nwrap_load_module_fn(b
, "getpwent_r");
1415 *(void **)(&fns
->_nss_endpwent
) =
1416 nwrap_load_module_fn(b
, "endpwent");
1417 *(void **)(&fns
->_nss_initgroups
) =
1418 nwrap_load_module_fn(b
, "initgroups_dyn");
1419 *(void **)(&fns
->_nss_getgrnam_r
) =
1420 nwrap_load_module_fn(b
, "getgrnam_r");
1421 *(void **)(&fns
->_nss_getgrgid_r
)=
1422 nwrap_load_module_fn(b
, "getgrgid_r");
1423 *(void **)(&fns
->_nss_setgrent
) =
1424 nwrap_load_module_fn(b
, "setgrent");
1425 *(void **)(&fns
->_nss_getgrent_r
) =
1426 nwrap_load_module_fn(b
, "getgrent_r");
1427 *(void **)(&fns
->_nss_endgrent
) =
1428 nwrap_load_module_fn(b
, "endgrent");
1433 static void *nwrap_load_module(const char *so_path
)
1437 if (!so_path
|| !strlen(so_path
)) {
1441 h
= dlopen(so_path
, RTLD_LAZY
);
1443 NWRAP_LOG(NWRAP_LOG_ERROR
,
1444 "Cannot open shared library %s",
1452 static bool nwrap_module_init(const char *name
,
1453 struct nwrap_ops
*ops
,
1454 const char *so_path
,
1456 struct nwrap_backend
**backends
)
1458 struct nwrap_backend
*b
;
1460 *backends
= (struct nwrap_backend
*)realloc(*backends
,
1461 sizeof(struct nwrap_backend
) * ((*num_backends
) + 1));
1463 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
1467 b
= &((*backends
)[*num_backends
]);
1471 b
->so_path
= so_path
;
1473 if (so_path
!= NULL
) {
1474 b
->so_handle
= nwrap_load_module(so_path
);
1475 b
->fns
= nwrap_load_module_fns(b
);
1476 if (b
->fns
== NULL
) {
1480 b
->so_handle
= NULL
;
1489 static void nwrap_libc_init(struct nwrap_main
*r
)
1491 r
->libc
= malloc(sizeof(struct nwrap_libc
));
1492 if (r
->libc
== NULL
) {
1493 printf("Failed to allocate memory for libc");
1496 ZERO_STRUCTP(r
->libc
);
1498 r
->libc
->fns
= malloc(sizeof(struct nwrap_libc_fns
));
1499 if (r
->libc
->fns
== NULL
) {
1500 printf("Failed to allocate memory for libc functions");
1503 ZERO_STRUCTP(r
->libc
->fns
);
1506 static void nwrap_backend_init(struct nwrap_main
*r
)
1508 const char *module_so_path
= getenv("NSS_WRAPPER_MODULE_SO_PATH");
1509 const char *module_fn_name
= getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1511 r
->num_backends
= 0;
1514 if (!nwrap_module_init("files", &nwrap_files_ops
, NULL
,
1517 NWRAP_LOG(NWRAP_LOG_ERROR
,
1518 "Failed to initialize 'files' backend");
1522 if (module_so_path
!= NULL
&&
1523 module_so_path
[0] != '\0' &&
1524 module_fn_name
!= NULL
&&
1525 module_fn_name
[0] != '\0') {
1526 if (!nwrap_module_init(module_fn_name
,
1531 NWRAP_LOG(NWRAP_LOG_ERROR
,
1532 "Failed to initialize '%s' backend",
1539 static void nwrap_init(void)
1543 size_t max_hostents_tmp
;
1545 NWRAP_LOCK(nwrap_initialized
);
1546 if (nwrap_initialized
) {
1547 NWRAP_UNLOCK(nwrap_initialized
);
1552 * Still holding nwrap_initialized lock here.
1553 * We don't use NWRAP_(UN)LOCK_ALL macros here because we
1554 * want to avoid overhead when other threads do their job.
1556 NWRAP_LOCK(nwrap_global
);
1557 NWRAP_LOCK(nwrap_gr_global
);
1558 NWRAP_LOCK(nwrap_he_global
);
1559 NWRAP_LOCK(nwrap_pw_global
);
1560 NWRAP_LOCK(nwrap_sp_global
);
1562 nwrap_initialized
= true;
1564 /* Initialize pthread_atfork handlers */
1565 pthread_atfork(&nwrap_thread_prepare
, &nwrap_thread_parent
,
1566 &nwrap_thread_child
);
1568 env
= getenv("NSS_WRAPPER_MAX_HOSTENTS");
1570 max_hostents_tmp
= (size_t)strtol(env
, &endptr
, 10);
1571 if (((env
!= '\0') && (endptr
== '\0')) ||
1572 (max_hostents_tmp
== 0)) {
1573 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1574 "Error parsing NSS_WRAPPER_MAX_HOSTENTS "
1575 "value or value is too small. "
1576 "Using default value: %lu.",
1577 (unsigned long)max_hostents
);
1579 max_hostents
= max_hostents_tmp
;
1582 /* Initialize hash table */
1583 NWRAP_LOG(NWRAP_LOG_DEBUG
,
1584 "Initializing hash table of size %lu items.",
1585 (unsigned long)max_hostents
);
1586 if (hcreate(max_hostents
) == 0) {
1587 NWRAP_LOG(NWRAP_LOG_ERROR
,
1588 "Failed to initialize hash table");
1592 nwrap_main_global
= &__nwrap_main_global
;
1594 nwrap_libc_init(nwrap_main_global
);
1596 nwrap_backend_init(nwrap_main_global
);
1599 nwrap_pw_global
.cache
= &__nwrap_cache_pw
;
1601 nwrap_pw_global
.cache
->path
= getenv("NSS_WRAPPER_PASSWD");
1602 nwrap_pw_global
.cache
->fp
= NULL
;
1603 nwrap_pw_global
.cache
->fd
= -1;
1604 nwrap_pw_global
.cache
->private_data
= &nwrap_pw_global
;
1605 nwrap_pw_global
.cache
->parse_line
= nwrap_pw_parse_line
;
1606 nwrap_pw_global
.cache
->unload
= nwrap_pw_unload
;
1609 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1610 nwrap_sp_global
.cache
= &__nwrap_cache_sp
;
1612 nwrap_sp_global
.cache
->path
= getenv("NSS_WRAPPER_SHADOW");
1613 nwrap_sp_global
.cache
->fp
= NULL
;
1614 nwrap_sp_global
.cache
->fd
= -1;
1615 nwrap_sp_global
.cache
->private_data
= &nwrap_sp_global
;
1616 nwrap_sp_global
.cache
->parse_line
= nwrap_sp_parse_line
;
1617 nwrap_sp_global
.cache
->unload
= nwrap_sp_unload
;
1618 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1621 nwrap_gr_global
.cache
= &__nwrap_cache_gr
;
1623 nwrap_gr_global
.cache
->path
= getenv("NSS_WRAPPER_GROUP");
1624 nwrap_gr_global
.cache
->fp
= NULL
;
1625 nwrap_gr_global
.cache
->fd
= -1;
1626 nwrap_gr_global
.cache
->private_data
= &nwrap_gr_global
;
1627 nwrap_gr_global
.cache
->parse_line
= nwrap_gr_parse_line
;
1628 nwrap_gr_global
.cache
->unload
= nwrap_gr_unload
;
1631 nwrap_he_global
.cache
= &__nwrap_cache_he
;
1633 nwrap_he_global
.cache
->path
= getenv("NSS_WRAPPER_HOSTS");
1634 nwrap_he_global
.cache
->fp
= NULL
;
1635 nwrap_he_global
.cache
->fd
= -1;
1636 nwrap_he_global
.cache
->private_data
= &nwrap_he_global
;
1637 nwrap_he_global
.cache
->parse_line
= nwrap_he_parse_line
;
1638 nwrap_he_global
.cache
->unload
= nwrap_he_unload
;
1641 /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1645 bool nss_wrapper_enabled(void)
1649 if (nwrap_pw_global
.cache
->path
== NULL
||
1650 nwrap_pw_global
.cache
->path
[0] == '\0') {
1653 if (nwrap_gr_global
.cache
->path
== NULL
||
1654 nwrap_gr_global
.cache
->path
[0] == '\0') {
1661 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1662 bool nss_wrapper_shadow_enabled(void)
1666 if (nwrap_sp_global
.cache
->path
== NULL
||
1667 nwrap_sp_global
.cache
->path
[0] == '\0') {
1673 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1675 bool nss_wrapper_hosts_enabled(void)
1679 if (nwrap_he_global
.cache
->path
== NULL
||
1680 nwrap_he_global
.cache
->path
[0] == '\0') {
1687 static bool nwrap_hostname_enabled(void)
1691 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL
) {
1698 static bool nwrap_parse_file(struct nwrap_cache
*nwrap
)
1702 /* Unused but getline needs it */
1706 if (nwrap
->st
.st_size
== 0) {
1707 NWRAP_LOG(NWRAP_LOG_DEBUG
, "size == 0");
1711 /* Support for 32-bit system I guess */
1712 if (nwrap
->st
.st_size
> INT32_MAX
) {
1713 NWRAP_LOG(NWRAP_LOG_ERROR
,
1714 "Size[%u] larger than INT32_MAX",
1715 (unsigned)nwrap
->st
.st_size
);
1722 n
= getline(&line
, &len
, nwrap
->fp
);
1725 if (feof(nwrap
->fp
)) {
1729 NWRAP_LOG(NWRAP_LOG_ERROR
,
1730 "Unable to read line from file: %s",
1735 if (line
[n
- 1] == '\n') {
1739 if (line
[0] == '\0') {
1744 ok
= nwrap
->parse_line(nwrap
, line
);
1746 NWRAP_LOG(NWRAP_LOG_ERROR
,
1747 "Unable to parse line file: %s",
1753 /* Line is parsed without issues so add it to list */
1754 ok
= nwrap_vector_add_item(&(nwrap
->lines
), (void *const) line
);
1756 NWRAP_LOG(NWRAP_LOG_ERROR
,
1757 "Unable to add line to vector");
1761 /* This forces getline to allocate new memory for line. */
1763 } while (!feof(nwrap
->fp
));
1768 static void nwrap_files_cache_unload(struct nwrap_cache
*nwrap
)
1770 nwrap
->unload(nwrap
);
1772 nwrap_lines_unload(nwrap
);
1775 static bool nwrap_files_cache_reload(struct nwrap_cache
*nwrap
)
1780 bool retried
= false;
1782 assert(nwrap
!= NULL
);
1785 if (nwrap
->fd
< 0) {
1786 nwrap
->fp
= fopen(nwrap
->path
, "re");
1787 if (nwrap
->fp
== NULL
) {
1789 NWRAP_LOG(NWRAP_LOG_ERROR
,
1790 "Unable to open '%s' readonly %d:%s",
1791 nwrap
->path
, nwrap
->fd
,
1796 nwrap
->fd
= fileno(nwrap
->fp
);
1797 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Open '%s'", nwrap
->path
);
1800 ret
= fstat(nwrap
->fd
, &st
);
1802 NWRAP_LOG(NWRAP_LOG_ERROR
,
1803 "fstat(%s) - %d:%s",
1813 if (retried
== false && st
.st_nlink
== 0) {
1814 /* maybe someone has replaced the file... */
1815 NWRAP_LOG(NWRAP_LOG_TRACE
,
1816 "st_nlink == 0, reopen %s",
1819 memset(&nwrap
->st
, 0, sizeof(nwrap
->st
));
1826 if (st
.st_mtime
== nwrap
->st
.st_mtime
) {
1827 NWRAP_LOG(NWRAP_LOG_TRACE
,
1828 "st_mtime[%u] hasn't changed, skip reload",
1829 (unsigned)st
.st_mtime
);
1833 NWRAP_LOG(NWRAP_LOG_TRACE
,
1834 "st_mtime has changed [%u] => [%u], start reload",
1835 (unsigned)st
.st_mtime
,
1836 (unsigned)nwrap
->st
.st_mtime
);
1840 nwrap_files_cache_unload(nwrap
);
1842 ok
= nwrap_parse_file(nwrap
);
1844 NWRAP_LOG(NWRAP_LOG_ERROR
, "Failed to reload %s", nwrap
->path
);
1845 nwrap_files_cache_unload(nwrap
);
1849 NWRAP_LOG(NWRAP_LOG_TRACE
, "Reloaded %s", nwrap
->path
);
1854 * the caller has to call nwrap_unload() on failure
1856 static bool nwrap_pw_parse_line(struct nwrap_cache
*nwrap
, char *line
)
1858 struct nwrap_pw
*nwrap_pw
;
1865 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
1867 list_size
= sizeof(*nwrap_pw
->list
) * (nwrap_pw
->num
+1);
1868 pw
= (struct passwd
*)realloc(nwrap_pw
->list
, list_size
);
1870 NWRAP_LOG(NWRAP_LOG_ERROR
,
1871 "realloc(%u) failed",
1872 (unsigned)list_size
);
1875 nwrap_pw
->list
= pw
;
1877 pw
= &nwrap_pw
->list
[nwrap_pw
->num
];
1884 NWRAP_LOG(NWRAP_LOG_ERROR
,
1885 "Invalid line[%s]: '%s'",
1895 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]\n", pw
->pw_name
);
1900 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1908 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]\n", pw
->pw_passwd
);
1913 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1919 pw
->pw_uid
= (uid_t
)strtoul(c
, &e
, 10);
1921 NWRAP_LOG(NWRAP_LOG_ERROR
,
1922 "Invalid line[%s]: '%s' - %s",
1923 line
, c
, strerror(errno
));
1927 NWRAP_LOG(NWRAP_LOG_ERROR
,
1928 "Invalid line[%s]: '%s' - %s",
1929 line
, c
, strerror(errno
));
1933 NWRAP_LOG(NWRAP_LOG_ERROR
,
1934 "Invalid line[%s]: '%s' - %s",
1935 line
, c
, strerror(errno
));
1940 NWRAP_LOG(NWRAP_LOG_TRACE
, "uid[%u]", pw
->pw_uid
);
1945 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
1951 pw
->pw_gid
= (gid_t
)strtoul(c
, &e
, 10);
1953 NWRAP_LOG(NWRAP_LOG_ERROR
,
1954 "Invalid line[%s]: '%s' - %s",
1955 line
, c
, strerror(errno
));
1959 NWRAP_LOG(NWRAP_LOG_ERROR
,
1960 "Invalid line[%s]: '%s' - %s",
1961 line
, c
, strerror(errno
));
1965 NWRAP_LOG(NWRAP_LOG_ERROR
,
1966 "Invalid line[%s]: '%s' - %s",
1967 line
, c
, strerror(errno
));
1972 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]\n", pw
->pw_gid
);
1974 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
1975 pw
->pw_class
= discard_const_p(char, "");
1977 NWRAP_LOG(NWRAP_LOG_TRACE
, "class[%s]", pw
->pw_class
);
1978 #endif /* HAVE_STRUCT_PASSWD_PW_CLASS */
1980 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
1983 NWRAP_LOG(NWRAP_LOG_TRACE
,
1985 (unsigned long)pw
->pw_change
);
1986 #endif /* HAVE_STRUCT_PASSWD_PW_CHANGE */
1988 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
1991 NWRAP_LOG(NWRAP_LOG_TRACE
,
1993 (unsigned long)pw
->pw_expire
);
1994 #endif /* HAVE_STRUCT_PASSWD_PW_EXPIRE */
1999 NWRAP_LOG(NWRAP_LOG_ERROR
, "invalid line[%s]: '%s'", line
, c
);
2007 NWRAP_LOG(NWRAP_LOG_TRACE
, "gecos[%s]", pw
->pw_gecos
);
2012 NWRAP_LOG(NWRAP_LOG_ERROR
, "'%s'", c
);
2020 NWRAP_LOG(NWRAP_LOG_TRACE
, "dir[%s]", pw
->pw_dir
);
2024 NWRAP_LOG(NWRAP_LOG_TRACE
, "shell[%s]", pw
->pw_shell
);
2026 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2027 "Added user[%s:%s:%u:%u:%s:%s:%s]",
2028 pw
->pw_name
, pw
->pw_passwd
,
2029 pw
->pw_uid
, pw
->pw_gid
,
2030 pw
->pw_gecos
, pw
->pw_dir
, pw
->pw_shell
);
2036 static void nwrap_pw_unload(struct nwrap_cache
*nwrap
)
2038 struct nwrap_pw
*nwrap_pw
;
2039 nwrap_pw
= (struct nwrap_pw
*)nwrap
->private_data
;
2041 SAFE_FREE(nwrap_pw
->list
);
2046 static int nwrap_pw_copy_r(const struct passwd
*src
, struct passwd
*dst
,
2047 char *buf
, size_t buflen
, struct passwd
**dstp
)
2053 first
= src
->pw_name
;
2055 last
= src
->pw_shell
;
2056 while (*last
) last
++;
2058 ofs
= PTR_DIFF(last
+ 1, first
);
2060 if (ofs
> (off_t
) buflen
) {
2064 memcpy(buf
, first
, ofs
);
2066 ofs
= PTR_DIFF(src
->pw_name
, first
);
2067 dst
->pw_name
= buf
+ ofs
;
2068 ofs
= PTR_DIFF(src
->pw_passwd
, first
);
2069 dst
->pw_passwd
= buf
+ ofs
;
2070 dst
->pw_uid
= src
->pw_uid
;
2071 dst
->pw_gid
= src
->pw_gid
;
2072 ofs
= PTR_DIFF(src
->pw_gecos
, first
);
2073 dst
->pw_gecos
= buf
+ ofs
;
2074 ofs
= PTR_DIFF(src
->pw_dir
, first
);
2075 dst
->pw_dir
= buf
+ ofs
;
2076 ofs
= PTR_DIFF(src
->pw_shell
, first
);
2077 dst
->pw_shell
= buf
+ ofs
;
2086 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2087 static bool nwrap_sp_parse_line(struct nwrap_cache
*nwrap
, char *line
)
2089 struct nwrap_sp
*nwrap_sp
;
2096 nwrap_sp
= (struct nwrap_sp
*)nwrap
->private_data
;
2098 list_size
= sizeof(*nwrap_sp
->list
) * (nwrap_sp
->num
+1);
2099 sp
= (struct spwd
*)realloc(nwrap_sp
->list
, list_size
);
2101 NWRAP_LOG(NWRAP_LOG_ERROR
,
2102 "realloc(%u) failed",
2103 (unsigned)list_size
);
2106 nwrap_sp
->list
= sp
;
2108 sp
= &nwrap_sp
->list
[nwrap_sp
->num
];
2115 NWRAP_LOG(NWRAP_LOG_ERROR
,
2116 "name -- Invalid line[%s]: '%s'",
2126 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]\n", sp
->sp_namp
);
2131 NWRAP_LOG(NWRAP_LOG_ERROR
,
2132 "pwd -- Invalid line[%s]: '%s'",
2149 NWRAP_LOG(NWRAP_LOG_ERROR
,
2150 "lstchg -- Invalid line[%s]: '%s'",
2157 sp
->sp_lstchg
= strtol(c
, &e
, 10);
2159 NWRAP_LOG(NWRAP_LOG_ERROR
,
2160 "lstchg -- Invalid line[%s]: '%s' - %s",
2161 line
, c
, strerror(errno
));
2165 NWRAP_LOG(NWRAP_LOG_ERROR
,
2166 "lstchg -- Invalid line[%s]: '%s' - %s",
2167 line
, c
, strerror(errno
));
2171 NWRAP_LOG(NWRAP_LOG_ERROR
,
2172 "lstchg -- Invalid line[%s]: '%s' - %s",
2173 line
, c
, strerror(errno
));
2186 NWRAP_LOG(NWRAP_LOG_ERROR
,
2187 "min -- Invalid line[%s]: '%s'",
2194 sp
->sp_min
= strtol(c
, &e
, 10);
2196 NWRAP_LOG(NWRAP_LOG_ERROR
,
2197 "min -- Invalid line[%s]: '%s' - %s",
2198 line
, c
, strerror(errno
));
2202 NWRAP_LOG(NWRAP_LOG_ERROR
,
2203 "min -- Invalid line[%s]: '%s' - %s",
2204 line
, c
, strerror(errno
));
2208 NWRAP_LOG(NWRAP_LOG_ERROR
,
2209 "min -- Invalid line[%s]: '%s' - %s",
2210 line
, c
, strerror(errno
));
2223 NWRAP_LOG(NWRAP_LOG_ERROR
,
2224 "max -- Invalid line[%s]: '%s'",
2231 sp
->sp_max
= strtol(c
, &e
, 10);
2233 NWRAP_LOG(NWRAP_LOG_ERROR
,
2234 "max -- Invalid line[%s]: '%s' - %s",
2235 line
, c
, strerror(errno
));
2239 NWRAP_LOG(NWRAP_LOG_ERROR
,
2240 "max -- Invalid line[%s]: '%s' - %s",
2241 line
, c
, strerror(errno
));
2245 NWRAP_LOG(NWRAP_LOG_ERROR
,
2246 "max -- Invalid line[%s]: '%s' - %s",
2247 line
, c
, strerror(errno
));
2260 NWRAP_LOG(NWRAP_LOG_ERROR
,
2261 "warn -- Invalid line[%s]: '%s'",
2268 sp
->sp_warn
= strtol(c
, &e
, 10);
2270 NWRAP_LOG(NWRAP_LOG_ERROR
,
2271 "warn -- Invalid line[%s]: '%s' - %s",
2272 line
, c
, strerror(errno
));
2276 NWRAP_LOG(NWRAP_LOG_ERROR
,
2277 "warn -- Invalid line[%s]: '%s' - %s",
2278 line
, c
, strerror(errno
));
2282 NWRAP_LOG(NWRAP_LOG_ERROR
,
2283 "warn -- Invalid line[%s]: '%s' - %s",
2284 line
, c
, strerror(errno
));
2297 NWRAP_LOG(NWRAP_LOG_ERROR
,
2298 "inact -- Invalid line[%s]: '%s'",
2305 sp
->sp_inact
= strtol(c
, &e
, 10);
2307 NWRAP_LOG(NWRAP_LOG_ERROR
,
2308 "inact -- Invalid line[%s]: '%s' - %s",
2309 line
, c
, strerror(errno
));
2313 NWRAP_LOG(NWRAP_LOG_ERROR
,
2314 "inact -- Invalid line[%s]: '%s' - %s",
2315 line
, c
, strerror(errno
));
2319 NWRAP_LOG(NWRAP_LOG_ERROR
,
2320 "inact -- Invalid line[%s]: '%s' - %s",
2321 line
, c
, strerror(errno
));
2334 NWRAP_LOG(NWRAP_LOG_ERROR
,
2335 "expire -- Invalid line[%s]: '%s'",
2342 sp
->sp_expire
= strtol(c
, &e
, 10);
2344 NWRAP_LOG(NWRAP_LOG_ERROR
,
2345 "expire -- Invalid line[%s]: '%s' - %s",
2346 line
, c
, strerror(errno
));
2350 NWRAP_LOG(NWRAP_LOG_ERROR
,
2351 "expire -- Invalid line[%s]: '%s' - %s",
2352 line
, c
, strerror(errno
));
2356 NWRAP_LOG(NWRAP_LOG_ERROR
,
2357 "expire -- Invalid line[%s]: '%s' - %s",
2358 line
, c
, strerror(errno
));
2368 static void nwrap_sp_unload(struct nwrap_cache
*nwrap
)
2370 struct nwrap_sp
*nwrap_sp
;
2371 nwrap_sp
= (struct nwrap_sp
*)nwrap
->private_data
;
2373 SAFE_FREE(nwrap_sp
->list
);
2377 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2380 * the caller has to call nwrap_unload() on failure
2382 static bool nwrap_gr_parse_line(struct nwrap_cache
*nwrap
, char *line
)
2384 struct nwrap_gr
*nwrap_gr
;
2392 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
2394 list_size
= sizeof(*nwrap_gr
->list
) * (nwrap_gr
->num
+1);
2395 gr
= (struct group
*)realloc(nwrap_gr
->list
, list_size
);
2397 NWRAP_LOG(NWRAP_LOG_ERROR
, "realloc failed");
2400 nwrap_gr
->list
= gr
;
2402 gr
= &nwrap_gr
->list
[nwrap_gr
->num
];
2409 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
2417 NWRAP_LOG(NWRAP_LOG_TRACE
, "name[%s]", gr
->gr_name
);
2422 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
2430 NWRAP_LOG(NWRAP_LOG_TRACE
, "password[%s]", gr
->gr_passwd
);
2435 NWRAP_LOG(NWRAP_LOG_ERROR
, "Invalid line[%s]: '%s'", line
, c
);
2441 gr
->gr_gid
= (gid_t
)strtoul(c
, &e
, 10);
2443 NWRAP_LOG(NWRAP_LOG_ERROR
,
2444 "Invalid line[%s]: '%s' - %s",
2445 line
, c
, strerror(errno
));
2449 NWRAP_LOG(NWRAP_LOG_ERROR
,
2450 "Invalid line[%s]: '%s' - %s",
2451 line
, c
, strerror(errno
));
2455 NWRAP_LOG(NWRAP_LOG_ERROR
,
2456 "Invalid line[%s]: '%s' - %s",
2457 line
, c
, strerror(errno
));
2462 NWRAP_LOG(NWRAP_LOG_TRACE
, "gid[%u]", gr
->gr_gid
);
2465 gr
->gr_mem
= (char **)malloc(sizeof(char *));
2467 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
2470 gr
->gr_mem
[0] = NULL
;
2472 for(nummem
=0; p
; nummem
++) {
2482 if (strlen(c
) == 0) {
2486 m_size
= sizeof(char *) * (nummem
+2);
2487 m
= (char **)realloc(gr
->gr_mem
, m_size
);
2489 NWRAP_LOG(NWRAP_LOG_ERROR
,
2490 "realloc(%zd) failed",
2495 gr
->gr_mem
[nummem
] = c
;
2496 gr
->gr_mem
[nummem
+1] = NULL
;
2498 NWRAP_LOG(NWRAP_LOG_TRACE
,
2500 nummem
, gr
->gr_mem
[nummem
]);
2503 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2504 "Added group[%s:%s:%u:] with %u members",
2505 gr
->gr_name
, gr
->gr_passwd
, gr
->gr_gid
, nummem
);
2511 static void nwrap_gr_unload(struct nwrap_cache
*nwrap
)
2514 struct nwrap_gr
*nwrap_gr
;
2515 nwrap_gr
= (struct nwrap_gr
*)nwrap
->private_data
;
2517 if (nwrap_gr
->list
) {
2518 for (i
=0; i
< nwrap_gr
->num
; i
++) {
2519 SAFE_FREE(nwrap_gr
->list
[i
].gr_mem
);
2521 SAFE_FREE(nwrap_gr
->list
);
2528 static int nwrap_gr_copy_r(const struct group
*src
, struct group
*dst
,
2529 char *buf
, size_t buflen
, struct group
**dstp
)
2539 first
= src
->gr_name
;
2541 lastm
= src
->gr_mem
;
2548 last
= src
->gr_passwd
;
2550 while (*last
) last
++;
2552 ofsb
= PTR_DIFF(last
+ 1, first
);
2553 ofsm
= PTR_DIFF(lastm
+ 1, src
->gr_mem
);
2555 if ((ofsb
+ ofsm
) > (off_t
) buflen
) {
2559 memcpy(buf
, first
, ofsb
);
2560 memcpy(buf
+ ofsb
, src
->gr_mem
, ofsm
);
2562 ofs
= PTR_DIFF(src
->gr_name
, first
);
2563 dst
->gr_name
= buf
+ ofs
;
2564 ofs
= PTR_DIFF(src
->gr_passwd
, first
);
2565 dst
->gr_passwd
= buf
+ ofs
;
2566 dst
->gr_gid
= src
->gr_gid
;
2568 dst
->gr_mem
= (char **)(buf
+ ofsb
);
2569 for (i
=0; src
->gr_mem
[i
]; i
++) {
2570 ofs
= PTR_DIFF(src
->gr_mem
[i
], first
);
2571 dst
->gr_mem
[i
] = buf
+ ofs
;
2581 static struct nwrap_entlist
*nwrap_entlist_init(struct nwrap_entdata
*ed
)
2583 struct nwrap_entlist
*el
;
2586 NWRAP_LOG(NWRAP_LOG_ERROR
,
2587 "entry is NULL, can't create list item");
2591 el
= (struct nwrap_entlist
*)malloc(sizeof(struct nwrap_entlist
));
2593 NWRAP_LOG(NWRAP_LOG_ERROR
, "malloc failed");
2603 static bool nwrap_ed_inventarize_add_new(char *const h_name
,
2604 struct nwrap_entdata
*const ed
)
2608 struct nwrap_entlist
*el
;
2611 if (h_name
== NULL
) {
2612 NWRAP_LOG(NWRAP_LOG_ERROR
, "h_name NULL - can't add");
2616 el
= nwrap_entlist_init(ed
);
2622 e
.data
= (void *)el
;
2624 p
= hsearch(e
, ENTER
);
2626 NWRAP_LOG(NWRAP_LOG_ERROR
, "Hash table is full!");
2630 ok
= nwrap_vector_add_item(&(nwrap_he_global
.lists
), (void *)el
);
2632 NWRAP_LOG(NWRAP_LOG_ERROR
,
2633 "Failed to add list entry to vector.");
2640 static bool nwrap_ed_inventarize_add_to_existing(struct nwrap_entdata
*const ed
,
2641 struct nwrap_entlist
*const el
)
2643 struct nwrap_entlist
*cursor
;
2644 struct nwrap_entlist
*el_new
;
2647 NWRAP_LOG(NWRAP_LOG_ERROR
, "list is NULL, can not add");
2652 for (cursor
= el
; cursor
->next
!= NULL
; cursor
= cursor
->next
)
2654 if (cursor
->ed
== ed
) {
2655 /* The entry already exists in this list. */
2660 if (cursor
->ed
== ed
) {
2661 /* The entry already exists in this list. */
2665 el_new
= nwrap_entlist_init(ed
);
2666 if (el_new
== NULL
) {
2670 cursor
->next
= el_new
;
2674 static bool nwrap_ed_inventarize(char *const name
,
2675 struct nwrap_entdata
*const ed
)
2684 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Searching name: %s", e
.key
);
2686 p
= hsearch(e
, FIND
);
2688 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name %s not found. Adding...", name
);
2689 ok
= nwrap_ed_inventarize_add_new(name
, ed
);
2691 struct nwrap_entlist
*el
= (struct nwrap_entlist
*)p
->data
;
2693 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name %s found. Add record to list.", name
);
2694 ok
= nwrap_ed_inventarize_add_to_existing(ed
, el
);
2700 static bool nwrap_add_hname(struct nwrap_entdata
*const ed
)
2702 char *const h_name
= (char *const)(ed
->ht
.h_name
);
2706 ok
= nwrap_ed_inventarize(h_name
, ed
);
2711 if (ed
->ht
.h_aliases
== NULL
) {
2715 /* Itemize aliases */
2716 for (i
= 0; ed
->ht
.h_aliases
[i
] != NULL
; ++i
) {
2719 h_name_alias
= ed
->ht
.h_aliases
[i
];
2721 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Add alias: %s", h_name_alias
);
2723 if (!nwrap_ed_inventarize(h_name_alias
, ed
)) {
2724 NWRAP_LOG(NWRAP_LOG_ERROR
,
2725 "Unable to add alias: %s", h_name_alias
);
2733 static bool nwrap_he_parse_line(struct nwrap_cache
*nwrap
, char *line
)
2735 struct nwrap_he
*nwrap_he
= (struct nwrap_he
*)nwrap
->private_data
;
2736 bool do_aliases
= true;
2737 ssize_t aliases_count
= 0;
2745 struct nwrap_entdata
*ed
= (struct nwrap_entdata
*)
2746 malloc(sizeof(struct nwrap_entdata
));
2748 NWRAP_LOG(NWRAP_LOG_ERROR
,
2749 "Unable to allocate memory for nwrap_entdata");
2760 /* Walk to first char */
2761 for (p
= i
; *p
!= '.' && *p
!= ':' && !isxdigit((int) *p
); p
++) {
2763 NWRAP_LOG(NWRAP_LOG_ERROR
,
2764 "Invalid line[%s]: '%s'",
2771 for (i
= p
; !isspace((int)*p
); p
++) {
2773 NWRAP_LOG(NWRAP_LOG_ERROR
,
2774 "Invalid line[%s]: '%s'",
2783 if (inet_pton(AF_INET
, i
, ed
->addr
.host_addr
)) {
2784 ed
->ht
.h_addrtype
= AF_INET
;
2785 ed
->ht
.h_length
= 4;
2787 } else if (inet_pton(AF_INET6
, i
, ed
->addr
.host_addr
)) {
2788 ed
->ht
.h_addrtype
= AF_INET6
;
2789 ed
->ht
.h_length
= 16;
2792 NWRAP_LOG(NWRAP_LOG_ERROR
,
2793 "Invalid line[%s]: '%s'",
2801 ok
= nwrap_vector_add_item(&(ed
->nwrap_addrdata
),
2802 (void *const)ed
->addr
.host_addr
);
2804 NWRAP_LOG(NWRAP_LOG_ERROR
, "Unable to add addrdata to vector");
2808 ed
->ht
.h_addr_list
= nwrap_vector_head(&ed
->nwrap_addrdata
);
2816 /* Walk to first char */
2817 for (n
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
2819 NWRAP_LOG(NWRAP_LOG_ERROR
,
2820 "Invalid line[%s]: '%s'",
2828 for (n
= p
; !isspace((int)*p
); p
++) {
2837 /* Convert to lowercase. This operate on same memory region */
2841 /* glib's getent always dereferences he->h_aliases */
2842 ed
->ht
.h_aliases
= malloc(sizeof(char *));
2843 if (ed
->ht
.h_aliases
== NULL
) {
2847 ed
->ht
.h_aliases
[0] = NULL
;
2852 while (do_aliases
) {
2858 /* Walk to first char */
2859 for (a
= p
; *p
!= '_' && !isalnum((int) *p
); p
++) {
2865 /* Only trailing spaces are left */
2870 for (a
= p
; !isspace((int)*p
); p
++) {
2879 aliases
= realloc(ed
->ht
.h_aliases
, sizeof(char *) * (aliases_count
+ 2));
2880 if (aliases
== NULL
) {
2884 ed
->ht
.h_aliases
= aliases
;
2887 aliases
[aliases_count
] = a
;
2888 aliases
[aliases_count
+ 1] = NULL
;
2893 ok
= nwrap_vector_add_item(&(nwrap_he
->entries
), (void *const)ed
);
2895 NWRAP_LOG(NWRAP_LOG_ERROR
, "Unable to add entry to vector");
2900 ed
->aliases_count
= aliases_count
;
2901 /* Inventarize item */
2902 ok
= nwrap_add_hname(ed
);
2907 ok
= nwrap_ed_inventarize(ip
, ed
);
2916 static void nwrap_he_unload(struct nwrap_cache
*nwrap
)
2918 struct nwrap_he
*nwrap_he
=
2919 (struct nwrap_he
*)nwrap
->private_data
;
2920 struct nwrap_entdata
*ed
;
2921 struct nwrap_entlist
*el
;
2925 nwrap_vector_foreach (ed
, nwrap_he
->entries
, i
)
2927 SAFE_FREE(ed
->nwrap_addrdata
.items
);
2928 SAFE_FREE(ed
->ht
.h_aliases
);
2931 SAFE_FREE(nwrap_he
->entries
.items
);
2932 nwrap_he
->entries
.count
= nwrap_he
->entries
.capacity
= 0;
2934 nwrap_vector_foreach(el
, nwrap_he
->lists
, i
)
2936 while (el
!= NULL
) {
2937 struct nwrap_entlist
*el_next
;
2944 SAFE_FREE(nwrap_he
->lists
.items
);
2945 nwrap_he
->lists
.count
= nwrap_he
->lists
.capacity
= 0;
2951 * If we unload the file, the pointers in the hash table point to
2952 * invalid memory. So we need to destroy the hash table and recreate
2956 rc
= hcreate(max_hostents
);
2958 NWRAP_LOG(NWRAP_LOG_ERROR
, "Failed to initialize hash table");
2964 /* user functions */
2965 static struct passwd
*nwrap_files_getpwnam(struct nwrap_backend
*b
,
2971 (void) b
; /* unused */
2973 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Lookup user %s in files", name
);
2975 ok
= nwrap_files_cache_reload(nwrap_pw_global
.cache
);
2977 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading passwd file");
2981 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
2982 if (strcmp(nwrap_pw_global
.list
[i
].pw_name
, name
) == 0) {
2983 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] found", name
);
2984 return &nwrap_pw_global
.list
[i
];
2986 NWRAP_LOG(NWRAP_LOG_DEBUG
,
2987 "user[%s] does not match [%s]",
2989 nwrap_pw_global
.list
[i
].pw_name
);
2992 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] not found\n", name
);
2998 static int nwrap_files_getpwnam_r(struct nwrap_backend
*b
,
2999 const char *name
, struct passwd
*pwdst
,
3000 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3004 pw
= nwrap_files_getpwnam(b
, name
);
3012 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
3015 static struct passwd
*nwrap_files_getpwuid(struct nwrap_backend
*b
,
3021 (void) b
; /* unused */
3023 ok
= nwrap_files_cache_reload(nwrap_pw_global
.cache
);
3025 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading passwd file");
3029 for (i
=0; i
<nwrap_pw_global
.num
; i
++) {
3030 if (nwrap_pw_global
.list
[i
].pw_uid
== uid
) {
3031 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] found", uid
);
3032 return &nwrap_pw_global
.list
[i
];
3034 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3035 "uid[%u] does not match [%u]",
3037 nwrap_pw_global
.list
[i
].pw_uid
);
3040 NWRAP_LOG(NWRAP_LOG_DEBUG
, "uid[%u] not found\n", uid
);
3046 static int nwrap_files_getpwuid_r(struct nwrap_backend
*b
,
3047 uid_t uid
, struct passwd
*pwdst
,
3048 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3052 pw
= nwrap_files_getpwuid(b
, uid
);
3060 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
3063 /* user enum functions */
3064 static void nwrap_files_setpwent(struct nwrap_backend
*b
)
3066 (void) b
; /* unused */
3068 nwrap_pw_global
.idx
= 0;
3071 static struct passwd
*nwrap_files_getpwent(struct nwrap_backend
*b
)
3075 (void) b
; /* unused */
3077 if (nwrap_pw_global
.idx
== 0) {
3079 ok
= nwrap_files_cache_reload(nwrap_pw_global
.cache
);
3081 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading passwd file");
3086 if (nwrap_pw_global
.idx
>= nwrap_pw_global
.num
) {
3091 pw
= &nwrap_pw_global
.list
[nwrap_pw_global
.idx
++];
3093 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3094 "return user[%s] uid[%u]",
3095 pw
->pw_name
, pw
->pw_uid
);
3100 static int nwrap_files_getpwent_r(struct nwrap_backend
*b
,
3101 struct passwd
*pwdst
, char *buf
,
3102 size_t buflen
, struct passwd
**pwdstp
)
3106 pw
= nwrap_files_getpwent(b
);
3114 return nwrap_pw_copy_r(pw
, pwdst
, buf
, buflen
, pwdstp
);
3117 static void nwrap_files_endpwent(struct nwrap_backend
*b
)
3119 (void) b
; /* unused */
3121 nwrap_pw_global
.idx
= 0;
3126 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
3128 #ifdef HAVE_SETSPENT
3129 static void nwrap_files_setspent(void)
3131 nwrap_sp_global
.idx
= 0;
3134 static struct spwd
*nwrap_files_getspent(void)
3138 if (nwrap_sp_global
.idx
== 0) {
3141 ok
= nwrap_files_cache_reload(nwrap_sp_global
.cache
);
3143 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading shadow file");
3148 if (nwrap_sp_global
.idx
>= nwrap_sp_global
.num
) {
3153 sp
= &nwrap_sp_global
.list
[nwrap_sp_global
.idx
++];
3155 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3162 static void nwrap_files_endspent(void)
3164 nwrap_sp_global
.idx
= 0;
3166 #endif /* HAVE_SETSPENT */
3168 static struct spwd
*nwrap_files_getspnam(const char *name
)
3173 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Lookup user %s in files", name
);
3175 ok
= nwrap_files_cache_reload(nwrap_sp_global
.cache
);
3177 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading shadow file");
3181 for (i
=0; i
<nwrap_sp_global
.num
; i
++) {
3182 if (strcmp(nwrap_sp_global
.list
[i
].sp_namp
, name
) == 0) {
3183 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] found", name
);
3184 return &nwrap_sp_global
.list
[i
];
3186 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3187 "user[%s] does not match [%s]",
3189 nwrap_sp_global
.list
[i
].sp_namp
);
3192 NWRAP_LOG(NWRAP_LOG_DEBUG
, "user[%s] not found\n", name
);
3197 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3199 /* misc functions */
3200 static int nwrap_files_initgroups(struct nwrap_backend
*b
,
3209 groups
= (gid_t
*)malloc(size
* sizeof(gid_t
));
3210 if (groups
== NULL
) {
3211 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
3217 nwrap_files_setgrent(b
);
3218 while ((grp
= nwrap_files_getgrent(b
)) != NULL
) {
3221 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3222 "Inspecting %s for group membership",
3225 for (i
=0; grp
->gr_mem
&& grp
->gr_mem
[i
] != NULL
; i
++) {
3226 if (group
!= grp
->gr_gid
&&
3227 (strcmp(user
, grp
->gr_mem
[i
]) == 0)) {
3228 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3229 "%s is member of %s",
3233 groups
= (gid_t
*)realloc(groups
,
3234 (size
+ 1) * sizeof(gid_t
));
3235 if (groups
== NULL
) {
3236 NWRAP_LOG(NWRAP_LOG_ERROR
,
3242 groups
[size
] = grp
->gr_gid
;
3248 nwrap_files_endgrent(b
);
3250 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3251 "%s is member of %d groups",
3254 /* This really only works if uid_wrapper is loaded */
3255 rc
= setgroups(size
, groups
);
3262 /* group functions */
3263 static struct group
*nwrap_files_getgrnam(struct nwrap_backend
*b
,
3269 (void) b
; /* unused */
3271 ok
= nwrap_files_cache_reload(nwrap_gr_global
.cache
);
3273 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading group file");
3277 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
3278 if (strcmp(nwrap_gr_global
.list
[i
].gr_name
, name
) == 0) {
3279 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] found", name
);
3280 return &nwrap_gr_global
.list
[i
];
3282 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3283 "group[%s] does not match [%s]",
3285 nwrap_gr_global
.list
[i
].gr_name
);
3288 NWRAP_LOG(NWRAP_LOG_DEBUG
, "group[%s] not found", name
);
3294 static int nwrap_files_getgrnam_r(struct nwrap_backend
*b
,
3295 const char *name
, struct group
*grdst
,
3296 char *buf
, size_t buflen
, struct group
**grdstp
)
3300 gr
= nwrap_files_getgrnam(b
, name
);
3308 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
3311 static struct group
*nwrap_files_getgrgid(struct nwrap_backend
*b
,
3317 (void) b
; /* unused */
3319 ok
= nwrap_files_cache_reload(nwrap_gr_global
.cache
);
3321 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading group file");
3325 for (i
=0; i
<nwrap_gr_global
.num
; i
++) {
3326 if (nwrap_gr_global
.list
[i
].gr_gid
== gid
) {
3327 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] found", gid
);
3328 return &nwrap_gr_global
.list
[i
];
3330 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3331 "gid[%u] does not match [%u]",
3333 nwrap_gr_global
.list
[i
].gr_gid
);
3336 NWRAP_LOG(NWRAP_LOG_DEBUG
, "gid[%u] not found", gid
);
3342 static int nwrap_files_getgrgid_r(struct nwrap_backend
*b
,
3343 gid_t gid
, struct group
*grdst
,
3344 char *buf
, size_t buflen
, struct group
**grdstp
)
3348 gr
= nwrap_files_getgrgid(b
, gid
);
3356 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
3359 /* group enum functions */
3360 static void nwrap_files_setgrent(struct nwrap_backend
*b
)
3362 (void) b
; /* unused */
3364 nwrap_gr_global
.idx
= 0;
3367 static struct group
*nwrap_files_getgrent(struct nwrap_backend
*b
)
3371 (void) b
; /* unused */
3373 if (nwrap_gr_global
.idx
== 0) {
3376 ok
= nwrap_files_cache_reload(nwrap_gr_global
.cache
);
3378 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading group file");
3383 if (nwrap_gr_global
.idx
>= nwrap_gr_global
.num
) {
3388 gr
= &nwrap_gr_global
.list
[nwrap_gr_global
.idx
++];
3390 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3391 "return group[%s] gid[%u]",
3392 gr
->gr_name
, gr
->gr_gid
);
3397 static int nwrap_files_getgrent_r(struct nwrap_backend
*b
,
3398 struct group
*grdst
, char *buf
,
3399 size_t buflen
, struct group
**grdstp
)
3403 gr
= nwrap_files_getgrent(b
);
3411 return nwrap_gr_copy_r(gr
, grdst
, buf
, buflen
, grdstp
);
3414 static void nwrap_files_endgrent(struct nwrap_backend
*b
)
3416 (void) b
; /* unused */
3418 nwrap_gr_global
.idx
= 0;
3421 /* hosts functions */
3422 static int nwrap_files_gethostbyname(const char *name
, int af
,
3423 struct hostent
*result
,
3424 struct nwrap_vector
*addr_list
)
3426 struct nwrap_entlist
*el
;
3431 char canon_name
[DNS_NAME_MAX
] = { 0 };
3433 bool he_found
= false;
3436 ok
= nwrap_files_cache_reload(nwrap_he_global
.cache
);
3438 NWRAP_LOG(NWRAP_LOG_ERROR
, "error loading hosts file");
3442 name_len
= strlen(name
);
3443 if (name_len
< sizeof(canon_name
) && name
[name_len
- 1] == '.') {
3444 strncpy(canon_name
, name
, name_len
- 1);
3448 if (!str_tolower_copy(&h_name_lower
, name
)) {
3449 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3450 "Out of memory while converting to lower case");
3454 /* Look at hash table for element */
3455 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Searching for name: %s", h_name_lower
);
3456 e
.key
= h_name_lower
;
3458 e_p
= hsearch(e
, FIND
);
3460 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name %s not found.", h_name_lower
);
3461 SAFE_FREE(h_name_lower
);
3464 SAFE_FREE(h_name_lower
);
3466 /* Always cleanup vector and results */
3467 if (!nwrap_vector_is_initialized(addr_list
)) {
3468 if (!nwrap_vector_init(addr_list
)) {
3469 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3470 "Unable to initialize memory for addr_list vector");
3474 /* When vector is initialized data are valid no more.
3475 * Quick way how to free vector is: */
3476 addr_list
->count
= 0;
3479 /* Iterate through results */
3480 for (el
= (struct nwrap_entlist
*)e_p
->data
; el
!= NULL
; el
= el
->next
)
3484 /* Filter by address familiy if provided */
3485 if (af
!= AF_UNSPEC
&& he
->h_addrtype
!= af
) {
3491 * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3493 if (af
== AF_UNSPEC
&& he
->h_addrtype
!= AF_INET
) {
3498 memcpy(result
, he
, sizeof(struct hostent
));
3499 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3500 "Name found. Returning record for %s",
3504 nwrap_vector_merge(addr_list
, &el
->ed
->nwrap_addrdata
);
3505 result
->h_addr_list
= nwrap_vector_head(addr_list
);
3511 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3512 "Name found in database. No records matches type.");
3519 #ifdef HAVE_GETHOSTBYNAME_R
3520 static int nwrap_gethostbyname_r(const char *name
,
3521 struct hostent
*ret
,
3522 char *buf
, size_t buflen
,
3523 struct hostent
**result
, int *h_errnop
)
3525 struct nwrap_vector
*addr_list
= malloc(sizeof(struct nwrap_vector
));
3528 if (addr_list
== NULL
) {
3529 NWRAP_LOG(NWRAP_LOG_ERROR
,
3530 "Unable to allocate memory for address list");
3535 ZERO_STRUCTP(addr_list
);
3537 rc
= nwrap_files_gethostbyname(name
, AF_UNSPEC
, ret
, addr_list
);
3539 *h_errnop
= h_errno
;
3540 if (addr_list
->items
!= NULL
) {
3541 free(addr_list
->items
);
3543 SAFE_FREE(addr_list
);
3548 if (buflen
< (addr_list
->count
* sizeof(void *))) {
3549 SAFE_FREE(addr_list
->items
);
3550 SAFE_FREE(addr_list
);
3554 /* Copy all to user provided buffer and change
3555 * pointers in returned structure.
3556 * +1 is for ending NULL pointer. */
3557 memcpy(buf
, addr_list
->items
, (addr_list
->count
+ 1) * sizeof(void *));
3559 free(addr_list
->items
);
3562 ret
->h_addr_list
= (char **)buf
;
3567 int gethostbyname_r(const char *name
,
3568 struct hostent
*ret
,
3569 char *buf
, size_t buflen
,
3570 struct hostent
**result
, int *h_errnop
)
3572 if (!nss_wrapper_hosts_enabled()) {
3573 return libc_gethostbyname_r(name
,
3581 return nwrap_gethostbyname_r(name
, ret
, buf
, buflen
, result
, h_errnop
);
3585 static int nwrap_files_getaddrinfo(const char *name
,
3586 unsigned short port
,
3587 const struct addrinfo
*hints
,
3588 struct addrinfo
**ai
)
3590 struct nwrap_entlist
*el
;
3592 struct addrinfo
*ai_head
= NULL
;
3593 struct addrinfo
*ai_cur
= NULL
;
3596 char canon_name
[DNS_NAME_MAX
] = { 0 };
3597 bool skip_canonname
= false;
3605 ok
= nwrap_files_cache_reload(nwrap_he_global
.cache
);
3607 NWRAP_LOG(NWRAP_LOG_ERROR
, "error loading hosts file");
3611 name_len
= strlen(name
);
3612 if (name_len
< DNS_NAME_MAX
&& name
[name_len
- 1] == '.') {
3613 strncpy(canon_name
, name
, name_len
- 1);
3617 if (!str_tolower_copy(&h_name_lower
, name
)) {
3618 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3619 "Out of memory while converting to lower case");
3623 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Searching for name: %s", h_name_lower
);
3624 e
.key
= h_name_lower
;
3626 e_p
= hsearch(e
, FIND
);
3628 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name %s not found.", h_name_lower
);
3629 SAFE_FREE(h_name_lower
);
3633 NWRAP_LOG(NWRAP_LOG_DEBUG
, "Name: %s found.", h_name_lower
);
3634 SAFE_FREE(h_name_lower
);
3637 for (el
= (struct nwrap_entlist
*)e_p
->data
; el
!= NULL
; el
= el
->next
)
3640 struct addrinfo
*ai_new
= NULL
;
3644 if (hints
->ai_family
!= AF_UNSPEC
&&
3645 he
->h_addrtype
!= hints
->ai_family
)
3647 NWRAP_LOG(NWRAP_LOG_DEBUG
,
3648 "Entry found but with wrong AF - "
3649 "remembering EAI_ADDRINFO.");
3650 rc
= EAI_ADDRFAMILY
;
3654 /* Function allocates memory and returns it in ai. */
3655 rc2
= nwrap_convert_he_ai(he
,
3661 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error converting he to ai");
3662 if (ai_head
!= NULL
) {
3663 freeaddrinfo(ai_head
);
3667 skip_canonname
= true;
3669 if (ai_head
== NULL
) {
3672 if (ai_cur
!= NULL
) {
3673 ai_cur
->ai_next
= ai_new
;
3678 if (ai_head
!= NULL
) {
3687 static struct hostent
*nwrap_files_gethostbyaddr(const void *addr
,
3688 socklen_t len
, int type
)
3691 char ip
[NWRAP_INET_ADDRSTRLEN
] = {0};
3692 struct nwrap_entdata
*ed
;
3697 (void) len
; /* unused */
3699 ok
= nwrap_files_cache_reload(nwrap_he_global
.cache
);
3701 NWRAP_LOG(NWRAP_LOG_ERROR
, "error loading hosts file");
3705 a
= inet_ntop(type
, addr
, ip
, sizeof(ip
));
3711 nwrap_vector_foreach(ed
, nwrap_he_global
.entries
, i
)
3714 if (he
->h_addrtype
!= type
) {
3718 if (memcmp(addr
, he
->h_addr_list
[0], he
->h_length
) == 0) {
3727 #ifdef HAVE_GETHOSTBYADDR_R
3728 static int nwrap_gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
3729 struct hostent
*ret
,
3730 char *buf
, size_t buflen
,
3731 struct hostent
**result
, int *h_errnop
)
3733 *result
= nwrap_files_gethostbyaddr(addr
, len
, type
);
3734 if (*result
!= NULL
) {
3735 memset(buf
, '\0', buflen
);
3739 *h_errnop
= h_errno
;
3744 int gethostbyaddr_r(const void *addr
, socklen_t len
, int type
,
3745 struct hostent
*ret
,
3746 char *buf
, size_t buflen
,
3747 struct hostent
**result
, int *h_errnop
)
3749 if (!nss_wrapper_hosts_enabled()) {
3750 return libc_gethostbyaddr_r(addr
,
3760 return nwrap_gethostbyaddr_r(addr
, len
, type
, ret
, buf
, buflen
, result
, h_errnop
);
3764 /* hosts enum functions */
3765 static void nwrap_files_sethostent(void)
3767 nwrap_he_global
.idx
= 0;
3770 static struct hostent
*nwrap_files_gethostent(void)
3774 if (nwrap_he_global
.idx
== 0) {
3777 ok
= nwrap_files_cache_reload(nwrap_he_global
.cache
);
3779 NWRAP_LOG(NWRAP_LOG_ERROR
, "Error loading hosts file");
3784 if (nwrap_he_global
.idx
>= nwrap_he_global
.num
) {
3789 he
= &((struct nwrap_entdata
*)nwrap_he_global
.entries
.items
[nwrap_he_global
.idx
++])->ht
;
3791 NWRAP_LOG(NWRAP_LOG_DEBUG
, "return hosts[%s]", he
->h_name
);
3796 static void nwrap_files_endhostent(void)
3798 nwrap_he_global
.idx
= 0;
3806 static struct passwd
*nwrap_module_getpwnam(struct nwrap_backend
*b
,
3809 static struct passwd pwd
;
3810 static char buf
[1000];
3813 if (!b
->fns
->_nss_getpwnam_r
) {
3817 status
= b
->fns
->_nss_getpwnam_r(name
, &pwd
, buf
, sizeof(buf
), &errno
);
3818 if (status
== NSS_STATUS_NOTFOUND
) {
3821 if (status
!= NSS_STATUS_SUCCESS
) {
3828 static int nwrap_module_getpwnam_r(struct nwrap_backend
*b
,
3829 const char *name
, struct passwd
*pwdst
,
3830 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3834 (void) b
; /* unused */
3835 (void) pwdst
; /* unused */
3836 (void) pwdstp
; /* unused */
3838 if (!b
->fns
->_nss_getpwnam_r
) {
3839 return NSS_STATUS_NOTFOUND
;
3842 ret
= b
->fns
->_nss_getpwnam_r(name
, pwdst
, buf
, buflen
, &errno
);
3844 case NSS_STATUS_SUCCESS
:
3846 case NSS_STATUS_NOTFOUND
:
3851 case NSS_STATUS_TRYAGAIN
:
3864 static struct passwd
*nwrap_module_getpwuid(struct nwrap_backend
*b
,
3867 static struct passwd pwd
;
3868 static char buf
[1000];
3871 if (!b
->fns
->_nss_getpwuid_r
) {
3875 status
= b
->fns
->_nss_getpwuid_r(uid
, &pwd
, buf
, sizeof(buf
), &errno
);
3876 if (status
== NSS_STATUS_NOTFOUND
) {
3879 if (status
!= NSS_STATUS_SUCCESS
) {
3885 static int nwrap_module_getpwuid_r(struct nwrap_backend
*b
,
3886 uid_t uid
, struct passwd
*pwdst
,
3887 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
3891 (void) pwdstp
; /* unused */
3893 if (!b
->fns
->_nss_getpwuid_r
) {
3897 ret
= b
->fns
->_nss_getpwuid_r(uid
, pwdst
, buf
, buflen
, &errno
);
3899 case NSS_STATUS_SUCCESS
:
3901 case NSS_STATUS_NOTFOUND
:
3906 case NSS_STATUS_TRYAGAIN
:
3919 static void nwrap_module_setpwent(struct nwrap_backend
*b
)
3921 if (!b
->fns
->_nss_setpwent
) {
3925 b
->fns
->_nss_setpwent();
3928 static struct passwd
*nwrap_module_getpwent(struct nwrap_backend
*b
)
3930 static struct passwd pwd
;
3931 static char buf
[1000];
3934 if (!b
->fns
->_nss_getpwent_r
) {
3938 status
= b
->fns
->_nss_getpwent_r(&pwd
, buf
, sizeof(buf
), &errno
);
3939 if (status
== NSS_STATUS_NOTFOUND
) {
3942 if (status
!= NSS_STATUS_SUCCESS
) {
3948 static int nwrap_module_getpwent_r(struct nwrap_backend
*b
,
3949 struct passwd
*pwdst
, char *buf
,
3950 size_t buflen
, struct passwd
**pwdstp
)
3954 (void) pwdstp
; /* unused */
3956 if (!b
->fns
->_nss_getpwent_r
) {
3960 ret
= b
->fns
->_nss_getpwent_r(pwdst
, buf
, buflen
, &errno
);
3962 case NSS_STATUS_SUCCESS
:
3964 case NSS_STATUS_NOTFOUND
:
3969 case NSS_STATUS_TRYAGAIN
:
3982 static void nwrap_module_endpwent(struct nwrap_backend
*b
)
3984 if (!b
->fns
->_nss_endpwent
) {
3988 b
->fns
->_nss_endpwent();
3991 static int nwrap_module_initgroups(struct nwrap_backend
*b
,
3992 const char *user
, gid_t group
)
3998 if (!b
->fns
->_nss_initgroups
) {
3999 return NSS_STATUS_UNAVAIL
;
4002 return b
->fns
->_nss_initgroups(user
, group
, &start
, &size
, &groups
, 0, &errno
);
4005 static struct group
*nwrap_module_getgrnam(struct nwrap_backend
*b
,
4008 static struct group grp
;
4010 static int buflen
= 1000;
4013 if (!b
->fns
->_nss_getgrnam_r
) {
4018 buf
= (char *)malloc(buflen
);
4021 status
= b
->fns
->_nss_getgrnam_r(name
, &grp
, buf
, buflen
, &errno
);
4022 if (status
== NSS_STATUS_TRYAGAIN
) {
4024 buf
= (char *)realloc(buf
, buflen
);
4030 if (status
== NSS_STATUS_NOTFOUND
) {
4034 if (status
!= NSS_STATUS_SUCCESS
) {
4041 static int nwrap_module_getgrnam_r(struct nwrap_backend
*b
,
4042 const char *name
, struct group
*grdst
,
4043 char *buf
, size_t buflen
, struct group
**grdstp
)
4047 (void) grdstp
; /* unused */
4049 if (!b
->fns
->_nss_getgrnam_r
) {
4053 ret
= b
->fns
->_nss_getgrnam_r(name
, grdst
, buf
, buflen
, &errno
);
4055 case NSS_STATUS_SUCCESS
:
4057 case NSS_STATUS_NOTFOUND
:
4062 case NSS_STATUS_TRYAGAIN
:
4075 static struct group
*nwrap_module_getgrgid(struct nwrap_backend
*b
,
4078 static struct group grp
;
4080 static int buflen
= 1000;
4083 if (!b
->fns
->_nss_getgrgid_r
) {
4088 buf
= (char *)malloc(buflen
);
4092 status
= b
->fns
->_nss_getgrgid_r(gid
, &grp
, buf
, buflen
, &errno
);
4093 if (status
== NSS_STATUS_TRYAGAIN
) {
4095 buf
= (char *)realloc(buf
, buflen
);
4101 if (status
== NSS_STATUS_NOTFOUND
) {
4105 if (status
!= NSS_STATUS_SUCCESS
) {
4112 static int nwrap_module_getgrgid_r(struct nwrap_backend
*b
,
4113 gid_t gid
, struct group
*grdst
,
4114 char *buf
, size_t buflen
, struct group
**grdstp
)
4118 (void) grdstp
; /* unused */
4120 if (!b
->fns
->_nss_getgrgid_r
) {
4124 ret
= b
->fns
->_nss_getgrgid_r(gid
, grdst
, buf
, buflen
, &errno
);
4126 case NSS_STATUS_SUCCESS
:
4128 case NSS_STATUS_NOTFOUND
:
4133 case NSS_STATUS_TRYAGAIN
:
4146 static void nwrap_module_setgrent(struct nwrap_backend
*b
)
4148 if (!b
->fns
->_nss_setgrent
) {
4152 b
->fns
->_nss_setgrent();
4155 static struct group
*nwrap_module_getgrent(struct nwrap_backend
*b
)
4157 static struct group grp
;
4159 static int buflen
= 1024;
4162 if (!b
->fns
->_nss_getgrent_r
) {
4167 buf
= (char *)malloc(buflen
);
4171 status
= b
->fns
->_nss_getgrent_r(&grp
, buf
, buflen
, &errno
);
4172 if (status
== NSS_STATUS_TRYAGAIN
) {
4174 buf
= (char *)realloc(buf
, buflen
);
4180 if (status
== NSS_STATUS_NOTFOUND
) {
4184 if (status
!= NSS_STATUS_SUCCESS
) {
4191 static int nwrap_module_getgrent_r(struct nwrap_backend
*b
,
4192 struct group
*grdst
, char *buf
,
4193 size_t buflen
, struct group
**grdstp
)
4197 (void) grdstp
; /* unused */
4199 if (!b
->fns
->_nss_getgrent_r
) {
4203 ret
= b
->fns
->_nss_getgrent_r(grdst
, buf
, buflen
, &errno
);
4205 case NSS_STATUS_SUCCESS
:
4207 case NSS_STATUS_NOTFOUND
:
4212 case NSS_STATUS_TRYAGAIN
:
4225 static void nwrap_module_endgrent(struct nwrap_backend
*b
)
4227 if (!b
->fns
->_nss_endgrent
) {
4231 b
->fns
->_nss_endgrent();
4234 /****************************************************************************
4236 ***************************************************************************/
4238 static struct passwd
*nwrap_getpwnam(const char *name
)
4243 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4244 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4245 pwd
= b
->ops
->nw_getpwnam(b
, name
);
4254 struct passwd
*getpwnam(const char *name
)
4256 if (!nss_wrapper_enabled()) {
4257 return libc_getpwnam(name
);
4260 return nwrap_getpwnam(name
);
4263 /****************************************************************************
4265 ***************************************************************************/
4267 static int nwrap_getpwnam_r(const char *name
, struct passwd
*pwdst
,
4268 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
4272 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4273 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4274 ret
= b
->ops
->nw_getpwnam_r(b
, name
, pwdst
, buf
, buflen
, pwdstp
);
4275 if (ret
== ENOENT
) {
4284 #ifdef HAVE_GETPWNAM_R
4285 # ifdef HAVE_SOLARIS_GETPWNAM_R
4286 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
4287 char *buf
, int buflen
, struct passwd
**pwdstp
)
4288 # else /* HAVE_SOLARIS_GETPWNAM_R */
4289 int getpwnam_r(const char *name
, struct passwd
*pwdst
,
4290 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
4291 # endif /* HAVE_SOLARIS_GETPWNAM_R */
4293 if (!nss_wrapper_enabled()) {
4294 return libc_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
4297 return nwrap_getpwnam_r(name
, pwdst
, buf
, buflen
, pwdstp
);
4301 /****************************************************************************
4303 ***************************************************************************/
4305 static struct passwd
*nwrap_getpwuid(uid_t uid
)
4310 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4311 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4312 pwd
= b
->ops
->nw_getpwuid(b
, uid
);
4321 struct passwd
*getpwuid(uid_t uid
)
4323 if (!nss_wrapper_enabled()) {
4324 return libc_getpwuid(uid
);
4327 return nwrap_getpwuid(uid
);
4330 /****************************************************************************
4332 ***************************************************************************/
4334 static int nwrap_getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
4335 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
4339 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4340 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4341 ret
= b
->ops
->nw_getpwuid_r(b
, uid
, pwdst
, buf
, buflen
, pwdstp
);
4342 if (ret
== ENOENT
) {
4351 #ifdef HAVE_SOLARIS_GETPWUID_R
4352 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
4353 char *buf
, int buflen
, struct passwd
**pwdstp
)
4355 int getpwuid_r(uid_t uid
, struct passwd
*pwdst
,
4356 char *buf
, size_t buflen
, struct passwd
**pwdstp
)
4359 if (!nss_wrapper_enabled()) {
4360 return libc_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
4363 return nwrap_getpwuid_r(uid
, pwdst
, buf
, buflen
, pwdstp
);
4366 /****************************************************************************
4368 ***************************************************************************/
4370 static void nwrap_setpwent(void)
4374 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4375 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4376 b
->ops
->nw_setpwent(b
);
4382 if (!nss_wrapper_enabled()) {
4390 /****************************************************************************
4392 ***************************************************************************/
4394 static struct passwd
*nwrap_getpwent(void)
4399 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4400 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4401 pwd
= b
->ops
->nw_getpwent(b
);
4410 struct passwd
*getpwent(void)
4412 if (!nss_wrapper_enabled()) {
4413 return libc_getpwent();
4416 return nwrap_getpwent();
4419 /****************************************************************************
4421 ***************************************************************************/
4423 static int nwrap_getpwent_r(struct passwd
*pwdst
, char *buf
,
4424 size_t buflen
, struct passwd
**pwdstp
)
4428 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4429 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4430 ret
= b
->ops
->nw_getpwent_r(b
, pwdst
, buf
, buflen
, pwdstp
);
4431 if (ret
== ENOENT
) {
4440 #ifdef HAVE_SOLARIS_GETPWENT_R
4441 struct passwd
*getpwent_r(struct passwd
*pwdst
, char *buf
, int buflen
)
4443 struct passwd
*pwdstp
= NULL
;
4446 if (!nss_wrapper_enabled()) {
4447 return libc_getpwent_r(pwdst
, buf
, buflen
);
4449 rc
= nwrap_getpwent_r(pwdst
, buf
, buflen
, &pwdstp
);
4456 #else /* HAVE_SOLARIS_GETPWENT_R */
4457 int getpwent_r(struct passwd
*pwdst
, char *buf
,
4458 size_t buflen
, struct passwd
**pwdstp
)
4460 if (!nss_wrapper_enabled()) {
4461 return libc_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
4464 return nwrap_getpwent_r(pwdst
, buf
, buflen
, pwdstp
);
4466 #endif /* HAVE_SOLARIS_GETPWENT_R */
4468 /****************************************************************************
4470 ***************************************************************************/
4472 static void nwrap_endpwent(void)
4476 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4477 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4478 b
->ops
->nw_endpwent(b
);
4484 if (!nss_wrapper_enabled()) {
4492 /****************************************************************************
4494 ***************************************************************************/
4496 static int nwrap_initgroups(const char *user
, gid_t group
)
4500 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4501 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4504 rc
= b
->ops
->nw_initgroups(b
, user
, group
);
4514 int initgroups(const char *user
, gid_t group
)
4516 if (!nss_wrapper_enabled()) {
4517 return libc_initgroups(user
, group
);
4520 return nwrap_initgroups(user
, group
);
4523 /****************************************************************************
4525 ***************************************************************************/
4527 static struct group
*nwrap_getgrnam(const char *name
)
4532 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4533 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4534 grp
= b
->ops
->nw_getgrnam(b
, name
);
4543 struct group
*getgrnam(const char *name
)
4545 if (!nss_wrapper_enabled()) {
4546 return libc_getgrnam(name
);
4549 return nwrap_getgrnam(name
);
4552 /****************************************************************************
4554 ***************************************************************************/
4556 static int nwrap_getgrnam_r(const char *name
, struct group
*grdst
,
4557 char *buf
, size_t buflen
, struct group
**grdstp
)
4561 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4562 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4563 ret
= b
->ops
->nw_getgrnam_r(b
, name
, grdst
, buf
, buflen
, grdstp
);
4564 if (ret
== ENOENT
) {
4573 #ifdef HAVE_GETGRNAM_R
4574 # ifdef HAVE_SOLARIS_GETGRNAM_R
4575 int getgrnam_r(const char *name
, struct group
*grp
,
4576 char *buf
, int buflen
, struct group
**pgrp
)
4577 # else /* HAVE_SOLARIS_GETGRNAM_R */
4578 int getgrnam_r(const char *name
, struct group
*grp
,
4579 char *buf
, size_t buflen
, struct group
**pgrp
)
4580 # endif /* HAVE_SOLARIS_GETGRNAM_R */
4582 if (!nss_wrapper_enabled()) {
4583 return libc_getgrnam_r(name
,
4590 return nwrap_getgrnam_r(name
, grp
, buf
, buflen
, pgrp
);
4592 #endif /* HAVE_GETGRNAM_R */
4594 /****************************************************************************
4596 ***************************************************************************/
4598 static struct group
*nwrap_getgrgid(gid_t gid
)
4603 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4604 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4605 grp
= b
->ops
->nw_getgrgid(b
, gid
);
4614 struct group
*getgrgid(gid_t gid
)
4616 if (!nss_wrapper_enabled()) {
4617 return libc_getgrgid(gid
);
4620 return nwrap_getgrgid(gid
);
4623 /****************************************************************************
4625 ***************************************************************************/
4627 static int nwrap_getgrgid_r(gid_t gid
, struct group
*grdst
,
4628 char *buf
, size_t buflen
, struct group
**grdstp
)
4632 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4633 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4634 ret
= b
->ops
->nw_getgrgid_r(b
, gid
, grdst
, buf
, buflen
, grdstp
);
4635 if (ret
== ENOENT
) {
4644 #ifdef HAVE_GETGRGID_R
4645 # ifdef HAVE_SOLARIS_GETGRGID_R
4646 int getgrgid_r(gid_t gid
, struct group
*grdst
,
4647 char *buf
, int buflen
, struct group
**grdstp
)
4648 # else /* HAVE_SOLARIS_GETGRGID_R */
4649 int getgrgid_r(gid_t gid
, struct group
*grdst
,
4650 char *buf
, size_t buflen
, struct group
**grdstp
)
4651 # endif /* HAVE_SOLARIS_GETGRGID_R */
4653 if (!nss_wrapper_enabled()) {
4654 return libc_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
4657 return nwrap_getgrgid_r(gid
, grdst
, buf
, buflen
, grdstp
);
4661 /****************************************************************************
4663 ***************************************************************************/
4665 static void nwrap_setgrent(void)
4669 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4670 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4671 b
->ops
->nw_setgrent(b
);
4675 #ifdef HAVE_BSD_SETGRENT
4681 if (!nss_wrapper_enabled()) {
4689 #ifdef HAVE_BSD_SETGRENT
4696 /****************************************************************************
4698 ***************************************************************************/
4700 static struct group
*nwrap_getgrent(void)
4705 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4706 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4707 grp
= b
->ops
->nw_getgrent(b
);
4716 struct group
*getgrent(void)
4718 if (!nss_wrapper_enabled()) {
4719 return libc_getgrent();
4722 return nwrap_getgrent();
4725 /****************************************************************************
4727 ***************************************************************************/
4729 static int nwrap_getgrent_r(struct group
*grdst
, char *buf
,
4730 size_t buflen
, struct group
**grdstp
)
4734 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4735 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4736 ret
= b
->ops
->nw_getgrent_r(b
, grdst
, buf
, buflen
, grdstp
);
4737 if (ret
== ENOENT
) {
4746 #ifdef HAVE_SOLARIS_GETGRENT_R
4747 struct group
*getgrent_r(struct group
*src
, char *buf
, int buflen
)
4749 struct group
*grdstp
= NULL
;
4752 if (!nss_wrapper_enabled()) {
4753 return libc_getgrent_r(src
, buf
, buflen
);
4756 rc
= nwrap_getgrent_r(src
, buf
, buflen
, &grdstp
);
4763 #else /* HAVE_SOLARIS_GETGRENT_R */
4764 int getgrent_r(struct group
*src
, char *buf
,
4765 size_t buflen
, struct group
**grdstp
)
4767 if (!nss_wrapper_enabled()) {
4768 return libc_getgrent_r(src
, buf
, buflen
, grdstp
);
4771 return nwrap_getgrent_r(src
, buf
, buflen
, grdstp
);
4773 #endif /* HAVE_SOLARIS_GETGRENT_R */
4775 /****************************************************************************
4777 ***************************************************************************/
4779 static void nwrap_endgrent(void)
4783 for (i
=0; i
< nwrap_main_global
->num_backends
; i
++) {
4784 struct nwrap_backend
*b
= &nwrap_main_global
->backends
[i
];
4785 b
->ops
->nw_endgrent(b
);
4791 if (!nss_wrapper_enabled()) {
4799 /****************************************************************************
4801 ***************************************************************************/
4803 #ifdef HAVE_GETGROUPLIST
4804 static int nwrap_getgrouplist(const char *user
, gid_t group
,
4805 gid_t
*groups
, int *ngroups
)
4811 NWRAP_LOG(NWRAP_LOG_DEBUG
, "getgrouplist called for %s", user
);
4813 groups_tmp
= (gid_t
*)malloc(count
* sizeof(gid_t
));
4815 NWRAP_LOG(NWRAP_LOG_ERROR
, "Out of memory");
4819 groups_tmp
[0] = group
;
4822 while ((grp
= nwrap_getgrent()) != NULL
) {
4825 NWRAP_LOG(NWRAP_LOG_DEBUG
,
4826 "Inspecting %s for group membership",
4829 for (i
=0; grp
->gr_mem
&& grp
->gr_mem
[i
] != NULL
; i
++) {
4831 if (group
!= grp
->gr_gid
&&
4832 (strcmp(user
, grp
->gr_mem
[i
]) == 0)) {
4834 NWRAP_LOG(NWRAP_LOG_DEBUG
,
4835 "%s is member of %s",
4839 groups_tmp
= (gid_t
*)realloc(groups_tmp
, (count
+ 1) * sizeof(gid_t
));
4841 NWRAP_LOG(NWRAP_LOG_ERROR
,
4846 groups_tmp
[count
] = grp
->gr_gid
;
4855 NWRAP_LOG(NWRAP_LOG_DEBUG
,
4856 "%s is member of %d groups",
4859 if (*ngroups
< count
) {
4866 memcpy(groups
, groups_tmp
, count
* sizeof(gid_t
));
4872 int getgrouplist(const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
)
4874 if (!nss_wrapper_enabled()) {
4875 return libc_getgrouplist(user
, group
, groups
, ngroups
);
4878 return nwrap_getgrouplist(user
, group
, groups
, ngroups
);
4882 /**********************************************************
4884 **********************************************************/
4886 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
4888 #ifdef HAVE_SETSPENT
4889 static void nwrap_setspent(void)
4891 nwrap_files_setspent();
4896 if (!nss_wrapper_shadow_enabled()) {
4903 static struct spwd
*nwrap_getspent(void)
4905 return nwrap_files_getspent();
4908 struct spwd
*getspent(void)
4910 if (!nss_wrapper_shadow_enabled()) {
4914 return nwrap_getspent();
4917 static void nwrap_endspent(void)
4919 nwrap_files_endspent();
4924 if (!nss_wrapper_shadow_enabled()) {
4930 #endif /* HAVE_SETSPENT */
4932 static struct spwd
*nwrap_getspnam(const char *name
)
4934 return nwrap_files_getspnam(name
);
4937 struct spwd
*getspnam(const char *name
)
4939 if (!nss_wrapper_shadow_enabled()) {
4943 return nwrap_getspnam(name
);
4946 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
4948 /**********************************************************
4950 **********************************************************/
4952 static void nwrap_sethostent(int stayopen
) {
4953 (void) stayopen
; /* ignored */
4955 nwrap_files_sethostent();
4958 #ifdef HAVE_SOLARIS_SETHOSTENT
4959 int sethostent(int stayopen
)
4961 if (!nss_wrapper_hosts_enabled()) {
4962 libc_sethostent(stayopen
);
4966 nwrap_sethostent(stayopen
);
4970 #else /* HAVE_SOLARIS_SETHOSTENT */
4971 void sethostent(int stayopen
)
4973 if (!nss_wrapper_hosts_enabled()) {
4974 libc_sethostent(stayopen
);
4978 nwrap_sethostent(stayopen
);
4980 #endif /* HAVE_SOLARIS_SETHOSTENT */
4982 static struct hostent
*nwrap_gethostent(void)
4984 return nwrap_files_gethostent();
4987 struct hostent
*gethostent(void) {
4988 if (!nss_wrapper_hosts_enabled()) {
4989 return libc_gethostent();
4992 return nwrap_gethostent();
4995 static void nwrap_endhostent(void) {
4996 nwrap_files_endhostent();
4999 #ifdef HAVE_SOLARIS_ENDHOSTENT
5000 int endhostent(void)
5002 if (!nss_wrapper_hosts_enabled()) {
5011 #else /* HAVE_SOLARIS_ENDHOSTENT */
5012 void endhostent(void)
5014 if (!nss_wrapper_hosts_enabled()) {
5021 #endif /* HAVE_SOLARIS_ENDHOSTENT */
5024 /* BSD implementation stores data in thread local storage but GLIBC does not */
5025 static __thread
struct hostent user_he
;
5026 static __thread
struct nwrap_vector user_addrlist
;
5028 static struct hostent user_he
;
5029 static struct nwrap_vector user_addrlist
;
5031 static struct hostent
*nwrap_gethostbyname(const char *name
)
5033 if (nwrap_files_gethostbyname(name
, AF_UNSPEC
, &user_he
, &user_addrlist
) == -1) {
5039 struct hostent
*gethostbyname(const char *name
)
5041 if (!nss_wrapper_hosts_enabled()) {
5042 return libc_gethostbyname(name
);
5045 return nwrap_gethostbyname(name
);
5048 /* This is a GNU extension - Also can be found on BSD systems */
5049 #ifdef HAVE_GETHOSTBYNAME2
5051 /* BSD implementation stores data in thread local storage but GLIBC not */
5052 static __thread
struct hostent user_he2
;
5053 static __thread
struct nwrap_vector user_addrlist2
;
5055 static struct hostent user_he2
;
5056 static struct nwrap_vector user_addrlist2
;
5058 static struct hostent
*nwrap_gethostbyname2(const char *name
, int af
)
5060 if (nwrap_files_gethostbyname(name
, af
, &user_he2
, &user_addrlist2
) == -1) {
5066 struct hostent
*gethostbyname2(const char *name
, int af
)
5068 if (!nss_wrapper_hosts_enabled()) {
5069 return libc_gethostbyname2(name
, af
);
5072 return nwrap_gethostbyname2(name
, af
);
5076 static struct hostent
*nwrap_gethostbyaddr(const void *addr
,
5077 socklen_t len
, int type
)
5079 return nwrap_files_gethostbyaddr(addr
, len
, type
);
5082 struct hostent
*gethostbyaddr(const void *addr
,
5083 socklen_t len
, int type
)
5085 if (!nss_wrapper_hosts_enabled()) {
5086 return libc_gethostbyaddr(addr
, len
, type
);
5089 return nwrap_gethostbyaddr(addr
, len
, type
);
5092 static const struct addrinfo default_hints
=
5094 .ai_flags
= AI_ADDRCONFIG
|AI_V4MAPPED
,
5095 .ai_family
= AF_UNSPEC
,
5100 .ai_canonname
= NULL
,
5104 static int nwrap_convert_he_ai(const struct hostent
*he
,
5105 unsigned short port
,
5106 const struct addrinfo
*hints
,
5107 struct addrinfo
**pai
,
5108 bool skip_canonname
)
5110 struct addrinfo
*ai
;
5117 switch (he
->h_addrtype
) {
5119 socklen
= sizeof(struct sockaddr_in
);
5123 socklen
= sizeof(struct sockaddr_in6
);
5130 ai
= (struct addrinfo
*)malloc(sizeof(struct addrinfo
) + socklen
);
5135 ai
->ai_flags
= hints
->ai_flags
;
5136 ai
->ai_family
= he
->h_addrtype
;
5137 ai
->ai_socktype
= hints
->ai_socktype
;
5138 ai
->ai_protocol
= hints
->ai_protocol
;
5139 ai
->ai_canonname
= NULL
;
5141 if (ai
->ai_socktype
== 0) {
5142 ai
->ai_socktype
= SOCK_DGRAM
;
5144 if (ai
->ai_protocol
== 0) {
5145 if (ai
->ai_socktype
== SOCK_DGRAM
) {
5146 ai
->ai_protocol
= IPPROTO_UDP
;
5147 } else if (ai
->ai_socktype
== SOCK_STREAM
) {
5148 ai
->ai_protocol
= IPPROTO_TCP
;
5152 ai
->ai_addrlen
= socklen
;
5153 ai
->ai_addr
= (void *)(ai
+ 1);
5155 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
5156 ai
->ai_addr
->sa_len
= socklen
;
5158 ai
->ai_addr
->sa_family
= he
->h_addrtype
;
5160 switch (he
->h_addrtype
) {
5163 struct sockaddr_in
*sinp
=
5164 (struct sockaddr_in
*) ai
->ai_addr
;
5166 memset(sinp
, 0, sizeof(struct sockaddr_in
));
5168 sinp
->sin_port
= htons(port
);
5169 sinp
->sin_family
= AF_INET
;
5171 memset (sinp
->sin_zero
, '\0', sizeof (sinp
->sin_zero
));
5172 memcpy(&sinp
->sin_addr
, he
->h_addr_list
[0], he
->h_length
);
5179 struct sockaddr_in6
*sin6p
=
5180 (struct sockaddr_in6
*) ai
->ai_addr
;
5182 memset(sin6p
, 0, sizeof(struct sockaddr_in6
));
5184 sin6p
->sin6_port
= htons(port
);
5185 sin6p
->sin6_family
= AF_INET6
;
5187 memcpy(&sin6p
->sin6_addr
,
5197 if (he
->h_name
&& !skip_canonname
) {
5198 ai
->ai_canonname
= strdup(he
->h_name
);
5199 if (ai
->ai_canonname
== NULL
) {
5209 static int nwrap_getaddrinfo(const char *node
,
5210 const char *service
,
5211 const struct addrinfo
*hints
,
5212 struct addrinfo
**res
)
5214 struct addrinfo
*ai
= NULL
;
5215 unsigned short port
= 0;
5225 .family
= AF_UNSPEC
,
5229 if (node
== NULL
&& service
== NULL
) {
5233 if (hints
== NULL
) {
5234 hints
= &default_hints
;
5238 hints.ai_flags contains invalid flags; or, hints.ai_flags
5239 included AI_CANONNAME and name was NULL.
5241 if ((hints
->ai_flags
& AI_CANONNAME
) && (node
== NULL
)) {
5242 return EAI_BADFLAGS
;
5245 /* If no node has been specified, let glibc deal with it */
5248 struct addrinfo
*p
= NULL
;
5250 ret
= libc_getaddrinfo(node
, service
, hints
, &p
);
5258 if (service
!= NULL
&& service
[0] != '\0') {
5259 const char *proto
= NULL
;
5265 sl
= strtol(service
, &end_ptr
, 10);
5267 if (*end_ptr
== '\0') {
5270 } else if (hints
->ai_flags
& AI_NUMERICSERV
) {
5274 if (hints
->ai_protocol
!= 0) {
5275 struct protoent
*pent
;
5277 pent
= getprotobynumber(hints
->ai_protocol
);
5279 proto
= pent
->p_name
;
5283 s
= getservbyname(service
, proto
);
5287 port
= ntohs(s
->s_port
);
5292 rc
= inet_pton(AF_INET
, node
, &addr
.in
.v4
);
5294 addr
.family
= AF_INET
;
5297 if (addr
.family
== AF_UNSPEC
) {
5298 rc
= inet_pton(AF_INET6
, node
, &addr
.in
.v6
);
5300 addr
.family
= AF_INET6
;
5305 if (addr
.family
== AF_UNSPEC
) {
5306 if (hints
->ai_flags
& AI_NUMERICHOST
) {
5309 } else if ((hints
->ai_family
!= AF_UNSPEC
) &&
5310 (hints
->ai_family
!= addr
.family
))
5312 return EAI_ADDRFAMILY
;
5315 rc
= nwrap_files_getaddrinfo(node
, port
, hints
, &ai
);
5318 struct addrinfo
*p
= NULL
;
5320 ret
= libc_getaddrinfo(node
, service
, hints
, &p
);
5324 * nwrap_files_getaddrinfo failed, but libc was
5325 * successful -- use the result from libc.
5335 * If the socktype was not specified, duplicate
5336 * each ai returned, so that we have variants for
5339 if (hints
->ai_socktype
== 0) {
5340 struct addrinfo
*ai_cur
;
5342 /* freeaddrinfo() frees ai_canonname and ai so allocate them */
5343 for (ai_cur
= ai
; ai_cur
!= NULL
; ai_cur
= ai_cur
->ai_next
) {
5344 struct addrinfo
*ai_new
;
5346 /* duplicate the current entry */
5348 ai_new
= malloc(sizeof(struct addrinfo
));
5349 if (ai_new
== NULL
) {
5354 memcpy(ai_new
, ai_cur
, sizeof(struct addrinfo
));
5355 ai_new
->ai_next
= NULL
;
5357 /* We need a deep copy or freeaddrinfo() will blow up */
5358 if (ai_cur
->ai_canonname
!= NULL
) {
5359 ai_new
->ai_canonname
=
5360 strdup(ai_cur
->ai_canonname
);
5363 if (ai_cur
->ai_socktype
== SOCK_DGRAM
) {
5364 ai_new
->ai_socktype
= SOCK_STREAM
;
5365 } else if (ai_cur
->ai_socktype
== SOCK_STREAM
) {
5366 ai_new
->ai_socktype
= SOCK_DGRAM
;
5368 if (ai_cur
->ai_protocol
== IPPROTO_TCP
) {
5369 ai_new
->ai_protocol
= IPPROTO_UDP
;
5370 } else if (ai_cur
->ai_protocol
== IPPROTO_UDP
) {
5371 ai_new
->ai_protocol
= IPPROTO_TCP
;
5374 /* now insert the new entry */
5376 ai_new
->ai_next
= ai_cur
->ai_next
;
5377 ai_cur
->ai_next
= ai_new
;
5379 /* and move on (don't duplicate the new entry) */
5390 int getaddrinfo(const char *node
, const char *service
,
5391 const struct addrinfo
*hints
,
5392 struct addrinfo
**res
)
5394 if (!nss_wrapper_hosts_enabled()) {
5395 return libc_getaddrinfo(node
, service
, hints
, res
);
5398 return nwrap_getaddrinfo(node
, service
, hints
, res
);
5401 static int nwrap_getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
5402 char *host
, size_t hostlen
,
5403 char *serv
, size_t servlen
,
5407 struct servent
*service
;
5414 if (sa
== NULL
|| salen
< sizeof(sa_family_t
)) {
5418 if ((flags
& NI_NAMEREQD
) && host
== NULL
&& serv
== NULL
) {
5422 type
= sa
->sa_family
;
5425 if (salen
< sizeof(struct sockaddr_in
))
5427 addr
= &((const struct sockaddr_in
*)sa
)->sin_addr
;
5428 addrlen
= sizeof(((const struct sockaddr_in
*)sa
)->sin_addr
);
5429 port
= ntohs(((const struct sockaddr_in
*)sa
)->sin_port
);
5433 if (salen
< sizeof(struct sockaddr_in6
))
5435 addr
= &((const struct sockaddr_in6
*)sa
)->sin6_addr
;
5436 addrlen
= sizeof(((const struct sockaddr_in6
*)sa
)->sin6_addr
);
5437 port
= ntohs(((const struct sockaddr_in6
*)sa
)->sin6_port
);
5446 if ((flags
& NI_NUMERICHOST
) == 0) {
5447 he
= nwrap_files_gethostbyaddr(addr
, addrlen
, type
);
5448 if ((flags
& NI_NAMEREQD
) && (he
== NULL
|| he
->h_name
== NULL
))
5451 if (he
!= NULL
&& he
->h_name
!= NULL
) {
5452 if (strlen(he
->h_name
) >= hostlen
)
5453 return EAI_OVERFLOW
;
5454 snprintf(host
, hostlen
, "%s", he
->h_name
);
5455 if (flags
& NI_NOFQDN
)
5456 host
[strcspn(host
, ".")] = '\0';
5458 if (inet_ntop(type
, addr
, host
, hostlen
) == NULL
)
5459 return (errno
== ENOSPC
) ? EAI_OVERFLOW
: EAI_FAIL
;
5465 if ((flags
& NI_NUMERICSERV
) == 0) {
5466 proto
= (flags
& NI_DGRAM
) ? "udp" : "tcp";
5467 service
= getservbyport(htons(port
), proto
);
5469 if (service
!= NULL
) {
5470 if (strlen(service
->s_name
) >= servlen
)
5471 return EAI_OVERFLOW
;
5472 snprintf(serv
, servlen
, "%s", service
->s_name
);
5474 if (snprintf(serv
, servlen
, "%u", port
) >= (int) servlen
)
5475 return EAI_OVERFLOW
;
5482 #ifdef HAVE_LINUX_GETNAMEINFO
5483 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
5484 char *host
, socklen_t hostlen
,
5485 char *serv
, socklen_t servlen
,
5487 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
5488 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
5489 char *host
, socklen_t hostlen
,
5490 char *serv
, socklen_t servlen
,
5493 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
5494 char *host
, size_t hostlen
,
5495 char *serv
, size_t servlen
,
5499 if (!nss_wrapper_hosts_enabled()) {
5500 return libc_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
5503 return nwrap_getnameinfo(sa
, salen
, host
, hostlen
, serv
, servlen
, flags
);
5506 static int nwrap_gethostname(char *name
, size_t len
)
5508 const char *hostname
= getenv("NSS_WRAPPER_HOSTNAME");
5510 if (strlen(hostname
) >= len
) {
5511 errno
= ENAMETOOLONG
;
5514 snprintf(name
, len
, "%s", hostname
);
5519 #ifdef HAVE_SOLARIS_GETHOSTNAME
5520 int gethostname(char *name
, int len
)
5521 #else /* HAVE_SOLARIS_GETHOSTNAME */
5522 int gethostname(char *name
, size_t len
)
5523 #endif /* HAVE_SOLARIS_GETHOSTNAME */
5525 if (!nwrap_hostname_enabled()) {
5526 return libc_gethostname(name
, len
);
5529 return nwrap_gethostname(name
, len
);
5532 /****************************
5534 ***************************/
5537 * This function is called when the library is unloaded and makes sure that
5538 * sockets get closed and the unix file for the socket are unlinked.
5540 void nwrap_destructor(void)
5545 if (nwrap_main_global
!= NULL
) {
5546 struct nwrap_main
*m
= nwrap_main_global
;
5549 SAFE_FREE(m
->libc
->fns
);
5550 if (m
->libc
->handle
!= NULL
) {
5551 dlclose(m
->libc
->handle
);
5553 if (m
->libc
->nsl_handle
!= NULL
) {
5554 dlclose(m
->libc
->nsl_handle
);
5556 if (m
->libc
->sock_handle
!= NULL
) {
5557 dlclose(m
->libc
->sock_handle
);
5562 for (i
= 0; i
< m
->num_backends
; i
++) {
5563 struct nwrap_backend
*b
= &(m
->backends
[i
]);
5565 if (b
->so_handle
!= NULL
) {
5566 dlclose(b
->so_handle
);
5570 SAFE_FREE(m
->backends
);
5573 if (nwrap_pw_global
.cache
!= NULL
) {
5574 struct nwrap_cache
*c
= nwrap_pw_global
.cache
;
5576 nwrap_files_cache_unload(c
);
5582 SAFE_FREE(nwrap_pw_global
.list
);
5583 nwrap_pw_global
.num
= 0;
5586 if (nwrap_gr_global
.cache
!= NULL
) {
5587 struct nwrap_cache
*c
= nwrap_gr_global
.cache
;
5589 nwrap_files_cache_unload(c
);
5595 SAFE_FREE(nwrap_gr_global
.list
);
5596 nwrap_pw_global
.num
= 0;
5599 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
5600 if (nwrap_sp_global
.cache
!= NULL
) {
5601 struct nwrap_cache
*c
= nwrap_sp_global
.cache
;
5603 nwrap_files_cache_unload(c
);
5609 nwrap_sp_global
.num
= 0;
5611 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
5613 if (nwrap_he_global
.cache
!= NULL
) {
5614 struct nwrap_cache
*c
= nwrap_he_global
.cache
;
5616 nwrap_files_cache_unload(c
);
5622 nwrap_he_global
.num
= 0;
5625 free(user_addrlist
.items
);
5626 #ifdef HAVE_GETHOSTBYNAME2
5627 free(user_addrlist2
.items
);