]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-network.c
1 /* Copyright (C) 1997,1998,2000-2003,2005,2006 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 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
42 #define NISENTRYLEN(idx, col, res) \
43 (NIS_RES_OBJECT (res)[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 (NIS_RES_OBJECT (result
)) != NIS_ENTRY_OBJ
58 || strcmp (NIS_RES_OBJECT (result
)[0].EN_data
.en_type
,
60 || NIS_RES_OBJECT (result
)[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 size_t len
= strlen (first_unused
) + 1;
79 network
->n_addrtype
= 0;
80 network
->n_net
= inet_network (NISENTRYVAL (0, 2, result
));
82 /* XXX Rewrite at some point to allocate the array first and then
83 copy the strings. It wasteful to first concatenate the strings
84 to just split them again later. */
85 char *line
= first_unused
;
86 for (unsigned int i
= 0; i
< NIS_RES_NUMOBJ (result
); ++i
)
88 if (strcmp (NISENTRYVAL (i
, 1, result
), network
->n_name
) != 0)
90 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
93 *first_unused
++ = ' ';
94 first_unused
= __stpncpy (first_unused
, NISENTRYVAL (i
, 1, result
),
95 NISENTRYLEN (i
, 1, result
));
96 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
99 *first_unused
++ = '\0';
101 /* Adjust the pointer so it is aligned for
103 size_t adjust
= ((__alignof__ (char *)
104 - (first_unused
- (char *) 0) % __alignof__ (char *))
105 % __alignof__ (char *));
106 if (room_left
< adjust
+ sizeof (char *))
108 first_unused
+= adjust
;
110 network
->n_aliases
= (char **) first_unused
;
112 /* For the terminating NULL pointer. */
113 room_left
-= sizeof (char *);
116 while (*line
!= '\0')
118 /* Skip leading blanks. */
119 while (isspace (*line
))
125 if (room_left
< sizeof (char *))
128 room_left
-= sizeof (char *);
129 network
->n_aliases
[i
++] = line
;
131 while (*line
!= '\0' && *line
!= ' ')
137 network
->n_aliases
[i
] = NULL
;
143 static enum nss_status
144 _nss_create_tablename (int *errnop
)
146 if (tablename_val
== NULL
)
148 const char *local_dir
= nis_local_directory ();
149 size_t local_dir_len
= strlen (local_dir
);
150 static const char prefix
[] = "networks.org_dir.";
152 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
156 return NSS_STATUS_TRYAGAIN
;
159 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
161 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
163 atomic_write_barrier ();
168 return NSS_STATUS_SUCCESS
;
172 _nss_nisplus_setnetent (int stayopen
)
174 enum nss_status status
= NSS_STATUS_SUCCESS
;
176 __libc_lock_lock (lock
);
180 nis_freeresult (result
);
184 if (tablename_val
== NULL
)
187 status
= _nss_create_tablename (&err
);
190 __libc_lock_unlock (lock
);
196 _nss_nisplus_endnetent (void)
198 __libc_lock_lock (lock
);
202 nis_freeresult (result
);
206 __libc_lock_unlock (lock
);
208 return NSS_STATUS_SUCCESS
;
211 static enum nss_status
212 internal_nisplus_getnetent_r (struct netent
*network
, char *buffer
,
213 size_t buflen
, int *errnop
, int *herrnop
)
217 /* Get the next entry until we found a correct one. */
220 nis_result
*saved_res
;
226 if (tablename_val
== NULL
)
228 enum nss_status status
= _nss_create_tablename (errnop
);
230 if (status
!= NSS_STATUS_SUCCESS
)
234 result
= nis_first_entry (tablename_val
);
238 return NSS_STATUS_TRYAGAIN
;
240 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
242 int retval
= niserr2nss (result
->status
);
243 nis_freeresult (result
);
245 if (retval
== NSS_STATUS_TRYAGAIN
)
247 *herrnop
= NETDB_INTERNAL
;
258 result
= nis_next_entry (tablename_val
, &result
->cookie
);
262 return NSS_STATUS_TRYAGAIN
;
264 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
266 int retval
= niserr2nss (result
->status
);
267 nis_freeresult (result
);
269 if (retval
== NSS_STATUS_TRYAGAIN
)
271 *herrnop
= NETDB_INTERNAL
;
278 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
,
282 *herrnop
= NETDB_INTERNAL
;
283 return NSS_STATUS_TRYAGAIN
;
289 return NSS_STATUS_SUCCESS
;
293 _nss_nisplus_getnetent_r (struct netent
*result
, char *buffer
,
294 size_t buflen
, int *errnop
, int *herrnop
)
298 __libc_lock_lock (lock
);
300 status
= internal_nisplus_getnetent_r (result
, buffer
, buflen
, errnop
,
303 __libc_lock_unlock (lock
);
309 _nss_nisplus_getnetbyname_r (const char *name
, struct netent
*network
,
310 char *buffer
, size_t buflen
, int *errnop
,
313 int parse_res
, retval
;
315 if (tablename_val
== NULL
)
317 __libc_lock_lock (lock
);
319 enum nss_status status
= _nss_create_tablename (errnop
);
321 __libc_lock_unlock (lock
);
323 if (status
!= NSS_STATUS_SUCCESS
)
330 *herrnop
= NETDB_INTERNAL
;
331 return NSS_STATUS_UNAVAIL
;
335 char buf
[strlen (name
) + 10 + tablename_len
];
338 /* Search at first in the alias list, and use the correct name
339 for the next search */
340 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
341 result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
347 /* If we do not find it, try it as original name. But if the
348 database is correct, we should find it in the first case, too */
349 if ((result
->status
!= NIS_SUCCESS
350 && result
->status
!= NIS_S_SUCCESS
)
351 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
352 || strcmp (result
->objects
.objects_val
[0].EN_data
.en_type
,
354 || (result
->objects
.objects_val
[0].EN_data
.en_cols
.en_cols_len
356 snprintf (buf
, sizeof (buf
), "[cname=%s],%s", name
, tablename_val
);
359 /* We need to allocate a new buffer since there is no
360 guarantee the returned name has a length limit. */
361 const char *entryval
= NISENTRYVAL (0, 0, result
);
362 size_t buflen
= strlen (entryval
) + 10 + tablename_len
;
363 bufptr
= alloca (buflen
);
364 snprintf (bufptr
, buflen
, "[cname=%s],%s",
365 entryval
, tablename_val
);
368 nis_freeresult (result
);
369 result
= nis_list (bufptr
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
374 __set_errno (ENOMEM
);
375 return NSS_STATUS_TRYAGAIN
;
378 retval
= niserr2nss (result
->status
);
379 if (__builtin_expect (retval
!= NSS_STATUS_SUCCESS
, 0))
381 if (retval
== NSS_STATUS_TRYAGAIN
)
384 *herrnop
= NETDB_INTERNAL
;
387 __set_errno (olderr
);
388 nis_freeresult (result
);
392 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
, buflen
,
395 nis_freeresult (result
);
398 return NSS_STATUS_SUCCESS
;
400 *herrnop
= NETDB_INTERNAL
;
404 return NSS_STATUS_TRYAGAIN
;
407 __set_errno (olderr
);
408 return NSS_STATUS_NOTFOUND
;
411 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
413 _nss_nisplus_getnetbyaddr_r (uint32_t addr
, const int type
,
414 struct netent
*network
, char *buffer
,
415 size_t buflen
, int *errnop
, int *herrnop
)
417 if (tablename_val
== NULL
)
419 __libc_lock_lock (lock
);
421 enum nss_status status
= _nss_create_tablename (errnop
);
423 __libc_lock_unlock (lock
);
425 if (status
!= NSS_STATUS_SUCCESS
)
430 char buf
[27 + tablename_len
];
434 struct in_addr in
= inet_makeaddr (addr
, 0);
435 strcpy (buf2
, inet_ntoa (in
));
436 size_t b2len
= strlen (buf2
);
440 snprintf (buf
, sizeof (buf
), "[addr=%s],%s", buf2
, tablename_val
);
441 nis_result
*result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
445 __set_errno (ENOMEM
);
446 return NSS_STATUS_TRYAGAIN
;
448 enum nss_status retval
= niserr2nss (result
->status
);
449 if (__builtin_expect (retval
!= NSS_STATUS_SUCCESS
, 0))
451 if (b2len
> 2 && buf2
[b2len
- 2] == '.' && buf2
[b2len
- 1] == '0')
453 /* Try again, but with trailing dot(s)
454 removed (one by one) */
455 buf2
[b2len
- 2] = '\0';
457 nis_freeresult (result
);
461 if (retval
== NSS_STATUS_TRYAGAIN
)
464 *herrnop
= NETDB_INTERNAL
;
467 __set_errno (olderr
);
468 nis_freeresult (result
);
472 int parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
,
475 nis_freeresult (result
);
478 return NSS_STATUS_SUCCESS
;
480 *herrnop
= NETDB_INTERNAL
;
484 return NSS_STATUS_TRYAGAIN
;
488 __set_errno (olderr
);
489 return NSS_STATUS_NOTFOUND
;