]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nis/nis-hosts.c
* iconvdata/gbk.c (BODY): Make buf and cp char instead of unsigned
[thirdparty/glibc.git] / nis / nss_nis / nis-hosts.c
CommitLineData
2f1687b9 1/* Copyright (C) 1996-2000, 2002, 2003, 2006, 2007 Free Software Foundation, Inc.
6259ec0d 2 This file is part of the GNU C Library.
1e275b9e 3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
6259ec0d
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
6259ec0d
UD
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
41bdb6e2 13 Lesser General Public License for more details.
6259ec0d 14
41bdb6e2
AJ
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. */
6259ec0d
UD
19
20#include <nss.h>
21#include <ctype.h>
6675b191
UD
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
6259ec0d 25#include <netdb.h>
6675b191 26#undef _nss_nis_endhostent
6259ec0d
UD
27#include <string.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30#include <resolv.h>
5107cf1d 31#include <bits/libc-lock.h>
6259ec0d
UD
32#include <rpcsvc/yp.h>
33#include <rpcsvc/ypclnt.h>
34
35#include "nss-nis.h"
36
37/* Get implementation for some internal functions. */
cf29ffbe 38#include <resolv/mapv4v6addr.h>
6259ec0d
UD
39
40#define ENTNAME hostent
41#define DATABASE "hosts"
42#define NEED_H_ERRNO
43
1e275b9e
UD
44#define EXTRA_ARGS , af, flags
45#define EXTRA_ARGS_DECL , int af, int flags
46
6259ec0d
UD
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
cf29ffbe 56#include <nss/nss_files/files-parse.c>
6259ec0d
UD
57LINE_PARSER
58("#",
59 {
60 char *addr;
61
62 STRING_FIELD (addr, isspace, 1);
63
64 /* Parse address. */
1e275b9e
UD
65 if (af == AF_INET && inet_pton (AF_INET, addr, entdata->host_addr) > 0)
66 {
67 if (flags & AI_V4MAPPED)
68 {
69 map_v4v6_address ((char *) entdata->host_addr,
70 (char *) entdata->host_addr);
71 result->h_addrtype = AF_INET6;
72 result->h_length = IN6ADDRSZ;
73 }
74 else
75 {
76 result->h_addrtype = AF_INET;
77 result->h_length = INADDRSZ;
78 }
79 }
80 else if (af == AF_INET6
81 && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
6259ec0d
UD
82 {
83 result->h_addrtype = AF_INET6;
84 result->h_length = IN6ADDRSZ;
85 }
86 else
1e275b9e
UD
87 /* Illegal address: ignore line. */
88 return 0;
6259ec0d
UD
89
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;
94
6259ec0d 95 STRING_FIELD (result->h_name, isspace, 1);
1e275b9e
UD
96 })
97
6259ec0d
UD
98
99__libc_lock_define_initialized (static, lock)
100
101static bool_t new_start = 1;
102static char *oldkey = NULL;
103static int oldkeylen = 0;
104
105enum nss_status
51eecc4a 106_nss_nis_sethostent (int stayopen)
6259ec0d
UD
107{
108 __libc_lock_lock (lock);
109
110 new_start = 1;
111 if (oldkey != NULL)
112 {
113 free (oldkey);
114 oldkey = NULL;
115 oldkeylen = 0;
116 }
117
118 __libc_lock_unlock (lock);
119
120 return NSS_STATUS_SUCCESS;
121}
6675b191
UD
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. */
125strong_alias (_nss_nis_sethostent, _nss_nis_endhostent)
6259ec0d 126
1e275b9e 127/* The calling function always need to get a lock first. */
6259ec0d
UD
128static enum nss_status
129internal_nis_gethostent_r (struct hostent *host, char *buffer,
1e275b9e
UD
130 size_t buflen, int *errnop, int *h_errnop,
131 int af, int flags)
6259ec0d
UD
132{
133 char *domain;
ab9a9ff8 134 if (__builtin_expect (yp_get_default_domain (&domain), 0))
a334319f 135 return NSS_STATUS_UNAVAIL;
0ecb606c 136
2f1687b9
UD
137 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
138 buffer += pad;
139
ab9a9ff8 140 struct parser_data *data = (void *) buffer;
2f1687b9 141 if (__builtin_expect (buflen < sizeof *data + 1 + pad, 0))
6259ec0d 142 {
d71b808a 143 *errnop = ERANGE;
6259ec0d
UD
144 *h_errnop = NETDB_INTERNAL;
145 return NSS_STATUS_TRYAGAIN;
146 }
2f1687b9 147 buflen -= pad;
6259ec0d
UD
148
149 /* Get the next entry until we found a correct one. */
ab9a9ff8
UD
150 const size_t linebuflen = buffer + buflen - data->linebuffer;
151 int parse_res;
6259ec0d
UD
152 do
153 {
ab9a9ff8
UD
154 char *result;
155 int len;
156 char *outkey;
157 int keylen;
158 int yperr;
6259ec0d 159 if (new_start)
ab9a9ff8
UD
160 yperr = yp_first (domain, "hosts.byname", &outkey, &keylen, &result,
161 &len);
6259ec0d 162 else
ab9a9ff8
UD
163 yperr = yp_next (domain, "hosts.byname", oldkey, oldkeylen, &outkey,
164 &keylen, &result, &len);
6259ec0d 165
ab9a9ff8 166 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
6259ec0d 167 {
ab9a9ff8
UD
168 enum nss_status retval = yperr2nss (yperr);
169
6259ec0d
UD
170 switch (retval)
171 {
172 case NSS_STATUS_TRYAGAIN:
d71b808a 173 *errnop = errno;
6259ec0d
UD
174 *h_errnop = TRY_AGAIN;
175 break;
176 case NSS_STATUS_NOTFOUND:
177 *h_errnop = HOST_NOT_FOUND;
178 break;
179 default:
180 *h_errnop = NO_RECOVERY;
181 break;
182 }
183 return retval;
184 }
185
ab9a9ff8 186 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
6259ec0d
UD
187 {
188 free (result);
189 *h_errnop = NETDB_INTERNAL;
d71b808a 190 *errnop = ERANGE;
6259ec0d
UD
191 return NSS_STATUS_TRYAGAIN;
192 }
193
ab9a9ff8 194 char *p = strncpy (data->linebuffer, result, len);
6259ec0d
UD
195 data->linebuffer[len] = '\0';
196 while (isspace (*p))
197 ++p;
198 free (result);
199
1e275b9e 200 parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
ab9a9ff8 201 if (__builtin_expect (parse_res == -1, 0))
6259ec0d 202 {
60c96635 203 free (outkey);
d71b808a
UD
204 *h_errnop = NETDB_INTERNAL;
205 *errnop = ERANGE;
6259ec0d
UD
206 return NSS_STATUS_TRYAGAIN;
207 }
208 free (oldkey);
209 oldkey = outkey;
210 oldkeylen = keylen;
211 new_start = 0;
212 }
213 while (!parse_res);
214
215 *h_errnop = NETDB_SUCCESS;
216 return NSS_STATUS_SUCCESS;
217}
218
1e275b9e 219enum nss_status
6259ec0d 220_nss_nis_gethostent_r (struct hostent *host, char *buffer, size_t buflen,
d71b808a 221 int *errnop, int *h_errnop)
6259ec0d 222{
1e275b9e 223 enum nss_status status;
6259ec0d
UD
224
225 __libc_lock_lock (lock);
226
1e275b9e
UD
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 ));
6259ec0d
UD
230
231 __libc_lock_unlock (lock);
232
233 return status;
234}
235
1e275b9e
UD
236static enum nss_status
237internal_gethostbyname2_r (const char *name, int af, struct hostent *host,
d71b808a 238 char *buffer, size_t buflen, int *errnop,
1e275b9e 239 int *h_errnop, int flags)
6259ec0d 240{
2f1687b9
UD
241 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
242 buffer += pad;
243
6259ec0d 244 struct parser_data *data = (void *) buffer;
6259ec0d
UD
245
246 if (name == NULL)
247 {
ac9f45cf 248 *errnop = EINVAL;
6259ec0d
UD
249 return NSS_STATUS_UNAVAIL;
250 }
251
ab9a9ff8 252 char *domain;
6259ec0d
UD
253 if (yp_get_default_domain (&domain))
254 return NSS_STATUS_UNAVAIL;
255
2f1687b9 256 if (buflen < sizeof *data + 1 + pad)
6259ec0d
UD
257 {
258 *h_errnop = NETDB_INTERNAL;
d71b808a 259 *errnop = ERANGE;
6259ec0d
UD
260 return NSS_STATUS_TRYAGAIN;
261 }
2f1687b9 262 buflen -= pad;
cd897fe7 263
ab9a9ff8
UD
264 /* Convert name to lowercase. */
265 size_t namlen = strlen (name);
266 char name2[namlen + 1];
267 size_t i;
cd897fe7 268
ab9a9ff8
UD
269 for (i = 0; i < namlen; ++i)
270 name2[i] = tolower (name[i]);
271 name2[i] = '\0';
cd897fe7 272
ab9a9ff8
UD
273 char *result;
274 int len;
275 int yperr = yp_match (domain, "hosts.byname", name2, namlen, &result, &len);
6259ec0d 276
ab9a9ff8 277 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
6259ec0d 278 {
ab9a9ff8
UD
279 enum nss_status retval = yperr2nss (yperr);
280
6259ec0d
UD
281 if (retval == NSS_STATUS_TRYAGAIN)
282 {
283 *h_errnop = TRY_AGAIN;
d71b808a 284 *errnop = errno;
6259ec0d
UD
285 }
286 if (retval == NSS_STATUS_NOTFOUND)
287 *h_errnop = HOST_NOT_FOUND;
288 return retval;
289 }
290
ab9a9ff8
UD
291 const size_t linebuflen = buffer + buflen - data->linebuffer;
292 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
6259ec0d
UD
293 {
294 free (result);
295 *h_errnop = NETDB_INTERNAL;
d71b808a 296 *errnop = ERANGE;
6259ec0d
UD
297 return NSS_STATUS_TRYAGAIN;
298 }
299
ab9a9ff8 300 char *p = strncpy (data->linebuffer, result, len);
6259ec0d
UD
301 data->linebuffer[len] = '\0';
302 while (isspace (*p))
303 ++p;
304 free (result);
305
ab9a9ff8 306 int parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
6259ec0d 307
ab9a9ff8 308 if (__builtin_expect (parse_res < 1 || host->h_addrtype != af, 0))
6259ec0d 309 {
60c96635 310 if (parse_res == -1)
6259ec0d
UD
311 {
312 *h_errnop = NETDB_INTERNAL;
313 return NSS_STATUS_TRYAGAIN;
314 }
315 else
316 {
317 *h_errnop = HOST_NOT_FOUND;
318 return NSS_STATUS_NOTFOUND;
319 }
320 }
321
322 *h_errnop = NETDB_SUCCESS;
323 return NSS_STATUS_SUCCESS;
324}
325
1e275b9e
UD
326enum nss_status
327_nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
328 char *buffer, size_t buflen, int *errnop,
329 int *h_errnop)
330{
331 return internal_gethostbyname2_r (name, af, host, buffer, buflen, errnop,
332 h_errnop,
333 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0));
334}
335
0d8733c4 336enum nss_status
d71b808a
UD
337_nss_nis_gethostbyname_r (const char *name, struct hostent *host, char *buffer,
338 size_t buflen, int *errnop, int *h_errnop)
0d8733c4
UD
339{
340 if (_res.options & RES_USE_INET6)
341 {
342 enum nss_status status;
343
1e275b9e
UD
344 status = internal_gethostbyname2_r (name, AF_INET6, host, buffer, buflen,
345 errnop, h_errnop, AI_V4MAPPED);
0d8733c4
UD
346 if (status == NSS_STATUS_SUCCESS)
347 return status;
348 }
349
1e275b9e
UD
350 return internal_gethostbyname2_r (name, AF_INET, host, buffer, buflen,
351 errnop, h_errnop, 0);
0d8733c4
UD
352}
353
6259ec0d 354enum nss_status
9d4d69b8 355_nss_nis_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af,
6259ec0d 356 struct hostent *host, char *buffer, size_t buflen,
d71b808a 357 int *errnop, int *h_errnop)
6259ec0d 358{
ab9a9ff8
UD
359 char *domain;
360 if (__builtin_expect (yp_get_default_domain (&domain), 0))
a334319f 361 return NSS_STATUS_UNAVAIL;
0ecb606c 362
2f1687b9
UD
363 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
364 buffer += pad;
365
ab9a9ff8 366 struct parser_data *data = (void *) buffer;
2f1687b9 367 if (__builtin_expect (buflen < sizeof *data + 1 + pad, 0))
6259ec0d 368 {
d71b808a 369 *errnop = ERANGE;
6259ec0d
UD
370 *h_errnop = NETDB_INTERNAL;
371 return NSS_STATUS_TRYAGAIN;
372 }
2f1687b9 373 buflen -= pad;
6259ec0d 374
ab9a9ff8 375 char *buf = inet_ntoa (*(const struct in_addr *) addr);
6259ec0d 376
ab9a9ff8
UD
377 char *result;
378 int len;
379 int yperr = yp_match (domain, "hosts.byaddr", buf, strlen (buf), &result,
380 &len);
6259ec0d 381
ab9a9ff8 382 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
6259ec0d 383 {
ab9a9ff8
UD
384 enum nss_status retval = yperr2nss (yperr);
385
6259ec0d
UD
386 if (retval == NSS_STATUS_TRYAGAIN)
387 {
388 *h_errnop = TRY_AGAIN;
d71b808a 389 *errnop = errno;
6259ec0d 390 }
ab9a9ff8 391 else if (retval == NSS_STATUS_NOTFOUND)
34816665
UD
392 *h_errnop = HOST_NOT_FOUND;
393
6259ec0d
UD
394 return retval;
395 }
396
ab9a9ff8
UD
397 const size_t linebuflen = buffer + buflen - data->linebuffer;
398 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
6259ec0d
UD
399 {
400 free (result);
d71b808a 401 *errnop = ERANGE;
6259ec0d
UD
402 *h_errnop = NETDB_INTERNAL;
403 return NSS_STATUS_TRYAGAIN;
404 }
405
ab9a9ff8 406 char *p = strncpy (data->linebuffer, result, len);
6259ec0d
UD
407 data->linebuffer[len] = '\0';
408 while (isspace (*p))
409 ++p;
410 free (result);
411
ab9a9ff8
UD
412 int parse_res = parse_line (p, host, data, buflen, errnop, af,
413 ((_res.options & RES_USE_INET6)
414 ? AI_V4MAPPED : 0));
415 if (__builtin_expect (parse_res < 1, 0))
6259ec0d 416 {
60c96635 417 if (parse_res == -1)
6259ec0d
UD
418 {
419 *h_errnop = NETDB_INTERNAL;
420 return NSS_STATUS_TRYAGAIN;
421 }
422 else
423 {
424 *h_errnop = HOST_NOT_FOUND;
425 return NSS_STATUS_NOTFOUND;
426 }
427 }
428
429 *h_errnop = NETDB_SUCCESS;
430 return NSS_STATUS_SUCCESS;
431}
1e275b9e 432
eba8c0e2 433#if 0
1e275b9e
UD
434enum nss_status
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)
438{
439 return internal_gethostbyname2_r (name, af, result, buffer, buflen,
440 errnop, herrnop, flags);
441}
eba8c0e2 442#endif