2 * Copyright (c) 2003 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by
6 * Jacques A. Vidrine, Safeport Network Services, and Network
7 * Associates Laboratories, the Security Research Division of Network
8 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
9 * ("CBOSS"), as part of the DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Compatibility shims for the GNU C Library-style nsswitch interface.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "namespace.h"
38 #include <sys/param.h>
42 #include <pthread_np.h>
43 #include "un-namespace.h"
44 #include "libc_private.h"
50 static int terminator
;
52 #define DECLARE_TERMINATOR(x) \
53 static pthread_key_t _term_key_##x; \
55 _term_create_##x(void) \
57 (void)_pthread_key_create(&_term_key_##x, NULL); \
59 static void *_term_main_##x; \
60 static pthread_once_t _term_once_##x = PTHREAD_ONCE_INIT
62 #define SET_TERMINATOR(x, y) \
64 if (!__isthreaded || _pthread_main_np()) \
65 _term_main_##x = (y); \
67 (void)_pthread_once(&_term_once_##x, _term_create_##x); \
68 (void)_pthread_setspecific(_term_key_##x, y); \
72 #define CHECK_TERMINATOR(x) \
73 (!__isthreaded || _pthread_main_np() ? \
75 ((void)_pthread_once(&_term_once_##x, _term_create_##x), \
76 _pthread_getspecific(_term_key_##x)))
80 DECLARE_TERMINATOR(group
);
82 int __nss_compat_getgrnam_r(void *retval
, void *mdata
, va_list ap
);
83 int __nss_compat_getgrgid_r(void *retval
, void *mdata
, va_list ap
);
84 int __nss_compat_getgrent_r(void *retval
, void *mdata
, va_list ap
);
85 int __nss_compat_setgrent(void *retval
, void *mdata
, va_list ap
);
86 int __nss_compat_endgrent(void *retval
, void *mdata
, va_list ap
);
87 int __nss_compat_getpwnam_r(void *retval
, void *mdata
, va_list ap
);
88 int __nss_compat_getpwuid_r(void *retval
, void *mdata
, va_list ap
);
89 int __nss_compat_getpwent_r(void *retval
, void *mdata
, va_list ap
);
90 int __nss_compat_setpwent(void *retval
, void *mdata
, va_list ap
);
91 int __nss_compat_endpwent(void *retval
, void *mdata
, va_list ap
);
94 __nss_compat_getgrnam_r(void *retval
, void *mdata
, va_list ap
)
96 int (*fn
)(const char *, struct group
*, char *, size_t, int *);
102 enum nss_status status
;
105 name
= va_arg(ap
, const char *);
106 grp
= va_arg(ap
, struct group
*);
107 buffer
= va_arg(ap
, char *);
108 bufsize
= va_arg(ap
, size_t);
109 errnop
= va_arg(ap
, int *);
110 status
= fn(name
, grp
, buffer
, bufsize
, errnop
);
111 status
= __nss_compat_result(status
, *errnop
);
112 if (status
== NS_SUCCESS
)
113 *(struct group
**)retval
= grp
;
119 __nss_compat_getgrgid_r(void *retval
, void *mdata
, va_list ap
)
121 int (*fn
)(gid_t
, struct group
*, char *, size_t, int *);
127 enum nss_status status
;
130 gid
= va_arg(ap
, gid_t
);
131 grp
= va_arg(ap
, struct group
*);
132 buffer
= va_arg(ap
, char *);
133 bufsize
= va_arg(ap
, size_t);
134 errnop
= va_arg(ap
, int *);
135 status
= fn(gid
, grp
, buffer
, bufsize
, errnop
);
136 status
= __nss_compat_result(status
, *errnop
);
137 if (status
== NS_SUCCESS
)
138 *(struct group
**)retval
= grp
;
144 __nss_compat_getgrent_r(void *retval
, void *mdata
, va_list ap
)
146 int (*fn
)(struct group
*, char *, size_t, int *);
151 enum nss_status status
;
153 if (CHECK_TERMINATOR(group
))
154 return (NS_NOTFOUND
);
156 grp
= va_arg(ap
, struct group
*);
157 buffer
= va_arg(ap
, char *);
158 bufsize
= va_arg(ap
, size_t);
159 errnop
= va_arg(ap
, int *);
160 status
= fn(grp
, buffer
, bufsize
, errnop
);
161 status
= __nss_compat_result(status
, *errnop
);
162 if (status
== NS_SUCCESS
)
163 *(struct group
**)retval
= grp
;
164 else if (status
!= NS_RETURN
)
165 SET_TERMINATOR(group
, &terminator
);
171 __nss_compat_setgrent(void *retval
, void *mdata
, va_list ap
)
174 SET_TERMINATOR(group
, NULL
);
175 ((int (*)(void))mdata
)();
181 __nss_compat_endgrent(void *retval
, void *mdata
, va_list ap
)
184 SET_TERMINATOR(group
, NULL
);
185 ((int (*)(void))mdata
)();
191 DECLARE_TERMINATOR(passwd
);
195 __nss_compat_getpwnam_r(void *retval
, void *mdata
, va_list ap
)
197 int (*fn
)(const char *, struct passwd
*, char *, size_t, int *);
203 enum nss_status status
;
206 name
= va_arg(ap
, const char *);
207 pwd
= va_arg(ap
, struct passwd
*);
208 buffer
= va_arg(ap
, char *);
209 bufsize
= va_arg(ap
, size_t);
210 errnop
= va_arg(ap
, int *);
211 status
= fn(name
, pwd
, buffer
, bufsize
, errnop
);
212 status
= __nss_compat_result(status
, *errnop
);
213 if (status
== NS_SUCCESS
)
214 *(struct passwd
**)retval
= pwd
;
220 __nss_compat_getpwuid_r(void *retval
, void *mdata
, va_list ap
)
222 int (*fn
)(uid_t
, struct passwd
*, char *, size_t, int *);
228 enum nss_status status
;
231 uid
= va_arg(ap
, uid_t
);
232 pwd
= va_arg(ap
, struct passwd
*);
233 buffer
= va_arg(ap
, char *);
234 bufsize
= va_arg(ap
, size_t);
235 errnop
= va_arg(ap
, int *);
236 status
= fn(uid
, pwd
, buffer
, bufsize
, errnop
);
237 status
= __nss_compat_result(status
, *errnop
);
238 if (status
== NS_SUCCESS
)
239 *(struct passwd
**)retval
= pwd
;
245 __nss_compat_getpwent_r(void *retval
, void *mdata
, va_list ap
)
247 int (*fn
)(struct passwd
*, char *, size_t, int *);
252 enum nss_status status
;
254 if (CHECK_TERMINATOR(passwd
))
255 return (NS_NOTFOUND
);
257 pwd
= va_arg(ap
, struct passwd
*);
258 buffer
= va_arg(ap
, char *);
259 bufsize
= va_arg(ap
, size_t);
260 errnop
= va_arg(ap
, int *);
261 status
= fn(pwd
, buffer
, bufsize
, errnop
);
262 status
= __nss_compat_result(status
, *errnop
);
263 if (status
== NS_SUCCESS
)
264 *(struct passwd
**)retval
= pwd
;
265 else if (status
!= NS_RETURN
)
266 SET_TERMINATOR(passwd
, &terminator
);
272 __nss_compat_setpwent(void *retval
, void *mdata
, va_list ap
)
275 SET_TERMINATOR(passwd
, NULL
);
276 ((int (*)(void))mdata
)();
282 __nss_compat_endpwent(void *retval
, void *mdata
, va_list ap
)
285 SET_TERMINATOR(passwd
, NULL
);
286 ((int (*)(void))mdata
)();