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.5 2005/04/25 18:52:28 joerg 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 **);
54 * We have to define these here due to clashes between yp_prot.h and
66 struct ypmatch_ent *ypc_next;
68 #define YPLIB_MAXCACHE 5 /* At most 5 entries */
69 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */
73 struct dom_binding *dom_pnext;
74 char dom_domain[YPMAXDOMAIN + 1];
75 struct sockaddr_in dom_server_addr;
76 u_short dom_server_port;
79 u_short dom_local_port; /* now I finally know what this is for. */
82 struct ypmatch_ent *cache;
87 #include <rpcsvc/yp.h>
88 #include <rpcsvc/ypclnt.h>
91 #define BINDINGDIR "/var/yp/binding"
93 #define MAX_RETRIES 20
97 static void _yp_unbind(struct dom_binding *);
98 struct dom_binding *_ypbindlist;
99 static char _yp_domain[MAXHOSTNAMELEN];
100 int _yplib_timeout = 10;
104 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
105 struct ypmatch_ent *cur)
108 ypdb->cache = cur->ypc_next;
110 prev->ypc_next = cur->ypc_next;
113 free(cur->ypc_key.keydat_val);
114 free(cur->ypc_val.valdat_val);
117 ypdb->ypmatch_cachecnt--;
123 ypmatch_cache_flush(struct dom_binding *ypdb)
125 struct ypmatch_ent *n, *c = ypdb->cache;
129 ypmatch_cache_delete(ypdb, NULL, c);
137 ypmatch_cache_expire(struct dom_binding *ypdb)
139 struct ypmatch_ent *c = ypdb->cache;
140 struct ypmatch_ent *n, *p = NULL;
146 if (t >= c->ypc_expire_t) {
148 ypmatch_cache_delete(ypdb, p, c);
160 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
163 struct ypmatch_ent *new;
165 /* Do an expire run to maybe open up a slot. */
166 if (ypdb->ypmatch_cachecnt)
167 ypmatch_cache_expire(ypdb);
170 * If there are no slots free, then force an expire of
171 * the least recently used entry.
173 if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
174 struct ypmatch_ent *o = NULL, *c = ypdb->cache;
180 if (c->ypc_expire_t < oldest) {
181 oldest = c->ypc_expire_t;
190 ypmatch_cache_expire(ypdb);
193 new = malloc(sizeof(struct ypmatch_ent));
197 new->ypc_map = strdup(map);
198 if (new->ypc_map == NULL) {
202 new->ypc_key.keydat_val = malloc(key->keydat_len);
203 if (new->ypc_key.keydat_val == NULL) {
208 new->ypc_val.valdat_val = malloc(val->valdat_len);
209 if (new->ypc_val.valdat_val == NULL) {
210 free(new->ypc_val.valdat_val);
216 new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE;
217 new->ypc_key.keydat_len = key->keydat_len;
218 new->ypc_val.valdat_len = val->valdat_len;
219 bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len);
220 bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len);
222 new->ypc_next = ypdb->cache;
225 ypdb->ypmatch_cachecnt++;
231 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
234 struct ypmatch_ent *c = ypdb->cache;
236 ypmatch_cache_expire(ypdb);
238 for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
239 if (strcmp(map, c->ypc_map))
241 if (key->keydat_len != c->ypc_key.keydat_len)
243 if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
251 val->valdat_len = c->ypc_val.valdat_len;
252 val->valdat_val = c->ypc_val.valdat_val;
259 ypbinderr_string(int incode)
268 errstr = "Internal ypbind error";
270 case YPBIND_ERR_NOSERV:
271 errstr = "Domain not bound";
273 case YPBIND_ERR_RESC:
274 errstr = "System resource allocation failure";
281 strlcpy(err, errstr, sizeof(err));
283 snprintf(err, sizeof(err), "Unknown ypbind error: #%d\n", incode);
288 _yp_dobind(const char *dom, struct dom_binding **ypdb)
290 static pid_t pid = -1;
291 char path[MAXPATHLEN];
292 struct dom_binding *ysd, *ysd2;
293 struct ypbind_resp ypbr;
295 struct sockaddr_in clnt_sin;
302 struct sockaddr_in check;
303 int checklen = sizeof(struct sockaddr_in);
305 /* Not allowed; bad doggie. Bad. */
306 if (strchr(dom, '/') != NULL)
307 return(YPERR_BADARGS);
310 if (!(pid == -1 || pid == gpid)) {
313 if (ysd->dom_client != NULL)
315 ysd2 = ysd->dom_pnext;
326 if (dom == NULL || strlen(dom) == 0)
327 return (YPERR_BADARGS);
329 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
330 if (strcmp(dom, ysd->dom_domain) == 0)
335 ysd = (struct dom_binding *)malloc(sizeof *ysd);
336 bzero((char *)ysd, sizeof *ysd);
337 ysd->dom_socket = -1;
341 /* Check the socket -- may have been hosed by the caller. */
342 if (_getsockname(ysd->dom_socket, (struct sockaddr *)&check,
343 &checklen) == -1 || check.sin_family != AF_INET ||
344 check.sin_port != ysd->dom_local_port) {
345 /* Socket became bogus somehow... need to rebind. */
348 sock = ysd->dom_socket;
349 save = _dup(ysd->dom_socket);
350 if (ysd->dom_client != NULL)
351 clnt_destroy(ysd->dom_client);
353 ysd->dom_client = NULL;
354 sock = _dup2(save, sock);
361 if (retries > MAX_RETRIES) {
364 return(YPERR_YPBIND);
367 if (ysd->dom_vers == 0) {
369 * We're trying to make a new binding: zorch the
370 * existing handle now (if any).
372 if (ysd->dom_client != NULL) {
373 clnt_destroy(ysd->dom_client);
374 ysd->dom_client = NULL;
375 ysd->dom_socket = -1;
377 snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2);
378 if ((fd = _open(path, O_RDONLY)) == -1) {
379 /* no binding file, YP is dead. */
380 /* Try to bring it back to life. */
384 if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
386 struct ypbind_resp ybr;
389 iov[0].iov_base = (caddr_t)&ypb_port;
390 iov[0].iov_len = sizeof ypb_port;
391 iov[1].iov_base = (caddr_t)&ybr;
392 iov[1].iov_len = sizeof ybr;
394 r = _readv(fd, iov, 2);
395 if (r != (ssize_t)(iov[0].iov_len + iov[1].iov_len)) {
401 bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
402 ysd->dom_server_addr.sin_family = AF_INET;
403 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
404 ysd->dom_server_addr.sin_addr.s_addr =
405 *(u_long *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
406 ysd->dom_server_addr.sin_port =
407 *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
409 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
413 /* no lock on binding file, YP is dead. */
414 /* Try to bring it back to life. */
421 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
423 * We're trying to make a new binding: zorch the
424 * existing handle now (if any).
426 if (ysd->dom_client != NULL) {
427 clnt_destroy(ysd->dom_client);
428 ysd->dom_client = NULL;
429 ysd->dom_socket = -1;
431 bzero((char *)&clnt_sin, sizeof clnt_sin);
432 clnt_sin.sin_family = AF_INET;
433 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
435 clnt_sock = RPC_ANYSOCK;
436 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
438 if (client == NULL) {
440 * These conditions indicate ypbind just isn't
441 * alive -- we probably don't want to shoot our
442 * mouth off in this case; instead generate error
443 * messages only for really exotic problems.
445 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
446 (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
447 rpc_createerr.cf_error.re_errno == ECONNREFUSED))
448 clnt_pcreateerror("clnttcp_create");
451 return (YPERR_YPBIND);
455 * Check the port number -- should be < IPPORT_RESERVED.
456 * If not, it's possible someone has registered a bogus
457 * ypbind with the portmapper and is trying to trick us.
459 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
461 clnt_destroy(client);
464 return(YPERR_YPBIND);
466 tv.tv_sec = _yplib_timeout/2;
468 r = clnt_call(client, YPBINDPROC_DOMAIN,
469 xdr_domainname, (char *)&dom, xdr_ypbind_resp, &ypbr, tv);
470 if (r != RPC_SUCCESS) {
471 clnt_destroy(client);
473 if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
476 return(YPERR_YPBIND);
479 "YP: server for domain %s not responding, retrying\n", dom);
482 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
483 struct timespec time_to_sleep, time_remaining;
485 clnt_destroy(client);
488 time_to_sleep.tv_sec = _yplib_timeout/2;
489 time_to_sleep.tv_nsec = 0;
490 _nanosleep(&time_to_sleep,
495 clnt_destroy(client);
497 bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
498 ysd->dom_server_addr.sin_family = AF_INET;
499 ysd->dom_server_addr.sin_port =
500 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
501 ysd->dom_server_addr.sin_addr.s_addr =
502 *(u_long *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
505 * We could do a reserved port check here too, but this
506 * could pose compatibility problems. The local ypbind is
507 * supposed to decide whether or not to trust yp servers
508 * on insecure ports. For now, we trust its judgement.
510 ysd->dom_server_port =
511 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
513 ysd->dom_vers = YPVERS;
514 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
517 /* Don't rebuild the connection to the server unless we have to. */
518 if (ysd->dom_client == NULL) {
519 tv.tv_sec = _yplib_timeout/2;
521 ysd->dom_socket = RPC_ANYSOCK;
522 ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr,
523 YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304);
524 if (ysd->dom_client == NULL) {
525 clnt_pcreateerror("clntudp_create");
529 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
530 perror("fcntl: F_SETFD");
532 * We want a port number associated with this socket
533 * so that we can check its authenticity later.
535 checklen = sizeof(struct sockaddr_in);
536 bzero((char *)&check, checklen);
537 _bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
538 check.sin_family = AF_INET;
539 if (!_getsockname(ysd->dom_socket,
540 (struct sockaddr *)&check, &checklen)) {
541 ysd->dom_local_port = check.sin_port;
543 clnt_destroy(ysd->dom_client);
546 return(YPERR_YPBIND);
551 ysd->dom_pnext = _ypbindlist;
561 _yp_unbind(struct dom_binding *ypb)
563 struct sockaddr_in check;
564 int checklen = sizeof(struct sockaddr_in);
566 if (ypb->dom_client != NULL) {
567 /* Check the socket -- may have been hosed by the caller. */
568 if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check,
569 &checklen) == -1 || check.sin_family != AF_INET ||
570 check.sin_port != ypb->dom_local_port) {
573 sock = ypb->dom_socket;
574 save = _dup(ypb->dom_socket);
575 clnt_destroy(ypb->dom_client);
576 sock = _dup2(save, sock);
579 clnt_destroy(ypb->dom_client);
582 ypb->dom_client = NULL;
583 ypb->dom_socket = -1;
586 ypmatch_cache_flush(ypb);
593 return (_yp_dobind(dom, NULL));
599 struct dom_binding *ypb, *ypbp;
602 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
603 if (strcmp(dom, ypb->dom_domain) == 0) {
606 ypbp->dom_pnext = ypb->dom_pnext;
608 _ypbindlist = ypb->dom_pnext;
618 yp_match(const char *indomain, const char *inmap, const char *inkey, int inkeylen,
619 char **outval, int *outvallen)
621 struct dom_binding *ysd;
622 struct ypresp_val yprv;
624 struct ypreq_key yprk;
632 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
633 inmap == NULL || !strlen(inmap) ||
634 indomain == NULL || !strlen(indomain))
635 return (YPERR_BADARGS);
637 if (_yp_dobind(indomain, &ysd) != 0)
638 return(YPERR_DOMAIN);
640 yprk.domain = indomain;
642 yprk.key.keydat_val = (char *)inkey;
643 yprk.key.keydat_len = inkeylen;
646 if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
648 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
649 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
651 *outvallen = yprv.val.valdat_len;
652 *outval = (char *)malloc(*outvallen+1);
653 bcopy(yprv.val.valdat_val, *outval, *outvallen);
654 (*outval)[*outvallen] = '\0';
660 if (_yp_dobind(indomain, &ysd) != 0)
661 return (YPERR_DOMAIN);
663 tv.tv_sec = _yplib_timeout;
666 bzero((char *)&yprv, sizeof yprv);
668 r = clnt_call(ysd->dom_client, YPPROC_MATCH,
669 xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
670 if (r != RPC_SUCCESS) {
671 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
676 if (!(r = ypprot_err(yprv.stat))) {
677 *outvallen = yprv.val.valdat_len;
678 *outval = (char *)malloc(*outvallen+1);
679 bcopy(yprv.val.valdat_val, *outval, *outvallen);
680 (*outval)[*outvallen] = '\0';
682 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
686 xdr_free(xdr_ypresp_val, (char *)&yprv);
691 yp_get_default_domain(char **domp)
694 if (_yp_domain[0] == '\0')
695 if (getdomainname(_yp_domain, sizeof _yp_domain))
696 return (YPERR_NODOM);
702 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
703 char **outval, int *outvallen)
705 struct ypresp_key_val yprkv;
706 struct ypreq_nokey yprnk;
707 struct dom_binding *ysd;
713 if (indomain == NULL || !strlen(indomain) ||
714 inmap == NULL || !strlen(inmap))
715 return (YPERR_BADARGS);
717 *outkey = *outval = NULL;
718 *outkeylen = *outvallen = 0;
721 if (_yp_dobind(indomain, &ysd) != 0)
722 return (YPERR_DOMAIN);
724 tv.tv_sec = _yplib_timeout;
727 yprnk.domain = indomain;
729 bzero((char *)&yprkv, sizeof yprkv);
731 r = clnt_call(ysd->dom_client, YPPROC_FIRST,
732 xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
733 if (r != RPC_SUCCESS) {
734 clnt_perror(ysd->dom_client, "yp_first: clnt_call");
738 if (!(r = ypprot_err(yprkv.stat))) {
739 *outkeylen = yprkv.key.keydat_len;
740 *outkey = (char *)malloc(*outkeylen+1);
741 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
742 (*outkey)[*outkeylen] = '\0';
743 *outvallen = yprkv.val.valdat_len;
744 *outval = (char *)malloc(*outvallen+1);
745 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
746 (*outval)[*outvallen] = '\0';
749 xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
754 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
755 char **outkey, int *outkeylen, char **outval, int *outvallen)
757 struct ypresp_key_val yprkv;
758 struct ypreq_key yprk;
759 struct dom_binding *ysd;
765 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
766 inmap == NULL || !strlen(inmap) ||
767 indomain == NULL || !strlen(indomain))
768 return (YPERR_BADARGS);
770 *outkey = *outval = NULL;
771 *outkeylen = *outvallen = 0;
774 if (_yp_dobind(indomain, &ysd) != 0)
775 return (YPERR_DOMAIN);
777 tv.tv_sec = _yplib_timeout;
780 yprk.domain = indomain;
782 yprk.key.keydat_val = inkey;
783 yprk.key.keydat_len = inkeylen;
784 bzero((char *)&yprkv, sizeof yprkv);
786 r = clnt_call(ysd->dom_client, YPPROC_NEXT,
787 xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
788 if (r != RPC_SUCCESS) {
789 clnt_perror(ysd->dom_client, "yp_next: clnt_call");
793 if (!(r = ypprot_err(yprkv.stat))) {
794 *outkeylen = yprkv.key.keydat_len;
795 *outkey = (char *)malloc(*outkeylen+1);
796 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
797 (*outkey)[*outkeylen] = '\0';
798 *outvallen = yprkv.val.valdat_len;
799 *outval = (char *)malloc(*outvallen+1);
800 bcopy(yprkv.val.valdat_val, *outval, *outvallen);
801 (*outval)[*outvallen] = '\0';
804 xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
809 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
811 struct ypreq_nokey yprnk;
812 struct dom_binding *ysd;
814 struct sockaddr_in clnt_sin;
816 u_long status, savstat;
821 if (indomain == NULL || !strlen(indomain) ||
822 inmap == NULL || !strlen(inmap))
823 return (YPERR_BADARGS);
827 if (_yp_dobind(indomain, &ysd) != 0)
828 return (YPERR_DOMAIN);
830 tv.tv_sec = _yplib_timeout;
833 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */
835 clnt_sock = RPC_ANYSOCK;
836 clnt_sin = ysd->dom_server_addr;
837 clnt_sin.sin_port = 0;
838 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
840 printf("clnttcp_create failed\n");
844 yprnk.domain = indomain;
846 ypresp_data = (void *)incallback->data;
848 if (clnt_call(clnt, YPPROC_ALL,
849 xdr_ypreq_nokey, &yprnk,
850 xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
851 clnt_perror(ysd->dom_client, "yp_all: clnt_call");
859 xdr_free(xdr_ypresp_all_seq, (char *)&status); /* not really needed... */
860 if (savstat != YP_NOMORE)
861 return (ypprot_err(savstat));
866 yp_order(char *indomain, char *inmap, int *outorder)
868 struct dom_binding *ysd;
869 struct ypresp_order ypro;
870 struct ypreq_nokey yprnk;
876 if (indomain == NULL || !strlen(indomain) ||
877 inmap == NULL || !strlen(inmap))
878 return (YPERR_BADARGS);
881 if (_yp_dobind(indomain, &ysd) != 0)
882 return (YPERR_DOMAIN);
884 tv.tv_sec = _yplib_timeout;
887 yprnk.domain = indomain;
890 bzero((char *)(char *)&ypro, sizeof ypro);
892 r = clnt_call(ysd->dom_client, YPPROC_ORDER,
893 xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
896 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
899 if (r == RPC_PROCUNAVAIL) {
903 if (r != RPC_SUCCESS) {
904 clnt_perror(ysd->dom_client, "yp_order: clnt_call");
909 if (!(r = ypprot_err(ypro.stat))) {
910 *outorder = ypro.ordernum;
913 xdr_free(xdr_ypresp_order, (char *)&ypro);
918 yp_master(char *indomain, char *inmap, char **outname)
920 struct dom_binding *ysd;
921 struct ypresp_master yprm;
922 struct ypreq_nokey yprnk;
928 if (indomain == NULL || !strlen(indomain) ||
929 inmap == NULL || !strlen(inmap))
930 return (YPERR_BADARGS);
932 if (_yp_dobind(indomain, &ysd) != 0)
933 return (YPERR_DOMAIN);
935 tv.tv_sec = _yplib_timeout;
938 yprnk.domain = indomain;
941 bzero((char *)&yprm, sizeof yprm);
943 r = clnt_call(ysd->dom_client, YPPROC_MASTER,
944 xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
945 if (r != RPC_SUCCESS) {
946 clnt_perror(ysd->dom_client, "yp_master: clnt_call");
951 if (!(r = ypprot_err(yprm.stat))) {
952 *outname = (char *)strdup(yprm.peer);
955 xdr_free(xdr_ypresp_master, (char *)&yprm);
961 yp_maplist(char *indomain, struct ypmaplist **outmaplist)
963 struct dom_binding *ysd;
964 struct ypresp_maplist ypml;
970 if (indomain == NULL || !strlen(indomain))
971 return (YPERR_BADARGS);
974 if (_yp_dobind(indomain, &ysd) != 0)
975 return (YPERR_DOMAIN);
977 tv.tv_sec = _yplib_timeout;
980 bzero((char *)&ypml, sizeof ypml);
982 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
983 xdr_domainname,(char *)&indomain,xdr_ypresp_maplist,&ypml,tv);
984 if (r != RPC_SUCCESS) {
985 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
989 if (!(r = ypprot_err(ypml.stat))) {
990 *outmaplist = ypml.maps;
993 /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
999 yperr_string(int incode)
1002 static char err[80];
1006 errstr = "Success";;
1009 errstr = "Request arguments bad";
1012 errstr = "RPC failure";
1015 errstr = "Can't bind to server which serves this domain";
1018 errstr = "No such map in server's domain";
1021 errstr = "No such key in map";
1024 errstr = "YP server error";
1027 errstr = "Local resource allocation failure";
1030 errstr = "No more records in map database";
1033 errstr = "Can't communicate with portmapper";
1036 errstr = "Can't communicate with ypbind";
1039 errstr = "Can't communicate with ypserv";
1042 errstr = "Local domain name not set";
1045 errstr = "Server data base is bad";
1048 errstr = "YP server version mismatch - server can't supply service.";
1051 errstr = "Access violation";
1054 errstr = "Database is busy";
1061 strlcpy(err, errstr, sizeof(err));
1063 snprintf(err, sizeof(err), "YP unknown error %d\n", incode);
1068 ypprot_err(unsigned int incode)
1074 return (YPERR_YPBIND);
1076 return (YPERR_NOMORE);
1080 return (YPERR_DOMAIN);
1084 return (YPERR_YPERR);
1086 return (YPERR_BADDB);
1088 return (YPERR_YPERR);
1090 return (YPERR_BADARGS);
1092 return (YPERR_VERS);
1094 return (YPERR_YPERR);
1098 _yp_check(char **dom)
1102 if (_yp_domain[0]=='\0')
1103 if (yp_get_default_domain(&unused))
1109 if (yp_bind(_yp_domain) == 0) {
1110 yp_unbind(_yp_domain);