1 /* Cache handling for host lookup.
2 Copyright (C) 1998-2002, 2003 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
76 /* This is the struct describing how to write this record. */
77 static const struct iovec iov_notfound =
79 .iov_base = (void *) ¬found,
80 .iov_len = sizeof (notfound)
86 hst_response_header resp;
92 cache_addhst (struct database *db, int fd, request_header *req, void *key,
93 struct hostent *hst, uid_t owner, int add_addr)
97 time_t t = time (NULL);
101 /* We have no data. This means we send the standard reply for this
105 total = sizeof (notfound);
107 written = TEMP_FAILURE_RETRY (writev (fd, &iov_notfound, 1));
109 copy = malloc (req->key_len);
111 error (EXIT_FAILURE, errno, _("while allocating key copy"));
112 memcpy (copy, key, req->key_len);
114 /* Compute the timeout time. */
117 /* Now get the lock to safely insert the records. */
118 pthread_rwlock_rdlock (&db->lock);
120 cache_add (req->type, copy, req->key_len, ¬found,
121 sizeof (notfound), (void *) -1, 0, t, db, owner);
123 pthread_rwlock_unlock (&db->lock);
127 /* Determine the I/O structure. */
128 struct hostdata *data;
129 size_t h_name_len = strlen (hst->h_name) + 1;
130 size_t h_aliases_cnt;
131 uint32_t *h_aliases_len;
132 size_t h_addr_list_cnt;
136 char *key_copy = NULL;
140 /* Determine the number of aliases. */
142 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
144 /* Determine the length of all aliases. */
145 h_aliases_len = (uint32_t *) alloca (h_aliases_cnt * sizeof (uint32_t));
147 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
149 h_aliases_len[cnt] = strlen (hst->h_aliases[cnt]) + 1;
150 total += h_aliases_len[cnt];
153 /* Determine the number of addresses. */
155 for (cnt = 0; hst->h_addr_list[cnt]; ++cnt)
158 /* We allocate all data in one memory block: the iov vector,
159 the response header and the dataset itself. */
160 total += (sizeof (struct hostdata)
162 + h_aliases_cnt * sizeof (uint32_t)
163 + h_addr_list_cnt * hst->h_length);
165 data = (struct hostdata *) malloc (total + req->key_len);
167 /* There is no reason to go on. */
168 error (EXIT_FAILURE, errno, _("while allocating cache entry"));
170 data->resp.found = 1;
171 data->resp.h_name_len = h_name_len;
172 data->resp.h_aliases_cnt = h_aliases_cnt;
173 data->resp.h_addrtype = hst->h_addrtype;
174 data->resp.h_length = hst->h_length;
175 data->resp.h_addr_list_cnt = h_addr_list_cnt;
176 data->resp.error = NETDB_SUCCESS;
180 cp = mempcpy (cp, hst->h_name, h_name_len);
181 cp = mempcpy (cp, h_aliases_len, h_aliases_cnt * sizeof (uint32_t));
183 /* The normal addresses first. */
185 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
186 cp = mempcpy (cp, hst->h_addr_list[cnt], hst->h_length);
188 /* Then the aliases. */
190 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
191 cp = mempcpy (cp, hst->h_aliases[cnt], h_aliases_len[cnt]);
193 assert (cp == data->strdata + total - sizeof (hst_response_header));
195 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
196 that the answer we get from the NSS does not contain the key
197 itself. This is the case if the resolver is used and the name
198 is extended by the domainnames from /etc/resolv.conf. Therefore
199 we explicitly add the name here. */
200 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
201 key_copy = memcpy (cp, key, req->key_len);
203 /* We write the dataset before inserting it to the database
204 since while inserting this thread might block and so would
205 unnecessarily let the receiver wait. */
206 written = TEMP_FAILURE_RETRY (write (fd, data, total));
208 addr_list_type = (hst->h_length == NS_INADDRSZ
209 ? GETHOSTBYADDR : GETHOSTBYADDRv6);
211 /* Compute the timeout time. */
214 /* Now get the lock to safely insert the records. */
215 pthread_rwlock_rdlock (&db->lock);
217 /* First add all the aliases. If the record contains more than
218 one IP address (used for load balancing etc) don't cache the
219 entry. This is something the current cache handling cannot
220 handle and it is more than questionable whether it is
221 worthwhile complicating the cache handling just for handling
222 such a special case. */
223 if (!add_addr && hst->h_addr_list[1] == NULL)
224 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
226 if (addr_list_type == GETHOSTBYADDR)
227 cache_add (GETHOSTBYNAME, aliases, h_aliases_len[cnt], data,
228 total, data, 0, t, db, owner);
230 cache_add (GETHOSTBYNAMEv6, aliases, h_aliases_len[cnt], data,
231 total, data, 0, t, db, owner);
233 aliases += h_aliases_len[cnt];
236 /* Next the normal addresses. */
238 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
240 cache_add (addr_list_type, addresses, hst->h_length, data, total,
241 data, 0, t, db, owner);
242 addresses += hst->h_length;
245 /* If necessary the IPv6 addresses. */
246 if (add_addr && addr_list_type == GETHOSTBYADDR)
247 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
249 cache_add (GETHOSTBYADDRv6, addresses, IN6ADDRSZ, data, total,
250 data, 0, t, db, owner);
251 addresses += IN6ADDRSZ;
254 /* Avoid adding names if more than one address is available. See
255 above for more info. */
256 if (!add_addr && hst->h_addr_list[1] == NULL)
258 /* If necessary add the key for this request. */
259 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
261 if (addr_list_type == GETHOSTBYADDR)
262 cache_add (GETHOSTBYNAME, key_copy, req->key_len, data, total,
263 data, 0, t, db, owner);
264 cache_add (GETHOSTBYNAMEv6, key_copy, req->key_len, data,
265 total, data, 0, t, db, owner);
268 /* And finally the name. We mark this as the last entry. */
269 if (addr_list_type == GETHOSTBYADDR)
270 cache_add (GETHOSTBYNAME, data->strdata, h_name_len, data, total,
271 data, 0, t, db, owner);
272 cache_add (GETHOSTBYNAMEv6, data->strdata, h_name_len, data,
273 total, data, 1, t, db, owner);
276 pthread_rwlock_unlock (&db->lock);
279 if (__builtin_expect (written != total, 0) && debug_level > 0)
282 dbg_log (_("short write in %s: %s"), __FUNCTION__,
283 strerror_r (errno, buf, sizeof (buf)));
289 addhstbyname (struct database *db, int fd, request_header *req,
290 void *key, uid_t uid)
292 /* Search for the entry matching the key. Please note that we don't
293 look again in the table whether the dataset is now available. We
294 simply insert it. It does not matter if it is in there twice. The
295 pruning function only will look at the timestamp. */
297 char *buffer = (char *) alloca (buflen);
298 struct hostent resultbuf;
301 bool use_malloc = false;
303 if (__builtin_expect (debug_level > 0, 0))
304 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
308 oldeuid = geteuid ();
312 while (__gethostbyname2_r (key, AF_INET, &resultbuf, buffer, buflen,
314 && h_errno == NETDB_INTERNAL
317 char *old_buffer = buffer;
321 if (__builtin_expect (buflen > 32768, 0))
324 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
327 /* We ran out of memory. We cannot do anything but
328 sending a negative response. In reality this should
337 /* Allocate a new buffer on the stack. If possible combine it
338 with the previously allocated buffer. */
339 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
345 cache_addhst (db, fd, req, key, hst, uid, 0);
353 addhstbyaddr (struct database *db, int fd, request_header *req,
354 void *key, uid_t uid)
356 /* Search for the entry matching the key. Please note that we don't
357 look again in the table whether the dataset is now available. We
358 simply insert it. It does not matter if it is in there twice. The
359 pruning function only will look at the timestamp. */
361 char *buffer = (char *) alloca (buflen);
362 struct hostent resultbuf;
365 bool use_malloc = false;
367 if (__builtin_expect (debug_level > 0, 0))
369 char buf[INET_ADDRSTRLEN];
370 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
371 inet_ntop (AF_INET, key, buf, sizeof (buf)));
376 oldeuid = geteuid ();
380 while (__gethostbyaddr_r (key, NS_INADDRSZ, AF_INET, &resultbuf, buffer,
381 buflen, &hst, &h_errno) != 0
382 && h_errno == NETDB_INTERNAL
385 char *old_buffer = buffer;
388 if (__builtin_expect (buflen > 32768, 0))
391 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
394 /* We ran out of memory. We cannot do anything but
395 sending a negative response. In reality this should
404 /* Allocate a new buffer on the stack. If possible combine it
405 with the previously allocated buffer. */
406 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
412 cache_addhst (db, fd, req, key, hst, uid, 1);
420 addhstbynamev6 (struct database *db, int fd, request_header *req,
421 void *key, uid_t uid)
423 /* Search for the entry matching the key. Please note that we don't
424 look again in the table whether the dataset is now available. We
425 simply insert it. It does not matter if it is in there twice. The
426 pruning function only will look at the timestamp. */
428 char *buffer = (char *) alloca (buflen);
429 struct hostent resultbuf;
432 bool use_malloc = false;
434 if (__builtin_expect (debug_level > 0, 0))
435 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
439 oldeuid = geteuid ();
443 while (__gethostbyname2_r (key, AF_INET6, &resultbuf, buffer, buflen,
445 && h_errno == NETDB_INTERNAL
448 char *old_buffer = buffer;
451 if (__builtin_expect (buflen > 32768, 0))
454 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
457 /* We ran out of memory. We cannot do anything but
458 sending a negative response. In reality this should
467 /* Allocate a new buffer on the stack. If possible combine it
468 with the previously allocated buffer. */
469 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
475 cache_addhst (db, fd, req, key, hst, uid, 0);
483 addhstbyaddrv6 (struct database *db, int fd, request_header *req,
484 void *key, uid_t uid)
486 /* Search for the entry matching the key. Please note that we don't
487 look again in the table whether the dataset is now available. We
488 simply insert it. It does not matter if it is in there twice. The
489 pruning function only will look at the timestamp. */
491 char *buffer = (char *) alloca (buflen);
492 struct hostent resultbuf;
495 bool use_malloc = false;
497 if (__builtin_expect (debug_level > 0, 0))
499 char buf[INET6_ADDRSTRLEN];
500 dbg_log (_("Haven't found \"%s\" in hosts cache!"),
501 inet_ntop (AF_INET6, key, buf, sizeof (buf)));
506 oldeuid = geteuid ();
510 while (__gethostbyaddr_r (key, NS_IN6ADDRSZ, AF_INET6, &resultbuf,
511 buffer, buflen, &hst, &h_errno) != 0
512 && h_errno == NETDB_INTERNAL
515 char *old_buffer = buffer;
518 if (__builtin_expect (buflen > 32768, 0))
521 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
524 /* We ran out of memory. We cannot do anything but
525 sending a negative response. In reality this should
534 /* Allocate a new buffer on the stack. If possible combine it
535 with the previously allocated buffer. */
536 buffer = (char *) extend_alloca (buffer, buflen, buflen + INCR);
542 cache_addhst (db, fd, req, key, hst, uid, 1);