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"
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
)
44 for (size_t i
= 0; str
[i
] != '\0'; ++i
)
45 if (str
[i
] == '.' && ++j
== 4)
49 return inet_addr (memcpy (buffer
, str
, i
));
52 return inet_addr (str
);
56 __nisbind_destroy (dir_binding
*bind
)
58 if (bind
->clnt
!= NULL
)
61 auth_destroy (bind
->clnt
->cl_auth
);
62 clnt_destroy (bind
->clnt
);
65 libnsl_hidden_def (__nisbind_destroy
)
68 __nisbind_next (dir_binding
*bind
)
72 if (bind
->clnt
!= NULL
)
75 auth_destroy (bind
->clnt
->cl_auth
);
76 clnt_destroy (bind
->clnt
);
80 if (bind
->trys
>= bind
->server_len
)
83 for (j
= bind
->current_ep
+ 1;
84 j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
85 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
87 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
95 if (bind
->server_used
>= bind
->server_len
)
96 bind
->server_used
= 0;
98 for (j
= 0; j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
99 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
101 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
103 bind
->current_ep
= j
;
109 libnsl_hidden_def (__nisbind_next
)
112 __nisbind_connect (dir_binding
*dbp
)
119 serv
= &dbp
->server_val
[dbp
->server_used
];
121 memset (&dbp
->addr
, '\0', sizeof (dbp
->addr
));
122 dbp
->addr
.sin_family
= AF_INET
;
124 dbp
->addr
.sin_addr
.s_addr
=
125 inetstr2int (serv
->ep
.ep_val
[dbp
->current_ep
].uaddr
);
127 if (dbp
->addr
.sin_addr
.s_addr
== 0)
130 /* Check, if the host is online and rpc.nisd is running. Much faster
131 then the clnt*_create functions: */
132 if (__pmap_getnisport (&dbp
->addr
, NIS_PROG
, NIS_VERSION
, IPPROTO_UDP
) == 0)
135 dbp
->socket
= RPC_ANYSOCK
;
137 dbp
->clnt
= clntudp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
138 UDPTIMEOUT
, &dbp
->socket
);
140 dbp
->clnt
= clnttcp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
143 if (dbp
->clnt
== NULL
)
146 clnt_control (dbp
->clnt
, CLSET_TIMEOUT
, (caddr_t
) &RPCTIMEOUT
);
147 /* If the program exists, close the socket */
148 if (fcntl (dbp
->socket
, F_SETFD
, 1) == -1)
149 perror ("fcntl: F_SETFD");
153 if (serv
->key_type
== NIS_PK_DH
)
155 char netname
[MAXNETNAMELEN
+ 1];
158 p
= stpcpy (netname
, "unix.");
159 strncpy (p
, serv
->name
, MAXNETNAMELEN
- 5);
160 netname
[MAXNETNAMELEN
] = '\0';
161 // XXX What is this supposed to do? If we really want to replace
162 // XXX the first dot, then we might as well use unix@ as the
163 // XXX prefix string. --drepper
164 p
= strchr (netname
, '.');
167 authdes_pk_create (netname
, &serv
->pkey
, 300, NULL
, NULL
);
168 if (!dbp
->clnt
->cl_auth
)
169 dbp
->clnt
->cl_auth
= authunix_create_default ();
172 dbp
->clnt
->cl_auth
= authunix_create_default ();
177 libnsl_hidden_def (__nisbind_connect
)
180 __nisbind_create (dir_binding
*dbp
, const nis_server
*serv_val
,
181 unsigned int serv_len
, unsigned int flags
)
185 dbp
->server_len
= serv_len
;
186 dbp
->server_val
= (nis_server
*)serv_val
;
188 if (flags
& USE_DGRAM
)
191 dbp
->use_udp
= FALSE
;
193 if (flags
& NO_AUTHINFO
)
194 dbp
->use_auth
= FALSE
;
196 dbp
->use_auth
= TRUE
;
198 if (flags
& MASTER_ONLY
)
199 dbp
->master_only
= TRUE
;
201 dbp
->master_only
= FALSE
;
203 /* We try the first server */
207 if (__nis_findfastest (dbp
) < 1)
208 return NIS_NAMEUNREACHABLE
;
212 libnsl_hidden_def (__nisbind_create
)
214 /* __nisbind_connect (dbp) must be run before calling this function !
215 So we could use the same binding twice */
217 __do_niscall3 (dir_binding
*dbp
, u_long prog
, xdrproc_t xargs
, caddr_t req
,
218 xdrproc_t xres
, caddr_t resp
, unsigned int flags
, nis_cb
*cb
)
220 enum clnt_stat result
;
224 return NIS_NAMEUNREACHABLE
;
229 result
= clnt_call (dbp
->clnt
, prog
, xargs
, req
, xres
, resp
, RPCTIMEOUT
);
231 if (result
!= RPC_SUCCESS
)
232 retcode
= NIS_RPCERROR
;
238 if ((((nis_result
*)resp
)->status
== NIS_CBRESULTS
) &&
241 __nis_do_callback (dbp
, &((nis_result
*) resp
)->cookie
, cb
);
244 /* Yes, the missing break is correct. If we doesn't have to
245 start a callback, look if we have to search another server */
255 if (((nis_result
*)resp
)->status
== NIS_SYSTEMERROR
256 || ((nis_result
*)resp
)->status
== NIS_NOSUCHNAME
257 || ((nis_result
*)resp
)->status
== NIS_NOT_ME
)
259 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
261 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
263 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
268 break; /* No more servers to search in */
272 case NIS_FINDDIRECTORY
:
273 if (((fd_result
*)resp
)->status
== NIS_SYSTEMERROR
274 || ((fd_result
*)resp
)->status
== NIS_NOSUCHNAME
275 || ((fd_result
*)resp
)->status
== NIS_NOT_ME
)
277 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
279 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
281 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
286 break; /* No more servers to search in */
290 case NIS_DUMPLOG
: /* log_result */
292 if (((log_result
*)resp
)->lr_status
== NIS_SYSTEMERROR
293 || ((log_result
*)resp
)->lr_status
== NIS_NOSUCHNAME
294 || ((log_result
*)resp
)->lr_status
== NIS_NOT_ME
)
296 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
298 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
300 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
305 break; /* No more servers to search in */
312 retcode
= NIS_SUCCESS
;
315 while ((flags
& HARD_LOOKUP
) && retcode
== NIS_RPCERROR
);
321 __do_niscall2 (const nis_server
*server
, u_int server_len
, u_long prog
,
322 xdrproc_t xargs
, caddr_t req
, xdrproc_t xres
, caddr_t resp
,
323 unsigned int flags
, nis_cb
*cb
)
328 if (flags
& MASTER_ONLY
)
331 status
= __nisbind_create (&dbp
, server
, server_len
, flags
);
332 if (status
!= NIS_SUCCESS
)
335 while (__nisbind_connect (&dbp
) != NIS_SUCCESS
)
336 if (__nisbind_next (&dbp
) != NIS_SUCCESS
)
337 return NIS_NAMEUNREACHABLE
;
339 status
= __do_niscall3 (&dbp
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
341 __nisbind_destroy (&dbp
);
347 static directory_obj
*
348 rec_dirsearch (const_nis_name name
, directory_obj
*dir
, nis_error
*status
)
353 switch (nis_dir_cmp (name
, dir
->do_name
))
356 *status
= NIS_SUCCESS
;
359 /* NOT_SEQUENTIAL means, go one up and try it there ! */
361 { /* We need data from a parent domain */
363 char ndomain
[strlen (name
) + 3];
365 nis_domain_of_r (dir
->do_name
, ndomain
, sizeof (ndomain
));
367 /* The root server of our domain is a replica of the parent
368 domain ! (Now I understand why a root server must be a
369 replica of the parent domain) */
370 fd_res
= __nis_finddirectory (dir
, ndomain
);
371 *status
= fd_res
->status
;
372 if (fd_res
->status
!= NIS_SUCCESS
)
374 /* Try the current directory obj, maybe it works */
375 __free_fdresult (fd_res
);
378 obj
= calloc (1, sizeof (directory_obj
));
379 xdrmem_create (&xdrs
, fd_res
->dir_data
.dir_data_val
,
380 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
381 _xdr_directory_obj (&xdrs
, obj
);
383 __free_fdresult (fd_res
);
386 /* We have found a NIS+ server serving ndomain, now
387 let us search for "name" */
388 nis_free_directory (dir
);
389 return rec_dirsearch (name
, obj
, status
);
393 /* Ups, very bad. Are we already the root server ? */
394 nis_free_directory (dir
);
402 size_t namelen
= strlen (name
);
403 char leaf
[namelen
+ 3];
404 char domain
[namelen
+ 3];
405 char ndomain
[namelen
+ 3];
409 strcpy (domain
, name
);
413 if (domain
[0] == '\0')
415 nis_free_directory (dir
);
418 nis_leaf_of_r (domain
, leaf
, sizeof (leaf
));
419 nis_domain_of_r (domain
, ndomain
, sizeof (ndomain
));
420 strcpy (domain
, ndomain
);
423 while (nis_dir_cmp (domain
, dir
->do_name
) != SAME_NAME
);
427 /* We have found the directory above. Use it. */
431 cp
= rawmemchr (leaf
, '\0');
435 fd_res
= __nis_finddirectory (dir
, leaf
);
436 *status
= fd_res
->status
;
437 if (fd_res
->status
!= NIS_SUCCESS
)
439 /* Try the current directory object, maybe it works */
440 __free_fdresult (fd_res
);
443 obj
= calloc(1, sizeof(directory_obj
));
444 xdrmem_create(&xdrs
, fd_res
->dir_data
.dir_data_val
,
445 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
446 _xdr_directory_obj(&xdrs
, obj
);
448 __free_fdresult (fd_res
);
451 /* We have found a NIS+ server serving ndomain, now
452 let us search for "name" */
453 nis_free_directory (dir
);
454 return rec_dirsearch (name
, obj
, status
);
459 nis_free_directory (dir
);
460 *status
= NIS_BADNAME
;
463 nis_free_directory (dir
);
468 /* We try to query the current server for the searched object,
469 maybe he know about it ? */
470 static directory_obj
*
471 first_shoot (const_nis_name name
, directory_obj
*dir
)
473 directory_obj
*obj
= NULL
;
476 char domain
[strlen (name
) + 3];
478 if (nis_dir_cmp (name
, dir
->do_name
) == SAME_NAME
)
481 nis_domain_of_r (name
, domain
, sizeof (domain
));
483 if (nis_dir_cmp (domain
, dir
->do_name
) == SAME_NAME
)
486 fd_res
= __nis_finddirectory (dir
, domain
);
487 if (fd_res
->status
== NIS_SUCCESS
488 && (obj
= calloc (1, sizeof (directory_obj
))) != NULL
)
490 xdrmem_create(&xdrs
, fd_res
->dir_data
.dir_data_val
,
491 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
492 _xdr_directory_obj (&xdrs
, obj
);
496 __free_fdresult (fd_res
);
499 nis_free_directory (dir
);
505 __nisfind_server (const_nis_name name
, directory_obj
**dir
)
511 /* Search in local cache. In the moment, we ignore the fastest server */
512 if (!(flags
& NO_CACHE
))
513 dir
= __nis_cache_search (name
, flags
, &cinfo
);
521 *dir
= readColdStartFile ();
522 if (*dir
== NULL
) /* No /var/nis/NIS_COLD_START->no NIS+ installed */
525 /* Try at first, if servers in "dir" know our object */
526 obj
= first_shoot (name
, *dir
);
529 *dir
= rec_dirsearch (name
, *dir
, &status
);
541 __do_niscall (const_nis_name name
, u_long prog
, xdrproc_t xargs
,
542 caddr_t req
, xdrproc_t xres
, caddr_t resp
, unsigned int flags
,
547 directory_obj
*dir
= NULL
;
550 int saved_errno
= errno
;
552 retcode
= __nisfind_server (name
, &dir
);
553 if (retcode
!= NIS_SUCCESS
)
556 if (flags
& MASTER_ONLY
)
558 server
= dir
->do_servers
.do_servers_val
;
563 server
= dir
->do_servers
.do_servers_val
;
564 server_len
= dir
->do_servers
.do_servers_len
;
567 retcode
= __nisbind_create (&bptr
, server
, server_len
, flags
);
568 if (retcode
== NIS_SUCCESS
)
570 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
572 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
574 nis_free_directory (dir
);
575 return NIS_NAMEUNREACHABLE
;
578 retcode
= __do_niscall3 (&bptr
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
580 __nisbind_destroy (&bptr
);
583 nis_free_directory (dir
);
585 __set_errno (saved_errno
);