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.
4
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.
9
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.
14
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/>. */
18
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>
34
35#include "nss-nis.h"
36
37/* Get implementation for some internal functions. */
38#include <resolv/mapv4v6addr.h>
39
40#define ENTNAME hostent
41#define DATABASE "hosts"
42#define NEED_H_ERRNO
43
44#define EXTRA_ARGS , af, flags
45#define EXTRA_ARGS_DECL , int af, int flags
46
47#define ENTDATA hostent_data
48struct hostent_data
49 {
50 unsigned char host_addr[16]; /* IPv4 or IPv6 address. */
51 char *h_addr_ptrs[2]; /* Points to that and null terminator. */
52 };
53
54#define TRAILING_LIST_MEMBER h_aliases
55#define TRAILING_LIST_SEPARATOR_P isspace
56#include <nss/nss_files/files-parse.c>
57LINE_PARSER
58("#",
59 {
60 char *addr;
61
62 STRING_FIELD (addr, isspace, 1);
63
64 assert (af == AF_INET || af == AF_INET6 || af == AF_UNSPEC);
65
66 /* Parse address. */
67 if (af != AF_INET6 && inet_pton (AF_INET, addr, entdata->host_addr) > 0)
68 {
69 assert ((flags & AI_V4MAPPED) == 0 || af != AF_UNSPEC);
70 if (flags & AI_V4MAPPED)
71 {
72 map_v4v6_address ((char *) entdata->host_addr,
73 (char *) entdata->host_addr);
74 result->h_addrtype = AF_INET6;
75 result->h_length = IN6ADDRSZ;
76 }
77 else
78 {
79 result->h_addrtype = AF_INET;
80 result->h_length = INADDRSZ;
81 }
82 }
83 else if (af != AF_INET
84 && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
85 {
86 result->h_addrtype = AF_INET6;
87 result->h_length = IN6ADDRSZ;
88 }
89 else
90 /* Illegal address: ignore line. */
91 return 0;
92
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;
97
98 STRING_FIELD (result->h_name, isspace, 1);
99 })
100
101
102__libc_lock_define_initialized (static, lock)
103
104static bool_t new_start = 1;
105static char *oldkey = NULL;
106static int oldkeylen = 0;
107
108
109enum nss_status
110_nss_nis_sethostent (int stayopen)
111{
112 __libc_lock_lock (lock);
113
114 new_start = 1;
115 if (oldkey != NULL)
116 {
117 free (oldkey);
118 oldkey = NULL;
119 oldkeylen = 0;
120 }
121
122 __libc_lock_unlock (lock);
123
124 return NSS_STATUS_SUCCESS;
125}
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. */
129strong_alias (_nss_nis_sethostent, _nss_nis_endhostent)
130
131
132/* The calling function always need to get a lock first. */
133static enum nss_status
134internal_nis_gethostent_r (struct hostent *host, char *buffer,
135 size_t buflen, int *errnop, int *h_errnop,
136 int af, int flags)
137{
138 char *domain;
139 if (__glibc_unlikely (yp_get_default_domain (&domain)))
140 return NSS_STATUS_UNAVAIL;
141
142 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
143 buffer += pad;
144
145 struct parser_data *data = (void *) buffer;
146 if (__glibc_unlikely (buflen < sizeof *data + 1 + pad))
147 {
148 *errnop = ERANGE;
149 *h_errnop = NETDB_INTERNAL;
150 return NSS_STATUS_TRYAGAIN;
151 }
152 buflen -= pad;
153
154 /* Get the next entry until we found a correct one. */
155 const size_t linebuflen = buffer + buflen - data->linebuffer;
156 int parse_res;
157 do
158 {
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);
170
171 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
172 {
173 enum nss_status retval = yperr2nss (yperr);
174
175 switch (retval)
176 {
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;
187 }
188 return retval;
189 }
190
191 if (__glibc_unlikely ((size_t) (len + 1) > linebuflen))
192 {
193 free (result);
194 *h_errnop = NETDB_INTERNAL;
195 *errnop = ERANGE;
196 return NSS_STATUS_TRYAGAIN;
197 }
198
199 char *p = strncpy (data->linebuffer, result, len);
200 data->linebuffer[len] = '\0';
201 while (isspace (*p))
202 ++p;
203 free (result);
204
205 parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
206 if (__glibc_unlikely (parse_res == -1))
207 {
208 free (outkey);
209 *h_errnop = NETDB_INTERNAL;
210 *errnop = ERANGE;
211 return NSS_STATUS_TRYAGAIN;
212 }
213 free (oldkey);
214 oldkey = outkey;
215 oldkeylen = keylen;
216 new_start = 0;
217 }
218 while (!parse_res);
219
220 *h_errnop = NETDB_SUCCESS;
221 return NSS_STATUS_SUCCESS;
222}
223
224
225enum nss_status
226_nss_nis_gethostent_r (struct hostent *host, char *buffer, size_t buflen,
227 int *errnop, int *h_errnop)
228{
229 enum nss_status status;
230
231 __libc_lock_lock (lock);
232
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 ));
236
237 __libc_lock_unlock (lock);
238
239 return status;
240}
241
242
243static enum nss_status
244internal_gethostbyname2_r (const char *name, int af, struct hostent *host,
245 char *buffer, size_t buflen, int *errnop,
246 int *h_errnop, int flags)
247{
248 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
249 buffer += pad;
250
251 struct parser_data *data = (void *) buffer;
252
253 if (name == NULL)
254 {
255 *errnop = EINVAL;
256 return NSS_STATUS_UNAVAIL;
257 }
258
259 char *domain;
260 if (yp_get_default_domain (&domain))
261 return NSS_STATUS_UNAVAIL;
262
263 if (buflen < sizeof *data + 1 + pad)
264 {
265 *h_errnop = NETDB_INTERNAL;
266 *errnop = ERANGE;
267 return NSS_STATUS_TRYAGAIN;
268 }
269 buflen -= pad;
270
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)
275 {
276 *errnop = ERANGE;
277 return NSS_STATUS_UNAVAIL;
278 }
279
280 char name2[namlen + 1];
281 size_t i;
282
283 for (i = 0; i < namlen; ++i)
284 name2[i] = tolower (name[i]);
285 name2[i] = '\0';
286
287 char *result;
288 int len;
289 int yperr = yp_match (domain, "hosts.byname", name2, namlen, &result, &len);
290
291 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
292 {
293 enum nss_status retval = yperr2nss (yperr);
294
295 if (retval == NSS_STATUS_TRYAGAIN)
296 {
297 *h_errnop = TRY_AGAIN;
298 *errnop = errno;
299 }
300 if (retval == NSS_STATUS_NOTFOUND)
301 *h_errnop = HOST_NOT_FOUND;
302 return retval;
303 }
304
305 const size_t linebuflen = buffer + buflen - data->linebuffer;
306 if (__glibc_unlikely ((size_t) (len + 1) > linebuflen))
307 {
308 free (result);
309 *h_errnop = NETDB_INTERNAL;
310 *errnop = ERANGE;
311 return NSS_STATUS_TRYAGAIN;
312 }
313
314 char *p = strncpy (data->linebuffer, result, len);
315 data->linebuffer[len] = '\0';
316 while (isspace (*p))
317 ++p;
318 free (result);
319
320 int parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
321
322 if (__glibc_unlikely (parse_res < 1 || host->h_addrtype != af))
323 {
324 if (parse_res == -1)
325 {
326 *h_errnop = NETDB_INTERNAL;
327 return NSS_STATUS_TRYAGAIN;
328 }
329 else
330 {
331 *h_errnop = HOST_NOT_FOUND;
332 return NSS_STATUS_NOTFOUND;
333 }
334 }
335
336 *h_errnop = NETDB_SUCCESS;
337 return NSS_STATUS_SUCCESS;
338}
339
340
341enum 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)
345{
346 if (af != AF_INET && af != AF_INET6)
347 {
348 *h_errnop = HOST_NOT_FOUND;
349 return NSS_STATUS_NOTFOUND;
350 }
351
352 return internal_gethostbyname2_r (name, af, host, buffer, buflen, errnop,
353 h_errnop,
354 (res_use_inet6 () ? AI_V4MAPPED : 0));
355}
356
357
358enum nss_status
359_nss_nis_gethostbyname_r (const char *name, struct hostent *host, char *buffer,
360 size_t buflen, int *errnop, int *h_errnop)
361{
362 if (res_use_inet6 ())
363 {
364 enum nss_status status;
365
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;
370 }
371
372 return internal_gethostbyname2_r (name, AF_INET, host, buffer, buflen,
373 errnop, h_errnop, 0);
374}
375
376
377enum 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)
381{
382 char *domain;
383 if (__glibc_unlikely (yp_get_default_domain (&domain)))
384 return NSS_STATUS_UNAVAIL;
385
386 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
387 buffer += pad;
388
389 struct parser_data *data = (void *) buffer;
390 if (__glibc_unlikely (buflen < sizeof *data + 1 + pad))
391 {
392 *errnop = ERANGE;
393 *h_errnop = NETDB_INTERNAL;
394 return NSS_STATUS_TRYAGAIN;
395 }
396 buflen -= pad;
397
398 char *buf = inet_ntoa (*(const struct in_addr *) addr);
399
400 char *result;
401 int len;
402 int yperr = yp_match (domain, "hosts.byaddr", buf, strlen (buf), &result,
403 &len);
404
405 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
406 {
407 enum nss_status retval = yperr2nss (yperr);
408
409 if (retval == NSS_STATUS_TRYAGAIN)
410 {
411 *h_errnop = TRY_AGAIN;
412 *errnop = errno;
413 }
414 else if (retval == NSS_STATUS_NOTFOUND)
415 *h_errnop = HOST_NOT_FOUND;
416
417 return retval;
418 }
419
420 const size_t linebuflen = buffer + buflen - data->linebuffer;
421 if (__glibc_unlikely ((size_t) (len + 1) > linebuflen))
422 {
423 free (result);
424 *errnop = ERANGE;
425 *h_errnop = NETDB_INTERNAL;
426 return NSS_STATUS_TRYAGAIN;
427 }
428
429 char *p = strncpy (data->linebuffer, result, len);
430 data->linebuffer[len] = '\0';
431 while (isspace (*p))
432 ++p;
433 free (result);
434
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))
438 {
439 if (parse_res == -1)
440 {
441 *h_errnop = NETDB_INTERNAL;
442 return NSS_STATUS_TRYAGAIN;
443 }
444 else
445 {
446 *h_errnop = HOST_NOT_FOUND;
447 return NSS_STATUS_NOTFOUND;
448 }
449 }
450
451 *h_errnop = NETDB_SUCCESS;
452 return NSS_STATUS_SUCCESS;
453}
454
455
456enum 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)
460{
461 char *domain;
462 if (yp_get_default_domain (&domain))
463 {
464 *herrnop = NO_DATA;
465 return NSS_STATUS_UNAVAIL;
466 }
467
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)
472 {
473 *errnop = ERANGE;
474 return NSS_STATUS_UNAVAIL;
475 }
476
477 char name2[namlen + 1];
478 size_t i;
479
480 for (i = 0; i < namlen; ++i)
481 name2[i] = tolower (name[i]);
482 name2[i] = '\0';
483
484 char *result;
485 int len;
486 int yperr = yp_match (domain, "hosts.byname", name2, namlen, &result, &len);
487
488 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
489 {
490 enum nss_status retval = yperr2nss (yperr);
491
492 if (retval == NSS_STATUS_TRYAGAIN)
493 {
494 *herrnop = TRY_AGAIN;
495 *errnop = errno;
496 }
497 if (retval == NSS_STATUS_NOTFOUND)
498 *herrnop = HOST_NOT_FOUND;
499 return retval;
500 }
501
502 if (*pat == NULL)
503 {
504 uintptr_t pad = (-(uintptr_t) buffer
505 % __alignof__ (struct gaih_addrtuple));
506 buffer += pad;
507 buflen = buflen > pad ? buflen - pad : 0;
508
509 if (__glibc_unlikely (buflen < sizeof (struct gaih_addrtuple)))
510 {
511 erange:
512 free (result);
513 *errnop = ERANGE;
514 *herrnop = NETDB_INTERNAL;
515 return NSS_STATUS_TRYAGAIN;
516 }
517
518 *pat = (struct gaih_addrtuple *) buffer;
519 buffer += sizeof (struct gaih_addrtuple);
520 buflen -= sizeof (struct gaih_addrtuple);
521 }
522
523 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
524 buffer += pad;
525
526 struct parser_data *data = (void *) buffer;
527
528 if (__glibc_unlikely (buflen < sizeof *data + 1 + pad))
529 goto erange;
530 buflen -= pad;
531
532 struct hostent host;
533 int parse_res = parse_line (result, &host, data, buflen, errnop, AF_UNSPEC,
534 0);
535 if (__glibc_unlikely (parse_res < 1))
536 {
537 if (parse_res == -1)
538 {
539 *herrnop = NETDB_INTERNAL;
540 return NSS_STATUS_TRYAGAIN;
541 }
542 else
543 {
544 *herrnop = HOST_NOT_FOUND;
545 return NSS_STATUS_NOTFOUND;
546 }
547 }
548
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);
554
555 /* Undo the alignment for parser_data. */
556 buffer -= pad;
557 buflen += pad;
558
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);
563
564 free (result);
565
566 return NSS_STATUS_SUCCESS;
567}
568