1 | /* Copyright (C) 1996-2016 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
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 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <assert.h> |
20 | #include <atomic.h> |
21 | #include <errno.h> |
22 | #include <stdbool.h> |
23 | #include "nsswitch.h" |
24 | #include "sysdep.h" |
25 | #ifdef USE_NSCD |
26 | # include <nscd/nscd_proto.h> |
27 | #endif |
28 | #ifdef NEED__RES_HCONF |
29 | # include <resolv/res_hconf.h> |
30 | #endif |
31 | #ifdef NEED__RES |
32 | # include <resolv.h> |
33 | #endif |
34 | /*******************************************************************\ |
35 | |* Here we assume several symbols to be defined: *| |
36 | |* *| |
37 | |* LOOKUP_TYPE - the return type of the function *| |
38 | |* *| |
39 | |* FUNCTION_NAME - name of the non-reentrant function *| |
40 | |* *| |
41 | |* DATABASE_NAME - name of the database the function accesses *| |
42 | |* (e.g., host, services, ...) *| |
43 | |* *| |
44 | |* ADD_PARAMS - additional parameters, can vary in number *| |
45 | |* *| |
46 | |* ADD_VARIABLES - names of additional parameters *| |
47 | |* *| |
48 | |* Optionally the following vars can be defined: *| |
49 | |* *| |
50 | |* EXTRA_PARAMS - optional parameters, can vary in number *| |
51 | |* *| |
52 | |* EXTRA_VARIABLES - names of optional parameter *| |
53 | |* *| |
54 | |* FUNCTION2_NAME - alternative name of the non-reentrant function *| |
55 | |* *| |
56 | |* NEED_H_ERRNO - an extra parameter will be passed to point to *| |
57 | |* the global `h_errno' variable. *| |
58 | |* *| |
59 | |* NEED__RES - the global _res variable might be used so we *| |
60 | |* will have to initialize it if necessary *| |
61 | |* *| |
62 | |* PREPROCESS - code run before anything else *| |
63 | |* *| |
64 | |* POSTPROCESS - code run after the lookup *| |
65 | |* *| |
66 | \*******************************************************************/ |
67 | |
68 | /* To make the real sources a bit prettier. */ |
69 | #define REENTRANT_NAME APPEND_R (FUNCTION_NAME) |
70 | #ifdef FUNCTION2_NAME |
71 | # define REENTRANT2_NAME APPEND_R (FUNCTION2_NAME) |
72 | #else |
73 | # define REENTRANT2_NAME NULL |
74 | #endif |
75 | #define APPEND_R(name) APPEND_R1 (name) |
76 | #define APPEND_R1(name) name##_r |
77 | #define INTERNAL(name) INTERNAL1 (name) |
78 | #define INTERNAL1(name) __##name |
79 | #define NEW(name) NEW1 (name) |
80 | #define NEW1(name) __new_##name |
81 | |
82 | #ifdef USE_NSCD |
83 | # define NSCD_NAME ADD_NSCD (REENTRANT_NAME) |
84 | # define ADD_NSCD(name) ADD_NSCD1 (name) |
85 | # define ADD_NSCD1(name) __nscd_##name |
86 | # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME) |
87 | # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name) |
88 | # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name |
89 | # define CONCAT2(arg1, arg2) CONCAT2_2 (arg1, arg2) |
90 | # define CONCAT2_2(arg1, arg2) arg1##arg2 |
91 | #endif |
92 | |
93 | #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME) |
94 | #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME) |
95 | #ifdef FUNCTION2_NAME |
96 | # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME) |
97 | #else |
98 | # define REENTRANT2_NAME_STRING NULL |
99 | #endif |
100 | #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME) |
101 | #define STRINGIZE(name) STRINGIZE1 (name) |
102 | #define STRINGIZE1(name) #name |
103 | |
104 | #ifndef DB_LOOKUP_FCT |
105 | # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2) |
106 | # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post) |
107 | # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post |
108 | #endif |
109 | |
110 | /* Sometimes we need to store error codes in the `h_errno' variable. */ |
111 | #ifdef NEED_H_ERRNO |
112 | # define H_ERRNO_PARM , int *h_errnop |
113 | # define H_ERRNO_VAR , h_errnop |
114 | # define H_ERRNO_VAR_P h_errnop |
115 | #else |
116 | # define H_ERRNO_PARM |
117 | # define H_ERRNO_VAR |
118 | # define H_ERRNO_VAR_P NULL |
119 | #endif |
120 | |
121 | #ifndef EXTRA_PARAMS |
122 | # define |
123 | #endif |
124 | #ifndef EXTRA_VARIABLES |
125 | # define |
126 | #endif |
127 | |
128 | #ifdef HAVE_AF |
129 | # define AF_VAL af |
130 | #else |
131 | # define AF_VAL AF_INET |
132 | #endif |
133 | |
134 | |
135 | /* Set defaults for merge functions that haven't been defined. */ |
136 | #ifndef DEEPCOPY_FN |
137 | static inline int |
138 | __copy_einval (LOOKUP_TYPE a, |
139 | const size_t b, |
140 | LOOKUP_TYPE *c, |
141 | char *d, |
142 | char **e) |
143 | { |
144 | return EINVAL; |
145 | } |
146 | # define DEEPCOPY_FN __copy_einval |
147 | #endif |
148 | |
149 | #ifndef MERGE_FN |
150 | static inline int |
151 | __merge_einval (LOOKUP_TYPE *a, |
152 | char *b, |
153 | char *c, |
154 | size_t d, |
155 | LOOKUP_TYPE *e, |
156 | char *f) |
157 | { |
158 | return EINVAL; |
159 | } |
160 | # define MERGE_FN __merge_einval |
161 | #endif |
162 | |
163 | #define CHECK_MERGE(err, status) \ |
164 | ({ \ |
165 | do \ |
166 | { \ |
167 | if (err) \ |
168 | { \ |
169 | __set_errno (err); \ |
170 | if (err == ERANGE) \ |
171 | status = NSS_STATUS_TRYAGAIN; \ |
172 | else \ |
173 | status = NSS_STATUS_UNAVAIL; \ |
174 | break; \ |
175 | } \ |
176 | } \ |
177 | while (0); \ |
178 | }) |
179 | |
180 | /* Type of the lookup function we need here. */ |
181 | typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *, |
182 | size_t, int * H_ERRNO_PARM |
183 | EXTRA_PARAMS); |
184 | |
185 | /* The lookup function for the first entry of this service. */ |
186 | extern int DB_LOOKUP_FCT (service_user **nip, const char *name, |
187 | const char *name2, void **fctp) |
188 | internal_function; |
189 | libc_hidden_proto (DB_LOOKUP_FCT) |
190 | |
191 | |
192 | int |
193 | INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer, |
194 | size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM |
195 | EXTRA_PARAMS) |
196 | { |
197 | static bool startp_initialized; |
198 | static service_user *startp; |
199 | static lookup_function start_fct; |
200 | service_user *nip; |
201 | int do_merge = 0; |
202 | LOOKUP_TYPE mergegrp; |
203 | char *mergebuf = NULL; |
204 | char *endptr = NULL; |
205 | union |
206 | { |
207 | lookup_function l; |
208 | void *ptr; |
209 | } fct; |
210 | int no_more, err; |
211 | enum nss_status status = NSS_STATUS_UNAVAIL; |
212 | #ifdef USE_NSCD |
213 | int nscd_status; |
214 | #endif |
215 | #ifdef NEED_H_ERRNO |
216 | bool any_service = false; |
217 | #endif |
218 | |
219 | #ifdef PREPROCESS |
220 | PREPROCESS; |
221 | #endif |
222 | |
223 | #ifdef HANDLE_DIGITS_DOTS |
224 | switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL, |
225 | buflen, result, &status, AF_VAL, |
226 | H_ERRNO_VAR_P)) |
227 | { |
228 | case -1: |
229 | return errno; |
230 | case 1: |
231 | #ifdef NEED_H_ERRNO |
232 | any_service = true; |
233 | #endif |
234 | goto done; |
235 | } |
236 | #endif |
237 | |
238 | #ifdef USE_NSCD |
239 | if (NOT_USENSCD_NAME > 0 && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY) |
240 | NOT_USENSCD_NAME = 0; |
241 | |
242 | if (!NOT_USENSCD_NAME |
243 | && !__nss_database_custom[CONCAT2 (NSS_DBSIDX_, DATABASE_NAME)]) |
244 | { |
245 | nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen, result |
246 | H_ERRNO_VAR); |
247 | if (nscd_status >= 0) |
248 | return nscd_status; |
249 | } |
250 | #endif |
251 | |
252 | if (! startp_initialized) |
253 | { |
254 | no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING, |
255 | REENTRANT2_NAME_STRING, &fct.ptr); |
256 | if (no_more) |
257 | { |
258 | void *tmp_ptr = (service_user *) -1l; |
259 | #ifdef PTR_MANGLE |
260 | PTR_MANGLE (tmp_ptr); |
261 | #endif |
262 | startp = tmp_ptr; |
263 | } |
264 | else |
265 | { |
266 | #ifdef NEED__RES |
267 | /* The resolver code will really be used so we have to |
268 | initialize it. */ |
269 | if (__res_maybe_init (&_res, 0) == -1) |
270 | { |
271 | *h_errnop = NETDB_INTERNAL; |
272 | *result = NULL; |
273 | return errno; |
274 | } |
275 | #endif /* need _res */ |
276 | #ifdef NEED__RES_HCONF |
277 | if (!_res_hconf.initialized) |
278 | _res_hconf_init (); |
279 | #endif /* need _res_hconf */ |
280 | |
281 | void *tmp_ptr = fct.l; |
282 | #ifdef PTR_MANGLE |
283 | PTR_MANGLE (tmp_ptr); |
284 | #endif |
285 | start_fct = tmp_ptr; |
286 | tmp_ptr = nip; |
287 | #ifdef PTR_MANGLE |
288 | PTR_MANGLE (tmp_ptr); |
289 | #endif |
290 | startp = tmp_ptr; |
291 | } |
292 | |
293 | /* Make sure start_fct and startp are written before |
294 | startp_initialized. */ |
295 | atomic_write_barrier (); |
296 | startp_initialized = true; |
297 | } |
298 | else |
299 | { |
300 | fct.l = start_fct; |
301 | nip = startp; |
302 | #ifdef PTR_DEMANGLE |
303 | PTR_DEMANGLE (fct.l); |
304 | PTR_DEMANGLE (nip); |
305 | #endif |
306 | no_more = nip == (service_user *) -1l; |
307 | } |
308 | |
309 | while (no_more == 0) |
310 | { |
311 | #ifdef NEED_H_ERRNO |
312 | any_service = true; |
313 | #endif |
314 | |
315 | status = DL_CALL_FCT (fct.l, (ADD_VARIABLES, resbuf, buffer, buflen, |
316 | &errno H_ERRNO_VAR EXTRA_VARIABLES)); |
317 | |
318 | /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the |
319 | provided buffer is too small. In this case we should give |
320 | the user the possibility to enlarge the buffer and we should |
321 | not simply go on with the next service (even if the TRYAGAIN |
322 | action tells us so). */ |
323 | if (status == NSS_STATUS_TRYAGAIN |
324 | #ifdef NEED_H_ERRNO |
325 | && *h_errnop == NETDB_INTERNAL |
326 | #endif |
327 | && errno == ERANGE) |
328 | break; |
329 | |
330 | if (do_merge) |
331 | { |
332 | |
333 | if (status == NSS_STATUS_SUCCESS) |
334 | { |
335 | /* The previous loop saved a buffer for merging. |
336 | Perform the merge now. */ |
337 | err = MERGE_FN (&mergegrp, mergebuf, endptr, buflen, resbuf, |
338 | buffer); |
339 | CHECK_MERGE (err,status); |
340 | do_merge = 0; |
341 | } |
342 | else |
343 | { |
344 | /* If the result wasn't SUCCESS, copy the saved buffer back |
345 | into the result buffer and set the status back to |
346 | NSS_STATUS_SUCCESS to match the previous pass through the |
347 | loop. |
348 | * If the next action is CONTINUE, it will overwrite the value |
349 | currently in the buffer and return the new value. |
350 | * If the next action is RETURN, we'll return the previously- |
351 | acquired values. |
352 | * If the next action is MERGE, then it will be added to the |
353 | buffer saved from the previous source. */ |
354 | err = DEEPCOPY_FN (mergegrp, buflen, resbuf, buffer, NULL); |
355 | CHECK_MERGE (err, status); |
356 | status = NSS_STATUS_SUCCESS; |
357 | } |
358 | } |
359 | |
360 | /* If we were are configured to merge this value with the next one, |
361 | save the current value of the group struct. */ |
362 | if (nss_next_action (nip, status) == NSS_ACTION_MERGE |
363 | && status == NSS_STATUS_SUCCESS) |
364 | { |
365 | /* Copy the current values into a buffer to be merged with the next |
366 | set of retrieved values. */ |
367 | if (mergebuf == NULL) |
368 | { |
369 | /* Only allocate once and reuse it for as many merges as we need |
370 | to perform. */ |
371 | mergebuf = malloc (buflen); |
372 | if (mergebuf == NULL) |
373 | { |
374 | __set_errno (ENOMEM); |
375 | status = NSS_STATUS_UNAVAIL; |
376 | break; |
377 | } |
378 | } |
379 | |
380 | err = DEEPCOPY_FN (*resbuf, buflen, &mergegrp, mergebuf, &endptr); |
381 | CHECK_MERGE (err, status); |
382 | do_merge = 1; |
383 | } |
384 | |
385 | no_more = __nss_next2 (&nip, REENTRANT_NAME_STRING, |
386 | REENTRANT2_NAME_STRING, &fct.ptr, status, 0); |
387 | } |
388 | free (mergebuf); |
389 | mergebuf = NULL; |
390 | |
391 | #ifdef HANDLE_DIGITS_DOTS |
392 | done: |
393 | #endif |
394 | *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL; |
395 | #ifdef NEED_H_ERRNO |
396 | if (status == NSS_STATUS_UNAVAIL && !any_service && errno != ENOENT) |
397 | /* This happens when we weren't able to use a service for reasons other |
398 | than the module not being found. In such a case, we'd want to tell the |
399 | caller that errno has the real reason for failure. */ |
400 | *h_errnop = NETDB_INTERNAL; |
401 | else if (status != NSS_STATUS_SUCCESS && !any_service) |
402 | /* We were not able to use any service. */ |
403 | *h_errnop = NO_RECOVERY; |
404 | #endif |
405 | #ifdef POSTPROCESS |
406 | POSTPROCESS; |
407 | #endif |
408 | |
409 | int res; |
410 | if (status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND) |
411 | res = 0; |
412 | /* Don't pass back ERANGE if this is not for a too-small buffer. */ |
413 | else if (errno == ERANGE && status != NSS_STATUS_TRYAGAIN) |
414 | res = EINVAL; |
415 | #ifdef NEED_H_ERRNO |
416 | /* These functions only set errno if h_errno is NETDB_INTERNAL. */ |
417 | else if (status == NSS_STATUS_TRYAGAIN && *h_errnop != NETDB_INTERNAL) |
418 | res = EAGAIN; |
419 | #endif |
420 | else |
421 | return errno; |
422 | |
423 | __set_errno (res); |
424 | return res; |
425 | } |
426 | |
427 | |
428 | #ifdef NO_COMPAT_NEEDED |
429 | strong_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME); |
430 | #elif !defined FUNCTION2_NAME |
431 | # include <shlib-compat.h> |
432 | # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2) |
433 | # define OLD(name) OLD1 (name) |
434 | # define OLD1(name) __old_##name |
435 | |
436 | int |
437 | attribute_compat_text_section |
438 | OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer, |
439 | size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM) |
440 | { |
441 | int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer, |
442 | buflen, result H_ERRNO_VAR); |
443 | |
444 | if (ret != 0 || result == NULL) |
445 | ret = -1; |
446 | |
447 | return ret; |
448 | } |
449 | |
450 | # define do_symbol_version(real, name, version) \ |
451 | compat_symbol (libc, real, name, version) |
452 | do_symbol_version (OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2_0); |
453 | # endif |
454 | |
455 | /* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias |
456 | in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not |
457 | hidden too. */ |
458 | strong_alias (INTERNAL (REENTRANT_NAME), NEW (REENTRANT_NAME)); |
459 | |
460 | # define do_default_symbol_version(real, name, version) \ |
461 | versioned_symbol (libc, real, name, version) |
462 | do_default_symbol_version (NEW (REENTRANT_NAME), |
463 | REENTRANT_NAME, GLIBC_2_1_2); |
464 | #endif |
465 | |
466 | nss_interface_function (REENTRANT_NAME) |
467 | |