1 /* Copyright (C) 1996-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 /* The following is an ugly trick to avoid a prototype declaration for
24 #define _nss_nis_endhostent _nss_nis_endhostent_XXX
26 #undef _nss_nis_endhostent
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <resolv/resolv-internal.h>
31 #include <libc-lock.h>
32 #include <rpcsvc/yp.h>
33 #include <rpcsvc/ypclnt.h>
37 /* Get implementation for some internal functions. */
38 #include <resolv/mapv4v6addr.h>
40 #define ENTNAME hostent
41 #define DATABASE "hosts"
44 #define EXTRA_ARGS , af, flags
45 #define EXTRA_ARGS_DECL , int af, int flags
47 #define ENTDATA hostent_data
50 unsigned char host_addr
[16]; /* IPv4 or IPv6 address. */
51 char *h_addr_ptrs
[2]; /* Points to that and null terminator. */
54 #define TRAILING_LIST_MEMBER h_aliases
55 #define TRAILING_LIST_SEPARATOR_P isspace
56 #include <nss/nss_files/files-parse.c>
62 STRING_FIELD (addr
, isspace
, 1);
64 assert (af
== AF_INET
|| af
== AF_INET6
|| af
== AF_UNSPEC
);
67 if (af
!= AF_INET6
&& inet_pton (AF_INET
, addr
, entdata
->host_addr
) > 0)
69 assert ((flags
& AI_V4MAPPED
) == 0 || af
!= AF_UNSPEC
);
70 if (flags
& AI_V4MAPPED
)
72 map_v4v6_address ((char *) entdata
->host_addr
,
73 (char *) entdata
->host_addr
);
74 result
->h_addrtype
= AF_INET6
;
75 result
->h_length
= IN6ADDRSZ
;
79 result
->h_addrtype
= AF_INET
;
80 result
->h_length
= INADDRSZ
;
83 else if (af
!= AF_INET
84 && inet_pton (AF_INET6
, addr
, entdata
->host_addr
) > 0)
86 result
->h_addrtype
= AF_INET6
;
87 result
->h_length
= IN6ADDRSZ
;
90 /* Illegal address: ignore line. */
93 /* Store a pointer to the address in the expected form. */
94 entdata
->h_addr_ptrs
[0] = (char *) entdata
->host_addr
;
95 entdata
->h_addr_ptrs
[1] = NULL
;
96 result
->h_addr_list
= entdata
->h_addr_ptrs
;
98 STRING_FIELD (result
->h_name
, isspace
, 1);
102 __libc_lock_define_initialized (static, lock
)
104 static bool_t new_start
= 1;
105 static char *oldkey
= NULL
;
106 static int oldkeylen
= 0;
110 _nss_nis_sethostent (int stayopen
)
112 __libc_lock_lock (lock
);
122 __libc_lock_unlock (lock
);
124 return NSS_STATUS_SUCCESS
;
126 /* Make _nss_nis_endhostent an alias of _nss_nis_sethostent. We do this
127 even though the prototypes don't match. The argument of sethostent
128 is used so this makes no difference. */
129 strong_alias (_nss_nis_sethostent
, _nss_nis_endhostent
)
132 /* The calling function always need to get a lock first. */
133 static enum nss_status
134 internal_nis_gethostent_r (struct hostent
*host
, char *buffer
,
135 size_t buflen
, int *errnop
, int *h_errnop
,
139 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
140 return NSS_STATUS_UNAVAIL
;
142 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
145 struct parser_data
*data
= (void *) buffer
;
146 if (__glibc_unlikely (buflen
< sizeof *data
+ 1 + pad
))
149 *h_errnop
= NETDB_INTERNAL
;
150 return NSS_STATUS_TRYAGAIN
;
154 /* Get the next entry until we found a correct one. */
155 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
165 yperr
= yp_first (domain
, "hosts.byname", &outkey
, &keylen
, &result
,
168 yperr
= yp_next (domain
, "hosts.byname", oldkey
, oldkeylen
, &outkey
,
169 &keylen
, &result
, &len
);
171 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
173 enum nss_status retval
= yperr2nss (yperr
);
177 case NSS_STATUS_TRYAGAIN
:
179 *h_errnop
= TRY_AGAIN
;
181 case NSS_STATUS_NOTFOUND
:
182 *h_errnop
= HOST_NOT_FOUND
;
185 *h_errnop
= NO_RECOVERY
;
191 if (__glibc_unlikely ((size_t) (len
+ 1) > linebuflen
))
194 *h_errnop
= NETDB_INTERNAL
;
196 return NSS_STATUS_TRYAGAIN
;
199 char *p
= strncpy (data
->linebuffer
, result
, len
);
200 data
->linebuffer
[len
] = '\0';
205 parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
, flags
);
206 if (__glibc_unlikely (parse_res
== -1))
209 *h_errnop
= NETDB_INTERNAL
;
211 return NSS_STATUS_TRYAGAIN
;
220 *h_errnop
= NETDB_SUCCESS
;
221 return NSS_STATUS_SUCCESS
;
226 _nss_nis_gethostent_r (struct hostent
*host
, char *buffer
, size_t buflen
,
227 int *errnop
, int *h_errnop
)
229 enum nss_status status
;
231 __libc_lock_lock (lock
);
233 status
= internal_nis_gethostent_r (host
, buffer
, buflen
, errnop
, h_errnop
,
234 (res_use_inet6 () ? AF_INET6
: AF_INET
),
235 (res_use_inet6 () ? AI_V4MAPPED
: 0 ));
237 __libc_lock_unlock (lock
);
243 static enum nss_status
244 internal_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
245 char *buffer
, size_t buflen
, int *errnop
,
246 int *h_errnop
, int flags
)
248 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
251 struct parser_data
*data
= (void *) buffer
;
256 return NSS_STATUS_UNAVAIL
;
260 if (yp_get_default_domain (&domain
))
261 return NSS_STATUS_UNAVAIL
;
263 if (buflen
< sizeof *data
+ 1 + pad
)
265 *h_errnop
= NETDB_INTERNAL
;
267 return NSS_STATUS_TRYAGAIN
;
271 /* Convert name to lowercase. */
272 size_t namlen
= strlen (name
);
273 /* Limit name length to the maximum size of an RPC packet. */
274 if (namlen
> UDPMSGSIZE
)
277 return NSS_STATUS_UNAVAIL
;
280 char name2
[namlen
+ 1];
283 for (i
= 0; i
< namlen
; ++i
)
284 name2
[i
] = tolower (name
[i
]);
289 int yperr
= yp_match (domain
, "hosts.byname", name2
, namlen
, &result
, &len
);
291 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
293 enum nss_status retval
= yperr2nss (yperr
);
295 if (retval
== NSS_STATUS_TRYAGAIN
)
297 *h_errnop
= TRY_AGAIN
;
300 if (retval
== NSS_STATUS_NOTFOUND
)
301 *h_errnop
= HOST_NOT_FOUND
;
305 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
306 if (__glibc_unlikely ((size_t) (len
+ 1) > linebuflen
))
309 *h_errnop
= NETDB_INTERNAL
;
311 return NSS_STATUS_TRYAGAIN
;
314 char *p
= strncpy (data
->linebuffer
, result
, len
);
315 data
->linebuffer
[len
] = '\0';
320 int parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
, flags
);
322 if (__glibc_unlikely (parse_res
< 1 || host
->h_addrtype
!= af
))
326 *h_errnop
= NETDB_INTERNAL
;
327 return NSS_STATUS_TRYAGAIN
;
331 *h_errnop
= HOST_NOT_FOUND
;
332 return NSS_STATUS_NOTFOUND
;
336 *h_errnop
= NETDB_SUCCESS
;
337 return NSS_STATUS_SUCCESS
;
342 _nss_nis_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
343 char *buffer
, size_t buflen
, int *errnop
,
346 if (af
!= AF_INET
&& af
!= AF_INET6
)
348 *h_errnop
= HOST_NOT_FOUND
;
349 return NSS_STATUS_NOTFOUND
;
352 return internal_gethostbyname2_r (name
, af
, host
, buffer
, buflen
, errnop
,
354 (res_use_inet6 () ? AI_V4MAPPED
: 0));
359 _nss_nis_gethostbyname_r (const char *name
, struct hostent
*host
, char *buffer
,
360 size_t buflen
, int *errnop
, int *h_errnop
)
362 if (res_use_inet6 ())
364 enum nss_status status
;
366 status
= internal_gethostbyname2_r (name
, AF_INET6
, host
, buffer
, buflen
,
367 errnop
, h_errnop
, AI_V4MAPPED
);
368 if (status
== NSS_STATUS_SUCCESS
)
372 return internal_gethostbyname2_r (name
, AF_INET
, host
, buffer
, buflen
,
373 errnop
, h_errnop
, 0);
378 _nss_nis_gethostbyaddr_r (const void *addr
, socklen_t addrlen
, int af
,
379 struct hostent
*host
, char *buffer
, size_t buflen
,
380 int *errnop
, int *h_errnop
)
383 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
384 return NSS_STATUS_UNAVAIL
;
386 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
389 struct parser_data
*data
= (void *) buffer
;
390 if (__glibc_unlikely (buflen
< sizeof *data
+ 1 + pad
))
393 *h_errnop
= NETDB_INTERNAL
;
394 return NSS_STATUS_TRYAGAIN
;
398 char *buf
= inet_ntoa (*(const struct in_addr
*) addr
);
402 int yperr
= yp_match (domain
, "hosts.byaddr", buf
, strlen (buf
), &result
,
405 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
407 enum nss_status retval
= yperr2nss (yperr
);
409 if (retval
== NSS_STATUS_TRYAGAIN
)
411 *h_errnop
= TRY_AGAIN
;
414 else if (retval
== NSS_STATUS_NOTFOUND
)
415 *h_errnop
= HOST_NOT_FOUND
;
420 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
421 if (__glibc_unlikely ((size_t) (len
+ 1) > linebuflen
))
425 *h_errnop
= NETDB_INTERNAL
;
426 return NSS_STATUS_TRYAGAIN
;
429 char *p
= strncpy (data
->linebuffer
, result
, len
);
430 data
->linebuffer
[len
] = '\0';
435 int parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
,
436 (res_use_inet6 () ? AI_V4MAPPED
: 0));
437 if (__glibc_unlikely (parse_res
< 1))
441 *h_errnop
= NETDB_INTERNAL
;
442 return NSS_STATUS_TRYAGAIN
;
446 *h_errnop
= HOST_NOT_FOUND
;
447 return NSS_STATUS_NOTFOUND
;
451 *h_errnop
= NETDB_SUCCESS
;
452 return NSS_STATUS_SUCCESS
;
457 _nss_nis_gethostbyname4_r (const char *name
, struct gaih_addrtuple
**pat
,
458 char *buffer
, size_t buflen
, int *errnop
,
459 int *herrnop
, int32_t *ttlp
)
462 if (yp_get_default_domain (&domain
))
465 return NSS_STATUS_UNAVAIL
;
468 /* Convert name to lowercase. */
469 size_t namlen
= strlen (name
);
470 /* Limit name length to the maximum size of an RPC packet. */
471 if (namlen
> UDPMSGSIZE
)
474 return NSS_STATUS_UNAVAIL
;
477 char name2
[namlen
+ 1];
480 for (i
= 0; i
< namlen
; ++i
)
481 name2
[i
] = tolower (name
[i
]);
486 int yperr
= yp_match (domain
, "hosts.byname", name2
, namlen
, &result
, &len
);
488 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
490 enum nss_status retval
= yperr2nss (yperr
);
492 if (retval
== NSS_STATUS_TRYAGAIN
)
494 *herrnop
= TRY_AGAIN
;
497 if (retval
== NSS_STATUS_NOTFOUND
)
498 *herrnop
= HOST_NOT_FOUND
;
504 uintptr_t pad
= (-(uintptr_t) buffer
505 % __alignof__ (struct gaih_addrtuple
));
507 buflen
= buflen
> pad
? buflen
- pad
: 0;
509 if (__glibc_unlikely (buflen
< sizeof (struct gaih_addrtuple
)))
514 *herrnop
= NETDB_INTERNAL
;
515 return NSS_STATUS_TRYAGAIN
;
518 *pat
= (struct gaih_addrtuple
*) buffer
;
519 buffer
+= sizeof (struct gaih_addrtuple
);
520 buflen
-= sizeof (struct gaih_addrtuple
);
523 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
526 struct parser_data
*data
= (void *) buffer
;
528 if (__glibc_unlikely (buflen
< sizeof *data
+ 1 + pad
))
533 int parse_res
= parse_line (result
, &host
, data
, buflen
, errnop
, AF_UNSPEC
,
535 if (__glibc_unlikely (parse_res
< 1))
539 *herrnop
= NETDB_INTERNAL
;
540 return NSS_STATUS_TRYAGAIN
;
544 *herrnop
= HOST_NOT_FOUND
;
545 return NSS_STATUS_NOTFOUND
;
550 (*pat
)->family
= host
.h_addrtype
;
551 memcpy ((*pat
)->addr
, host
.h_addr_list
[0], host
.h_length
);
553 assert (host
.h_addr_list
[1] == NULL
);
555 /* Undo the alignment for parser_data. */
559 size_t h_name_len
= strlen (host
.h_name
) + 1;
560 if (h_name_len
>= buflen
)
562 (*pat
)->name
= memcpy (buffer
, host
.h_name
, h_name_len
);
566 return NSS_STATUS_SUCCESS
;