1 /* Copyright (C) 1997, 1998, 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 <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 struct timeval RPCTIMEOUT
= {10, 0};
35 static 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
);
76 __nisbind_next (dir_binding
*bind
)
80 if (bind
->clnt
!= NULL
)
83 auth_destroy (bind
->clnt
->cl_auth
);
84 clnt_destroy (bind
->clnt
);
88 if (bind
->trys
>= bind
->server_len
)
91 for (j
= bind
->current_ep
+ 1;
92 j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
93 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
95 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
103 if (bind
->server_used
>= bind
->server_len
)
104 bind
->server_used
= 0;
106 for (j
= 0; j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
107 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
109 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
111 bind
->current_ep
= j
;
119 __nisbind_connect (dir_binding
*dbp
)
126 serv
= &dbp
->server_val
[dbp
->server_used
];
128 memset (&dbp
->addr
, '\0', sizeof (dbp
->addr
));
129 dbp
->addr
.sin_family
= AF_INET
;
131 dbp
->addr
.sin_addr
.s_addr
=
132 inetstr2int (serv
->ep
.ep_val
[dbp
->current_ep
].uaddr
);
134 if (dbp
->addr
.sin_addr
.s_addr
== 0)
137 /* Check, if the host is online and rpc.nisd is running. Much faster
138 then the clnt*_create functions: */
139 if (__pmap_getnisport (&dbp
->addr
, NIS_PROG
, NIS_VERSION
, IPPROTO_UDP
) == 0)
142 dbp
->socket
= RPC_ANYSOCK
;
144 dbp
->clnt
= clntudp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
145 UDPTIMEOUT
, &dbp
->socket
);
147 dbp
->clnt
= clnttcp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
150 if (dbp
->clnt
== NULL
)
153 clnt_control (dbp
->clnt
, CLSET_TIMEOUT
, (caddr_t
)&RPCTIMEOUT
);
154 /* If the program exists, close the socket */
155 if (fcntl (dbp
->socket
, F_SETFD
, 1) == -1)
156 perror ("fcntl: F_SETFD");
160 if (serv
->key_type
== NIS_PK_DH
)
162 char netname
[MAXNETNAMELEN
+1];
165 p
= stpcpy (netname
, "unix.");
166 strncpy (p
, serv
->name
,MAXNETNAMELEN
-5);
167 netname
[MAXNETNAMELEN
] = '\0';
168 p
= strchr (netname
, '.');
171 authdes_pk_create (netname
, &serv
->pkey
, 300, NULL
, NULL
);
172 if (!dbp
->clnt
->cl_auth
)
173 dbp
->clnt
->cl_auth
= authunix_create_default ();
176 dbp
->clnt
->cl_auth
= authunix_create_default ();
177 dbp
->use_auth
= TRUE
;
184 __nisbind_create (dir_binding
*dbp
, const nis_server
*serv_val
,
185 unsigned int serv_len
, unsigned int flags
)
189 dbp
->server_len
= serv_len
;
190 dbp
->server_val
= (nis_server
*)serv_val
;
192 if (flags
& USE_DGRAM
)
195 dbp
->use_udp
= FALSE
;
197 if (flags
& NO_AUTHINFO
)
198 dbp
->use_auth
= FALSE
;
200 dbp
->use_auth
= TRUE
;
202 if (flags
& MASTER_ONLY
)
203 dbp
->master_only
= TRUE
;
205 dbp
->master_only
= FALSE
;
207 /* We try the first server */
211 if (__nis_findfastest (dbp
) < 1)
213 __nisbind_destroy (dbp
);
214 return NIS_NAMEUNREACHABLE
;
220 /* __nisbind_connect (dbp) must be run before calling this function !
221 So we could use the same binding twice */
223 __do_niscall3 (dir_binding
*dbp
, u_long prog
, xdrproc_t xargs
, caddr_t req
,
224 xdrproc_t xres
, caddr_t resp
, unsigned int flags
, nis_cb
*cb
)
226 enum clnt_stat result
;
230 return NIS_NAMEUNREACHABLE
;
235 result
= clnt_call (dbp
->clnt
, prog
, xargs
, req
, xres
, resp
, RPCTIMEOUT
);
237 if (result
!= RPC_SUCCESS
)
238 retcode
= NIS_RPCERROR
;
244 if ((((nis_result
*)resp
)->status
== NIS_CBRESULTS
) &&
247 __nis_do_callback(dbp
, &((nis_result
*)resp
)->cookie
, cb
);
250 /* Yes, the missing break is correct. If we doesn't have to
251 start a callback, look if we have to search another server */
261 if ((((nis_result
*)resp
)->status
== NIS_SYSTEMERROR
) ||
262 (((nis_result
*)resp
)->status
== NIS_NOSUCHNAME
) ||
263 (((nis_result
*)resp
)->status
== NIS_NOT_ME
))
265 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
267 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
269 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
274 break; /* No more servers to search in */
278 case NIS_FINDDIRECTORY
:
279 if ((((fd_result
*)resp
)->status
== NIS_SYSTEMERROR
) ||
280 (((fd_result
*)resp
)->status
== NIS_NOSUCHNAME
) ||
281 (((fd_result
*)resp
)->status
== NIS_NOT_ME
))
283 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
285 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
287 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
292 break; /* No more servers to search in */
296 case NIS_DUMPLOG
: /* log_result */
298 if ((((log_result
*)resp
)->lr_status
== NIS_SYSTEMERROR
) ||
299 (((log_result
*)resp
)->lr_status
== NIS_NOSUCHNAME
) ||
300 (((log_result
*)resp
)->lr_status
== NIS_NOT_ME
))
302 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
304 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
306 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
311 break; /* No more servers to search in */
318 retcode
= NIS_SUCCESS
;
321 while ((flags
& HARD_LOOKUP
) && retcode
== NIS_RPCERROR
);
327 __do_niscall2 (const nis_server
*server
, u_int server_len
, u_long prog
,
328 xdrproc_t xargs
, caddr_t req
, xdrproc_t xres
, caddr_t resp
,
329 unsigned int flags
, nis_cb
*cb
)
334 if (flags
& MASTER_ONLY
)
337 status
= __nisbind_create (&dbp
, server
, server_len
, flags
);
338 if (status
!= NIS_SUCCESS
)
341 while (__nisbind_connect (&dbp
) != NIS_SUCCESS
)
343 if (__nisbind_next (&dbp
) != NIS_SUCCESS
)
344 return NIS_NAMEUNREACHABLE
;
347 status
= __do_niscall3 (&dbp
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
349 __nisbind_destroy (&dbp
);
355 static directory_obj
*
356 rec_dirsearch (const_nis_name name
, directory_obj
*dir
, nis_error
*status
)
361 switch (nis_dir_cmp (name
, dir
->do_name
))
364 *status
= NIS_SUCCESS
;
367 /* NOT_SEQUENTIAL means, go one up and try it there ! */
369 { /* We need data from a parent domain */
371 char ndomain
[strlen (name
) + 3];
373 nis_domain_of_r (dir
->do_name
, ndomain
, sizeof (ndomain
));
375 /* The root server of our domain is a replica of the parent
376 domain ! (Now I understand why a root server must be a
377 replica of the parent domain) */
378 fd_res
= __nis_finddirectory (dir
, ndomain
);
379 *status
= fd_res
->status
;
380 if (fd_res
->status
!= NIS_SUCCESS
)
382 /* Try the current directory obj, maybe it works */
383 __free_fdresult (fd_res
);
386 obj
= calloc(1, sizeof(directory_obj
));
387 xdrmem_create(&xdrs
, fd_res
->dir_data
.dir_data_val
,
388 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
389 _xdr_directory_obj(&xdrs
, obj
);
391 __free_fdresult (fd_res
);
394 /* We have found a NIS+ server serving ndomain, now
395 let us search for "name" */
396 nis_free_directory (dir
);
397 return rec_dirsearch (name
, obj
, status
);
401 /* Ups, very bad. Are we already the root server ? */
402 nis_free_directory (dir
);
410 size_t namelen
= strlen (name
);
411 char leaf
[namelen
+ 3];
412 char domain
[namelen
+ 3];
413 char ndomain
[namelen
+ 3];
417 strcpy (domain
, name
);
421 if (domain
[0] == '\0')
423 nis_free_directory (dir
);
426 nis_leaf_of_r (domain
, leaf
, sizeof (leaf
));
427 nis_domain_of_r (domain
, ndomain
, sizeof (ndomain
));
428 strcpy (domain
, ndomain
);
431 while (nis_dir_cmp (domain
, dir
->do_name
) != SAME_NAME
);
435 /* We have found the directory above. Use it. */
439 cp
= strchr (leaf
, '\0');
443 fd_res
= __nis_finddirectory (dir
, leaf
);
444 *status
= fd_res
->status
;
445 if (fd_res
->status
!= NIS_SUCCESS
)
447 /* Try the current directory object, maybe it works */
448 __free_fdresult (fd_res
);
451 obj
= calloc(1, sizeof(directory_obj
));
452 xdrmem_create(&xdrs
, fd_res
->dir_data
.dir_data_val
,
453 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
454 _xdr_directory_obj(&xdrs
, obj
);
456 __free_fdresult (fd_res
);
459 /* We have found a NIS+ server serving ndomain, now
460 let us search for "name" */
461 nis_free_directory (dir
);
462 return rec_dirsearch (name
, obj
, status
);
467 nis_free_directory (dir
);
468 *status
= NIS_BADNAME
;
471 nis_free_directory (dir
);
476 /* We try to query the current server for the searched object,
477 maybe he know about it ? */
478 static directory_obj
*
479 first_shoot (const_nis_name name
, directory_obj
*dir
)
484 char domain
[strlen (name
) + 3];
486 if (nis_dir_cmp (name
, dir
->do_name
) == SAME_NAME
)
489 nis_domain_of_r (name
, domain
, sizeof (domain
));
491 if (nis_dir_cmp (domain
, dir
->do_name
) == SAME_NAME
)
494 fd_res
= __nis_finddirectory (dir
, domain
);
495 if (fd_res
->status
!= NIS_SUCCESS
)
497 __free_fdresult (fd_res
);
500 obj
= calloc(1, sizeof(directory_obj
));
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
);
507 __free_fdresult (fd_res
);
510 nis_free_directory (dir
);
517 __nisfind_server (const_nis_name name
, directory_obj
**dir
)
523 /* Search in local cache. In the moment, we ignore the fastest server */
524 if (!(flags
& NO_CACHE
))
525 dir
= __nis_cache_search (name
, flags
, &cinfo
);
533 *dir
= readColdStartFile ();
534 if (*dir
== NULL
) /* No /var/nis/NIS_COLD_START->no NIS+ installed */
537 /* Try at first, if servers in "dir" know our object */
538 obj
= first_shoot (name
, *dir
);
541 *dir
= rec_dirsearch (name
, *dir
, &status
);
553 __do_niscall (const_nis_name name
, u_long prog
, xdrproc_t xargs
,
554 caddr_t req
, xdrproc_t xres
, caddr_t resp
, unsigned int flags
,
559 directory_obj
*dir
= NULL
;
562 int saved_errno
= errno
;
564 retcode
= __nisfind_server (name
, &dir
);
565 if (retcode
!= NIS_SUCCESS
)
568 if (flags
& MASTER_ONLY
)
570 server
= dir
->do_servers
.do_servers_val
;
575 server
= dir
->do_servers
.do_servers_val
;
576 server_len
= dir
->do_servers
.do_servers_len
;
579 retcode
= __nisbind_create (&bptr
, server
, server_len
, flags
);
580 if (retcode
== NIS_SUCCESS
)
582 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
584 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
586 nis_free_directory (dir
);
587 __nisbind_destroy (&bptr
);
588 return NIS_NAMEUNREACHABLE
;
591 retcode
= __do_niscall3 (&bptr
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
593 __nisbind_destroy (&bptr
);
596 nis_free_directory (dir
);
598 __set_errno (saved_errno
);