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