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.
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.
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.
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. */
25 #include <arpa/inet.h>
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock
)
33 static nis_result
*result
= NULL
;
34 static nis_name tablename_val
= NULL
;
35 static u_long tablename_len
= 0;
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)
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)
45 _nss_nisplus_parse_netent (nis_result
*result
, struct netent
*network
,
46 char *buffer
, size_t buflen
)
48 char *first_unused
= buffer
;
49 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
,
59 "networks_tbl") != 0 ||
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
));
82 for (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
!= ' ')
128 if (line
!= network
->n_aliases
[i
])
138 network
->n_aliases
[i
] = NULL
;
144 static enum nss_status
145 _nss_create_tablename (void)
147 if (tablename_val
== NULL
)
149 char buf
[40 + strlen (nis_local_directory ())];
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 return NSS_STATUS_TRYAGAIN
;
157 tablename_len
= strlen (tablename_val
);
159 return NSS_STATUS_SUCCESS
;
163 _nss_nisplus_setnetent (void)
165 enum nss_status status
= NSS_STATUS_SUCCESS
;
167 __libc_lock_lock (lock
);
170 nis_freeresult (result
);
173 if (tablename_val
== NULL
)
174 if (_nss_create_tablename () != NSS_STATUS_SUCCESS
)
175 status
= NSS_STATUS_UNAVAIL
;
177 __libc_lock_unlock (lock
);
183 _nss_nisplus_endnetent (void)
185 __libc_lock_lock (lock
);
188 nis_freeresult (result
);
191 __libc_lock_unlock (lock
);
193 return NSS_STATUS_SUCCESS
;
196 static enum nss_status
197 internal_nisplus_getnetent_r (struct netent
*network
, char *buffer
,
198 size_t buflen
, int *herrnop
)
202 /* Get the next entry until we found a correct one. */
205 nis_result
*saved_res
;
211 if (tablename_val
== NULL
)
212 if (_nss_create_tablename() != NSS_STATUS_SUCCESS
)
213 return NSS_STATUS_UNAVAIL
;
215 result
= nis_first_entry(tablename_val
);
216 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
220 retval
= niserr2nss (result
->status
);
221 nis_freeresult (result
);
223 if (retval
== NSS_STATUS_TRYAGAIN
)
225 *herrnop
= NETDB_INTERNAL
;
226 __set_errno (EAGAIN
);
237 res
= nis_next_entry(tablename_val
, &result
->cookie
);
240 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
244 retval
= niserr2nss (result
->status
);
245 nis_freeresult (result
);
247 if (retval
== NSS_STATUS_TRYAGAIN
)
249 *herrnop
= NETDB_INTERNAL
;
250 __set_errno (EAGAIN
);
256 if ((parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
,
259 *herrnop
= NETDB_INTERNAL
;
260 return NSS_STATUS_TRYAGAIN
;
263 } while (!parse_res
);
265 return NSS_STATUS_SUCCESS
;
269 _nss_nisplus_getnetent_r (struct netent
*result
, char *buffer
,
270 size_t buflen
, int *herrnop
)
274 __libc_lock_lock (lock
);
276 status
= internal_nisplus_getnetent_r (result
, buffer
, buflen
, herrnop
);
278 __libc_lock_unlock (lock
);
284 _nss_nisplus_getnetbyname_r (const char *name
, struct netent
*network
,
285 char *buffer
, size_t buflen
, int *herrnop
)
287 int parse_res
, retval
;
289 if (tablename_val
== NULL
)
290 if (_nss_create_tablename() != NSS_STATUS_SUCCESS
)
291 return NSS_STATUS_UNAVAIL
;
295 __set_errno (EINVAL
);
296 *herrnop
= NETDB_INTERNAL
;
297 return NSS_STATUS_UNAVAIL
;
302 char buf
[strlen (name
) + 255 + tablename_len
];
304 /* Search at first in the alias list, and use the correct name
305 for the next search */
306 sprintf(buf
, "[name=%s],%s", name
, tablename_val
);
307 result
= nis_list(buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
309 /* If we do not find it, try it as original name. But if the
310 database is correct, we should find it in the first case, too */
311 if ((result
->status
!= NIS_SUCCESS
&&
312 result
->status
!= NIS_S_SUCCESS
) ||
313 __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
||
314 strcmp(result
->objects
.objects_val
[0].EN_data
.en_type
,
315 "networks_tbl") != 0 ||
316 result
->objects
.objects_val
[0].EN_data
.en_cols
.en_cols_len
< 3)
317 sprintf(buf
, "[cname=%s],%s", name
, tablename_val
);
319 sprintf(buf
, "[cname=%s],%s", NISENTRYVAL(0, 0, result
),
322 nis_freeresult (result
);
323 result
= nis_list(buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
325 retval
= niserr2nss (result
->status
);
326 if (retval
!= NSS_STATUS_SUCCESS
)
328 if (retval
== NSS_STATUS_TRYAGAIN
)
330 __set_errno (EAGAIN
);
331 *herrnop
= NETDB_INTERNAL
;
333 nis_freeresult (result
);
337 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
, buflen
);
339 nis_freeresult (result
);
342 return NSS_STATUS_SUCCESS
;
344 *herrnop
= NETDB_INTERNAL
;
346 return NSS_STATUS_TRYAGAIN
;
348 return NSS_STATUS_NOTFOUND
;
352 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
354 _nss_nisplus_getnetbyaddr_r (const unsigned long addr
, const int type
,
355 struct netent
*network
,
356 char *buffer
, size_t buflen
, int *herrnop
)
358 if (tablename_val
== NULL
)
359 if (_nss_create_tablename() != NSS_STATUS_SUCCESS
)
360 return NSS_STATUS_UNAVAIL
;
363 int parse_res
, retval
;
365 char buf
[1024 + tablename_len
];
368 in
= inet_makeaddr (addr
, 0);
369 sprintf (buf
, "[addr=%s],%s", inet_ntoa (in
), tablename_val
);
371 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
373 retval
= niserr2nss (result
->status
);
374 if (retval
!= NSS_STATUS_SUCCESS
)
376 if (retval
== NSS_STATUS_TRYAGAIN
)
378 __set_errno (EAGAIN
);
379 *herrnop
= NETDB_INTERNAL
;
381 nis_freeresult (result
);
385 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
, buflen
);
387 nis_freeresult (result
);
390 return NSS_STATUS_SUCCESS
;
392 *herrnop
= NETDB_INTERNAL
;
394 return NSS_STATUS_TRYAGAIN
;
396 return NSS_STATUS_NOTFOUND
;