ctdb-tests: Indentation fixups
[Samba.git] / lib / nss_wrapper / nss_wrapper.c
blob82581b17aaf73d868e5d5f62814f9178255fb545
1 /*
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>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "config.h"
38 #include <pthread.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/socket.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdarg.h>
46 #include <stdbool.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdint.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <ctype.h>
55 #include <netinet/in.h>
57 #include <search.h>
58 #include <assert.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
63 * Solaris
65 #ifndef _POSIX_PTHREAD_SEMANTICS
66 #define _POSIX_PTHREAD_SEMANTICS
67 #endif
69 #include <pwd.h>
70 #include <grp.h>
71 #ifdef HAVE_SHADOW_H
72 #include <shadow.h>
73 #endif /* HAVE_SHADOW_H */
75 #include <netdb.h>
76 #include <arpa/inet.h>
77 #include <netinet/in.h>
79 #include <dlfcn.h>
81 #if defined(HAVE_NSS_H)
82 /* Linux and BSD */
83 #include <nss.h>
85 typedef enum nss_status NSS_STATUS;
86 #elif defined(HAVE_NSS_COMMON_H)
87 /* Solaris */
88 #include <nss_common.h>
89 #include <nss_dbdefs.h>
90 #include <nsswitch.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
98 #else
99 # error "No nsswitch support detected"
100 #endif
102 #ifndef PTR_DIFF
103 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
104 #endif
106 #ifndef _PUBLIC_
107 #define _PUBLIC_
108 #endif
110 #ifndef EAI_NODATA
111 #define EAI_NODATA EAI_NONAME
112 #endif
114 #ifndef EAI_ADDRFAMILY
115 #define EAI_ADDRFAMILY EAI_FAMILY
116 #endif
118 #ifndef __STRING
119 #define __STRING(x) #x
120 #endif
122 #ifndef __STRINGSTRING
123 #define __STRINGSTRING(x) __STRING(x)
124 #endif
126 #ifndef __LINESTR__
127 #define __LINESTR__ __STRINGSTRING(__LINE__)
128 #endif
130 #ifndef __location__
131 #define __location__ __FILE__ ":" __LINESTR__
132 #endif
134 #ifndef DNS_NAME_MAX
135 #define DNS_NAME_MAX 255
136 #endif
138 /* GCC have printf type attribute check. */
139 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
140 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
141 #else
142 #define PRINTF_ATTRIBUTE(a,b)
143 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
145 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
146 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
147 #else
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)
153 #ifndef SAFE_FREE
154 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
155 #endif
157 #ifndef discard_const
158 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
159 #endif
161 #ifndef discard_const_p
162 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
163 #endif
165 #ifdef HAVE_IPV6
166 #define NWRAP_INET_ADDRSTRLEN INET6_ADDRSTRLEN
167 #else
168 #define NWRAP_INET_ADDRSTRLEN INET_ADDRSTRLEN
169 #endif
171 #define NWRAP_LOCK(m) do { \
172 pthread_mutex_lock(&( m ## _mutex)); \
173 } while(0)
175 #define NWRAP_UNLOCK(m) do { \
176 pthread_mutex_unlock(&( m ## _mutex)); \
177 } while(0)
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); \
201 } while (0);
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); \
210 } while (0);
212 static void nwrap_thread_prepare(void)
214 NWRAP_LOCK_ALL;
217 static void nwrap_thread_parent(void)
219 NWRAP_UNLOCK_ALL;
222 static void nwrap_thread_child(void)
224 NWRAP_UNLOCK_ALL;
227 enum nwrap_dbglvl_e {
228 NWRAP_LOG_ERROR = 0,
229 NWRAP_LOG_WARN,
230 NWRAP_LOG_DEBUG,
231 NWRAP_LOG_TRACE
234 #ifdef NDEBUG
235 # define NWRAP_LOG(...)
236 #else
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,
242 const char *func,
243 const char *format, ...)
245 char buffer[1024];
246 va_list va;
247 const char *d;
248 unsigned int lvl = 0;
249 int pid = getpid();
251 d = getenv("NSS_WRAPPER_DEBUGLEVEL");
252 if (d != NULL) {
253 lvl = atoi(d);
256 va_start(va, format);
257 vsnprintf(buffer, sizeof(buffer), format, va);
258 va_end(va);
260 if (lvl >= dbglvl) {
261 switch (dbglvl) {
262 case NWRAP_LOG_ERROR:
263 fprintf(stderr,
264 "NWRAP_ERROR(%d) - %s: %s\n",
265 pid, func, buffer);
266 break;
267 case NWRAP_LOG_WARN:
268 fprintf(stderr,
269 "NWRAP_WARN(%d) - %s: %s\n",
270 pid, func, buffer);
271 break;
272 case NWRAP_LOG_DEBUG:
273 fprintf(stderr,
274 "NWRAP_DEBUG(%d) - %s: %s\n",
275 pid, func, buffer);
276 break;
277 case NWRAP_LOG_TRACE:
278 fprintf(stderr,
279 "NWRAP_TRACE(%d) - %s: %s\n",
280 pid, func, buffer);
281 break;
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);
297 #else
298 int (*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp);
299 #endif
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);
310 #else
311 int (*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen, struct group **result);
312 #endif
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);
323 #endif
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,
332 int flags);
333 int (*_libc_gethostname)(char *name, size_t len);
334 #ifdef HAVE_GETHOSTBYNAME_R
335 int (*_libc_gethostbyname_r)(const char *name,
336 struct hostent *ret,
337 char *buf, size_t buflen,
338 struct hostent **result, int *h_errnop);
339 #endif
340 #ifdef HAVE_GETHOSTBYADDR_R
341 int (*_libc_gethostbyaddr_r)(const void *addr, socklen_t len, int type,
342 struct hostent *ret,
343 char *buf, size_t buflen,
344 struct hostent **result, int *h_errnop);
345 #endif
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 {
370 const char *name;
371 const char *so_path;
372 void *so_handle;
373 struct nwrap_ops *ops;
374 struct nwrap_module_nss_fns *fns;
377 struct nwrap_ops {
378 struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
379 const char *name);
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,
384 uid_t uid);
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,
397 const char *name);
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,
402 gid_t gid);
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,
424 const char *name);
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,
429 uid_t uid);
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,
442 const char *name);
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,
447 gid_t gid);
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,
465 const char *name);
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,
470 uid_t uid);
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,
481 const char *name);
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,
486 gid_t gid);
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,
535 struct nwrap_libc {
536 void *handle;
537 void *nsl_handle;
538 void *sock_handle;
539 struct nwrap_libc_fns *fns;
542 struct nwrap_main {
543 int num_backends;
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;
552 * PROTOTYPES
554 static int nwrap_convert_he_ai(const struct hostent *he,
555 unsigned short port,
556 const struct addrinfo *hints,
557 struct addrinfo **pai,
558 bool skip_canonname);
561 * VECTORS
564 #define DEFAULT_VECTOR_CAPACITY 16
566 struct nwrap_vector {
567 void **items;
568 size_t count;
569 size_t capacity;
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
578 * calls.
580 * nwrap_vector->items is array inside nwrap_vector which can be
581 * directly pointed by libc structure assembled by cwrap itself.
583 * EXAMPLE:
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
588 * file parsing etc.
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
593 * implementation.
594 * (Yes, not fully - array terminated by NULL is needed because
595 * it's result expected by libc function caller.)
598 * CODE EXAMPLE:
600 * struct hostent he;
601 * struct nwrap_vector *vector = malloc(sizeof(struct nwrap_vector));
602 * ... don't care about failed allocation now ...
604 * ... fill nwrap vector ...
606 * struct hostent he;
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]; \
614 item != NULL; \
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) {
622 return false;
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) {
629 return false;
631 vector->capacity = DEFAULT_VECTOR_CAPACITY;
632 memset(vector->items, '\0', sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
634 return true;
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));
650 if (items == NULL) {
651 return false;
653 vector->items = items;
655 /* Don't count ending NULL to capacity */
656 vector->capacity *= 2;
659 vector->items[vector->count] = item;
661 vector->count += 1;
662 vector->items[vector->count] = NULL;
664 return true;
667 static bool nwrap_vector_merge(struct nwrap_vector *dst,
668 struct nwrap_vector *src)
670 void **dst_items = NULL;
671 size_t count;
673 if (src->count == 0) {
674 return true;
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) {
683 return false;
685 dst->items = dst_items;
686 dst->capacity = count;
689 memcpy((void *)(((long *)dst->items) + dst->count),
690 src->items,
691 src->count * sizeof(void *));
692 dst->count = count;
694 return true;
697 struct nwrap_cache {
698 const char *path;
699 int fd;
700 FILE *fp;
701 struct stat st;
702 void *private_data;
704 struct nwrap_vector lines;
706 bool (*parse_line)(struct nwrap_cache *, char *line);
707 void (*unload)(struct nwrap_cache *);
710 /* passwd */
711 struct nwrap_pw {
712 struct nwrap_cache *cache;
714 struct passwd *list;
715 int num;
716 int idx;
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);
725 /* shadow */
726 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
727 struct nwrap_sp {
728 struct nwrap_cache *cache;
730 struct spwd *list;
731 int num;
732 int idx;
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) */
742 /* group */
743 struct nwrap_gr {
744 struct nwrap_cache *cache;
746 struct group *list;
747 int num;
748 int idx;
751 struct nwrap_cache __nwrap_cache_gr;
752 struct nwrap_gr nwrap_gr_global;
754 /* hosts */
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;
766 struct hostent ht;
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;
778 struct nwrap_he {
779 struct nwrap_cache *cache;
781 struct nwrap_vector entries;
782 struct nwrap_vector lists;
784 int num;
785 int idx;
788 static struct nwrap_cache __nwrap_cache_he;
789 static struct nwrap_he nwrap_he_global;
792 /*********************************************************
793 * NWRAP PROTOTYPES
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 *********************************************************/
805 enum nwrap_lib {
806 NWRAP_LIBC,
807 NWRAP_LIBNSL,
808 NWRAP_LIBSOCKET,
811 #ifndef NDEBUG
812 static const char *nwrap_str_lib(enum nwrap_lib lib)
814 switch (lib) {
815 case NWRAP_LIBC:
816 return "libc";
817 case NWRAP_LIBNSL:
818 return "libnsl";
819 case NWRAP_LIBSOCKET:
820 return "libsocket";
823 /* Compiler would warn us about unhandled enum value if we get here */
824 return "unknown";
826 #endif
828 static void *nwrap_load_lib_handle(enum nwrap_lib lib)
830 int flags = RTLD_LAZY;
831 void *handle = NULL;
832 int i;
834 #ifdef RTLD_DEEPBIND
835 flags |= RTLD_DEEPBIND;
836 #endif
838 switch (lib) {
839 case NWRAP_LIBNSL:
840 #ifdef HAVE_LIBNSL
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) {
849 break;
853 nwrap_main_global->libc->nsl_handle = handle;
855 break;
856 #endif
857 /* FALL TROUGH */
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) {
868 break;
872 nwrap_main_global->libc->sock_handle = handle;
874 break;
875 #endif
876 /* FALL TROUGH */
877 case NWRAP_LIBC:
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) {
886 break;
890 nwrap_main_global->libc->handle = handle;
892 break;
895 if (handle == NULL) {
896 #ifdef RTLD_NEXT
897 handle = nwrap_main_global->libc->handle
898 = nwrap_main_global->libc->sock_handle
899 = nwrap_main_global->libc->nsl_handle
900 = RTLD_NEXT;
901 #else
902 NWRAP_LOG(NWRAP_LOG_ERROR,
903 "Failed to dlopen library: %s\n",
904 dlerror());
905 exit(-1);
906 #endif
909 return handle;
912 static void *_nwrap_load_lib_function(enum nwrap_lib lib, const char *fn_name)
914 void *handle;
915 void *func;
917 nwrap_init();
919 handle = nwrap_load_lib_handle(lib);
921 func = dlsym(handle, fn_name);
922 if (func == NULL) {
923 NWRAP_LOG(NWRAP_LOG_ERROR,
924 "Failed to find %s: %s\n",
925 fn_name, dlerror());
926 exit(-1);
929 NWRAP_LOG(NWRAP_LOG_TRACE,
930 "Loaded %s from %s",
931 fn_name, nwrap_str_lib(lib));
932 return func;
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)
944 size_t p;
945 void *item;
946 nwrap_vector_foreach(item, nwrap->lines, p) {
947 /* Maybe some vectors were merged ... */
948 SAFE_FREE(item);
950 SAFE_FREE(nwrap->lines.items);
951 ZERO_STRUCTP(&nwrap->lines);
955 * IMPORTANT
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,
971 struct passwd *pwd,
972 char *buf,
973 size_t buflen,
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");
981 #else
982 nwrap_load_lib_function(NWRAP_LIBC, getpwnam_r);
983 #endif
985 return nwrap_main_global->libc->fns->_libc_getpwnam_r(name,
986 pwd,
987 buf,
988 buflen,
989 result);
991 #endif
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,
1002 struct passwd *pwd,
1003 char *buf,
1004 size_t buflen,
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");
1012 #else
1013 nwrap_load_lib_function(NWRAP_LIBC, getpwuid_r);
1014 #endif
1016 return nwrap_main_global->libc->fns->_libc_getpwuid_r(uid,
1017 pwd,
1018 buf,
1019 buflen,
1020 result);
1022 #endif
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);
1031 ++src_tmp;
1032 ++dst_tmp;
1036 static bool str_tolower_copy(char **dst_name, const char *const src_name)
1038 char *h_name_lower;
1040 if ((dst_name == NULL) || (src_name == NULL)) {
1041 return false;
1044 h_name_lower = strdup(src_name);
1045 if (h_name_lower == NULL) {
1046 NWRAP_LOG(NWRAP_LOG_DEBUG, "Out of memory while strdup");
1047 return false;
1050 str_tolower(h_name_lower, h_name_lower);
1051 *dst_name = h_name_lower;
1052 return true;
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,
1071 char *buf,
1072 int buflen)
1074 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1076 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1077 buf,
1078 buflen);
1080 #else /* HAVE_SOLARIS_GETPWENT_R */
1081 static int libc_getpwent_r(struct passwd *pwdst,
1082 char *buf,
1083 size_t buflen,
1084 struct passwd **pwdstp)
1086 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1088 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1089 buf,
1090 buflen,
1091 pwdstp);
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,
1118 struct group *grp,
1119 char *buf,
1120 size_t buflen,
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");
1128 #else
1129 nwrap_load_lib_function(NWRAP_LIBC, getgrnam_r);
1130 #endif
1132 return nwrap_main_global->libc->fns->_libc_getgrnam_r(name,
1133 grp,
1134 buf,
1135 buflen,
1136 result);
1138 #endif
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,
1149 struct group *grp,
1150 char *buf,
1151 size_t buflen,
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");
1159 #else
1160 nwrap_load_lib_function(NWRAP_LIBC, getgrgid_r);
1161 #endif
1163 return nwrap_main_global->libc->fns->_libc_getgrgid_r(gid,
1164 grp,
1165 buf,
1166 buflen,
1167 result);
1169 #endif
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,
1188 char *buf,
1189 size_t buflen)
1191 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1193 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1194 buf,
1195 buflen);
1197 #else /* !HAVE_SOLARIS_GETGRENT_R */
1198 static int libc_getgrent_r(struct group *group,
1199 char *buf,
1200 size_t buflen,
1201 struct group **result)
1203 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1205 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1206 buf,
1207 buflen,
1208 result);
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,
1222 gid_t group,
1223 gid_t *groups,
1224 int *ngroups)
1226 nwrap_load_lib_function(NWRAP_LIBC, getgrouplist);
1228 return nwrap_main_global->libc->fns->_libc_getgrouplist(user,
1229 group,
1230 groups,
1231 ngroups);
1233 #endif
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);
1270 #endif
1272 static struct hostent *libc_gethostbyaddr(const void *addr,
1273 socklen_t len,
1274 int type)
1276 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr);
1278 return nwrap_main_global->libc->fns->_libc_gethostbyaddr(addr,
1279 len,
1280 type);
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,
1293 char *buf,
1294 size_t buflen,
1295 struct hostent **result,
1296 int *h_errnop)
1298 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname_r);
1300 return nwrap_main_global->libc->fns->_libc_gethostbyname_r(name,
1301 ret,
1302 buf,
1303 buflen,
1304 result,
1305 h_errnop);
1307 #endif
1309 #ifdef HAVE_GETHOSTBYADDR_R
1310 static int libc_gethostbyaddr_r(const void *addr,
1311 socklen_t len,
1312 int type,
1313 struct hostent *ret,
1314 char *buf,
1315 size_t buflen,
1316 struct hostent **result,
1317 int *h_errnop)
1319 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr_r);
1321 return nwrap_main_global->libc->fns->_libc_gethostbyaddr_r(addr,
1322 len,
1323 type,
1324 ret,
1325 buf,
1326 buflen,
1327 result,
1328 h_errnop);
1330 #endif
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,
1340 service,
1341 hints,
1342 res);
1345 static int libc_getnameinfo(const struct sockaddr *sa,
1346 socklen_t salen,
1347 char *host,
1348 size_t hostlen,
1349 char *serv,
1350 size_t servlen,
1351 int flags)
1353 nwrap_load_lib_function(NWRAP_LIBSOCKET, getnameinfo);
1355 return nwrap_main_global->libc->fns->_libc_getnameinfo(sa,
1356 salen,
1357 host,
1358 hostlen,
1359 serv,
1360 servlen,
1361 flags);
1364 /*********************************************************
1365 * NWRAP NSS MODULE LOADER FUNCTIONS
1366 *********************************************************/
1368 static void *nwrap_load_module_fn(struct nwrap_backend *b,
1369 const char *fn_name)
1371 void *res;
1372 char *s;
1374 if (!b->so_handle) {
1375 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1376 return NULL;
1379 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
1380 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1381 return NULL;
1384 res = dlsym(b->so_handle, s);
1385 if (!res) {
1386 NWRAP_LOG(NWRAP_LOG_ERROR,
1387 "Cannot find function %s in %s",
1388 s, b->so_path);
1390 SAFE_FREE(s);
1391 return res;
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) {
1399 return NULL;
1402 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
1403 if (!fns) {
1404 return NULL;
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");
1430 return fns;
1433 static void *nwrap_load_module(const char *so_path)
1435 void *h;
1437 if (!so_path || !strlen(so_path)) {
1438 return NULL;
1441 h = dlopen(so_path, RTLD_LAZY);
1442 if (!h) {
1443 NWRAP_LOG(NWRAP_LOG_ERROR,
1444 "Cannot open shared library %s",
1445 so_path);
1446 return NULL;
1449 return h;
1452 static bool nwrap_module_init(const char *name,
1453 struct nwrap_ops *ops,
1454 const char *so_path,
1455 int *num_backends,
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));
1462 if (!*backends) {
1463 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1464 return false;
1467 b = &((*backends)[*num_backends]);
1469 b->name = name;
1470 b->ops = ops;
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) {
1477 return false;
1479 } else {
1480 b->so_handle = NULL;
1481 b->fns = NULL;
1484 (*num_backends)++;
1486 return true;
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");
1494 exit(-1);
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");
1501 exit(-1);
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;
1512 r->backends = NULL;
1514 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1515 &r->num_backends,
1516 &r->backends)) {
1517 NWRAP_LOG(NWRAP_LOG_ERROR,
1518 "Failed to initialize 'files' backend");
1519 return;
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,
1527 &nwrap_module_ops,
1528 module_so_path,
1529 &r->num_backends,
1530 &r->backends)) {
1531 NWRAP_LOG(NWRAP_LOG_ERROR,
1532 "Failed to initialize '%s' backend",
1533 module_fn_name);
1534 return;
1539 static void nwrap_init(void)
1541 const char *env;
1542 char *endptr;
1543 size_t max_hostents_tmp;
1545 NWRAP_LOCK(nwrap_initialized);
1546 if (nwrap_initialized) {
1547 NWRAP_UNLOCK(nwrap_initialized);
1548 return;
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");
1569 if (env != NULL) {
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);
1578 } else {
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");
1589 goto done;
1592 nwrap_main_global = &__nwrap_main_global;
1594 nwrap_libc_init(nwrap_main_global);
1596 nwrap_backend_init(nwrap_main_global);
1598 /* passwd */
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;
1608 /* shadow */
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) */
1620 /* group */
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;
1630 /* hosts */
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;
1640 done:
1641 /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1642 NWRAP_UNLOCK_ALL;
1645 bool nss_wrapper_enabled(void)
1647 nwrap_init();
1649 if (nwrap_pw_global.cache->path == NULL ||
1650 nwrap_pw_global.cache->path[0] == '\0') {
1651 return false;
1653 if (nwrap_gr_global.cache->path == NULL ||
1654 nwrap_gr_global.cache->path[0] == '\0') {
1655 return false;
1658 return true;
1661 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1662 bool nss_wrapper_shadow_enabled(void)
1664 nwrap_init();
1666 if (nwrap_sp_global.cache->path == NULL ||
1667 nwrap_sp_global.cache->path[0] == '\0') {
1668 return false;
1671 return true;
1673 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1675 bool nss_wrapper_hosts_enabled(void)
1677 nwrap_init();
1679 if (nwrap_he_global.cache->path == NULL ||
1680 nwrap_he_global.cache->path[0] == '\0') {
1681 return false;
1684 return true;
1687 static bool nwrap_hostname_enabled(void)
1689 nwrap_init();
1691 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
1692 return false;
1695 return true;
1698 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
1700 char *line = NULL;
1701 ssize_t n;
1702 /* Unused but getline needs it */
1703 size_t len;
1704 bool ok;
1706 if (nwrap->st.st_size == 0) {
1707 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
1708 return true;
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);
1716 return false;
1719 rewind(nwrap->fp);
1721 do {
1722 n = getline(&line, &len, nwrap->fp);
1723 if (n < 0) {
1724 SAFE_FREE(line);
1725 if (feof(nwrap->fp)) {
1726 break;
1729 NWRAP_LOG(NWRAP_LOG_ERROR,
1730 "Unable to read line from file: %s",
1731 nwrap->path);
1732 return false;
1735 if (line[n - 1] == '\n') {
1736 line[n - 1] = '\0';
1739 if (line[0] == '\0') {
1740 SAFE_FREE(line);
1741 continue;
1744 ok = nwrap->parse_line(nwrap, line);
1745 if (!ok) {
1746 NWRAP_LOG(NWRAP_LOG_ERROR,
1747 "Unable to parse line file: %s",
1748 line);
1749 SAFE_FREE(line);
1750 return false;
1753 /* Line is parsed without issues so add it to list */
1754 ok = nwrap_vector_add_item(&(nwrap->lines), (void *const) line);
1755 if (!ok) {
1756 NWRAP_LOG(NWRAP_LOG_ERROR,
1757 "Unable to add line to vector");
1758 return false;
1761 /* This forces getline to allocate new memory for line. */
1762 line = NULL;
1763 } while (!feof(nwrap->fp));
1765 return true;
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)
1777 struct stat st;
1778 int ret;
1779 bool ok;
1780 bool retried = false;
1782 assert(nwrap != NULL);
1784 reopen:
1785 if (nwrap->fd < 0) {
1786 nwrap->fp = fopen(nwrap->path, "re");
1787 if (nwrap->fp == NULL) {
1788 nwrap->fd = -1;
1789 NWRAP_LOG(NWRAP_LOG_ERROR,
1790 "Unable to open '%s' readonly %d:%s",
1791 nwrap->path, nwrap->fd,
1792 strerror(errno));
1793 return false;
1796 nwrap->fd = fileno(nwrap->fp);
1797 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
1800 ret = fstat(nwrap->fd, &st);
1801 if (ret != 0) {
1802 NWRAP_LOG(NWRAP_LOG_ERROR,
1803 "fstat(%s) - %d:%s",
1804 nwrap->path,
1805 ret,
1806 strerror(errno));
1807 fclose(nwrap->fp);
1808 nwrap->fp = NULL;
1809 nwrap->fd = -1;
1810 return false;
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",
1817 nwrap->path);
1818 retried = true;
1819 memset(&nwrap->st, 0, sizeof(nwrap->st));
1820 fclose(nwrap->fp);
1821 nwrap->fp = NULL;
1822 nwrap->fd = -1;
1823 goto reopen;
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);
1830 return true;
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);
1838 nwrap->st = st;
1840 nwrap_files_cache_unload(nwrap);
1842 ok = nwrap_parse_file(nwrap);
1843 if (!ok) {
1844 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
1845 nwrap_files_cache_unload(nwrap);
1846 return false;
1849 NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
1850 return true;
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;
1859 char *c;
1860 char *p;
1861 char *e;
1862 struct passwd *pw;
1863 size_t list_size;
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);
1869 if (!pw) {
1870 NWRAP_LOG(NWRAP_LOG_ERROR,
1871 "realloc(%u) failed",
1872 (unsigned)list_size);
1873 return false;
1875 nwrap_pw->list = pw;
1877 pw = &nwrap_pw->list[nwrap_pw->num];
1879 c = line;
1881 /* name */
1882 p = strchr(c, ':');
1883 if (!p) {
1884 NWRAP_LOG(NWRAP_LOG_ERROR,
1885 "Invalid line[%s]: '%s'",
1886 line,
1888 return false;
1890 *p = '\0';
1891 p++;
1892 pw->pw_name = c;
1893 c = p;
1895 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
1897 /* password */
1898 p = strchr(c, ':');
1899 if (!p) {
1900 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1901 return false;
1903 *p = '\0';
1904 p++;
1905 pw->pw_passwd = c;
1906 c = p;
1908 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
1910 /* uid */
1911 p = strchr(c, ':');
1912 if (!p) {
1913 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1914 return false;
1916 *p = '\0';
1917 p++;
1918 e = NULL;
1919 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
1920 if (c == e) {
1921 NWRAP_LOG(NWRAP_LOG_ERROR,
1922 "Invalid line[%s]: '%s' - %s",
1923 line, c, strerror(errno));
1924 return false;
1926 if (e == NULL) {
1927 NWRAP_LOG(NWRAP_LOG_ERROR,
1928 "Invalid line[%s]: '%s' - %s",
1929 line, c, strerror(errno));
1930 return false;
1932 if (e[0] != '\0') {
1933 NWRAP_LOG(NWRAP_LOG_ERROR,
1934 "Invalid line[%s]: '%s' - %s",
1935 line, c, strerror(errno));
1936 return false;
1938 c = p;
1940 NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
1942 /* gid */
1943 p = strchr(c, ':');
1944 if (!p) {
1945 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1946 return false;
1948 *p = '\0';
1949 p++;
1950 e = NULL;
1951 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
1952 if (c == e) {
1953 NWRAP_LOG(NWRAP_LOG_ERROR,
1954 "Invalid line[%s]: '%s' - %s",
1955 line, c, strerror(errno));
1956 return false;
1958 if (e == NULL) {
1959 NWRAP_LOG(NWRAP_LOG_ERROR,
1960 "Invalid line[%s]: '%s' - %s",
1961 line, c, strerror(errno));
1962 return false;
1964 if (e[0] != '\0') {
1965 NWRAP_LOG(NWRAP_LOG_ERROR,
1966 "Invalid line[%s]: '%s' - %s",
1967 line, c, strerror(errno));
1968 return false;
1970 c = p;
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
1981 pw->pw_change = 0;
1983 NWRAP_LOG(NWRAP_LOG_TRACE,
1984 "change[%lu]",
1985 (unsigned long)pw->pw_change);
1986 #endif /* HAVE_STRUCT_PASSWD_PW_CHANGE */
1988 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
1989 pw->pw_expire = 0;
1991 NWRAP_LOG(NWRAP_LOG_TRACE,
1992 "expire[%lu]",
1993 (unsigned long)pw->pw_expire);
1994 #endif /* HAVE_STRUCT_PASSWD_PW_EXPIRE */
1996 /* gecos */
1997 p = strchr(c, ':');
1998 if (!p) {
1999 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
2000 return false;
2002 *p = '\0';
2003 p++;
2004 pw->pw_gecos = c;
2005 c = p;
2007 NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
2009 /* dir */
2010 p = strchr(c, ':');
2011 if (!p) {
2012 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
2013 return false;
2015 *p = '\0';
2016 p++;
2017 pw->pw_dir = c;
2018 c = p;
2020 NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
2022 /* shell */
2023 pw->pw_shell = c;
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);
2032 nwrap_pw->num++;
2033 return true;
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);
2042 nwrap_pw->num = 0;
2043 nwrap_pw->idx = 0;
2046 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
2047 char *buf, size_t buflen, struct passwd **dstp)
2049 char *first;
2050 char *last;
2051 off_t ofs;
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) {
2061 return ERANGE;
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;
2079 if (dstp) {
2080 *dstp = dst;
2083 return 0;
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;
2090 struct spwd *sp;
2091 size_t list_size;
2092 char *c;
2093 char *e;
2094 char *p;
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);
2100 if (sp == NULL) {
2101 NWRAP_LOG(NWRAP_LOG_ERROR,
2102 "realloc(%u) failed",
2103 (unsigned)list_size);
2104 return false;
2106 nwrap_sp->list = sp;
2108 sp = &nwrap_sp->list[nwrap_sp->num];
2110 c = line;
2112 /* name */
2113 p = strchr(c, ':');
2114 if (p == NULL) {
2115 NWRAP_LOG(NWRAP_LOG_ERROR,
2116 "name -- Invalid line[%s]: '%s'",
2117 line,
2119 return false;
2121 *p = '\0';
2122 p++;
2123 sp->sp_namp = c;
2124 c = p;
2126 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", sp->sp_namp);
2128 /* pwd */
2129 p = strchr(c, ':');
2130 if (p == NULL) {
2131 NWRAP_LOG(NWRAP_LOG_ERROR,
2132 "pwd -- Invalid line[%s]: '%s'",
2133 line,
2135 return false;
2137 *p = '\0';
2138 p++;
2139 sp->sp_pwdp = c;
2140 c = p;
2142 /* lstchg (long) */
2143 if (c[0] == ':') {
2144 sp->sp_lstchg = -1;
2145 p++;
2146 } else {
2147 p = strchr(c, ':');
2148 if (p == NULL) {
2149 NWRAP_LOG(NWRAP_LOG_ERROR,
2150 "lstchg -- Invalid line[%s]: '%s'",
2151 line,
2153 return false;
2155 *p = '\0';
2156 p++;
2157 sp->sp_lstchg = strtol(c, &e, 10);
2158 if (c == e) {
2159 NWRAP_LOG(NWRAP_LOG_ERROR,
2160 "lstchg -- Invalid line[%s]: '%s' - %s",
2161 line, c, strerror(errno));
2162 return false;
2164 if (e == NULL) {
2165 NWRAP_LOG(NWRAP_LOG_ERROR,
2166 "lstchg -- Invalid line[%s]: '%s' - %s",
2167 line, c, strerror(errno));
2168 return false;
2170 if (e[0] != '\0') {
2171 NWRAP_LOG(NWRAP_LOG_ERROR,
2172 "lstchg -- Invalid line[%s]: '%s' - %s",
2173 line, c, strerror(errno));
2174 return false;
2177 c = p;
2179 /* min (long) */
2180 if (c[0] == ':') {
2181 sp->sp_min = -1;
2182 p++;
2183 } else {
2184 p = strchr(c, ':');
2185 if (p == NULL) {
2186 NWRAP_LOG(NWRAP_LOG_ERROR,
2187 "min -- Invalid line[%s]: '%s'",
2188 line,
2190 return false;
2192 *p = '\0';
2193 p++;
2194 sp->sp_min = strtol(c, &e, 10);
2195 if (c == e) {
2196 NWRAP_LOG(NWRAP_LOG_ERROR,
2197 "min -- Invalid line[%s]: '%s' - %s",
2198 line, c, strerror(errno));
2199 return false;
2201 if (e == NULL) {
2202 NWRAP_LOG(NWRAP_LOG_ERROR,
2203 "min -- Invalid line[%s]: '%s' - %s",
2204 line, c, strerror(errno));
2205 return false;
2207 if (e[0] != '\0') {
2208 NWRAP_LOG(NWRAP_LOG_ERROR,
2209 "min -- Invalid line[%s]: '%s' - %s",
2210 line, c, strerror(errno));
2211 return false;
2214 c = p;
2216 /* max (long) */
2217 if (c[0] == ':') {
2218 sp->sp_max = -1;
2219 p++;
2220 } else {
2221 p = strchr(c, ':');
2222 if (p == NULL) {
2223 NWRAP_LOG(NWRAP_LOG_ERROR,
2224 "max -- Invalid line[%s]: '%s'",
2225 line,
2227 return false;
2229 *p = '\0';
2230 p++;
2231 sp->sp_max = strtol(c, &e, 10);
2232 if (c == e) {
2233 NWRAP_LOG(NWRAP_LOG_ERROR,
2234 "max -- Invalid line[%s]: '%s' - %s",
2235 line, c, strerror(errno));
2236 return false;
2238 if (e == NULL) {
2239 NWRAP_LOG(NWRAP_LOG_ERROR,
2240 "max -- Invalid line[%s]: '%s' - %s",
2241 line, c, strerror(errno));
2242 return false;
2244 if (e[0] != '\0') {
2245 NWRAP_LOG(NWRAP_LOG_ERROR,
2246 "max -- Invalid line[%s]: '%s' - %s",
2247 line, c, strerror(errno));
2248 return false;
2251 c = p;
2253 /* warn (long) */
2254 if (c[0] == ':') {
2255 sp->sp_warn = -1;
2256 p++;
2257 } else {
2258 p = strchr(c, ':');
2259 if (p == NULL) {
2260 NWRAP_LOG(NWRAP_LOG_ERROR,
2261 "warn -- Invalid line[%s]: '%s'",
2262 line,
2264 return false;
2266 *p = '\0';
2267 p++;
2268 sp->sp_warn = strtol(c, &e, 10);
2269 if (c == e) {
2270 NWRAP_LOG(NWRAP_LOG_ERROR,
2271 "warn -- Invalid line[%s]: '%s' - %s",
2272 line, c, strerror(errno));
2273 return false;
2275 if (e == NULL) {
2276 NWRAP_LOG(NWRAP_LOG_ERROR,
2277 "warn -- Invalid line[%s]: '%s' - %s",
2278 line, c, strerror(errno));
2279 return false;
2281 if (e[0] != '\0') {
2282 NWRAP_LOG(NWRAP_LOG_ERROR,
2283 "warn -- Invalid line[%s]: '%s' - %s",
2284 line, c, strerror(errno));
2285 return false;
2288 c = p;
2290 /* inact (long) */
2291 if (c[0] == ':') {
2292 sp->sp_inact = -1;
2293 p++;
2294 } else {
2295 p = strchr(c, ':');
2296 if (p == NULL) {
2297 NWRAP_LOG(NWRAP_LOG_ERROR,
2298 "inact -- Invalid line[%s]: '%s'",
2299 line,
2301 return false;
2303 *p = '\0';
2304 p++;
2305 sp->sp_inact = strtol(c, &e, 10);
2306 if (c == e) {
2307 NWRAP_LOG(NWRAP_LOG_ERROR,
2308 "inact -- Invalid line[%s]: '%s' - %s",
2309 line, c, strerror(errno));
2310 return false;
2312 if (e == NULL) {
2313 NWRAP_LOG(NWRAP_LOG_ERROR,
2314 "inact -- Invalid line[%s]: '%s' - %s",
2315 line, c, strerror(errno));
2316 return false;
2318 if (e[0] != '\0') {
2319 NWRAP_LOG(NWRAP_LOG_ERROR,
2320 "inact -- Invalid line[%s]: '%s' - %s",
2321 line, c, strerror(errno));
2322 return false;
2325 c = p;
2327 /* expire (long) */
2328 if (c[0] == ':') {
2329 sp->sp_expire = -1;
2330 p++;
2331 } else {
2332 p = strchr(c, ':');
2333 if (p == NULL) {
2334 NWRAP_LOG(NWRAP_LOG_ERROR,
2335 "expire -- Invalid line[%s]: '%s'",
2336 line,
2338 return false;
2340 *p = '\0';
2341 p++;
2342 sp->sp_expire = strtol(c, &e, 10);
2343 if (c == e) {
2344 NWRAP_LOG(NWRAP_LOG_ERROR,
2345 "expire -- Invalid line[%s]: '%s' - %s",
2346 line, c, strerror(errno));
2347 return false;
2349 if (e == NULL) {
2350 NWRAP_LOG(NWRAP_LOG_ERROR,
2351 "expire -- Invalid line[%s]: '%s' - %s",
2352 line, c, strerror(errno));
2353 return false;
2355 if (e[0] != '\0') {
2356 NWRAP_LOG(NWRAP_LOG_ERROR,
2357 "expire -- Invalid line[%s]: '%s' - %s",
2358 line, c, strerror(errno));
2359 return false;
2362 c = p;
2364 nwrap_sp->num++;
2365 return true;
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);
2374 nwrap_sp->num = 0;
2375 nwrap_sp->idx = 0;
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;
2385 char *c;
2386 char *p;
2387 char *e;
2388 struct group *gr;
2389 size_t list_size;
2390 unsigned nummem;
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);
2396 if (!gr) {
2397 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
2398 return false;
2400 nwrap_gr->list = gr;
2402 gr = &nwrap_gr->list[nwrap_gr->num];
2404 c = line;
2406 /* name */
2407 p = strchr(c, ':');
2408 if (!p) {
2409 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2410 return false;
2412 *p = '\0';
2413 p++;
2414 gr->gr_name = c;
2415 c = p;
2417 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
2419 /* password */
2420 p = strchr(c, ':');
2421 if (!p) {
2422 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2423 return false;
2425 *p = '\0';
2426 p++;
2427 gr->gr_passwd = c;
2428 c = p;
2430 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
2432 /* gid */
2433 p = strchr(c, ':');
2434 if (!p) {
2435 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2436 return false;
2438 *p = '\0';
2439 p++;
2440 e = NULL;
2441 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
2442 if (c == e) {
2443 NWRAP_LOG(NWRAP_LOG_ERROR,
2444 "Invalid line[%s]: '%s' - %s",
2445 line, c, strerror(errno));
2446 return false;
2448 if (e == NULL) {
2449 NWRAP_LOG(NWRAP_LOG_ERROR,
2450 "Invalid line[%s]: '%s' - %s",
2451 line, c, strerror(errno));
2452 return false;
2454 if (e[0] != '\0') {
2455 NWRAP_LOG(NWRAP_LOG_ERROR,
2456 "Invalid line[%s]: '%s' - %s",
2457 line, c, strerror(errno));
2458 return false;
2460 c = p;
2462 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
2464 /* members */
2465 gr->gr_mem = (char **)malloc(sizeof(char *));
2466 if (!gr->gr_mem) {
2467 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
2468 return false;
2470 gr->gr_mem[0] = NULL;
2472 for(nummem=0; p; nummem++) {
2473 char **m;
2474 size_t m_size;
2475 c = p;
2476 p = strchr(c, ',');
2477 if (p) {
2478 *p = '\0';
2479 p++;
2482 if (strlen(c) == 0) {
2483 break;
2486 m_size = sizeof(char *) * (nummem+2);
2487 m = (char **)realloc(gr->gr_mem, m_size);
2488 if (!m) {
2489 NWRAP_LOG(NWRAP_LOG_ERROR,
2490 "realloc(%zd) failed",
2491 m_size);
2492 return false;
2494 gr->gr_mem = m;
2495 gr->gr_mem[nummem] = c;
2496 gr->gr_mem[nummem+1] = NULL;
2498 NWRAP_LOG(NWRAP_LOG_TRACE,
2499 "member[%u]: '%s'",
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);
2507 nwrap_gr->num++;
2508 return true;
2511 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
2513 int i;
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);
2524 nwrap_gr->num = 0;
2525 nwrap_gr->idx = 0;
2528 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
2529 char *buf, size_t buflen, struct group **dstp)
2531 char *first;
2532 char **lastm;
2533 char *last = NULL;
2534 off_t ofsb;
2535 off_t ofsm;
2536 off_t ofs;
2537 unsigned i;
2539 first = src->gr_name;
2541 lastm = src->gr_mem;
2542 while (*lastm) {
2543 last = *lastm;
2544 lastm++;
2547 if (last == NULL) {
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) {
2556 return ERANGE;
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;
2574 if (dstp) {
2575 *dstp = dst;
2578 return 0;
2581 static struct nwrap_entlist *nwrap_entlist_init(struct nwrap_entdata *ed)
2583 struct nwrap_entlist *el;
2585 if (ed == NULL) {
2586 NWRAP_LOG(NWRAP_LOG_ERROR,
2587 "entry is NULL, can't create list item");
2588 return NULL;
2591 el = (struct nwrap_entlist *)malloc(sizeof(struct nwrap_entlist));
2592 if (el == NULL) {
2593 NWRAP_LOG(NWRAP_LOG_ERROR, "malloc failed");
2594 return NULL;
2597 el->next = NULL;
2598 el->ed = ed;
2600 return el;
2603 static bool nwrap_ed_inventarize_add_new(char *const h_name,
2604 struct nwrap_entdata *const ed)
2606 ENTRY e;
2607 ENTRY *p;
2608 struct nwrap_entlist *el;
2609 bool ok;
2611 if (h_name == NULL) {
2612 NWRAP_LOG(NWRAP_LOG_ERROR, "h_name NULL - can't add");
2613 return false;
2616 el = nwrap_entlist_init(ed);
2617 if (el == NULL) {
2618 return false;
2621 e.key = h_name;
2622 e.data = (void *)el;
2624 p = hsearch(e, ENTER);
2625 if (p == NULL) {
2626 NWRAP_LOG(NWRAP_LOG_ERROR, "Hash table is full!");
2627 return false;
2630 ok = nwrap_vector_add_item(&(nwrap_he_global.lists), (void *)el);
2631 if (!ok) {
2632 NWRAP_LOG(NWRAP_LOG_ERROR,
2633 "Failed to add list entry to vector.");
2634 return false;
2637 return true;
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;
2646 if (el == NULL) {
2647 NWRAP_LOG(NWRAP_LOG_ERROR, "list is NULL, can not add");
2648 return false;
2652 for (cursor = el; cursor->next != NULL; cursor = cursor->next)
2654 if (cursor->ed == ed) {
2655 /* The entry already exists in this list. */
2656 return true;
2660 if (cursor->ed == ed) {
2661 /* The entry already exists in this list. */
2662 return true;
2665 el_new = nwrap_entlist_init(ed);
2666 if (el_new == NULL) {
2667 return false;
2670 cursor->next = el_new;
2671 return true;
2674 static bool nwrap_ed_inventarize(char *const name,
2675 struct nwrap_entdata *const ed)
2677 ENTRY e;
2678 ENTRY *p;
2679 bool ok;
2681 e.key = name;
2682 e.data = NULL;
2684 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
2686 p = hsearch(e, FIND);
2687 if (p == NULL) {
2688 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", name);
2689 ok = nwrap_ed_inventarize_add_new(name, ed);
2690 } else {
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);
2697 return ok;
2700 static bool nwrap_add_hname(struct nwrap_entdata *const ed)
2702 char *const h_name = (char *const)(ed->ht.h_name);
2703 unsigned i;
2704 bool ok;
2706 ok = nwrap_ed_inventarize(h_name, ed);
2707 if (!ok) {
2708 return false;
2711 if (ed->ht.h_aliases == NULL) {
2712 return true;
2715 /* Itemize aliases */
2716 for (i = 0; ed->ht.h_aliases[i] != NULL; ++i) {
2717 char *h_name_alias;
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);
2726 return false;
2730 return true;
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;
2738 char *p;
2739 char *i;
2740 char *n;
2742 char *ip;
2743 bool ok;
2745 struct nwrap_entdata *ed = (struct nwrap_entdata *)
2746 malloc(sizeof(struct nwrap_entdata));
2747 if (ed == NULL) {
2748 NWRAP_LOG(NWRAP_LOG_ERROR,
2749 "Unable to allocate memory for nwrap_entdata");
2750 return false;
2752 ZERO_STRUCTP(ed);
2754 i = line;
2757 * IP
2760 /* Walk to first char */
2761 for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
2762 if (*p == '\0') {
2763 NWRAP_LOG(NWRAP_LOG_ERROR,
2764 "Invalid line[%s]: '%s'",
2765 line, i);
2766 free(ed);
2767 return false;
2771 for (i = p; !isspace((int)*p); p++) {
2772 if (*p == '\0') {
2773 NWRAP_LOG(NWRAP_LOG_ERROR,
2774 "Invalid line[%s]: '%s'",
2775 line, i);
2776 free(ed);
2777 return false;
2781 *p = '\0';
2783 if (inet_pton(AF_INET, i, ed->addr.host_addr)) {
2784 ed->ht.h_addrtype = AF_INET;
2785 ed->ht.h_length = 4;
2786 #ifdef HAVE_IPV6
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;
2790 #endif
2791 } else {
2792 NWRAP_LOG(NWRAP_LOG_ERROR,
2793 "Invalid line[%s]: '%s'",
2794 line, i);
2796 free(ed);
2797 return false;
2799 ip = i;
2801 ok = nwrap_vector_add_item(&(ed->nwrap_addrdata),
2802 (void *const)ed->addr.host_addr);
2803 if (!ok) {
2804 NWRAP_LOG(NWRAP_LOG_ERROR, "Unable to add addrdata to vector");
2805 free(ed);
2806 return false;
2808 ed->ht.h_addr_list = nwrap_vector_head(&ed->nwrap_addrdata);
2810 p++;
2813 * FQDN
2816 /* Walk to first char */
2817 for (n = p; *p != '_' && !isalnum((int) *p); p++) {
2818 if (*p == '\0') {
2819 NWRAP_LOG(NWRAP_LOG_ERROR,
2820 "Invalid line[%s]: '%s'",
2821 line, n);
2823 free(ed);
2824 return false;
2828 for (n = p; !isspace((int)*p); p++) {
2829 if (*p == '\0') {
2830 do_aliases = false;
2831 break;
2835 *p = '\0';
2837 /* Convert to lowercase. This operate on same memory region */
2838 str_tolower(n, n);
2839 ed->ht.h_name = n;
2841 /* glib's getent always dereferences he->h_aliases */
2842 ed->ht.h_aliases = malloc(sizeof(char *));
2843 if (ed->ht.h_aliases == NULL) {
2844 free(ed);
2845 return false;
2847 ed->ht.h_aliases[0] = NULL;
2850 * Aliases
2852 while (do_aliases) {
2853 char **aliases;
2854 char *a;
2856 p++;
2858 /* Walk to first char */
2859 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
2860 if (*p == '\0') {
2861 do_aliases = false;
2862 break;
2865 /* Only trailing spaces are left */
2866 if (!do_aliases) {
2867 break;
2870 for (a = p; !isspace((int)*p); p++) {
2871 if (*p == '\0') {
2872 do_aliases = false;
2873 break;
2877 *p = '\0';
2879 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
2880 if (aliases == NULL) {
2881 free(ed);
2882 return false;
2884 ed->ht.h_aliases = aliases;
2886 str_tolower(a, a);
2887 aliases[aliases_count] = a;
2888 aliases[aliases_count + 1] = NULL;
2890 aliases_count += 1;
2893 ok = nwrap_vector_add_item(&(nwrap_he->entries), (void *const)ed);
2894 if (!ok) {
2895 NWRAP_LOG(NWRAP_LOG_ERROR, "Unable to add entry to vector");
2896 free(ed);
2897 return false;
2900 ed->aliases_count = aliases_count;
2901 /* Inventarize item */
2902 ok = nwrap_add_hname(ed);
2903 if (!ok) {
2904 return false;
2907 ok = nwrap_ed_inventarize(ip, ed);
2908 if (!ok) {
2909 return false;
2912 nwrap_he->num++;
2913 return true;
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;
2922 size_t i;
2923 int rc;
2925 nwrap_vector_foreach (ed, nwrap_he->entries, i)
2927 SAFE_FREE(ed->nwrap_addrdata.items);
2928 SAFE_FREE(ed->ht.h_aliases);
2929 SAFE_FREE(ed);
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;
2939 el_next = el->next;
2940 SAFE_FREE(el);
2941 el = el_next;
2944 SAFE_FREE(nwrap_he->lists.items);
2945 nwrap_he->lists.count = nwrap_he->lists.capacity = 0;
2947 nwrap_he->num = 0;
2948 nwrap_he->idx = 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
2953 * it.
2955 hdestroy();
2956 rc = hcreate(max_hostents);
2957 if (rc == 0) {
2958 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to initialize hash table");
2959 exit(-1);
2964 /* user functions */
2965 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
2966 const char *name)
2968 int i;
2969 bool ok;
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);
2976 if (!ok) {
2977 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
2978 return NULL;
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]",
2988 name,
2989 nwrap_pw_global.list[i].pw_name);
2992 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
2994 errno = ENOENT;
2995 return NULL;
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)
3002 struct passwd *pw;
3004 pw = nwrap_files_getpwnam(b, name);
3005 if (!pw) {
3006 if (errno == 0) {
3007 return ENOENT;
3009 return errno;
3012 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3015 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
3016 uid_t uid)
3018 int i;
3019 bool ok;
3021 (void) b; /* unused */
3023 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3024 if (!ok) {
3025 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3026 return NULL;
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]",
3036 uid,
3037 nwrap_pw_global.list[i].pw_uid);
3040 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
3042 errno = ENOENT;
3043 return NULL;
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)
3050 struct passwd *pw;
3052 pw = nwrap_files_getpwuid(b, uid);
3053 if (!pw) {
3054 if (errno == 0) {
3055 return ENOENT;
3057 return errno;
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)
3073 struct passwd *pw;
3075 (void) b; /* unused */
3077 if (nwrap_pw_global.idx == 0) {
3078 bool ok;
3079 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3080 if (!ok) {
3081 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3082 return NULL;
3086 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
3087 errno = ENOENT;
3088 return NULL;
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);
3097 return pw;
3100 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
3101 struct passwd *pwdst, char *buf,
3102 size_t buflen, struct passwd **pwdstp)
3104 struct passwd *pw;
3106 pw = nwrap_files_getpwent(b);
3107 if (!pw) {
3108 if (errno == 0) {
3109 return ENOENT;
3111 return errno;
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;
3124 /* shadow */
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)
3136 struct spwd *sp;
3138 if (nwrap_sp_global.idx == 0) {
3139 bool ok;
3141 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3142 if (!ok) {
3143 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3144 return NULL;
3148 if (nwrap_sp_global.idx >= nwrap_sp_global.num) {
3149 errno = ENOENT;
3150 return NULL;
3153 sp = &nwrap_sp_global.list[nwrap_sp_global.idx++];
3155 NWRAP_LOG(NWRAP_LOG_DEBUG,
3156 "return user[%s]",
3157 sp->sp_namp);
3159 return sp;
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)
3170 int i;
3171 bool ok;
3173 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
3175 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3176 if (!ok) {
3177 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3178 return NULL;
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]",
3188 name,
3189 nwrap_sp_global.list[i].sp_namp);
3192 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3194 errno = ENOENT;
3195 return NULL;
3197 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3199 /* misc functions */
3200 static int nwrap_files_initgroups(struct nwrap_backend *b,
3201 const char *user,
3202 gid_t group)
3204 struct group *grp;
3205 gid_t *groups;
3206 int size = 1;
3207 int rc;
3209 groups = (gid_t *)malloc(size * sizeof(gid_t));
3210 if (groups == NULL) {
3211 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3212 errno = ENOMEM;
3213 return -1;
3215 groups[0] = group;
3217 nwrap_files_setgrent(b);
3218 while ((grp = nwrap_files_getgrent(b)) != NULL) {
3219 int i = 0;
3221 NWRAP_LOG(NWRAP_LOG_DEBUG,
3222 "Inspecting %s for group membership",
3223 grp->gr_name);
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",
3230 user,
3231 grp->gr_name);
3233 groups = (gid_t *)realloc(groups,
3234 (size + 1) * sizeof(gid_t));
3235 if (groups == NULL) {
3236 NWRAP_LOG(NWRAP_LOG_ERROR,
3237 "Out of memory");
3238 errno = ENOMEM;
3239 return -1;
3242 groups[size] = grp->gr_gid;
3243 size++;
3248 nwrap_files_endgrent(b);
3250 NWRAP_LOG(NWRAP_LOG_DEBUG,
3251 "%s is member of %d groups",
3252 user, size);
3254 /* This really only works if uid_wrapper is loaded */
3255 rc = setgroups(size, groups);
3257 free(groups);
3259 return rc;
3262 /* group functions */
3263 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
3264 const char *name)
3266 int i;
3267 bool ok;
3269 (void) b; /* unused */
3271 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3272 if (!ok) {
3273 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3274 return NULL;
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]",
3284 name,
3285 nwrap_gr_global.list[i].gr_name);
3288 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
3290 errno = ENOENT;
3291 return NULL;
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)
3298 struct group *gr;
3300 gr = nwrap_files_getgrnam(b, name);
3301 if (!gr) {
3302 if (errno == 0) {
3303 return ENOENT;
3305 return errno;
3308 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3311 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
3312 gid_t gid)
3314 int i;
3315 bool ok;
3317 (void) b; /* unused */
3319 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3320 if (!ok) {
3321 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3322 return NULL;
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]",
3332 gid,
3333 nwrap_gr_global.list[i].gr_gid);
3336 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
3338 errno = ENOENT;
3339 return NULL;
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)
3346 struct group *gr;
3348 gr = nwrap_files_getgrgid(b, gid);
3349 if (!gr) {
3350 if (errno == 0) {
3351 return ENOENT;
3353 return errno;
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)
3369 struct group *gr;
3371 (void) b; /* unused */
3373 if (nwrap_gr_global.idx == 0) {
3374 bool ok;
3376 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3377 if (!ok) {
3378 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3379 return NULL;
3383 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
3384 errno = ENOENT;
3385 return NULL;
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);
3394 return gr;
3397 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
3398 struct group *grdst, char *buf,
3399 size_t buflen, struct group **grdstp)
3401 struct group *gr;
3403 gr = nwrap_files_getgrent(b);
3404 if (!gr) {
3405 if (errno == 0) {
3406 return ENOENT;
3408 return errno;
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;
3427 struct hostent *he;
3428 char *h_name_lower;
3429 ENTRY e;
3430 ENTRY *e_p;
3431 char canon_name[DNS_NAME_MAX] = { 0 };
3432 size_t name_len;
3433 bool he_found = false;
3434 bool ok;
3436 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3437 if (!ok) {
3438 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3439 goto no_ent;
3442 name_len = strlen(name);
3443 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
3444 strncpy(canon_name, name, name_len - 1);
3445 name = canon_name;
3448 if (!str_tolower_copy(&h_name_lower, name)) {
3449 NWRAP_LOG(NWRAP_LOG_DEBUG,
3450 "Out of memory while converting to lower case");
3451 goto no_ent;
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;
3457 e.data = NULL;
3458 e_p = hsearch(e, FIND);
3459 if (e_p == NULL) {
3460 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3461 SAFE_FREE(h_name_lower);
3462 goto no_ent;
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");
3471 goto no_ent;
3473 } else {
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)
3482 he = &(el->ed->ht);
3484 /* Filter by address familiy if provided */
3485 if (af != AF_UNSPEC && he->h_addrtype != af) {
3486 continue;
3490 * GLIBC HACK?
3491 * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3493 if (af == AF_UNSPEC && he->h_addrtype != AF_INET) {
3494 continue;
3497 if (!he_found) {
3498 memcpy(result, he, sizeof(struct hostent));
3499 NWRAP_LOG(NWRAP_LOG_DEBUG,
3500 "Name found. Returning record for %s",
3501 he->h_name);
3502 he_found = true;
3504 nwrap_vector_merge(addr_list, &el->ed->nwrap_addrdata);
3505 result->h_addr_list = nwrap_vector_head(addr_list);
3508 if (he_found) {
3509 return 0;
3511 NWRAP_LOG(NWRAP_LOG_DEBUG,
3512 "Name found in database. No records matches type.");
3514 no_ent:
3515 errno = ENOENT;
3516 return -1;
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));
3526 int rc;
3528 if (addr_list == NULL) {
3529 NWRAP_LOG(NWRAP_LOG_ERROR,
3530 "Unable to allocate memory for address list");
3531 errno = ENOENT;
3532 return -1;
3535 ZERO_STRUCTP(addr_list);
3537 rc = nwrap_files_gethostbyname(name, AF_UNSPEC, ret, addr_list);
3538 if (rc == -1) {
3539 *h_errnop = h_errno;
3540 if (addr_list->items != NULL) {
3541 free(addr_list->items);
3543 SAFE_FREE(addr_list);
3544 errno = ENOENT;
3545 return -1;
3548 if (buflen < (addr_list->count * sizeof(void *))) {
3549 SAFE_FREE(addr_list->items);
3550 SAFE_FREE(addr_list);
3551 return ERANGE;
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);
3560 free(addr_list);
3562 ret->h_addr_list = (char **)buf;
3563 *result = ret;
3564 return 0;
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,
3574 ret,
3575 buf,
3576 buflen,
3577 result,
3578 h_errnop);
3581 return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
3583 #endif
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;
3591 struct hostent *he;
3592 struct addrinfo *ai_head = NULL;
3593 struct addrinfo *ai_cur = NULL;
3594 char *h_name_lower;
3595 size_t name_len;
3596 char canon_name[DNS_NAME_MAX] = { 0 };
3597 bool skip_canonname = false;
3598 ENTRY e = {
3599 .key = NULL,
3601 ENTRY *e_p = NULL;
3602 int rc;
3603 bool ok;
3605 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3606 if (!ok) {
3607 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3608 return EAI_SYSTEM;
3611 name_len = strlen(name);
3612 if (name_len < DNS_NAME_MAX && name[name_len - 1] == '.') {
3613 strncpy(canon_name, name, name_len - 1);
3614 name = canon_name;
3617 if (!str_tolower_copy(&h_name_lower, name)) {
3618 NWRAP_LOG(NWRAP_LOG_DEBUG,
3619 "Out of memory while converting to lower case");
3620 return EAI_MEMORY;
3623 NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3624 e.key = h_name_lower;
3625 e.data = NULL;
3626 e_p = hsearch(e, FIND);
3627 if (e_p == NULL) {
3628 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3629 SAFE_FREE(h_name_lower);
3630 errno = ENOENT;
3631 return EAI_NONAME;
3633 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name: %s found.", h_name_lower);
3634 SAFE_FREE(h_name_lower);
3636 rc = EAI_NONAME;
3637 for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3639 int rc2;
3640 struct addrinfo *ai_new = NULL;
3642 he = &(el->ed->ht);
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;
3651 continue;
3654 /* Function allocates memory and returns it in ai. */
3655 rc2 = nwrap_convert_he_ai(he,
3656 port,
3657 hints,
3658 &ai_new,
3659 skip_canonname);
3660 if (rc2 != 0) {
3661 NWRAP_LOG(NWRAP_LOG_ERROR, "Error converting he to ai");
3662 if (ai_head != NULL) {
3663 freeaddrinfo(ai_head);
3665 return rc2;
3667 skip_canonname = true;
3669 if (ai_head == NULL) {
3670 ai_head = ai_new;
3672 if (ai_cur != NULL) {
3673 ai_cur->ai_next = ai_new;
3675 ai_cur = ai_new;
3678 if (ai_head != NULL) {
3679 rc = 0;
3682 *ai = ai_head;
3684 return rc;
3687 static struct hostent *nwrap_files_gethostbyaddr(const void *addr,
3688 socklen_t len, int type)
3690 struct hostent *he;
3691 char ip[NWRAP_INET_ADDRSTRLEN] = {0};
3692 struct nwrap_entdata *ed;
3693 const char *a;
3694 size_t i;
3695 bool ok;
3697 (void) len; /* unused */
3699 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3700 if (!ok) {
3701 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3702 return NULL;
3705 a = inet_ntop(type, addr, ip, sizeof(ip));
3706 if (a == NULL) {
3707 errno = EINVAL;
3708 return NULL;
3711 nwrap_vector_foreach(ed, nwrap_he_global.entries, i)
3713 he = &(ed->ht);
3714 if (he->h_addrtype != type) {
3715 continue;
3718 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
3719 return he;
3723 errno = ENOENT;
3724 return NULL;
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);
3736 *ret = **result;
3737 return 0;
3738 } else {
3739 *h_errnop = h_errno;
3740 return -1;
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,
3751 len,
3752 type,
3753 ret,
3754 buf,
3755 buflen,
3756 result,
3757 h_errnop);
3760 return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
3762 #endif
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)
3772 struct hostent *he;
3774 if (nwrap_he_global.idx == 0) {
3775 bool ok;
3777 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3778 if (!ok) {
3779 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading hosts file");
3780 return NULL;
3784 if (nwrap_he_global.idx >= nwrap_he_global.num) {
3785 errno = ENOENT;
3786 return NULL;
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);
3793 return he;
3796 static void nwrap_files_endhostent(void)
3798 nwrap_he_global.idx = 0;
3802 * module backend
3806 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
3807 const char *name)
3809 static struct passwd pwd;
3810 static char buf[1000];
3811 NSS_STATUS status;
3813 if (!b->fns->_nss_getpwnam_r) {
3814 return NULL;
3817 status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
3818 if (status == NSS_STATUS_NOTFOUND) {
3819 return NULL;
3821 if (status != NSS_STATUS_SUCCESS) {
3822 return NULL;
3825 return &pwd;
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)
3832 int ret;
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);
3843 switch (ret) {
3844 case NSS_STATUS_SUCCESS:
3845 return 0;
3846 case NSS_STATUS_NOTFOUND:
3847 if (errno != 0) {
3848 return errno;
3850 return ENOENT;
3851 case NSS_STATUS_TRYAGAIN:
3852 if (errno != 0) {
3853 return errno;
3855 return ERANGE;
3856 default:
3857 if (errno != 0) {
3858 return errno;
3860 return ret;
3864 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
3865 uid_t uid)
3867 static struct passwd pwd;
3868 static char buf[1000];
3869 NSS_STATUS status;
3871 if (!b->fns->_nss_getpwuid_r) {
3872 return NULL;
3875 status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
3876 if (status == NSS_STATUS_NOTFOUND) {
3877 return NULL;
3879 if (status != NSS_STATUS_SUCCESS) {
3880 return NULL;
3882 return &pwd;
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)
3889 int ret;
3891 (void) pwdstp; /* unused */
3893 if (!b->fns->_nss_getpwuid_r) {
3894 return ENOENT;
3897 ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
3898 switch (ret) {
3899 case NSS_STATUS_SUCCESS:
3900 return 0;
3901 case NSS_STATUS_NOTFOUND:
3902 if (errno != 0) {
3903 return errno;
3905 return ENOENT;
3906 case NSS_STATUS_TRYAGAIN:
3907 if (errno != 0) {
3908 return errno;
3910 return ERANGE;
3911 default:
3912 if (errno != 0) {
3913 return errno;
3915 return ret;
3919 static void nwrap_module_setpwent(struct nwrap_backend *b)
3921 if (!b->fns->_nss_setpwent) {
3922 return;
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];
3932 NSS_STATUS status;
3934 if (!b->fns->_nss_getpwent_r) {
3935 return NULL;
3938 status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
3939 if (status == NSS_STATUS_NOTFOUND) {
3940 return NULL;
3942 if (status != NSS_STATUS_SUCCESS) {
3943 return NULL;
3945 return &pwd;
3948 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
3949 struct passwd *pwdst, char *buf,
3950 size_t buflen, struct passwd **pwdstp)
3952 int ret;
3954 (void) pwdstp; /* unused */
3956 if (!b->fns->_nss_getpwent_r) {
3957 return ENOENT;
3960 ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
3961 switch (ret) {
3962 case NSS_STATUS_SUCCESS:
3963 return 0;
3964 case NSS_STATUS_NOTFOUND:
3965 if (errno != 0) {
3966 return errno;
3968 return ENOENT;
3969 case NSS_STATUS_TRYAGAIN:
3970 if (errno != 0) {
3971 return errno;
3973 return ERANGE;
3974 default:
3975 if (errno != 0) {
3976 return errno;
3978 return ret;
3982 static void nwrap_module_endpwent(struct nwrap_backend *b)
3984 if (!b->fns->_nss_endpwent) {
3985 return;
3988 b->fns->_nss_endpwent();
3991 static int nwrap_module_initgroups(struct nwrap_backend *b,
3992 const char *user, gid_t group)
3994 gid_t *groups;
3995 long int start;
3996 long int size;
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,
4006 const char *name)
4008 static struct group grp;
4009 static char *buf;
4010 static int buflen = 1000;
4011 NSS_STATUS status;
4013 if (!b->fns->_nss_getgrnam_r) {
4014 return NULL;
4017 if (!buf) {
4018 buf = (char *)malloc(buflen);
4020 again:
4021 status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
4022 if (status == NSS_STATUS_TRYAGAIN) {
4023 buflen *= 2;
4024 buf = (char *)realloc(buf, buflen);
4025 if (!buf) {
4026 return NULL;
4028 goto again;
4030 if (status == NSS_STATUS_NOTFOUND) {
4031 SAFE_FREE(buf);
4032 return NULL;
4034 if (status != NSS_STATUS_SUCCESS) {
4035 SAFE_FREE(buf);
4036 return NULL;
4038 return &grp;
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)
4045 int ret;
4047 (void) grdstp; /* unused */
4049 if (!b->fns->_nss_getgrnam_r) {
4050 return ENOENT;
4053 ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
4054 switch (ret) {
4055 case NSS_STATUS_SUCCESS:
4056 return 0;
4057 case NSS_STATUS_NOTFOUND:
4058 if (errno != 0) {
4059 return errno;
4061 return ENOENT;
4062 case NSS_STATUS_TRYAGAIN:
4063 if (errno != 0) {
4064 return errno;
4066 return ERANGE;
4067 default:
4068 if (errno != 0) {
4069 return errno;
4071 return ret;
4075 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
4076 gid_t gid)
4078 static struct group grp;
4079 static char *buf;
4080 static int buflen = 1000;
4081 NSS_STATUS status;
4083 if (!b->fns->_nss_getgrgid_r) {
4084 return NULL;
4087 if (!buf) {
4088 buf = (char *)malloc(buflen);
4091 again:
4092 status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
4093 if (status == NSS_STATUS_TRYAGAIN) {
4094 buflen *= 2;
4095 buf = (char *)realloc(buf, buflen);
4096 if (!buf) {
4097 return NULL;
4099 goto again;
4101 if (status == NSS_STATUS_NOTFOUND) {
4102 SAFE_FREE(buf);
4103 return NULL;
4105 if (status != NSS_STATUS_SUCCESS) {
4106 SAFE_FREE(buf);
4107 return NULL;
4109 return &grp;
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)
4116 int ret;
4118 (void) grdstp; /* unused */
4120 if (!b->fns->_nss_getgrgid_r) {
4121 return ENOENT;
4124 ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
4125 switch (ret) {
4126 case NSS_STATUS_SUCCESS:
4127 return 0;
4128 case NSS_STATUS_NOTFOUND:
4129 if (errno != 0) {
4130 return errno;
4132 return ENOENT;
4133 case NSS_STATUS_TRYAGAIN:
4134 if (errno != 0) {
4135 return errno;
4137 return ERANGE;
4138 default:
4139 if (errno != 0) {
4140 return errno;
4142 return ret;
4146 static void nwrap_module_setgrent(struct nwrap_backend *b)
4148 if (!b->fns->_nss_setgrent) {
4149 return;
4152 b->fns->_nss_setgrent();
4155 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
4157 static struct group grp;
4158 static char *buf;
4159 static int buflen = 1024;
4160 NSS_STATUS status;
4162 if (!b->fns->_nss_getgrent_r) {
4163 return NULL;
4166 if (!buf) {
4167 buf = (char *)malloc(buflen);
4170 again:
4171 status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
4172 if (status == NSS_STATUS_TRYAGAIN) {
4173 buflen *= 2;
4174 buf = (char *)realloc(buf, buflen);
4175 if (!buf) {
4176 return NULL;
4178 goto again;
4180 if (status == NSS_STATUS_NOTFOUND) {
4181 SAFE_FREE(buf);
4182 return NULL;
4184 if (status != NSS_STATUS_SUCCESS) {
4185 SAFE_FREE(buf);
4186 return NULL;
4188 return &grp;
4191 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
4192 struct group *grdst, char *buf,
4193 size_t buflen, struct group **grdstp)
4195 int ret;
4197 (void) grdstp; /* unused */
4199 if (!b->fns->_nss_getgrent_r) {
4200 return ENOENT;
4203 ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
4204 switch (ret) {
4205 case NSS_STATUS_SUCCESS:
4206 return 0;
4207 case NSS_STATUS_NOTFOUND:
4208 if (errno != 0) {
4209 return errno;
4211 return ENOENT;
4212 case NSS_STATUS_TRYAGAIN:
4213 if (errno != 0) {
4214 return errno;
4216 return ERANGE;
4217 default:
4218 if (errno != 0) {
4219 return errno;
4221 return ret;
4225 static void nwrap_module_endgrent(struct nwrap_backend *b)
4227 if (!b->fns->_nss_endgrent) {
4228 return;
4231 b->fns->_nss_endgrent();
4234 /****************************************************************************
4235 * GETPWNAM
4236 ***************************************************************************/
4238 static struct passwd *nwrap_getpwnam(const char *name)
4240 int i;
4241 struct passwd *pwd;
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);
4246 if (pwd) {
4247 return pwd;
4251 return NULL;
4254 struct passwd *getpwnam(const char *name)
4256 if (!nss_wrapper_enabled()) {
4257 return libc_getpwnam(name);
4260 return nwrap_getpwnam(name);
4263 /****************************************************************************
4264 * GETPWNAM_R
4265 ***************************************************************************/
4267 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
4268 char *buf, size_t buflen, struct passwd **pwdstp)
4270 int i,ret;
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) {
4276 continue;
4278 return ret;
4281 return 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);
4299 #endif
4301 /****************************************************************************
4302 * GETPWUID
4303 ***************************************************************************/
4305 static struct passwd *nwrap_getpwuid(uid_t uid)
4307 int i;
4308 struct passwd *pwd;
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);
4313 if (pwd) {
4314 return pwd;
4318 return NULL;
4321 struct passwd *getpwuid(uid_t uid)
4323 if (!nss_wrapper_enabled()) {
4324 return libc_getpwuid(uid);
4327 return nwrap_getpwuid(uid);
4330 /****************************************************************************
4331 * GETPWUID_R
4332 ***************************************************************************/
4334 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
4335 char *buf, size_t buflen, struct passwd **pwdstp)
4337 int i,ret;
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) {
4343 continue;
4345 return ret;
4348 return 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)
4354 #else
4355 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4356 char *buf, size_t buflen, struct passwd **pwdstp)
4357 #endif
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 /****************************************************************************
4367 * SETPWENT
4368 ***************************************************************************/
4370 static void nwrap_setpwent(void)
4372 int i;
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);
4380 void setpwent(void)
4382 if (!nss_wrapper_enabled()) {
4383 libc_setpwent();
4384 return;
4387 nwrap_setpwent();
4390 /****************************************************************************
4391 * GETPWENT
4392 ***************************************************************************/
4394 static struct passwd *nwrap_getpwent(void)
4396 int i;
4397 struct passwd *pwd;
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);
4402 if (pwd) {
4403 return pwd;
4407 return NULL;
4410 struct passwd *getpwent(void)
4412 if (!nss_wrapper_enabled()) {
4413 return libc_getpwent();
4416 return nwrap_getpwent();
4419 /****************************************************************************
4420 * GETPWENT_R
4421 ***************************************************************************/
4423 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
4424 size_t buflen, struct passwd **pwdstp)
4426 int i,ret;
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) {
4432 continue;
4434 return ret;
4437 return ENOENT;
4440 #ifdef HAVE_SOLARIS_GETPWENT_R
4441 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
4443 struct passwd *pwdstp = NULL;
4444 int rc;
4446 if (!nss_wrapper_enabled()) {
4447 return libc_getpwent_r(pwdst, buf, buflen);
4449 rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
4450 if (rc < 0) {
4451 return NULL;
4454 return 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 /****************************************************************************
4469 * ENDPWENT
4470 ***************************************************************************/
4472 static void nwrap_endpwent(void)
4474 int i;
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);
4482 void endpwent(void)
4484 if (!nss_wrapper_enabled()) {
4485 libc_endpwent();
4486 return;
4489 nwrap_endpwent();
4492 /****************************************************************************
4493 * INITGROUPS
4494 ***************************************************************************/
4496 static int nwrap_initgroups(const char *user, gid_t group)
4498 int i;
4500 for (i=0; i < nwrap_main_global->num_backends; i++) {
4501 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4502 int rc;
4504 rc = b->ops->nw_initgroups(b, user, group);
4505 if (rc == 0) {
4506 return 0;
4510 errno = ENOENT;
4511 return -1;
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 /****************************************************************************
4524 * GETGRNAM
4525 ***************************************************************************/
4527 static struct group *nwrap_getgrnam(const char *name)
4529 int i;
4530 struct group *grp;
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);
4535 if (grp) {
4536 return grp;
4540 return NULL;
4543 struct group *getgrnam(const char *name)
4545 if (!nss_wrapper_enabled()) {
4546 return libc_getgrnam(name);
4549 return nwrap_getgrnam(name);
4552 /****************************************************************************
4553 * GETGRNAM_R
4554 ***************************************************************************/
4556 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
4557 char *buf, size_t buflen, struct group **grdstp)
4559 int i, ret;
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) {
4565 continue;
4567 return ret;
4570 return 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,
4584 grp,
4585 buf,
4586 buflen,
4587 pgrp);
4590 return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
4592 #endif /* HAVE_GETGRNAM_R */
4594 /****************************************************************************
4595 * GETGRGID
4596 ***************************************************************************/
4598 static struct group *nwrap_getgrgid(gid_t gid)
4600 int i;
4601 struct group *grp;
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);
4606 if (grp) {
4607 return grp;
4611 return NULL;
4614 struct group *getgrgid(gid_t gid)
4616 if (!nss_wrapper_enabled()) {
4617 return libc_getgrgid(gid);
4620 return nwrap_getgrgid(gid);
4623 /****************************************************************************
4624 * GETGRGID_R
4625 ***************************************************************************/
4627 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
4628 char *buf, size_t buflen, struct group **grdstp)
4630 int i,ret;
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) {
4636 continue;
4638 return ret;
4641 return 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);
4659 #endif
4661 /****************************************************************************
4662 * SETGRENT
4663 ***************************************************************************/
4665 static void nwrap_setgrent(void)
4667 int i;
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
4676 int setgrent(void)
4677 #else
4678 void setgrent(void)
4679 #endif
4681 if (!nss_wrapper_enabled()) {
4682 libc_setgrent();
4683 goto out;
4686 nwrap_setgrent();
4688 out:
4689 #ifdef HAVE_BSD_SETGRENT
4690 return 0;
4691 #else
4692 return;
4693 #endif
4696 /****************************************************************************
4697 * GETGRENT
4698 ***************************************************************************/
4700 static struct group *nwrap_getgrent(void)
4702 int i;
4703 struct group *grp;
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);
4708 if (grp) {
4709 return grp;
4713 return NULL;
4716 struct group *getgrent(void)
4718 if (!nss_wrapper_enabled()) {
4719 return libc_getgrent();
4722 return nwrap_getgrent();
4725 /****************************************************************************
4726 * GETGRENT_R
4727 ***************************************************************************/
4729 static int nwrap_getgrent_r(struct group *grdst, char *buf,
4730 size_t buflen, struct group **grdstp)
4732 int i,ret;
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) {
4738 continue;
4740 return ret;
4743 return ENOENT;
4746 #ifdef HAVE_SOLARIS_GETGRENT_R
4747 struct group *getgrent_r(struct group *src, char *buf, int buflen)
4749 struct group *grdstp = NULL;
4750 int rc;
4752 if (!nss_wrapper_enabled()) {
4753 return libc_getgrent_r(src, buf, buflen);
4756 rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
4757 if (rc < 0) {
4758 return NULL;
4761 return 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 /****************************************************************************
4776 * ENDGRENT
4777 ***************************************************************************/
4779 static void nwrap_endgrent(void)
4781 int i;
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);
4789 void endgrent(void)
4791 if (!nss_wrapper_enabled()) {
4792 libc_endgrent();
4793 return;
4796 nwrap_endgrent();
4799 /****************************************************************************
4800 * GETGROUPLIST
4801 ***************************************************************************/
4803 #ifdef HAVE_GETGROUPLIST
4804 static int nwrap_getgrouplist(const char *user, gid_t group,
4805 gid_t *groups, int *ngroups)
4807 struct group *grp;
4808 gid_t *groups_tmp;
4809 int count = 1;
4811 NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
4813 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
4814 if (!groups_tmp) {
4815 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
4816 errno = ENOMEM;
4817 return -1;
4819 groups_tmp[0] = group;
4821 nwrap_setgrent();
4822 while ((grp = nwrap_getgrent()) != NULL) {
4823 int i = 0;
4825 NWRAP_LOG(NWRAP_LOG_DEBUG,
4826 "Inspecting %s for group membership",
4827 grp->gr_name);
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",
4836 user,
4837 grp->gr_name);
4839 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
4840 if (!groups_tmp) {
4841 NWRAP_LOG(NWRAP_LOG_ERROR,
4842 "Out of memory");
4843 errno = ENOMEM;
4844 return -1;
4846 groups_tmp[count] = grp->gr_gid;
4848 count++;
4853 nwrap_endgrent();
4855 NWRAP_LOG(NWRAP_LOG_DEBUG,
4856 "%s is member of %d groups",
4857 user, *ngroups);
4859 if (*ngroups < count) {
4860 *ngroups = count;
4861 free(groups_tmp);
4862 return -1;
4865 *ngroups = count;
4866 memcpy(groups, groups_tmp, count * sizeof(gid_t));
4867 free(groups_tmp);
4869 return count;
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);
4880 #endif
4882 /**********************************************************
4883 * SHADOW
4884 **********************************************************/
4886 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
4888 #ifdef HAVE_SETSPENT
4889 static void nwrap_setspent(void)
4891 nwrap_files_setspent();
4894 void setspent(void)
4896 if (!nss_wrapper_shadow_enabled()) {
4897 return;
4900 nwrap_setspent();
4903 static struct spwd *nwrap_getspent(void)
4905 return nwrap_files_getspent();
4908 struct spwd *getspent(void)
4910 if (!nss_wrapper_shadow_enabled()) {
4911 return NULL;
4914 return nwrap_getspent();
4917 static void nwrap_endspent(void)
4919 nwrap_files_endspent();
4922 void endspent(void)
4924 if (!nss_wrapper_shadow_enabled()) {
4925 return;
4928 nwrap_endspent();
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()) {
4940 return NULL;
4943 return nwrap_getspnam(name);
4946 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
4948 /**********************************************************
4949 * NETDB
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);
4963 return 0;
4966 nwrap_sethostent(stayopen);
4968 return 0;
4970 #else /* HAVE_SOLARIS_SETHOSTENT */
4971 void sethostent(int stayopen)
4973 if (!nss_wrapper_hosts_enabled()) {
4974 libc_sethostent(stayopen);
4975 return;
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()) {
5003 libc_endhostent();
5004 return 0;
5007 nwrap_endhostent();
5009 return 0;
5011 #else /* HAVE_SOLARIS_ENDHOSTENT */
5012 void endhostent(void)
5014 if (!nss_wrapper_hosts_enabled()) {
5015 libc_endhostent();
5016 return;
5019 nwrap_endhostent();
5021 #endif /* HAVE_SOLARIS_ENDHOSTENT */
5023 #ifdef BSD
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;
5027 #else
5028 static struct hostent user_he;
5029 static struct nwrap_vector user_addrlist;
5030 #endif /* BSD */
5031 static struct hostent *nwrap_gethostbyname(const char *name)
5033 if (nwrap_files_gethostbyname(name, AF_UNSPEC, &user_he, &user_addrlist) == -1) {
5034 return NULL;
5036 return &user_he;
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
5050 #ifdef BSD
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;
5054 #else
5055 static struct hostent user_he2;
5056 static struct nwrap_vector user_addrlist2;
5057 #endif /* BSD */
5058 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
5060 if (nwrap_files_gethostbyname(name, af, &user_he2, &user_addrlist2) == -1) {
5061 return NULL;
5063 return &user_he2;
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);
5074 #endif
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,
5096 .ai_socktype = 0,
5097 .ai_protocol = 0,
5098 .ai_addrlen = 0,
5099 .ai_addr = NULL,
5100 .ai_canonname = NULL,
5101 .ai_next = 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;
5111 socklen_t socklen;
5113 if (he == NULL) {
5114 return EAI_MEMORY;
5117 switch (he->h_addrtype) {
5118 case AF_INET:
5119 socklen = sizeof(struct sockaddr_in);
5120 break;
5121 #ifdef HAVE_IPV6
5122 case AF_INET6:
5123 socklen = sizeof(struct sockaddr_in6);
5124 break;
5125 #endif
5126 default:
5127 return EAI_FAMILY;
5130 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
5131 if (ai == NULL) {
5132 return EAI_MEMORY;
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;
5157 #endif
5158 ai->ai_addr->sa_family = he->h_addrtype;
5160 switch (he->h_addrtype) {
5161 case AF_INET:
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);
5175 break;
5176 #ifdef HAVE_IPV6
5177 case AF_INET6:
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,
5188 he->h_addr_list[0],
5189 he->h_length);
5191 break;
5192 #endif
5195 ai->ai_next = NULL;
5197 if (he->h_name && !skip_canonname) {
5198 ai->ai_canonname = strdup(he->h_name);
5199 if (ai->ai_canonname == NULL) {
5200 freeaddrinfo(ai);
5201 return EAI_MEMORY;
5205 *pai = ai;
5206 return 0;
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;
5216 struct {
5217 int family;
5218 union {
5219 struct in_addr v4;
5220 #ifdef HAVE_IPV6
5221 struct in6_addr v6;
5222 } in;
5223 #endif
5224 } addr = {
5225 .family = AF_UNSPEC,
5227 int rc;
5229 if (node == NULL && service == NULL) {
5230 return EAI_NONAME;
5233 if (hints == NULL) {
5234 hints = &default_hints;
5237 /* EAI_BADFLAGS
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 */
5246 if (node == NULL) {
5247 int ret;
5248 struct addrinfo *p = NULL;
5250 ret = libc_getaddrinfo(node, service, hints, &p);
5252 if (ret == 0) {
5253 *res = p;
5255 return ret;
5258 if (service != NULL && service[0] != '\0') {
5259 const char *proto = NULL;
5260 struct servent *s;
5261 char *end_ptr;
5262 long sl;
5264 errno = 0;
5265 sl = strtol(service, &end_ptr, 10);
5267 if (*end_ptr == '\0') {
5268 port = sl;
5269 goto valid_port;
5270 } else if (hints->ai_flags & AI_NUMERICSERV) {
5271 return EAI_NONAME;
5274 if (hints->ai_protocol != 0) {
5275 struct protoent *pent;
5277 pent = getprotobynumber(hints->ai_protocol);
5278 if (pent != NULL) {
5279 proto = pent->p_name;
5283 s = getservbyname(service, proto);
5284 if (s == NULL) {
5285 return EAI_NONAME;
5287 port = ntohs(s->s_port);
5290 valid_port:
5292 rc = inet_pton(AF_INET, node, &addr.in.v4);
5293 if (rc == 1) {
5294 addr.family = AF_INET;
5296 #ifdef HAVE_IPV6
5297 if (addr.family == AF_UNSPEC) {
5298 rc = inet_pton(AF_INET6, node, &addr.in.v6);
5299 if (rc == 1) {
5300 addr.family = AF_INET6;
5303 #endif
5305 if (addr.family == AF_UNSPEC) {
5306 if (hints->ai_flags & AI_NUMERICHOST) {
5307 return EAI_NONAME;
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);
5316 if (rc != 0) {
5317 int ret;
5318 struct addrinfo *p = NULL;
5320 ret = libc_getaddrinfo(node, service, hints, &p);
5322 if (ret == 0) {
5324 * nwrap_files_getaddrinfo failed, but libc was
5325 * successful -- use the result from libc.
5327 *res = p;
5328 return 0;
5331 return rc;
5335 * If the socktype was not specified, duplicate
5336 * each ai returned, so that we have variants for
5337 * both UDP and TCP.
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) {
5350 freeaddrinfo(ai);
5351 return EAI_MEMORY;
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) */
5381 ai_cur = ai_new;
5385 *res = ai;
5387 return 0;
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,
5404 int flags)
5406 struct hostent *he;
5407 struct servent *service;
5408 const char *proto;
5409 const void *addr;
5410 socklen_t addrlen;
5411 uint16_t port;
5412 sa_family_t type;
5414 if (sa == NULL || salen < sizeof(sa_family_t)) {
5415 return EAI_FAMILY;
5418 if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
5419 return EAI_NONAME;
5422 type = sa->sa_family;
5423 switch (type) {
5424 case AF_INET:
5425 if (salen < sizeof(struct sockaddr_in))
5426 return EAI_FAMILY;
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);
5430 break;
5431 #ifdef HAVE_IPV6
5432 case AF_INET6:
5433 if (salen < sizeof(struct sockaddr_in6))
5434 return EAI_FAMILY;
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);
5438 break;
5439 #endif
5440 default:
5441 return EAI_FAMILY;
5444 if (host != NULL) {
5445 he = NULL;
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))
5449 return EAI_NONAME;
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';
5457 } else {
5458 if (inet_ntop(type, addr, host, hostlen) == NULL)
5459 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
5463 if (serv != NULL) {
5464 service = NULL;
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);
5473 } else {
5474 if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
5475 return EAI_OVERFLOW;
5479 return 0;
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,
5486 int flags)
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,
5491 unsigned int flags)
5492 #else
5493 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5494 char *host, size_t hostlen,
5495 char *serv, size_t servlen,
5496 int flags)
5497 #endif
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;
5512 return -1;
5514 snprintf(name, len, "%s", hostname);
5516 return 0;
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 /****************************
5533 * DESTRUCTOR
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)
5542 int i;
5544 NWRAP_LOCK_ALL;
5545 if (nwrap_main_global != NULL) {
5546 struct nwrap_main *m = nwrap_main_global;
5548 /* libc */
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);
5559 SAFE_FREE(m->libc);
5561 /* backends */
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);
5568 SAFE_FREE(b->fns);
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);
5577 if (c->fd >= 0) {
5578 fclose(c->fp);
5579 c->fd = -1;
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);
5590 if (c->fd >= 0) {
5591 fclose(c->fp);
5592 c->fd = -1;
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);
5604 if (c->fd >= 0) {
5605 fclose(c->fp);
5606 c->fd = -1;
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);
5617 if (c->fd >= 0) {
5618 fclose(c->fp);
5619 c->fd = -1;
5622 nwrap_he_global.num = 0;
5625 free(user_addrlist.items);
5626 #ifdef HAVE_GETHOSTBYNAME2
5627 free(user_addrlist2.items);
5628 #endif
5630 hdestroy();
5631 NWRAP_UNLOCK_ALL;