]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nisplus/nisplus-network.c
Update.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-network.c
CommitLineData
e61abf83
UD
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.
4
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.
9
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.
14
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. */
19
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>
29
30#include "nss-nisplus.h"
31
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"
38LINE_PARSER
39("#",
40 {
41 char *addr;
42
43 STRING_FIELD (result->n_name, isspace, 1);
44
45 STRING_FIELD (addr, isspace, 1);
46 result->n_net = inet_network (addr);
47
48 })
49
50__libc_lock_define_initialized (static, lock)
51
52static nis_result *result = NULL;
53static nis_name *names = NULL;
54
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)
57
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)
60
61
62static int
63_nss_nisplus_parse_netent (nis_result *result, struct netent *network,
64 char *buffer, size_t buflen)
65{
66 char *p = buffer;
67 size_t room_left = buflen;
7799b7b3 68 unsigned int i;
e61abf83
UD
69 struct parser_data *data = (void *) buffer;
70
71 if (result == NULL)
26dee9c4 72 return 0;
e61abf83
UD
73
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)
26dee9c4 79 return 0;
e61abf83
UD
80
81 /* Generate the network entry format and use the normal parser */
82 if (NISENTRYLEN (0, 0, result) +1 > room_left)
83 {
84 __set_errno (ERANGE);
26dee9c4 85 return 0;
e61abf83
UD
86 }
87
88 memset (p, '\0', room_left);
89
90 strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result));
91 room_left -= (NISENTRYLEN (0, 0, result) +1);
92
93 if (NISENTRYLEN (0, 2, result) +1 > room_left)
94 {
95 __set_errno (ERANGE);
26dee9c4 96 return 0;
e61abf83
UD
97 }
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 */
102
103 for (i = 1; i < result->objects.objects_len; i++)
e61abf83
UD
104 {
105 if (NISENTRYLEN (i, 1, result) +1 > room_left)
106 {
107 __set_errno (ERANGE);
26dee9c4 108 return 0;
e61abf83
UD
109 }
110 strcat (p, " ");
111 strncat (p, NISENTRYVAL (i, 1, result), NISENTRYLEN (i, 1, result));
112 room_left -= (NISENTRYLEN (i, 1, result) + 1);
113 }
114
115 return _nss_files_parse_netent (p, network, data, buflen);
116}
117
118enum nss_status
119_nss_nisplus_setnetent (void)
120{
121 __libc_lock_lock (lock);
122
123 if (result)
124 nis_freeresult (result);
125 result = NULL;
126 if (names)
127 {
128 nis_freenames (names);
129 names = NULL;
130 }
131
132 __libc_lock_unlock (lock);
133
134 return NSS_STATUS_SUCCESS;
135}
136
137enum nss_status
138_nss_nisplus_endnetent (void)
139{
140 __libc_lock_lock (lock);
141
142 if (result)
143 nis_freeresult (result);
144 result = NULL;
145 if (names)
146 {
147 nis_freenames (names);
148 names = NULL;
149 }
150
151 __libc_lock_unlock (lock);
152
153 return NSS_STATUS_SUCCESS;
154}
155
156static enum nss_status
157internal_nisplus_getnetent_r (struct netent *network, char *buffer,
158 size_t buflen, int *herrnop)
159{
160 int parse_res;
161
162 /* Get the next entry until we found a correct one. */
163 do
164 {
165 if (result == NULL)
166 {
167 names = nis_getnames("networks.org_dir");
168 if (names == NULL || names[0] == NULL)
169 return NSS_STATUS_UNAVAIL;
170
171 result = nis_first_entry(names[0]);
172 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
173 {
174 int retval;
175
176 retval = niserr2nss (result->status);
177 if (retval == NSS_STATUS_TRYAGAIN)
178 {
179 *herrnop = NETDB_INTERNAL;
180 __set_errno (EAGAIN);
181 }
182 return retval;
183 }
184 }
185 else
186 {
187 nis_result *res;
188
189 res = nis_next_entry(names[0], &result->cookie);
190 nis_freeresult (result);
191 result = res;
192 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
193 {
194 int retval;
195
196 retval = niserr2nss (result->status);
197 if (retval == NSS_STATUS_TRYAGAIN)
198 {
199 *herrnop = NETDB_INTERNAL;
200 __set_errno (EAGAIN);
201 }
202 return retval;
203 }
204 }
205
206 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen);
207 if (!parse_res && errno == ERANGE)
208 {
209 *herrnop = NETDB_INTERNAL;
210 return NSS_STATUS_TRYAGAIN;
211 }
212
213 } while (!parse_res);
214
215 return NSS_STATUS_SUCCESS;
216}
217
218enum nss_status
219_nss_nisplus_getnetent_r (struct netent *result, char *buffer,
220 size_t buflen, int *herrnop)
221{
222 int status;
223
224 __libc_lock_lock (lock);
225
226 status = internal_nisplus_getnetent_r (result, buffer, buflen, herrnop);
227
228 __libc_lock_unlock (lock);
229
230 return status;
231}
232
233enum nss_status
234_nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
235 char *buffer, size_t buflen, int *herrnop)
236{
237 int parse_res, retval;
238
239 if (name == NULL)
240 {
241 __set_errno (EINVAL);
242 *herrnop = NETDB_INTERNAL;
243 return NSS_STATUS_UNAVAIL;
244 }
245 else
246 {
247 nis_result *result;
248 char buf[strlen (name) + 255];
249
250
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);
255
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));
267
268 nis_freeresult (result);
269 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
270
271 retval = niserr2nss (result->status);
272 if (retval != NSS_STATUS_SUCCESS)
273 {
274 if (retval == NSS_STATUS_TRYAGAIN)
275 {
276 __set_errno (EAGAIN);
277 *herrnop = NETDB_INTERNAL;
278 }
279 nis_freeresult (result);
280 return retval;
281 }
282
283 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen);
284
285 nis_freeresult (result);
286
287 if (parse_res)
288 return NSS_STATUS_SUCCESS;
289
290 *herrnop = NETDB_INTERNAL;
291 if (!parse_res && errno == ERANGE)
292 return NSS_STATUS_TRYAGAIN;
293 else
294 return NSS_STATUS_NOTFOUND;
295 }
296}
297
298/* XXX type is ignored, SUN's NIS+ table doesn't support it */
299enum 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)
303{
304 int parse_res, retval;
305 nis_result *result;
306 char buf[1024];
307 struct in_addr in;
308
309 in = inet_makeaddr (addr, 0);
310 snprintf(buf, sizeof (buf) - 1, "[addr=%s],networks.org_dir",
311 inet_ntoa (in));
312
313 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
314
315 retval = niserr2nss (result->status);
316 if (retval != NSS_STATUS_SUCCESS)
317 {
318 if (retval == NSS_STATUS_TRYAGAIN)
319 {
320 __set_errno (EAGAIN);
321 *herrnop = NETDB_INTERNAL;
322 }
323 nis_freeresult (result);
324 return retval;
325 }
326
327 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen);
328
329 nis_freeresult (result);
330
331 if (parse_res)
332 return NSS_STATUS_SUCCESS;
333
334 *herrnop = NETDB_INTERNAL;
335 if (!parse_res && errno == ERANGE)
336 return NSS_STATUS_TRYAGAIN;
337 else
338 return NSS_STATUS_NOTFOUND;
339}