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.
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 <http://www.gnu.org/licenses/>. */
26 # include <nscd/nscd_proto.h>
28 #ifdef NEED__RES_HCONF
29 # include <resolv/res_hconf.h>
34 /*******************************************************************\
35 |* Here we assume several symbols to be defined: *|
37 |* LOOKUP_TYPE - the return type of the function *|
39 |* FUNCTION_NAME - name of the non-reentrant function *|
41 |* DATABASE_NAME - name of the database the function accesses *|
42 |* (e.g., host, services, ...) *|
44 |* ADD_PARAMS - additional parameters, can vary in number *|
46 |* ADD_VARIABLES - names of additional parameters *|
48 |* Optionally the following vars can be defined: *|
50 |* EXTRA_PARAMS - optional parameters, can vary in number *|
52 |* EXTRA_VARIABLES - names of optional parameter *|
54 |* FUNCTION2_NAME - alternative name of the non-reentrant function *|
56 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
57 |* the global `h_errno' variable. *|
59 |* NEED__RES - the global _res variable might be used so we *|
60 |* will have to initialize it if necessary *|
62 |* PREPROCESS - code run before anything else *|
64 |* POSTPROCESS - code run after the lookup *|
66 \*******************************************************************/
68 /* To make the real sources a bit prettier. */
69 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
71 # define REENTRANT2_NAME APPEND_R (FUNCTION2_NAME)
73 # define REENTRANT2_NAME NULL
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
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
93 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
94 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
96 # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
98 # define REENTRANT2_NAME_STRING NULL
100 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
101 #define STRINGIZE(name) STRINGIZE1 (name)
102 #define STRINGIZE1(name) #name
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
110 /* Sometimes we need to store error codes in the `h_errno' variable. */
112 # define H_ERRNO_PARM , int *h_errnop
113 # define H_ERRNO_VAR , h_errnop
114 # define H_ERRNO_VAR_P h_errnop
116 # define H_ERRNO_PARM
118 # define H_ERRNO_VAR_P NULL
122 # define EXTRA_PARAMS
124 #ifndef EXTRA_VARIABLES
125 # define EXTRA_VARIABLES
131 # define AF_VAL AF_INET
135 /* Set defaults for merge functions that haven't been defined. */
138 __copy_einval (LOOKUP_TYPE a
,
146 # define DEEPCOPY_FN __copy_einval
151 __merge_einval (LOOKUP_TYPE
*a
,
160 # define MERGE_FN __merge_einval
163 #define CHECK_MERGE(err, status) \
171 status = NSS_STATUS_TRYAGAIN; \
173 status = NSS_STATUS_UNAVAIL; \
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
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
)
189 libc_hidden_proto (DB_LOOKUP_FCT
)
193 INTERNAL (REENTRANT_NAME
) (ADD_PARAMS
, LOOKUP_TYPE
*resbuf
, char *buffer
,
194 size_t buflen
, LOOKUP_TYPE
**result H_ERRNO_PARM
197 static bool startp_initialized
;
198 static service_user
*startp
;
199 static lookup_function start_fct
;
202 LOOKUP_TYPE mergegrp
;
203 char *mergebuf
= NULL
;
211 enum nss_status status
= NSS_STATUS_UNAVAIL
;
216 bool any_service
= false;
223 #ifdef HANDLE_DIGITS_DOTS
224 switch (__nss_hostname_digits_dots (name
, resbuf
, &buffer
, NULL
,
225 buflen
, result
, &status
, AF_VAL
,
239 if (NOT_USENSCD_NAME
> 0 && ++NOT_USENSCD_NAME
> NSS_NSCD_RETRY
)
240 NOT_USENSCD_NAME
= 0;
242 if (!NOT_USENSCD_NAME
243 && !__nss_database_custom
[CONCAT2 (NSS_DBSIDX_
, DATABASE_NAME
)])
245 nscd_status
= NSCD_NAME (ADD_VARIABLES
, resbuf
, buffer
, buflen
, result
247 if (nscd_status
>= 0)
252 if (! startp_initialized
)
254 no_more
= DB_LOOKUP_FCT (&nip
, REENTRANT_NAME_STRING
,
255 REENTRANT2_NAME_STRING
, &fct
.ptr
);
258 void *tmp_ptr
= (service_user
*) -1l;
260 PTR_MANGLE (tmp_ptr
);
267 /* The resolver code will really be used so we have to
269 if (__res_maybe_init (&_res
, 0) == -1)
271 *h_errnop
= NETDB_INTERNAL
;
275 #endif /* need _res */
276 #ifdef NEED__RES_HCONF
278 #endif /* need _res_hconf */
280 void *tmp_ptr
= fct
.l
;
282 PTR_MANGLE (tmp_ptr
);
287 PTR_MANGLE (tmp_ptr
);
292 /* Make sure start_fct and startp are written before
293 startp_initialized. */
294 atomic_write_barrier ();
295 startp_initialized
= true;
302 PTR_DEMANGLE (fct
.l
);
305 no_more
= nip
== (service_user
*) -1l;
314 status
= DL_CALL_FCT (fct
.l
, (ADD_VARIABLES
, resbuf
, buffer
, buflen
,
315 &errno H_ERRNO_VAR EXTRA_VARIABLES
));
317 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
318 provided buffer is too small. In this case we should give
319 the user the possibility to enlarge the buffer and we should
320 not simply go on with the next service (even if the TRYAGAIN
321 action tells us so). */
322 if (status
== NSS_STATUS_TRYAGAIN
324 && *h_errnop
== NETDB_INTERNAL
332 if (status
== NSS_STATUS_SUCCESS
)
334 /* The previous loop saved a buffer for merging.
335 Perform the merge now. */
336 err
= MERGE_FN (&mergegrp
, mergebuf
, endptr
, buflen
, resbuf
,
338 CHECK_MERGE (err
,status
);
343 /* If the result wasn't SUCCESS, copy the saved buffer back
344 into the result buffer and set the status back to
345 NSS_STATUS_SUCCESS to match the previous pass through the
347 * If the next action is CONTINUE, it will overwrite the value
348 currently in the buffer and return the new value.
349 * If the next action is RETURN, we'll return the previously-
351 * If the next action is MERGE, then it will be added to the
352 buffer saved from the previous source. */
353 err
= DEEPCOPY_FN (mergegrp
, buflen
, resbuf
, buffer
, NULL
);
354 CHECK_MERGE (err
, status
);
355 status
= NSS_STATUS_SUCCESS
;
359 /* If we were are configured to merge this value with the next one,
360 save the current value of the group struct. */
361 if (nss_next_action (nip
, status
) == NSS_ACTION_MERGE
362 && status
== NSS_STATUS_SUCCESS
)
364 /* Copy the current values into a buffer to be merged with the next
365 set of retrieved values. */
366 if (mergebuf
== NULL
)
368 /* Only allocate once and reuse it for as many merges as we need
370 mergebuf
= malloc (buflen
);
371 if (mergebuf
== NULL
)
373 __set_errno (ENOMEM
);
374 status
= NSS_STATUS_UNAVAIL
;
379 err
= DEEPCOPY_FN (*resbuf
, buflen
, &mergegrp
, mergebuf
, &endptr
);
380 CHECK_MERGE (err
, status
);
384 no_more
= __nss_next2 (&nip
, REENTRANT_NAME_STRING
,
385 REENTRANT2_NAME_STRING
, &fct
.ptr
, status
, 0);
390 #ifdef HANDLE_DIGITS_DOTS
393 *result
= status
== NSS_STATUS_SUCCESS
? resbuf
: NULL
;
395 if (status
== NSS_STATUS_UNAVAIL
&& !any_service
&& errno
!= ENOENT
)
396 /* This happens when we weren't able to use a service for reasons other
397 than the module not being found. In such a case, we'd want to tell the
398 caller that errno has the real reason for failure. */
399 *h_errnop
= NETDB_INTERNAL
;
400 else if (status
!= NSS_STATUS_SUCCESS
&& !any_service
)
401 /* We were not able to use any service. */
402 *h_errnop
= NO_RECOVERY
;
409 if (status
== NSS_STATUS_SUCCESS
|| status
== NSS_STATUS_NOTFOUND
)
411 /* Don't pass back ERANGE if this is not for a too-small buffer. */
412 else if (errno
== ERANGE
&& status
!= NSS_STATUS_TRYAGAIN
)
415 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
416 else if (status
== NSS_STATUS_TRYAGAIN
&& *h_errnop
!= NETDB_INTERNAL
)
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
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
)
440 int ret
= INTERNAL (REENTRANT_NAME
) (ADD_VARIABLES
, resbuf
, buffer
,
441 buflen
, result H_ERRNO_VAR
);
443 if (ret
!= 0 || result
== NULL
)
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
);
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
457 strong_alias (INTERNAL (REENTRANT_NAME
), NEW (REENTRANT_NAME
));
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
);
465 nss_interface_function (REENTRANT_NAME
)