]> git.ipfire.org Git - thirdparty/glibc.git/blame - nscd/nscd_getai.c
nscd: Use time_t for return type of addgetnetgrentX
[thirdparty/glibc.git] / nscd / nscd_getai.c
CommitLineData
dff8da6b 1/* Copyright (C) 2004-2024 Free Software Foundation, Inc.
d19687d6 2 This file is part of the GNU C Library.
d19687d6
UD
3
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.
8
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.
13
14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
d19687d6
UD
17
18#include <assert.h>
19#include <errno.h>
20#include <netdb.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
d19687d6
UD
24#include <not-cancel.h>
25
26#include "nscd-client.h"
62417d7e 27#include "nscd_proto.h"
d19687d6
UD
28
29
30/* Define in nscd_gethst_r.c. */
31extern int __nss_not_use_nscd_hosts;
32
33
ed2ced8a 34/* We use the mapping from nscd_gethst. */
6f8a7dff 35libc_locked_map_ptr (extern, __hst_map_handle) attribute_hidden;
d19687d6 36
50e481ce
UD
37/* Defined in nscd_gethst_r.c. */
38extern int __nss_have_localdomain attribute_hidden;
39
d19687d6
UD
40
41int
42__nscd_getai (const char *key, struct nscd_ai_result **result, int *h_errnop)
43{
a1ffb40e 44 if (__glibc_unlikely (__nss_have_localdomain >= 0))
50e481ce
UD
45 {
46 if (__nss_have_localdomain == 0)
47 __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
48 if (__nss_have_localdomain > 0)
49 {
50 __nss_not_use_nscd_hosts = 1;
51 return -1;
52 }
53 }
54
d19687d6 55 size_t keylen = strlen (key) + 1;
0891f970 56 int gc_cycle;
1a77d37f 57 int nretries = 0;
0891f970
UD
58
59 /* If the mapping is available, try to search there instead of
60 communicating with the nscd. */
61 struct mapped_database *mapped;
ed2ced8a
UD
62 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
63 &gc_cycle);
0891f970
UD
64
65 retry:;
d19687d6
UD
66 struct nscd_ai_result *resultbuf = NULL;
67 const char *recend = (const char *) ~UINTMAX_C (0);
68 char *respdata = NULL;
69 int retval = -1;
70 int sock = -1;
1a77d37f 71 ai_response_header ai_resp;
d19687d6 72
81177191 73 if (mapped != NO_MAPPING)
d19687d6 74 {
1a77d37f 75 struct datahead *found = __nscd_cache_search (GETAI, key, keylen,
cfe1fc10 76 mapped, sizeof ai_resp);
d19687d6
UD
77 if (found != NULL)
78 {
1a77d37f
JJ
79 respdata = (char *) (&found->data[0].aidata + 1);
80 ai_resp = found->data[0].aidata;
d19687d6 81 recend = (const char *) found->data + found->recsize;
1a77d37f
JJ
82 /* Now check if we can trust ai_resp fields. If GC is
83 in progress, it can contain anything. */
84 if (mapped->head->gc_cycle != gc_cycle)
85 {
86 retval = -2;
87 goto out;
88 }
d19687d6
UD
89 }
90 }
91
92 /* If we do not have the cache mapped, try to get the data over the
93 socket. */
1a77d37f 94 if (respdata == NULL)
d19687d6 95 {
1a77d37f
JJ
96 sock = __nscd_open_socket (key, keylen, GETAI, &ai_resp,
97 sizeof (ai_resp));
d19687d6
UD
98 if (sock == -1)
99 {
6aa10807 100 /* nscd not running or wrong version. */
d19687d6 101 __nss_not_use_nscd_hosts = 1;
81177191 102 goto out;
d19687d6 103 }
d19687d6
UD
104 }
105
1a77d37f 106 if (ai_resp.found == 1)
d19687d6 107 {
1a77d37f 108 size_t datalen = ai_resp.naddrs + ai_resp.addrslen + ai_resp.canonlen;
d19687d6 109
1a77d37f 110 /* This check really only affects the case where the data
d19687d6 111 comes from the mapped cache. */
1a77d37f 112 if (respdata + datalen > recend)
d19687d6
UD
113 {
114 assert (sock == -1);
115 goto out;
116 }
117
118 /* Create result. */
119 resultbuf = (struct nscd_ai_result *) malloc (sizeof (*resultbuf)
120 + datalen);
121 if (resultbuf == NULL)
122 {
123 *h_errnop = NETDB_INTERNAL;
8dd71997 124 goto out_close;
d19687d6
UD
125 }
126
127 /* Set up the data structure, including pointers. */
1a77d37f 128 resultbuf->naddrs = ai_resp.naddrs;
d19687d6 129 resultbuf->addrs = (char *) (resultbuf + 1);
1a77d37f
JJ
130 resultbuf->family = (uint8_t *) (resultbuf->addrs + ai_resp.addrslen);
131 if (ai_resp.canonlen != 0)
d19687d6
UD
132 resultbuf->canon = (char *) (resultbuf->family + resultbuf->naddrs);
133 else
134 resultbuf->canon = NULL;
135
136 if (respdata == NULL)
137 {
138 /* Read the data from the socket. */
d2dc7d84 139 if ((size_t) __readall (sock, resultbuf + 1, datalen) == datalen)
d19687d6
UD
140 {
141 retval = 0;
142 *result = resultbuf;
143 }
0891f970
UD
144 else
145 {
146 free (resultbuf);
147 *h_errnop = NETDB_INTERNAL;
148 }
d19687d6
UD
149 }
150 else
151 {
152 /* Copy the data in the block. */
153 memcpy (resultbuf + 1, respdata, datalen);
154
5429ff76
UD
155 /* Try to detect corrupt databases. */
156 if (resultbuf->canon != NULL
1a77d37f 157 && resultbuf->canon[ai_resp.canonlen - 1] != '\0')
5429ff76 158 /* We cannot use the database. */
8dd71997 159 {
1a77d37f
JJ
160 if (mapped->head->gc_cycle != gc_cycle)
161 retval = -2;
162 else
163 free (resultbuf);
8dd71997
UD
164 goto out_close;
165 }
5429ff76 166
d19687d6
UD
167 retval = 0;
168 *result = resultbuf;
169 }
170 }
171 else
172 {
a1ffb40e 173 if (__glibc_unlikely (ai_resp.found == -1))
6aa10807
UD
174 {
175 /* The daemon does not cache this database. */
176 __nss_not_use_nscd_hosts = 1;
177 goto out_close;
178 }
179
d19687d6 180 /* Store the error number. */
1a77d37f 181 *h_errnop = ai_resp.error;
d19687d6 182
cfca0aa3
UD
183 /* Set errno to 0 to indicate no error, just no found record. */
184 __set_errno (0);
d19687d6
UD
185 /* Even though we have not found anything, the result is zero. */
186 retval = 0;
187 }
188
5429ff76 189 out_close:
d19687d6 190 if (sock != -1)
c181840c 191 __close_nocancel_nostatus (sock);
d19687d6 192 out:
1a77d37f 193 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
0891f970
UD
194 {
195 /* When we come here this means there has been a GC cycle while we
196 were looking for the data. This means the data might have been
197 inconsistent. Retry if possible. */
1a77d37f 198 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
0891f970
UD
199 {
200 /* nscd is just running gc now. Disable using the mapping. */
a364a3a7 201 if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
1a77d37f 202 __nscd_unmap (mapped);
0891f970
UD
203 mapped = NO_MAPPING;
204 }
205
1a77d37f
JJ
206 if (retval != -1)
207 {
208 *result = NULL;
209 free (resultbuf);
210 goto retry;
211 }
0891f970 212 }
d19687d6
UD
213
214 return retval;
215}