nwrap: Use DNS_NAME_MAX cause it is not available on BSD.
[Samba.git] / lib / nss_wrapper / nss_wrapper.c
blobc066e47f51d464fe864ff880699e398717386916
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 (handle = NULL, i = 10; handle == NULL && i >= 0; i--) {
579 char soname[256] = {0};
581 snprintf(soname, sizeof(soname), "libnsl.so.%d", i);
582 handle = dlopen(soname, flags);
585 nwrap_main_global->libc->nsl_handle = handle;
587 break;
588 #endif
589 /* FALL TROUGH */
590 case NWRAP_LIBSOCKET:
591 #ifdef HAVE_LIBSOCKET
592 handle = nwrap_main_global->libc->sock_handle;
593 if (handle == NULL) {
594 for (handle = NULL, i = 10; handle == NULL && i >= 0; i--) {
595 char soname[256] = {0};
597 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
598 handle = dlopen(soname, flags);
601 nwrap_main_global->libc->sock_handle = handle;
603 break;
604 #endif
605 /* FALL TROUGH */
606 case NWRAP_LIBC:
607 handle = nwrap_main_global->libc->handle;
608 if (handle == NULL) {
609 for (handle = NULL, i = 10; handle == NULL && i >= 0; i--) {
610 char soname[256] = {0};
612 snprintf(soname, sizeof(soname), "libc.so.%d", i);
613 handle = dlopen(soname, flags);
616 nwrap_main_global->libc->handle = handle;
618 break;
621 if (handle == NULL) {
622 #ifdef RTLD_NEXT
623 handle = nwrap_main_global->libc->handle
624 = nwrap_main_global->libc->sock_handle
625 = nwrap_main_global->libc->nsl_handle
626 = RTLD_NEXT;
627 #else
628 NWRAP_LOG(NWRAP_LOG_ERROR,
629 "Failed to dlopen library: %s\n",
630 dlerror());
631 exit(-1);
632 #endif
635 return handle;
638 static void *_nwrap_load_lib_function(enum nwrap_lib lib, const char *fn_name)
640 void *handle;
641 void *func;
643 nwrap_init();
645 handle = nwrap_load_lib_handle(lib);
647 func = dlsym(handle, fn_name);
648 if (func == NULL) {
649 NWRAP_LOG(NWRAP_LOG_ERROR,
650 "Failed to find %s: %s\n",
651 fn_name, dlerror());
652 exit(-1);
655 NWRAP_LOG(NWRAP_LOG_TRACE,
656 "Loaded %s from %s",
657 fn_name, nwrap_str_lib(lib));
658 return func;
661 #define nwrap_load_lib_function(lib, fn_name) \
662 if (nwrap_main_global->libc->fns->_libc_##fn_name == NULL) { \
663 *(void **) (&nwrap_main_global->libc->fns->_libc_##fn_name) = \
664 _nwrap_load_lib_function(lib, #fn_name); \
668 * IMPORTANT
670 * Functions expeciall from libc need to be loaded individually, you can't load
671 * all at once or gdb will segfault at startup. The same applies to valgrind and
672 * has probably something todo with with the linker.
673 * So we need load each function at the point it is called the first time.
675 static struct passwd *libc_getpwnam(const char *name)
677 nwrap_load_lib_function(NWRAP_LIBC, getpwnam);
679 return nwrap_main_global->libc->fns->_libc_getpwnam(name);
682 #ifdef HAVE_GETPWNAM_R
683 static int libc_getpwnam_r(const char *name,
684 struct passwd *pwd,
685 char *buf,
686 size_t buflen,
687 struct passwd **result)
689 #ifdef HAVE___POSIX_GETPWNAM_R
690 if (nwrap_main_global->libc->fns->_libc_getpwnam_r == NULL) {
691 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwnam_r) =
692 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwnam_r");
694 #else
695 nwrap_load_lib_function(NWRAP_LIBC, getpwnam_r);
696 #endif
698 return nwrap_main_global->libc->fns->_libc_getpwnam_r(name,
699 pwd,
700 buf,
701 buflen,
702 result);
704 #endif
706 static struct passwd *libc_getpwuid(uid_t uid)
708 nwrap_load_lib_function(NWRAP_LIBC, getpwuid);
710 return nwrap_main_global->libc->fns->_libc_getpwuid(uid);
713 #ifdef HAVE_GETPWUID_R
714 static int libc_getpwuid_r(uid_t uid,
715 struct passwd *pwd,
716 char *buf,
717 size_t buflen,
718 struct passwd **result)
720 #ifdef HAVE___POSIX_GETPWUID_R
721 if (nwrap_main_global->libc->fns->_libc_getpwuid_r == NULL) {
722 *(void **) (&nwrap_main_global->libc->fns->_libc_getpwuid_r) =
723 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getpwuid_r");
725 #else
726 nwrap_load_lib_function(NWRAP_LIBC, getpwuid_r);
727 #endif
729 return nwrap_main_global->libc->fns->_libc_getpwuid_r(uid,
730 pwd,
731 buf,
732 buflen,
733 result);
735 #endif
737 static void libc_setpwent(void)
739 nwrap_load_lib_function(NWRAP_LIBC, setpwent);
741 nwrap_main_global->libc->fns->_libc_setpwent();
744 static struct passwd *libc_getpwent(void)
746 nwrap_load_lib_function(NWRAP_LIBC, getpwent);
748 return nwrap_main_global->libc->fns->_libc_getpwent();
751 #ifdef HAVE_SOLARIS_GETPWENT_R
752 static struct passwd *libc_getpwent_r(struct passwd *pwdst,
753 char *buf,
754 int buflen)
756 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
758 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
759 buf,
760 buflen);
762 #else /* HAVE_SOLARIS_GETPWENT_R */
763 static int libc_getpwent_r(struct passwd *pwdst,
764 char *buf,
765 size_t buflen,
766 struct passwd **pwdstp)
768 nwrap_load_lib_function(NWRAP_LIBC, getpwent_r);
770 return nwrap_main_global->libc->fns->_libc_getpwent_r(pwdst,
771 buf,
772 buflen,
773 pwdstp);
775 #endif /* HAVE_SOLARIS_GETPWENT_R */
777 static void libc_endpwent(void)
779 nwrap_load_lib_function(NWRAP_LIBC, endpwent);
781 nwrap_main_global->libc->fns->_libc_endpwent();
784 static int libc_initgroups(const char *user, gid_t gid)
786 nwrap_load_lib_function(NWRAP_LIBC, initgroups);
788 return nwrap_main_global->libc->fns->_libc_initgroups(user, gid);
791 static struct group *libc_getgrnam(const char *name)
793 nwrap_load_lib_function(NWRAP_LIBC, getgrnam);
795 return nwrap_main_global->libc->fns->_libc_getgrnam(name);
798 #ifdef HAVE_GETGRNAM_R
799 static int libc_getgrnam_r(const char *name,
800 struct group *grp,
801 char *buf,
802 size_t buflen,
803 struct group **result)
805 #ifdef HAVE___POSIX_GETGRNAM_R
806 if (nwrap_main_global->libc->fns->_libc_getgrnam_r == NULL) {
807 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrnam_r) =
808 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrnam_r");
810 #else
811 nwrap_load_lib_function(NWRAP_LIBC, getgrnam_r);
812 #endif
814 return nwrap_main_global->libc->fns->_libc_getgrnam_r(name,
815 grp,
816 buf,
817 buflen,
818 result);
820 #endif
822 static struct group *libc_getgrgid(gid_t gid)
824 nwrap_load_lib_function(NWRAP_LIBC, getgrgid);
826 return nwrap_main_global->libc->fns->_libc_getgrgid(gid);
829 #ifdef HAVE_GETGRGID_R
830 static int libc_getgrgid_r(gid_t gid,
831 struct group *grp,
832 char *buf,
833 size_t buflen,
834 struct group **result)
836 #ifdef HAVE___POSIX_GETGRGID_R
837 if (nwrap_main_global->libc->fns->_libc_getgrgid_r == NULL) {
838 *(void **) (&nwrap_main_global->libc->fns->_libc_getgrgid_r) =
839 _nwrap_load_lib_function(NWRAP_LIBC, "__posix_getgrgid_r");
841 #else
842 nwrap_load_lib_function(NWRAP_LIBC, getgrgid_r);
843 #endif
845 return nwrap_main_global->libc->fns->_libc_getgrgid_r(gid,
846 grp,
847 buf,
848 buflen,
849 result);
851 #endif
853 static void libc_setgrent(void)
855 nwrap_load_lib_function(NWRAP_LIBC, setgrent);
857 nwrap_main_global->libc->fns->_libc_setgrent();
860 static struct group *libc_getgrent(void)
862 nwrap_load_lib_function(NWRAP_LIBC, getgrent);
864 return nwrap_main_global->libc->fns->_libc_getgrent();
867 #ifdef HAVE_GETGRENT_R
868 #ifdef HAVE_SOLARIS_GETGRENT_R
869 static struct group *libc_getgrent_r(struct group *group,
870 char *buf,
871 size_t buflen)
873 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
875 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
876 buf,
877 buflen);
879 #else /* !HAVE_SOLARIS_GETGRENT_R */
880 static int libc_getgrent_r(struct group *group,
881 char *buf,
882 size_t buflen,
883 struct group **result)
885 nwrap_load_lib_function(NWRAP_LIBC, getgrent_r);
887 return nwrap_main_global->libc->fns->_libc_getgrent_r(group,
888 buf,
889 buflen,
890 result);
892 #endif /* HAVE_SOLARIS_GETGRENT_R */
893 #endif /* HAVE_GETGRENT_R */
895 static void libc_endgrent(void)
897 nwrap_load_lib_function(NWRAP_LIBC, endgrent);
899 nwrap_main_global->libc->fns->_libc_endgrent();
902 #ifdef HAVE_GETGROUPLIST
903 static int libc_getgrouplist(const char *user,
904 gid_t group,
905 gid_t *groups,
906 int *ngroups)
908 nwrap_load_lib_function(NWRAP_LIBC, getgrouplist);
910 return nwrap_main_global->libc->fns->_libc_getgrouplist(user,
911 group,
912 groups,
913 ngroups);
915 #endif
917 static void libc_sethostent(int stayopen)
919 nwrap_load_lib_function(NWRAP_LIBNSL, sethostent);
921 nwrap_main_global->libc->fns->_libc_sethostent(stayopen);
924 static struct hostent *libc_gethostent(void)
926 nwrap_load_lib_function(NWRAP_LIBNSL, gethostent);
928 return nwrap_main_global->libc->fns->_libc_gethostent();
931 static void libc_endhostent(void)
933 nwrap_load_lib_function(NWRAP_LIBNSL, endhostent);
935 nwrap_main_global->libc->fns->_libc_endhostent();
938 static struct hostent *libc_gethostbyname(const char *name)
940 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname);
942 return nwrap_main_global->libc->fns->_libc_gethostbyname(name);
945 #ifdef HAVE_GETHOSTBYNAME2 /* GNU extension */
946 static struct hostent *libc_gethostbyname2(const char *name, int af)
948 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname2);
950 return nwrap_main_global->libc->fns->_libc_gethostbyname2(name, af);
952 #endif
954 static struct hostent *libc_gethostbyaddr(const void *addr,
955 socklen_t len,
956 int type)
958 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr);
960 return nwrap_main_global->libc->fns->_libc_gethostbyaddr(addr,
961 len,
962 type);
965 static int libc_gethostname(char *name, size_t len)
967 nwrap_load_lib_function(NWRAP_LIBNSL, gethostname);
969 return nwrap_main_global->libc->fns->_libc_gethostname(name, len);
972 #ifdef HAVE_GETHOSTBYNAME_R
973 static int libc_gethostbyname_r(const char *name,
974 struct hostent *ret,
975 char *buf,
976 size_t buflen,
977 struct hostent **result,
978 int *h_errnop)
980 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyname_r);
982 return nwrap_main_global->libc->fns->_libc_gethostbyname_r(name,
983 ret,
984 buf,
985 buflen,
986 result,
987 h_errnop);
989 #endif
991 #ifdef HAVE_GETHOSTBYADDR_R
992 static int libc_gethostbyaddr_r(const void *addr,
993 socklen_t len,
994 int type,
995 struct hostent *ret,
996 char *buf,
997 size_t buflen,
998 struct hostent **result,
999 int *h_errnop)
1001 nwrap_load_lib_function(NWRAP_LIBNSL, gethostbyaddr_r);
1003 return nwrap_main_global->libc->fns->_libc_gethostbyaddr_r(addr,
1004 len,
1005 type,
1006 ret,
1007 buf,
1008 buflen,
1009 result,
1010 h_errnop);
1012 #endif
1014 static int libc_getaddrinfo(const char *node,
1015 const char *service,
1016 const struct addrinfo *hints,
1017 struct addrinfo **res)
1019 nwrap_load_lib_function(NWRAP_LIBSOCKET, getaddrinfo);
1021 return nwrap_main_global->libc->fns->_libc_getaddrinfo(node,
1022 service,
1023 hints,
1024 res);
1027 static int libc_getnameinfo(const struct sockaddr *sa,
1028 socklen_t salen,
1029 char *host,
1030 size_t hostlen,
1031 char *serv,
1032 size_t servlen,
1033 int flags)
1035 nwrap_load_lib_function(NWRAP_LIBSOCKET, getnameinfo);
1037 return nwrap_main_global->libc->fns->_libc_getnameinfo(sa,
1038 salen,
1039 host,
1040 hostlen,
1041 serv,
1042 servlen,
1043 flags);
1046 /*********************************************************
1047 * NWRAP NSS MODULE LOADER FUNCTIONS
1048 *********************************************************/
1050 static void *nwrap_load_module_fn(struct nwrap_backend *b,
1051 const char *fn_name)
1053 void *res;
1054 char *s;
1056 if (!b->so_handle) {
1057 NWRAP_LOG(NWRAP_LOG_ERROR, "No handle");
1058 return NULL;
1061 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
1062 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1063 return NULL;
1066 res = dlsym(b->so_handle, s);
1067 if (!res) {
1068 NWRAP_LOG(NWRAP_LOG_ERROR,
1069 "Cannot find function %s in %s",
1070 s, b->so_path);
1072 free(s);
1073 s = NULL;
1074 return res;
1077 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
1079 struct nwrap_module_nss_fns *fns;
1081 if (!b->so_handle) {
1082 return NULL;
1085 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
1086 if (!fns) {
1087 return NULL;
1090 *(void **)(&fns->_nss_getpwnam_r) =
1091 nwrap_load_module_fn(b, "getpwnam_r");
1092 *(void **)(&fns->_nss_getpwuid_r) =
1093 nwrap_load_module_fn(b, "getpwuid_r");
1094 *(void **)(&fns->_nss_setpwent) =
1095 nwrap_load_module_fn(b, "setpwent");
1096 *(void **)(&fns->_nss_getpwent_r) =
1097 nwrap_load_module_fn(b, "getpwent_r");
1098 *(void **)(&fns->_nss_endpwent) =
1099 nwrap_load_module_fn(b, "endpwent");
1100 *(void **)(&fns->_nss_initgroups) =
1101 nwrap_load_module_fn(b, "initgroups_dyn");
1102 *(void **)(&fns->_nss_getgrnam_r) =
1103 nwrap_load_module_fn(b, "getgrnam_r");
1104 *(void **)(&fns->_nss_getgrgid_r)=
1105 nwrap_load_module_fn(b, "getgrgid_r");
1106 *(void **)(&fns->_nss_setgrent) =
1107 nwrap_load_module_fn(b, "setgrent");
1108 *(void **)(&fns->_nss_getgrent_r) =
1109 nwrap_load_module_fn(b, "getgrent_r");
1110 *(void **)(&fns->_nss_endgrent) =
1111 nwrap_load_module_fn(b, "endgrent");
1113 return fns;
1116 static void *nwrap_load_module(const char *so_path)
1118 void *h;
1120 if (!so_path || !strlen(so_path)) {
1121 return NULL;
1124 h = dlopen(so_path, RTLD_LAZY);
1125 if (!h) {
1126 NWRAP_LOG(NWRAP_LOG_ERROR,
1127 "Cannot open shared library %s",
1128 so_path);
1129 return NULL;
1132 return h;
1135 static bool nwrap_module_init(const char *name,
1136 struct nwrap_ops *ops,
1137 const char *so_path,
1138 int *num_backends,
1139 struct nwrap_backend **backends)
1141 struct nwrap_backend *b;
1143 *backends = (struct nwrap_backend *)realloc(*backends,
1144 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
1145 if (!*backends) {
1146 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1147 return false;
1150 b = &((*backends)[*num_backends]);
1152 b->name = name;
1153 b->ops = ops;
1154 b->so_path = so_path;
1156 if (so_path != NULL) {
1157 b->so_handle = nwrap_load_module(so_path);
1158 b->fns = nwrap_load_module_fns(b);
1159 if (b->fns == NULL) {
1160 return false;
1162 } else {
1163 b->so_handle = NULL;
1164 b->fns = NULL;
1167 (*num_backends)++;
1169 return true;
1172 static void nwrap_libc_init(struct nwrap_main *r)
1174 r->libc = malloc(sizeof(struct nwrap_libc));
1175 if (r->libc == NULL) {
1176 printf("Failed to allocate memory for libc");
1177 exit(-1);
1179 ZERO_STRUCTP(r->libc);
1181 r->libc->fns = malloc(sizeof(struct nwrap_libc_fns));
1182 if (r->libc->fns == NULL) {
1183 printf("Failed to allocate memory for libc functions");
1184 exit(-1);
1186 ZERO_STRUCTP(r->libc->fns);
1189 static void nwrap_backend_init(struct nwrap_main *r)
1191 const char *module_so_path = getenv("NSS_WRAPPER_MODULE_SO_PATH");
1192 const char *module_fn_name = getenv("NSS_WRAPPER_MODULE_FN_PREFIX");
1194 r->num_backends = 0;
1195 r->backends = NULL;
1197 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
1198 &r->num_backends,
1199 &r->backends)) {
1200 NWRAP_LOG(NWRAP_LOG_ERROR,
1201 "Failed to initialize 'files' backend");
1202 return;
1205 if (module_so_path != NULL &&
1206 module_so_path[0] != '\0' &&
1207 module_fn_name != NULL &&
1208 module_fn_name[0] != '\0') {
1209 if (!nwrap_module_init(module_fn_name,
1210 &nwrap_module_ops,
1211 module_so_path,
1212 &r->num_backends,
1213 &r->backends)) {
1214 NWRAP_LOG(NWRAP_LOG_ERROR,
1215 "Failed to initialize '%s' backend",
1216 module_fn_name);
1217 return;
1222 static void nwrap_init(void)
1224 static bool initialized;
1226 if (initialized) return;
1227 initialized = true;
1229 nwrap_main_global = &__nwrap_main_global;
1231 nwrap_libc_init(nwrap_main_global);
1233 nwrap_backend_init(nwrap_main_global);
1235 nwrap_pw_global.cache = &__nwrap_cache_pw;
1237 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
1238 nwrap_pw_global.cache->fd = -1;
1239 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
1240 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
1241 nwrap_pw_global.cache->unload = nwrap_pw_unload;
1243 nwrap_gr_global.cache = &__nwrap_cache_gr;
1245 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
1246 nwrap_gr_global.cache->fd = -1;
1247 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
1248 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
1249 nwrap_gr_global.cache->unload = nwrap_gr_unload;
1251 nwrap_he_global.cache = &__nwrap_cache_he;
1253 nwrap_he_global.cache->path = getenv("NSS_WRAPPER_HOSTS");
1254 nwrap_he_global.cache->fd = -1;
1255 nwrap_he_global.cache->private_data = &nwrap_he_global;
1256 nwrap_he_global.cache->parse_line = nwrap_he_parse_line;
1257 nwrap_he_global.cache->unload = nwrap_he_unload;
1260 bool nss_wrapper_enabled(void)
1262 nwrap_init();
1264 if (nwrap_pw_global.cache->path == NULL ||
1265 nwrap_pw_global.cache->path[0] == '\0') {
1266 return false;
1268 if (nwrap_gr_global.cache->path == NULL ||
1269 nwrap_gr_global.cache->path[0] == '\0') {
1270 return false;
1273 return true;
1276 bool nss_wrapper_hosts_enabled(void)
1278 nwrap_init();
1280 if (nwrap_he_global.cache->path == NULL ||
1281 nwrap_he_global.cache->path[0] == '\0') {
1282 return false;
1285 return true;
1288 static bool nwrap_hostname_enabled(void)
1290 nwrap_init();
1292 if (getenv("NSS_WRAPPER_HOSTNAME") == NULL) {
1293 return false;
1296 return true;
1299 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
1301 int ret;
1302 uint8_t *buf = NULL;
1303 char *nline;
1305 if (nwrap->st.st_size == 0) {
1306 NWRAP_LOG(NWRAP_LOG_DEBUG, "size == 0");
1307 goto done;
1310 if (nwrap->st.st_size > INT32_MAX) {
1311 NWRAP_LOG(NWRAP_LOG_ERROR,
1312 "Size[%u] larger than INT32_MAX",
1313 (unsigned)nwrap->st.st_size);
1314 goto failed;
1317 ret = lseek(nwrap->fd, 0, SEEK_SET);
1318 if (ret != 0) {
1319 NWRAP_LOG(NWRAP_LOG_ERROR, "lseek - rc=%d\n", ret);
1320 goto failed;
1323 buf = (uint8_t *)malloc(nwrap->st.st_size + 1);
1324 if (!buf) {
1325 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1326 goto failed;
1329 ret = read(nwrap->fd, buf, nwrap->st.st_size);
1330 if (ret != nwrap->st.st_size) {
1331 NWRAP_LOG(NWRAP_LOG_ERROR,
1332 "read(%u) rc=%d\n",
1333 (unsigned)nwrap->st.st_size, ret);
1334 goto failed;
1337 buf[nwrap->st.st_size] = '\0';
1339 nline = (char *)buf;
1340 while (nline != NULL && nline[0] != '\0') {
1341 char *line;
1342 char *e;
1343 bool ok;
1345 line = nline;
1346 nline = NULL;
1348 e = strchr(line, '\n');
1349 if (e) {
1350 e[0] = '\0';
1351 e++;
1352 if (e[0] == '\r') {
1353 e[0] = '\0';
1354 e++;
1356 nline = e;
1359 if (strlen(line) == 0) {
1360 continue;
1363 ok = nwrap->parse_line(nwrap, line);
1364 if (!ok) {
1365 goto failed;
1369 done:
1370 nwrap->buf = buf;
1371 return true;
1373 failed:
1374 if (buf) free(buf);
1375 return false;
1378 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
1380 nwrap->unload(nwrap);
1382 if (nwrap->buf) free(nwrap->buf);
1384 nwrap->buf = NULL;
1387 static void nwrap_files_cache_reload(struct nwrap_cache *nwrap)
1389 struct stat st;
1390 int ret;
1391 bool ok;
1392 bool retried = false;
1394 reopen:
1395 if (nwrap->fd < 0) {
1396 nwrap->fd = open(nwrap->path, O_RDONLY);
1397 if (nwrap->fd < 0) {
1398 NWRAP_LOG(NWRAP_LOG_ERROR,
1399 "Unable to open '%s' readonly %d:%s",
1400 nwrap->path, nwrap->fd,
1401 strerror(errno));
1402 return;
1404 NWRAP_LOG(NWRAP_LOG_DEBUG, "Open '%s'", nwrap->path);
1407 ret = fstat(nwrap->fd, &st);
1408 if (ret != 0) {
1409 NWRAP_LOG(NWRAP_LOG_ERROR,
1410 "fstat(%s) - %d:%s",
1411 nwrap->path,
1412 ret,
1413 strerror(errno));
1414 return;
1417 if (retried == false && st.st_nlink == 0) {
1418 /* maybe someone has replaced the file... */
1419 NWRAP_LOG(NWRAP_LOG_TRACE,
1420 "st_nlink == 0, reopen %s",
1421 nwrap->path);
1422 retried = true;
1423 memset(&nwrap->st, 0, sizeof(nwrap->st));
1424 close(nwrap->fd);
1425 nwrap->fd = -1;
1426 goto reopen;
1429 if (st.st_mtime == nwrap->st.st_mtime) {
1430 NWRAP_LOG(NWRAP_LOG_TRACE,
1431 "st_mtime[%u] hasn't changed, skip reload",
1432 (unsigned)st.st_mtime);
1433 return;
1436 NWRAP_LOG(NWRAP_LOG_TRACE,
1437 "st_mtime has changed [%u] => [%u], start reload",
1438 (unsigned)st.st_mtime,
1439 (unsigned)nwrap->st.st_mtime);
1441 nwrap->st = st;
1443 nwrap_files_cache_unload(nwrap);
1445 ok = nwrap_parse_file(nwrap);
1446 if (!ok) {
1447 NWRAP_LOG(NWRAP_LOG_ERROR, "Failed to reload %s", nwrap->path);
1448 nwrap_files_cache_unload(nwrap);
1451 NWRAP_LOG(NWRAP_LOG_TRACE, "Reloaded %s", nwrap->path);
1455 * the caller has to call nwrap_unload() on failure
1457 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
1459 struct nwrap_pw *nwrap_pw;
1460 char *c;
1461 char *p;
1462 char *e;
1463 struct passwd *pw;
1464 size_t list_size;
1466 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1468 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
1469 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
1470 if (!pw) {
1471 NWRAP_LOG(NWRAP_LOG_ERROR,
1472 "realloc(%u) failed",
1473 (unsigned)list_size);
1474 return false;
1476 nwrap_pw->list = pw;
1478 pw = &nwrap_pw->list[nwrap_pw->num];
1480 c = line;
1482 /* name */
1483 p = strchr(c, ':');
1484 if (!p) {
1485 NWRAP_LOG(NWRAP_LOG_ERROR,
1486 "Invalid line[%s]: '%s'",
1487 line,
1489 return false;
1491 *p = '\0';
1492 p++;
1493 pw->pw_name = c;
1494 c = p;
1496 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]\n", pw->pw_name);
1498 /* password */
1499 p = strchr(c, ':');
1500 if (!p) {
1501 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1502 return false;
1504 *p = '\0';
1505 p++;
1506 pw->pw_passwd = c;
1507 c = p;
1509 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]\n", pw->pw_passwd);
1511 /* uid */
1512 p = strchr(c, ':');
1513 if (!p) {
1514 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1515 return false;
1517 *p = '\0';
1518 p++;
1519 e = NULL;
1520 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
1521 if (c == e) {
1522 NWRAP_LOG(NWRAP_LOG_ERROR,
1523 "Invalid line[%s]: '%s' - %s",
1524 line, c, strerror(errno));
1525 return false;
1527 if (e == NULL) {
1528 NWRAP_LOG(NWRAP_LOG_ERROR,
1529 "Invalid line[%s]: '%s' - %s",
1530 line, c, strerror(errno));
1531 return false;
1533 if (e[0] != '\0') {
1534 NWRAP_LOG(NWRAP_LOG_ERROR,
1535 "Invalid line[%s]: '%s' - %s",
1536 line, c, strerror(errno));
1537 return false;
1539 c = p;
1541 NWRAP_LOG(NWRAP_LOG_TRACE, "uid[%u]", pw->pw_uid);
1543 /* gid */
1544 p = strchr(c, ':');
1545 if (!p) {
1546 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1547 return false;
1549 *p = '\0';
1550 p++;
1551 e = NULL;
1552 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
1553 if (c == e) {
1554 NWRAP_LOG(NWRAP_LOG_ERROR,
1555 "Invalid line[%s]: '%s' - %s",
1556 line, c, strerror(errno));
1557 return false;
1559 if (e == NULL) {
1560 NWRAP_LOG(NWRAP_LOG_ERROR,
1561 "Invalid line[%s]: '%s' - %s",
1562 line, c, strerror(errno));
1563 return false;
1565 if (e[0] != '\0') {
1566 NWRAP_LOG(NWRAP_LOG_ERROR,
1567 "Invalid line[%s]: '%s' - %s",
1568 line, c, strerror(errno));
1569 return false;
1571 c = p;
1573 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]\n", pw->pw_gid);
1575 /* gecos */
1576 p = strchr(c, ':');
1577 if (!p) {
1578 NWRAP_LOG(NWRAP_LOG_ERROR, "invalid line[%s]: '%s'", line, c);
1579 return false;
1581 *p = '\0';
1582 p++;
1583 pw->pw_gecos = c;
1584 c = p;
1586 NWRAP_LOG(NWRAP_LOG_TRACE, "gecos[%s]", pw->pw_gecos);
1588 /* dir */
1589 p = strchr(c, ':');
1590 if (!p) {
1591 NWRAP_LOG(NWRAP_LOG_ERROR, "'%s'", c);
1592 return false;
1594 *p = '\0';
1595 p++;
1596 pw->pw_dir = c;
1597 c = p;
1599 NWRAP_LOG(NWRAP_LOG_TRACE, "dir[%s]", pw->pw_dir);
1601 /* shell */
1602 pw->pw_shell = c;
1603 NWRAP_LOG(NWRAP_LOG_TRACE, "shell[%s]", pw->pw_shell);
1605 NWRAP_LOG(NWRAP_LOG_DEBUG,
1606 "Added user[%s:%s:%u:%u:%s:%s:%s]",
1607 pw->pw_name, pw->pw_passwd,
1608 pw->pw_uid, pw->pw_gid,
1609 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
1611 nwrap_pw->num++;
1612 return true;
1615 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
1617 struct nwrap_pw *nwrap_pw;
1618 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
1620 if (nwrap_pw->list) free(nwrap_pw->list);
1622 nwrap_pw->list = NULL;
1623 nwrap_pw->num = 0;
1624 nwrap_pw->idx = 0;
1627 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
1628 char *buf, size_t buflen, struct passwd **dstp)
1630 char *first;
1631 char *last;
1632 off_t ofs;
1634 first = src->pw_name;
1636 last = src->pw_shell;
1637 while (*last) last++;
1639 ofs = PTR_DIFF(last + 1, first);
1641 if (ofs > (off_t) buflen) {
1642 return ERANGE;
1645 memcpy(buf, first, ofs);
1647 ofs = PTR_DIFF(src->pw_name, first);
1648 dst->pw_name = buf + ofs;
1649 ofs = PTR_DIFF(src->pw_passwd, first);
1650 dst->pw_passwd = buf + ofs;
1651 dst->pw_uid = src->pw_uid;
1652 dst->pw_gid = src->pw_gid;
1653 ofs = PTR_DIFF(src->pw_gecos, first);
1654 dst->pw_gecos = buf + ofs;
1655 ofs = PTR_DIFF(src->pw_dir, first);
1656 dst->pw_dir = buf + ofs;
1657 ofs = PTR_DIFF(src->pw_shell, first);
1658 dst->pw_shell = buf + ofs;
1660 if (dstp) {
1661 *dstp = dst;
1664 return 0;
1668 * the caller has to call nwrap_unload() on failure
1670 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
1672 struct nwrap_gr *nwrap_gr;
1673 char *c;
1674 char *p;
1675 char *e;
1676 struct group *gr;
1677 size_t list_size;
1678 unsigned nummem;
1680 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
1682 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
1683 gr = (struct group *)realloc(nwrap_gr->list, list_size);
1684 if (!gr) {
1685 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc failed");
1686 return false;
1688 nwrap_gr->list = gr;
1690 gr = &nwrap_gr->list[nwrap_gr->num];
1692 c = line;
1694 /* name */
1695 p = strchr(c, ':');
1696 if (!p) {
1697 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1698 return false;
1700 *p = '\0';
1701 p++;
1702 gr->gr_name = c;
1703 c = p;
1705 NWRAP_LOG(NWRAP_LOG_TRACE, "name[%s]", gr->gr_name);
1707 /* password */
1708 p = strchr(c, ':');
1709 if (!p) {
1710 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1711 return false;
1713 *p = '\0';
1714 p++;
1715 gr->gr_passwd = c;
1716 c = p;
1718 NWRAP_LOG(NWRAP_LOG_TRACE, "password[%s]", gr->gr_passwd);
1720 /* gid */
1721 p = strchr(c, ':');
1722 if (!p) {
1723 NWRAP_LOG(NWRAP_LOG_ERROR, "Invalid line[%s]: '%s'", line, c);
1724 return false;
1726 *p = '\0';
1727 p++;
1728 e = NULL;
1729 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
1730 if (c == e) {
1731 NWRAP_LOG(NWRAP_LOG_ERROR,
1732 "Invalid line[%s]: '%s' - %s",
1733 line, c, strerror(errno));
1734 return false;
1736 if (e == NULL) {
1737 NWRAP_LOG(NWRAP_LOG_ERROR,
1738 "Invalid line[%s]: '%s' - %s",
1739 line, c, strerror(errno));
1740 return false;
1742 if (e[0] != '\0') {
1743 NWRAP_LOG(NWRAP_LOG_ERROR,
1744 "Invalid line[%s]: '%s' - %s",
1745 line, c, strerror(errno));
1746 return false;
1748 c = p;
1750 NWRAP_LOG(NWRAP_LOG_TRACE, "gid[%u]", gr->gr_gid);
1752 /* members */
1753 gr->gr_mem = (char **)malloc(sizeof(char *));
1754 if (!gr->gr_mem) {
1755 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
1756 return false;
1758 gr->gr_mem[0] = NULL;
1760 for(nummem=0; p; nummem++) {
1761 char **m;
1762 size_t m_size;
1763 c = p;
1764 p = strchr(c, ',');
1765 if (p) {
1766 *p = '\0';
1767 p++;
1770 if (strlen(c) == 0) {
1771 break;
1774 m_size = sizeof(char *) * (nummem+2);
1775 m = (char **)realloc(gr->gr_mem, m_size);
1776 if (!m) {
1777 NWRAP_LOG(NWRAP_LOG_ERROR,
1778 "realloc(%zd) failed",
1779 m_size);
1780 return false;
1782 gr->gr_mem = m;
1783 gr->gr_mem[nummem] = c;
1784 gr->gr_mem[nummem+1] = NULL;
1786 NWRAP_LOG(NWRAP_LOG_TRACE,
1787 "member[%u]: '%s'",
1788 nummem, gr->gr_mem[nummem]);
1791 NWRAP_LOG(NWRAP_LOG_DEBUG,
1792 "Added group[%s:%s:%u:] with %u members",
1793 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem);
1795 nwrap_gr->num++;
1796 return true;
1799 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
1801 int i;
1802 struct nwrap_gr *nwrap_gr;
1803 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
1805 if (nwrap_gr->list) {
1806 for (i=0; i < nwrap_gr->num; i++) {
1807 if (nwrap_gr->list[i].gr_mem) {
1808 free(nwrap_gr->list[i].gr_mem);
1811 free(nwrap_gr->list);
1814 nwrap_gr->list = NULL;
1815 nwrap_gr->num = 0;
1816 nwrap_gr->idx = 0;
1819 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
1820 char *buf, size_t buflen, struct group **dstp)
1822 char *first;
1823 char **lastm;
1824 char *last = NULL;
1825 off_t ofsb;
1826 off_t ofsm;
1827 off_t ofs;
1828 unsigned i;
1830 first = src->gr_name;
1832 lastm = src->gr_mem;
1833 while (*lastm) {
1834 last = *lastm;
1835 lastm++;
1838 if (last == NULL) {
1839 last = src->gr_passwd;
1841 while (*last) last++;
1843 ofsb = PTR_DIFF(last + 1, first);
1844 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
1846 if ((ofsb + ofsm) > (off_t) buflen) {
1847 return ERANGE;
1850 memcpy(buf, first, ofsb);
1851 memcpy(buf + ofsb, src->gr_mem, ofsm);
1853 ofs = PTR_DIFF(src->gr_name, first);
1854 dst->gr_name = buf + ofs;
1855 ofs = PTR_DIFF(src->gr_passwd, first);
1856 dst->gr_passwd = buf + ofs;
1857 dst->gr_gid = src->gr_gid;
1859 dst->gr_mem = (char **)(buf + ofsb);
1860 for (i=0; src->gr_mem[i]; i++) {
1861 ofs = PTR_DIFF(src->gr_mem[i], first);
1862 dst->gr_mem[i] = buf + ofs;
1865 if (dstp) {
1866 *dstp = dst;
1869 return 0;
1872 static bool nwrap_he_parse_line(struct nwrap_cache *nwrap, char *line)
1874 struct nwrap_he *nwrap_he = (struct nwrap_he *)nwrap->private_data;
1875 struct nwrap_entdata *ed;
1876 size_t list_size;
1877 bool do_aliases = true;
1878 int aliases_count = 0;
1879 char *p;
1880 char *i;
1881 char *n;
1883 list_size = sizeof(struct nwrap_entdata) * (nwrap_he->num + 1);
1885 ed = (struct nwrap_entdata *)realloc(nwrap_he->list, list_size);
1886 if (ed == NULL) {
1887 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc[%zd] failed", list_size);
1888 return false;
1890 nwrap_he->list = ed;
1892 /* set it to the last element */
1893 ed = &(nwrap_he->list[nwrap_he->num]);
1895 ed->addr = malloc(sizeof(struct nwrap_addrdata));
1896 if (ed->addr == NULL) {
1897 NWRAP_LOG(NWRAP_LOG_ERROR, "realloc[%zd] failed", list_size);
1898 return false;
1901 i = line;
1904 * IP
1907 /* Walk to first char */
1908 for (p = i; *p != '.' && *p != ':' && !isxdigit((int) *p); p++) {
1909 if (*p == '\0') {
1910 NWRAP_LOG(NWRAP_LOG_ERROR,
1911 "Invalid line[%s]: '%s'",
1912 line, i);
1913 return false;
1917 for (i = p; !isspace((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 *p = '\0';
1928 if (inet_pton(AF_INET, i, ed->addr->host_addr)) {
1929 ed->ht.h_addrtype = AF_INET;
1930 ed->ht.h_length = 4;
1931 #ifdef HAVE_IPV6
1932 } else if (inet_pton(AF_INET6, i, ed->addr->host_addr)) {
1933 ed->ht.h_addrtype = AF_INET6;
1934 ed->ht.h_length = 16;
1935 #endif
1936 } else {
1937 NWRAP_LOG(NWRAP_LOG_ERROR,
1938 "Invalid line[%s]: '%s'",
1939 line, i);
1941 return false;
1944 ed->addr->h_addr_ptrs[0] = (char *)ed->addr->host_addr;
1945 ed->addr->h_addr_ptrs[1] = NULL;
1947 ed->ht.h_addr_list = ed->addr->h_addr_ptrs;
1949 p++;
1952 * FQDN
1955 /* Walk to first char */
1956 for (n = p; *p != '_' && !isalnum((int) *p); p++) {
1957 if (*p == '\0') {
1958 NWRAP_LOG(NWRAP_LOG_ERROR,
1959 "Invalid line[%s]: '%s'",
1960 line, n);
1962 return false;
1966 for (n = p; !isspace((int)*p); p++) {
1967 if (*p == '\0') {
1968 do_aliases = false;
1969 break;
1973 *p = '\0';
1975 ed->ht.h_name = n;
1977 /* glib's getent always dereferences he->h_aliases */
1978 ed->ht.h_aliases = malloc(sizeof(char *));
1979 if (ed->ht.h_aliases == NULL) {
1980 return false;
1982 ed->ht.h_aliases[0] = NULL;
1985 * Aliases
1987 while (do_aliases) {
1988 char **aliases;
1989 char *a;
1991 p++;
1993 /* Walk to first char */
1994 for (a = p; *p != '_' && !isalnum((int) *p); p++) {
1995 if (*p == '\0') {
1996 do_aliases = false;
1997 break;
2000 /* Only trailing spaces are left */
2001 if (!do_aliases) {
2002 break;
2005 for (a = p; !isspace((int)*p); p++) {
2006 if (*p == '\0') {
2007 do_aliases = false;
2008 break;
2012 *p = '\0';
2014 aliases = realloc(ed->ht.h_aliases, sizeof(char *) * (aliases_count + 2));
2015 if (aliases == NULL) {
2016 return false;
2018 ed->ht.h_aliases = aliases;
2020 aliases[aliases_count] = a;
2021 aliases[aliases_count + 1] = NULL;
2023 aliases_count++;
2026 nwrap_he->num++;
2027 return true;
2030 static void nwrap_he_unload(struct nwrap_cache *nwrap)
2032 struct nwrap_he *nwrap_he =
2033 (struct nwrap_he *)nwrap->private_data;
2034 int i;
2036 if (nwrap_he->list != NULL) {
2037 for (i = 0; i < nwrap_he->num; i++) {
2038 if (nwrap_he->list[i].ht.h_aliases != NULL) {
2039 free(nwrap_he->list[i].ht.h_aliases);
2041 if (nwrap_he->list[i].addr != NULL) {
2042 free(nwrap_he->list[i].addr);
2045 free(nwrap_he->list);
2048 nwrap_he->list = NULL;
2049 nwrap_he->num = 0;
2050 nwrap_he->idx = 0;
2054 /* user functions */
2055 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
2056 const char *name)
2058 int i;
2060 (void) b; /* unused */
2062 NWRAP_LOG(NWRAP_LOG_DEBUG, "Lookup user %s in files", name);
2064 nwrap_files_cache_reload(nwrap_pw_global.cache);
2066 for (i=0; i<nwrap_pw_global.num; i++) {
2067 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
2068 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] found", name);
2069 return &nwrap_pw_global.list[i];
2071 NWRAP_LOG(NWRAP_LOG_DEBUG,
2072 "user[%s] does not match [%s]",
2073 name,
2074 nwrap_pw_global.list[i].pw_name);
2077 NWRAP_LOG(NWRAP_LOG_DEBUG, "user[%s] not found\n", name);
2079 errno = ENOENT;
2080 return NULL;
2083 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
2084 const char *name, struct passwd *pwdst,
2085 char *buf, size_t buflen, struct passwd **pwdstp)
2087 struct passwd *pw;
2089 pw = nwrap_files_getpwnam(b, name);
2090 if (!pw) {
2091 if (errno == 0) {
2092 return ENOENT;
2094 return errno;
2097 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2100 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
2101 uid_t uid)
2103 int i;
2105 (void) b; /* unused */
2107 nwrap_files_cache_reload(nwrap_pw_global.cache);
2109 for (i=0; i<nwrap_pw_global.num; i++) {
2110 if (nwrap_pw_global.list[i].pw_uid == uid) {
2111 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] found", uid);
2112 return &nwrap_pw_global.list[i];
2114 NWRAP_LOG(NWRAP_LOG_DEBUG,
2115 "uid[%u] does not match [%u]",
2116 uid,
2117 nwrap_pw_global.list[i].pw_uid);
2120 NWRAP_LOG(NWRAP_LOG_DEBUG, "uid[%u] not found\n", uid);
2122 errno = ENOENT;
2123 return NULL;
2126 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
2127 uid_t uid, struct passwd *pwdst,
2128 char *buf, size_t buflen, struct passwd **pwdstp)
2130 struct passwd *pw;
2132 pw = nwrap_files_getpwuid(b, uid);
2133 if (!pw) {
2134 if (errno == 0) {
2135 return ENOENT;
2137 return errno;
2140 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2143 /* user enum functions */
2144 static void nwrap_files_setpwent(struct nwrap_backend *b)
2146 (void) b; /* unused */
2148 nwrap_pw_global.idx = 0;
2151 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
2153 struct passwd *pw;
2155 (void) b; /* unused */
2157 if (nwrap_pw_global.idx == 0) {
2158 nwrap_files_cache_reload(nwrap_pw_global.cache);
2161 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
2162 errno = ENOENT;
2163 return NULL;
2166 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
2168 NWRAP_LOG(NWRAP_LOG_DEBUG,
2169 "return user[%s] uid[%u]",
2170 pw->pw_name, pw->pw_uid);
2172 return pw;
2175 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
2176 struct passwd *pwdst, char *buf,
2177 size_t buflen, struct passwd **pwdstp)
2179 struct passwd *pw;
2181 pw = nwrap_files_getpwent(b);
2182 if (!pw) {
2183 if (errno == 0) {
2184 return ENOENT;
2186 return errno;
2189 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
2192 static void nwrap_files_endpwent(struct nwrap_backend *b)
2194 (void) b; /* unused */
2196 nwrap_pw_global.idx = 0;
2199 /* misc functions */
2200 static int nwrap_files_initgroups(struct nwrap_backend *b,
2201 const char *user, gid_t group)
2203 (void) b; /* unused */
2204 (void) user; /* unused */
2205 (void) group; /* used */
2207 /* TODO: maybe we should also fake this... */
2208 return EPERM;
2211 /* group functions */
2212 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
2213 const char *name)
2215 int i;
2217 (void) b; /* unused */
2219 nwrap_files_cache_reload(nwrap_gr_global.cache);
2221 for (i=0; i<nwrap_gr_global.num; i++) {
2222 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
2223 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] found", name);
2224 return &nwrap_gr_global.list[i];
2226 NWRAP_LOG(NWRAP_LOG_DEBUG,
2227 "group[%s] does not match [%s]",
2228 name,
2229 nwrap_gr_global.list[i].gr_name);
2232 NWRAP_LOG(NWRAP_LOG_DEBUG, "group[%s] not found", name);
2234 errno = ENOENT;
2235 return NULL;
2238 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
2239 const char *name, struct group *grdst,
2240 char *buf, size_t buflen, struct group **grdstp)
2242 struct group *gr;
2244 gr = nwrap_files_getgrnam(b, name);
2245 if (!gr) {
2246 if (errno == 0) {
2247 return ENOENT;
2249 return errno;
2252 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
2255 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
2256 gid_t gid)
2258 int i;
2260 (void) b; /* unused */
2262 nwrap_files_cache_reload(nwrap_gr_global.cache);
2264 for (i=0; i<nwrap_gr_global.num; i++) {
2265 if (nwrap_gr_global.list[i].gr_gid == gid) {
2266 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] found", gid);
2267 return &nwrap_gr_global.list[i];
2269 NWRAP_LOG(NWRAP_LOG_DEBUG,
2270 "gid[%u] does not match [%u]",
2271 gid,
2272 nwrap_gr_global.list[i].gr_gid);
2275 NWRAP_LOG(NWRAP_LOG_DEBUG, "gid[%u] not found", gid);
2277 errno = ENOENT;
2278 return NULL;
2281 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
2282 gid_t gid, struct group *grdst,
2283 char *buf, size_t buflen, struct group **grdstp)
2285 struct group *gr;
2287 gr = nwrap_files_getgrgid(b, gid);
2288 if (!gr) {
2289 if (errno == 0) {
2290 return ENOENT;
2292 return errno;
2295 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
2298 /* group enum functions */
2299 static void nwrap_files_setgrent(struct nwrap_backend *b)
2301 (void) b; /* unused */
2303 nwrap_gr_global.idx = 0;
2306 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
2308 struct group *gr;
2310 (void) b; /* unused */
2312 if (nwrap_gr_global.idx == 0) {
2313 nwrap_files_cache_reload(nwrap_gr_global.cache);
2316 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
2317 errno = ENOENT;
2318 return NULL;
2321 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
2323 NWRAP_LOG(NWRAP_LOG_DEBUG,
2324 "return group[%s] gid[%u]",
2325 gr->gr_name, gr->gr_gid);
2327 return gr;
2330 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
2331 struct group *grdst, char *buf,
2332 size_t buflen, struct group **grdstp)
2334 struct group *gr;
2336 gr = nwrap_files_getgrent(b);
2337 if (!gr) {
2338 if (errno == 0) {
2339 return ENOENT;
2341 return errno;
2344 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
2347 static void nwrap_files_endgrent(struct nwrap_backend *b)
2349 (void) b; /* unused */
2351 nwrap_gr_global.idx = 0;
2354 /* hosts functions */
2355 static struct hostent *nwrap_files_gethostbyname(const char *name, int af)
2357 struct hostent *he;
2358 char canon_name[DNS_NAME_MAX] = { 0 };
2359 size_t name_len;
2360 int i;
2362 nwrap_files_cache_reload(nwrap_he_global.cache);
2364 name_len = strlen(name);
2365 if (name_len < sizeof(canon_name) && name[name_len - 1] == '.') {
2366 strncpy(canon_name, name, name_len - 1);
2367 name = canon_name;
2370 for (i = 0; i < nwrap_he_global.num; i++) {
2371 int j;
2373 he = &nwrap_he_global.list[i].ht;
2375 /* Filter by address familiy if provided */
2376 if (af != AF_UNSPEC && he->h_addrtype != af) {
2377 continue;
2380 if (strcasecmp(he->h_name, name) == 0) {
2381 NWRAP_LOG(NWRAP_LOG_DEBUG, "name[%s] found", name);
2382 return he;
2385 if (he->h_aliases == NULL) {
2386 continue;
2389 for (j = 0; he->h_aliases[j] != NULL; j++) {
2390 if (strcasecmp(he->h_aliases[j], name) == 0) {
2391 NWRAP_LOG(NWRAP_LOG_DEBUG,
2392 "name[%s] found",
2393 name);
2394 return he;
2399 errno = ENOENT;
2400 return NULL;
2403 #ifdef HAVE_GETHOSTBYNAME_R
2404 static int nwrap_gethostbyname_r(const char *name,
2405 struct hostent *ret,
2406 char *buf, size_t buflen,
2407 struct hostent **result, int *h_errnop)
2409 *result = nwrap_files_gethostbyname(name, AF_UNSPEC);
2410 if (*result != NULL) {
2411 memset(buf, '\0', buflen);
2412 *ret = **result;
2413 return 0;
2414 } else {
2415 *h_errnop = h_errno;
2416 return -1;
2420 int gethostbyname_r(const char *name,
2421 struct hostent *ret,
2422 char *buf, size_t buflen,
2423 struct hostent **result, int *h_errnop)
2425 if (!nss_wrapper_hosts_enabled()) {
2426 return libc_gethostbyname_r(name,
2427 ret,
2428 buf,
2429 buflen,
2430 result,
2431 h_errnop);
2434 return nwrap_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
2436 #endif
2438 static struct hostent *nwrap_files_gethostbyaddr(const void *addr,
2439 socklen_t len, int type)
2441 struct hostent *he;
2442 char ip[INET6_ADDRSTRLEN] = {0};
2443 const char *a;
2444 int i;
2446 (void) len; /* unused */
2448 nwrap_files_cache_reload(nwrap_he_global.cache);
2450 a = inet_ntop(type, addr, ip, sizeof(ip));
2451 if (a == NULL) {
2452 errno = EINVAL;
2453 return NULL;
2456 for (i = 0; i < nwrap_he_global.num; i++) {
2457 he = &nwrap_he_global.list[i].ht;
2459 if (he->h_addrtype != type) {
2460 continue;
2463 if (memcmp(addr, he->h_addr_list[0], he->h_length) == 0) {
2464 return he;
2468 errno = ENOENT;
2469 return NULL;
2472 #ifdef HAVE_GETHOSTBYADDR_R
2473 static int nwrap_gethostbyaddr_r(const void *addr, socklen_t len, int type,
2474 struct hostent *ret,
2475 char *buf, size_t buflen,
2476 struct hostent **result, int *h_errnop)
2478 *result = nwrap_files_gethostbyaddr(addr, len, type);
2479 if (*result != NULL) {
2480 memset(buf, '\0', buflen);
2481 *ret = **result;
2482 return 0;
2483 } else {
2484 *h_errnop = h_errno;
2485 return -1;
2489 int gethostbyaddr_r(const void *addr, socklen_t len, int type,
2490 struct hostent *ret,
2491 char *buf, size_t buflen,
2492 struct hostent **result, int *h_errnop)
2494 if (!nss_wrapper_hosts_enabled()) {
2495 return libc_gethostbyaddr_r(addr,
2496 len,
2497 type,
2498 ret,
2499 buf,
2500 buflen,
2501 result,
2502 h_errnop);
2505 return nwrap_gethostbyaddr_r(addr, len, type, ret, buf, buflen, result, h_errnop);
2507 #endif
2509 /* hosts enum functions */
2510 static void nwrap_files_sethostent(void)
2512 nwrap_he_global.idx = 0;
2515 static struct hostent *nwrap_files_gethostent(void)
2517 struct hostent *he;
2519 if (nwrap_he_global.idx == 0) {
2520 nwrap_files_cache_reload(nwrap_he_global.cache);
2523 if (nwrap_he_global.idx >= nwrap_he_global.num) {
2524 errno = ENOENT;
2525 return NULL;
2528 he = &nwrap_he_global.list[nwrap_he_global.idx++].ht;
2530 NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
2532 return he;
2535 static void nwrap_files_endhostent(void)
2537 nwrap_he_global.idx = 0;
2541 * module backend
2544 #ifndef SAFE_FREE
2545 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
2546 #endif
2548 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
2549 const char *name)
2551 static struct passwd pwd;
2552 static char buf[1000];
2553 NSS_STATUS status;
2555 if (!b->fns->_nss_getpwnam_r) {
2556 return NULL;
2559 status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
2560 if (status == NSS_STATUS_NOTFOUND) {
2561 return NULL;
2563 if (status != NSS_STATUS_SUCCESS) {
2564 return NULL;
2567 return &pwd;
2570 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
2571 const char *name, struct passwd *pwdst,
2572 char *buf, size_t buflen, struct passwd **pwdstp)
2574 int ret;
2576 (void) b; /* unused */
2577 (void) pwdst; /* unused */
2578 (void) pwdstp; /* unused */
2580 if (!b->fns->_nss_getpwnam_r) {
2581 return NSS_STATUS_NOTFOUND;
2584 ret = b->fns->_nss_getpwnam_r(name, pwdst, buf, buflen, &errno);
2585 switch (ret) {
2586 case NSS_STATUS_SUCCESS:
2587 return 0;
2588 case NSS_STATUS_NOTFOUND:
2589 if (errno != 0) {
2590 return errno;
2592 return ENOENT;
2593 case NSS_STATUS_TRYAGAIN:
2594 if (errno != 0) {
2595 return errno;
2597 return ERANGE;
2598 default:
2599 if (errno != 0) {
2600 return errno;
2602 return ret;
2606 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
2607 uid_t uid)
2609 static struct passwd pwd;
2610 static char buf[1000];
2611 NSS_STATUS status;
2613 if (!b->fns->_nss_getpwuid_r) {
2614 return NULL;
2617 status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
2618 if (status == NSS_STATUS_NOTFOUND) {
2619 return NULL;
2621 if (status != NSS_STATUS_SUCCESS) {
2622 return NULL;
2624 return &pwd;
2627 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
2628 uid_t uid, struct passwd *pwdst,
2629 char *buf, size_t buflen, struct passwd **pwdstp)
2631 int ret;
2633 (void) pwdstp; /* unused */
2635 if (!b->fns->_nss_getpwuid_r) {
2636 return ENOENT;
2639 ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
2640 switch (ret) {
2641 case NSS_STATUS_SUCCESS:
2642 return 0;
2643 case NSS_STATUS_NOTFOUND:
2644 if (errno != 0) {
2645 return errno;
2647 return ENOENT;
2648 case NSS_STATUS_TRYAGAIN:
2649 if (errno != 0) {
2650 return errno;
2652 return ERANGE;
2653 default:
2654 if (errno != 0) {
2655 return errno;
2657 return ret;
2661 static void nwrap_module_setpwent(struct nwrap_backend *b)
2663 if (!b->fns->_nss_setpwent) {
2664 return;
2667 b->fns->_nss_setpwent();
2670 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
2672 static struct passwd pwd;
2673 static char buf[1000];
2674 NSS_STATUS status;
2676 if (!b->fns->_nss_getpwent_r) {
2677 return NULL;
2680 status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
2681 if (status == NSS_STATUS_NOTFOUND) {
2682 return NULL;
2684 if (status != NSS_STATUS_SUCCESS) {
2685 return NULL;
2687 return &pwd;
2690 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
2691 struct passwd *pwdst, char *buf,
2692 size_t buflen, struct passwd **pwdstp)
2694 int ret;
2696 (void) pwdstp; /* unused */
2698 if (!b->fns->_nss_getpwent_r) {
2699 return ENOENT;
2702 ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
2703 switch (ret) {
2704 case NSS_STATUS_SUCCESS:
2705 return 0;
2706 case NSS_STATUS_NOTFOUND:
2707 if (errno != 0) {
2708 return errno;
2710 return ENOENT;
2711 case NSS_STATUS_TRYAGAIN:
2712 if (errno != 0) {
2713 return errno;
2715 return ERANGE;
2716 default:
2717 if (errno != 0) {
2718 return errno;
2720 return ret;
2724 static void nwrap_module_endpwent(struct nwrap_backend *b)
2726 if (!b->fns->_nss_endpwent) {
2727 return;
2730 b->fns->_nss_endpwent();
2733 static int nwrap_module_initgroups(struct nwrap_backend *b,
2734 const char *user, gid_t group)
2736 gid_t *groups;
2737 long int start;
2738 long int size;
2740 if (!b->fns->_nss_initgroups) {
2741 return NSS_STATUS_UNAVAIL;
2744 return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
2747 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
2748 const char *name)
2750 static struct group grp;
2751 static char *buf;
2752 static int buflen = 1000;
2753 NSS_STATUS status;
2755 if (!b->fns->_nss_getgrnam_r) {
2756 return NULL;
2759 if (!buf) {
2760 buf = (char *)malloc(buflen);
2762 again:
2763 status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
2764 if (status == NSS_STATUS_TRYAGAIN) {
2765 buflen *= 2;
2766 buf = (char *)realloc(buf, buflen);
2767 if (!buf) {
2768 return NULL;
2770 goto again;
2772 if (status == NSS_STATUS_NOTFOUND) {
2773 SAFE_FREE(buf);
2774 return NULL;
2776 if (status != NSS_STATUS_SUCCESS) {
2777 SAFE_FREE(buf);
2778 return NULL;
2780 return &grp;
2783 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
2784 const char *name, struct group *grdst,
2785 char *buf, size_t buflen, struct group **grdstp)
2787 int ret;
2789 (void) grdstp; /* unused */
2791 if (!b->fns->_nss_getgrnam_r) {
2792 return ENOENT;
2795 ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
2796 switch (ret) {
2797 case NSS_STATUS_SUCCESS:
2798 return 0;
2799 case NSS_STATUS_NOTFOUND:
2800 if (errno != 0) {
2801 return errno;
2803 return ENOENT;
2804 case NSS_STATUS_TRYAGAIN:
2805 if (errno != 0) {
2806 return errno;
2808 return ERANGE;
2809 default:
2810 if (errno != 0) {
2811 return errno;
2813 return ret;
2817 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
2818 gid_t gid)
2820 static struct group grp;
2821 static char *buf;
2822 static int buflen = 1000;
2823 NSS_STATUS status;
2825 if (!b->fns->_nss_getgrgid_r) {
2826 return NULL;
2829 if (!buf) {
2830 buf = (char *)malloc(buflen);
2833 again:
2834 status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
2835 if (status == NSS_STATUS_TRYAGAIN) {
2836 buflen *= 2;
2837 buf = (char *)realloc(buf, buflen);
2838 if (!buf) {
2839 return NULL;
2841 goto again;
2843 if (status == NSS_STATUS_NOTFOUND) {
2844 SAFE_FREE(buf);
2845 return NULL;
2847 if (status != NSS_STATUS_SUCCESS) {
2848 SAFE_FREE(buf);
2849 return NULL;
2851 return &grp;
2854 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
2855 gid_t gid, struct group *grdst,
2856 char *buf, size_t buflen, struct group **grdstp)
2858 int ret;
2860 (void) grdstp; /* unused */
2862 if (!b->fns->_nss_getgrgid_r) {
2863 return ENOENT;
2866 ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
2867 switch (ret) {
2868 case NSS_STATUS_SUCCESS:
2869 return 0;
2870 case NSS_STATUS_NOTFOUND:
2871 if (errno != 0) {
2872 return errno;
2874 return ENOENT;
2875 case NSS_STATUS_TRYAGAIN:
2876 if (errno != 0) {
2877 return errno;
2879 return ERANGE;
2880 default:
2881 if (errno != 0) {
2882 return errno;
2884 return ret;
2888 static void nwrap_module_setgrent(struct nwrap_backend *b)
2890 if (!b->fns->_nss_setgrent) {
2891 return;
2894 b->fns->_nss_setgrent();
2897 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
2899 static struct group grp;
2900 static char *buf;
2901 static int buflen = 1024;
2902 NSS_STATUS status;
2904 if (!b->fns->_nss_getgrent_r) {
2905 return NULL;
2908 if (!buf) {
2909 buf = (char *)malloc(buflen);
2912 again:
2913 status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
2914 if (status == NSS_STATUS_TRYAGAIN) {
2915 buflen *= 2;
2916 buf = (char *)realloc(buf, buflen);
2917 if (!buf) {
2918 return NULL;
2920 goto again;
2922 if (status == NSS_STATUS_NOTFOUND) {
2923 SAFE_FREE(buf);
2924 return NULL;
2926 if (status != NSS_STATUS_SUCCESS) {
2927 SAFE_FREE(buf);
2928 return NULL;
2930 return &grp;
2933 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
2934 struct group *grdst, char *buf,
2935 size_t buflen, struct group **grdstp)
2937 int ret;
2939 (void) grdstp; /* unused */
2941 if (!b->fns->_nss_getgrent_r) {
2942 return ENOENT;
2945 ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
2946 switch (ret) {
2947 case NSS_STATUS_SUCCESS:
2948 return 0;
2949 case NSS_STATUS_NOTFOUND:
2950 if (errno != 0) {
2951 return errno;
2953 return ENOENT;
2954 case NSS_STATUS_TRYAGAIN:
2955 if (errno != 0) {
2956 return errno;
2958 return ERANGE;
2959 default:
2960 if (errno != 0) {
2961 return errno;
2963 return ret;
2967 static void nwrap_module_endgrent(struct nwrap_backend *b)
2969 if (!b->fns->_nss_endgrent) {
2970 return;
2973 b->fns->_nss_endgrent();
2976 /****************************************************************************
2977 * GETPWNAM
2978 ***************************************************************************/
2980 static struct passwd *nwrap_getpwnam(const char *name)
2982 int i;
2983 struct passwd *pwd;
2985 for (i=0; i < nwrap_main_global->num_backends; i++) {
2986 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2987 pwd = b->ops->nw_getpwnam(b, name);
2988 if (pwd) {
2989 return pwd;
2993 return NULL;
2996 struct passwd *getpwnam(const char *name)
2998 if (!nss_wrapper_enabled()) {
2999 return libc_getpwnam(name);
3002 return nwrap_getpwnam(name);
3005 /****************************************************************************
3006 * GETPWNAM_R
3007 ***************************************************************************/
3009 static int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
3010 char *buf, size_t buflen, struct passwd **pwdstp)
3012 int i,ret;
3014 for (i=0; i < nwrap_main_global->num_backends; i++) {
3015 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3016 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
3017 if (ret == ENOENT) {
3018 continue;
3020 return ret;
3023 return ENOENT;
3026 #ifdef HAVE_GETPWNAM_R
3027 # ifdef HAVE_SOLARIS_GETPWNAM_R
3028 int getpwnam_r(const char *name, struct passwd *pwdst,
3029 char *buf, int buflen, struct passwd **pwdstp)
3030 # else /* HAVE_SOLARIS_GETPWNAM_R */
3031 int getpwnam_r(const char *name, struct passwd *pwdst,
3032 char *buf, size_t buflen, struct passwd **pwdstp)
3033 # endif /* HAVE_SOLARIS_GETPWNAM_R */
3035 if (!nss_wrapper_enabled()) {
3036 return libc_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
3039 return nwrap_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
3041 #endif
3043 /****************************************************************************
3044 * GETPWUID
3045 ***************************************************************************/
3047 static struct passwd *nwrap_getpwuid(uid_t uid)
3049 int i;
3050 struct passwd *pwd;
3052 for (i=0; i < nwrap_main_global->num_backends; i++) {
3053 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3054 pwd = b->ops->nw_getpwuid(b, uid);
3055 if (pwd) {
3056 return pwd;
3060 return NULL;
3063 struct passwd *getpwuid(uid_t uid)
3065 if (!nss_wrapper_enabled()) {
3066 return libc_getpwuid(uid);
3069 return nwrap_getpwuid(uid);
3072 /****************************************************************************
3073 * GETPWUID_R
3074 ***************************************************************************/
3076 static int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
3077 char *buf, size_t buflen, struct passwd **pwdstp)
3079 int i,ret;
3081 for (i=0; i < nwrap_main_global->num_backends; i++) {
3082 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3083 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
3084 if (ret == ENOENT) {
3085 continue;
3087 return ret;
3090 return ENOENT;
3093 #ifdef HAVE_SOLARIS_GETPWUID_R
3094 int getpwuid_r(uid_t uid, struct passwd *pwdst,
3095 char *buf, int buflen, struct passwd **pwdstp)
3096 #else
3097 int getpwuid_r(uid_t uid, struct passwd *pwdst,
3098 char *buf, size_t buflen, struct passwd **pwdstp)
3099 #endif
3101 if (!nss_wrapper_enabled()) {
3102 return libc_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
3105 return nwrap_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
3108 /****************************************************************************
3109 * SETPWENT
3110 ***************************************************************************/
3112 static void nwrap_setpwent(void)
3114 int i;
3116 for (i=0; i < nwrap_main_global->num_backends; i++) {
3117 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3118 b->ops->nw_setpwent(b);
3122 void setpwent(void)
3124 if (!nss_wrapper_enabled()) {
3125 libc_setpwent();
3126 return;
3129 nwrap_setpwent();
3132 /****************************************************************************
3133 * GETPWENT
3134 ***************************************************************************/
3136 static struct passwd *nwrap_getpwent(void)
3138 int i;
3139 struct passwd *pwd;
3141 for (i=0; i < nwrap_main_global->num_backends; i++) {
3142 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3143 pwd = b->ops->nw_getpwent(b);
3144 if (pwd) {
3145 return pwd;
3149 return NULL;
3152 struct passwd *getpwent(void)
3154 if (!nss_wrapper_enabled()) {
3155 return libc_getpwent();
3158 return nwrap_getpwent();
3161 /****************************************************************************
3162 * GETPWENT_R
3163 ***************************************************************************/
3165 static int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
3166 size_t buflen, struct passwd **pwdstp)
3168 int i,ret;
3170 for (i=0; i < nwrap_main_global->num_backends; i++) {
3171 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3172 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
3173 if (ret == ENOENT) {
3174 continue;
3176 return ret;
3179 return ENOENT;
3182 #ifdef HAVE_SOLARIS_GETPWENT_R
3183 struct passwd *getpwent_r(struct passwd *pwdst, char *buf, int buflen)
3185 struct passwd *pwdstp = NULL;
3186 int rc;
3188 if (!nss_wrapper_enabled()) {
3189 return libc_getpwent_r(pwdst, buf, buflen);
3191 rc = nwrap_getpwent_r(pwdst, buf, buflen, &pwdstp);
3192 if (rc < 0) {
3193 return NULL;
3196 return pwdstp;
3198 #else /* HAVE_SOLARIS_GETPWENT_R */
3199 int getpwent_r(struct passwd *pwdst, char *buf,
3200 size_t buflen, struct passwd **pwdstp)
3202 if (!nss_wrapper_enabled()) {
3203 return libc_getpwent_r(pwdst, buf, buflen, pwdstp);
3206 return nwrap_getpwent_r(pwdst, buf, buflen, pwdstp);
3208 #endif /* HAVE_SOLARIS_GETPWENT_R */
3210 /****************************************************************************
3211 * ENDPWENT
3212 ***************************************************************************/
3214 static void nwrap_endpwent(void)
3216 int i;
3218 for (i=0; i < nwrap_main_global->num_backends; i++) {
3219 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3220 b->ops->nw_endpwent(b);
3224 void endpwent(void)
3226 if (!nss_wrapper_enabled()) {
3227 libc_endpwent();
3228 return;
3231 nwrap_endpwent();
3234 /****************************************************************************
3235 * INITGROUPS
3236 ***************************************************************************/
3238 static int nwrap_initgroups(const char *user, gid_t group)
3240 int i;
3242 for (i=0; i < nwrap_main_global->num_backends; i++) {
3243 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3244 int rc;
3246 rc = b->ops->nw_initgroups(b, user, group);
3247 if (rc == 0) {
3248 return 0;
3252 errno = ENOENT;
3253 return -1;
3256 int initgroups(const char *user, gid_t group)
3258 if (!nss_wrapper_enabled()) {
3259 return libc_initgroups(user, group);
3262 return nwrap_initgroups(user, group);
3265 /****************************************************************************
3266 * GETGRNAM
3267 ***************************************************************************/
3269 static struct group *nwrap_getgrnam(const char *name)
3271 int i;
3272 struct group *grp;
3274 for (i=0; i < nwrap_main_global->num_backends; i++) {
3275 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3276 grp = b->ops->nw_getgrnam(b, name);
3277 if (grp) {
3278 return grp;
3282 return NULL;
3285 struct group *getgrnam(const char *name)
3287 if (!nss_wrapper_enabled()) {
3288 return libc_getgrnam(name);
3291 return nwrap_getgrnam(name);
3294 /****************************************************************************
3295 * GETGRNAM_R
3296 ***************************************************************************/
3298 static int nwrap_getgrnam_r(const char *name, struct group *grdst,
3299 char *buf, size_t buflen, struct group **grdstp)
3301 int i, ret;
3303 for (i=0; i < nwrap_main_global->num_backends; i++) {
3304 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3305 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
3306 if (ret == ENOENT) {
3307 continue;
3309 return ret;
3312 return ENOENT;
3315 #ifdef HAVE_GETGRNAM_R
3316 # ifdef HAVE_SOLARIS_GETGRNAM_R
3317 int getgrnam_r(const char *name, struct group *grp,
3318 char *buf, int buflen, struct group **pgrp)
3319 # else /* HAVE_SOLARIS_GETGRNAM_R */
3320 int getgrnam_r(const char *name, struct group *grp,
3321 char *buf, size_t buflen, struct group **pgrp)
3322 # endif /* HAVE_SOLARIS_GETGRNAM_R */
3324 if (!nss_wrapper_enabled()) {
3325 return libc_getgrnam_r(name,
3326 grp,
3327 buf,
3328 buflen,
3329 pgrp);
3332 return nwrap_getgrnam_r(name, grp, buf, buflen, pgrp);
3334 #endif /* HAVE_GETGRNAM_R */
3336 /****************************************************************************
3337 * GETGRGID
3338 ***************************************************************************/
3340 static struct group *nwrap_getgrgid(gid_t gid)
3342 int i;
3343 struct group *grp;
3345 for (i=0; i < nwrap_main_global->num_backends; i++) {
3346 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3347 grp = b->ops->nw_getgrgid(b, gid);
3348 if (grp) {
3349 return grp;
3353 return NULL;
3356 struct group *getgrgid(gid_t gid)
3358 if (!nss_wrapper_enabled()) {
3359 return libc_getgrgid(gid);
3362 return nwrap_getgrgid(gid);
3365 /****************************************************************************
3366 * GETGRGID_R
3367 ***************************************************************************/
3369 static int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
3370 char *buf, size_t buflen, struct group **grdstp)
3372 int i,ret;
3374 for (i=0; i < nwrap_main_global->num_backends; i++) {
3375 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3376 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
3377 if (ret == ENOENT) {
3378 continue;
3380 return ret;
3383 return ENOENT;
3386 #ifdef HAVE_GETGRGID_R
3387 # ifdef HAVE_SOLARIS_GETGRGID_R
3388 int getgrgid_r(gid_t gid, struct group *grdst,
3389 char *buf, int buflen, struct group **grdstp)
3390 # else /* HAVE_SOLARIS_GETGRGID_R */
3391 int getgrgid_r(gid_t gid, struct group *grdst,
3392 char *buf, size_t buflen, struct group **grdstp)
3393 # endif /* HAVE_SOLARIS_GETGRGID_R */
3395 if (!nss_wrapper_enabled()) {
3396 return libc_getgrgid_r(gid, grdst, buf, buflen, grdstp);
3399 return nwrap_getgrgid_r(gid, grdst, buf, buflen, grdstp);
3401 #endif
3403 /****************************************************************************
3404 * SETGRENT
3405 ***************************************************************************/
3407 static void nwrap_setgrent(void)
3409 int i;
3411 for (i=0; i < nwrap_main_global->num_backends; i++) {
3412 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3413 b->ops->nw_setgrent(b);
3417 #ifdef HAVE_BSD_SETGRENT
3418 int setgrent(void)
3419 #else
3420 void setgrent(void)
3421 #endif
3423 if (!nss_wrapper_enabled()) {
3424 libc_setgrent();
3425 goto out;
3428 nwrap_setgrent();
3430 out:
3431 #ifdef HAVE_BSD_SETGRENT
3432 return 0;
3433 #else
3434 return;
3435 #endif
3438 /****************************************************************************
3439 * GETGRENT
3440 ***************************************************************************/
3442 static struct group *nwrap_getgrent(void)
3444 int i;
3445 struct group *grp;
3447 for (i=0; i < nwrap_main_global->num_backends; i++) {
3448 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3449 grp = b->ops->nw_getgrent(b);
3450 if (grp) {
3451 return grp;
3455 return NULL;
3458 struct group *getgrent(void)
3460 if (!nss_wrapper_enabled()) {
3461 return libc_getgrent();
3464 return nwrap_getgrent();
3467 /****************************************************************************
3468 * GETGRENT_R
3469 ***************************************************************************/
3471 static int nwrap_getgrent_r(struct group *grdst, char *buf,
3472 size_t buflen, struct group **grdstp)
3474 int i,ret;
3476 for (i=0; i < nwrap_main_global->num_backends; i++) {
3477 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3478 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
3479 if (ret == ENOENT) {
3480 continue;
3482 return ret;
3485 return ENOENT;
3488 #ifdef HAVE_SOLARIS_GETGRENT_R
3489 struct group *getgrent_r(struct group *src, char *buf, int buflen)
3491 struct group *grdstp = NULL;
3492 int rc;
3494 if (!nss_wrapper_enabled()) {
3495 return libc_getgrent_r(src, buf, buflen);
3498 rc = nwrap_getgrent_r(src, buf, buflen, &grdstp);
3499 if (rc < 0) {
3500 return NULL;
3503 return grdstp;
3505 #else /* HAVE_SOLARIS_GETGRENT_R */
3506 int getgrent_r(struct group *src, char *buf,
3507 size_t buflen, struct group **grdstp)
3509 if (!nss_wrapper_enabled()) {
3510 return libc_getgrent_r(src, buf, buflen, grdstp);
3513 return nwrap_getgrent_r(src, buf, buflen, grdstp);
3515 #endif /* HAVE_SOLARIS_GETGRENT_R */
3517 /****************************************************************************
3518 * ENDGRENT
3519 ***************************************************************************/
3521 static void nwrap_endgrent(void)
3523 int i;
3525 for (i=0; i < nwrap_main_global->num_backends; i++) {
3526 struct nwrap_backend *b = &nwrap_main_global->backends[i];
3527 b->ops->nw_endgrent(b);
3531 void endgrent(void)
3533 if (!nss_wrapper_enabled()) {
3534 libc_endgrent();
3535 return;
3538 nwrap_endgrent();
3541 /****************************************************************************
3542 * GETGROUPLIST
3543 ***************************************************************************/
3545 #ifdef HAVE_GETGROUPLIST
3546 static int nwrap_getgrouplist(const char *user, gid_t group,
3547 gid_t *groups, int *ngroups)
3549 struct group *grp;
3550 gid_t *groups_tmp;
3551 int count = 1;
3552 const char *name_of_group = "";
3554 NWRAP_LOG(NWRAP_LOG_DEBUG, "getgrouplist called for %s", user);
3556 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
3557 if (!groups_tmp) {
3558 NWRAP_LOG(NWRAP_LOG_ERROR, "Out of memory");
3559 errno = ENOMEM;
3560 return -1;
3563 memcpy(groups_tmp, &group, sizeof(gid_t));
3565 grp = nwrap_getgrgid(group);
3566 if (grp) {
3567 name_of_group = grp->gr_name;
3570 nwrap_setgrent();
3571 while ((grp = nwrap_getgrent()) != NULL) {
3572 int i = 0;
3574 NWRAP_LOG(NWRAP_LOG_DEBUG,
3575 "Inspecting %s for group membership",
3576 grp->gr_name);
3578 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
3580 if ((strcmp(user, grp->gr_mem[i]) == 0) &&
3581 (strcmp(name_of_group, grp->gr_name) != 0)) {
3583 NWRAP_LOG(NWRAP_LOG_DEBUG,
3584 "%s is member of %s",
3585 user,
3586 grp->gr_name);
3588 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
3589 if (!groups_tmp) {
3590 NWRAP_LOG(NWRAP_LOG_ERROR,
3591 "Out of memory");
3592 errno = ENOMEM;
3593 return -1;
3596 memcpy(&groups_tmp[count], &grp->gr_gid, sizeof(gid_t));
3597 count++;
3602 nwrap_endgrent();
3604 NWRAP_LOG(NWRAP_LOG_DEBUG,
3605 "%s is member of %d groups",
3606 user, *ngroups);
3608 if (*ngroups < count) {
3609 *ngroups = count;
3610 free(groups_tmp);
3611 return -1;
3614 *ngroups = count;
3615 memcpy(groups, groups_tmp, count * sizeof(gid_t));
3616 free(groups_tmp);
3618 return count;
3621 int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
3623 if (!nss_wrapper_enabled()) {
3624 return libc_getgrouplist(user, group, groups, ngroups);
3627 return nwrap_getgrouplist(user, group, groups, ngroups);
3629 #endif
3631 /**********************************************************
3632 * NETDB
3633 **********************************************************/
3635 static void nwrap_sethostent(int stayopen) {
3636 (void) stayopen; /* ignored */
3638 nwrap_files_sethostent();
3641 #ifdef HAVE_SOLARIS_SETHOSTENT
3642 int sethostent(int stayopen)
3644 if (!nss_wrapper_hosts_enabled()) {
3645 libc_sethostent(stayopen);
3646 return 0;
3649 nwrap_sethostent(stayopen);
3651 return 0;
3653 #else /* HAVE_SOLARIS_SETHOSTENT */
3654 void sethostent(int stayopen)
3656 if (!nss_wrapper_hosts_enabled()) {
3657 libc_sethostent(stayopen);
3658 return;
3661 nwrap_sethostent(stayopen);
3663 #endif /* HAVE_SOLARIS_SETHOSTENT */
3665 static struct hostent *nwrap_gethostent(void)
3667 return nwrap_files_gethostent();
3670 struct hostent *gethostent(void) {
3671 if (!nss_wrapper_hosts_enabled()) {
3672 return libc_gethostent();
3675 return nwrap_gethostent();
3678 static void nwrap_endhostent(void) {
3679 nwrap_files_endhostent();
3682 #ifdef HAVE_SOLARIS_ENDHOSTENT
3683 int endhostent(void)
3685 if (!nss_wrapper_hosts_enabled()) {
3686 libc_endhostent();
3687 return 0;
3690 nwrap_endhostent();
3692 return 0;
3694 #else /* HAVE_SOLARIS_ENDHOSTENT */
3695 void endhostent(void)
3697 if (!nss_wrapper_hosts_enabled()) {
3698 libc_endhostent();
3699 return;
3702 nwrap_endhostent();
3704 #endif /* HAVE_SOLARIS_ENDHOSTENT */
3706 static struct hostent *nwrap_gethostbyname(const char *name)
3708 return nwrap_files_gethostbyname(name, AF_UNSPEC);
3711 struct hostent *gethostbyname(const char *name)
3713 if (!nss_wrapper_hosts_enabled()) {
3714 return libc_gethostbyname(name);
3717 return nwrap_gethostbyname(name);
3720 /* This is a GNU extension */
3721 #ifdef HAVE_GETHOSTBYNAME2
3722 static struct hostent *nwrap_gethostbyname2(const char *name, int af)
3724 return nwrap_files_gethostbyname(name, af);
3727 struct hostent *gethostbyname2(const char *name, int af)
3729 if (!nss_wrapper_hosts_enabled()) {
3730 return libc_gethostbyname2(name, af);
3733 return nwrap_gethostbyname2(name, af);
3735 #endif
3737 static struct hostent *nwrap_gethostbyaddr(const void *addr,
3738 socklen_t len, int type)
3740 return nwrap_files_gethostbyaddr(addr, len, type);
3743 struct hostent *gethostbyaddr(const void *addr,
3744 socklen_t len, int type)
3746 if (!nss_wrapper_hosts_enabled()) {
3747 return libc_gethostbyaddr(addr, len, type);
3750 return nwrap_gethostbyaddr(addr, len, type);
3753 static const struct addrinfo default_hints =
3755 .ai_flags = AI_ADDRCONFIG|AI_V4MAPPED,
3756 .ai_family = AF_UNSPEC,
3757 .ai_socktype = 0,
3758 .ai_protocol = 0,
3759 .ai_addrlen = 0,
3760 .ai_addr = NULL,
3761 .ai_canonname = NULL,
3762 .ai_next = NULL
3765 static int nwrap_convert_he_ai(const struct hostent *he,
3766 unsigned short port,
3767 const struct addrinfo *hints,
3768 struct addrinfo **pai)
3770 struct addrinfo *ai;
3771 socklen_t socklen;
3773 switch (he->h_addrtype) {
3774 case AF_INET:
3775 socklen = sizeof(struct sockaddr_in);
3776 break;
3777 #ifdef HAVE_IPV6
3778 case AF_INET6:
3779 socklen = sizeof(struct sockaddr_in6);
3780 break;
3781 #endif
3782 default:
3783 return EAI_FAMILY;
3786 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) + socklen);
3787 if (ai == NULL) {
3788 return EAI_MEMORY;
3791 ai->ai_flags = 0;
3792 ai->ai_family = he->h_addrtype;
3793 ai->ai_socktype = hints->ai_socktype;
3794 ai->ai_protocol = hints->ai_protocol;
3796 ai->ai_addrlen = socklen;
3797 ai->ai_addr = (void *)(ai + 1);
3799 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
3800 ai->ai_addr->sa_len = socklen;
3801 #endif
3802 ai->ai_addr->sa_family = he->h_addrtype;
3804 switch (he->h_addrtype) {
3805 case AF_INET:
3807 struct sockaddr_in *sinp =
3808 (struct sockaddr_in *) ai->ai_addr;
3810 memset(sinp, 0, sizeof(struct sockaddr_in));
3812 sinp->sin_port = htons(port);
3813 sinp->sin_family = AF_INET;
3815 memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
3816 memcpy(&sinp->sin_addr, he->h_addr_list[0], he->h_length);
3819 break;
3820 #ifdef HAVE_IPV6
3821 case AF_INET6:
3823 struct sockaddr_in6 *sin6p =
3824 (struct sockaddr_in6 *) ai->ai_addr;
3826 memset(sin6p, 0, sizeof(struct sockaddr_in6));
3828 sin6p->sin6_port = htons(port);
3829 sin6p->sin6_family = AF_INET6;
3831 memcpy(&sin6p->sin6_addr, he->h_addr_list[0], he->h_length);
3833 break;
3834 #endif
3837 ai->ai_next = NULL;
3839 if (he->h_name) {
3840 ai->ai_canonname = strdup(he->h_name);
3841 if (ai->ai_canonname == NULL) {
3842 freeaddrinfo(ai);
3843 return EAI_MEMORY;
3847 *pai = ai;
3848 return 0;
3851 static int nwrap_getaddrinfo(const char *node,
3852 const char *service,
3853 const struct addrinfo *hints,
3854 struct addrinfo **res)
3856 struct addrinfo *ai = NULL;
3857 struct addrinfo *p = NULL;
3858 unsigned short port = 0;
3859 struct hostent *he;
3860 struct {
3861 int family;
3862 union {
3863 struct in_addr v4;
3864 #ifdef HAVE_IPV6
3865 struct in6_addr v6;
3866 } in;
3867 #endif
3868 } addr;
3869 int eai = EAI_SYSTEM;
3870 int ret;
3871 int rc;
3873 if (node == NULL && service == NULL) {
3874 return EAI_NONAME;
3877 ret = libc_getaddrinfo(node, service, hints, &p);
3878 if (ret == 0) {
3879 *res = p;
3882 /* If no node has been specified, let glibc deal with it */
3883 if (node == NULL) {
3884 return ret;
3887 if (hints == NULL) {
3888 hints = &default_hints;
3891 if ((hints->ai_flags & AI_CANONNAME) && node == NULL) {
3892 return EAI_BADFLAGS;
3895 if (service != NULL && service[0] != '\0') {
3896 if (isdigit((int)service[0])) {
3897 port = (unsigned short)atoi(service);
3898 } else {
3899 const char *proto = NULL;
3900 struct servent *s;
3902 if (hints->ai_protocol != 0) {
3903 struct protoent *pent;
3905 pent = getprotobynumber(hints->ai_protocol);
3906 if (pent != NULL) {
3907 proto = pent->p_name;
3911 s = getservbyname(service, proto);
3912 if (s != NULL) {
3913 port = ntohs(s->s_port);
3914 } else {
3915 if (p != NULL) {
3916 freeaddrinfo(p);
3918 return EAI_SERVICE;
3923 rc = 0;
3924 if (hints->ai_family == AF_UNSPEC || hints->ai_family == AF_INET) {
3925 rc = inet_pton(AF_INET, node, &addr.in.v4);
3927 if (rc == 1) {
3928 addr.family = AF_INET;
3929 #ifdef HAVE_IPV6
3930 } else {
3931 rc = inet_pton(AF_INET6, node, &addr.in.v6);
3932 if (rc == 1) {
3933 addr.family = AF_INET6;
3935 #endif
3938 if (addr.family == AF_INET) {
3939 he = nwrap_files_gethostbyaddr(&addr.in.v4,
3940 sizeof(struct in_addr),
3941 addr.family);
3942 if (he != NULL) {
3943 rc = nwrap_convert_he_ai(he, port, hints, &ai);
3944 } else {
3945 eai = EAI_NODATA;
3946 rc = -1;
3948 #ifdef HAVE_IPV6
3949 } else if (addr.family == AF_INET6) {
3950 he = nwrap_files_gethostbyaddr(&addr.in.v6,
3951 sizeof(struct in6_addr),
3952 addr.family);
3953 if (he != NULL) {
3954 rc = nwrap_convert_he_ai(he, port, hints, &ai);
3955 eai = rc;
3956 } else {
3957 eai = EAI_NODATA;
3958 rc = -1;
3960 #endif
3961 } else {
3962 he = nwrap_files_gethostbyname(node, hints->ai_family);
3963 if (he != NULL) {
3964 rc = nwrap_convert_he_ai(he, port, hints, &ai);
3965 eai = rc;
3966 } else {
3967 eai = EAI_NODATA;
3968 rc = -1;
3972 if (rc < 0) {
3973 return ret == 0 ? 0 : eai;
3976 if (ret == 0) {
3977 freeaddrinfo(p);
3980 if (ai->ai_flags == 0) {
3981 ai->ai_flags = hints->ai_flags;
3983 if (ai->ai_socktype == 0) {
3984 ai->ai_socktype = SOCK_DGRAM;
3986 if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_DGRAM) {
3987 ai->ai_protocol = 17; /* UDP */
3988 } else if (ai->ai_protocol == 0 && ai->ai_socktype == SOCK_STREAM) {
3989 ai->ai_protocol = 6; /* TCP */
3992 if (hints->ai_socktype == 0) {
3993 /* Add second ai */
3994 rc = nwrap_convert_he_ai(he, port, hints, &ai->ai_next);
3995 if (rc < 0) {
3996 freeaddrinfo(ai);
3997 return rc;
4000 if (ai->ai_next->ai_flags == 0) {
4001 ai->ai_next->ai_flags = hints->ai_flags;
4003 if (ai->ai_socktype == SOCK_DGRAM) {
4004 ai->ai_next->ai_socktype = SOCK_STREAM;
4005 } else if (ai->ai_socktype == SOCK_STREAM) {
4006 ai->ai_next->ai_socktype = SOCK_DGRAM;
4008 if (ai->ai_next->ai_socktype == SOCK_DGRAM) {
4009 ai->ai_next->ai_protocol = 17; /* UDP */
4010 } else if (ai->ai_next->ai_socktype == SOCK_STREAM) {
4011 ai->ai_next->ai_protocol = 6; /* TCP */
4015 *res = ai;
4017 return 0;
4020 int getaddrinfo(const char *node, const char *service,
4021 const struct addrinfo *hints,
4022 struct addrinfo **res)
4024 if (!nss_wrapper_hosts_enabled()) {
4025 return libc_getaddrinfo(node, service, hints, res);
4028 return nwrap_getaddrinfo(node, service, hints, res);
4031 static int nwrap_getnameinfo(const struct sockaddr *sa, socklen_t salen,
4032 char *host, size_t hostlen,
4033 char *serv, size_t servlen,
4034 int flags)
4036 struct hostent *he;
4037 struct servent *service;
4038 const char *proto;
4039 const void *addr;
4040 socklen_t addrlen;
4041 uint16_t port;
4042 sa_family_t type;
4044 if (sa == NULL || salen < sizeof(sa_family_t)) {
4045 return EAI_FAMILY;
4048 if ((flags & NI_NAMEREQD) && host == NULL && serv == NULL) {
4049 return EAI_NONAME;
4052 type = sa->sa_family;
4053 switch (type) {
4054 case AF_INET:
4055 if (salen < sizeof(struct sockaddr_in))
4056 return EAI_FAMILY;
4057 addr = &((const struct sockaddr_in *)sa)->sin_addr;
4058 addrlen = sizeof(((const struct sockaddr_in *)sa)->sin_addr);
4059 port = ntohs(((const struct sockaddr_in *)sa)->sin_port);
4060 break;
4061 #ifdef HAVE_IPV6
4062 case AF_INET6:
4063 if (salen < sizeof(struct sockaddr_in6))
4064 return EAI_FAMILY;
4065 addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
4066 addrlen = sizeof(((const struct sockaddr_in6 *)sa)->sin6_addr);
4067 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port);
4068 break;
4069 #endif
4070 default:
4071 return EAI_FAMILY;
4074 if (host != NULL) {
4075 he = NULL;
4076 if ((flags & NI_NUMERICHOST) == 0) {
4077 he = nwrap_files_gethostbyaddr(addr, addrlen, type);
4078 if ((flags & NI_NAMEREQD) && (he == NULL || he->h_name == NULL))
4079 return EAI_NONAME;
4081 if (he != NULL && he->h_name != NULL) {
4082 if (strlen(he->h_name) >= hostlen)
4083 return EAI_OVERFLOW;
4084 strcpy(host, he->h_name);
4085 if (flags & NI_NOFQDN)
4086 host[strcspn(host, ".")] = '\0';
4087 } else {
4088 if (inet_ntop(type, addr, host, hostlen) == NULL)
4089 return (errno == ENOSPC) ? EAI_OVERFLOW : EAI_FAIL;
4093 if (serv != NULL) {
4094 service = NULL;
4095 if ((flags & NI_NUMERICSERV) == 0) {
4096 proto = (flags & NI_DGRAM) ? "udp" : "tcp";
4097 service = getservbyport(htons(port), proto);
4099 if (service != NULL) {
4100 if (strlen(service->s_name) >= servlen)
4101 return EAI_OVERFLOW;
4102 strcpy(serv, service->s_name);
4103 } else {
4104 if (snprintf(serv, servlen, "%u", port) >= (int) servlen)
4105 return EAI_OVERFLOW;
4109 return 0;
4112 #ifdef HAVE_LINUX_GETNAMEINFO
4113 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
4114 char *host, socklen_t hostlen,
4115 char *serv, socklen_t servlen,
4116 int flags)
4117 #elif defined(HAVE_LINUX_GETNAMEINFO_UNSIGNED)
4118 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
4119 char *host, socklen_t hostlen,
4120 char *serv, socklen_t servlen,
4121 unsigned int flags)
4122 #else
4123 int getnameinfo(const struct sockaddr *sa, socklen_t salen,
4124 char *host, size_t hostlen,
4125 char *serv, size_t servlen,
4126 int flags)
4127 #endif
4129 if (!nss_wrapper_hosts_enabled()) {
4130 return libc_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
4133 return nwrap_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
4136 static int nwrap_gethostname(char *name, size_t len)
4138 const char *hostname = getenv("NSS_WRAPPER_HOSTNAME");
4140 if (strlen(hostname) >= len) {
4141 errno = ENAMETOOLONG;
4142 return -1;
4144 snprintf(name, len, "%s", hostname);
4146 return 0;
4149 #ifdef HAVE_SOLARIS_GETHOSTNAME
4150 int gethostname(char *name, int len)
4151 #else /* HAVE_SOLARIS_GETHOSTNAME */
4152 int gethostname(char *name, size_t len)
4153 #endif /* HAVE_SOLARIS_GETHOSTNAME */
4155 if (!nwrap_hostname_enabled()) {
4156 return libc_gethostname(name, len);
4159 return nwrap_gethostname(name, len);
4162 /****************************
4163 * DESTRUCTOR
4164 ***************************/
4167 * This function is called when the library is unloaded and makes sure that
4168 * sockets get closed and the unix file for the socket are unlinked.
4170 void nwrap_destructor(void)
4172 int i;
4174 if (nwrap_main_global != NULL) {
4175 struct nwrap_main *m = nwrap_main_global;
4177 /* libc */
4178 SAFE_FREE(m->libc->fns);
4179 if (m->libc->handle != NULL) {
4180 dlclose(m->libc->handle);
4182 if (m->libc->nsl_handle != NULL) {
4183 dlclose(m->libc->nsl_handle);
4185 if (m->libc->sock_handle != NULL) {
4186 dlclose(m->libc->sock_handle);
4188 SAFE_FREE(m->libc);
4190 /* backends */
4191 for (i = 0; i < m->num_backends; i++) {
4192 struct nwrap_backend *b = &(m->backends[i]);
4194 if (b->so_handle != NULL) {
4195 dlclose(b->so_handle);
4197 SAFE_FREE(b->fns);
4199 SAFE_FREE(m->backends);
4202 if (nwrap_pw_global.cache != NULL) {
4203 struct nwrap_cache *c = nwrap_pw_global.cache;
4205 nwrap_files_cache_unload(c);
4206 if (c->fd >= 0) {
4207 close(c->fd);
4210 SAFE_FREE(nwrap_pw_global.list);
4211 nwrap_pw_global.num = 0;
4214 if (nwrap_gr_global.cache != NULL) {
4215 struct nwrap_cache *c = nwrap_gr_global.cache;
4217 nwrap_files_cache_unload(c);
4218 if (c->fd >= 0) {
4219 close(c->fd);
4222 SAFE_FREE(nwrap_gr_global.list);
4223 nwrap_pw_global.num = 0;
4226 if (nwrap_he_global.cache != NULL) {
4227 struct nwrap_cache *c = nwrap_he_global.cache;
4229 nwrap_files_cache_unload(c);
4230 if (c->fd >= 0) {
4231 close(c->fd);
4234 SAFE_FREE(nwrap_he_global.list);
4235 nwrap_he_global.num = 0;