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.34.2.2 2002/02/15 00:46:53 des Exp $
31 * $DragonFly: src/lib/libc/yp/yplib.c,v 1.7 2005/04/26 06:30:52 y0netan1 Exp $
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
47 #include <rpcsvc/yp.h>
48 #include "un-namespace.h"
50 bool_t
xdr_ypresp_all_seq(XDR
*, u_long
*);
51 int _yp_check(char **);
53 int (*ypresp_allfn
)(unsigned long, char *, int, char *, int, void *);
56 * We have to define these here due to clashes between yp_prot.h and
68 struct ypmatch_ent
*ypc_next
;
70 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
71 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
75 struct dom_binding
*dom_pnext
;
76 char dom_domain
[YPMAXDOMAIN
+ 1];
77 struct sockaddr_in dom_server_addr
;
78 u_short dom_server_port
;
81 u_short dom_local_port
; /* now I finally know what this is for. */
84 struct ypmatch_ent
*cache
;
89 #include <rpcsvc/yp.h>
90 #include <rpcsvc/ypclnt.h>
93 #define BINDINGDIR "/var/yp/binding"
95 #define MAX_RETRIES 20
99 static void _yp_unbind(struct dom_binding
*);
100 struct dom_binding
*_ypbindlist
;
101 static char _yp_domain
[MAXHOSTNAMELEN
];
102 int _yplib_timeout
= 10;
106 ypmatch_cache_delete(struct dom_binding
*ypdb
, struct ypmatch_ent
*prev
,
107 struct ypmatch_ent
*cur
)
110 ypdb
->cache
= cur
->ypc_next
;
112 prev
->ypc_next
= cur
->ypc_next
;
115 free(cur
->ypc_key
.keydat_val
);
116 free(cur
->ypc_val
.valdat_val
);
119 ypdb
->ypmatch_cachecnt
--;
125 ypmatch_cache_flush(struct dom_binding
*ypdb
)
127 struct ypmatch_ent
*n
, *c
= ypdb
->cache
;
131 ypmatch_cache_delete(ypdb
, NULL
, c
);
139 ypmatch_cache_expire(struct dom_binding
*ypdb
)
141 struct ypmatch_ent
*c
= ypdb
->cache
;
142 struct ypmatch_ent
*n
, *p
= NULL
;
148 if (t
>= c
->ypc_expire_t
) {
150 ypmatch_cache_delete(ypdb
, p
, c
);
162 ypmatch_cache_insert(struct dom_binding
*ypdb
, char *map
, keydat
*key
,
165 struct ypmatch_ent
*new;
167 /* Do an expire run to maybe open up a slot. */
168 if (ypdb
->ypmatch_cachecnt
)
169 ypmatch_cache_expire(ypdb
);
172 * If there are no slots free, then force an expire of
173 * the least recently used entry.
175 if (ypdb
->ypmatch_cachecnt
>= YPLIB_MAXCACHE
) {
176 struct ypmatch_ent
*o
= NULL
, *c
= ypdb
->cache
;
182 if (c
->ypc_expire_t
< oldest
) {
183 oldest
= c
->ypc_expire_t
;
192 ypmatch_cache_expire(ypdb
);
195 new = malloc(sizeof(struct ypmatch_ent
));
199 new->ypc_map
= strdup(map
);
200 if (new->ypc_map
== NULL
) {
204 new->ypc_key
.keydat_val
= malloc(key
->keydat_len
);
205 if (new->ypc_key
.keydat_val
== NULL
) {
210 new->ypc_val
.valdat_val
= malloc(val
->valdat_len
);
211 if (new->ypc_val
.valdat_val
== NULL
) {
212 free(new->ypc_val
.valdat_val
);
218 new->ypc_expire_t
= time(NULL
) + YPLIB_EXPIRE
;
219 new->ypc_key
.keydat_len
= key
->keydat_len
;
220 new->ypc_val
.valdat_len
= val
->valdat_len
;
221 bcopy(key
->keydat_val
, new->ypc_key
.keydat_val
, key
->keydat_len
);
222 bcopy(val
->valdat_val
, new->ypc_val
.valdat_val
, val
->valdat_len
);
224 new->ypc_next
= ypdb
->cache
;
227 ypdb
->ypmatch_cachecnt
++;
233 ypmatch_cache_lookup(struct dom_binding
*ypdb
, char *map
, keydat
*key
,
236 struct ypmatch_ent
*c
= ypdb
->cache
;
238 ypmatch_cache_expire(ypdb
);
240 for (c
= ypdb
->cache
; c
!= NULL
; c
= c
->ypc_next
) {
241 if (strcmp(map
, c
->ypc_map
))
243 if (key
->keydat_len
!= c
->ypc_key
.keydat_len
)
245 if (bcmp(key
->keydat_val
, c
->ypc_key
.keydat_val
,
253 val
->valdat_len
= c
->ypc_val
.valdat_len
;
254 val
->valdat_val
= c
->ypc_val
.valdat_val
;
261 ypbinderr_string(int incode
)
270 errstr
= "Internal ypbind error";
272 case YPBIND_ERR_NOSERV
:
273 errstr
= "Domain not bound";
275 case YPBIND_ERR_RESC
:
276 errstr
= "System resource allocation failure";
283 strlcpy(err
, errstr
, sizeof(err
));
285 snprintf(err
, sizeof(err
), "Unknown ypbind error: #%d\n", incode
);
290 _yp_dobind(const char *dom
, struct dom_binding
**ypdb
)
292 static pid_t pid
= -1;
293 char path
[MAXPATHLEN
];
294 struct dom_binding
*ysd
, *ysd2
;
295 struct ypbind_resp ypbr
;
297 struct sockaddr_in clnt_sin
;
304 struct sockaddr_in check
;
305 int checklen
= sizeof(struct sockaddr_in
);
307 /* Not allowed; bad doggie. Bad. */
308 if (strchr(dom
, '/') != NULL
)
309 return(YPERR_BADARGS
);
312 if (!(pid
== -1 || pid
== gpid
)) {
315 if (ysd
->dom_client
!= NULL
)
317 ysd2
= ysd
->dom_pnext
;
328 if (dom
== NULL
|| strlen(dom
) == 0)
329 return (YPERR_BADARGS
);
331 for (ysd
= _ypbindlist
; ysd
; ysd
= ysd
->dom_pnext
)
332 if (strcmp(dom
, ysd
->dom_domain
) == 0)
337 ysd
= (struct dom_binding
*)malloc(sizeof *ysd
);
338 bzero((char *)ysd
, sizeof *ysd
);
339 ysd
->dom_socket
= -1;
343 /* Check the socket -- may have been hosed by the caller. */
344 if (_getsockname(ysd
->dom_socket
, (struct sockaddr
*)&check
,
345 &checklen
) == -1 || check
.sin_family
!= AF_INET
||
346 check
.sin_port
!= ysd
->dom_local_port
) {
347 /* Socket became bogus somehow... need to rebind. */
350 sock
= ysd
->dom_socket
;
351 save
= _dup(ysd
->dom_socket
);
352 if (ysd
->dom_client
!= NULL
)
353 clnt_destroy(ysd
->dom_client
);
355 ysd
->dom_client
= NULL
;
356 sock
= _dup2(save
, sock
);
363 if (retries
> MAX_RETRIES
) {
366 return(YPERR_YPBIND
);
369 if (ysd
->dom_vers
== 0) {
371 * We're trying to make a new binding: zorch the
372 * existing handle now (if any).
374 if (ysd
->dom_client
!= NULL
) {
375 clnt_destroy(ysd
->dom_client
);
376 ysd
->dom_client
= NULL
;
377 ysd
->dom_socket
= -1;
379 snprintf(path
, sizeof(path
), "%s/%s.%d", BINDINGDIR
, dom
, 2);
380 if ((fd
= _open(path
, O_RDONLY
)) == -1) {
381 /* no binding file, YP is dead. */
382 /* Try to bring it back to life. */
386 if (_flock(fd
, LOCK_EX
|LOCK_NB
) == -1 && errno
== EWOULDBLOCK
) {
388 struct ypbind_resp ybr
;
391 iov
[0].iov_base
= (caddr_t
)&ypb_port
;
392 iov
[0].iov_len
= sizeof ypb_port
;
393 iov
[1].iov_base
= (caddr_t
)&ybr
;
394 iov
[1].iov_len
= sizeof ybr
;
396 r
= _readv(fd
, iov
, 2);
397 if (r
!= (ssize_t
)(iov
[0].iov_len
+ iov
[1].iov_len
)) {
403 bzero(&ysd
->dom_server_addr
, sizeof ysd
->dom_server_addr
);
404 ysd
->dom_server_addr
.sin_family
= AF_INET
;
405 ysd
->dom_server_addr
.sin_len
= sizeof(struct sockaddr_in
);
406 ysd
->dom_server_addr
.sin_addr
.s_addr
=
407 *(u_long
*)&ybr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_addr
;
408 ysd
->dom_server_addr
.sin_port
=
409 *(u_short
*)&ybr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
;
411 ysd
->dom_server_port
= ysd
->dom_server_addr
.sin_port
;
415 /* no lock on binding file, YP is dead. */
416 /* Try to bring it back to life. */
423 if (ysd
->dom_vers
== -1 || ysd
->dom_vers
== 0) {
425 * We're trying to make a new binding: zorch the
426 * existing handle now (if any).
428 if (ysd
->dom_client
!= NULL
) {
429 clnt_destroy(ysd
->dom_client
);
430 ysd
->dom_client
= NULL
;
431 ysd
->dom_socket
= -1;
433 bzero((char *)&clnt_sin
, sizeof clnt_sin
);
434 clnt_sin
.sin_family
= AF_INET
;
435 clnt_sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
437 clnt_sock
= RPC_ANYSOCK
;
438 client
= clnttcp_create(&clnt_sin
, YPBINDPROG
, YPBINDVERS
, &clnt_sock
,
440 if (client
== NULL
) {
442 * These conditions indicate ypbind just isn't
443 * alive -- we probably don't want to shoot our
444 * mouth off in this case; instead generate error
445 * messages only for really exotic problems.
447 if (rpc_createerr
.cf_stat
!= RPC_PROGNOTREGISTERED
&&
448 (rpc_createerr
.cf_stat
!= RPC_SYSTEMERROR
&&
449 rpc_createerr
.cf_error
.re_errno
== ECONNREFUSED
))
450 clnt_pcreateerror("clnttcp_create");
453 return (YPERR_YPBIND
);
457 * Check the port number -- should be < IPPORT_RESERVED.
458 * If not, it's possible someone has registered a bogus
459 * ypbind with the portmapper and is trying to trick us.
461 if (ntohs(clnt_sin
.sin_port
) >= IPPORT_RESERVED
) {
463 clnt_destroy(client
);
466 return(YPERR_YPBIND
);
468 tv
.tv_sec
= _yplib_timeout
/2;
470 r
= clnt_call(client
, YPBINDPROC_DOMAIN
,
471 xdr_domainname
, (char *)&dom
, xdr_ypbind_resp
, &ypbr
, tv
);
472 if (r
!= RPC_SUCCESS
) {
473 clnt_destroy(client
);
475 if (r
== RPC_PROGUNAVAIL
|| r
== RPC_PROCUNAVAIL
) {
478 return(YPERR_YPBIND
);
481 "YP: server for domain %s not responding, retrying\n", dom
);
484 if (ypbr
.ypbind_status
!= YPBIND_SUCC_VAL
) {
485 struct timespec time_to_sleep
, time_remaining
;
487 clnt_destroy(client
);
490 time_to_sleep
.tv_sec
= _yplib_timeout
/2;
491 time_to_sleep
.tv_nsec
= 0;
492 _nanosleep(&time_to_sleep
,
497 clnt_destroy(client
);
499 bzero((char *)&ysd
->dom_server_addr
, sizeof ysd
->dom_server_addr
);
500 ysd
->dom_server_addr
.sin_family
= AF_INET
;
501 ysd
->dom_server_addr
.sin_port
=
502 *(u_short
*)&ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
;
503 ysd
->dom_server_addr
.sin_addr
.s_addr
=
504 *(u_long
*)&ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_addr
;
507 * We could do a reserved port check here too, but this
508 * could pose compatibility problems. The local ypbind is
509 * supposed to decide whether or not to trust yp servers
510 * on insecure ports. For now, we trust its judgement.
512 ysd
->dom_server_port
=
513 *(u_short
*)&ypbr
.ypbind_resp_u
.ypbind_bindinfo
.ypbind_binding_port
;
515 ysd
->dom_vers
= YPVERS
;
516 strlcpy(ysd
->dom_domain
, dom
, sizeof(ysd
->dom_domain
));
519 /* Don't rebuild the connection to the server unless we have to. */
520 if (ysd
->dom_client
== NULL
) {
521 tv
.tv_sec
= _yplib_timeout
/2;
523 ysd
->dom_socket
= RPC_ANYSOCK
;
524 ysd
->dom_client
= clntudp_bufcreate(&ysd
->dom_server_addr
,
525 YPPROG
, YPVERS
, tv
, &ysd
->dom_socket
, 1280, 2304);
526 if (ysd
->dom_client
== NULL
) {
527 clnt_pcreateerror("clntudp_create");
531 if (_fcntl(ysd
->dom_socket
, F_SETFD
, 1) == -1)
532 perror("fcntl: F_SETFD");
534 * We want a port number associated with this socket
535 * so that we can check its authenticity later.
537 checklen
= sizeof(struct sockaddr_in
);
538 bzero((char *)&check
, checklen
);
539 _bind(ysd
->dom_socket
, (struct sockaddr
*)&check
, checklen
);
540 check
.sin_family
= AF_INET
;
541 if (!_getsockname(ysd
->dom_socket
,
542 (struct sockaddr
*)&check
, &checklen
)) {
543 ysd
->dom_local_port
= check
.sin_port
;
545 clnt_destroy(ysd
->dom_client
);
548 return(YPERR_YPBIND
);
553 ysd
->dom_pnext
= _ypbindlist
;
563 _yp_unbind(struct dom_binding
*ypb
)
565 struct sockaddr_in check
;
566 int checklen
= sizeof(struct sockaddr_in
);
568 if (ypb
->dom_client
!= NULL
) {
569 /* Check the socket -- may have been hosed by the caller. */
570 if (_getsockname(ypb
->dom_socket
, (struct sockaddr
*)&check
,
571 &checklen
) == -1 || check
.sin_family
!= AF_INET
||
572 check
.sin_port
!= ypb
->dom_local_port
) {
575 sock
= ypb
->dom_socket
;
576 save
= _dup(ypb
->dom_socket
);
577 clnt_destroy(ypb
->dom_client
);
578 sock
= _dup2(save
, sock
);
581 clnt_destroy(ypb
->dom_client
);
584 ypb
->dom_client
= NULL
;
585 ypb
->dom_socket
= -1;
588 ypmatch_cache_flush(ypb
);
595 return (_yp_dobind(dom
, NULL
));
601 struct dom_binding
*ypb
, *ypbp
;
604 for (ypb
= _ypbindlist
; ypb
; ypb
= ypb
->dom_pnext
) {
605 if (strcmp(dom
, ypb
->dom_domain
) == 0) {
608 ypbp
->dom_pnext
= ypb
->dom_pnext
;
610 _ypbindlist
= ypb
->dom_pnext
;
620 yp_match(const char *indomain
, const char *inmap
, const char *inkey
, int inkeylen
,
621 char **outval
, int *outvallen
)
623 struct dom_binding
*ysd
;
624 struct ypresp_val yprv
;
626 struct ypreq_key yprk
;
634 if (inkey
== NULL
|| !strlen(inkey
) || inkeylen
<= 0 ||
635 inmap
== NULL
|| !strlen(inmap
) ||
636 indomain
== NULL
|| !strlen(indomain
))
637 return (YPERR_BADARGS
);
639 if (_yp_dobind(indomain
, &ysd
) != 0)
640 return(YPERR_DOMAIN
);
642 yprk
.domain
= indomain
;
644 yprk
.key
.keydat_val
= (char *)inkey
;
645 yprk
.key
.keydat_len
= inkeylen
;
648 if (ypmatch_cache_lookup(ysd
, yprk
.map
, &yprk
.key
, &yprv
.val
) == TRUE
) {
650 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
651 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
653 *outvallen
= yprv
.val
.valdat_len
;
654 *outval
= (char *)malloc(*outvallen
+1);
655 bcopy(yprv
.val
.valdat_val
, *outval
, *outvallen
);
656 (*outval
)[*outvallen
] = '\0';
662 if (_yp_dobind(indomain
, &ysd
) != 0)
663 return (YPERR_DOMAIN
);
665 tv
.tv_sec
= _yplib_timeout
;
668 bzero((char *)&yprv
, sizeof yprv
);
670 r
= clnt_call(ysd
->dom_client
, YPPROC_MATCH
,
671 xdr_ypreq_key
, &yprk
, xdr_ypresp_val
, &yprv
, tv
);
672 if (r
!= RPC_SUCCESS
) {
673 clnt_perror(ysd
->dom_client
, "yp_match: clnt_call");
678 if (!(r
= ypprot_err(yprv
.stat
))) {
679 *outvallen
= yprv
.val
.valdat_len
;
680 *outval
= (char *)malloc(*outvallen
+1);
681 bcopy(yprv
.val
.valdat_val
, *outval
, *outvallen
);
682 (*outval
)[*outvallen
] = '\0';
684 ypmatch_cache_insert(ysd
, yprk
.map
, &yprk
.key
, &yprv
.val
);
688 xdr_free(xdr_ypresp_val
, (char *)&yprv
);
693 yp_get_default_domain(char **domp
)
696 if (_yp_domain
[0] == '\0')
697 if (getdomainname(_yp_domain
, sizeof _yp_domain
))
698 return (YPERR_NODOM
);
704 yp_first(char *indomain
, char *inmap
, char **outkey
, int *outkeylen
,
705 char **outval
, int *outvallen
)
707 struct ypresp_key_val yprkv
;
708 struct ypreq_nokey yprnk
;
709 struct dom_binding
*ysd
;
715 if (indomain
== NULL
|| !strlen(indomain
) ||
716 inmap
== NULL
|| !strlen(inmap
))
717 return (YPERR_BADARGS
);
719 *outkey
= *outval
= NULL
;
720 *outkeylen
= *outvallen
= 0;
723 if (_yp_dobind(indomain
, &ysd
) != 0)
724 return (YPERR_DOMAIN
);
726 tv
.tv_sec
= _yplib_timeout
;
729 yprnk
.domain
= indomain
;
731 bzero((char *)&yprkv
, sizeof yprkv
);
733 r
= clnt_call(ysd
->dom_client
, YPPROC_FIRST
,
734 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_key_val
, &yprkv
, tv
);
735 if (r
!= RPC_SUCCESS
) {
736 clnt_perror(ysd
->dom_client
, "yp_first: clnt_call");
740 if (!(r
= ypprot_err(yprkv
.stat
))) {
741 *outkeylen
= yprkv
.key
.keydat_len
;
742 *outkey
= (char *)malloc(*outkeylen
+1);
743 bcopy(yprkv
.key
.keydat_val
, *outkey
, *outkeylen
);
744 (*outkey
)[*outkeylen
] = '\0';
745 *outvallen
= yprkv
.val
.valdat_len
;
746 *outval
= (char *)malloc(*outvallen
+1);
747 bcopy(yprkv
.val
.valdat_val
, *outval
, *outvallen
);
748 (*outval
)[*outvallen
] = '\0';
751 xdr_free(xdr_ypresp_key_val
, (char *)&yprkv
);
756 yp_next(char *indomain
, char *inmap
, char *inkey
, int inkeylen
,
757 char **outkey
, int *outkeylen
, char **outval
, int *outvallen
)
759 struct ypresp_key_val yprkv
;
760 struct ypreq_key yprk
;
761 struct dom_binding
*ysd
;
767 if (inkey
== NULL
|| !strlen(inkey
) || inkeylen
<= 0 ||
768 inmap
== NULL
|| !strlen(inmap
) ||
769 indomain
== NULL
|| !strlen(indomain
))
770 return (YPERR_BADARGS
);
772 *outkey
= *outval
= NULL
;
773 *outkeylen
= *outvallen
= 0;
776 if (_yp_dobind(indomain
, &ysd
) != 0)
777 return (YPERR_DOMAIN
);
779 tv
.tv_sec
= _yplib_timeout
;
782 yprk
.domain
= indomain
;
784 yprk
.key
.keydat_val
= inkey
;
785 yprk
.key
.keydat_len
= inkeylen
;
786 bzero((char *)&yprkv
, sizeof yprkv
);
788 r
= clnt_call(ysd
->dom_client
, YPPROC_NEXT
,
789 xdr_ypreq_key
, &yprk
, xdr_ypresp_key_val
, &yprkv
, tv
);
790 if (r
!= RPC_SUCCESS
) {
791 clnt_perror(ysd
->dom_client
, "yp_next: clnt_call");
795 if (!(r
= ypprot_err(yprkv
.stat
))) {
796 *outkeylen
= yprkv
.key
.keydat_len
;
797 *outkey
= (char *)malloc(*outkeylen
+1);
798 bcopy(yprkv
.key
.keydat_val
, *outkey
, *outkeylen
);
799 (*outkey
)[*outkeylen
] = '\0';
800 *outvallen
= yprkv
.val
.valdat_len
;
801 *outval
= (char *)malloc(*outvallen
+1);
802 bcopy(yprkv
.val
.valdat_val
, *outval
, *outvallen
);
803 (*outval
)[*outvallen
] = '\0';
806 xdr_free(xdr_ypresp_key_val
, (char *)&yprkv
);
811 yp_all(char *indomain
, char *inmap
, struct ypall_callback
*incallback
)
813 struct ypreq_nokey yprnk
;
814 struct dom_binding
*ysd
;
816 struct sockaddr_in clnt_sin
;
818 u_long status
, savstat
;
823 if (indomain
== NULL
|| !strlen(indomain
) ||
824 inmap
== NULL
|| !strlen(inmap
))
825 return (YPERR_BADARGS
);
829 if (_yp_dobind(indomain
, &ysd
) != 0)
830 return (YPERR_DOMAIN
);
832 tv
.tv_sec
= _yplib_timeout
;
835 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
837 clnt_sock
= RPC_ANYSOCK
;
838 clnt_sin
= ysd
->dom_server_addr
;
839 clnt_sin
.sin_port
= 0;
840 clnt
= clnttcp_create(&clnt_sin
, YPPROG
, YPVERS
, &clnt_sock
, 0, 0);
842 printf("clnttcp_create failed\n");
846 yprnk
.domain
= indomain
;
848 ypresp_allfn
= incallback
->foreach
;
849 ypresp_data
= (void *)incallback
->data
;
851 if (clnt_call(clnt
, YPPROC_ALL
,
852 xdr_ypreq_nokey
, &yprnk
,
853 xdr_ypresp_all_seq
, &status
, tv
) != RPC_SUCCESS
) {
854 clnt_perror(ysd
->dom_client
, "yp_all: clnt_call");
862 xdr_free(xdr_ypresp_all_seq
, (char *)&status
); /* not really needed... */
863 if (savstat
!= YP_NOMORE
)
864 return (ypprot_err(savstat
));
869 yp_order(char *indomain
, char *inmap
, int *outorder
)
871 struct dom_binding
*ysd
;
872 struct ypresp_order ypro
;
873 struct ypreq_nokey yprnk
;
879 if (indomain
== NULL
|| !strlen(indomain
) ||
880 inmap
== NULL
|| !strlen(inmap
))
881 return (YPERR_BADARGS
);
884 if (_yp_dobind(indomain
, &ysd
) != 0)
885 return (YPERR_DOMAIN
);
887 tv
.tv_sec
= _yplib_timeout
;
890 yprnk
.domain
= indomain
;
893 bzero((char *)(char *)&ypro
, sizeof ypro
);
895 r
= clnt_call(ysd
->dom_client
, YPPROC_ORDER
,
896 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_order
, &ypro
, tv
);
899 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
902 if (r
== RPC_PROCUNAVAIL
) {
906 if (r
!= RPC_SUCCESS
) {
907 clnt_perror(ysd
->dom_client
, "yp_order: clnt_call");
912 if (!(r
= ypprot_err(ypro
.stat
))) {
913 *outorder
= ypro
.ordernum
;
916 xdr_free(xdr_ypresp_order
, (char *)&ypro
);
921 yp_master(char *indomain
, char *inmap
, char **outname
)
923 struct dom_binding
*ysd
;
924 struct ypresp_master yprm
;
925 struct ypreq_nokey yprnk
;
931 if (indomain
== NULL
|| !strlen(indomain
) ||
932 inmap
== NULL
|| !strlen(inmap
))
933 return (YPERR_BADARGS
);
935 if (_yp_dobind(indomain
, &ysd
) != 0)
936 return (YPERR_DOMAIN
);
938 tv
.tv_sec
= _yplib_timeout
;
941 yprnk
.domain
= indomain
;
944 bzero((char *)&yprm
, sizeof yprm
);
946 r
= clnt_call(ysd
->dom_client
, YPPROC_MASTER
,
947 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_master
, &yprm
, tv
);
948 if (r
!= RPC_SUCCESS
) {
949 clnt_perror(ysd
->dom_client
, "yp_master: clnt_call");
954 if (!(r
= ypprot_err(yprm
.stat
))) {
955 *outname
= (char *)strdup(yprm
.peer
);
958 xdr_free(xdr_ypresp_master
, (char *)&yprm
);
963 yp_maplist(char *indomain
, struct ypmaplist
**outmaplist
)
965 struct dom_binding
*ysd
;
966 struct ypresp_maplist ypml
;
972 if (indomain
== NULL
|| !strlen(indomain
))
973 return (YPERR_BADARGS
);
976 if (_yp_dobind(indomain
, &ysd
) != 0)
977 return (YPERR_DOMAIN
);
979 tv
.tv_sec
= _yplib_timeout
;
982 bzero((char *)&ypml
, sizeof ypml
);
984 r
= clnt_call(ysd
->dom_client
, YPPROC_MAPLIST
,
985 xdr_domainname
,(char *)&indomain
,xdr_ypresp_maplist
,&ypml
,tv
);
986 if (r
!= RPC_SUCCESS
) {
987 clnt_perror(ysd
->dom_client
, "yp_maplist: clnt_call");
991 if (!(r
= ypprot_err(ypml
.stat
))) {
992 *outmaplist
= ypml
.maps
;
995 /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
1000 yperr_string(int incode
)
1003 static char err
[80];
1007 errstr
= "Success";;
1010 errstr
= "Request arguments bad";
1013 errstr
= "RPC failure";
1016 errstr
= "Can't bind to server which serves this domain";
1019 errstr
= "No such map in server's domain";
1022 errstr
= "No such key in map";
1025 errstr
= "YP server error";
1028 errstr
= "Local resource allocation failure";
1031 errstr
= "No more records in map database";
1034 errstr
= "Can't communicate with portmapper";
1037 errstr
= "Can't communicate with ypbind";
1040 errstr
= "Can't communicate with ypserv";
1043 errstr
= "Local domain name not set";
1046 errstr
= "Server data base is bad";
1049 errstr
= "YP server version mismatch - server can't supply service.";
1052 errstr
= "Access violation";
1055 errstr
= "Database is busy";
1062 strlcpy(err
, errstr
, sizeof(err
));
1064 snprintf(err
, sizeof(err
), "YP unknown error %d\n", incode
);
1069 ypprot_err(unsigned int incode
)
1075 return (YPERR_YPBIND
);
1077 return (YPERR_NOMORE
);
1081 return (YPERR_DOMAIN
);
1085 return (YPERR_YPERR
);
1087 return (YPERR_BADDB
);
1089 return (YPERR_YPERR
);
1091 return (YPERR_BADARGS
);
1093 return (YPERR_VERS
);
1095 return (YPERR_YPERR
);
1099 _yp_check(char **dom
)
1103 if (_yp_domain
[0]=='\0')
1104 if (yp_get_default_domain(&unused
))
1110 if (yp_bind(_yp_domain
) == 0) {
1111 yp_unbind(_yp_domain
);