1 /* Copyright (C) 1996-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
25 # include <nscd/nscd_proto.h>
28 # include <resolv/resolv_context.h>
30 /*******************************************************************\
31 |* Here we assume several symbols to be defined: *|
33 |* LOOKUP_TYPE - the return type of the function *|
35 |* FUNCTION_NAME - name of the non-reentrant function *|
37 |* DATABASE_NAME - name of the database the function accesses *|
38 |* (e.g., host, services, ...) *|
40 |* ADD_PARAMS - additional parameters, can vary in number *|
42 |* ADD_VARIABLES - names of additional parameters *|
44 |* Optionally the following vars can be defined: *|
46 |* EXTRA_PARAMS - optional parameters, can vary in number *|
48 |* EXTRA_VARIABLES - names of optional parameter *|
50 |* FUNCTION2_NAME - alternative name of the non-reentrant function *|
52 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
53 |* the global `h_errno' variable. *|
55 |* NEED__RES - obtain a struct resolv_context resolver context *|
57 |* PREPROCESS - code run before anything else *|
59 |* POSTPROCESS - code run after the lookup *|
61 \*******************************************************************/
63 /* To make the real sources a bit prettier. */
64 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
66 # define REENTRANT2_NAME APPEND_R (FUNCTION2_NAME)
68 # define REENTRANT2_NAME NULL
70 #define APPEND_R(name) APPEND_R1 (name)
71 #define APPEND_R1(name) name##_r
72 #define INTERNAL(name) INTERNAL1 (name)
73 #define INTERNAL1(name) __##name
74 #define NEW(name) NEW1 (name)
75 #define NEW1(name) __new_##name
78 # define NSCD_NAME ADD_NSCD (REENTRANT_NAME)
79 # define ADD_NSCD(name) ADD_NSCD1 (name)
80 # define ADD_NSCD1(name) __nscd_##name
81 # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME)
82 # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name)
83 # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name
84 # define CONCAT2(arg1, arg2) CONCAT2_2 (arg1, arg2)
85 # define CONCAT2_2(arg1, arg2) arg1##arg2
88 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
89 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
91 # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
93 # define REENTRANT2_NAME_STRING NULL
95 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
96 #define STRINGIZE(name) STRINGIZE1 (name)
97 #define STRINGIZE1(name) #name
100 # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2)
101 # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
102 # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
105 /* Sometimes we need to store error codes in the `h_errno' variable. */
107 # define H_ERRNO_PARM , int *h_errnop
108 # define H_ERRNO_VAR , h_errnop
109 # define H_ERRNO_VAR_P h_errnop
111 # define H_ERRNO_PARM
113 # define H_ERRNO_VAR_P NULL
117 # define EXTRA_PARAMS
119 #ifndef EXTRA_VARIABLES
120 # define EXTRA_VARIABLES
126 # define AF_VAL AF_INET
130 /* Set defaults for merge functions that haven't been defined. */
133 __copy_einval (LOOKUP_TYPE a
,
141 # define DEEPCOPY_FN __copy_einval
146 __merge_einval (LOOKUP_TYPE
*a
,
155 # define MERGE_FN __merge_einval
158 #define CHECK_MERGE(err, status) \
166 status = NSS_STATUS_TRYAGAIN; \
168 status = NSS_STATUS_UNAVAIL; \
175 /* Type of the lookup function we need here. */
176 typedef enum nss_status (*lookup_function
) (ADD_PARAMS
, LOOKUP_TYPE
*, char *,
177 size_t, int * H_ERRNO_PARM
180 /* The lookup function for the first entry of this service. */
181 extern int DB_LOOKUP_FCT (nss_action_list
*nip
, const char *name
,
182 const char *name2
, void **fctp
);
183 libc_hidden_proto (DB_LOOKUP_FCT
)
187 INTERNAL (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
, char *buffer
,
188 size_t buflen
, LOOKUP_TYPE
**result H_ERRNO_PARM
193 LOOKUP_TYPE mergegrp
;
194 char *mergebuf
= NULL
;
202 enum nss_status status
= NSS_STATUS_UNAVAIL
;
207 bool any_service
= false;
211 /* The HANDLE_DIGITS_DOTS case below already needs the resolver
212 configuration, so this has to happen early. */
213 struct resolv_context
*res_ctx
= __resolv_context_get ();
216 *h_errnop
= NETDB_INTERNAL
;
220 #endif /* NEED__RES */
227 #ifdef HANDLE_DIGITS_DOTS
228 switch (__nss_hostname_digits_dots (name
, resbuf
, &buffer
, NULL
,
229 buflen
, result
, &status
, AF_VAL
,
234 __resolv_context_put (res_ctx
);
246 if (NOT_USENSCD_NAME
> 0 && ++NOT_USENSCD_NAME
> NSS_NSCD_RETRY
)
247 NOT_USENSCD_NAME
= 0;
249 if (!NOT_USENSCD_NAME
250 && !__nss_database_custom
[CONCAT2 (NSS_DBSIDX_
, DATABASE_NAME
)])
252 nscd_status
= NSCD_NAME (ADD_VARIABLES
, resbuf
, buffer
, buflen
, result
254 if (nscd_status
>= 0)
257 __resolv_context_put (res_ctx
);
264 no_more
= DB_LOOKUP_FCT (&nip
, REENTRANT_NAME_STRING
,
265 REENTRANT2_NAME_STRING
, &fct
.ptr
);
273 status
= DL_CALL_FCT (fct
.l
, (ADD_VARIABLES
, resbuf
, buffer
, buflen
,
274 &errno H_ERRNO_VAR EXTRA_VARIABLES
));
276 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
277 provided buffer is too small. In this case we should give
278 the user the possibility to enlarge the buffer and we should
279 not simply go on with the next service (even if the TRYAGAIN
280 action tells us so). */
281 if (status
== NSS_STATUS_TRYAGAIN
283 && *h_errnop
== NETDB_INTERNAL
291 if (status
== NSS_STATUS_SUCCESS
)
293 /* The previous loop saved a buffer for merging.
294 Perform the merge now. */
295 err
= MERGE_FN (&mergegrp
, mergebuf
, endptr
, buflen
, resbuf
,
297 CHECK_MERGE (err
,status
);
302 /* If the result wasn't SUCCESS, copy the saved buffer back
303 into the result buffer and set the status back to
304 NSS_STATUS_SUCCESS to match the previous pass through the
306 * If the next action is CONTINUE, it will overwrite the value
307 currently in the buffer and return the new value.
308 * If the next action is RETURN, we'll return the previously-
310 * If the next action is MERGE, then it will be added to the
311 buffer saved from the previous source. */
312 err
= DEEPCOPY_FN (mergegrp
, buflen
, resbuf
, buffer
, NULL
);
313 CHECK_MERGE (err
, status
);
314 status
= NSS_STATUS_SUCCESS
;
318 /* If we were are configured to merge this value with the next one,
319 save the current value of the group struct. */
320 if (nss_next_action (nip
, status
) == NSS_ACTION_MERGE
321 && status
== NSS_STATUS_SUCCESS
)
323 /* Copy the current values into a buffer to be merged with the next
324 set of retrieved values. */
325 if (mergebuf
== NULL
)
327 /* Only allocate once and reuse it for as many merges as we need
329 mergebuf
= malloc (buflen
);
330 if (mergebuf
== NULL
)
332 __set_errno (ENOMEM
);
333 status
= NSS_STATUS_UNAVAIL
;
338 err
= DEEPCOPY_FN (*resbuf
, buflen
, &mergegrp
, mergebuf
, &endptr
);
339 CHECK_MERGE (err
, status
);
343 no_more
= __nss_next2 (&nip
, REENTRANT_NAME_STRING
,
344 REENTRANT2_NAME_STRING
, &fct
.ptr
, status
, 0);
349 #ifdef HANDLE_DIGITS_DOTS
352 *result
= status
== NSS_STATUS_SUCCESS
? resbuf
: NULL
;
354 if (status
== NSS_STATUS_UNAVAIL
&& !any_service
&& errno
!= ENOENT
)
355 /* This happens when we weren't able to use a service for reasons other
356 than the module not being found. In such a case, we'd want to tell the
357 caller that errno has the real reason for failure. */
358 *h_errnop
= NETDB_INTERNAL
;
359 else if (status
!= NSS_STATUS_SUCCESS
&& !any_service
)
360 /* We were not able to use any service. */
361 *h_errnop
= NO_RECOVERY
;
368 /* This has to happen late because the POSTPROCESS stage above might
369 need the resolver context. */
370 __resolv_context_put (res_ctx
);
371 #endif /* NEED__RES */
374 if (status
== NSS_STATUS_SUCCESS
|| status
== NSS_STATUS_NOTFOUND
)
376 /* Don't pass back ERANGE if this is not for a too-small buffer. */
377 else if (errno
== ERANGE
&& status
!= NSS_STATUS_TRYAGAIN
)
380 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
381 else if (status
== NSS_STATUS_TRYAGAIN
&& *h_errnop
!= NETDB_INTERNAL
)
392 #ifdef NO_COMPAT_NEEDED
393 strong_alias (INTERNAL (REENTRANT_NAME
), REENTRANT_NAME
);
394 #elif !defined FUNCTION2_NAME
395 # include <shlib-compat.h>
396 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
397 # define OLD(name) OLD1 (name)
398 # define OLD1(name) __old_##name
401 attribute_compat_text_section
402 OLD (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
, char *buffer
,
403 size_t buflen
, LOOKUP_TYPE
**result H_ERRNO_PARM
)
405 int ret
= INTERNAL (REENTRANT_NAME
) (ADD_VARIABLES
, resbuf
, buffer
,
406 buflen
, result H_ERRNO_VAR
);
408 if (ret
!= 0 || result
== NULL
)
414 # define do_symbol_version(real, name, version) \
415 compat_symbol (libc, real, name, version)
416 do_symbol_version (OLD (REENTRANT_NAME
), REENTRANT_NAME
, GLIBC_2_0
);
419 /* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias
420 in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not
422 strong_alias (INTERNAL (REENTRANT_NAME
), NEW (REENTRANT_NAME
));
424 # define do_default_symbol_version(real, name, version) \
425 versioned_symbol (libc, real, name, version)
426 do_default_symbol_version (NEW (REENTRANT_NAME
),
427 REENTRANT_NAME
, GLIBC_2_1_2
);
430 nss_interface_function (REENTRANT_NAME
)