]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-network.c
1 /* Copyright (C) 1997,1998,2000-2003,2005 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 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.
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.
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
27 #include <arpa/inet.h>
28 #include <rpcsvc/nis.h>
29 #include <bits/libc-lock.h>
31 #include "nss-nisplus.h"
33 __libc_lock_define_initialized (static, lock
)
35 static nis_result
*result
;
36 static nis_name tablename_val
;
37 static u_long tablename_len
;
39 #define NISENTRYVAL(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
42 #define NISENTRYLEN(idx,col,res) \
43 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
47 _nss_nisplus_parse_netent (nis_result
*result
, struct netent
*network
,
48 char *buffer
, size_t buflen
, int *errnop
)
50 char *first_unused
= buffer
;
51 size_t room_left
= buflen
;
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
,
60 || result
->objects
.objects_val
[0].EN_data
.en_cols
.en_cols_len
< 3)
63 if (NISENTRYLEN (0, 0, result
) >= room_left
)
65 /* The line is too long for our buffer. */
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 char *p
= first_unused
;
82 for (unsigned int i
= 0; i
< result
->objects
.objects_len
; ++i
)
84 if (strcmp (NISENTRYVAL (i
, 1, result
), network
->n_name
) != 0)
86 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
90 p
= __stpncpy (p
, NISENTRYVAL (i
, 1, result
),
91 NISENTRYLEN (i
, 1, result
));
93 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
99 /* Adjust the pointer so it is aligned for
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 *))
106 room_left
-= (2 * sizeof (char *));
107 network
->n_aliases
[0] = NULL
;
110 while (*line
!= '\0')
112 /* Skip leading blanks. */
113 while (isspace (*line
))
119 if (room_left
< sizeof (char *))
122 room_left
-= sizeof (char *);
123 network
->n_aliases
[i
] = line
;
125 while (*line
!= '\0' && *line
!= ' ')
135 network
->n_aliases
[i
+ 1] = NULL
;
141 static enum nss_status
142 _nss_create_tablename (int *errnop
)
144 if (tablename_val
== NULL
)
146 const char *local_dir
= nis_local_directory ();
147 size_t local_dir_len
= strlen (local_dir
);
148 static const char prefix
[] = "networks.org_dir.";
150 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
151 if (tablename_val
== NULL
)
154 return NSS_STATUS_TRYAGAIN
;
157 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
159 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
161 atomic_write_barrier ();
166 return NSS_STATUS_SUCCESS
;
170 _nss_nisplus_setnetent (int stayopen
)
172 enum nss_status status
= NSS_STATUS_SUCCESS
;
174 __libc_lock_lock (lock
);
178 nis_freeresult (result
);
182 if (tablename_val
== NULL
)
185 status
= _nss_create_tablename (&err
);
188 __libc_lock_unlock (lock
);
194 _nss_nisplus_endnetent (void)
196 __libc_lock_lock (lock
);
200 nis_freeresult (result
);
204 __libc_lock_unlock (lock
);
206 return NSS_STATUS_SUCCESS
;
209 static enum nss_status
210 internal_nisplus_getnetent_r (struct netent
*network
, char *buffer
,
211 size_t buflen
, int *errnop
, int *herrnop
)
215 /* Get the next entry until we found a correct one. */
218 nis_result
*saved_res
;
224 if (tablename_val
== NULL
)
226 enum nss_status status
= _nss_create_tablename (errnop
);
228 if (status
!= NSS_STATUS_SUCCESS
)
232 result
= nis_first_entry (tablename_val
);
233 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
235 int retval
= niserr2nss (result
->status
);
236 nis_freeresult (result
);
238 if (retval
== NSS_STATUS_TRYAGAIN
)
240 *herrnop
= NETDB_INTERNAL
;
250 nis_result
*res
= nis_next_entry (tablename_val
, &result
->cookie
);
253 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
255 int retval
= niserr2nss (result
->status
);
256 nis_freeresult (result
);
258 if (retval
== NSS_STATUS_TRYAGAIN
)
260 *herrnop
= NETDB_INTERNAL
;
267 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
,
271 *herrnop
= NETDB_INTERNAL
;
272 return NSS_STATUS_TRYAGAIN
;
278 return NSS_STATUS_SUCCESS
;
282 _nss_nisplus_getnetent_r (struct netent
*result
, char *buffer
,
283 size_t buflen
, int *errnop
, int *herrnop
)
287 __libc_lock_lock (lock
);
289 status
= internal_nisplus_getnetent_r (result
, buffer
, buflen
, errnop
,
292 __libc_lock_unlock (lock
);
298 _nss_nisplus_getnetbyname_r (const char *name
, struct netent
*network
,
299 char *buffer
, size_t buflen
, int *errnop
,
302 int parse_res
, retval
;
304 if (tablename_val
== NULL
)
306 __libc_lock_lock (lock
);
308 enum nss_status status
= _nss_create_tablename (errnop
);
310 __libc_lock_unlock (lock
);
312 if (status
!= NSS_STATUS_SUCCESS
)
319 *herrnop
= NETDB_INTERNAL
;
320 return NSS_STATUS_UNAVAIL
;
324 char buf
[strlen (name
) + 10 + tablename_len
];
327 /* Search at first in the alias list, and use the correct name
328 for the next search */
329 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
330 result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
336 /* If we do not find it, try it as original name. But if the
337 database is correct, we should find it in the first case, too */
338 if ((result
->status
!= NIS_SUCCESS
339 && result
->status
!= NIS_S_SUCCESS
)
340 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
341 || strcmp (result
->objects
.objects_val
[0].EN_data
.en_type
,
343 || (result
->objects
.objects_val
[0].EN_data
.en_cols
.en_cols_len
345 snprintf (buf
, sizeof (buf
), "[cname=%s],%s", name
, tablename_val
);
348 /* We need to allocate a new buffer since there is no
349 guarantee the returned name has a length limit. */
350 const char *entryval
= NISENTRYVAL (0, 0, result
);
351 size_t buflen
= strlen (entryval
) + 10 + tablename_len
;
352 bufptr
= alloca (buflen
);
353 snprintf (bufptr
, buflen
, "[cname=%s],%s",
354 entryval
, tablename_val
);
357 nis_freeresult (result
);
358 result
= nis_list (bufptr
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
363 __set_errno (ENOMEM
);
364 return NSS_STATUS_TRYAGAIN
;
367 retval
= niserr2nss (result
->status
);
368 if (__builtin_expect (retval
!= NSS_STATUS_SUCCESS
, 0))
370 if (retval
== NSS_STATUS_TRYAGAIN
)
373 *herrnop
= NETDB_INTERNAL
;
376 __set_errno (olderr
);
377 nis_freeresult (result
);
381 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
, buflen
,
384 nis_freeresult (result
);
387 return NSS_STATUS_SUCCESS
;
389 *herrnop
= NETDB_INTERNAL
;
393 return NSS_STATUS_TRYAGAIN
;
396 __set_errno (olderr
);
397 return NSS_STATUS_NOTFOUND
;
400 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
402 _nss_nisplus_getnetbyaddr_r (uint32_t addr
, const int type
,
403 struct netent
*network
, char *buffer
,
404 size_t buflen
, int *errnop
, int *herrnop
)
406 if (tablename_val
== NULL
)
408 __libc_lock_lock (lock
);
410 enum nss_status status
= _nss_create_tablename (errnop
);
412 __libc_lock_unlock (lock
);
414 if (status
!= NSS_STATUS_SUCCESS
)
419 int parse_res
, retval
;
421 char buf
[1024 + tablename_len
];
427 in
= inet_makeaddr (addr
, 0);
428 strcpy (buf2
, inet_ntoa (in
));
429 b2len
= strlen (buf2
);
433 sprintf (buf
, "[addr=%s],%s", buf2
, tablename_val
);
434 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
438 __set_errno (ENOMEM
);
439 return NSS_STATUS_TRYAGAIN
;
441 retval
= niserr2nss (result
->status
);
442 if (retval
!= NSS_STATUS_SUCCESS
)
444 if (buf2
[b2len
-2] == '.' && buf2
[b2len
-1] == '0')
446 /* Try again, but with trailing dot(s)
447 removed (one by one) */
448 buf2
[b2len
- 2] = '\0';
453 return NSS_STATUS_NOTFOUND
;
455 if (retval
== NSS_STATUS_TRYAGAIN
)
458 *herrnop
= NETDB_INTERNAL
;
461 __set_errno (olderr
);
462 nis_freeresult (result
);
466 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
,
469 nis_freeresult (result
);
472 return NSS_STATUS_SUCCESS
;
474 *herrnop
= NETDB_INTERNAL
;
478 return NSS_STATUS_TRYAGAIN
;
482 __set_errno (olderr
);
483 return NSS_STATUS_NOTFOUND
;