nwrap: Fix the handle loops for older gcc versions.
[Samba.git] / lib / nss_wrapper / nss_wrapper.c
blob28c23d81117e81c2f9d41bac6a1b0b1b386db1af
1 /*
2 * Copyright (C) Stefan Metzmacher 2007 <metze@samba.org>
3 * Copyright (C) Guenther Deschner 2009 <gd@samba.org>
4 * Copyright (C) Andreas Schneider 2013 <asn@samba.org>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the author nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "config.h"
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/socket.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdarg.h>
44 #include <stdbool.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdint.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <ctype.h>
54 * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h gives us
55 * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
56 * Solaris
58 #ifndef _POSIX_PTHREAD_SEMANTICS
59 #define _POSIX_PTHREAD_SEMANTICS
60 #endif
62 #include <pwd.h>
63 #include <grp.h>
65 #include <netdb.h>
66 #include <arpa/inet.h>
67 #include <netinet/in.h>
69 #include <dlfcn.h>
71 #if defined(HAVE_NSS_H)
72 /* Linux and BSD */
73 #include <nss.h>
75 typedef enum nss_status NSS_STATUS;
76 #elif defined(HAVE_NSS_COMMON_H)
77 /* Solaris */
78 #include <nss_common.h>
79 #include <nss_dbdefs.h>
80 #include <nsswitch.h>
82 typedef nss_status_t NSS_STATUS;
84 # define NSS_STATUS_SUCCESS NSS_SUCCESS
85 # define NSS_STATUS_NOTFOUND NSS_NOTFOUND
86 # define NSS_STATUS_UNAVAIL NSS_UNAVAIL
87 # define NSS_STATUS_TRYAGAIN NSS_TRYAGAIN
88 #else
89 # error "No nsswitch support detected"
90 #endif
92 #ifndef PTR_DIFF
93 #define PTR_DIFF(p1, p2) ((ptrdiff_t)(((const char *)(p1)) - (const char *)(p2)))
94 #endif
96 #ifndef _PUBLIC_
97 #define _PUBLIC_
98 #endif
100 #ifndef EAI_NODATA
101 #define EAI_NODATA EAI_NONAME
102 #endif
104 #ifndef EAI_ADDRFAMILY
105 #define EAI_ADDRFAMILY EAI_FAMILY
106 #endif
108 #ifndef __STRING
109 #define __STRING(x) #x
110 #endif
112 #ifndef __STRINGSTRING
113 #define __STRINGSTRING(x) __STRING(x)
114 #endif
116 #ifndef __LINESTR__
117 #define __LINESTR__ __STRINGSTRING(__LINE__)
118 #endif
120 #ifndef __location__
121 #define __location__ __FILE__ ":" __LINESTR__
122 #endif
124 #ifndef DNS_NAME_MAX
125 #define DNS_NAME_MAX 255
126 #endif
128 /* GCC have printf type attribute check. */
129 #ifdef HAVE_ATTRIBUTE_PRINTF_FORMAT
130 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
131 #else
132 #define PRINTF_ATTRIBUTE(a,b)
133 #endif /* HAVE_ATTRIBUTE_PRINTF_FORMAT */
135 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
136 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
137 #else
138 #define DESTRUCTOR_ATTRIBUTE
139 #endif /* HAVE_DESTRUCTOR_ATTRIBUTE */
141 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
143 enum nwrap_dbglvl_e {
144 NWRAP_LOG_ERROR = 0,
145 NWRAP_LOG_WARN,
146 NWRAP_LOG_DEBUG,
147 NWRAP_LOG_TRACE
150 #ifdef NDEBUG
151 # define NWRAP_LOG(...)
152 #else
154 static void nwrap_log(enum nwrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
155 # define NWRAP_LOG(dbglvl, ...) nwrap_log((dbglvl), __func__, __VA_ARGS__)
157 static void nwrap_log(enum nwrap_dbglvl_e dbglvl,
158 const char *func,
159 const char *format, ...)
161 char buffer[1024];
162 va_list va;
163 const char *d;
164 unsigned int lvl = 0;
165 int pid = getpid();
167 d = getenv("NSS_WRAPPER_DEBUGLEVEL");
168 if (d != NULL) {
169 lvl = atoi(d);
172 va_start(va, format);
173 vsnprintf(buffer, sizeof(buffer), format, va);
174 va_end(va);
176 if (lvl >= dbglvl) {
177 switch (dbglvl) {
178 case NWRAP_LOG_ERROR:
179 fprintf(stderr,
180 "NWRAP_ERROR(%d) - %s: %s\n",
181 pid, func, buffer);
182 break;
183 case NWRAP_LOG_WARN:
184 fprintf(stderr,
185 "NWRAP_WARN(%d) - %s: %s\n",
186 pid, func, buffer);
187 break;
188 case NWRAP_LOG_DEBUG:
189 fprintf(stderr,
190 "NWRAP_DEBUG(%d) - %s: %s\n",
191 pid, func, buffer);
192 break;
193 case NWRAP_LOG_TRACE:
194 fprintf(stderr,
195 "NWRAP_TRACE(%d) - %s: %s\n",
196 pid, func, buffer);
197 break;
201 #endif /* NDEBUG NWRAP_LOG */
203 struct nwrap_libc_fns {
204 struct passwd *(*_libc_getpwnam)(const char *name);
205 int (*_libc_getpwnam_r)(const char *name, struct passwd *pwd,
206 char *buf, size_t buflen, struct passwd **result);
207 struct passwd *(*_libc_getpwuid)(uid_t uid);
208 int (*_libc_getpwuid_r)(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);
209 void (*_libc_setpwent)(void);
210 struct passwd *(*_libc_getpwent)(void);
211 #ifdef HAVE_SOLARIS_GETPWENT_R
212 struct passwd *(*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen);
213 #else
214 int (*_libc_getpwent_r)(struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp);
215 #endif
216 void (*_libc_endpwent)(void);
217 int (*_libc_initgroups)(const char *user, gid_t gid);
218 struct group *(*_libc_getgrnam)(const char *name);
219 int (*_libc_getgrnam_r)(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result);
220 struct group *(*_libc_getgrgid)(gid_t gid);
221 int (*_libc_getgrgid_r)(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result);
222 void (*_libc_setgrent)(void);
223 struct group *(*_libc_getgrent)(void);
224 #ifdef HAVE_SOLARIS_GETGRENT_R
225 struct group *(*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen);
226 #else
227 int (*_libc_getgrent_r)(struct group *group, char *buf, size_t buflen, struct group **result);
228 #endif
229 void (*_libc_endgrent)(void);
230 int (*_libc_getgrouplist)(const char *user, gid_t group, gid_t *groups, int *ngroups);
232 void (*_libc_sethostent)(int stayopen);
233 struct hostent *(*_libc_gethostent)(void);
234 void (*_libc_endhostent)(void);
236 struct hostent *(*_libc_gethostbyname)(const char *name);
237 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
238 struct hostent *(*_libc_gethostbyname2)(const char *name, int af);
239 #endif
240 struct hostent *(*_libc_gethostbyaddr)(const void *addr, socklen_t len, int type);
242 int (*_libc_getaddrinfo)(const char *node, const char *service,
243 const struct addrinfo *hints,
244 struct addrinfo **res);
245 int (*_libc_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
246 char *host, size_t hostlen,
247 char *serv, size_t servlen,
248 int flags);
249 int (*_libc_gethostname)(char *name, size_t len);
250 #ifdef HAVE_GETHOSTBYNAME_R
251 int (*_libc_gethostbyname_r)(const char *name,
252 struct hostent *ret,
253 char *buf, size_t buflen,
254 struct hostent **result, int *h_errnop);
255 #endif
256 #ifdef HAVE_GETHOSTBYADDR_R
257 int (*_libc_gethostbyaddr_r)(const void *addr, socklen_t len, int type,
258 struct hostent *ret,
259 char *buf, size_t buflen,
260 struct hostent **result, int *h_errnop);
261 #endif
264 struct nwrap_module_nss_fns {
265 NSS_STATUS (*_nss_getpwnam_r)(const char *name, struct passwd *result, char *buffer,
266 size_t buflen, int *errnop);
267 NSS_STATUS (*_nss_getpwuid_r)(uid_t uid, struct passwd *result, char *buffer,
268 size_t buflen, int *errnop);
269 NSS_STATUS (*_nss_setpwent)(void);
270 NSS_STATUS (*_nss_getpwent_r)(struct passwd *result, char *buffer,
271 size_t buflen, int *errnop);
272 NSS_STATUS (*_nss_endpwent)(void);
273 NSS_STATUS (*_nss_initgroups)(const char *user, gid_t group, long int *start,
274 long int *size, gid_t **groups, long int limit, int *errnop);
275 NSS_STATUS (*_nss_getgrnam_r)(const char *name, struct group *result, char *buffer,
276 size_t buflen, int *errnop);
277 NSS_STATUS (*_nss_getgrgid_r)(gid_t gid, struct group *result, char *buffer,
278 size_t buflen, int *errnop);
279 NSS_STATUS (*_nss_setgrent)(void);
280 NSS_STATUS (*_nss_getgrent_r)(struct group *result, char *buffer,
281 size_t buflen, int *errnop);
282 NSS_STATUS (*_nss_endgrent)(void);
285 struct nwrap_backend {
286 const char *name;
287 const char *so_path;
288 void *so_handle;
289 struct nwrap_ops *ops;
290 struct nwrap_module_nss_fns *fns;
293 struct nwrap_ops {
294 struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
295 const char *name);
296 int (*nw_getpwnam_r)(struct nwrap_backend *b,
297 const char *name, struct passwd *pwdst,
298 char *buf, size_t buflen, struct passwd **pwdstp);
299 struct passwd * (*nw_getpwuid)(struct nwrap_backend *b,
300 uid_t uid);
301 int (*nw_getpwuid_r)(struct nwrap_backend *b,
302 uid_t uid, struct passwd *pwdst,
303 char *buf, size_t buflen, struct passwd **pwdstp);
304 void (*nw_setpwent)(struct nwrap_backend *b);
305 struct passwd * (*nw_getpwent)(struct nwrap_backend *b);
306 int (*nw_getpwent_r)(struct nwrap_backend *b,
307 struct passwd *pwdst, char *buf,
308 size_t buflen, struct passwd **pwdstp);
309 void (*nw_endpwent)(struct nwrap_backend *b);
310 int (*nw_initgroups)(struct nwrap_backend *b,
311 const char *user, gid_t group);
312 struct group * (*nw_getgrnam)(struct nwrap_backend *b,
313 const char *name);
314 int (*nw_getgrnam_r)(struct nwrap_backend *b,
315 const char *name, struct group *grdst,
316 char *buf, size_t buflen, struct group **grdstp);
317 struct group * (*nw_getgrgid)(struct nwrap_backend *b,
318 gid_t gid);
319 int (*nw_getgrgid_r)(struct nwrap_backend *b,
320 gid_t gid, struct group *grdst,
321 char *buf, size_t buflen, struct group **grdstp);
322 void (*nw_setgrent)(struct nwrap_backend *b);
323 struct group * (*nw_getgrent)(struct nwrap_backend *b);
324 int (*nw_getgrent_r)(struct nwrap_backend *b,
325 struct group *grdst, char *buf,
326 size_t buflen, struct group **grdstp);
327 void (*nw_endgrent)(struct nwrap_backend *b);
330 /* Public prototypes */
332 bool nss_wrapper_enabled(void);
333 bool nss_wrapper_hosts_enabled(void);
335 /* prototypes for files backend */
338 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
339 const char *name);
340 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
341 const char *name, struct passwd *pwdst,
342 char *buf, size_t buflen, struct passwd **pwdstp);
343 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
344 uid_t uid);
345 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
346 uid_t uid, struct passwd *pwdst,
347 char *buf, size_t buflen, struct passwd **pwdstp);
348 static void nwrap_files_setpwent(struct nwrap_backend *b);
349 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b);
350 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
351 struct passwd *pwdst, char *buf,
352 size_t buflen, struct passwd **pwdstp);
353 static void nwrap_files_endpwent(struct nwrap_backend *b);
354 static int nwrap_files_initgroups(struct nwrap_backend *b,
355 const char *user, gid_t group);
356 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
357 const char *name);
358 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
359 const char *name, struct group *grdst,
360 char *buf, size_t buflen, struct group **grdstp);
361 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
362 gid_t gid);
363 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
364 gid_t gid, struct group *grdst,
365 char *buf, size_t buflen, struct group **grdstp);
366 static void nwrap_files_setgrent(struct nwrap_backend *b);
367 static struct group *nwrap_files_getgrent(struct nwrap_backend *b);
368 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
369 struct group *grdst, char *buf,
370 size_t buflen, struct group **grdstp);
371 static void nwrap_files_endgrent(struct nwrap_backend *b);
373 /* prototypes for module backend */
375 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b);
376 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
377 struct passwd *pwdst, char *buf,
378 size_t buflen, struct passwd **pwdstp);
379 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
380 const char *name);
381 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
382 const char *name, struct passwd *pwdst,
383 char *buf, size_t buflen, struct passwd **pwdstp);
384 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
385 uid_t uid);
386 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
387 uid_t uid, struct passwd *pwdst,
388 char *buf, size_t buflen, struct passwd **pwdstp);
389 static void nwrap_module_setpwent(struct nwrap_backend *b);
390 static void nwrap_module_endpwent(struct nwrap_backend *b);
391 static struct group *nwrap_module_getgrent(struct nwrap_backend *b);
392 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
393 struct group *grdst, char *buf,
394 size_t buflen, struct group **grdstp);
395 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
396 const char *name);
397 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
398 const char *name, struct group *grdst,
399 char *buf, size_t buflen, struct group **grdstp);
400 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
401 gid_t gid);
402 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
403 gid_t gid, struct group *grdst,
404 char *buf, size_t buflen, struct group **grdstp);
405 static void nwrap_module_setgrent(struct nwrap_backend *b);
406 static void nwrap_module_endgrent(struct nwrap_backend *b);
407 static int nwrap_module_initgroups(struct nwrap_backend *b,
408 const char *user, gid_t group);
410 struct nwrap_ops nwrap_files_ops = {
411 .nw_getpwnam = nwrap_files_getpwnam,
412 .nw_getpwnam_r = nwrap_files_getpwnam_r,
413 .nw_getpwuid = nwrap_files_getpwuid,
414 .nw_getpwuid_r = nwrap_files_getpwuid_r,
415 .nw_setpwent = nwrap_files_setpwent,
416 .nw_getpwent = nwrap_files_getpwent,
417 .nw_getpwent_r = nwrap_files_getpwent_r,
418 .nw_endpwent = nwrap_files_endpwent,
419 .nw_initgroups = nwrap_files_initgroups,
420 .nw_getgrnam = nwrap_files_getgrnam,
421 .nw_getgrnam_r = nwrap_files_getgrnam_r,
422 .nw_getgrgid = nwrap_files_getgrgid,
423 .nw_getgrgid_r = nwrap_files_getgrgid_r,
424 .nw_setgrent = nwrap_files_setgrent,
425 .nw_getgrent = nwrap_files_getgrent,
426 .nw_getgrent_r = nwrap_files_getgrent_r,
427 .nw_endgrent = nwrap_files_endgrent,
430 struct nwrap_ops nwrap_module_ops = {
431 .nw_getpwnam = nwrap_module_getpwnam,
432 .nw_getpwnam_r = nwrap_module_getpwnam_r,
433 .nw_getpwuid = nwrap_module_getpwuid,
434 .nw_getpwuid_r = nwrap_module_getpwuid_r,
435 .nw_setpwent = nwrap_module_setpwent,
436 .nw_getpwent = nwrap_module_getpwent,
437 .nw_getpwent_r = nwrap_module_getpwent_r,
438 .nw_endpwent = nwrap_module_endpwent,
439 .nw_initgroups = nwrap_module_initgroups,
440 .nw_getgrnam = nwrap_module_getgrnam,
441 .nw_getgrnam_r = nwrap_module_getgrnam_r,
442 .nw_getgrgid = nwrap_module_getgrgid,
443 .nw_getgrgid_r = nwrap_module_getgrgid_r,
444 .nw_setgrent = nwrap_module_setgrent,
445 .nw_getgrent = nwrap_module_getgrent,
446 .nw_getgrent_r = nwrap_module_getgrent_r,
447 .nw_endgrent = nwrap_module_endgrent,
450 struct nwrap_libc {
451 void *handle;
452 void *nsl_handle;
453 void *sock_handle;
454 struct nwrap_libc_fns *fns;
457 struct nwrap_main {
458 const char *nwrap_switch;
459 int num_backends;
460 struct nwrap_backend *backends;
461 struct nwrap_libc *libc;
464 struct nwrap_main *nwrap_main_global;
465 struct nwrap_main __nwrap_main_global;
467 struct nwrap_cache {
468 const char *path;
469 int fd;
470 struct stat st;
471 uint8_t *buf;
472 void *private_data;
473 bool (*parse_line)(struct nwrap_cache *, char *line);
474 void (*unload)(struct nwrap_cache *);
477 struct nwrap_pw {
478 struct nwrap_cache *cache;
480 struct passwd *list;
481 int num;
482 int idx;
485 struct nwrap_cache __nwrap_cache_pw;
486 struct nwrap_pw nwrap_pw_global;
488 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
489 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
491 struct nwrap_gr {
492 struct nwrap_cache *cache;
494 struct group *list;
495 int num;
496 int idx;
499 struct nwrap_cache __nwrap_cache_gr;
500 struct nwrap_gr nwrap_gr_global;
502 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line);
503 static void nwrap_he_unload(struct nwrap_cache *nwrap);
505 struct nwrap_addrdata {
506 unsigned char host_addr[16]; /* IPv4 or IPv6 address */
507 char *h_addr_ptrs[2]; /* host_addr pointer + NULL */
510 struct nwrap_entdata {
511 struct nwrap_addrdata *addr;
512 struct hostent ht;
515 struct nwrap_he {
516 struct nwrap_cache *cache;
518 struct nwrap_entdata *list;
519 int num;
520 int idx;
523 struct nwrap_cache __nwrap_cache_he;
524 struct nwrap_he nwrap_he_global;
527 /*********************************************************
528 * NWRAP PROTOTYPES
529 *********************************************************/
531 static void nwrap_init(void);
532 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
533 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
534 void nwrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
536 /*********************************************************
537 * NWRAP LIBC LOADER FUNCTIONS
538 *********************************************************/
540 enum nwrap_lib {
541 NWRAP_LIBC,
542 NWRAP_LIBNSL,
543 NWRAP_LIBSOCKET,
546 #ifndef NDEBUG
547 static const char *nwrap_str_lib(enum nwrap_lib lib)
549 switch (lib) {
550 case NWRAP_LIBC:
551 return "libc";
552 case NWRAP_LIBNSL:
553 return "libnsl";
554 case NWRAP_LIBSOCKET:
555 return "libsocket";
558 /* Compiler would warn us about unhandled enum value if we get here */
559 return "unknown";
561 #endif
563 static void *nwrap_load_lib_handle(enum nwrap_lib lib)
565 int flags = RTLD_LAZY;
566 void *handle = NULL;
567 int i;
569 #ifdef RTLD_DEEPBIND
570 flags |= RTLD_DEEPBIND;
571 #endif
573 switch (lib) {
574 case NWRAP_LIBNSL:
575 #ifdef HAVE_LIBNSL
576 handle = nwrap_main_global->libc->nsl_handle;
577 if (handle == NULL) {
578 for (i = 10; i >= 0; i--) {
579 char soname[256] = {0};
581 snprintf(soname, sizeof(soname), "libnsl.so.%d", i);
582 handle = dlopen(soname, flags);
583 if (handle != NULL) {
584 break;
588 nwrap_main_global->libc->nsl_handle = handle;
590 break;
591 #endif
592 /* FALL TROUGH */
593 case NWRAP_LIBSOCKET:
594 #ifdef HAVE_LIBSOCKET
595 handle = nwrap_main_global->libc->sock_handle;
596 if (handle == NULL) {
597 for (i = 10; i >= 0; i--) {
598 char soname[256] = {0};
600 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
601 handle = dlopen(soname, flags);
602 if (handle != NULL) {
603 break;
607 nwrap_main_global->libc->sock_handle = handle;
609 break;
610 #endif
611 /* FALL TROUGH */
612 case NWRAP_LIBC:
613 handle = nwrap_main_global->libc->handle;
614 if (handle == NULL) {
615 for (i = 10; i >= 0; i--) {
616 char soname[256] = {0};
618 snprintf(soname, sizeof(soname), "libc.so.%d", i);
619 handle = dlopen(soname, flags);
620 if (handle != NULL) {
621 break;
625 nwrap_main_global->libc->handle = handle;
627 break;
630 if (handle == NULL) {
631 #ifdef RTLD_NEXT
632 handle = nwrap_main_global->libc->handle
633 = nwrap_main_global->libc->sock_handle
634 = nwrap_main_global->libc->nsl_handle
635 = RTLD_NEXT;
636 #else
637 NWRAP_LOG(NWRAP_LOG_ERROR,
638 "Failed to dlopen library: %s\n",
639 dlerror());
640 exit(-1);
641 #endif
644 return handle;
647 static void *_nwrap_load_lib_function(enum nwrap_lib lib, const char *fn_name)
649 void *handle;
650 void *func;
652 nwrap_init();
654 handle = nwrap_load_lib_handle(lib);
656 func = dlsym(handle, fn_name);
657 if (func == NULL) {
658 NWRAP_LOG(NWRAP_LOG_ERROR,
659 "Failed to find %s: %s\n",
660 fn_name, dlerror());
661 exit(-1);
664 NWRAP_LOG(NWRAP_LOG_TRACE,
665 "Loaded %s from %s",
666 fn_name, nwrap_str_lib(lib));
667 return func;
670 #define nwrap_load_lib_function(lib, fn_name) \
671 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
672 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
673 _nwrap_load_lib_function(lib, #fn_name); \
677 * IMPORTANT
679 * Functions expeciall from libc need to be loaded individually, you can't load
680 * all at once or gdb will segfault at startup. The same applies to valgrind and
681 * has probably something todo with with the linker.
682 * So we need load each function at the point it is called the first time.
684 static struct passwd *libc_getpwnam(const char *name)
686 nwrap_load_lib_function(NWRAP_LIBC, getpwnam);
688 return nwrap_main_global->libc->fns->_libc_getpwnam(name);
691 #ifdef HAVE_GETPWNAM_R
692 static int libc_getpwnam_r(const char *name,
693 struct passwd *pwd,
694 char *buf,
695 size_t buflen,
696 struct passwd **result)
698 #ifdef HAVE___POSIX_GETPWNAM_R
699 if (nwrap_main_global->libc->fns->_libc_getpwnam_r == NULL) {
700 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwnam_r) =
701 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwnam_r");
703 #else
704 nwrap_load_lib_function(NWRAP_LIBC, getpwnam_r);
705 #endif
707 return nwrap_main_global->libc->fns->_libc_getpwnam_r(name,
708 pwd,
709 buf,
710 buflen,
711 result);
713 #endif
715 static struct passwd *libc_getpwuid(uid_t uid)
717 nwrap_load_lib_function(NWRAP_LIBC, getpwuid);
719 return nwrap_main_global->libc->fns->_libc_getpwuid(uid);
722 #ifdef HAVE_GETPWUID_R
723 static int libc_getpwuid_r(uid_t uid,
724 struct passwd *pwd,
725 char *buf,
726 size_t buflen,
727 struct passwd **result)
729 #ifdef HAVE___POSIX_GETPWUID_R
730 if (nwrap_main_global->libc->fns->_libc_getpwuid_r == NULL) {
731 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwuid_r) =
732 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwuid_r");
734 #else
735 nwrap_load_lib_function(NWRAP_LIBC, getpwuid_r);
736 #endif
738 return nwrap_main_global->libc->fns->_libc_getpwuid_r(uid,
739 pwd,
740 buf,
741 buflen,
742 result);
744 #endif
746 static void libc_setpwent(void)
748 nwrap_load_lib_function(NWRAP_LIBC, setpwent);
750 nwrap_main_global->libc->fns->_libc_setpwent();
753 static struct passwd *libc_getpwent(void)
755 nwrap_load_lib_function(NWRAP_LIBC, getpwent);
757 return nwrap_main_global->libc->fns->_libc_getpwent();
760 #ifdef HAVE_SOLARIS_GETPWENT_R
761 static struct passwd *libc_getpwent_r(struct passwd *pwdst,
762 char *buf,
763 int buflen)
765 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
767 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
768 buf,
769 buflen);
771 #else /* HAVE_SOLARIS_GETPWENT_R */
772 static int libc_getpwent_r(struct passwd *pwdst,
773 char *buf,
774 size_t buflen,
775 struct passwd **pwdstp)
777 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
779 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
780 buf,
781 buflen,
782 pwdstp);
784 #endif /* HAVE_SOLARIS_GETPWENT_R */
786 static void libc_endpwent(void)
788 nwrap_load_lib_function(NWRAP_LIBC, endpwent);
790 nwrap_main_global->libc->fns->_libc_endpwent();
793 static int libc_initgroups(const char *user, gid_t gid)
795 nwrap_load_lib_function(NWRAP_LIBC, initgroups);
797 return nwrap_main_global->libc->fns->_libc_initgroups(user, gid);
800 static struct group *libc_getgrnam(const char *name)
802 nwrap_load_lib_function(NWRAP_LIBC, getgrnam);
804 return nwrap_main_global->libc->fns->_libc_getgrnam(name);
807 #ifdef HAVE_GETGRNAM_R
808 static int libc_getgrnam_r(const char *name,
809 struct group *grp,
810 char *buf,
811 size_t buflen,
812 struct group **result)
814 #ifdef HAVE___POSIX_GETGRNAM_R
815 if (nwrap_main_global->libc->fns->_libc_getgrnam_r == NULL) {
816 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrnam_r) =
817 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrnam_r");
819 #else
820 nwrap_load_lib_function(NWRAP_LIBC, getgrnam_r);
821 #endif
823 return nwrap_main_global->libc->fns->_libc_getgrnam_r(name,
824 grp,
825 buf,
826 buflen,
827 result);
829 #endif
831 static struct group *libc_getgrgid(gid_t gid)
833 nwrap_load_lib_function(NWRAP_LIBC, getgrgid);
835 return nwrap_main_global->libc->fns->_libc_getgrgid(gid);
838 #ifdef HAVE_GETGRGID_R
839 static int libc_getgrgid_r(gid_t gid,
840 struct group *grp,
841 char *buf,
842 size_t buflen,
843 struct group **result)
845 #ifdef HAVE___POSIX_GETGRGID_R
846 if (nwrap_main_global->libc->fns->_libc_getgrgid_r == NULL) {
847 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrgid_r) =
848 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrgid_r");
850 #else
851 nwrap_load_lib_function(NWRAP_LIBC, getgrgid_r);
852 #endif
854 return nwrap_main_global->libc->fns->_libc_getgrgid_r(gid,
855 grp,
856 buf,
857 buflen,
858 result);
860 #endif
862 static void libc_setgrent(void)
864 nwrap_load_lib_function(NWRAP_LIBC, setgrent);
866 nwrap_main_global->libc->fns->_libc_setgrent();
869 static struct group *libc_getgrent(void)
871 nwrap_load_lib_function(NWRAP_LIBC, getgrent);
873 return nwrap_main_global->libc->fns->_libc_getgrent();
876 #ifdef HAVE_GETGRENT_R
877 #ifdef HAVE_SOLARIS_GETGRENT_R
878 static struct group *libc_getgrent_r(struct group *group,
879 char *buf,
880 size_t buflen)
882 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
884 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
885 buf,
886 buflen);
888 #else /* !HAVE_SOLARIS_GETGRENT_R */
889 static int libc_getgrent_r(struct group *group,
890 char *buf,
891 size_t buflen,
892 struct group **result)
894 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
896 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
897 buf,
898 buflen,
899 result);
901 #endif /* HAVE_SOLARIS_GETGRENT_R */
902 #endif /* HAVE_GETGRENT_R */
904 static void libc_endgrent(void)
906 nwrap_load_lib_function(NWRAP_LIBC, endgrent);
908 nwrap_main_global->libc->fns->_libc_endgrent();
911 #ifdef HAVE_GETGROUPLIST
912 static int libc_getgrouplist(const char *user,
913 gid_t group,
914 gid_t *groups,
915 int *ngroups)
917 nwrap_load_lib_function(NWRAP_LIBC, getgrouplist);
919 return nwrap_main_global->libc->fns->_libc_getgrouplist(user,
920 group,
921 groups,
922 ngroups);
924 #endif
926 static void libc_sethostent(int stayopen)
928 nwrap_load_lib_function(NWRAP_LIBNSL, sethostent);
930 nwrap_main_global->libc->fns->_libc_sethostent(stayopen);
933 static struct hostent *libc_gethostent(void)
935 nwrap_load_lib_function(NWRAP_LIBNSL, gethostent);
937 return nwrap_main_global->libc->fns->_libc_gethostent();
940 static void libc_endhostent(void)
942 nwrap_load_lib_function(NWRAP_LIBNSL, endhostent);
944 nwrap_main_global->libc->fns->_libc_endhostent();
947 static struct hostent *libc_gethostbyname(const char *name)
949 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname);
951 return nwrap_main_global->libc->fns->_libc_gethostbyname(name);
954 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
955 static struct hostent *libc_gethostbyname2(const char *name, int af)
957 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname2);
959 return nwrap_main_global->libc->fns->_libc_gethostbyname2(name, af);
961 #endif
963 static struct hostent *libc_gethostbyaddr(const void *addr,
964 socklen_t len,
965 int type)
967 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr);
969 return nwrap_main_global->libc->fns->_libc_gethostbyaddr(addr,
970 len,
971 type);
974 static int libc_gethostname(char *name, size_t len)
976 nwrap_load_lib_function(NWRAP_LIBNSL, gethostname);
978 return nwrap_main_global->libc->fns->_libc_gethostname(name, len);
981 #ifdef HAVE_GETHOSTBYNAME_R
982 static int libc_gethostbyname_r(const char *name,
983 struct hostent *ret,
984 char *buf,
985 size_t buflen,
986 struct hostent **result,
987 int *h_errnop)
989 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname_r);
991 return nwrap_main_global->libc->fns->_libc_gethostbyname_r(name,
992 ret,
993 buf,
994 buflen,
995 result,
996 h_errnop);
998 #endif
1000 #ifdef HAVE_GETHOSTBYADDR_R
1001 static int libc_gethostbyaddr_r(const void *addr,
1002 socklen_t len,
1003 int type,
1004 struct hostent *ret,
1005 char *buf,
1006 size_t buflen,
1007 struct hostent **result,
1008 int *h_errnop)
1010 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr_r);
1012 return nwrap_main_global->libc->fns->_libc_gethostbyaddr_r(addr,
1013 len,
1014 type,
1015 ret,
1016 buf,
1017 buflen,
1018 result,
1019 h_errnop);
1021 #endif
1023 static int libc_getaddrinfo(const char *node,
1024 const char *service,
1025 const struct addrinfo *hints,
1026 struct addrinfo **res)
1028 nwrap_load_lib_function(NWRAP_LIBSOCKET, getaddrinfo);
1030 return nwrap_main_global->libc->fns->_libc_getaddrinfo(node,
1031 service,
1032 hints,
1033 res);
1036 static int libc_getnameinfo(const struct sockaddr *sa,
1037 socklen_t salen,
1038 char *host,
1039 size_t hostlen,
1040 char *serv,
1041 size_t servlen,
1042 int flags)
1044 nwrap_load_lib_function(NWRAP_LIBSOCKET, getnameinfo);
1046 return nwrap_main_global->libc->fns->_libc_getnameinfo(sa,
1047 salen,
1048 host,
1049 hostlen,
1050 serv,
1051 servlen,
1052 flags);
1055 /*********************************************************
1056 * NWRAP NSS MODULE LOADER FUNCTIONS
1057 *********************************************************/
1059 static void *nwrap_load_module_fn(struct nwrap_backend *b,
1060 const char *fn_name)
1062 void *res;
1063 char *s;
1065 if (!b->so_handle) {
1066 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1067 return NULL;
1070 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
1071 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1072 return NULL;
1075 res = dlsym(b->so_handle, s);
1076 if (!res) {
1077 NWRAP_LOG(NWRAP_LOG_ERROR,
1078 "Cannot find function %s in %s",
1079 s, b->so_path);
1081 free(s);
1082 s = NULL;
1083 return res;
1086 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
1088 struct nwrap_module_nss_fns *fns;
1090 if (!b->so_handle) {
1091 return NULL;
1094 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
1095 if (!fns) {
1096 return NULL;
1099 *(void **)(&fns->_nss_getpwnam_r) =
1100 nwrap_load_module_fn(b, "getpwnam_r");
1101 *(void **)(&fns->_nss_getpwuid_r) =
1102 nwrap_load_module_fn(b, "getpwuid_r");
1103 *(void **)(&fns->_nss_setpwent) =
1104 nwrap_load_module_fn(b, "setpwent");
1105 *(void **)(&fns->_nss_getpwent_r) =
1106 nwrap_load_module_fn(b, "getpwent_r");
1107 *(void **)(&fns->_nss_endpwent) =
1108 nwrap_load_module_fn(b, "endpwent");
1109 *(void **)(&fns->_nss_initgroups) =
1110 nwrap_load_module_fn(b, "initgroups_dyn");
1111 *(void **)(&fns->_nss_getgrnam_r) =
1112 nwrap_load_module_fn(b, "getgrnam_r");
1113 *(void **)(&fns->_nss_getgrgid_r)=
1114 nwrap_load_module_fn(b, "getgrgid_r");
1115 *(void **)(&fns->_nss_setgrent) =
1116 nwrap_load_module_fn(b, "setgrent");
1117 *(void **)(&fns->_nss_getgrent_r) =
1118 nwrap_load_module_fn(b, "getgrent_r");
1119 *(void **)(&fns->_nss_endgrent) =
1120 nwrap_load_module_fn(b, "endgrent");
1122 return fns;
1125 static void *nwrap_load_module(const char *so_path)
1127 void *h;
1129 if (!so_path || !strlen(so_path)) {
1130 return NULL;
1133 h = dlopen(so_path, RTLD_LAZY);
1134 if (!h) {
1135 NWRAP_LOG(NWRAP_LOG_ERROR,
1136 "Cannot open shared library %s",
1137 so_path);
1138 return NULL;
1141 return h;
1144 static bool nwrap_module_init(const char *name,
1145 struct nwrap_ops *ops,
1146 const char *so_path,
1147 int *num_backends,
1148 struct nwrap_backend **backends)
1150 struct nwrap_backend *b;
1152 *backends = (struct nwrap_backend *)realloc(*backends,
1153 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
1154 if (!*backends) {
1155 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1156 return false;
1159 b = &((*backends)[*num_backends]);
1161 b->name = name;
1162 b->ops = ops;
1163 b->so_path = so_path;
1165 if (so_path != NULL) {
1166 b->so_handle = nwrap_load_module(so_path);
1167 b->fns = nwrap_load_module_fns(b);
1168 if (b->fns == NULL) {
1169 return false;
1171 } else {
1172 b->so_handle = NULL;
1173 b->fns = NULL;
1176 (*num_backends)++;
1178 return true;
1181 static void nwrap_libc_init(struct nwrap_main *r)
1183 r->libc = malloc(sizeof(struct nwrap_libc));
1184 if (r->libc == NULL) {
1185 printf("Failed to allocate memory for libc");
1186 exit(-1);
1188 ZERO_STRUCTP(r->libc);
1190 r->libc->fns = malloc(sizeof(struct nwrap_libc_fns));
1191 if (r->libc->fns == NULL) {
1192 printf("Failed to allocate memory for libc functions");
1193 exit(-1);
1195 ZERO_STRUCTP(r->libc->fns);
1198 static void nwrap_backend_init(struct nwrap_main *r)
1200 const char *module_so_path = getenv("NSS_WRAPPER_MODULE_SO_PATH");
1201 const char *module_fn_name = getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1203 r->num_backends = 0;
1204 r->backends = NULL;
1206 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1207 &r->num_backends,
1208 &r->backends)) {
1209 NWRAP_LOG(NWRAP_LOG_ERROR,
1210 "Failed to initialize 'files' backend");
1211 return;
1214 if (module_so_path != NULL &&
1215 module_so_path[0] != '\0' &&
1216 module_fn_name != NULL &&
1217 module_fn_name[0] != '\0') {
1218 if (!nwrap_module_init(module_fn_name,
1219 &nwrap_module_ops,
1220 module_so_path,
1221 &r->num_backends,
1222 &r->backends)) {
1223 NWRAP_LOG(NWRAP_LOG_ERROR,
1224 "Failed to initialize '%s' backend",
1225 module_fn_name);
1226 return;
1231 static void nwrap_init(void)
1233 static bool initialized;
1235 if (initialized) return;
1236 initialized = true;
1238 nwrap_main_global = &__nwrap_main_global;
1240 nwrap_libc_init(nwrap_main_global);
1242 nwrap_backend_init(nwrap_main_global);
1244 nwrap_pw_global.cache = &__nwrap_cache_pw;
1246 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
1247 nwrap_pw_global.cache->fd = -1;
1248 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
1249 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
1250 nwrap_pw_global.cache->unload = nwrap_pw_unload;
1252 nwrap_gr_global.cache = &__nwrap_cache_gr;
1254 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
1255 nwrap_gr_global.cache->fd = -1;
1256 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
1257 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
1258 nwrap_gr_global.cache->unload = nwrap_gr_unload;
1260 nwrap_he_global.cache = &__nwrap_cache_he;
1262 nwrap_he_global.cache->path = getenv("NSS_WRAPPER_HOSTS");
1263 nwrap_he_global.cache->fd = -1;
1264 nwrap_he_global.cache->private_data = &nwrap_he_global;
1265 nwrap_he_global.cache->parse_line = nwrap_he_parse_line;
1266 nwrap_he_global.cache->unload = nwrap_he_unload;
1269 bool nss_wrapper_enabled(void)
1271 nwrap_init();
1273 if (nwrap_pw_global.cache->path == NULL ||
1274 nwrap_pw_global.cache->path[0] == '\0') {
1275 return false;
1277 if (nwrap_gr_global.cache->path == NULL ||
1278 nwrap_gr_global.cache->path[0] == '\0') {
1279 return false;
1282 return true;
1285 bool nss_wrapper_hosts_enabled(void)
1287 nwrap_init();
1289 if (nwrap_he_global.cache->path == NULL ||
1290 nwrap_he_global.cache->path[0] == '\0') {
1291 return false;
1294 return true;
1297 static bool nwrap_hostname_enabled(void)
1299 nwrap_init();
1301 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
1302 return false;
1305 return true;
1308 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
1310 int ret;
1311 uint8_t *buf = NULL;
1312 char *nline;
1314 if (nwrap->st.st_size == 0) {
1315 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
1316 goto done;
1319 if (nwrap->st.st_size > INT32_MAX) {
1320 NWRAP_LOG(NWRAP_LOG_ERROR,
1321 "Size[%u] larger than INT32_MAX",
1322 (unsigned)nwrap->st.st_size);
1323 goto failed;
1326 ret = lseek(nwrap->fd, 0, SEEK_SET);
1327 if (ret != 0) {
1328 NWRAP_LOG(NWRAP_LOG_ERROR, "lseek - rc=%d\n", ret);
1329 goto failed;
1332 buf = (uint8_t *)malloc(nwrap->st.st_size + 1);
1333 if (!buf) {
1334 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1335 goto failed;
1338 ret = read(nwrap->fd, buf, nwrap->st.st_size);
1339 if (ret != nwrap->st.st_size) {
1340 NWRAP_LOG(NWRAP_LOG_ERROR,
1341 "read(%u) rc=%d\n",
1342 (unsigned)nwrap->st.st_size, ret);
1343 goto failed;
1346 buf[nwrap->st.st_size] = '\0';
1348 nline = (char *)buf;
1349 while (nline != NULL && nline[0] != '\0') {
1350 char *line;
1351 char *e;
1352 bool ok;
1354 line = nline;
1355 nline = NULL;
1357 e = strchr(line, '\n');
1358 if (e) {
1359 e[0] = '\0';
1360 e++;
1361 if (e[0] == '\r') {
1362 e[0] = '\0';
1363 e++;
1365 nline = e;
1368 if (strlen(line) == 0) {
1369 continue;
1372 ok = nwrap->parse_line(nwrap, line);
1373 if (!ok) {
1374 goto failed;
1378 done:
1379 nwrap->buf = buf;
1380 return true;
1382 failed:
1383 if (buf) free(buf);
1384 return false;
1387 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
1389 nwrap->unload(nwrap);
1391 if (nwrap->buf) free(nwrap->buf);
1393 nwrap->buf = NULL;
1396 static void nwrap_files_cache_reload(struct nwrap_cache *nwrap)
1398 struct stat st;
1399 int ret;
1400 bool ok;
1401 bool retried = false;
1403 reopen:
1404 if (nwrap->fd < 0) {
1405 nwrap->fd = open(nwrap->path, O_RDONLY);
1406 if (nwrap->fd < 0) {
1407 NWRAP_LOG(NWRAP_LOG_ERROR,
1408 "Unable to open '%s' readonly %d:%s",
1409 nwrap->path, nwrap->fd,
1410 strerror(errno));
1411 return;
1413 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
1416 ret = fstat(nwrap->fd, &st);
1417 if (ret != 0) {
1418 NWRAP_LOG(NWRAP_LOG_ERROR,
1419 "fstat(%s) - %d:%s",
1420 nwrap->path,
1421 ret,
1422 strerror(errno));
1423 return;
1426 if (retried == false && st.st_nlink == 0) {
1427 /* maybe someone has replaced the file... */
1428 NWRAP_LOG(NWRAP_LOG_TRACE,
1429 "st_nlink == 0, reopen %s",
1430 nwrap->path);
1431 retried = true;
1432 memset(&nwrap->st, 0, sizeof(nwrap->st));
1433 close(nwrap->fd);
1434 nwrap->fd = -1;
1435 goto reopen;
1438 if (st.st_mtime == nwrap->st.st_mtime) {
1439 NWRAP_LOG(NWRAP_LOG_TRACE,
1440 "st_mtime[%u] hasn't changed, skip reload",
1441 (unsigned)st.st_mtime);
1442 return;
1445 NWRAP_LOG(NWRAP_LOG_TRACE,
1446 "st_mtime has changed [%u] => [%u], start reload",
1447 (unsigned)st.st_mtime,
1448 (unsigned)nwrap->st.st_mtime);
1450 nwrap->st = st;
1452 nwrap_files_cache_unload(nwrap);
1454 ok = nwrap_parse_file(nwrap);
1455 if (!ok) {
1456 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
1457 nwrap_files_cache_unload(nwrap);
1460 NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
1464 * the caller has to call nwrap_unload() on failure
1466 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
1468 struct nwrap_pw *nwrap_pw;
1469 char *c;
1470 char *p;
1471 char *e;
1472 struct passwd *pw;
1473 size_t list_size;
1475 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1477 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
1478 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
1479 if (!pw) {
1480 NWRAP_LOG(NWRAP_LOG_ERROR,
1481 "realloc(%u) failed",
1482 (unsigned)list_size);
1483 return false;
1485 nwrap_pw->list = pw;
1487 pw = &nwrap_pw->list[nwrap_pw->num];
1489 c = line;
1491 /* name */
1492 p = strchr(c, ':');
1493 if (!p) {
1494 NWRAP_LOG(NWRAP_LOG_ERROR,
1495 "Invalid line[%s]: '%s'",
1496 line,
1498 return false;
1500 *p = '\0';
1501 p++;
1502 pw->pw_name = c;
1503 c = p;
1505 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
1507 /* password */
1508 p = strchr(c, ':');
1509 if (!p) {
1510 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1511 return false;
1513 *p = '\0';
1514 p++;
1515 pw->pw_passwd = c;
1516 c = p;
1518 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
1520 /* uid */
1521 p = strchr(c, ':');
1522 if (!p) {
1523 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1524 return false;
1526 *p = '\0';
1527 p++;
1528 e = NULL;
1529 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
1530 if (c == e) {
1531 NWRAP_LOG(NWRAP_LOG_ERROR,
1532 "Invalid line[%s]: '%s' - %s",
1533 line, c, strerror(errno));
1534 return false;
1536 if (e == NULL) {
1537 NWRAP_LOG(NWRAP_LOG_ERROR,
1538 "Invalid line[%s]: '%s' - %s",
1539 line, c, strerror(errno));
1540 return false;
1542 if (e[0] != '\0') {
1543 NWRAP_LOG(NWRAP_LOG_ERROR,
1544 "Invalid line[%s]: '%s' - %s",
1545 line, c, strerror(errno));
1546 return false;
1548 c = p;
1550 NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
1552 /* gid */
1553 p = strchr(c, ':');
1554 if (!p) {
1555 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1556 return false;
1558 *p = '\0';
1559 p++;
1560 e = NULL;
1561 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
1562 if (c == e) {
1563 NWRAP_LOG(NWRAP_LOG_ERROR,
1564 "Invalid line[%s]: '%s' - %s",
1565 line, c, strerror(errno));
1566 return false;
1568 if (e == NULL) {
1569 NWRAP_LOG(NWRAP_LOG_ERROR,
1570 "Invalid line[%s]: '%s' - %s",
1571 line, c, strerror(errno));
1572 return false;
1574 if (e[0] != '\0') {
1575 NWRAP_LOG(NWRAP_LOG_ERROR,
1576 "Invalid line[%s]: '%s' - %s",
1577 line, c, strerror(errno));
1578 return false;
1580 c = p;
1582 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]\n", pw->pw_gid);
1584 /* gecos */
1585 p = strchr(c, ':');
1586 if (!p) {
1587 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
1588 return false;
1590 *p = '\0';
1591 p++;
1592 pw->pw_gecos = c;
1593 c = p;
1595 NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
1597 /* dir */
1598 p = strchr(c, ':');
1599 if (!p) {
1600 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
1601 return false;
1603 *p = '\0';
1604 p++;
1605 pw->pw_dir = c;
1606 c = p;
1608 NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
1610 /* shell */
1611 pw->pw_shell = c;
1612 NWRAP_LOG(NWRAP_LOG_TRACE, "shell[%s]", pw->pw_shell);
1614 NWRAP_LOG(NWRAP_LOG_DEBUG,
1615 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1616 pw->pw_name, pw->pw_passwd,
1617 pw->pw_uid, pw->pw_gid,
1618 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
1620 nwrap_pw->num++;
1621 return true;
1624 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
1626 struct nwrap_pw *nwrap_pw;
1627 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1629 if (nwrap_pw->list) free(nwrap_pw->list);
1631 nwrap_pw->list = NULL;
1632 nwrap_pw->num = 0;
1633 nwrap_pw->idx = 0;
1636 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
1637 char *buf, size_t buflen, struct passwd **dstp)
1639 char *first;
1640 char *last;
1641 off_t ofs;
1643 first = src->pw_name;
1645 last = src->pw_shell;
1646 while (*last) last++;
1648 ofs = PTR_DIFF(last + 1, first);
1650 if (ofs > (off_t) buflen) {
1651 return ERANGE;
1654 memcpy(buf, first, ofs);
1656 ofs = PTR_DIFF(src->pw_name, first);
1657 dst->pw_name = buf + ofs;
1658 ofs = PTR_DIFF(src->pw_passwd, first);
1659 dst->pw_passwd = buf + ofs;
1660 dst->pw_uid = src->pw_uid;
1661 dst->pw_gid = src->pw_gid;
1662 ofs = PTR_DIFF(src->pw_gecos, first);
1663 dst->pw_gecos = buf + ofs;
1664 ofs = PTR_DIFF(src->pw_dir, first);
1665 dst->pw_dir = buf + ofs;
1666 ofs = PTR_DIFF(src->pw_shell, first);
1667 dst->pw_shell = buf + ofs;
1669 if (dstp) {
1670 *dstp = dst;
1673 return 0;
1677 * the caller has to call nwrap_unload() on failure
1679 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
1681 struct nwrap_gr *nwrap_gr;
1682 char *c;
1683 char *p;
1684 char *e;
1685 struct group *gr;
1686 size_t list_size;
1687 unsigned nummem;
1689 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
1691 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
1692 gr = (struct group *)realloc(nwrap_gr->list, list_size);
1693 if (!gr) {
1694 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
1695 return false;
1697 nwrap_gr->list = gr;
1699 gr = &nwrap_gr->list[nwrap_gr->num];
1701 c = line;
1703 /* name */
1704 p = strchr(c, ':');
1705 if (!p) {
1706 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1707 return false;
1709 *p = '\0';
1710 p++;
1711 gr->gr_name = c;
1712 c = p;
1714 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
1716 /* password */
1717 p = strchr(c, ':');
1718 if (!p) {
1719 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1720 return false;
1722 *p = '\0';
1723 p++;
1724 gr->gr_passwd = c;
1725 c = p;
1727 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
1729 /* gid */
1730 p = strchr(c, ':');
1731 if (!p) {
1732 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1733 return false;
1735 *p = '\0';
1736 p++;
1737 e = NULL;
1738 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
1739 if (c == e) {
1740 NWRAP_LOG(NWRAP_LOG_ERROR,
1741 "Invalid line[%s]: '%s' - %s",
1742 line, c, strerror(errno));
1743 return false;
1745 if (e == NULL) {
1746 NWRAP_LOG(NWRAP_LOG_ERROR,
1747 "Invalid line[%s]: '%s' - %s",
1748 line, c, strerror(errno));
1749 return false;
1751 if (e[0] != '\0') {
1752 NWRAP_LOG(NWRAP_LOG_ERROR,
1753 "Invalid line[%s]: '%s' - %s",
1754 line, c, strerror(errno));
1755 return false;
1757 c = p;
1759 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
1761 /* members */
1762 gr->gr_mem = (char **)malloc(sizeof(char *));
1763 if (!gr->gr_mem) {
1764 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1765 return false;
1767 gr->gr_mem[0] = NULL;
1769 for(nummem=0; p; nummem++) {
1770 char **m;
1771 size_t m_size;
1772 c = p;
1773 p = strchr(c, ',');
1774 if (p) {
1775 *p = '\0';
1776 p++;
1779 if (strlen(c) == 0) {
1780 break;
1783 m_size = sizeof(char *) * (nummem+2);
1784 m = (char **)realloc(gr->gr_mem, m_size);
1785 if (!m) {
1786 NWRAP_LOG(NWRAP_LOG_ERROR,
1787 "realloc(%zd) failed",
1788 m_size);
1789 return false;
1791 gr->gr_mem = m;
1792 gr->gr_mem[nummem] = c;
1793 gr->gr_mem[nummem+1] = NULL;
1795 NWRAP_LOG(NWRAP_LOG_TRACE,
1796 "member[%u]: '%s'",
1797 nummem, gr->gr_mem[nummem]);
1800 NWRAP_LOG(NWRAP_LOG_DEBUG,
1801 "Added group[%s:%s:%u:] with %u members",
1802 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem);
1804 nwrap_gr->num++;
1805 return true;
1808 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
1810 int i;
1811 struct nwrap_gr *nwrap_gr;
1812 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
1814 if (nwrap_gr->list) {
1815 for (i=0; i < nwrap_gr->num; i++) {
1816 if (nwrap_gr->list[i].gr_mem) {
1817 free(nwrap_gr->list[i].gr_mem);
1820 free(nwrap_gr->list);
1823 nwrap_gr->list = NULL;
1824 nwrap_gr->num = 0;
1825 nwrap_gr->idx = 0;
1828 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
1829 char *buf, size_t buflen, struct group **dstp)
1831 char *first;
1832 char **lastm;
1833 char *last = NULL;
1834 off_t ofsb;
1835 off_t ofsm;
1836 off_t ofs;
1837 unsigned i;
1839 first = src->gr_name;
1841 lastm = src->gr_mem;
1842 while (*lastm) {
1843 last = *lastm;
1844 lastm++;
1847 if (last == NULL) {
1848 last = src->gr_passwd;
1850 while (*last) last++;
1852 ofsb = PTR_DIFF(last + 1, first);
1853 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
1855 if ((ofsb + ofsm) > (off_t) buflen) {
1856 return ERANGE;
1859 memcpy(buf, first, ofsb);
1860 memcpy(buf + ofsb, src->gr_mem, ofsm);
1862 ofs = PTR_DIFF(src->gr_name, first);
1863 dst->gr_name = buf + ofs;
1864 ofs = PTR_DIFF(src->gr_passwd, first);
1865 dst->gr_passwd = buf + ofs;
1866 dst->gr_gid = src->gr_gid;
1868 dst->gr_mem = (char **)(buf + ofsb);
1869 for (i=0; src->gr_mem[i]; i++) {
1870 ofs = PTR_DIFF(src->gr_mem[i], first);
1871 dst->gr_mem[i] = buf + ofs;
1874 if (dstp) {
1875 *dstp = dst;
1878 return 0;
1881 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line)
1883 struct nwrap_he *nwrap_he = (struct nwrap_he *)nwrap->private_data;
1884 struct nwrap_entdata *ed;
1885 size_t list_size;
1886 bool do_aliases = true;
1887 int aliases_count = 0;
1888 char *p;
1889 char *i;
1890 char *n;
1892 list_size = sizeof(struct nwrap_entdata) * (nwrap_he->num + 1);
1894 ed = (struct nwrap_entdata *)realloc(nwrap_he->list, list_size);
1895 if (ed == NULL) {
1896 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc[%zd] failed", list_size);
1897 return false;
1899 nwrap_he->list = ed;
1901 /* set it to the last element */
1902 ed = &(nwrap_he->list[nwrap_he->num]);
1904 ed->addr = malloc(sizeof(struct nwrap_addrdata));
1905 if (ed->addr == NULL) {
1906 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc[%zd] failed", list_size);
1907 return false;
1910 i = line;
1913 * IP
1916 /* Walk to first char */
1917 for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
1918 if (*p == '\0') {
1919 NWRAP_LOG(NWRAP_LOG_ERROR,
1920 "Invalid line[%s]: '%s'",
1921 line, i);
1922 return false;
1926 for (i = p; !isspace((int)*p); p++) {
1927 if (*p == '\0') {
1928 NWRAP_LOG(NWRAP_LOG_ERROR,
1929 "Invalid line[%s]: '%s'",
1930 line, i);
1931 return false;
1935 *p = '\0';
1937 if (inet_pton(AF_INET, i, ed->addr->host_addr)) {
1938 ed->ht.h_addrtype = AF_INET;
1939 ed->ht.h_length = 4;
1940 #ifdef HAVE_IPV6
1941 } else if (inet_pton(AF_INET6, i, ed->addr->host_addr)) {
1942 ed->ht.h_addrtype = AF_INET6;
1943 ed->ht.h_length = 16;
1944 #endif
1945 } else {
1946 NWRAP_LOG(NWRAP_LOG_ERROR,
1947 "Invalid line[%s]: '%s'",
1948 line, i);
1950 return false;
1953 ed->addr->h_addr_ptrs[0] = (char *)ed->addr->host_addr;
1954 ed->addr->h_addr_ptrs[1] = NULL;
1956 ed->ht.h_addr_list = ed->addr->h_addr_ptrs;
1958 p++;
1961 * FQDN
1964 /* Walk to first char */
1965 for (n = p; *p != '_' && !isalnum((int) *p); p++) {
1966 if (*p == '\0') {
1967 NWRAP_LOG(NWRAP_LOG_ERROR,
1968 "Invalid line[%s]: '%s'",
1969 line, n);
1971 return false;
1975 for (n = p; !isspace((int)*p); p++) {
1976 if (*p == '\0') {
1977 do_aliases = false;
1978 break;
1982 *p = '\0';
1984 ed->ht.h_name = n;
1986 /* glib's getent always dereferences he->h_aliases */
1987 ed->ht.h_aliases = malloc(sizeof(char *));
1988 if (ed->ht.h_aliases == NULL) {
1989 return false;
1991 ed->ht.h_aliases[0] = NULL;
1994 * Aliases
1996 while (do_aliases) {
1997 char **aliases;
1998 char *a;
2000 p++;
2002 /* Walk to first char */
2003 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
2004 if (*p == '\0') {
2005 do_aliases = false;
2006 break;
2009 /* Only trailing spaces are left */
2010 if (!do_aliases) {
2011 break;
2014 for (a = p; !isspace((int)*p); p++) {
2015 if (*p == '\0') {
2016 do_aliases = false;
2017 break;
2021 *p = '\0';
2023 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
2024 if (aliases == NULL) {
2025 return false;
2027 ed->ht.h_aliases = aliases;
2029 aliases[aliases_count] = a;
2030 aliases[aliases_count + 1] = NULL;
2032 aliases_count++;
2035 nwrap_he->num++;
2036 return true;
2039 static void nwrap_he_unload(struct nwrap_cache *nwrap)
2041 struct nwrap_he *nwrap_he =
2042 (struct nwrap_he *)nwrap->private_data;
2043 int i;
2045 if (nwrap_he->list != NULL) {
2046 for (i = 0; i < nwrap_he->num; i++) {
2047 if (nwrap_he->list[i].ht.h_aliases != NULL) {
2048 free(nwrap_he->list[i].ht.h_aliases);
2050 if (nwrap_he->list[i].addr != NULL) {
2051 free(nwrap_he->list[i].addr);
2054 free(nwrap_he->list);
2057 nwrap_he->list = NULL;
2058 nwrap_he->num = 0;
2059 nwrap_he->idx = 0;
2063 /* user functions */
2064 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
2065 const char *name)
2067 int i;
2069 (void) b; /* unused */
2071 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
2073 nwrap_files_cache_reload(nwrap_pw_global.cache);
2075 for (i=0; i<nwrap_pw_global.num; i++) {
2076 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
2077 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
2078 return &nwrap_pw_global.list[i];
2080 NWRAP_LOG(NWRAP_LOG_DEBUG,
2081 "user[%s] does not match [%s]",
2082 name,
2083 nwrap_pw_global.list[i].pw_name);
2086 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
2088 errno = ENOENT;
2089 return NULL;
2092 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
2093 const char *name, struct passwd *pwdst,
2094 char *buf, size_t buflen, struct passwd **pwdstp)
2096 struct passwd *pw;
2098 pw = nwrap_files_getpwnam(b, name);
2099 if (!pw) {
2100 if (errno == 0) {
2101 return ENOENT;
2103 return errno;
2106 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2109 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
2110 uid_t uid)
2112 int i;
2114 (void) b; /* unused */
2116 nwrap_files_cache_reload(nwrap_pw_global.cache);
2118 for (i=0; i<nwrap_pw_global.num; i++) {
2119 if (nwrap_pw_global.list[i].pw_uid == uid) {
2120 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] found", uid);
2121 return &nwrap_pw_global.list[i];
2123 NWRAP_LOG(NWRAP_LOG_DEBUG,
2124 "uid[%u] does not match [%u]",
2125 uid,
2126 nwrap_pw_global.list[i].pw_uid);
2129 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
2131 errno = ENOENT;
2132 return NULL;
2135 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
2136 uid_t uid, struct passwd *pwdst,
2137 char *buf, size_t buflen, struct passwd **pwdstp)
2139 struct passwd *pw;
2141 pw = nwrap_files_getpwuid(b, uid);
2142 if (!pw) {
2143 if (errno == 0) {
2144 return ENOENT;
2146 return errno;
2149 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2152 /* user enum functions */
2153 static void nwrap_files_setpwent(struct nwrap_backend *b)
2155 (void) b; /* unused */
2157 nwrap_pw_global.idx = 0;
2160 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
2162 struct passwd *pw;
2164 (void) b; /* unused */
2166 if (nwrap_pw_global.idx == 0) {
2167 nwrap_files_cache_reload(nwrap_pw_global.cache);
2170 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
2171 errno = ENOENT;
2172 return NULL;
2175 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
2177 NWRAP_LOG(NWRAP_LOG_DEBUG,
2178 "return user[%s] uid[%u]",
2179 pw->pw_name, pw->pw_uid);
2181 return pw;
2184 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
2185 struct passwd *pwdst, char *buf,
2186 size_t buflen, struct passwd **pwdstp)
2188 struct passwd *pw;
2190 pw = nwrap_files_getpwent(b);
2191 if (!pw) {
2192 if (errno == 0) {
2193 return ENOENT;
2195 return errno;
2198 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2201 static void nwrap_files_endpwent(struct nwrap_backend *b)
2203 (void) b; /* unused */
2205 nwrap_pw_global.idx = 0;
2208 /* misc functions */
2209 static int nwrap_files_initgroups(struct nwrap_backend *b,
2210 const char *user, gid_t group)
2212 (void) b; /* unused */
2213 (void) user; /* unused */
2214 (void) group; /* used */
2216 /* TODO: maybe we should also fake this... */
2217 return EPERM;
2220 /* group functions */
2221 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
2222 const char *name)
2224 int i;
2226 (void) b; /* unused */
2228 nwrap_files_cache_reload(nwrap_gr_global.cache);
2230 for (i=0; i<nwrap_gr_global.num; i++) {
2231 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
2232 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] found", name);
2233 return &nwrap_gr_global.list[i];
2235 NWRAP_LOG(NWRAP_LOG_DEBUG,
2236 "group[%s] does not match [%s]",
2237 name,
2238 nwrap_gr_global.list[i].gr_name);
2241 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
2243 errno = ENOENT;
2244 return NULL;
2247 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
2248 const char *name, struct group *grdst,
2249 char *buf, size_t buflen, struct group **grdstp)
2251 struct group *gr;
2253 gr = nwrap_files_getgrnam(b, name);
2254 if (!gr) {
2255 if (errno == 0) {
2256 return ENOENT;
2258 return errno;
2261 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
2264 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
2265 gid_t gid)
2267 int i;
2269 (void) b; /* unused */
2271 nwrap_files_cache_reload(nwrap_gr_global.cache);
2273 for (i=0; i<nwrap_gr_global.num; i++) {
2274 if (nwrap_gr_global.list[i].gr_gid == gid) {
2275 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] found", gid);
2276 return &nwrap_gr_global.list[i];
2278 NWRAP_LOG(NWRAP_LOG_DEBUG,
2279 "gid[%u] does not match [%u]",
2280 gid,
2281 nwrap_gr_global.list[i].gr_gid);
2284 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
2286 errno = ENOENT;
2287 return NULL;
2290 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
2291 gid_t gid, struct group *grdst,
2292 char *buf, size_t buflen, struct group **grdstp)
2294 struct group *gr;
2296 gr = nwrap_files_getgrgid(b, gid);
2297 if (!gr) {
2298 if (errno == 0) {
2299 return ENOENT;
2301 return errno;
2304 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
2307 /* group enum functions */
2308 static void nwrap_files_setgrent(struct nwrap_backend *b)
2310 (void) b; /* unused */
2312 nwrap_gr_global.idx = 0;
2315 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
2317 struct group *gr;
2319 (void) b; /* unused */
2321 if (nwrap_gr_global.idx == 0) {
2322 nwrap_files_cache_reload(nwrap_gr_global.cache);
2325 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
2326 errno = ENOENT;
2327 return NULL;
2330 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
2332 NWRAP_LOG(NWRAP_LOG_DEBUG,
2333 "return group[%s] gid[%u]",
2334 gr->gr_name, gr->gr_gid);
2336 return gr;
2339 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
2340 struct group *grdst, char *buf,
2341 size_t buflen, struct group **grdstp)
2343 struct group *gr;
2345 gr = nwrap_files_getgrent(b);
2346 if (!gr) {
2347 if (errno == 0) {
2348 return ENOENT;
2350 return errno;
2353 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
2356 static void nwrap_files_endgrent(struct nwrap_backend *b)
2358 (void) b; /* unused */
2360 nwrap_gr_global.idx = 0;
2363 /* hosts functions */
2364 static struct hostent *nwrap_files_gethostbyname(const char *name, int af)
2366 struct hostent *he;
2367 char canon_name[DNS_NAME_MAX] = { 0 };
2368 size_t name_len;
2369 int i;
2371 nwrap_files_cache_reload(nwrap_he_global.cache);
2373 name_len = strlen(name);
2374 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
2375 strncpy(canon_name, name, name_len - 1);
2376 name = canon_name;
2379 for (i = 0; i < nwrap_he_global.num; i++) {
2380 int j;
2382 he = &nwrap_he_global.list[i].ht;
2384 /* Filter by address familiy if provided */
2385 if (af != AF_UNSPEC && he->h_addrtype != af) {
2386 continue;
2389 if (strcasecmp(he->h_name, name) == 0) {
2390 NWRAP_LOG(NWRAP_LOG_DEBUG, "name[%s] found", name);
2391 return he;
2394 if (he->h_aliases == NULL) {
2395 continue;
2398 for (j = 0; he->h_aliases[j] != NULL; j++) {
2399 if (strcasecmp(he->h_aliases[j], name) == 0) {
2400 NWRAP_LOG(NWRAP_LOG_DEBUG,
2401 "name[%s] found",
2402 name);
2403 return he;
2408 errno = ENOENT;
2409 return NULL;
2412 #ifdef HAVE_GETHOSTBYNAME_R
2413 static int nwrap_gethostbyname_r(const char *name,
2414 struct hostent *ret,
2415 char *buf, size_t buflen,
2416 struct hostent **result, int *h_errnop)
2418 *result = nwrap_files_gethostbyname(name, AF_UNSPEC);
2419 if (*result != NULL) {
2420 memset(buf, '\0', buflen);
2421 *ret = **result;
2422 return 0;
2423 } else {
2424 *h_errnop = h_errno;
2425 return -1;
2429 int gethostbyname_r(const char *name,
2430 struct hostent *ret,
2431 char *buf, size_t buflen,
2432 struct hostent **result, int *h_errnop)
2434 if (!nss_wrapper_hosts_enabled()) {
2435 return libc_gethostbyname_r(name,
2436 ret,
2437 buf,
2438 buflen,
2439 result,
2440 h_errnop);
2443 return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
2445 #endif
2447 static struct hostent *nwrap_files_gethostbyaddr(const void *addr,
2448 socklen_t len, int type)
2450 struct hostent *he;
2451 char ip[INET6_ADDRSTRLEN] = {0};
2452 const char *a;
2453 int i;
2455 (void) len; /* unused */
2457 nwrap_files_cache_reload(nwrap_he_global.cache);
2459 a = inet_ntop(type, addr, ip, sizeof(ip));
2460 if (a == NULL) {
2461 errno = EINVAL;
2462 return NULL;
2465 for (i = 0; i < nwrap_he_global.num; i++) {
2466 he = &nwrap_he_global.list[i].ht;
2468 if (he->h_addrtype != type) {
2469 continue;
2472 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
2473 return he;
2477 errno = ENOENT;
2478 return NULL;
2481 #ifdef HAVE_GETHOSTBYADDR_R
2482 static int nwrap_gethostbyaddr_r(const void *addr, socklen_t len, int type,
2483 struct hostent *ret,
2484 char *buf, size_t buflen,
2485 struct hostent **result, int *h_errnop)
2487 *result = nwrap_files_gethostbyaddr(addr, len, type);
2488 if (*result != NULL) {
2489 memset(buf, '\0', buflen);
2490 *ret = **result;
2491 return 0;
2492 } else {
2493 *h_errnop = h_errno;
2494 return -1;
2498 int gethostbyaddr_r(const void *addr, socklen_t len, int type,
2499 struct hostent *ret,
2500 char *buf, size_t buflen,
2501 struct hostent **result, int *h_errnop)
2503 if (!nss_wrapper_hosts_enabled()) {
2504 return libc_gethostbyaddr_r(addr,
2505 len,
2506 type,
2507 ret,
2508 buf,
2509 buflen,
2510 result,
2511 h_errnop);
2514 return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
2516 #endif
2518 /* hosts enum functions */
2519 static void nwrap_files_sethostent(void)
2521 nwrap_he_global.idx = 0;
2524 static struct hostent *nwrap_files_gethostent(void)
2526 struct hostent *he;
2528 if (nwrap_he_global.idx == 0) {
2529 nwrap_files_cache_reload(nwrap_he_global.cache);
2532 if (nwrap_he_global.idx >= nwrap_he_global.num) {
2533 errno = ENOENT;
2534 return NULL;
2537 he = &nwrap_he_global.list[nwrap_he_global.idx++].ht;
2539 NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
2541 return he;
2544 static void nwrap_files_endhostent(void)
2546 nwrap_he_global.idx = 0;
2550 * module backend
2553 #ifndef SAFE_FREE
2554 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
2555 #endif
2557 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
2558 const char *name)
2560 static struct passwd pwd;
2561 static char buf[1000];
2562 NSS_STATUS status;
2564 if (!b->fns->_nss_getpwnam_r) {
2565 return NULL;
2568 status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
2569 if (status == NSS_STATUS_NOTFOUND) {
2570 return NULL;
2572 if (status != NSS_STATUS_SUCCESS) {
2573 return NULL;
2576 return &pwd;
2579 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
2580 const char *name, struct passwd *pwdst,
2581 char *buf, size_t buflen, struct passwd **pwdstp)
2583 int ret;
2585 (void) b; /* unused */
2586 (void) pwdst; /* unused */
2587 (void) pwdstp; /* unused */
2589 if (!b->fns->_nss_getpwnam_r) {
2590 return NSS_STATUS_NOTFOUND;
2593 ret = b->fns->_nss_getpwnam_r(name, pwdst, buf, buflen, &errno);
2594 switch (ret) {
2595 case NSS_STATUS_SUCCESS:
2596 return 0;
2597 case NSS_STATUS_NOTFOUND:
2598 if (errno != 0) {
2599 return errno;
2601 return ENOENT;
2602 case NSS_STATUS_TRYAGAIN:
2603 if (errno != 0) {
2604 return errno;
2606 return ERANGE;
2607 default:
2608 if (errno != 0) {
2609 return errno;
2611 return ret;
2615 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
2616 uid_t uid)
2618 static struct passwd pwd;
2619 static char buf[1000];
2620 NSS_STATUS status;
2622 if (!b->fns->_nss_getpwuid_r) {
2623 return NULL;
2626 status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
2627 if (status == NSS_STATUS_NOTFOUND) {
2628 return NULL;
2630 if (status != NSS_STATUS_SUCCESS) {
2631 return NULL;
2633 return &pwd;
2636 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
2637 uid_t uid, struct passwd *pwdst,
2638 char *buf, size_t buflen, struct passwd **pwdstp)
2640 int ret;
2642 (void) pwdstp; /* unused */
2644 if (!b->fns->_nss_getpwuid_r) {
2645 return ENOENT;
2648 ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
2649 switch (ret) {
2650 case NSS_STATUS_SUCCESS:
2651 return 0;
2652 case NSS_STATUS_NOTFOUND:
2653 if (errno != 0) {
2654 return errno;
2656 return ENOENT;
2657 case NSS_STATUS_TRYAGAIN:
2658 if (errno != 0) {
2659 return errno;
2661 return ERANGE;
2662 default:
2663 if (errno != 0) {
2664 return errno;
2666 return ret;
2670 static void nwrap_module_setpwent(struct nwrap_backend *b)
2672 if (!b->fns->_nss_setpwent) {
2673 return;
2676 b->fns->_nss_setpwent();
2679 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
2681 static struct passwd pwd;
2682 static char buf[1000];
2683 NSS_STATUS status;
2685 if (!b->fns->_nss_getpwent_r) {
2686 return NULL;
2689 status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
2690 if (status == NSS_STATUS_NOTFOUND) {
2691 return NULL;
2693 if (status != NSS_STATUS_SUCCESS) {
2694 return NULL;
2696 return &pwd;
2699 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
2700 struct passwd *pwdst, char *buf,
2701 size_t buflen, struct passwd **pwdstp)
2703 int ret;
2705 (void) pwdstp; /* unused */
2707 if (!b->fns->_nss_getpwent_r) {
2708 return ENOENT;
2711 ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
2712 switch (ret) {
2713 case NSS_STATUS_SUCCESS:
2714 return 0;
2715 case NSS_STATUS_NOTFOUND:
2716 if (errno != 0) {
2717 return errno;
2719 return ENOENT;
2720 case NSS_STATUS_TRYAGAIN:
2721 if (errno != 0) {
2722 return errno;
2724 return ERANGE;
2725 default:
2726 if (errno != 0) {
2727 return errno;
2729 return ret;
2733 static void nwrap_module_endpwent(struct nwrap_backend *b)
2735 if (!b->fns->_nss_endpwent) {
2736 return;
2739 b->fns->_nss_endpwent();
2742 static int nwrap_module_initgroups(struct nwrap_backend *b,
2743 const char *user, gid_t group)
2745 gid_t *groups;
2746 long int start;
2747 long int size;
2749 if (!b->fns->_nss_initgroups) {
2750 return NSS_STATUS_UNAVAIL;
2753 return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
2756 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
2757 const char *name)
2759 static struct group grp;
2760 static char *buf;
2761 static int buflen = 1000;
2762 NSS_STATUS status;
2764 if (!b->fns->_nss_getgrnam_r) {
2765 return NULL;
2768 if (!buf) {
2769 buf = (char *)malloc(buflen);
2771 again:
2772 status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
2773 if (status == NSS_STATUS_TRYAGAIN) {
2774 buflen *= 2;
2775 buf = (char *)realloc(buf, buflen);
2776 if (!buf) {
2777 return NULL;
2779 goto again;
2781 if (status == NSS_STATUS_NOTFOUND) {
2782 SAFE_FREE(buf);
2783 return NULL;
2785 if (status != NSS_STATUS_SUCCESS) {
2786 SAFE_FREE(buf);
2787 return NULL;
2789 return &grp;
2792 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
2793 const char *name, struct group *grdst,
2794 char *buf, size_t buflen, struct group **grdstp)
2796 int ret;
2798 (void) grdstp; /* unused */
2800 if (!b->fns->_nss_getgrnam_r) {
2801 return ENOENT;
2804 ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
2805 switch (ret) {
2806 case NSS_STATUS_SUCCESS:
2807 return 0;
2808 case NSS_STATUS_NOTFOUND:
2809 if (errno != 0) {
2810 return errno;
2812 return ENOENT;
2813 case NSS_STATUS_TRYAGAIN:
2814 if (errno != 0) {
2815 return errno;
2817 return ERANGE;
2818 default:
2819 if (errno != 0) {
2820 return errno;
2822 return ret;
2826 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
2827 gid_t gid)
2829 static struct group grp;
2830 static char *buf;
2831 static int buflen = 1000;
2832 NSS_STATUS status;
2834 if (!b->fns->_nss_getgrgid_r) {
2835 return NULL;
2838 if (!buf) {
2839 buf = (char *)malloc(buflen);
2842 again:
2843 status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
2844 if (status == NSS_STATUS_TRYAGAIN) {
2845 buflen *= 2;
2846 buf = (char *)realloc(buf, buflen);
2847 if (!buf) {
2848 return NULL;
2850 goto again;
2852 if (status == NSS_STATUS_NOTFOUND) {
2853 SAFE_FREE(buf);
2854 return NULL;
2856 if (status != NSS_STATUS_SUCCESS) {
2857 SAFE_FREE(buf);
2858 return NULL;
2860 return &grp;
2863 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
2864 gid_t gid, struct group *grdst,
2865 char *buf, size_t buflen, struct group **grdstp)
2867 int ret;
2869 (void) grdstp; /* unused */
2871 if (!b->fns->_nss_getgrgid_r) {
2872 return ENOENT;
2875 ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
2876 switch (ret) {
2877 case NSS_STATUS_SUCCESS:
2878 return 0;
2879 case NSS_STATUS_NOTFOUND:
2880 if (errno != 0) {
2881 return errno;
2883 return ENOENT;
2884 case NSS_STATUS_TRYAGAIN:
2885 if (errno != 0) {
2886 return errno;
2888 return ERANGE;
2889 default:
2890 if (errno != 0) {
2891 return errno;
2893 return ret;
2897 static void nwrap_module_setgrent(struct nwrap_backend *b)
2899 if (!b->fns->_nss_setgrent) {
2900 return;
2903 b->fns->_nss_setgrent();
2906 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
2908 static struct group grp;
2909 static char *buf;
2910 static int buflen = 1024;
2911 NSS_STATUS status;
2913 if (!b->fns->_nss_getgrent_r) {
2914 return NULL;
2917 if (!buf) {
2918 buf = (char *)malloc(buflen);
2921 again:
2922 status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
2923 if (status == NSS_STATUS_TRYAGAIN) {
2924 buflen *= 2;
2925 buf = (char *)realloc(buf, buflen);
2926 if (!buf) {
2927 return NULL;
2929 goto again;
2931 if (status == NSS_STATUS_NOTFOUND) {
2932 SAFE_FREE(buf);
2933 return NULL;
2935 if (status != NSS_STATUS_SUCCESS) {
2936 SAFE_FREE(buf);
2937 return NULL;
2939 return &grp;
2942 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
2943 struct group *grdst, char *buf,
2944 size_t buflen, struct group **grdstp)
2946 int ret;
2948 (void) grdstp; /* unused */
2950 if (!b->fns->_nss_getgrent_r) {
2951 return ENOENT;
2954 ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
2955 switch (ret) {
2956 case NSS_STATUS_SUCCESS:
2957 return 0;
2958 case NSS_STATUS_NOTFOUND:
2959 if (errno != 0) {
2960 return errno;
2962 return ENOENT;
2963 case NSS_STATUS_TRYAGAIN:
2964 if (errno != 0) {
2965 return errno;
2967 return ERANGE;
2968 default:
2969 if (errno != 0) {
2970 return errno;
2972 return ret;
2976 static void nwrap_module_endgrent(struct nwrap_backend *b)
2978 if (!b->fns->_nss_endgrent) {
2979 return;
2982 b->fns->_nss_endgrent();
2985 /****************************************************************************
2986 * GETPWNAM
2987 ***************************************************************************/
2989 static struct passwd *nwrap_getpwnam(const char *name)
2991 int i;
2992 struct passwd *pwd;
2994 for (i=0; i < nwrap_main_global->num_backends; i++) {
2995 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2996 pwd = b->ops->nw_getpwnam(b, name);
2997 if (pwd) {
2998 return pwd;
3002 return NULL;
3005 struct passwd *getpwnam(const char *name)
3007 if (!nss_wrapper_enabled()) {
3008 return libc_getpwnam(name);
3011 return nwrap_getpwnam(name);
3014 /****************************************************************************
3015 * GETPWNAM_R
3016 ***************************************************************************/
3018 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
3019 char *buf, size_t buflen, struct passwd **pwdstp)
3021 int i,ret;
3023 for (i=0; i < nwrap_main_global->num_backends; i++) {
3024 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3025 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
3026 if (ret == ENOENT) {
3027 continue;
3029 return ret;
3032 return ENOENT;
3035 #ifdef HAVE_GETPWNAM_R
3036 # ifdef HAVE_SOLARIS_GETPWNAM_R
3037 int getpwnam_r(const char *name, struct passwd *pwdst,
3038 char *buf, int buflen, struct passwd **pwdstp)
3039 # else /* HAVE_SOLARIS_GETPWNAM_R */
3040 int getpwnam_r(const char *name, struct passwd *pwdst,
3041 char *buf, size_t buflen, struct passwd **pwdstp)
3042 # endif /* HAVE_SOLARIS_GETPWNAM_R */
3044 if (!nss_wrapper_enabled()) {
3045 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
3048 return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
3050 #endif
3052 /****************************************************************************
3053 * GETPWUID
3054 ***************************************************************************/
3056 static struct passwd *nwrap_getpwuid(uid_t uid)
3058 int i;
3059 struct passwd *pwd;
3061 for (i=0; i < nwrap_main_global->num_backends; i++) {
3062 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3063 pwd = b->ops->nw_getpwuid(b, uid);
3064 if (pwd) {
3065 return pwd;
3069 return NULL;
3072 struct passwd *getpwuid(uid_t uid)
3074 if (!nss_wrapper_enabled()) {
3075 return libc_getpwuid(uid);
3078 return nwrap_getpwuid(uid);
3081 /****************************************************************************
3082 * GETPWUID_R
3083 ***************************************************************************/
3085 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
3086 char *buf, size_t buflen, struct passwd **pwdstp)
3088 int i,ret;
3090 for (i=0; i < nwrap_main_global->num_backends; i++) {
3091 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3092 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
3093 if (ret == ENOENT) {
3094 continue;
3096 return ret;
3099 return ENOENT;
3102 #ifdef HAVE_SOLARIS_GETPWUID_R
3103 int getpwuid_r(uid_t uid, struct passwd *pwdst,
3104 char *buf, int buflen, struct passwd **pwdstp)
3105 #else
3106 int getpwuid_r(uid_t uid, struct passwd *pwdst,
3107 char *buf, size_t buflen, struct passwd **pwdstp)
3108 #endif
3110 if (!nss_wrapper_enabled()) {
3111 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
3114 return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
3117 /****************************************************************************
3118 * SETPWENT
3119 ***************************************************************************/
3121 static void nwrap_setpwent(void)
3123 int i;
3125 for (i=0; i < nwrap_main_global->num_backends; i++) {
3126 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3127 b->ops->nw_setpwent(b);
3131 void setpwent(void)
3133 if (!nss_wrapper_enabled()) {
3134 libc_setpwent();
3135 return;
3138 nwrap_setpwent();
3141 /****************************************************************************
3142 * GETPWENT
3143 ***************************************************************************/
3145 static struct passwd *nwrap_getpwent(void)
3147 int i;
3148 struct passwd *pwd;
3150 for (i=0; i < nwrap_main_global->num_backends; i++) {
3151 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3152 pwd = b->ops->nw_getpwent(b);
3153 if (pwd) {
3154 return pwd;
3158 return NULL;
3161 struct passwd *getpwent(void)
3163 if (!nss_wrapper_enabled()) {
3164 return libc_getpwent();
3167 return nwrap_getpwent();
3170 /****************************************************************************
3171 * GETPWENT_R
3172 ***************************************************************************/
3174 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
3175 size_t buflen, struct passwd **pwdstp)
3177 int i,ret;
3179 for (i=0; i < nwrap_main_global->num_backends; i++) {
3180 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3181 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
3182 if (ret == ENOENT) {
3183 continue;
3185 return ret;
3188 return ENOENT;
3191 #ifdef HAVE_SOLARIS_GETPWENT_R
3192 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
3194 struct passwd *pwdstp = NULL;
3195 int rc;
3197 if (!nss_wrapper_enabled()) {
3198 return libc_getpwent_r(pwdst, buf, buflen);
3200 rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
3201 if (rc < 0) {
3202 return NULL;
3205 return pwdstp;
3207 #else /* HAVE_SOLARIS_GETPWENT_R */
3208 int getpwent_r(struct passwd *pwdst, char *buf,
3209 size_t buflen, struct passwd **pwdstp)
3211 if (!nss_wrapper_enabled()) {
3212 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
3215 return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
3217 #endif /* HAVE_SOLARIS_GETPWENT_R */
3219 /****************************************************************************
3220 * ENDPWENT
3221 ***************************************************************************/
3223 static void nwrap_endpwent(void)
3225 int i;
3227 for (i=0; i < nwrap_main_global->num_backends; i++) {
3228 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3229 b->ops->nw_endpwent(b);
3233 void endpwent(void)
3235 if (!nss_wrapper_enabled()) {
3236 libc_endpwent();
3237 return;
3240 nwrap_endpwent();
3243 /****************************************************************************
3244 * INITGROUPS
3245 ***************************************************************************/
3247 static int nwrap_initgroups(const char *user, gid_t group)
3249 int i;
3251 for (i=0; i < nwrap_main_global->num_backends; i++) {
3252 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3253 int rc;
3255 rc = b->ops->nw_initgroups(b, user, group);
3256 if (rc == 0) {
3257 return 0;
3261 errno = ENOENT;
3262 return -1;
3265 int initgroups(const char *user, gid_t group)
3267 if (!nss_wrapper_enabled()) {
3268 return libc_initgroups(user, group);
3271 return nwrap_initgroups(user, group);
3274 /****************************************************************************
3275 * GETGRNAM
3276 ***************************************************************************/
3278 static struct group *nwrap_getgrnam(const char *name)
3280 int i;
3281 struct group *grp;
3283 for (i=0; i < nwrap_main_global->num_backends; i++) {
3284 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3285 grp = b->ops->nw_getgrnam(b, name);
3286 if (grp) {
3287 return grp;
3291 return NULL;
3294 struct group *getgrnam(const char *name)
3296 if (!nss_wrapper_enabled()) {
3297 return libc_getgrnam(name);
3300 return nwrap_getgrnam(name);
3303 /****************************************************************************
3304 * GETGRNAM_R
3305 ***************************************************************************/
3307 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
3308 char *buf, size_t buflen, struct group **grdstp)
3310 int i, ret;
3312 for (i=0; i < nwrap_main_global->num_backends; i++) {
3313 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3314 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
3315 if (ret == ENOENT) {
3316 continue;
3318 return ret;
3321 return ENOENT;
3324 #ifdef HAVE_GETGRNAM_R
3325 # ifdef HAVE_SOLARIS_GETGRNAM_R
3326 int getgrnam_r(const char *name, struct group *grp,
3327 char *buf, int buflen, struct group **pgrp)
3328 # else /* HAVE_SOLARIS_GETGRNAM_R */
3329 int getgrnam_r(const char *name, struct group *grp,
3330 char *buf, size_t buflen, struct group **pgrp)
3331 # endif /* HAVE_SOLARIS_GETGRNAM_R */
3333 if (!nss_wrapper_enabled()) {
3334 return libc_getgrnam_r(name,
3335 grp,
3336 buf,
3337 buflen,
3338 pgrp);
3341 return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
3343 #endif /* HAVE_GETGRNAM_R */
3345 /****************************************************************************
3346 * GETGRGID
3347 ***************************************************************************/
3349 static struct group *nwrap_getgrgid(gid_t gid)
3351 int i;
3352 struct group *grp;
3354 for (i=0; i < nwrap_main_global->num_backends; i++) {
3355 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3356 grp = b->ops->nw_getgrgid(b, gid);
3357 if (grp) {
3358 return grp;
3362 return NULL;
3365 struct group *getgrgid(gid_t gid)
3367 if (!nss_wrapper_enabled()) {
3368 return libc_getgrgid(gid);
3371 return nwrap_getgrgid(gid);
3374 /****************************************************************************
3375 * GETGRGID_R
3376 ***************************************************************************/
3378 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
3379 char *buf, size_t buflen, struct group **grdstp)
3381 int i,ret;
3383 for (i=0; i < nwrap_main_global->num_backends; i++) {
3384 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3385 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
3386 if (ret == ENOENT) {
3387 continue;
3389 return ret;
3392 return ENOENT;
3395 #ifdef HAVE_GETGRGID_R
3396 # ifdef HAVE_SOLARIS_GETGRGID_R
3397 int getgrgid_r(gid_t gid, struct group *grdst,
3398 char *buf, int buflen, struct group **grdstp)
3399 # else /* HAVE_SOLARIS_GETGRGID_R */
3400 int getgrgid_r(gid_t gid, struct group *grdst,
3401 char *buf, size_t buflen, struct group **grdstp)
3402 # endif /* HAVE_SOLARIS_GETGRGID_R */
3404 if (!nss_wrapper_enabled()) {
3405 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
3408 return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
3410 #endif
3412 /****************************************************************************
3413 * SETGRENT
3414 ***************************************************************************/
3416 static void nwrap_setgrent(void)
3418 int i;
3420 for (i=0; i < nwrap_main_global->num_backends; i++) {
3421 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3422 b->ops->nw_setgrent(b);
3426 #ifdef HAVE_BSD_SETGRENT
3427 int setgrent(void)
3428 #else
3429 void setgrent(void)
3430 #endif
3432 if (!nss_wrapper_enabled()) {
3433 libc_setgrent();
3434 goto out;
3437 nwrap_setgrent();
3439 out:
3440 #ifdef HAVE_BSD_SETGRENT
3441 return 0;
3442 #else
3443 return;
3444 #endif
3447 /****************************************************************************
3448 * GETGRENT
3449 ***************************************************************************/
3451 static struct group *nwrap_getgrent(void)
3453 int i;
3454 struct group *grp;
3456 for (i=0; i < nwrap_main_global->num_backends; i++) {
3457 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3458 grp = b->ops->nw_getgrent(b);
3459 if (grp) {
3460 return grp;
3464 return NULL;
3467 struct group *getgrent(void)
3469 if (!nss_wrapper_enabled()) {
3470 return libc_getgrent();
3473 return nwrap_getgrent();
3476 /****************************************************************************
3477 * GETGRENT_R
3478 ***************************************************************************/
3480 static int nwrap_getgrent_r(struct group *grdst, char *buf,
3481 size_t buflen, struct group **grdstp)
3483 int i,ret;
3485 for (i=0; i < nwrap_main_global->num_backends; i++) {
3486 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3487 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
3488 if (ret == ENOENT) {
3489 continue;
3491 return ret;
3494 return ENOENT;
3497 #ifdef HAVE_SOLARIS_GETGRENT_R
3498 struct group *getgrent_r(struct group *src, char *buf, int buflen)
3500 struct group *grdstp = NULL;
3501 int rc;
3503 if (!nss_wrapper_enabled()) {
3504 return libc_getgrent_r(src, buf, buflen);
3507 rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
3508 if (rc < 0) {
3509 return NULL;
3512 return grdstp;
3514 #else /* HAVE_SOLARIS_GETGRENT_R */
3515 int getgrent_r(struct group *src, char *buf,
3516 size_t buflen, struct group **grdstp)
3518 if (!nss_wrapper_enabled()) {
3519 return libc_getgrent_r(src, buf, buflen, grdstp);
3522 return nwrap_getgrent_r(src, buf, buflen, grdstp);
3524 #endif /* HAVE_SOLARIS_GETGRENT_R */
3526 /****************************************************************************
3527 * ENDGRENT
3528 ***************************************************************************/
3530 static void nwrap_endgrent(void)
3532 int i;
3534 for (i=0; i < nwrap_main_global->num_backends; i++) {
3535 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3536 b->ops->nw_endgrent(b);
3540 void endgrent(void)
3542 if (!nss_wrapper_enabled()) {
3543 libc_endgrent();
3544 return;
3547 nwrap_endgrent();
3550 /****************************************************************************
3551 * GETGROUPLIST
3552 ***************************************************************************/
3554 #ifdef HAVE_GETGROUPLIST
3555 static int nwrap_getgrouplist(const char *user, gid_t group,
3556 gid_t *groups, int *ngroups)
3558 struct group *grp;
3559 gid_t *groups_tmp;
3560 int count = 1;
3561 const char *name_of_group = "";
3563 NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
3565 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
3566 if (!groups_tmp) {
3567 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3568 errno = ENOMEM;
3569 return -1;
3572 memcpy(groups_tmp, &group, sizeof(gid_t));
3574 grp = nwrap_getgrgid(group);
3575 if (grp) {
3576 name_of_group = grp->gr_name;
3579 nwrap_setgrent();
3580 while ((grp = nwrap_getgrent()) != NULL) {
3581 int i = 0;
3583 NWRAP_LOG(NWRAP_LOG_DEBUG,
3584 "Inspecting %s for group membership",
3585 grp->gr_name);
3587 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
3589 if ((strcmp(user, grp->gr_mem[i]) == 0) &&
3590 (strcmp(name_of_group, grp->gr_name) != 0)) {
3592 NWRAP_LOG(NWRAP_LOG_DEBUG,
3593 "%s is member of %s",
3594 user,
3595 grp->gr_name);
3597 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
3598 if (!groups_tmp) {
3599 NWRAP_LOG(NWRAP_LOG_ERROR,
3600 "Out of memory");
3601 errno = ENOMEM;
3602 return -1;
3605 memcpy(&groups_tmp[count], &grp->gr_gid, sizeof(gid_t));
3606 count++;
3611 nwrap_endgrent();
3613 NWRAP_LOG(NWRAP_LOG_DEBUG,
3614 "%s is member of %d groups",
3615 user, *ngroups);
3617 if (*ngroups < count) {
3618 *ngroups = count;
3619 free(groups_tmp);
3620 return -1;
3623 *ngroups = count;
3624 memcpy(groups, groups_tmp, count * sizeof(gid_t));
3625 free(groups_tmp);
3627 return count;
3630 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
3632 if (!nss_wrapper_enabled()) {
3633 return libc_getgrouplist(user, group, groups, ngroups);
3636 return nwrap_getgrouplist(user, group, groups, ngroups);
3638 #endif
3640 /**********************************************************
3641 * NETDB
3642 **********************************************************/
3644 static void nwrap_sethostent(int stayopen) {
3645 (void) stayopen; /* ignored */
3647 nwrap_files_sethostent();
3650 #ifdef HAVE_SOLARIS_SETHOSTENT
3651 int sethostent(int stayopen)
3653 if (!nss_wrapper_hosts_enabled()) {
3654 libc_sethostent(stayopen);
3655 return 0;
3658 nwrap_sethostent(stayopen);
3660 return 0;
3662 #else /* HAVE_SOLARIS_SETHOSTENT */
3663 void sethostent(int stayopen)
3665 if (!nss_wrapper_hosts_enabled()) {
3666 libc_sethostent(stayopen);
3667 return;
3670 nwrap_sethostent(stayopen);
3672 #endif /* HAVE_SOLARIS_SETHOSTENT */
3674 static struct hostent *nwrap_gethostent(void)
3676 return nwrap_files_gethostent();
3679 struct hostent *gethostent(void) {
3680 if (!nss_wrapper_hosts_enabled()) {
3681 return libc_gethostent();
3684 return nwrap_gethostent();
3687 static void nwrap_endhostent(void) {
3688 nwrap_files_endhostent();
3691 #ifdef HAVE_SOLARIS_ENDHOSTENT
3692 int endhostent(void)
3694 if (!nss_wrapper_hosts_enabled()) {
3695 libc_endhostent();
3696 return 0;
3699 nwrap_endhostent();
3701 return 0;
3703 #else /* HAVE_SOLARIS_ENDHOSTENT */
3704 void endhostent(void)
3706 if (!nss_wrapper_hosts_enabled()) {
3707 libc_endhostent();
3708 return;
3711 nwrap_endhostent();
3713 #endif /* HAVE_SOLARIS_ENDHOSTENT */
3715 static struct hostent *nwrap_gethostbyname(const char *name)
3717 return nwrap_files_gethostbyname(name, AF_UNSPEC);
3720 struct hostent *gethostbyname(const char *name)
3722 if (!nss_wrapper_hosts_enabled()) {
3723 return libc_gethostbyname(name);
3726 return nwrap_gethostbyname(name);
3729 /* This is a GNU extension */
3730 #ifdef HAVE_GETHOSTBYNAME2
3731 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
3733 return nwrap_files_gethostbyname(name, af);
3736 struct hostent *gethostbyname2(const char *name, int af)
3738 if (!nss_wrapper_hosts_enabled()) {
3739 return libc_gethostbyname2(name, af);
3742 return nwrap_gethostbyname2(name, af);
3744 #endif
3746 static struct hostent *nwrap_gethostbyaddr(const void *addr,
3747 socklen_t len, int type)
3749 return nwrap_files_gethostbyaddr(addr, len, type);
3752 struct hostent *gethostbyaddr(const void *addr,
3753 socklen_t len, int type)
3755 if (!nss_wrapper_hosts_enabled()) {
3756 return libc_gethostbyaddr(addr, len, type);
3759 return nwrap_gethostbyaddr(addr, len, type);
3762 static const struct addrinfo default_hints =
3764 .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
3765 .ai_family = AF_UNSPEC,
3766 .ai_socktype = 0,
3767 .ai_protocol = 0,
3768 .ai_addrlen = 0,
3769 .ai_addr = NULL,
3770 .ai_canonname = NULL,
3771 .ai_next = NULL
3774 static int nwrap_convert_he_ai(const struct hostent *he,
3775 unsigned short port,
3776 const struct addrinfo *hints,
3777 struct addrinfo **pai)
3779 struct addrinfo *ai;
3780 socklen_t socklen;
3782 switch (he->h_addrtype) {
3783 case AF_INET:
3784 socklen = sizeof(struct sockaddr_in);
3785 break;
3786 #ifdef HAVE_IPV6
3787 case AF_INET6:
3788 socklen = sizeof(struct sockaddr_in6);
3789 break;
3790 #endif
3791 default:
3792 return EAI_FAMILY;
3795 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
3796 if (ai == NULL) {
3797 return EAI_MEMORY;
3800 ai->ai_flags = 0;
3801 ai->ai_family = he->h_addrtype;
3802 ai->ai_socktype = hints->ai_socktype;
3803 ai->ai_protocol = hints->ai_protocol;
3805 ai->ai_addrlen = socklen;
3806 ai->ai_addr = (void *)(ai + 1);
3808 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
3809 ai->ai_addr->sa_len = socklen;
3810 #endif
3811 ai->ai_addr->sa_family = he->h_addrtype;
3813 switch (he->h_addrtype) {
3814 case AF_INET:
3816 struct sockaddr_in *sinp =
3817 (struct sockaddr_in *) ai->ai_addr;
3819 memset(sinp, 0, sizeof(struct sockaddr_in));
3821 sinp->sin_port = htons(port);
3822 sinp->sin_family = AF_INET;
3824 memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
3825 memcpy(&sinp->sin_addr, he->h_addr_list[0], he->h_length);
3828 break;
3829 #ifdef HAVE_IPV6
3830 case AF_INET6:
3832 struct sockaddr_in6 *sin6p =
3833 (struct sockaddr_in6 *) ai->ai_addr;
3835 memset(sin6p, 0, sizeof(struct sockaddr_in6));
3837 sin6p->sin6_port = htons(port);
3838 sin6p->sin6_family = AF_INET6;
3840 memcpy(&sin6p->sin6_addr, he->h_addr_list[0], he->h_length);
3842 break;
3843 #endif
3846 ai->ai_next = NULL;
3848 if (he->h_name) {
3849 ai->ai_canonname = strdup(he->h_name);
3850 if (ai->ai_canonname == NULL) {
3851 freeaddrinfo(ai);
3852 return EAI_MEMORY;
3856 *pai = ai;
3857 return 0;
3860 static int nwrap_getaddrinfo(const char *node,
3861 const char *service,
3862 const struct addrinfo *hints,
3863 struct addrinfo **res)
3865 struct addrinfo *ai = NULL;
3866 struct addrinfo *p = NULL;
3867 unsigned short port = 0;
3868 struct hostent *he;
3869 struct {
3870 int family;
3871 union {
3872 struct in_addr v4;
3873 #ifdef HAVE_IPV6
3874 struct in6_addr v6;
3875 } in;
3876 #endif
3877 } addr = {
3878 .family = AF_UNSPEC,
3880 int eai = EAI_SYSTEM;
3881 int ret;
3882 int rc;
3884 if (node == NULL && service == NULL) {
3885 return EAI_NONAME;
3888 ret = libc_getaddrinfo(node, service, hints, &p);
3889 if (ret == 0) {
3890 *res = p;
3893 /* If no node has been specified, let glibc deal with it */
3894 if (node == NULL) {
3895 return ret;
3898 if (hints == NULL) {
3899 hints = &default_hints;
3902 if ((hints->ai_flags & AI_CANONNAME) && node == NULL) {
3903 return EAI_BADFLAGS;
3906 if (service != NULL && service[0] != '\0') {
3907 if (isdigit((int)service[0])) {
3908 port = (unsigned short)atoi(service);
3909 } else {
3910 const char *proto = NULL;
3911 struct servent *s;
3913 if (hints->ai_protocol != 0) {
3914 struct protoent *pent;
3916 pent = getprotobynumber(hints->ai_protocol);
3917 if (pent != NULL) {
3918 proto = pent->p_name;
3922 s = getservbyname(service, proto);
3923 if (s != NULL) {
3924 port = ntohs(s->s_port);
3925 } else {
3926 if (p != NULL) {
3927 freeaddrinfo(p);
3929 return EAI_SERVICE;
3934 rc = 0;
3935 if (hints->ai_family == AF_UNSPEC || hints->ai_family == AF_INET) {
3936 rc = inet_pton(AF_INET, node, &addr.in.v4);
3938 if (rc == 1) {
3939 addr.family = AF_INET;
3940 #ifdef HAVE_IPV6
3941 } else {
3942 rc = inet_pton(AF_INET6, node, &addr.in.v6);
3943 if (rc == 1) {
3944 addr.family = AF_INET6;
3946 #endif
3949 if (addr.family == AF_INET) {
3950 he = nwrap_files_gethostbyaddr(&addr.in.v4,
3951 sizeof(struct in_addr),
3952 addr.family);
3953 if (he != NULL) {
3954 rc = nwrap_convert_he_ai(he, port, hints, &ai);
3955 } else {
3956 eai = EAI_NODATA;
3957 rc = -1;
3959 #ifdef HAVE_IPV6
3960 } else if (addr.family == AF_INET6) {
3961 he = nwrap_files_gethostbyaddr(&addr.in.v6,
3962 sizeof(struct in6_addr),
3963 addr.family);
3964 if (he != NULL) {
3965 rc = nwrap_convert_he_ai(he, port, hints, &ai);
3966 eai = rc;
3967 } else {
3968 eai = EAI_NODATA;
3969 rc = -1;
3971 #endif
3972 } else {
3973 he = nwrap_files_gethostbyname(node, hints->ai_family);
3974 if (he != NULL) {
3975 rc = nwrap_convert_he_ai(he, port, hints, &ai);
3976 eai = rc;
3977 } else {
3978 eai = EAI_NODATA;
3979 rc = -1;
3983 if (rc < 0) {
3984 return ret == 0 ? 0 : eai;
3987 if (ret == 0) {
3988 freeaddrinfo(p);
3991 if (ai->ai_flags == 0) {
3992 ai->ai_flags = hints->ai_flags;
3994 if (ai->ai_socktype == 0) {
3995 ai->ai_socktype = SOCK_DGRAM;
3997 if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_DGRAM) {
3998 ai->ai_protocol = 17; /* UDP */
3999 } else if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_STREAM) {
4000 ai->ai_protocol = 6; /* TCP */
4003 if (hints->ai_socktype == 0) {
4004 /* Add second ai */
4005 rc = nwrap_convert_he_ai(he, port, hints, &ai->ai_next);
4006 if (rc < 0) {
4007 freeaddrinfo(ai);
4008 return rc;
4011 if (ai->ai_next->ai_flags == 0) {
4012 ai->ai_next->ai_flags = hints->ai_flags;
4014 if (ai->ai_socktype == SOCK_DGRAM) {
4015 ai->ai_next->ai_socktype = SOCK_STREAM;
4016 } else if (ai->ai_socktype == SOCK_STREAM) {
4017 ai->ai_next->ai_socktype = SOCK_DGRAM;
4019 if (ai->ai_next->ai_socktype == SOCK_DGRAM) {
4020 ai->ai_next->ai_protocol = 17; /* UDP */
4021 } else if (ai->ai_next->ai_socktype == SOCK_STREAM) {
4022 ai->ai_next->ai_protocol = 6; /* TCP */
4026 *res = ai;
4028 return 0;
4031 int getaddrinfo(const char *node, const char *service,
4032 const struct addrinfo *hints,
4033 struct addrinfo **res)
4035 if (!nss_wrapper_hosts_enabled()) {
4036 return libc_getaddrinfo(node, service, hints, res);
4039 return nwrap_getaddrinfo(node, service, hints, res);
4042 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
4043 char *host, size_t hostlen,
4044 char *serv, size_t servlen,
4045 int flags)
4047 struct hostent *he;
4048 struct servent *service;
4049 const char *proto;
4050 const void *addr;
4051 socklen_t addrlen;
4052 uint16_t port;
4053 sa_family_t type;
4055 if (sa == NULL || salen < sizeof(sa_family_t)) {
4056 return EAI_FAMILY;
4059 if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
4060 return EAI_NONAME;
4063 type = sa->sa_family;
4064 switch (type) {
4065 case AF_INET:
4066 if (salen < sizeof(struct sockaddr_in))
4067 return EAI_FAMILY;
4068 addr = &((const struct sockaddr_in *)sa)->sin_addr;
4069 addrlen = sizeof(((const struct sockaddr_in *)sa)->sin_addr);
4070 port = ntohs(((const struct sockaddr_in *)sa)->sin_port);
4071 break;
4072 #ifdef HAVE_IPV6
4073 case AF_INET6:
4074 if (salen < sizeof(struct sockaddr_in6))
4075 return EAI_FAMILY;
4076 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
4077 addrlen = sizeof(((const struct sockaddr_in6 *)sa)->sin6_addr);
4078 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port);
4079 break;
4080 #endif
4081 default:
4082 return EAI_FAMILY;
4085 if (host != NULL) {
4086 he = NULL;
4087 if ((flags & NI_NUMERICHOST) == 0) {
4088 he = nwrap_files_gethostbyaddr(addr, addrlen, type);
4089 if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
4090 return EAI_NONAME;
4092 if (he != NULL && he->h_name != NULL) {
4093 if (strlen(he->h_name) >= hostlen)
4094 return EAI_OVERFLOW;
4095 strcpy(host, he->h_name);
4096 if (flags & NI_NOFQDN)
4097 host[strcspn(host, ".")] = '\0';
4098 } else {
4099 if (inet_ntop(type, addr, host, hostlen) == NULL)
4100 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
4104 if (serv != NULL) {
4105 service = NULL;
4106 if ((flags & NI_NUMERICSERV) == 0) {
4107 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
4108 service = getservbyport(htons(port), proto);
4110 if (service != NULL) {
4111 if (strlen(service->s_name) >= servlen)
4112 return EAI_OVERFLOW;
4113 strcpy(serv, service->s_name);
4114 } else {
4115 if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
4116 return EAI_OVERFLOW;
4120 return 0;
4123 #ifdef HAVE_LINUX_GETNAMEINFO
4124 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
4125 char *host, socklen_t hostlen,
4126 char *serv, socklen_t servlen,
4127 int flags)
4128 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
4129 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
4130 char *host, socklen_t hostlen,
4131 char *serv, socklen_t servlen,
4132 unsigned int flags)
4133 #else
4134 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
4135 char *host, size_t hostlen,
4136 char *serv, size_t servlen,
4137 int flags)
4138 #endif
4140 if (!nss_wrapper_hosts_enabled()) {
4141 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
4144 return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
4147 static int nwrap_gethostname(char *name, size_t len)
4149 const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
4151 if (strlen(hostname) >= len) {
4152 errno = ENAMETOOLONG;
4153 return -1;
4155 snprintf(name, len, "%s", hostname);
4157 return 0;
4160 #ifdef HAVE_SOLARIS_GETHOSTNAME
4161 int gethostname(char *name, int len)
4162 #else /* HAVE_SOLARIS_GETHOSTNAME */
4163 int gethostname(char *name, size_t len)
4164 #endif /* HAVE_SOLARIS_GETHOSTNAME */
4166 if (!nwrap_hostname_enabled()) {
4167 return libc_gethostname(name, len);
4170 return nwrap_gethostname(name, len);
4173 /****************************
4174 * DESTRUCTOR
4175 ***************************/
4178 * This function is called when the library is unloaded and makes sure that
4179 * sockets get closed and the unix file for the socket are unlinked.
4181 void nwrap_destructor(void)
4183 int i;
4185 if (nwrap_main_global != NULL) {
4186 struct nwrap_main *m = nwrap_main_global;
4188 /* libc */
4189 SAFE_FREE(m->libc->fns);
4190 if (m->libc->handle != NULL) {
4191 dlclose(m->libc->handle);
4193 if (m->libc->nsl_handle != NULL) {
4194 dlclose(m->libc->nsl_handle);
4196 if (m->libc->sock_handle != NULL) {
4197 dlclose(m->libc->sock_handle);
4199 SAFE_FREE(m->libc);
4201 /* backends */
4202 for (i = 0; i < m->num_backends; i++) {
4203 struct nwrap_backend *b = &(m->backends[i]);
4205 if (b->so_handle != NULL) {
4206 dlclose(b->so_handle);
4208 SAFE_FREE(b->fns);
4210 SAFE_FREE(m->backends);
4213 if (nwrap_pw_global.cache != NULL) {
4214 struct nwrap_cache *c = nwrap_pw_global.cache;
4216 nwrap_files_cache_unload(c);
4217 if (c->fd >= 0) {
4218 close(c->fd);
4221 SAFE_FREE(nwrap_pw_global.list);
4222 nwrap_pw_global.num = 0;
4225 if (nwrap_gr_global.cache != NULL) {
4226 struct nwrap_cache *c = nwrap_gr_global.cache;
4228 nwrap_files_cache_unload(c);
4229 if (c->fd >= 0) {
4230 close(c->fd);
4233 SAFE_FREE(nwrap_gr_global.list);
4234 nwrap_pw_global.num = 0;
4237 if (nwrap_he_global.cache != NULL) {
4238 struct nwrap_cache *c = nwrap_he_global.cache;
4240 nwrap_files_cache_unload(c);
4241 if (c->fd >= 0) {
4242 close(c->fd);
4245 SAFE_FREE(nwrap_he_global.list);
4246 nwrap_he_global.num = 0;