]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-network.c
Update.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-network.c
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 <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
28
29 #include "nss-nisplus.h"
30
31 __libc_lock_define_initialized (static, lock)
32
33 static nis_result *result = NULL;
34 static nis_name tablename_val = NULL;
35 static u_long tablename_len = 0;
36
37 #define NISENTRYVAL(idx,col,res) \
38 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
39
40 #define NISENTRYLEN(idx,col,res) \
41 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
42
43
44 static int
45 _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
46 char *buffer, size_t buflen, int *errnop)
47 {
48 char *first_unused = buffer;
49 size_t room_left = buflen;
50 unsigned int i;
51 char *p, *line;
52
53 if (result == NULL)
54 return 0;
55
56 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
57 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
58 || strcmp (result->objects.objects_val[0].EN_data.en_type,
59 "networks_tbl") != 0
60 || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
61 return 0;
62
63 if (NISENTRYLEN(0, 0, result) >= room_left)
64 {
65 /* The line is too long for our buffer. */
66 no_more_room:
67 *errnop = ERANGE;
68 return -1;
69 }
70
71 strncpy (first_unused, NISENTRYVAL(0, 0, result),
72 NISENTRYLEN (0, 0, result));
73 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
74 network->n_name = first_unused;
75 room_left -= (strlen (first_unused) +1);
76 first_unused += strlen (first_unused) +1;
77 network->n_addrtype = 0;
78 network->n_net = inet_network (NISENTRYVAL (0, 2, result));
79 p = first_unused;
80
81 line = p;
82 for (i = 0; i < result->objects.objects_len; i++)
83 {
84 if (strcmp (NISENTRYVAL (i, 1, result), network->n_name) != 0)
85 {
86 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
87 goto no_more_room;
88
89 *p++ = ' ';
90 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
91 NISENTRYLEN (i, 1, result));
92 *p = '\0';
93 room_left -= (NISENTRYLEN (i, 1, result) + 1);
94 }
95 }
96 ++p;
97 first_unused = p;
98
99 /* Adjust the pointer so it is aligned for
100 storing pointers. */
101 first_unused += __alignof__ (char *) - 1;
102 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
103 network->n_aliases = (char **) first_unused;
104 if (room_left < 2 * sizeof (char *))
105 goto no_more_room;
106 room_left -= (2 * sizeof (char *));
107 network->n_aliases[0] = NULL;
108
109 i = 0;
110 while (*line != '\0')
111 {
112 /* Skip leading blanks. */
113 while (isspace (*line))
114 line++;
115
116 if (*line == '\0')
117 break;
118
119 if (room_left < sizeof (char *))
120 goto no_more_room;
121
122 room_left -= sizeof (char *);
123 network->n_aliases[i] = line;
124
125 while (*line != '\0' && *line != ' ')
126 ++line;
127
128 if (line != network->n_aliases[i])
129 {
130 if (*line != '\0')
131 {
132 *line = '\0';
133 ++line;
134 }
135 ++i;
136 }
137 else
138 network->n_aliases[i] = NULL;
139 }
140
141 return 1;
142 }
143
144 static enum nss_status
145 _nss_create_tablename (int *errnop)
146 {
147 if (tablename_val == NULL)
148 {
149 char buf [40 + strlen (nis_local_directory ())];
150 char *p;
151
152 p = __stpcpy (buf, "networks.org_dir.");
153 p = __stpcpy (p, nis_local_directory ());
154 tablename_val = __strdup (buf);
155 if (tablename_val == NULL)
156 {
157 *errnop = errno;
158 return NSS_STATUS_TRYAGAIN;
159 }
160 tablename_len = strlen (tablename_val);
161 }
162 return NSS_STATUS_SUCCESS;
163 }
164
165 enum nss_status
166 _nss_nisplus_setnetent (void)
167 {
168 enum nss_status status = NSS_STATUS_SUCCESS;
169 int err;
170
171 __libc_lock_lock (lock);
172
173 if (result)
174 nis_freeresult (result);
175 result = NULL;
176
177 if (tablename_val == NULL)
178 status = _nss_create_tablename (&err);
179
180 __libc_lock_unlock (lock);
181
182 return status;
183 }
184
185 enum nss_status
186 _nss_nisplus_endnetent (void)
187 {
188 __libc_lock_lock (lock);
189
190 if (result)
191 nis_freeresult (result);
192 result = NULL;
193
194 __libc_lock_unlock (lock);
195
196 return NSS_STATUS_SUCCESS;
197 }
198
199 static enum nss_status
200 internal_nisplus_getnetent_r (struct netent *network, char *buffer,
201 size_t buflen, int *errnop, int *herrnop)
202 {
203 int parse_res;
204
205 /* Get the next entry until we found a correct one. */
206 do
207 {
208 nis_result *saved_res;
209
210 if (result == NULL)
211 {
212 saved_res = NULL;
213
214 if (tablename_val == NULL)
215 {
216 enum nss_status status = _nss_create_tablename (errnop);
217
218 if (status != NSS_STATUS_SUCCESS)
219 return status;
220 }
221
222 result = nis_first_entry (tablename_val);
223 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
224 {
225 int retval;
226
227 retval = niserr2nss (result->status);
228 nis_freeresult (result);
229 result = NULL;
230 if (retval == NSS_STATUS_TRYAGAIN)
231 {
232 *herrnop = NETDB_INTERNAL;
233 *errnop = errno;
234 return retval;
235 }
236 else
237 return retval;
238 }
239 }
240 else
241 {
242 nis_result *res;
243
244 res = nis_next_entry (tablename_val, &result->cookie);
245 saved_res = result;
246 result = res;
247 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
248 {
249 int retval;
250
251 retval = niserr2nss (result->status);
252 nis_freeresult (result);
253 result = saved_res;
254 if (retval == NSS_STATUS_TRYAGAIN)
255 {
256 *herrnop = NETDB_INTERNAL;
257 *errnop = errno;
258 }
259 return retval;
260 }
261 }
262
263 parse_res = _nss_nisplus_parse_netent (result, network, buffer,
264 buflen, errnop);
265 if (parse_res == -1)
266 {
267 *herrnop = NETDB_INTERNAL;
268 return NSS_STATUS_TRYAGAIN;
269 }
270
271 } while (!parse_res);
272
273 return NSS_STATUS_SUCCESS;
274 }
275
276 enum nss_status
277 _nss_nisplus_getnetent_r (struct netent *result, char *buffer,
278 size_t buflen, int *errnop, int *herrnop)
279 {
280 int status;
281
282 __libc_lock_lock (lock);
283
284 status = internal_nisplus_getnetent_r (result, buffer, buflen, errnop,
285 herrnop);
286
287 __libc_lock_unlock (lock);
288
289 return status;
290 }
291
292 enum nss_status
293 _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
294 char *buffer, size_t buflen, int *errnop,
295 int *herrnop)
296 {
297 int parse_res, retval;
298
299 if (tablename_val == NULL)
300 {
301 enum nss_status status = _nss_create_tablename (errnop);
302
303 if (status != NSS_STATUS_SUCCESS)
304 return status;
305 }
306
307 if (name == NULL)
308 {
309 *errnop = EINVAL;
310 *herrnop = NETDB_INTERNAL;
311 return NSS_STATUS_UNAVAIL;
312 }
313 else
314 {
315 nis_result *result;
316 char buf[strlen (name) + 255 + tablename_len];
317
318 /* Search at first in the alias list, and use the correct name
319 for the next search */
320 sprintf (buf, "[name=%s],%s", name, tablename_val);
321 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
322
323 /* If we do not find it, try it as original name. But if the
324 database is correct, we should find it in the first case, too */
325 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
326 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
327 || strcmp (result->objects.objects_val[0].EN_data.en_type,
328 "networks_tbl") != 0
329 || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
330 sprintf (buf, "[cname=%s],%s", name, tablename_val);
331 else
332 sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
333 tablename_val);
334
335 nis_freeresult (result);
336 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
337
338 retval = niserr2nss (result->status);
339 if (retval != NSS_STATUS_SUCCESS)
340 {
341 if (retval == NSS_STATUS_TRYAGAIN)
342 {
343 *errnop = errno;
344 *herrnop = NETDB_INTERNAL;
345 }
346 nis_freeresult (result);
347 return retval;
348 }
349
350 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen,
351 errnop);
352
353 nis_freeresult (result);
354
355 if (parse_res > 0)
356 return NSS_STATUS_SUCCESS;
357
358 *herrnop = NETDB_INTERNAL;
359 if (parse_res == -1)
360 {
361 *errnop = ERANGE;
362 return NSS_STATUS_TRYAGAIN;
363 }
364 else
365 return NSS_STATUS_NOTFOUND;
366 }
367 }
368
369 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
370 enum nss_status
371 _nss_nisplus_getnetbyaddr_r (const unsigned long addr, const int type,
372 struct netent *network, char *buffer,
373 size_t buflen, int *errnop, int *herrnop)
374 {
375 if (tablename_val == NULL)
376 {
377 enum nss_status status = _nss_create_tablename (errnop);
378
379 if (status != NSS_STATUS_SUCCESS)
380 return status;
381 }
382
383 {
384 int parse_res, retval;
385 nis_result *result;
386 char buf[1024 + tablename_len];
387 struct in_addr in;
388
389 in = inet_makeaddr (addr, 0);
390 sprintf (buf, "[addr=%s],%s", inet_ntoa (in), tablename_val);
391
392 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
393
394 retval = niserr2nss (result->status);
395 if (retval != NSS_STATUS_SUCCESS)
396 {
397 if (retval == NSS_STATUS_TRYAGAIN)
398 {
399 *errnop = errno;
400 *herrnop = NETDB_INTERNAL;
401 }
402 nis_freeresult (result);
403 return retval;
404 }
405
406 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen,
407 errnop);
408
409 nis_freeresult (result);
410
411 if (parse_res > 0)
412 return NSS_STATUS_SUCCESS;
413
414 *herrnop = NETDB_INTERNAL;
415 if (parse_res == -1)
416 {
417 *errnop = ERANGE;
418 return NSS_STATUS_TRYAGAIN;
419 }
420 else
421 return NSS_STATUS_NOTFOUND;
422 }
423 }