1 /* Copyright (C) 1997, 1998, 2001, 2004, 2005, 2006, 2007, 2008
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
26 #include <rpcsvc/nis.h>
27 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <bits/libc-lock.h>
35 #include "nis_intern.h"
38 static const struct timeval RPCTIMEOUT
= {10, 0};
39 static const struct timeval UDPTIMEOUT
= {5, 0};
41 extern u_short
__pmap_getnisport (struct sockaddr_in
*address
, u_long program
,
42 u_long version
, u_int protocol
);
45 inetstr2int (const char *str
)
48 for (size_t i
= 0; str
[i
] != '\0'; ++i
)
49 if (str
[i
] == '.' && __builtin_expect (++j
== 4, 0))
53 return inet_addr (memcpy (buffer
, str
, i
));
56 return inet_addr (str
);
60 __nisbind_destroy (dir_binding
*bind
)
62 if (bind
->clnt
!= NULL
)
65 auth_destroy (bind
->clnt
->cl_auth
);
66 clnt_destroy (bind
->clnt
);
69 libnsl_hidden_def (__nisbind_destroy
)
72 __nisbind_next (dir_binding
*bind
)
74 if (bind
->clnt
!= NULL
)
77 auth_destroy (bind
->clnt
->cl_auth
);
78 clnt_destroy (bind
->clnt
);
82 if (bind
->trys
>= bind
->server_len
)
85 for (u_int j
= bind
->current_ep
+ 1;
86 j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
87 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
89 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
97 if (bind
->server_used
>= bind
->server_len
)
98 bind
->server_used
= 0;
100 for (u_int j
= 0; j
< bind
->server_val
[bind
->server_used
].ep
.ep_len
; ++j
)
101 if (strcmp (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].family
,
103 if (bind
->server_val
[bind
->server_used
].ep
.ep_val
[j
].proto
[0] == '-')
105 bind
->current_ep
= j
;
111 libnsl_hidden_def (__nisbind_next
)
113 static struct ckey_cache_entry
115 struct in_addr inaddr
;
117 unsigned int protocol
;
120 static size_t ckey_cache_size
;
121 static size_t ckey_cache_allocated
;
122 static pid_t ckey_cache_pid
;
123 static uid_t ckey_cache_euid
;
124 __libc_lock_define_initialized (static, ckey_cache_lock
)
127 get_ckey (des_block
*ckey
, struct sockaddr_in
*addr
, unsigned int protocol
)
130 pid_t pid
= getpid ();
131 uid_t euid
= geteuid ();
134 __libc_lock_lock (ckey_cache_lock
);
136 if (ckey_cache_pid
!= pid
|| ckey_cache_euid
!= euid
)
139 ckey_cache_pid
= pid
;
140 ckey_cache_euid
= euid
;
143 for (i
= 0; i
< ckey_cache_size
; ++i
)
144 if (ckey_cache
[i
].port
== addr
->sin_port
145 && ckey_cache
[i
].protocol
== protocol
146 && memcmp (&ckey_cache
[i
].inaddr
, &addr
->sin_addr
,
147 sizeof (addr
->sin_addr
)) == 0)
149 *ckey
= ckey_cache
[i
].ckey
;
154 if (!ret
&& key_gendes (ckey
) >= 0)
157 /* Don't grow the cache indefinitely. */
158 if (ckey_cache_size
== 256)
160 if (ckey_cache_size
== ckey_cache_allocated
)
162 size_t size
= ckey_cache_allocated
? ckey_cache_allocated
* 2 : 16;
163 struct ckey_cache_entry
*new_cache
164 = realloc (ckey_cache
, size
* sizeof (*ckey_cache
));
165 if (new_cache
!= NULL
)
167 ckey_cache
= new_cache
;
168 ckey_cache_allocated
= size
;
171 ckey_cache
[ckey_cache_size
].inaddr
= addr
->sin_addr
;
172 ckey_cache
[ckey_cache_size
].port
= addr
->sin_port
;
173 ckey_cache
[ckey_cache_size
].protocol
= protocol
;
174 ckey_cache
[ckey_cache_size
++].ckey
= *ckey
;
177 __libc_lock_unlock (ckey_cache_lock
);
182 __nisbind_connect (dir_binding
*dbp
)
190 serv
= &dbp
->server_val
[dbp
->server_used
];
192 memset (&dbp
->addr
, '\0', sizeof (dbp
->addr
));
193 dbp
->addr
.sin_family
= AF_INET
;
195 dbp
->addr
.sin_addr
.s_addr
=
196 inetstr2int (serv
->ep
.ep_val
[dbp
->current_ep
].uaddr
);
198 if (dbp
->addr
.sin_addr
.s_addr
== INADDR_NONE
)
201 /* Check, if the host is online and rpc.nisd is running. Much faster
202 then the clnt*_create functions: */
203 port
= __pmap_getnisport (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
204 dbp
->use_udp
? IPPROTO_UDP
: IPPROTO_TCP
);
208 dbp
->addr
.sin_port
= htons (port
);
209 dbp
->socket
= RPC_ANYSOCK
;
211 dbp
->clnt
= clntudp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
212 UDPTIMEOUT
, &dbp
->socket
);
214 dbp
->clnt
= clnttcp_create (&dbp
->addr
, NIS_PROG
, NIS_VERSION
,
217 if (dbp
->clnt
== NULL
)
220 clnt_control (dbp
->clnt
, CLSET_TIMEOUT
, (caddr_t
) &RPCTIMEOUT
);
221 /* If the program exists, close the socket */
222 if (fcntl (dbp
->socket
, F_SETFD
, 1) == -1)
223 perror ("fcntl: F_SETFD");
227 if (serv
->key_type
== NIS_PK_DH
)
229 char netname
[MAXNETNAMELEN
+ 1];
233 p
= stpcpy (netname
, "unix@");
234 strncpy (p
, serv
->name
, MAXNETNAMELEN
- 5);
235 netname
[MAXNETNAMELEN
] = '\0';
236 dbp
->clnt
->cl_auth
= NULL
;
237 if (get_ckey (&ckey
, &dbp
->addr
,
238 dbp
->use_udp
? IPPROTO_UDP
: IPPROTO_TCP
))
240 authdes_pk_create (netname
, &serv
->pkey
, 300, NULL
, &ckey
);
241 if (!dbp
->clnt
->cl_auth
)
242 dbp
->clnt
->cl_auth
= authunix_create_default ();
245 dbp
->clnt
->cl_auth
= authunix_create_default ();
250 libnsl_hidden_def (__nisbind_connect
)
253 __nisbind_create (dir_binding
*dbp
, const nis_server
*serv_val
,
254 unsigned int serv_len
, unsigned int server_used
,
255 unsigned int current_ep
, unsigned int flags
)
259 dbp
->server_len
= serv_len
;
260 dbp
->server_val
= (nis_server
*)serv_val
;
262 if (flags
& USE_DGRAM
)
265 dbp
->use_udp
= FALSE
;
267 if (flags
& NO_AUTHINFO
)
268 dbp
->use_auth
= FALSE
;
270 dbp
->use_auth
= TRUE
;
272 if (flags
& MASTER_ONLY
)
273 dbp
->master_only
= TRUE
;
275 dbp
->master_only
= FALSE
;
277 /* We try the first server */
281 if (server_used
== ~0)
283 if (__nis_findfastest (dbp
) < 1)
284 return NIS_NAMEUNREACHABLE
;
288 dbp
->server_used
= server_used
;
289 dbp
->current_ep
= current_ep
;
294 libnsl_hidden_def (__nisbind_create
)
296 /* __nisbind_connect (dbp) must be run before calling this function !
297 So we could use the same binding twice */
299 __do_niscall3 (dir_binding
*dbp
, u_long prog
, xdrproc_t xargs
, caddr_t req
,
300 xdrproc_t xres
, caddr_t resp
, unsigned int flags
, nis_cb
*cb
)
302 enum clnt_stat result
;
306 return NIS_NAMEUNREACHABLE
;
311 result
= clnt_call (dbp
->clnt
, prog
, xargs
, req
, xres
, resp
, RPCTIMEOUT
);
313 if (result
!= RPC_SUCCESS
)
314 retcode
= NIS_RPCERROR
;
320 if ((((nis_result
*)resp
)->status
== NIS_CBRESULTS
) &&
323 __nis_do_callback (dbp
, &((nis_result
*) resp
)->cookie
, cb
);
326 /* Yes, the missing break is correct. If we doesn't have to
327 start a callback, look if we have to search another server */
337 if (((nis_result
*)resp
)->status
== NIS_SYSTEMERROR
338 || ((nis_result
*)resp
)->status
== NIS_NOSUCHNAME
339 || ((nis_result
*)resp
)->status
== NIS_NOT_ME
)
342 if (__nisbind_next (dbp
) == NIS_SUCCESS
)
344 while (__nisbind_connect (dbp
) != NIS_SUCCESS
)
346 if (__nisbind_next (dbp
) != NIS_SUCCESS
)
351 break; /* No more servers to search in */
355 case NIS_FINDDIRECTORY
:
356 if (((fd_result
*)resp
)->status
== NIS_SYSTEMERROR
357 || ((fd_result
*)resp
)->status
== NIS_NOSUCHNAME
358 || ((fd_result
*)resp
)->status
== NIS_NOT_ME
)
361 case NIS_DUMPLOG
: /* log_result */
363 if (((log_result
*)resp
)->lr_status
== NIS_SYSTEMERROR
364 || ((log_result
*)resp
)->lr_status
== NIS_NOSUCHNAME
365 || ((log_result
*)resp
)->lr_status
== NIS_NOT_ME
)
371 retcode
= NIS_SUCCESS
;
374 while ((flags
& HARD_LOOKUP
) && retcode
== NIS_RPCERROR
);
378 libnsl_hidden_def (__do_niscall3
)
382 __do_niscall2 (const nis_server
*server
, u_int server_len
, u_long prog
,
383 xdrproc_t xargs
, caddr_t req
, xdrproc_t xres
, caddr_t resp
,
384 unsigned int flags
, nis_cb
*cb
)
389 if (flags
& MASTER_ONLY
)
392 status
= __nisbind_create (&dbp
, server
, server_len
, ~0, ~0, flags
);
393 if (status
!= NIS_SUCCESS
)
396 while (__nisbind_connect (&dbp
) != NIS_SUCCESS
)
397 if (__nisbind_next (&dbp
) != NIS_SUCCESS
)
398 return NIS_NAMEUNREACHABLE
;
400 status
= __do_niscall3 (&dbp
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
402 __nisbind_destroy (&dbp
);
408 static directory_obj
*
409 rec_dirsearch (const_nis_name name
, directory_obj
*dir
, nis_error
*status
)
414 switch (nis_dir_cmp (name
, dir
->do_name
))
417 *status
= NIS_SUCCESS
;
420 /* NOT_SEQUENTIAL means, go one up and try it there ! */
422 { /* We need data from a parent domain */
424 const char *ndomain
= __nis_domain_of (dir
->do_name
);
426 /* The root server of our domain is a replica of the parent
427 domain ! (Now I understand why a root server must be a
428 replica of the parent domain) */
429 fd_res
= __nis_finddirectory (dir
, ndomain
);
432 nis_free_directory (dir
);
433 *status
= NIS_NOMEMORY
;
436 *status
= fd_res
->status
;
437 if (fd_res
->status
!= NIS_SUCCESS
)
439 /* Try the current directory obj, maybe it works */
440 __free_fdresult (fd_res
);
443 nis_free_directory (dir
);
444 obj
= calloc (1, sizeof (directory_obj
));
447 __free_fdresult (fd_res
);
448 *status
= NIS_NOMEMORY
;
451 xdrmem_create (&xdrs
, fd_res
->dir_data
.dir_data_val
,
452 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
453 _xdr_directory_obj (&xdrs
, obj
);
455 __free_fdresult (fd_res
);
457 /* We have found a NIS+ server serving ndomain, now
458 let us search for "name" */
459 return rec_dirsearch (name
, obj
, status
);
465 size_t namelen
= strlen (name
);
466 char leaf
[namelen
+ 3];
467 char domain
[namelen
+ 3];
471 strcpy (domain
, name
);
475 if (domain
[0] == '\0')
477 nis_free_directory (dir
);
480 nis_leaf_of_r (domain
, leaf
, sizeof (leaf
));
481 ndomain
= __nis_domain_of (domain
);
482 memmove (domain
, ndomain
, strlen (ndomain
) + 1);
484 while (nis_dir_cmp (domain
, dir
->do_name
) != SAME_NAME
);
486 cp
= rawmemchr (leaf
, '\0');
490 fd_res
= __nis_finddirectory (dir
, leaf
);
493 nis_free_directory (dir
);
494 *status
= NIS_NOMEMORY
;
497 *status
= fd_res
->status
;
498 if (fd_res
->status
!= NIS_SUCCESS
)
500 /* Try the current directory object, maybe it works */
501 __free_fdresult (fd_res
);
504 nis_free_directory (dir
);
505 obj
= calloc (1, sizeof(directory_obj
));
508 __free_fdresult (fd_res
);
509 *status
= NIS_NOMEMORY
;
512 xdrmem_create (&xdrs
, fd_res
->dir_data
.dir_data_val
,
513 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
514 _xdr_directory_obj (&xdrs
, obj
);
516 __free_fdresult (fd_res
);
517 /* We have found a NIS+ server serving ndomain, now
518 let us search for "name" */
519 return rec_dirsearch (name
, obj
, status
);
523 nis_free_directory (dir
);
524 *status
= NIS_BADNAME
;
527 nis_free_directory (dir
);
532 /* We try to query the current server for the searched object,
533 maybe he know about it ? */
534 static directory_obj
*
535 first_shoot (const_nis_name name
, directory_obj
*dir
)
537 directory_obj
*obj
= NULL
;
541 if (nis_dir_cmp (name
, dir
->do_name
) == SAME_NAME
)
544 fd_res
= __nis_finddirectory (dir
, name
);
547 if (fd_res
->status
== NIS_SUCCESS
548 && (obj
= calloc (1, sizeof (directory_obj
))) != NULL
)
550 xdrmem_create (&xdrs
, fd_res
->dir_data
.dir_data_val
,
551 fd_res
->dir_data
.dir_data_len
, XDR_DECODE
);
552 _xdr_directory_obj (&xdrs
, obj
);
555 if (strcmp (dir
->do_name
, obj
->do_name
) != 0)
557 nis_free_directory (obj
);
562 __free_fdresult (fd_res
);
565 nis_free_directory (dir
);
570 static struct nis_server_cache
575 unsigned int server_used
;
576 unsigned int current_ep
;
579 } *nis_server_cache
[16];
580 static time_t nis_cold_start_mtime
;
581 __libc_lock_define_initialized (static, nis_server_cache_lock
)
583 static directory_obj
*
584 nis_server_cache_search (const_nis_name name
, int search_parent
,
585 unsigned int *server_used
, unsigned int *current_ep
,
588 directory_obj
*ret
= NULL
;
594 int saved_errno
= errno
;
595 if (stat64 ("/var/nis/NIS_COLD_START", &st
) < 0)
596 st
.st_mtime
= nis_cold_start_mtime
+ 1;
597 __set_errno (saved_errno
);
599 __libc_lock_lock (nis_server_cache_lock
);
601 for (i
= 0; i
< 16; ++i
)
602 if (nis_server_cache
[i
] == NULL
)
604 else if (st
.st_mtime
!= nis_cold_start_mtime
605 || now
->tv_sec
> nis_server_cache
[i
]->expires
)
607 free (nis_server_cache
[i
]);
608 nis_server_cache
[i
] = NULL
;
610 else if (nis_server_cache
[i
]->search_parent
== search_parent
611 && strcmp (nis_server_cache
[i
]->name
, name
) == 0)
613 ret
= calloc (1, sizeof (directory_obj
));
617 addr
= rawmemchr (nis_server_cache
[i
]->name
, '\0') + 8;
618 addr
= (char *) ((uintptr_t) addr
& ~(uintptr_t) 7);
619 xdrmem_create (&xdrs
, addr
, nis_server_cache
[i
]->size
, XDR_DECODE
);
620 if (!_xdr_directory_obj (&xdrs
, ret
))
625 free (nis_server_cache
[i
]);
626 nis_server_cache
[i
] = NULL
;
630 *server_used
= nis_server_cache
[i
]->server_used
;
631 *current_ep
= nis_server_cache
[i
]->current_ep
;
635 nis_cold_start_mtime
= st
.st_mtime
;
637 __libc_lock_unlock (nis_server_cache_lock
);
642 nis_server_cache_add (const_nis_name name
, int search_parent
,
643 directory_obj
*dir
, unsigned int server_used
,
644 unsigned int current_ep
, struct timeval
*now
)
646 struct nis_server_cache
**loc
;
647 struct nis_server_cache
*new;
648 struct nis_server_cache
*old
;
657 size
= xdr_sizeof ((xdrproc_t
) _xdr_directory_obj
, (char *) dir
);
658 new = calloc (1, sizeof (*new) + strlen (name
) + 8 + size
);
661 new->search_parent
= search_parent
;
663 new->expires
= now
->tv_sec
+ dir
->do_ttl
;
665 new->server_used
= server_used
;
666 new->current_ep
= current_ep
;
667 addr
= stpcpy (new->name
, name
) + 8;
668 addr
= (char *) ((uintptr_t) addr
& ~(uintptr_t) 7);
670 xdrmem_create(&xdrs
, addr
, size
, XDR_ENCODE
);
671 if (!_xdr_directory_obj (&xdrs
, dir
))
679 __libc_lock_lock (nis_server_cache_lock
);
681 /* Choose which entry should be evicted from the cache. */
682 loc
= &nis_server_cache
[0];
684 for (i
= 1; i
< 16; ++i
)
685 if (nis_server_cache
[i
] == NULL
)
687 loc
= &nis_server_cache
[i
];
690 else if ((*loc
)->uses
> nis_server_cache
[i
]->uses
691 || ((*loc
)->uses
== nis_server_cache
[i
]->uses
692 && (*loc
)->expires
> nis_server_cache
[i
]->expires
))
693 loc
= &nis_server_cache
[i
];
697 __libc_lock_unlock (nis_server_cache_lock
);
702 __nisfind_server (const_nis_name name
, int search_parent
,
703 directory_obj
**dir
, dir_binding
*dbp
, unsigned int flags
)
705 nis_error result
= NIS_SUCCESS
;
709 unsigned int server_used
= ~0;
710 unsigned int current_ep
= ~0;
718 (void) gettimeofday (&now
, NULL
);
720 if ((flags
& NO_CACHE
) == 0)
721 *dir
= nis_server_cache_search (name
, search_parent
, &server_used
,
725 unsigned int server_len
= (*dir
)->do_servers
.do_servers_len
;
726 if (flags
& MASTER_ONLY
)
729 if (server_used
!= 0)
735 result
= __nisbind_create (dbp
, (*dir
)->do_servers
.do_servers_val
,
736 server_len
, server_used
, current_ep
, flags
);
737 if (result
!= NIS_SUCCESS
)
739 nis_free_directory (*dir
);
745 int saved_errno
= errno
;
746 *dir
= readColdStartFile ();
747 __set_errno (saved_errno
);
749 /* No /var/nis/NIS_COLD_START->no NIS+ installed. */
752 /* Try at first, if servers in "dir" know our object */
753 const char *search_name
= name
;
755 search_name
= __nis_domain_of (name
);
756 obj
= first_shoot (search_name
, *dir
);
759 obj
= rec_dirsearch (search_name
, *dir
, &status
);
764 if (result
== NIS_SUCCESS
)
766 unsigned int server_len
= obj
->do_servers
.do_servers_len
;
767 if (flags
& MASTER_ONLY
)
769 result
= __nisbind_create (dbp
, obj
->do_servers
.do_servers_val
,
770 server_len
, ~0, ~0, flags
);
771 if (result
== NIS_SUCCESS
)
773 if ((flags
& MASTER_ONLY
) == 0
774 || obj
->do_servers
.do_servers_len
== 1)
776 server_used
= dbp
->server_used
;
777 current_ep
= dbp
->current_ep
;
779 if ((flags
& NO_CACHE
) == 0)
780 nis_server_cache_add (name
, search_parent
, obj
,
781 server_used
, current_ep
, &now
);
785 nis_free_directory (obj
);
797 __prepare_niscall (const_nis_name name
, directory_obj
**dirp
,
798 dir_binding
*bptrp
, unsigned int flags
)
800 nis_error retcode
= __nisfind_server (name
, 1, dirp
, bptrp
, flags
);
801 if (__builtin_expect (retcode
!= NIS_SUCCESS
, 0))
805 if (__nisbind_connect (bptrp
) == NIS_SUCCESS
)
807 while (__nisbind_next (bptrp
) == NIS_SUCCESS
);
809 __nisbind_destroy (bptrp
);
810 memset (bptrp
, '\0', sizeof (*bptrp
));
812 retcode
= NIS_NAMEUNREACHABLE
;
813 nis_free_directory (*dirp
);
818 libnsl_hidden_def (__prepare_niscall
)
822 __do_niscall (const_nis_name name
, u_long prog
, xdrproc_t xargs
,
823 caddr_t req
, xdrproc_t xres
, caddr_t resp
, unsigned int flags
,
827 directory_obj
*dir
= NULL
;
828 int saved_errno
= errno
;
830 nis_error retcode
= __prepare_niscall (name
, &dir
, &bptr
, flags
);
831 if (retcode
== NIS_SUCCESS
)
833 retcode
= __do_niscall3 (&bptr
, prog
, xargs
, req
, xres
, resp
, flags
, cb
);
835 __nisbind_destroy (&bptr
);
837 nis_free_directory (dir
);
840 __set_errno (saved_errno
);