nwrap: fix nwrap_module_getgrgid_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         *pwdstp = NULL;
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                 *pwdstp = pwdst;
3979                 return 0;
3980         case NSS_STATUS_NOTFOUND:
3981                 if (errno != 0) {
3982                         return errno;
3983                 }
3984                 return ENOENT;
3985         case NSS_STATUS_TRYAGAIN:
3986                 if (errno != 0) {
3987                         return errno;
3988                 }
3989                 return ERANGE;
3990         default:
3991                 if (errno != 0) {
3992                         return errno;
3993                 }
3994                 return ret;
3995         }
3996 }
3997
3998 static void nwrap_module_endpwent(struct nwrap_backend *b)
3999 {
4000         if (!b->fns->_nss_endpwent) {
4001                 return;
4002         }
4003
4004         b->fns->_nss_endpwent();
4005 }
4006
4007 static int nwrap_module_initgroups(struct nwrap_backend *b,
4008                                    const char *user, gid_t group)
4009 {
4010         gid_t *groups;
4011         long int start;
4012         long int size;
4013
4014         if (!b->fns->_nss_initgroups) {
4015                 return NSS_STATUS_UNAVAIL;
4016         }
4017
4018         return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
4019 }
4020
4021 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
4022                                            const char *name)
4023 {
4024         static struct group grp;
4025         static char *buf;
4026         static int buflen = 1000;
4027         NSS_STATUS status;
4028
4029         if (!b->fns->_nss_getgrnam_r) {
4030                 return NULL;
4031         }
4032
4033         if (!buf) {
4034                 buf = (char *)malloc(buflen);
4035         }
4036 again:
4037         status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
4038         if (status == NSS_STATUS_TRYAGAIN) {
4039                 buflen *= 2;
4040                 buf = (char *)realloc(buf, buflen);
4041                 if (!buf) {
4042                         return NULL;
4043                 }
4044                 goto again;
4045         }
4046         if (status == NSS_STATUS_NOTFOUND) {
4047                 SAFE_FREE(buf);
4048                 return NULL;
4049         }
4050         if (status != NSS_STATUS_SUCCESS) {
4051                 SAFE_FREE(buf);
4052                 return NULL;
4053         }
4054         return &grp;
4055 }
4056
4057 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
4058                                    const char *name, struct group *grdst,
4059                                    char *buf, size_t buflen, struct group **grdstp)
4060 {
4061         int ret;
4062
4063         *grdstp = NULL;
4064
4065         if (!b->fns->_nss_getgrnam_r) {
4066                 return ENOENT;
4067         }
4068
4069         ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
4070         switch (ret) {
4071         case NSS_STATUS_SUCCESS:
4072                 *grdstp = grdst;
4073                 return 0;
4074         case NSS_STATUS_NOTFOUND:
4075                 if (errno != 0) {
4076                         return errno;
4077                 }
4078                 return ENOENT;
4079         case NSS_STATUS_TRYAGAIN:
4080                 if (errno != 0) {
4081                         return errno;
4082                 }
4083                 return ERANGE;
4084         default:
4085                 if (errno != 0) {
4086                         return errno;
4087                 }
4088                 return ret;
4089         }
4090 }
4091
4092 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
4093                                            gid_t gid)
4094 {
4095         static struct group grp;
4096         static char *buf;
4097         static int buflen = 1000;
4098         NSS_STATUS status;
4099
4100         if (!b->fns->_nss_getgrgid_r) {
4101                 return NULL;
4102         }
4103
4104         if (!buf) {
4105                 buf = (char *)malloc(buflen);
4106         }
4107
4108 again:
4109         status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
4110         if (status == NSS_STATUS_TRYAGAIN) {
4111                 buflen *= 2;
4112                 buf = (char *)realloc(buf, buflen);
4113                 if (!buf) {
4114                         return NULL;
4115                 }
4116                 goto again;
4117         }
4118         if (status == NSS_STATUS_NOTFOUND) {
4119                 SAFE_FREE(buf);
4120                 return NULL;
4121         }
4122         if (status != NSS_STATUS_SUCCESS) {
4123                 SAFE_FREE(buf);
4124                 return NULL;
4125         }
4126         return &grp;
4127 }
4128
4129 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
4130                                    gid_t gid, struct group *grdst,
4131                                    char *buf, size_t buflen, struct group **grdstp)
4132 {
4133         int ret;
4134
4135         *grdstp = NULL;
4136
4137         if (!b->fns->_nss_getgrgid_r) {
4138                 return ENOENT;
4139         }
4140
4141         ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
4142         switch (ret) {
4143         case NSS_STATUS_SUCCESS:
4144                 *grdstp = grdst;
4145                 return 0;
4146         case NSS_STATUS_NOTFOUND:
4147                 if (errno != 0) {
4148                         return errno;
4149                 }
4150                 return ENOENT;
4151         case NSS_STATUS_TRYAGAIN:
4152                 if (errno != 0) {
4153                         return errno;
4154                 }
4155                 return ERANGE;
4156         default:
4157                 if (errno != 0) {
4158                         return errno;
4159                 }
4160                 return ret;
4161         }
4162 }
4163
4164 static void nwrap_module_setgrent(struct nwrap_backend *b)
4165 {
4166         if (!b->fns->_nss_setgrent) {
4167                 return;
4168         }
4169
4170         b->fns->_nss_setgrent();
4171 }
4172
4173 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
4174 {
4175         static struct group grp;
4176         static char *buf;
4177         static int buflen = 1024;
4178         NSS_STATUS status;
4179
4180         if (!b->fns->_nss_getgrent_r) {
4181                 return NULL;
4182         }
4183
4184         if (!buf) {
4185                 buf = (char *)malloc(buflen);
4186         }
4187
4188 again:
4189         status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
4190         if (status == NSS_STATUS_TRYAGAIN) {
4191                 buflen *= 2;
4192                 buf = (char *)realloc(buf, buflen);
4193                 if (!buf) {
4194                         return NULL;
4195                 }
4196                 goto again;
4197         }
4198         if (status == NSS_STATUS_NOTFOUND) {
4199                 SAFE_FREE(buf);
4200                 return NULL;
4201         }
4202         if (status != NSS_STATUS_SUCCESS) {
4203                 SAFE_FREE(buf);
4204                 return NULL;
4205         }
4206         return &grp;
4207 }
4208
4209 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
4210                                    struct group *grdst, char *buf,
4211                                    size_t buflen, struct group **grdstp)
4212 {
4213         int ret;
4214
4215         (void) grdstp; /* unused */
4216
4217         if (!b->fns->_nss_getgrent_r) {
4218                 return ENOENT;
4219         }
4220
4221         ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
4222         switch (ret) {
4223         case NSS_STATUS_SUCCESS:
4224                 return 0;
4225         case NSS_STATUS_NOTFOUND:
4226                 if (errno != 0) {
4227                         return errno;
4228                 }
4229                 return ENOENT;
4230         case NSS_STATUS_TRYAGAIN:
4231                 if (errno != 0) {
4232                         return errno;
4233                 }
4234                 return ERANGE;
4235         default:
4236                 if (errno != 0) {
4237                         return errno;
4238                 }
4239                 return ret;
4240         }
4241 }
4242
4243 static void nwrap_module_endgrent(struct nwrap_backend *b)
4244 {
4245         if (!b->fns->_nss_endgrent) {
4246                 return;
4247         }
4248
4249         b->fns->_nss_endgrent();
4250 }
4251
4252 /****************************************************************************
4253  *   GETPWNAM
4254  ***************************************************************************/
4255
4256 static struct passwd *nwrap_getpwnam(const char *name)
4257 {
4258         int i;
4259         struct passwd *pwd;
4260
4261         for (i=0; i < nwrap_main_global->num_backends; i++) {
4262                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4263                 pwd = b->ops->nw_getpwnam(b, name);
4264                 if (pwd) {
4265                         return pwd;
4266                 }
4267         }
4268
4269         return NULL;
4270 }
4271
4272 struct passwd *getpwnam(const char *name)
4273 {
4274         if (!nss_wrapper_enabled()) {
4275                 return libc_getpwnam(name);
4276         }
4277
4278         return nwrap_getpwnam(name);
4279 }
4280
4281 /****************************************************************************
4282  *   GETPWNAM_R
4283  ***************************************************************************/
4284
4285 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
4286                             char *buf, size_t buflen, struct passwd **pwdstp)
4287 {
4288         int i,ret;
4289
4290         for (i=0; i < nwrap_main_global->num_backends; i++) {
4291                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4292                 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
4293                 if (ret == ENOENT) {
4294                         continue;
4295                 }
4296                 return ret;
4297         }
4298
4299         return ENOENT;
4300 }
4301
4302 #ifdef HAVE_GETPWNAM_R
4303 # ifdef HAVE_SOLARIS_GETPWNAM_R
4304 int getpwnam_r(const char *name, struct passwd *pwdst,
4305                char *buf, int buflen, struct passwd **pwdstp)
4306 # else /* HAVE_SOLARIS_GETPWNAM_R */
4307 int getpwnam_r(const char *name, struct passwd *pwdst,
4308                char *buf, size_t buflen, struct passwd **pwdstp)
4309 # endif /* HAVE_SOLARIS_GETPWNAM_R */
4310 {
4311         if (!nss_wrapper_enabled()) {
4312                 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4313         }
4314
4315         return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
4316 }
4317 #endif
4318
4319 /****************************************************************************
4320  *   GETPWUID
4321  ***************************************************************************/
4322
4323 static struct passwd *nwrap_getpwuid(uid_t uid)
4324 {
4325         int i;
4326         struct passwd *pwd;
4327
4328         for (i=0; i < nwrap_main_global->num_backends; i++) {
4329                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4330                 pwd = b->ops->nw_getpwuid(b, uid);
4331                 if (pwd) {
4332                         return pwd;
4333                 }
4334         }
4335
4336         return NULL;
4337 }
4338
4339 struct passwd *getpwuid(uid_t uid)
4340 {
4341         if (!nss_wrapper_enabled()) {
4342                 return libc_getpwuid(uid);
4343         }
4344
4345         return nwrap_getpwuid(uid);
4346 }
4347
4348 /****************************************************************************
4349  *   GETPWUID_R
4350  ***************************************************************************/
4351
4352 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
4353                             char *buf, size_t buflen, struct passwd **pwdstp)
4354 {
4355         int i,ret;
4356
4357         for (i=0; i < nwrap_main_global->num_backends; i++) {
4358                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4359                 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
4360                 if (ret == ENOENT) {
4361                         continue;
4362                 }
4363                 return ret;
4364         }
4365
4366         return ENOENT;
4367 }
4368
4369 #ifdef HAVE_SOLARIS_GETPWUID_R
4370 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4371                char *buf, int buflen, struct passwd **pwdstp)
4372 #else
4373 int getpwuid_r(uid_t uid, struct passwd *pwdst,
4374                char *buf, size_t buflen, struct passwd **pwdstp)
4375 #endif
4376 {
4377         if (!nss_wrapper_enabled()) {
4378                 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4379         }
4380
4381         return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
4382 }
4383
4384 /****************************************************************************
4385  *   SETPWENT
4386  ***************************************************************************/
4387
4388 static void nwrap_setpwent(void)
4389 {
4390         int i;
4391
4392         for (i=0; i < nwrap_main_global->num_backends; i++) {
4393                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4394                 b->ops->nw_setpwent(b);
4395         }
4396 }
4397
4398 void setpwent(void)
4399 {
4400         if (!nss_wrapper_enabled()) {
4401                 libc_setpwent();
4402                 return;
4403         }
4404
4405         nwrap_setpwent();
4406 }
4407
4408 /****************************************************************************
4409  *   GETPWENT
4410  ***************************************************************************/
4411
4412 static struct passwd *nwrap_getpwent(void)
4413 {
4414         int i;
4415         struct passwd *pwd;
4416
4417         for (i=0; i < nwrap_main_global->num_backends; i++) {
4418                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4419                 pwd = b->ops->nw_getpwent(b);
4420                 if (pwd) {
4421                         return pwd;
4422                 }
4423         }
4424
4425         return NULL;
4426 }
4427
4428 struct passwd *getpwent(void)
4429 {
4430         if (!nss_wrapper_enabled()) {
4431                 return libc_getpwent();
4432         }
4433
4434         return nwrap_getpwent();
4435 }
4436
4437 /****************************************************************************
4438  *   GETPWENT_R
4439  ***************************************************************************/
4440
4441 #ifdef HAVE_GETPWENT_R
4442 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
4443                             size_t buflen, struct passwd **pwdstp)
4444 {
4445         int i,ret;
4446
4447         for (i=0; i < nwrap_main_global->num_backends; i++) {
4448                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4449                 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
4450                 if (ret == ENOENT) {
4451                         continue;
4452                 }
4453                 return ret;
4454         }
4455
4456         return ENOENT;
4457 }
4458
4459 #  ifdef HAVE_SOLARIS_GETPWENT_R
4460 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
4461 {
4462         struct passwd *pwdstp = NULL;
4463         int rc;
4464
4465         if (!nss_wrapper_enabled()) {
4466                 return libc_getpwent_r(pwdst, buf, buflen);
4467         }
4468         rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
4469         if (rc < 0) {
4470                 return NULL;
4471         }
4472
4473         return pwdstp;
4474 }
4475 #  else /* HAVE_SOLARIS_GETPWENT_R */
4476 int getpwent_r(struct passwd *pwdst, char *buf,
4477                size_t buflen, struct passwd **pwdstp)
4478 {
4479         if (!nss_wrapper_enabled()) {
4480                 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
4481         }
4482
4483         return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
4484 }
4485 #  endif /* HAVE_SOLARIS_GETPWENT_R */
4486 #endif /* HAVE_GETPWENT_R */
4487
4488 /****************************************************************************
4489  *   ENDPWENT
4490  ***************************************************************************/
4491
4492 static void nwrap_endpwent(void)
4493 {
4494         int i;
4495
4496         for (i=0; i < nwrap_main_global->num_backends; i++) {
4497                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4498                 b->ops->nw_endpwent(b);
4499         }
4500 }
4501
4502 void endpwent(void)
4503 {
4504         if (!nss_wrapper_enabled()) {
4505                 libc_endpwent();
4506                 return;
4507         }
4508
4509         nwrap_endpwent();
4510 }
4511
4512 /****************************************************************************
4513  *   INITGROUPS
4514  ***************************************************************************/
4515
4516 static int nwrap_initgroups(const char *user, gid_t group)
4517 {
4518         int i;
4519
4520         for (i=0; i < nwrap_main_global->num_backends; i++) {
4521                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4522                 int rc;
4523
4524                 rc = b->ops->nw_initgroups(b, user, group);
4525                 if (rc == 0) {
4526                         return 0;
4527                 }
4528         }
4529
4530         errno = ENOENT;
4531         return -1;
4532 }
4533
4534 int initgroups(const char *user, gid_t group)
4535 {
4536         if (!nss_wrapper_enabled()) {
4537                 return libc_initgroups(user, group);
4538         }
4539
4540         return nwrap_initgroups(user, group);
4541 }
4542
4543 /****************************************************************************
4544  *   GETGRNAM
4545  ***************************************************************************/
4546
4547 static struct group *nwrap_getgrnam(const char *name)
4548 {
4549         int i;
4550         struct group *grp;
4551
4552         for (i=0; i < nwrap_main_global->num_backends; i++) {
4553                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4554                 grp = b->ops->nw_getgrnam(b, name);
4555                 if (grp) {
4556                         return grp;
4557                 }
4558         }
4559
4560         return NULL;
4561 }
4562
4563 struct group *getgrnam(const char *name)
4564 {
4565         if (!nss_wrapper_enabled()) {
4566                 return libc_getgrnam(name);
4567         }
4568
4569         return nwrap_getgrnam(name);
4570 }
4571
4572 /****************************************************************************
4573  *   GETGRNAM_R
4574  ***************************************************************************/
4575
4576 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
4577                             char *buf, size_t buflen, struct group **grdstp)
4578 {
4579         int i, ret;
4580
4581         for (i=0; i < nwrap_main_global->num_backends; i++) {
4582                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4583                 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
4584                 if (ret == ENOENT) {
4585                         continue;
4586                 }
4587                 return ret;
4588         }
4589
4590         return ENOENT;
4591 }
4592
4593 #ifdef HAVE_GETGRNAM_R
4594 # ifdef HAVE_SOLARIS_GETGRNAM_R
4595 int getgrnam_r(const char *name, struct group *grp,
4596                 char *buf, int buflen, struct group **pgrp)
4597 # else /* HAVE_SOLARIS_GETGRNAM_R */
4598 int getgrnam_r(const char *name, struct group *grp,
4599                char *buf, size_t buflen, struct group **pgrp)
4600 # endif /* HAVE_SOLARIS_GETGRNAM_R */
4601 {
4602         if (!nss_wrapper_enabled()) {
4603                 return libc_getgrnam_r(name,
4604                                        grp,
4605                                        buf,
4606                                        buflen,
4607                                        pgrp);
4608         }
4609
4610         return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
4611 }
4612 #endif /* HAVE_GETGRNAM_R */
4613
4614 /****************************************************************************
4615  *   GETGRGID
4616  ***************************************************************************/
4617
4618 static struct group *nwrap_getgrgid(gid_t gid)
4619 {
4620         int i;
4621         struct group *grp;
4622
4623         for (i=0; i < nwrap_main_global->num_backends; i++) {
4624                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4625                 grp = b->ops->nw_getgrgid(b, gid);
4626                 if (grp) {
4627                         return grp;
4628                 }
4629         }
4630
4631         return NULL;
4632 }
4633
4634 struct group *getgrgid(gid_t gid)
4635 {
4636         if (!nss_wrapper_enabled()) {
4637                 return libc_getgrgid(gid);
4638         }
4639
4640         return nwrap_getgrgid(gid);
4641 }
4642
4643 /****************************************************************************
4644  *   GETGRGID_R
4645  ***************************************************************************/
4646
4647 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
4648                             char *buf, size_t buflen, struct group **grdstp)
4649 {
4650         int i,ret;
4651
4652         for (i=0; i < nwrap_main_global->num_backends; i++) {
4653                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4654                 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
4655                 if (ret == ENOENT) {
4656                         continue;
4657                 }
4658                 return ret;
4659         }
4660
4661         return ENOENT;
4662 }
4663
4664 #ifdef HAVE_GETGRGID_R
4665 # ifdef HAVE_SOLARIS_GETGRGID_R
4666 int getgrgid_r(gid_t gid, struct group *grdst,
4667                char *buf, int buflen, struct group **grdstp)
4668 # else /* HAVE_SOLARIS_GETGRGID_R */
4669 int getgrgid_r(gid_t gid, struct group *grdst,
4670                char *buf, size_t buflen, struct group **grdstp)
4671 # endif /* HAVE_SOLARIS_GETGRGID_R */
4672 {
4673         if (!nss_wrapper_enabled()) {
4674                 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4675         }
4676
4677         return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
4678 }
4679 #endif
4680
4681 /****************************************************************************
4682  *   SETGRENT
4683  ***************************************************************************/
4684
4685 static void nwrap_setgrent(void)
4686 {
4687         int i;
4688
4689         for (i=0; i < nwrap_main_global->num_backends; i++) {
4690                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4691                 b->ops->nw_setgrent(b);
4692         }
4693 }
4694
4695 #ifdef HAVE_BSD_SETGRENT
4696 int setgrent(void)
4697 #else
4698 void setgrent(void)
4699 #endif
4700 {
4701         if (!nss_wrapper_enabled()) {
4702                 libc_setgrent();
4703                 goto out;
4704         }
4705
4706         nwrap_setgrent();
4707
4708 out:
4709 #ifdef HAVE_BSD_SETGRENT
4710         return 0;
4711 #else
4712         return;
4713 #endif
4714 }
4715
4716 /****************************************************************************
4717  *   GETGRENT
4718  ***************************************************************************/
4719
4720 static struct group *nwrap_getgrent(void)
4721 {
4722         int i;
4723         struct group *grp;
4724
4725         for (i=0; i < nwrap_main_global->num_backends; i++) {
4726                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4727                 grp = b->ops->nw_getgrent(b);
4728                 if (grp) {
4729                         return grp;
4730                 }
4731         }
4732
4733         return NULL;
4734 }
4735
4736 struct group *getgrent(void)
4737 {
4738         if (!nss_wrapper_enabled()) {
4739                 return libc_getgrent();
4740         }
4741
4742         return nwrap_getgrent();
4743 }
4744
4745 /****************************************************************************
4746  *   GETGRENT_R
4747  ***************************************************************************/
4748
4749 #ifdef HAVE_GETGRENT_R
4750 static int nwrap_getgrent_r(struct group *grdst, char *buf,
4751                             size_t buflen, struct group **grdstp)
4752 {
4753         int i,ret;
4754
4755         for (i=0; i < nwrap_main_global->num_backends; i++) {
4756                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4757                 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
4758                 if (ret == ENOENT) {
4759                         continue;
4760                 }
4761                 return ret;
4762         }
4763
4764         return ENOENT;
4765 }
4766
4767 #  ifdef HAVE_SOLARIS_GETGRENT_R
4768 struct group *getgrent_r(struct group *src, char *buf, int buflen)
4769 {
4770         struct group *grdstp = NULL;
4771         int rc;
4772
4773         if (!nss_wrapper_enabled()) {
4774                 return libc_getgrent_r(src, buf, buflen);
4775         }
4776
4777         rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
4778         if (rc < 0) {
4779                 return NULL;
4780         }
4781
4782         return grdstp;
4783 }
4784 #  else /* HAVE_SOLARIS_GETGRENT_R */
4785 int getgrent_r(struct group *src, char *buf,
4786                size_t buflen, struct group **grdstp)
4787 {
4788         if (!nss_wrapper_enabled()) {
4789                 return libc_getgrent_r(src, buf, buflen, grdstp);
4790         }
4791
4792         return nwrap_getgrent_r(src, buf, buflen, grdstp);
4793 }
4794 #  endif /* HAVE_SOLARIS_GETGRENT_R */
4795 #endif /* HAVE_GETGRENT_R */
4796
4797 /****************************************************************************
4798  *   ENDGRENT
4799  ***************************************************************************/
4800
4801 static void nwrap_endgrent(void)
4802 {
4803         int i;
4804
4805         for (i=0; i < nwrap_main_global->num_backends; i++) {
4806                 struct nwrap_backend *b = &nwrap_main_global->backends[i];
4807                 b->ops->nw_endgrent(b);
4808         }
4809 }
4810
4811 void endgrent(void)
4812 {
4813         if (!nss_wrapper_enabled()) {
4814                 libc_endgrent();
4815                 return;
4816         }
4817
4818         nwrap_endgrent();
4819 }
4820
4821 /****************************************************************************
4822  *   GETGROUPLIST
4823  ***************************************************************************/
4824
4825 #ifdef HAVE_GETGROUPLIST
4826 static int nwrap_getgrouplist(const char *user, gid_t group,
4827                               gid_t *groups, int *ngroups)
4828 {
4829         struct group *grp;
4830         gid_t *groups_tmp;
4831         int count = 1;
4832
4833         NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
4834
4835         groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
4836         if (!groups_tmp) {
4837                 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
4838                 errno = ENOMEM;
4839                 return -1;
4840         }
4841         groups_tmp[0] = group;
4842
4843         nwrap_setgrent();
4844         while ((grp = nwrap_getgrent()) != NULL) {
4845                 int i = 0;
4846
4847                 NWRAP_LOG(NWRAP_LOG_DEBUG,
4848                           "Inspecting %s for group membership",
4849                           grp->gr_name);
4850
4851                 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
4852
4853                         if (group != grp->gr_gid &&
4854                             (strcmp(user, grp->gr_mem[i]) == 0)) {
4855
4856                                 NWRAP_LOG(NWRAP_LOG_DEBUG,
4857                                           "%s is member of %s",
4858                                           user,
4859                                           grp->gr_name);
4860
4861                                 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
4862                                 if (!groups_tmp) {
4863                                         NWRAP_LOG(NWRAP_LOG_ERROR,
4864                                                   "Out of memory");
4865                                         errno = ENOMEM;
4866                                         return -1;
4867                                 }
4868                                 groups_tmp[count] = grp->gr_gid;
4869
4870                                 count++;
4871                         }
4872                 }
4873         }
4874
4875         nwrap_endgrent();
4876
4877         NWRAP_LOG(NWRAP_LOG_DEBUG,
4878                   "%s is member of %d groups",
4879                   user, *ngroups);
4880
4881         if (*ngroups < count) {
4882                 *ngroups = count;
4883                 free(groups_tmp);
4884                 return -1;
4885         }
4886
4887         *ngroups = count;
4888         memcpy(groups, groups_tmp, count * sizeof(gid_t));
4889         free(groups_tmp);
4890
4891         return count;
4892 }
4893
4894 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
4895 {
4896         if (!nss_wrapper_enabled()) {
4897                 return libc_getgrouplist(user, group, groups, ngroups);
4898         }
4899
4900         return nwrap_getgrouplist(user, group, groups, ngroups);
4901 }
4902 #endif
4903
4904 /**********************************************************
4905  * SHADOW
4906  **********************************************************/
4907
4908 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
4909
4910 #ifdef HAVE_SETSPENT
4911 static void nwrap_setspent(void)
4912 {
4913         nwrap_files_setspent();
4914 }
4915
4916 void setspent(void)
4917 {
4918         if (!nss_wrapper_shadow_enabled()) {
4919                 return;
4920         }
4921
4922         nwrap_setspent();
4923 }
4924
4925 static struct spwd *nwrap_getspent(void)
4926 {
4927         return nwrap_files_getspent();
4928 }
4929
4930 struct spwd *getspent(void)
4931 {
4932         if (!nss_wrapper_shadow_enabled()) {
4933                 return NULL;
4934         }
4935
4936         return nwrap_getspent();
4937 }
4938
4939 static void nwrap_endspent(void)
4940 {
4941         nwrap_files_endspent();
4942 }
4943
4944 void endspent(void)
4945 {
4946         if (!nss_wrapper_shadow_enabled()) {
4947                 return;
4948         }
4949
4950         nwrap_endspent();
4951 }
4952 #endif /* HAVE_SETSPENT */
4953
4954 static struct spwd *nwrap_getspnam(const char *name)
4955 {
4956         return nwrap_files_getspnam(name);
4957 }
4958
4959 struct spwd *getspnam(const char *name)
4960 {
4961         if (!nss_wrapper_shadow_enabled()) {
4962                 return NULL;
4963         }
4964
4965         return nwrap_getspnam(name);
4966 }
4967
4968 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
4969
4970 /**********************************************************
4971  * NETDB
4972  **********************************************************/
4973
4974 static void nwrap_sethostent(int stayopen) {
4975         (void) stayopen; /* ignored */
4976
4977         nwrap_files_sethostent();
4978 }
4979
4980 #ifdef HAVE_SOLARIS_SETHOSTENT
4981 int sethostent(int stayopen)
4982 {
4983         if (!nss_wrapper_hosts_enabled()) {
4984                 libc_sethostent(stayopen);
4985                 return 0;
4986         }
4987
4988         nwrap_sethostent(stayopen);
4989
4990         return 0;
4991 }
4992 #else /* HAVE_SOLARIS_SETHOSTENT */
4993 void sethostent(int stayopen)
4994 {
4995         if (!nss_wrapper_hosts_enabled()) {
4996                 libc_sethostent(stayopen);
4997                 return;
4998         }
4999
5000         nwrap_sethostent(stayopen);
5001 }
5002 #endif /* HAVE_SOLARIS_SETHOSTENT */
5003
5004 static struct hostent *nwrap_gethostent(void)
5005 {
5006         return nwrap_files_gethostent();
5007 }
5008
5009 struct hostent *gethostent(void) {
5010         if (!nss_wrapper_hosts_enabled()) {
5011                 return libc_gethostent();
5012         }
5013
5014         return nwrap_gethostent();
5015 }
5016
5017 static void nwrap_endhostent(void) {
5018         nwrap_files_endhostent();
5019 }
5020
5021 #ifdef HAVE_SOLARIS_ENDHOSTENT
5022 int endhostent(void)
5023 {
5024         if (!nss_wrapper_hosts_enabled()) {
5025                 libc_endhostent();
5026                 return 0;
5027         }
5028
5029         nwrap_endhostent();
5030
5031         return 0;
5032 }
5033 #else /* HAVE_SOLARIS_ENDHOSTENT */
5034 void endhostent(void)
5035 {
5036         if (!nss_wrapper_hosts_enabled()) {
5037                 libc_endhostent();
5038                 return;
5039         }
5040
5041         nwrap_endhostent();
5042 }
5043 #endif /* HAVE_SOLARIS_ENDHOSTENT */
5044
5045 #ifdef BSD
5046 /* BSD implementation stores data in thread local storage but GLIBC does not */
5047 static __thread struct hostent user_he;
5048 static __thread struct nwrap_vector user_addrlist;
5049 #else
5050 static struct hostent user_he;
5051 static struct nwrap_vector user_addrlist;
5052 #endif /* BSD */
5053 static struct hostent *nwrap_gethostbyname(const char *name)
5054 {
5055         if (nwrap_files_gethostbyname(name, AF_UNSPEC, &user_he, &user_addrlist) == -1) {
5056                 return NULL;
5057         }
5058         return &user_he;
5059 }
5060
5061 struct hostent *gethostbyname(const char *name)
5062 {
5063         if (!nss_wrapper_hosts_enabled()) {
5064                 return libc_gethostbyname(name);
5065         }
5066
5067         return nwrap_gethostbyname(name);
5068 }
5069
5070 /* This is a GNU extension - Also can be found on BSD systems */
5071 #ifdef HAVE_GETHOSTBYNAME2
5072 #ifdef BSD
5073 /* BSD implementation stores data in  thread local storage but GLIBC not */
5074 static __thread struct hostent user_he2;
5075 static __thread struct nwrap_vector user_addrlist2;
5076 #else
5077 static struct hostent user_he2;
5078 static struct nwrap_vector user_addrlist2;
5079 #endif /* BSD */
5080 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
5081 {
5082         if (nwrap_files_gethostbyname(name, af, &user_he2, &user_addrlist2) == -1) {
5083                 return NULL;
5084         }
5085         return &user_he2;
5086 }
5087
5088 struct hostent *gethostbyname2(const char *name, int af)
5089 {
5090         if (!nss_wrapper_hosts_enabled()) {
5091                 return libc_gethostbyname2(name, af);
5092         }
5093
5094         return nwrap_gethostbyname2(name, af);
5095 }
5096 #endif
5097
5098 static struct hostent *nwrap_gethostbyaddr(const void *addr,
5099                                            socklen_t len, int type)
5100 {
5101         return nwrap_files_gethostbyaddr(addr, len, type);
5102 }
5103
5104 struct hostent *gethostbyaddr(const void *addr,
5105                               socklen_t len, int type)
5106 {
5107         if (!nss_wrapper_hosts_enabled()) {
5108                 return libc_gethostbyaddr(addr, len, type);
5109         }
5110
5111         return nwrap_gethostbyaddr(addr, len, type);
5112 }
5113
5114 static const struct addrinfo default_hints =
5115 {
5116         .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
5117         .ai_family = AF_UNSPEC,
5118         .ai_socktype = 0,
5119         .ai_protocol = 0,
5120         .ai_addrlen = 0,
5121         .ai_addr = NULL,
5122         .ai_canonname = NULL,
5123         .ai_next = NULL
5124 };
5125
5126 static int nwrap_convert_he_ai(const struct hostent *he,
5127                                unsigned short port,
5128                                const struct addrinfo *hints,
5129                                struct addrinfo **pai,
5130                                bool skip_canonname)
5131 {
5132         struct addrinfo *ai;
5133         socklen_t socklen;
5134
5135         if (he == NULL) {
5136                 return EAI_MEMORY;
5137         }
5138
5139         switch (he->h_addrtype) {
5140                 case AF_INET:
5141                         socklen = sizeof(struct sockaddr_in);
5142                         break;
5143 #ifdef HAVE_IPV6
5144                 case AF_INET6:
5145                         socklen = sizeof(struct sockaddr_in6);
5146                         break;
5147 #endif
5148                 default:
5149                         return EAI_FAMILY;
5150         }
5151
5152         ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
5153         if (ai == NULL) {
5154                 return EAI_MEMORY;
5155         }
5156
5157         ai->ai_flags = hints->ai_flags;
5158         ai->ai_family = he->h_addrtype;
5159         ai->ai_socktype = hints->ai_socktype;
5160         ai->ai_protocol = hints->ai_protocol;
5161         ai->ai_canonname = NULL;
5162
5163         if (ai->ai_socktype == 0) {
5164                 ai->ai_socktype = SOCK_DGRAM;
5165         }
5166         if (ai->ai_protocol == 0) {
5167                 if (ai->ai_socktype == SOCK_DGRAM) {
5168                         ai->ai_protocol = IPPROTO_UDP;
5169                 } else if (ai->ai_socktype == SOCK_STREAM) {
5170                         ai->ai_protocol = IPPROTO_TCP;
5171                 }
5172         }
5173
5174         ai->ai_addrlen = socklen;
5175         ai->ai_addr = (void *)(ai + 1);
5176
5177 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
5178         ai->ai_addr->sa_len = socklen;
5179 #endif
5180         ai->ai_addr->sa_family = he->h_addrtype;
5181
5182         switch (he->h_addrtype) {
5183                 case AF_INET:
5184                 {
5185                         struct sockaddr_in *sinp =
5186                                 (struct sockaddr_in *) ai->ai_addr;
5187
5188                         memset(sinp, 0, sizeof(struct sockaddr_in));
5189
5190                         sinp->sin_port = htons(port);
5191                         sinp->sin_family = AF_INET;
5192
5193                         memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
5194                         memcpy(&sinp->sin_addr, he->h_addr_list[0], he->h_length);
5195
5196                 }
5197                 break;
5198 #ifdef HAVE_IPV6
5199                 case AF_INET6:
5200                 {
5201                         struct sockaddr_in6 *sin6p =
5202                                 (struct sockaddr_in6 *) ai->ai_addr;
5203
5204                         memset(sin6p, 0, sizeof(struct sockaddr_in6));
5205
5206                         sin6p->sin6_port = htons(port);
5207                         sin6p->sin6_family = AF_INET6;
5208
5209                         memcpy(&sin6p->sin6_addr,
5210                                he->h_addr_list[0],
5211                                he->h_length);
5212                 }
5213                 break;
5214 #endif
5215         }
5216
5217         ai->ai_next = NULL;
5218
5219         if (he->h_name && !skip_canonname) {
5220                 ai->ai_canonname = strdup(he->h_name);
5221                 if (ai->ai_canonname == NULL) {
5222                         freeaddrinfo(ai);
5223                         return EAI_MEMORY;
5224                 }
5225         }
5226
5227         *pai = ai;
5228         return 0;
5229 }
5230
5231 static int nwrap_getaddrinfo(const char *node,
5232                              const char *service,
5233                              const struct addrinfo *hints,
5234                              struct addrinfo **res)
5235 {
5236         struct addrinfo *ai = NULL;
5237         unsigned short port = 0;
5238         struct {
5239                 int family;
5240                 union {
5241                         struct in_addr v4;
5242 #ifdef HAVE_IPV6
5243                         struct in6_addr v6;
5244                 } in;
5245 #endif
5246         } addr = {
5247                 .family = AF_UNSPEC,
5248         };
5249         int rc;
5250
5251         if (node == NULL && service == NULL) {
5252                 return EAI_NONAME;
5253         }
5254
5255         if (hints == NULL) {
5256                 hints = &default_hints;
5257         }
5258
5259         /* EAI_BADFLAGS
5260               hints.ai_flags   contains   invalid  flags;  or,  hints.ai_flags
5261               included AI_CANONNAME and name was NULL.
5262         */
5263         if ((hints->ai_flags & AI_CANONNAME) && (node == NULL)) {
5264                 return EAI_BADFLAGS;
5265         }
5266
5267         /* If no node has been specified, let glibc deal with it */
5268         if (node == NULL) {
5269                 int ret;
5270                 struct addrinfo *p = NULL;
5271
5272                 ret = libc_getaddrinfo(node, service, hints, &p);
5273
5274                 if (ret == 0) {
5275                         *res = p;
5276                 }
5277                 return ret;
5278         }
5279
5280         if (service != NULL && service[0] != '\0') {
5281                 const char *proto = NULL;
5282                 struct servent *s;
5283                 char *end_ptr;
5284                 long sl;
5285
5286                 errno = 0;
5287                 sl = strtol(service, &end_ptr, 10);
5288
5289                 if (*end_ptr == '\0') {
5290                         port = sl;
5291                         goto valid_port;
5292                 } else if (hints->ai_flags & AI_NUMERICSERV) {
5293                         return EAI_NONAME;
5294                 }
5295
5296                 if (hints->ai_protocol != 0) {
5297                         struct protoent *pent;
5298
5299                         pent = getprotobynumber(hints->ai_protocol);
5300                         if (pent != NULL) {
5301                                 proto = pent->p_name;
5302                         }
5303                 }
5304
5305                 s = getservbyname(service, proto);
5306                 if (s == NULL) {
5307                         return EAI_NONAME;
5308                 }
5309                 port = ntohs(s->s_port);
5310         }
5311
5312 valid_port:
5313
5314         rc = inet_pton(AF_INET, node, &addr.in.v4);
5315         if (rc == 1) {
5316                 addr.family = AF_INET;
5317         }
5318 #ifdef HAVE_IPV6
5319         if (addr.family == AF_UNSPEC) {
5320                 rc = inet_pton(AF_INET6, node, &addr.in.v6);
5321                 if (rc == 1) {
5322                         addr.family = AF_INET6;
5323                 }
5324         }
5325 #endif
5326
5327         if (addr.family == AF_UNSPEC) {
5328                if (hints->ai_flags & AI_NUMERICHOST) {
5329                         return EAI_NONAME;
5330                 }
5331         } else if ((hints->ai_family != AF_UNSPEC) &&
5332                    (hints->ai_family != addr.family))
5333         {
5334                 return EAI_ADDRFAMILY;
5335         }
5336
5337         rc = nwrap_files_getaddrinfo(node, port, hints, &ai);
5338         if (rc != 0) {
5339                 int ret;
5340                 struct addrinfo *p = NULL;
5341
5342                 ret = libc_getaddrinfo(node, service, hints, &p);
5343
5344                 if (ret == 0) {
5345                         /*
5346                          * nwrap_files_getaddrinfo failed, but libc was
5347                          * successful -- use the result from libc.
5348                          */
5349                         *res = p;
5350                         return 0;
5351                 }
5352
5353                 return rc;
5354         }
5355
5356         /*
5357          * If the socktype was not specified, duplicate
5358          * each ai returned, so that we have variants for
5359          * both UDP and TCP.
5360          */
5361         if (hints->ai_socktype == 0) {
5362                 struct addrinfo *ai_cur;
5363
5364                 /* freeaddrinfo() frees ai_canonname and ai so allocate them */
5365                 for (ai_cur = ai; ai_cur != NULL; ai_cur = ai_cur->ai_next) {
5366                         struct addrinfo *ai_new;
5367
5368                         /* duplicate the current entry */
5369
5370                         ai_new = malloc(sizeof(struct addrinfo));
5371                         if (ai_new == NULL) {
5372                                 freeaddrinfo(ai);
5373                                 return EAI_MEMORY;
5374                         }
5375
5376                         memcpy(ai_new, ai_cur, sizeof(struct addrinfo));
5377                         ai_new->ai_next = NULL;
5378
5379                         /* We need a deep copy or freeaddrinfo() will blow up */
5380                         if (ai_cur->ai_canonname != NULL) {
5381                                 ai_new->ai_canonname =
5382                                         strdup(ai_cur->ai_canonname);
5383                         }
5384
5385                         if (ai_cur->ai_socktype == SOCK_DGRAM) {
5386                                 ai_new->ai_socktype = SOCK_STREAM;
5387                         } else if (ai_cur->ai_socktype == SOCK_STREAM) {
5388                                 ai_new->ai_socktype = SOCK_DGRAM;
5389                         }
5390                         if (ai_cur->ai_protocol == IPPROTO_TCP) {
5391                                 ai_new->ai_protocol = IPPROTO_UDP;
5392                         } else if (ai_cur->ai_protocol == IPPROTO_UDP) {
5393                                 ai_new->ai_protocol = IPPROTO_TCP;
5394                         }
5395
5396                         /* now insert the new entry */
5397
5398                         ai_new->ai_next = ai_cur->ai_next;
5399                         ai_cur->ai_next = ai_new;
5400
5401                         /* and move on (don't duplicate the new entry) */
5402
5403                         ai_cur = ai_new;
5404                 }
5405         }
5406
5407         *res = ai;
5408
5409         return 0;
5410 }
5411
5412 int getaddrinfo(const char *node, const char *service,
5413                 const struct addrinfo *hints,
5414                 struct addrinfo **res)
5415 {
5416         if (!nss_wrapper_hosts_enabled()) {
5417                 return libc_getaddrinfo(node, service, hints, res);
5418         }
5419
5420         return nwrap_getaddrinfo(node, service, hints, res);
5421 }
5422
5423 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
5424                              char *host, size_t hostlen,
5425                              char *serv, size_t servlen,
5426                              int flags)
5427 {
5428         struct hostent *he;
5429         struct servent *service;
5430         const char *proto;
5431         const void *addr;
5432         socklen_t addrlen;
5433         uint16_t port;
5434         sa_family_t type;
5435
5436         if (sa == NULL || salen < sizeof(sa_family_t)) {
5437                 return EAI_FAMILY;
5438         }
5439
5440         if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
5441                 return EAI_NONAME;
5442         }
5443
5444         type = sa->sa_family;
5445         switch (type) {
5446         case AF_INET:
5447                 if (salen < sizeof(struct sockaddr_in))
5448                         return EAI_FAMILY;
5449                 addr = &((const struct sockaddr_in *)sa)->sin_addr;
5450                 addrlen = sizeof(((const struct sockaddr_in *)sa)->sin_addr);
5451                 port = ntohs(((const struct sockaddr_in *)sa)->sin_port);
5452                 break;
5453 #ifdef HAVE_IPV6
5454         case AF_INET6:
5455                 if (salen < sizeof(struct sockaddr_in6))
5456                         return EAI_FAMILY;
5457                 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
5458                 addrlen = sizeof(((const struct sockaddr_in6 *)sa)->sin6_addr);
5459                 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port);
5460                 break;
5461 #endif
5462         default:
5463                 return EAI_FAMILY;
5464         }
5465
5466         if (host != NULL) {
5467                 he = NULL;
5468                 if ((flags & NI_NUMERICHOST) == 0) {
5469                         he = nwrap_files_gethostbyaddr(addr, addrlen, type);
5470                         if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
5471                                 return EAI_NONAME;
5472                 }
5473                 if (he != NULL && he->h_name != NULL) {
5474                         if (strlen(he->h_name) >= hostlen)
5475                                 return EAI_OVERFLOW;
5476                         snprintf(host, hostlen, "%s", he->h_name);
5477                         if (flags & NI_NOFQDN)
5478                                 host[strcspn(host, ".")] = '\0';
5479                 } else {
5480                         if (inet_ntop(type, addr, host, hostlen) == NULL)
5481                                 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
5482                 }
5483         }
5484
5485         if (serv != NULL) {
5486                 service = NULL;
5487                 if ((flags & NI_NUMERICSERV) == 0) {
5488                         proto = (flags & NI_DGRAM) ? "udp" : "tcp";
5489                         service = getservbyport(htons(port), proto);
5490                 }
5491                 if (service != NULL) {
5492                         if (strlen(service->s_name) >= servlen)
5493                                 return EAI_OVERFLOW;
5494                         snprintf(serv, servlen, "%s", service->s_name);
5495                 } else {
5496                         if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
5497                                 return EAI_OVERFLOW;
5498                 }
5499         }
5500
5501         return 0;
5502 }
5503
5504 #ifdef HAVE_LINUX_GETNAMEINFO
5505 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5506                 char *host, socklen_t hostlen,
5507                 char *serv, socklen_t servlen,
5508                 int flags)
5509 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
5510 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5511                 char *host, socklen_t hostlen,
5512                 char *serv, socklen_t servlen,
5513                 unsigned int flags)
5514 #else
5515 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
5516                 char *host, size_t hostlen,
5517                 char *serv, size_t servlen,
5518                 int flags)
5519 #endif
5520 {
5521         if (!nss_wrapper_hosts_enabled()) {
5522                 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5523         }
5524
5525         return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
5526 }
5527
5528 static int nwrap_gethostname(char *name, size_t len)
5529 {
5530         const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
5531
5532         if (strlen(hostname) >= len) {
5533                 errno = ENAMETOOLONG;
5534                 return -1;
5535         }
5536         snprintf(name, len, "%s", hostname);
5537
5538         return 0;
5539 }
5540
5541 #ifdef HAVE_SOLARIS_GETHOSTNAME
5542 int gethostname(char *name, int len)
5543 #else /* HAVE_SOLARIS_GETHOSTNAME */
5544 int gethostname(char *name, size_t len)
5545 #endif /* HAVE_SOLARIS_GETHOSTNAME */
5546 {
5547         if (!nwrap_hostname_enabled()) {
5548                 return libc_gethostname(name, len);
5549         }
5550
5551         return nwrap_gethostname(name, len);
5552 }
5553
5554 /****************************
5555  * CONSTRUCTOR
5556  ***************************/
5557 void nwrap_constructor(void)
5558 {
5559         /*
5560          * If we hold a lock and the application forks, then the child
5561          * is not able to unlock the mutex and we are in a deadlock.
5562          *
5563          * Setting these handlers should prevent such deadlocks.
5564          */
5565         pthread_atfork(&nwrap_thread_prepare,
5566                        &nwrap_thread_parent,
5567                        &nwrap_thread_child);
5568
5569         /*
5570          * Here is safe place to call nwrap_init() and initialize data
5571          * for the main process.
5572          */
5573         nwrap_init();
5574 }
5575
5576 /****************************
5577  * DESTRUCTOR
5578  ***************************/
5579
5580 /*
5581  * This function is called when the library is unloaded and makes sure that
5582  * sockets get closed and the unix file for the socket are unlinked.
5583  */
5584 void nwrap_destructor(void)
5585 {
5586         int i;
5587
5588         NWRAP_LOCK_ALL;
5589         if (nwrap_main_global != NULL) {
5590                 struct nwrap_main *m = nwrap_main_global;
5591
5592                 /* libc */
5593                 if (m->libc != NULL) {
5594                         SAFE_FREE(m->libc->fns);
5595                         if (m->libc->handle != NULL) {
5596                                 dlclose(m->libc->handle);
5597                         }
5598                         if (m->libc->nsl_handle != NULL) {
5599                                 dlclose(m->libc->nsl_handle);
5600                         }
5601                         if (m->libc->sock_handle != NULL) {
5602                                 dlclose(m->libc->sock_handle);
5603                         }
5604                         SAFE_FREE(m->libc);
5605                 }
5606
5607                 /* backends */
5608                 if (m->backends != NULL) {
5609                         for (i = 0; i < m->num_backends; i++) {
5610                                 struct nwrap_backend *b = &(m->backends[i]);
5611
5612                                 if (b->so_handle != NULL) {
5613                                         dlclose(b->so_handle);
5614                                 }
5615                                 SAFE_FREE(b->fns);
5616                         }
5617                         SAFE_FREE(m->backends);
5618                 }
5619         }
5620
5621         if (nwrap_pw_global.cache != NULL) {
5622                 struct nwrap_cache *c = nwrap_pw_global.cache;
5623
5624                 nwrap_files_cache_unload(c);
5625                 if (c->fd >= 0) {
5626                         fclose(c->fp);
5627                         c->fd = -1;
5628                 }
5629
5630                 SAFE_FREE(nwrap_pw_global.list);
5631                 nwrap_pw_global.num = 0;
5632         }
5633
5634         if (nwrap_gr_global.cache != NULL) {
5635                 struct nwrap_cache *c = nwrap_gr_global.cache;
5636
5637                 nwrap_files_cache_unload(c);
5638                 if (c->fd >= 0) {
5639                         fclose(c->fp);
5640                         c->fd = -1;
5641                 }
5642
5643                 SAFE_FREE(nwrap_gr_global.list);
5644                 nwrap_pw_global.num = 0;
5645         }
5646
5647 #if defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM)
5648         if (nwrap_sp_global.cache != NULL) {
5649                 struct nwrap_cache *c = nwrap_sp_global.cache;
5650
5651                 nwrap_files_cache_unload(c);
5652                 if (c->fd >= 0) {
5653                         fclose(c->fp);
5654                         c->fd = -1;
5655                 }
5656
5657                 nwrap_sp_global.num = 0;
5658         }
5659 #endif /* defined(HAVE_SHADOW_H) && defined(HAVE_GETSPNAM) */
5660
5661         if (nwrap_he_global.cache != NULL) {
5662                 struct nwrap_cache *c = nwrap_he_global.cache;
5663
5664                 nwrap_files_cache_unload(c);
5665                 if (c->fd >= 0) {
5666                         fclose(c->fp);
5667                         c->fd = -1;
5668                 }
5669
5670                 nwrap_he_global.num = 0;
5671         }
5672
5673         free(user_addrlist.items);
5674 #ifdef HAVE_GETHOSTBYNAME2
5675         free(user_addrlist2.items);
5676 #endif
5677
5678         hdestroy();
5679         NWRAP_UNLOCK_ALL;
5680 }