xref: /freebsd/lib/libc/yp/yplib.c (revision ccbcef60f5398d103aa40055a030cf684bdb85d6)
14415cd19SGarrett Wollman /*
24415cd19SGarrett Wollman  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
34415cd19SGarrett Wollman  * All rights reserved.
44415cd19SGarrett Wollman  *
54415cd19SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
64415cd19SGarrett Wollman  * modification, are permitted provided that the following conditions
74415cd19SGarrett Wollman  * are met:
84415cd19SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
94415cd19SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
104415cd19SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
114415cd19SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
124415cd19SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
134415cd19SGarrett Wollman  * 3. The name of the author may not be used to endorse or promote
144415cd19SGarrett Wollman  *    products derived from this software without specific prior written
154415cd19SGarrett Wollman  *    permission.
164415cd19SGarrett Wollman  *
174415cd19SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
184415cd19SGarrett Wollman  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
194415cd19SGarrett Wollman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204415cd19SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
214415cd19SGarrett Wollman  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224415cd19SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234415cd19SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244415cd19SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254415cd19SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264415cd19SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274415cd19SGarrett Wollman  * SUCH DAMAGE.
284415cd19SGarrett Wollman  */
294415cd19SGarrett Wollman 
304415cd19SGarrett Wollman #ifndef LINT
31ccbcef60SJames Raynard static char *rcsid = "$Id: yplib.c,v 1.21 1996/06/03 13:19:10 jraynard Exp $";
324415cd19SGarrett Wollman #endif
334415cd19SGarrett Wollman 
344415cd19SGarrett Wollman #include <sys/param.h>
354415cd19SGarrett Wollman #include <sys/types.h>
364415cd19SGarrett Wollman #include <sys/socket.h>
374415cd19SGarrett Wollman #include <sys/file.h>
384415cd19SGarrett Wollman #include <sys/uio.h>
394415cd19SGarrett Wollman #include <errno.h>
404415cd19SGarrett Wollman #include <stdio.h>
414415cd19SGarrett Wollman #include <string.h>
42b9729ac2SBill Paul #include <stdlib.h>
43b9729ac2SBill Paul #include <unistd.h>
444415cd19SGarrett Wollman #include <rpc/rpc.h>
454415cd19SGarrett Wollman #include <rpc/xdr.h>
460e276383SBill Paul #include <rpcsvc/yp.h>
470e276383SBill Paul 
480e276383SBill Paul 
490e276383SBill Paul /*
500e276383SBill Paul  * We have to define these here due to clashes between yp_prot.h and
510e276383SBill Paul  * yp.h.
520e276383SBill Paul  */
530e276383SBill Paul 
540e276383SBill Paul struct dom_binding {
550e276383SBill Paul 	struct dom_binding *dom_pnext;
560e276383SBill Paul 	char dom_domain[YPMAXDOMAIN + 1];
570e276383SBill Paul 	struct sockaddr_in dom_server_addr;
580e276383SBill Paul 	u_short dom_server_port;
590e276383SBill Paul 	int dom_socket;
600e276383SBill Paul 	CLIENT *dom_client;
61a2308772SBill Paul 	u_short dom_local_port;	/* now I finally know what this is for. */
620e276383SBill Paul 	long dom_vers;
630e276383SBill Paul };
640e276383SBill Paul 
654415cd19SGarrett Wollman #include <rpcsvc/ypclnt.h>
664415cd19SGarrett Wollman 
674415cd19SGarrett Wollman #ifndef BINDINGDIR
684415cd19SGarrett Wollman #define BINDINGDIR "/var/yp/binding"
694415cd19SGarrett Wollman #endif
704415cd19SGarrett Wollman #define YPMATCHCACHE
71456ebbf8SBill Paul #define MAX_RETRIES 20
724415cd19SGarrett Wollman 
734415cd19SGarrett Wollman extern bool_t xdr_domainname(), xdr_ypbind_resp();
744415cd19SGarrett Wollman extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
754415cd19SGarrett Wollman extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
764415cd19SGarrett Wollman extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
774415cd19SGarrett Wollman extern bool_t xdr_ypresp_master();
784415cd19SGarrett Wollman 
794415cd19SGarrett Wollman int (*ypresp_allfn)();
804415cd19SGarrett Wollman void *ypresp_data;
814415cd19SGarrett Wollman 
824415cd19SGarrett Wollman struct dom_binding *_ypbindlist;
834415cd19SGarrett Wollman static char _yp_domain[MAXHOSTNAMELEN];
844415cd19SGarrett Wollman int _yplib_timeout = 10;
854415cd19SGarrett Wollman 
864415cd19SGarrett Wollman #ifdef YPMATCHCACHE
874415cd19SGarrett Wollman int _yplib_cache = 5;
884415cd19SGarrett Wollman 
894415cd19SGarrett Wollman static struct ypmatch_ent {
904415cd19SGarrett Wollman 	struct ypmatch_ent *next;
914415cd19SGarrett Wollman 	char *map, *key, *val;
924415cd19SGarrett Wollman 	int keylen, vallen;
934415cd19SGarrett Wollman 	time_t expire_t;
944415cd19SGarrett Wollman } *ypmc;
954415cd19SGarrett Wollman 
964415cd19SGarrett Wollman static void
974415cd19SGarrett Wollman ypmatch_add(map, key, keylen, val, vallen)
984415cd19SGarrett Wollman 	char *map;
994415cd19SGarrett Wollman 	char *key;
1004415cd19SGarrett Wollman 	int keylen;
1014415cd19SGarrett Wollman 	char *val;
1024415cd19SGarrett Wollman 	int vallen;
1034415cd19SGarrett Wollman {
1044415cd19SGarrett Wollman 	struct ypmatch_ent *ep;
1054415cd19SGarrett Wollman 	time_t t;
1064415cd19SGarrett Wollman 
1074415cd19SGarrett Wollman 	time(&t);
1084415cd19SGarrett Wollman 
1094415cd19SGarrett Wollman 	for(ep=ypmc; ep; ep=ep->next)
1104415cd19SGarrett Wollman 		if(ep->expire_t < t)
1114415cd19SGarrett Wollman 			break;
1124415cd19SGarrett Wollman 	if(ep==NULL) {
1134415cd19SGarrett Wollman 		ep = (struct ypmatch_ent *)malloc(sizeof *ep);
1144415cd19SGarrett Wollman 		bzero((char *)ep, sizeof *ep);
1154415cd19SGarrett Wollman 		if(ypmc)
1164415cd19SGarrett Wollman 			ep->next = ypmc;
1174415cd19SGarrett Wollman 		ypmc = ep;
1184415cd19SGarrett Wollman 	}
1194415cd19SGarrett Wollman 
1204415cd19SGarrett Wollman 	if(ep->key)
1214415cd19SGarrett Wollman 		free(ep->key);
1224415cd19SGarrett Wollman 	if(ep->val)
1234415cd19SGarrett Wollman 		free(ep->val);
1244415cd19SGarrett Wollman 
1254415cd19SGarrett Wollman 	ep->key = NULL;
1264415cd19SGarrett Wollman 	ep->val = NULL;
1274415cd19SGarrett Wollman 
1284415cd19SGarrett Wollman 	ep->key = (char *)malloc(keylen);
1294415cd19SGarrett Wollman 	if(ep->key==NULL)
1304415cd19SGarrett Wollman 		return;
1314415cd19SGarrett Wollman 
1324415cd19SGarrett Wollman 	ep->val = (char *)malloc(vallen);
1334415cd19SGarrett Wollman 	if(ep->key==NULL) {
1344415cd19SGarrett Wollman 		free(ep->key);
1354415cd19SGarrett Wollman 		ep->key = NULL;
1364415cd19SGarrett Wollman 		return;
1374415cd19SGarrett Wollman 	}
1384415cd19SGarrett Wollman 	ep->keylen = keylen;
1394415cd19SGarrett Wollman 	ep->vallen = vallen;
1404415cd19SGarrett Wollman 
1414415cd19SGarrett Wollman 	bcopy(key, ep->key, ep->keylen);
1424415cd19SGarrett Wollman 	bcopy(val, ep->val, ep->vallen);
1434415cd19SGarrett Wollman 
1444415cd19SGarrett Wollman 	if(ep->map) {
1454415cd19SGarrett Wollman 		if( strcmp(ep->map, map) ) {
1464415cd19SGarrett Wollman 			free(ep->map);
1474415cd19SGarrett Wollman 			ep->map = strdup(map);
1484415cd19SGarrett Wollman 		}
1494415cd19SGarrett Wollman 	} else {
1504415cd19SGarrett Wollman 		ep->map = strdup(map);
1514415cd19SGarrett Wollman 	}
1524415cd19SGarrett Wollman 
1534415cd19SGarrett Wollman 	ep->expire_t = t + _yplib_cache;
1544415cd19SGarrett Wollman }
1554415cd19SGarrett Wollman 
1564415cd19SGarrett Wollman static bool_t
1574415cd19SGarrett Wollman ypmatch_find(map, key, keylen, val, vallen)
1584415cd19SGarrett Wollman 	char *map;
1594415cd19SGarrett Wollman 	char *key;
1604415cd19SGarrett Wollman 	int keylen;
1614415cd19SGarrett Wollman 	char **val;
1624415cd19SGarrett Wollman 	int *vallen;
1634415cd19SGarrett Wollman {
1644415cd19SGarrett Wollman 	struct ypmatch_ent *ep;
1654415cd19SGarrett Wollman 	time_t t;
1664415cd19SGarrett Wollman 
1674415cd19SGarrett Wollman 	if(ypmc==NULL)
1684415cd19SGarrett Wollman 		return 0;
1694415cd19SGarrett Wollman 
1704415cd19SGarrett Wollman 	time(&t);
1714415cd19SGarrett Wollman 
1724415cd19SGarrett Wollman 	for(ep=ypmc; ep; ep=ep->next) {
1734415cd19SGarrett Wollman 		if(ep->keylen != keylen)
1744415cd19SGarrett Wollman 			continue;
1754415cd19SGarrett Wollman 		if(strcmp(ep->map, map))
1764415cd19SGarrett Wollman 			continue;
1774415cd19SGarrett Wollman 		if(bcmp(ep->key, key, keylen))
1784415cd19SGarrett Wollman 			continue;
1794415cd19SGarrett Wollman 		if(t > ep->expire_t)
1804415cd19SGarrett Wollman 			continue;
1814415cd19SGarrett Wollman 
1824415cd19SGarrett Wollman 		*val = ep->val;
1834415cd19SGarrett Wollman 		*vallen = ep->vallen;
1844415cd19SGarrett Wollman 		return 1;
1854415cd19SGarrett Wollman 	}
1864415cd19SGarrett Wollman 	return 0;
1874415cd19SGarrett Wollman }
1884415cd19SGarrett Wollman #endif
1894415cd19SGarrett Wollman 
19064416168SBill Paul char *
19164416168SBill Paul ypbinderr_string(incode)
19264416168SBill Paul 	int incode;
19364416168SBill Paul {
19464416168SBill Paul 	static char err[80];
19564416168SBill Paul 	switch(incode) {
19664416168SBill Paul 	case 0:
19764416168SBill Paul 		return "Success";
19862967ca2SBill Paul 	case YPBIND_ERR_ERR:
19964416168SBill Paul 		return "Internal ypbind error";
20062967ca2SBill Paul 	case YPBIND_ERR_NOSERV:
20164416168SBill Paul 		return "Domain not bound";
20262967ca2SBill Paul 	case YPBIND_ERR_RESC:
20364416168SBill Paul 		return "System resource allocation failure";
20464416168SBill Paul 	}
20562967ca2SBill Paul 	sprintf(err, "Unknown ypbind error: #%d\n", incode);
20664416168SBill Paul 	return err;
20764416168SBill Paul }
20864416168SBill Paul 
2094415cd19SGarrett Wollman int
2104415cd19SGarrett Wollman _yp_dobind(dom, ypdb)
2114415cd19SGarrett Wollman 	char *dom;
2124415cd19SGarrett Wollman 	struct dom_binding **ypdb;
2134415cd19SGarrett Wollman {
214e75ad74aSJames Raynard 	static pid_t pid = -1;
2154415cd19SGarrett Wollman 	char path[MAXPATHLEN];
2164415cd19SGarrett Wollman 	struct dom_binding *ysd, *ysd2;
2179d80b1ddSBill Paul 	struct ypbind_resp ypbr;
2184415cd19SGarrett Wollman 	struct timeval tv;
2194415cd19SGarrett Wollman 	struct sockaddr_in clnt_sin;
220ccbcef60SJames Raynard 	int clnt_sock, lfd, fd;
221ccbcef60SJames Raynard 	pid_t gpid;
2224415cd19SGarrett Wollman 	CLIENT *client;
2234415cd19SGarrett Wollman 	int new = 0, r;
224456ebbf8SBill Paul 	int retries = 0;
225a2308772SBill Paul 	struct sockaddr_in check;
226a2308772SBill Paul 	int checklen = sizeof(struct sockaddr_in);
2274415cd19SGarrett Wollman 
2284415cd19SGarrett Wollman 	gpid = getpid();
2294415cd19SGarrett Wollman 	if( !(pid==-1 || pid==gpid) ) {
2304415cd19SGarrett Wollman 		ysd = _ypbindlist;
2314415cd19SGarrett Wollman 		while(ysd) {
2324415cd19SGarrett Wollman 			if(ysd->dom_client)
2334415cd19SGarrett Wollman 				clnt_destroy(ysd->dom_client);
2344415cd19SGarrett Wollman 			ysd2 = ysd->dom_pnext;
2354415cd19SGarrett Wollman 			free(ysd);
2364415cd19SGarrett Wollman 			ysd = ysd2;
2374415cd19SGarrett Wollman 		}
2384415cd19SGarrett Wollman 		_ypbindlist = NULL;
2394415cd19SGarrett Wollman 	}
2404415cd19SGarrett Wollman 	pid = gpid;
2414415cd19SGarrett Wollman 
2424415cd19SGarrett Wollman 	if(ypdb!=NULL)
2434415cd19SGarrett Wollman 		*ypdb = NULL;
2444415cd19SGarrett Wollman 
2454415cd19SGarrett Wollman 	if(dom==NULL || strlen(dom)==0)
2464415cd19SGarrett Wollman 		return YPERR_BADARGS;
2474415cd19SGarrett Wollman 
2484415cd19SGarrett Wollman 	for(ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
2494415cd19SGarrett Wollman 		if( strcmp(dom, ysd->dom_domain) == 0)
2504415cd19SGarrett Wollman 			break;
251a169c9b1SBill Paul 
252a2308772SBill Paul 
2534415cd19SGarrett Wollman 	if(ysd==NULL) {
2544415cd19SGarrett Wollman 		ysd = (struct dom_binding *)malloc(sizeof *ysd);
2554415cd19SGarrett Wollman 		bzero((char *)ysd, sizeof *ysd);
2564415cd19SGarrett Wollman 		ysd->dom_socket = -1;
2574415cd19SGarrett Wollman 		ysd->dom_vers = 0;
2584415cd19SGarrett Wollman 		new = 1;
259a2308772SBill Paul 	} else {
260a2308772SBill Paul 	/* Check the socket -- may have been hosed by the caller. */
261a2308772SBill Paul 		if (getsockname(ysd->dom_socket, (struct sockaddr *)&check,
262a2308772SBill Paul 		    &checklen) == -1 || check.sin_family != AF_INET ||
263a2308772SBill Paul 		    check.sin_port != ysd->dom_local_port) {
264a2308772SBill Paul 		/* Socket became bogus somehow... need to rebind. */
265a2308772SBill Paul 			int save, sock;
266a2308772SBill Paul 
267a2308772SBill Paul 			sock = ysd->dom_socket;
268a2308772SBill Paul 			save = dup(ysd->dom_socket);
269a2308772SBill Paul 			clnt_destroy(ysd->dom_client);
270a2308772SBill Paul 			ysd->dom_vers = 0;
271a2308772SBill Paul 			ysd->dom_client = NULL;
272a2308772SBill Paul 			sock = dup2(save, sock);
273a2308772SBill Paul 			close(save);
274a2308772SBill Paul 		}
2754415cd19SGarrett Wollman 	}
2760f98415eSBill Paul 
2774415cd19SGarrett Wollman again:
278456ebbf8SBill Paul 	retries++;
279456ebbf8SBill Paul 	if (retries > MAX_RETRIES) {
280456ebbf8SBill Paul 		if (new)
281456ebbf8SBill Paul 			free(ysd);
282456ebbf8SBill Paul 		return(YPERR_YPBIND);
283456ebbf8SBill Paul 	}
2844415cd19SGarrett Wollman #ifdef BINDINGDIR
2854415cd19SGarrett Wollman 	if(ysd->dom_vers==0) {
286a169c9b1SBill Paul 		/*
287a169c9b1SBill Paul 		 * We're trying to make a new binding: zorch the
288a169c9b1SBill Paul 		 * existing handle now (if any).
289a169c9b1SBill Paul 		 */
290a169c9b1SBill Paul 		if(ysd->dom_client) {
291a169c9b1SBill Paul 			clnt_destroy(ysd->dom_client);
292a169c9b1SBill Paul 			ysd->dom_client = NULL;
293a2308772SBill Paul 			ysd->dom_socket = -1;
294a169c9b1SBill Paul 		}
2954415cd19SGarrett Wollman 		sprintf(path, "%s/%s.%d", BINDINGDIR, dom, 2);
2964415cd19SGarrett Wollman 		if( (fd=open(path, O_RDONLY)) == -1) {
2974415cd19SGarrett Wollman 			/* no binding file, YP is dead. */
298456ebbf8SBill Paul 			/* Try to bring it back to life. */
299b9729ac2SBill Paul 			close(fd);
300b9729ac2SBill Paul 			goto skipit;
3014415cd19SGarrett Wollman 		}
3024415cd19SGarrett Wollman 		if( flock(fd, LOCK_EX|LOCK_NB) == -1 && errno==EWOULDBLOCK) {
3034415cd19SGarrett Wollman 			struct iovec iov[2];
3049d80b1ddSBill Paul 			struct ypbind_resp ybr;
3054415cd19SGarrett Wollman 			u_short	ypb_port;
3064415cd19SGarrett Wollman 
3074415cd19SGarrett Wollman 			iov[0].iov_base = (caddr_t)&ypb_port;
3084415cd19SGarrett Wollman 			iov[0].iov_len = sizeof ypb_port;
3094415cd19SGarrett Wollman 			iov[1].iov_base = (caddr_t)&ybr;
3104415cd19SGarrett Wollman 			iov[1].iov_len = sizeof ybr;
3114415cd19SGarrett Wollman 
3124415cd19SGarrett Wollman 			r = readv(fd, iov, 2);
3134415cd19SGarrett Wollman 			if(r != iov[0].iov_len + iov[1].iov_len) {
3144415cd19SGarrett Wollman 				close(fd);
3154415cd19SGarrett Wollman 				ysd->dom_vers = -1;
3164415cd19SGarrett Wollman 				goto again;
3174415cd19SGarrett Wollman 			}
3184415cd19SGarrett Wollman 
3194415cd19SGarrett Wollman 			bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
3204415cd19SGarrett Wollman 			ysd->dom_server_addr.sin_family = AF_INET;
3214415cd19SGarrett Wollman 			ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
3229d80b1ddSBill Paul 			ysd->dom_server_addr.sin_addr.s_addr =
3239d80b1ddSBill Paul 			    *(u_long *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
3244415cd19SGarrett Wollman 			ysd->dom_server_addr.sin_port =
3259d80b1ddSBill Paul 			    *(u_short *)&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
3264415cd19SGarrett Wollman 
3274415cd19SGarrett Wollman 			ysd->dom_server_port = ysd->dom_server_addr.sin_port;
3284415cd19SGarrett Wollman 			close(fd);
3294415cd19SGarrett Wollman 			goto gotit;
3304415cd19SGarrett Wollman 		} else {
3314415cd19SGarrett Wollman 			/* no lock on binding file, YP is dead. */
332456ebbf8SBill Paul 			/* Try to bring it back to life. */
3334415cd19SGarrett Wollman 			close(fd);
334456ebbf8SBill Paul 			goto skipit;
335f067dfeaSBill Paul 		}
336f067dfeaSBill Paul 	}
337b9729ac2SBill Paul skipit:
3384415cd19SGarrett Wollman #endif
3394415cd19SGarrett Wollman 	if(ysd->dom_vers==-1 || ysd->dom_vers==0) {
340a169c9b1SBill Paul 		/*
341a169c9b1SBill Paul 		 * We're trying to make a new binding: zorch the
342a169c9b1SBill Paul 		 * existing handle now (if any).
343a169c9b1SBill Paul 		 */
344a169c9b1SBill Paul 		if(ysd->dom_client) {
345a169c9b1SBill Paul 			clnt_destroy(ysd->dom_client);
346a169c9b1SBill Paul 			ysd->dom_client = NULL;
347a2308772SBill Paul 			ysd->dom_socket = -1;
348a169c9b1SBill Paul 		}
3494415cd19SGarrett Wollman 		bzero((char *)&clnt_sin, sizeof clnt_sin);
3504415cd19SGarrett Wollman 		clnt_sin.sin_family = AF_INET;
3514415cd19SGarrett Wollman 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3524415cd19SGarrett Wollman 
3534415cd19SGarrett Wollman 		clnt_sock = RPC_ANYSOCK;
3544415cd19SGarrett Wollman 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
3554415cd19SGarrett Wollman 			0, 0);
3564415cd19SGarrett Wollman 		if(client==NULL) {
35720e4b00aSBill Paul 			/*
35820e4b00aSBill Paul 			 * These conditions indicate ypbind just isn't
35920e4b00aSBill Paul 			 * alive -- we probably don't want to shoot our
360a169c9b1SBill Paul 			 * mouth off in this case; instead generate error
36120e4b00aSBill Paul 			 * messages only for really exotic problems.
36220e4b00aSBill Paul 			 */
36320e4b00aSBill Paul 			if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
36420e4b00aSBill Paul 			   (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
36520e4b00aSBill Paul 			   rpc_createerr.cf_error.re_errno == ECONNREFUSED))
3664415cd19SGarrett Wollman 				clnt_pcreateerror("clnttcp_create");
3674415cd19SGarrett Wollman 			if(new)
3684415cd19SGarrett Wollman 				free(ysd);
369456ebbf8SBill Paul 			return (YPERR_YPBIND);
3704415cd19SGarrett Wollman 		}
3714415cd19SGarrett Wollman 
372456ebbf8SBill Paul 		tv.tv_sec = _yplib_timeout/2;
3734415cd19SGarrett Wollman 		tv.tv_usec = 0;
3744415cd19SGarrett Wollman 		r = clnt_call(client, YPBINDPROC_DOMAIN,
3759d80b1ddSBill Paul 			xdr_domainname, (char *)&dom, xdr_ypbind_resp, &ypbr, tv);
3764415cd19SGarrett Wollman 		if(r != RPC_SUCCESS) {
3774415cd19SGarrett Wollman 			clnt_destroy(client);
3784415cd19SGarrett Wollman 			ysd->dom_vers = -1;
379a2308772SBill Paul 			if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
380a2308772SBill Paul 				if (new)
381a2308772SBill Paul 					free(ysd);
38220e4b00aSBill Paul 				return(YPERR_YPBIND);
383a2308772SBill Paul 			}
38420e4b00aSBill Paul 			fprintf(stderr,
38520e4b00aSBill Paul 			"YP: server for domain %s not responding, retrying\n", dom);
3864415cd19SGarrett Wollman 			goto again;
38764416168SBill Paul 		} else {
38864416168SBill Paul 			if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
389456ebbf8SBill Paul 				clnt_destroy(client);
390456ebbf8SBill Paul 				ysd->dom_vers = -1;
391456ebbf8SBill Paul 				sleep(_yplib_timeout/2);
392456ebbf8SBill Paul 				goto again;
39364416168SBill Paul 			}
3944415cd19SGarrett Wollman 		}
3954415cd19SGarrett Wollman 		clnt_destroy(client);
3964415cd19SGarrett Wollman 
3974415cd19SGarrett Wollman 		bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
3984415cd19SGarrett Wollman 		ysd->dom_server_addr.sin_family = AF_INET;
3994415cd19SGarrett Wollman 		ysd->dom_server_addr.sin_port =
4009d80b1ddSBill Paul 			*(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
4014415cd19SGarrett Wollman 		ysd->dom_server_addr.sin_addr.s_addr =
4029d80b1ddSBill Paul 			*(u_long *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
4034415cd19SGarrett Wollman 		ysd->dom_server_port =
4049d80b1ddSBill Paul 			*(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
4054415cd19SGarrett Wollman gotit:
4064415cd19SGarrett Wollman 		ysd->dom_vers = YPVERS;
4074415cd19SGarrett Wollman 		strcpy(ysd->dom_domain, dom);
4084415cd19SGarrett Wollman 	}
4094415cd19SGarrett Wollman 
410a169c9b1SBill Paul 	/* Don't rebuild the connection to the server unless we have to. */
411a169c9b1SBill Paul 	if (ysd->dom_client == NULL) {
4124415cd19SGarrett Wollman 		tv.tv_sec = _yplib_timeout/2;
4134415cd19SGarrett Wollman 		tv.tv_usec = 0;
4144415cd19SGarrett Wollman 		ysd->dom_socket = RPC_ANYSOCK;
4154415cd19SGarrett Wollman 		ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
4164415cd19SGarrett Wollman 			YPPROG, YPVERS, tv, &ysd->dom_socket);
4174415cd19SGarrett Wollman 		if(ysd->dom_client==NULL) {
4184415cd19SGarrett Wollman 			clnt_pcreateerror("clntudp_create");
4194415cd19SGarrett Wollman 			ysd->dom_vers = -1;
4204415cd19SGarrett Wollman 			goto again;
4214415cd19SGarrett Wollman 		}
4224415cd19SGarrett Wollman 		if( fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
4234415cd19SGarrett Wollman 			perror("fcntl: F_SETFD");
424a2308772SBill Paul 		/*
425a2308772SBill Paul 		 * We want a port number associated with this socket
426a2308772SBill Paul 		 * so that we can check its authenticity later.
427a2308772SBill Paul 		 */
428a2308772SBill Paul 		checklen = sizeof(struct sockaddr_in);
429a2308772SBill Paul 		bzero((char *)&check, checklen);
430a2308772SBill Paul 		bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
431a2308772SBill Paul 		check.sin_family = AF_INET;
432a2308772SBill Paul 		if (!getsockname(ysd->dom_socket,
433a2308772SBill Paul 		    (struct sockaddr *)&check, &checklen)) {
434a2308772SBill Paul 			ysd->dom_local_port = check.sin_port;
435a2308772SBill Paul 		} else {
436a2308772SBill Paul 			clnt_destroy(ysd->dom_client);
437a2308772SBill Paul 			if (new)
438a2308772SBill Paul 				free(ysd);
439a2308772SBill Paul 			return(YPERR_YPBIND);
440a2308772SBill Paul 		}
441a169c9b1SBill Paul 	}
4424415cd19SGarrett Wollman 
4434415cd19SGarrett Wollman 	if(new) {
4444415cd19SGarrett Wollman 		ysd->dom_pnext = _ypbindlist;
4454415cd19SGarrett Wollman 		_ypbindlist = ysd;
4464415cd19SGarrett Wollman 	}
4474415cd19SGarrett Wollman 
4484415cd19SGarrett Wollman 	if(ypdb!=NULL)
4494415cd19SGarrett Wollman 		*ypdb = ysd;
4504415cd19SGarrett Wollman 	return 0;
4514415cd19SGarrett Wollman }
4524415cd19SGarrett Wollman 
4534415cd19SGarrett Wollman static void
4544415cd19SGarrett Wollman _yp_unbind(ypb)
4554415cd19SGarrett Wollman 	struct dom_binding *ypb;
4564415cd19SGarrett Wollman {
4572694f9b9SBill Paul 	if (ypb->dom_client)
4584415cd19SGarrett Wollman 		clnt_destroy(ypb->dom_client);
4594415cd19SGarrett Wollman 	ypb->dom_client = NULL;
4604415cd19SGarrett Wollman 	ypb->dom_socket = -1;
4612694f9b9SBill Paul 	ypb->dom_vers = -1;
4624415cd19SGarrett Wollman }
4634415cd19SGarrett Wollman 
4644415cd19SGarrett Wollman int
4654415cd19SGarrett Wollman yp_bind(dom)
4664415cd19SGarrett Wollman 	char *dom;
4674415cd19SGarrett Wollman {
4684415cd19SGarrett Wollman 	return _yp_dobind(dom, NULL);
4694415cd19SGarrett Wollman }
4704415cd19SGarrett Wollman 
4714415cd19SGarrett Wollman void
4724415cd19SGarrett Wollman yp_unbind(dom)
4734415cd19SGarrett Wollman 	char *dom;
4744415cd19SGarrett Wollman {
4754415cd19SGarrett Wollman 	struct dom_binding *ypb, *ypbp;
4764415cd19SGarrett Wollman 
4774415cd19SGarrett Wollman 	ypbp = NULL;
4784415cd19SGarrett Wollman 	for(ypb=_ypbindlist; ypb; ypb=ypb->dom_pnext) {
4794415cd19SGarrett Wollman 		if( strcmp(dom, ypb->dom_domain) == 0) {
4802694f9b9SBill Paul 			_yp_unbind(ypb);
4814415cd19SGarrett Wollman 			if(ypbp)
4824415cd19SGarrett Wollman 				ypbp->dom_pnext = ypb->dom_pnext;
4834415cd19SGarrett Wollman 			else
4844415cd19SGarrett Wollman 				_ypbindlist = ypb->dom_pnext;
4854415cd19SGarrett Wollman 			free(ypb);
4864415cd19SGarrett Wollman 			return;
4874415cd19SGarrett Wollman 		}
4884415cd19SGarrett Wollman 		ypbp = ypb;
4894415cd19SGarrett Wollman 	}
4904415cd19SGarrett Wollman 	return;
4914415cd19SGarrett Wollman }
4924415cd19SGarrett Wollman 
4934415cd19SGarrett Wollman int
4944415cd19SGarrett Wollman yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
4954415cd19SGarrett Wollman 	char *indomain;
4964415cd19SGarrett Wollman 	char *inmap;
4974415cd19SGarrett Wollman 	const char *inkey;
4984415cd19SGarrett Wollman 	int inkeylen;
4994415cd19SGarrett Wollman 	char **outval;
5004415cd19SGarrett Wollman 	int *outvallen;
5014415cd19SGarrett Wollman {
5024415cd19SGarrett Wollman 	struct dom_binding *ysd;
5034415cd19SGarrett Wollman 	struct ypresp_val yprv;
5044415cd19SGarrett Wollman 	struct timeval tv;
5054415cd19SGarrett Wollman 	struct ypreq_key yprk;
5064415cd19SGarrett Wollman 	int r;
5074415cd19SGarrett Wollman 
5084415cd19SGarrett Wollman 	*outval = NULL;
5094415cd19SGarrett Wollman 	*outvallen = 0;
5104415cd19SGarrett Wollman 
51106643071SBill Paul 	/* Sanity check */
512e17334c3SBill Paul 
51306643071SBill Paul 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
51406643071SBill Paul 	    inmap == NULL || !strlen(inmap) ||
51506643071SBill Paul 	    indomain == NULL || !strlen(indomain))
51606643071SBill Paul 		return YPERR_BADARGS;
517e17334c3SBill Paul 
5184415cd19SGarrett Wollman #ifdef YPMATCHCACHE
5194415cd19SGarrett Wollman 	if( !strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
5200e276383SBill Paul 	    inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
5210e276383SBill Paul 		*outvallen = yprv.val.valdat_len;
5224415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
5230e276383SBill Paul 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
5244415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
5254415cd19SGarrett Wollman 		return 0;
5264415cd19SGarrett Wollman 	}
5274415cd19SGarrett Wollman #endif
5284415cd19SGarrett Wollman 
5294cc738f7SBill Paul again:
5304cc738f7SBill Paul 	if( _yp_dobind(indomain, &ysd) != 0)
5314cc738f7SBill Paul 		return YPERR_DOMAIN;
5324cc738f7SBill Paul 
5334415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
5344415cd19SGarrett Wollman 	tv.tv_usec = 0;
5354415cd19SGarrett Wollman 
5364415cd19SGarrett Wollman 	yprk.domain = indomain;
5374415cd19SGarrett Wollman 	yprk.map = inmap;
5380e276383SBill Paul 	yprk.key.keydat_val = (char *)inkey;
5390e276383SBill Paul 	yprk.key.keydat_len = inkeylen;
5404415cd19SGarrett Wollman 
5414415cd19SGarrett Wollman 	bzero((char *)&yprv, sizeof yprv);
5424415cd19SGarrett Wollman 
5434415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_MATCH,
5444415cd19SGarrett Wollman 		xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
5454415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
5464415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_match: clnt_call");
5472694f9b9SBill Paul 		_yp_unbind(ysd);
5484415cd19SGarrett Wollman 		goto again;
5494415cd19SGarrett Wollman 	}
550a169c9b1SBill Paul 
5510e276383SBill Paul 	if( !(r=ypprot_err(yprv.stat)) ) {
5520e276383SBill Paul 		*outvallen = yprv.val.valdat_len;
5534415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
5540e276383SBill Paul 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
5554415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
5564415cd19SGarrett Wollman #ifdef YPMATCHCACHE
5574415cd19SGarrett Wollman 		if( strcmp(_yp_domain, indomain)==0 )
5584415cd19SGarrett Wollman 			 ypmatch_add(inmap, inkey, inkeylen, *outval, *outvallen);
5594415cd19SGarrett Wollman #endif
5602694f9b9SBill Paul 	}
561a169c9b1SBill Paul 
562a169c9b1SBill Paul 	xdr_free(xdr_ypresp_val, (char *)&yprv);
5634415cd19SGarrett Wollman 	return r;
5644415cd19SGarrett Wollman }
5654415cd19SGarrett Wollman 
5664415cd19SGarrett Wollman int
5674415cd19SGarrett Wollman yp_get_default_domain(domp)
5684415cd19SGarrett Wollman char **domp;
5694415cd19SGarrett Wollman {
5704415cd19SGarrett Wollman 	*domp = NULL;
5714415cd19SGarrett Wollman 	if(_yp_domain[0] == '\0')
5724415cd19SGarrett Wollman 		if( getdomainname(_yp_domain, sizeof _yp_domain))
5734415cd19SGarrett Wollman 			return YPERR_NODOM;
5744415cd19SGarrett Wollman 	*domp = _yp_domain;
5754415cd19SGarrett Wollman 	return 0;
5764415cd19SGarrett Wollman }
5774415cd19SGarrett Wollman 
5784415cd19SGarrett Wollman int
5794415cd19SGarrett Wollman yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
5804415cd19SGarrett Wollman 	char *indomain;
5814415cd19SGarrett Wollman 	char *inmap;
5824415cd19SGarrett Wollman 	char **outkey;
5834415cd19SGarrett Wollman 	int *outkeylen;
5844415cd19SGarrett Wollman 	char **outval;
5854415cd19SGarrett Wollman 	int *outvallen;
5864415cd19SGarrett Wollman {
5874415cd19SGarrett Wollman 	struct ypresp_key_val yprkv;
5884415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
5894415cd19SGarrett Wollman 	struct dom_binding *ysd;
5904415cd19SGarrett Wollman 	struct timeval tv;
5914415cd19SGarrett Wollman 	int r;
5924415cd19SGarrett Wollman 
59306643071SBill Paul 	/* Sanity check */
59406643071SBill Paul 
59506643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
59606643071SBill Paul 	    inmap == NULL || !strlen(inmap))
59706643071SBill Paul 		return YPERR_BADARGS;
59806643071SBill Paul 
5994415cd19SGarrett Wollman 	*outkey = *outval = NULL;
6004415cd19SGarrett Wollman 	*outkeylen = *outvallen = 0;
6014415cd19SGarrett Wollman 
6024415cd19SGarrett Wollman again:
6034415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
6044415cd19SGarrett Wollman 		return YPERR_DOMAIN;
6054415cd19SGarrett Wollman 
6064415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
6074415cd19SGarrett Wollman 	tv.tv_usec = 0;
6084415cd19SGarrett Wollman 
6094415cd19SGarrett Wollman 	yprnk.domain = indomain;
6104415cd19SGarrett Wollman 	yprnk.map = inmap;
6114415cd19SGarrett Wollman 	bzero((char *)&yprkv, sizeof yprkv);
6124415cd19SGarrett Wollman 
6134415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
6144415cd19SGarrett Wollman 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
6154415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
6164415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_first: clnt_call");
6172694f9b9SBill Paul 		_yp_unbind(ysd);
6184415cd19SGarrett Wollman 		goto again;
6194415cd19SGarrett Wollman 	}
6200e276383SBill Paul 	if( !(r=ypprot_err(yprkv.stat)) ) {
6210e276383SBill Paul 		*outkeylen = yprkv.key.keydat_len;
6224415cd19SGarrett Wollman 		*outkey = (char *)malloc(*outkeylen+1);
6230e276383SBill Paul 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
6244415cd19SGarrett Wollman 		(*outkey)[*outkeylen] = '\0';
6250e276383SBill Paul 		*outvallen = yprkv.val.valdat_len;
6264415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
6270e276383SBill Paul 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
6284415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
6292694f9b9SBill Paul 	}
630a169c9b1SBill Paul 
631a169c9b1SBill Paul 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
6324415cd19SGarrett Wollman 	return r;
6334415cd19SGarrett Wollman }
6344415cd19SGarrett Wollman 
6354415cd19SGarrett Wollman int
6364415cd19SGarrett Wollman yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
6374415cd19SGarrett Wollman 	char *indomain;
6384415cd19SGarrett Wollman 	char *inmap;
6394415cd19SGarrett Wollman 	char *inkey;
6404415cd19SGarrett Wollman 	int inkeylen;
6414415cd19SGarrett Wollman 	char **outkey;
6424415cd19SGarrett Wollman 	int *outkeylen;
6434415cd19SGarrett Wollman 	char **outval;
6444415cd19SGarrett Wollman 	int *outvallen;
6454415cd19SGarrett Wollman {
6464415cd19SGarrett Wollman 	struct ypresp_key_val yprkv;
6474415cd19SGarrett Wollman 	struct ypreq_key yprk;
6484415cd19SGarrett Wollman 	struct dom_binding *ysd;
6494415cd19SGarrett Wollman 	struct timeval tv;
6504415cd19SGarrett Wollman 	int r;
6514415cd19SGarrett Wollman 
65206643071SBill Paul 	/* Sanity check */
65306643071SBill Paul 
65406643071SBill Paul 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
65506643071SBill Paul 	    inmap == NULL || !strlen(inmap) ||
65606643071SBill Paul 	    indomain == NULL || !strlen(indomain))
65706643071SBill Paul 		return YPERR_BADARGS;
65806643071SBill Paul 
6594415cd19SGarrett Wollman 	*outkey = *outval = NULL;
6604415cd19SGarrett Wollman 	*outkeylen = *outvallen = 0;
6614415cd19SGarrett Wollman 
6624415cd19SGarrett Wollman again:
6634415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
6644415cd19SGarrett Wollman 		return YPERR_DOMAIN;
6654415cd19SGarrett Wollman 
6664415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
6674415cd19SGarrett Wollman 	tv.tv_usec = 0;
6684415cd19SGarrett Wollman 
6694415cd19SGarrett Wollman 	yprk.domain = indomain;
6704415cd19SGarrett Wollman 	yprk.map = inmap;
6710e276383SBill Paul 	yprk.key.keydat_val = inkey;
6720e276383SBill Paul 	yprk.key.keydat_len = inkeylen;
6734415cd19SGarrett Wollman 	bzero((char *)&yprkv, sizeof yprkv);
6744415cd19SGarrett Wollman 
6754415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_NEXT,
6764415cd19SGarrett Wollman 		xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
6774415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
6784415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_next: clnt_call");
6792694f9b9SBill Paul 		_yp_unbind(ysd);
6804415cd19SGarrett Wollman 		goto again;
6814415cd19SGarrett Wollman 	}
6820e276383SBill Paul 	if( !(r=ypprot_err(yprkv.stat)) ) {
6830e276383SBill Paul 		*outkeylen = yprkv.key.keydat_len;
6844415cd19SGarrett Wollman 		*outkey = (char *)malloc(*outkeylen+1);
6850e276383SBill Paul 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
6864415cd19SGarrett Wollman 		(*outkey)[*outkeylen] = '\0';
6870e276383SBill Paul 		*outvallen = yprkv.val.valdat_len;
6884415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
6890e276383SBill Paul 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
6904415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
6912694f9b9SBill Paul 	}
692a169c9b1SBill Paul 
693a169c9b1SBill Paul 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
6944415cd19SGarrett Wollman 	return r;
6954415cd19SGarrett Wollman }
6964415cd19SGarrett Wollman 
6974415cd19SGarrett Wollman int
6984415cd19SGarrett Wollman yp_all(indomain, inmap, incallback)
6994415cd19SGarrett Wollman 	char *indomain;
7004415cd19SGarrett Wollman 	char *inmap;
7014415cd19SGarrett Wollman 	struct ypall_callback *incallback;
7024415cd19SGarrett Wollman {
7034415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
7044415cd19SGarrett Wollman 	struct dom_binding *ysd;
7054415cd19SGarrett Wollman 	struct timeval tv;
7064415cd19SGarrett Wollman 	struct sockaddr_in clnt_sin;
7074415cd19SGarrett Wollman 	CLIENT *clnt;
70808aff01bSBill Paul 	u_long status, savstat;
7094415cd19SGarrett Wollman 	int clnt_sock;
7104415cd19SGarrett Wollman 
71106643071SBill Paul 	/* Sanity check */
71206643071SBill Paul 
71306643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
71406643071SBill Paul 	    inmap == NULL || !strlen(inmap))
71506643071SBill Paul 		return YPERR_BADARGS;
71606643071SBill Paul 
7172694f9b9SBill Paul again:
7182694f9b9SBill Paul 
7194415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
7204415cd19SGarrett Wollman 		return YPERR_DOMAIN;
7214415cd19SGarrett Wollman 
7224415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
7234415cd19SGarrett Wollman 	tv.tv_usec = 0;
7242694f9b9SBill Paul 
7252694f9b9SBill Paul 	/* YPPROC_ALL manufactures its own channel to ypserv using TCP */
7262694f9b9SBill Paul 
7274415cd19SGarrett Wollman 	clnt_sock = RPC_ANYSOCK;
7284415cd19SGarrett Wollman 	clnt_sin = ysd->dom_server_addr;
7294415cd19SGarrett Wollman 	clnt_sin.sin_port = 0;
7304415cd19SGarrett Wollman 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
7314415cd19SGarrett Wollman 	if(clnt==NULL) {
7324415cd19SGarrett Wollman 		printf("clnttcp_create failed\n");
7334415cd19SGarrett Wollman 		return YPERR_PMAP;
7344415cd19SGarrett Wollman 	}
7354415cd19SGarrett Wollman 
7364415cd19SGarrett Wollman 	yprnk.domain = indomain;
7374415cd19SGarrett Wollman 	yprnk.map = inmap;
7384415cd19SGarrett Wollman 	ypresp_allfn = incallback->foreach;
7394415cd19SGarrett Wollman 	ypresp_data = (void *)incallback->data;
7404415cd19SGarrett Wollman 
7412694f9b9SBill Paul 	if (clnt_call(clnt, YPPROC_ALL,
7422694f9b9SBill Paul 		xdr_ypreq_nokey, &yprnk,
7432694f9b9SBill Paul 		xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
7442694f9b9SBill Paul 			clnt_perror(ysd->dom_client, "yp_next: clnt_call");
7452694f9b9SBill Paul 			clnt_destroy(clnt);
7462694f9b9SBill Paul 			_yp_unbind(ysd);
7472694f9b9SBill Paul 			goto again;
7482694f9b9SBill Paul 	}
7492694f9b9SBill Paul 
7504415cd19SGarrett Wollman 	clnt_destroy(clnt);
75108aff01bSBill Paul 	savstat = status;
7524415cd19SGarrett Wollman 	xdr_free(xdr_ypresp_all_seq, (char *)&status);	/* not really needed... */
75308aff01bSBill Paul 	if(savstat != YP_NOMORE)
75408aff01bSBill Paul 		return ypprot_err(savstat);
7554415cd19SGarrett Wollman 	return 0;
7564415cd19SGarrett Wollman }
7574415cd19SGarrett Wollman 
7584415cd19SGarrett Wollman int
7594415cd19SGarrett Wollman yp_order(indomain, inmap, outorder)
7604415cd19SGarrett Wollman 	char *indomain;
7614415cd19SGarrett Wollman 	char *inmap;
7624415cd19SGarrett Wollman 	int *outorder;
7634415cd19SGarrett Wollman {
7644415cd19SGarrett Wollman  	struct dom_binding *ysd;
7654415cd19SGarrett Wollman 	struct ypresp_order ypro;
7664415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
7674415cd19SGarrett Wollman 	struct timeval tv;
7684415cd19SGarrett Wollman 	int r;
7694415cd19SGarrett Wollman 
77006643071SBill Paul 	/* Sanity check */
77106643071SBill Paul 
77206643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
77306643071SBill Paul 	    inmap == NULL || !strlen(inmap))
77406643071SBill Paul 		return YPERR_BADARGS;
77506643071SBill Paul 
7764415cd19SGarrett Wollman again:
7774415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
7784415cd19SGarrett Wollman 		return YPERR_DOMAIN;
7794415cd19SGarrett Wollman 
7804415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
7814415cd19SGarrett Wollman 	tv.tv_usec = 0;
7824415cd19SGarrett Wollman 
7834415cd19SGarrett Wollman 	yprnk.domain = indomain;
7844415cd19SGarrett Wollman 	yprnk.map = inmap;
7854415cd19SGarrett Wollman 
7864415cd19SGarrett Wollman 	bzero((char *)(char *)&ypro, sizeof ypro);
7874415cd19SGarrett Wollman 
7884415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_ORDER,
7894415cd19SGarrett Wollman 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
7904415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
7914415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_order: clnt_call");
7922694f9b9SBill Paul 		_yp_unbind(ysd);
7934415cd19SGarrett Wollman 		goto again;
7944415cd19SGarrett Wollman 	}
7954415cd19SGarrett Wollman 
796a169c9b1SBill Paul 	if( !(r=ypprot_err(ypro.stat)) ) {
7974415cd19SGarrett Wollman 		*outorder = ypro.ordernum;
7982694f9b9SBill Paul 	}
799a169c9b1SBill Paul 
8004415cd19SGarrett Wollman 	xdr_free(xdr_ypresp_order, (char *)&ypro);
8012694f9b9SBill Paul 	return (r);
8024415cd19SGarrett Wollman }
8034415cd19SGarrett Wollman 
8044415cd19SGarrett Wollman int
8054415cd19SGarrett Wollman yp_master(indomain, inmap, outname)
8064415cd19SGarrett Wollman 	char *indomain;
8074415cd19SGarrett Wollman 	char *inmap;
8084415cd19SGarrett Wollman 	char **outname;
8094415cd19SGarrett Wollman {
8104415cd19SGarrett Wollman 	struct dom_binding *ysd;
8114415cd19SGarrett Wollman 	struct ypresp_master yprm;
8124415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
8134415cd19SGarrett Wollman 	struct timeval tv;
8144415cd19SGarrett Wollman 	int r;
8154415cd19SGarrett Wollman 
81606643071SBill Paul 	/* Sanity check */
81706643071SBill Paul 
81806643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
81906643071SBill Paul 	    inmap == NULL || !strlen(inmap))
82006643071SBill Paul 		return YPERR_BADARGS;
8214415cd19SGarrett Wollman again:
8224415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
8234415cd19SGarrett Wollman 		return YPERR_DOMAIN;
8244415cd19SGarrett Wollman 
8254415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
8264415cd19SGarrett Wollman 	tv.tv_usec = 0;
8274415cd19SGarrett Wollman 
8284415cd19SGarrett Wollman 	yprnk.domain = indomain;
8294415cd19SGarrett Wollman 	yprnk.map = inmap;
8304415cd19SGarrett Wollman 
8314415cd19SGarrett Wollman 	bzero((char *)&yprm, sizeof yprm);
8324415cd19SGarrett Wollman 
8334415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_MASTER,
8344415cd19SGarrett Wollman 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
8354415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
8364415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_master: clnt_call");
8372694f9b9SBill Paul 		_yp_unbind(ysd);
8384415cd19SGarrett Wollman 		goto again;
8394415cd19SGarrett Wollman 	}
8402694f9b9SBill Paul 
8410e276383SBill Paul 	if( !(r=ypprot_err(yprm.stat)) ) {
8420e276383SBill Paul 		*outname = (char *)strdup(yprm.peer);
8432694f9b9SBill Paul 	}
844a169c9b1SBill Paul 
845a169c9b1SBill Paul 	xdr_free(xdr_ypresp_master, (char *)&yprm);
8462694f9b9SBill Paul 	return (r);
8474415cd19SGarrett Wollman }
848b9729ac2SBill Paul int
8494415cd19SGarrett Wollman yp_maplist(indomain, outmaplist)
8504415cd19SGarrett Wollman 	char *indomain;
8514415cd19SGarrett Wollman 	struct ypmaplist **outmaplist;
8524415cd19SGarrett Wollman {
8534415cd19SGarrett Wollman 	struct dom_binding *ysd;
8544415cd19SGarrett Wollman 	struct ypresp_maplist ypml;
8554415cd19SGarrett Wollman 	struct timeval tv;
8564415cd19SGarrett Wollman 	int r;
8574415cd19SGarrett Wollman 
85806643071SBill Paul 	/* Sanity check */
85906643071SBill Paul 
86006643071SBill Paul 	if (indomain == NULL || !strlen(indomain))
86106643071SBill Paul 		return YPERR_BADARGS;
86206643071SBill Paul 
8634415cd19SGarrett Wollman again:
8644415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
8654415cd19SGarrett Wollman 		return YPERR_DOMAIN;
8664415cd19SGarrett Wollman 
8674415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
8684415cd19SGarrett Wollman 	tv.tv_usec = 0;
8694415cd19SGarrett Wollman 
8704415cd19SGarrett Wollman 	bzero((char *)&ypml, sizeof ypml);
8714415cd19SGarrett Wollman 
8724415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
8739d80b1ddSBill Paul 		xdr_domainname,(char *)&indomain,xdr_ypresp_maplist,&ypml,tv);
8744415cd19SGarrett Wollman 	if (r != RPC_SUCCESS) {
8754415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
8762694f9b9SBill Paul 		_yp_unbind(ysd);
8774415cd19SGarrett Wollman 		goto again;
8784415cd19SGarrett Wollman 	}
879a169c9b1SBill Paul 	if( !(r=ypprot_err(ypml.stat)) ) {
8800e276383SBill Paul 		*outmaplist = ypml.maps;
8812694f9b9SBill Paul 	}
8822694f9b9SBill Paul 
883a169c9b1SBill Paul 	/* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
8842694f9b9SBill Paul 	return (r);
8854415cd19SGarrett Wollman }
8864415cd19SGarrett Wollman 
8874415cd19SGarrett Wollman char *
8884415cd19SGarrett Wollman yperr_string(incode)
8894415cd19SGarrett Wollman 	int incode;
8904415cd19SGarrett Wollman {
8914415cd19SGarrett Wollman 	static char err[80];
8924415cd19SGarrett Wollman 
8934415cd19SGarrett Wollman 	switch(incode) {
8944415cd19SGarrett Wollman 	case 0:
8954415cd19SGarrett Wollman 		return "Success";
8964415cd19SGarrett Wollman 	case YPERR_BADARGS:
8974415cd19SGarrett Wollman 		return "Request arguments bad";
8984415cd19SGarrett Wollman 	case YPERR_RPC:
8994415cd19SGarrett Wollman 		return "RPC failure";
9004415cd19SGarrett Wollman 	case YPERR_DOMAIN:
9014415cd19SGarrett Wollman 		return "Can't bind to server which serves this domain";
9024415cd19SGarrett Wollman 	case YPERR_MAP:
9034415cd19SGarrett Wollman 		return "No such map in server's domain";
9044415cd19SGarrett Wollman 	case YPERR_KEY:
9054415cd19SGarrett Wollman 		return "No such key in map";
9064415cd19SGarrett Wollman 	case YPERR_YPERR:
9074415cd19SGarrett Wollman 		return "YP server error";
9084415cd19SGarrett Wollman 	case YPERR_RESRC:
9094415cd19SGarrett Wollman 		return "Local resource allocation failure";
9104415cd19SGarrett Wollman 	case YPERR_NOMORE:
9114415cd19SGarrett Wollman 		return "No more records in map database";
9124415cd19SGarrett Wollman 	case YPERR_PMAP:
9134415cd19SGarrett Wollman 		return "Can't communicate with portmapper";
9144415cd19SGarrett Wollman 	case YPERR_YPBIND:
9154415cd19SGarrett Wollman 		return "Can't communicate with ypbind";
9164415cd19SGarrett Wollman 	case YPERR_YPSERV:
9174415cd19SGarrett Wollman 		return "Can't communicate with ypserv";
9184415cd19SGarrett Wollman 	case YPERR_NODOM:
9194415cd19SGarrett Wollman 		return "Local domain name not set";
9204415cd19SGarrett Wollman 	case YPERR_BADDB:
9214415cd19SGarrett Wollman 		return "Server data base is bad";
9224415cd19SGarrett Wollman 	case YPERR_VERS:
9234415cd19SGarrett Wollman 		return "YP server version mismatch - server can't supply service.";
9244415cd19SGarrett Wollman 	case YPERR_ACCESS:
9254415cd19SGarrett Wollman 		return "Access violation";
9264415cd19SGarrett Wollman 	case YPERR_BUSY:
9274415cd19SGarrett Wollman 		return "Database is busy";
9284415cd19SGarrett Wollman 	}
9294415cd19SGarrett Wollman 	sprintf(err, "YP unknown error %d\n", incode);
9304415cd19SGarrett Wollman 	return err;
9314415cd19SGarrett Wollman }
9324415cd19SGarrett Wollman 
9334415cd19SGarrett Wollman int
9344415cd19SGarrett Wollman ypprot_err(incode)
9354415cd19SGarrett Wollman 	unsigned int incode;
9364415cd19SGarrett Wollman {
9374415cd19SGarrett Wollman 	switch(incode) {
9384415cd19SGarrett Wollman 	case YP_TRUE:
9394415cd19SGarrett Wollman 		return 0;
9404415cd19SGarrett Wollman 	case YP_FALSE:
9414415cd19SGarrett Wollman 		return YPERR_YPBIND;
9424415cd19SGarrett Wollman 	case YP_NOMORE:
9434415cd19SGarrett Wollman 		return YPERR_NOMORE;
9444415cd19SGarrett Wollman 	case YP_NOMAP:
9454415cd19SGarrett Wollman 		return YPERR_MAP;
9464415cd19SGarrett Wollman 	case YP_NODOM:
947bc8e373cSBill Paul 		return YPERR_DOMAIN;
9484415cd19SGarrett Wollman 	case YP_NOKEY:
9494415cd19SGarrett Wollman 		return YPERR_KEY;
9504415cd19SGarrett Wollman 	case YP_BADOP:
9514415cd19SGarrett Wollman 		return YPERR_YPERR;
9524415cd19SGarrett Wollman 	case YP_BADDB:
9534415cd19SGarrett Wollman 		return YPERR_BADDB;
9544415cd19SGarrett Wollman 	case YP_YPERR:
9554415cd19SGarrett Wollman 		return YPERR_YPERR;
9564415cd19SGarrett Wollman 	case YP_BADARGS:
9574415cd19SGarrett Wollman 		return YPERR_BADARGS;
9584415cd19SGarrett Wollman 	case YP_VERS:
9594415cd19SGarrett Wollman 		return YPERR_VERS;
9604415cd19SGarrett Wollman 	}
9614415cd19SGarrett Wollman 	return YPERR_YPERR;
9624415cd19SGarrett Wollman }
9634415cd19SGarrett Wollman 
9644415cd19SGarrett Wollman int
9654415cd19SGarrett Wollman _yp_check(dom)
9664415cd19SGarrett Wollman 	char **dom;
9674415cd19SGarrett Wollman {
9684415cd19SGarrett Wollman 	char *unused;
9694415cd19SGarrett Wollman 
9704415cd19SGarrett Wollman 	if( _yp_domain[0]=='\0' )
9714415cd19SGarrett Wollman 		if( yp_get_default_domain(&unused) )
9724415cd19SGarrett Wollman 			return 0;
9734415cd19SGarrett Wollman 
9744415cd19SGarrett Wollman 	if(dom)
9754415cd19SGarrett Wollman 		*dom = _yp_domain;
9764415cd19SGarrett Wollman 
9770f98415eSBill Paul 	if( yp_bind(_yp_domain)==0 ) {
9780f98415eSBill Paul 		yp_unbind(_yp_domain);
9794415cd19SGarrett Wollman 		return 1;
9800f98415eSBill Paul 	}
9814415cd19SGarrett Wollman 	return 0;
9824415cd19SGarrett Wollman }
983