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