1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 <libc-lock.h>
28 #include <rpcsvc/nis.h>
29 #include <rpcsvc/nislib.h>
31 #include "nss-nisplus.h"
33 __libc_lock_define_initialized (static, lock
)
35 static nis_result
*result
= NULL
;
36 static nis_name
*names
= NULL
;
38 #define NISENTRYVAL(idx,col,res) \
39 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.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)].zo_data.objdata_u.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"
46 #include "../../resolv/mapv4v6hostent.h"
48 #define ENTNAME hostent
49 #define DATABASE "hosts"
52 #define ENTDATA hostent_data
55 unsigned char host_addr
[16]; /* IPv4 or IPv6 address. */
56 char *h_addr_ptrs
[2]; /* Points to that and null terminator. */
59 #define TRAILING_LIST_MEMBER h_aliases
60 #define TRAILING_LIST_SEPARATOR_P isspace
61 #include "../../nss/nss_files/files-parse.c"
67 STRING_FIELD (addr
, isspace
, 1);
70 if ((_res
.options
& RES_USE_INET6
)
71 && inet_pton (AF_INET6
, addr
, entdata
->host_addr
) > 0)
73 result
->h_addrtype
= AF_INET6
;
74 result
->h_length
= IN6ADDRSZ
;
77 if (inet_pton (AF_INET
, addr
, entdata
->host_addr
) > 0)
79 if (_res
.options
& RES_USE_INET6
)
81 map_v4v6_address ((char *) entdata
->host_addr
,
82 (char *) entdata
->host_addr
);
83 result
->h_addrtype
= AF_INET6
;
84 result
->h_length
= IN6ADDRSZ
;
88 result
->h_addrtype
= AF_INET
;
89 result
->h_length
= INADDRSZ
;
93 /* Illegal address: ignore line. */
96 /* Store a pointer to the address in the expected form. */
97 entdata
->h_addr_ptrs
[0] = entdata
->host_addr
;
98 entdata
->h_addr_ptrs
[1] = NULL
;
99 result
->h_addr_list
= entdata
->h_addr_ptrs
;
101 /* If we need the host entry in IPv6 form change it now. */
102 if (_res
.options
& RES_USE_INET6
)
104 char *bufptr
= data
->linebuffer
;
105 size_t buflen
= (char *) data
+ datalen
- bufptr
;
106 map_v4v6_hostent (result
, &bufptr
, &buflen
);
109 STRING_FIELD (result
->h_name
, isspace
, 1);
115 _nss_nisplus_parse_hostent (nis_result
*result
, struct hostent
*host
,
116 char *buffer
, size_t buflen
)
119 size_t room_left
= buflen
;
122 struct parser_data
*data
= (void *) buffer
;
127 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
) ||
128 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
129 strcmp(result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
131 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 4)
134 memset (p
, '\0', room_left
);
136 /* Generate the hosts entry format and use the normal parser */
137 if (NISENTRYLEN (0, 2, result
) + 1 > room_left
)
139 __set_errno (ERANGE
);
142 strncpy (p
, NISENTRYVAL (0, 2, result
),
143 NISENTRYLEN (0, 2, result
));
144 room_left
-= (NISENTRYLEN (0, 2, result
) + 1);
146 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
148 __set_errno (ERANGE
);
152 strncat (p
, NISENTRYVAL (0, 0, result
), NISENTRYLEN (0, 0, result
));
153 room_left
-= (NISENTRYLEN (0, 0, result
) + 1);
154 /* + 1: We overwrite the last \0 */
156 for (i
= 1; i
< result
->objects
.objects_len
; i
++)
158 if (NISENTRYLEN (i
, 1, result
) + 1 > room_left
)
160 __set_errno (ERANGE
);
164 strcat (p
, NISENTRYVAL (i
, 1, result
));
165 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
168 parse_res
= parse_line (p
, host
, data
, buflen
);
174 _nss_nisplus_sethostent (void)
176 __libc_lock_lock (lock
);
179 nis_freeresult (result
);
183 nis_freenames (names
);
187 __libc_lock_unlock (lock
);
189 return NSS_STATUS_SUCCESS
;
193 _nss_nisplus_endhostent (void)
195 __libc_lock_lock (lock
);
198 nis_freeresult (result
);
202 nis_freenames (names
);
206 __libc_lock_unlock (lock
);
208 return NSS_STATUS_SUCCESS
;
211 static enum nss_status
212 internal_nisplus_gethostent_r (struct hostent
*host
, char *buffer
,
213 size_t buflen
, int *herrnop
)
217 /* Get the next entry until we found a correct one. */
222 names
= nis_getnames("hosts.org_dir");
223 if (names
== NULL
|| names
[0] == NULL
)
224 return NSS_STATUS_UNAVAIL
;
226 result
= nis_first_entry(names
[0]);
227 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
231 retval
= niserr2nss (result
->status
);
232 if (retval
== NSS_STATUS_TRYAGAIN
)
234 *herrnop
= NETDB_INTERNAL
;
235 __set_errno (EAGAIN
);
245 res2
= nis_next_entry(names
[0], &result
->cookie
);
246 nis_freeresult (result
);
248 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
252 retval
= niserr2nss (result
->status
);
253 if (retval
== NSS_STATUS_TRYAGAIN
)
255 *herrnop
= NETDB_INTERNAL
;
256 __set_errno (EAGAIN
);
262 parse_res
= _nss_nisplus_parse_hostent (result
, host
, buffer
, buflen
);
263 if (!parse_res
&& errno
== ERANGE
)
265 *herrnop
= NETDB_INTERNAL
;
266 return NSS_STATUS_TRYAGAIN
;
269 } while (!parse_res
);
271 return NSS_STATUS_SUCCESS
;
275 _nss_nisplus_gethostent_r (struct hostent
*result
, char *buffer
,
276 size_t buflen
, int *herrnop
)
280 __libc_lock_lock (lock
);
282 status
= internal_nisplus_gethostent_r (result
, buffer
, buflen
, herrnop
);
284 __libc_lock_unlock (lock
);
290 _nss_nisplus_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
291 char *buffer
, size_t buflen
, int *herrnop
)
293 int parse_res
, retval
;
297 __set_errno (EINVAL
);
298 *herrnop
= NETDB_INTERNAL
;
299 return NSS_STATUS_NOTFOUND
;
304 char buf
[strlen (name
) + 255];
306 /* Search at first in the alias list, and use the correct name
307 for the next search */
308 sprintf(buf
, "[name=%s],hosts.org_dir", name
);
309 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
311 /* If we do not find it, try it as original name. But if the
312 database is correct, we should find it in the first case, too */
313 if ((result
->status
!= NIS_SUCCESS
&&
314 result
->status
!= NIS_S_SUCCESS
) ||
315 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
316 strcmp(result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
318 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
320 sprintf(buf
, "[cname=%s],hosts.org_dir", name
);
322 sprintf(buf
, "[cname=%s],hosts.org_dir", NISENTRYVAL(0, 0, result
));
324 nis_freeresult (result
);
325 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
327 retval
= niserr2nss (result
->status
);
328 if (retval
!= NSS_STATUS_SUCCESS
)
330 if (retval
== NSS_STATUS_TRYAGAIN
)
332 __set_errno (EAGAIN
);
333 *herrnop
= NETDB_INTERNAL
;
335 nis_freeresult (result
);
339 parse_res
= _nss_nisplus_parse_hostent (result
, host
, buffer
, buflen
);
341 nis_freeresult (result
);
344 return NSS_STATUS_SUCCESS
;
346 *herrnop
= NETDB_INTERNAL
;
347 if (!parse_res
&& errno
== ERANGE
)
348 return NSS_STATUS_TRYAGAIN
;
350 return NSS_STATUS_NOTFOUND
;
355 _nss_nisplus_gethostbyname_r (const char *name
, struct hostent
*host
,
356 char *buffer
, size_t buflen
, int *h_errnop
)
358 if (_res
.options
& RES_USE_INET6
)
360 enum nss_status status
;
362 status
= _nss_nisplus_gethostbyname2_r (name
, AF_INET6
, host
, buffer
,
364 if (status
== NSS_STATUS_SUCCESS
)
368 return _nss_nisplus_gethostbyname2_r (name
, AF_INET
, host
, buffer
,
373 _nss_nisplus_gethostbyaddr_r (const char *addr
, struct hostent
*host
,
374 char *buffer
, size_t buflen
, int *herrnop
)
377 return NSS_STATUS_NOTFOUND
;
381 char buf
[24 + strlen (addr
)];
382 int retval
, parse_res
;
384 sprintf(buf
, "[addr=%s],hosts.org_dir", addr
);
386 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
388 retval
= niserr2nss (result
->status
);
389 if (retval
!= NSS_STATUS_SUCCESS
)
391 if (retval
== NSS_STATUS_TRYAGAIN
)
393 __set_errno (EAGAIN
);
394 *herrnop
= NETDB_INTERNAL
;
396 nis_freeresult (result
);
400 parse_res
= _nss_nisplus_parse_hostent (result
, host
, buffer
, buflen
);
402 nis_freeresult (result
);
405 return NSS_STATUS_SUCCESS
;
407 *herrnop
= NETDB_INTERNAL
;
408 if (!parse_res
&& errno
== ERANGE
)
409 return NSS_STATUS_TRYAGAIN
;
411 return NSS_STATUS_NOTFOUND
;