1 /* Cache handling for host lookup.
2 Copyright (C) 1998-2005, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
35 #include <stackinfo.h>
40 # include <kernel-features.h>
44 /* This is the standard reply in case the service is disabled. */
45 static const hst_response_header disabled
=
47 .version
= NSCD_VERSION
,
54 .error
= NETDB_INTERNAL
57 /* This is the struct describing how to write this record. */
58 const struct iovec hst_iov_disabled
=
60 .iov_base
= (void *) &disabled
,
61 .iov_len
= sizeof (disabled
)
65 /* This is the standard reply in case we haven't found the dataset. */
66 static const hst_response_header notfound
=
68 .version
= NSCD_VERSION
,
75 .error
= HOST_NOT_FOUND
80 cache_addhst (struct database_dyn
*db
, int fd
, request_header
*req
,
81 const void *key
, struct hostent
*hst
, uid_t owner
,
82 struct hashentry
*he
, struct datahead
*dh
, int errval
)
86 time_t t
= time (NULL
);
88 /* We allocate all data in one memory block: the iov vector,
89 the response header and the dataset itself. */
93 hst_response_header resp
;
97 assert (offsetof (struct dataset
, resp
) == offsetof (struct datahead
, data
));
101 if (he
!= NULL
&& errval
== EAGAIN
)
103 /* If we have an old record available but cannot find one
104 now because the service is not available we keep the old
105 record and make sure it does not get removed. */
106 if (reload_count
!= UINT_MAX
)
107 /* Do not reset the value if we never not reload the record. */
108 dh
->nreloads
= reload_count
- 1;
114 /* We have no data. This means we send the standard reply for this
116 written
= total
= sizeof (notfound
);
119 written
= TEMP_FAILURE_RETRY (send (fd
, ¬found
, total
,
122 dataset
= mempool_alloc (db
, sizeof (struct dataset
) + req
->key_len
);
123 /* If we cannot permanently store the result, so be it. */
126 dataset
->head
.allocsize
= sizeof (struct dataset
) + req
->key_len
;
127 dataset
->head
.recsize
= total
;
128 dataset
->head
.notfound
= true;
129 dataset
->head
.nreloads
= 0;
130 dataset
->head
.usable
= true;
132 /* Compute the timeout time. */
133 dataset
->head
.timeout
= t
+ db
->negtimeout
;
135 /* This is the reply. */
136 memcpy (&dataset
->resp
, ¬found
, total
);
138 /* Copy the key data. */
139 memcpy (dataset
->strdata
, key
, req
->key_len
);
141 /* If necessary, we also propagate the data to disk. */
145 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
146 msync ((void *) pval
,
147 ((uintptr_t) dataset
& pagesize_m1
)
148 + sizeof (struct dataset
) + req
->key_len
, MS_ASYNC
);
151 /* Now get the lock to safely insert the records. */
152 pthread_rwlock_rdlock (&db
->lock
);
154 if (cache_add (req
->type
, &dataset
->strdata
, req
->key_len
,
155 &dataset
->head
, true, db
, owner
) < 0)
156 /* Ensure the data can be recovered. */
157 dataset
->head
.usable
= false;
159 pthread_rwlock_unlock (&db
->lock
);
161 /* Mark the old entry as obsolete. */
166 ++db
->head
->addfailed
;
171 /* Determine the I/O structure. */
172 size_t h_name_len
= strlen (hst
->h_name
) + 1;
173 size_t h_aliases_cnt
;
174 uint32_t *h_aliases_len
;
175 size_t h_addr_list_cnt
;
179 char *key_copy
= NULL
;
183 /* Determine the number of aliases. */
185 for (cnt
= 0; hst
->h_aliases
[cnt
] != NULL
; ++cnt
)
187 /* Determine the length of all aliases. */
188 h_aliases_len
= (uint32_t *) alloca (h_aliases_cnt
* sizeof (uint32_t));
190 for (cnt
= 0; cnt
< h_aliases_cnt
; ++cnt
)
192 h_aliases_len
[cnt
] = strlen (hst
->h_aliases
[cnt
]) + 1;
193 total
+= h_aliases_len
[cnt
];
196 /* Determine the number of addresses. */
198 for (cnt
= 0; hst
->h_addr_list
[cnt
]; ++cnt
)
201 if (h_addr_list_cnt
== 0)
205 total
+= (sizeof (struct dataset
)
207 + h_aliases_cnt
* sizeof (uint32_t)
208 + h_addr_list_cnt
* hst
->h_length
);
211 /* If we refill the cache, first assume the reconrd did not
212 change. Allocate memory on the cache since it is likely
213 discarded anyway. If it turns out to be necessary to have a
214 new record we can still allocate real memory. */
215 bool alloca_used
= false;
218 /* If the record contains more than one IP address (used for
219 load balancing etc) don't cache the entry. This is something
220 the current cache handling cannot handle and it is more than
221 questionable whether it is worthwhile complicating the cache
222 handling just for handling such a special case. */
223 if (he
== NULL
&& hst
->h_addr_list
[1] == NULL
)
225 dataset
= (struct dataset
*) mempool_alloc (db
,
226 total
+ req
->key_len
);
228 ++db
->head
->addfailed
;
233 /* We cannot permanently add the result in the moment. But
234 we can provide the result as is. Store the data in some
236 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
238 /* We cannot add this record to the permanent database. */
242 dataset
->head
.allocsize
= total
+ req
->key_len
;
243 dataset
->head
.recsize
= total
- offsetof (struct dataset
, resp
);
244 dataset
->head
.notfound
= false;
245 dataset
->head
.nreloads
= he
== NULL
? 0 : (dh
->nreloads
+ 1);
246 dataset
->head
.usable
= true;
248 /* Compute the timeout time. */
249 dataset
->head
.timeout
= t
+ db
->postimeout
;
251 dataset
->resp
.version
= NSCD_VERSION
;
252 dataset
->resp
.found
= 1;
253 dataset
->resp
.h_name_len
= h_name_len
;
254 dataset
->resp
.h_aliases_cnt
= h_aliases_cnt
;
255 dataset
->resp
.h_addrtype
= hst
->h_addrtype
;
256 dataset
->resp
.h_length
= hst
->h_length
;
257 dataset
->resp
.h_addr_list_cnt
= h_addr_list_cnt
;
258 dataset
->resp
.error
= NETDB_SUCCESS
;
260 cp
= dataset
->strdata
;
262 cp
= mempcpy (cp
, hst
->h_name
, h_name_len
);
263 cp
= mempcpy (cp
, h_aliases_len
, h_aliases_cnt
* sizeof (uint32_t));
265 /* The normal addresses first. */
267 for (cnt
= 0; cnt
< h_addr_list_cnt
; ++cnt
)
268 cp
= mempcpy (cp
, hst
->h_addr_list
[cnt
], hst
->h_length
);
270 /* Then the aliases. */
272 for (cnt
= 0; cnt
< h_aliases_cnt
; ++cnt
)
273 cp
= mempcpy (cp
, hst
->h_aliases
[cnt
], h_aliases_len
[cnt
]);
276 == dataset
->strdata
+ total
- offsetof (struct dataset
,
279 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
280 that the answer we get from the NSS does not contain the key
281 itself. This is the case if the resolver is used and the name
282 is extended by the domainnames from /etc/resolv.conf. Therefore
283 we explicitly add the name here. */
284 key_copy
= memcpy (cp
, key
, req
->key_len
);
286 /* Now we can determine whether on refill we have to create a new
292 if (total
+ req
->key_len
== dh
->allocsize
293 && total
- offsetof (struct dataset
, resp
) == dh
->recsize
294 && memcmp (&dataset
->resp
, dh
->data
,
295 dh
->allocsize
- offsetof (struct dataset
, resp
)) == 0)
297 /* The data has not changed. We will just bump the
298 timeout value. Note that the new record has been
299 allocated on the stack and need not be freed. */
300 dh
->timeout
= dataset
->head
.timeout
;
305 /* We have to create a new record. Just allocate
306 appropriate memory and copy it. */
308 = (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
);
311 /* Adjust pointers into the memory block. */
312 addresses
= (char *) newp
+ (addresses
- (char *) dataset
);
313 aliases
= (char *) newp
+ (aliases
- (char *) dataset
);
314 if (key_copy
!= NULL
)
315 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
317 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
321 /* Mark the old record as obsolete. */
327 /* We write the dataset before inserting it to the database
328 since while inserting this thread might block and so would
329 unnecessarily keep the receiver waiting. */
333 if (__builtin_expect (db
->mmap_used
, 1) && !alloca_used
)
335 assert (db
->wr_fd
!= -1);
336 assert ((char *) &dataset
->resp
> (char *) db
->data
);
337 assert ((char *) &dataset
->resp
- (char *) db
->head
339 <= (sizeof (struct database_pers_head
)
340 + db
->head
->module
* sizeof (ref_t
)
341 + db
->head
->data_size
));
342 written
= sendfileall (fd
, db
->wr_fd
,
343 (char *) &dataset
->resp
344 - (char *) db
->head
, total
);
345 # ifndef __ASSUME_SENDFILE
346 if (written
== -1 && errno
== ENOSYS
)
351 # ifndef __ASSUME_SENDFILE
355 written
= writeall (fd
, &dataset
->resp
, total
);
358 /* Add the record to the database. But only if it has not been
361 If the record contains more than one IP address (used for
362 load balancing etc) don't cache the entry. This is something
363 the current cache handling cannot handle and it is more than
364 questionable whether it is worthwhile complicating the cache
365 handling just for handling such a special case. */
368 /* If necessary, we also propagate the data to disk. */
372 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
373 msync ((void *) pval
,
374 ((uintptr_t) dataset
& pagesize_m1
)
375 + total
+ req
->key_len
, MS_ASYNC
);
378 addr_list_type
= (hst
->h_length
== NS_INADDRSZ
379 ? GETHOSTBYADDR
: GETHOSTBYADDRv6
);
381 /* Now get the lock to safely insert the records. */
382 pthread_rwlock_rdlock (&db
->lock
);
384 /* NB: the following code is really complicated. It has
385 seemlingly duplicated code paths which do the same. The
386 problem is that we always must add the hash table entry
387 with the FIRST flag set first. Otherwise we get dangling
388 pointers in case memory allocation fails. */
389 assert (hst
->h_addr_list
[1] == NULL
);
391 /* Avoid adding names if more than one address is available. See
392 above for more info. */
393 assert (req
->type
== GETHOSTBYNAME
394 || req
->type
== GETHOSTBYNAMEv6
395 || req
->type
== GETHOSTBYADDR
396 || req
->type
== GETHOSTBYADDRv6
);
398 if (cache_add (req
->type
, key_copy
, req
->key_len
,
399 &dataset
->head
, true, db
, owner
) < 0)
400 /* Could not allocate memory. Make sure the
401 data gets discarded. */
402 dataset
->head
.usable
= false;
404 pthread_rwlock_unlock (&db
->lock
);
408 if (__builtin_expect (written
!= total
, 0) && debug_level
> 0)
411 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
412 strerror_r (errno
, buf
, sizeof (buf
)));
418 lookup (int type
, void *key
, struct hostent
*resultbufp
, char *buffer
,
419 size_t buflen
, struct hostent
**hst
)
421 if (type
== GETHOSTBYNAME
)
422 return __gethostbyname2_r (key
, AF_INET
, resultbufp
, buffer
, buflen
, hst
,
424 if (type
== GETHOSTBYNAMEv6
)
425 return __gethostbyname2_r (key
, AF_INET6
, resultbufp
, buffer
, buflen
, hst
,
427 if (type
== GETHOSTBYADDR
)
428 return __gethostbyaddr_r (key
, NS_INADDRSZ
, AF_INET
, resultbufp
, buffer
,
429 buflen
, hst
, &h_errno
);
430 return __gethostbyaddr_r (key
, NS_IN6ADDRSZ
, AF_INET6
, resultbufp
, buffer
,
431 buflen
, hst
, &h_errno
);
436 addhstbyX (struct database_dyn
*db
, int fd
, request_header
*req
,
437 void *key
, uid_t uid
, struct hashentry
*he
, struct datahead
*dh
)
439 /* Search for the entry matching the key. Please note that we don't
440 look again in the table whether the dataset is now available. We
441 simply insert it. It does not matter if it is in there twice. The
442 pruning function only will look at the timestamp. */
444 char *buffer
= (char *) alloca (buflen
);
445 struct hostent resultbuf
;
447 bool use_malloc
= false;
450 if (__builtin_expect (debug_level
> 0, 0))
453 char buf
[INET6_ADDRSTRLEN
+ 1];
454 if (req
->type
== GETHOSTBYNAME
|| req
->type
== GETHOSTBYNAMEv6
)
457 str
= inet_ntop (req
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
458 key
, buf
, sizeof (buf
));
461 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) str
);
463 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) str
);
470 oldeuid
= geteuid ();
471 pthread_seteuid_np (uid
);
475 while (lookup (req
->type
, key
, &resultbuf
, buffer
, buflen
, &hst
) != 0
476 && h_errno
== NETDB_INTERNAL
477 && (errval
= errno
) == ERANGE
)
479 char *old_buffer
= buffer
;
482 if (__builtin_expect (buflen
> 32768, 0))
485 buffer
= (char *) realloc (use_malloc
? buffer
: NULL
, buflen
);
488 /* We ran out of memory. We cannot do anything but
489 sending a negative response. In reality this should
494 /* We set the error to indicate this is (possibly) a
495 temporary error and that it does not mean the entry
496 is not available at all. */
503 /* Allocate a new buffer on the stack. If possible combine it
504 with the previously allocated buffer. */
505 buffer
= (char *) extend_alloca (buffer
, buflen
, 2 * buflen
);
510 pthread_seteuid_np (oldeuid
);
513 cache_addhst (db
, fd
, req
, key
, hst
, uid
, he
, dh
,
514 h_errno
== TRY_AGAIN
? errval
: 0);
522 addhstbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
523 void *key
, uid_t uid
)
525 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
530 readdhstbyname (struct database_dyn
*db
, struct hashentry
*he
,
535 .type
= GETHOSTBYNAME
,
539 addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
544 addhstbyaddr (struct database_dyn
*db
, int fd
, request_header
*req
,
545 void *key
, uid_t uid
)
547 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
552 readdhstbyaddr (struct database_dyn
*db
, struct hashentry
*he
,
557 .type
= GETHOSTBYADDR
,
561 addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
566 addhstbynamev6 (struct database_dyn
*db
, int fd
, request_header
*req
,
567 void *key
, uid_t uid
)
569 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
574 readdhstbynamev6 (struct database_dyn
*db
, struct hashentry
*he
,
579 .type
= GETHOSTBYNAMEv6
,
583 addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
588 addhstbyaddrv6 (struct database_dyn
*db
, int fd
, request_header
*req
,
589 void *key
, uid_t uid
)
591 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
596 readdhstbyaddrv6 (struct database_dyn
*db
, struct hashentry
*he
,
601 .type
= GETHOSTBYADDRv6
,
605 addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);