Update.
[glibc.git] / nis / nss_nisplus / nisplus-network.c
blob7f849e21e909b9cc052cb04c5d12cc2122610252
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. */
20 #include <nss.h>
21 #include <netdb.h>
22 #include <errno.h>
23 #include <ctype.h>
24 #include <string.h>
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"
38 LINE_PARSER
39 ("#",
41 char *addr;
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)
62 static int
63 _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
64 char *buffer, size_t buflen)
66 char *p = buffer;
67 size_t room_left = buflen;
68 unsigned int i;
69 struct parser_data *data = (void *) buffer;
71 if (result == NULL)
72 return 0;
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)
79 return 0;
81 /* Generate the network entry format and use the normal parser */
82 if (NISENTRYLEN (0, 0, result) +1 > room_left)
84 __set_errno (ERANGE);
85 return 0;
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)
95 __set_errno (ERANGE);
96 return 0;
98 strcat (p, "\t");
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);
108 return 0;
110 strcat (p, " ");
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);
118 enum nss_status
119 _nss_nisplus_setnetent (void)
121 __libc_lock_lock (lock);
123 if (result)
124 nis_freeresult (result);
125 result = NULL;
126 if (names)
128 nis_freenames (names);
129 names = NULL;
132 __libc_lock_unlock (lock);
134 return NSS_STATUS_SUCCESS;
137 enum nss_status
138 _nss_nisplus_endnetent (void)
140 __libc_lock_lock (lock);
142 if (result)
143 nis_freeresult (result);
144 result = NULL;
145 if (names)
147 nis_freenames (names);
148 names = NULL;
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)
160 int parse_res;
162 /* Get the next entry until we found a correct one. */
165 if (result == NULL)
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)
174 int retval;
176 retval = niserr2nss (result->status);
177 if (retval == NSS_STATUS_TRYAGAIN)
179 *herrnop = NETDB_INTERNAL;
180 __set_errno (EAGAIN);
182 return retval;
185 else
187 nis_result *res;
189 res = nis_next_entry(names[0], &result->cookie);
190 nis_freeresult (result);
191 result = res;
192 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
194 int retval;
196 retval = niserr2nss (result->status);
197 if (retval == NSS_STATUS_TRYAGAIN)
199 *herrnop = NETDB_INTERNAL;
200 __set_errno (EAGAIN);
202 return retval;
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;
218 enum nss_status
219 _nss_nisplus_getnetent_r (struct netent *result, char *buffer,
220 size_t buflen, int *herrnop)
222 int status;
224 __libc_lock_lock (lock);
226 status = internal_nisplus_getnetent_r (result, buffer, buflen, herrnop);
228 __libc_lock_unlock (lock);
230 return status;
233 enum nss_status
234 _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
235 char *buffer, size_t buflen, int *herrnop)
237 int parse_res, retval;
239 if (name == NULL)
241 __set_errno (EINVAL);
242 *herrnop = NETDB_INTERNAL;
243 return NSS_STATUS_UNAVAIL;
245 else
247 nis_result *result;
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);
265 else
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);
280 return retval;
283 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen);
285 nis_freeresult (result);
287 if (parse_res)
288 return NSS_STATUS_SUCCESS;
290 *herrnop = NETDB_INTERNAL;
291 if (!parse_res && errno == ERANGE)
292 return NSS_STATUS_TRYAGAIN;
293 else
294 return NSS_STATUS_NOTFOUND;
298 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
299 enum nss_status
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;
305 nis_result *result;
306 char buf[1024];
307 struct in_addr in;
309 in = inet_makeaddr (addr, 0);
310 snprintf(buf, sizeof (buf) - 1, "[addr=%s],networks.org_dir",
311 inet_ntoa (in));
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);
324 return retval;
327 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen);
329 nis_freeresult (result);
331 if (parse_res)
332 return NSS_STATUS_SUCCESS;
334 *herrnop = NETDB_INTERNAL;
335 if (!parse_res && errno == ERANGE)
336 return NSS_STATUS_TRYAGAIN;
337 else
338 return NSS_STATUS_NOTFOUND;