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 <arpa/inet.h>
26 #include <libc-lock.h>
27 #include <rpcsvc/nis.h>
28 #include <rpcsvc/nislib.h>
30 #include "nss-nisplus.h"
32 /* Get the declaration of the parser function. */
33 #define ENTNAME netent
34 #define DATABASE "networks"
35 #define TRAILING_LIST_MEMBER n_aliases
36 #define TRAILING_LIST_SEPARATOR_P isspace
37 #include "../nss/nss_files/files-parse.c"
43 STRING_FIELD (result
->n_name
, isspace
, 1);
45 STRING_FIELD (addr
, isspace
, 1);
46 result
->n_net
= inet_network (addr
);
50 __libc_lock_define_initialized (static, lock
)
52 static nis_result
*result
= NULL
;
53 static nis_name
*names
= NULL
;
55 #define NISENTRYVAL(idx,col,res) \
56 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
58 #define NISENTRYLEN(idx,col,res) \
59 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
63 _nss_nisplus_parse_netent (nis_result
*result
, struct netent
*network
,
64 char *buffer
, size_t buflen
)
67 size_t room_left
= buflen
;
69 struct parser_data
*data
= (void *) buffer
;
74 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
) ||
75 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
76 strcmp(result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
77 "networks_tbl") != 0 ||
78 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 3)
81 /* Generate the network entry format and use the normal parser */
82 if (NISENTRYLEN (0, 0, result
) +1 > room_left
)
88 memset (p
, '\0', room_left
);
90 strncpy (p
, NISENTRYVAL (0, 0, result
), NISENTRYLEN (0, 0, result
));
91 room_left
-= (NISENTRYLEN (0, 0, result
) +1);
93 if (NISENTRYLEN (0, 2, result
) +1 > room_left
)
99 strncat (p
, NISENTRYVAL (0, 2, result
), NISENTRYLEN (0, 2, result
));
100 room_left
-= (NISENTRYLEN (0, 2, result
) + 1);
101 /* + 1: We overwrite the last \0 */
103 for (i
= 1; i
< result
->objects
.objects_len
; i
++)
105 if (NISENTRYLEN (i
, 1, result
) +1 > room_left
)
107 __set_errno (ERANGE
);
111 strncat (p
, NISENTRYVAL (i
, 1, result
), NISENTRYLEN (i
, 1, result
));
112 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
115 return _nss_files_parse_netent (p
, network
, data
, buflen
);
119 _nss_nisplus_setnetent (void)
121 __libc_lock_lock (lock
);
124 nis_freeresult (result
);
128 nis_freenames (names
);
132 __libc_lock_unlock (lock
);
134 return NSS_STATUS_SUCCESS
;
138 _nss_nisplus_endnetent (void)
140 __libc_lock_lock (lock
);
143 nis_freeresult (result
);
147 nis_freenames (names
);
151 __libc_lock_unlock (lock
);
153 return NSS_STATUS_SUCCESS
;
156 static enum nss_status
157 internal_nisplus_getnetent_r (struct netent
*network
, char *buffer
,
158 size_t buflen
, int *herrnop
)
162 /* Get the next entry until we found a correct one. */
167 names
= nis_getnames("networks.org_dir");
168 if (names
== NULL
|| names
[0] == NULL
)
169 return NSS_STATUS_UNAVAIL
;
171 result
= nis_first_entry(names
[0]);
172 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
176 retval
= niserr2nss (result
->status
);
177 if (retval
== NSS_STATUS_TRYAGAIN
)
179 *herrnop
= NETDB_INTERNAL
;
180 __set_errno (EAGAIN
);
189 res
= nis_next_entry(names
[0], &result
->cookie
);
190 nis_freeresult (result
);
192 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
196 retval
= niserr2nss (result
->status
);
197 if (retval
== NSS_STATUS_TRYAGAIN
)
199 *herrnop
= NETDB_INTERNAL
;
200 __set_errno (EAGAIN
);
206 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
, buflen
);
207 if (!parse_res
&& errno
== ERANGE
)
209 *herrnop
= NETDB_INTERNAL
;
210 return NSS_STATUS_TRYAGAIN
;
213 } while (!parse_res
);
215 return NSS_STATUS_SUCCESS
;
219 _nss_nisplus_getnetent_r (struct netent
*result
, char *buffer
,
220 size_t buflen
, int *herrnop
)
224 __libc_lock_lock (lock
);
226 status
= internal_nisplus_getnetent_r (result
, buffer
, buflen
, herrnop
);
228 __libc_lock_unlock (lock
);
234 _nss_nisplus_getnetbyname_r (const char *name
, struct netent
*network
,
235 char *buffer
, size_t buflen
, int *herrnop
)
237 int parse_res
, retval
;
241 __set_errno (EINVAL
);
242 *herrnop
= NETDB_INTERNAL
;
243 return NSS_STATUS_UNAVAIL
;
248 char buf
[strlen (name
) + 255];
251 /* Search at first in the alias list, and use the correct name
252 for the next search */
253 sprintf(buf
, "[name=%s],networks.org_dir", name
);
254 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
256 /* If we do not find it, try it as original name. But if the
257 database is correct, we should find it in the first case, too */
258 if ((result
->status
!= NIS_SUCCESS
&&
259 result
->status
!= NIS_S_SUCCESS
) ||
260 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
261 strcmp(result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
262 "networks_tbl") != 0 ||
263 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 3)
264 sprintf(buf
, "[cname=%s],networks.org_dir", name
);
266 sprintf(buf
, "[cname=%s],networks.org_dir", NISENTRYVAL(0, 0, result
));
268 nis_freeresult (result
);
269 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
271 retval
= niserr2nss (result
->status
);
272 if (retval
!= NSS_STATUS_SUCCESS
)
274 if (retval
== NSS_STATUS_TRYAGAIN
)
276 __set_errno (EAGAIN
);
277 *herrnop
= NETDB_INTERNAL
;
279 nis_freeresult (result
);
283 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
, buflen
);
285 nis_freeresult (result
);
288 return NSS_STATUS_SUCCESS
;
290 *herrnop
= NETDB_INTERNAL
;
291 if (!parse_res
&& errno
== ERANGE
)
292 return NSS_STATUS_TRYAGAIN
;
294 return NSS_STATUS_NOTFOUND
;
298 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
300 _nss_nisplus_getnetbyaddr_r (const unsigned long addr
, const int type
,
301 struct netent
*network
,
302 char *buffer
, size_t buflen
, int *herrnop
)
304 int parse_res
, retval
;
309 in
= inet_makeaddr (addr
, 0);
310 snprintf(buf
, sizeof (buf
) - 1, "[addr=%s],networks.org_dir",
313 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
315 retval
= niserr2nss (result
->status
);
316 if (retval
!= NSS_STATUS_SUCCESS
)
318 if (retval
== NSS_STATUS_TRYAGAIN
)
320 __set_errno (EAGAIN
);
321 *herrnop
= NETDB_INTERNAL
;
323 nis_freeresult (result
);
327 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
, buflen
);
329 nis_freeresult (result
);
332 return NSS_STATUS_SUCCESS
;
334 *herrnop
= NETDB_INTERNAL
;
335 if (!parse_res
&& errno
== ERANGE
)
336 return NSS_STATUS_TRYAGAIN
;
338 return NSS_STATUS_NOTFOUND
;