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.
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
26 #include <arpa/inet.h>
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/nis.h>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock
)
34 static nis_result
*result
;
35 static nis_name tablename_val
;
36 static u_long tablename_len
;
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)
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)
46 _nss_nisplus_parse_netent (nis_result
*result
, struct netent
*network
,
47 char *buffer
, size_t buflen
, int *errnop
)
49 char *first_unused
= buffer
;
50 size_t room_left
= buflen
;
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
,
61 || result
->objects
.objects_val
[0].EN_data
.en_cols
.en_cols_len
< 3)
64 if (NISENTRYLEN(0, 0, result
) >= room_left
)
66 /* The line is too long for our buffer. */
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
));
83 for (i
= 0; i
< result
->objects
.objects_len
; ++i
)
85 if (strcmp (NISENTRYVAL (i
, 1, result
), network
->n_name
) != 0)
87 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
91 p
= __stpncpy (p
, NISENTRYVAL (i
, 1, result
),
92 NISENTRYLEN (i
, 1, result
));
94 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
100 /* Adjust the pointer so it is aligned for
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 *))
107 room_left
-= (2 * sizeof (char *));
108 network
->n_aliases
[0] = NULL
;
111 while (*line
!= '\0')
113 /* Skip leading blanks. */
114 while (isspace (*line
))
120 if (room_left
< sizeof (char *))
123 room_left
-= sizeof (char *);
124 network
->n_aliases
[i
] = line
;
126 while (*line
!= '\0' && *line
!= ' ')
136 network
->n_aliases
[i
+1] = NULL
;
142 static enum nss_status
143 _nss_create_tablename (int *errnop
)
145 if (tablename_val
== NULL
)
147 char buf
[40 + strlen (nis_local_directory ())];
150 p
= __stpcpy (buf
, "networks.org_dir.");
151 p
= __stpcpy (p
, nis_local_directory ());
152 tablename_val
= __strdup (buf
);
153 if (tablename_val
== NULL
)
156 return NSS_STATUS_TRYAGAIN
;
158 tablename_len
= strlen (tablename_val
);
160 return NSS_STATUS_SUCCESS
;
164 _nss_nisplus_setnetent (int stayopen
)
166 enum nss_status status
= NSS_STATUS_SUCCESS
;
169 __libc_lock_lock (lock
);
172 nis_freeresult (result
);
175 if (tablename_val
== NULL
)
176 status
= _nss_create_tablename (&err
);
178 __libc_lock_unlock (lock
);
184 _nss_nisplus_endnetent (void)
186 __libc_lock_lock (lock
);
189 nis_freeresult (result
);
192 __libc_lock_unlock (lock
);
194 return NSS_STATUS_SUCCESS
;
197 static enum nss_status
198 internal_nisplus_getnetent_r (struct netent
*network
, char *buffer
,
199 size_t buflen
, int *errnop
, int *herrnop
)
203 /* Get the next entry until we found a correct one. */
206 nis_result
*saved_res
;
212 if (tablename_val
== NULL
)
214 enum nss_status status
= _nss_create_tablename (errnop
);
216 if (status
!= NSS_STATUS_SUCCESS
)
220 result
= nis_first_entry (tablename_val
);
221 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
225 retval
= niserr2nss (result
->status
);
226 nis_freeresult (result
);
228 if (retval
== NSS_STATUS_TRYAGAIN
)
230 *herrnop
= NETDB_INTERNAL
;
242 res
= nis_next_entry (tablename_val
, &result
->cookie
);
245 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
249 retval
= niserr2nss (result
->status
);
250 nis_freeresult (result
);
252 if (retval
== NSS_STATUS_TRYAGAIN
)
254 *herrnop
= NETDB_INTERNAL
;
261 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
,
265 *herrnop
= NETDB_INTERNAL
;
266 return NSS_STATUS_TRYAGAIN
;
269 } while (!parse_res
);
271 return NSS_STATUS_SUCCESS
;
275 _nss_nisplus_getnetent_r (struct netent
*result
, char *buffer
,
276 size_t buflen
, int *errnop
, int *herrnop
)
280 __libc_lock_lock (lock
);
282 status
= internal_nisplus_getnetent_r (result
, buffer
, buflen
, errnop
,
285 __libc_lock_unlock (lock
);
291 _nss_nisplus_getnetbyname_r (const char *name
, struct netent
*network
,
292 char *buffer
, size_t buflen
, int *errnop
,
295 int parse_res
, retval
;
297 if (tablename_val
== NULL
)
299 enum nss_status status
= _nss_create_tablename (errnop
);
301 if (status
!= NSS_STATUS_SUCCESS
)
308 *herrnop
= NETDB_INTERNAL
;
309 return NSS_STATUS_UNAVAIL
;
314 char buf
[strlen (name
) + 255 + tablename_len
];
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
);
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
,
327 || result
->objects
.objects_val
[0].EN_data
.en_cols
.en_cols_len
< 3)
328 sprintf (buf
, "[cname=%s],%s", name
, tablename_val
);
330 sprintf (buf
, "[cname=%s],%s", NISENTRYVAL (0, 0, result
),
333 nis_freeresult (result
);
334 result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
336 retval
= niserr2nss (result
->status
);
337 if (retval
!= NSS_STATUS_SUCCESS
)
339 if (retval
== NSS_STATUS_TRYAGAIN
)
342 *herrnop
= NETDB_INTERNAL
;
344 nis_freeresult (result
);
348 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
, buflen
,
351 nis_freeresult (result
);
354 return NSS_STATUS_SUCCESS
;
356 *herrnop
= NETDB_INTERNAL
;
360 return NSS_STATUS_TRYAGAIN
;
363 return NSS_STATUS_NOTFOUND
;
367 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
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
)
373 if (tablename_val
== NULL
)
375 enum nss_status status
= _nss_create_tablename (errnop
);
377 if (status
!= NSS_STATUS_SUCCESS
)
382 int parse_res
, retval
;
384 char buf
[1024 + tablename_len
];
389 in
= inet_makeaddr (addr
, 0);
390 strcpy (buf2
, inet_ntoa (in
));
391 b2len
= strlen (buf2
);
395 sprintf (buf
, "[addr=%s],%s", buf2
, tablename_val
);
396 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
398 retval
= niserr2nss (result
->status
);
399 if (retval
!= NSS_STATUS_SUCCESS
)
401 if (buf2
[b2len
-2] == '.' && buf2
[b2len
-1] == '0')
403 /* Try again, but with trailing dot(s)
404 removed (one by one) */
405 buf2
[b2len
- 2] = '\0';
410 return NSS_STATUS_NOTFOUND
;
412 if (retval
== NSS_STATUS_TRYAGAIN
)
415 *herrnop
= NETDB_INTERNAL
;
417 nis_freeresult (result
);
421 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
,
424 nis_freeresult (result
);
427 return NSS_STATUS_SUCCESS
;
429 *herrnop
= NETDB_INTERNAL
;
433 return NSS_STATUS_TRYAGAIN
;
436 return NSS_STATUS_NOTFOUND
;