1 /* Copyright (C) 1996-2000, 2002, 2003, 2006 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 struct parser_data
*data
= (void *) buffer
;
138 if (__builtin_expect (buflen
< sizeof *data
+ 1, 0))
141 *h_errnop
= NETDB_INTERNAL
;
142 return NSS_STATUS_TRYAGAIN
;
145 /* Get the next entry until we found a correct one. */
146 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
156 yperr
= yp_first (domain
, "hosts.byname", &outkey
, &keylen
, &result
,
159 yperr
= yp_next (domain
, "hosts.byname", oldkey
, oldkeylen
, &outkey
,
160 &keylen
, &result
, &len
);
162 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
164 enum nss_status retval
= yperr2nss (yperr
);
168 case NSS_STATUS_TRYAGAIN
:
170 *h_errnop
= TRY_AGAIN
;
172 case NSS_STATUS_NOTFOUND
:
173 *h_errnop
= HOST_NOT_FOUND
;
176 *h_errnop
= NO_RECOVERY
;
182 if (__builtin_expect ((size_t) (len
+ 1) > linebuflen
, 0))
185 *h_errnop
= NETDB_INTERNAL
;
187 return NSS_STATUS_TRYAGAIN
;
190 char *p
= strncpy (data
->linebuffer
, result
, len
);
191 data
->linebuffer
[len
] = '\0';
196 parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
, flags
);
197 if (__builtin_expect (parse_res
== -1, 0))
200 *h_errnop
= NETDB_INTERNAL
;
202 return NSS_STATUS_TRYAGAIN
;
211 *h_errnop
= NETDB_SUCCESS
;
212 return NSS_STATUS_SUCCESS
;
216 _nss_nis_gethostent_r (struct hostent
*host
, char *buffer
, size_t buflen
,
217 int *errnop
, int *h_errnop
)
219 enum nss_status status
;
221 __libc_lock_lock (lock
);
223 status
= internal_nis_gethostent_r (host
, buffer
, buflen
, errnop
, h_errnop
,
224 ((_res
.options
& RES_USE_INET6
) ? AF_INET6
: AF_INET
),
225 ((_res
.options
& RES_USE_INET6
) ? AI_V4MAPPED
: 0 ));
227 __libc_lock_unlock (lock
);
232 static enum nss_status
233 internal_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
234 char *buffer
, size_t buflen
, int *errnop
,
235 int *h_errnop
, int flags
)
237 struct parser_data
*data
= (void *) buffer
;
242 return NSS_STATUS_UNAVAIL
;
246 if (yp_get_default_domain (&domain
))
247 return NSS_STATUS_UNAVAIL
;
249 if (buflen
< sizeof *data
+ 1)
251 *h_errnop
= NETDB_INTERNAL
;
253 return NSS_STATUS_TRYAGAIN
;
256 /* Convert name to lowercase. */
257 size_t namlen
= strlen (name
);
258 char name2
[namlen
+ 1];
261 for (i
= 0; i
< namlen
; ++i
)
262 name2
[i
] = tolower (name
[i
]);
267 int yperr
= yp_match (domain
, "hosts.byname", name2
, namlen
, &result
, &len
);
269 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
271 enum nss_status retval
= yperr2nss (yperr
);
273 if (retval
== NSS_STATUS_TRYAGAIN
)
275 *h_errnop
= TRY_AGAIN
;
278 if (retval
== NSS_STATUS_NOTFOUND
)
279 *h_errnop
= HOST_NOT_FOUND
;
283 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
284 if (__builtin_expect ((size_t) (len
+ 1) > linebuflen
, 0))
287 *h_errnop
= NETDB_INTERNAL
;
289 return NSS_STATUS_TRYAGAIN
;
292 char *p
= strncpy (data
->linebuffer
, result
, len
);
293 data
->linebuffer
[len
] = '\0';
298 int parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
, flags
);
300 if (__builtin_expect (parse_res
< 1 || host
->h_addrtype
!= af
, 0))
304 *h_errnop
= NETDB_INTERNAL
;
305 return NSS_STATUS_TRYAGAIN
;
309 *h_errnop
= HOST_NOT_FOUND
;
310 return NSS_STATUS_NOTFOUND
;
314 *h_errnop
= NETDB_SUCCESS
;
315 return NSS_STATUS_SUCCESS
;
319 _nss_nis_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
320 char *buffer
, size_t buflen
, int *errnop
,
323 return internal_gethostbyname2_r (name
, af
, host
, buffer
, buflen
, errnop
,
325 ((_res
.options
& RES_USE_INET6
) ? AI_V4MAPPED
: 0));
329 _nss_nis_gethostbyname_r (const char *name
, struct hostent
*host
, char *buffer
,
330 size_t buflen
, int *errnop
, int *h_errnop
)
332 if (_res
.options
& RES_USE_INET6
)
334 enum nss_status status
;
336 status
= internal_gethostbyname2_r (name
, AF_INET6
, host
, buffer
, buflen
,
337 errnop
, h_errnop
, AI_V4MAPPED
);
338 if (status
== NSS_STATUS_SUCCESS
)
342 return internal_gethostbyname2_r (name
, AF_INET
, host
, buffer
, buflen
,
343 errnop
, h_errnop
, 0);
347 _nss_nis_gethostbyaddr_r (const void *addr
, socklen_t addrlen
, int af
,
348 struct hostent
*host
, char *buffer
, size_t buflen
,
349 int *errnop
, int *h_errnop
)
352 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
353 return NSS_STATUS_UNAVAIL
;
355 struct parser_data
*data
= (void *) buffer
;
356 if (__builtin_expect (buflen
< sizeof *data
+ 1, 0))
359 *h_errnop
= NETDB_INTERNAL
;
360 return NSS_STATUS_TRYAGAIN
;
363 char *buf
= inet_ntoa (*(const struct in_addr
*) addr
);
367 int yperr
= yp_match (domain
, "hosts.byaddr", buf
, strlen (buf
), &result
,
370 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
372 enum nss_status retval
= yperr2nss (yperr
);
374 if (retval
== NSS_STATUS_TRYAGAIN
)
376 *h_errnop
= TRY_AGAIN
;
379 else if (retval
== NSS_STATUS_NOTFOUND
)
380 *h_errnop
= HOST_NOT_FOUND
;
385 const size_t linebuflen
= buffer
+ buflen
- data
->linebuffer
;
386 if (__builtin_expect ((size_t) (len
+ 1) > linebuflen
, 0))
390 *h_errnop
= NETDB_INTERNAL
;
391 return NSS_STATUS_TRYAGAIN
;
394 char *p
= strncpy (data
->linebuffer
, result
, len
);
395 data
->linebuffer
[len
] = '\0';
400 int parse_res
= parse_line (p
, host
, data
, buflen
, errnop
, af
,
401 ((_res
.options
& RES_USE_INET6
)
403 if (__builtin_expect (parse_res
< 1, 0))
407 *h_errnop
= NETDB_INTERNAL
;
408 return NSS_STATUS_TRYAGAIN
;
412 *h_errnop
= HOST_NOT_FOUND
;
413 return NSS_STATUS_NOTFOUND
;
417 *h_errnop
= NETDB_SUCCESS
;
418 return NSS_STATUS_SUCCESS
;
423 _nss_nis_getipnodebyname_r (const char *name
, int af
, int flags
,
424 struct hostent
*result
, char *buffer
,
425 size_t buflen
, int *errnop
, int *herrnop
)
427 return internal_gethostbyname2_r (name
, af
, result
, buffer
, buflen
,
428 errnop
, herrnop
, flags
);