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 /* If the record contains more than one IP address (used for
203 load balancing etc) don't cache the entry. This is something
204 the current cache handling cannot handle and it is more than
205 questionable whether it is worthwhile complicating the cache
206 handling just for handling such a special case. */
207 if (!add_addr && hst->h_addr_list[1] != NULL)
213 addr_list_type = (hst->h_length == NS_INADDRSZ
214 ? GETHOSTBYADDR : GETHOSTBYADDRv6);
216 /* Compute the timeout time. */
219 /* Now get the lock to safely insert the records. */
220 pthread_rwlock_rdlock (&db->lock);
222 /* First add all the aliases. */
223 assert (add_addr || hst->h_addr_list[1] == NULL);
225 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
227 if (addr_list_type == GETHOSTBYADDR)
228 cache_add (GETHOSTBYNAME, aliases, h_aliases_len[cnt], data,
229 total, data, 0, t, db, owner);
231 cache_add (GETHOSTBYNAMEv6, aliases, h_aliases_len[cnt], data,
232 total, data, 0, t, db, owner);
234 aliases += h_aliases_len[cnt];
237 /* Next the normal addresses. */
239 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
241 cache_add (addr_list_type, addresses, hst->h_length, data, total,
242 data, cnt + 1 == h_addr_list_cnt, t, db, owner);
243 addresses += hst->h_length;
246 /* If necessary the IPv6 addresses. */
247 if (add_addr && addr_list_type == GETHOSTBYADDR)
248 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
250 cache_add (GETHOSTBYADDRv6, addresses, IN6ADDRSZ, data, total,
251 data, 0, t, db, owner);
252 addresses += IN6ADDRSZ;
255 /* Avoid adding names if more than one address is available. See
256 above for more info. */
259 /* If necessary add the key for this request.
261 Note: hst->h_addr_list[1] == NULL. */
262 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
264 if (addr_list_type == GETHOSTBYADDR)
265 cache_add (GETHOSTBYNAME, key_copy, req->key_len, data, total,
266 data, 0, t, db, owner);
267 cache_add (GETHOSTBYNAMEv6, key_copy, req->key_len, data,
268 total, data, 0, t, db, owner);
271 /* And finally the name. We mark this as the last entry. */
272 if (addr_list_type == GETHOSTBYADDR)
273 cache_add (GETHOSTBYNAME, data->strdata, h_name_len, data, total,
274 data, 0, t, db, owner);
275 cache_add (GETHOSTBYNAMEv6, data->strdata, h_name_len, data,
276 total, data, 1, t, db, owner);
279 pthread_rwlock_unlock (&db->lock);
282 if (__builtin_expect (written != total, 0) && debug_level > 0)
285 dbg_log (_("short write in %s: %s"), __FUNCTION__,
286 strerror_r (errno, buf, sizeof (buf)));
292 addhstbyname (struct database *db, int fd, request_header *req,
293 void *key, uid_t uid)
295 /* Search for the entry matching the key. Please note that we don't
296 look again in the table whether the dataset is now available. We
297 simply insert it. It does not matter if it is in there twice. The
298 pruning function only will look at the timestamp. */
300 char *buffer = (char *) alloca (buflen);
301 struct hostent resultbuf;
304 bool use_malloc = false;
306 if (__builtin_expect (debug_level > 0, 0))
307 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
311 oldeuid = geteuid ();
315 while (__gethostbyname2_r (key, AF_INET, &resultbuf, buffer, buflen,
317 && h_errno == NETDB_INTERNAL
320 char *old_buffer = buffer;
324 if (__builtin_expect (buflen > 32768, 0))
327 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
330 /* We ran out of memory. We cannot do anything but
331 sending a negative response. In reality this should
340 /* Allocate a new buffer on the stack. If possible combine it
341 with the previously allocated buffer. */
342 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
348 cache_addhst (db, fd, req, key, hst, uid, 0);
356 addhstbyaddr (struct database *db, int fd, request_header *req,
357 void *key, uid_t uid)
359 /* Search for the entry matching the key. Please note that we don't
360 look again in the table whether the dataset is now available. We
361 simply insert it. It does not matter if it is in there twice. The
362 pruning function only will look at the timestamp. */
364 char *buffer = (char *) alloca (buflen);
365 struct hostent resultbuf;
368 bool use_malloc = false;
370 if (__builtin_expect (debug_level > 0, 0))
372 char buf[INET_ADDRSTRLEN];
373 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
374 inet_ntop (AF_INET, key, buf, sizeof (buf)));
379 oldeuid = geteuid ();
383 while (__gethostbyaddr_r (key, NS_INADDRSZ, AF_INET, &resultbuf, buffer,
384 buflen, &hst, &h_errno) != 0
385 && h_errno == NETDB_INTERNAL
388 char *old_buffer = buffer;
391 if (__builtin_expect (buflen > 32768, 0))
394 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
397 /* We ran out of memory. We cannot do anything but
398 sending a negative response. In reality this should
407 /* Allocate a new buffer on the stack. If possible combine it
408 with the previously allocated buffer. */
409 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
415 cache_addhst (db, fd, req, key, hst, uid, 1);
423 addhstbynamev6 (struct database *db, int fd, request_header *req,
424 void *key, uid_t uid)
426 /* Search for the entry matching the key. Please note that we don't
427 look again in the table whether the dataset is now available. We
428 simply insert it. It does not matter if it is in there twice. The
429 pruning function only will look at the timestamp. */
431 char *buffer = (char *) alloca (buflen);
432 struct hostent resultbuf;
435 bool use_malloc = false;
437 if (__builtin_expect (debug_level > 0, 0))
438 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
442 oldeuid = geteuid ();
446 while (__gethostbyname2_r (key, AF_INET6, &resultbuf, buffer, buflen,
448 && h_errno == NETDB_INTERNAL
451 char *old_buffer = buffer;
454 if (__builtin_expect (buflen > 32768, 0))
457 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
460 /* We ran out of memory. We cannot do anything but
461 sending a negative response. In reality this should
470 /* Allocate a new buffer on the stack. If possible combine it
471 with the previously allocated buffer. */
472 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
478 cache_addhst (db, fd, req, key, hst, uid, 0);
486 addhstbyaddrv6 (struct database *db, int fd, request_header *req,
487 void *key, uid_t uid)
489 /* Search for the entry matching the key. Please note that we don't
490 look again in the table whether the dataset is now available. We
491 simply insert it. It does not matter if it is in there twice. The
492 pruning function only will look at the timestamp. */
494 char *buffer = (char *) alloca (buflen);
495 struct hostent resultbuf;
498 bool use_malloc = false;
500 if (__builtin_expect (debug_level > 0, 0))
502 char buf[INET6_ADDRSTRLEN];
503 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
504 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
509 oldeuid = geteuid ();
513 while (__gethostbyaddr_r (key, NS_IN6ADDRSZ, AF_INET6, &resultbuf,
514 buffer, buflen, &hst, &h_errno) != 0
515 && h_errno == NETDB_INTERNAL
518 char *old_buffer = buffer;
521 if (__builtin_expect (buflen > 32768, 0))
524 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
527 /* We ran out of memory. We cannot do anything but
528 sending a negative response. In reality this should
537 /* Allocate a new buffer on the stack. If possible combine it
538 with the previously allocated buffer. */
539 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
545 cache_addhst (db, fd, req, key, hst, uid, 1);