1 /* Cache handling for host lookup.
2 Copyright (C) 1998-2002, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
34 #include <arpa/inet.h>
35 #include <arpa/nameser.h>
36 #include <stackinfo.h>
42 /* This is the standard reply in case the service is disabled. */
43 static const hst_response_header disabled =
45 .version = NSCD_VERSION,
52 .error = NETDB_INTERNAL
55 /* This is the struct describing how to write this record. */
56 const struct iovec hst_iov_disabled =
58 .iov_base = (void *) &disabled,
59 .iov_len = sizeof (disabled)
63 /* This is the standard reply in case we haven't found the dataset. */
64 static const hst_response_header notfound =
66 .version = NSCD_VERSION,
73 .error = HOST_NOT_FOUND
79 hst_response_header resp;
85 cache_addhst (struct database *db, int fd, request_header *req, void *key,
86 struct hostent *hst, uid_t owner, int add_addr)
90 time_t t = time (NULL);
94 /* We have no data. This means we send the standard reply for this
96 total = sizeof (notfound);
98 written = TEMP_FAILURE_RETRY (write (fd, ¬found, total));
100 void *copy = malloc (req->key_len);
101 /* If we cannot allocate memory simply do not cache the information. */
104 memcpy (copy, key, req->key_len);
106 /* Compute the timeout time. */
109 /* Now get the lock to safely insert the records. */
110 pthread_rwlock_rdlock (&db->lock);
112 cache_add (req->type, copy, req->key_len, ¬found,
113 sizeof (notfound), (void *) -1, 0, t, db, owner);
115 pthread_rwlock_unlock (&db->lock);
120 /* Determine the I/O structure. */
121 struct hostdata *data;
122 size_t h_name_len = strlen (hst->h_name) + 1;
123 size_t h_aliases_cnt;
124 uint32_t *h_aliases_len;
125 size_t h_addr_list_cnt;
129 char *key_copy = NULL;
133 /* Determine the number of aliases. */
135 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
137 /* Determine the length of all aliases. */
138 h_aliases_len = (uint32_t *) alloca (h_aliases_cnt * sizeof (uint32_t));
140 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
142 h_aliases_len[cnt] = strlen (hst->h_aliases[cnt]) + 1;
143 total += h_aliases_len[cnt];
146 /* Determine the number of addresses. */
148 for (cnt = 0; hst->h_addr_list[cnt]; ++cnt)
151 /* We allocate all data in one memory block: the iov vector,
152 the response header and the dataset itself. */
153 total += (sizeof (struct hostdata)
155 + h_aliases_cnt * sizeof (uint32_t)
156 + h_addr_list_cnt * hst->h_length);
158 data = (struct hostdata *) malloc (total + req->key_len);
160 /* There is no reason to go on. */
161 error (EXIT_FAILURE, errno, _("while allocating cache entry"));
163 data->resp.version = NSCD_VERSION;
164 data->resp.found = 1;
165 data->resp.h_name_len = h_name_len;
166 data->resp.h_aliases_cnt = h_aliases_cnt;
167 data->resp.h_addrtype = hst->h_addrtype;
168 data->resp.h_length = hst->h_length;
169 data->resp.h_addr_list_cnt = h_addr_list_cnt;
170 data->resp.error = NETDB_SUCCESS;
174 cp = mempcpy (cp, hst->h_name, h_name_len);
175 cp = mempcpy (cp, h_aliases_len, h_aliases_cnt * sizeof (uint32_t));
177 /* The normal addresses first. */
179 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
180 cp = mempcpy (cp, hst->h_addr_list[cnt], hst->h_length);
182 /* Then the aliases. */
184 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
185 cp = mempcpy (cp, hst->h_aliases[cnt], h_aliases_len[cnt]);
187 assert (cp == data->strdata + total - sizeof (hst_response_header));
189 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
190 that the answer we get from the NSS does not contain the key
191 itself. This is the case if the resolver is used and the name
192 is extended by the domainnames from /etc/resolv.conf. Therefore
193 we explicitly add the name here. */
194 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
195 key_copy = memcpy (cp, key, req->key_len);
197 /* We write the dataset before inserting it to the database
198 since while inserting this thread might block and so would
199 unnecessarily let the receiver wait. */
200 written = TEMP_FAILURE_RETRY (write (fd, data, total));
202 addr_list_type = (hst->h_length == NS_INADDRSZ
203 ? GETHOSTBYADDR : GETHOSTBYADDRv6);
205 /* Compute the timeout time. */
208 /* Now get the lock to safely insert the records. */
209 pthread_rwlock_rdlock (&db->lock);
211 /* First add all the aliases. If the record contains more than
212 one IP address (used for load balancing etc) don't cache the
213 entry. This is something the current cache handling cannot
214 handle and it is more than questionable whether it is
215 worthwhile complicating the cache handling just for handling
216 such a special case. */
217 if (!add_addr && hst->h_addr_list[1] == NULL)
218 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
220 if (addr_list_type == GETHOSTBYADDR)
221 cache_add (GETHOSTBYNAME, aliases, h_aliases_len[cnt], data,
222 total, data, 0, t, db, owner);
224 cache_add (GETHOSTBYNAMEv6, aliases, h_aliases_len[cnt], data,
225 total, data, 0, t, db, owner);
227 aliases += h_aliases_len[cnt];
230 /* Next the normal addresses. */
232 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
234 cache_add (addr_list_type, addresses, hst->h_length, data, total,
235 data, 0, t, db, owner);
236 addresses += hst->h_length;
239 /* If necessary the IPv6 addresses. */
240 if (add_addr && addr_list_type == GETHOSTBYADDR)
241 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
243 cache_add (GETHOSTBYADDRv6, addresses, IN6ADDRSZ, data, total,
244 data, 0, t, db, owner);
245 addresses += IN6ADDRSZ;
248 /* Avoid adding names if more than one address is available. See
249 above for more info. */
250 if (!add_addr && hst->h_addr_list[1] == NULL)
252 /* If necessary add the key for this request. */
253 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
255 if (addr_list_type == GETHOSTBYADDR)
256 cache_add (GETHOSTBYNAME, key_copy, req->key_len, data, total,
257 data, 0, t, db, owner);
258 cache_add (GETHOSTBYNAMEv6, key_copy, req->key_len, data,
259 total, data, 0, t, db, owner);
262 /* And finally the name. We mark this as the last entry. */
263 if (addr_list_type == GETHOSTBYADDR)
264 cache_add (GETHOSTBYNAME, data->strdata, h_name_len, data, total,
265 data, 0, t, db, owner);
266 cache_add (GETHOSTBYNAMEv6, data->strdata, h_name_len, data,
267 total, data, 1, t, db, owner);
270 pthread_rwlock_unlock (&db->lock);
273 if (__builtin_expect (written != total, 0) && debug_level > 0)
276 dbg_log (_("short write in %s: %s"), __FUNCTION__,
277 strerror_r (errno, buf, sizeof (buf)));
283 addhstbyname (struct database *db, int fd, request_header *req,
284 void *key, uid_t uid)
286 /* Search for the entry matching the key. Please note that we don't
287 look again in the table whether the dataset is now available. We
288 simply insert it. It does not matter if it is in there twice. The
289 pruning function only will look at the timestamp. */
291 char *buffer = (char *) alloca (buflen);
292 struct hostent resultbuf;
295 bool use_malloc = false;
297 if (__builtin_expect (debug_level > 0, 0))
298 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
302 oldeuid = geteuid ();
306 while (__gethostbyname2_r (key, AF_INET, &resultbuf, buffer, buflen,
308 && h_errno == NETDB_INTERNAL
311 char *old_buffer = buffer;
315 if (__builtin_expect (buflen > 32768, 0))
318 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
321 /* We ran out of memory. We cannot do anything but
322 sending a negative response. In reality this should
331 /* Allocate a new buffer on the stack. If possible combine it
332 with the previously allocated buffer. */
333 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
339 cache_addhst (db, fd, req, key, hst, uid, 0);
347 addhstbyaddr (struct database *db, int fd, request_header *req,
348 void *key, uid_t uid)
350 /* Search for the entry matching the key. Please note that we don't
351 look again in the table whether the dataset is now available. We
352 simply insert it. It does not matter if it is in there twice. The
353 pruning function only will look at the timestamp. */
355 char *buffer = (char *) alloca (buflen);
356 struct hostent resultbuf;
359 bool use_malloc = false;
361 if (__builtin_expect (debug_level > 0, 0))
363 char buf[INET_ADDRSTRLEN];
364 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
365 inet_ntop (AF_INET, key, buf, sizeof (buf)));
370 oldeuid = geteuid ();
374 while (__gethostbyaddr_r (key, NS_INADDRSZ, AF_INET, &resultbuf, buffer,
375 buflen, &hst, &h_errno) != 0
376 && h_errno == NETDB_INTERNAL
379 char *old_buffer = buffer;
382 if (__builtin_expect (buflen > 32768, 0))
385 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
388 /* We ran out of memory. We cannot do anything but
389 sending a negative response. In reality this should
398 /* Allocate a new buffer on the stack. If possible combine it
399 with the previously allocated buffer. */
400 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
406 cache_addhst (db, fd, req, key, hst, uid, 1);
414 addhstbynamev6 (struct database *db, int fd, request_header *req,
415 void *key, uid_t uid)
417 /* Search for the entry matching the key. Please note that we don't
418 look again in the table whether the dataset is now available. We
419 simply insert it. It does not matter if it is in there twice. The
420 pruning function only will look at the timestamp. */
422 char *buffer = (char *) alloca (buflen);
423 struct hostent resultbuf;
426 bool use_malloc = false;
428 if (__builtin_expect (debug_level > 0, 0))
429 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
433 oldeuid = geteuid ();
437 while (__gethostbyname2_r (key, AF_INET6, &resultbuf, buffer, buflen,
439 && h_errno == NETDB_INTERNAL
442 char *old_buffer = buffer;
445 if (__builtin_expect (buflen > 32768, 0))
448 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
451 /* We ran out of memory. We cannot do anything but
452 sending a negative response. In reality this should
461 /* Allocate a new buffer on the stack. If possible combine it
462 with the previously allocated buffer. */
463 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
469 cache_addhst (db, fd, req, key, hst, uid, 0);
477 addhstbyaddrv6 (struct database *db, int fd, request_header *req,
478 void *key, uid_t uid)
480 /* Search for the entry matching the key. Please note that we don't
481 look again in the table whether the dataset is now available. We
482 simply insert it. It does not matter if it is in there twice. The
483 pruning function only will look at the timestamp. */
485 char *buffer = (char *) alloca (buflen);
486 struct hostent resultbuf;
489 bool use_malloc = false;
491 if (__builtin_expect (debug_level > 0, 0))
493 char buf[INET6_ADDRSTRLEN];
494 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
495 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
500 oldeuid = geteuid ();
504 while (__gethostbyaddr_r (key, NS_IN6ADDRSZ, AF_INET6, &resultbuf,
505 buffer, buflen, &hst, &h_errno) != 0
506 && h_errno == NETDB_INTERNAL
509 char *old_buffer = buffer;
512 if (__builtin_expect (buflen > 32768, 0))
515 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
518 /* We ran out of memory. We cannot do anything but
519 sending a negative response. In reality this should
528 /* Allocate a new buffer on the stack. If possible combine it
529 with the previously allocated buffer. */
530 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
536 cache_addhst (db, fd, req, key, hst, uid, 1);