1 /* Copyright (C) 1997, 1998, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/nis.h>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock
)
34 static nis_result
*result
= NULL
;
35 static nis_name tablename_val
= NULL
;
36 static u_long tablename_len
= 0;
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)
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)
44 /* Get implementation for some internal functions. */
45 #include <resolv/mapv4v6addr.h>
48 _nss_nisplus_parse_hostent (nis_result
*result
, int af
, struct hostent
*host
,
49 char *buffer
, size_t buflen
, int *errnop
,
53 char *first_unused
= buffer
;
54 size_t room_left
= buflen
;
55 char *data
, *p
, *line
;
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
,
64 result
->objects
.objects_val
[0].EN_data
.en_cols
.en_cols_len
< 4)
67 if (room_left
< NISENTRYLEN (0, 2, result
) + 1)
77 if (af
== AF_INET
&& inet_pton (af
, NISENTRYVAL (0, 2, result
), data
) > 0)
79 if (flags
& AI_V4MAPPED
)
81 map_v4v6_address (data
, data
);
82 host
->h_addrtype
= AF_INET6
;
83 host
->h_length
= IN6ADDRSZ
;
87 host
->h_addrtype
= AF_INET
;
88 host
->h_length
= INADDRSZ
;
91 else if (af
== AF_INET6
92 && inet_pton (AF_INET6
, NISENTRYVAL (0, 2, result
), data
) > 0)
94 host
->h_addrtype
= AF_INET6
;
95 host
->h_length
= IN6ADDRSZ
;
98 /* Illegal address: ignore line. */
101 first_unused
+=host
->h_length
;
102 room_left
-=host
->h_length
;
104 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
107 p
= __stpncpy (first_unused
, NISENTRYVAL (0, 0, result
),
108 NISENTRYLEN (0, 0, result
));
110 room_left
-= (NISENTRYLEN (0, 0, result
) + 1);
111 host
->h_name
= first_unused
;
112 first_unused
+= NISENTRYLEN (0, 0, result
) +1;
116 for (i
= 0; i
< result
->objects
.objects_len
; ++i
)
118 if (strcmp (NISENTRYVAL (i
, 1, result
), host
->h_name
) != 0)
120 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
124 p
= __stpncpy (p
, NISENTRYVAL (i
, 1, result
),
125 NISENTRYLEN (i
, 1, result
));
127 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
133 /* Adjust the pointer so it is aligned for
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 *))
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
;
148 while (*line
!= '\0')
150 /* Skip leading blanks. */
151 while (isspace (*line
))
157 if (room_left
< sizeof (char *))
160 room_left
-= sizeof (char *);
161 host
->h_aliases
[i
] = line
;
163 while (*line
!= '\0' && *line
!= ' ')
173 host
->h_aliases
[i
+1] = NULL
;
178 static enum nss_status
179 _nss_create_tablename (int *errnop
)
181 if (tablename_val
== NULL
)
183 char buf
[40 + strlen (nis_local_directory ())];
186 p
= __stpcpy (buf
, "hosts.org_dir.");
187 p
= __stpcpy (p
, nis_local_directory ());
188 tablename_val
= __strdup (buf
);
189 if (tablename_val
== NULL
)
192 return NSS_STATUS_TRYAGAIN
;
194 tablename_len
= strlen (tablename_val
);
196 return NSS_STATUS_SUCCESS
;
200 _nss_nisplus_sethostent (void)
202 enum nss_status status
= NSS_STATUS_SUCCESS
;
205 __libc_lock_lock (lock
);
208 nis_freeresult (result
);
211 if (tablename_val
== NULL
)
212 status
= _nss_create_tablename (&err
);
214 __libc_lock_unlock (lock
);
220 _nss_nisplus_endhostent (void)
222 __libc_lock_lock (lock
);
225 nis_freeresult (result
);
228 __libc_lock_unlock (lock
);
230 return NSS_STATUS_SUCCESS
;
233 static enum nss_status
234 internal_nisplus_gethostent_r (struct hostent
*host
, char *buffer
,
235 size_t buflen
, int *errnop
, int *herrnop
)
239 /* Get the next entry until we found a correct one. */
242 nis_result
*saved_res
;
247 if (tablename_val
== NULL
)
249 enum nss_status status
= _nss_create_tablename (errnop
);
251 if (status
!= NSS_STATUS_SUCCESS
)
255 result
= nis_first_entry (tablename_val
);
256 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
258 enum nss_status retval
= niserr2nss (result
->status
);
259 if (retval
== NSS_STATUS_TRYAGAIN
)
261 *herrnop
= NETDB_INTERNAL
;
273 res2
= nis_next_entry(tablename_val
, &result
->cookie
);
275 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
277 enum nss_status retval
= niserr2nss (result
->status
);
279 nis_freeresult (result
);
281 if (retval
== NSS_STATUS_TRYAGAIN
)
283 *herrnop
= NETDB_INTERNAL
;
290 if (_res
.options
& RES_USE_INET6
)
291 parse_res
= _nss_nisplus_parse_hostent (result
, AF_INET6
, host
, buffer
,
292 buflen
, errnop
, AI_V4MAPPED
);
294 parse_res
= _nss_nisplus_parse_hostent (result
, AF_INET
, host
, buffer
,
299 nis_freeresult (result
);
301 *herrnop
= NETDB_INTERNAL
;
303 return NSS_STATUS_TRYAGAIN
;
305 if (saved_res
!= NULL
)
306 nis_freeresult (saved_res
);
308 } while (!parse_res
);
310 return NSS_STATUS_SUCCESS
;
314 _nss_nisplus_gethostent_r (struct hostent
*result
, char *buffer
,
315 size_t buflen
, int *errnop
, int *herrnop
)
319 __libc_lock_lock (lock
);
321 status
= internal_nisplus_gethostent_r (result
, buffer
, buflen
, errnop
,
324 __libc_lock_unlock (lock
);
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
)
334 int parse_res
, retval
;
336 if (tablename_val
== NULL
)
338 enum nss_status status
= _nss_create_tablename (errnop
);
340 if (status
!= NSS_STATUS_SUCCESS
)
342 *herrnop
= NETDB_INTERNAL
;
343 return NSS_STATUS_UNAVAIL
;
350 *herrnop
= NETDB_INTERNAL
;
351 return NSS_STATUS_NOTFOUND
;
356 char buf
[strlen (name
) + 255 + tablename_len
];
358 /* Search at first in the alias list, and use the correct name
359 for the next search */
360 sprintf (buf
, "[name=%s],%s", name
, tablename_val
);
361 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
363 /* If we do not find it, try it as original name. But if the
364 database is correct, we should find it in the first case, too */
365 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
366 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
367 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
369 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 3)
370 sprintf (buf
, "[cname=%s],%s", name
, tablename_val
);
372 sprintf (buf
, "[cname=%s],%s", NISENTRYVAL(0, 0, result
),
375 nis_freeresult (result
);
376 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
378 retval
= niserr2nss (result
->status
);
379 if (retval
!= NSS_STATUS_SUCCESS
)
381 if (retval
== NSS_STATUS_TRYAGAIN
)
384 *herrnop
= NETDB_INTERNAL
;
386 nis_freeresult (result
);
390 parse_res
= _nss_nisplus_parse_hostent (result
, af
, host
, buffer
,
391 buflen
, errnop
, flags
);
393 nis_freeresult (result
);
396 return NSS_STATUS_SUCCESS
;
398 *herrnop
= NETDB_INTERNAL
;
402 return NSS_STATUS_TRYAGAIN
;
405 return NSS_STATUS_NOTFOUND
;
410 _nss_nisplus_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
411 char *buffer
, size_t buflen
, int *errnop
,
414 return internal_gethostbyname2_r (name
, af
, host
, buffer
, buflen
, errnop
,
416 ((_res
.options
& RES_USE_INET6
) ? AI_V4MAPPED
: 0));
420 _nss_nisplus_getipnodebyname_r (const char *name
, int af
, int flags
,
421 struct hostent
*result
, char *buffer
,
422 size_t buflen
, int *errnop
, int *herrnop
)
424 return internal_gethostbyname2_r (name
, af
, result
, buffer
, buflen
,
425 errnop
, herrnop
, flags
);
429 _nss_nisplus_gethostbyname_r (const char *name
, struct hostent
*host
,
430 char *buffer
, size_t buflen
, int *errnop
,
433 if (_res
.options
& RES_USE_INET6
)
435 enum nss_status status
;
437 status
= internal_gethostbyname2_r (name
, AF_INET6
, host
, buffer
,
438 buflen
, errnop
, h_errnop
,
440 if (status
== NSS_STATUS_SUCCESS
)
444 return internal_gethostbyname2_r (name
, AF_INET
, host
, buffer
,
445 buflen
, errnop
, h_errnop
, 0);
449 _nss_nisplus_gethostbyaddr_r (const char *addr
, size_t addrlen
, int af
,
450 struct hostent
*host
, char *buffer
,
451 size_t buflen
, int *errnop
, int *herrnop
)
453 if (tablename_val
== NULL
)
455 enum nss_status status
= _nss_create_tablename (errnop
);
457 if (status
!= NSS_STATUS_SUCCESS
)
462 return NSS_STATUS_NOTFOUND
;
466 char buf
[255 + tablename_len
];
467 int retval
, parse_res
;
469 sprintf (buf
, "[addr=%s],%s",
470 inet_ntoa (*(struct in_addr
*) addr
), tablename_val
);
471 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
473 retval
= niserr2nss (result
->status
);
474 if (retval
!= NSS_STATUS_SUCCESS
)
476 if (retval
== NSS_STATUS_TRYAGAIN
)
479 *herrnop
= NETDB_INTERNAL
;
481 nis_freeresult (result
);
485 parse_res
= _nss_nisplus_parse_hostent (result
, af
, host
,
486 buffer
, buflen
, errnop
,
487 ((_res
.options
& RES_USE_INET6
) ? AI_V4MAPPED
: 0));
488 nis_freeresult (result
);
491 return NSS_STATUS_SUCCESS
;
493 *herrnop
= NETDB_INTERNAL
;
497 return NSS_STATUS_TRYAGAIN
;
500 return NSS_STATUS_NOTFOUND
;