nwrap: fix nwrap_module_getpwuid_r
[nss_wrapper.git] / src / nss_wrapper.c
1 /*
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2007,      Stefan Metzmacher <metze@samba.org>
5  * Copyright (c) 2009,      Guenther Deschner <gd@samba.org>
6  * Copyright (c) 2014-2015, Michael Adam <obnox@samba.org>
7  * Copyright (c) 2015,      Robin Hack <hack.robin@gmail.com>
8  * Copyright (c) 2013-2018, Andreas Schneider <asn@samba.org>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * 3. Neither the name of the author nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38
39 #include "config.h"
40
41 #include <pthread.h>
42
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/socket.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdarg.h>
49 #include <stdbool.h>
50 #include <stddef.h>
51 #include <stdio.h>
52 #include <stdint.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <ctype.h>
57
58 #include <netinet/in.h>
59
60 #include <search.h>
61 #include <assert.h>
62
63 /*
64  * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h  gives us
65  * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
66  * Solaris
67  */
68 #ifndef _POSIX_PTHREAD_SEMANTICS
69 #define _POSIX_PTHREAD_SEMANTICS
70 #endif
71
72 #include <pwd.h>
73 #include <grp.h>
74 #ifdef HAVE_SHADOW_H
75 #include <shadow.h>
76 #endif /* HAVE_SHADOW_H */
77
78 #include <netdb.h>
79 #include <arpa/inet.h>
80 #include <netinet/in.h>
81
82 #include <dlfcn.h>
83
84 #if defined(HAVE_NSS_H)
85 /* Linux and BSD */
86 #include <nss.h>
87
88 typedef enum nss_status NSS_STATUS;
89 #elif defined(HAVE_NSS_COMMON_H)
90 /* Solaris */
91 #include <nss_common.h>
92 #include <nss_dbdefs.h>
93 #include <nsswitch.h>
94
95 typedef nss_status_t NSS_STATUS;
96
97 # define NSS_STATUS_SUCCESS     NSS_SUCCESS
98 # define NSS_STATUS_NOTFOUND    NSS_NOTFOUND
99 # define NSS_STATUS_UNAVAIL     NSS_UNAVAIL
100 # define NSS_STATUS_TRYAGAIN    NSS_TRYAGAIN
101 #else
102 # error "No nsswitch support detected"
103 #endif
104
105 #ifndef PTR_DIFF
106 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
107 #endif
108
109 #ifndef _PUBLIC_
110 #define _PUBLIC_
111 #endif
112
113 #ifndef EAI_NODATA
114 #define EAI_NODATA EAI_NONAME
115 #endif
116
117 #ifndef EAI_ADDRFAMILY
118 #define EAI_ADDRFAMILY EAI_FAMILY
119 #endif
120
121 #ifndef __STRING
122 #define __STRING(x)    #x
123 #endif
124
125 #ifndef __STRINGSTRING
126 #define __STRINGSTRING(x) __STRING(x)
127 #endif
128
129 #ifndef __LINESTR__
130 #define __LINESTR__ __STRINGSTRING(__LINE__)
131 #endif
132
133 #ifndef __location__
134 #define __location__ __FILE__ ":" __LINESTR__
135 #endif
136
137 #ifndef DNS_NAME_MAX
138 #define DNS_NAME_MAX 255
139 #endif
140
141 /* GCC have printf type attribute check. */
142 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
143 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
144 #else
145 #define PRINTF_ATTRIBUTE(a,b)
146 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
147
148 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
149 #define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
150 #else
151 #define CONSTRUCTOR_ATTRIBUTE
152 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
153
154 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
155 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
156 #else
157 #define DESTRUCTOR_ATTRIBUTE
158 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
159
160 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
161
162 #ifndef SAFE_FREE
163 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
164 #endif
165
166 #ifndef discard_const
167 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
168 #endif
169
170 #ifndef discard_const_p
171 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
172 #endif
173
174 #ifdef HAVE_IPV6
175 #define NWRAP_INET_ADDRSTRLEN INET6_ADDRSTRLEN
176 #else
177 #define NWRAP_INET_ADDRSTRLEN INET_ADDRSTRLEN
178 #endif
179
180 #define NWRAP_LOCK(m) do { \
181         pthread_mutex_lock(&( m ## _mutex)); \
182 } while(0)
183
184 #define NWRAP_UNLOCK(m) do { \
185         pthread_mutex_unlock(&( m ## _mutex)); \
186 } while(0)
187
188
189 static bool nwrap_initialized = false;
190 static pthread_mutex_t nwrap_initialized_mutex = PTHREAD_MUTEX_INITIALIZER;
191
192 /* The mutex or accessing the id */
193 static pthread_mutex_t nwrap_global_mutex = PTHREAD_MUTEX_INITIALIZER;
194 static pthread_mutex_t nwrap_gr_global_mutex = PTHREAD_MUTEX_INITIALIZER;
195 static pthread_mutex_t nwrap_he_global_mutex = PTHREAD_MUTEX_INITIALIZER;
196 static pthread_mutex_t nwrap_pw_global_mutex = PTHREAD_MUTEX_INITIALIZER;
197 static pthread_mutex_t nwrap_sp_global_mutex = PTHREAD_MUTEX_INITIALIZER;
198
199 /* Add new global locks here please */
200 /* Also don't forget to add locks to
201  * nwrap_init() function.
202  */
203 # define NWRAP_LOCK_ALL do { \
204         NWRAP_LOCK(nwrap_initialized); \
205         NWRAP_LOCK(nwrap_global); \
206         NWRAP_LOCK(nwrap_gr_global); \
207         NWRAP_LOCK(nwrap_he_global); \
208         NWRAP_LOCK(nwrap_pw_global); \
209         NWRAP_LOCK(nwrap_sp_global); \
210 } while (0);
211
212 # define NWRAP_UNLOCK_ALL do {\
213         NWRAP_UNLOCK(nwrap_sp_global); \
214         NWRAP_UNLOCK(nwrap_pw_global); \
215         NWRAP_UNLOCK(nwrap_he_global); \
216         NWRAP_UNLOCK(nwrap_gr_global); \
217         NWRAP_UNLOCK(nwrap_global); \
218         NWRAP_UNLOCK(nwrap_initialized); \
219 } while (0);
220
221 static void nwrap_thread_prepare(void)
222 {
223         NWRAP_LOCK_ALL;
224 }
225
226 static void nwrap_thread_parent(void)
227 {
228         NWRAP_UNLOCK_ALL;
229 }
230
231 static void nwrap_thread_child(void)
232 {
233         NWRAP_UNLOCK_ALL;
234 }
235
236 enum nwrap_dbglvl_e {
237         NWRAP_LOG_ERROR = 0,
238         NWRAP_LOG_WARN,
239         NWRAP_LOG_DEBUG,
240         NWRAP_LOG_TRACE
241 };
242
243 #ifdef NDEBUG
244 # define NWRAP_LOG(...)
245 #else
246
247 static void nwrap_log(enum nwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
248 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
249
250 static void nwrap_log(enum nwrap_dbglvl_e dbglvl,
251                       const char *func,
252                       const char *format, ...)
253 {
254         char buffer[1024];
255         va_list va;
256         const char *d;
257         unsigned int lvl = 0;
258         int pid = getpid();
259
260         d = getenv("NSS_WRAPPER_DEBUGLEVEL");
261         if (d != NULL) {
262                 lvl = atoi(d);
263         }
264
265         va_start(va, format);
266         vsnprintf(buffer, sizeof(buffer), format, va);
267         va_end(va);
268
269         if (lvl >= dbglvl) {
270                 switch (dbglvl) {
271                         case NWRAP_LOG_ERROR:
272                                 fprintf(stderr,
273                                         "NWRAP_ERROR(%d) - %s: %s\n",
274                                         pid, func, buffer);
275                                 break;
276                         case NWRAP_LOG_WARN:
277                                 fprintf(stderr,
278                                         "NWRAP_WARN(%d) - %s: %s\n",
279                                         pid, func, buffer);
280                                 break;
281                         case NWRAP_LOG_DEBUG:
282                                 fprintf(stderr,
283                                         "NWRAP_DEBUG(%d) - %s: %s\n",
284                                         pid, func, buffer);
285                                 break;
286                         case NWRAP_LOG_TRACE:
287                                 fprintf(stderr,
288                                         "NWRAP_TRACE(%d) - %s: %s\n",
289                                         pid, func, buffer);
290                                 break;
291                 }
292         }
293 }
294 #endif /* NDEBUG NWRAP_LOG */
295
296 struct nwrap_libc_fns {
297         struct passwd *(*_libc_getpwnam)(const char *name);
298         int (*_libc_getpwnam_r)(const char *name, struct passwd *pwd,
299                        char *buf, size_t buflen, struct passwd **result);
300         struct passwd *(*_libc_getpwuid)(uid_t uid);
301         int (*_libc_getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);
302         void (*_libc_setpwent)(void);
303         struct passwd *(*_libc_getpwent)(void);
304 #ifdef HAVE_GETPWENT_R
305 #  ifdef HAVE_SOLARIS_GETPWENT_R
306         struct passwd *(*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen);
307 #  else /* HAVE_SOLARIS_GETPWENT_R */
308         int (*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp);
309 #  endif /* HAVE_SOLARIS_GETPWENT_R */
310 #endif /* HAVE_GETPWENT_R */
311         void (*_libc_endpwent)(void);
312         int (*_libc_initgroups)(const char *user, gid_t gid);
313         struct group *(*_libc_getgrnam)(const char *name);
314         int (*_libc_getgrnam_r)(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result);
315         struct group *(*_libc_getgrgid)(gid_t gid);
316         int (*_libc_getgrgid_r)(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result);
317         void (*_libc_setgrent)(void);
318         struct group *(*_libc_getgrent)(void);
319 #ifdef HAVE_GETGRENT_R
320 #  ifdef HAVE_SOLARIS_GETGRENT_R
321         struct group *(*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen);
322 #  else /* HAVE_SOLARIS_GETGRENT_R */
323         int (*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen, struct group **result);
324 #  endif /* HAVE_SOLARIS_GETGRENT_R */
325 #endif /* HAVE_GETGRENT_R */
326         void (*_libc_endgrent)(void);
327         int (*_libc_getgrouplist)(const char *user, gid_t group, gid_t *groups, int *ngroups);
328
329         void (*_libc_sethostent)(int stayopen);
330         struct hostent *(*_libc_gethostent)(void);
331         void (*_libc_endhostent)(void);
332
333         struct hostent *(*_libc_gethostbyname)(const char *name);
334 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
335         struct hostent *(*_libc_gethostbyname2)(const char *name, int af);
336 #endif
337         struct hostent *(*_libc_gethostbyaddr)(const void *addr, socklen_t len, int type);
338
339         int (*_libc_getaddrinfo)(const char *node, const char *service,
340                                  const struct addrinfo *hints,
341                                  struct addrinfo **res);
342         int (*_libc_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
343                                  char *host, size_t hostlen,
344                                  char *serv, size_t servlen,
345                                  int flags);
346         int (*_libc_gethostname)(char *name, size_t len);
347 #ifdef HAVE_GETHOSTBYNAME_R
348         int (*_libc_gethostbyname_r)(const char *name,
349                                      struct hostent *ret,
350                                      char *buf, size_t buflen,
351                                      struct hostent **result, int *h_errnop);
352 #endif
353 #ifdef HAVE_GETHOSTBYADDR_R
354         int (*_libc_gethostbyaddr_r)(const void *addr, socklen_t len, int type,
355                                      struct hostent *ret,
356                                      char *buf, size_t buflen,
357                                      struct hostent **result, int *h_errnop);
358 #endif
359 };
360
361 struct nwrap_module_nss_fns {
362         NSS_STATUS (*_nss_getpwnam_r)(const char *name, struct passwd *result, char *buffer,
363                                       size_t buflen, int *errnop);
364         NSS_STATUS (*_nss_getpwuid_r)(uid_t uid, struct passwd *result, char *buffer,
365                                       size_t buflen, int *errnop);
366         NSS_STATUS (*_nss_setpwent)(void);
367         NSS_STATUS (*_nss_getpwent_r)(struct passwd *result, char *buffer,
368                                       size_t buflen, int *errnop);
369         NSS_STATUS (*_nss_endpwent)(void);
370         NSS_STATUS (*_nss_initgroups)(const char *user, gid_t group, long int *start,
371                                       long int *size, gid_t **groups, long int limit, int *errnop);
372         NSS_STATUS (*_nss_getgrnam_r)(const char *name, struct group *result, char *buffer,
373                                       size_t buflen, int *errnop);
374         NSS_STATUS (*_nss_getgrgid_r)(gid_t gid, struct group *result, char *buffer,
375                                       size_t buflen, int *errnop);
376         NSS_STATUS (*_nss_setgrent)(void);
377         NSS_STATUS (*_nss_getgrent_r)(struct group *result, char *buffer,
378                                       size_t buflen, int *errnop);
379         NSS_STATUS (*_nss_endgrent)(void);
380 };
381
382 struct nwrap_backend {
383         const char *name;
384         const char *so_path;
385         void *so_handle;
386         struct nwrap_ops *ops;
387         struct nwrap_module_nss_fns *fns;
388 };
389
390 struct nwrap_ops {
391         struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
392                                        const char *name);
393         int             (*nw_getpwnam_r)(struct nwrap_backend *b,
394                                          const char *name, struct passwd *pwdst,
395                                          char *buf, size_t buflen, struct passwd **pwdstp);
396         struct passwd * (*nw_getpwuid)(struct nwrap_backend *b,
397                                        uid_t uid);
398         int             (*nw_getpwuid_r)(struct nwrap_backend *b,
399                                          uid_t uid, struct passwd *pwdst,
400                                          char *buf, size_t buflen, struct passwd **pwdstp);
401         void            (*nw_setpwent)(struct nwrap_backend *b);
402         struct passwd * (*nw_getpwent)(struct nwrap_backend *b);
403         int             (*nw_getpwent_r)(struct nwrap_backend *b,
404                                          struct passwd *pwdst, char *buf,
405                                          size_t buflen, struct passwd **pwdstp);
406         void            (*nw_endpwent)(struct nwrap_backend *b);
407         int             (*nw_initgroups)(struct nwrap_backend *b,
408                                          const char *user, gid_t group);
409         struct group *  (*nw_getgrnam)(struct nwrap_backend *b,
410                                        const char *name);
411         int             (*nw_getgrnam_r)(struct nwrap_backend *b,
412                                          const char *name, struct group *grdst,
413                                          char *buf, size_t buflen, struct group **grdstp);
414         struct group *  (*nw_getgrgid)(struct nwrap_backend *b,
415                                        gid_t gid);
416         int             (*nw_getgrgid_r)(struct nwrap_backend *b,
417                                          gid_t gid, struct group *grdst,
418                                          char *buf, size_t buflen, struct group **grdstp);
419         void            (*nw_setgrent)(struct nwrap_backend *b);
420         struct group *  (*nw_getgrent)(struct nwrap_backend *b);
421         int             (*nw_getgrent_r)(struct nwrap_backend *b,
422                                          struct group *grdst, char *buf,
423                                          size_t buflen, struct group **grdstp);
424         void            (*nw_endgrent)(struct nwrap_backend *b);
425 };
426
427 /* Public prototypes */
428
429 bool nss_wrapper_enabled(void);
430 bool nss_wrapper_shadow_enabled(void);
431 bool nss_wrapper_hosts_enabled(void);
432
433 /* prototypes for files backend */
434
435
436 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
437                                            const char *name);
438 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
439                                   const char *name, struct passwd *pwdst,
440                                   char *buf, size_t buflen, struct passwd **pwdstp);
441 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
442                                            uid_t uid);
443 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
444                                   uid_t uid, struct passwd *pwdst,
445                                   char *buf, size_t buflen, struct passwd **pwdstp);
446 static void nwrap_files_setpwent(struct nwrap_backend *b);
447 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b);
448 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
449                                   struct passwd *pwdst, char *buf,
450                                   size_t buflen, struct passwd **pwdstp);
451 static void nwrap_files_endpwent(struct nwrap_backend *b);
452 static int nwrap_files_initgroups(struct nwrap_backend *b,
453                                   const char *user, gid_t group);
454 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
455                                           const char *name);
456 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
457                                   const char *name, struct group *grdst,
458                                   char *buf, size_t buflen, struct group **grdstp);
459 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
460                                           gid_t gid);
461 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
462                                   gid_t gid, struct group *grdst,
463                                   char *buf, size_t buflen, struct group **grdstp);
464 static void nwrap_files_setgrent(struct nwrap_backend *b);
465 static struct group *nwrap_files_getgrent(struct nwrap_backend *b);
466 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
467                                   struct group *grdst, char *buf,
468                                   size_t buflen, struct group **grdstp);
469 static void nwrap_files_endgrent(struct nwrap_backend *b);
470
471 /* prototypes for module backend */
472
473 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b);
474 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
475                                    struct passwd *pwdst, char *buf,
476                                    size_t buflen, struct passwd **pwdstp);
477 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
478                                             const char *name);
479 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
480                                    const char *name, struct passwd *pwdst,
481                                    char *buf, size_t buflen, struct passwd **pwdstp);
482 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
483                                             uid_t uid);
484 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
485                                    uid_t uid, struct passwd *pwdst,
486                                    char *buf, size_t buflen, struct passwd **pwdstp);
487 static void nwrap_module_setpwent(struct nwrap_backend *b);
488 static void nwrap_module_endpwent(struct nwrap_backend *b);
489 static struct group *nwrap_module_getgrent(struct nwrap_backend *b);
490 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
491                                    struct group *grdst, char *buf,
492                                    size_t buflen, struct group **grdstp);
493 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
494                                            const char *name);
495 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
496                                    const char *name, struct group *grdst,
497                                    char *buf, size_t buflen, struct group **grdstp);
498 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
499                                            gid_t gid);
500 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
501                                    gid_t gid, struct group *grdst,
502                                    char *buf, size_t buflen, struct group **grdstp);
503 static void nwrap_module_setgrent(struct nwrap_backend *b);
504 static void nwrap_module_endgrent(struct nwrap_backend *b);
505 static int nwrap_module_initgroups(struct nwrap_backend *b,
506                                    const char *user, gid_t group);
507
508 struct nwrap_ops nwrap_files_ops = {
509         .nw_getpwnam    = nwrap_files_getpwnam,
510         .nw_getpwnam_r  = nwrap_files_getpwnam_r,
511         .nw_getpwuid    = nwrap_files_getpwuid,
512         .nw_getpwuid_r  = nwrap_files_getpwuid_r,
513         .nw_setpwent    = nwrap_files_setpwent,
514         .nw_getpwent    = nwrap_files_getpwent,
515         .nw_getpwent_r  = nwrap_files_getpwent_r,
516         .nw_endpwent    = nwrap_files_endpwent,
517         .nw_initgroups  = nwrap_files_initgroups,
518         .nw_getgrnam    = nwrap_files_getgrnam,
519         .nw_getgrnam_r  = nwrap_files_getgrnam_r,
520         .nw_getgrgid    = nwrap_files_getgrgid,
521         .nw_getgrgid_r  = nwrap_files_getgrgid_r,
522         .nw_setgrent    = nwrap_files_setgrent,
523         .nw_getgrent    = nwrap_files_getgrent,
524         .nw_getgrent_r  = nwrap_files_getgrent_r,
525         .nw_endgrent    = nwrap_files_endgrent,
526 };
527
528 struct nwrap_ops nwrap_module_ops = {
529         .nw_getpwnam    = nwrap_module_getpwnam,
530         .nw_getpwnam_r  = nwrap_module_getpwnam_r,
531         .nw_getpwuid    = nwrap_module_getpwuid,
532         .nw_getpwuid_r  = nwrap_module_getpwuid_r,
533         .nw_setpwent    = nwrap_module_setpwent,
534         .nw_getpwent    = nwrap_module_getpwent,
535         .nw_getpwent_r  = nwrap_module_getpwent_r,
536         .nw_endpwent    = nwrap_module_endpwent,
537         .nw_initgroups  = nwrap_module_initgroups,
538         .nw_getgrnam    = nwrap_module_getgrnam,
539         .nw_getgrnam_r  = nwrap_module_getgrnam_r,
540         .nw_getgrgid    = nwrap_module_getgrgid,
541         .nw_getgrgid_r  = nwrap_module_getgrgid_r,
542         .nw_setgrent    = nwrap_module_setgrent,
543         .nw_getgrent    = nwrap_module_getgrent,
544         .nw_getgrent_r  = nwrap_module_getgrent_r,
545         .nw_endgrent    = nwrap_module_endgrent,
546 };
547
548 struct nwrap_libc {
549         void *handle;
550         void *nsl_handle;
551         void *sock_handle;
552         struct nwrap_libc_fns *fns;
553 };
554
555 struct nwrap_main {
556         int num_backends;
557         struct nwrap_backend *backends;
558         struct nwrap_libc *libc;
559 };
560
561 static struct nwrap_main *nwrap_main_global;
562 static struct nwrap_main __nwrap_main_global;
563
564 /*
565  * PROTOTYPES
566  */
567 static int nwrap_convert_he_ai(const struct hostent *he,
568                                unsigned short port,
569                                const struct addrinfo *hints,
570                                struct addrinfo **pai,
571                                bool skip_canonname);
572
573 /*
574  * VECTORS
575  */
576
577 #define DEFAULT_VECTOR_CAPACITY 16
578
579 struct nwrap_vector {
580         void **items;
581         size_t count;
582         size_t capacity;
583 };
584
585 /* Macro returns pointer to first element of vector->items array.
586  *
587  * nwrap_vector is used as a memory backend which take care of
588  * memory allocations and other stuff like memory growing.
589  * nwrap_vectors should not be considered as some abstract structures.
590  * On this level, vectors are more handy than direct realloc/malloc
591  * calls.
592  *
593  * nwrap_vector->items is array inside nwrap_vector which can be
594  * directly pointed by libc structure assembled by cwrap itself.
595  *
596  * EXAMPLE:
597  *
598  * 1) struct hostent contains char **h_addr_list element.
599  * 2) nwrap_vector holds array of pointers to addresses.
600  *    It's easier to use vector to store results of
601  *    file parsing etc.
602  *
603  * Now, pretend that cwrap assembled struct hostent and
604  * we need to set h_addr_list to point to nwrap_vector.
605  * Idea behind is to shield users from internal nwrap_vector
606  * implementation.
607  * (Yes, not fully - array terminated by NULL is needed because
608  * it's result expected by libc function caller.)
609  *
610  *
611  * CODE EXAMPLE:
612  *
613  * struct hostent he;
614  * struct nwrap_vector *vector = malloc(sizeof(struct nwrap_vector));
615  * ... don't care about failed allocation now ...
616  *
617  * ... fill nwrap vector ...
618  *
619  * struct hostent he;
620  * he.h_addr_list = nwrap_vector_head(vector);
621  *
622  */
623 #define nwrap_vector_head(vect) ((void *)((vect)->items))
624
625 #define nwrap_vector_foreach(item, vect, iter) \
626         for (iter = 0, (item) = (vect).items == NULL ? NULL : (vect).items[0]; \
627              item != NULL; \
628              (item) = (vect).items[++iter])
629
630 #define nwrap_vector_is_initialized(vector) ((vector)->items != NULL)
631
632 static inline bool nwrap_vector_init(struct nwrap_vector *const vector)
633 {
634         if (vector == NULL) {
635                 return false;
636         }
637
638         /* count is initialized by ZERO_STRUCTP */
639         ZERO_STRUCTP(vector);
640         vector->items = malloc(sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
641         if (vector->items == NULL) {
642                 return false;
643         }
644         vector->capacity = DEFAULT_VECTOR_CAPACITY;
645         memset(vector->items, '\0', sizeof(void *) * (DEFAULT_VECTOR_CAPACITY + 1));
646
647         return true;
648 }
649
650 static bool nwrap_vector_add_item(struct nwrap_vector *vector, void *const item)
651 {
652         assert (vector != NULL);
653
654         if (vector->items == NULL) {
655                 nwrap_vector_init(vector);
656         }
657
658         if (vector->count == vector->capacity) {
659                 /* Items array _MUST_ be NULL terminated because it's passed
660                  * as result to caller which expect NULL terminated array from libc.
661                  */
662                 void **items = realloc(vector->items, sizeof(void *) * ((vector->capacity * 2) + 1));
663                 if (items == NULL) {
664                         return false;
665                 }
666                 vector->items = items;
667
668                 /* Don't count ending NULL to capacity */
669                 vector->capacity *= 2;
670         }
671
672         vector->items[vector->count] = item;
673
674         vector->count += 1;
675         vector->items[vector->count] = NULL;
676
677         return true;
678 }
679
680 static bool nwrap_vector_merge(struct nwrap_vector *dst,
681                                struct nwrap_vector *src)
682 {
683         void **dst_items = NULL;
684         size_t count;
685
686         if (src->count == 0) {
687                 return true;
688         }
689
690         count = dst->count + src->count;
691
692         /* We don't need reallocation if we have enough capacity. */
693         if (src->count > (dst->capacity - dst->count)) {
694                 dst_items = (void **)realloc(dst->items, (count + 1) * sizeof(void *));
695                 if (dst_items == NULL) {
696                         return false;
697                 }
698                 dst->items = dst_items;
699                 dst->capacity = count;
700         }
701
702         memcpy((void *)(((long *)dst->items) + dst->count),
703                src->items,
704                src->count * sizeof(void *));
705         dst->count = count;
706
707         return true;
708 }
709
710 struct nwrap_cache {
711         const char *path;
712         int fd;
713         FILE *fp;
714         struct stat st;
715         void *private_data;
716
717         struct nwrap_vector lines;
718
719         bool (*parse_line)(struct nwrap_cache *, char *line);
720         void (*unload)(struct nwrap_cache *);
721 };
722
723 /* passwd */
724 struct nwrap_pw {
725         struct nwrap_cache *cache;
726
727         struct passwd *list;
728         int num;
729         int idx;
730 };
731
732 struct nwrap_cache __nwrap_cache_pw;
733 struct nwrap_pw nwrap_pw_global;
734
735 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
736 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
737
738 /* shadow */
739 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
740 struct nwrap_sp {
741         struct nwrap_cache *cache;
742
743         struct spwd *list;
744         int num;
745         int idx;
746 };
747
748 struct nwrap_cache __nwrap_cache_sp;
749 struct nwrap_sp nwrap_sp_global;
750
751 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line);
752 static void nwrap_sp_unload(struct nwrap_cache *nwrap);
753 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
754
755 /* group */
756 struct nwrap_gr {
757         struct nwrap_cache *cache;
758
759         struct group *list;
760         int num;
761         int idx;
762 };
763
764 struct nwrap_cache __nwrap_cache_gr;
765 struct nwrap_gr nwrap_gr_global;
766
767 /* hosts */
768 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line);
769 static void nwrap_he_unload(struct nwrap_cache *nwrap);
770
771 struct nwrap_addrdata {
772         unsigned char host_addr[16]; /* IPv4 or IPv6 address */
773 };
774
775 static size_t max_hostents = 100;
776
777 struct nwrap_entdata {
778         struct nwrap_addrdata addr;
779         struct hostent ht;
780
781         struct nwrap_vector nwrap_addrdata;
782
783         ssize_t aliases_count;
784 };
785
786 struct nwrap_entlist {
787         struct nwrap_entlist *next;
788         struct nwrap_entdata *ed;
789 };
790
791 struct nwrap_he {
792         struct nwrap_cache *cache;
793
794         struct nwrap_vector entries;
795         struct nwrap_vector lists;
796
797         int num;
798         int idx;
799 };
800
801 static struct nwrap_cache __nwrap_cache_he;
802 static struct nwrap_he nwrap_he_global;
803
804
805 /*********************************************************
806  * NWRAP PROTOTYPES
807  *********************************************************/
808
809 static void nwrap_init(void);
810 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
811 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
812 void nwrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
813 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
814
815 /*********************************************************
816  * NWRAP LIBC LOADER FUNCTIONS
817  *********************************************************/
818
819 enum nwrap_lib {
820     NWRAP_LIBC,
821     NWRAP_LIBNSL,
822     NWRAP_LIBSOCKET,
823 };
824
825 #ifndef NDEBUG
826 static const char *nwrap_str_lib(enum nwrap_lib lib)
827 {
828         switch (lib) {
829         case NWRAP_LIBC:
830                 return "libc";
831         case NWRAP_LIBNSL:
832                 return "libnsl";
833         case NWRAP_LIBSOCKET:
834                 return "libsocket";
835         }
836
837         /* Compiler would warn us about unhandled enum value if we get here */
838         return "unknown";
839 }
840 #endif
841
842 static void *nwrap_load_lib_handle(enum nwrap_lib lib)
843 {
844         int flags = RTLD_LAZY;
845         void *handle = NULL;
846         int i;
847
848 #ifdef RTLD_DEEPBIND
849         flags |= RTLD_DEEPBIND;
850 #endif
851
852         switch (lib) {
853         case NWRAP_LIBNSL:
854 #ifdef HAVE_LIBNSL
855                 handle = nwrap_main_global->libc->nsl_handle;
856                 if (handle == NULL) {
857                         for (i = 10; i >= 0; i--) {
858                                 char soname[256] = {0};
859
860                                 snprintf(soname, sizeof(soname), "libnsl.so.%d", i);
861                                 handle = dlopen(soname, flags);
862                                 if (handle != NULL) {
863                                         break;
864                                 }
865                         }
866
867                         nwrap_main_global->libc->nsl_handle = handle;
868                 }
869                 break;
870 #endif
871                 /* FALL TROUGH */
872         case NWRAP_LIBSOCKET:
873 #ifdef HAVE_LIBSOCKET
874                 handle = nwrap_main_global->libc->sock_handle;
875                 if (handle == NULL) {
876                         for (i = 10; i >= 0; i--) {
877                                 char soname[256] = {0};
878
879                                 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
880                                 handle = dlopen(soname, flags);
881                                 if (handle != NULL) {
882                                         break;
883                                 }
884                         }
885
886                         nwrap_main_global->libc->sock_handle = handle;
887                 }
888                 break;
889 #endif
890                 /* FALL TROUGH */
891         case NWRAP_LIBC:
892                 handle = nwrap_main_global->libc->handle;
893                 if (handle == NULL) {
894                         for (i = 10; i >= 0; i--) {
895                                 char soname[256] = {0};
896
897                                 snprintf(soname, sizeof(soname), "libc.so.%d", i);
898                                 handle = dlopen(soname, flags);
899                                 if (handle != NULL) {
900                                         break;
901                                 }
902                         }
903
904                         nwrap_main_global->libc->handle = handle;
905                 }
906                 break;
907         }
908
909         if (handle == NULL) {
910 #ifdef RTLD_NEXT
911                 handle = nwrap_main_global->libc->handle
912                        = nwrap_main_global->libc->sock_handle
913                        = nwrap_main_global->libc->nsl_handle
914                        = RTLD_NEXT;
915 #else
916                 NWRAP_LOG(NWRAP_LOG_ERROR,
917                           "Failed to dlopen library: %s\n",
918                           dlerror());
919                 exit(-1);
920 #endif
921         }
922
923         return handle;
924 }
925
926 static void *_nwrap_load_lib_function(enum nwrap_lib lib, const char *fn_name)
927 {
928         void *handle;
929         void *func;
930
931         nwrap_init();
932
933         handle = nwrap_load_lib_handle(lib);
934
935         func = dlsym(handle, fn_name);
936         if (func == NULL) {
937                 NWRAP_LOG(NWRAP_LOG_ERROR,
938                                 "Failed to find %s: %s\n",
939                                 fn_name, dlerror());
940                 exit(-1);
941         }
942
943         NWRAP_LOG(NWRAP_LOG_TRACE,
944                         "Loaded %s from %s",
945                         fn_name, nwrap_str_lib(lib));
946         return func;
947 }
948
949 #define nwrap_load_lib_function(lib, fn_name) \
950         if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
951                 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
952                         _nwrap_load_lib_function(lib, #fn_name); \
953         }
954
955 /* INTERNAL HELPER FUNCTIONS */
956 static void nwrap_lines_unload(struct nwrap_cache *const nwrap)
957 {
958         size_t p;
959         void *item;
960         nwrap_vector_foreach(item, nwrap->lines, p) {
961                 /* Maybe some vectors were merged ... */
962                 SAFE_FREE(item);
963         }
964         SAFE_FREE(nwrap->lines.items);
965         ZERO_STRUCTP(&nwrap->lines);
966 }
967
968 /*
969  * IMPORTANT
970  *
971  * Functions expeciall from libc need to be loaded individually, you can't load
972  * all at once or gdb will segfault at startup. The same applies to valgrind and
973  * has probably something todo with with the linker.
974  * So we need load each function at the point it is called the first time.
975  */
976 static struct passwd *libc_getpwnam(const char *name)
977 {
978         nwrap_load_lib_function(NWRAP_LIBC, getpwnam);
979
980         return nwrap_main_global->libc->fns->_libc_getpwnam(name);
981 }
982
983 #ifdef HAVE_GETPWNAM_R
984 static int libc_getpwnam_r(const char *name,
985                            struct passwd *pwd,
986                            char *buf,
987                            size_t buflen,
988                            struct passwd **result)
989 {
990 #ifdef HAVE___POSIX_GETPWNAM_R
991         if (nwrap_main_global->libc->fns->_libc_getpwnam_r == NULL) {
992                 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwnam_r) =
993                         _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwnam_r");
994         }
995 #else
996         nwrap_load_lib_function(NWRAP_LIBC, getpwnam_r);
997 #endif
998
999         return nwrap_main_global->libc->fns->_libc_getpwnam_r(name,
1000                                                               pwd,
1001                                                               buf,
1002                                                               buflen,
1003                                                               result);
1004 }
1005 #endif
1006
1007 static struct passwd *libc_getpwuid(uid_t uid)
1008 {
1009         nwrap_load_lib_function(NWRAP_LIBC, getpwuid);
1010
1011         return nwrap_main_global->libc->fns->_libc_getpwuid(uid);
1012 }
1013
1014 #ifdef HAVE_GETPWUID_R
1015 static int libc_getpwuid_r(uid_t uid,
1016                            struct passwd *pwd,
1017                            char *buf,
1018                            size_t buflen,
1019                            struct passwd **result)
1020 {
1021 #ifdef HAVE___POSIX_GETPWUID_R
1022         if (nwrap_main_global->libc->fns->_libc_getpwuid_r == NULL) {
1023                 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwuid_r) =
1024                         _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwuid_r");
1025         }
1026 #else
1027         nwrap_load_lib_function(NWRAP_LIBC, getpwuid_r);
1028 #endif
1029
1030         return nwrap_main_global->libc->fns->_libc_getpwuid_r(uid,
1031                                                               pwd,
1032                                                               buf,
1033                                                               buflen,
1034                                                               result);
1035 }
1036 #endif
1037
1038 static inline void str_tolower(char *dst, char *src)
1039 {
1040         register char *src_tmp = src;
1041         register char *dst_tmp = dst;
1042
1043         while (*src_tmp != '\0') {
1044                 *dst_tmp = tolower(*src_tmp);
1045                 ++src_tmp;
1046                 ++dst_tmp;
1047         }
1048 }
1049
1050 static bool str_tolower_copy(char **dst_name, const char *const src_name)
1051 {
1052         char *h_name_lower;
1053
1054         if ((dst_name == NULL) || (src_name == NULL)) {
1055                 return false;
1056         }
1057
1058         h_name_lower = strdup(src_name);
1059         if (h_name_lower == NULL) {
1060                 NWRAP_LOG(NWRAP_LOG_DEBUG, "Out of memory while strdup");
1061                 return false;
1062         }
1063
1064         str_tolower(h_name_lower, h_name_lower);
1065         *dst_name = h_name_lower;
1066         return true;
1067 }
1068
1069 static void libc_setpwent(void)
1070 {
1071         nwrap_load_lib_function(NWRAP_LIBC, setpwent);
1072
1073         nwrap_main_global->libc->fns->_libc_setpwent();
1074 }
1075
1076 static struct passwd *libc_getpwent(void)
1077 {
1078         nwrap_load_lib_function(NWRAP_LIBC, getpwent);
1079
1080         return nwrap_main_global->libc->fns->_libc_getpwent();
1081 }
1082
1083 #ifdef HAVE_GETPWENT_R
1084 #  ifdef HAVE_SOLARIS_GETPWENT_R
1085 static struct passwd *libc_getpwent_r(struct passwd *pwdst,
1086                                       char *buf,
1087                                       int buflen)
1088 {
1089         nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1090
1091         return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1092                                                               buf,
1093                                                               buflen);
1094 }
1095 #  else /* HAVE_SOLARIS_GETPWENT_R */
1096 static int libc_getpwent_r(struct passwd *pwdst,
1097                            char *buf,
1098                            size_t buflen,
1099                            struct passwd **pwdstp)
1100 {
1101         nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
1102
1103         return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
1104                                                               buf,
1105                                                               buflen,
1106                                                               pwdstp);
1107 }
1108 #  endif /* HAVE_SOLARIS_GETPWENT_R */
1109 #endif /* HAVE_GETPWENT_R */
1110
1111 static void libc_endpwent(void)
1112 {
1113         nwrap_load_lib_function(NWRAP_LIBC, endpwent);
1114
1115         nwrap_main_global->libc->fns->_libc_endpwent();
1116 }
1117
1118 static int libc_initgroups(const char *user, gid_t gid)
1119 {
1120         nwrap_load_lib_function(NWRAP_LIBC, initgroups);
1121
1122         return nwrap_main_global->libc->fns->_libc_initgroups(user, gid);
1123 }
1124
1125 static struct group *libc_getgrnam(const char *name)
1126 {
1127         nwrap_load_lib_function(NWRAP_LIBC, getgrnam);
1128
1129         return nwrap_main_global->libc->fns->_libc_getgrnam(name);
1130 }
1131
1132 #ifdef HAVE_GETGRNAM_R
1133 static int libc_getgrnam_r(const char *name,
1134                            struct group *grp,
1135                            char *buf,
1136                            size_t buflen,
1137                            struct group **result)
1138 {
1139 #ifdef HAVE___POSIX_GETGRNAM_R
1140         if (nwrap_main_global->libc->fns->_libc_getgrnam_r == NULL) {
1141                 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrnam_r) =
1142                         _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrnam_r");
1143         }
1144 #else
1145         nwrap_load_lib_function(NWRAP_LIBC, getgrnam_r);
1146 #endif
1147
1148         return nwrap_main_global->libc->fns->_libc_getgrnam_r(name,
1149                                                               grp,
1150                                                               buf,
1151                                                               buflen,
1152                                                               result);
1153 }
1154 #endif
1155
1156 static struct group *libc_getgrgid(gid_t gid)
1157 {
1158         nwrap_load_lib_function(NWRAP_LIBC, getgrgid);
1159
1160         return nwrap_main_global->libc->fns->_libc_getgrgid(gid);
1161 }
1162
1163 #ifdef HAVE_GETGRGID_R
1164 static int libc_getgrgid_r(gid_t gid,
1165                            struct group *grp,
1166                            char *buf,
1167                            size_t buflen,
1168                            struct group **result)
1169 {
1170 #ifdef HAVE___POSIX_GETGRGID_R
1171         if (nwrap_main_global->libc->fns->_libc_getgrgid_r == NULL) {
1172                 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrgid_r) =
1173                         _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrgid_r");
1174         }
1175 #else
1176         nwrap_load_lib_function(NWRAP_LIBC, getgrgid_r);
1177 #endif
1178
1179         return nwrap_main_global->libc->fns->_libc_getgrgid_r(gid,
1180                                                               grp,
1181                                                               buf,
1182                                                               buflen,
1183                                                               result);
1184 }
1185 #endif
1186
1187 static void libc_setgrent(void)
1188 {
1189         nwrap_load_lib_function(NWRAP_LIBC, setgrent);
1190
1191         nwrap_main_global->libc->fns->_libc_setgrent();
1192 }
1193
1194 static struct group *libc_getgrent(void)
1195 {
1196         nwrap_load_lib_function(NWRAP_LIBC, getgrent);
1197
1198         return nwrap_main_global->libc->fns->_libc_getgrent();
1199 }
1200
1201 #ifdef HAVE_GETGRENT_R
1202 #  ifdef HAVE_SOLARIS_GETGRENT_R
1203 static struct group *libc_getgrent_r(struct group *group,
1204                                      char *buf,
1205                                      size_t buflen)
1206 {
1207         nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1208
1209         return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1210                                                               buf,
1211                                                               buflen);
1212 }
1213 #  else /* HAVE_SOLARIS_GETGRENT_R */
1214 static int libc_getgrent_r(struct group *group,
1215                            char *buf,
1216                            size_t buflen,
1217                            struct group **result)
1218 {
1219         nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
1220
1221         return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
1222                                                               buf,
1223                                                               buflen,
1224                                                               result);
1225 }
1226 #  endif /* HAVE_SOLARIS_GETGRENT_R */
1227 #endif /* HAVE_GETGRENT_R */
1228
1229 static void libc_endgrent(void)
1230 {
1231         nwrap_load_lib_function(NWRAP_LIBC, endgrent);
1232
1233         nwrap_main_global->libc->fns->_libc_endgrent();
1234 }
1235
1236 #ifdef HAVE_GETGROUPLIST
1237 static int libc_getgrouplist(const char *user,
1238                              gid_t group,
1239                              gid_t *groups,
1240                              int *ngroups)
1241 {
1242         nwrap_load_lib_function(NWRAP_LIBC, getgrouplist);
1243
1244         return nwrap_main_global->libc->fns->_libc_getgrouplist(user,
1245                                                                 group,
1246                                                                 groups,
1247                                                                 ngroups);
1248 }
1249 #endif
1250
1251 static void libc_sethostent(int stayopen)
1252 {
1253         nwrap_load_lib_function(NWRAP_LIBNSL, sethostent);
1254
1255         nwrap_main_global->libc->fns->_libc_sethostent(stayopen);
1256 }
1257
1258 static struct hostent *libc_gethostent(void)
1259 {
1260         nwrap_load_lib_function(NWRAP_LIBNSL, gethostent);
1261
1262         return nwrap_main_global->libc->fns->_libc_gethostent();
1263 }
1264
1265 static void libc_endhostent(void)
1266 {
1267         nwrap_load_lib_function(NWRAP_LIBNSL, endhostent);
1268
1269         nwrap_main_global->libc->fns->_libc_endhostent();
1270 }
1271
1272 static struct hostent *libc_gethostbyname(const char *name)
1273 {
1274         nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname);
1275
1276         return nwrap_main_global->libc->fns->_libc_gethostbyname(name);
1277 }
1278
1279 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
1280 static struct hostent *libc_gethostbyname2(const char *name, int af)
1281 {
1282         nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname2);
1283
1284         return nwrap_main_global->libc->fns->_libc_gethostbyname2(name, af);
1285 }
1286 #endif
1287
1288 static struct hostent *libc_gethostbyaddr(const void *addr,
1289                                           socklen_t len,
1290                                           int type)
1291 {
1292         nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr);
1293
1294         return nwrap_main_global->libc->fns->_libc_gethostbyaddr(addr,
1295                                                                  len,
1296                                                                  type);
1297 }
1298
1299 static int libc_gethostname(char *name, size_t len)
1300 {
1301         nwrap_load_lib_function(NWRAP_LIBNSL, gethostname);
1302
1303         return nwrap_main_global->libc->fns->_libc_gethostname(name, len);
1304 }
1305
1306 #ifdef HAVE_GETHOSTBYNAME_R
1307 static int libc_gethostbyname_r(const char *name,
1308                                 struct hostent *ret,
1309                                 char *buf,
1310                                 size_t buflen,
1311                                 struct hostent **result,
1312                                 int *h_errnop)
1313 {
1314         nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname_r);
1315
1316         return nwrap_main_global->libc->fns->_libc_gethostbyname_r(name,
1317                                                                    ret,
1318                                                                    buf,
1319                                                                    buflen,
1320                                                                    result,
1321                                                                    h_errnop);
1322 }
1323 #endif
1324
1325 #ifdef HAVE_GETHOSTBYADDR_R
1326 static int libc_gethostbyaddr_r(const void *addr,
1327                                 socklen_t len,
1328                                 int type,
1329                                 struct hostent *ret,
1330                                 char *buf,
1331                                 size_t buflen,
1332                                 struct hostent **result,
1333                                 int *h_errnop)
1334 {
1335         nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr_r);
1336
1337         return nwrap_main_global->libc->fns->_libc_gethostbyaddr_r(addr,
1338                                                                    len,
1339                                                                    type,
1340                                                                    ret,
1341                                                                    buf,
1342                                                                    buflen,
1343                                                                    result,
1344                                                                    h_errnop);
1345 }
1346 #endif
1347
1348 static int libc_getaddrinfo(const char *node,
1349                             const char *service,
1350                             const struct addrinfo *hints,
1351                             struct addrinfo **res)
1352 {
1353         nwrap_load_lib_function(NWRAP_LIBSOCKET, getaddrinfo);
1354
1355         return nwrap_main_global->libc->fns->_libc_getaddrinfo(node,
1356                                                                service,
1357                                                                hints,
1358                                                                res);
1359 }
1360
1361 static int libc_getnameinfo(const struct sockaddr *sa,
1362                             socklen_t salen,
1363                             char *host,
1364                             size_t hostlen,
1365                             char *serv,
1366                             size_t servlen,
1367                             int flags)
1368 {
1369         nwrap_load_lib_function(NWRAP_LIBSOCKET, getnameinfo);
1370
1371         return nwrap_main_global->libc->fns->_libc_getnameinfo(sa,
1372                                                                salen,
1373                                                                host,
1374                                                                hostlen,
1375                                                                serv,
1376                                                                servlen,
1377                                                                flags);
1378 }
1379
1380 /*********************************************************
1381  * NWRAP NSS MODULE LOADER FUNCTIONS
1382  *********************************************************/
1383
1384 static void *nwrap_load_module_fn(struct nwrap_backend *b,
1385                                   const char *fn_name)
1386 {
1387         void *res;
1388         char *s;
1389
1390         if (!b->so_handle) {
1391                 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1392                 return NULL;
1393         }
1394
1395         if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
1396                 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1397                 return NULL;
1398         }
1399
1400         res = dlsym(b->so_handle, s);
1401         if (!res) {
1402                 NWRAP_LOG(NWRAP_LOG_ERROR,
1403                           "Cannot find function %s in %s",
1404                           s, b->so_path);
1405         }
1406         SAFE_FREE(s);
1407         return res;
1408 }
1409
1410 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
1411 {
1412         struct nwrap_module_nss_fns *fns;
1413
1414         if (!b->so_handle) {
1415                 return NULL;
1416         }
1417
1418         fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
1419         if (!fns) {
1420                 return NULL;
1421         }
1422
1423         *(void **)(&fns->_nss_getpwnam_r) =
1424                 nwrap_load_module_fn(b, "getpwnam_r");
1425         *(void **)(&fns->_nss_getpwuid_r) =
1426                 nwrap_load_module_fn(b, "getpwuid_r");
1427         *(void **)(&fns->_nss_setpwent) =
1428                 nwrap_load_module_fn(b, "setpwent");
1429         *(void **)(&fns->_nss_getpwent_r) =
1430                 nwrap_load_module_fn(b, "getpwent_r");
1431         *(void **)(&fns->_nss_endpwent) =
1432                 nwrap_load_module_fn(b, "endpwent");
1433         *(void **)(&fns->_nss_initgroups) =
1434                 nwrap_load_module_fn(b, "initgroups_dyn");
1435         *(void **)(&fns->_nss_getgrnam_r) =
1436                 nwrap_load_module_fn(b, "getgrnam_r");
1437         *(void **)(&fns->_nss_getgrgid_r)=
1438                 nwrap_load_module_fn(b, "getgrgid_r");
1439         *(void **)(&fns->_nss_setgrent) =
1440                 nwrap_load_module_fn(b, "setgrent");
1441         *(void **)(&fns->_nss_getgrent_r) =
1442                 nwrap_load_module_fn(b, "getgrent_r");
1443         *(void **)(&fns->_nss_endgrent) =
1444                 nwrap_load_module_fn(b, "endgrent");
1445
1446         return fns;
1447 }
1448
1449 static void *nwrap_load_module(const char *so_path)
1450 {
1451         void *h;
1452
1453         if (!so_path || !strlen(so_path)) {
1454                 return NULL;
1455         }
1456
1457         h = dlopen(so_path, RTLD_LAZY);
1458         if (!h) {
1459                 NWRAP_LOG(NWRAP_LOG_ERROR,
1460                           "Cannot open shared library %s",
1461                           so_path);
1462                 return NULL;
1463         }
1464
1465         return h;
1466 }
1467
1468 static bool nwrap_module_init(const char *name,
1469                               struct nwrap_ops *ops,
1470                               const char *so_path,
1471                               int *num_backends,
1472                               struct nwrap_backend **backends)
1473 {
1474         struct nwrap_backend *b;
1475
1476         *backends = (struct nwrap_backend *)realloc(*backends,
1477                 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
1478         if (!*backends) {
1479                 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1480                 return false;
1481         }
1482
1483         b = &((*backends)[*num_backends]);
1484
1485         b->name = name;
1486         b->ops = ops;
1487         b->so_path = so_path;
1488
1489         if (so_path != NULL) {
1490                 b->so_handle = nwrap_load_module(so_path);
1491                 b->fns = nwrap_load_module_fns(b);
1492                 if (b->fns == NULL) {
1493                         return false;
1494                 }
1495         } else {
1496                 b->so_handle = NULL;
1497                 b->fns = NULL;
1498         }
1499
1500         (*num_backends)++;
1501
1502         return true;
1503 }
1504
1505 static void nwrap_libc_init(struct nwrap_main *r)
1506 {
1507         r->libc = malloc(sizeof(struct nwrap_libc));
1508         if (r->libc == NULL) {
1509                 printf("Failed to allocate memory for libc");
1510                 exit(-1);
1511         }
1512         ZERO_STRUCTP(r->libc);
1513
1514         r->libc->fns = malloc(sizeof(struct nwrap_libc_fns));
1515         if (r->libc->fns == NULL) {
1516                 printf("Failed to allocate memory for libc functions");
1517                 exit(-1);
1518         }
1519         ZERO_STRUCTP(r->libc->fns);
1520 }
1521
1522 static void nwrap_backend_init(struct nwrap_main *r)
1523 {
1524         const char *module_so_path = getenv("NSS_WRAPPER_MODULE_SO_PATH");
1525         const char *module_fn_name = getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1526
1527         r->num_backends = 0;
1528         r->backends = NULL;
1529
1530         if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1531                                &r->num_backends,
1532                                &r->backends)) {
1533                 NWRAP_LOG(NWRAP_LOG_ERROR,
1534                           "Failed to initialize 'files' backend");
1535                 return;
1536         }
1537
1538         if (module_so_path != NULL &&
1539             module_so_path[0] != '\0' &&
1540             module_fn_name != NULL &&
1541             module_fn_name[0] != '\0') {
1542                 if (!nwrap_module_init(module_fn_name,
1543                                        &nwrap_module_ops,
1544                                        module_so_path,
1545                                        &r->num_backends,
1546                                        &r->backends)) {
1547                         NWRAP_LOG(NWRAP_LOG_ERROR,
1548                                   "Failed to initialize '%s' backend",
1549                                   module_fn_name);
1550                         return;
1551                 }
1552         }
1553 }
1554
1555 static void nwrap_init(void)
1556 {
1557         const char *env;
1558         char *endptr;
1559         size_t max_hostents_tmp;
1560
1561         NWRAP_LOCK(nwrap_initialized);
1562         if (nwrap_initialized) {
1563                 NWRAP_UNLOCK(nwrap_initialized);
1564                 return;
1565         }
1566
1567         /*
1568          * Still holding nwrap_initialized lock here.
1569          * We don't use NWRAP_(UN)LOCK_ALL macros here because we
1570          * want to avoid overhead when other threads do their job.
1571          */
1572         NWRAP_LOCK(nwrap_global);
1573         NWRAP_LOCK(nwrap_gr_global);
1574         NWRAP_LOCK(nwrap_he_global);
1575         NWRAP_LOCK(nwrap_pw_global);
1576         NWRAP_LOCK(nwrap_sp_global);
1577
1578         nwrap_initialized = true;
1579
1580         env = getenv("NSS_WRAPPER_MAX_HOSTENTS");
1581         if (env != NULL) {
1582                 max_hostents_tmp = (size_t)strtoul(env, &endptr, 10);
1583                 if ((*env == '\0') ||
1584                     (*endptr != '\0') ||
1585                     (max_hostents_tmp == 0)) {
1586                         NWRAP_LOG(NWRAP_LOG_DEBUG,
1587                                   "Error parsing NSS_WRAPPER_MAX_HOSTENTS "
1588                                   "value or value is too small. "
1589                                   "Using default value: %lu.",
1590                                   (unsigned long)max_hostents);
1591                 } else {
1592                         max_hostents = max_hostents_tmp;
1593                 }
1594         }
1595         /* Initialize hash table */
1596         NWRAP_LOG(NWRAP_LOG_DEBUG,
1597                   "Initializing hash table of size %lu items.",
1598                   (unsigned long)max_hostents);
1599         if (hcreate(max_hostents) == 0) {
1600                 NWRAP_LOG(NWRAP_LOG_ERROR,
1601                           "Failed to initialize hash table");
1602                 goto done;
1603         }
1604
1605         nwrap_main_global = &__nwrap_main_global;
1606
1607         nwrap_libc_init(nwrap_main_global);
1608
1609         nwrap_backend_init(nwrap_main_global);
1610
1611         /* passwd */
1612         nwrap_pw_global.cache = &__nwrap_cache_pw;
1613
1614         nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
1615         nwrap_pw_global.cache->fp = NULL;
1616         nwrap_pw_global.cache->fd = -1;
1617         nwrap_pw_global.cache->private_data = &nwrap_pw_global;
1618         nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
1619         nwrap_pw_global.cache->unload = nwrap_pw_unload;
1620
1621         /* shadow */
1622 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1623         nwrap_sp_global.cache = &__nwrap_cache_sp;
1624
1625         nwrap_sp_global.cache->path = getenv("NSS_WRAPPER_SHADOW");
1626         nwrap_sp_global.cache->fp = NULL;
1627         nwrap_sp_global.cache->fd = -1;
1628         nwrap_sp_global.cache->private_data = &nwrap_sp_global;
1629         nwrap_sp_global.cache->parse_line = nwrap_sp_parse_line;
1630         nwrap_sp_global.cache->unload = nwrap_sp_unload;
1631 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1632
1633         /* group */
1634         nwrap_gr_global.cache = &__nwrap_cache_gr;
1635
1636         nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
1637         nwrap_gr_global.cache->fp = NULL;
1638         nwrap_gr_global.cache->fd = -1;
1639         nwrap_gr_global.cache->private_data = &nwrap_gr_global;
1640         nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
1641         nwrap_gr_global.cache->unload = nwrap_gr_unload;
1642
1643         /* hosts */
1644         nwrap_he_global.cache = &__nwrap_cache_he;
1645
1646         nwrap_he_global.cache->path = getenv("NSS_WRAPPER_HOSTS");
1647         nwrap_he_global.cache->fp = NULL;
1648         nwrap_he_global.cache->fd = -1;
1649         nwrap_he_global.cache->private_data = &nwrap_he_global;
1650         nwrap_he_global.cache->parse_line = nwrap_he_parse_line;
1651         nwrap_he_global.cache->unload = nwrap_he_unload;
1652
1653 done:
1654         /* We hold all locks here so we can use NWRAP_UNLOCK_ALL. */
1655         NWRAP_UNLOCK_ALL;
1656 }
1657
1658 bool nss_wrapper_enabled(void)
1659 {
1660         nwrap_init();
1661
1662         if (nwrap_pw_global.cache->path == NULL ||
1663             nwrap_pw_global.cache->path[0] == '\0') {
1664                 return false;
1665         }
1666         if (nwrap_gr_global.cache->path == NULL ||
1667             nwrap_gr_global.cache->path[0] == '\0') {
1668                 return false;
1669         }
1670
1671         return true;
1672 }
1673
1674 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
1675 bool nss_wrapper_shadow_enabled(void)
1676 {
1677         nwrap_init();
1678
1679         if (nwrap_sp_global.cache->path == NULL ||
1680             nwrap_sp_global.cache->path[0] == '\0') {
1681                 return false;
1682         }
1683
1684         return true;
1685 }
1686 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
1687
1688 bool nss_wrapper_hosts_enabled(void)
1689 {
1690         nwrap_init();
1691
1692         if (nwrap_he_global.cache->path == NULL ||
1693             nwrap_he_global.cache->path[0] == '\0') {
1694                 return false;
1695         }
1696
1697         return true;
1698 }
1699
1700 static bool nwrap_hostname_enabled(void)
1701 {
1702         nwrap_init();
1703
1704         if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
1705                 return false;
1706         }
1707
1708         return true;
1709 }
1710
1711 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
1712 {
1713         char *line = NULL;
1714         ssize_t n;
1715         /* Unused but getline needs it */
1716         size_t len;
1717         bool ok;
1718
1719         if (nwrap->st.st_size == 0) {
1720                 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
1721                 return true;
1722         }
1723
1724         /* Support for 32-bit system I guess */
1725         if (nwrap->st.st_size > INT32_MAX) {
1726                 NWRAP_LOG(NWRAP_LOG_ERROR,
1727                           "Size[%u] larger than INT32_MAX",
1728                           (unsigned)nwrap->st.st_size);
1729                 return false;
1730         }
1731
1732         rewind(nwrap->fp);
1733
1734         do {
1735                 n = getline(&line, &len, nwrap->fp);
1736                 if (n < 0) {
1737                         SAFE_FREE(line);
1738                         if (feof(nwrap->fp)) {
1739                                 break;
1740                         }
1741
1742                         NWRAP_LOG(NWRAP_LOG_ERROR,
1743                                   "Unable to read line from file: %s",
1744                                   nwrap->path);
1745                         return false;
1746                 }
1747
1748                 if (line[n - 1] == '\n') {
1749                         line[n - 1] = '\0';
1750                 }
1751
1752                 if (line[0] == '\0') {
1753                         SAFE_FREE(line);
1754                         continue;
1755                 }
1756
1757                 ok = nwrap->parse_line(nwrap, line);
1758                 if (!ok) {
1759                         NWRAP_LOG(NWRAP_LOG_ERROR,
1760                                   "Unable to parse line file: %s",
1761                                   line);
1762                         SAFE_FREE(line);
1763                         return false;
1764                 }
1765
1766                 /* Line is parsed without issues so add it to list */
1767                 ok = nwrap_vector_add_item(&(nwrap->lines), (void *const) line);
1768                 if (!ok) {
1769                         NWRAP_LOG(NWRAP_LOG_ERROR,
1770                                   "Unable to add line to vector");
1771                         return false;
1772                 }
1773
1774                 /* This forces getline to allocate new memory for line. */
1775                 line = NULL;
1776         } while (!feof(nwrap->fp));
1777
1778         return true;
1779 }
1780
1781 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
1782 {
1783         nwrap->unload(nwrap);
1784
1785         nwrap_lines_unload(nwrap);
1786 }
1787
1788 static bool nwrap_files_cache_reload(struct nwrap_cache *nwrap)
1789 {
1790         struct stat st;
1791         int ret;
1792         bool ok;
1793         bool retried = false;
1794
1795         assert(nwrap != NULL);
1796
1797 reopen:
1798         if (nwrap->fd < 0) {
1799                 nwrap->fp = fopen(nwrap->path, "re");
1800                 if (nwrap->fp == NULL) {
1801                         nwrap->fd = -1;
1802                         NWRAP_LOG(NWRAP_LOG_ERROR,
1803                                   "Unable to open '%s' readonly %d:%s",
1804                                   nwrap->path, nwrap->fd,
1805                                   strerror(errno));
1806                         return false;
1807
1808                 }
1809                 nwrap->fd = fileno(nwrap->fp);
1810                 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
1811         }
1812
1813         ret = fstat(nwrap->fd, &st);
1814         if (ret != 0) {
1815                 NWRAP_LOG(NWRAP_LOG_ERROR,
1816                           "fstat(%s) - %d:%s",
1817                           nwrap->path,
1818                           ret,
1819                           strerror(errno));
1820                 fclose(nwrap->fp);
1821                 nwrap->fp = NULL;
1822                 nwrap->fd = -1;
1823                 return false;
1824         }
1825
1826         if (retried == false && st.st_nlink == 0) {
1827                 /* maybe someone has replaced the file... */
1828                 NWRAP_LOG(NWRAP_LOG_TRACE,
1829                           "st_nlink == 0, reopen %s",
1830                           nwrap->path);
1831                 retried = true;
1832                 memset(&nwrap->st, 0, sizeof(nwrap->st));
1833                 fclose(nwrap->fp);
1834                 nwrap->fp = NULL;
1835                 nwrap->fd = -1;
1836                 goto reopen;
1837         }
1838
1839         if (st.st_mtime == nwrap->st.st_mtime) {
1840                 NWRAP_LOG(NWRAP_LOG_TRACE,
1841                           "st_mtime[%u] hasn't changed, skip reload",
1842                           (unsigned)st.st_mtime);
1843                 return true;
1844         }
1845
1846         NWRAP_LOG(NWRAP_LOG_TRACE,
1847                   "st_mtime has changed [%u] => [%u], start reload",
1848                   (unsigned)st.st_mtime,
1849                   (unsigned)nwrap->st.st_mtime);
1850
1851         nwrap->st = st;
1852
1853         nwrap_files_cache_unload(nwrap);
1854
1855         ok = nwrap_parse_file(nwrap);
1856         if (!ok) {
1857                 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
1858                 nwrap_files_cache_unload(nwrap);
1859                 return false;
1860         }
1861
1862         NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
1863         return true;
1864 }
1865
1866 /*
1867  * the caller has to call nwrap_unload() on failure
1868  */
1869 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
1870 {
1871         struct nwrap_pw *nwrap_pw;
1872         char *c;
1873         char *p;
1874         char *e;
1875         struct passwd *pw;
1876         size_t list_size;
1877
1878         nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1879
1880         list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
1881         pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
1882         if (!pw) {
1883                 NWRAP_LOG(NWRAP_LOG_ERROR,
1884                           "realloc(%u) failed",
1885                           (unsigned)list_size);
1886                 return false;
1887         }
1888         nwrap_pw->list = pw;
1889
1890         pw = &nwrap_pw->list[nwrap_pw->num];
1891
1892         c = line;
1893
1894         /* name */
1895         p = strchr(c, ':');
1896         if (!p) {
1897                 NWRAP_LOG(NWRAP_LOG_ERROR,
1898                           "Invalid line[%s]: '%s'",
1899                           line,
1900                           c);
1901                 return false;
1902         }
1903         *p = '\0';
1904         p++;
1905         pw->pw_name = c;
1906         c = p;
1907
1908         NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
1909
1910         /* password */
1911         p = strchr(c, ':');
1912         if (!p) {
1913                 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1914                 return false;
1915         }
1916         *p = '\0';
1917         p++;
1918         pw->pw_passwd = c;
1919         c = p;
1920
1921         NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
1922
1923         /* uid */
1924         p = strchr(c, ':');
1925         if (!p) {
1926                 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1927                 return false;
1928         }
1929         *p = '\0';
1930         p++;
1931         e = NULL;
1932         pw->pw_uid = (uid_t)strtoul(c, &e, 10);
1933         if (c == e) {
1934                 NWRAP_LOG(NWRAP_LOG_ERROR,
1935                           "Invalid line[%s]: '%s' - %s",
1936                           line, c, strerror(errno));
1937                 return false;
1938         }
1939         if (e == NULL) {
1940                 NWRAP_LOG(NWRAP_LOG_ERROR,
1941                           "Invalid line[%s]: '%s' - %s",
1942                           line, c, strerror(errno));
1943                 return false;
1944         }
1945         if (e[0] != '\0') {
1946                 NWRAP_LOG(NWRAP_LOG_ERROR,
1947                           "Invalid line[%s]: '%s' - %s",
1948                           line, c, strerror(errno));
1949                 return false;
1950         }
1951         c = p;
1952
1953         NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
1954
1955         /* gid */
1956         p = strchr(c, ':');
1957         if (!p) {
1958                 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1959                 return false;
1960         }
1961         *p = '\0';
1962         p++;
1963         e = NULL;
1964         pw->pw_gid = (gid_t)strtoul(c, &e, 10);
1965         if (c == e) {
1966                 NWRAP_LOG(NWRAP_LOG_ERROR,
1967                           "Invalid line[%s]: '%s' - %s",
1968                           line, c, strerror(errno));
1969                 return false;
1970         }
1971         if (e == NULL) {
1972                 NWRAP_LOG(NWRAP_LOG_ERROR,
1973                           "Invalid line[%s]: '%s' - %s",
1974                           line, c, strerror(errno));
1975                 return false;
1976         }
1977         if (e[0] != '\0') {
1978                 NWRAP_LOG(NWRAP_LOG_ERROR,
1979                           "Invalid line[%s]: '%s' - %s",
1980                           line, c, strerror(errno));
1981                 return false;
1982         }
1983         c = p;
1984
1985         NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]\n", pw->pw_gid);
1986
1987 #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
1988         pw->pw_class = discard_const_p(char, "");
1989
1990         NWRAP_LOG(NWRAP_LOG_TRACE, "class[%s]", pw->pw_class);
1991 #endif /* HAVE_STRUCT_PASSWD_PW_CLASS */
1992
1993 #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
1994         pw->pw_change = 0;
1995
1996         NWRAP_LOG(NWRAP_LOG_TRACE,
1997                   "change[%lu]",
1998                   (unsigned long)pw->pw_change);
1999 #endif /* HAVE_STRUCT_PASSWD_PW_CHANGE */
2000
2001 #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
2002         pw->pw_expire = 0;
2003
2004         NWRAP_LOG(NWRAP_LOG_TRACE,
2005                   "expire[%lu]",
2006                   (unsigned long)pw->pw_expire);
2007 #endif /* HAVE_STRUCT_PASSWD_PW_EXPIRE */
2008
2009         /* gecos */
2010         p = strchr(c, ':');
2011         if (!p) {
2012                 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
2013                 return false;
2014         }
2015         *p = '\0';
2016         p++;
2017         pw->pw_gecos = c;
2018         c = p;
2019
2020         NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
2021
2022         /* dir */
2023         p = strchr(c, ':');
2024         if (!p) {
2025                 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
2026                 return false;
2027         }
2028         *p = '\0';
2029         p++;
2030         pw->pw_dir = c;
2031         c = p;
2032
2033         NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
2034
2035         /* shell */
2036         pw->pw_shell = c;
2037         NWRAP_LOG(NWRAP_LOG_TRACE, "shell[%s]", pw->pw_shell);
2038
2039         NWRAP_LOG(NWRAP_LOG_DEBUG,
2040                   "Added user[%s:%s:%u:%u:%s:%s:%s]",
2041                   pw->pw_name, pw->pw_passwd,
2042                   pw->pw_uid, pw->pw_gid,
2043                   pw->pw_gecos, pw->pw_dir, pw->pw_shell);
2044
2045         nwrap_pw->num++;
2046         return true;
2047 }
2048
2049 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
2050 {
2051         struct nwrap_pw *nwrap_pw;
2052         nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
2053
2054         SAFE_FREE(nwrap_pw->list);
2055         nwrap_pw->num = 0;
2056         nwrap_pw->idx = 0;
2057 }
2058
2059 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
2060                            char *buf, size_t buflen, struct passwd **dstp)
2061 {
2062         char *first;
2063         char *last;
2064         off_t ofs;
2065
2066         first = src->pw_name;
2067
2068         last = src->pw_shell;
2069         while (*last) last++;
2070
2071         ofs = PTR_DIFF(last + 1, first);
2072
2073         if (ofs > (off_t) buflen) {
2074                 return ERANGE;
2075         }
2076
2077         memcpy(buf, first, ofs);
2078
2079         ofs = PTR_DIFF(src->pw_name, first);
2080         dst->pw_name = buf + ofs;
2081         ofs = PTR_DIFF(src->pw_passwd, first);
2082         dst->pw_passwd = buf + ofs;
2083         dst->pw_uid = src->pw_uid;
2084         dst->pw_gid = src->pw_gid;
2085         ofs = PTR_DIFF(src->pw_gecos, first);
2086         dst->pw_gecos = buf + ofs;
2087         ofs = PTR_DIFF(src->pw_dir, first);
2088         dst->pw_dir = buf + ofs;
2089         ofs = PTR_DIFF(src->pw_shell, first);
2090         dst->pw_shell = buf + ofs;
2091
2092         if (dstp) {
2093                 *dstp = dst;
2094         }
2095
2096         return 0;
2097 }
2098
2099 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
2100 static bool nwrap_sp_parse_line(struct nwrap_cache *nwrap, char *line)
2101 {
2102         struct nwrap_sp *nwrap_sp;
2103         struct spwd *sp;
2104         size_t list_size;
2105         char *c;
2106         char *e;
2107         char *p;
2108
2109         nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2110
2111         list_size = sizeof(*nwrap_sp->list) * (nwrap_sp->num+1);
2112         sp = (struct spwd *)realloc(nwrap_sp->list, list_size);
2113         if (sp == NULL) {
2114                 NWRAP_LOG(NWRAP_LOG_ERROR,
2115                           "realloc(%u) failed",
2116                           (unsigned)list_size);
2117                 return false;
2118         }
2119         nwrap_sp->list = sp;
2120
2121         sp = &nwrap_sp->list[nwrap_sp->num];
2122
2123         c = line;
2124
2125         /* name */
2126         p = strchr(c, ':');
2127         if (p == NULL) {
2128                 NWRAP_LOG(NWRAP_LOG_ERROR,
2129                           "name -- Invalid line[%s]: '%s'",
2130                           line,
2131                           c);
2132                 return false;
2133         }
2134         *p = '\0';
2135         p++;
2136         sp->sp_namp = c;
2137         c = p;
2138
2139         NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", sp->sp_namp);
2140
2141         /* pwd */
2142         p = strchr(c, ':');
2143         if (p == NULL) {
2144                 NWRAP_LOG(NWRAP_LOG_ERROR,
2145                           "pwd -- Invalid line[%s]: '%s'",
2146                           line,
2147                           c);
2148                 return false;
2149         }
2150         *p = '\0';
2151         p++;
2152         sp->sp_pwdp = c;
2153         c = p;
2154
2155         /* lstchg (long) */
2156         if (c[0] == ':') {
2157                 sp->sp_lstchg = -1;
2158                 p++;
2159         } else {
2160                 p = strchr(c, ':');
2161                 if (p == NULL) {
2162                         NWRAP_LOG(NWRAP_LOG_ERROR,
2163                                   "lstchg -- Invalid line[%s]: '%s'",
2164                                   line,
2165                                   c);
2166                         return false;
2167                 }
2168                 *p = '\0';
2169                 p++;
2170                 sp->sp_lstchg = strtol(c, &e, 10);
2171                 if (c == e) {
2172                         NWRAP_LOG(NWRAP_LOG_ERROR,
2173                                   "lstchg -- Invalid line[%s]: '%s' - %s",
2174                                   line, c, strerror(errno));
2175                         return false;
2176                 }
2177                 if (e == NULL) {
2178                         NWRAP_LOG(NWRAP_LOG_ERROR,
2179                                   "lstchg -- Invalid line[%s]: '%s' - %s",
2180                                   line, c, strerror(errno));
2181                         return false;
2182                 }
2183                 if (e[0] != '\0') {
2184                         NWRAP_LOG(NWRAP_LOG_ERROR,
2185                                   "lstchg -- Invalid line[%s]: '%s' - %s",
2186                                   line, c, strerror(errno));
2187                         return false;
2188                 }
2189         }
2190         c = p;
2191
2192         /* min (long) */
2193         if (c[0] == ':') {
2194                 sp->sp_min = -1;
2195                 p++;
2196         } else {
2197                 p = strchr(c, ':');
2198                 if (p == NULL) {
2199                         NWRAP_LOG(NWRAP_LOG_ERROR,
2200                                   "min -- Invalid line[%s]: '%s'",
2201                                   line,
2202                                   c);
2203                         return false;
2204                 }
2205                 *p = '\0';
2206                 p++;
2207                 sp->sp_min = strtol(c, &e, 10);
2208                 if (c == e) {
2209                         NWRAP_LOG(NWRAP_LOG_ERROR,
2210                                   "min -- Invalid line[%s]: '%s' - %s",
2211                                   line, c, strerror(errno));
2212                         return false;
2213                 }
2214                 if (e == NULL) {
2215                         NWRAP_LOG(NWRAP_LOG_ERROR,
2216                                   "min -- Invalid line[%s]: '%s' - %s",
2217                                   line, c, strerror(errno));
2218                         return false;
2219                 }
2220                 if (e[0] != '\0') {
2221                         NWRAP_LOG(NWRAP_LOG_ERROR,
2222                                   "min -- Invalid line[%s]: '%s' - %s",
2223                                   line, c, strerror(errno));
2224                         return false;
2225                 }
2226         }
2227         c = p;
2228
2229         /* max (long) */
2230         if (c[0] == ':') {
2231                 sp->sp_max = -1;
2232                 p++;
2233         } else {
2234                 p = strchr(c, ':');
2235                 if (p == NULL) {
2236                         NWRAP_LOG(NWRAP_LOG_ERROR,
2237                                   "max -- Invalid line[%s]: '%s'",
2238                                   line,
2239                                   c);
2240                         return false;
2241                 }
2242                 *p = '\0';
2243                 p++;
2244                 sp->sp_max = strtol(c, &e, 10);
2245                 if (c == e) {
2246                         NWRAP_LOG(NWRAP_LOG_ERROR,
2247                                   "max -- Invalid line[%s]: '%s' - %s",
2248                                   line, c, strerror(errno));
2249                         return false;
2250                 }
2251                 if (e == NULL) {
2252                         NWRAP_LOG(NWRAP_LOG_ERROR,
2253                                   "max -- Invalid line[%s]: '%s' - %s",
2254                                   line, c, strerror(errno));
2255                         return false;
2256                 }
2257                 if (e[0] != '\0') {
2258                         NWRAP_LOG(NWRAP_LOG_ERROR,
2259                                   "max -- Invalid line[%s]: '%s' - %s",
2260                                   line, c, strerror(errno));
2261                         return false;
2262                 }
2263         }
2264         c = p;
2265
2266         /* warn (long) */
2267         if (c[0] == ':') {
2268                 sp->sp_warn = -1;
2269                 p++;
2270         } else {
2271                 p = strchr(c, ':');
2272                 if (p == NULL) {
2273                         NWRAP_LOG(NWRAP_LOG_ERROR,
2274                                   "warn -- Invalid line[%s]: '%s'",
2275                                   line,
2276                                   c);
2277                         return false;
2278                 }
2279                 *p = '\0';
2280                 p++;
2281                 sp->sp_warn = strtol(c, &e, 10);
2282                 if (c == e) {
2283                         NWRAP_LOG(NWRAP_LOG_ERROR,
2284                                   "warn -- Invalid line[%s]: '%s' - %s",
2285                                   line, c, strerror(errno));
2286                         return false;
2287                 }
2288                 if (e == NULL) {
2289                         NWRAP_LOG(NWRAP_LOG_ERROR,
2290                                   "warn -- Invalid line[%s]: '%s' - %s",
2291                                   line, c, strerror(errno));
2292                         return false;
2293                 }
2294                 if (e[0] != '\0') {
2295                         NWRAP_LOG(NWRAP_LOG_ERROR,
2296                                   "warn -- Invalid line[%s]: '%s' - %s",
2297                                   line, c, strerror(errno));
2298                         return false;
2299                 }
2300         }
2301         c = p;
2302
2303         /* inact (long) */
2304         if (c[0] == ':') {
2305                 sp->sp_inact = -1;
2306                 p++;
2307         } else {
2308                 p = strchr(c, ':');
2309                 if (p == NULL) {
2310                         NWRAP_LOG(NWRAP_LOG_ERROR,
2311                                   "inact -- Invalid line[%s]: '%s'",
2312                                   line,
2313                                   c);
2314                         return false;
2315                 }
2316                 *p = '\0';
2317                 p++;
2318                 sp->sp_inact = strtol(c, &e, 10);
2319                 if (c == e) {
2320                         NWRAP_LOG(NWRAP_LOG_ERROR,
2321                                   "inact -- Invalid line[%s]: '%s' - %s",
2322                                   line, c, strerror(errno));
2323                         return false;
2324                 }
2325                 if (e == NULL) {
2326                         NWRAP_LOG(NWRAP_LOG_ERROR,
2327                                   "inact -- Invalid line[%s]: '%s' - %s",
2328                                   line, c, strerror(errno));
2329                         return false;
2330                 }
2331                 if (e[0] != '\0') {
2332                         NWRAP_LOG(NWRAP_LOG_ERROR,
2333                                   "inact -- Invalid line[%s]: '%s' - %s",
2334                                   line, c, strerror(errno));
2335                         return false;
2336                 }
2337         }
2338         c = p;
2339
2340         /* expire (long) */
2341         if (c[0] == ':') {
2342                 sp->sp_expire = -1;
2343                 p++;
2344         } else {
2345                 p = strchr(c, ':');
2346                 if (p == NULL) {
2347                         NWRAP_LOG(NWRAP_LOG_ERROR,
2348                                   "expire -- Invalid line[%s]: '%s'",
2349                                   line,
2350                                   c);
2351                         return false;
2352                 }
2353                 *p = '\0';
2354                 p++;
2355                 sp->sp_expire = strtol(c, &e, 10);
2356                 if (c == e) {
2357                         NWRAP_LOG(NWRAP_LOG_ERROR,
2358                                   "expire -- Invalid line[%s]: '%s' - %s",
2359                                   line, c, strerror(errno));
2360                         return false;
2361                 }
2362                 if (e == NULL) {
2363                         NWRAP_LOG(NWRAP_LOG_ERROR,
2364                                   "expire -- Invalid line[%s]: '%s' - %s",
2365                                   line, c, strerror(errno));
2366                         return false;
2367                 }
2368                 if (e[0] != '\0') {
2369                         NWRAP_LOG(NWRAP_LOG_ERROR,
2370                                   "expire -- Invalid line[%s]: '%s' - %s",
2371                                   line, c, strerror(errno));
2372                         return false;
2373                 }
2374         }
2375         c = p;
2376
2377         nwrap_sp->num++;
2378         return true;
2379 }
2380
2381 static void nwrap_sp_unload(struct nwrap_cache *nwrap)
2382 {
2383         struct nwrap_sp *nwrap_sp;
2384         nwrap_sp = (struct nwrap_sp *)nwrap->private_data;
2385
2386         SAFE_FREE(nwrap_sp->list);
2387         nwrap_sp->num = 0;
2388         nwrap_sp->idx = 0;
2389 }
2390 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
2391
2392 /*
2393  * the caller has to call nwrap_unload() on failure
2394  */
2395 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
2396 {
2397         struct nwrap_gr *nwrap_gr;
2398         char *c;
2399         char *p;
2400         char *e;
2401         struct group *gr;
2402         size_t list_size;
2403         unsigned nummem;
2404
2405         nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2406
2407         list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
2408         gr = (struct group *)realloc(nwrap_gr->list, list_size);
2409         if (!gr) {
2410                 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
2411                 return false;
2412         }
2413         nwrap_gr->list = gr;
2414
2415         gr = &nwrap_gr->list[nwrap_gr->num];
2416
2417         c = line;
2418
2419         /* name */
2420         p = strchr(c, ':');
2421         if (!p) {
2422                 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2423                 return false;
2424         }
2425         *p = '\0';
2426         p++;
2427         gr->gr_name = c;
2428         c = p;
2429
2430         NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
2431
2432         /* password */
2433         p = strchr(c, ':');
2434         if (!p) {
2435                 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2436                 return false;
2437         }
2438         *p = '\0';
2439         p++;
2440         gr->gr_passwd = c;
2441         c = p;
2442
2443         NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
2444
2445         /* gid */
2446         p = strchr(c, ':');
2447         if (!p) {
2448                 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
2449                 return false;
2450         }
2451         *p = '\0';
2452         p++;
2453         e = NULL;
2454         gr->gr_gid = (gid_t)strtoul(c, &e, 10);
2455         if (c == e) {
2456                 NWRAP_LOG(NWRAP_LOG_ERROR,
2457                           "Invalid line[%s]: '%s' - %s",
2458                           line, c, strerror(errno));
2459                 return false;
2460         }
2461         if (e == NULL) {
2462                 NWRAP_LOG(NWRAP_LOG_ERROR,
2463                           "Invalid line[%s]: '%s' - %s",
2464                           line, c, strerror(errno));
2465                 return false;
2466         }
2467         if (e[0] != '\0') {
2468                 NWRAP_LOG(NWRAP_LOG_ERROR,
2469                           "Invalid line[%s]: '%s' - %s",
2470                           line, c, strerror(errno));
2471                 return false;
2472         }
2473         c = p;
2474
2475         NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
2476
2477         /* members */
2478         gr->gr_mem = (char **)malloc(sizeof(char *));
2479         if (!gr->gr_mem) {
2480                 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
2481                 return false;
2482         }
2483         gr->gr_mem[0] = NULL;
2484
2485         for(nummem=0; p; nummem++) {
2486                 char **m;
2487                 size_t m_size;
2488                 c = p;
2489                 p = strchr(c, ',');
2490                 if (p) {
2491                         *p = '\0';
2492                         p++;
2493                 }
2494
2495                 if (strlen(c) == 0) {
2496                         break;
2497                 }
2498
2499                 m_size = sizeof(char *) * (nummem+2);
2500                 m = (char **)realloc(gr->gr_mem, m_size);
2501                 if (!m) {
2502                         NWRAP_LOG(NWRAP_LOG_ERROR,
2503                                   "realloc(%zd) failed",
2504                                   m_size);
2505                         return false;
2506                 }
2507                 gr->gr_mem = m;
2508                 gr->gr_mem[nummem] = c;
2509                 gr->gr_mem[nummem+1] = NULL;
2510
2511                 NWRAP_LOG(NWRAP_LOG_TRACE,
2512                           "member[%u]: '%s'",
2513                           nummem, gr->gr_mem[nummem]);
2514         }
2515
2516         NWRAP_LOG(NWRAP_LOG_DEBUG,
2517                   "Added group[%s:%s:%u:] with %u members",
2518                   gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem);
2519
2520         nwrap_gr->num++;
2521         return true;
2522 }
2523
2524 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
2525 {
2526         int i;
2527         struct nwrap_gr *nwrap_gr;
2528         nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
2529
2530         if (nwrap_gr->list) {
2531                 for (i=0; i < nwrap_gr->num; i++) {
2532                         SAFE_FREE(nwrap_gr->list[i].gr_mem);
2533                 }
2534                 SAFE_FREE(nwrap_gr->list);
2535         }
2536
2537         nwrap_gr->num = 0;
2538         nwrap_gr->idx = 0;
2539 }
2540
2541 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
2542                            char *buf, size_t buflen, struct group **dstp)
2543 {
2544         char *first;
2545         char **lastm;
2546         char *last = NULL;
2547         off_t ofsb;
2548         off_t ofsm;
2549         off_t ofs;
2550         unsigned i;
2551
2552         first = src->gr_name;
2553
2554         lastm = src->gr_mem;
2555         while (*lastm) {
2556                 last = *lastm;
2557                 lastm++;
2558         }
2559
2560         if (last == NULL) {
2561                 last = src->gr_passwd;
2562         }
2563         while (*last) last++;
2564
2565         ofsb = PTR_DIFF(last + 1, first);
2566         ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
2567
2568         if ((ofsb + ofsm) > (off_t) buflen) {
2569                 return ERANGE;
2570         }
2571
2572         memcpy(buf, first, ofsb);
2573         memcpy(buf + ofsb, src->gr_mem, ofsm);
2574
2575         ofs = PTR_DIFF(src->gr_name, first);
2576         dst->gr_name = buf + ofs;
2577         ofs = PTR_DIFF(src->gr_passwd, first);
2578         dst->gr_passwd = buf + ofs;
2579         dst->gr_gid = src->gr_gid;
2580
2581         dst->gr_mem = (char **)(buf + ofsb);
2582         for (i=0; src->gr_mem[i]; i++) {
2583                 ofs = PTR_DIFF(src->gr_mem[i], first);
2584                 dst->gr_mem[i] = buf + ofs;
2585         }
2586
2587         if (dstp) {
2588                 *dstp = dst;
2589         }
2590
2591         return 0;
2592 }
2593
2594 static struct nwrap_entlist *nwrap_entlist_init(struct nwrap_entdata *ed)
2595 {
2596         struct nwrap_entlist *el;
2597
2598         if (ed == NULL) {
2599                 NWRAP_LOG(NWRAP_LOG_ERROR,
2600                           "entry is NULL, can't create list item");
2601                 return NULL;
2602         }
2603
2604         el = (struct nwrap_entlist *)malloc(sizeof(struct nwrap_entlist));
2605         if (el == NULL) {
2606                 NWRAP_LOG(NWRAP_LOG_ERROR, "malloc failed");
2607                 return NULL;
2608         }
2609
2610         el->next = NULL;
2611         el->ed = ed;
2612
2613         return el;
2614 }
2615
2616 static bool nwrap_ed_inventarize_add_new(char *const h_name,
2617                                          struct nwrap_entdata *const ed)
2618 {
2619         ENTRY e;
2620         ENTRY *p;
2621         struct nwrap_entlist *el;
2622         bool ok;
2623
2624         if (h_name == NULL) {
2625                 NWRAP_LOG(NWRAP_LOG_ERROR, "h_name NULL - can't add");
2626                 return false;
2627         }
2628
2629         el = nwrap_entlist_init(ed);
2630         if (el == NULL) {
2631                 return false;
2632         }
2633
2634         e.key = h_name;
2635         e.data = (void *)el;
2636
2637         p = hsearch(e, ENTER);
2638         if (p == NULL) {
2639                 NWRAP_LOG(NWRAP_LOG_ERROR,
2640                           "Hash table is full (%s)!",
2641                           strerror(errno));
2642                 return false;
2643         }
2644
2645         ok = nwrap_vector_add_item(&(nwrap_he_global.lists), (void *)el);
2646         if (!ok) {
2647                 NWRAP_LOG(NWRAP_LOG_ERROR,
2648                           "Failed to add list entry to vector.");
2649                 return false;
2650         }
2651
2652         return true;
2653 }
2654
2655 static bool nwrap_ed_inventarize_add_to_existing(struct nwrap_entdata *const ed,
2656                                                  struct nwrap_entlist *const el)
2657 {
2658         struct nwrap_entlist *cursor;
2659         struct nwrap_entlist *el_new;
2660
2661         if (el == NULL) {
2662                 NWRAP_LOG(NWRAP_LOG_ERROR, "list is NULL, can not add");
2663                 return false;
2664         }
2665
2666
2667         for (cursor = el; cursor->next != NULL; cursor = cursor->next)
2668         {
2669                 if (cursor->ed == ed) {
2670                         /* The entry already exists in this list. */
2671                         return true;
2672                 }
2673         }
2674
2675         if (cursor->ed == ed) {
2676                 /* The entry already exists in this list. */
2677                 return true;
2678         }
2679
2680         el_new = nwrap_entlist_init(ed);
2681         if (el_new == NULL) {
2682                 return false;
2683         }
2684
2685         cursor->next = el_new;
2686         return true;
2687 }
2688
2689 static bool nwrap_ed_inventarize(char *const name,
2690                                  struct nwrap_entdata *const ed)
2691 {
2692         ENTRY e;
2693         ENTRY *p;
2694         bool ok;
2695
2696         e.key = name;
2697         e.data = NULL;
2698
2699         NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching name: %s", e.key);
2700
2701         p = hsearch(e, FIND);
2702         if (p == NULL) {
2703                 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found. Adding...", name);
2704                 ok = nwrap_ed_inventarize_add_new(name, ed);
2705         } else {
2706                 struct nwrap_entlist *el = (struct nwrap_entlist *)p->data;
2707
2708                 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s found. Add record to list.", name);
2709                 ok = nwrap_ed_inventarize_add_to_existing(ed, el);
2710         }
2711
2712         return ok;
2713 }
2714
2715 static bool nwrap_add_hname(struct nwrap_entdata *const ed)
2716 {
2717         char *const h_name = (char *const)(ed->ht.h_name);
2718         unsigned i;
2719         bool ok;
2720
2721         ok = nwrap_ed_inventarize(h_name, ed);
2722         if (!ok) {
2723                 return false;
2724         }
2725
2726         if (ed->ht.h_aliases == NULL) {
2727                 return true;
2728         }
2729
2730         /* Itemize aliases */
2731         for (i = 0; ed->ht.h_aliases[i] != NULL; ++i) {
2732                 char *h_name_alias;
2733
2734                 h_name_alias = ed->ht.h_aliases[i];
2735
2736                 NWRAP_LOG(NWRAP_LOG_DEBUG, "Add alias: %s", h_name_alias);
2737
2738                 if (!nwrap_ed_inventarize(h_name_alias, ed)) {
2739                         NWRAP_LOG(NWRAP_LOG_ERROR,
2740                                   "Unable to add alias: %s", h_name_alias);
2741                         return false;
2742                 }
2743         }
2744
2745         return true;
2746 }
2747
2748 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line)
2749 {
2750         struct nwrap_he *nwrap_he = (struct nwrap_he *)nwrap->private_data;
2751         bool do_aliases = true;
2752         ssize_t aliases_count = 0;
2753         char *p;
2754         char *i;
2755         char *n;
2756
2757         char *ip;
2758         bool ok;
2759
2760         struct nwrap_entdata *ed = (struct nwrap_entdata *)
2761                                    malloc(sizeof(struct nwrap_entdata));
2762         if (ed == NULL) {
2763                 NWRAP_LOG(NWRAP_LOG_ERROR,
2764                           "Unable to allocate memory for nwrap_entdata");
2765                 return false;
2766         }
2767         ZERO_STRUCTP(ed);
2768
2769         i = line;
2770
2771         /*
2772          * IP
2773          */
2774
2775         /* Walk to first char */
2776         for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
2777                 if (*p == '\0') {
2778                         NWRAP_LOG(NWRAP_LOG_ERROR,
2779                                   "Invalid line[%s]: '%s'",
2780                                   line, i);
2781                         free(ed);
2782                         return false;
2783                 }
2784         }
2785
2786         for (i = p; !isspace((int)*p); p++) {
2787                 if (*p == '\0') {
2788                         NWRAP_LOG(NWRAP_LOG_ERROR,
2789                                   "Invalid line[%s]: '%s'",
2790                                   line, i);
2791                         free(ed);
2792                         return false;
2793                 }
2794         }
2795
2796         *p = '\0';
2797
2798         if (inet_pton(AF_INET, i, ed->addr.host_addr)) {
2799                 ed->ht.h_addrtype = AF_INET;
2800                 ed->ht.h_length = 4;
2801 #ifdef HAVE_IPV6
2802         } else if (inet_pton(AF_INET6, i, ed->addr.host_addr)) {
2803                 ed->ht.h_addrtype = AF_INET6;
2804                 ed->ht.h_length = 16;
2805 #endif
2806         } else {
2807                 NWRAP_LOG(NWRAP_LOG_ERROR,
2808                           "Invalid line[%s]: '%s'",
2809                           line, i);
2810
2811                 free(ed);
2812                 return false;
2813         }
2814         ip = i;
2815
2816         ok = nwrap_vector_add_item(&(ed->nwrap_addrdata),
2817                                    (void *const)ed->addr.host_addr);
2818         if (!ok) {
2819                 NWRAP_LOG(NWRAP_LOG_ERROR, "Unable to add addrdata to vector");
2820                 free(ed);
2821                 return false;
2822         }
2823         ed->ht.h_addr_list = nwrap_vector_head(&ed->nwrap_addrdata);
2824
2825         p++;
2826
2827         /*
2828          * FQDN
2829          */
2830
2831         /* Walk to first char */
2832         for (n = p; *p != '_' && !isalnum((int) *p); p++) {
2833                 if (*p == '\0') {
2834                         NWRAP_LOG(NWRAP_LOG_ERROR,
2835                                   "Invalid line[%s]: '%s'",
2836                                   line, n);
2837
2838                         free(ed);
2839                         return false;
2840                 }
2841         }
2842
2843         for (n = p; !isspace((int)*p); p++) {
2844                 if (*p == '\0') {
2845                         do_aliases = false;
2846                         break;
2847                 }
2848         }
2849
2850         *p = '\0';
2851
2852         /* Convert to lowercase. This operate on same memory region */
2853         str_tolower(n, n);
2854         ed->ht.h_name = n;
2855
2856         /* glib's getent always dereferences he->h_aliases */
2857         ed->ht.h_aliases = malloc(sizeof(char *));
2858         if (ed->ht.h_aliases == NULL) {
2859                 free(ed);
2860                 return false;
2861         }
2862         ed->ht.h_aliases[0] = NULL;
2863
2864         /*
2865          * Aliases
2866          */
2867         while (do_aliases) {
2868                 char **aliases;
2869                 char *a;
2870
2871                 p++;
2872
2873                 /* Walk to first char */
2874                 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
2875                         if (*p == '\0') {
2876                                 do_aliases = false;
2877                                 break;
2878                         }
2879                 }
2880                 /* Only trailing spaces are left */
2881                 if (!do_aliases) {
2882                         break;
2883                 }
2884
2885                 for (a = p; !isspace((int)*p); p++) {
2886                         if (*p == '\0') {
2887                                 do_aliases = false;
2888                                 break;
2889                         }
2890                 }
2891
2892                 *p = '\0';
2893
2894                 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
2895                 if (aliases == NULL) {
2896                         free(ed);
2897                         return false;
2898                 }
2899                 ed->ht.h_aliases = aliases;
2900
2901                 str_tolower(a, a);
2902                 aliases[aliases_count] = a;
2903                 aliases[aliases_count + 1] = NULL;
2904
2905                 aliases_count += 1;
2906         }
2907
2908         ok = nwrap_vector_add_item(&(nwrap_he->entries), (void *const)ed);
2909         if (!ok) {
2910                 NWRAP_LOG(NWRAP_LOG_ERROR, "Unable to add entry to vector");
2911                 free(ed);
2912                 return false;
2913         }
2914
2915         ed->aliases_count = aliases_count;
2916         /* Inventarize item */
2917         ok = nwrap_add_hname(ed);
2918         if (!ok) {
2919                 return false;
2920         }
2921
2922         ok = nwrap_ed_inventarize(ip, ed);
2923         if (!ok) {
2924                 return false;
2925         }
2926
2927         nwrap_he->num++;
2928         return true;
2929 }
2930
2931 static void nwrap_he_unload(struct nwrap_cache *nwrap)
2932 {
2933         struct nwrap_he *nwrap_he =
2934                 (struct nwrap_he *)nwrap->private_data;
2935         struct nwrap_entdata *ed;
2936         struct nwrap_entlist *el;
2937         size_t i;
2938         int rc;
2939
2940         nwrap_vector_foreach (ed, nwrap_he->entries, i)
2941         {
2942                 SAFE_FREE(ed->nwrap_addrdata.items);
2943                 SAFE_FREE(ed->ht.h_aliases);
2944                 SAFE_FREE(ed);
2945         }
2946         SAFE_FREE(nwrap_he->entries.items);
2947         nwrap_he->entries.count = nwrap_he->entries.capacity = 0;
2948
2949         nwrap_vector_foreach(el, nwrap_he->lists, i)
2950         {
2951                 while (el != NULL) {
2952                         struct nwrap_entlist *el_next;
2953
2954                         el_next = el->next;
2955                         SAFE_FREE(el);
2956                         el = el_next;
2957                 }
2958         }
2959         SAFE_FREE(nwrap_he->lists.items);
2960         nwrap_he->lists.count = nwrap_he->lists.capacity = 0;
2961
2962         nwrap_he->num = 0;
2963         nwrap_he->idx = 0;
2964
2965         /*
2966          * If we unload the file, the pointers in the hash table point to
2967          * invalid memory. So we need to destroy the hash table and recreate
2968          * it.
2969          */
2970         hdestroy();
2971         rc = hcreate(max_hostents);
2972         if (rc == 0) {
2973                 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to initialize hash table");
2974                 exit(-1);
2975         }
2976 }
2977
2978
2979 /* user functions */
2980 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
2981                                            const char *name)
2982 {
2983         int i;
2984         bool ok;
2985
2986         (void) b; /* unused */
2987
2988         NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
2989
2990         ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
2991         if (!ok) {
2992                 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
2993                 return NULL;
2994         }
2995
2996         for (i=0; i<nwrap_pw_global.num; i++) {
2997                 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
2998                         NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
2999                         return &nwrap_pw_global.list[i];
3000                 }
3001                 NWRAP_LOG(NWRAP_LOG_DEBUG,
3002                           "user[%s] does not match [%s]",
3003                           name,
3004                           nwrap_pw_global.list[i].pw_name);
3005         }
3006
3007         NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3008
3009         errno = ENOENT;
3010         return NULL;
3011 }
3012
3013 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
3014                                   const char *name, struct passwd *pwdst,
3015                                   char *buf, size_t buflen, struct passwd **pwdstp)
3016 {
3017         struct passwd *pw;
3018
3019         pw = nwrap_files_getpwnam(b, name);
3020         if (!pw) {
3021                 if (errno == 0) {
3022                         return ENOENT;
3023                 }
3024                 return errno;
3025         }
3026
3027         return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3028 }
3029
3030 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
3031                                            uid_t uid)
3032 {
3033         int i;
3034         bool ok;
3035
3036         (void) b; /* unused */
3037
3038         ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3039         if (!ok) {
3040                 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3041                 return NULL;
3042         }
3043
3044         for (i=0; i<nwrap_pw_global.num; i++) {
3045                 if (nwrap_pw_global.list[i].pw_uid == uid) {
3046                         NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] found", uid);
3047                         return &nwrap_pw_global.list[i];
3048                 }
3049                 NWRAP_LOG(NWRAP_LOG_DEBUG,
3050                           "uid[%u] does not match [%u]",
3051                           uid,
3052                           nwrap_pw_global.list[i].pw_uid);
3053         }
3054
3055         NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
3056
3057         errno = ENOENT;
3058         return NULL;
3059 }
3060
3061 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
3062                                   uid_t uid, struct passwd *pwdst,
3063                                   char *buf, size_t buflen, struct passwd **pwdstp)
3064 {
3065         struct passwd *pw;
3066
3067         pw = nwrap_files_getpwuid(b, uid);
3068         if (!pw) {
3069                 if (errno == 0) {
3070                         return ENOENT;
3071                 }
3072                 return errno;
3073         }
3074
3075         return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3076 }
3077
3078 /* user enum functions */
3079 static void nwrap_files_setpwent(struct nwrap_backend *b)
3080 {
3081         (void) b; /* unused */
3082
3083         nwrap_pw_global.idx = 0;
3084 }
3085
3086 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
3087 {
3088         struct passwd *pw;
3089
3090         (void) b; /* unused */
3091
3092         if (nwrap_pw_global.idx == 0) {
3093                 bool ok;
3094                 ok = nwrap_files_cache_reload(nwrap_pw_global.cache);
3095                 if (!ok) {
3096                         NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading passwd file");
3097                         return NULL;
3098                 }
3099         }
3100
3101         if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
3102                 errno = ENOENT;
3103                 return NULL;
3104         }
3105
3106         pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
3107
3108         NWRAP_LOG(NWRAP_LOG_DEBUG,
3109                   "return user[%s] uid[%u]",
3110                   pw->pw_name, pw->pw_uid);
3111
3112         return pw;
3113 }
3114
3115 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
3116                                   struct passwd *pwdst, char *buf,
3117                                   size_t buflen, struct passwd **pwdstp)
3118 {
3119         struct passwd *pw;
3120
3121         pw = nwrap_files_getpwent(b);
3122         if (!pw) {
3123                 if (errno == 0) {
3124                         return ENOENT;
3125                 }
3126                 return errno;
3127         }
3128
3129         return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
3130 }
3131
3132 static void nwrap_files_endpwent(struct nwrap_backend *b)
3133 {
3134         (void) b; /* unused */
3135
3136         nwrap_pw_global.idx = 0;
3137 }
3138
3139 /* shadow */
3140
3141 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
3142
3143 #ifdef HAVE_SETSPENT
3144 static void nwrap_files_setspent(void)
3145 {
3146         nwrap_sp_global.idx = 0;
3147 }
3148
3149 static struct spwd *nwrap_files_getspent(void)
3150 {
3151         struct spwd *sp;
3152
3153         if (nwrap_sp_global.idx == 0) {
3154                 bool ok;
3155
3156                 ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3157                 if (!ok) {
3158                         NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3159                         return NULL;
3160                 }
3161         }
3162
3163         if (nwrap_sp_global.idx >= nwrap_sp_global.num) {
3164                 errno = ENOENT;
3165                 return NULL;
3166         }
3167
3168         sp = &nwrap_sp_global.list[nwrap_sp_global.idx++];
3169
3170         NWRAP_LOG(NWRAP_LOG_DEBUG,
3171                   "return user[%s]",
3172                   sp->sp_namp);
3173
3174         return sp;
3175 }
3176
3177 static void nwrap_files_endspent(void)
3178 {
3179         nwrap_sp_global.idx = 0;
3180 }
3181 #endif /* HAVE_SETSPENT */
3182
3183 static struct spwd *nwrap_files_getspnam(const char *name)
3184 {
3185         int i;
3186         bool ok;
3187
3188         NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
3189
3190         ok = nwrap_files_cache_reload(nwrap_sp_global.cache);
3191         if (!ok) {
3192                 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading shadow file");
3193                 return NULL;
3194         }
3195
3196         for (i=0; i<nwrap_sp_global.num; i++) {
3197                 if (strcmp(nwrap_sp_global.list[i].sp_namp, name) == 0) {
3198                         NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
3199                         return &nwrap_sp_global.list[i];
3200                 }
3201                 NWRAP_LOG(NWRAP_LOG_DEBUG,
3202                           "user[%s] does not match [%s]",
3203                           name,
3204                           nwrap_sp_global.list[i].sp_namp);
3205         }
3206
3207         NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
3208
3209         errno = ENOENT;
3210         return NULL;
3211 }
3212 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
3213
3214 /* misc functions */
3215 static int nwrap_files_initgroups(struct nwrap_backend *b,
3216                                   const char *user,
3217                                   gid_t group)
3218 {
3219         struct group *grp;
3220         gid_t *groups;
3221         int size = 1;
3222         int rc;
3223
3224         groups = (gid_t *)malloc(size * sizeof(gid_t));
3225         if (groups == NULL) {
3226                 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3227                 errno = ENOMEM;
3228                 return -1;
3229         }
3230         groups[0] = group;
3231
3232         nwrap_files_setgrent(b);
3233         while ((grp = nwrap_files_getgrent(b)) != NULL) {
3234                 int i = 0;
3235
3236                 NWRAP_LOG(NWRAP_LOG_DEBUG,
3237                           "Inspecting %s for group membership",
3238                           grp->gr_name);
3239
3240                 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
3241                         if (group != grp->gr_gid &&
3242                             (strcmp(user, grp->gr_mem[i]) == 0)) {
3243                                 NWRAP_LOG(NWRAP_LOG_DEBUG,
3244                                           "%s is member of %s",
3245                                           user,
3246                                           grp->gr_name);
3247
3248                                 groups = (gid_t *)realloc(groups,
3249                                                           (size + 1) * sizeof(gid_t));
3250                                 if (groups == NULL) {
3251                                         NWRAP_LOG(NWRAP_LOG_ERROR,
3252                                                   "Out of memory");
3253                                         errno = ENOMEM;
3254                                         return -1;
3255                                 }
3256
3257                                 groups[size] = grp->gr_gid;
3258                                 size++;
3259                         }
3260                 }
3261         }
3262
3263         nwrap_files_endgrent(b);
3264
3265         NWRAP_LOG(NWRAP_LOG_DEBUG,
3266                   "%s is member of %d groups",
3267                   user, size);
3268
3269         /* This really only works if uid_wrapper is loaded */
3270         rc = setgroups(size, groups);
3271
3272         free(groups);
3273
3274         return rc;
3275 }
3276
3277 /* group functions */
3278 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
3279                                           const char *name)
3280 {
3281         int i;
3282         bool ok;
3283
3284         (void) b; /* unused */
3285
3286         ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3287         if (!ok) {
3288                 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3289                 return NULL;
3290         }
3291
3292         for (i=0; i<nwrap_gr_global.num; i++) {
3293                 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
3294                         NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] found", name);
3295                         return &nwrap_gr_global.list[i];
3296                 }
3297                 NWRAP_LOG(NWRAP_LOG_DEBUG,
3298                           "group[%s] does not match [%s]",
3299                           name,
3300                           nwrap_gr_global.list[i].gr_name);
3301         }
3302
3303         NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
3304
3305         errno = ENOENT;
3306         return NULL;
3307 }
3308
3309 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
3310                                   const char *name, struct group *grdst,
3311                                   char *buf, size_t buflen, struct group **grdstp)
3312 {
3313         struct group *gr;
3314
3315         gr = nwrap_files_getgrnam(b, name);
3316         if (!gr) {
3317                 if (errno == 0) {
3318                         return ENOENT;
3319                 }
3320                 return errno;
3321         }
3322
3323         return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3324 }
3325
3326 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
3327                                           gid_t gid)
3328 {
3329         int i;
3330         bool ok;
3331
3332         (void) b; /* unused */
3333
3334         ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3335         if (!ok) {
3336                 NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3337                 return NULL;
3338         }
3339
3340         for (i=0; i<nwrap_gr_global.num; i++) {
3341                 if (nwrap_gr_global.list[i].gr_gid == gid) {
3342                         NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] found", gid);
3343                         return &nwrap_gr_global.list[i];
3344                 }
3345                 NWRAP_LOG(NWRAP_LOG_DEBUG,
3346                           "gid[%u] does not match [%u]",
3347                           gid,
3348                           nwrap_gr_global.list[i].gr_gid);
3349         }
3350
3351         NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
3352
3353         errno = ENOENT;
3354         return NULL;
3355 }
3356
3357 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
3358                                   gid_t gid, struct group *grdst,
3359                                   char *buf, size_t buflen, struct group **grdstp)
3360 {
3361         struct group *gr;
3362
3363         gr = nwrap_files_getgrgid(b, gid);
3364         if (!gr) {
3365                 if (errno == 0) {
3366                         return ENOENT;
3367                 }
3368                 return errno;
3369         }
3370
3371         return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3372 }
3373
3374 /* group enum functions */
3375 static void nwrap_files_setgrent(struct nwrap_backend *b)
3376 {
3377         (void) b; /* unused */
3378
3379         nwrap_gr_global.idx = 0;
3380 }
3381
3382 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
3383 {
3384         struct group *gr;
3385
3386         (void) b; /* unused */
3387
3388         if (nwrap_gr_global.idx == 0) {
3389                 bool ok;
3390
3391                 ok = nwrap_files_cache_reload(nwrap_gr_global.cache);
3392                 if (!ok) {
3393                         NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading group file");
3394                         return NULL;
3395                 }
3396         }
3397
3398         if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
3399                 errno = ENOENT;
3400                 return NULL;
3401         }
3402
3403         gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
3404
3405         NWRAP_LOG(NWRAP_LOG_DEBUG,
3406                   "return group[%s] gid[%u]",
3407                   gr->gr_name, gr->gr_gid);
3408
3409         return gr;
3410 }
3411
3412 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
3413                                   struct group *grdst, char *buf,
3414                                   size_t buflen, struct group **grdstp)
3415 {
3416         struct group *gr;
3417
3418         gr = nwrap_files_getgrent(b);
3419         if (!gr) {
3420                 if (errno == 0) {
3421                         return ENOENT;
3422                 }
3423                 return errno;
3424         }
3425
3426         return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
3427 }
3428
3429 static void nwrap_files_endgrent(struct nwrap_backend *b)
3430 {
3431         (void) b; /* unused */
3432
3433         nwrap_gr_global.idx = 0;
3434 }
3435
3436 /* hosts functions */
3437 static int nwrap_files_gethostbyname(const char *name, int af,
3438                                      struct hostent *result,
3439                                      struct nwrap_vector *addr_list)
3440 {
3441         struct nwrap_entlist *el;
3442         struct hostent *he;
3443         char *h_name_lower;
3444         ENTRY e;
3445         ENTRY *e_p;
3446         char canon_name[DNS_NAME_MAX] = { 0 };
3447         size_t name_len;
3448         bool he_found = false;
3449         bool ok;
3450
3451         ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3452         if (!ok) {
3453                 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3454                 goto no_ent;
3455         }
3456
3457         name_len = strlen(name);
3458         if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
3459                 strncpy(canon_name, name, name_len - 1);
3460                 name = canon_name;
3461         }
3462
3463         if (!str_tolower_copy(&h_name_lower, name)) {
3464                 NWRAP_LOG(NWRAP_LOG_DEBUG,
3465                           "Out of memory while converting to lower case");
3466                 goto no_ent;
3467         }
3468
3469         /* Look at hash table for element */
3470         NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3471         e.key = h_name_lower;
3472         e.data = NULL;
3473         e_p = hsearch(e, FIND);
3474         if (e_p == NULL) {
3475                 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3476                 SAFE_FREE(h_name_lower);
3477                 goto no_ent;
3478         }
3479         SAFE_FREE(h_name_lower);
3480
3481         /* Always cleanup vector and results */
3482         if (!nwrap_vector_is_initialized(addr_list)) {
3483                 if (!nwrap_vector_init(addr_list)) {
3484                         NWRAP_LOG(NWRAP_LOG_DEBUG,
3485                                   "Unable to initialize memory for addr_list vector");
3486                         goto no_ent;
3487                 }
3488         } else {
3489                 /* When vector is initialized data are valid no more.
3490                  * Quick way how to free vector is: */
3491                 addr_list->count = 0;
3492         }
3493
3494         /* Iterate through results */
3495         for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3496         {
3497                 he = &(el->ed->ht);
3498
3499                 /* Filter by address familiy if provided */
3500                 if (af != AF_UNSPEC && he->h_addrtype != af) {
3501                         continue;
3502                 }
3503
3504                 /*
3505                  * GLIBC HACK?
3506                  * glibc doesn't return ipv6 addresses when AF_UNSPEC is used
3507                  */
3508                 if (af == AF_UNSPEC && he->h_addrtype != AF_INET) {
3509                         continue;
3510                 }
3511
3512                 if (!he_found) {
3513                         memcpy(result, he, sizeof(struct hostent));
3514                         NWRAP_LOG(NWRAP_LOG_DEBUG,
3515                                   "Name found. Returning record for %s",
3516                                   he->h_name);
3517                         he_found = true;
3518                 }
3519                 nwrap_vector_merge(addr_list, &el->ed->nwrap_addrdata);
3520                 result->h_addr_list = nwrap_vector_head(addr_list);
3521         }
3522
3523         if (he_found) {
3524                 return 0;
3525         }
3526         NWRAP_LOG(NWRAP_LOG_DEBUG,
3527                   "Name found in database. No records matches type.");
3528
3529 no_ent:
3530         errno = ENOENT;
3531         return -1;
3532 }
3533
3534 #ifdef HAVE_GETHOSTBYNAME_R
3535 static int nwrap_gethostbyname_r(const char *name,
3536                                  struct hostent *ret,
3537                                  char *buf, size_t buflen,
3538                                  struct hostent **result, int *h_errnop)
3539 {
3540         struct nwrap_vector *addr_list = malloc(sizeof(struct nwrap_vector));
3541         int rc;
3542
3543         if (addr_list == NULL) {
3544                 NWRAP_LOG(NWRAP_LOG_ERROR,
3545                           "Unable to allocate memory for address list");
3546                 errno = ENOENT;
3547                 return -1;
3548         }
3549
3550         ZERO_STRUCTP(addr_list);
3551
3552         rc = nwrap_files_gethostbyname(name, AF_UNSPEC, ret, addr_list);
3553         if (rc == -1) {
3554                 *h_errnop = h_errno;
3555                 if (addr_list->items != NULL) {
3556                         free(addr_list->items);
3557                 }
3558                 SAFE_FREE(addr_list);
3559                 errno = ENOENT;
3560                 return -1;
3561         }
3562
3563         if (buflen < (addr_list->count * sizeof(void *))) {
3564                 SAFE_FREE(addr_list->items);
3565                 SAFE_FREE(addr_list);
3566                 return ERANGE;
3567         }
3568
3569         /* Copy all to user provided buffer and change
3570          * pointers in returned structure.
3571          * +1 is for ending NULL pointer. */
3572         memcpy(buf, addr_list->items, (addr_list->count + 1) * sizeof(void *));
3573
3574         free(addr_list->items);
3575         free(addr_list);
3576
3577         ret->h_addr_list = (char **)buf;
3578         *result = ret;
3579         return 0;
3580 }
3581
3582 int gethostbyname_r(const char *name,
3583                     struct hostent *ret,
3584                     char *buf, size_t buflen,
3585                     struct hostent **result, int *h_errnop)
3586 {
3587         if (!nss_wrapper_hosts_enabled()) {
3588                 return libc_gethostbyname_r(name,
3589                                             ret,
3590                                             buf,
3591                                             buflen,
3592                                             result,
3593                                             h_errnop);
3594         }
3595
3596         return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
3597 }
3598 #endif
3599
3600 static int nwrap_files_getaddrinfo(const char *name,
3601                                    unsigned short port,
3602                                    const struct addrinfo *hints,
3603                                    struct addrinfo **ai)
3604 {
3605         struct nwrap_entlist *el;
3606         struct hostent *he;
3607         struct addrinfo *ai_head = NULL;
3608         struct addrinfo *ai_cur = NULL;
3609         char *h_name_lower;
3610         size_t name_len;
3611         char canon_name[DNS_NAME_MAX] = { 0 };
3612         bool skip_canonname = false;
3613         ENTRY e = {
3614                 .key = NULL,
3615         };
3616         ENTRY *e_p = NULL;
3617         int rc;
3618         bool ok;
3619
3620         ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3621         if (!ok) {
3622                 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3623                 return EAI_SYSTEM;
3624         }
3625
3626         name_len = strlen(name);
3627         if (name_len < DNS_NAME_MAX && name[name_len - 1] == '.') {
3628                 strncpy(canon_name, name, name_len - 1);
3629                 name = canon_name;
3630         }
3631
3632         if (!str_tolower_copy(&h_name_lower, name)) {
3633                 NWRAP_LOG(NWRAP_LOG_DEBUG,
3634                           "Out of memory while converting to lower case");
3635                 return EAI_MEMORY;
3636         }
3637
3638         NWRAP_LOG(NWRAP_LOG_DEBUG, "Searching for name: %s", h_name_lower);
3639         e.key = h_name_lower;
3640         e.data = NULL;
3641         e_p = hsearch(e, FIND);
3642         if (e_p == NULL) {
3643                 NWRAP_LOG(NWRAP_LOG_DEBUG, "Name %s not found.", h_name_lower);
3644                 SAFE_FREE(h_name_lower);
3645                 errno = ENOENT;
3646                 return EAI_NONAME;
3647         }
3648         NWRAP_LOG(NWRAP_LOG_DEBUG, "Name: %s found.", h_name_lower);
3649         SAFE_FREE(h_name_lower);
3650
3651         rc = EAI_NONAME;
3652         for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
3653         {
3654                 int rc2;
3655                 struct addrinfo *ai_new = NULL;
3656
3657                 he = &(el->ed->ht);
3658
3659                 if (hints->ai_family != AF_UNSPEC &&
3660                     he->h_addrtype != hints->ai_family)
3661                 {
3662                         NWRAP_LOG(NWRAP_LOG_DEBUG,
3663                                   "Entry found but with wrong AF - "
3664                                   "remembering EAI_ADDRINFO.");
3665                         rc = EAI_ADDRFAMILY;
3666                         continue;
3667                 }
3668
3669                 /* Function allocates memory and returns it in ai. */
3670                 rc2 = nwrap_convert_he_ai(he,
3671                                          port,
3672                                          hints,
3673                                          &ai_new,
3674                                          skip_canonname);
3675                 if (rc2 != 0) {
3676                         NWRAP_LOG(NWRAP_LOG_ERROR, "Error converting he to ai");
3677                         if (ai_head != NULL) {
3678                                 freeaddrinfo(ai_head);
3679                         }
3680                         return rc2;
3681                 }
3682                 skip_canonname = true;
3683
3684                 if (ai_head == NULL) {
3685                         ai_head = ai_new;
3686                 }
3687                 if (ai_cur != NULL) {
3688                         ai_cur->ai_next = ai_new;
3689                 }
3690                 ai_cur = ai_new;
3691         }
3692
3693         if (ai_head != NULL) {
3694                 rc = 0;
3695         }
3696
3697         *ai = ai_head;
3698
3699         return rc;
3700 }
3701
3702 static struct hostent *nwrap_files_gethostbyaddr(const void *addr,
3703                                                  socklen_t len, int type)
3704 {
3705         struct hostent *he;
3706         char ip[NWRAP_INET_ADDRSTRLEN] = {0};
3707         struct nwrap_entdata *ed;
3708         const char *a;
3709         size_t i;
3710         bool ok;
3711
3712         (void) len; /* unused */
3713
3714         ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3715         if (!ok) {
3716                 NWRAP_LOG(NWRAP_LOG_ERROR, "error loading hosts file");
3717                 return NULL;
3718         }
3719
3720         a = inet_ntop(type, addr, ip, sizeof(ip));
3721         if (a == NULL) {
3722                 errno = EINVAL;
3723                 return NULL;
3724         }
3725
3726         nwrap_vector_foreach(ed, nwrap_he_global.entries, i)
3727         {
3728                 he = &(ed->ht);
3729                 if (he->h_addrtype != type) {
3730                         continue;
3731                 }
3732
3733                 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
3734                         return he;
3735                 }
3736         }
3737
3738         errno = ENOENT;
3739         return NULL;
3740 }
3741
3742 #ifdef HAVE_GETHOSTBYADDR_R
3743 static int nwrap_gethostbyaddr_r(const void *addr, socklen_t len, int type,
3744                                  struct hostent *ret,
3745                                  char *buf, size_t buflen,
3746                                  struct hostent **result, int *h_errnop)
3747 {
3748         *result = nwrap_files_gethostbyaddr(addr, len, type);
3749         if (*result != NULL) {
3750                 memset(buf, '\0', buflen);
3751                 *ret = **result;
3752                 return 0;
3753         } else {
3754                 *h_errnop = h_errno;
3755                 return -1;
3756         }
3757 }
3758
3759 int gethostbyaddr_r(const void *addr, socklen_t len, int type,
3760                     struct hostent *ret,
3761                     char *buf, size_t buflen,
3762                     struct hostent **result, int *h_errnop)
3763 {
3764         if (!nss_wrapper_hosts_enabled()) {
3765                 return libc_gethostbyaddr_r(addr,
3766                                             len,
3767                                             type,
3768                                             ret,
3769                                             buf,
3770                                             buflen,
3771                                             result,
3772                                             h_errnop);
3773         }
3774
3775         return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
3776 }
3777 #endif
3778
3779 /* hosts enum functions */
3780 static void nwrap_files_sethostent(void)
3781 {
3782         nwrap_he_global.idx = 0;
3783 }
3784
3785 static struct hostent *nwrap_files_gethostent(void)
3786 {
3787         struct hostent *he;
3788
3789         if (nwrap_he_global.idx == 0) {
3790                 bool ok;
3791
3792                 ok = nwrap_files_cache_reload(nwrap_he_global.cache);
3793                 if (!ok) {
3794                         NWRAP_LOG(NWRAP_LOG_ERROR, "Error loading hosts file");
3795                         return NULL;
3796                 }
3797         }
3798
3799         if (nwrap_he_global.idx >= nwrap_he_global.num) {
3800                 errno = ENOENT;
3801                 return NULL;
3802         }
3803
3804         he = &((struct nwrap_entdata *)nwrap_he_global.entries.items[nwrap_he_global.idx++])->ht;
3805
3806         NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
3807
3808         return he;
3809 }
3810
3811 static void nwrap_files_endhostent(void)
3812 {
3813         nwrap_he_global.idx = 0;
3814 }
3815
3816 /*
3817  * module backend
3818  */
3819
3820
3821 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
3822                                             const char *name)
3823 {
3824         static struct passwd pwd;
3825         static char buf[1000];
3826         NSS_STATUS status;
3827
3828         if (!b->fns->_nss_getpwnam_r) {
3829                 return NULL;
3830         }
3831
3832         status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
3833         if (status == NSS_STATUS_NOTFOUND) {
3834                 return NULL;
3835         }
3836         if (status != NSS_STATUS_SUCCESS) {
3837                 return NULL;
3838         }
3839
3840         return &pwd;
3841 }
3842
3843 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
3844                                    const char *name, struct passwd *pwdst,
3845                                    char *buf, size_t buflen, struct passwd **pwdstp)
3846 {
3847         int ret;
3848
3849         *pwdstp = NULL;
3850
3851         if (!b->fns->_nss_getpwnam_r) {
3852                 return NSS_STATUS_NOTFOUND;
3853         }
3854
3855         ret = b->fns->_nss_getpwnam_r(name, pwdst, buf, buflen, &errno);
3856         switch (ret) {
3857         case NSS_STATUS_SUCCESS:
3858                 *pwdstp = pwdst;
3859                 return 0;
3860         case NSS_STATUS_NOTFOUND:
3861                 if (errno != 0) {
3862                         return errno;
3863                 }
3864                 return ENOENT;
3865         case NSS_STATUS_TRYAGAIN:
3866                 if (errno != 0) {
3867                         return errno;
3868                 }
3869                 return ERANGE;
3870         default:
3871                 if (errno != 0) {
3872                         return errno;
3873                 }
3874                 return ret;
3875         }
3876 }
3877
3878 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
3879                                             uid_t uid)
3880 {
3881         static struct passwd pwd;
3882         static char buf[1000];
3883         NSS_STATUS status;
3884
3885         if (!b->fns->_nss_getpwuid_r) {
3886                 return NULL;
3887         }
3888
3889         status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
3890         if (status == NSS_STATUS_NOTFOUND) {
3891                 return NULL;
3892         }
3893         if (status != NSS_STATUS_SUCCESS) {
3894                 return NULL;
3895         }
3896         return &pwd;
3897 }
3898
3899 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
3900                                    uid_t uid, struct passwd *pwdst,
3901                                    char *buf, size_t buflen, struct passwd **pwdstp)
3902 {
3903         int ret;
3904
3905         *pwdstp = NULL;
3906
3907         if (!b->fns->_nss_getpwuid_r) {
3908                 return ENOENT;
3909         }
3910
3911         ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
3912         switch (ret) {
3913         case NSS_STATUS_SUCCESS:
3914                 *pwdstp = pwdst;
3915                 return 0;
3916         case NSS_STATUS_NOTFOUND:
3917                 if (errno != 0) {
3918                         return errno;
3919                 }
3920                 return ENOENT;
3921         case NSS_STATUS_TRYAGAIN:
3922                 if (errno != 0) {
3923                         return errno;
3924                 }
3925                 return ERANGE;
3926         default:
3927                 if (errno != 0) {
3928                         return errno;
3929                 }
3930                 return ret;
3931         }
3932 }
3933
3934 static void nwrap_module_setpwent(struct nwrap_backend *b)
3935 {
3936         if (!b->fns->_nss_setpwent) {
3937                 return;
3938         }
3939
3940         b->fns->_nss_setpwent();
3941 }
3942
3943 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
3944 {
3945         static struct passwd pwd;
3946         static char buf[1000];
3947         NSS_STATUS status;
3948
3949         if (!b->fns->_nss_getpwent_r) {
3950                 return NULL;
3951         }
3952
3953         status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
3954         if (status == NSS_STATUS_NOTFOUND) {
3955                 return NULL;
3956         }
3957         if (status != NSS_STATUS_SUCCESS) {
3958                 return NULL;
3959         }
3960         return &pwd;
3961 }
3962
3963 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
3964                                    struct passwd *pwdst, char *buf,
3965                                    size_t buflen, struct passwd **pwdstp)
3966 {
3967         int ret;
3968
3969         (void) pwdstp; /* unused */
3970
3971         if (!b->fns->_nss_getpwent_r) {
3972                 return ENOENT;
3973         }
3974
3975         ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
3976         switch (ret) {
3977         case NSS_STATUS_SUCCESS:
3978                 return 0;
3979         case NSS_STATUS_NOTFOUND:
3980                 if (errno != 0) {
3981                         return errno;
3982                 }
3983                 return ENOENT;
3984         case NSS_STATUS_TRYAGAIN:
3985                 if (errno != 0) {
3986                         return errno;
3987                 }
3988                 return ERANGE;
3989         default:
3990                 if (errno != 0) {
3991                         return errno;
3992                 }
3993                 return ret;
3994         }
3995 }
3996
3997 static void nwrap_module_endpwent(struct nwrap_backend *b)
3998 {
3999         if (!b->fns->_nss_endpwent) {
4000                 return;
4001         }
4002
4003         b->fns->_nss_endpwent();
4004 }
4005
4006 static int nwrap_module_initgroups(struct nwrap_backend *b,
4007                                    const char *user, gid_t group)
4008 {
4009         gid_t *groups;
4010         long int start;
4011         long int size;
4012
4013         if (!b->fns->_nss_initgroups) {
4014                 return NSS_STATUS_UNAVAIL;
4015         }
4016
4017         return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
4018 }
4019
4020 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
4021                                            const char *name)
4022 {
4023         static struct group grp;
4024         static char *buf;
4025         static int buflen = 1000;
4026         NSS_STATUS status;
4027
4028         if (!b->fns->_nss_getgrnam_r) {
4029                 return NULL;
4030         }
4031
4032         if (!buf) {
4033                 buf = (char *)malloc(buflen);
4034         }
4035 again:
4036         status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
4037         if (status == NSS_STATUS_TRYAGAIN) {
4038                 buflen *= 2;
4039                 buf = (char *)realloc(buf, buflen);
4040                 if (!buf) {
4041                         return NULL;
4042                 }
4043                 goto again;
4044         }
4045         if (status == NSS_STATUS_NOTFOUND) {
4046                 SAFE_FREE(buf);
4047                 return NULL;
4048         }
4049         if (status != NSS_STATUS_SUCCESS) {
4050                 SAFE_FREE(buf);
4051                 return NULL;
4052         }
4053         return &grp;
4054 }
4055
4056 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
4057                                    const char *name, struct group *grdst,
4058                                    char *buf, size_t buflen, struct group **grdstp)
4059 {
4060         int ret;
4061
4062         (void) grdstp; /* unused */
4063
4064         if (!b->fns->_nss_getgrnam_r) {
4065                 return ENOENT;
4066         }
4067
4068         ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
4069         switch (ret) {
4070         case NSS_STATUS_SUCCESS:
4071                 return 0;
4072         case NSS_STATUS_NOTFOUND:
4073                 if (errno != 0) {
4074                         return errno;
4075                 }
4076                 return ENOENT;
4077         case NSS_STATUS_TRYAGAIN:
4078                 if (errno != 0) {
4079                         return errno;
4080                 }
4081                 return ERANGE;
4082         default:
4083                 if (errno != 0) {
4084                         return errno;
4085                 }
4086                 return ret;
4087         }
4088 }
4089
4090 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
4091                                            gid_t gid)
4092 {
4093         static struct group grp;
4094         static char *buf;
4095         static int buflen = 1000;
4096         NSS_STATUS status;
4097
4098         if (!b->fns->_nss_getgrgid_r) {
4099                 return NULL;
4100         }
4101
4102         if (!buf) {
4103                 buf = (char *)malloc(buflen);
4104         }
4105
4106 again:
4107         status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
4108         if (status == NSS_STATUS_TRYAGAIN) {
4109                 buflen *= 2;
4110                 buf = (char *)realloc(buf, buflen);
4111                 if (!buf) {
4112                         return NULL;
4113                 }
4114                 goto again;
4115         }
4116         if (status == NSS_STATUS_NOTFOUND) {
4117                 SAFE_FREE(buf);
4118                 return NULL;
4119         }
4120         if (status != NSS_STATUS_SUCCESS) {
4121                 SAFE_FREE(buf);
4122                 return NULL;
4123         }
4124         return &grp;
4125 }
4126
4127 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
4128                                    gid_t gid, struct group *grdst,
4129                                    char *buf, size_t buflen, struct group **grdstp)
4130 {
4131         int ret;
4132
4133         (void) grdstp; /* unused */
4134
4135         if (!b->fns->_nss_getgrgid_r) {
4136                 return ENOENT;
4137         }
4138
4139         ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
4140         switch (ret) {
4141         case NSS_STATUS_SUCCESS:
4142                 return 0;
4143         case NSS_STATUS_NOTFOUND:
4144                 if (errno != 0) {
4145                         return errno;
4146                 }
4147                 return ENOENT;
4148         case NSS_STATUS_TRYAGAIN:
4149                 if (errno != 0) {
4150                         return errno;
4151                 }
4152                 return ERANGE;
4153         default:
4154                 if (errno != 0) {
4155                         return errno;
4156                 }
4157                 return ret;
4158         }
4159 }
4160
4161 static void nwrap_module_setgrent(struct nwrap_backend *b)
4162 {
4163         if (!b->fns->_nss_setgrent) {
4164                 return;
4165         }
4166
4167         b->fns->_nss_setgrent();
4168 }
4169
4170 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
4171 {
4172         static struct group grp;
4173         static char *buf;
4174         static int buflen = 1024;
4175         NSS_STATUS status;
4176
4177         if (!b->fns->_nss_getgrent_r) {
4178                 return NULL;
4179         }
4180
4181         if (!buf) {
4182                 buf = (char *)malloc(buflen);
4183         }
4184
4185 again:
4186         status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
4187         if (status == NSS_STATUS_TRYAGAIN) {
4188                 buflen *= 2;
4189                 buf = (char *)realloc(buf, buflen);
4190                 if (!buf) {
4191                         return NULL;
4192                 }
4193                 goto again;
4194         }
4195         if (status == NSS_STATUS_NOTFOUND) {
4196                 SAFE_FREE(buf);
4197                 return NULL;
4198         }
4199         if (status != NSS_STATUS_SUCCESS) {
4200                 SAFE_FREE(buf);
4201                 return NULL;
4202         }
4203         return &grp;
4204 }
4205
4206 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
4207                                    struct group *grdst, char *buf,
4208                                    size_t buflen, struct group **grdstp)
4209 {
4210         int ret;
4211
4212         (void) grdstp; /* unused */
4213
4214         if (!b->fns->_nss_getgrent_r) {
4215                 return ENOENT;
4216         }
4217
4218         ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
4219         switch (ret) {
4220         case NSS_STATUS_SUCCESS:
4221                 return 0;
4222         case NSS_STATUS_NOTFOUND:
4223                 if (errno != 0) {
4224                         return errno;
4225                 }
4226                 return ENOENT;
4227         case NSS_STATUS_TRYAGAIN:
4228                 if (errno != 0) {
4229                         return errno;
4230                 }
4231                 return ERANGE;
4232         default:
4233                 if (errno != 0) {
4234                         return errno;
4235                 }
4236                 return ret;
4237         }
4238 }
4239
4240 static void nwrap_module_endgrent(struct nwrap_backend *b)
4241 {
4242         if (!b->fns->_nss_endgrent) {
4243                 return;
4244         }
4245
4246         b->fns->_nss_endgrent();
4247 }
4248
4249 /****************************************************************************
4250  *   GETPWNAM
4251  ***************************************************************************/
4252
4253 static struct passwd *nwrap_getpwnam(const char *name)
4254 {
4255         int i;
4256         struct passwd *pwd;
4257
4258         for (i=0; i < nwrap_main_global->num_backends; i++) {
4259                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4260                 pwd = b->ops->nw_getpwnam(b, name);
4261                 if (pwd) {
4262                         return pwd;
4263                 }
4264         }
4265
4266         return NULL;
4267 }
4268
4269 struct passwd *getpwnam(const char *name)
4270 {
4271         if (!nss_wrapper_enabled()) {
4272                 return libc_getpwnam(name);
4273         }
4274
4275         return nwrap_getpwnam(name);
4276 }
4277
4278 /****************************************************************************
4279  *   GETPWNAM_R
4280  ***************************************************************************/
4281
4282 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
4283                             char *buf, size_t buflen, struct passwd **pwdstp)
4284 {
4285         int i,ret;
4286
4287         for (i=0; i < nwrap_main_global->num_backends; i++) {
4288                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4289                 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
4290                 if (ret == ENOENT) {
4291                         continue;
4292                 }
4293                 return ret;
4294         }
4295
4296         return ENOENT;
4297 }
4298
4299 #ifdef HAVE_GETPWNAM_R
4300 # ifdef HAVE_SOLARIS_GETPWNAM_R
4301 int getpwnam_r(const char *name, struct passwd *pwdst,
4302                char *buf, int buflen, struct passwd **pwdstp)
4303 # else /* HAVE_SOLARIS_GETPWNAM_R */
4304 int getpwnam_r(const char *name, struct passwd *pwdst,
4305                char *buf, size_t buflen, struct passwd **pwdstp)
4306 # endif /* HAVE_SOLARIS_GETPWNAM_R */
4307 {
4308         if (!nss_wrapper_enabled()) {
4309                 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4310         }
4311
4312         return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4313 }
4314 #endif
4315
4316 /****************************************************************************
4317  *   GETPWUID
4318  ***************************************************************************/
4319
4320 static struct passwd *nwrap_getpwuid(uid_t uid)
4321 {
4322         int i;
4323         struct passwd *pwd;
4324
4325         for (i=0; i < nwrap_main_global->num_backends; i++) {
4326                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4327                 pwd = b->ops->nw_getpwuid(b, uid);
4328                 if (pwd) {
4329                         return pwd;
4330                 }
4331         }
4332
4333         return NULL;
4334 }
4335
4336 struct passwd *getpwuid(uid_t uid)
4337 {
4338         if (!nss_wrapper_enabled()) {
4339                 return libc_getpwuid(uid);
4340         }
4341
4342         return nwrap_getpwuid(uid);
4343 }
4344
4345 /****************************************************************************
4346  *   GETPWUID_R
4347  ***************************************************************************/
4348
4349 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
4350                             char *buf, size_t buflen, struct passwd **pwdstp)
4351 {
4352         int i,ret;
4353
4354         for (i=0; i < nwrap_main_global->num_backends; i++) {
4355                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4356                 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
4357                 if (ret == ENOENT) {
4358                         continue;
4359                 }
4360                 return ret;
4361         }
4362
4363         return ENOENT;
4364 }
4365
4366 #ifdef HAVE_SOLARIS_GETPWUID_R
4367 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4368                char *buf, int buflen, struct passwd **pwdstp)
4369 #else
4370 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4371                char *buf, size_t buflen, struct passwd **pwdstp)
4372 #endif
4373 {
4374         if (!nss_wrapper_enabled()) {
4375                 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4376         }
4377
4378         return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4379 }
4380
4381 /****************************************************************************
4382  *   SETPWENT
4383  ***************************************************************************/
4384
4385 static void nwrap_setpwent(void)
4386 {
4387         int i;
4388
4389         for (i=0; i < nwrap_main_global->num_backends; i++) {
4390                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4391                 b->ops->nw_setpwent(b);
4392         }
4393 }
4394
4395 void setpwent(void)
4396 {
4397         if (!nss_wrapper_enabled()) {
4398                 libc_setpwent();
4399                 return;
4400         }
4401
4402         nwrap_setpwent();
4403 }
4404
4405 /****************************************************************************
4406  *   GETPWENT
4407  ***************************************************************************/
4408
4409 static struct passwd *nwrap_getpwent(void)
4410 {
4411         int i;
4412         struct passwd *pwd;
4413
4414         for (i=0; i < nwrap_main_global->num_backends; i++) {
4415                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4416                 pwd = b->ops->nw_getpwent(b);
4417                 if (pwd) {
4418                         return pwd;
4419                 }
4420         }
4421
4422         return NULL;
4423 }
4424
4425 struct passwd *getpwent(void)
4426 {
4427         if (!nss_wrapper_enabled()) {
4428                 return libc_getpwent();
4429         }
4430
4431         return nwrap_getpwent();
4432 }
4433
4434 /****************************************************************************
4435  *   GETPWENT_R
4436  ***************************************************************************/
4437
4438 #ifdef HAVE_GETPWENT_R
4439 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
4440                             size_t buflen, struct passwd **pwdstp)
4441 {
4442         int i,ret;
4443
4444         for (i=0; i < nwrap_main_global->num_backends; i++) {
4445                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4446                 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
4447                 if (ret == ENOENT) {
4448                         continue;
4449                 }
4450                 return ret;
4451         }
4452
4453         return ENOENT;
4454 }
4455
4456 #  ifdef HAVE_SOLARIS_GETPWENT_R
4457 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
4458 {
4459         struct passwd *pwdstp = NULL;
4460         int rc;
4461
4462         if (!nss_wrapper_enabled()) {
4463                 return libc_getpwent_r(pwdst, buf, buflen);
4464         }
4465         rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
4466         if (rc < 0) {
4467                 return NULL;
4468         }
4469
4470         return pwdstp;
4471 }
4472 #  else /* HAVE_SOLARIS_GETPWENT_R */
4473 int getpwent_r(struct passwd *pwdst, char *buf,
4474                size_t buflen, struct passwd **pwdstp)
4475 {
4476         if (!nss_wrapper_enabled()) {
4477                 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
4478         }
4479
4480         return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
4481 }
4482 #  endif /* HAVE_SOLARIS_GETPWENT_R */
4483 #endif /* HAVE_GETPWENT_R */
4484
4485 /****************************************************************************
4486  *   ENDPWENT
4487  ***************************************************************************/
4488
4489 static void nwrap_endpwent(void)
4490 {
4491         int i;
4492
4493         for (i=0; i < nwrap_main_global->num_backends; i++) {
4494                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4495                 b->ops->nw_endpwent(b);
4496         }
4497 }
4498
4499 void endpwent(void)
4500 {
4501         if (!nss_wrapper_enabled()) {
4502                 libc_endpwent();
4503                 return;
4504         }
4505
4506         nwrap_endpwent();
4507 }
4508
4509 /****************************************************************************
4510  *   INITGROUPS
4511  ***************************************************************************/
4512
4513 static int nwrap_initgroups(const char *user, gid_t group)
4514 {
4515         int i;
4516
4517         for (i=0; i < nwrap_main_global->num_backends; i++) {
4518                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4519                 int rc;
4520
4521                 rc = b->ops->nw_initgroups(b, user, group);
4522                 if (rc == 0) {
4523                         return 0;
4524                 }
4525         }
4526
4527         errno = ENOENT;
4528         return -1;
4529 }
4530
4531 int initgroups(const char *user, gid_t group)
4532 {
4533         if (!nss_wrapper_enabled()) {
4534                 return libc_initgroups(user, group);
4535         }
4536
4537         return nwrap_initgroups(user, group);
4538 }
4539
4540 /****************************************************************************
4541  *   GETGRNAM
4542  ***************************************************************************/
4543
4544 static struct group *nwrap_getgrnam(const char *name)
4545 {
4546         int i;
4547         struct group *grp;
4548
4549         for (i=0; i < nwrap_main_global->num_backends; i++) {
4550                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4551                 grp = b->ops->nw_getgrnam(b, name);
4552                 if (grp) {
4553                         return grp;
4554                 }
4555         }
4556
4557         return NULL;
4558 }
4559
4560 struct group *getgrnam(const char *name)
4561 {
4562         if (!nss_wrapper_enabled()) {
4563                 return libc_getgrnam(name);
4564         }
4565
4566         return nwrap_getgrnam(name);
4567 }
4568
4569 /****************************************************************************
4570  *   GETGRNAM_R
4571  ***************************************************************************/
4572
4573 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
4574                             char *buf, size_t buflen, struct group **grdstp)
4575 {
4576         int i, ret;
4577
4578         for (i=0; i < nwrap_main_global->num_backends; i++) {
4579                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4580                 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
4581                 if (ret == ENOENT) {
4582                         continue;
4583                 }
4584                 return ret;
4585         }
4586
4587         return ENOENT;
4588 }
4589
4590 #ifdef HAVE_GETGRNAM_R
4591 # ifdef HAVE_SOLARIS_GETGRNAM_R
4592 int getgrnam_r(const char *name, struct group *grp,
4593                 char *buf, int buflen, struct group **pgrp)
4594 # else /* HAVE_SOLARIS_GETGRNAM_R */
4595 int getgrnam_r(const char *name, struct group *grp,
4596                char *buf, size_t buflen, struct group **pgrp)
4597 # endif /* HAVE_SOLARIS_GETGRNAM_R */
4598 {
4599         if (!nss_wrapper_enabled()) {
4600                 return libc_getgrnam_r(name,
4601                                        grp,
4602                                        buf,
4603                                        buflen,
4604                                        pgrp);
4605         }
4606
4607         return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
4608 }
4609 #endif /* HAVE_GETGRNAM_R */
4610
4611 /****************************************************************************
4612  *   GETGRGID
4613  ***************************************************************************/
4614
4615 static struct group *nwrap_getgrgid(gid_t gid)
4616 {
4617         int i;
4618         struct group *grp;
4619
4620         for (i=0; i < nwrap_main_global->num_backends; i++) {
4621                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4622                 grp = b->ops->nw_getgrgid(b, gid);
4623                 if (grp) {
4624                         return grp;
4625                 }
4626         }
4627
4628         return NULL;
4629 }
4630
4631 struct group *getgrgid(gid_t gid)
4632 {
4633         if (!nss_wrapper_enabled()) {
4634                 return libc_getgrgid(gid);
4635         }
4636
4637         return nwrap_getgrgid(gid);
4638 }
4639
4640 /****************************************************************************
4641  *   GETGRGID_R
4642  ***************************************************************************/
4643
4644 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
4645                             char *buf, size_t buflen, struct group **grdstp)
4646 {
4647         int i,ret;
4648
4649         for (i=0; i < nwrap_main_global->num_backends; i++) {
4650                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4651                 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
4652                 if (ret == ENOENT) {
4653                         continue;
4654                 }
4655                 return ret;
4656         }
4657
4658         return ENOENT;
4659 }
4660
4661 #ifdef HAVE_GETGRGID_R
4662 # ifdef HAVE_SOLARIS_GETGRGID_R
4663 int getgrgid_r(gid_t gid, struct group *grdst,
4664                char *buf, int buflen, struct group **grdstp)
4665 # else /* HAVE_SOLARIS_GETGRGID_R */
4666 int getgrgid_r(gid_t gid, struct group *grdst,
4667                char *buf, size_t buflen, struct group **grdstp)
4668 # endif /* HAVE_SOLARIS_GETGRGID_R */
4669 {
4670         if (!nss_wrapper_enabled()) {
4671                 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4672         }
4673
4674         return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4675 }
4676 #endif
4677
4678 /****************************************************************************
4679  *   SETGRENT
4680  ***************************************************************************/
4681
4682 static void nwrap_setgrent(void)
4683 {
4684         int i;
4685
4686         for (i=0; i < nwrap_main_global->num_backends; i++) {
4687                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4688                 b->ops->nw_setgrent(b);
4689         }
4690 }
4691
4692 #ifdef HAVE_BSD_SETGRENT
4693 int setgrent(void)
4694 #else
4695 void setgrent(void)
4696 #endif
4697 {
4698         if (!nss_wrapper_enabled()) {
4699                 libc_setgrent();
4700                 goto out;
4701         }
4702
4703         nwrap_setgrent();
4704
4705 out:
4706 #ifdef HAVE_BSD_SETGRENT
4707         return 0;
4708 #else
4709         return;
4710 #endif
4711 }
4712
4713 /****************************************************************************
4714  *   GETGRENT
4715  ***************************************************************************/
4716
4717 static struct group *nwrap_getgrent(void)
4718 {
4719         int i;
4720         struct group *grp;
4721
4722         for (i=0; i < nwrap_main_global->num_backends; i++) {
4723                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4724                 grp = b->ops->nw_getgrent(b);
4725                 if (grp) {
4726                         return grp;
4727                 }
4728         }
4729
4730         return NULL;
4731 }
4732
4733 struct group *getgrent(void)
4734 {
4735         if (!nss_wrapper_enabled()) {
4736                 return libc_getgrent();
4737         }
4738
4739         return nwrap_getgrent();
4740 }
4741
4742 /****************************************************************************
4743  *   GETGRENT_R
4744  ***************************************************************************/
4745
4746 #ifdef HAVE_GETGRENT_R
4747 static int nwrap_getgrent_r(struct group *grdst, char *buf,
4748                             size_t buflen, struct group **grdstp)
4749 {
4750         int i,ret;
4751
4752         for (i=0; i < nwrap_main_global->num_backends; i++) {
4753                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4754                 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
4755                 if (ret == ENOENT) {
4756                         continue;
4757                 }
4758                 return ret;
4759         }
4760
4761         return ENOENT;
4762 }
4763
4764 #  ifdef HAVE_SOLARIS_GETGRENT_R
4765 struct group *getgrent_r(struct group *src, char *buf, int buflen)
4766 {
4767         struct group *grdstp = NULL;
4768         int rc;
4769
4770         if (!nss_wrapper_enabled()) {
4771                 return libc_getgrent_r(src, buf, buflen);
4772         }
4773
4774         rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
4775         if (rc < 0) {
4776                 return NULL;
4777         }
4778
4779         return grdstp;
4780 }
4781 #  else /* HAVE_SOLARIS_GETGRENT_R */
4782 int getgrent_r(struct group *src, char *buf,
4783                size_t buflen, struct group **grdstp)
4784 {
4785         if (!nss_wrapper_enabled()) {
4786                 return libc_getgrent_r(src, buf, buflen, grdstp);
4787         }
4788
4789         return nwrap_getgrent_r(src, buf, buflen, grdstp);
4790 }
4791 #  endif /* HAVE_SOLARIS_GETGRENT_R */
4792 #endif /* HAVE_GETGRENT_R */
4793
4794 /****************************************************************************
4795  *   ENDGRENT
4796  ***************************************************************************/
4797
4798 static void nwrap_endgrent(void)
4799 {
4800         int i;
4801
4802         for (i=0; i < nwrap_main_global->num_backends; i++) {
4803                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4804                 b->ops->nw_endgrent(b);
4805         }
4806 }
4807
4808 void endgrent(void)
4809 {
4810         if (!nss_wrapper_enabled()) {
4811                 libc_endgrent();
4812                 return;
4813         }
4814
4815         nwrap_endgrent();
4816 }
4817
4818 /****************************************************************************
4819  *   GETGROUPLIST
4820  ***************************************************************************/
4821
4822 #ifdef HAVE_GETGROUPLIST
4823 static int nwrap_getgrouplist(const char *user, gid_t group,
4824                               gid_t *groups, int *ngroups)
4825 {
4826         struct group *grp;
4827         gid_t *groups_tmp;
4828         int count = 1;
4829
4830         NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
4831
4832         groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
4833         if (!groups_tmp) {
4834                 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
4835                 errno = ENOMEM;
4836                 return -1;
4837         }
4838         groups_tmp[0] = group;
4839
4840         nwrap_setgrent();
4841         while ((grp = nwrap_getgrent()) != NULL) {
4842                 int i = 0;
4843
4844                 NWRAP_LOG(NWRAP_LOG_DEBUG,
4845                           "Inspecting %s for group membership",
4846                           grp->gr_name);
4847
4848                 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
4849
4850                         if (group != grp->gr_gid &&
4851                             (strcmp(user, grp->gr_mem[i]) == 0)) {
4852
4853                                 NWRAP_LOG(NWRAP_LOG_DEBUG,
4854                                           "%s is member of %s",
4855                                           user,
4856                                           grp->gr_name);
4857
4858                                 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
4859                                 if (!groups_tmp) {
4860                                         NWRAP_LOG(NWRAP_LOG_ERROR,
4861                                                   "Out of memory");
4862                                         errno = ENOMEM;
4863                                         return -1;
4864                                 }
4865                                 groups_tmp[count] = grp->gr_gid;
4866
4867                                 count++;
4868                         }
4869                 }
4870         }
4871
4872         nwrap_endgrent();
4873
4874         NWRAP_LOG(NWRAP_LOG_DEBUG,
4875                   "%s is member of %d groups",
4876                   user, *ngroups);
4877
4878         if (*ngroups < count) {
4879                 *ngroups = count;
4880                 free(groups_tmp);
4881                 return -1;
4882         }
4883
4884         *ngroups = count;
4885         memcpy(groups, groups_tmp, count * sizeof(gid_t));
4886         free(groups_tmp);
4887
4888         return count;
4889 }
4890
4891 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
4892 {
4893         if (!nss_wrapper_enabled()) {
4894                 return libc_getgrouplist(user, group, groups, ngroups);
4895         }
4896
4897         return nwrap_getgrouplist(user, group, groups, ngroups);
4898 }
4899 #endif
4900
4901 /**********************************************************
4902  * SHADOW
4903  **********************************************************/
4904
4905 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
4906
4907 #ifdef HAVE_SETSPENT
4908 static void nwrap_setspent(void)
4909 {
4910         nwrap_files_setspent();
4911 }
4912
4913 void setspent(void)
4914 {
4915         if (!nss_wrapper_shadow_enabled()) {
4916                 return;
4917         }
4918
4919         nwrap_setspent();
4920 }
4921
4922 static struct spwd *nwrap_getspent(void)
4923 {
4924         return nwrap_files_getspent();
4925 }
4926
4927 struct spwd *getspent(void)
4928 {
4929         if (!nss_wrapper_shadow_enabled()) {
4930                 return NULL;
4931         }
4932
4933         return nwrap_getspent();
4934 }
4935
4936 static void nwrap_endspent(void)
4937 {
4938         nwrap_files_endspent();
4939 }
4940
4941 void endspent(void)
4942 {
4943         if (!nss_wrapper_shadow_enabled()) {
4944                 return;
4945         }
4946
4947         nwrap_endspent();
4948 }
4949 #endif /* HAVE_SETSPENT */
4950
4951 static struct spwd *nwrap_getspnam(const char *name)
4952 {
4953         return nwrap_files_getspnam(name);
4954 }
4955
4956 struct spwd *getspnam(const char *name)
4957 {
4958         if (!nss_wrapper_shadow_enabled()) {
4959                 return NULL;
4960         }
4961
4962         return nwrap_getspnam(name);
4963 }
4964
4965 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
4966
4967 /**********************************************************
4968  * NETDB
4969  **********************************************************/
4970
4971 static void nwrap_sethostent(int stayopen) {
4972         (void) stayopen; /* ignored */
4973
4974         nwrap_files_sethostent();
4975 }
4976
4977 #ifdef HAVE_SOLARIS_SETHOSTENT
4978 int sethostent(int stayopen)
4979 {
4980         if (!nss_wrapper_hosts_enabled()) {
4981                 libc_sethostent(stayopen);
4982                 return 0;
4983         }
4984
4985         nwrap_sethostent(stayopen);
4986
4987         return 0;
4988 }
4989 #else /* HAVE_SOLARIS_SETHOSTENT */
4990 void sethostent(int stayopen)
4991 {
4992         if (!nss_wrapper_hosts_enabled()) {
4993                 libc_sethostent(stayopen);
4994                 return;
4995         }
4996
4997         nwrap_sethostent(stayopen);
4998 }
4999 #endif /* HAVE_SOLARIS_SETHOSTENT */
5000
5001 static struct hostent *nwrap_gethostent(void)
5002 {
5003         return nwrap_files_gethostent();
5004 }
5005
5006 struct hostent *gethostent(void) {
5007         if (!nss_wrapper_hosts_enabled()) {
5008                 return libc_gethostent();
5009         }
5010
5011         return nwrap_gethostent();
5012 }
5013
5014 static void nwrap_endhostent(void) {
5015         nwrap_files_endhostent();
5016 }
5017
5018 #ifdef HAVE_SOLARIS_ENDHOSTENT
5019 int endhostent(void)
5020 {
5021         if (!nss_wrapper_hosts_enabled()) {
5022                 libc_endhostent();
5023                 return 0;
5024         }
5025
5026         nwrap_endhostent();
5027
5028         return 0;
5029 }
5030 #else /* HAVE_SOLARIS_ENDHOSTENT */
5031 void endhostent(void)
5032 {
5033         if (!nss_wrapper_hosts_enabled()) {
5034                 libc_endhostent();
5035                 return;
5036         }
5037
5038         nwrap_endhostent();
5039 }
5040 #endif /* HAVE_SOLARIS_ENDHOSTENT */
5041
5042 #ifdef BSD
5043 /* BSD implementation stores data in thread local storage but GLIBC does not */
5044 static __thread struct hostent user_he;
5045 static __thread struct nwrap_vector user_addrlist;
5046 #else
5047 static struct hostent user_he;
5048 static struct nwrap_vector user_addrlist;
5049 #endif /* BSD */
5050 static struct hostent *nwrap_gethostbyname(const char *name)
5051 {
5052         if (nwrap_files_gethostbyname(name, AF_UNSPEC, &user_he, &user_addrlist) == -1) {
5053                 return NULL;
5054         }
5055         return &user_he;
5056 }
5057
5058 struct hostent *gethostbyname(const char *name)
5059 {
5060         if (!nss_wrapper_hosts_enabled()) {
5061                 return libc_gethostbyname(name);
5062         }
5063
5064         return nwrap_gethostbyname(name);
5065 }
5066
5067 /* This is a GNU extension - Also can be found on BSD systems */
5068 #ifdef HAVE_GETHOSTBYNAME2
5069 #ifdef BSD
5070 /* BSD implementation stores data in  thread local storage but GLIBC not */
5071 static __thread struct hostent user_he2;
5072 static __thread struct nwrap_vector user_addrlist2;
5073 #else
5074 static struct hostent user_he2;
5075 static struct nwrap_vector user_addrlist2;
5076 #endif /* BSD */
5077 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
5078 {
5079         if (nwrap_files_gethostbyname(name, af, &user_he2, &user_addrlist2) == -1) {
5080                 return NULL;
5081         }
5082         return &user_he2;
5083 }
5084
5085 struct hostent *gethostbyname2(const char *name, int af)
5086 {
5087         if (!nss_wrapper_hosts_enabled()) {
5088                 return libc_gethostbyname2(name, af);
5089         }
5090
5091         return nwrap_gethostbyname2(name, af);
5092 }
5093 #endif
5094
5095 static struct hostent *nwrap_gethostbyaddr(const void *addr,
5096                                            socklen_t len, int type)
5097 {
5098         return nwrap_files_gethostbyaddr(addr, len, type);
5099 }
5100
5101 struct hostent *gethostbyaddr(const void *addr,
5102                               socklen_t len, int type)
5103 {
5104         if (!nss_wrapper_hosts_enabled()) {
5105                 return libc_gethostbyaddr(addr, len, type);
5106         }
5107
5108         return nwrap_gethostbyaddr(addr, len, type);
5109 }
5110
5111 static const struct addrinfo default_hints =
5112 {
5113         .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
5114         .ai_family = AF_UNSPEC,
5115         .ai_socktype = 0,
5116         .ai_protocol = 0,
5117         .ai_addrlen = 0,
5118         .ai_addr = NULL,
5119         .ai_canonname = NULL,
5120         .ai_next = NULL
5121 };
5122
5123 static int nwrap_convert_he_ai(const struct hostent *he,
5124                                unsigned short port,
5125                                const struct addrinfo *hints,
5126                                struct addrinfo **pai,
5127                                bool skip_canonname)
5128 {
5129         struct addrinfo *ai;
5130         socklen_t socklen;
5131
5132         if (he == NULL) {
5133                 return EAI_MEMORY;
5134         }
5135
5136         switch (he->h_addrtype) {
5137                 case AF_INET:
5138                         socklen = sizeof(struct sockaddr_in);
5139                         break;
5140 #ifdef HAVE_IPV6
5141                 case AF_INET6:
5142                         socklen = sizeof(struct sockaddr_in6);
5143                         break;
5144 #endif
5145                 default:
5146                         return EAI_FAMILY;
5147         }
5148
5149         ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
5150         if (ai == NULL) {
5151                 return EAI_MEMORY;
5152         }
5153
5154         ai->ai_flags = hints->ai_flags;
5155         ai->ai_family = he->h_addrtype;
5156         ai->ai_socktype = hints->ai_socktype;
5157         ai->ai_protocol = hints->ai_protocol;
5158         ai->ai_canonname = NULL;
5159
5160         if (ai->ai_socktype == 0) {
5161                 ai->ai_socktype = SOCK_DGRAM;
5162         }
5163         if (ai->ai_protocol == 0) {
5164                 if (ai->ai_socktype == SOCK_DGRAM) {
5165                         ai->ai_protocol = IPPROTO_UDP;
5166                 } else if (ai->ai_socktype == SOCK_STREAM) {
5167                         ai->ai_protocol = IPPROTO_TCP;
5168                 }
5169         }
5170
5171         ai->ai_addrlen = socklen;
5172         ai->ai_addr = (void *)(ai + 1);
5173
5174 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
5175         ai->ai_addr->sa_len = socklen;
5176 #endif
5177         ai->ai_addr->sa_family = he->h_addrtype;
5178
5179         switch (he->h_addrtype) {
5180                 case AF_INET:
5181                 {
5182                         struct sockaddr_in *sinp =
5183                                 (struct sockaddr_in *) ai->ai_addr;
5184
5185                         memset(sinp, 0, sizeof(struct sockaddr_in));
5186
5187                         sinp->sin_port = htons(port);
5188                         sinp->sin_family = AF_INET;
5189
5190                         memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
5191                         memcpy(&sinp->sin_addr, he->h_addr_list[0], he->h_length);
5192
5193                 }
5194                 break;
5195 #ifdef HAVE_IPV6
5196                 case AF_INET6:
5197                 {
5198                         struct sockaddr_in6 *sin6p =
5199                                 (struct sockaddr_in6 *) ai->ai_addr;
5200
5201                         memset(sin6p, 0, sizeof(struct sockaddr_in6));
5202
5203                         sin6p->sin6_port = htons(port);
5204                         sin6p->sin6_family = AF_INET6;
5205
5206                         memcpy(&sin6p->sin6_addr,
5207                                he->h_addr_list[0],
5208                                he->h_length);
5209                 }
5210                 break;
5211 #endif
5212         }
5213
5214         ai->ai_next = NULL;
5215
5216         if (he->h_name && !skip_canonname) {
5217                 ai->ai_canonname = strdup(he->h_name);
5218                 if (ai->ai_canonname == NULL) {
5219                         freeaddrinfo(ai);
5220                         return EAI_MEMORY;
5221                 }
5222         }
5223
5224         *pai = ai;
5225         return 0;
5226 }
5227
5228 static int nwrap_getaddrinfo(const char *node,
5229                              const char *service,
5230                              const struct addrinfo *hints,
5231                              struct addrinfo **res)
5232 {
5233         struct addrinfo *ai = NULL;
5234         unsigned short port = 0;
5235         struct {
5236                 int family;
5237                 union {
5238                         struct in_addr v4;
5239 #ifdef HAVE_IPV6
5240                         struct in6_addr v6;
5241                 } in;
5242 #endif
5243         } addr = {
5244                 .family = AF_UNSPEC,
5245         };
5246         int rc;
5247
5248         if (node == NULL && service == NULL) {
5249                 return EAI_NONAME;
5250         }
5251
5252         if (hints == NULL) {
5253                 hints = &default_hints;
5254         }
5255
5256         /* EAI_BADFLAGS
5257               hints.ai_flags   contains   invalid  flags;  or,  hints.ai_flags
5258               included AI_CANONNAME and name was NULL.
5259         */
5260         if ((hints->ai_flags & AI_CANONNAME) && (node == NULL)) {
5261                 return EAI_BADFLAGS;
5262         }
5263
5264         /* If no node has been specified, let glibc deal with it */
5265         if (node == NULL) {
5266                 int ret;
5267                 struct addrinfo *p = NULL;
5268
5269                 ret = libc_getaddrinfo(node, service, hints, &p);
5270
5271                 if (ret == 0) {
5272                         *res = p;
5273                 }
5274                 return ret;
5275         }
5276
5277         if (service != NULL && service[0] != '\0') {
5278                 const char *proto = NULL;
5279                 struct servent *s;
5280                 char *end_ptr;
5281                 long sl;
5282
5283                 errno = 0;
5284                 sl = strtol(service, &end_ptr, 10);
5285
5286                 if (*end_ptr == '\0') {
5287                         port = sl;
5288                         goto valid_port;
5289                 } else if (hints->ai_flags & AI_NUMERICSERV) {
5290                         return EAI_NONAME;
5291                 }
5292
5293                 if (hints->ai_protocol != 0) {
5294                         struct protoent *pent;
5295
5296                         pent = getprotobynumber(hints->ai_protocol);
5297                         if (pent != NULL) {
5298                                 proto = pent->p_name;
5299                         }
5300                 }
5301
5302                 s = getservbyname(service, proto);
5303                 if (s == NULL) {
5304                         return EAI_NONAME;
5305                 }
5306                 port = ntohs(s->s_port);
5307         }
5308
5309 valid_port:
5310
5311         rc = inet_pton(AF_INET, node, &addr.in.v4);
5312         if (rc == 1) {
5313                 addr.family = AF_INET;
5314         }
5315 #ifdef HAVE_IPV6
5316         if (addr.family == AF_UNSPEC) {
5317                 rc = inet_pton(AF_INET6, node, &addr.in.v6);
5318                 if (rc == 1) {
5319                         addr.family = AF_INET6;
5320                 }
5321         }
5322 #endif
5323
5324         if (addr.family == AF_UNSPEC) {
5325                if (hints->ai_flags & AI_NUMERICHOST) {
5326                         return EAI_NONAME;
5327                 }
5328         } else if ((hints->ai_family != AF_UNSPEC) &&
5329                    (hints->ai_family != addr.family))
5330         {
5331                 return EAI_ADDRFAMILY;
5332         }
5333
5334         rc = nwrap_files_getaddrinfo(node, port, hints, &ai);
5335         if (rc != 0) {
5336                 int ret;
5337                 struct addrinfo *p = NULL;
5338
5339                 ret = libc_getaddrinfo(node, service, hints, &p);
5340
5341                 if (ret == 0) {
5342                         /*
5343                          * nwrap_files_getaddrinfo failed, but libc was
5344                          * successful -- use the result from libc.
5345                          */
5346                         *res = p;
5347                         return 0;
5348                 }
5349
5350                 return rc;
5351         }
5352
5353         /*
5354          * If the socktype was not specified, duplicate
5355          * each ai returned, so that we have variants for
5356          * both UDP and TCP.
5357          */
5358         if (hints->ai_socktype == 0) {
5359                 struct addrinfo *ai_cur;
5360
5361                 /* freeaddrinfo() frees ai_canonname and ai so allocate them */
5362                 for (ai_cur = ai; ai_cur != NULL; ai_cur = ai_cur->ai_next) {
5363                         struct addrinfo *ai_new;
5364
5365                         /* duplicate the current entry */
5366
5367                         ai_new = malloc(sizeof(struct addrinfo));
5368                         if (ai_new == NULL) {
5369                                 freeaddrinfo(ai);
5370                                 return EAI_MEMORY;
5371                         }
5372
5373                         memcpy(ai_new, ai_cur, sizeof(struct addrinfo));
5374                         ai_new->ai_next = NULL;
5375
5376                         /* We need a deep copy or freeaddrinfo() will blow up */
5377                         if (ai_cur->ai_canonname != NULL) {
5378                                 ai_new->ai_canonname =
5379                                         strdup(ai_cur->ai_canonname);
5380                         }
5381
5382                         if (ai_cur->ai_socktype == SOCK_DGRAM) {
5383                                 ai_new->ai_socktype = SOCK_STREAM;
5384                         } else if (ai_cur->ai_socktype == SOCK_STREAM) {
5385                                 ai_new->ai_socktype = SOCK_DGRAM;
5386                         }
5387                         if (ai_cur->ai_protocol == IPPROTO_TCP) {
5388                                 ai_new->ai_protocol = IPPROTO_UDP;
5389                         } else if (ai_cur->ai_protocol == IPPROTO_UDP) {
5390                                 ai_new->ai_protocol = IPPROTO_TCP;
5391                         }
5392
5393                         /* now insert the new entry */
5394
5395                         ai_new->ai_next = ai_cur->ai_next;
5396                         ai_cur->ai_next = ai_new;
5397
5398                         /* and move on (don't duplicate the new entry) */
5399
5400                         ai_cur = ai_new;
5401                 }
5402         }
5403
5404         *res = ai;
5405
5406         return 0;
5407 }
5408
5409 int getaddrinfo(const char *node, const char *service,
5410                 const struct addrinfo *hints,
5411                 struct addrinfo **res)
5412 {
5413         if (!nss_wrapper_hosts_enabled()) {
5414                 return libc_getaddrinfo(node, service, hints, res);
5415         }
5416
5417         return nwrap_getaddrinfo(node, service, hints, res);
5418 }
5419
5420 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
5421                              char *host, size_t hostlen,
5422                              char *serv, size_t servlen,
5423                              int flags)
5424 {
5425         struct hostent *he;
5426         struct servent *service;
5427         const char *proto;
5428         const void *addr;
5429         socklen_t addrlen;
5430         uint16_t port;
5431         sa_family_t type;
5432
5433         if (sa == NULL || salen < sizeof(sa_family_t)) {
5434                 return EAI_FAMILY;
5435         }
5436
5437         if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
5438                 return EAI_NONAME;
5439         }
5440
5441         type = sa->sa_family;
5442         switch (type) {
5443         case AF_INET:
5444                 if (salen < sizeof(struct sockaddr_in))
5445                         return EAI_FAMILY;
5446                 addr = &((const struct sockaddr_in *)sa)->sin_addr;
5447                 addrlen = sizeof(((const struct sockaddr_in *)sa)->sin_addr);
5448                 port = ntohs(((const struct sockaddr_in *)sa)->sin_port);
5449                 break;
5450 #ifdef HAVE_IPV6
5451         case AF_INET6:
5452                 if (salen < sizeof(struct sockaddr_in6))
5453                         return EAI_FAMILY;
5454                 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
5455                 addrlen = sizeof(((const struct sockaddr_in6 *)sa)->sin6_addr);
5456                 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port);
5457                 break;
5458 #endif
5459         default:
5460                 return EAI_FAMILY;
5461         }
5462
5463         if (host != NULL) {
5464                 he = NULL;
5465                 if ((flags & NI_NUMERICHOST) == 0) {
5466                         he = nwrap_files_gethostbyaddr(addr, addrlen, type);
5467                         if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
5468                                 return EAI_NONAME;
5469                 }
5470                 if (he != NULL && he->h_name != NULL) {
5471                         if (strlen(he->h_name) >= hostlen)
5472                                 return EAI_OVERFLOW;
5473                         snprintf(host, hostlen, "%s", he->h_name);
5474                         if (flags & NI_NOFQDN)
5475                                 host[strcspn(host, ".")] = '\0';
5476                 } else {
5477                         if (inet_ntop(type, addr, host, hostlen) == NULL)
5478                                 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
5479                 }
5480         }
5481
5482         if (serv != NULL) {
5483                 service = NULL;
5484                 if ((flags & NI_NUMERICSERV) == 0) {
5485                         proto = (flags & NI_DGRAM) ? "udp" : "tcp";
5486                         service = getservbyport(htons(port), proto);
5487                 }
5488                 if (service != NULL) {
5489                         if (strlen(service->s_name) >= servlen)
5490                                 return EAI_OVERFLOW;
5491                         snprintf(serv, servlen, "%s", service->s_name);
5492                 } else {
5493                         if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
5494                                 return EAI_OVERFLOW;
5495                 }
5496         }
5497
5498         return 0;
5499 }
5500
5501 #ifdef HAVE_LINUX_GETNAMEINFO
5502 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5503                 char *host, socklen_t hostlen,
5504                 char *serv, socklen_t servlen,
5505                 int flags)
5506 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
5507 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5508                 char *host, socklen_t hostlen,
5509                 char *serv, socklen_t servlen,
5510                 unsigned int flags)
5511 #else
5512 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5513                 char *host, size_t hostlen,
5514                 char *serv, size_t servlen,
5515                 int flags)
5516 #endif
5517 {
5518         if (!nss_wrapper_hosts_enabled()) {
5519                 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5520         }
5521
5522         return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5523 }
5524
5525 static int nwrap_gethostname(char *name, size_t len)
5526 {
5527         const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
5528
5529         if (strlen(hostname) >= len) {
5530                 errno = ENAMETOOLONG;
5531                 return -1;
5532         }
5533         snprintf(name, len, "%s", hostname);
5534
5535         return 0;
5536 }
5537
5538 #ifdef HAVE_SOLARIS_GETHOSTNAME
5539 int gethostname(char *name, int len)
5540 #else /* HAVE_SOLARIS_GETHOSTNAME */
5541 int gethostname(char *name, size_t len)
5542 #endif /* HAVE_SOLARIS_GETHOSTNAME */
5543 {
5544         if (!nwrap_hostname_enabled()) {
5545                 return libc_gethostname(name, len);
5546         }
5547
5548         return nwrap_gethostname(name, len);
5549 }
5550
5551 /****************************
5552  * CONSTRUCTOR
5553  ***************************/
5554 void nwrap_constructor(void)
5555 {
5556         /*
5557          * If we hold a lock and the application forks, then the child
5558          * is not able to unlock the mutex and we are in a deadlock.
5559          *
5560          * Setting these handlers should prevent such deadlocks.
5561          */
5562         pthread_atfork(&nwrap_thread_prepare,
5563                        &nwrap_thread_parent,
5564                        &nwrap_thread_child);
5565
5566         /*
5567          * Here is safe place to call nwrap_init() and initialize data
5568          * for the main process.
5569          */
5570         nwrap_init();
5571 }
5572
5573 /****************************
5574  * DESTRUCTOR
5575  ***************************/
5576
5577 /*
5578  * This function is called when the library is unloaded and makes sure that
5579  * sockets get closed and the unix file for the socket are unlinked.
5580  */
5581 void nwrap_destructor(void)
5582 {
5583         int i;
5584
5585         NWRAP_LOCK_ALL;
5586         if (nwrap_main_global != NULL) {
5587                 struct nwrap_main *m = nwrap_main_global;
5588
5589                 /* libc */
5590                 if (m->libc != NULL) {
5591                         SAFE_FREE(m->libc->fns);
5592                         if (m->libc->handle != NULL) {
5593                                 dlclose(m->libc->handle);
5594                         }
5595                         if (m->libc->nsl_handle != NULL) {
5596                                 dlclose(m->libc->nsl_handle);
5597                         }
5598                         if (m->libc->sock_handle != NULL) {
5599                                 dlclose(m->libc->sock_handle);
5600                         }
5601                         SAFE_FREE(m->libc);
5602                 }
5603
5604                 /* backends */
5605                 if (m->backends != NULL) {
5606                         for (i = 0; i < m->num_backends; i++) {
5607                                 struct nwrap_backend *b = &(m->backends[i]);
5608
5609                                 if (b->so_handle != NULL) {
5610                                         dlclose(b->so_handle);
5611                                 }
5612                                 SAFE_FREE(b->fns);
5613                         }
5614                         SAFE_FREE(m->backends);
5615                 }
5616         }
5617
5618         if (nwrap_pw_global.cache != NULL) {
5619                 struct nwrap_cache *c = nwrap_pw_global.cache;
5620
5621                 nwrap_files_cache_unload(c);
5622                 if (c->fd >= 0) {
5623                         fclose(c->fp);
5624                         c->fd = -1;
5625                 }
5626
5627                 SAFE_FREE(nwrap_pw_global.list);
5628                 nwrap_pw_global.num = 0;
5629         }
5630
5631         if (nwrap_gr_global.cache != NULL) {
5632                 struct nwrap_cache *c = nwrap_gr_global.cache;
5633
5634                 nwrap_files_cache_unload(c);
5635                 if (c->fd >= 0) {
5636                         fclose(c->fp);
5637                         c->fd = -1;
5638                 }
5639
5640                 SAFE_FREE(nwrap_gr_global.list);
5641                 nwrap_pw_global.num = 0;
5642         }
5643
5644 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
5645         if (nwrap_sp_global.cache != NULL) {
5646                 struct nwrap_cache *c = nwrap_sp_global.cache;
5647
5648                 nwrap_files_cache_unload(c);
5649                 if (c->fd >= 0) {
5650                         fclose(c->fp);
5651                         c->fd = -1;
5652                 }
5653
5654                 nwrap_sp_global.num = 0;
5655         }
5656 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
5657
5658         if (nwrap_he_global.cache != NULL) {
5659                 struct nwrap_cache *c = nwrap_he_global.cache;
5660
5661                 nwrap_files_cache_unload(c);
5662                 if (c->fd >= 0) {
5663                         fclose(c->fp);
5664                         c->fd = -1;
5665                 }
5666
5667                 nwrap_he_global.num = 0;
5668         }
5669
5670         free(user_addrlist.items);
5671 #ifdef HAVE_GETHOSTBYNAME2
5672         free(user_addrlist2.items);
5673 #endif
5674
5675         hdestroy();
5676         NWRAP_UNLOCK_ALL;
5677 }