xref: /freebsd/lib/libc/yp/yplib.c (revision f067dfeabcf09a6ffc55207f965e6549b61dbc3d)
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
31f067dfeaSBill Paul static char *rcsid = "$Id: yplib.c,v 1.4 1995/03/24 21:21:37 wpaul 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>
424415cd19SGarrett Wollman #include <rpc/rpc.h>
434415cd19SGarrett Wollman #include <rpc/xdr.h>
444415cd19SGarrett Wollman #include <rpcsvc/yp_prot.h>
454415cd19SGarrett Wollman #include <rpcsvc/ypclnt.h>
464415cd19SGarrett Wollman 
474415cd19SGarrett Wollman #ifndef BINDINGDIR
484415cd19SGarrett Wollman #define BINDINGDIR "/var/yp/binding"
494415cd19SGarrett Wollman #endif
504415cd19SGarrett Wollman #define YPMATCHCACHE
514415cd19SGarrett Wollman 
524415cd19SGarrett Wollman extern bool_t xdr_domainname(), xdr_ypbind_resp();
534415cd19SGarrett Wollman extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
544415cd19SGarrett Wollman extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
554415cd19SGarrett Wollman extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
564415cd19SGarrett Wollman extern bool_t xdr_ypresp_master();
574415cd19SGarrett Wollman 
584415cd19SGarrett Wollman int (*ypresp_allfn)();
594415cd19SGarrett Wollman void *ypresp_data;
604415cd19SGarrett Wollman 
614415cd19SGarrett Wollman struct dom_binding *_ypbindlist;
624415cd19SGarrett Wollman static char _yp_domain[MAXHOSTNAMELEN];
634415cd19SGarrett Wollman int _yplib_timeout = 10;
644415cd19SGarrett Wollman 
654415cd19SGarrett Wollman #ifdef YPMATCHCACHE
664415cd19SGarrett Wollman int _yplib_cache = 5;
674415cd19SGarrett Wollman 
684415cd19SGarrett Wollman static struct ypmatch_ent {
694415cd19SGarrett Wollman 	struct ypmatch_ent *next;
704415cd19SGarrett Wollman 	char *map, *key, *val;
714415cd19SGarrett Wollman 	int keylen, vallen;
724415cd19SGarrett Wollman 	time_t expire_t;
734415cd19SGarrett Wollman } *ypmc;
744415cd19SGarrett Wollman 
754415cd19SGarrett Wollman static void
764415cd19SGarrett Wollman ypmatch_add(map, key, keylen, val, vallen)
774415cd19SGarrett Wollman char *map;
784415cd19SGarrett Wollman char *key;
794415cd19SGarrett Wollman int keylen;
804415cd19SGarrett Wollman char *val;
814415cd19SGarrett Wollman int vallen;
824415cd19SGarrett Wollman {
834415cd19SGarrett Wollman 	struct ypmatch_ent *ep;
844415cd19SGarrett Wollman 	time_t t;
854415cd19SGarrett Wollman 
864415cd19SGarrett Wollman 	time(&t);
874415cd19SGarrett Wollman 
884415cd19SGarrett Wollman 	for(ep=ypmc; ep; ep=ep->next)
894415cd19SGarrett Wollman 		if(ep->expire_t < t)
904415cd19SGarrett Wollman 			break;
914415cd19SGarrett Wollman 	if(ep==NULL) {
924415cd19SGarrett Wollman 		ep = (struct ypmatch_ent *)malloc(sizeof *ep);
934415cd19SGarrett Wollman 		bzero((char *)ep, sizeof *ep);
944415cd19SGarrett Wollman 		if(ypmc)
954415cd19SGarrett Wollman 			ep->next = ypmc;
964415cd19SGarrett Wollman 		ypmc = ep;
974415cd19SGarrett Wollman 	}
984415cd19SGarrett Wollman 
994415cd19SGarrett Wollman 	if(ep->key)
1004415cd19SGarrett Wollman 		free(ep->key);
1014415cd19SGarrett Wollman 	if(ep->val)
1024415cd19SGarrett Wollman 		free(ep->val);
1034415cd19SGarrett Wollman 
1044415cd19SGarrett Wollman 	ep->key = NULL;
1054415cd19SGarrett Wollman 	ep->val = NULL;
1064415cd19SGarrett Wollman 
1074415cd19SGarrett Wollman 	ep->key = (char *)malloc(keylen);
1084415cd19SGarrett Wollman 	if(ep->key==NULL)
1094415cd19SGarrett Wollman 		return;
1104415cd19SGarrett Wollman 
1114415cd19SGarrett Wollman 	ep->val = (char *)malloc(vallen);
1124415cd19SGarrett Wollman 	if(ep->key==NULL) {
1134415cd19SGarrett Wollman 		free(ep->key);
1144415cd19SGarrett Wollman 		ep->key = NULL;
1154415cd19SGarrett Wollman 		return;
1164415cd19SGarrett Wollman 	}
1174415cd19SGarrett Wollman 	ep->keylen = keylen;
1184415cd19SGarrett Wollman 	ep->vallen = vallen;
1194415cd19SGarrett Wollman 
1204415cd19SGarrett Wollman 	bcopy(key, ep->key, ep->keylen);
1214415cd19SGarrett Wollman 	bcopy(val, ep->val, ep->vallen);
1224415cd19SGarrett Wollman 
1234415cd19SGarrett Wollman 	if(ep->map) {
1244415cd19SGarrett Wollman 		if( strcmp(ep->map, map) ) {
1254415cd19SGarrett Wollman 			free(ep->map);
1264415cd19SGarrett Wollman 			ep->map = strdup(map);
1274415cd19SGarrett Wollman 		}
1284415cd19SGarrett Wollman 	} else {
1294415cd19SGarrett Wollman 		ep->map = strdup(map);
1304415cd19SGarrett Wollman 	}
1314415cd19SGarrett Wollman 
1324415cd19SGarrett Wollman 	ep->expire_t = t + _yplib_cache;
1334415cd19SGarrett Wollman }
1344415cd19SGarrett Wollman 
1354415cd19SGarrett Wollman static bool_t
1364415cd19SGarrett Wollman ypmatch_find(map, key, keylen, val, vallen)
1374415cd19SGarrett Wollman char *map;
1384415cd19SGarrett Wollman char *key;
1394415cd19SGarrett Wollman int keylen;
1404415cd19SGarrett Wollman char **val;
1414415cd19SGarrett Wollman int *vallen;
1424415cd19SGarrett Wollman {
1434415cd19SGarrett Wollman 	struct ypmatch_ent *ep;
1444415cd19SGarrett Wollman 	time_t t;
1454415cd19SGarrett Wollman 
1464415cd19SGarrett Wollman 	if(ypmc==NULL)
1474415cd19SGarrett Wollman 		return 0;
1484415cd19SGarrett Wollman 
1494415cd19SGarrett Wollman 	time(&t);
1504415cd19SGarrett Wollman 
1514415cd19SGarrett Wollman 	for(ep=ypmc; ep; ep=ep->next) {
1524415cd19SGarrett Wollman 		if(ep->keylen != keylen)
1534415cd19SGarrett Wollman 			continue;
1544415cd19SGarrett Wollman 		if(strcmp(ep->map, map))
1554415cd19SGarrett Wollman 			continue;
1564415cd19SGarrett Wollman 		if(bcmp(ep->key, key, keylen))
1574415cd19SGarrett Wollman 			continue;
1584415cd19SGarrett Wollman 		if(t > ep->expire_t)
1594415cd19SGarrett Wollman 			continue;
1604415cd19SGarrett Wollman 
1614415cd19SGarrett Wollman 		*val = ep->val;
1624415cd19SGarrett Wollman 		*vallen = ep->vallen;
1634415cd19SGarrett Wollman 		return 1;
1644415cd19SGarrett Wollman 	}
1654415cd19SGarrett Wollman 	return 0;
1664415cd19SGarrett Wollman }
1674415cd19SGarrett Wollman #endif
1684415cd19SGarrett Wollman 
1694415cd19SGarrett Wollman int
1704415cd19SGarrett Wollman _yp_dobind(dom, ypdb)
1714415cd19SGarrett Wollman char *dom;
1724415cd19SGarrett Wollman struct dom_binding **ypdb;
1734415cd19SGarrett Wollman {
1744415cd19SGarrett Wollman 	static int pid = -1;
1754415cd19SGarrett Wollman 	char path[MAXPATHLEN];
1764415cd19SGarrett Wollman 	struct dom_binding *ysd, *ysd2;
1774415cd19SGarrett Wollman 	struct ypbind_resp ypbr;
1784415cd19SGarrett Wollman 	struct timeval tv;
1794415cd19SGarrett Wollman 	struct sockaddr_in clnt_sin;
1804415cd19SGarrett Wollman 	int clnt_sock, fd, gpid;
1814415cd19SGarrett Wollman 	CLIENT *client;
1824415cd19SGarrett Wollman 	int new=0, r;
183f067dfeaSBill Paul 	char *_defaultdom;
1844415cd19SGarrett Wollman 
1854415cd19SGarrett Wollman 	gpid = getpid();
1864415cd19SGarrett Wollman 	if( !(pid==-1 || pid==gpid) ) {
1874415cd19SGarrett Wollman 		ysd = _ypbindlist;
1884415cd19SGarrett Wollman 		while(ysd) {
1894415cd19SGarrett Wollman 			if(ysd->dom_client)
1904415cd19SGarrett Wollman 				clnt_destroy(ysd->dom_client);
1914415cd19SGarrett Wollman 			ysd2 = ysd->dom_pnext;
1924415cd19SGarrett Wollman 			free(ysd);
1934415cd19SGarrett Wollman 			ysd = ysd2;
1944415cd19SGarrett Wollman 		}
1954415cd19SGarrett Wollman 		_ypbindlist = NULL;
1964415cd19SGarrett Wollman 	}
1974415cd19SGarrett Wollman 	pid = gpid;
1984415cd19SGarrett Wollman 
1994415cd19SGarrett Wollman 	if(ypdb!=NULL)
2004415cd19SGarrett Wollman 		*ypdb = NULL;
2014415cd19SGarrett Wollman 
2024415cd19SGarrett Wollman 	if(dom==NULL || strlen(dom)==0)
2034415cd19SGarrett Wollman 		return YPERR_BADARGS;
2044415cd19SGarrett Wollman 
2054415cd19SGarrett Wollman 	for(ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
2064415cd19SGarrett Wollman 		if( strcmp(dom, ysd->dom_domain) == 0)
2074415cd19SGarrett Wollman 			break;
2084415cd19SGarrett Wollman 	if(ysd==NULL) {
2094415cd19SGarrett Wollman 		ysd = (struct dom_binding *)malloc(sizeof *ysd);
2104415cd19SGarrett Wollman 		bzero((char *)ysd, sizeof *ysd);
2114415cd19SGarrett Wollman 		ysd->dom_socket = -1;
2124415cd19SGarrett Wollman 		ysd->dom_vers = 0;
2134415cd19SGarrett Wollman 		new = 1;
2144415cd19SGarrett Wollman 	}
2154415cd19SGarrett Wollman again:
2164415cd19SGarrett Wollman #ifdef BINDINGDIR
2174415cd19SGarrett Wollman 	if(ysd->dom_vers==0) {
2184415cd19SGarrett Wollman 		sprintf(path, "%s/%s.%d", BINDINGDIR, dom, 2);
2194415cd19SGarrett Wollman 		if( (fd=open(path, O_RDONLY)) == -1) {
2204415cd19SGarrett Wollman 			/* no binding file, YP is dead. */
2214415cd19SGarrett Wollman 			if(new)
2224415cd19SGarrett Wollman 				free(ysd);
2234415cd19SGarrett Wollman 			return YPERR_YPBIND;
2244415cd19SGarrett Wollman 		}
2254415cd19SGarrett Wollman 		if( flock(fd, LOCK_EX|LOCK_NB) == -1 && errno==EWOULDBLOCK) {
2264415cd19SGarrett Wollman 			struct iovec iov[2];
2274415cd19SGarrett Wollman 			struct ypbind_resp ybr;
2284415cd19SGarrett Wollman 			u_short	ypb_port;
2294415cd19SGarrett Wollman 
2304415cd19SGarrett Wollman 			iov[0].iov_base = (caddr_t)&ypb_port;
2314415cd19SGarrett Wollman 			iov[0].iov_len = sizeof ypb_port;
2324415cd19SGarrett Wollman 			iov[1].iov_base = (caddr_t)&ybr;
2334415cd19SGarrett Wollman 			iov[1].iov_len = sizeof ybr;
2344415cd19SGarrett Wollman 
2354415cd19SGarrett Wollman 			r = readv(fd, iov, 2);
2364415cd19SGarrett Wollman 			if(r != iov[0].iov_len + iov[1].iov_len) {
2374415cd19SGarrett Wollman 				close(fd);
2384415cd19SGarrett Wollman 				ysd->dom_vers = -1;
2394415cd19SGarrett Wollman 				goto again;
2404415cd19SGarrett Wollman 			}
2414415cd19SGarrett Wollman 
2424415cd19SGarrett Wollman 			bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
2434415cd19SGarrett Wollman 			ysd->dom_server_addr.sin_family = AF_INET;
2444415cd19SGarrett Wollman 			ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
2454415cd19SGarrett Wollman 			ysd->dom_server_addr.sin_addr =
2464415cd19SGarrett Wollman 			    ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
2474415cd19SGarrett Wollman 			ysd->dom_server_addr.sin_port =
2484415cd19SGarrett Wollman 			    ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
2494415cd19SGarrett Wollman 
2504415cd19SGarrett Wollman 			ysd->dom_server_port = ysd->dom_server_addr.sin_port;
2514415cd19SGarrett Wollman 			close(fd);
2524415cd19SGarrett Wollman 			goto gotit;
2534415cd19SGarrett Wollman 		} else {
2544415cd19SGarrett Wollman 			/* no lock on binding file, YP is dead. */
255f067dfeaSBill Paul 			/*
256f067dfeaSBill Paul 			 * XXX Not necessarily: the caller might be asking
257f067dfeaSBill Paul 			 * for a domain that we simply aren't bound to yet.
258f067dfeaSBill Paul 			 * Check that the binding file for the default domain
259f067dfeaSBill Paul 			 * is also unlocked before giving up.
260f067dfeaSBill Paul 			 */
2614415cd19SGarrett Wollman 			close(fd);
262f067dfeaSBill Paul 			if (new) {
263f067dfeaSBill Paul 				if (yp_get_default_domain(&_defaultdom))
264f067dfeaSBill Paul 					return YPERR_NODOM;
265f067dfeaSBill Paul 				sprintf(path, "%s/%s.%d", BINDINGDIR, _defaultdom, 2);
266f067dfeaSBill Paul 				if((fd=open(path, O_RDONLY)) > 0 &&
267f067dfeaSBill Paul 					(flock(fd, LOCK_EX|LOCK_NB)) == -1 &&
268f067dfeaSBill Paul 						errno==EWOULDBLOCK) {
269f067dfeaSBill Paul 						close(fd);
270f067dfeaSBill Paul 				} else {
271f067dfeaSBill Paul 					close(fd); /* for paranoia's sake */
2724415cd19SGarrett Wollman 					free(ysd);
2734415cd19SGarrett Wollman 					return YPERR_YPBIND;
2744415cd19SGarrett Wollman 				}
2754415cd19SGarrett Wollman 			}
276f067dfeaSBill Paul 		}
277f067dfeaSBill Paul 	}
2784415cd19SGarrett Wollman #endif
2794415cd19SGarrett Wollman 	if(ysd->dom_vers==-1 || ysd->dom_vers==0) {
2804415cd19SGarrett Wollman 		bzero((char *)&clnt_sin, sizeof clnt_sin);
2814415cd19SGarrett Wollman 		clnt_sin.sin_family = AF_INET;
2824415cd19SGarrett Wollman 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2834415cd19SGarrett Wollman 
2844415cd19SGarrett Wollman 		clnt_sock = RPC_ANYSOCK;
2854415cd19SGarrett Wollman 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
2864415cd19SGarrett Wollman 			0, 0);
2874415cd19SGarrett Wollman 		if(client==NULL) {
2884415cd19SGarrett Wollman 			clnt_pcreateerror("clnttcp_create");
2894415cd19SGarrett Wollman 			if(new)
2904415cd19SGarrett Wollman 				free(ysd);
2914415cd19SGarrett Wollman 			return YPERR_YPBIND;
2924415cd19SGarrett Wollman 		}
2934415cd19SGarrett Wollman 
2944415cd19SGarrett Wollman 		tv.tv_sec = _yplib_timeout;
2954415cd19SGarrett Wollman 		tv.tv_usec = 0;
2964415cd19SGarrett Wollman 		r = clnt_call(client, YPBINDPROC_DOMAIN,
2974415cd19SGarrett Wollman 			xdr_domainname, dom, xdr_ypbind_resp, &ypbr, tv);
2984415cd19SGarrett Wollman 		if(r != RPC_SUCCESS) {
2994415cd19SGarrett Wollman 			fprintf(stderr,
300f067dfeaSBill Paul 			"YP: server for domain %s not responding, retrying\n", dom);
3014415cd19SGarrett Wollman 			clnt_destroy(client);
3024415cd19SGarrett Wollman 			ysd->dom_vers = -1;
3034415cd19SGarrett Wollman 			goto again;
3044415cd19SGarrett Wollman 		}
3054415cd19SGarrett Wollman 		clnt_destroy(client);
3064415cd19SGarrett Wollman 
3074415cd19SGarrett Wollman 		bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
3084415cd19SGarrett Wollman 		ysd->dom_server_addr.sin_family = AF_INET;
3094415cd19SGarrett Wollman 		ysd->dom_server_addr.sin_port =
3104415cd19SGarrett Wollman 			ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
3114415cd19SGarrett Wollman 		ysd->dom_server_addr.sin_addr.s_addr =
3124415cd19SGarrett Wollman 			ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr;
3134415cd19SGarrett Wollman 		ysd->dom_server_port =
3144415cd19SGarrett Wollman 			ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port;
3154415cd19SGarrett Wollman gotit:
3164415cd19SGarrett Wollman 		ysd->dom_vers = YPVERS;
3174415cd19SGarrett Wollman 		strcpy(ysd->dom_domain, dom);
3184415cd19SGarrett Wollman 	}
3194415cd19SGarrett Wollman 
3204415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout/2;
3214415cd19SGarrett Wollman 	tv.tv_usec = 0;
3224415cd19SGarrett Wollman 	if(ysd->dom_client)
3234415cd19SGarrett Wollman 		clnt_destroy(ysd->dom_client);
3244415cd19SGarrett Wollman 	ysd->dom_socket = RPC_ANYSOCK;
3254415cd19SGarrett Wollman 	ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
3264415cd19SGarrett Wollman 		YPPROG, YPVERS, tv, &ysd->dom_socket);
3274415cd19SGarrett Wollman 	if(ysd->dom_client==NULL) {
3284415cd19SGarrett Wollman 		clnt_pcreateerror("clntudp_create");
3294415cd19SGarrett Wollman 		ysd->dom_vers = -1;
3304415cd19SGarrett Wollman 		goto again;
3314415cd19SGarrett Wollman 	}
3324415cd19SGarrett Wollman 	if( fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
3334415cd19SGarrett Wollman 		perror("fcntl: F_SETFD");
3344415cd19SGarrett Wollman 
3354415cd19SGarrett Wollman 	if(new) {
3364415cd19SGarrett Wollman 		ysd->dom_pnext = _ypbindlist;
3374415cd19SGarrett Wollman 		_ypbindlist = ysd;
3384415cd19SGarrett Wollman 	}
3394415cd19SGarrett Wollman 
3404415cd19SGarrett Wollman 	if(ypdb!=NULL)
3414415cd19SGarrett Wollman 		*ypdb = ysd;
3424415cd19SGarrett Wollman 	return 0;
3434415cd19SGarrett Wollman }
3444415cd19SGarrett Wollman 
3454415cd19SGarrett Wollman static void
3464415cd19SGarrett Wollman _yp_unbind(ypb)
3474415cd19SGarrett Wollman struct dom_binding *ypb;
3484415cd19SGarrett Wollman {
3494415cd19SGarrett Wollman 	clnt_destroy(ypb->dom_client);
3504415cd19SGarrett Wollman 	ypb->dom_client = NULL;
3514415cd19SGarrett Wollman 	ypb->dom_socket = -1;
3524415cd19SGarrett Wollman }
3534415cd19SGarrett Wollman 
3544415cd19SGarrett Wollman int
3554415cd19SGarrett Wollman yp_bind(dom)
3564415cd19SGarrett Wollman char *dom;
3574415cd19SGarrett Wollman {
3584415cd19SGarrett Wollman 	return _yp_dobind(dom, NULL);
3594415cd19SGarrett Wollman }
3604415cd19SGarrett Wollman 
3614415cd19SGarrett Wollman void
3624415cd19SGarrett Wollman yp_unbind(dom)
3634415cd19SGarrett Wollman char *dom;
3644415cd19SGarrett Wollman {
3654415cd19SGarrett Wollman 	struct dom_binding *ypb, *ypbp;
3664415cd19SGarrett Wollman 
3674415cd19SGarrett Wollman 	ypbp = NULL;
3684415cd19SGarrett Wollman 	for(ypb=_ypbindlist; ypb; ypb=ypb->dom_pnext) {
3694415cd19SGarrett Wollman 		if( strcmp(dom, ypb->dom_domain) == 0) {
3704415cd19SGarrett Wollman 			clnt_destroy(ypb->dom_client);
3714415cd19SGarrett Wollman 			if(ypbp)
3724415cd19SGarrett Wollman 				ypbp->dom_pnext = ypb->dom_pnext;
3734415cd19SGarrett Wollman 			else
3744415cd19SGarrett Wollman 				_ypbindlist = ypb->dom_pnext;
3754415cd19SGarrett Wollman 			free(ypb);
3764415cd19SGarrett Wollman 			return;
3774415cd19SGarrett Wollman 		}
3784415cd19SGarrett Wollman 		ypbp = ypb;
3794415cd19SGarrett Wollman 	}
3804415cd19SGarrett Wollman 	return;
3814415cd19SGarrett Wollman }
3824415cd19SGarrett Wollman 
3834415cd19SGarrett Wollman int
3844415cd19SGarrett Wollman yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
3854415cd19SGarrett Wollman char *indomain;
3864415cd19SGarrett Wollman char *inmap;
3874415cd19SGarrett Wollman const char *inkey;
3884415cd19SGarrett Wollman int inkeylen;
3894415cd19SGarrett Wollman char **outval;
3904415cd19SGarrett Wollman int *outvallen;
3914415cd19SGarrett Wollman {
3924415cd19SGarrett Wollman 	struct dom_binding *ysd;
3934415cd19SGarrett Wollman 	struct ypresp_val yprv;
3944415cd19SGarrett Wollman 	struct timeval tv;
3954415cd19SGarrett Wollman 	struct ypreq_key yprk;
3964415cd19SGarrett Wollman 	int r;
3974415cd19SGarrett Wollman 
3984415cd19SGarrett Wollman 	*outval = NULL;
3994415cd19SGarrett Wollman 	*outvallen = 0;
4004415cd19SGarrett Wollman 
40106643071SBill Paul 	/* Sanity check */
402e17334c3SBill Paul 
40306643071SBill Paul 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
40406643071SBill Paul 	    inmap == NULL || !strlen(inmap) ||
40506643071SBill Paul 	    indomain == NULL || !strlen(indomain))
40606643071SBill Paul 		return YPERR_BADARGS;
407e17334c3SBill Paul 
4084415cd19SGarrett Wollman again:
4094415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
4104415cd19SGarrett Wollman 		return YPERR_DOMAIN;
4114415cd19SGarrett Wollman 
4124415cd19SGarrett Wollman #ifdef YPMATCHCACHE
4134415cd19SGarrett Wollman 	if( !strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
4144415cd19SGarrett Wollman 	    inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
4154415cd19SGarrett Wollman 		*outvallen = yprv.valdat.dsize;
4164415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
4174415cd19SGarrett Wollman 		bcopy(yprv.valdat.dptr, *outval, *outvallen);
4184415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
4194415cd19SGarrett Wollman 		return 0;
4204415cd19SGarrett Wollman 	}
4214415cd19SGarrett Wollman #endif
4224415cd19SGarrett Wollman 
4234415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
4244415cd19SGarrett Wollman 	tv.tv_usec = 0;
4254415cd19SGarrett Wollman 
4264415cd19SGarrett Wollman 	yprk.domain = indomain;
4274415cd19SGarrett Wollman 	yprk.map = inmap;
4284415cd19SGarrett Wollman 	yprk.keydat.dptr = (char *)inkey;
4294415cd19SGarrett Wollman 	yprk.keydat.dsize = inkeylen;
4304415cd19SGarrett Wollman 
4314415cd19SGarrett Wollman 	bzero((char *)&yprv, sizeof yprv);
4324415cd19SGarrett Wollman 
4334415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_MATCH,
4344415cd19SGarrett Wollman 		xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
4354415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
4364415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_match: clnt_call");
4374415cd19SGarrett Wollman 		ysd->dom_vers = -1;
4384415cd19SGarrett Wollman 		goto again;
4394415cd19SGarrett Wollman 	}
4404415cd19SGarrett Wollman 	if( !(r=ypprot_err(yprv.status)) ) {
4414415cd19SGarrett Wollman 		*outvallen = yprv.valdat.dsize;
4424415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
4434415cd19SGarrett Wollman 		bcopy(yprv.valdat.dptr, *outval, *outvallen);
4444415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
4454415cd19SGarrett Wollman #ifdef YPMATCHCACHE
4464415cd19SGarrett Wollman 		if( strcmp(_yp_domain, indomain)==0 )
4474415cd19SGarrett Wollman 			 ypmatch_add(inmap, inkey, inkeylen, *outval, *outvallen);
4484415cd19SGarrett Wollman #endif
4494415cd19SGarrett Wollman 	}
4504415cd19SGarrett Wollman 	xdr_free(xdr_ypresp_val, (char *)&yprv);
4514415cd19SGarrett Wollman 	_yp_unbind(ysd);
4524415cd19SGarrett Wollman 	return r;
4534415cd19SGarrett Wollman }
4544415cd19SGarrett Wollman 
4554415cd19SGarrett Wollman int
4564415cd19SGarrett Wollman yp_get_default_domain(domp)
4574415cd19SGarrett Wollman char **domp;
4584415cd19SGarrett Wollman {
4594415cd19SGarrett Wollman 	*domp = NULL;
4604415cd19SGarrett Wollman 	if(_yp_domain[0] == '\0')
4614415cd19SGarrett Wollman 		if( getdomainname(_yp_domain, sizeof _yp_domain))
4624415cd19SGarrett Wollman 			return YPERR_NODOM;
4634415cd19SGarrett Wollman 	*domp = _yp_domain;
4644415cd19SGarrett Wollman 	return 0;
4654415cd19SGarrett Wollman }
4664415cd19SGarrett Wollman 
4674415cd19SGarrett Wollman int
4684415cd19SGarrett Wollman yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
4694415cd19SGarrett Wollman char *indomain;
4704415cd19SGarrett Wollman char *inmap;
4714415cd19SGarrett Wollman char **outkey;
4724415cd19SGarrett Wollman int *outkeylen;
4734415cd19SGarrett Wollman char **outval;
4744415cd19SGarrett Wollman int *outvallen;
4754415cd19SGarrett Wollman {
4764415cd19SGarrett Wollman 	struct ypresp_key_val yprkv;
4774415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
4784415cd19SGarrett Wollman 	struct dom_binding *ysd;
4794415cd19SGarrett Wollman 	struct timeval tv;
4804415cd19SGarrett Wollman 	int r;
4814415cd19SGarrett Wollman 
48206643071SBill Paul 	/* Sanity check */
48306643071SBill Paul 
48406643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
48506643071SBill Paul 	    inmap == NULL || !strlen(inmap))
48606643071SBill Paul 		return YPERR_BADARGS;
48706643071SBill Paul 
4884415cd19SGarrett Wollman 	*outkey = *outval = NULL;
4894415cd19SGarrett Wollman 	*outkeylen = *outvallen = 0;
4904415cd19SGarrett Wollman 
4914415cd19SGarrett Wollman again:
4924415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
4934415cd19SGarrett Wollman 		return YPERR_DOMAIN;
4944415cd19SGarrett Wollman 
4954415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
4964415cd19SGarrett Wollman 	tv.tv_usec = 0;
4974415cd19SGarrett Wollman 
4984415cd19SGarrett Wollman 	yprnk.domain = indomain;
4994415cd19SGarrett Wollman 	yprnk.map = inmap;
5004415cd19SGarrett Wollman 	bzero((char *)&yprkv, sizeof yprkv);
5014415cd19SGarrett Wollman 
5024415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
5034415cd19SGarrett Wollman 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv);
5044415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
5054415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_first: clnt_call");
5062943bd1cSBill Paul 		ysd->dom_vers = 0;
5074415cd19SGarrett Wollman 		goto again;
5084415cd19SGarrett Wollman 	}
5094415cd19SGarrett Wollman 	if( !(r=ypprot_err(yprkv.status)) ) {
5104415cd19SGarrett Wollman 		*outkeylen = yprkv.keydat.dsize;
5114415cd19SGarrett Wollman 		*outkey = (char *)malloc(*outkeylen+1);
5124415cd19SGarrett Wollman 		bcopy(yprkv.keydat.dptr, *outkey, *outkeylen);
5134415cd19SGarrett Wollman 		(*outkey)[*outkeylen] = '\0';
5144415cd19SGarrett Wollman 		*outvallen = yprkv.valdat.dsize;
5154415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
5164415cd19SGarrett Wollman 		bcopy(yprkv.valdat.dptr, *outval, *outvallen);
5174415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
5184415cd19SGarrett Wollman 	}
5194415cd19SGarrett Wollman 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
5204415cd19SGarrett Wollman 	_yp_unbind(ysd);
5214415cd19SGarrett Wollman 	return r;
5224415cd19SGarrett Wollman }
5234415cd19SGarrett Wollman 
5244415cd19SGarrett Wollman int
5254415cd19SGarrett Wollman yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
5264415cd19SGarrett Wollman char *indomain;
5274415cd19SGarrett Wollman char *inmap;
5284415cd19SGarrett Wollman char *inkey;
5294415cd19SGarrett Wollman int inkeylen;
5304415cd19SGarrett Wollman char **outkey;
5314415cd19SGarrett Wollman int *outkeylen;
5324415cd19SGarrett Wollman char **outval;
5334415cd19SGarrett Wollman int *outvallen;
5344415cd19SGarrett Wollman {
5354415cd19SGarrett Wollman 	struct ypresp_key_val yprkv;
5364415cd19SGarrett Wollman 	struct ypreq_key yprk;
5374415cd19SGarrett Wollman 	struct dom_binding *ysd;
5384415cd19SGarrett Wollman 	struct timeval tv;
5394415cd19SGarrett Wollman 	int r;
5404415cd19SGarrett Wollman 
54106643071SBill Paul 	/* Sanity check */
54206643071SBill Paul 
54306643071SBill Paul 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
54406643071SBill Paul 	    inmap == NULL || !strlen(inmap) ||
54506643071SBill Paul 	    indomain == NULL || !strlen(indomain))
54606643071SBill Paul 		return YPERR_BADARGS;
54706643071SBill Paul 
5484415cd19SGarrett Wollman 	*outkey = *outval = NULL;
5494415cd19SGarrett Wollman 	*outkeylen = *outvallen = 0;
5504415cd19SGarrett Wollman 
5514415cd19SGarrett Wollman again:
5524415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
5534415cd19SGarrett Wollman 		return YPERR_DOMAIN;
5544415cd19SGarrett Wollman 
5554415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
5564415cd19SGarrett Wollman 	tv.tv_usec = 0;
5574415cd19SGarrett Wollman 
5584415cd19SGarrett Wollman 	yprk.domain = indomain;
5594415cd19SGarrett Wollman 	yprk.map = inmap;
5604415cd19SGarrett Wollman 	yprk.keydat.dptr = inkey;
5614415cd19SGarrett Wollman 	yprk.keydat.dsize = inkeylen;
5624415cd19SGarrett Wollman 	bzero((char *)&yprkv, sizeof yprkv);
5634415cd19SGarrett Wollman 
5644415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_NEXT,
5654415cd19SGarrett Wollman 		xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv);
5664415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
5674415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_next: clnt_call");
5684415cd19SGarrett Wollman 		ysd->dom_vers = -1;
5694415cd19SGarrett Wollman 		goto again;
5704415cd19SGarrett Wollman 	}
5714415cd19SGarrett Wollman 	if( !(r=ypprot_err(yprkv.status)) ) {
5724415cd19SGarrett Wollman 		*outkeylen = yprkv.keydat.dsize;
5734415cd19SGarrett Wollman 		*outkey = (char *)malloc(*outkeylen+1);
5744415cd19SGarrett Wollman 		bcopy(yprkv.keydat.dptr, *outkey, *outkeylen);
5754415cd19SGarrett Wollman 		(*outkey)[*outkeylen] = '\0';
5764415cd19SGarrett Wollman 		*outvallen = yprkv.valdat.dsize;
5774415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
5784415cd19SGarrett Wollman 		bcopy(yprkv.valdat.dptr, *outval, *outvallen);
5794415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
5804415cd19SGarrett Wollman 	}
5814415cd19SGarrett Wollman 	xdr_free(xdr_ypresp_key_val, (char *)&yprkv);
5824415cd19SGarrett Wollman 	_yp_unbind(ysd);
5834415cd19SGarrett Wollman 	return r;
5844415cd19SGarrett Wollman }
5854415cd19SGarrett Wollman 
5864415cd19SGarrett Wollman int
5874415cd19SGarrett Wollman yp_all(indomain, inmap, incallback)
5884415cd19SGarrett Wollman char *indomain;
5894415cd19SGarrett Wollman char *inmap;
5904415cd19SGarrett Wollman struct ypall_callback *incallback;
5914415cd19SGarrett Wollman {
5924415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
5934415cd19SGarrett Wollman 	struct dom_binding *ysd;
5944415cd19SGarrett Wollman 	struct timeval tv;
5954415cd19SGarrett Wollman 	struct sockaddr_in clnt_sin;
5964415cd19SGarrett Wollman 	CLIENT *clnt;
5974415cd19SGarrett Wollman 	u_long status;
5984415cd19SGarrett Wollman 	int clnt_sock;
5994415cd19SGarrett Wollman 
60006643071SBill Paul 	/* Sanity check */
60106643071SBill Paul 
60206643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
60306643071SBill Paul 	    inmap == NULL || !strlen(inmap))
60406643071SBill Paul 		return YPERR_BADARGS;
60506643071SBill Paul 
6064415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
6074415cd19SGarrett Wollman 		return YPERR_DOMAIN;
6084415cd19SGarrett Wollman 
6094415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
6104415cd19SGarrett Wollman 	tv.tv_usec = 0;
6114415cd19SGarrett Wollman 	clnt_sock = RPC_ANYSOCK;
6124415cd19SGarrett Wollman 	clnt_sin = ysd->dom_server_addr;
6134415cd19SGarrett Wollman 	clnt_sin.sin_port = 0;
6144415cd19SGarrett Wollman 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
6154415cd19SGarrett Wollman 	if(clnt==NULL) {
6164415cd19SGarrett Wollman 		printf("clnttcp_create failed\n");
6174415cd19SGarrett Wollman 		return YPERR_PMAP;
6184415cd19SGarrett Wollman 	}
6194415cd19SGarrett Wollman 
6204415cd19SGarrett Wollman 	yprnk.domain = indomain;
6214415cd19SGarrett Wollman 	yprnk.map = inmap;
6224415cd19SGarrett Wollman 	ypresp_allfn = incallback->foreach;
6234415cd19SGarrett Wollman 	ypresp_data = (void *)incallback->data;
6244415cd19SGarrett Wollman 
6254415cd19SGarrett Wollman 	(void) clnt_call(clnt, YPPROC_ALL,
6264415cd19SGarrett Wollman 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv);
6274415cd19SGarrett Wollman 	clnt_destroy(clnt);
6284415cd19SGarrett Wollman 	xdr_free(xdr_ypresp_all_seq, (char *)&status);	/* not really needed... */
6294415cd19SGarrett Wollman 	_yp_unbind(ysd);
6304415cd19SGarrett Wollman 
6314415cd19SGarrett Wollman 	if(status != YP_FALSE)
6324415cd19SGarrett Wollman 		return ypprot_err(status);
6334415cd19SGarrett Wollman 	return 0;
6344415cd19SGarrett Wollman }
6354415cd19SGarrett Wollman 
6364415cd19SGarrett Wollman int
6374415cd19SGarrett Wollman yp_order(indomain, inmap, outorder)
6384415cd19SGarrett Wollman char *indomain;
6394415cd19SGarrett Wollman char *inmap;
6404415cd19SGarrett Wollman int *outorder;
6414415cd19SGarrett Wollman {
6424415cd19SGarrett Wollman  	struct dom_binding *ysd;
6434415cd19SGarrett Wollman 	struct ypresp_order ypro;
6444415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
6454415cd19SGarrett Wollman 	struct timeval tv;
6464415cd19SGarrett Wollman 	int r;
6474415cd19SGarrett Wollman 
64806643071SBill Paul 	/* Sanity check */
64906643071SBill Paul 
65006643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
65106643071SBill Paul 	    inmap == NULL || !strlen(inmap))
65206643071SBill Paul 		return YPERR_BADARGS;
65306643071SBill Paul 
6544415cd19SGarrett Wollman again:
6554415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
6564415cd19SGarrett Wollman 		return YPERR_DOMAIN;
6574415cd19SGarrett Wollman 
6584415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
6594415cd19SGarrett Wollman 	tv.tv_usec = 0;
6604415cd19SGarrett Wollman 
6614415cd19SGarrett Wollman 	yprnk.domain = indomain;
6624415cd19SGarrett Wollman 	yprnk.map = inmap;
6634415cd19SGarrett Wollman 
6644415cd19SGarrett Wollman 	bzero((char *)(char *)&ypro, sizeof ypro);
6654415cd19SGarrett Wollman 
6664415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_ORDER,
6674415cd19SGarrett Wollman 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv);
6684415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
6694415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_order: clnt_call");
6704415cd19SGarrett Wollman 		ysd->dom_vers = -1;
6714415cd19SGarrett Wollman 		goto again;
6724415cd19SGarrett Wollman 	}
6734415cd19SGarrett Wollman 
6744415cd19SGarrett Wollman 	*outorder = ypro.ordernum;
6754415cd19SGarrett Wollman 	xdr_free(xdr_ypresp_order, (char *)&ypro);
6764415cd19SGarrett Wollman 	_yp_unbind(ysd);
6774415cd19SGarrett Wollman 	return ypprot_err(ypro.status);
6784415cd19SGarrett Wollman }
6794415cd19SGarrett Wollman 
6804415cd19SGarrett Wollman int
6814415cd19SGarrett Wollman yp_master(indomain, inmap, outname)
6824415cd19SGarrett Wollman char *indomain;
6834415cd19SGarrett Wollman char *inmap;
6844415cd19SGarrett Wollman char **outname;
6854415cd19SGarrett Wollman {
6864415cd19SGarrett Wollman 	struct dom_binding *ysd;
6874415cd19SGarrett Wollman 	struct ypresp_master yprm;
6884415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
6894415cd19SGarrett Wollman 	struct timeval tv;
6904415cd19SGarrett Wollman 	int r;
6914415cd19SGarrett Wollman 
69206643071SBill Paul 	/* Sanity check */
69306643071SBill Paul 
69406643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
69506643071SBill Paul 	    inmap == NULL || !strlen(inmap))
69606643071SBill Paul 		return YPERR_BADARGS;
6974415cd19SGarrett Wollman again:
6984415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
6994415cd19SGarrett Wollman 		return YPERR_DOMAIN;
7004415cd19SGarrett Wollman 
7014415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
7024415cd19SGarrett Wollman 	tv.tv_usec = 0;
7034415cd19SGarrett Wollman 
7044415cd19SGarrett Wollman 	yprnk.domain = indomain;
7054415cd19SGarrett Wollman 	yprnk.map = inmap;
7064415cd19SGarrett Wollman 
7074415cd19SGarrett Wollman 	bzero((char *)&yprm, sizeof yprm);
7084415cd19SGarrett Wollman 
7094415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_MASTER,
7104415cd19SGarrett Wollman 		xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
7114415cd19SGarrett Wollman 	if(r != RPC_SUCCESS) {
7124415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_master: clnt_call");
7134415cd19SGarrett Wollman 		ysd->dom_vers = -1;
7144415cd19SGarrett Wollman 		goto again;
7154415cd19SGarrett Wollman 	}
7164415cd19SGarrett Wollman 	if( !(r=ypprot_err(yprm.status)) ) {
7174415cd19SGarrett Wollman 		*outname = (char *)strdup(yprm.master);
7184415cd19SGarrett Wollman 	}
7194415cd19SGarrett Wollman 	xdr_free(xdr_ypresp_master, (char *)&yprm);
7204415cd19SGarrett Wollman 	_yp_unbind(ysd);
7214415cd19SGarrett Wollman 	return r;
7224415cd19SGarrett Wollman }
7234415cd19SGarrett Wollman 
7244415cd19SGarrett Wollman yp_maplist(indomain, outmaplist)
7254415cd19SGarrett Wollman char *indomain;
7264415cd19SGarrett Wollman struct ypmaplist **outmaplist;
7274415cd19SGarrett Wollman {
7284415cd19SGarrett Wollman 	struct dom_binding *ysd;
7294415cd19SGarrett Wollman 	struct ypresp_maplist ypml;
7304415cd19SGarrett Wollman 	struct timeval tv;
7314415cd19SGarrett Wollman 	int r;
7324415cd19SGarrett Wollman 
73306643071SBill Paul 	/* Sanity check */
73406643071SBill Paul 
73506643071SBill Paul 	if (indomain == NULL || !strlen(indomain))
73606643071SBill Paul 		return YPERR_BADARGS;
73706643071SBill Paul 
7384415cd19SGarrett Wollman again:
7394415cd19SGarrett Wollman 	if( _yp_dobind(indomain, &ysd) != 0)
7404415cd19SGarrett Wollman 		return YPERR_DOMAIN;
7414415cd19SGarrett Wollman 
7424415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
7434415cd19SGarrett Wollman 	tv.tv_usec = 0;
7444415cd19SGarrett Wollman 
7454415cd19SGarrett Wollman 	bzero((char *)&ypml, sizeof ypml);
7464415cd19SGarrett Wollman 
7474415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
7484415cd19SGarrett Wollman 		xdr_domainname, indomain, xdr_ypresp_maplist, &ypml, tv);
7494415cd19SGarrett Wollman 	if (r != RPC_SUCCESS) {
7504415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
7514415cd19SGarrett Wollman 		ysd->dom_vers = -1;
7524415cd19SGarrett Wollman 		goto again;
7534415cd19SGarrett Wollman 	}
7544415cd19SGarrett Wollman 	*outmaplist = ypml.list;
7554415cd19SGarrett Wollman 	/* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
7564415cd19SGarrett Wollman 	_yp_unbind(ysd);
7574415cd19SGarrett Wollman 	return ypprot_err(ypml.status);
7584415cd19SGarrett Wollman }
7594415cd19SGarrett Wollman 
7604415cd19SGarrett Wollman char *
7614415cd19SGarrett Wollman yperr_string(incode)
7624415cd19SGarrett Wollman int incode;
7634415cd19SGarrett Wollman {
7644415cd19SGarrett Wollman 	static char err[80];
7654415cd19SGarrett Wollman 
7664415cd19SGarrett Wollman 	switch(incode) {
7674415cd19SGarrett Wollman 	case 0:
7684415cd19SGarrett Wollman 		return "Success";
7694415cd19SGarrett Wollman 	case YPERR_BADARGS:
7704415cd19SGarrett Wollman 		return "Request arguments bad";
7714415cd19SGarrett Wollman 	case YPERR_RPC:
7724415cd19SGarrett Wollman 		return "RPC failure";
7734415cd19SGarrett Wollman 	case YPERR_DOMAIN:
7744415cd19SGarrett Wollman 		return "Can't bind to server which serves this domain";
7754415cd19SGarrett Wollman 	case YPERR_MAP:
7764415cd19SGarrett Wollman 		return "No such map in server's domain";
7774415cd19SGarrett Wollman 	case YPERR_KEY:
7784415cd19SGarrett Wollman 		return "No such key in map";
7794415cd19SGarrett Wollman 	case YPERR_YPERR:
7804415cd19SGarrett Wollman 		return "YP server error";
7814415cd19SGarrett Wollman 	case YPERR_RESRC:
7824415cd19SGarrett Wollman 		return "Local resource allocation failure";
7834415cd19SGarrett Wollman 	case YPERR_NOMORE:
7844415cd19SGarrett Wollman 		return "No more records in map database";
7854415cd19SGarrett Wollman 	case YPERR_PMAP:
7864415cd19SGarrett Wollman 		return "Can't communicate with portmapper";
7874415cd19SGarrett Wollman 	case YPERR_YPBIND:
7884415cd19SGarrett Wollman 		return "Can't communicate with ypbind";
7894415cd19SGarrett Wollman 	case YPERR_YPSERV:
7904415cd19SGarrett Wollman 		return "Can't communicate with ypserv";
7914415cd19SGarrett Wollman 	case YPERR_NODOM:
7924415cd19SGarrett Wollman 		return "Local domain name not set";
7934415cd19SGarrett Wollman 	case YPERR_BADDB:
7944415cd19SGarrett Wollman 		return "Server data base is bad";
7954415cd19SGarrett Wollman 	case YPERR_VERS:
7964415cd19SGarrett Wollman 		return "YP server version mismatch - server can't supply service.";
7974415cd19SGarrett Wollman 	case YPERR_ACCESS:
7984415cd19SGarrett Wollman 		return "Access violation";
7994415cd19SGarrett Wollman 	case YPERR_BUSY:
8004415cd19SGarrett Wollman 		return "Database is busy";
8014415cd19SGarrett Wollman 	}
8024415cd19SGarrett Wollman 	sprintf(err, "YP unknown error %d\n", incode);
8034415cd19SGarrett Wollman 	return err;
8044415cd19SGarrett Wollman }
8054415cd19SGarrett Wollman 
8064415cd19SGarrett Wollman int
8074415cd19SGarrett Wollman ypprot_err(incode)
8084415cd19SGarrett Wollman unsigned int incode;
8094415cd19SGarrett Wollman {
8104415cd19SGarrett Wollman 	switch(incode) {
8114415cd19SGarrett Wollman 	case YP_TRUE:
8124415cd19SGarrett Wollman 		return 0;
8134415cd19SGarrett Wollman 	case YP_FALSE:
8144415cd19SGarrett Wollman 		return YPERR_YPBIND;
8154415cd19SGarrett Wollman 	case YP_NOMORE:
8164415cd19SGarrett Wollman 		return YPERR_NOMORE;
8174415cd19SGarrett Wollman 	case YP_NOMAP:
8184415cd19SGarrett Wollman 		return YPERR_MAP;
8194415cd19SGarrett Wollman 	case YP_NODOM:
8204415cd19SGarrett Wollman 		return YPERR_NODOM;
8214415cd19SGarrett Wollman 	case YP_NOKEY:
8224415cd19SGarrett Wollman 		return YPERR_KEY;
8234415cd19SGarrett Wollman 	case YP_BADOP:
8244415cd19SGarrett Wollman 		return YPERR_YPERR;
8254415cd19SGarrett Wollman 	case YP_BADDB:
8264415cd19SGarrett Wollman 		return YPERR_BADDB;
8274415cd19SGarrett Wollman 	case YP_YPERR:
8284415cd19SGarrett Wollman 		return YPERR_YPERR;
8294415cd19SGarrett Wollman 	case YP_BADARGS:
8304415cd19SGarrett Wollman 		return YPERR_BADARGS;
8314415cd19SGarrett Wollman 	case YP_VERS:
8324415cd19SGarrett Wollman 		return YPERR_VERS;
8334415cd19SGarrett Wollman 	}
8344415cd19SGarrett Wollman 	return YPERR_YPERR;
8354415cd19SGarrett Wollman }
8364415cd19SGarrett Wollman 
8374415cd19SGarrett Wollman int
8384415cd19SGarrett Wollman _yp_check(dom)
8394415cd19SGarrett Wollman char **dom;
8404415cd19SGarrett Wollman {
8414415cd19SGarrett Wollman 	int use_yp = 0;
8424415cd19SGarrett Wollman 	char *unused;
8434415cd19SGarrett Wollman 
8444415cd19SGarrett Wollman 	if( _yp_domain[0]=='\0' )
8454415cd19SGarrett Wollman 		if( yp_get_default_domain(&unused) )
8464415cd19SGarrett Wollman 			return 0;
8474415cd19SGarrett Wollman 
8484415cd19SGarrett Wollman 	if(dom)
8494415cd19SGarrett Wollman 		*dom = _yp_domain;
8504415cd19SGarrett Wollman 
8514415cd19SGarrett Wollman 	if( yp_bind(_yp_domain)==0 )
8524415cd19SGarrett Wollman 		return 1;
8534415cd19SGarrett Wollman 	return 0;
8544415cd19SGarrett Wollman }
855