1 /* Copyright (C) 1996-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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.
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.
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 <https://www.gnu.org/licenses/>. */
26 # include <nscd/nscd_proto.h>
29 # include <resolv/resolv_context.h>
31 /*******************************************************************\
32 |* Here we assume several symbols to be defined: *|
34 |* LOOKUP_TYPE - the return type of the function *|
36 |* FUNCTION_NAME - name of the non-reentrant function *|
38 |* DATABASE_NAME - name of the database the function accesses *|
39 |* (e.g., host, services, ...) *|
41 |* ADD_PARAMS - additional parameters, can vary in number *|
43 |* ADD_VARIABLES - names of additional parameters *|
45 |* Optionally the following vars can be defined: *|
47 |* EXTRA_PARAMS - optional parameters, can vary in number *|
49 |* EXTRA_VARIABLES - names of optional parameter *|
51 |* FUNCTION2_NAME - alternative name of the non-reentrant function *|
53 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
54 |* the global `h_errno' variable. *|
56 |* NEED__RES - obtain a struct resolv_context resolver context *|
58 |* PREPROCESS - code run before anything else *|
60 |* POSTPROCESS - code run after the lookup *|
62 \*******************************************************************/
64 /* To make the real sources a bit prettier. */
65 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
67 # define REENTRANT2_NAME APPEND_R (FUNCTION2_NAME)
69 # define REENTRANT2_NAME NULL
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
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
89 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
90 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
92 # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
94 # define REENTRANT2_NAME_STRING NULL
96 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
97 #define STRINGIZE(name) STRINGIZE1 (name)
98 #define STRINGIZE1(name) #name
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
106 /* Sometimes we need to store error codes in the `h_errno' variable. */
108 # define H_ERRNO_PARM , int *h_errnop
109 # define H_ERRNO_VAR , h_errnop
110 # define H_ERRNO_VAR_P h_errnop
112 # define H_ERRNO_PARM
114 # define H_ERRNO_VAR_P NULL
118 # define EXTRA_PARAMS
120 #ifndef EXTRA_VARIABLES
121 # define EXTRA_VARIABLES
127 # define AF_VAL AF_INET
131 /* Set defaults for merge functions that haven't been defined. */
134 __copy_einval (LOOKUP_TYPE a
,
142 # define DEEPCOPY_FN __copy_einval
147 __merge_einval (LOOKUP_TYPE
*a
,
156 # define MERGE_FN __merge_einval
159 #define CHECK_MERGE(err, status) \
167 status = NSS_STATUS_TRYAGAIN; \
169 status = NSS_STATUS_UNAVAIL; \
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
181 /* The lookup function for the first entry of this service. */
182 extern int DB_LOOKUP_FCT (nss_action_list
*nip
, const char *name
,
183 const char *name2
, void **fctp
);
184 libc_hidden_proto (DB_LOOKUP_FCT
)
188 INTERNAL (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
, char *buffer
,
189 size_t buflen
, LOOKUP_TYPE
**result H_ERRNO_PARM
194 LOOKUP_TYPE mergegrp
;
195 char *mergebuf
= NULL
;
203 enum nss_status status
= NSS_STATUS_UNAVAIL
;
208 bool any_service
= false;
212 /* The HANDLE_DIGITS_DOTS case below already needs the resolver
213 configuration, so this has to happen early. */
214 struct resolv_context
*res_ctx
= __resolv_context_get ();
217 *h_errnop
= NETDB_INTERNAL
;
221 #endif /* NEED__RES */
228 #ifdef HANDLE_DIGITS_DOTS
229 switch (__nss_hostname_digits_dots (name
, resbuf
, &buffer
, NULL
,
230 buflen
, result
, &status
, AF_VAL
,
235 __resolv_context_put (res_ctx
);
247 if (NOT_USENSCD_NAME
> 0 && ++NOT_USENSCD_NAME
> NSS_NSCD_RETRY
)
248 NOT_USENSCD_NAME
= 0;
250 if (!NOT_USENSCD_NAME
251 && !__nss_database_custom
[CONCAT2 (NSS_DBSIDX_
, DATABASE_NAME
)])
253 nscd_status
= NSCD_NAME (ADD_VARIABLES
, resbuf
, buffer
, buflen
, result
255 if (nscd_status
>= 0)
258 __resolv_context_put (res_ctx
);
265 no_more
= DB_LOOKUP_FCT (&nip
, REENTRANT_NAME_STRING
,
266 REENTRANT2_NAME_STRING
, &fct
.ptr
);
274 status
= DL_CALL_FCT (fct
.l
, (ADD_VARIABLES
, resbuf
, buffer
, buflen
,
275 &errno H_ERRNO_VAR EXTRA_VARIABLES
));
277 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
278 provided buffer is too small. In this case we should give
279 the user the possibility to enlarge the buffer and we should
280 not simply go on with the next service (even if the TRYAGAIN
281 action tells us so). */
282 if (status
== NSS_STATUS_TRYAGAIN
284 && *h_errnop
== NETDB_INTERNAL
292 if (status
== NSS_STATUS_SUCCESS
)
294 /* The previous loop saved a buffer for merging.
295 Perform the merge now. */
296 err
= MERGE_FN (&mergegrp
, mergebuf
, endptr
, buflen
, resbuf
,
298 CHECK_MERGE (err
,status
);
303 /* If the result wasn't SUCCESS, copy the saved buffer back
304 into the result buffer and set the status back to
305 NSS_STATUS_SUCCESS to match the previous pass through the
307 * If the next action is CONTINUE, it will overwrite the value
308 currently in the buffer and return the new value.
309 * If the next action is RETURN, we'll return the previously-
311 * If the next action is MERGE, then it will be added to the
312 buffer saved from the previous source. */
313 err
= DEEPCOPY_FN (mergegrp
, buflen
, resbuf
, buffer
, NULL
);
314 CHECK_MERGE (err
, status
);
315 status
= NSS_STATUS_SUCCESS
;
319 /* If we were are configured to merge this value with the next one,
320 save the current value of the group struct. */
321 if (nss_next_action (nip
, status
) == NSS_ACTION_MERGE
322 && status
== NSS_STATUS_SUCCESS
)
324 /* Copy the current values into a buffer to be merged with the next
325 set of retrieved values. */
326 if (mergebuf
== NULL
)
328 /* Only allocate once and reuse it for as many merges as we need
330 mergebuf
= malloc (buflen
);
331 if (mergebuf
== NULL
)
333 __set_errno (ENOMEM
);
334 status
= NSS_STATUS_UNAVAIL
;
339 err
= DEEPCOPY_FN (*resbuf
, buflen
, &mergegrp
, mergebuf
, &endptr
);
340 CHECK_MERGE (err
, status
);
344 no_more
= __nss_next2 (&nip
, REENTRANT_NAME_STRING
,
345 REENTRANT2_NAME_STRING
, &fct
.ptr
, status
, 0);
350 #ifdef HANDLE_DIGITS_DOTS
353 *result
= status
== NSS_STATUS_SUCCESS
? resbuf
: NULL
;
355 if (status
== NSS_STATUS_UNAVAIL
&& !any_service
&& errno
!= ENOENT
)
356 /* This happens when we weren't able to use a service for reasons other
357 than the module not being found. In such a case, we'd want to tell the
358 caller that errno has the real reason for failure. */
359 *h_errnop
= NETDB_INTERNAL
;
360 else if (status
!= NSS_STATUS_SUCCESS
&& !any_service
)
361 /* We were not able to use any service. */
362 *h_errnop
= NO_RECOVERY
;
369 /* This has to happen late because the POSTPROCESS stage above might
370 need the resolver context. */
371 __resolv_context_put (res_ctx
);
372 #endif /* NEED__RES */
375 if (status
== NSS_STATUS_SUCCESS
|| status
== NSS_STATUS_NOTFOUND
)
377 /* Don't pass back ERANGE if this is not for a too-small buffer. */
378 else if (errno
== ERANGE
&& status
!= NSS_STATUS_TRYAGAIN
)
381 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
382 else if (status
== NSS_STATUS_TRYAGAIN
&& *h_errnop
!= NETDB_INTERNAL
)
393 #ifdef NO_COMPAT_NEEDED
394 strong_alias (INTERNAL (REENTRANT_NAME
), REENTRANT_NAME
);
395 #elif !defined FUNCTION2_NAME
396 # include <shlib-compat.h>
397 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
398 # define OLD(name) OLD1 (name)
399 # define OLD1(name) __old_##name
402 attribute_compat_text_section
403 OLD (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
, char *buffer
,
404 size_t buflen
, LOOKUP_TYPE
**result H_ERRNO_PARM
)
406 int ret
= INTERNAL (REENTRANT_NAME
) (ADD_VARIABLES
, resbuf
, buffer
,
407 buflen
, result H_ERRNO_VAR
);
409 if (ret
!= 0 || result
== NULL
)
415 # define do_symbol_version(real, name, version) \
416 compat_symbol (libc, real, name, version)
417 do_symbol_version (OLD (REENTRANT_NAME
), REENTRANT_NAME
, GLIBC_2_0
);
420 /* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias
421 in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not
423 strong_alias (INTERNAL (REENTRANT_NAME
), NEW (REENTRANT_NAME
));
425 # define do_default_symbol_version(real, name, version) \
426 versioned_symbol (libc, real, name, version)
427 do_default_symbol_version (NEW (REENTRANT_NAME
),
428 REENTRANT_NAME
, GLIBC_2_1_2
);
431 nss_interface_function (REENTRANT_NAME
)