xref: /freebsd/lib/libc/yp/yplib.c (revision c1dfca84c6a50b86686bfcdaef839162015f60c0)
14415cd19SGarrett Wollman /*
24415cd19SGarrett Wollman  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
34e6ace08SBill Paul  * Copyright (c) 1998 Bill Paul <wpaul@ctr.columbia.edu>
44415cd19SGarrett Wollman  * All rights reserved.
54415cd19SGarrett Wollman  *
64415cd19SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
74415cd19SGarrett Wollman  * modification, are permitted provided that the following conditions
84415cd19SGarrett Wollman  * are met:
94415cd19SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
104415cd19SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
114415cd19SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
124415cd19SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
134415cd19SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
144415cd19SGarrett Wollman  * 3. The name of the author may not be used to endorse or promote
154415cd19SGarrett Wollman  *    products derived from this software without specific prior written
164415cd19SGarrett Wollman  *    permission.
174415cd19SGarrett Wollman  *
184415cd19SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
194415cd19SGarrett Wollman  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
204415cd19SGarrett Wollman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214415cd19SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
224415cd19SGarrett Wollman  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
234415cd19SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
244415cd19SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
254415cd19SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
264415cd19SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
274415cd19SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
284415cd19SGarrett Wollman  * SUCH DAMAGE.
294415cd19SGarrett Wollman  */
304415cd19SGarrett Wollman 
31333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
32333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
334415cd19SGarrett Wollman 
34d201fe46SDaniel Eischen #include "namespace.h"
35069eb2caSHajimu UMEMOTO #include "reentrant.h"
364415cd19SGarrett Wollman #include <sys/param.h>
374415cd19SGarrett Wollman #include <sys/types.h>
384415cd19SGarrett Wollman #include <sys/socket.h>
394415cd19SGarrett Wollman #include <sys/file.h>
404415cd19SGarrett Wollman #include <sys/uio.h>
41fd8e4ebcSMike Barcroft #include <arpa/inet.h>
424415cd19SGarrett Wollman #include <errno.h>
434415cd19SGarrett Wollman #include <stdio.h>
444415cd19SGarrett Wollman #include <string.h>
45b9729ac2SBill Paul #include <stdlib.h>
46b9729ac2SBill Paul #include <unistd.h>
474415cd19SGarrett Wollman #include <rpc/rpc.h>
484415cd19SGarrett Wollman #include <rpc/xdr.h>
490e276383SBill Paul #include <rpcsvc/yp.h>
50d201fe46SDaniel Eischen #include "un-namespace.h"
51d201fe46SDaniel Eischen #include "libc_private.h"
520e276383SBill Paul 
530e276383SBill Paul /*
540e276383SBill Paul  * We have to define these here due to clashes between yp_prot.h and
550e276383SBill Paul  * yp.h.
560e276383SBill Paul  */
570e276383SBill Paul 
584e6ace08SBill Paul #define YPMATCHCACHE
594e6ace08SBill Paul 
604e6ace08SBill Paul #ifdef YPMATCHCACHE
614e6ace08SBill Paul struct ypmatch_ent {
624e6ace08SBill Paul         char			*ypc_map;
634e6ace08SBill Paul 	keydat			ypc_key;
644e6ace08SBill Paul 	valdat			ypc_val;
654e6ace08SBill Paul         time_t			ypc_expire_t;
664e6ace08SBill Paul         struct ypmatch_ent	*ypc_next;
674e6ace08SBill Paul };
684e6ace08SBill Paul #define YPLIB_MAXCACHE	5	/* At most 5 entries */
694e6ace08SBill Paul #define YPLIB_EXPIRE	5	/* Expire after 5 seconds */
704e6ace08SBill Paul #endif
714e6ace08SBill Paul 
720e276383SBill Paul struct dom_binding {
730e276383SBill Paul         struct dom_binding *dom_pnext;
740e276383SBill Paul         char dom_domain[YPMAXDOMAIN + 1];
750e276383SBill Paul         struct sockaddr_in dom_server_addr;
760e276383SBill Paul         u_short dom_server_port;
770e276383SBill Paul         int dom_socket;
780e276383SBill Paul         CLIENT *dom_client;
79a2308772SBill Paul         u_short dom_local_port; /* now I finally know what this is for. */
800e276383SBill Paul         long dom_vers;
814e6ace08SBill Paul #ifdef YPMATCHCACHE
824e6ace08SBill Paul 	struct ypmatch_ent *cache;
834e6ace08SBill Paul 	int ypmatch_cachecnt;
844e6ace08SBill Paul #endif
850e276383SBill Paul };
860e276383SBill Paul 
874415cd19SGarrett Wollman #include <rpcsvc/ypclnt.h>
884415cd19SGarrett Wollman 
894415cd19SGarrett Wollman #ifndef BINDINGDIR
904415cd19SGarrett Wollman #define BINDINGDIR "/var/yp/binding"
914415cd19SGarrett Wollman #endif
92456ebbf8SBill Paul #define MAX_RETRIES 20
934415cd19SGarrett Wollman 
944415cd19SGarrett Wollman extern bool_t xdr_domainname(), xdr_ypbind_resp();
954415cd19SGarrett Wollman extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
964415cd19SGarrett Wollman extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val();
974415cd19SGarrett Wollman extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq();
984415cd19SGarrett Wollman extern bool_t xdr_ypresp_master();
994415cd19SGarrett Wollman 
1004415cd19SGarrett Wollman int (*ypresp_allfn)();
1014415cd19SGarrett Wollman void *ypresp_data;
1024415cd19SGarrett Wollman 
103ed4d1c46SDag-Erling Smørgrav static void _yp_unbind(struct dom_binding *);
1044415cd19SGarrett Wollman struct dom_binding *_ypbindlist;
1054415cd19SGarrett Wollman static char _yp_domain[MAXHOSTNAMELEN];
1062861f68dSSimon L. B. Nielsen int _yplib_timeout = 20;
1074415cd19SGarrett Wollman 
1081b482912SHajimu UMEMOTO static mutex_t _ypmutex = MUTEX_INITIALIZER;
109069eb2caSHajimu UMEMOTO #define YPLOCK()	mutex_lock(&_ypmutex);
110069eb2caSHajimu UMEMOTO #define YPUNLOCK()	mutex_unlock(&_ypmutex);
111069eb2caSHajimu UMEMOTO 
1124415cd19SGarrett Wollman #ifdef YPMATCHCACHE
113dc584ddbSDag-Erling Smørgrav static void
114dc584ddbSDag-Erling Smørgrav ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev,
115dc584ddbSDag-Erling Smørgrav     struct ypmatch_ent *cur)
1164415cd19SGarrett Wollman {
1174e6ace08SBill Paul 	if (prev == NULL)
1184e6ace08SBill Paul 		ypdb->cache = cur->ypc_next;
1194e6ace08SBill Paul 	else
1204e6ace08SBill Paul 		prev->ypc_next = cur->ypc_next;
1214e6ace08SBill Paul 
1224e6ace08SBill Paul 	free(cur->ypc_map);
1234e6ace08SBill Paul 	free(cur->ypc_key.keydat_val);
1244e6ace08SBill Paul 	free(cur->ypc_val.valdat_val);
1254e6ace08SBill Paul 	free(cur);
1264e6ace08SBill Paul 
1274e6ace08SBill Paul 	ypdb->ypmatch_cachecnt--;
1284e6ace08SBill Paul 
1294e6ace08SBill Paul 	return;
1304e6ace08SBill Paul }
1314e6ace08SBill Paul 
132dc584ddbSDag-Erling Smørgrav static void
133dc584ddbSDag-Erling Smørgrav ypmatch_cache_flush(struct dom_binding *ypdb)
1344e6ace08SBill Paul {
1354e6ace08SBill Paul 	struct ypmatch_ent	*n, *c = ypdb->cache;
1364e6ace08SBill Paul 
1374e6ace08SBill Paul 	while (c != NULL) {
1384e6ace08SBill Paul 		n = c->ypc_next;
1394e6ace08SBill Paul 		ypmatch_cache_delete(ypdb, NULL, c);
1404e6ace08SBill Paul 		c = n;
1414e6ace08SBill Paul 	}
1424e6ace08SBill Paul 
1434e6ace08SBill Paul 	return;
1444e6ace08SBill Paul }
1454e6ace08SBill Paul 
146dc584ddbSDag-Erling Smørgrav static void
147dc584ddbSDag-Erling Smørgrav ypmatch_cache_expire(struct dom_binding *ypdb)
1484e6ace08SBill Paul {
1494e6ace08SBill Paul 	struct ypmatch_ent	*c = ypdb->cache;
1504e6ace08SBill Paul 	struct ypmatch_ent	*n, *p = NULL;
1514415cd19SGarrett Wollman 	time_t			t;
1524415cd19SGarrett Wollman 
1534415cd19SGarrett Wollman 	time(&t);
1544415cd19SGarrett Wollman 
1554e6ace08SBill Paul 	while (c != NULL) {
1564e6ace08SBill Paul 		if (t >= c->ypc_expire_t) {
1574e6ace08SBill Paul 			n = c->ypc_next;
1584e6ace08SBill Paul 			ypmatch_cache_delete(ypdb, p, c);
1594e6ace08SBill Paul 			c = n;
1604415cd19SGarrett Wollman 		} else {
1614e6ace08SBill Paul 			p = c;
1624e6ace08SBill Paul 			c = c->ypc_next;
1634e6ace08SBill Paul 		}
1644415cd19SGarrett Wollman 	}
1654415cd19SGarrett Wollman 
1664e6ace08SBill Paul 	return;
1674415cd19SGarrett Wollman }
1684415cd19SGarrett Wollman 
169dc584ddbSDag-Erling Smørgrav static void
170dc584ddbSDag-Erling Smørgrav ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key,
171dc584ddbSDag-Erling Smørgrav     valdat *val)
1724415cd19SGarrett Wollman {
1734e6ace08SBill Paul 	struct ypmatch_ent	*new;
1744415cd19SGarrett Wollman 
1754e6ace08SBill Paul 	/* Do an expire run to maybe open up a slot. */
1764e6ace08SBill Paul 	if (ypdb->ypmatch_cachecnt)
1774e6ace08SBill Paul 		ypmatch_cache_expire(ypdb);
1784415cd19SGarrett Wollman 
1794e6ace08SBill Paul 	/*
1804e6ace08SBill Paul 	 * If there are no slots free, then force an expire of
1814e6ace08SBill Paul 	 * the least recently used entry.
1824e6ace08SBill Paul  	 */
1834e6ace08SBill Paul 	if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) {
1844e6ace08SBill Paul 		struct ypmatch_ent	*o = NULL, *c = ypdb->cache;
1854e6ace08SBill Paul 		time_t			oldest = 0;
1864415cd19SGarrett Wollman 
1874e6ace08SBill Paul 		oldest = ~oldest;
1884415cd19SGarrett Wollman 
1894e6ace08SBill Paul 		while (c != NULL) {
1904e6ace08SBill Paul 			if (c->ypc_expire_t < oldest) {
1914e6ace08SBill Paul 				oldest = c->ypc_expire_t;
1924e6ace08SBill Paul 				o = c;
1934415cd19SGarrett Wollman 			}
1944e6ace08SBill Paul 			c = c->ypc_next;
1954e6ace08SBill Paul 		}
1964e6ace08SBill Paul 
1974e6ace08SBill Paul 		if (o == NULL)
1984e6ace08SBill Paul 			return;
1994e6ace08SBill Paul 		o->ypc_expire_t = 0;
2004e6ace08SBill Paul 		ypmatch_cache_expire(ypdb);
2014e6ace08SBill Paul 	}
2024e6ace08SBill Paul 
2034e6ace08SBill Paul 	new = malloc(sizeof(struct ypmatch_ent));
2044e6ace08SBill Paul 	if (new == NULL)
2054e6ace08SBill Paul 		return;
2064e6ace08SBill Paul 
2074e6ace08SBill Paul 	new->ypc_map = strdup(map);
2084e6ace08SBill Paul 	if (new->ypc_map == NULL) {
2094e6ace08SBill Paul 		free(new);
2104e6ace08SBill Paul 		return;
2114e6ace08SBill Paul 	}
2124e6ace08SBill Paul 	new->ypc_key.keydat_val = malloc(key->keydat_len);
2134e6ace08SBill Paul 	if (new->ypc_key.keydat_val == NULL) {
2144e6ace08SBill Paul 		free(new->ypc_map);
2154e6ace08SBill Paul 		free(new);
2164e6ace08SBill Paul 		return;
2174e6ace08SBill Paul 	}
2184e6ace08SBill Paul 	new->ypc_val.valdat_val = malloc(val->valdat_len);
2194e6ace08SBill Paul 	if (new->ypc_val.valdat_val == NULL) {
2204e6ace08SBill Paul 		free(new->ypc_val.valdat_val);
2214e6ace08SBill Paul 		free(new->ypc_map);
2224e6ace08SBill Paul 		free(new);
2234e6ace08SBill Paul 		return;
2244e6ace08SBill Paul 	}
2254e6ace08SBill Paul 
2264e6ace08SBill Paul 	new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE;
2274e6ace08SBill Paul 	new->ypc_key.keydat_len = key->keydat_len;
2284e6ace08SBill Paul 	new->ypc_val.valdat_len = val->valdat_len;
2294e6ace08SBill Paul 	bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len);
2304e6ace08SBill Paul 	bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len);
2314e6ace08SBill Paul 
2324e6ace08SBill Paul 	new->ypc_next = ypdb->cache;
2334e6ace08SBill Paul 	ypdb->cache = new;
2344e6ace08SBill Paul 
2354e6ace08SBill Paul 	ypdb->ypmatch_cachecnt++;
2364e6ace08SBill Paul 
2374e6ace08SBill Paul 	return;
2384e6ace08SBill Paul }
2394e6ace08SBill Paul 
240dc584ddbSDag-Erling Smørgrav static bool_t
241dc584ddbSDag-Erling Smørgrav ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key,
242dc584ddbSDag-Erling Smørgrav     valdat *val)
2434e6ace08SBill Paul {
2440c0349bfSGarrett Wollman 	struct ypmatch_ent	*c;
2454e6ace08SBill Paul 
2464e6ace08SBill Paul 	ypmatch_cache_expire(ypdb);
2474e6ace08SBill Paul 
2484e6ace08SBill Paul 	for (c = ypdb->cache; c != NULL; c = c->ypc_next) {
2494e6ace08SBill Paul 		if (strcmp(map, c->ypc_map))
2504e6ace08SBill Paul 			continue;
2514e6ace08SBill Paul 		if (key->keydat_len != c->ypc_key.keydat_len)
2524e6ace08SBill Paul 			continue;
2534e6ace08SBill Paul 		if (bcmp(key->keydat_val, c->ypc_key.keydat_val,
2544e6ace08SBill Paul 				key->keydat_len))
2554e6ace08SBill Paul 			continue;
2564e6ace08SBill Paul 	}
2574e6ace08SBill Paul 
2584e6ace08SBill Paul 	if (c == NULL)
2594e6ace08SBill Paul 		return(FALSE);
2604e6ace08SBill Paul 
2614e6ace08SBill Paul 	val->valdat_len = c->ypc_val.valdat_len;
2624e6ace08SBill Paul 	val->valdat_val = c->ypc_val.valdat_val;
2634e6ace08SBill Paul 
2644e6ace08SBill Paul 	return(TRUE);
2654415cd19SGarrett Wollman }
2664415cd19SGarrett Wollman #endif
2674415cd19SGarrett Wollman 
268f249dbccSDag-Erling Smørgrav const char *
269dc584ddbSDag-Erling Smørgrav ypbinderr_string(int incode)
27064416168SBill Paul {
27164416168SBill Paul 	static char err[80];
27264416168SBill Paul 	switch (incode) {
27364416168SBill Paul 	case 0:
274ed4d1c46SDag-Erling Smørgrav 		return ("Success");
27562967ca2SBill Paul 	case YPBIND_ERR_ERR:
276ed4d1c46SDag-Erling Smørgrav 		return ("Internal ypbind error");
27762967ca2SBill Paul 	case YPBIND_ERR_NOSERV:
278ed4d1c46SDag-Erling Smørgrav 		return ("Domain not bound");
27962967ca2SBill Paul 	case YPBIND_ERR_RESC:
280ed4d1c46SDag-Erling Smørgrav 		return ("System resource allocation failure");
28164416168SBill Paul 	}
28262967ca2SBill Paul 	sprintf(err, "Unknown ypbind error: #%d\n", incode);
283ed4d1c46SDag-Erling Smørgrav 	return (err);
28464416168SBill Paul }
28564416168SBill Paul 
2864415cd19SGarrett Wollman int
287dc584ddbSDag-Erling Smørgrav _yp_dobind(char *dom, struct dom_binding **ypdb)
2884415cd19SGarrett Wollman {
289e75ad74aSJames Raynard 	static pid_t pid = -1;
2904415cd19SGarrett Wollman 	char path[MAXPATHLEN];
2914415cd19SGarrett Wollman 	struct dom_binding *ysd, *ysd2;
2929d80b1ddSBill Paul 	struct ypbind_resp ypbr;
2934415cd19SGarrett Wollman 	struct timeval tv;
2944415cd19SGarrett Wollman 	struct sockaddr_in clnt_sin;
29551295a4dSJordan K. Hubbard 	int clnt_sock, fd;
296ccbcef60SJames Raynard 	pid_t gpid;
2974415cd19SGarrett Wollman 	CLIENT *client;
2984415cd19SGarrett Wollman 	int new = 0, r;
299456ebbf8SBill Paul 	int retries = 0;
300a2308772SBill Paul 	struct sockaddr_in check;
301595e5323SStefan Farfeleder 	socklen_t checklen = sizeof(struct sockaddr_in);
3024415cd19SGarrett Wollman 
3036e8caff7SBill Paul 	/* Not allowed; bad doggie. Bad. */
3046e8caff7SBill Paul 	if (strchr(dom, '/') != NULL)
3056e8caff7SBill Paul 		return(YPERR_BADARGS);
3066e8caff7SBill Paul 
3074415cd19SGarrett Wollman 	gpid = getpid();
3084415cd19SGarrett Wollman 	if (!(pid == -1 || pid == gpid)) {
3094415cd19SGarrett Wollman 		ysd = _ypbindlist;
3104415cd19SGarrett Wollman 		while (ysd) {
3119fa75c15SBill Paul 			if (ysd->dom_client != NULL)
312cc64a2bfSBill Paul 				_yp_unbind(ysd);
3134415cd19SGarrett Wollman 			ysd2 = ysd->dom_pnext;
3144415cd19SGarrett Wollman 			free(ysd);
3154415cd19SGarrett Wollman 			ysd = ysd2;
3164415cd19SGarrett Wollman 		}
3174415cd19SGarrett Wollman 		_ypbindlist = NULL;
3184415cd19SGarrett Wollman 	}
3194415cd19SGarrett Wollman 	pid = gpid;
3204415cd19SGarrett Wollman 
3214415cd19SGarrett Wollman 	if (ypdb != NULL)
3224415cd19SGarrett Wollman 		*ypdb = NULL;
3234415cd19SGarrett Wollman 
3244415cd19SGarrett Wollman 	if (dom == NULL || strlen(dom) == 0)
325ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADARGS);
3264415cd19SGarrett Wollman 
3274415cd19SGarrett Wollman 	for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
3284415cd19SGarrett Wollman 		if (strcmp(dom, ysd->dom_domain) == 0)
3294415cd19SGarrett Wollman 			break;
330a169c9b1SBill Paul 
331a2308772SBill Paul 
3324415cd19SGarrett Wollman 	if (ysd == NULL) {
3334415cd19SGarrett Wollman 		ysd = (struct dom_binding *)malloc(sizeof *ysd);
3346890d156SGuy Helmer 		if (ysd == NULL)
3356890d156SGuy Helmer 			return (YPERR_RESRC);
3364415cd19SGarrett Wollman 		bzero((char *)ysd, sizeof *ysd);
3374415cd19SGarrett Wollman 		ysd->dom_socket = -1;
3384415cd19SGarrett Wollman 		ysd->dom_vers = 0;
3394415cd19SGarrett Wollman 		new = 1;
340a2308772SBill Paul 	} else {
341a2308772SBill Paul 	/* Check the socket -- may have been hosed by the caller. */
342d201fe46SDaniel Eischen 		if (_getsockname(ysd->dom_socket, (struct sockaddr *)&check,
343a2308772SBill Paul 		    &checklen) == -1 || check.sin_family != AF_INET ||
344a2308772SBill Paul 		    check.sin_port != ysd->dom_local_port) {
345a2308772SBill Paul 		/* Socket became bogus somehow... need to rebind. */
346a2308772SBill Paul 			int save, sock;
347a2308772SBill Paul 
348a2308772SBill Paul 			sock = ysd->dom_socket;
349d201fe46SDaniel Eischen 			save = _dup(ysd->dom_socket);
3509fa75c15SBill Paul 			if (ysd->dom_client != NULL)
351a2308772SBill Paul 				clnt_destroy(ysd->dom_client);
352a2308772SBill Paul 			ysd->dom_vers = 0;
353a2308772SBill Paul 			ysd->dom_client = NULL;
354d201fe46SDaniel Eischen 			sock = _dup2(save, sock);
3559233c4d9SJason Evans 			_close(save);
356a2308772SBill Paul 		}
3574415cd19SGarrett Wollman 	}
3580f98415eSBill Paul 
3594415cd19SGarrett Wollman again:
360456ebbf8SBill Paul 	retries++;
361456ebbf8SBill Paul 	if (retries > MAX_RETRIES) {
362456ebbf8SBill Paul 		if (new)
363456ebbf8SBill Paul 			free(ysd);
364456ebbf8SBill Paul 		return(YPERR_YPBIND);
365456ebbf8SBill Paul 	}
3664415cd19SGarrett Wollman #ifdef BINDINGDIR
3674415cd19SGarrett Wollman 	if (ysd->dom_vers == 0) {
368a169c9b1SBill Paul 		/*
369a169c9b1SBill Paul 		 * We're trying to make a new binding: zorch the
370a169c9b1SBill Paul 		 * existing handle now (if any).
371a169c9b1SBill Paul 		 */
3729fa75c15SBill Paul 		if (ysd->dom_client != NULL) {
373a169c9b1SBill Paul 			clnt_destroy(ysd->dom_client);
374a169c9b1SBill Paul 			ysd->dom_client = NULL;
375a2308772SBill Paul 			ysd->dom_socket = -1;
376a169c9b1SBill Paul 		}
37783ac6cdcSKris Kennaway 		snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2);
37805eb11cbSJilles Tjoelker 		if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) {
3794415cd19SGarrett Wollman 			/* no binding file, YP is dead. */
380456ebbf8SBill Paul 			/* Try to bring it back to life. */
3819233c4d9SJason Evans 			_close(fd);
382b9729ac2SBill Paul 			goto skipit;
3834415cd19SGarrett Wollman 		}
384d201fe46SDaniel Eischen 		if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
3854415cd19SGarrett Wollman 			struct iovec iov[2];
3869d80b1ddSBill Paul 			struct ypbind_resp ybr;
3874415cd19SGarrett Wollman 			u_short	ypb_port;
3884415cd19SGarrett Wollman 
3894415cd19SGarrett Wollman 			iov[0].iov_base = (caddr_t)&ypb_port;
3904415cd19SGarrett Wollman 			iov[0].iov_len = sizeof ypb_port;
3914415cd19SGarrett Wollman 			iov[1].iov_base = (caddr_t)&ybr;
3924415cd19SGarrett Wollman 			iov[1].iov_len = sizeof ybr;
3934415cd19SGarrett Wollman 
394d201fe46SDaniel Eischen 			r = _readv(fd, iov, 2);
3954415cd19SGarrett Wollman 			if (r != iov[0].iov_len + iov[1].iov_len) {
3969233c4d9SJason Evans 				_close(fd);
3974415cd19SGarrett Wollman 				ysd->dom_vers = -1;
3984415cd19SGarrett Wollman 				goto again;
3994415cd19SGarrett Wollman 			}
4004415cd19SGarrett Wollman 
4014415cd19SGarrett Wollman 			bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
4024415cd19SGarrett Wollman 			ysd->dom_server_addr.sin_family = AF_INET;
4034415cd19SGarrett Wollman 			ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
4047b5f5922SJohn Baldwin 			bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
4057b5f5922SJohn Baldwin 			    &ysd->dom_server_addr.sin_addr.s_addr,
4067b5f5922SJohn Baldwin 			    sizeof(ysd->dom_server_addr.sin_addr.s_addr));
4077b5f5922SJohn Baldwin 			bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
4087b5f5922SJohn Baldwin 			    &ysd->dom_server_addr.sin_port,
4097b5f5922SJohn Baldwin 			    sizeof(ysd->dom_server_addr.sin_port));
4104415cd19SGarrett Wollman 
4114415cd19SGarrett Wollman 			ysd->dom_server_port = ysd->dom_server_addr.sin_port;
4129233c4d9SJason Evans 			_close(fd);
4134415cd19SGarrett Wollman 			goto gotit;
4144415cd19SGarrett Wollman 		} else {
4154415cd19SGarrett Wollman 			/* no lock on binding file, YP is dead. */
416456ebbf8SBill Paul 			/* Try to bring it back to life. */
4179233c4d9SJason Evans 			_close(fd);
418456ebbf8SBill Paul 			goto skipit;
419f067dfeaSBill Paul 		}
420f067dfeaSBill Paul 	}
421b9729ac2SBill Paul skipit:
4224415cd19SGarrett Wollman #endif
4234415cd19SGarrett Wollman 	if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
424a169c9b1SBill Paul 		/*
425a169c9b1SBill Paul 		 * We're trying to make a new binding: zorch the
426a169c9b1SBill Paul 		 * existing handle now (if any).
427a169c9b1SBill Paul 		 */
4289fa75c15SBill Paul 		if (ysd->dom_client != NULL) {
429a169c9b1SBill Paul 			clnt_destroy(ysd->dom_client);
430a169c9b1SBill Paul 			ysd->dom_client = NULL;
431a2308772SBill Paul 			ysd->dom_socket = -1;
432a169c9b1SBill Paul 		}
4334415cd19SGarrett Wollman 		bzero((char *)&clnt_sin, sizeof clnt_sin);
4344415cd19SGarrett Wollman 		clnt_sin.sin_family = AF_INET;
4354415cd19SGarrett Wollman 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4364415cd19SGarrett Wollman 
4374415cd19SGarrett Wollman 		clnt_sock = RPC_ANYSOCK;
4384415cd19SGarrett Wollman 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock,
4394415cd19SGarrett Wollman 			0, 0);
4404415cd19SGarrett Wollman 		if (client == NULL) {
44120e4b00aSBill Paul 			/*
44220e4b00aSBill Paul 			 * These conditions indicate ypbind just isn't
44320e4b00aSBill Paul 			 * alive -- we probably don't want to shoot our
444a169c9b1SBill Paul 			 * mouth off in this case; instead generate error
44520e4b00aSBill Paul 			 * messages only for really exotic problems.
44620e4b00aSBill Paul 			 */
44720e4b00aSBill Paul 			if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
44820e4b00aSBill Paul 			   (rpc_createerr.cf_stat != RPC_SYSTEMERROR &&
44920e4b00aSBill Paul 			   rpc_createerr.cf_error.re_errno == ECONNREFUSED))
4504415cd19SGarrett Wollman 				clnt_pcreateerror("clnttcp_create");
4514415cd19SGarrett Wollman 			if (new)
4524415cd19SGarrett Wollman 				free(ysd);
453456ebbf8SBill Paul 			return (YPERR_YPBIND);
4544415cd19SGarrett Wollman 		}
4554415cd19SGarrett Wollman 
45656d18edaSBill Paul 		/*
45756d18edaSBill Paul 		 * Check the port number -- should be < IPPORT_RESERVED.
45856d18edaSBill Paul 		 * If not, it's possible someone has registered a bogus
45956d18edaSBill Paul 		 * ypbind with the portmapper and is trying to trick us.
46056d18edaSBill Paul 		 */
46156d18edaSBill Paul 		if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) {
4629fa75c15SBill Paul 			if (client != NULL)
46356d18edaSBill Paul 				clnt_destroy(client);
46456d18edaSBill Paul 			if (new)
46556d18edaSBill Paul 				free(ysd);
46656d18edaSBill Paul 			return(YPERR_YPBIND);
46756d18edaSBill Paul 		}
468456ebbf8SBill Paul 		tv.tv_sec = _yplib_timeout/2;
4694415cd19SGarrett Wollman 		tv.tv_usec = 0;
4704415cd19SGarrett Wollman 		r = clnt_call(client, YPBINDPROC_DOMAIN,
471f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_domainname, &dom,
472f249dbccSDag-Erling Smørgrav 			(xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
4734415cd19SGarrett Wollman 		if (r != RPC_SUCCESS) {
4744415cd19SGarrett Wollman 			clnt_destroy(client);
4754415cd19SGarrett Wollman 			ysd->dom_vers = -1;
476a2308772SBill Paul 			if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) {
477a2308772SBill Paul 				if (new)
478a2308772SBill Paul 					free(ysd);
47920e4b00aSBill Paul 				return(YPERR_YPBIND);
480a2308772SBill Paul 			}
48120e4b00aSBill Paul 			fprintf(stderr,
48220e4b00aSBill Paul 			"YP: server for domain %s not responding, retrying\n", dom);
4834415cd19SGarrett Wollman 			goto again;
48464416168SBill Paul 		} else {
48564416168SBill Paul 			if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
4869233c4d9SJason Evans 				struct timespec time_to_sleep, time_remaining;
4879233c4d9SJason Evans 
488456ebbf8SBill Paul 				clnt_destroy(client);
489456ebbf8SBill Paul 				ysd->dom_vers = -1;
4909233c4d9SJason Evans 
4919233c4d9SJason Evans 				time_to_sleep.tv_sec = _yplib_timeout/2;
4929233c4d9SJason Evans 				time_to_sleep.tv_nsec = 0;
4939233c4d9SJason Evans 				_nanosleep(&time_to_sleep,
4949233c4d9SJason Evans 				    &time_remaining);
495456ebbf8SBill Paul 				goto again;
49664416168SBill Paul 			}
4974415cd19SGarrett Wollman 		}
4984415cd19SGarrett Wollman 		clnt_destroy(client);
4994415cd19SGarrett Wollman 
5004415cd19SGarrett Wollman 		bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr);
5014415cd19SGarrett Wollman 		ysd->dom_server_addr.sin_family = AF_INET;
5027b5f5922SJohn Baldwin 		bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
5037b5f5922SJohn Baldwin 		    &ysd->dom_server_addr.sin_port,
5047b5f5922SJohn Baldwin 		    sizeof(ysd->dom_server_addr.sin_port));
5057b5f5922SJohn Baldwin 		bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
5067b5f5922SJohn Baldwin 		    &ysd->dom_server_addr.sin_addr.s_addr,
5077b5f5922SJohn Baldwin 		    sizeof(ysd->dom_server_addr.sin_addr.s_addr));
50856d18edaSBill Paul 
50956d18edaSBill Paul 		/*
51056d18edaSBill Paul 		 * We could do a reserved port check here too, but this
51156d18edaSBill Paul 		 * could pose compatibility problems. The local ypbind is
51256d18edaSBill Paul 		 * supposed to decide whether or not to trust yp servers
51356d18edaSBill Paul 		 * on insecure ports. For now, we trust its judgement.
51456d18edaSBill Paul 		 */
5154415cd19SGarrett Wollman 		ysd->dom_server_port =
5169d80b1ddSBill Paul 			*(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port;
5174415cd19SGarrett Wollman gotit:
5184415cd19SGarrett Wollman 		ysd->dom_vers = YPVERS;
519d0509082SJacques Vidrine 		strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
5204415cd19SGarrett Wollman 	}
5214415cd19SGarrett Wollman 
522a169c9b1SBill Paul 	/* Don't rebuild the connection to the server unless we have to. */
523a169c9b1SBill Paul 	if (ysd->dom_client == NULL) {
5244415cd19SGarrett Wollman 		tv.tv_sec = _yplib_timeout/2;
5254415cd19SGarrett Wollman 		tv.tv_usec = 0;
5264415cd19SGarrett Wollman 		ysd->dom_socket = RPC_ANYSOCK;
5274e6ace08SBill Paul 		ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr,
5284e6ace08SBill Paul 			YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304);
5294415cd19SGarrett Wollman 		if (ysd->dom_client == NULL) {
5304415cd19SGarrett Wollman 			clnt_pcreateerror("clntudp_create");
5314415cd19SGarrett Wollman 			ysd->dom_vers = -1;
5324415cd19SGarrett Wollman 			goto again;
5334415cd19SGarrett Wollman 		}
5349233c4d9SJason Evans 		if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1)
5354415cd19SGarrett Wollman 			perror("fcntl: F_SETFD");
536a2308772SBill Paul 		/*
537a2308772SBill Paul 		 * We want a port number associated with this socket
538a2308772SBill Paul 		 * so that we can check its authenticity later.
539a2308772SBill Paul 		 */
540a2308772SBill Paul 		checklen = sizeof(struct sockaddr_in);
541a2308772SBill Paul 		bzero((char *)&check, checklen);
542d201fe46SDaniel Eischen 		_bind(ysd->dom_socket, (struct sockaddr *)&check, checklen);
543a2308772SBill Paul 		check.sin_family = AF_INET;
544d201fe46SDaniel Eischen 		if (!_getsockname(ysd->dom_socket,
545a2308772SBill Paul 		    (struct sockaddr *)&check, &checklen)) {
546a2308772SBill Paul 			ysd->dom_local_port = check.sin_port;
547a2308772SBill Paul 		} else {
548a2308772SBill Paul 			clnt_destroy(ysd->dom_client);
549a2308772SBill Paul 			if (new)
550a2308772SBill Paul 				free(ysd);
551a2308772SBill Paul 			return(YPERR_YPBIND);
552a2308772SBill Paul 		}
553a169c9b1SBill Paul 	}
5544415cd19SGarrett Wollman 
5554415cd19SGarrett Wollman 	if (new) {
5564415cd19SGarrett Wollman 		ysd->dom_pnext = _ypbindlist;
5574415cd19SGarrett Wollman 		_ypbindlist = ysd;
5584415cd19SGarrett Wollman 	}
5594415cd19SGarrett Wollman 
56033e6f9c1SSimon L. B. Nielsen 	/*
56133e6f9c1SSimon L. B. Nielsen 	 * Set low retry timeout to realistically handle UDP packet
56233e6f9c1SSimon L. B. Nielsen 	 * loss for YP packet bursts.
56333e6f9c1SSimon L. B. Nielsen 	 */
56433e6f9c1SSimon L. B. Nielsen 	tv.tv_sec = 1;
56533e6f9c1SSimon L. B. Nielsen 	tv.tv_usec = 0;
56633e6f9c1SSimon L. B. Nielsen 	clnt_control(ysd->dom_client, CLSET_RETRY_TIMEOUT, (char*)&tv);
56733e6f9c1SSimon L. B. Nielsen 
5684415cd19SGarrett Wollman 	if (ypdb != NULL)
5694415cd19SGarrett Wollman 		*ypdb = ysd;
570ed4d1c46SDag-Erling Smørgrav 	return (0);
5714415cd19SGarrett Wollman }
5724415cd19SGarrett Wollman 
5734415cd19SGarrett Wollman static void
574dc584ddbSDag-Erling Smørgrav _yp_unbind(struct dom_binding *ypb)
5754415cd19SGarrett Wollman {
5769fa75c15SBill Paul 	struct sockaddr_in check;
577595e5323SStefan Farfeleder 	socklen_t checklen = sizeof(struct sockaddr_in);
5789fa75c15SBill Paul 
5799fa75c15SBill Paul 	if (ypb->dom_client != NULL) {
5809fa75c15SBill Paul 		/* Check the socket -- may have been hosed by the caller. */
581d201fe46SDaniel Eischen 		if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check,
5829fa75c15SBill Paul 	    	&checklen) == -1 || check.sin_family != AF_INET ||
5839fa75c15SBill Paul 	    	check.sin_port != ypb->dom_local_port) {
5849fa75c15SBill Paul 			int save, sock;
5859fa75c15SBill Paul 
5869fa75c15SBill Paul 			sock = ypb->dom_socket;
587d201fe46SDaniel Eischen 			save = _dup(ypb->dom_socket);
5884415cd19SGarrett Wollman 			clnt_destroy(ypb->dom_client);
589d201fe46SDaniel Eischen 			sock = _dup2(save, sock);
5909233c4d9SJason Evans 			_close(save);
5919fa75c15SBill Paul 		} else
5929fa75c15SBill Paul 			clnt_destroy(ypb->dom_client);
5939fa75c15SBill Paul 	}
5949fa75c15SBill Paul 
5954415cd19SGarrett Wollman 	ypb->dom_client = NULL;
5964415cd19SGarrett Wollman 	ypb->dom_socket = -1;
5972694f9b9SBill Paul 	ypb->dom_vers = -1;
5984e6ace08SBill Paul #ifdef YPMATCHCACHE
5994e6ace08SBill Paul 	ypmatch_cache_flush(ypb);
6004e6ace08SBill Paul #endif
6014415cd19SGarrett Wollman }
6024415cd19SGarrett Wollman 
603d21de89aSHajimu UMEMOTO static int
604d21de89aSHajimu UMEMOTO yp_bind_locked(char *dom)
6054415cd19SGarrett Wollman {
606ed4d1c46SDag-Erling Smørgrav 	return (_yp_dobind(dom, NULL));
6074415cd19SGarrett Wollman }
6084415cd19SGarrett Wollman 
609d21de89aSHajimu UMEMOTO int
610d21de89aSHajimu UMEMOTO yp_bind(char *dom)
611d21de89aSHajimu UMEMOTO {
612d21de89aSHajimu UMEMOTO 	int r;
613d21de89aSHajimu UMEMOTO 
614d21de89aSHajimu UMEMOTO 	YPLOCK();
615d21de89aSHajimu UMEMOTO 	r = yp_bind_locked(dom);
616d21de89aSHajimu UMEMOTO 	YPUNLOCK();
617d21de89aSHajimu UMEMOTO 	return (r);
618d21de89aSHajimu UMEMOTO }
619d21de89aSHajimu UMEMOTO 
620d21de89aSHajimu UMEMOTO static void
621d21de89aSHajimu UMEMOTO yp_unbind_locked(char *dom)
6224415cd19SGarrett Wollman {
6234415cd19SGarrett Wollman 	struct dom_binding *ypb, *ypbp;
6244415cd19SGarrett Wollman 
6254415cd19SGarrett Wollman 	ypbp = NULL;
6264415cd19SGarrett Wollman 	for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
6274415cd19SGarrett Wollman 		if (strcmp(dom, ypb->dom_domain) == 0) {
6282694f9b9SBill Paul 			_yp_unbind(ypb);
6294415cd19SGarrett Wollman 			if (ypbp)
6304415cd19SGarrett Wollman 				ypbp->dom_pnext = ypb->dom_pnext;
6314415cd19SGarrett Wollman 			else
6324415cd19SGarrett Wollman 				_ypbindlist = ypb->dom_pnext;
6334415cd19SGarrett Wollman 			free(ypb);
6344415cd19SGarrett Wollman 			return;
6354415cd19SGarrett Wollman 		}
6364415cd19SGarrett Wollman 		ypbp = ypb;
6374415cd19SGarrett Wollman 	}
6384415cd19SGarrett Wollman 	return;
6394415cd19SGarrett Wollman }
6404415cd19SGarrett Wollman 
641d21de89aSHajimu UMEMOTO void
642d21de89aSHajimu UMEMOTO yp_unbind(char *dom)
643d21de89aSHajimu UMEMOTO {
644d21de89aSHajimu UMEMOTO 	YPLOCK();
645d21de89aSHajimu UMEMOTO 	yp_unbind_locked(dom);
646d21de89aSHajimu UMEMOTO 	YPUNLOCK();
647d21de89aSHajimu UMEMOTO }
648d21de89aSHajimu UMEMOTO 
6494415cd19SGarrett Wollman int
650dc584ddbSDag-Erling Smørgrav yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen,
651dc584ddbSDag-Erling Smørgrav     char **outval, int *outvallen)
6524415cd19SGarrett Wollman {
6534415cd19SGarrett Wollman 	struct dom_binding *ysd;
6544415cd19SGarrett Wollman 	struct ypresp_val yprv;
6554415cd19SGarrett Wollman 	struct timeval tv;
6564415cd19SGarrett Wollman 	struct ypreq_key yprk;
6574415cd19SGarrett Wollman 	int r;
658*c1dfca84SMarcelo Araujo 	int retries = 0;
6594415cd19SGarrett Wollman 	*outval = NULL;
6604415cd19SGarrett Wollman 	*outvallen = 0;
6614415cd19SGarrett Wollman 
66206643071SBill Paul 	/* Sanity check */
663e17334c3SBill Paul 
66406643071SBill Paul 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
66506643071SBill Paul 	    inmap == NULL || !strlen(inmap) ||
66606643071SBill Paul 	    indomain == NULL || !strlen(indomain))
667ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADARGS);
668e17334c3SBill Paul 
669d21de89aSHajimu UMEMOTO 	YPLOCK();
670d21de89aSHajimu UMEMOTO 	if (_yp_dobind(indomain, &ysd) != 0) {
671d21de89aSHajimu UMEMOTO 		YPUNLOCK();
6724e6ace08SBill Paul 		return(YPERR_DOMAIN);
673d21de89aSHajimu UMEMOTO 	}
6744e6ace08SBill Paul 
6754e6ace08SBill Paul 	yprk.domain = indomain;
6764e6ace08SBill Paul 	yprk.map = inmap;
6774e6ace08SBill Paul 	yprk.key.keydat_val = (char *)inkey;
6784e6ace08SBill Paul 	yprk.key.keydat_len = inkeylen;
6794e6ace08SBill Paul 
6804415cd19SGarrett Wollman #ifdef YPMATCHCACHE
6814e6ace08SBill Paul 	if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) {
6824e6ace08SBill Paul /*
6834415cd19SGarrett Wollman 	if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
6840e276383SBill Paul 	    inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) {
6854e6ace08SBill Paul */
6860e276383SBill Paul 		*outvallen = yprv.val.valdat_len;
6874415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
6886890d156SGuy Helmer 		if (*outval == NULL) {
6896890d156SGuy Helmer 			_yp_unbind(ysd);
6906890d156SGuy Helmer 			*outvallen = 0;
6916890d156SGuy Helmer 			YPUNLOCK();
6926890d156SGuy Helmer 			return (YPERR_RESRC);
6936890d156SGuy Helmer 		}
6940e276383SBill Paul 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
6954415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
696d21de89aSHajimu UMEMOTO 		YPUNLOCK();
697ed4d1c46SDag-Erling Smørgrav 		return (0);
6984415cd19SGarrett Wollman 	}
6996890d156SGuy Helmer 	_yp_unbind(ysd);
7004415cd19SGarrett Wollman #endif
7014415cd19SGarrett Wollman 
7024cc738f7SBill Paul again:
703*c1dfca84SMarcelo Araujo 	if (retries > MAX_RETRIES) {
704*c1dfca84SMarcelo Araujo 		YPUNLOCK();
705*c1dfca84SMarcelo Araujo 		return (YPERR_RPC);
706*c1dfca84SMarcelo Araujo 	}
707*c1dfca84SMarcelo Araujo 
708d21de89aSHajimu UMEMOTO 	if (_yp_dobind(indomain, &ysd) != 0) {
709d21de89aSHajimu UMEMOTO 		YPUNLOCK();
710ed4d1c46SDag-Erling Smørgrav 		return (YPERR_DOMAIN);
711d21de89aSHajimu UMEMOTO 	}
7124cc738f7SBill Paul 
7134415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
7144415cd19SGarrett Wollman 	tv.tv_usec = 0;
7154415cd19SGarrett Wollman 
7164415cd19SGarrett Wollman 	bzero((char *)&yprv, sizeof yprv);
7174415cd19SGarrett Wollman 
7184415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_MATCH,
719f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypreq_key, &yprk,
720f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypresp_val, &yprv, tv);
7214415cd19SGarrett Wollman 	if (r != RPC_SUCCESS) {
7224415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_match: clnt_call");
7232694f9b9SBill Paul 		_yp_unbind(ysd);
724*c1dfca84SMarcelo Araujo 		retries++;
7254415cd19SGarrett Wollman 		goto again;
7264415cd19SGarrett Wollman 	}
727a169c9b1SBill Paul 
7280e276383SBill Paul 	if (!(r = ypprot_err(yprv.stat))) {
7290e276383SBill Paul 		*outvallen = yprv.val.valdat_len;
7304415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
7316890d156SGuy Helmer 		if (*outval == NULL) {
7326890d156SGuy Helmer 			_yp_unbind(ysd);
7336890d156SGuy Helmer 			*outvallen = 0;
7346890d156SGuy Helmer 			xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
7356890d156SGuy Helmer 			YPUNLOCK();
7366890d156SGuy Helmer 			return (YPERR_RESRC);
7376890d156SGuy Helmer 		}
7380e276383SBill Paul 		bcopy(yprv.val.valdat_val, *outval, *outvallen);
7394415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
7404415cd19SGarrett Wollman #ifdef YPMATCHCACHE
7414e6ace08SBill Paul 		ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val);
7424415cd19SGarrett Wollman #endif
7432694f9b9SBill Paul 	}
744a169c9b1SBill Paul 
745f249dbccSDag-Erling Smørgrav 	xdr_free((xdrproc_t)xdr_ypresp_val, &yprv);
746d21de89aSHajimu UMEMOTO 	YPUNLOCK();
747ed4d1c46SDag-Erling Smørgrav 	return (r);
7484415cd19SGarrett Wollman }
7494415cd19SGarrett Wollman 
750069eb2caSHajimu UMEMOTO static int
751069eb2caSHajimu UMEMOTO yp_get_default_domain_locked(char **domp)
7524415cd19SGarrett Wollman {
7534415cd19SGarrett Wollman 	*domp = NULL;
7544415cd19SGarrett Wollman 	if (_yp_domain[0] == '\0')
7554415cd19SGarrett Wollman 		if (getdomainname(_yp_domain, sizeof _yp_domain))
756ed4d1c46SDag-Erling Smørgrav 			return (YPERR_NODOM);
7574415cd19SGarrett Wollman 	*domp = _yp_domain;
758ed4d1c46SDag-Erling Smørgrav 	return (0);
7594415cd19SGarrett Wollman }
7604415cd19SGarrett Wollman 
7614415cd19SGarrett Wollman int
762069eb2caSHajimu UMEMOTO yp_get_default_domain(char **domp)
763069eb2caSHajimu UMEMOTO {
764069eb2caSHajimu UMEMOTO 	int r;
765069eb2caSHajimu UMEMOTO 
766069eb2caSHajimu UMEMOTO 	YPLOCK();
767069eb2caSHajimu UMEMOTO 	r = yp_get_default_domain_locked(domp);
768069eb2caSHajimu UMEMOTO 	YPUNLOCK();
769069eb2caSHajimu UMEMOTO 	return (r);
770069eb2caSHajimu UMEMOTO }
771069eb2caSHajimu UMEMOTO 
772069eb2caSHajimu UMEMOTO int
773dc584ddbSDag-Erling Smørgrav yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen,
774dc584ddbSDag-Erling Smørgrav     char **outval, int *outvallen)
7754415cd19SGarrett Wollman {
7764415cd19SGarrett Wollman 	struct ypresp_key_val yprkv;
7774415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
7784415cd19SGarrett Wollman 	struct dom_binding *ysd;
7794415cd19SGarrett Wollman 	struct timeval tv;
7804415cd19SGarrett Wollman 	int r;
781*c1dfca84SMarcelo Araujo 	int retries = 0;
78206643071SBill Paul 	/* Sanity check */
78306643071SBill Paul 
78406643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
78506643071SBill Paul 	    inmap == NULL || !strlen(inmap))
786ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADARGS);
78706643071SBill Paul 
7884415cd19SGarrett Wollman 	*outkey = *outval = NULL;
7894415cd19SGarrett Wollman 	*outkeylen = *outvallen = 0;
7904415cd19SGarrett Wollman 
791d21de89aSHajimu UMEMOTO 	YPLOCK();
7924415cd19SGarrett Wollman again:
793*c1dfca84SMarcelo Araujo 	if (retries > MAX_RETRIES) {
794*c1dfca84SMarcelo Araujo 		YPUNLOCK();
795*c1dfca84SMarcelo Araujo 		return (YPERR_RPC);
796*c1dfca84SMarcelo Araujo 	}
797*c1dfca84SMarcelo Araujo 
798d21de89aSHajimu UMEMOTO 	if (_yp_dobind(indomain, &ysd) != 0) {
799d21de89aSHajimu UMEMOTO 		YPUNLOCK();
800ed4d1c46SDag-Erling Smørgrav 		return (YPERR_DOMAIN);
801d21de89aSHajimu UMEMOTO 	}
8024415cd19SGarrett Wollman 
8034415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
8044415cd19SGarrett Wollman 	tv.tv_usec = 0;
8054415cd19SGarrett Wollman 
8064415cd19SGarrett Wollman 	yprnk.domain = indomain;
8074415cd19SGarrett Wollman 	yprnk.map = inmap;
8084415cd19SGarrett Wollman 	bzero((char *)&yprkv, sizeof yprkv);
8094415cd19SGarrett Wollman 
8104415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_FIRST,
811f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
812f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
8134415cd19SGarrett Wollman 	if (r != RPC_SUCCESS) {
8144415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_first: clnt_call");
8152694f9b9SBill Paul 		_yp_unbind(ysd);
816*c1dfca84SMarcelo Araujo 		retries++;
8174415cd19SGarrett Wollman 		goto again;
8184415cd19SGarrett Wollman 	}
8190e276383SBill Paul 	if (!(r = ypprot_err(yprkv.stat))) {
8200e276383SBill Paul 		*outkeylen = yprkv.key.keydat_len;
8214415cd19SGarrett Wollman 		*outkey = (char *)malloc(*outkeylen+1);
8226890d156SGuy Helmer 		if (*outkey == NULL) {
8236890d156SGuy Helmer 			_yp_unbind(ysd);
8246890d156SGuy Helmer 			*outkeylen = 0;
8256890d156SGuy Helmer 			xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
8266890d156SGuy Helmer 			YPUNLOCK();
8276890d156SGuy Helmer 			return (YPERR_RESRC);
8286890d156SGuy Helmer 		}
8290e276383SBill Paul 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
8304415cd19SGarrett Wollman 		(*outkey)[*outkeylen] = '\0';
8310e276383SBill Paul 		*outvallen = yprkv.val.valdat_len;
8324415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
8336890d156SGuy Helmer 		if (*outval == NULL) {
8346890d156SGuy Helmer 			free(*outkey);
8356890d156SGuy Helmer 			_yp_unbind(ysd);
8366890d156SGuy Helmer 			*outkeylen = *outvallen = 0;
8376890d156SGuy Helmer 			xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
8386890d156SGuy Helmer 			YPUNLOCK();
8396890d156SGuy Helmer 			return (YPERR_RESRC);
8406890d156SGuy Helmer 		}
8410e276383SBill Paul 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
8424415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
8432694f9b9SBill Paul 	}
844a169c9b1SBill Paul 
845f249dbccSDag-Erling Smørgrav 	xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
846d21de89aSHajimu UMEMOTO 	YPUNLOCK();
847ed4d1c46SDag-Erling Smørgrav 	return (r);
8484415cd19SGarrett Wollman }
8494415cd19SGarrett Wollman 
8504415cd19SGarrett Wollman int
851dc584ddbSDag-Erling Smørgrav yp_next(char *indomain, char *inmap, char *inkey, int inkeylen,
852dc584ddbSDag-Erling Smørgrav     char **outkey, int *outkeylen, char **outval, int *outvallen)
8534415cd19SGarrett Wollman {
8544415cd19SGarrett Wollman 	struct ypresp_key_val yprkv;
8554415cd19SGarrett Wollman 	struct ypreq_key yprk;
8564415cd19SGarrett Wollman 	struct dom_binding *ysd;
8574415cd19SGarrett Wollman 	struct timeval tv;
8584415cd19SGarrett Wollman 	int r;
859*c1dfca84SMarcelo Araujo 	int retries = 0;
86006643071SBill Paul 	/* Sanity check */
86106643071SBill Paul 
86206643071SBill Paul 	if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 ||
86306643071SBill Paul 	    inmap == NULL || !strlen(inmap) ||
86406643071SBill Paul 	    indomain == NULL || !strlen(indomain))
865ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADARGS);
86606643071SBill Paul 
8674415cd19SGarrett Wollman 	*outkey = *outval = NULL;
8684415cd19SGarrett Wollman 	*outkeylen = *outvallen = 0;
8694415cd19SGarrett Wollman 
870d21de89aSHajimu UMEMOTO 	YPLOCK();
8714415cd19SGarrett Wollman again:
872*c1dfca84SMarcelo Araujo 	if (retries > MAX_RETRIES) {
873*c1dfca84SMarcelo Araujo 		YPUNLOCK();
874*c1dfca84SMarcelo Araujo 		return (YPERR_RPC);
875*c1dfca84SMarcelo Araujo 	}
876*c1dfca84SMarcelo Araujo 
877d21de89aSHajimu UMEMOTO 	if (_yp_dobind(indomain, &ysd) != 0) {
878d21de89aSHajimu UMEMOTO 		YPUNLOCK();
879ed4d1c46SDag-Erling Smørgrav 		return (YPERR_DOMAIN);
880d21de89aSHajimu UMEMOTO 	}
8814415cd19SGarrett Wollman 
8824415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
8834415cd19SGarrett Wollman 	tv.tv_usec = 0;
8844415cd19SGarrett Wollman 
8854415cd19SGarrett Wollman 	yprk.domain = indomain;
8864415cd19SGarrett Wollman 	yprk.map = inmap;
8870e276383SBill Paul 	yprk.key.keydat_val = inkey;
8880e276383SBill Paul 	yprk.key.keydat_len = inkeylen;
8894415cd19SGarrett Wollman 	bzero((char *)&yprkv, sizeof yprkv);
8904415cd19SGarrett Wollman 
8914415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_NEXT,
892f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypreq_key, &yprk,
893f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypresp_key_val, &yprkv, tv);
8944415cd19SGarrett Wollman 	if (r != RPC_SUCCESS) {
8954415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_next: clnt_call");
8962694f9b9SBill Paul 		_yp_unbind(ysd);
897*c1dfca84SMarcelo Araujo 		retries++;
8984415cd19SGarrett Wollman 		goto again;
8994415cd19SGarrett Wollman 	}
9000e276383SBill Paul 	if (!(r = ypprot_err(yprkv.stat))) {
9010e276383SBill Paul 		*outkeylen = yprkv.key.keydat_len;
9024415cd19SGarrett Wollman 		*outkey = (char *)malloc(*outkeylen+1);
9036890d156SGuy Helmer 		if (*outkey == NULL) {
9046890d156SGuy Helmer 			_yp_unbind(ysd);
9056890d156SGuy Helmer 			*outkeylen = 0;
9066890d156SGuy Helmer 			xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
9076890d156SGuy Helmer 			YPUNLOCK();
9086890d156SGuy Helmer 			return (YPERR_RESRC);
9096890d156SGuy Helmer 		}
9100e276383SBill Paul 		bcopy(yprkv.key.keydat_val, *outkey, *outkeylen);
9114415cd19SGarrett Wollman 		(*outkey)[*outkeylen] = '\0';
9120e276383SBill Paul 		*outvallen = yprkv.val.valdat_len;
9134415cd19SGarrett Wollman 		*outval = (char *)malloc(*outvallen+1);
9146890d156SGuy Helmer 		if (*outval == NULL) {
9156890d156SGuy Helmer 			free(*outkey);
9166890d156SGuy Helmer 			_yp_unbind(ysd);
9176890d156SGuy Helmer 			*outkeylen = *outvallen = 0;
9186890d156SGuy Helmer 			xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
9196890d156SGuy Helmer 			YPUNLOCK();
9206890d156SGuy Helmer 			return (YPERR_RESRC);
9216890d156SGuy Helmer 		}
9220e276383SBill Paul 		bcopy(yprkv.val.valdat_val, *outval, *outvallen);
9234415cd19SGarrett Wollman 		(*outval)[*outvallen] = '\0';
9242694f9b9SBill Paul 	}
925a169c9b1SBill Paul 
926f249dbccSDag-Erling Smørgrav 	xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv);
927d21de89aSHajimu UMEMOTO 	YPUNLOCK();
928ed4d1c46SDag-Erling Smørgrav 	return (r);
9294415cd19SGarrett Wollman }
9304415cd19SGarrett Wollman 
9314415cd19SGarrett Wollman int
932dc584ddbSDag-Erling Smørgrav yp_all(char *indomain, char *inmap, struct ypall_callback *incallback)
9334415cd19SGarrett Wollman {
9344415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
9354415cd19SGarrett Wollman 	struct dom_binding *ysd;
9364415cd19SGarrett Wollman 	struct timeval tv;
9374415cd19SGarrett Wollman 	struct sockaddr_in clnt_sin;
9384415cd19SGarrett Wollman 	CLIENT *clnt;
93908aff01bSBill Paul 	u_long status, savstat;
9404415cd19SGarrett Wollman 	int clnt_sock;
941*c1dfca84SMarcelo Araujo 	int retries = 0;
94206643071SBill Paul 	/* Sanity check */
94306643071SBill Paul 
94406643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
94506643071SBill Paul 	    inmap == NULL || !strlen(inmap))
946ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADARGS);
94706643071SBill Paul 
948d21de89aSHajimu UMEMOTO 	YPLOCK();
9492694f9b9SBill Paul again:
950*c1dfca84SMarcelo Araujo 	if (retries > MAX_RETRIES) {
951*c1dfca84SMarcelo Araujo 		YPUNLOCK();
952*c1dfca84SMarcelo Araujo 		return (YPERR_RPC);
953*c1dfca84SMarcelo Araujo 	}
9542694f9b9SBill Paul 
955d21de89aSHajimu UMEMOTO 	if (_yp_dobind(indomain, &ysd) != 0) {
956d21de89aSHajimu UMEMOTO 		YPUNLOCK();
957ed4d1c46SDag-Erling Smørgrav 		return (YPERR_DOMAIN);
958d21de89aSHajimu UMEMOTO 	}
9594415cd19SGarrett Wollman 
9604415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
9614415cd19SGarrett Wollman 	tv.tv_usec = 0;
9622694f9b9SBill Paul 
9632694f9b9SBill Paul 	/* YPPROC_ALL manufactures its own channel to ypserv using TCP */
9642694f9b9SBill Paul 
9654415cd19SGarrett Wollman 	clnt_sock = RPC_ANYSOCK;
9664415cd19SGarrett Wollman 	clnt_sin = ysd->dom_server_addr;
9674415cd19SGarrett Wollman 	clnt_sin.sin_port = 0;
9684415cd19SGarrett Wollman 	clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
9694415cd19SGarrett Wollman 	if (clnt == NULL) {
970d21de89aSHajimu UMEMOTO 		YPUNLOCK();
9714415cd19SGarrett Wollman 		printf("clnttcp_create failed\n");
972ed4d1c46SDag-Erling Smørgrav 		return (YPERR_PMAP);
9734415cd19SGarrett Wollman 	}
9744415cd19SGarrett Wollman 
9754415cd19SGarrett Wollman 	yprnk.domain = indomain;
9764415cd19SGarrett Wollman 	yprnk.map = inmap;
9774415cd19SGarrett Wollman 	ypresp_allfn = incallback->foreach;
9784415cd19SGarrett Wollman 	ypresp_data = (void *)incallback->data;
9794415cd19SGarrett Wollman 
9802694f9b9SBill Paul 	if (clnt_call(clnt, YPPROC_ALL,
981f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
982f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) {
983*c1dfca84SMarcelo Araujo 			clnt_perror(clnt, "yp_all: clnt_call");
9842694f9b9SBill Paul 			clnt_destroy(clnt);
9852694f9b9SBill Paul 			_yp_unbind(ysd);
986*c1dfca84SMarcelo Araujo 			retries++;
9872694f9b9SBill Paul 			goto again;
9882694f9b9SBill Paul 	}
9892694f9b9SBill Paul 
9904415cd19SGarrett Wollman 	clnt_destroy(clnt);
99108aff01bSBill Paul 	savstat = status;
992f249dbccSDag-Erling Smørgrav 	xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status);	/* not really needed... */
993d21de89aSHajimu UMEMOTO 	YPUNLOCK();
99408aff01bSBill Paul 	if (savstat != YP_NOMORE)
995ed4d1c46SDag-Erling Smørgrav 		return (ypprot_err(savstat));
996ed4d1c46SDag-Erling Smørgrav 	return (0);
9974415cd19SGarrett Wollman }
9984415cd19SGarrett Wollman 
9994415cd19SGarrett Wollman int
1000dc584ddbSDag-Erling Smørgrav yp_order(char *indomain, char *inmap, int *outorder)
10014415cd19SGarrett Wollman {
10024415cd19SGarrett Wollman  	struct dom_binding *ysd;
10034415cd19SGarrett Wollman 	struct ypresp_order ypro;
10044415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
10054415cd19SGarrett Wollman 	struct timeval tv;
10064415cd19SGarrett Wollman 	int r;
10074415cd19SGarrett Wollman 
100806643071SBill Paul 	/* Sanity check */
100906643071SBill Paul 
101006643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
101106643071SBill Paul 	    inmap == NULL || !strlen(inmap))
1012ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADARGS);
101306643071SBill Paul 
1014d21de89aSHajimu UMEMOTO 	YPLOCK();
10154415cd19SGarrett Wollman again:
1016d21de89aSHajimu UMEMOTO 	if (_yp_dobind(indomain, &ysd) != 0) {
1017d21de89aSHajimu UMEMOTO 		YPUNLOCK();
1018ed4d1c46SDag-Erling Smørgrav 		return (YPERR_DOMAIN);
1019d21de89aSHajimu UMEMOTO 	}
10204415cd19SGarrett Wollman 
10214415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
10224415cd19SGarrett Wollman 	tv.tv_usec = 0;
10234415cd19SGarrett Wollman 
10244415cd19SGarrett Wollman 	yprnk.domain = indomain;
10254415cd19SGarrett Wollman 	yprnk.map = inmap;
10264415cd19SGarrett Wollman 
10274415cd19SGarrett Wollman 	bzero((char *)(char *)&ypro, sizeof ypro);
10284415cd19SGarrett Wollman 
10294415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_ORDER,
1030f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
1031f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypresp_order, &ypro, tv);
10329fa75c15SBill Paul 
10339fa75c15SBill Paul 	/*
10349fa75c15SBill Paul 	 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER
10359fa75c15SBill Paul 	 * procedure.
10369fa75c15SBill Paul 	 */
10379fa75c15SBill Paul 	if (r == RPC_PROCUNAVAIL) {
1038d21de89aSHajimu UMEMOTO 		YPUNLOCK();
10399fa75c15SBill Paul 		return(YPERR_YPERR);
10409fa75c15SBill Paul 	}
10419fa75c15SBill Paul 
10424415cd19SGarrett Wollman 	if (r != RPC_SUCCESS) {
10434415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_order: clnt_call");
10442694f9b9SBill Paul 		_yp_unbind(ysd);
10454415cd19SGarrett Wollman 		goto again;
10464415cd19SGarrett Wollman 	}
10474415cd19SGarrett Wollman 
1048a169c9b1SBill Paul 	if (!(r = ypprot_err(ypro.stat))) {
10494415cd19SGarrett Wollman 		*outorder = ypro.ordernum;
10502694f9b9SBill Paul 	}
1051a169c9b1SBill Paul 
1052f249dbccSDag-Erling Smørgrav 	xdr_free((xdrproc_t)xdr_ypresp_order, &ypro);
1053d21de89aSHajimu UMEMOTO 	YPUNLOCK();
10542694f9b9SBill Paul 	return (r);
10554415cd19SGarrett Wollman }
10564415cd19SGarrett Wollman 
10574415cd19SGarrett Wollman int
1058dc584ddbSDag-Erling Smørgrav yp_master(char *indomain, char *inmap, char **outname)
10594415cd19SGarrett Wollman {
10604415cd19SGarrett Wollman 	struct dom_binding *ysd;
10614415cd19SGarrett Wollman 	struct ypresp_master yprm;
10624415cd19SGarrett Wollman 	struct ypreq_nokey yprnk;
10634415cd19SGarrett Wollman 	struct timeval tv;
10644415cd19SGarrett Wollman 	int r;
10654415cd19SGarrett Wollman 
106606643071SBill Paul 	/* Sanity check */
106706643071SBill Paul 
106806643071SBill Paul 	if (indomain == NULL || !strlen(indomain) ||
106906643071SBill Paul 	    inmap == NULL || !strlen(inmap))
1070ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADARGS);
1071d21de89aSHajimu UMEMOTO 	YPLOCK();
10724415cd19SGarrett Wollman again:
1073d21de89aSHajimu UMEMOTO 	if (_yp_dobind(indomain, &ysd) != 0) {
1074d21de89aSHajimu UMEMOTO 		YPUNLOCK();
1075ed4d1c46SDag-Erling Smørgrav 		return (YPERR_DOMAIN);
1076d21de89aSHajimu UMEMOTO 	}
10774415cd19SGarrett Wollman 
10784415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
10794415cd19SGarrett Wollman 	tv.tv_usec = 0;
10804415cd19SGarrett Wollman 
10814415cd19SGarrett Wollman 	yprnk.domain = indomain;
10824415cd19SGarrett Wollman 	yprnk.map = inmap;
10834415cd19SGarrett Wollman 
10844415cd19SGarrett Wollman 	bzero((char *)&yprm, sizeof yprm);
10854415cd19SGarrett Wollman 
10864415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_MASTER,
1087f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypreq_nokey, &yprnk,
1088f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypresp_master, &yprm, tv);
10894415cd19SGarrett Wollman 	if (r != RPC_SUCCESS) {
10904415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_master: clnt_call");
10912694f9b9SBill Paul 		_yp_unbind(ysd);
10924415cd19SGarrett Wollman 		goto again;
10934415cd19SGarrett Wollman 	}
10942694f9b9SBill Paul 
10950e276383SBill Paul 	if (!(r = ypprot_err(yprm.stat))) {
10960e276383SBill Paul 		*outname = (char *)strdup(yprm.peer);
10972694f9b9SBill Paul 	}
1098a169c9b1SBill Paul 
1099f249dbccSDag-Erling Smørgrav 	xdr_free((xdrproc_t)xdr_ypresp_master, &yprm);
1100d21de89aSHajimu UMEMOTO 	YPUNLOCK();
11012694f9b9SBill Paul 	return (r);
11024415cd19SGarrett Wollman }
1103dc584ddbSDag-Erling Smørgrav 
1104b9729ac2SBill Paul int
1105dc584ddbSDag-Erling Smørgrav yp_maplist(char *indomain, struct ypmaplist **outmaplist)
11064415cd19SGarrett Wollman {
11074415cd19SGarrett Wollman 	struct dom_binding *ysd;
11084415cd19SGarrett Wollman 	struct ypresp_maplist ypml;
11094415cd19SGarrett Wollman 	struct timeval tv;
11104415cd19SGarrett Wollman 	int r;
11114415cd19SGarrett Wollman 
111206643071SBill Paul 	/* Sanity check */
111306643071SBill Paul 
111406643071SBill Paul 	if (indomain == NULL || !strlen(indomain))
1115ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADARGS);
111606643071SBill Paul 
1117d21de89aSHajimu UMEMOTO 	YPLOCK();
11184415cd19SGarrett Wollman again:
1119d21de89aSHajimu UMEMOTO 	if (_yp_dobind(indomain, &ysd) != 0) {
1120d21de89aSHajimu UMEMOTO 		YPUNLOCK();
1121ed4d1c46SDag-Erling Smørgrav 		return (YPERR_DOMAIN);
1122d21de89aSHajimu UMEMOTO 	}
11234415cd19SGarrett Wollman 
11244415cd19SGarrett Wollman 	tv.tv_sec = _yplib_timeout;
11254415cd19SGarrett Wollman 	tv.tv_usec = 0;
11264415cd19SGarrett Wollman 
11274415cd19SGarrett Wollman 	bzero((char *)&ypml, sizeof ypml);
11284415cd19SGarrett Wollman 
11294415cd19SGarrett Wollman 	r = clnt_call(ysd->dom_client, YPPROC_MAPLIST,
1130f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_domainname, &indomain,
1131f249dbccSDag-Erling Smørgrav 		(xdrproc_t)xdr_ypresp_maplist, &ypml,tv);
11324415cd19SGarrett Wollman 	if (r != RPC_SUCCESS) {
11334415cd19SGarrett Wollman 		clnt_perror(ysd->dom_client, "yp_maplist: clnt_call");
11342694f9b9SBill Paul 		_yp_unbind(ysd);
11354415cd19SGarrett Wollman 		goto again;
11364415cd19SGarrett Wollman 	}
1137a169c9b1SBill Paul 	if (!(r = ypprot_err(ypml.stat))) {
11380e276383SBill Paul 		*outmaplist = ypml.maps;
11392694f9b9SBill Paul 	}
11402694f9b9SBill Paul 
1141f249dbccSDag-Erling Smørgrav 	/* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/
1142d21de89aSHajimu UMEMOTO 	YPUNLOCK();
11432694f9b9SBill Paul 	return (r);
11444415cd19SGarrett Wollman }
11454415cd19SGarrett Wollman 
1146f249dbccSDag-Erling Smørgrav const char *
1147dc584ddbSDag-Erling Smørgrav yperr_string(int incode)
11484415cd19SGarrett Wollman {
11494415cd19SGarrett Wollman 	static char err[80];
11504415cd19SGarrett Wollman 
11514415cd19SGarrett Wollman 	switch (incode) {
11524415cd19SGarrett Wollman 	case 0:
1153ed4d1c46SDag-Erling Smørgrav 		return ("Success");
11544415cd19SGarrett Wollman 	case YPERR_BADARGS:
1155ed4d1c46SDag-Erling Smørgrav 		return ("Request arguments bad");
11564415cd19SGarrett Wollman 	case YPERR_RPC:
1157ed4d1c46SDag-Erling Smørgrav 		return ("RPC failure");
11584415cd19SGarrett Wollman 	case YPERR_DOMAIN:
1159ed4d1c46SDag-Erling Smørgrav 		return ("Can't bind to server which serves this domain");
11604415cd19SGarrett Wollman 	case YPERR_MAP:
1161ed4d1c46SDag-Erling Smørgrav 		return ("No such map in server's domain");
11624415cd19SGarrett Wollman 	case YPERR_KEY:
1163ed4d1c46SDag-Erling Smørgrav 		return ("No such key in map");
11644415cd19SGarrett Wollman 	case YPERR_YPERR:
1165ed4d1c46SDag-Erling Smørgrav 		return ("YP server error");
11664415cd19SGarrett Wollman 	case YPERR_RESRC:
1167ed4d1c46SDag-Erling Smørgrav 		return ("Local resource allocation failure");
11684415cd19SGarrett Wollman 	case YPERR_NOMORE:
1169ed4d1c46SDag-Erling Smørgrav 		return ("No more records in map database");
11704415cd19SGarrett Wollman 	case YPERR_PMAP:
1171ed4d1c46SDag-Erling Smørgrav 		return ("Can't communicate with portmapper");
11724415cd19SGarrett Wollman 	case YPERR_YPBIND:
1173ed4d1c46SDag-Erling Smørgrav 		return ("Can't communicate with ypbind");
11744415cd19SGarrett Wollman 	case YPERR_YPSERV:
1175ed4d1c46SDag-Erling Smørgrav 		return ("Can't communicate with ypserv");
11764415cd19SGarrett Wollman 	case YPERR_NODOM:
1177ed4d1c46SDag-Erling Smørgrav 		return ("Local domain name not set");
11784415cd19SGarrett Wollman 	case YPERR_BADDB:
1179ed4d1c46SDag-Erling Smørgrav 		return ("Server data base is bad");
11804415cd19SGarrett Wollman 	case YPERR_VERS:
1181ed4d1c46SDag-Erling Smørgrav 		return ("YP server version mismatch - server can't supply service.");
11824415cd19SGarrett Wollman 	case YPERR_ACCESS:
1183ed4d1c46SDag-Erling Smørgrav 		return ("Access violation");
11844415cd19SGarrett Wollman 	case YPERR_BUSY:
1185ed4d1c46SDag-Erling Smørgrav 		return ("Database is busy");
11864415cd19SGarrett Wollman 	}
11874415cd19SGarrett Wollman 	sprintf(err, "YP unknown error %d\n", incode);
1188ed4d1c46SDag-Erling Smørgrav 	return (err);
11894415cd19SGarrett Wollman }
11904415cd19SGarrett Wollman 
11914415cd19SGarrett Wollman int
1192dc584ddbSDag-Erling Smørgrav ypprot_err(unsigned int incode)
11934415cd19SGarrett Wollman {
11944415cd19SGarrett Wollman 	switch (incode) {
11954415cd19SGarrett Wollman 	case YP_TRUE:
1196ed4d1c46SDag-Erling Smørgrav 		return (0);
11974415cd19SGarrett Wollman 	case YP_FALSE:
1198ed4d1c46SDag-Erling Smørgrav 		return (YPERR_YPBIND);
11994415cd19SGarrett Wollman 	case YP_NOMORE:
1200ed4d1c46SDag-Erling Smørgrav 		return (YPERR_NOMORE);
12014415cd19SGarrett Wollman 	case YP_NOMAP:
1202ed4d1c46SDag-Erling Smørgrav 		return (YPERR_MAP);
12034415cd19SGarrett Wollman 	case YP_NODOM:
1204ed4d1c46SDag-Erling Smørgrav 		return (YPERR_DOMAIN);
12054415cd19SGarrett Wollman 	case YP_NOKEY:
1206ed4d1c46SDag-Erling Smørgrav 		return (YPERR_KEY);
12074415cd19SGarrett Wollman 	case YP_BADOP:
1208ed4d1c46SDag-Erling Smørgrav 		return (YPERR_YPERR);
12094415cd19SGarrett Wollman 	case YP_BADDB:
1210ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADDB);
12114415cd19SGarrett Wollman 	case YP_YPERR:
1212ed4d1c46SDag-Erling Smørgrav 		return (YPERR_YPERR);
12134415cd19SGarrett Wollman 	case YP_BADARGS:
1214ed4d1c46SDag-Erling Smørgrav 		return (YPERR_BADARGS);
12154415cd19SGarrett Wollman 	case YP_VERS:
1216ed4d1c46SDag-Erling Smørgrav 		return (YPERR_VERS);
12174415cd19SGarrett Wollman 	}
1218ed4d1c46SDag-Erling Smørgrav 	return (YPERR_YPERR);
12194415cd19SGarrett Wollman }
12204415cd19SGarrett Wollman 
12214415cd19SGarrett Wollman int
1222dc584ddbSDag-Erling Smørgrav _yp_check(char **dom)
12234415cd19SGarrett Wollman {
12244415cd19SGarrett Wollman 	char *unused;
12254415cd19SGarrett Wollman 
1226069eb2caSHajimu UMEMOTO 	YPLOCK();
12274415cd19SGarrett Wollman 	if (_yp_domain[0]=='\0')
1228069eb2caSHajimu UMEMOTO 		if (yp_get_default_domain_locked(&unused)) {
1229069eb2caSHajimu UMEMOTO 			YPUNLOCK();
1230ed4d1c46SDag-Erling Smørgrav 			return (0);
1231069eb2caSHajimu UMEMOTO 		}
12324415cd19SGarrett Wollman 
12334415cd19SGarrett Wollman 	if (dom)
12344415cd19SGarrett Wollman 		*dom = _yp_domain;
12354415cd19SGarrett Wollman 
1236d21de89aSHajimu UMEMOTO 	if (yp_bind_locked(_yp_domain) == 0) {
1237d21de89aSHajimu UMEMOTO 		yp_unbind_locked(_yp_domain);
1238069eb2caSHajimu UMEMOTO 		YPUNLOCK();
1239ed4d1c46SDag-Erling Smørgrav 		return (1);
12400f98415eSBill Paul 	}
1241069eb2caSHajimu UMEMOTO 	YPUNLOCK();
1242ed4d1c46SDag-Erling Smørgrav 	return (0);
12434415cd19SGarrett Wollman }
1244