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"
48 _nss_nisplus_parse_hostent (nis_result
*result
, int af
, struct hostent
*host
,
49 char *buffer
, size_t buflen
)
52 char *first_unused
= buffer
;
53 size_t room_left
= buflen
;
54 char *data
, *p
, *line
;
59 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
) ||
60 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
61 strcmp(result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
63 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 4)
66 if (room_left
< NISENTRYLEN (0, 2, result
) + 1)
73 if (inet_pton (af
, NISENTRYVAL (0, 2, result
), data
) < 1)
74 /* Illegal address: ignore line. */
77 host
->h_addrtype
= af
;
79 host
->h_length
= IN6ADDRSZ
;
82 if (_res
.options
& RES_USE_INET6
)
84 map_v4v6_address (data
, data
);
85 host
->h_addrtype
= AF_INET6
;
86 host
->h_length
= IN6ADDRSZ
;
90 host
->h_addrtype
= AF_INET
;
91 host
->h_length
= INADDRSZ
;
94 first_unused
+=host
->h_length
;
95 room_left
-=host
->h_length
;
97 if (NISENTRYLEN (0, 0, result
) + 1 > room_left
)
102 p
= stpncpy (first_unused
, NISENTRYVAL (0, 0, result
),
103 NISENTRYLEN (0, 0, result
));
105 room_left
-= (NISENTRYLEN (0, 0, result
) + 1);
106 host
->h_name
= first_unused
;
107 first_unused
+= NISENTRYLEN (0, 0, result
) +1;
111 for (i
= 0; i
< result
->objects
.objects_len
; i
++)
113 if (strcmp (NISENTRYVAL (i
, 1, result
), host
->h_name
) != 0)
115 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
117 __set_errno (ERANGE
);
121 p
= stpncpy (p
, NISENTRYVAL (i
, 1, result
),
122 NISENTRYLEN (i
, 1, result
));
124 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
129 /* Adjust the pointer so it is aligned for
131 first_unused
+= __alignof__ (char *) - 1;
132 first_unused
-= ((first_unused
- (char *) 0) % __alignof__ (char *));
133 host
->h_addr_list
= (char **) first_unused
;
134 if (room_left
< 2 * sizeof (char *))
136 __set_errno (ERANGE
);
139 room_left
-= (2 * sizeof (char *));
140 host
->h_addr_list
[0] = data
;
141 host
->h_addr_list
[1] = NULL
;
142 host
->h_aliases
= &host
->h_addr_list
[2];
143 host
->h_aliases
[0] = NULL
;
146 while (*line
!= '\0')
148 /* Skip leading blanks. */
149 while (isspace (*line
))
155 if (room_left
< sizeof (char *))
157 __set_errno (ERANGE
);
161 room_left
-= sizeof (char *);
162 host
->h_aliases
[i
] = line
;
164 while (*line
!= '\0' && *line
!= ' ')
167 if (line
!= host
->h_aliases
[i
])
180 _nss_nisplus_sethostent (void)
182 __libc_lock_lock (lock
);
185 nis_freeresult (result
);
189 nis_freenames (names
);
193 __libc_lock_unlock (lock
);
195 return NSS_STATUS_SUCCESS
;
199 _nss_nisplus_endhostent (void)
201 __libc_lock_lock (lock
);
204 nis_freeresult (result
);
208 nis_freenames (names
);
212 __libc_lock_unlock (lock
);
214 return NSS_STATUS_SUCCESS
;
217 static enum nss_status
218 internal_nisplus_gethostent_r (struct hostent
*host
, char *buffer
,
219 size_t buflen
, int *herrnop
)
223 /* Get the next entry until we found a correct one. */
228 names
= nis_getnames("hosts.org_dir");
229 if (names
== NULL
|| names
[0] == NULL
)
230 return NSS_STATUS_UNAVAIL
;
232 result
= nis_first_entry(names
[0]);
233 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
237 retval
= niserr2nss (result
->status
);
238 if (retval
== NSS_STATUS_TRYAGAIN
)
240 *herrnop
= NETDB_INTERNAL
;
241 __set_errno (EAGAIN
);
251 res2
= nis_next_entry(names
[0], &result
->cookie
);
252 nis_freeresult (result
);
254 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
258 retval
= niserr2nss (result
->status
);
259 if (retval
== NSS_STATUS_TRYAGAIN
)
261 *herrnop
= NETDB_INTERNAL
;
262 __set_errno (EAGAIN
);
268 parse_res
= _nss_nisplus_parse_hostent (result
, AF_INET6
,
269 host
, buffer
, buflen
);
270 if (!parse_res
&& errno
!= ERANGE
)
271 parse_res
= _nss_nisplus_parse_hostent (result
, AF_INET
, host
,
273 if (!parse_res
&& errno
== ERANGE
)
275 *herrnop
= NETDB_INTERNAL
;
276 return NSS_STATUS_TRYAGAIN
;
279 } while (!parse_res
);
281 return NSS_STATUS_SUCCESS
;
285 _nss_nisplus_gethostent_r (struct hostent
*result
, char *buffer
,
286 size_t buflen
, int *herrnop
)
290 __libc_lock_lock (lock
);
292 status
= internal_nisplus_gethostent_r (result
, buffer
, buflen
, herrnop
);
294 __libc_lock_unlock (lock
);
300 _nss_nisplus_gethostbyname2_r (const char *name
, int af
, struct hostent
*host
,
301 char *buffer
, size_t buflen
, int *herrnop
)
303 int parse_res
, retval
;
307 __set_errno (EINVAL
);
308 *herrnop
= NETDB_INTERNAL
;
309 return NSS_STATUS_NOTFOUND
;
314 char buf
[strlen (name
) + 255];
316 /* Search at first in the alias list, and use the correct name
317 for the next search */
318 sprintf(buf
, "[name=%s],hosts.org_dir", name
);
319 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
321 /* If we do not find it, try it as original name. But if the
322 database is correct, we should find it in the first case, too */
323 if ((result
->status
!= NIS_SUCCESS
&&
324 result
->status
!= NIS_S_SUCCESS
) ||
325 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
326 strcmp(result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
328 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
330 sprintf(buf
, "[cname=%s],hosts.org_dir", name
);
332 sprintf(buf
, "[cname=%s],hosts.org_dir", NISENTRYVAL(0, 0, result
));
334 nis_freeresult (result
);
335 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
337 retval
= niserr2nss (result
->status
);
338 if (retval
!= NSS_STATUS_SUCCESS
)
340 if (retval
== NSS_STATUS_TRYAGAIN
)
342 __set_errno (EAGAIN
);
343 *herrnop
= NETDB_INTERNAL
;
345 nis_freeresult (result
);
350 _nss_nisplus_parse_hostent (result
, af
, host
, buffer
, buflen
);
352 nis_freeresult (result
);
355 return NSS_STATUS_SUCCESS
;
357 *herrnop
= NETDB_INTERNAL
;
358 if (!parse_res
&& errno
== ERANGE
)
359 return NSS_STATUS_TRYAGAIN
;
361 return NSS_STATUS_NOTFOUND
;
366 _nss_nisplus_gethostbyname_r (const char *name
, struct hostent
*host
,
367 char *buffer
, size_t buflen
, int *h_errnop
)
369 if (_res
.options
& RES_USE_INET6
)
371 enum nss_status status
;
373 status
= _nss_nisplus_gethostbyname2_r (name
, AF_INET6
, host
, buffer
,
375 if (status
== NSS_STATUS_SUCCESS
)
379 return _nss_nisplus_gethostbyname2_r (name
, AF_INET
, host
, buffer
,
384 _nss_nisplus_gethostbyaddr_r (const char *addr
, int addrlen
, int type
,
385 struct hostent
*host
, char *buffer
,
386 size_t buflen
, int *herrnop
)
389 return NSS_STATUS_NOTFOUND
;
394 int retval
, parse_res
;
396 snprintf(buf
, sizeof (buf
) -1, "[addr=%s],hosts.org_dir",
397 inet_ntoa (*(struct in_addr
*)addr
));
398 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
400 retval
= niserr2nss (result
->status
);
401 if (retval
!= NSS_STATUS_SUCCESS
)
403 if (retval
== NSS_STATUS_TRYAGAIN
)
405 __set_errno (EAGAIN
);
406 *herrnop
= NETDB_INTERNAL
;
408 nis_freeresult (result
);
412 parse_res
= _nss_nisplus_parse_hostent (result
, type
, host
,
415 nis_freeresult (result
);
418 return NSS_STATUS_SUCCESS
;
420 *herrnop
= NETDB_INTERNAL
;
421 if (!parse_res
&& errno
== ERANGE
)
422 return NSS_STATUS_TRYAGAIN
;
424 return NSS_STATUS_NOTFOUND
;