]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/getXXbyYY_r.c
nss: Call __resolv_context_put before early return in get*_r [BZ #21932]
[thirdparty/glibc.git] / nss / getXXbyYY_r.c
CommitLineData
bfff8b1b 1/* Copyright (C) 1996-2017 Free Software Foundation, Inc.
2303f5fd
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5f0e6fc7 4
2303f5fd 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
5f0e6fc7 9
2303f5fd
UD
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
41bdb6e2 13 Lesser General Public License for more details.
5f0e6fc7 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
5f0e6fc7 18
344c67b6 19#include <assert.h>
44901495 20#include <atomic.h>
d71b808a 21#include <errno.h>
b2ab1f5d 22#include <stdbool.h>
5f0e6fc7 23#include "nsswitch.h"
58058fa2 24#include "sysdep.h"
67479a70
UD
25#ifdef USE_NSCD
26# include <nscd/nscd_proto.h>
27#endif
b43b13ac 28#ifdef NEED__RES
352f4ff9 29# include <resolv/resolv_context.h>
b43b13ac 30#endif
5f0e6fc7
RM
31/*******************************************************************\
32|* Here we assume several symbols to be defined: *|
fb776f3e 33|* *|
5f0e6fc7 34|* LOOKUP_TYPE - the return type of the function *|
fb776f3e 35|* *|
5f0e6fc7 36|* FUNCTION_NAME - name of the non-reentrant function *|
fb776f3e 37|* *|
5f0e6fc7
RM
38|* DATABASE_NAME - name of the database the function accesses *|
39|* (e.g., host, services, ...) *|
fb776f3e 40|* *|
384ca551 41|* ADD_PARAMS - additional parameters, can vary in number *|
fb776f3e 42|* *|
384ca551 43|* ADD_VARIABLES - names of additional parameters *|
fb776f3e 44|* *|
5f0e6fc7 45|* Optionally the following vars can be defined: *|
fb776f3e 46|* *|
384ca551
UD
47|* EXTRA_PARAMS - optional parameters, can vary in number *|
48|* *|
49|* EXTRA_VARIABLES - names of optional parameter *|
50|* *|
c5f80821 51|* FUNCTION2_NAME - alternative name of the non-reentrant function *|
384ca551 52|* *|
5f0e6fc7
RM
53|* NEED_H_ERRNO - an extra parameter will be passed to point to *|
54|* the global `h_errno' variable. *|
fb776f3e 55|* *|
352f4ff9 56|* NEED__RES - obtain a struct resolv_context resolver context *|
fb776f3e 57|* *|
ae81730f 58|* PREPROCESS - code run before anything else *|
fb776f3e 59|* *|
ae81730f 60|* POSTPROCESS - code run after the lookup *|
fb776f3e 61|* *|
5f0e6fc7
RM
62\*******************************************************************/
63
64/* To make the real sources a bit prettier. */
65#define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
384ca551
UD
66#ifdef FUNCTION2_NAME
67# define REENTRANT2_NAME APPEND_R (FUNCTION2_NAME)
68#else
69# define REENTRANT2_NAME NULL
70#endif
5f0e6fc7
RM
71#define APPEND_R(name) APPEND_R1 (name)
72#define APPEND_R1(name) name##_r
23396375
UD
73#define INTERNAL(name) INTERNAL1 (name)
74#define INTERNAL1(name) __##name
bff334e0
UD
75#define NEW(name) NEW1 (name)
76#define NEW1(name) __new_##name
5f0e6fc7 77
d67281a7
UD
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
ac16e905
UD
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
c3e2f19b
UD
85# define CONCAT2(arg1, arg2) CONCAT2_2 (arg1, arg2)
86# define CONCAT2_2(arg1, arg2) arg1##arg2
d67281a7
UD
87#endif
88
5f0e6fc7
RM
89#define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
90#define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
384ca551
UD
91#ifdef FUNCTION2_NAME
92# define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
93#else
94# define REENTRANT2_NAME_STRING NULL
95#endif
5f0e6fc7
RM
96#define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
97#define STRINGIZE(name) STRINGIZE1 (name)
98#define STRINGIZE1(name) #name
99
77fe0b9c 100#ifndef DB_LOOKUP_FCT
384ca551 101# define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2)
77fe0b9c
UD
102# define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
103# define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
104#endif
5f0e6fc7
RM
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
a5fdf99b 110# define H_ERRNO_VAR_P h_errnop
5f0e6fc7
RM
111#else
112# define H_ERRNO_PARM
113# define H_ERRNO_VAR
a5fdf99b 114# define H_ERRNO_VAR_P NULL
5f0e6fc7
RM
115#endif
116
384ca551
UD
117#ifndef EXTRA_PARAMS
118# define EXTRA_PARAMS
119#endif
120#ifndef EXTRA_VARIABLES
121# define EXTRA_VARIABLES
122#endif
123
a5fdf99b 124#ifdef HAVE_AF
1773d1ba 125# define AF_VAL af
a5fdf99b 126#else
1773d1ba 127# define AF_VAL AF_INET
a5fdf99b 128#endif
5f0e6fc7 129
ced8f893
SG
130
131/* Set defaults for merge functions that haven't been defined. */
132#ifndef DEEPCOPY_FN
133static 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
146static 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
5f0e6fc7 176/* Type of the lookup function we need here. */
899d423e 177typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
384ca551
UD
178 size_t, int * H_ERRNO_PARM
179 EXTRA_PARAMS);
5f0e6fc7 180
5f0e6fc7 181/* The lookup function for the first entry of this service. */
384ca551
UD
182extern int DB_LOOKUP_FCT (service_user **nip, const char *name,
183 const char *name2, void **fctp)
37ba7d66
UD
184 internal_function;
185libc_hidden_proto (DB_LOOKUP_FCT)
5f0e6fc7 186
5f0e6fc7 187
ba1ffaa1
UD
188int
189INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
384ca551
UD
190 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM
191 EXTRA_PARAMS)
5f0e6fc7 192{
58058fa2 193 static bool startp_initialized;
c4563d2d 194 static service_user *startp;
5f0e6fc7
RM
195 static lookup_function start_fct;
196 service_user *nip;
ced8f893
SG
197 int do_merge = 0;
198 LOOKUP_TYPE mergegrp;
199 char *mergebuf = NULL;
200 char *endptr = NULL;
fb776f3e
AJ
201 union
202 {
203 lookup_function l;
204 void *ptr;
205 } fct;
ced8f893 206 int no_more, err;
5f0e6fc7 207 enum nss_status status = NSS_STATUS_UNAVAIL;
d67281a7
UD
208#ifdef USE_NSCD
209 int nscd_status;
210#endif
b2ab1f5d
UD
211#ifdef NEED_H_ERRNO
212 bool any_service = false;
213#endif
5f0e6fc7 214
352f4ff9
FW
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
ae81730f
UD
227#ifdef PREPROCESS
228 PREPROCESS;
229#endif
230
61c162b5 231#ifdef HANDLE_DIGITS_DOTS
a5fdf99b 232 switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL,
1773d1ba 233 buflen, result, &status, AF_VAL,
a5fdf99b 234 H_ERRNO_VAR_P))
d17a729b 235 {
a5fdf99b 236 case -1:
30161498
FW
237# ifdef NEED__RES
238 __resolv_context_put (res_ctx);
239# endif
1670698f 240 return errno;
a5fdf99b 241 case 1:
d5dd6189
AS
242#ifdef NEED_H_ERRNO
243 any_service = true;
244#endif
a5fdf99b 245 goto done;
d17a729b 246 }
61c162b5
UD
247#endif
248
d67281a7 249#ifdef USE_NSCD
ef4d5b32 250 if (NOT_USENSCD_NAME > 0 && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
ac16e905 251 NOT_USENSCD_NAME = 0;
ea278354 252
c3e2f19b
UD
253 if (!NOT_USENSCD_NAME
254 && !__nss_database_custom[CONCAT2 (NSS_DBSIDX_, DATABASE_NAME)])
d67281a7 255 {
261eada2 256 nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen, result
ea278354 257 H_ERRNO_VAR);
1670698f 258 if (nscd_status >= 0)
30161498
FW
259 {
260# ifdef NEED__RES
261 __resolv_context_put (res_ctx);
262# endif
263 return nscd_status;
264 }
d67281a7
UD
265 }
266#endif
267
58058fa2 268 if (! startp_initialized)
5f0e6fc7 269 {
384ca551
UD
270 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING,
271 REENTRANT2_NAME_STRING, &fct.ptr);
5f0e6fc7 272 if (no_more)
58058fa2
UD
273 {
274 void *tmp_ptr = (service_user *) -1l;
40ce302d 275#ifdef PTR_MANGLE
58058fa2 276 PTR_MANGLE (tmp_ptr);
40ce302d 277#endif
58058fa2
UD
278 startp = tmp_ptr;
279 }
5f0e6fc7
RM
280 else
281 {
58058fa2 282 void *tmp_ptr = fct.l;
40ce302d 283#ifdef PTR_MANGLE
58058fa2 284 PTR_MANGLE (tmp_ptr);
40ce302d 285#endif
58058fa2
UD
286 start_fct = tmp_ptr;
287 tmp_ptr = nip;
40ce302d 288#ifdef PTR_MANGLE
58058fa2 289 PTR_MANGLE (tmp_ptr);
40ce302d 290#endif
58058fa2 291 startp = tmp_ptr;
5f0e6fc7 292 }
58058fa2
UD
293
294 /* Make sure start_fct and startp are written before
295 startp_initialized. */
296 atomic_write_barrier ();
297 startp_initialized = true;
5f0e6fc7
RM
298 }
299 else
300 {
fb776f3e 301 fct.l = start_fct;
58058fa2 302 nip = startp;
40ce302d
RM
303#ifdef PTR_DEMANGLE
304 PTR_DEMANGLE (fct.l);
58058fa2 305 PTR_DEMANGLE (nip);
40ce302d 306#endif
58058fa2 307 no_more = nip == (service_user *) -1l;
5f0e6fc7
RM
308 }
309
310 while (no_more == 0)
311 {
b2ab1f5d
UD
312#ifdef NEED_H_ERRNO
313 any_service = true;
314#endif
315
fb776f3e 316 status = DL_CALL_FCT (fct.l, (ADD_VARIABLES, resbuf, buffer, buflen,
384ca551 317 &errno H_ERRNO_VAR EXTRA_VARIABLES));
5f0e6fc7 318
ea278354 319 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
566efee2
UD
320 provided buffer is too small. In this case we should give
321 the user the possibility to enlarge the buffer and we should
322 not simply go on with the next service (even if the TRYAGAIN
323 action tells us so). */
324 if (status == NSS_STATUS_TRYAGAIN
325#ifdef NEED_H_ERRNO
326 && *h_errnop == NETDB_INTERNAL
327#endif
328 && errno == ERANGE)
329 break;
330
ced8f893
SG
331 if (do_merge)
332 {
333
334 if (status == NSS_STATUS_SUCCESS)
335 {
336 /* The previous loop saved a buffer for merging.
337 Perform the merge now. */
338 err = MERGE_FN (&mergegrp, mergebuf, endptr, buflen, resbuf,
339 buffer);
340 CHECK_MERGE (err,status);
341 do_merge = 0;
342 }
343 else
344 {
345 /* If the result wasn't SUCCESS, copy the saved buffer back
346 into the result buffer and set the status back to
347 NSS_STATUS_SUCCESS to match the previous pass through the
348 loop.
349 * If the next action is CONTINUE, it will overwrite the value
350 currently in the buffer and return the new value.
351 * If the next action is RETURN, we'll return the previously-
352 acquired values.
353 * If the next action is MERGE, then it will be added to the
354 buffer saved from the previous source. */
355 err = DEEPCOPY_FN (mergegrp, buflen, resbuf, buffer, NULL);
356 CHECK_MERGE (err, status);
357 status = NSS_STATUS_SUCCESS;
358 }
359 }
360
361 /* If we were are configured to merge this value with the next one,
362 save the current value of the group struct. */
363 if (nss_next_action (nip, status) == NSS_ACTION_MERGE
364 && status == NSS_STATUS_SUCCESS)
365 {
366 /* Copy the current values into a buffer to be merged with the next
367 set of retrieved values. */
368 if (mergebuf == NULL)
369 {
370 /* Only allocate once and reuse it for as many merges as we need
371 to perform. */
372 mergebuf = malloc (buflen);
373 if (mergebuf == NULL)
374 {
375 __set_errno (ENOMEM);
376 status = NSS_STATUS_UNAVAIL;
377 break;
378 }
379 }
380
381 err = DEEPCOPY_FN (*resbuf, buflen, &mergegrp, mergebuf, &endptr);
382 CHECK_MERGE (err, status);
383 do_merge = 1;
384 }
385
384ca551
UD
386 no_more = __nss_next2 (&nip, REENTRANT_NAME_STRING,
387 REENTRANT2_NAME_STRING, &fct.ptr, status, 0);
5f0e6fc7 388 }
ced8f893
SG
389 free (mergebuf);
390 mergebuf = NULL;
5f0e6fc7 391
61c162b5
UD
392#ifdef HANDLE_DIGITS_DOTS
393done:
394#endif
ba1ffaa1 395 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
b2ab1f5d 396#ifdef NEED_H_ERRNO
3d04f5db
SP
397 if (status == NSS_STATUS_UNAVAIL && !any_service && errno != ENOENT)
398 /* This happens when we weren't able to use a service for reasons other
399 than the module not being found. In such a case, we'd want to tell the
400 caller that errno has the real reason for failure. */
d5dd6189 401 *h_errnop = NETDB_INTERNAL;
cfde9b46 402 else if (status != NSS_STATUS_SUCCESS && !any_service)
b2ab1f5d
UD
403 /* We were not able to use any service. */
404 *h_errnop = NO_RECOVERY;
405#endif
5edb9387
UD
406#ifdef POSTPROCESS
407 POSTPROCESS;
408#endif
35504a6f 409
352f4ff9
FW
410#ifdef NEED__RES
411 /* This has to happen late because the POSTPROCESS stage above might
412 need the resolver context. */
413 __resolv_context_put (res_ctx);
414#endif /* NEED__RES */
415
a452e4e6 416 int res;
95479dc2 417 if (status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND)
a452e4e6 418 res = 0;
35504a6f
UD
419 /* Don't pass back ERANGE if this is not for a too-small buffer. */
420 else if (errno == ERANGE && status != NSS_STATUS_TRYAGAIN)
a452e4e6 421 res = EINVAL;
9bfce4bf 422#ifdef NEED_H_ERRNO
a452e4e6
UD
423 /* These functions only set errno if h_errno is NETDB_INTERNAL. */
424 else if (status == NSS_STATUS_TRYAGAIN && *h_errnop != NETDB_INTERNAL)
425 res = EAGAIN;
9bfce4bf 426#endif
35504a6f
UD
427 else
428 return errno;
429
a452e4e6
UD
430 __set_errno (res);
431 return res;
5f0e6fc7 432}
23396375 433
16710d58 434
deb84c43
UD
435#ifdef NO_COMPAT_NEEDED
436strong_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME);
437#elif !defined FUNCTION2_NAME
384ca551
UD
438# include <shlib-compat.h>
439# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
440# define OLD(name) OLD1 (name)
441# define OLD1(name) __old_##name
c2fa5b5a
UD
442
443int
4a381a81 444attribute_compat_text_section
c2fa5b5a
UD
445OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
446 size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
447{
448 int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer,
fb776f3e 449 buflen, result H_ERRNO_VAR);
c2fa5b5a 450
2cfe49db 451 if (ret != 0 || result == NULL)
c2fa5b5a
UD
452 ret = -1;
453
454 return ret;
455}
456
384ca551 457# define do_symbol_version(real, name, version) \
16710d58 458 compat_symbol (libc, real, name, version)
47963d10 459do_symbol_version (OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2_0);
384ca551 460# endif
c2fa5b5a 461
bff334e0
UD
462/* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias
463 in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not
464 hidden too. */
465strong_alias (INTERNAL (REENTRANT_NAME), NEW (REENTRANT_NAME));
466
384ca551 467# define do_default_symbol_version(real, name, version) \
16710d58 468 versioned_symbol (libc, real, name, version)
bff334e0 469do_default_symbol_version (NEW (REENTRANT_NAME),
16710d58 470 REENTRANT_NAME, GLIBC_2_1_2);
384ca551 471#endif
2f7f7bc6 472
01767843 473nss_interface_function (REENTRANT_NAME)