1 /* Copyright (C) 1997, 1998, 2001, 2004 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 ();
182 dbp
->use_auth
= TRUE
;
187 libnsl_hidden_def (__nisbind_connect
)
190 __nisbind_create (dir_binding
*dbp
, const nis_server
*serv_val
,
191 unsigned int serv_len
, unsigned int flags
)
195 dbp
->server_len
= serv_len
;
196 dbp
->server_val
= (nis_server
*)serv_val
;
198 if (flags
& USE_DGRAM
)
201 dbp
->use_udp
= FALSE
;
203 if (flags
& NO_AUTHINFO
)
204 dbp
->use_auth
= FALSE
;
206 dbp
->use_auth
= TRUE
;
208 if (flags
& MASTER_ONLY
)
209 dbp
->master_only
= TRUE
;
211 dbp
->master_only
= FALSE
;
213 /* We try the first server */
217 if (__nis_findfastest (dbp
) < 1)
219 __nisbind_destroy (dbp
);
220 return NIS_NAMEUNREACHABLE
;
225 libnsl_hidden_def (__nisbind_create
)
227 /* __nisbind_connect (dbp) must be run before calling this function !
228 So we could use the same binding twice */
230 __do_niscall3 (dir_binding
*dbp
, u_long prog
, xdrproc_t xargs
, caddr_t req
,
231 xdrproc_t xres
, caddr_t resp
, unsigned int flags
, nis_cb
*cb
)
233 enum clnt_stat result
;
237 return NIS_NAMEUNREACHABLE
;
242 result
= clnt_call (dbp
->clnt
, prog
, xargs
, req
, xres
, resp
, RPCTIMEOUT
);
244 if (result
!= RPC_SUCCESS
)
245 retcode
= NIS_RPCERROR
;
251 if ((((nis_result
*)resp
)->status
== NIS_CBRESULTS
) &&
254 __nis_do_callback (dbp
, &((nis_result
*) resp
)->cookie
, cb
);
257 /* Yes, the missing break is correct. If we doesn't have to
258 start a callback, look if we have to search another server */
268 if (((nis_result
*)resp
)->status
== NIS_SYSTEMERROR
269 || ((nis_result
*)resp
)->status
== NIS_NOSUCHNAME
270 || ((nis_result
*)resp
)->status
== NIS_NOT_ME
)
272 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
274 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
276 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
281 break; /* No more servers to search in */
285 case NIS_FINDDIRECTORY
:
286 if (((fd_result
*)resp
)->status
== NIS_SYSTEMERROR
287 || ((fd_result
*)resp
)->status
== NIS_NOSUCHNAME
288 || ((fd_result
*)resp
)->status
== NIS_NOT_ME
)
290 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
292 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
294 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
299 break; /* No more servers to search in */
303 case NIS_DUMPLOG
: /* log_result */
305 if (((log_result
*)resp
)->lr_status
== NIS_SYSTEMERROR
306 || ((log_result
*)resp
)->lr_status
== NIS_NOSUCHNAME
307 || ((log_result
*)resp
)->lr_status
== NIS_NOT_ME
)
309 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
311 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
313 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
318 break; /* No more servers to search in */
325 retcode
= NIS_SUCCESS
;
328 while ((flags
& HARD_LOOKUP
) && retcode
== NIS_RPCERROR
);
334 __do_niscall2 (const nis_server
*server
, u_int server_len
, u_long prog
,
335 xdrproc_t xargs
, caddr_t req
, xdrproc_t xres
, caddr_t resp
,
336 unsigned int flags
, nis_cb
*cb
)
341 if (flags
& MASTER_ONLY
)
344 status
= __nisbind_create (&dbp
, server
, server_len
, flags
);
345 if (status
!= NIS_SUCCESS
)
348 while (__nisbind_connect (&dbp
) != NIS_SUCCESS
)
349 if (__nisbind_next (&dbp
) != NIS_SUCCESS
)
350 return NIS_NAMEUNREACHABLE
;
352 status
= __do_niscall3 (&dbp
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
354 __nisbind_destroy (&dbp
);
360 static directory_obj
*
361 rec_dirsearch (const_nis_name name
, directory_obj
*dir
, nis_error
*status
)
366 switch (nis_dir_cmp (name
, dir
->do_name
))
369 *status
= NIS_SUCCESS
;
372 /* NOT_SEQUENTIAL means, go one up and try it there ! */
374 { /* We need data from a parent domain */
376 char ndomain
[strlen (name
) + 3];
378 nis_domain_of_r (dir
->do_name
, ndomain
, sizeof (ndomain
));
380 /* The root server of our domain is a replica of the parent
381 domain ! (Now I understand why a root server must be a
382 replica of the parent domain) */
383 fd_res
= __nis_finddirectory (dir
, ndomain
);
384 *status
= fd_res
->status
;
385 if (fd_res
->status
!= NIS_SUCCESS
)
387 /* Try the current directory obj, maybe it works */
388 __free_fdresult (fd_res
);
391 obj
= calloc (1, sizeof (directory_obj
));
392 xdrmem_create (&xdrs
, fd_res
->dir_data
.dir_data_val
,
393 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
394 _xdr_directory_obj (&xdrs
, obj
);
396 __free_fdresult (fd_res
);
399 /* We have found a NIS+ server serving ndomain, now
400 let us search for "name" */
401 nis_free_directory (dir
);
402 return rec_dirsearch (name
, obj
, status
);
406 /* Ups, very bad. Are we already the root server ? */
407 nis_free_directory (dir
);
415 size_t namelen
= strlen (name
);
416 char leaf
[namelen
+ 3];
417 char domain
[namelen
+ 3];
418 char ndomain
[namelen
+ 3];
422 strcpy (domain
, name
);
426 if (domain
[0] == '\0')
428 nis_free_directory (dir
);
431 nis_leaf_of_r (domain
, leaf
, sizeof (leaf
));
432 nis_domain_of_r (domain
, ndomain
, sizeof (ndomain
));
433 strcpy (domain
, ndomain
);
436 while (nis_dir_cmp (domain
, dir
->do_name
) != SAME_NAME
);
440 /* We have found the directory above. Use it. */
444 cp
= strchr (leaf
, '\0');
448 fd_res
= __nis_finddirectory (dir
, leaf
);
449 *status
= fd_res
->status
;
450 if (fd_res
->status
!= NIS_SUCCESS
)
452 /* Try the current directory object, maybe it works */
453 __free_fdresult (fd_res
);
456 obj
= calloc(1, sizeof(directory_obj
));
457 xdrmem_create(&xdrs
, fd_res
->dir_data
.dir_data_val
,
458 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
459 _xdr_directory_obj(&xdrs
, obj
);
461 __free_fdresult (fd_res
);
464 /* We have found a NIS+ server serving ndomain, now
465 let us search for "name" */
466 nis_free_directory (dir
);
467 return rec_dirsearch (name
, obj
, status
);
472 nis_free_directory (dir
);
473 *status
= NIS_BADNAME
;
476 nis_free_directory (dir
);
481 /* We try to query the current server for the searched object,
482 maybe he know about it ? */
483 static directory_obj
*
484 first_shoot (const_nis_name name
, directory_obj
*dir
)
486 directory_obj
*obj
= NULL
;
489 char domain
[strlen (name
) + 3];
491 if (nis_dir_cmp (name
, dir
->do_name
) == SAME_NAME
)
494 nis_domain_of_r (name
, domain
, sizeof (domain
));
496 if (nis_dir_cmp (domain
, dir
->do_name
) == SAME_NAME
)
499 fd_res
= __nis_finddirectory (dir
, domain
);
500 if (fd_res
->status
== NIS_SUCCESS
501 && (obj
= calloc (1, sizeof (directory_obj
))) != NULL
)
503 xdrmem_create(&xdrs
, fd_res
->dir_data
.dir_data_val
,
504 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
505 _xdr_directory_obj (&xdrs
, obj
);
509 __free_fdresult (fd_res
);
512 nis_free_directory (dir
);
518 __nisfind_server (const_nis_name name
, directory_obj
**dir
)
524 /* Search in local cache. In the moment, we ignore the fastest server */
525 if (!(flags
& NO_CACHE
))
526 dir
= __nis_cache_search (name
, flags
, &cinfo
);
534 *dir
= readColdStartFile ();
535 if (*dir
== NULL
) /* No /var/nis/NIS_COLD_START->no NIS+ installed */
538 /* Try at first, if servers in "dir" know our object */
539 obj
= first_shoot (name
, *dir
);
542 *dir
= rec_dirsearch (name
, *dir
, &status
);
554 __do_niscall (const_nis_name name
, u_long prog
, xdrproc_t xargs
,
555 caddr_t req
, xdrproc_t xres
, caddr_t resp
, unsigned int flags
,
560 directory_obj
*dir
= NULL
;
563 int saved_errno
= errno
;
565 retcode
= __nisfind_server (name
, &dir
);
566 if (retcode
!= NIS_SUCCESS
)
569 if (flags
& MASTER_ONLY
)
571 server
= dir
->do_servers
.do_servers_val
;
576 server
= dir
->do_servers
.do_servers_val
;
577 server_len
= dir
->do_servers
.do_servers_len
;
580 retcode
= __nisbind_create (&bptr
, server
, server_len
, flags
);
581 if (retcode
== NIS_SUCCESS
)
583 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
585 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
587 nis_free_directory (dir
);
588 __nisbind_destroy (&bptr
);
589 return NIS_NAMEUNREACHABLE
;
592 retcode
= __do_niscall3 (&bptr
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
594 __nisbind_destroy (&bptr
);
597 nis_free_directory (dir
);
599 __set_errno (saved_errno
);