1 /* Copyright (C) 1996-2000, 2002, 2003, 2006, 2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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>
31 #include <bits/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);
65 if (af
== AF_INET
&& inet_pton (AF_INET
, addr
, entdata
->host_addr
) > 0)
67 if (flags
& AI_V4MAPPED
)
69 map_v4v6_address ((char *) entdata
->host_addr
,
70 (char *) entdata
->host_addr
);
71 result
->h_addrtype
= AF_INET6
;
72 result
->h_length
= IN6ADDRSZ
;
76 result
->h_addrtype
= AF_INET
;
77 result
->h_length
= INADDRSZ
;
80 else if (af
== AF_INET6
81 && inet_pton (AF_INET6
, addr
, entdata
->host_addr
) > 0)
83 result
->h_addrtype
= AF_INET6
;
84 result
->h_length
= IN6ADDRSZ
;
87 /* Illegal address: ignore line. */
90 /* Store a pointer to the address in the expected form. */
91 entdata
->h_addr_ptrs
[0] = entdata
->host_addr
;
92 entdata
->h_addr_ptrs
[1] = NULL
;
93 result
->h_addr_list
= entdata
->h_addr_ptrs
;
95 STRING_FIELD (result
->h_name
, isspace
, 1);
99 __libc_lock_define_initialized (static, lock
)
101 static bool_t new_start
= 1;
102 static char *oldkey
= NULL
;
103 static int oldkeylen
= 0;
106 _nss_nis_sethostent (int stayopen
)
108 __libc_lock_lock (lock
);
118 __libc_lock_unlock (lock
);
120 return NSS_STATUS_SUCCESS
;
122 /* Make _nss_nis_endhostent an alias of _nss_nis_sethostent. We do this
123 even though the prototypes don't match. The argument of sethostent
124 is used so this makes no difference. */
125 strong_alias (_nss_nis_sethostent
, _nss_nis_endhostent
)
127 /* The calling function always need to get a lock first. */
128 static enum nss_status
129 internal_nis_gethostent_r (struct hostent
*host
, char *buffer
,
130 size_t buflen
, int *errnop
, int *h_errnop
,
134 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
135 return NSS_STATUS_UNAVAIL
;
137 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
140 struct parser_data
*data
= (void *) buffer
;
141 if (__builtin_expect (buflen
< sizeof *data
+ 1 + pad
, 0))
144 *h_errnop
= NETDB_INTERNAL
;
145 return NSS_STATUS_TRYAGAIN
;
149 /* Get the next entry until we found a correct one. */
150 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
160 yperr
= yp_first (domain
, "hosts.byname", &outkey
, &keylen
, &result
,
163 yperr
= yp_next (domain
, "hosts.byname", oldkey
, oldkeylen
, &outkey
,
164 &keylen
, &result
, &len
);
166 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
168 enum nss_status retval
= yperr2nss (yperr
);
172 case NSS_STATUS_TRYAGAIN
:
174 *h_errnop
= TRY_AGAIN
;
176 case NSS_STATUS_NOTFOUND
:
177 *h_errnop
= HOST_NOT_FOUND
;
180 *h_errnop
= NO_RECOVERY
;
186 if (__builtin_expect ((size_t) (len
+ 1) > linebuflen
, 0))
189 *h_errnop
= NETDB_INTERNAL
;
191 return NSS_STATUS_TRYAGAIN
;
194 char *p
= strncpy (data
->linebuffer
, result
, len
);
195 data
->linebuffer
[len
] = '\0';
200 parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
, flags
);
201 if (__builtin_expect (parse_res
== -1, 0))
204 *h_errnop
= NETDB_INTERNAL
;
206 return NSS_STATUS_TRYAGAIN
;
215 *h_errnop
= NETDB_SUCCESS
;
216 return NSS_STATUS_SUCCESS
;
220 _nss_nis_gethostent_r (struct hostent
*host
, char *buffer
, size_t buflen
,
221 int *errnop
, int *h_errnop
)
223 enum nss_status status
;
225 __libc_lock_lock (lock
);
227 status
= internal_nis_gethostent_r (host
, buffer
, buflen
, errnop
, h_errnop
,
228 ((_res
.options
& RES_USE_INET6
) ? AF_INET6
: AF_INET
),
229 ((_res
.options
& RES_USE_INET6
) ? AI_V4MAPPED
: 0 ));
231 __libc_lock_unlock (lock
);
236 static enum nss_status
237 internal_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
238 char *buffer
, size_t buflen
, int *errnop
,
239 int *h_errnop
, int flags
)
241 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
244 struct parser_data
*data
= (void *) buffer
;
249 return NSS_STATUS_UNAVAIL
;
253 if (yp_get_default_domain (&domain
))
254 return NSS_STATUS_UNAVAIL
;
256 if (buflen
< sizeof *data
+ 1 + pad
)
258 *h_errnop
= NETDB_INTERNAL
;
260 return NSS_STATUS_TRYAGAIN
;
264 /* Convert name to lowercase. */
265 size_t namlen
= strlen (name
);
266 char name2
[namlen
+ 1];
269 for (i
= 0; i
< namlen
; ++i
)
270 name2
[i
] = tolower (name
[i
]);
275 int yperr
= yp_match (domain
, "hosts.byname", name2
, namlen
, &result
, &len
);
277 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
279 enum nss_status retval
= yperr2nss (yperr
);
281 if (retval
== NSS_STATUS_TRYAGAIN
)
283 *h_errnop
= TRY_AGAIN
;
286 if (retval
== NSS_STATUS_NOTFOUND
)
287 *h_errnop
= HOST_NOT_FOUND
;
291 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
292 if (__builtin_expect ((size_t) (len
+ 1) > linebuflen
, 0))
295 *h_errnop
= NETDB_INTERNAL
;
297 return NSS_STATUS_TRYAGAIN
;
300 char *p
= strncpy (data
->linebuffer
, result
, len
);
301 data
->linebuffer
[len
] = '\0';
306 int parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
, flags
);
308 if (__builtin_expect (parse_res
< 1 || host
->h_addrtype
!= af
, 0))
312 *h_errnop
= NETDB_INTERNAL
;
313 return NSS_STATUS_TRYAGAIN
;
317 *h_errnop
= HOST_NOT_FOUND
;
318 return NSS_STATUS_NOTFOUND
;
322 *h_errnop
= NETDB_SUCCESS
;
323 return NSS_STATUS_SUCCESS
;
327 _nss_nis_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
328 char *buffer
, size_t buflen
, int *errnop
,
331 return internal_gethostbyname2_r (name
, af
, host
, buffer
, buflen
, errnop
,
333 ((_res
.options
& RES_USE_INET6
) ? AI_V4MAPPED
: 0));
337 _nss_nis_gethostbyname_r (const char *name
, struct hostent
*host
, char *buffer
,
338 size_t buflen
, int *errnop
, int *h_errnop
)
340 if (_res
.options
& RES_USE_INET6
)
342 enum nss_status status
;
344 status
= internal_gethostbyname2_r (name
, AF_INET6
, host
, buffer
, buflen
,
345 errnop
, h_errnop
, AI_V4MAPPED
);
346 if (status
== NSS_STATUS_SUCCESS
)
350 return internal_gethostbyname2_r (name
, AF_INET
, host
, buffer
, buflen
,
351 errnop
, h_errnop
, 0);
355 _nss_nis_gethostbyaddr_r (const void *addr
, socklen_t addrlen
, int af
,
356 struct hostent
*host
, char *buffer
, size_t buflen
,
357 int *errnop
, int *h_errnop
)
360 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
361 return NSS_STATUS_UNAVAIL
;
363 uintptr_t pad
= -(uintptr_t) buffer
% __alignof__ (struct parser_data
);
366 struct parser_data
*data
= (void *) buffer
;
367 if (__builtin_expect (buflen
< sizeof *data
+ 1 + pad
, 0))
370 *h_errnop
= NETDB_INTERNAL
;
371 return NSS_STATUS_TRYAGAIN
;
375 char *buf
= inet_ntoa (*(const struct in_addr
*) addr
);
379 int yperr
= yp_match (domain
, "hosts.byaddr", buf
, strlen (buf
), &result
,
382 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
384 enum nss_status retval
= yperr2nss (yperr
);
386 if (retval
== NSS_STATUS_TRYAGAIN
)
388 *h_errnop
= TRY_AGAIN
;
391 else if (retval
== NSS_STATUS_NOTFOUND
)
392 *h_errnop
= HOST_NOT_FOUND
;
397 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
398 if (__builtin_expect ((size_t) (len
+ 1) > linebuflen
, 0))
402 *h_errnop
= NETDB_INTERNAL
;
403 return NSS_STATUS_TRYAGAIN
;
406 char *p
= strncpy (data
->linebuffer
, result
, len
);
407 data
->linebuffer
[len
] = '\0';
412 int parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
,
413 ((_res
.options
& RES_USE_INET6
)
415 if (__builtin_expect (parse_res
< 1, 0))
419 *h_errnop
= NETDB_INTERNAL
;
420 return NSS_STATUS_TRYAGAIN
;
424 *h_errnop
= HOST_NOT_FOUND
;
425 return NSS_STATUS_NOTFOUND
;
429 *h_errnop
= NETDB_SUCCESS
;
430 return NSS_STATUS_SUCCESS
;
435 _nss_nis_getipnodebyname_r (const char *name
, int af
, int flags
,
436 struct hostent
*result
, char *buffer
,
437 size_t buflen
, int *errnop
, int *herrnop
)
439 return internal_gethostbyname2_r (name
, af
, result
, buffer
, buflen
,
440 errnop
, herrnop
, flags
);