powerpc: Avoid misaligned stores in memset
[glibc.git] / nis / nss_nis / nis-hosts.c
blobf64dbdaecb38debaa5622b7a7ac45e36e9bee1cd
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/>. */
19 #include <assert.h>
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/resolv-internal.h>
31 #include <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 assert (af == AF_INET || af == AF_INET6 || af == AF_UNSPEC);
66 /* Parse address. */
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;
77 else
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;
89 else
90 /* Illegal address: ignore line. */
91 return 0;
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;
109 enum nss_status
110 _nss_nis_sethostent (int stayopen)
112 __libc_lock_lock (lock);
114 new_start = 1;
115 if (oldkey != NULL)
117 free (oldkey);
118 oldkey = NULL;
119 oldkeylen = 0;
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,
136 int af, int flags)
138 char *domain;
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);
143 buffer += pad;
145 struct parser_data *data = (void *) buffer;
146 if (__glibc_unlikely (buflen < sizeof *data + 1 + pad))
148 *errnop = ERANGE;
149 *h_errnop = NETDB_INTERNAL;
150 return NSS_STATUS_TRYAGAIN;
152 buflen -= pad;
154 /* Get the next entry until we found a correct one. */
155 const size_t linebuflen = buffer + buflen - data->linebuffer;
156 int parse_res;
159 char *result;
160 int len;
161 char *outkey;
162 int keylen;
163 int yperr;
164 if (new_start)
165 yperr = yp_first (domain, "hosts.byname", &outkey, &keylen, &result,
166 &len);
167 else
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);
175 switch (retval)
177 case NSS_STATUS_TRYAGAIN:
178 *errnop = errno;
179 *h_errnop = TRY_AGAIN;
180 break;
181 case NSS_STATUS_NOTFOUND:
182 *h_errnop = HOST_NOT_FOUND;
183 break;
184 default:
185 *h_errnop = NO_RECOVERY;
186 break;
188 return retval;
191 if (__glibc_unlikely ((size_t) (len + 1) > linebuflen))
193 free (result);
194 *h_errnop = NETDB_INTERNAL;
195 *errnop = ERANGE;
196 return NSS_STATUS_TRYAGAIN;
199 char *p = strncpy (data->linebuffer, result, len);
200 data->linebuffer[len] = '\0';
201 while (isspace (*p))
202 ++p;
203 free (result);
205 parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
206 if (__glibc_unlikely (parse_res == -1))
208 free (outkey);
209 *h_errnop = NETDB_INTERNAL;
210 *errnop = ERANGE;
211 return NSS_STATUS_TRYAGAIN;
213 free (oldkey);
214 oldkey = outkey;
215 oldkeylen = keylen;
216 new_start = 0;
218 while (!parse_res);
220 *h_errnop = NETDB_SUCCESS;
221 return NSS_STATUS_SUCCESS;
225 enum nss_status
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);
239 return status;
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);
249 buffer += pad;
251 struct parser_data *data = (void *) buffer;
253 if (name == NULL)
255 *errnop = EINVAL;
256 return NSS_STATUS_UNAVAIL;
259 char *domain;
260 if (yp_get_default_domain (&domain))
261 return NSS_STATUS_UNAVAIL;
263 if (buflen < sizeof *data + 1 + pad)
265 *h_errnop = NETDB_INTERNAL;
266 *errnop = ERANGE;
267 return NSS_STATUS_TRYAGAIN;
269 buflen -= pad;
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)
276 *errnop = ERANGE;
277 return NSS_STATUS_UNAVAIL;
280 char name2[namlen + 1];
281 size_t i;
283 for (i = 0; i < namlen; ++i)
284 name2[i] = tolower (name[i]);
285 name2[i] = '\0';
287 char *result;
288 int len;
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;
298 *errnop = errno;
300 if (retval == NSS_STATUS_NOTFOUND)
301 *h_errnop = HOST_NOT_FOUND;
302 return retval;
305 const size_t linebuflen = buffer + buflen - data->linebuffer;
306 if (__glibc_unlikely ((size_t) (len + 1) > linebuflen))
308 free (result);
309 *h_errnop = NETDB_INTERNAL;
310 *errnop = ERANGE;
311 return NSS_STATUS_TRYAGAIN;
314 char *p = strncpy (data->linebuffer, result, len);
315 data->linebuffer[len] = '\0';
316 while (isspace (*p))
317 ++p;
318 free (result);
320 int parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
322 if (__glibc_unlikely (parse_res < 1 || host->h_addrtype != af))
324 if (parse_res == -1)
326 *h_errnop = NETDB_INTERNAL;
327 return NSS_STATUS_TRYAGAIN;
329 else
331 *h_errnop = HOST_NOT_FOUND;
332 return NSS_STATUS_NOTFOUND;
336 *h_errnop = NETDB_SUCCESS;
337 return NSS_STATUS_SUCCESS;
341 enum nss_status
342 _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
343 char *buffer, size_t buflen, int *errnop,
344 int *h_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,
353 h_errnop,
354 (res_use_inet6 () ? AI_V4MAPPED : 0));
358 enum nss_status
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)
369 return status;
372 return internal_gethostbyname2_r (name, AF_INET, host, buffer, buflen,
373 errnop, h_errnop, 0);
377 enum nss_status
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)
382 char *domain;
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);
387 buffer += pad;
389 struct parser_data *data = (void *) buffer;
390 if (__glibc_unlikely (buflen < sizeof *data + 1 + pad))
392 *errnop = ERANGE;
393 *h_errnop = NETDB_INTERNAL;
394 return NSS_STATUS_TRYAGAIN;
396 buflen -= pad;
398 char *buf = inet_ntoa (*(const struct in_addr *) addr);
400 char *result;
401 int len;
402 int yperr = yp_match (domain, "hosts.byaddr", buf, strlen (buf), &result,
403 &len);
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;
412 *errnop = errno;
414 else if (retval == NSS_STATUS_NOTFOUND)
415 *h_errnop = HOST_NOT_FOUND;
417 return retval;
420 const size_t linebuflen = buffer + buflen - data->linebuffer;
421 if (__glibc_unlikely ((size_t) (len + 1) > linebuflen))
423 free (result);
424 *errnop = ERANGE;
425 *h_errnop = NETDB_INTERNAL;
426 return NSS_STATUS_TRYAGAIN;
429 char *p = strncpy (data->linebuffer, result, len);
430 data->linebuffer[len] = '\0';
431 while (isspace (*p))
432 ++p;
433 free (result);
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))
439 if (parse_res == -1)
441 *h_errnop = NETDB_INTERNAL;
442 return NSS_STATUS_TRYAGAIN;
444 else
446 *h_errnop = HOST_NOT_FOUND;
447 return NSS_STATUS_NOTFOUND;
451 *h_errnop = NETDB_SUCCESS;
452 return NSS_STATUS_SUCCESS;
456 enum nss_status
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)
461 char *domain;
462 if (yp_get_default_domain (&domain))
464 *herrnop = NO_DATA;
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)
473 *errnop = ERANGE;
474 return NSS_STATUS_UNAVAIL;
477 char name2[namlen + 1];
478 size_t i;
480 for (i = 0; i < namlen; ++i)
481 name2[i] = tolower (name[i]);
482 name2[i] = '\0';
484 char *result;
485 int len;
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;
495 *errnop = errno;
497 if (retval == NSS_STATUS_NOTFOUND)
498 *herrnop = HOST_NOT_FOUND;
499 return retval;
502 if (*pat == NULL)
504 uintptr_t pad = (-(uintptr_t) buffer
505 % __alignof__ (struct gaih_addrtuple));
506 buffer += pad;
507 buflen = buflen > pad ? buflen - pad : 0;
509 if (__glibc_unlikely (buflen < sizeof (struct gaih_addrtuple)))
511 erange:
512 free (result);
513 *errnop = ERANGE;
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);
524 buffer += pad;
526 struct parser_data *data = (void *) buffer;
528 if (__glibc_unlikely (buflen < sizeof *data + 1 + pad))
529 goto erange;
530 buflen -= pad;
532 struct hostent host;
533 int parse_res = parse_line (result, &host, data, buflen, errnop, AF_UNSPEC,
535 if (__glibc_unlikely (parse_res < 1))
537 if (parse_res == -1)
539 *herrnop = NETDB_INTERNAL;
540 return NSS_STATUS_TRYAGAIN;
542 else
544 *herrnop = HOST_NOT_FOUND;
545 return NSS_STATUS_NOTFOUND;
549 (*pat)->next = NULL;
550 (*pat)->family = host.h_addrtype;
551 memcpy ((*pat)->addr, host.h_addr_list[0], host.h_length);
552 (*pat)->scopeid = 0;
553 assert (host.h_addr_list[1] == NULL);
555 /* Undo the alignment for parser_data. */
556 buffer -= pad;
557 buflen += pad;
559 size_t h_name_len = strlen (host.h_name) + 1;
560 if (h_name_len >= buflen)
561 goto erange;
562 (*pat)->name = memcpy (buffer, host.h_name, h_name_len);
564 free (result);
566 return NSS_STATUS_SUCCESS;