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 EXTRA_PARAMS
123#endif
124#ifndef EXTRA_VARIABLES
125# define EXTRA_VARIABLES
126#endif
127
128#ifdef HAVE_AF
129# define AF_VAL af
130#else
131# define AF_VAL AF_INET
132#endif
133
134/* Type of the lookup function we need here. */
135typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
136 size_t, int * H_ERRNO_PARM
137 EXTRA_PARAMS);
138
139/* The lookup function for the first entry of this service. */
140extern int DB_LOOKUP_FCT (service_user **nip, const char *name,
141 const char *name2, void **fctp)
142 internal_function;
143libc_hidden_proto (DB_LOOKUP_FCT)
144
145
146int
147INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
148 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM
149 EXTRA_PARAMS)
150{
151 static bool startp_initialized;
152 static service_user *startp;
153 static lookup_function start_fct;
154 service_user *nip;
155 union
156 {
157 lookup_function l;
158 void *ptr;
159 } fct;
160
161 int no_more;
162 enum nss_status status = NSS_STATUS_UNAVAIL;
163#ifdef USE_NSCD
164 int nscd_status;
165#endif
166#ifdef NEED_H_ERRNO
167 bool any_service = false;
168#endif
169
170#ifdef PREPROCESS
171 PREPROCESS;
172#endif
173
174#ifdef HANDLE_DIGITS_DOTS
175 switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL,
176 buflen, result, &status, AF_VAL,
177 H_ERRNO_VAR_P))
178 {
179 case -1:
180 return errno;
181 case 1:
182#ifdef NEED_H_ERRNO
183 any_service = true;
184#endif
185 goto done;
186 }
187#endif
188
189#ifdef USE_NSCD
190 if (NOT_USENSCD_NAME > 0 && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
191 NOT_USENSCD_NAME = 0;
192
193 if (!NOT_USENSCD_NAME
194 && !__nss_database_custom[CONCAT2 (NSS_DBSIDX_, DATABASE_NAME)])
195 {
196 nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen, result
197 H_ERRNO_VAR);
198 if (nscd_status >= 0)
199 return nscd_status;
200 }
201#endif
202
203 if (! startp_initialized)
204 {
205 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING,
206 REENTRANT2_NAME_STRING, &fct.ptr);
207 if (no_more)
208 {
209 void *tmp_ptr = (service_user *) -1l;
210#ifdef PTR_MANGLE
211 PTR_MANGLE (tmp_ptr);
212#endif
213 startp = tmp_ptr;
214 }
215 else
216 {
217#ifdef NEED__RES
218 /* The resolver code will really be used so we have to
219 initialize it. */
220 if (__res_maybe_init (&_res, 0) == -1)
221 {
222 *h_errnop = NETDB_INTERNAL;
223 *result = NULL;
224 return errno;
225 }
226#endif /* need _res */
227#ifdef NEED__RES_HCONF
228 if (!_res_hconf.initialized)
229 _res_hconf_init ();
230#endif /* need _res_hconf */
231
232 void *tmp_ptr = fct.l;
233#ifdef PTR_MANGLE
234 PTR_MANGLE (tmp_ptr);
235#endif
236 start_fct = tmp_ptr;
237 tmp_ptr = nip;
238#ifdef PTR_MANGLE
239 PTR_MANGLE (tmp_ptr);
240#endif
241 startp = tmp_ptr;
242 }
243
244 /* Make sure start_fct and startp are written before
245 startp_initialized. */
246 atomic_write_barrier ();
247 startp_initialized = true;
248 }
249 else
250 {
251 fct.l = start_fct;
252 nip = startp;
253#ifdef PTR_DEMANGLE
254 PTR_DEMANGLE (fct.l);
255 PTR_DEMANGLE (nip);
256#endif
257 no_more = nip == (service_user *) -1l;
258 }
259
260 while (no_more == 0)
261 {
262#ifdef NEED_H_ERRNO
263 any_service = true;
264#endif
265
266 status = DL_CALL_FCT (fct.l, (ADD_VARIABLES, resbuf, buffer, buflen,
267 &errno H_ERRNO_VAR EXTRA_VARIABLES));
268
269 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
270 provided buffer is too small. In this case we should give
271 the user the possibility to enlarge the buffer and we should
272 not simply go on with the next service (even if the TRYAGAIN
273 action tells us so). */
274 if (status == NSS_STATUS_TRYAGAIN
275#ifdef NEED_H_ERRNO
276 && *h_errnop == NETDB_INTERNAL
277#endif
278 && errno == ERANGE)
279 break;
280
281 no_more = __nss_next2 (&nip, REENTRANT_NAME_STRING,
282 REENTRANT2_NAME_STRING, &fct.ptr, status, 0);
283 }
284
285#ifdef HANDLE_DIGITS_DOTS
286done:
287#endif
288 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
289#ifdef NEED_H_ERRNO
290 if (status == NSS_STATUS_UNAVAIL && !any_service && errno != ENOENT)
291 /* This happens when we weren't able to use a service for reasons other
292 than the module not being found. In such a case, we'd want to tell the
293 caller that errno has the real reason for failure. */
294 *h_errnop = NETDB_INTERNAL;
295 else if (status != NSS_STATUS_SUCCESS && !any_service)
296 /* We were not able to use any service. */
297 *h_errnop = NO_RECOVERY;
298#endif
299#ifdef POSTPROCESS
300 POSTPROCESS;
301#endif
302
303 int res;
304 if (status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND)
305 res = 0;
306 /* Don't pass back ERANGE if this is not for a too-small buffer. */
307 else if (errno == ERANGE && status != NSS_STATUS_TRYAGAIN)
308 res = EINVAL;
309#ifdef NEED_H_ERRNO
310 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
311 else if (status == NSS_STATUS_TRYAGAIN && *h_errnop != NETDB_INTERNAL)
312 res = EAGAIN;
313#endif
314 else
315 return errno;
316
317 __set_errno (res);
318 return res;
319}
320
321
322#ifdef NO_COMPAT_NEEDED
323strong_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME);
324#elif !defined FUNCTION2_NAME
325# include <shlib-compat.h>
326# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
327# define OLD(name) OLD1 (name)
328# define OLD1(name) __old_##name
329
330int
331attribute_compat_text_section
332OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
333 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
334{
335 int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer,
336 buflen, result H_ERRNO_VAR);
337
338 if (ret != 0 || result == NULL)
339 ret = -1;
340
341 return ret;
342}
343
344# define do_symbol_version(real, name, version) \
345 compat_symbol (libc, real, name, version)
346do_symbol_version (OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2_0);
347# endif
348
349/* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias
350 in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not
351 hidden too. */
352strong_alias (INTERNAL (REENTRANT_NAME), NEW (REENTRANT_NAME));
353
354# define do_default_symbol_version(real, name, version) \
355 versioned_symbol (libc, real, name, version)
356do_default_symbol_version (NEW (REENTRANT_NAME),
357 REENTRANT_NAME, GLIBC_2_1_2);
358#endif
359
360nss_interface_function (REENTRANT_NAME)
361