1 /* Copyright (C) 1997,1998,2001,2004,2005,2006 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"
35 static const struct timeval RPCTIMEOUT
= {10, 0};
36 static const struct timeval UDPTIMEOUT
= {5, 0};
38 extern u_short
__pmap_getnisport (struct sockaddr_in
*address
, u_long program
,
39 u_long version
, u_int protocol
);
42 inetstr2int (const char *str
)
45 for (size_t i
= 0; str
[i
] != '\0'; ++i
)
46 if (str
[i
] == '.' && ++j
== 4)
50 return inet_addr (memcpy (buffer
, str
, i
));
53 return inet_addr (str
);
57 __nisbind_destroy (dir_binding
*bind
)
59 if (bind
->clnt
!= NULL
)
62 auth_destroy (bind
->clnt
->cl_auth
);
63 clnt_destroy (bind
->clnt
);
66 libnsl_hidden_def (__nisbind_destroy
)
69 __nisbind_next (dir_binding
*bind
)
73 if (bind
->clnt
!= NULL
)
76 auth_destroy (bind
->clnt
->cl_auth
);
77 clnt_destroy (bind
->clnt
);
81 if (bind
->trys
>= bind
->server_len
)
84 for (j
= bind
->current_ep
+ 1;
85 j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
86 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
88 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
96 if (bind
->server_used
>= bind
->server_len
)
97 bind
->server_used
= 0;
99 for (j
= 0; j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
100 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
102 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
104 bind
->current_ep
= j
;
110 libnsl_hidden_def (__nisbind_next
)
113 __nisbind_connect (dir_binding
*dbp
)
120 serv
= &dbp
->server_val
[dbp
->server_used
];
122 memset (&dbp
->addr
, '\0', sizeof (dbp
->addr
));
123 dbp
->addr
.sin_family
= AF_INET
;
125 dbp
->addr
.sin_addr
.s_addr
=
126 inetstr2int (serv
->ep
.ep_val
[dbp
->current_ep
].uaddr
);
128 if (dbp
->addr
.sin_addr
.s_addr
== 0)
131 /* Check, if the host is online and rpc.nisd is running. Much faster
132 then the clnt*_create functions: */
133 if (__pmap_getnisport (&dbp
->addr
, NIS_PROG
, NIS_VERSION
, IPPROTO_UDP
) == 0)
136 dbp
->socket
= RPC_ANYSOCK
;
138 dbp
->clnt
= clntudp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
139 UDPTIMEOUT
, &dbp
->socket
);
141 dbp
->clnt
= clnttcp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
144 if (dbp
->clnt
== NULL
)
147 clnt_control (dbp
->clnt
, CLSET_TIMEOUT
, (caddr_t
) &RPCTIMEOUT
);
148 /* If the program exists, close the socket */
149 if (fcntl (dbp
->socket
, F_SETFD
, 1) == -1)
150 perror ("fcntl: F_SETFD");
154 if (serv
->key_type
== NIS_PK_DH
)
156 char netname
[MAXNETNAMELEN
+ 1];
159 p
= stpcpy (netname
, "unix.");
160 strncpy (p
, serv
->name
, MAXNETNAMELEN
- 5);
161 netname
[MAXNETNAMELEN
] = '\0';
162 // XXX What is this supposed to do? If we really want to replace
163 // XXX the first dot, then we might as well use unix@ as the
164 // XXX prefix string. --drepper
165 p
= strchr (netname
, '.');
168 authdes_pk_create (netname
, &serv
->pkey
, 300, NULL
, NULL
);
169 if (!dbp
->clnt
->cl_auth
)
170 dbp
->clnt
->cl_auth
= authunix_create_default ();
173 dbp
->clnt
->cl_auth
= authunix_create_default ();
178 libnsl_hidden_def (__nisbind_connect
)
181 __nisbind_create (dir_binding
*dbp
, const nis_server
*serv_val
,
182 unsigned int serv_len
, unsigned int flags
)
186 dbp
->server_len
= serv_len
;
187 dbp
->server_val
= (nis_server
*)serv_val
;
189 if (flags
& USE_DGRAM
)
192 dbp
->use_udp
= FALSE
;
194 if (flags
& NO_AUTHINFO
)
195 dbp
->use_auth
= FALSE
;
197 dbp
->use_auth
= TRUE
;
199 if (flags
& MASTER_ONLY
)
200 dbp
->master_only
= TRUE
;
202 dbp
->master_only
= FALSE
;
204 /* We try the first server */
208 if (__nis_findfastest (dbp
) < 1)
209 return NIS_NAMEUNREACHABLE
;
213 libnsl_hidden_def (__nisbind_create
)
215 /* __nisbind_connect (dbp) must be run before calling this function !
216 So we could use the same binding twice */
218 __do_niscall3 (dir_binding
*dbp
, u_long prog
, xdrproc_t xargs
, caddr_t req
,
219 xdrproc_t xres
, caddr_t resp
, unsigned int flags
, nis_cb
*cb
)
221 enum clnt_stat result
;
225 return NIS_NAMEUNREACHABLE
;
230 result
= clnt_call (dbp
->clnt
, prog
, xargs
, req
, xres
, resp
, RPCTIMEOUT
);
232 if (result
!= RPC_SUCCESS
)
233 retcode
= NIS_RPCERROR
;
239 if ((((nis_result
*)resp
)->status
== NIS_CBRESULTS
) &&
242 __nis_do_callback (dbp
, &((nis_result
*) resp
)->cookie
, cb
);
245 /* Yes, the missing break is correct. If we doesn't have to
246 start a callback, look if we have to search another server */
256 if (((nis_result
*)resp
)->status
== NIS_SYSTEMERROR
257 || ((nis_result
*)resp
)->status
== NIS_NOSUCHNAME
258 || ((nis_result
*)resp
)->status
== NIS_NOT_ME
)
261 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
263 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
265 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
270 break; /* No more servers to search in */
274 case NIS_FINDDIRECTORY
:
275 if (((fd_result
*)resp
)->status
== NIS_SYSTEMERROR
276 || ((fd_result
*)resp
)->status
== NIS_NOSUCHNAME
277 || ((fd_result
*)resp
)->status
== NIS_NOT_ME
)
280 case NIS_DUMPLOG
: /* log_result */
282 if (((log_result
*)resp
)->lr_status
== NIS_SYSTEMERROR
283 || ((log_result
*)resp
)->lr_status
== NIS_NOSUCHNAME
284 || ((log_result
*)resp
)->lr_status
== NIS_NOT_ME
)
290 retcode
= NIS_SUCCESS
;
293 while ((flags
& HARD_LOOKUP
) && retcode
== NIS_RPCERROR
);
297 libnsl_hidden_def (__do_niscall3
)
301 __do_niscall2 (const nis_server
*server
, u_int server_len
, u_long prog
,
302 xdrproc_t xargs
, caddr_t req
, xdrproc_t xres
, caddr_t resp
,
303 unsigned int flags
, nis_cb
*cb
)
308 if (flags
& MASTER_ONLY
)
311 status
= __nisbind_create (&dbp
, server
, server_len
, flags
);
312 if (status
!= NIS_SUCCESS
)
315 while (__nisbind_connect (&dbp
) != NIS_SUCCESS
)
316 if (__nisbind_next (&dbp
) != NIS_SUCCESS
)
317 return NIS_NAMEUNREACHABLE
;
319 status
= __do_niscall3 (&dbp
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
321 __nisbind_destroy (&dbp
);
327 static directory_obj
*
328 rec_dirsearch (const_nis_name name
, directory_obj
*dir
, nis_error
*status
)
333 switch (nis_dir_cmp (name
, dir
->do_name
))
336 *status
= NIS_SUCCESS
;
339 /* NOT_SEQUENTIAL means, go one up and try it there ! */
341 { /* We need data from a parent domain */
343 char ndomain
[strlen (name
) + 3];
345 nis_domain_of_r (dir
->do_name
, ndomain
, sizeof (ndomain
));
347 /* The root server of our domain is a replica of the parent
348 domain ! (Now I understand why a root server must be a
349 replica of the parent domain) */
350 fd_res
= __nis_finddirectory (dir
, ndomain
);
353 nis_free_directory (dir
);
354 *status
= NIS_NOMEMORY
;
357 *status
= fd_res
->status
;
358 if (fd_res
->status
!= NIS_SUCCESS
)
360 /* Try the current directory obj, maybe it works */
361 __free_fdresult (fd_res
);
364 nis_free_directory (dir
);
365 obj
= calloc (1, sizeof (directory_obj
));
368 __free_fdresult (fd_res
);
369 *status
= NIS_NOMEMORY
;
372 xdrmem_create (&xdrs
, fd_res
->dir_data
.dir_data_val
,
373 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
374 _xdr_directory_obj (&xdrs
, obj
);
376 __free_fdresult (fd_res
);
378 /* We have found a NIS+ server serving ndomain, now
379 let us search for "name" */
380 return rec_dirsearch (name
, obj
, status
);
386 size_t namelen
= strlen (name
);
387 char leaf
[namelen
+ 3];
388 char domain
[namelen
+ 3];
389 char ndomain
[namelen
+ 3];
393 strcpy (domain
, name
);
397 if (domain
[0] == '\0')
399 nis_free_directory (dir
);
402 nis_leaf_of_r (domain
, leaf
, sizeof (leaf
));
403 nis_domain_of_r (domain
, ndomain
, sizeof (ndomain
));
404 strcpy (domain
, ndomain
);
407 while (nis_dir_cmp (domain
, dir
->do_name
) != SAME_NAME
);
408 printf("%s: run=%u\n", __func__
, run
);
411 /* We have found the directory above. Use it. */
415 cp
= rawmemchr (leaf
, '\0');
419 fd_res
= __nis_finddirectory (dir
, leaf
);
422 nis_free_directory (dir
);
423 *status
= NIS_NOMEMORY
;
426 *status
= fd_res
->status
;
427 if (fd_res
->status
!= NIS_SUCCESS
)
429 /* Try the current directory object, maybe it works */
430 __free_fdresult (fd_res
);
433 nis_free_directory (dir
);
434 obj
= calloc (1, sizeof(directory_obj
));
437 __free_fdresult (fd_res
);
438 *status
= NIS_NOMEMORY
;
441 xdrmem_create (&xdrs
, fd_res
->dir_data
.dir_data_val
,
442 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
443 _xdr_directory_obj (&xdrs
, obj
);
445 __free_fdresult (fd_res
);
446 /* We have found a NIS+ server serving ndomain, now
447 let us search for "name" */
448 return rec_dirsearch (name
, obj
, status
);
452 nis_free_directory (dir
);
453 *status
= NIS_BADNAME
;
456 nis_free_directory (dir
);
461 /* We try to query the current server for the searched object,
462 maybe he know about it ? */
463 static directory_obj
*
464 first_shoot (const_nis_name name
, directory_obj
*dir
)
466 directory_obj
*obj
= NULL
;
469 char domain
[strlen (name
) + 3];
471 if (nis_dir_cmp (name
, dir
->do_name
) == SAME_NAME
)
474 nis_domain_of_r (name
, domain
, sizeof (domain
));
476 if (nis_dir_cmp (domain
, dir
->do_name
) == SAME_NAME
)
479 fd_res
= __nis_finddirectory (dir
, domain
);
482 if (fd_res
->status
== NIS_SUCCESS
483 && (obj
= calloc (1, sizeof (directory_obj
))) != NULL
)
485 xdrmem_create(&xdrs
, fd_res
->dir_data
.dir_data_val
,
486 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
487 _xdr_directory_obj (&xdrs
, obj
);
491 __free_fdresult (fd_res
);
494 nis_free_directory (dir
);
500 __nisfind_server (const_nis_name name
, directory_obj
**dir
)
506 /* Search in local cache. In the moment, we ignore the fastest server */
507 if (!(flags
& NO_CACHE
))
508 dir
= __nis_cache_search (name
, flags
, &cinfo
);
511 nis_error result
= NIS_SUCCESS
;
517 *dir
= readColdStartFile ();
519 /* No /var/nis/NIS_COLD_START->no NIS+ installed. */
522 /* Try at first, if servers in "dir" know our object */
523 obj
= first_shoot (name
, *dir
);
526 obj
= rec_dirsearch (name
, *dir
, &status
);
539 __prepare_niscall (const_nis_name name
, directory_obj
**dirp
,
540 dir_binding
*bptrp
, unsigned int flags
)
542 nis_error retcode
= __nisfind_server (name
, dirp
);
543 if (__builtin_expect (retcode
!= NIS_SUCCESS
, 0))
549 if (flags
& MASTER_ONLY
)
551 server
= (*dirp
)->do_servers
.do_servers_val
;
556 server
= (*dirp
)->do_servers
.do_servers_val
;
557 server_len
= (*dirp
)->do_servers
.do_servers_len
;
560 retcode
= __nisbind_create (bptrp
, server
, server_len
, flags
);
561 if (retcode
== NIS_SUCCESS
)
564 if (__nisbind_connect (bptrp
) == NIS_SUCCESS
)
566 while (__nisbind_next (bptrp
) == NIS_SUCCESS
);
568 __nisbind_destroy (bptrp
);
569 memset (bptrp
, '\0', sizeof (*bptrp
));
571 retcode
= NIS_NAMEUNREACHABLE
;
574 nis_free_directory (*dirp
);
579 libnsl_hidden_def (__prepare_niscall
)
583 __do_niscall (const_nis_name name
, u_long prog
, xdrproc_t xargs
,
584 caddr_t req
, xdrproc_t xres
, caddr_t resp
, unsigned int flags
,
588 directory_obj
*dir
= NULL
;
589 int saved_errno
= errno
;
591 nis_error retcode
= __prepare_niscall (name
, &dir
, &bptr
, flags
);
592 if (retcode
== NIS_SUCCESS
)
594 retcode
= __do_niscall3 (&bptr
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
596 __nisbind_destroy (&bptr
);
598 nis_free_directory (dir
);
601 __set_errno (saved_errno
);