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 $
31 * $DragonFly: src/lib/libc/yp/yplib.c,v 1.9 2006/08/03 16:40:46 swildner Exp $
34 #include "namespace.h"
35 #include "reentrant.h"
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <arpa/inet.h>
49 #include <rpcsvc/yp.h>
50 #include "un-namespace.h"
51 #include "libc_private.h"
53 bool_t
xdr_ypresp_all_seq(XDR
*, u_long
*);
54 int _yp_check(char **);
56 int (*ypresp_allfn
)(unsigned long, char *, int, char *, int, void *);
59 * We have to define these here due to clashes between yp_prot.h and
71 struct ypmatch_ent
*ypc_next
;
73 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
74 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
78 struct dom_binding
*dom_pnext
;
79 char dom_domain
[YPMAXDOMAIN
+ 1];
80 struct sockaddr_in dom_server_addr
;
81 u_short dom_server_port
;
84 u_short dom_local_port
; /* now I finally know what this is for. */
87 struct ypmatch_ent
*cache
;
92 #include <rpcsvc/yp.h>
93 #include <rpcsvc/ypclnt.h>
96 #define BINDINGDIR "/var/yp/binding"
98 #define MAX_RETRIES 20
102 static void _yp_unbind(struct dom_binding
*);
103 struct dom_binding
*_ypbindlist
;
104 static char _yp_domain
[MAXHOSTNAMELEN
];
105 int _yplib_timeout
= 20;
107 static mutex_t _ypmutex
= MUTEX_INITIALIZER
;
108 #define YPLOCK() mutex_lock(&_ypmutex);
109 #define YPUNLOCK() mutex_unlock(&_ypmutex);
113 ypmatch_cache_delete(struct dom_binding
*ypdb
, struct ypmatch_ent
*prev
,
114 struct ypmatch_ent
*cur
)
117 ypdb
->cache
= cur
->ypc_next
;
119 prev
->ypc_next
= cur
->ypc_next
;
122 free(cur
->ypc_key
.keydat_val
);
123 free(cur
->ypc_val
.valdat_val
);
126 ypdb
->ypmatch_cachecnt
--;
132 ypmatch_cache_flush(struct dom_binding
*ypdb
)
134 struct ypmatch_ent
*n
, *c
= ypdb
->cache
;
138 ypmatch_cache_delete(ypdb
, NULL
, c
);
146 ypmatch_cache_expire(struct dom_binding
*ypdb
)
148 struct ypmatch_ent
*c
= ypdb
->cache
;
149 struct ypmatch_ent
*n
, *p
= NULL
;
155 if (t
>= c
->ypc_expire_t
) {
157 ypmatch_cache_delete(ypdb
, p
, c
);
169 ypmatch_cache_insert(struct dom_binding
*ypdb
, char *map
, keydat
*key
,
172 struct ypmatch_ent
*new;
174 /* Do an expire run to maybe open up a slot. */
175 if (ypdb
->ypmatch_cachecnt
)
176 ypmatch_cache_expire(ypdb
);
179 * If there are no slots free, then force an expire of
180 * the least recently used entry.
182 if (ypdb
->ypmatch_cachecnt
>= YPLIB_MAXCACHE
) {
183 struct ypmatch_ent
*o
= NULL
, *c
= ypdb
->cache
;
189 if (c
->ypc_expire_t
< oldest
) {
190 oldest
= c
->ypc_expire_t
;
199 ypmatch_cache_expire(ypdb
);
202 new = malloc(sizeof(struct ypmatch_ent
));
206 new->ypc_map
= strdup(map
);
207 if (new->ypc_map
== NULL
) {
211 new->ypc_key
.keydat_val
= malloc(key
->keydat_len
);
212 if (new->ypc_key
.keydat_val
== NULL
) {
217 new->ypc_val
.valdat_val
= malloc(val
->valdat_len
);
218 if (new->ypc_val
.valdat_val
== NULL
) {
219 free(new->ypc_val
.valdat_val
);
225 new->ypc_expire_t
= time(NULL
) + YPLIB_EXPIRE
;
226 new->ypc_key
.keydat_len
= key
->keydat_len
;
227 new->ypc_val
.valdat_len
= val
->valdat_len
;
228 bcopy(key
->keydat_val
, new->ypc_key
.keydat_val
, key
->keydat_len
);
229 bcopy(val
->valdat_val
, new->ypc_val
.valdat_val
, val
->valdat_len
);
231 new->ypc_next
= ypdb
->cache
;
234 ypdb
->ypmatch_cachecnt
++;
240 ypmatch_cache_lookup(struct dom_binding
*ypdb
, char *map
, keydat
*key
,
243 struct ypmatch_ent
*c
= ypdb
->cache
;
245 ypmatch_cache_expire(ypdb
);
247 for (c
= ypdb
->cache
; c
!= NULL
; c
= c
->ypc_next
) {
248 if (strcmp(map
, c
->ypc_map
))
250 if (key
->keydat_len
!= c
->ypc_key
.keydat_len
)
252 if (bcmp(key
->keydat_val
, c
->ypc_key
.keydat_val
,
260 val
->valdat_len
= c
->ypc_val
.valdat_len
;
261 val
->valdat_val
= c
->ypc_val
.valdat_val
;
268 ypbinderr_string(int incode
)
277 errstr
= "Internal ypbind error";
279 case YPBIND_ERR_NOSERV
:
280 errstr
= "Domain not bound";
282 case YPBIND_ERR_RESC
:
283 errstr
= "System resource allocation failure";
290 strlcpy(err
, errstr
, sizeof(err
));
292 snprintf(err
, sizeof(err
), "Unknown ypbind error: #%d\n", incode
);
297 _yp_dobind(const char *dom
, struct dom_binding
**ypdb
)
299 static pid_t pid
= -1;
300 char path
[MAXPATHLEN
];
301 struct dom_binding
*ysd
, *ysd2
;
302 struct ypbind_resp ypbr
;
304 struct sockaddr_in clnt_sin
;
311 struct sockaddr_in check
;
312 socklen_t checklen
= sizeof(struct sockaddr_in
);
314 /* Not allowed; bad doggie. Bad. */
315 if (strchr(dom
, '/') != NULL
)
316 return(YPERR_BADARGS
);
319 if (!(pid
== -1 || pid
== gpid
)) {
322 if (ysd
->dom_client
!= NULL
)
324 ysd2
= ysd
->dom_pnext
;
335 if (dom
== NULL
|| strlen(dom
) == 0)
336 return (YPERR_BADARGS
);
338 for (ysd
= _ypbindlist
; ysd
; ysd
= ysd
->dom_pnext
)
339 if (strcmp(dom
, ysd
->dom_domain
) == 0)
344 ysd
= (struct dom_binding
*)malloc(sizeof *ysd
);
345 bzero((char *)ysd
, sizeof *ysd
);
346 ysd
->dom_socket
= -1;
350 /* Check the socket -- may have been hosed by the caller. */
351 if (_getsockname(ysd
->dom_socket
, (struct sockaddr
*)&check
,
352 &checklen
) == -1 || check
.sin_family
!= AF_INET
||
353 check
.sin_port
!= ysd
->dom_local_port
) {
354 /* Socket became bogus somehow... need to rebind. */
357 sock
= ysd
->dom_socket
;
358 save
= _dup(ysd
->dom_socket
);
359 if (ysd
->dom_client
!= NULL
)
360 clnt_destroy(ysd
->dom_client
);
362 ysd
->dom_client
= NULL
;
363 sock
= _dup2(save
, sock
);
370 if (retries
> MAX_RETRIES
) {
373 return(YPERR_YPBIND
);
376 if (ysd
->dom_vers
== 0) {
378 * We're trying to make a new binding: zorch the
379 * existing handle now (if any).
381 if (ysd
->dom_client
!= NULL
) {
382 clnt_destroy(ysd
->dom_client
);
383 ysd
->dom_client
= NULL
;
384 ysd
->dom_socket
= -1;
386 snprintf(path
, sizeof(path
), "%s/%s.%d", BINDINGDIR
, dom
, 2);
387 if ((fd
= _open(path
, O_RDONLY
)) == -1) {
388 /* no binding file, YP is dead. */
389 /* Try to bring it back to life. */
393 if (_flock(fd
, LOCK_EX
|LOCK_NB
) == -1 && errno
== EWOULDBLOCK
) {
395 struct ypbind_resp ybr
;
398 iov
[0].iov_base
= (caddr_t
)&ypb_port
;
399 iov
[0].iov_len
= sizeof ypb_port
;
400 iov
[1].iov_base
= (caddr_t
)&ybr
;
401 iov
[1].iov_len
= sizeof ybr
;
403 r
= _readv(fd
, iov
, 2);
404 if (r
!= (ssize_t
)(iov
[0].iov_len
+ iov
[1].iov_len
)) {
410 bzero(&ysd
->dom_server_addr
, sizeof ysd
->dom_server_addr
);
411 ysd
->dom_server_addr
.sin_family
= AF_INET
;
412 ysd
->dom_server_addr
.sin_len
= sizeof(struct sockaddr_in
);
413 bcopy(&ybr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_addr
,
414 &ysd
->dom_server_addr
.sin_addr
.s_addr
,
415 sizeof(ysd
->dom_server_addr
.sin_addr
.s_addr
));
416 bcopy(&ybr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
,
417 &ysd
->dom_server_addr
.sin_port
,
418 sizeof(ysd
->dom_server_addr
.sin_port
));
420 ysd
->dom_server_port
= ysd
->dom_server_addr
.sin_port
;
424 /* no lock on binding file, YP is dead. */
425 /* Try to bring it back to life. */
432 if (ysd
->dom_vers
== -1 || ysd
->dom_vers
== 0) {
434 * We're trying to make a new binding: zorch the
435 * existing handle now (if any).
437 if (ysd
->dom_client
!= NULL
) {
438 clnt_destroy(ysd
->dom_client
);
439 ysd
->dom_client
= NULL
;
440 ysd
->dom_socket
= -1;
442 bzero((char *)&clnt_sin
, sizeof clnt_sin
);
443 clnt_sin
.sin_family
= AF_INET
;
444 clnt_sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
446 clnt_sock
= RPC_ANYSOCK
;
447 client
= clnttcp_create(&clnt_sin
, YPBINDPROG
, YPBINDVERS
, &clnt_sock
,
449 if (client
== NULL
) {
451 * These conditions indicate ypbind just isn't
452 * alive -- we probably don't want to shoot our
453 * mouth off in this case; instead generate error
454 * messages only for really exotic problems.
456 if (rpc_createerr
.cf_stat
!= RPC_PROGNOTREGISTERED
&&
457 (rpc_createerr
.cf_stat
!= RPC_SYSTEMERROR
&&
458 rpc_createerr
.cf_error
.re_errno
== ECONNREFUSED
))
459 clnt_pcreateerror("clnttcp_create");
462 return (YPERR_YPBIND
);
466 * Check the port number -- should be < IPPORT_RESERVED.
467 * If not, it's possible someone has registered a bogus
468 * ypbind with the portmapper and is trying to trick us.
470 if (ntohs(clnt_sin
.sin_port
) >= IPPORT_RESERVED
) {
472 clnt_destroy(client
);
475 return(YPERR_YPBIND
);
477 tv
.tv_sec
= _yplib_timeout
/2;
479 r
= clnt_call(client
, YPBINDPROC_DOMAIN
,
480 (xdrproc_t
)xdr_domainname
, &dom
,
481 (xdrproc_t
)xdr_ypbind_resp
, &ypbr
, tv
);
482 if (r
!= RPC_SUCCESS
) {
483 clnt_destroy(client
);
485 if (r
== RPC_PROGUNAVAIL
|| r
== RPC_PROCUNAVAIL
) {
488 return(YPERR_YPBIND
);
491 "YP: server for domain %s not responding, retrying\n", dom
);
494 if (ypbr
.ypbind_status
!= YPBIND_SUCC_VAL
) {
495 struct timespec time_to_sleep
, time_remaining
;
497 clnt_destroy(client
);
500 time_to_sleep
.tv_sec
= _yplib_timeout
/2;
501 time_to_sleep
.tv_nsec
= 0;
502 _nanosleep(&time_to_sleep
,
507 clnt_destroy(client
);
509 bzero((char *)&ysd
->dom_server_addr
, sizeof ysd
->dom_server_addr
);
510 ysd
->dom_server_addr
.sin_family
= AF_INET
;
511 bcopy(&ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
,
512 &ysd
->dom_server_addr
.sin_port
,
513 sizeof(ysd
->dom_server_addr
.sin_port
));
514 bcopy(&ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_addr
,
515 &ysd
->dom_server_addr
.sin_addr
.s_addr
,
516 sizeof(ysd
->dom_server_addr
.sin_addr
.s_addr
));
519 * We could do a reserved port check here too, but this
520 * could pose compatibility problems. The local ypbind is
521 * supposed to decide whether or not to trust yp servers
522 * on insecure ports. For now, we trust its judgement.
524 ysd
->dom_server_port
=
525 *(u_short
*)&ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
;
527 ysd
->dom_vers
= YPVERS
;
528 strlcpy(ysd
->dom_domain
, dom
, sizeof(ysd
->dom_domain
));
531 /* Don't rebuild the connection to the server unless we have to. */
532 if (ysd
->dom_client
== NULL
) {
533 tv
.tv_sec
= _yplib_timeout
/2;
535 ysd
->dom_socket
= RPC_ANYSOCK
;
536 ysd
->dom_client
= clntudp_bufcreate(&ysd
->dom_server_addr
,
537 YPPROG
, YPVERS
, tv
, &ysd
->dom_socket
, 1280, 2304);
538 if (ysd
->dom_client
== NULL
) {
539 clnt_pcreateerror("clntudp_create");
543 if (_fcntl(ysd
->dom_socket
, F_SETFD
, 1) == -1)
544 perror("fcntl: F_SETFD");
546 * We want a port number associated with this socket
547 * so that we can check its authenticity later.
549 checklen
= sizeof(struct sockaddr_in
);
550 bzero((char *)&check
, checklen
);
551 _bind(ysd
->dom_socket
, (struct sockaddr
*)&check
, checklen
);
552 check
.sin_family
= AF_INET
;
553 if (!_getsockname(ysd
->dom_socket
,
554 (struct sockaddr
*)&check
, &checklen
)) {
555 ysd
->dom_local_port
= check
.sin_port
;
557 clnt_destroy(ysd
->dom_client
);
560 return(YPERR_YPBIND
);
565 ysd
->dom_pnext
= _ypbindlist
;
570 * Set low retry timeout to realistically handle UDP packet
571 * loss for YP packet bursts.
575 clnt_control(ysd
->dom_client
, CLSET_RETRY_TIMEOUT
, (char*)&tv
);
583 _yp_unbind(struct dom_binding
*ypb
)
585 struct sockaddr_in check
;
586 socklen_t checklen
= sizeof(struct sockaddr_in
);
588 if (ypb
->dom_client
!= NULL
) {
589 /* Check the socket -- may have been hosed by the caller. */
590 if (_getsockname(ypb
->dom_socket
, (struct sockaddr
*)&check
,
591 &checklen
) == -1 || check
.sin_family
!= AF_INET
||
592 check
.sin_port
!= ypb
->dom_local_port
) {
595 sock
= ypb
->dom_socket
;
596 save
= _dup(ypb
->dom_socket
);
597 clnt_destroy(ypb
->dom_client
);
598 sock
= _dup2(save
, sock
);
601 clnt_destroy(ypb
->dom_client
);
604 ypb
->dom_client
= NULL
;
605 ypb
->dom_socket
= -1;
608 ypmatch_cache_flush(ypb
);
613 yp_bind_locked(char *dom
)
615 return (_yp_dobind(dom
, NULL
));
624 r
= yp_bind_locked(dom
);
630 yp_unbind_locked(char *dom
)
632 struct dom_binding
*ypb
, *ypbp
;
635 for (ypb
= _ypbindlist
; ypb
; ypb
= ypb
->dom_pnext
) {
636 if (strcmp(dom
, ypb
->dom_domain
) == 0) {
639 ypbp
->dom_pnext
= ypb
->dom_pnext
;
641 _ypbindlist
= ypb
->dom_pnext
;
654 yp_unbind_locked(dom
);
659 yp_match(char *indomain
, char *inmap
, const char *inkey
, int inkeylen
,
660 char **outval
, int *outvallen
)
662 struct dom_binding
*ysd
;
663 struct ypresp_val yprv
;
665 struct ypreq_key yprk
;
673 if (inkey
== NULL
|| !strlen(inkey
) || inkeylen
<= 0 ||
674 inmap
== NULL
|| !strlen(inmap
) ||
675 indomain
== NULL
|| !strlen(indomain
))
676 return (YPERR_BADARGS
);
679 if (_yp_dobind(indomain
, &ysd
) != 0) {
681 return(YPERR_DOMAIN
);
684 yprk
.domain
= indomain
;
686 yprk
.key
.keydat_val
= (char *)inkey
;
687 yprk
.key
.keydat_len
= inkeylen
;
690 if (ypmatch_cache_lookup(ysd
, yprk
.map
, &yprk
.key
, &yprv
.val
) == TRUE
) {
692 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
693 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
695 *outvallen
= yprv
.val
.valdat_len
;
696 *outval
= (char *)malloc(*outvallen
+1);
697 bcopy(yprv
.val
.valdat_val
, *outval
, *outvallen
);
698 (*outval
)[*outvallen
] = '\0';
705 if (_yp_dobind(indomain
, &ysd
) != 0) {
707 return (YPERR_DOMAIN
);
710 tv
.tv_sec
= _yplib_timeout
;
713 bzero((char *)&yprv
, sizeof yprv
);
715 r
= clnt_call(ysd
->dom_client
, YPPROC_MATCH
,
716 (xdrproc_t
)xdr_ypreq_key
, &yprk
,
717 (xdrproc_t
)xdr_ypresp_val
, &yprv
, tv
);
718 if (r
!= RPC_SUCCESS
) {
719 clnt_perror(ysd
->dom_client
, "yp_match: clnt_call");
724 if (!(r
= ypprot_err(yprv
.stat
))) {
725 *outvallen
= yprv
.val
.valdat_len
;
726 *outval
= (char *)malloc(*outvallen
+1);
727 bcopy(yprv
.val
.valdat_val
, *outval
, *outvallen
);
728 (*outval
)[*outvallen
] = '\0';
730 ypmatch_cache_insert(ysd
, yprk
.map
, &yprk
.key
, &yprv
.val
);
734 xdr_free((xdrproc_t
)xdr_ypresp_val
, &yprv
);
740 yp_get_default_domain_locked(char **domp
)
743 if (_yp_domain
[0] == '\0')
744 if (getdomainname(_yp_domain
, sizeof _yp_domain
))
745 return (YPERR_NODOM
);
751 yp_get_default_domain(char **domp
)
756 r
= yp_get_default_domain_locked(domp
);
762 yp_first(char *indomain
, char *inmap
, char **outkey
, size_t *outkeylen
,
763 char **outval
, size_t *outvallen
)
765 struct ypresp_key_val yprkv
;
766 struct ypreq_nokey yprnk
;
767 struct dom_binding
*ysd
;
773 if (indomain
== NULL
|| !strlen(indomain
) ||
774 inmap
== NULL
|| !strlen(inmap
))
775 return (YPERR_BADARGS
);
777 *outkey
= *outval
= NULL
;
778 *outkeylen
= *outvallen
= 0;
782 if (_yp_dobind(indomain
, &ysd
) != 0) {
784 return (YPERR_DOMAIN
);
787 tv
.tv_sec
= _yplib_timeout
;
790 yprnk
.domain
= indomain
;
792 bzero((char *)&yprkv
, sizeof yprkv
);
794 r
= clnt_call(ysd
->dom_client
, YPPROC_FIRST
,
795 (xdrproc_t
)xdr_ypreq_nokey
, &yprnk
,
796 (xdrproc_t
)xdr_ypresp_key_val
, &yprkv
, tv
);
797 if (r
!= RPC_SUCCESS
) {
798 clnt_perror(ysd
->dom_client
, "yp_first: clnt_call");
802 if (!(r
= ypprot_err(yprkv
.stat
))) {
803 *outkeylen
= yprkv
.key
.keydat_len
;
804 *outkey
= (char *)malloc(*outkeylen
+1);
805 bcopy(yprkv
.key
.keydat_val
, *outkey
, *outkeylen
);
806 (*outkey
)[*outkeylen
] = '\0';
807 *outvallen
= yprkv
.val
.valdat_len
;
808 *outval
= (char *)malloc(*outvallen
+1);
809 bcopy(yprkv
.val
.valdat_val
, *outval
, *outvallen
);
810 (*outval
)[*outvallen
] = '\0';
813 xdr_free((xdrproc_t
)xdr_ypresp_key_val
, &yprkv
);
819 yp_next(char *indomain
, char *inmap
, char *inkey
, size_t inkeylen
,
820 char **outkey
, size_t *outkeylen
, char **outval
, size_t *outvallen
)
822 struct ypresp_key_val yprkv
;
823 struct ypreq_key yprk
;
824 struct dom_binding
*ysd
;
830 if (inkey
== NULL
|| !strlen(inkey
) || inkeylen
<= 0 ||
831 inmap
== NULL
|| !strlen(inmap
) ||
832 indomain
== NULL
|| !strlen(indomain
))
833 return (YPERR_BADARGS
);
835 *outkey
= *outval
= NULL
;
836 *outkeylen
= *outvallen
= 0;
840 if (_yp_dobind(indomain
, &ysd
) != 0) {
842 return (YPERR_DOMAIN
);
845 tv
.tv_sec
= _yplib_timeout
;
848 yprk
.domain
= indomain
;
850 yprk
.key
.keydat_val
= inkey
;
851 yprk
.key
.keydat_len
= inkeylen
;
852 bzero((char *)&yprkv
, sizeof yprkv
);
854 r
= clnt_call(ysd
->dom_client
, YPPROC_NEXT
,
855 (xdrproc_t
)xdr_ypreq_key
, &yprk
,
856 (xdrproc_t
)xdr_ypresp_key_val
, &yprkv
, tv
);
857 if (r
!= RPC_SUCCESS
) {
858 clnt_perror(ysd
->dom_client
, "yp_next: clnt_call");
862 if (!(r
= ypprot_err(yprkv
.stat
))) {
863 *outkeylen
= yprkv
.key
.keydat_len
;
864 *outkey
= (char *)malloc(*outkeylen
+1);
865 bcopy(yprkv
.key
.keydat_val
, *outkey
, *outkeylen
);
866 (*outkey
)[*outkeylen
] = '\0';
867 *outvallen
= yprkv
.val
.valdat_len
;
868 *outval
= (char *)malloc(*outvallen
+1);
869 bcopy(yprkv
.val
.valdat_val
, *outval
, *outvallen
);
870 (*outval
)[*outvallen
] = '\0';
873 xdr_free((xdrproc_t
)xdr_ypresp_key_val
, &yprkv
);
879 yp_all(char *indomain
, char *inmap
, struct ypall_callback
*incallback
)
881 struct ypreq_nokey yprnk
;
882 struct dom_binding
*ysd
;
884 struct sockaddr_in clnt_sin
;
886 u_long status
, savstat
;
891 if (indomain
== NULL
|| !strlen(indomain
) ||
892 inmap
== NULL
|| !strlen(inmap
))
893 return (YPERR_BADARGS
);
898 if (_yp_dobind(indomain
, &ysd
) != 0) {
900 return (YPERR_DOMAIN
);
903 tv
.tv_sec
= _yplib_timeout
;
906 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
908 clnt_sock
= RPC_ANYSOCK
;
909 clnt_sin
= ysd
->dom_server_addr
;
910 clnt_sin
.sin_port
= 0;
911 clnt
= clnttcp_create(&clnt_sin
, YPPROG
, YPVERS
, &clnt_sock
, 0, 0);
914 printf("clnttcp_create failed\n");
918 yprnk
.domain
= indomain
;
920 ypresp_allfn
= incallback
->foreach
;
921 ypresp_data
= (void *)incallback
->data
;
923 if (clnt_call(clnt
, YPPROC_ALL
,
924 (xdrproc_t
)xdr_ypreq_nokey
, &yprnk
,
925 (xdrproc_t
)xdr_ypresp_all_seq
, &status
, tv
) != RPC_SUCCESS
) {
926 clnt_perror(ysd
->dom_client
, "yp_all: clnt_call");
934 xdr_free((xdrproc_t
)xdr_ypresp_all_seq
, &status
); /* not really needed... */
936 if (savstat
!= YP_NOMORE
)
937 return (ypprot_err(savstat
));
942 yp_order(char *indomain
, char *inmap
, int *outorder
)
944 struct dom_binding
*ysd
;
945 struct ypresp_order ypro
;
946 struct ypreq_nokey yprnk
;
952 if (indomain
== NULL
|| !strlen(indomain
) ||
953 inmap
== NULL
|| !strlen(inmap
))
954 return (YPERR_BADARGS
);
958 if (_yp_dobind(indomain
, &ysd
) != 0) {
960 return (YPERR_DOMAIN
);
963 tv
.tv_sec
= _yplib_timeout
;
966 yprnk
.domain
= indomain
;
969 bzero((char *)(char *)&ypro
, sizeof ypro
);
971 r
= clnt_call(ysd
->dom_client
, YPPROC_ORDER
,
972 (xdrproc_t
)xdr_ypreq_nokey
, &yprnk
,
973 (xdrproc_t
)xdr_ypresp_order
, &ypro
, tv
);
976 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
979 if (r
== RPC_PROCUNAVAIL
) {
984 if (r
!= RPC_SUCCESS
) {
985 clnt_perror(ysd
->dom_client
, "yp_order: clnt_call");
990 if (!(r
= ypprot_err(ypro
.stat
))) {
991 *outorder
= ypro
.ordernum
;
994 xdr_free((xdrproc_t
)xdr_ypresp_order
, &ypro
);
1000 yp_master(char *indomain
, char *inmap
, char **outname
)
1002 struct dom_binding
*ysd
;
1003 struct ypresp_master yprm
;
1004 struct ypreq_nokey yprnk
;
1010 if (indomain
== NULL
|| !strlen(indomain
) ||
1011 inmap
== NULL
|| !strlen(inmap
))
1012 return (YPERR_BADARGS
);
1015 if (_yp_dobind(indomain
, &ysd
) != 0) {
1017 return (YPERR_DOMAIN
);
1020 tv
.tv_sec
= _yplib_timeout
;
1023 yprnk
.domain
= indomain
;
1026 bzero((char *)&yprm
, sizeof yprm
);
1028 r
= clnt_call(ysd
->dom_client
, YPPROC_MASTER
,
1029 (xdrproc_t
)xdr_ypreq_nokey
, &yprnk
,
1030 (xdrproc_t
)xdr_ypresp_master
, &yprm
, tv
);
1031 if (r
!= RPC_SUCCESS
) {
1032 clnt_perror(ysd
->dom_client
, "yp_master: clnt_call");
1037 if (!(r
= ypprot_err(yprm
.stat
))) {
1038 *outname
= (char *)strdup(yprm
.peer
);
1041 xdr_free((xdrproc_t
)xdr_ypresp_master
, &yprm
);
1047 yp_maplist(char *indomain
, struct ypmaplist
**outmaplist
)
1049 struct dom_binding
*ysd
;
1050 struct ypresp_maplist ypml
;
1056 if (indomain
== NULL
|| !strlen(indomain
))
1057 return (YPERR_BADARGS
);
1061 if (_yp_dobind(indomain
, &ysd
) != 0) {
1063 return (YPERR_DOMAIN
);
1066 tv
.tv_sec
= _yplib_timeout
;
1069 bzero((char *)&ypml
, sizeof ypml
);
1071 r
= clnt_call(ysd
->dom_client
, YPPROC_MAPLIST
,
1072 (xdrproc_t
)xdr_domainname
, &indomain
,
1073 (xdrproc_t
)xdr_ypresp_maplist
, &ypml
, tv
);
1074 if (r
!= RPC_SUCCESS
) {
1075 clnt_perror(ysd
->dom_client
, "yp_maplist: clnt_call");
1079 if (!(r
= ypprot_err(ypml
.stat
))) {
1080 *outmaplist
= ypml
.maps
;
1083 /* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1089 yperr_string(int incode
)
1092 static char err
[80];
1099 errstr
= "Request arguments bad";
1102 errstr
= "RPC failure";
1105 errstr
= "Can't bind to server which serves this domain";
1108 errstr
= "No such map in server's domain";
1111 errstr
= "No such key in map";
1114 errstr
= "YP server error";
1117 errstr
= "Local resource allocation failure";
1120 errstr
= "No more records in map database";
1123 errstr
= "Can't communicate with portmapper";
1126 errstr
= "Can't communicate with ypbind";
1129 errstr
= "Can't communicate with ypserv";
1132 errstr
= "Local domain name not set";
1135 errstr
= "Server data base is bad";
1138 errstr
= "YP server version mismatch - server can't supply service.";
1141 errstr
= "Access violation";
1144 errstr
= "Database is busy";
1151 strlcpy(err
, errstr
, sizeof(err
));
1153 snprintf(err
, sizeof(err
), "YP unknown error %d\n", incode
);
1158 ypprot_err(unsigned int incode
)
1164 return (YPERR_YPBIND
);
1166 return (YPERR_NOMORE
);
1170 return (YPERR_DOMAIN
);
1174 return (YPERR_YPERR
);
1176 return (YPERR_BADDB
);
1178 return (YPERR_YPERR
);
1180 return (YPERR_BADARGS
);
1182 return (YPERR_VERS
);
1184 return (YPERR_YPERR
);
1188 _yp_check(char **dom
)
1193 if (_yp_domain
[0]=='\0')
1194 if (yp_get_default_domain_locked(&unused
)) {
1202 if (yp_bind_locked(_yp_domain
) == 0) {
1203 yp_unbind_locked(_yp_domain
);