]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-hosts.c
Update.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-hosts.c
1 /* Copyright (C) 1997,1998,1999,2000,2001,2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
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 <netdb.h>
22 #include <errno.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/nis.h>
29
30 #include "nss-nisplus.h"
31
32 __libc_lock_define_initialized (static, lock)
33
34 static nis_result *result;
35 static nis_name tablename_val;
36 static u_long tablename_len;
37
38 #define NISENTRYVAL(idx,col,res) \
39 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
40
41 #define NISENTRYLEN(idx,col,res) \
42 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43
44 /* Get implementation for some internal functions. */
45 #include <resolv/mapv4v6addr.h>
46
47 static int
48 _nss_nisplus_parse_hostent (nis_result *result, int af, struct hostent *host,
49 char *buffer, size_t buflen, int *errnop,
50 int flags)
51 {
52 unsigned int i;
53 char *first_unused = buffer;
54 size_t room_left = buflen;
55 char *data, *p, *line;
56
57 if (result == NULL)
58 return 0;
59
60 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
61 __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
62 strcmp(result->objects.objects_val[0].EN_data.en_type,
63 "hosts_tbl") != 0 ||
64 result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 4)
65 return 0;
66
67 if (room_left < NISENTRYLEN (0, 2, result) + 1)
68 {
69 no_more_room:
70 *errnop = ERANGE;
71 return -1;
72 }
73
74 data = first_unused;
75
76 /* Parse address. */
77 if (af == AF_INET && inet_pton (af, NISENTRYVAL (0, 2, result), data) > 0)
78 {
79 if (flags & AI_V4MAPPED)
80 {
81 map_v4v6_address (data, data);
82 host->h_addrtype = AF_INET6;
83 host->h_length = IN6ADDRSZ;
84 }
85 else
86 {
87 host->h_addrtype = AF_INET;
88 host->h_length = INADDRSZ;
89 }
90 }
91 else if (af == AF_INET6
92 && inet_pton (AF_INET6, NISENTRYVAL (0, 2, result), data) > 0)
93 {
94 host->h_addrtype = AF_INET6;
95 host->h_length = IN6ADDRSZ;
96 }
97 else
98 /* Illegal address: ignore line. */
99 return 0;
100
101 first_unused+=host->h_length;
102 room_left-=host->h_length;
103
104 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
105 goto no_more_room;
106
107 p = __stpncpy (first_unused, NISENTRYVAL (0, 0, result),
108 NISENTRYLEN (0, 0, result));
109 *p = '\0';
110 room_left -= (NISENTRYLEN (0, 0, result) + 1);
111 host->h_name = first_unused;
112 first_unused += NISENTRYLEN (0, 0, result) +1;
113 p = first_unused;
114
115 line = p;
116 for (i = 0; i < result->objects.objects_len; ++i)
117 {
118 if (strcmp (NISENTRYVAL (i, 1, result), host->h_name) != 0)
119 {
120 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
121 goto no_more_room;
122
123 *p++ = ' ';
124 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
125 NISENTRYLEN (i, 1, result));
126 *p = '\0';
127 room_left -= (NISENTRYLEN (i, 1, result) + 1);
128 }
129 }
130 *p++ = '\0';
131 first_unused = p;
132
133 /* Adjust the pointer so it is aligned for
134 storing pointers. */
135 first_unused += __alignof__ (char *) - 1;
136 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
137 host->h_addr_list = (char **) first_unused;
138 if (room_left < 2 * sizeof (char *))
139 goto no_more_room;
140
141 room_left -= (2 * sizeof (char *));
142 host->h_addr_list[0] = data;
143 host->h_addr_list[1] = NULL;
144 host->h_aliases = &host->h_addr_list[2];
145 host->h_aliases[0] = NULL;
146
147 i = 0;
148 while (*line != '\0')
149 {
150 /* Skip leading blanks. */
151 while (isspace (*line))
152 ++line;
153
154 if (*line == '\0')
155 break;
156
157 if (room_left < sizeof (char *))
158 goto no_more_room;
159
160 room_left -= sizeof (char *);
161 host->h_aliases[i] = line;
162
163 while (*line != '\0' && *line != ' ')
164 ++line;
165
166 if (*line == ' ')
167 {
168 *line = '\0';
169 ++line;
170 ++i;
171 }
172 else
173 host->h_aliases[i+1] = NULL;
174 }
175 return 1;
176 }
177
178 static enum nss_status
179 _nss_create_tablename (int *errnop)
180 {
181 if (tablename_val == NULL)
182 {
183 char buf [40 + strlen (nis_local_directory ())];
184 char *p;
185
186 p = __stpcpy (buf, "hosts.org_dir.");
187 p = __stpcpy (p, nis_local_directory ());
188 tablename_val = __strdup (buf);
189 if (tablename_val == NULL)
190 {
191 *errnop = errno;
192 return NSS_STATUS_TRYAGAIN;
193 }
194 tablename_len = strlen (tablename_val);
195 }
196 return NSS_STATUS_SUCCESS;
197 }
198
199 enum nss_status
200 _nss_nisplus_sethostent (int stayopen)
201 {
202 enum nss_status status = NSS_STATUS_SUCCESS;
203 int err;
204
205 __libc_lock_lock (lock);
206
207 if (result)
208 nis_freeresult (result);
209 result = NULL;
210
211 if (tablename_val == NULL)
212 status = _nss_create_tablename (&err);
213
214 __libc_lock_unlock (lock);
215
216 return status;
217 }
218
219 enum nss_status
220 _nss_nisplus_endhostent (void)
221 {
222 __libc_lock_lock (lock);
223
224 if (result)
225 nis_freeresult (result);
226 result = NULL;
227
228 __libc_lock_unlock (lock);
229
230 return NSS_STATUS_SUCCESS;
231 }
232
233 static enum nss_status
234 internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
235 size_t buflen, int *errnop, int *herrnop)
236 {
237 int parse_res;
238
239 /* Get the next entry until we found a correct one. */
240 do
241 {
242 nis_result *saved_res;
243
244 if (result == NULL)
245 {
246 saved_res = NULL;
247 if (tablename_val == NULL)
248 {
249 enum nss_status status = _nss_create_tablename (errnop);
250
251 if (status != NSS_STATUS_SUCCESS)
252 return status;
253 }
254
255 result = nis_first_entry (tablename_val);
256 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
257 {
258 enum nss_status retval = niserr2nss (result->status);
259 if (retval == NSS_STATUS_TRYAGAIN)
260 {
261 *herrnop = NETDB_INTERNAL;
262 *errnop = errno;
263 }
264 return retval;
265 }
266
267 }
268 else
269 {
270 nis_result *res2;
271
272 saved_res = result;
273 res2 = nis_next_entry(tablename_val, &result->cookie);
274 result = res2;
275 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
276 {
277 enum nss_status retval= niserr2nss (result->status);
278
279 nis_freeresult (result);
280 result = saved_res;
281 if (retval == NSS_STATUS_TRYAGAIN)
282 {
283 *herrnop = NETDB_INTERNAL;
284 *errnop = errno;
285 }
286 return retval;
287 }
288 }
289
290 if (_res.options & RES_USE_INET6)
291 parse_res = _nss_nisplus_parse_hostent (result, AF_INET6, host, buffer,
292 buflen, errnop, AI_V4MAPPED);
293 else
294 parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host, buffer,
295 buflen, errnop, 0);
296
297 if (parse_res == -1)
298 {
299 nis_freeresult (result);
300 result = saved_res;
301 *herrnop = NETDB_INTERNAL;
302 *errnop = ERANGE;
303 return NSS_STATUS_TRYAGAIN;
304 }
305 if (saved_res != NULL)
306 nis_freeresult (saved_res);
307
308 } while (!parse_res);
309
310 return NSS_STATUS_SUCCESS;
311 }
312
313 enum nss_status
314 _nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
315 size_t buflen, int *errnop, int *herrnop)
316 {
317 int status;
318
319 __libc_lock_lock (lock);
320
321 status = internal_nisplus_gethostent_r (result, buffer, buflen, errnop,
322 herrnop);
323
324 __libc_lock_unlock (lock);
325
326 return status;
327 }
328
329 static enum nss_status
330 internal_gethostbyname2_r (const char *name, int af, struct hostent *host,
331 char *buffer, size_t buflen, int *errnop,
332 int *herrnop, int flags)
333 {
334 int parse_res, retval;
335
336 if (tablename_val == NULL)
337 {
338 enum nss_status status = _nss_create_tablename (errnop);
339
340 if (status != NSS_STATUS_SUCCESS)
341 {
342 *herrnop = NETDB_INTERNAL;
343 return NSS_STATUS_UNAVAIL;
344 }
345 }
346
347 if (name == NULL)
348 {
349 *errnop = EINVAL;
350 *herrnop = NETDB_INTERNAL;
351 return NSS_STATUS_NOTFOUND;
352 }
353 else
354 {
355 nis_result *result;
356 char buf[strlen (name) + 255 + tablename_len];
357 int olderr = errno;
358
359 /* Search at first in the alias list, and use the correct name
360 for the next search */
361 sprintf (buf, "[name=%s],%s", name, tablename_val);
362 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
363
364 /* If we do not find it, try it as original name. But if the
365 database is correct, we should find it in the first case, too */
366 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
367 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
368 || strcmp (result->objects.objects_val->EN_data.en_type,
369 "hosts_tbl") != 0
370 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
371 sprintf (buf, "[cname=%s],%s", name, tablename_val);
372 else
373 sprintf (buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
374 tablename_val);
375
376 nis_freeresult (result);
377 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
378
379 retval = niserr2nss (result->status);
380 if (retval != NSS_STATUS_SUCCESS)
381 {
382 if (retval == NSS_STATUS_TRYAGAIN)
383 {
384 *errnop = errno;
385 *herrnop = NETDB_INTERNAL;
386 }
387 else
388 __set_errno (olderr);
389 nis_freeresult (result);
390 return retval;
391 }
392
393 parse_res = _nss_nisplus_parse_hostent (result, af, host, buffer,
394 buflen, errnop, flags);
395
396 nis_freeresult (result);
397
398 if (parse_res > 0)
399 return NSS_STATUS_SUCCESS;
400
401 *herrnop = NETDB_INTERNAL;
402 if (parse_res == -1)
403 {
404 *errnop = ERANGE;
405 return NSS_STATUS_TRYAGAIN;
406 }
407 else
408 {
409 __set_errno (olderr);
410 return NSS_STATUS_NOTFOUND;
411 }
412 }
413 }
414
415 enum nss_status
416 _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
417 char *buffer, size_t buflen, int *errnop,
418 int *herrnop)
419 {
420 return internal_gethostbyname2_r (name, af, host, buffer, buflen, errnop,
421 herrnop,
422 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0));
423 }
424
425 #if 0
426 enum nss_status
427 _nss_nisplus_getipnodebyname_r (const char *name, int af, int flags,
428 struct hostent *result, char *buffer,
429 size_t buflen, int *errnop, int *herrnop)
430 {
431 return internal_gethostbyname2_r (name, af, result, buffer, buflen,
432 errnop, herrnop, flags);
433 }
434 #endif
435
436 enum nss_status
437 _nss_nisplus_gethostbyname_r (const char *name, struct hostent *host,
438 char *buffer, size_t buflen, int *errnop,
439 int *h_errnop)
440 {
441 if (_res.options & RES_USE_INET6)
442 {
443 enum nss_status status;
444
445 status = internal_gethostbyname2_r (name, AF_INET6, host, buffer,
446 buflen, errnop, h_errnop,
447 AI_V4MAPPED);
448 if (status == NSS_STATUS_SUCCESS)
449 return status;
450 }
451
452 return internal_gethostbyname2_r (name, AF_INET, host, buffer,
453 buflen, errnop, h_errnop, 0);
454 }
455
456 enum nss_status
457 _nss_nisplus_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af,
458 struct hostent *host, char *buffer,
459 size_t buflen, int *errnop, int *herrnop)
460 {
461 if (tablename_val == NULL)
462 {
463 enum nss_status status = _nss_create_tablename (errnop);
464
465 if (status != NSS_STATUS_SUCCESS)
466 return status;
467 }
468
469 if (addr == NULL)
470 return NSS_STATUS_NOTFOUND;
471 else
472 {
473 nis_result *result;
474 char buf[255 + tablename_len];
475 int retval, parse_res;
476 int olderr = errno;
477
478 sprintf (buf, "[addr=%s],%s",
479 inet_ntoa (*(const struct in_addr *) addr), tablename_val);
480 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
481
482 retval = niserr2nss (result->status);
483 if (retval != NSS_STATUS_SUCCESS)
484 {
485 if (retval == NSS_STATUS_TRYAGAIN)
486 {
487 *errnop = errno;
488 *herrnop = NETDB_INTERNAL;
489 }
490 else
491 __set_errno (olderr);
492 nis_freeresult (result);
493 return retval;
494 }
495
496 parse_res = _nss_nisplus_parse_hostent (result, af, host,
497 buffer, buflen, errnop,
498 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0));
499 nis_freeresult (result);
500
501 if (parse_res > 0)
502 return NSS_STATUS_SUCCESS;
503
504 *herrnop = NETDB_INTERNAL;
505 if (parse_res == -1)
506 {
507 *errnop = ERANGE;
508 return NSS_STATUS_TRYAGAIN;
509 }
510 else
511 {
512 __set_errno (olderr);
513 return NSS_STATUS_NOTFOUND;
514 }
515 }
516 }