xref: /minix3/lib/libc/yp/yplib.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: yplib.c,v 1.46 2014/09/18 13:58:20 christos Exp $	 */
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  *
162fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
172fe8fb19SBen Gras  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
182fe8fb19SBen Gras  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
202fe8fb19SBen Gras  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262fe8fb19SBen Gras  * SUCH DAMAGE.
272fe8fb19SBen Gras  */
282fe8fb19SBen Gras 
292fe8fb19SBen Gras #include <sys/cdefs.h>
302fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
31*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: yplib.c,v 1.46 2014/09/18 13:58:20 christos Exp $");
322fe8fb19SBen Gras #endif
332fe8fb19SBen Gras 
342fe8fb19SBen Gras #include "namespace.h"
352fe8fb19SBen Gras #include "reentrant.h"
362fe8fb19SBen Gras 
372fe8fb19SBen Gras #include <sys/param.h>
382fe8fb19SBen Gras #include <sys/socket.h>
392fe8fb19SBen Gras #include <sys/file.h>
402fe8fb19SBen Gras #include <sys/uio.h>
412fe8fb19SBen Gras 
422fe8fb19SBen Gras #include <arpa/nameser.h>
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include <assert.h>
452fe8fb19SBen Gras #include <errno.h>
462fe8fb19SBen Gras #include <stdio.h>
472fe8fb19SBen Gras #include <stdlib.h>
482fe8fb19SBen Gras #include <string.h>
492fe8fb19SBen Gras #include <unistd.h>
502fe8fb19SBen Gras 
512fe8fb19SBen Gras #include <rpc/rpc.h>
522fe8fb19SBen Gras #include <rpc/xdr.h>
532fe8fb19SBen Gras #include <rpcsvc/yp_prot.h>
542fe8fb19SBen Gras #include <rpcsvc/ypclnt.h>
552fe8fb19SBen Gras #include "local.h"
562fe8fb19SBen Gras 
572fe8fb19SBen Gras #define BINDINGDIR	"/var/yp/binding"
582fe8fb19SBen Gras #define YPBINDLOCK	"/var/run/ypbind.lock"
592fe8fb19SBen Gras 
602fe8fb19SBen Gras struct dom_binding *_ypbindlist;
612fe8fb19SBen Gras char _yp_domain[MAXHOSTNAMELEN];
622fe8fb19SBen Gras 
632fe8fb19SBen Gras #define YPLIB_TIMEOUT		10
642fe8fb19SBen Gras #define YPLIB_RPC_RETRIES	4
652fe8fb19SBen Gras 
662fe8fb19SBen Gras struct timeval _yplib_timeout = { YPLIB_TIMEOUT, 0 };
672fe8fb19SBen Gras struct timeval _yplib_rpc_timeout = { YPLIB_TIMEOUT / YPLIB_RPC_RETRIES,
682fe8fb19SBen Gras 	1000000 * (YPLIB_TIMEOUT % YPLIB_RPC_RETRIES) / YPLIB_RPC_RETRIES };
692fe8fb19SBen Gras int _yplib_nerrs = 5;
70f14fb602SLionel Sambuc int _yplib_bindtries = 0;
712fe8fb19SBen Gras 
722fe8fb19SBen Gras #ifdef __weak_alias
732fe8fb19SBen Gras __weak_alias(yp_bind, _yp_bind)
742fe8fb19SBen Gras __weak_alias(yp_unbind, _yp_unbind)
752fe8fb19SBen Gras __weak_alias(yp_get_default_domain, _yp_get_default_domain)
76f14fb602SLionel Sambuc __weak_alias(yp_setbindtries, _yp_setbindtries)
772fe8fb19SBen Gras #endif
782fe8fb19SBen Gras 
792fe8fb19SBen Gras #ifdef _REENTRANT
802fe8fb19SBen Gras static 	mutex_t			_ypmutex = MUTEX_INITIALIZER;
812fe8fb19SBen Gras #define YPLOCK()		mutex_lock(&_ypmutex)
822fe8fb19SBen Gras #define YPUNLOCK()		mutex_unlock(&_ypmutex)
832fe8fb19SBen Gras #else
842fe8fb19SBen Gras #define YPLOCK()
852fe8fb19SBen Gras #define YPUNLOCK()
862fe8fb19SBen Gras #endif
872fe8fb19SBen Gras 
882fe8fb19SBen Gras int
yp_setbindtries(int ntries)89f14fb602SLionel Sambuc yp_setbindtries(int ntries)
90f14fb602SLionel Sambuc {
91f14fb602SLionel Sambuc 	int old_val = _yplib_bindtries;
92f14fb602SLionel Sambuc 
93f14fb602SLionel Sambuc 	if (ntries >= 0)
94f14fb602SLionel Sambuc 		_yplib_bindtries = ntries;
95f14fb602SLionel Sambuc 	return old_val;
96f14fb602SLionel Sambuc }
97f14fb602SLionel Sambuc 
98f14fb602SLionel Sambuc int
_yp_dobind(const char * dom,struct dom_binding ** ypdb)99f14fb602SLionel Sambuc _yp_dobind(const char *dom, struct dom_binding **ypdb)
1002fe8fb19SBen Gras {
1012fe8fb19SBen Gras 	static int      pid = -1;
1022fe8fb19SBen Gras 	char            path[MAXPATHLEN];
1032fe8fb19SBen Gras 	struct dom_binding *ysd, *ysd2;
1042fe8fb19SBen Gras 	struct ypbind_resp ypbr;
1052fe8fb19SBen Gras 	struct sockaddr_in clnt_sin;
1062fe8fb19SBen Gras 	int             clnt_sock, fd, gpid;
1072fe8fb19SBen Gras 	CLIENT         *client;
1082fe8fb19SBen Gras 	int             new = 0;
1092fe8fb19SBen Gras 	int             nerrs = 0;
1102fe8fb19SBen Gras 	ssize_t		r;
1112fe8fb19SBen Gras 
1122fe8fb19SBen Gras 	if (dom == NULL || *dom == '\0')
1132fe8fb19SBen Gras 		return YPERR_BADARGS;
1142fe8fb19SBen Gras 
1152fe8fb19SBen Gras 	/*
1162fe8fb19SBen Gras 	 * test if YP is running or not
1172fe8fb19SBen Gras 	 */
118*0a6a1f1dSLionel Sambuc 	if ((fd = open(YPBINDLOCK, O_RDONLY | O_CLOEXEC)) == -1)
1192fe8fb19SBen Gras 		return YPERR_YPBIND;
1202fe8fb19SBen Gras 	if (!(flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK)) {
1212fe8fb19SBen Gras 		(void)close(fd);
1222fe8fb19SBen Gras 		return YPERR_YPBIND;
1232fe8fb19SBen Gras 	}
1242fe8fb19SBen Gras 	(void)close(fd);
1252fe8fb19SBen Gras 
1262fe8fb19SBen Gras 	gpid = getpid();
1272fe8fb19SBen Gras 	if (!(pid == -1 || pid == gpid)) {
1282fe8fb19SBen Gras 		ysd = _ypbindlist;
1292fe8fb19SBen Gras 		while (ysd) {
1302fe8fb19SBen Gras 			if (ysd->dom_client)
1312fe8fb19SBen Gras 				clnt_destroy(ysd->dom_client);
1322fe8fb19SBen Gras 			ysd2 = ysd->dom_pnext;
1332fe8fb19SBen Gras 			free(ysd);
1342fe8fb19SBen Gras 			ysd = ysd2;
1352fe8fb19SBen Gras 		}
1362fe8fb19SBen Gras 		_ypbindlist = NULL;
1372fe8fb19SBen Gras 	}
1382fe8fb19SBen Gras 	pid = gpid;
1392fe8fb19SBen Gras 
1402fe8fb19SBen Gras 	if (ypdb != NULL)
1412fe8fb19SBen Gras 		*ypdb = NULL;
1422fe8fb19SBen Gras 
1432fe8fb19SBen Gras 	for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
1442fe8fb19SBen Gras 		if (strcmp(dom, ysd->dom_domain) == 0)
1452fe8fb19SBen Gras 			break;
1462fe8fb19SBen Gras 	if (ysd == NULL) {
1472fe8fb19SBen Gras 		if ((ysd = malloc(sizeof *ysd)) == NULL)
1482fe8fb19SBen Gras 			return YPERR_YPERR;
1492fe8fb19SBen Gras 		(void)memset(ysd, 0, sizeof *ysd);
1502fe8fb19SBen Gras 		ysd->dom_socket = -1;
1512fe8fb19SBen Gras 		ysd->dom_vers = 0;
1522fe8fb19SBen Gras 		new = 1;
1532fe8fb19SBen Gras 	}
1542fe8fb19SBen Gras again:
1552fe8fb19SBen Gras 	if (ysd->dom_vers == 0) {
1562fe8fb19SBen Gras 		(void) snprintf(path, sizeof(path), "%s/%s.%d",
1572fe8fb19SBen Gras 				BINDINGDIR, dom, 2);
158*0a6a1f1dSLionel Sambuc 		if ((fd = open(path, O_RDONLY | O_CLOEXEC)) == -1) {
1592fe8fb19SBen Gras 			/*
1602fe8fb19SBen Gras 			 * no binding file, YP is dead, or not yet fully
1612fe8fb19SBen Gras 			 * alive.
1622fe8fb19SBen Gras 			 */
1632fe8fb19SBen Gras 			goto trynet;
1642fe8fb19SBen Gras 		}
1652fe8fb19SBen Gras 		if (flock(fd, LOCK_EX | LOCK_NB) == -1 &&
1662fe8fb19SBen Gras 		    errno == EWOULDBLOCK) {
1672fe8fb19SBen Gras 			struct iovec    iov[2];
1682fe8fb19SBen Gras 			struct ypbind_resp ybr;
1692fe8fb19SBen Gras 			u_short         ypb_port;
1702fe8fb19SBen Gras 			struct ypbind_binding *bn;
1712fe8fb19SBen Gras 
1722fe8fb19SBen Gras 			iov[0].iov_base = &ypb_port;
1732fe8fb19SBen Gras 			iov[0].iov_len = sizeof ypb_port;
1742fe8fb19SBen Gras 			iov[1].iov_base = &ybr;
1752fe8fb19SBen Gras 			iov[1].iov_len = sizeof ybr;
1762fe8fb19SBen Gras 
1772fe8fb19SBen Gras 			r = readv(fd, iov, 2);
1782fe8fb19SBen Gras 			if (r != (ssize_t)(iov[0].iov_len + iov[1].iov_len)) {
1792fe8fb19SBen Gras 				(void)close(fd);
1802fe8fb19SBen Gras 				ysd->dom_vers = -1;
1812fe8fb19SBen Gras 				goto again;
1822fe8fb19SBen Gras 			}
1832fe8fb19SBen Gras 			(void)memset(&ysd->dom_server_addr, 0,
1842fe8fb19SBen Gras 				     sizeof ysd->dom_server_addr);
1852fe8fb19SBen Gras 			ysd->dom_server_addr.sin_len =
1862fe8fb19SBen Gras 				sizeof(struct sockaddr_in);
1872fe8fb19SBen Gras 			ysd->dom_server_addr.sin_family = AF_INET;
1882fe8fb19SBen Gras 			bn = &ybr.ypbind_respbody.ypbind_bindinfo;
1892fe8fb19SBen Gras 			ysd->dom_server_addr.sin_port =
1902fe8fb19SBen Gras 				bn->ypbind_binding_port;
1912fe8fb19SBen Gras 
1922fe8fb19SBen Gras 			ysd->dom_server_addr.sin_addr =
1932fe8fb19SBen Gras 				bn->ypbind_binding_addr;
1942fe8fb19SBen Gras 
1952fe8fb19SBen Gras 			ysd->dom_server_port = ysd->dom_server_addr.sin_port;
1962fe8fb19SBen Gras 			(void)close(fd);
1972fe8fb19SBen Gras 			goto gotit;
1982fe8fb19SBen Gras 		} else {
1992fe8fb19SBen Gras 			/* no lock on binding file, YP is dead. */
2002fe8fb19SBen Gras 			(void)close(fd);
2012fe8fb19SBen Gras 			if (new)
2022fe8fb19SBen Gras 				free(ysd);
2032fe8fb19SBen Gras 			return YPERR_YPBIND;
2042fe8fb19SBen Gras 		}
2052fe8fb19SBen Gras 	}
2062fe8fb19SBen Gras trynet:
2072fe8fb19SBen Gras 	if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
2082fe8fb19SBen Gras 		struct ypbind_binding *bn;
2092fe8fb19SBen Gras 		(void)memset(&clnt_sin, 0, sizeof clnt_sin);
2102fe8fb19SBen Gras 		clnt_sin.sin_len = sizeof(struct sockaddr_in);
2112fe8fb19SBen Gras 		clnt_sin.sin_family = AF_INET;
2122fe8fb19SBen Gras 		clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2132fe8fb19SBen Gras 
2142fe8fb19SBen Gras 		clnt_sock = RPC_ANYSOCK;
2152fe8fb19SBen Gras 		client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS,
2162fe8fb19SBen Gras 					&clnt_sock, 0, 0);
2172fe8fb19SBen Gras 		if (client == NULL) {
2182fe8fb19SBen Gras 			clnt_pcreateerror("clnttcp_create");
2192fe8fb19SBen Gras 			if (new)
2202fe8fb19SBen Gras 				free(ysd);
2212fe8fb19SBen Gras 			return YPERR_YPBIND;
2222fe8fb19SBen Gras 		}
2232fe8fb19SBen Gras 		r = clnt_call(client, (rpcproc_t)YPBINDPROC_DOMAIN,
2242fe8fb19SBen Gras 		    (xdrproc_t)xdr_ypdomain_wrap_string, &dom,
2252fe8fb19SBen Gras 		    (xdrproc_t)xdr_ypbind_resp, &ypbr, _yplib_timeout);
2262fe8fb19SBen Gras 		if (r != RPC_SUCCESS) {
227f14fb602SLionel Sambuc 			if (_yplib_bindtries <= 0 && new == 0 &&
228f14fb602SLionel Sambuc 			    ++nerrs == _yplib_nerrs) {
2292fe8fb19SBen Gras 				nerrs = 0;
2302fe8fb19SBen Gras 				fprintf(stderr,
2312fe8fb19SBen Gras 		    "YP server for domain %s not responding, still trying\n",
2322fe8fb19SBen Gras 				    dom);
2332fe8fb19SBen Gras 			}
234f14fb602SLionel Sambuc 			else if (_yplib_bindtries > 0 &&
235f14fb602SLionel Sambuc 			         ++nerrs == _yplib_bindtries) {
236f14fb602SLionel Sambuc 				free(ysd);
237f14fb602SLionel Sambuc 				return YPERR_YPBIND;
238f14fb602SLionel Sambuc 			}
2392fe8fb19SBen Gras 			clnt_destroy(client);
2402fe8fb19SBen Gras 			ysd->dom_vers = -1;
2412fe8fb19SBen Gras 			goto again;
2422fe8fb19SBen Gras 		}
2432fe8fb19SBen Gras 		clnt_destroy(client);
2442fe8fb19SBen Gras 
2452fe8fb19SBen Gras 		(void)memset(&ysd->dom_server_addr, 0,
2462fe8fb19SBen Gras 			     sizeof ysd->dom_server_addr);
2472fe8fb19SBen Gras 		ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
2482fe8fb19SBen Gras 		ysd->dom_server_addr.sin_family = AF_INET;
2492fe8fb19SBen Gras 		bn = &ypbr.ypbind_respbody.ypbind_bindinfo;
2502fe8fb19SBen Gras 		ysd->dom_server_addr.sin_port =
2512fe8fb19SBen Gras 			bn->ypbind_binding_port;
2522fe8fb19SBen Gras 		ysd->dom_server_addr.sin_addr.s_addr =
2532fe8fb19SBen Gras 			bn->ypbind_binding_addr.s_addr;
2542fe8fb19SBen Gras 		ysd->dom_server_port =
2552fe8fb19SBen Gras 			bn->ypbind_binding_port;
2562fe8fb19SBen Gras gotit:
2572fe8fb19SBen Gras 		ysd->dom_vers = YPVERS;
2582fe8fb19SBen Gras 		(void)strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
2592fe8fb19SBen Gras 	}
2602fe8fb19SBen Gras 	if (ysd->dom_client)
2612fe8fb19SBen Gras 		clnt_destroy(ysd->dom_client);
2622fe8fb19SBen Gras 	ysd->dom_socket = RPC_ANYSOCK;
2632fe8fb19SBen Gras 	ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
2642fe8fb19SBen Gras 	    YPPROG, YPVERS, _yplib_rpc_timeout, &ysd->dom_socket);
2652fe8fb19SBen Gras 	if (ysd->dom_client == NULL) {
2662fe8fb19SBen Gras 		clnt_pcreateerror("clntudp_create");
2672fe8fb19SBen Gras 		ysd->dom_vers = -1;
2682fe8fb19SBen Gras 		goto again;
2692fe8fb19SBen Gras 	}
2702fe8fb19SBen Gras 	if (fcntl(ysd->dom_socket, F_SETFD, FD_CLOEXEC) == -1)
2712fe8fb19SBen Gras 		perror("fcntl: F_SETFD");
2722fe8fb19SBen Gras 
2732fe8fb19SBen Gras 	if (new) {
2742fe8fb19SBen Gras 		ysd->dom_pnext = _ypbindlist;
2752fe8fb19SBen Gras 		_ypbindlist = ysd;
2762fe8fb19SBen Gras 	}
2772fe8fb19SBen Gras 	if (ypdb != NULL)
2782fe8fb19SBen Gras 		*ypdb = ysd;
2792fe8fb19SBen Gras 	return 0;
2802fe8fb19SBen Gras }
2812fe8fb19SBen Gras 
2822fe8fb19SBen Gras void
__yp_unbind(struct dom_binding * ypb)283f14fb602SLionel Sambuc __yp_unbind(struct dom_binding *ypb)
2842fe8fb19SBen Gras {
2852fe8fb19SBen Gras 
2862fe8fb19SBen Gras 	_DIAGASSERT(ypb != NULL);
2872fe8fb19SBen Gras 
2882fe8fb19SBen Gras 	clnt_destroy(ypb->dom_client);
2892fe8fb19SBen Gras 	ypb->dom_client = NULL;
2902fe8fb19SBen Gras 	ypb->dom_socket = -1;
2912fe8fb19SBen Gras }
2922fe8fb19SBen Gras 
2932fe8fb19SBen Gras int
yp_bind(const char * dom)294f14fb602SLionel Sambuc yp_bind(const char *dom)
2952fe8fb19SBen Gras {
2962fe8fb19SBen Gras 	if (_yp_invalid_domain(dom))
2972fe8fb19SBen Gras 		return YPERR_BADARGS;
2982fe8fb19SBen Gras 
2992fe8fb19SBen Gras 	return _yp_dobind(dom, NULL);
3002fe8fb19SBen Gras }
3012fe8fb19SBen Gras 
3022fe8fb19SBen Gras void
yp_unbind(const char * dom)303f14fb602SLionel Sambuc yp_unbind(const char *dom)
3042fe8fb19SBen Gras {
3052fe8fb19SBen Gras 	struct dom_binding *ypb, *ypbp;
3062fe8fb19SBen Gras 
3072fe8fb19SBen Gras 	if (_yp_invalid_domain(dom))
3082fe8fb19SBen Gras 		return;
3092fe8fb19SBen Gras 
3102fe8fb19SBen Gras 	ypbp = NULL;
3112fe8fb19SBen Gras 	for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
3122fe8fb19SBen Gras 		if (strcmp(dom, ypb->dom_domain) == 0) {
3132fe8fb19SBen Gras 			clnt_destroy(ypb->dom_client);
3142fe8fb19SBen Gras 			if (ypbp)
3152fe8fb19SBen Gras 				ypbp->dom_pnext = ypb->dom_pnext;
3162fe8fb19SBen Gras 			else
3172fe8fb19SBen Gras 				_ypbindlist = ypb->dom_pnext;
3182fe8fb19SBen Gras 			free(ypb);
3192fe8fb19SBen Gras 			return;
3202fe8fb19SBen Gras 		}
3212fe8fb19SBen Gras 		ypbp = ypb;
3222fe8fb19SBen Gras 	}
3232fe8fb19SBen Gras 	return;
3242fe8fb19SBen Gras }
3252fe8fb19SBen Gras 
3262fe8fb19SBen Gras int
yp_get_default_domain(char ** domp)327f14fb602SLionel Sambuc yp_get_default_domain(char **domp)
3282fe8fb19SBen Gras {
3292fe8fb19SBen Gras 	if (domp == NULL)
3302fe8fb19SBen Gras 		return YPERR_BADARGS;
3312fe8fb19SBen Gras 	*domp = NULL;
3322fe8fb19SBen Gras 	if (_yp_domain[0] == '\0')
3332fe8fb19SBen Gras 		if (getdomainname(_yp_domain, sizeof _yp_domain))
3342fe8fb19SBen Gras 			return YPERR_NODOM;
3352fe8fb19SBen Gras 	*domp = _yp_domain;
3362fe8fb19SBen Gras 	return 0;
3372fe8fb19SBen Gras }
3382fe8fb19SBen Gras 
3392fe8fb19SBen Gras int
_yp_check(char ** dom)340f14fb602SLionel Sambuc _yp_check(char **dom)
3412fe8fb19SBen Gras {
3422fe8fb19SBen Gras 	char           *unused;
3432fe8fb19SBen Gras 	int 		good;
3442fe8fb19SBen Gras 
3452fe8fb19SBen Gras 	YPLOCK();
3462fe8fb19SBen Gras 
3472fe8fb19SBen Gras 	if (_yp_domain[0] == '\0')
3482fe8fb19SBen Gras 		if (yp_get_default_domain(&unused)) {
3492fe8fb19SBen Gras 			good = 0;
3502fe8fb19SBen Gras 			goto done;
3512fe8fb19SBen Gras 		}
3522fe8fb19SBen Gras 	if (dom)
3532fe8fb19SBen Gras 		*dom = _yp_domain;
3542fe8fb19SBen Gras 
3552fe8fb19SBen Gras 	good = yp_bind(_yp_domain) == 0;
3562fe8fb19SBen Gras done:
3572fe8fb19SBen Gras 	YPUNLOCK();
3582fe8fb19SBen Gras 	return good;
3592fe8fb19SBen Gras }
3602fe8fb19SBen Gras 
3612fe8fb19SBen Gras /*
3622fe8fb19SBen Gras  * _yp_invalid_domain: check if given domainname isn't legal.
3632fe8fb19SBen Gras  * returns non-zero if invalid
3642fe8fb19SBen Gras  */
3652fe8fb19SBen Gras int
_yp_invalid_domain(const char * dom)366f14fb602SLionel Sambuc _yp_invalid_domain(const char *dom)
3672fe8fb19SBen Gras {
3682fe8fb19SBen Gras 	if (dom == NULL || *dom == '\0')
3692fe8fb19SBen Gras 		return 1;
3702fe8fb19SBen Gras 
3712fe8fb19SBen Gras 	if (strlen(dom) > YPMAXDOMAIN)
3722fe8fb19SBen Gras 		return 1;
3732fe8fb19SBen Gras 
3742fe8fb19SBen Gras 	if (strchr(dom, '/') != NULL)
3752fe8fb19SBen Gras 		return 1;
3762fe8fb19SBen Gras 
3772fe8fb19SBen Gras 	return 0;
3782fe8fb19SBen Gras }
379