1 /* Copyright (C) 1997, 1998, 2001, 2004, 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
26 #include <rpcsvc/nis.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
32 #include "nis_intern.h"
34 static const struct timeval RPCTIMEOUT
= {10, 0};
35 static const struct timeval UDPTIMEOUT
= {5, 0};
37 extern u_short
__pmap_getnisport (struct sockaddr_in
*address
, u_long program
,
38 u_long version
, u_int protocol
);
41 inetstr2int (const char *str
)
43 char buffer
[strlen (str
) + 3];
47 buflen
= stpcpy (buffer
, str
) - buffer
;
50 for (i
= 0; i
< buflen
; ++i
)
61 return inet_addr (buffer
);
65 __nisbind_destroy (dir_binding
*bind
)
67 if (bind
->clnt
!= NULL
)
70 auth_destroy (bind
->clnt
->cl_auth
);
71 clnt_destroy (bind
->clnt
);
74 libnsl_hidden_def (__nisbind_destroy
)
77 __nisbind_next (dir_binding
*bind
)
81 if (bind
->clnt
!= NULL
)
84 auth_destroy (bind
->clnt
->cl_auth
);
85 clnt_destroy (bind
->clnt
);
89 if (bind
->trys
>= bind
->server_len
)
92 for (j
= bind
->current_ep
+ 1;
93 j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
94 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
96 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
104 if (bind
->server_used
>= bind
->server_len
)
105 bind
->server_used
= 0;
107 for (j
= 0; j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
108 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
110 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
112 bind
->current_ep
= j
;
118 libnsl_hidden_def (__nisbind_next
)
121 __nisbind_connect (dir_binding
*dbp
)
128 serv
= &dbp
->server_val
[dbp
->server_used
];
130 memset (&dbp
->addr
, '\0', sizeof (dbp
->addr
));
131 dbp
->addr
.sin_family
= AF_INET
;
133 dbp
->addr
.sin_addr
.s_addr
=
134 inetstr2int (serv
->ep
.ep_val
[dbp
->current_ep
].uaddr
);
136 if (dbp
->addr
.sin_addr
.s_addr
== 0)
139 /* Check, if the host is online and rpc.nisd is running. Much faster
140 then the clnt*_create functions: */
141 if (__pmap_getnisport (&dbp
->addr
, NIS_PROG
, NIS_VERSION
, IPPROTO_UDP
) == 0)
144 dbp
->socket
= RPC_ANYSOCK
;
146 dbp
->clnt
= clntudp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
147 UDPTIMEOUT
, &dbp
->socket
);
149 dbp
->clnt
= clnttcp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
152 if (dbp
->clnt
== NULL
)
155 clnt_control (dbp
->clnt
, CLSET_TIMEOUT
, (caddr_t
) &RPCTIMEOUT
);
156 /* If the program exists, close the socket */
157 if (fcntl (dbp
->socket
, F_SETFD
, 1) == -1)
158 perror ("fcntl: F_SETFD");
162 if (serv
->key_type
== NIS_PK_DH
)
164 char netname
[MAXNETNAMELEN
+ 1];
167 p
= stpcpy (netname
, "unix.");
168 strncpy (p
, serv
->name
, MAXNETNAMELEN
- 5);
169 netname
[MAXNETNAMELEN
] = '\0';
170 // XXX What is this supposed to do? If we really want to replace
171 // XXX the first dot, then we might as well use unix@ as the
172 // XXX prefix string. --drepper
173 p
= strchr (netname
, '.');
176 authdes_pk_create (netname
, &serv
->pkey
, 300, NULL
, NULL
);
177 if (!dbp
->clnt
->cl_auth
)
178 dbp
->clnt
->cl_auth
= authunix_create_default ();
181 dbp
->clnt
->cl_auth
= authunix_create_default ();
186 libnsl_hidden_def (__nisbind_connect
)
189 __nisbind_create (dir_binding
*dbp
, const nis_server
*serv_val
,
190 unsigned int serv_len
, unsigned int flags
)
194 dbp
->server_len
= serv_len
;
195 dbp
->server_val
= (nis_server
*)serv_val
;
197 if (flags
& USE_DGRAM
)
200 dbp
->use_udp
= FALSE
;
202 if (flags
& NO_AUTHINFO
)
203 dbp
->use_auth
= FALSE
;
205 dbp
->use_auth
= TRUE
;
207 if (flags
& MASTER_ONLY
)
208 dbp
->master_only
= TRUE
;
210 dbp
->master_only
= FALSE
;
212 /* We try the first server */
216 if (__nis_findfastest (dbp
) < 1)
217 return NIS_NAMEUNREACHABLE
;
221 libnsl_hidden_def (__nisbind_create
)
223 /* __nisbind_connect (dbp) must be run before calling this function !
224 So we could use the same binding twice */
226 __do_niscall3 (dir_binding
*dbp
, u_long prog
, xdrproc_t xargs
, caddr_t req
,
227 xdrproc_t xres
, caddr_t resp
, unsigned int flags
, nis_cb
*cb
)
229 enum clnt_stat result
;
233 return NIS_NAMEUNREACHABLE
;
238 result
= clnt_call (dbp
->clnt
, prog
, xargs
, req
, xres
, resp
, RPCTIMEOUT
);
240 if (result
!= RPC_SUCCESS
)
241 retcode
= NIS_RPCERROR
;
247 if ((((nis_result
*)resp
)->status
== NIS_CBRESULTS
) &&
250 __nis_do_callback (dbp
, &((nis_result
*) resp
)->cookie
, cb
);
253 /* Yes, the missing break is correct. If we doesn't have to
254 start a callback, look if we have to search another server */
264 if (((nis_result
*)resp
)->status
== NIS_SYSTEMERROR
265 || ((nis_result
*)resp
)->status
== NIS_NOSUCHNAME
266 || ((nis_result
*)resp
)->status
== NIS_NOT_ME
)
268 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
270 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
272 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
277 break; /* No more servers to search in */
281 case NIS_FINDDIRECTORY
:
282 if (((fd_result
*)resp
)->status
== NIS_SYSTEMERROR
283 || ((fd_result
*)resp
)->status
== NIS_NOSUCHNAME
284 || ((fd_result
*)resp
)->status
== NIS_NOT_ME
)
286 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
288 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
290 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
295 break; /* No more servers to search in */
299 case NIS_DUMPLOG
: /* log_result */
301 if (((log_result
*)resp
)->lr_status
== NIS_SYSTEMERROR
302 || ((log_result
*)resp
)->lr_status
== NIS_NOSUCHNAME
303 || ((log_result
*)resp
)->lr_status
== NIS_NOT_ME
)
305 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
307 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
309 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
314 break; /* No more servers to search in */
321 retcode
= NIS_SUCCESS
;
324 while ((flags
& HARD_LOOKUP
) && retcode
== NIS_RPCERROR
);
330 __do_niscall2 (const nis_server
*server
, u_int server_len
, u_long prog
,
331 xdrproc_t xargs
, caddr_t req
, xdrproc_t xres
, caddr_t resp
,
332 unsigned int flags
, nis_cb
*cb
)
337 if (flags
& MASTER_ONLY
)
340 status
= __nisbind_create (&dbp
, server
, server_len
, flags
);
341 if (status
!= NIS_SUCCESS
)
344 while (__nisbind_connect (&dbp
) != NIS_SUCCESS
)
345 if (__nisbind_next (&dbp
) != NIS_SUCCESS
)
346 return NIS_NAMEUNREACHABLE
;
348 status
= __do_niscall3 (&dbp
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
350 __nisbind_destroy (&dbp
);
356 static directory_obj
*
357 rec_dirsearch (const_nis_name name
, directory_obj
*dir
, nis_error
*status
)
362 switch (nis_dir_cmp (name
, dir
->do_name
))
365 *status
= NIS_SUCCESS
;
368 /* NOT_SEQUENTIAL means, go one up and try it there ! */
370 { /* We need data from a parent domain */
372 char ndomain
[strlen (name
) + 3];
374 nis_domain_of_r (dir
->do_name
, ndomain
, sizeof (ndomain
));
376 /* The root server of our domain is a replica of the parent
377 domain ! (Now I understand why a root server must be a
378 replica of the parent domain) */
379 fd_res
= __nis_finddirectory (dir
, ndomain
);
380 *status
= fd_res
->status
;
381 if (fd_res
->status
!= NIS_SUCCESS
)
383 /* Try the current directory obj, maybe it works */
384 __free_fdresult (fd_res
);
387 obj
= calloc (1, sizeof (directory_obj
));
388 xdrmem_create (&xdrs
, fd_res
->dir_data
.dir_data_val
,
389 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
390 _xdr_directory_obj (&xdrs
, obj
);
392 __free_fdresult (fd_res
);
395 /* We have found a NIS+ server serving ndomain, now
396 let us search for "name" */
397 nis_free_directory (dir
);
398 return rec_dirsearch (name
, obj
, status
);
402 /* Ups, very bad. Are we already the root server ? */
403 nis_free_directory (dir
);
411 size_t namelen
= strlen (name
);
412 char leaf
[namelen
+ 3];
413 char domain
[namelen
+ 3];
414 char ndomain
[namelen
+ 3];
418 strcpy (domain
, name
);
422 if (domain
[0] == '\0')
424 nis_free_directory (dir
);
427 nis_leaf_of_r (domain
, leaf
, sizeof (leaf
));
428 nis_domain_of_r (domain
, ndomain
, sizeof (ndomain
));
429 strcpy (domain
, ndomain
);
432 while (nis_dir_cmp (domain
, dir
->do_name
) != SAME_NAME
);
436 /* We have found the directory above. Use it. */
440 cp
= strchr (leaf
, '\0');
444 fd_res
= __nis_finddirectory (dir
, leaf
);
445 *status
= fd_res
->status
;
446 if (fd_res
->status
!= NIS_SUCCESS
)
448 /* Try the current directory object, maybe it works */
449 __free_fdresult (fd_res
);
452 obj
= calloc(1, sizeof(directory_obj
));
453 xdrmem_create(&xdrs
, fd_res
->dir_data
.dir_data_val
,
454 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
455 _xdr_directory_obj(&xdrs
, obj
);
457 __free_fdresult (fd_res
);
460 /* We have found a NIS+ server serving ndomain, now
461 let us search for "name" */
462 nis_free_directory (dir
);
463 return rec_dirsearch (name
, obj
, status
);
468 nis_free_directory (dir
);
469 *status
= NIS_BADNAME
;
472 nis_free_directory (dir
);
477 /* We try to query the current server for the searched object,
478 maybe he know about it ? */
479 static directory_obj
*
480 first_shoot (const_nis_name name
, directory_obj
*dir
)
482 directory_obj
*obj
= NULL
;
485 char domain
[strlen (name
) + 3];
487 if (nis_dir_cmp (name
, dir
->do_name
) == SAME_NAME
)
490 nis_domain_of_r (name
, domain
, sizeof (domain
));
492 if (nis_dir_cmp (domain
, dir
->do_name
) == SAME_NAME
)
495 fd_res
= __nis_finddirectory (dir
, domain
);
496 if (fd_res
->status
== NIS_SUCCESS
497 && (obj
= calloc (1, sizeof (directory_obj
))) != NULL
)
499 xdrmem_create(&xdrs
, fd_res
->dir_data
.dir_data_val
,
500 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
501 _xdr_directory_obj (&xdrs
, obj
);
505 __free_fdresult (fd_res
);
508 nis_free_directory (dir
);
514 __nisfind_server (const_nis_name name
, directory_obj
**dir
)
520 /* Search in local cache. In the moment, we ignore the fastest server */
521 if (!(flags
& NO_CACHE
))
522 dir
= __nis_cache_search (name
, flags
, &cinfo
);
530 *dir
= readColdStartFile ();
531 if (*dir
== NULL
) /* No /var/nis/NIS_COLD_START->no NIS+ installed */
534 /* Try at first, if servers in "dir" know our object */
535 obj
= first_shoot (name
, *dir
);
538 *dir
= rec_dirsearch (name
, *dir
, &status
);
550 __do_niscall (const_nis_name name
, u_long prog
, xdrproc_t xargs
,
551 caddr_t req
, xdrproc_t xres
, caddr_t resp
, unsigned int flags
,
556 directory_obj
*dir
= NULL
;
559 int saved_errno
= errno
;
561 retcode
= __nisfind_server (name
, &dir
);
562 if (retcode
!= NIS_SUCCESS
)
565 if (flags
& MASTER_ONLY
)
567 server
= dir
->do_servers
.do_servers_val
;
572 server
= dir
->do_servers
.do_servers_val
;
573 server_len
= dir
->do_servers
.do_servers_len
;
576 retcode
= __nisbind_create (&bptr
, server
, server_len
, flags
);
577 if (retcode
== NIS_SUCCESS
)
579 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
581 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
583 nis_free_directory (dir
);
584 return NIS_NAMEUNREACHABLE
;
587 retcode
= __do_niscall3 (&bptr
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
589 __nisbind_destroy (&bptr
);
592 nis_free_directory (dir
);
594 __set_errno (saved_errno
);