2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3 * Copyright (c) 1998 Bill Paul <wpaul@ctr.columbia.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $FreeBSD: src/lib/libc/yp/yplib.c,v 1.51 2007/07/24 13:06:08 simon Exp $
33 #include "namespace.h"
34 #include "reentrant.h"
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
40 #include <arpa/inet.h>
48 #include <rpcsvc/yp.h>
49 #include "un-namespace.h"
50 #include "libc_private.h"
52 bool_t
xdr_ypresp_all_seq(XDR
*, u_long
*);
54 int (*ypresp_allfn
)(unsigned long, char *, int, char *, int, void *);
57 * We have to define these here due to clashes between yp_prot.h and
69 struct ypmatch_ent
*ypc_next
;
71 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
72 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
76 struct dom_binding
*dom_pnext
;
77 char dom_domain
[YPMAXDOMAIN
+ 1];
78 struct sockaddr_in dom_server_addr
;
79 u_short dom_server_port
;
82 u_short dom_local_port
; /* now I finally know what this is for. */
85 struct ypmatch_ent
*cache
;
90 #include <rpcsvc/yp.h>
91 #include <rpcsvc/ypclnt.h>
94 #define BINDINGDIR "/var/yp/binding"
96 #define MAX_RETRIES 20
100 static void _yp_unbind(struct dom_binding
*);
101 struct dom_binding
*_ypbindlist
;
102 static char _yp_domain
[MAXHOSTNAMELEN
];
103 int _yplib_timeout
= 20;
105 static mutex_t _ypmutex
= MUTEX_INITIALIZER
;
106 #define YPLOCK() mutex_lock(&_ypmutex);
107 #define YPUNLOCK() mutex_unlock(&_ypmutex);
111 ypmatch_cache_delete(struct dom_binding
*ypdb
, struct ypmatch_ent
*prev
,
112 struct ypmatch_ent
*cur
)
115 ypdb
->cache
= cur
->ypc_next
;
117 prev
->ypc_next
= cur
->ypc_next
;
120 free(cur
->ypc_key
.keydat_val
);
121 free(cur
->ypc_val
.valdat_val
);
124 ypdb
->ypmatch_cachecnt
--;
130 ypmatch_cache_flush(struct dom_binding
*ypdb
)
132 struct ypmatch_ent
*n
, *c
= ypdb
->cache
;
136 ypmatch_cache_delete(ypdb
, NULL
, c
);
144 ypmatch_cache_expire(struct dom_binding
*ypdb
)
146 struct ypmatch_ent
*c
= ypdb
->cache
;
147 struct ypmatch_ent
*n
, *p
= NULL
;
153 if (t
>= c
->ypc_expire_t
) {
155 ypmatch_cache_delete(ypdb
, p
, c
);
167 ypmatch_cache_insert(struct dom_binding
*ypdb
, char *map
, keydat
*key
,
170 struct ypmatch_ent
*new;
172 /* Do an expire run to maybe open up a slot. */
173 if (ypdb
->ypmatch_cachecnt
)
174 ypmatch_cache_expire(ypdb
);
177 * If there are no slots free, then force an expire of
178 * the least recently used entry.
180 if (ypdb
->ypmatch_cachecnt
>= YPLIB_MAXCACHE
) {
181 struct ypmatch_ent
*o
= NULL
, *c
= ypdb
->cache
;
187 if (c
->ypc_expire_t
< oldest
) {
188 oldest
= c
->ypc_expire_t
;
197 ypmatch_cache_expire(ypdb
);
200 new = malloc(sizeof(struct ypmatch_ent
));
204 new->ypc_map
= strdup(map
);
205 if (new->ypc_map
== NULL
) {
209 new->ypc_key
.keydat_val
= malloc(key
->keydat_len
);
210 if (new->ypc_key
.keydat_val
== NULL
) {
215 new->ypc_val
.valdat_val
= malloc(val
->valdat_len
);
216 if (new->ypc_val
.valdat_val
== NULL
) {
217 free(new->ypc_val
.valdat_val
);
223 new->ypc_expire_t
= time(NULL
) + YPLIB_EXPIRE
;
224 new->ypc_key
.keydat_len
= key
->keydat_len
;
225 new->ypc_val
.valdat_len
= val
->valdat_len
;
226 bcopy(key
->keydat_val
, new->ypc_key
.keydat_val
, key
->keydat_len
);
227 bcopy(val
->valdat_val
, new->ypc_val
.valdat_val
, val
->valdat_len
);
229 new->ypc_next
= ypdb
->cache
;
232 ypdb
->ypmatch_cachecnt
++;
238 ypmatch_cache_lookup(struct dom_binding
*ypdb
, char *map
, keydat
*key
,
241 struct ypmatch_ent
*c
= ypdb
->cache
;
243 ypmatch_cache_expire(ypdb
);
245 for (c
= ypdb
->cache
; c
!= NULL
; c
= c
->ypc_next
) {
246 if (strcmp(map
, c
->ypc_map
))
248 if (key
->keydat_len
!= c
->ypc_key
.keydat_len
)
250 if (bcmp(key
->keydat_val
, c
->ypc_key
.keydat_val
,
258 val
->valdat_len
= c
->ypc_val
.valdat_len
;
259 val
->valdat_val
= c
->ypc_val
.valdat_val
;
266 ypbinderr_string(int incode
)
275 errstr
= "Internal ypbind error";
277 case YPBIND_ERR_NOSERV
:
278 errstr
= "Domain not bound";
280 case YPBIND_ERR_RESC
:
281 errstr
= "System resource allocation failure";
288 strlcpy(err
, errstr
, sizeof(err
));
290 snprintf(err
, sizeof(err
), "Unknown ypbind error: #%d\n", incode
);
295 _yp_dobind(const char *dom
, struct dom_binding
**ypdb
)
297 static pid_t pid
= -1;
298 char path
[MAXPATHLEN
];
299 struct dom_binding
*ysd
, *ysd2
;
300 struct ypbind_resp ypbr
;
302 struct sockaddr_in clnt_sin
;
309 struct sockaddr_in check
;
310 socklen_t checklen
= sizeof(struct sockaddr_in
);
312 /* Not allowed; bad doggie. Bad. */
313 if (strchr(dom
, '/') != NULL
)
314 return(YPERR_BADARGS
);
317 if (!(pid
== -1 || pid
== gpid
)) {
320 if (ysd
->dom_client
!= NULL
)
322 ysd2
= ysd
->dom_pnext
;
333 if (dom
== NULL
|| strlen(dom
) == 0)
334 return (YPERR_BADARGS
);
336 for (ysd
= _ypbindlist
; ysd
; ysd
= ysd
->dom_pnext
)
337 if (strcmp(dom
, ysd
->dom_domain
) == 0)
342 ysd
= (struct dom_binding
*)malloc(sizeof *ysd
);
343 bzero((char *)ysd
, sizeof *ysd
);
344 ysd
->dom_socket
= -1;
348 /* Check the socket -- may have been hosed by the caller. */
349 if (_getsockname(ysd
->dom_socket
, (struct sockaddr
*)&check
,
350 &checklen
) == -1 || check
.sin_family
!= AF_INET
||
351 check
.sin_port
!= ysd
->dom_local_port
) {
352 /* Socket became bogus somehow... need to rebind. */
355 sock
= ysd
->dom_socket
;
356 save
= _dup(ysd
->dom_socket
);
357 if (ysd
->dom_client
!= NULL
)
358 clnt_destroy(ysd
->dom_client
);
360 ysd
->dom_client
= NULL
;
361 sock
= _dup2(save
, sock
);
368 if (retries
> MAX_RETRIES
) {
371 return(YPERR_YPBIND
);
374 if (ysd
->dom_vers
== 0) {
376 * We're trying to make a new binding: zorch the
377 * existing handle now (if any).
379 if (ysd
->dom_client
!= NULL
) {
380 clnt_destroy(ysd
->dom_client
);
381 ysd
->dom_client
= NULL
;
382 ysd
->dom_socket
= -1;
384 snprintf(path
, sizeof(path
), "%s/%s.%d", BINDINGDIR
, dom
, 2);
385 if ((fd
= _open(path
, O_RDONLY
| O_CLOEXEC
)) == -1) {
386 /* no binding file, YP is dead. */
387 /* Try to bring it back to life. */
391 if (_flock(fd
, LOCK_EX
|LOCK_NB
) == -1 && errno
== EWOULDBLOCK
) {
393 struct ypbind_resp ybr
;
396 iov
[0].iov_base
= (caddr_t
)&ypb_port
;
397 iov
[0].iov_len
= sizeof ypb_port
;
398 iov
[1].iov_base
= (caddr_t
)&ybr
;
399 iov
[1].iov_len
= sizeof ybr
;
401 r
= _readv(fd
, iov
, 2);
402 if (r
!= (ssize_t
)(iov
[0].iov_len
+ iov
[1].iov_len
)) {
408 bzero(&ysd
->dom_server_addr
, sizeof ysd
->dom_server_addr
);
409 ysd
->dom_server_addr
.sin_family
= AF_INET
;
410 ysd
->dom_server_addr
.sin_len
= sizeof(struct sockaddr_in
);
411 bcopy(&ybr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_addr
,
412 &ysd
->dom_server_addr
.sin_addr
.s_addr
,
413 sizeof(ysd
->dom_server_addr
.sin_addr
.s_addr
));
414 bcopy(&ybr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
,
415 &ysd
->dom_server_addr
.sin_port
,
416 sizeof(ysd
->dom_server_addr
.sin_port
));
418 ysd
->dom_server_port
= ysd
->dom_server_addr
.sin_port
;
422 /* no lock on binding file, YP is dead. */
423 /* Try to bring it back to life. */
430 if (ysd
->dom_vers
== -1 || ysd
->dom_vers
== 0) {
432 * We're trying to make a new binding: zorch the
433 * existing handle now (if any).
435 if (ysd
->dom_client
!= NULL
) {
436 clnt_destroy(ysd
->dom_client
);
437 ysd
->dom_client
= NULL
;
438 ysd
->dom_socket
= -1;
440 bzero((char *)&clnt_sin
, sizeof clnt_sin
);
441 clnt_sin
.sin_family
= AF_INET
;
442 clnt_sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
444 clnt_sock
= RPC_ANYSOCK
;
445 client
= clnttcp_create(&clnt_sin
, YPBINDPROG
, YPBINDVERS
, &clnt_sock
,
447 if (client
== NULL
) {
449 * These conditions indicate ypbind just isn't
450 * alive -- we probably don't want to shoot our
451 * mouth off in this case; instead generate error
452 * messages only for really exotic problems.
454 if (rpc_createerr
.cf_stat
!= RPC_PROGNOTREGISTERED
&&
455 (rpc_createerr
.cf_stat
!= RPC_SYSTEMERROR
&&
456 rpc_createerr
.cf_error
.re_errno
== ECONNREFUSED
))
457 clnt_pcreateerror("clnttcp_create");
460 return (YPERR_YPBIND
);
464 * Check the port number -- should be < IPPORT_RESERVED.
465 * If not, it's possible someone has registered a bogus
466 * ypbind with the portmapper and is trying to trick us.
468 if (ntohs(clnt_sin
.sin_port
) >= IPPORT_RESERVED
) {
470 clnt_destroy(client
);
473 return(YPERR_YPBIND
);
475 tv
.tv_sec
= _yplib_timeout
/2;
477 r
= clnt_call(client
, YPBINDPROC_DOMAIN
,
478 (xdrproc_t
)xdr_domainname
, &dom
,
479 (xdrproc_t
)xdr_ypbind_resp
, &ypbr
, tv
);
480 if (r
!= RPC_SUCCESS
) {
481 clnt_destroy(client
);
483 if (r
== RPC_PROGUNAVAIL
|| r
== RPC_PROCUNAVAIL
) {
486 return(YPERR_YPBIND
);
489 "YP: server for domain %s not responding, retrying\n", dom
);
492 if (ypbr
.ypbind_status
!= YPBIND_SUCC_VAL
) {
493 struct timespec time_to_sleep
, time_remaining
;
495 clnt_destroy(client
);
498 time_to_sleep
.tv_sec
= _yplib_timeout
/2;
499 time_to_sleep
.tv_nsec
= 0;
500 _nanosleep(&time_to_sleep
,
505 clnt_destroy(client
);
507 bzero((char *)&ysd
->dom_server_addr
, sizeof ysd
->dom_server_addr
);
508 ysd
->dom_server_addr
.sin_family
= AF_INET
;
509 bcopy(&ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
,
510 &ysd
->dom_server_addr
.sin_port
,
511 sizeof(ysd
->dom_server_addr
.sin_port
));
512 bcopy(&ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_addr
,
513 &ysd
->dom_server_addr
.sin_addr
.s_addr
,
514 sizeof(ysd
->dom_server_addr
.sin_addr
.s_addr
));
517 * We could do a reserved port check here too, but this
518 * could pose compatibility problems. The local ypbind is
519 * supposed to decide whether or not to trust yp servers
520 * on insecure ports. For now, we trust its judgement.
522 ysd
->dom_server_port
=
523 *(u_short
*)&ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
;
525 ysd
->dom_vers
= YPVERS
;
526 strlcpy(ysd
->dom_domain
, dom
, sizeof(ysd
->dom_domain
));
529 /* Don't rebuild the connection to the server unless we have to. */
530 if (ysd
->dom_client
== NULL
) {
531 tv
.tv_sec
= _yplib_timeout
/2;
533 ysd
->dom_socket
= RPC_ANYSOCK
;
534 ysd
->dom_client
= clntudp_bufcreate(&ysd
->dom_server_addr
,
535 YPPROG
, YPVERS
, tv
, &ysd
->dom_socket
, 1280, 2304);
536 if (ysd
->dom_client
== NULL
) {
537 clnt_pcreateerror("clntudp_create");
541 if (_fcntl(ysd
->dom_socket
, F_SETFD
, 1) == -1)
542 perror("fcntl: F_SETFD");
544 * We want a port number associated with this socket
545 * so that we can check its authenticity later.
547 checklen
= sizeof(struct sockaddr_in
);
548 bzero((char *)&check
, checklen
);
549 _bind(ysd
->dom_socket
, (struct sockaddr
*)&check
, checklen
);
550 check
.sin_family
= AF_INET
;
551 if (!_getsockname(ysd
->dom_socket
,
552 (struct sockaddr
*)&check
, &checklen
)) {
553 ysd
->dom_local_port
= check
.sin_port
;
555 clnt_destroy(ysd
->dom_client
);
558 return(YPERR_YPBIND
);
563 ysd
->dom_pnext
= _ypbindlist
;
568 * Set low retry timeout to realistically handle UDP packet
569 * loss for YP packet bursts.
573 clnt_control(ysd
->dom_client
, CLSET_RETRY_TIMEOUT
, (char*)&tv
);
581 _yp_unbind(struct dom_binding
*ypb
)
583 struct sockaddr_in check
;
584 socklen_t checklen
= sizeof(struct sockaddr_in
);
586 if (ypb
->dom_client
!= NULL
) {
587 /* Check the socket -- may have been hosed by the caller. */
588 if (_getsockname(ypb
->dom_socket
, (struct sockaddr
*)&check
,
589 &checklen
) == -1 || check
.sin_family
!= AF_INET
||
590 check
.sin_port
!= ypb
->dom_local_port
) {
593 sock
= ypb
->dom_socket
;
594 save
= _dup(ypb
->dom_socket
);
595 clnt_destroy(ypb
->dom_client
);
596 sock
= _dup2(save
, sock
);
599 clnt_destroy(ypb
->dom_client
);
602 ypb
->dom_client
= NULL
;
603 ypb
->dom_socket
= -1;
606 ypmatch_cache_flush(ypb
);
611 yp_bind_locked(char *dom
)
613 return (_yp_dobind(dom
, NULL
));
622 r
= yp_bind_locked(dom
);
628 yp_unbind_locked(char *dom
)
630 struct dom_binding
*ypb
, *ypbp
;
633 for (ypb
= _ypbindlist
; ypb
; ypb
= ypb
->dom_pnext
) {
634 if (strcmp(dom
, ypb
->dom_domain
) == 0) {
637 ypbp
->dom_pnext
= ypb
->dom_pnext
;
639 _ypbindlist
= ypb
->dom_pnext
;
652 yp_unbind_locked(dom
);
657 yp_match(char *indomain
, char *inmap
, const char *inkey
, int inkeylen
,
658 char **outval
, int *outvallen
)
660 struct dom_binding
*ysd
;
661 struct ypresp_val yprv
;
663 struct ypreq_key yprk
;
671 if (inkey
== NULL
|| !strlen(inkey
) || inkeylen
<= 0 ||
672 inmap
== NULL
|| !strlen(inmap
) ||
673 indomain
== NULL
|| !strlen(indomain
))
674 return (YPERR_BADARGS
);
677 if (_yp_dobind(indomain
, &ysd
) != 0) {
679 return(YPERR_DOMAIN
);
682 yprk
.domain
= indomain
;
684 yprk
.key
.keydat_val
= (char *)inkey
;
685 yprk
.key
.keydat_len
= inkeylen
;
688 if (ypmatch_cache_lookup(ysd
, yprk
.map
, &yprk
.key
, &yprv
.val
) == TRUE
) {
690 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
691 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
693 *outvallen
= yprv
.val
.valdat_len
;
694 *outval
= (char *)malloc(*outvallen
+1);
695 bcopy(yprv
.val
.valdat_val
, *outval
, *outvallen
);
696 (*outval
)[*outvallen
] = '\0';
703 if (_yp_dobind(indomain
, &ysd
) != 0) {
705 return (YPERR_DOMAIN
);
708 tv
.tv_sec
= _yplib_timeout
;
711 bzero((char *)&yprv
, sizeof yprv
);
713 r
= clnt_call(ysd
->dom_client
, YPPROC_MATCH
,
714 (xdrproc_t
)xdr_ypreq_key
, &yprk
,
715 (xdrproc_t
)xdr_ypresp_val
, &yprv
, tv
);
716 if (r
!= RPC_SUCCESS
) {
717 clnt_perror(ysd
->dom_client
, "yp_match: clnt_call");
722 if (!(r
= ypprot_err(yprv
.stat
))) {
723 *outvallen
= yprv
.val
.valdat_len
;
724 *outval
= (char *)malloc(*outvallen
+1);
725 bcopy(yprv
.val
.valdat_val
, *outval
, *outvallen
);
726 (*outval
)[*outvallen
] = '\0';
728 ypmatch_cache_insert(ysd
, yprk
.map
, &yprk
.key
, &yprv
.val
);
732 xdr_free((xdrproc_t
)xdr_ypresp_val
, &yprv
);
738 yp_get_default_domain_locked(char **domp
)
741 if (_yp_domain
[0] == '\0')
742 if (getdomainname(_yp_domain
, sizeof _yp_domain
))
743 return (YPERR_NODOM
);
749 yp_get_default_domain(char **domp
)
754 r
= yp_get_default_domain_locked(domp
);
760 yp_first(char *indomain
, char *inmap
, char **outkey
, int *outkeylen
,
761 char **outval
, int *outvallen
)
763 struct ypresp_key_val yprkv
;
764 struct ypreq_nokey yprnk
;
765 struct dom_binding
*ysd
;
771 if (indomain
== NULL
|| !strlen(indomain
) ||
772 inmap
== NULL
|| !strlen(inmap
))
773 return (YPERR_BADARGS
);
775 *outkey
= *outval
= NULL
;
776 *outkeylen
= *outvallen
= 0;
780 if (_yp_dobind(indomain
, &ysd
) != 0) {
782 return (YPERR_DOMAIN
);
785 tv
.tv_sec
= _yplib_timeout
;
788 yprnk
.domain
= indomain
;
790 bzero((char *)&yprkv
, sizeof yprkv
);
792 r
= clnt_call(ysd
->dom_client
, YPPROC_FIRST
,
793 (xdrproc_t
)xdr_ypreq_nokey
, &yprnk
,
794 (xdrproc_t
)xdr_ypresp_key_val
, &yprkv
, tv
);
795 if (r
!= RPC_SUCCESS
) {
796 clnt_perror(ysd
->dom_client
, "yp_first: clnt_call");
800 if (!(r
= ypprot_err(yprkv
.stat
))) {
801 *outkeylen
= yprkv
.key
.keydat_len
;
802 *outkey
= (char *)malloc(*outkeylen
+1);
803 bcopy(yprkv
.key
.keydat_val
, *outkey
, *outkeylen
);
804 (*outkey
)[*outkeylen
] = '\0';
805 *outvallen
= yprkv
.val
.valdat_len
;
806 *outval
= (char *)malloc(*outvallen
+1);
807 bcopy(yprkv
.val
.valdat_val
, *outval
, *outvallen
);
808 (*outval
)[*outvallen
] = '\0';
811 xdr_free((xdrproc_t
)xdr_ypresp_key_val
, &yprkv
);
817 yp_next(char *indomain
, char *inmap
, char *inkey
, int inkeylen
,
818 char **outkey
, int *outkeylen
, char **outval
, int *outvallen
)
820 struct ypresp_key_val yprkv
;
821 struct ypreq_key yprk
;
822 struct dom_binding
*ysd
;
828 if (inkey
== NULL
|| !strlen(inkey
) || inkeylen
<= 0 ||
829 inmap
== NULL
|| !strlen(inmap
) ||
830 indomain
== NULL
|| !strlen(indomain
))
831 return (YPERR_BADARGS
);
833 *outkey
= *outval
= NULL
;
834 *outkeylen
= *outvallen
= 0;
838 if (_yp_dobind(indomain
, &ysd
) != 0) {
840 return (YPERR_DOMAIN
);
843 tv
.tv_sec
= _yplib_timeout
;
846 yprk
.domain
= indomain
;
848 yprk
.key
.keydat_val
= inkey
;
849 yprk
.key
.keydat_len
= inkeylen
;
850 bzero((char *)&yprkv
, sizeof yprkv
);
852 r
= clnt_call(ysd
->dom_client
, YPPROC_NEXT
,
853 (xdrproc_t
)xdr_ypreq_key
, &yprk
,
854 (xdrproc_t
)xdr_ypresp_key_val
, &yprkv
, tv
);
855 if (r
!= RPC_SUCCESS
) {
856 clnt_perror(ysd
->dom_client
, "yp_next: clnt_call");
860 if (!(r
= ypprot_err(yprkv
.stat
))) {
861 *outkeylen
= yprkv
.key
.keydat_len
;
862 *outkey
= (char *)malloc(*outkeylen
+1);
863 bcopy(yprkv
.key
.keydat_val
, *outkey
, *outkeylen
);
864 (*outkey
)[*outkeylen
] = '\0';
865 *outvallen
= yprkv
.val
.valdat_len
;
866 *outval
= (char *)malloc(*outvallen
+1);
867 bcopy(yprkv
.val
.valdat_val
, *outval
, *outvallen
);
868 (*outval
)[*outvallen
] = '\0';
871 xdr_free((xdrproc_t
)xdr_ypresp_key_val
, &yprkv
);
877 yp_all(char *indomain
, char *inmap
, struct ypall_callback
*incallback
)
879 struct ypreq_nokey yprnk
;
880 struct dom_binding
*ysd
;
882 struct sockaddr_in clnt_sin
;
884 u_long status
, savstat
;
889 if (indomain
== NULL
|| !strlen(indomain
) ||
890 inmap
== NULL
|| !strlen(inmap
))
891 return (YPERR_BADARGS
);
896 if (_yp_dobind(indomain
, &ysd
) != 0) {
898 return (YPERR_DOMAIN
);
901 tv
.tv_sec
= _yplib_timeout
;
904 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
906 clnt_sock
= RPC_ANYSOCK
;
907 clnt_sin
= ysd
->dom_server_addr
;
908 clnt_sin
.sin_port
= 0;
909 clnt
= clnttcp_create(&clnt_sin
, YPPROG
, YPVERS
, &clnt_sock
, 0, 0);
912 printf("clnttcp_create failed\n");
916 yprnk
.domain
= indomain
;
918 ypresp_allfn
= incallback
->foreach
;
919 ypresp_data
= (void *)incallback
->data
;
921 if (clnt_call(clnt
, YPPROC_ALL
,
922 (xdrproc_t
)xdr_ypreq_nokey
, &yprnk
,
923 (xdrproc_t
)xdr_ypresp_all_seq
, &status
, tv
) != RPC_SUCCESS
) {
924 clnt_perror(ysd
->dom_client
, "yp_all: clnt_call");
932 xdr_free((xdrproc_t
)xdr_ypresp_all_seq
, &status
); /* not really needed... */
934 if (savstat
!= YP_NOMORE
)
935 return (ypprot_err(savstat
));
940 yp_order(char *indomain
, char *inmap
, int *outorder
)
942 struct dom_binding
*ysd
;
943 struct ypresp_order ypro
;
944 struct ypreq_nokey yprnk
;
950 if (indomain
== NULL
|| !strlen(indomain
) ||
951 inmap
== NULL
|| !strlen(inmap
))
952 return (YPERR_BADARGS
);
956 if (_yp_dobind(indomain
, &ysd
) != 0) {
958 return (YPERR_DOMAIN
);
961 tv
.tv_sec
= _yplib_timeout
;
964 yprnk
.domain
= indomain
;
967 bzero((char *)(char *)&ypro
, sizeof ypro
);
969 r
= clnt_call(ysd
->dom_client
, YPPROC_ORDER
,
970 (xdrproc_t
)xdr_ypreq_nokey
, &yprnk
,
971 (xdrproc_t
)xdr_ypresp_order
, &ypro
, tv
);
974 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
977 if (r
== RPC_PROCUNAVAIL
) {
982 if (r
!= RPC_SUCCESS
) {
983 clnt_perror(ysd
->dom_client
, "yp_order: clnt_call");
988 if (!(r
= ypprot_err(ypro
.stat
))) {
989 *outorder
= ypro
.ordernum
;
992 xdr_free((xdrproc_t
)xdr_ypresp_order
, &ypro
);
998 yp_master(char *indomain
, char *inmap
, char **outname
)
1000 struct dom_binding
*ysd
;
1001 struct ypresp_master yprm
;
1002 struct ypreq_nokey yprnk
;
1008 if (indomain
== NULL
|| !strlen(indomain
) ||
1009 inmap
== NULL
|| !strlen(inmap
))
1010 return (YPERR_BADARGS
);
1013 if (_yp_dobind(indomain
, &ysd
) != 0) {
1015 return (YPERR_DOMAIN
);
1018 tv
.tv_sec
= _yplib_timeout
;
1021 yprnk
.domain
= indomain
;
1024 bzero((char *)&yprm
, sizeof yprm
);
1026 r
= clnt_call(ysd
->dom_client
, YPPROC_MASTER
,
1027 (xdrproc_t
)xdr_ypreq_nokey
, &yprnk
,
1028 (xdrproc_t
)xdr_ypresp_master
, &yprm
, tv
);
1029 if (r
!= RPC_SUCCESS
) {
1030 clnt_perror(ysd
->dom_client
, "yp_master: clnt_call");
1035 if (!(r
= ypprot_err(yprm
.stat
))) {
1036 *outname
= (char *)strdup(yprm
.peer
);
1039 xdr_free((xdrproc_t
)xdr_ypresp_master
, &yprm
);
1045 yp_maplist(char *indomain
, struct ypmaplist
**outmaplist
)
1047 struct dom_binding
*ysd
;
1048 struct ypresp_maplist ypml
;
1054 if (indomain
== NULL
|| !strlen(indomain
))
1055 return (YPERR_BADARGS
);
1059 if (_yp_dobind(indomain
, &ysd
) != 0) {
1061 return (YPERR_DOMAIN
);
1064 tv
.tv_sec
= _yplib_timeout
;
1067 bzero((char *)&ypml
, sizeof ypml
);
1069 r
= clnt_call(ysd
->dom_client
, YPPROC_MAPLIST
,
1070 (xdrproc_t
)xdr_domainname
, &indomain
,
1071 (xdrproc_t
)xdr_ypresp_maplist
, &ypml
, tv
);
1072 if (r
!= RPC_SUCCESS
) {
1073 clnt_perror(ysd
->dom_client
, "yp_maplist: clnt_call");
1077 if (!(r
= ypprot_err(ypml
.stat
))) {
1078 *outmaplist
= ypml
.maps
;
1081 /* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1087 yperr_string(int incode
)
1090 static char err
[80];
1097 errstr
= "Request arguments bad";
1100 errstr
= "RPC failure";
1103 errstr
= "Can't bind to server which serves this domain";
1106 errstr
= "No such map in server's domain";
1109 errstr
= "No such key in map";
1112 errstr
= "YP server error";
1115 errstr
= "Local resource allocation failure";
1118 errstr
= "No more records in map database";
1121 errstr
= "Can't communicate with portmapper";
1124 errstr
= "Can't communicate with ypbind";
1127 errstr
= "Can't communicate with ypserv";
1130 errstr
= "Local domain name not set";
1133 errstr
= "Server data base is bad";
1136 errstr
= "YP server version mismatch - server can't supply service.";
1139 errstr
= "Access violation";
1142 errstr
= "Database is busy";
1149 strlcpy(err
, errstr
, sizeof(err
));
1151 snprintf(err
, sizeof(err
), "YP unknown error %d\n", incode
);
1156 ypprot_err(unsigned int incode
)
1162 return (YPERR_YPBIND
);
1164 return (YPERR_NOMORE
);
1168 return (YPERR_DOMAIN
);
1172 return (YPERR_YPERR
);
1174 return (YPERR_BADDB
);
1176 return (YPERR_YPERR
);
1178 return (YPERR_BADARGS
);
1180 return (YPERR_VERS
);
1182 return (YPERR_YPERR
);
1186 _yp_check(char **dom
)
1191 if (_yp_domain
[0]=='\0')
1192 if (yp_get_default_domain_locked(&unused
)) {
1200 if (yp_bind_locked(_yp_domain
) == 0) {
1201 yp_unbind_locked(_yp_domain
);