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 * $FreeBSD: src/lib/libc/net/nss_compat.c,v 1.3 2004/03/30 15:56:15 nectar Exp $
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 _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 _pthread_once(&_term_once_##x, _term_create_##x); \
68 _pthread_setspecific(_term_key_##x, y); \
72 #define CHECK_TERMINATOR(x) \
73 (!__isthreaded || _pthread_main_np() ? \
75 (_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 *, void *, va_list);
83 int __nss_compat_getgrgid_r(void *, void *, va_list);
84 int __nss_compat_getgrent_r(void *, void *, va_list);
85 int __nss_compat_setgrent(void *, void *, va_list);
86 int __nss_compat_endgrent(void *, void *, va_list);
89 __nss_compat_getgrnam_r(void *retval
, void *mdata
, va_list ap
)
91 int (*fn
)(const char *, struct group
*, char *, size_t, int *);
97 enum nss_status status
;
100 name
= va_arg(ap
, const char *);
101 grp
= va_arg(ap
, struct group
*);
102 buffer
= va_arg(ap
, char *);
103 bufsize
= va_arg(ap
, size_t);
104 errnop
= va_arg(ap
, int *);
105 status
= fn(name
, grp
, buffer
, bufsize
, errnop
);
106 status
= __nss_compat_result(status
, *errnop
);
107 if (status
== NS_SUCCESS
)
108 *(struct group
**)retval
= grp
;
114 __nss_compat_getgrgid_r(void *retval
, void *mdata
, va_list ap
)
116 int (*fn
)(gid_t
, struct group
*, char *, size_t, int *);
122 enum nss_status status
;
125 gid
= va_arg(ap
, gid_t
);
126 grp
= va_arg(ap
, struct group
*);
127 buffer
= va_arg(ap
, char *);
128 bufsize
= va_arg(ap
, size_t);
129 errnop
= va_arg(ap
, int *);
130 status
= fn(gid
, grp
, buffer
, bufsize
, errnop
);
131 status
= __nss_compat_result(status
, *errnop
);
132 if (status
== NS_SUCCESS
)
133 *(struct group
**)retval
= grp
;
139 __nss_compat_getgrent_r(void *retval
, void *mdata
, va_list ap
)
141 int (*fn
)(struct group
*, char *, size_t, int *);
146 enum nss_status status
;
148 if (CHECK_TERMINATOR(group
))
149 return (NS_NOTFOUND
);
151 grp
= va_arg(ap
, struct group
*);
152 buffer
= va_arg(ap
, char *);
153 bufsize
= va_arg(ap
, size_t);
154 errnop
= va_arg(ap
, int *);
155 status
= fn(grp
, buffer
, bufsize
, errnop
);
156 status
= __nss_compat_result(status
, *errnop
);
157 if (status
== NS_SUCCESS
)
158 *(struct group
**)retval
= grp
;
159 else if (status
!= NS_RETURN
)
160 SET_TERMINATOR(group
, &terminator
);
166 __nss_compat_setgrent(void *retval __unused
, void *mdata
, va_list ap __unused
)
169 SET_TERMINATOR(group
, NULL
);
170 ((int (*)(void))mdata
)();
176 __nss_compat_endgrent(void *retval __unused
, void *mdata
, va_list ap __unused
)
179 SET_TERMINATOR(group
, NULL
);
180 ((int (*)(void))mdata
)();
186 DECLARE_TERMINATOR(passwd
);
188 int __nss_compat_getpwnam_r(void *, void *, va_list);
189 int __nss_compat_getpwuid_r(void *, void *, va_list);
190 int __nss_compat_getpwent_r(void *, void *, va_list);
191 int __nss_compat_setpwent(void *, void *, va_list);
192 int __nss_compat_endpwent(void *, void *, va_list);
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 __unused
, void *mdata
, va_list ap __unused
)
275 SET_TERMINATOR(passwd
, NULL
);
276 ((int (*)(void))mdata
)();
282 __nss_compat_endpwent(void *retval __unused
, void *mdata
, va_list ap __unused
)
285 SET_TERMINATOR(passwd
, NULL
);
286 ((int (*)(void))mdata
)();