1 /* Copyright (C) 1997-2002, 2003, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <rpcsvc/nis.h>
29 #include <bits/libc-lock.h>
31 #include "nss-nisplus.h"
33 __libc_lock_define_initialized (static, lock
)
35 static nis_result
*result
;
36 static nis_name tablename_val
;
37 static u_long tablename_len
;
39 #define NISENTRYVAL(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
42 #define NISENTRYLEN(idx,col,res) \
43 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
45 /* Get implementation for some internal functions. */
46 #include <resolv/mapv4v6addr.h>
49 _nss_nisplus_parse_hostent (nis_result
*result
, int af
, struct hostent
*host
,
50 char *buffer
, size_t buflen
, int *errnop
,
54 char *first_unused
= buffer
;
55 size_t room_left
= buflen
;
56 char *data
, *p
, *line
;
61 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
) ||
62 __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
||
63 strcmp(result
->objects
.objects_val
[0].EN_data
.en_type
,
65 result
->objects
.objects_val
[0].EN_data
.en_cols
.en_cols_len
< 4)
68 if (room_left
< NISENTRYLEN (0, 2, result
) + 1)
78 if (af
== AF_INET
&& inet_pton (af
, NISENTRYVAL (0, 2, result
), data
) > 0)
80 if (flags
& AI_V4MAPPED
)
82 map_v4v6_address (data
, data
);
83 host
->h_addrtype
= AF_INET6
;
84 host
->h_length
= IN6ADDRSZ
;
88 host
->h_addrtype
= AF_INET
;
89 host
->h_length
= INADDRSZ
;
92 else if (af
== AF_INET6
93 && inet_pton (AF_INET6
, NISENTRYVAL (0, 2, result
), data
) > 0)
95 host
->h_addrtype
= AF_INET6
;
96 host
->h_length
= IN6ADDRSZ
;
99 /* Illegal address: ignore line. */
102 first_unused
+=host
->h_length
;
103 room_left
-=host
->h_length
;
105 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
108 p
= __stpncpy (first_unused
, NISENTRYVAL (0, 0, result
),
109 NISENTRYLEN (0, 0, result
));
111 room_left
-= (NISENTRYLEN (0, 0, result
) + 1);
112 host
->h_name
= first_unused
;
113 first_unused
+= NISENTRYLEN (0, 0, result
) +1;
117 for (i
= 0; i
< result
->objects
.objects_len
; ++i
)
119 if (strcmp (NISENTRYVAL (i
, 1, result
), host
->h_name
) != 0)
121 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
125 p
= __stpncpy (p
, NISENTRYVAL (i
, 1, result
),
126 NISENTRYLEN (i
, 1, result
));
128 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
134 /* Adjust the pointer so it is aligned for
136 first_unused
+= __alignof__ (char *) - 1;
137 first_unused
-= ((first_unused
- (char *) 0) % __alignof__ (char *));
138 host
->h_addr_list
= (char **) first_unused
;
139 if (room_left
< 2 * sizeof (char *))
142 room_left
-= (2 * sizeof (char *));
143 host
->h_addr_list
[0] = data
;
144 host
->h_addr_list
[1] = NULL
;
145 host
->h_aliases
= &host
->h_addr_list
[2];
146 host
->h_aliases
[0] = NULL
;
149 while (*line
!= '\0')
151 /* Skip leading blanks. */
152 while (isspace (*line
))
158 if (room_left
< sizeof (char *))
161 room_left
-= sizeof (char *);
162 host
->h_aliases
[i
] = line
;
164 while (*line
!= '\0' && *line
!= ' ')
174 host
->h_aliases
[i
+1] = NULL
;
179 static enum nss_status
180 _nss_create_tablename (int *errnop
)
182 if (tablename_val
== NULL
)
184 const char *local_dir
= nis_local_directory ();
185 size_t local_dir_len
= strlen (local_dir
);
186 static const char prefix
[] = "hosts.org_dir.";
188 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
189 if (tablename_val
== NULL
)
192 return NSS_STATUS_TRYAGAIN
;
195 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
197 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
199 atomic_write_barrier ();
204 return NSS_STATUS_SUCCESS
;
208 _nss_nisplus_sethostent (int stayopen
)
210 enum nss_status status
= NSS_STATUS_SUCCESS
;
213 __libc_lock_lock (lock
);
217 nis_freeresult (result
);
221 if (tablename_val
== NULL
)
222 status
= _nss_create_tablename (&err
);
224 __libc_lock_unlock (lock
);
230 _nss_nisplus_endhostent (void)
232 __libc_lock_lock (lock
);
236 nis_freeresult (result
);
240 __libc_lock_unlock (lock
);
242 return NSS_STATUS_SUCCESS
;
245 static enum nss_status
246 internal_nisplus_gethostent_r (struct hostent
*host
, char *buffer
,
247 size_t buflen
, int *errnop
, int *herrnop
)
251 /* Get the next entry until we found a correct one. */
254 nis_result
*saved_res
;
259 if (tablename_val
== NULL
)
261 enum nss_status status
= _nss_create_tablename (errnop
);
263 if (status
!= NSS_STATUS_SUCCESS
)
267 result
= nis_first_entry (tablename_val
);
268 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
270 enum nss_status retval
= niserr2nss (result
->status
);
271 if (retval
== NSS_STATUS_TRYAGAIN
)
273 *herrnop
= NETDB_INTERNAL
;
285 res2
= nis_next_entry(tablename_val
, &result
->cookie
);
287 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
289 enum nss_status retval
= niserr2nss (result
->status
);
291 nis_freeresult (result
);
293 if (retval
== NSS_STATUS_TRYAGAIN
)
295 *herrnop
= NETDB_INTERNAL
;
302 if (_res
.options
& RES_USE_INET6
)
303 parse_res
= _nss_nisplus_parse_hostent (result
, AF_INET6
, host
, buffer
,
304 buflen
, errnop
, AI_V4MAPPED
);
306 parse_res
= _nss_nisplus_parse_hostent (result
, AF_INET
, host
, buffer
,
311 nis_freeresult (result
);
313 *herrnop
= NETDB_INTERNAL
;
315 return NSS_STATUS_TRYAGAIN
;
317 if (saved_res
!= NULL
)
318 nis_freeresult (saved_res
);
320 } while (!parse_res
);
322 return NSS_STATUS_SUCCESS
;
326 _nss_nisplus_gethostent_r (struct hostent
*result
, char *buffer
,
327 size_t buflen
, int *errnop
, int *herrnop
)
331 __libc_lock_lock (lock
);
333 status
= internal_nisplus_gethostent_r (result
, buffer
, buflen
, errnop
,
336 __libc_lock_unlock (lock
);
341 static enum nss_status
342 internal_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
343 char *buffer
, size_t buflen
, int *errnop
,
344 int *herrnop
, int flags
)
346 int parse_res
, retval
;
348 if (tablename_val
== NULL
)
350 __libc_lock_lock (lock
);
352 enum nss_status status
= _nss_create_tablename (errnop
);
354 __libc_lock_unlock (lock
);
356 if (status
!= NSS_STATUS_SUCCESS
)
358 *herrnop
= NETDB_INTERNAL
;
359 return NSS_STATUS_UNAVAIL
;
366 *herrnop
= NETDB_INTERNAL
;
367 return NSS_STATUS_NOTFOUND
;
371 char buf
[strlen (name
) + 10 + tablename_len
];
374 /* Search at first in the alias list, and use the correct name
375 for the next search. */
376 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
377 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
383 /* If we did not find it, try it as original name. But if the
384 database is correct, we should find it in the first case, too. */
385 if ((result
->status
!= NIS_SUCCESS
386 && result
->status
!= NIS_S_SUCCESS
)
387 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
388 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
390 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 3)
391 snprintf (buf
, sizeof (buf
), "[cname=%s],%s", name
, tablename_val
);
394 /* We need to allocate a new buffer since there is no
395 guarantee the returned name has a length limit. */
396 const char *entryval
= NISENTRYVAL(0, 0, result
);
397 size_t buflen
= strlen (entryval
) + 10 + tablename_len
;
398 bufptr
= alloca (buflen
);
399 snprintf (bufptr
, buflen
, "[cname=%s],%s",
400 entryval
, tablename_val
);
403 nis_freeresult (result
);
404 result
= nis_list (bufptr
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
410 return NSS_STATUS_TRYAGAIN
;
413 retval
= niserr2nss (result
->status
);
414 if (__builtin_expect (retval
!= NSS_STATUS_SUCCESS
, 0))
416 if (retval
== NSS_STATUS_TRYAGAIN
)
419 *herrnop
= NETDB_INTERNAL
;
422 __set_errno (olderr
);
423 nis_freeresult (result
);
427 parse_res
= _nss_nisplus_parse_hostent (result
, af
, host
, buffer
,
428 buflen
, errnop
, flags
);
430 nis_freeresult (result
);
433 return NSS_STATUS_SUCCESS
;
435 *herrnop
= NETDB_INTERNAL
;
439 return NSS_STATUS_TRYAGAIN
;
442 __set_errno (olderr
);
443 return NSS_STATUS_NOTFOUND
;
447 _nss_nisplus_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
448 char *buffer
, size_t buflen
, int *errnop
,
451 return internal_gethostbyname2_r (name
, af
, host
, buffer
, buflen
, errnop
,
453 ((_res
.options
& RES_USE_INET6
) ? AI_V4MAPPED
: 0));
457 _nss_nisplus_gethostbyname_r (const char *name
, struct hostent
*host
,
458 char *buffer
, size_t buflen
, int *errnop
,
461 if (_res
.options
& RES_USE_INET6
)
463 enum nss_status status
;
465 status
= internal_gethostbyname2_r (name
, AF_INET6
, host
, buffer
,
466 buflen
, errnop
, h_errnop
,
468 if (status
== NSS_STATUS_SUCCESS
)
472 return internal_gethostbyname2_r (name
, AF_INET
, host
, buffer
,
473 buflen
, errnop
, h_errnop
, 0);
477 _nss_nisplus_gethostbyaddr_r (const void *addr
, socklen_t addrlen
, int af
,
478 struct hostent
*host
, char *buffer
,
479 size_t buflen
, int *errnop
, int *herrnop
)
481 if (tablename_val
== NULL
)
483 __libc_lock_lock (lock
);
485 enum nss_status status
= _nss_create_tablename (errnop
);
487 __libc_lock_unlock (lock
);
489 if (status
!= NSS_STATUS_SUCCESS
)
494 return NSS_STATUS_NOTFOUND
;
496 char buf
[24 + tablename_len
];
497 int retval
, parse_res
;
500 snprintf (buf
, sizeof (buf
), "[addr=%s],%s",
501 inet_ntoa (*(const struct in_addr
*) addr
), tablename_val
);
502 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
506 __set_errno (ENOMEM
);
507 return NSS_STATUS_TRYAGAIN
;
510 retval
= niserr2nss (result
->status
);
511 if (__builtin_expect (retval
!= NSS_STATUS_SUCCESS
, 0))
513 if (retval
== NSS_STATUS_TRYAGAIN
)
516 *herrnop
= NETDB_INTERNAL
;
519 __set_errno (olderr
);
520 nis_freeresult (result
);
524 parse_res
= _nss_nisplus_parse_hostent (result
, af
, host
,
525 buffer
, buflen
, errnop
,
526 ((_res
.options
& RES_USE_INET6
)
528 nis_freeresult (result
);
531 return NSS_STATUS_SUCCESS
;
533 *herrnop
= NETDB_INTERNAL
;
537 return NSS_STATUS_TRYAGAIN
;
540 __set_errno (olderr
);
541 return NSS_STATUS_NOTFOUND
;