1 /* Copyright (C) 1996-2000, 2002, 2003, 2006, 2007, 2008
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
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, see
18 <http://www.gnu.org/licenses/>. */
23 /* The following is an ugly trick to avoid a prototype declaration for
25 #define _nss_nis_endhostent _nss_nis_endhostent_XXX
27 #undef _nss_nis_endhostent
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
32 #include <bits/libc-lock.h>
33 #include <rpcsvc/yp.h>
34 #include <rpcsvc/ypclnt.h>
38 /* Get implementation for some internal functions. */
39 #include <resolv/mapv4v6addr.h>
41 #define ENTNAME hostent
42 #define DATABASE "hosts"
45 #define EXTRA_ARGS , af, flags
46 #define EXTRA_ARGS_DECL , int af, int flags
48 #define ENTDATA hostent_data
51 unsigned char host_addr
[16]; /* IPv4 or IPv6 address. */
52 char *h_addr_ptrs
[2]; /* Points to that and null terminator. */
55 #define TRAILING_LIST_MEMBER h_aliases
56 #define TRAILING_LIST_SEPARATOR_P isspace
57 #include <nss/nss_files/files-parse.c>
63 STRING_FIELD (addr
, isspace
, 1);
65 assert (af
== AF_INET
|| af
== AF_INET6
|| af
== AF_UNSPEC
);
68 if (af
!= AF_INET6
&& inet_pton (AF_INET
, addr
, entdata
->host_addr
) > 0)
70 assert ((flags
& AI_V4MAPPED
) == 0 || af
!= AF_UNSPEC
);
71 if (flags
& AI_V4MAPPED
)
73 map_v4v6_address ((char *) entdata
->host_addr
,
74 (char *) entdata
->host_addr
);
75 result
->h_addrtype
= AF_INET6
;
76 result
->h_length
= IN6ADDRSZ
;
80 result
->h_addrtype
= AF_INET
;
81 result
->h_length
= INADDRSZ
;
84 else if (af
!= AF_INET
85 && inet_pton (AF_INET6
, addr
, entdata
->host_addr
) > 0)
87 result
->h_addrtype
= AF_INET6
;
88 result
->h_length
= IN6ADDRSZ
;
91 /* Illegal address: ignore line. */
94 /* Store a pointer to the address in the expected form. */
95 entdata
->h_addr_ptrs
[0] = (char *) entdata
->host_addr
;
96 entdata
->h_addr_ptrs
[1] = NULL
;
97 result
->h_addr_list
= entdata
->h_addr_ptrs
;
99 STRING_FIELD (result
->h_name
, isspace
, 1);
103 __libc_lock_define_initialized (static, lock
)
105 static bool_t new_start
= 1;
106 static char *oldkey
= NULL
;
107 static int oldkeylen
= 0;
111 _nss_nis_sethostent (int stayopen
)
113 __libc_lock_lock (lock
);
123 __libc_lock_unlock (lock
);
125 return NSS_STATUS_SUCCESS
;
127 /* Make _nss_nis_endhostent an alias of _nss_nis_sethostent. We do this
128 even though the prototypes don't match. The argument of sethostent
129 is used so this makes no difference. */
130 strong_alias (_nss_nis_sethostent
, _nss_nis_endhostent
)
133 /* The calling function always need to get a lock first. */
134 static enum nss_status
135 internal_nis_gethostent_r (struct hostent
*host
, char *buffer
,
136 size_t buflen
, int *errnop
, int *h_errnop
,
140 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
141 return NSS_STATUS_UNAVAIL
;
143 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
146 struct parser_data
*data
= (void *) buffer
;
147 if (__builtin_expect (buflen
< sizeof *data
+ 1 + pad
, 0))
150 *h_errnop
= NETDB_INTERNAL
;
151 return NSS_STATUS_TRYAGAIN
;
155 /* Get the next entry until we found a correct one. */
156 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
166 yperr
= yp_first (domain
, "hosts.byname", &outkey
, &keylen
, &result
,
169 yperr
= yp_next (domain
, "hosts.byname", oldkey
, oldkeylen
, &outkey
,
170 &keylen
, &result
, &len
);
172 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
174 enum nss_status retval
= yperr2nss (yperr
);
178 case NSS_STATUS_TRYAGAIN
:
180 *h_errnop
= TRY_AGAIN
;
182 case NSS_STATUS_NOTFOUND
:
183 *h_errnop
= HOST_NOT_FOUND
;
186 *h_errnop
= NO_RECOVERY
;
192 if (__builtin_expect ((size_t) (len
+ 1) > linebuflen
, 0))
195 *h_errnop
= NETDB_INTERNAL
;
197 return NSS_STATUS_TRYAGAIN
;
200 char *p
= strncpy (data
->linebuffer
, result
, len
);
201 data
->linebuffer
[len
] = '\0';
206 parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
, flags
);
207 if (__builtin_expect (parse_res
== -1, 0))
210 *h_errnop
= NETDB_INTERNAL
;
212 return NSS_STATUS_TRYAGAIN
;
221 *h_errnop
= NETDB_SUCCESS
;
222 return NSS_STATUS_SUCCESS
;
227 _nss_nis_gethostent_r (struct hostent
*host
, char *buffer
, size_t buflen
,
228 int *errnop
, int *h_errnop
)
230 enum nss_status status
;
232 __libc_lock_lock (lock
);
234 status
= internal_nis_gethostent_r (host
, buffer
, buflen
, errnop
, h_errnop
,
235 ((_res
.options
& RES_USE_INET6
) ? AF_INET6
: AF_INET
),
236 ((_res
.options
& RES_USE_INET6
) ? AI_V4MAPPED
: 0 ));
238 __libc_lock_unlock (lock
);
244 static enum nss_status
245 internal_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
246 char *buffer
, size_t buflen
, int *errnop
,
247 int *h_errnop
, int flags
)
249 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
252 struct parser_data
*data
= (void *) buffer
;
257 return NSS_STATUS_UNAVAIL
;
261 if (yp_get_default_domain (&domain
))
262 return NSS_STATUS_UNAVAIL
;
264 if (buflen
< sizeof *data
+ 1 + pad
)
266 *h_errnop
= NETDB_INTERNAL
;
268 return NSS_STATUS_TRYAGAIN
;
272 /* Convert name to lowercase. */
273 size_t namlen
= strlen (name
);
274 char name2
[namlen
+ 1];
277 for (i
= 0; i
< namlen
; ++i
)
278 name2
[i
] = tolower (name
[i
]);
283 int yperr
= yp_match (domain
, "hosts.byname", name2
, namlen
, &result
, &len
);
285 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
287 enum nss_status retval
= yperr2nss (yperr
);
289 if (retval
== NSS_STATUS_TRYAGAIN
)
291 *h_errnop
= TRY_AGAIN
;
294 if (retval
== NSS_STATUS_NOTFOUND
)
295 *h_errnop
= HOST_NOT_FOUND
;
299 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
300 if (__builtin_expect ((size_t) (len
+ 1) > linebuflen
, 0))
303 *h_errnop
= NETDB_INTERNAL
;
305 return NSS_STATUS_TRYAGAIN
;
308 char *p
= strncpy (data
->linebuffer
, result
, len
);
309 data
->linebuffer
[len
] = '\0';
314 int parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
, flags
);
316 if (__builtin_expect (parse_res
< 1 || host
->h_addrtype
!= af
, 0))
320 *h_errnop
= NETDB_INTERNAL
;
321 return NSS_STATUS_TRYAGAIN
;
325 *h_errnop
= HOST_NOT_FOUND
;
326 return NSS_STATUS_NOTFOUND
;
330 *h_errnop
= NETDB_SUCCESS
;
331 return NSS_STATUS_SUCCESS
;
336 _nss_nis_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
337 char *buffer
, size_t buflen
, int *errnop
,
340 if (af
!= AF_INET
&& af
!= AF_INET6
)
342 *h_errnop
= HOST_NOT_FOUND
;
343 return NSS_STATUS_NOTFOUND
;
346 return internal_gethostbyname2_r (name
, af
, host
, buffer
, buflen
, errnop
,
348 ((_res
.options
& RES_USE_INET6
) ? AI_V4MAPPED
: 0));
353 _nss_nis_gethostbyname_r (const char *name
, struct hostent
*host
, char *buffer
,
354 size_t buflen
, int *errnop
, int *h_errnop
)
356 if (_res
.options
& RES_USE_INET6
)
358 enum nss_status status
;
360 status
= internal_gethostbyname2_r (name
, AF_INET6
, host
, buffer
, buflen
,
361 errnop
, h_errnop
, AI_V4MAPPED
);
362 if (status
== NSS_STATUS_SUCCESS
)
366 return internal_gethostbyname2_r (name
, AF_INET
, host
, buffer
, buflen
,
367 errnop
, h_errnop
, 0);
372 _nss_nis_gethostbyaddr_r (const void *addr
, socklen_t addrlen
, int af
,
373 struct hostent
*host
, char *buffer
, size_t buflen
,
374 int *errnop
, int *h_errnop
)
377 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
378 return NSS_STATUS_UNAVAIL
;
380 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
383 struct parser_data
*data
= (void *) buffer
;
384 if (__builtin_expect (buflen
< sizeof *data
+ 1 + pad
, 0))
387 *h_errnop
= NETDB_INTERNAL
;
388 return NSS_STATUS_TRYAGAIN
;
392 char *buf
= inet_ntoa (*(const struct in_addr
*) addr
);
396 int yperr
= yp_match (domain
, "hosts.byaddr", buf
, strlen (buf
), &result
,
399 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
401 enum nss_status retval
= yperr2nss (yperr
);
403 if (retval
== NSS_STATUS_TRYAGAIN
)
405 *h_errnop
= TRY_AGAIN
;
408 else if (retval
== NSS_STATUS_NOTFOUND
)
409 *h_errnop
= HOST_NOT_FOUND
;
414 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
415 if (__builtin_expect ((size_t) (len
+ 1) > linebuflen
, 0))
419 *h_errnop
= NETDB_INTERNAL
;
420 return NSS_STATUS_TRYAGAIN
;
423 char *p
= strncpy (data
->linebuffer
, result
, len
);
424 data
->linebuffer
[len
] = '\0';
429 int parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
,
430 ((_res
.options
& RES_USE_INET6
)
432 if (__builtin_expect (parse_res
< 1, 0))
436 *h_errnop
= NETDB_INTERNAL
;
437 return NSS_STATUS_TRYAGAIN
;
441 *h_errnop
= HOST_NOT_FOUND
;
442 return NSS_STATUS_NOTFOUND
;
446 *h_errnop
= NETDB_SUCCESS
;
447 return NSS_STATUS_SUCCESS
;
452 _nss_nis_gethostbyname4_r (const char *name
, struct gaih_addrtuple
**pat
,
453 char *buffer
, size_t buflen
, int *errnop
,
454 int *herrnop
, int32_t *ttlp
)
457 if (yp_get_default_domain (&domain
))
460 return NSS_STATUS_UNAVAIL
;
463 /* Convert name to lowercase. */
464 size_t namlen
= strlen (name
);
465 char name2
[namlen
+ 1];
468 for (i
= 0; i
< namlen
; ++i
)
469 name2
[i
] = tolower (name
[i
]);
474 int yperr
= yp_match (domain
, "hosts.byname", name2
, namlen
, &result
, &len
);
476 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
478 enum nss_status retval
= yperr2nss (yperr
);
480 if (retval
== NSS_STATUS_TRYAGAIN
)
482 *herrnop
= TRY_AGAIN
;
485 if (retval
== NSS_STATUS_NOTFOUND
)
486 *herrnop
= HOST_NOT_FOUND
;
492 uintptr_t pad
= (-(uintptr_t) buffer
493 % __alignof__ (struct gaih_addrtuple
));
495 buflen
= buflen
> pad
? buflen
- pad
: 0;
497 if (__builtin_expect (buflen
< sizeof (struct gaih_addrtuple
), 0))
502 *herrnop
= NETDB_INTERNAL
;
503 return NSS_STATUS_TRYAGAIN
;
506 *pat
= (struct gaih_addrtuple
*) buffer
;
507 buffer
+= sizeof (struct gaih_addrtuple
);
508 buflen
-= sizeof (struct gaih_addrtuple
);
511 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
514 struct parser_data
*data
= (void *) buffer
;
516 if (__builtin_expect (buflen
< sizeof *data
+ 1 + pad
, 0))
521 int parse_res
= parse_line (result
, &host
, data
, buflen
, errnop
, AF_UNSPEC
,
523 if (__builtin_expect (parse_res
< 1, 0))
527 *herrnop
= NETDB_INTERNAL
;
528 return NSS_STATUS_TRYAGAIN
;
532 *herrnop
= HOST_NOT_FOUND
;
533 return NSS_STATUS_NOTFOUND
;
538 (*pat
)->family
= host
.h_addrtype
;
539 memcpy ((*pat
)->addr
, host
.h_addr_list
[0], host
.h_length
);
541 assert (host
.h_addr_list
[1] == NULL
);
543 /* Undo the alignment for parser_data. */
547 size_t h_name_len
= strlen (host
.h_name
) + 1;
548 if (h_name_len
>= buflen
)
550 (*pat
)->name
= memcpy (buffer
, host
.h_name
, h_name_len
);
554 return NSS_STATUS_SUCCESS
;