]>
Commit | Line | Data |
---|---|---|
16710d58 | 1 | /* Copyright (C) 1996,97,98,99,2000 Free Software Foundation, Inc. |
2303f5fd UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. | |
5f0e6fc7 | 4 | |
2303f5fd UD |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
5f0e6fc7 | 9 | |
2303f5fd UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Library General Public License for more details. | |
5f0e6fc7 | 14 | |
2303f5fd UD |
15 | You should have received a copy of the GNU Library General Public |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
5f0e6fc7 | 19 | |
344c67b6 | 20 | #include <assert.h> |
d71b808a | 21 | #include <errno.h> |
5f0e6fc7 | 22 | #include "nsswitch.h" |
67479a70 UD |
23 | #ifdef USE_NSCD |
24 | # include <nscd/nscd_proto.h> | |
25 | #endif | |
5edb9387 UD |
26 | #ifdef NEED__RES_HCONF |
27 | # include <resolv/res_hconf.h> | |
28 | #endif | |
b43b13ac UD |
29 | #ifdef NEED__RES |
30 | # include <resolv.h> | |
31 | #endif | |
5f0e6fc7 RM |
32 | /*******************************************************************\ |
33 | |* Here we assume several symbols to be defined: *| | |
34 | |* *| | |
35 | |* LOOKUP_TYPE - the return type of the function *| | |
36 | |* *| | |
37 | |* FUNCTION_NAME - name of the non-reentrant function *| | |
38 | |* *| | |
39 | |* DATABASE_NAME - name of the database the function accesses *| | |
40 | |* (e.g., host, services, ...) *| | |
41 | |* *| | |
42 | |* ADD_PARAMS - additional parameter, can vary in number *| | |
43 | |* *| | |
44 | |* ADD_VARIABLES - names of additional parameter *| | |
45 | |* *| | |
46 | |* Optionally the following vars can be defined: *| | |
47 | |* *| | |
48 | |* NEED_H_ERRNO - an extra parameter will be passed to point to *| | |
49 | |* the global `h_errno' variable. *| | |
50 | |* *| | |
51 | |* NEED__RES - the global _res variable might be used so we *| | |
52 | |* will have to initialize it if necessary *| | |
53 | |* *| | |
ae81730f UD |
54 | |* PREPROCESS - code run before anything else *| |
55 | |* *| | |
56 | |* POSTPROCESS - code run after the lookup *| | |
57 | |* *| | |
5f0e6fc7 RM |
58 | \*******************************************************************/ |
59 | ||
60 | /* To make the real sources a bit prettier. */ | |
61 | #define REENTRANT_NAME APPEND_R (FUNCTION_NAME) | |
62 | #define APPEND_R(name) APPEND_R1 (name) | |
63 | #define APPEND_R1(name) name##_r | |
23396375 UD |
64 | #define INTERNAL(name) INTERNAL1 (name) |
65 | #define INTERNAL1(name) __##name | |
5f0e6fc7 | 66 | |
d67281a7 UD |
67 | #ifdef USE_NSCD |
68 | # define NSCD_NAME ADD_NSCD (REENTRANT_NAME) | |
69 | # define ADD_NSCD(name) ADD_NSCD1 (name) | |
70 | # define ADD_NSCD1(name) __nscd_##name | |
ac16e905 UD |
71 | # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME) |
72 | # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name) | |
73 | # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name | |
d67281a7 UD |
74 | #endif |
75 | ||
5f0e6fc7 RM |
76 | #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME) |
77 | #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME) | |
78 | #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME) | |
79 | #define STRINGIZE(name) STRINGIZE1 (name) | |
80 | #define STRINGIZE1(name) #name | |
81 | ||
82 | #define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup) | |
83 | #define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post) | |
84 | #define CONCAT3_2(Pre, Name, Post) Pre##Name##Post | |
85 | ||
86 | /* Sometimes we need to store error codes in the `h_errno' variable. */ | |
87 | #ifdef NEED_H_ERRNO | |
88 | # define H_ERRNO_PARM , int *h_errnop | |
89 | # define H_ERRNO_VAR , h_errnop | |
90 | #else | |
91 | # define H_ERRNO_PARM | |
92 | # define H_ERRNO_VAR | |
93 | #endif | |
94 | ||
95 | ||
96 | /* Type of the lookup function we need here. */ | |
899d423e UD |
97 | typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *, |
98 | size_t, int * H_ERRNO_PARM); | |
5f0e6fc7 | 99 | |
5f0e6fc7 RM |
100 | /* The lookup function for the first entry of this service. */ |
101 | extern int DB_LOOKUP_FCT (service_user **nip, const char *name, void **fctp); | |
102 | ||
ea278354 UD |
103 | /* Interval in which we transfer retry to contact the NSCD. */ |
104 | #define NSS_NSCD_RETRY 100 | |
5f0e6fc7 RM |
105 | |
106 | ||
ba1ffaa1 UD |
107 | int |
108 | INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer, | |
109 | size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM) | |
5f0e6fc7 | 110 | { |
c4563d2d | 111 | static service_user *startp; |
5f0e6fc7 RM |
112 | static lookup_function start_fct; |
113 | service_user *nip; | |
114 | lookup_function fct; | |
115 | int no_more; | |
116 | enum nss_status status = NSS_STATUS_UNAVAIL; | |
d67281a7 UD |
117 | #ifdef USE_NSCD |
118 | int nscd_status; | |
119 | #endif | |
5f0e6fc7 | 120 | |
ae81730f UD |
121 | #ifdef PREPROCESS |
122 | PREPROCESS; | |
123 | #endif | |
124 | ||
61c162b5 | 125 | #ifdef HANDLE_DIGITS_DOTS |
d17a729b UD |
126 | /* We have to test for the use of IPv6 which can only be done by |
127 | examining `_res'. */ | |
b43b13ac | 128 | if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1) |
d17a729b UD |
129 | { |
130 | *h_errnop = NETDB_INTERNAL; | |
131 | *result = NULL; | |
1670698f | 132 | return errno; |
d17a729b | 133 | } |
dfd2257a | 134 | # define resbuf (*resbuf) |
6044517f | 135 | # define result *result |
61c162b5 | 136 | # include "digits_dots.c" |
dfd2257a | 137 | # undef resbuf |
6044517f | 138 | # undef result |
61c162b5 UD |
139 | #endif |
140 | ||
d67281a7 | 141 | #ifdef USE_NSCD |
ac16e905 UD |
142 | if (NOT_USENSCD_NAME && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY) |
143 | NOT_USENSCD_NAME = 0; | |
ea278354 | 144 | |
ac16e905 | 145 | if (!NOT_USENSCD_NAME) |
d67281a7 | 146 | { |
ea278354 UD |
147 | nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen |
148 | H_ERRNO_VAR); | |
1670698f | 149 | if (nscd_status >= 0) |
ea278354 UD |
150 | { |
151 | *result = nscd_status == 0 ? resbuf : NULL; | |
152 | return nscd_status; | |
153 | } | |
d67281a7 UD |
154 | } |
155 | #endif | |
156 | ||
5f0e6fc7 RM |
157 | if (startp == NULL) |
158 | { | |
159 | no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING, (void **) &fct); | |
160 | if (no_more) | |
8a523922 | 161 | startp = (service_user *) -1l; |
5f0e6fc7 RM |
162 | else |
163 | { | |
164 | startp = nip; | |
165 | start_fct = fct; | |
166 | ||
167 | #ifdef NEED__RES | |
168 | /* The resolver code will really be used so we have to | |
169 | initialize it. */ | |
b43b13ac | 170 | if ((_res.options & RES_INIT) == 0 && __res_ninit (&_res) == -1) |
5f0e6fc7 | 171 | { |
adc6ff7f | 172 | *h_errnop = NETDB_INTERNAL; |
afd4eb37 | 173 | *result = NULL; |
1670698f | 174 | return errno; |
5f0e6fc7 RM |
175 | } |
176 | #endif /* need _res */ | |
5edb9387 UD |
177 | #ifdef NEED__RES_HCONF |
178 | if (!_res_hconf.initialized) | |
179 | _res_hconf_init (); | |
180 | #endif /* need _res_hconf */ | |
5f0e6fc7 RM |
181 | } |
182 | } | |
183 | else | |
184 | { | |
185 | fct = start_fct; | |
8a523922 | 186 | no_more = (nip = startp) == (service_user *) -1l; |
5f0e6fc7 RM |
187 | } |
188 | ||
189 | while (no_more == 0) | |
190 | { | |
94e365c6 | 191 | status = DL_CALL_FCT (fct, (ADD_VARIABLES, resbuf, buffer, buflen, |
5ad49c07 | 192 | &errno H_ERRNO_VAR)); |
5f0e6fc7 | 193 | |
ea278354 | 194 | /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the |
566efee2 UD |
195 | provided buffer is too small. In this case we should give |
196 | the user the possibility to enlarge the buffer and we should | |
197 | not simply go on with the next service (even if the TRYAGAIN | |
198 | action tells us so). */ | |
199 | if (status == NSS_STATUS_TRYAGAIN | |
200 | #ifdef NEED_H_ERRNO | |
201 | && *h_errnop == NETDB_INTERNAL | |
202 | #endif | |
203 | && errno == ERANGE) | |
204 | break; | |
205 | ||
503054c0 RM |
206 | no_more = __nss_next (&nip, REENTRANT_NAME_STRING, |
207 | (void **) &fct, status, 0); | |
5f0e6fc7 RM |
208 | } |
209 | ||
61c162b5 UD |
210 | #ifdef HANDLE_DIGITS_DOTS |
211 | done: | |
212 | #endif | |
ba1ffaa1 | 213 | *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL; |
5edb9387 UD |
214 | #ifdef POSTPROCESS |
215 | POSTPROCESS; | |
216 | #endif | |
1670698f | 217 | return status == NSS_STATUS_SUCCESS ? 0 : errno; |
5f0e6fc7 | 218 | } |
23396375 | 219 | |
16710d58 RM |
220 | |
221 | #include <shlib-compat.h> | |
222 | #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2) | |
c2fa5b5a UD |
223 | #define OLD(name) OLD1 (name) |
224 | #define OLD1(name) __old_##name | |
225 | ||
226 | int | |
227 | OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer, | |
228 | size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM) | |
229 | { | |
230 | int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer, | |
231 | buflen, result H_ERRNO_VAR); | |
232 | ||
233 | if (ret != 0) | |
234 | ret = -1; | |
235 | ||
236 | return ret; | |
237 | } | |
238 | ||
239 | #define do_symbol_version(real, name, version) \ | |
16710d58 | 240 | compat_symbol (libc, real, name, version) |
47963d10 | 241 | do_symbol_version (OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2_0); |
c2fa5b5a UD |
242 | |
243 | #define do_default_symbol_version(real, name, version) \ | |
16710d58 RM |
244 | versioned_symbol (libc, real, name, version) |
245 | do_default_symbol_version (INTERNAL (REENTRANT_NAME), | |
246 | REENTRANT_NAME, GLIBC_2_1_2); | |
c2fa5b5a | 247 | #else |
2293395f | 248 | #define do_weak_alias(n1, n2) weak_alias (n1, n2) |
23396375 | 249 | do_weak_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME) |
c2fa5b5a | 250 | #endif |