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>
37 #include <stackinfo.h>
43 /* This is the standard reply in case the service is disabled. */
44 static const hst_response_header disabled
=
46 .version
= NSCD_VERSION
,
53 .error
= NETDB_INTERNAL
56 /* This is the struct describing how to write this record. */
57 const struct iovec hst_iov_disabled
=
59 .iov_base
= (void *) &disabled
,
60 .iov_len
= sizeof (disabled
)
64 /* This is the standard reply in case we haven't found the dataset. */
65 static const hst_response_header notfound
=
67 .version
= NSCD_VERSION
,
74 .error
= HOST_NOT_FOUND
79 cache_addhst (struct database_dyn
*db
, int fd
, request_header
*req
,
80 const void *key
, struct hostent
*hst
, uid_t owner
,
81 struct hashentry
*he
, struct datahead
*dh
, int errval
)
85 time_t t
= time (NULL
);
87 /* We allocate all data in one memory block: the iov vector,
88 the response header and the dataset itself. */
92 hst_response_header resp
;
96 assert (offsetof (struct dataset
, resp
) == offsetof (struct datahead
, data
));
100 if (he
!= NULL
&& errval
== EAGAIN
)
102 /* If we have an old record available but cannot find one
103 now because the service is not available we keep the old
104 record and make sure it does not get removed. */
105 if (reload_count
!= UINT_MAX
)
106 /* Do not reset the value if we never not reload the record. */
107 dh
->nreloads
= reload_count
- 1;
113 /* We have no data. This means we send the standard reply for this
115 written
= total
= sizeof (notfound
);
118 written
= TEMP_FAILURE_RETRY (write (fd
, ¬found
, total
));
120 dataset
= mempool_alloc (db
, sizeof (struct dataset
) + req
->key_len
);
121 /* If we cannot permanently store the result, so be it. */
124 dataset
->head
.allocsize
= sizeof (struct dataset
) + req
->key_len
;
125 dataset
->head
.recsize
= total
;
126 dataset
->head
.notfound
= true;
127 dataset
->head
.nreloads
= 0;
128 dataset
->head
.usable
= true;
130 /* Compute the timeout time. */
131 dataset
->head
.timeout
= t
+ db
->negtimeout
;
133 /* This is the reply. */
134 memcpy (&dataset
->resp
, ¬found
, total
);
136 /* Copy the key data. */
137 memcpy (dataset
->strdata
, key
, req
->key_len
);
139 /* Now get the lock to safely insert the records. */
140 pthread_rwlock_rdlock (&db
->lock
);
142 if (cache_add (req
->type
, &dataset
->strdata
, req
->key_len
,
143 &dataset
->head
, true, db
, owner
) < 0)
144 /* Ensure the data can be recovered. */
145 dataset
->head
.usable
= false;
147 pthread_rwlock_unlock (&db
->lock
);
149 /* Mark the old entry as obsolete. */
154 ++db
->head
->addfailed
;
159 /* Determine the I/O structure. */
160 size_t h_name_len
= strlen (hst
->h_name
) + 1;
161 size_t h_aliases_cnt
;
162 uint32_t *h_aliases_len
;
163 size_t h_addr_list_cnt
;
167 char *key_copy
= NULL
;
171 /* Determine the number of aliases. */
173 for (cnt
= 0; hst
->h_aliases
[cnt
] != NULL
; ++cnt
)
175 /* Determine the length of all aliases. */
176 h_aliases_len
= (uint32_t *) alloca (h_aliases_cnt
* sizeof (uint32_t));
178 for (cnt
= 0; cnt
< h_aliases_cnt
; ++cnt
)
180 h_aliases_len
[cnt
] = strlen (hst
->h_aliases
[cnt
]) + 1;
181 total
+= h_aliases_len
[cnt
];
184 /* Determine the number of addresses. */
186 for (cnt
= 0; hst
->h_addr_list
[cnt
]; ++cnt
)
189 if (h_addr_list_cnt
== 0)
193 total
+= (sizeof (struct dataset
)
195 + h_aliases_cnt
* sizeof (uint32_t)
196 + h_addr_list_cnt
* hst
->h_length
);
199 /* If we refill the cache, first assume the reconrd did not
200 change. Allocate memory on the cache since it is likely
201 discarded anyway. If it turns out to be necessary to have a
202 new record we can still allocate real memory. */
203 bool alloca_used
= false;
206 /* If the record contains more than one IP address (used for
207 load balancing etc) don't cache the entry. This is something
208 the current cache handling cannot handle and it is more than
209 questionable whether it is worthwhile complicating the cache
210 handling just for handling such a special case. */
211 if (he
== NULL
&& hst
->h_addr_list
[1] == NULL
)
213 dataset
= (struct dataset
*) mempool_alloc (db
,
214 total
+ req
->key_len
);
216 ++db
->head
->addfailed
;
221 /* We cannot permanently add the result in the moment. But
222 we can provide the result as is. Store the data in some
224 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
226 /* We cannot add this record to the permanent database. */
230 dataset
->head
.allocsize
= total
+ req
->key_len
;
231 dataset
->head
.recsize
= total
- offsetof (struct dataset
, resp
);
232 dataset
->head
.notfound
= false;
233 dataset
->head
.nreloads
= he
== NULL
? 0 : (dh
->nreloads
+ 1);
234 dataset
->head
.usable
= true;
236 /* Compute the timeout time. */
237 dataset
->head
.timeout
= t
+ db
->postimeout
;
239 dataset
->resp
.version
= NSCD_VERSION
;
240 dataset
->resp
.found
= 1;
241 dataset
->resp
.h_name_len
= h_name_len
;
242 dataset
->resp
.h_aliases_cnt
= h_aliases_cnt
;
243 dataset
->resp
.h_addrtype
= hst
->h_addrtype
;
244 dataset
->resp
.h_length
= hst
->h_length
;
245 dataset
->resp
.h_addr_list_cnt
= h_addr_list_cnt
;
246 dataset
->resp
.error
= NETDB_SUCCESS
;
248 cp
= dataset
->strdata
;
250 cp
= mempcpy (cp
, hst
->h_name
, h_name_len
);
251 cp
= mempcpy (cp
, h_aliases_len
, h_aliases_cnt
* sizeof (uint32_t));
253 /* The normal addresses first. */
255 for (cnt
= 0; cnt
< h_addr_list_cnt
; ++cnt
)
256 cp
= mempcpy (cp
, hst
->h_addr_list
[cnt
], hst
->h_length
);
258 /* Then the aliases. */
260 for (cnt
= 0; cnt
< h_aliases_cnt
; ++cnt
)
261 cp
= mempcpy (cp
, hst
->h_aliases
[cnt
], h_aliases_len
[cnt
]);
264 == dataset
->strdata
+ total
- offsetof (struct dataset
,
267 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
268 that the answer we get from the NSS does not contain the key
269 itself. This is the case if the resolver is used and the name
270 is extended by the domainnames from /etc/resolv.conf. Therefore
271 we explicitly add the name here. */
272 key_copy
= memcpy (cp
, key
, req
->key_len
);
274 /* Now we can determine whether on refill we have to create a new
280 if (total
+ req
->key_len
== dh
->allocsize
281 && total
- offsetof (struct dataset
, resp
) == dh
->recsize
282 && memcmp (&dataset
->resp
, dh
->data
,
283 dh
->allocsize
- offsetof (struct dataset
, resp
)) == 0)
285 /* The sata has not changed. We will just bump the
286 timeout value. Note that the new record has been
287 allocated on the stack and need not be freed. */
288 dh
->timeout
= dataset
->head
.timeout
;
293 /* We have to create a new record. Just allocate
294 appropriate memory and copy it. */
296 = (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
);
299 /* Adjust pointers into the memory block. */
300 addresses
= (char *) newp
+ (addresses
- (char *) dataset
);
301 aliases
= (char *) newp
+ (aliases
- (char *) dataset
);
302 if (key_copy
!= NULL
)
303 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
305 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
309 /* Mark the old record as obsolete. */
315 /* We write the dataset before inserting it to the database
316 since while inserting this thread might block and so would
317 unnecessarily keep the receiver waiting. */
320 written
= TEMP_FAILURE_RETRY (write (fd
, &dataset
->resp
, total
));
323 /* Add the record to the database. But only if it has not been
326 If the record contains more than one IP address (used for
327 load balancing etc) don't cache the entry. This is something
328 the current cache handling cannot handle and it is more than
329 questionable whether it is worthwhile complicating the cache
330 handling just for handling such a special case. */
333 /* If necessary, we also propagate the data to disk. */
337 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
338 msync ((void *) pval
,
339 ((uintptr_t) dataset
& pagesize_m1
)
340 + total
+ req
->key_len
, MS_ASYNC
);
343 addr_list_type
= (hst
->h_length
== NS_INADDRSZ
344 ? GETHOSTBYADDR
: GETHOSTBYADDRv6
);
346 /* Now get the lock to safely insert the records. */
347 pthread_rwlock_rdlock (&db
->lock
);
349 /* NB: the following code is really complicated. It has
350 seemlingly duplicated code paths which do the same. The
351 problem is that we always must add the hash table entry
352 with the FIRST flag set first. Otherwise we get dangling
353 pointers in case memory allocation fails. */
354 assert (hst
->h_addr_list
[1] == NULL
);
356 /* Avoid adding names if more than one address is available. See
357 above for more info. */
358 assert (req
->type
== GETHOSTBYNAME
359 || req
->type
== GETHOSTBYNAMEv6
360 || req
->type
== GETHOSTBYADDR
361 || req
->type
== GETHOSTBYADDRv6
);
363 if (cache_add (req
->type
, key_copy
, req
->key_len
,
364 &dataset
->head
, true, db
, owner
) < 0)
365 /* Could not allocate memory. Make sure the
366 data gets discarded. */
367 dataset
->head
.usable
= false;
369 pthread_rwlock_unlock (&db
->lock
);
373 if (__builtin_expect (written
!= total
, 0) && debug_level
> 0)
376 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
377 strerror_r (errno
, buf
, sizeof (buf
)));
383 lookup (int type
, void *key
, struct hostent
*resultbufp
, char *buffer
,
384 size_t buflen
, struct hostent
**hst
)
386 if (type
== GETHOSTBYNAME
)
387 return __gethostbyname2_r (key
, AF_INET
, resultbufp
, buffer
, buflen
, hst
,
389 else if (type
== GETHOSTBYNAMEv6
)
390 return __gethostbyname2_r (key
, AF_INET6
, resultbufp
, buffer
, buflen
, hst
,
392 else if (type
== GETHOSTBYADDR
)
393 return __gethostbyaddr_r (key
, NS_INADDRSZ
, AF_INET
, resultbufp
, buffer
,
394 buflen
, hst
, &h_errno
);
396 return __gethostbyaddr_r (key
, NS_IN6ADDRSZ
, AF_INET6
, resultbufp
, buffer
,
397 buflen
, hst
, &h_errno
);
402 addhstbyX (struct database_dyn
*db
, int fd
, request_header
*req
,
403 void *key
, uid_t uid
, struct hashentry
*he
, struct datahead
*dh
)
405 /* Search for the entry matching the key. Please note that we don't
406 look again in the table whether the dataset is now available. We
407 simply insert it. It does not matter if it is in there twice. The
408 pruning function only will look at the timestamp. */
410 char *buffer
= (char *) alloca (buflen
);
411 struct hostent resultbuf
;
414 bool use_malloc
= false;
417 if (__builtin_expect (debug_level
> 0, 0))
420 char buf
[INET6_ADDRSTRLEN
+ 1];
421 if (req
->type
== GETHOSTBYNAME
|| req
->type
== GETHOSTBYNAMEv6
)
424 str
= inet_ntop (req
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
425 key
, buf
, sizeof (buf
));
428 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) str
);
430 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) str
);
435 oldeuid
= geteuid ();
439 while (lookup (req
->type
, key
, &resultbuf
, buffer
, buflen
, &hst
) != 0
440 && h_errno
== NETDB_INTERNAL
441 && (errval
= errno
) == ERANGE
)
443 char *old_buffer
= buffer
;
447 if (__builtin_expect (buflen
> 32768, 0))
450 buffer
= (char *) realloc (use_malloc
? buffer
: NULL
, buflen
);
453 /* We ran out of memory. We cannot do anything but
454 sending a negative response. In reality this should
459 /* We set the error to indicate this is (possibly) a
460 temporary error and that it does not mean the entry
461 is not available at all. */
468 /* Allocate a new buffer on the stack. If possible combine it
469 with the previously allocated buffer. */
470 buffer
= (char *) extend_alloca (buffer
, buflen
, buflen
+ INCR
);
476 cache_addhst (db
, fd
, req
, key
, hst
, uid
, he
, dh
,
477 h_errno
== TRY_AGAIN
? errval
: 0);
485 addhstbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
486 void *key
, uid_t uid
)
488 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
493 readdhstbyname (struct database_dyn
*db
, struct hashentry
*he
,
498 .type
= GETHOSTBYNAME
,
502 addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
507 addhstbyaddr (struct database_dyn
*db
, int fd
, request_header
*req
,
508 void *key
, uid_t uid
)
510 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
515 readdhstbyaddr (struct database_dyn
*db
, struct hashentry
*he
,
520 .type
= GETHOSTBYADDR
,
524 addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
529 addhstbynamev6 (struct database_dyn
*db
, int fd
, request_header
*req
,
530 void *key
, uid_t uid
)
532 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
537 readdhstbynamev6 (struct database_dyn
*db
, struct hashentry
*he
,
542 .type
= GETHOSTBYNAMEv6
,
546 addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
551 addhstbyaddrv6 (struct database_dyn
*db
, int fd
, request_header
*req
,
552 void *key
, uid_t uid
)
554 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
559 readdhstbyaddrv6 (struct database_dyn
*db
, struct hashentry
*he
,
564 .type
= GETHOSTBYADDRv6
,
568 addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);