* sysdeps/unix/sysv/linux/dl-osinfo.h: Remove unnecessary include.
[glibc.git] / nis / nss_nis / nis-hosts.c
blob4fa03616813fa00e1240fb3c4338f0f2c5ff7dc9
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
18 02111-1307 USA. */
20 #include <nss.h>
21 #include <ctype.h>
22 /* The following is an ugly trick to avoid a prototype declaration for
23 _nss_nis_endgrent. */
24 #define _nss_nis_endhostent _nss_nis_endhostent_XXX
25 #include <netdb.h>
26 #undef _nss_nis_endhostent
27 #include <string.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <resolv.h>
31 #include <bits/libc-lock.h>
32 #include <rpcsvc/yp.h>
33 #include <rpcsvc/ypclnt.h>
35 #include "nss-nis.h"
37 /* Get implementation for some internal functions. */
38 #include <resolv/mapv4v6addr.h>
40 #define ENTNAME hostent
41 #define DATABASE "hosts"
42 #define NEED_H_ERRNO
44 #define EXTRA_ARGS , af, flags
45 #define EXTRA_ARGS_DECL , int af, int flags
47 #define ENTDATA hostent_data
48 struct 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>
57 LINE_PARSER
58 ("#",
60 char *addr;
62 STRING_FIELD (addr, isspace, 1);
64 /* Parse address. */
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;
74 else
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;
86 else
87 /* Illegal address: ignore line. */
88 return 0;
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;
105 enum nss_status
106 _nss_nis_sethostent (int stayopen)
108 __libc_lock_lock (lock);
110 new_start = 1;
111 if (oldkey != NULL)
113 free (oldkey);
114 oldkey = NULL;
115 oldkeylen = 0;
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,
131 int af, int flags)
133 char *domain;
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))
140 *errnop = ERANGE;
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;
147 int parse_res;
150 char *result;
151 int len;
152 char *outkey;
153 int keylen;
154 int yperr;
155 if (new_start)
156 yperr = yp_first (domain, "hosts.byname", &outkey, &keylen, &result,
157 &len);
158 else
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);
166 switch (retval)
168 case NSS_STATUS_TRYAGAIN:
169 *errnop = errno;
170 *h_errnop = TRY_AGAIN;
171 break;
172 case NSS_STATUS_NOTFOUND:
173 *h_errnop = HOST_NOT_FOUND;
174 break;
175 default:
176 *h_errnop = NO_RECOVERY;
177 break;
179 return retval;
182 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
184 free (result);
185 *h_errnop = NETDB_INTERNAL;
186 *errnop = ERANGE;
187 return NSS_STATUS_TRYAGAIN;
190 char *p = strncpy (data->linebuffer, result, len);
191 data->linebuffer[len] = '\0';
192 while (isspace (*p))
193 ++p;
194 free (result);
196 parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
197 if (__builtin_expect (parse_res == -1, 0))
199 free (outkey);
200 *h_errnop = NETDB_INTERNAL;
201 *errnop = ERANGE;
202 return NSS_STATUS_TRYAGAIN;
204 free (oldkey);
205 oldkey = outkey;
206 oldkeylen = keylen;
207 new_start = 0;
209 while (!parse_res);
211 *h_errnop = NETDB_SUCCESS;
212 return NSS_STATUS_SUCCESS;
215 enum nss_status
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);
229 return status;
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;
239 if (name == NULL)
241 *errnop = EINVAL;
242 return NSS_STATUS_UNAVAIL;
245 char *domain;
246 if (yp_get_default_domain (&domain))
247 return NSS_STATUS_UNAVAIL;
249 if (buflen < sizeof *data + 1)
251 *h_errnop = NETDB_INTERNAL;
252 *errnop = ERANGE;
253 return NSS_STATUS_TRYAGAIN;
256 /* Convert name to lowercase. */
257 size_t namlen = strlen (name);
258 char name2[namlen + 1];
259 size_t i;
261 for (i = 0; i < namlen; ++i)
262 name2[i] = tolower (name[i]);
263 name2[i] = '\0';
265 char *result;
266 int len;
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;
276 *errnop = errno;
278 if (retval == NSS_STATUS_NOTFOUND)
279 *h_errnop = HOST_NOT_FOUND;
280 return retval;
283 const size_t linebuflen = buffer + buflen - data->linebuffer;
284 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
286 free (result);
287 *h_errnop = NETDB_INTERNAL;
288 *errnop = ERANGE;
289 return NSS_STATUS_TRYAGAIN;
292 char *p = strncpy (data->linebuffer, result, len);
293 data->linebuffer[len] = '\0';
294 while (isspace (*p))
295 ++p;
296 free (result);
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))
302 if (parse_res == -1)
304 *h_errnop = NETDB_INTERNAL;
305 return NSS_STATUS_TRYAGAIN;
307 else
309 *h_errnop = HOST_NOT_FOUND;
310 return NSS_STATUS_NOTFOUND;
314 *h_errnop = NETDB_SUCCESS;
315 return NSS_STATUS_SUCCESS;
318 enum nss_status
319 _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
320 char *buffer, size_t buflen, int *errnop,
321 int *h_errnop)
323 return internal_gethostbyname2_r (name, af, host, buffer, buflen, errnop,
324 h_errnop,
325 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0));
328 enum nss_status
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)
339 return status;
342 return internal_gethostbyname2_r (name, AF_INET, host, buffer, buflen,
343 errnop, h_errnop, 0);
346 enum nss_status
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)
351 char *domain;
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))
358 *errnop = ERANGE;
359 *h_errnop = NETDB_INTERNAL;
360 return NSS_STATUS_TRYAGAIN;
363 char *buf = inet_ntoa (*(const struct in_addr *) addr);
365 char *result;
366 int len;
367 int yperr = yp_match (domain, "hosts.byaddr", buf, strlen (buf), &result,
368 &len);
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;
377 *errnop = errno;
379 else if (retval == NSS_STATUS_NOTFOUND)
380 *h_errnop = HOST_NOT_FOUND;
382 return retval;
385 const size_t linebuflen = buffer + buflen - data->linebuffer;
386 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
388 free (result);
389 *errnop = ERANGE;
390 *h_errnop = NETDB_INTERNAL;
391 return NSS_STATUS_TRYAGAIN;
394 char *p = strncpy (data->linebuffer, result, len);
395 data->linebuffer[len] = '\0';
396 while (isspace (*p))
397 ++p;
398 free (result);
400 int parse_res = parse_line (p, host, data, buflen, errnop, af,
401 ((_res.options & RES_USE_INET6)
402 ? AI_V4MAPPED : 0));
403 if (__builtin_expect (parse_res < 1, 0))
405 if (parse_res == -1)
407 *h_errnop = NETDB_INTERNAL;
408 return NSS_STATUS_TRYAGAIN;
410 else
412 *h_errnop = HOST_NOT_FOUND;
413 return NSS_STATUS_NOTFOUND;
417 *h_errnop = NETDB_SUCCESS;
418 return NSS_STATUS_SUCCESS;
421 #if 0
422 enum nss_status
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);
430 #endif