]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nis/nis-hosts.c
2.5-18.1
[thirdparty/glibc.git] / nis / nss_nis / nis-hosts.c
1 /* Copyright (C) 1996-2000, 2002, 2003, 2006, 2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
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>
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
48 struct 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>
57 LINE_PARSER
58 ("#",
59 {
60 char *addr;
61
62 STRING_FIELD (addr, isspace, 1);
63
64 /* Parse address. */
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)
82 {
83 result->h_addrtype = AF_INET6;
84 result->h_length = IN6ADDRSZ;
85 }
86 else
87 /* Illegal address: ignore line. */
88 return 0;
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
95 STRING_FIELD (result->h_name, isspace, 1);
96 })
97
98
99 __libc_lock_define_initialized (static, lock)
100
101 static bool_t new_start = 1;
102 static char *oldkey = NULL;
103 static int oldkeylen = 0;
104
105 enum nss_status
106 _nss_nis_sethostent (int stayopen)
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 }
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)
126
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)
132 {
133 char *domain;
134 if (__builtin_expect (yp_get_default_domain (&domain), 0))
135 return NSS_STATUS_UNAVAIL;
136
137 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
138 buffer += pad;
139
140 struct parser_data *data = (void *) buffer;
141 if (__builtin_expect (buflen < sizeof *data + 1 + pad, 0))
142 {
143 *errnop = ERANGE;
144 *h_errnop = NETDB_INTERNAL;
145 return NSS_STATUS_TRYAGAIN;
146 }
147 buflen -= pad;
148
149 /* Get the next entry until we found a correct one. */
150 const size_t linebuflen = buffer + buflen - data->linebuffer;
151 int parse_res;
152 do
153 {
154 char *result;
155 int len;
156 char *outkey;
157 int keylen;
158 int yperr;
159 if (new_start)
160 yperr = yp_first (domain, "hosts.byname", &outkey, &keylen, &result,
161 &len);
162 else
163 yperr = yp_next (domain, "hosts.byname", oldkey, oldkeylen, &outkey,
164 &keylen, &result, &len);
165
166 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
167 {
168 enum nss_status retval = yperr2nss (yperr);
169
170 switch (retval)
171 {
172 case NSS_STATUS_TRYAGAIN:
173 *errnop = errno;
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
186 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
187 {
188 free (result);
189 *h_errnop = NETDB_INTERNAL;
190 *errnop = ERANGE;
191 return NSS_STATUS_TRYAGAIN;
192 }
193
194 char *p = strncpy (data->linebuffer, result, len);
195 data->linebuffer[len] = '\0';
196 while (isspace (*p))
197 ++p;
198 free (result);
199
200 parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
201 if (__builtin_expect (parse_res == -1, 0))
202 {
203 free (outkey);
204 *h_errnop = NETDB_INTERNAL;
205 *errnop = ERANGE;
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
219 enum nss_status
220 _nss_nis_gethostent_r (struct hostent *host, char *buffer, size_t buflen,
221 int *errnop, int *h_errnop)
222 {
223 enum nss_status status;
224
225 __libc_lock_lock (lock);
226
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 ));
230
231 __libc_lock_unlock (lock);
232
233 return status;
234 }
235
236 static enum nss_status
237 internal_gethostbyname2_r (const char *name, int af, struct hostent *host,
238 char *buffer, size_t buflen, int *errnop,
239 int *h_errnop, int flags)
240 {
241 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
242 buffer += pad;
243
244 struct parser_data *data = (void *) buffer;
245
246 if (name == NULL)
247 {
248 *errnop = EINVAL;
249 return NSS_STATUS_UNAVAIL;
250 }
251
252 char *domain;
253 if (yp_get_default_domain (&domain))
254 return NSS_STATUS_UNAVAIL;
255
256 if (buflen < sizeof *data + 1 + pad)
257 {
258 *h_errnop = NETDB_INTERNAL;
259 *errnop = ERANGE;
260 return NSS_STATUS_TRYAGAIN;
261 }
262 buflen -= pad;
263
264 /* Convert name to lowercase. */
265 size_t namlen = strlen (name);
266 char name2[namlen + 1];
267 size_t i;
268
269 for (i = 0; i < namlen; ++i)
270 name2[i] = tolower (name[i]);
271 name2[i] = '\0';
272
273 char *result;
274 int len;
275 int yperr = yp_match (domain, "hosts.byname", name2, namlen, &result, &len);
276
277 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
278 {
279 enum nss_status retval = yperr2nss (yperr);
280
281 if (retval == NSS_STATUS_TRYAGAIN)
282 {
283 *h_errnop = TRY_AGAIN;
284 *errnop = errno;
285 }
286 if (retval == NSS_STATUS_NOTFOUND)
287 *h_errnop = HOST_NOT_FOUND;
288 return retval;
289 }
290
291 const size_t linebuflen = buffer + buflen - data->linebuffer;
292 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
293 {
294 free (result);
295 *h_errnop = NETDB_INTERNAL;
296 *errnop = ERANGE;
297 return NSS_STATUS_TRYAGAIN;
298 }
299
300 char *p = strncpy (data->linebuffer, result, len);
301 data->linebuffer[len] = '\0';
302 while (isspace (*p))
303 ++p;
304 free (result);
305
306 int parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
307
308 if (__builtin_expect (parse_res < 1 || host->h_addrtype != af, 0))
309 {
310 if (parse_res == -1)
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
326 enum 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
336 enum nss_status
337 _nss_nis_gethostbyname_r (const char *name, struct hostent *host, char *buffer,
338 size_t buflen, int *errnop, int *h_errnop)
339 {
340 if (_res.options & RES_USE_INET6)
341 {
342 enum nss_status status;
343
344 status = internal_gethostbyname2_r (name, AF_INET6, host, buffer, buflen,
345 errnop, h_errnop, AI_V4MAPPED);
346 if (status == NSS_STATUS_SUCCESS)
347 return status;
348 }
349
350 return internal_gethostbyname2_r (name, AF_INET, host, buffer, buflen,
351 errnop, h_errnop, 0);
352 }
353
354 enum nss_status
355 _nss_nis_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af,
356 struct hostent *host, char *buffer, size_t buflen,
357 int *errnop, int *h_errnop)
358 {
359 char *domain;
360 if (__builtin_expect (yp_get_default_domain (&domain), 0))
361 return NSS_STATUS_UNAVAIL;
362
363 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
364 buffer += pad;
365
366 struct parser_data *data = (void *) buffer;
367 if (__builtin_expect (buflen < sizeof *data + 1 + pad, 0))
368 {
369 *errnop = ERANGE;
370 *h_errnop = NETDB_INTERNAL;
371 return NSS_STATUS_TRYAGAIN;
372 }
373 buflen -= pad;
374
375 char *buf = inet_ntoa (*(const struct in_addr *) addr);
376
377 char *result;
378 int len;
379 int yperr = yp_match (domain, "hosts.byaddr", buf, strlen (buf), &result,
380 &len);
381
382 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
383 {
384 enum nss_status retval = yperr2nss (yperr);
385
386 if (retval == NSS_STATUS_TRYAGAIN)
387 {
388 *h_errnop = TRY_AGAIN;
389 *errnop = errno;
390 }
391 else if (retval == NSS_STATUS_NOTFOUND)
392 *h_errnop = HOST_NOT_FOUND;
393
394 return retval;
395 }
396
397 const size_t linebuflen = buffer + buflen - data->linebuffer;
398 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
399 {
400 free (result);
401 *errnop = ERANGE;
402 *h_errnop = NETDB_INTERNAL;
403 return NSS_STATUS_TRYAGAIN;
404 }
405
406 char *p = strncpy (data->linebuffer, result, len);
407 data->linebuffer[len] = '\0';
408 while (isspace (*p))
409 ++p;
410 free (result);
411
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))
416 {
417 if (parse_res == -1)
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 }
432
433 #if 0
434 enum 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 }
442 #endif