xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_bind.c (revision 61961e0f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*61961e0fSrobinson  */
22*61961e0fSrobinson 
23*61961e0fSrobinson /*
24*61961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
337c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
347c478bd9Sstevel@tonic-gate  * California.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "mt.h"
407c478bd9Sstevel@tonic-gate #include "../rpc/rpc_mt.h"
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
497c478bd9Sstevel@tonic-gate #include <netconfig.h>
507c478bd9Sstevel@tonic-gate #include <netdir.h>
517c478bd9Sstevel@tonic-gate #include <syslog.h>
527c478bd9Sstevel@tonic-gate #include "yp_b.h"
537c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
547c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
557c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
567c478bd9Sstevel@tonic-gate #include "nsl_stdio_prv.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #if defined(sparc)
597c478bd9Sstevel@tonic-gate #define	_FSTAT	_fstat
607c478bd9Sstevel@tonic-gate extern int _fstat(int, struct stat *);
617c478bd9Sstevel@tonic-gate #else  /* !sparc */
627c478bd9Sstevel@tonic-gate #define	_FSTAT	fstat
637c478bd9Sstevel@tonic-gate #endif	/* sparc */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #define	BFSIZE	(YPMAXDOMAIN + 32)	/* size of binding file */
677c478bd9Sstevel@tonic-gate int	 __ypipbufsize = 8192;		/* size used for clnt_tli_create */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* This should match the one in ypbind.c */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate extern int getdomainname(char *, int);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static CLIENT *getclnt(rpcprog_t, rpcvers_t, struct netconfig *, int *);
747c478bd9Sstevel@tonic-gate static struct dom_binding *load_dom_binding(struct ypbind_resp *, char *,
757c478bd9Sstevel@tonic-gate     int *);
767c478bd9Sstevel@tonic-gate static ypbind_resp *get_cached_domain(char *);
777c478bd9Sstevel@tonic-gate static int get_cached_transport(struct netconfig *, int, char *, int);
787c478bd9Sstevel@tonic-gate static int ypbind_running(int, int);
797c478bd9Sstevel@tonic-gate static void set_rdev(struct dom_binding *);
807c478bd9Sstevel@tonic-gate static int check_rdev(struct dom_binding *);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static char nullstring[] = "";
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Time parameters when talking to the ypbind and pmap processes
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	YPSLEEPTIME	5		/* Time to sleep between tries */
887c478bd9Sstevel@tonic-gate unsigned int _ypsleeptime = YPSLEEPTIME;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Time parameters when talking to the ypserv process
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #ifdef  DEBUG
957c478bd9Sstevel@tonic-gate #define	YPTIMEOUT	120		/* Total seconds for timeout */
967c478bd9Sstevel@tonic-gate #define	YPINTER_TRY	60		/* Seconds between tries */
977c478bd9Sstevel@tonic-gate #else
987c478bd9Sstevel@tonic-gate #define	YPTIMEOUT	20		/* Total seconds for timeout */
997c478bd9Sstevel@tonic-gate #define	YPINTER_TRY	5		/* Seconds between tries */
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	MAX_TRIES_FOR_NEW_YP	1	/* Number of times we'll try to */
1037c478bd9Sstevel@tonic-gate 					/* get a new YP server before   */
1047c478bd9Sstevel@tonic-gate 					/* we'll settle for an old one. */
1057c478bd9Sstevel@tonic-gate struct timeval _ypserv_timeout = {
1067c478bd9Sstevel@tonic-gate 	YPTIMEOUT,			/* Seconds */
1077c478bd9Sstevel@tonic-gate 	0				/* Microseconds */
1087c478bd9Sstevel@tonic-gate 	};
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static mutex_t			default_domain_lock = DEFAULTMUTEX;
1117c478bd9Sstevel@tonic-gate static char			*default_domain;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * The bound_domains_lock serializes all action in yp_unbind(), __yp_dobind(),
1157c478bd9Sstevel@tonic-gate  *   newborn(), check_binding() and laod_dom_binding(), not just the direct
1167c478bd9Sstevel@tonic-gate  *   manipulation of the bound_domains list.
1177c478bd9Sstevel@tonic-gate  * It also protects all of the fields within a domain binding except
1187c478bd9Sstevel@tonic-gate  *   the server_name field (which is protected by the server_name_lock).
1197c478bd9Sstevel@tonic-gate  * A better implementation might try to serialize each domain separately,
1207c478bd9Sstevel@tonic-gate  *   but normally we're only dealing with one domain (the default) anyway.
1217c478bd9Sstevel@tonic-gate  * To avoid one thread freeing a domain binding while another is using
1227c478bd9Sstevel@tonic-gate  *   the binding, we maintain a reference count for each binding.  The
1237c478bd9Sstevel@tonic-gate  *   reference count is incremented in __yp_dobind.  The thread calls
1247c478bd9Sstevel@tonic-gate  *   __yp_rel_binding() when it has finished using the binding (which
1257c478bd9Sstevel@tonic-gate  *   decrements the reference count).  If the reference count is non-zero
1267c478bd9Sstevel@tonic-gate  *   when a thread tries to free a binding, the need_free flag is set and
1277c478bd9Sstevel@tonic-gate  *   the free is delayed.  The __yp_rel_binding() routine checks the flag
1287c478bd9Sstevel@tonic-gate  *   and calls the free routine if the flag is set and the reference
1297c478bd9Sstevel@tonic-gate  *   count is zero.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate static mutex_t			bound_domains_lock = DEFAULTMUTEX;
1327c478bd9Sstevel@tonic-gate static struct dom_binding	*bound_domains; /* List of bound domains */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  *  Must be called with bound_domains_lock held or with a dom_binding
1377c478bd9Sstevel@tonic-gate  *  that cannot be referenced by another thread.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate void
140*61961e0fSrobinson free_dom_binding(struct dom_binding *p)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	if (p->ref_count != 0) {
1437c478bd9Sstevel@tonic-gate 		p->need_free = 1;
1447c478bd9Sstevel@tonic-gate 		return;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	(void) check_rdev(p);
1477c478bd9Sstevel@tonic-gate 	clnt_destroy(p->dom_client);
1487c478bd9Sstevel@tonic-gate 	free(p->dom_domain);
149*61961e0fSrobinson 	free(p);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Attempts to find a dom_binding in the list at bound_domains having the
1547c478bd9Sstevel@tonic-gate  * domain name field equal to the passed domain name, and removes it if found.
1557c478bd9Sstevel@tonic-gate  * The domain-server binding will not exist after the call to this function.
1567c478bd9Sstevel@tonic-gate  * All resources associated with the binding will be freed.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * yp_unbind is MT-safe because it serializes on bound_domains_lock.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static void
162*61961e0fSrobinson __yp_unbind_nolock(char *domain)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	struct dom_binding *p;
1657c478bd9Sstevel@tonic-gate 	struct dom_binding **prev;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if ((domain == NULL) || (strlen(domain) == 0)) {
1687c478bd9Sstevel@tonic-gate 		return;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 *  If we used a cache file to bind, then we will mark the
1737c478bd9Sstevel@tonic-gate 	 *  cache bad.  This will cause a subsequent call to __yp_dobind
1747c478bd9Sstevel@tonic-gate 	 *  to ignore the cache and talk to ypbind.  Otherwise, we
1757c478bd9Sstevel@tonic-gate 	 *  have already gotten a binding by talking to ypbind and
1767c478bd9Sstevel@tonic-gate 	 *  the binding is not good.
1777c478bd9Sstevel@tonic-gate 	 *
1787c478bd9Sstevel@tonic-gate 	 *  An optimization could be to check to see if the cache
1797c478bd9Sstevel@tonic-gate 	 *  file has changed (ypbind is pointing at a new server) and
1807c478bd9Sstevel@tonic-gate 	 *  reload the binding from it.  But that is too much work
1817c478bd9Sstevel@tonic-gate 	 *  for now.
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	for (prev = &bound_domains;  (p = *prev) != 0;  prev = &p->dom_pnext) {
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		if (strcmp(domain, p->dom_domain) == 0) {
1867c478bd9Sstevel@tonic-gate 			if (!p->cache_bad) {
1877c478bd9Sstevel@tonic-gate 				p->cache_bad = 1;
1887c478bd9Sstevel@tonic-gate 				break;
1897c478bd9Sstevel@tonic-gate 			}
1907c478bd9Sstevel@tonic-gate 			*prev = p->dom_pnext;
1917c478bd9Sstevel@tonic-gate 			free_dom_binding(p);
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate void
200*61961e0fSrobinson yp_unbind(char *domain)
2017c478bd9Sstevel@tonic-gate {
202*61961e0fSrobinson 	(void) mutex_lock(&bound_domains_lock);
2037c478bd9Sstevel@tonic-gate 	__yp_unbind_nolock(domain);
204*61961e0fSrobinson 	(void) mutex_unlock(&bound_domains_lock);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * This checks to see if this is a new process incarnation which has
2107c478bd9Sstevel@tonic-gate  * inherited bindings from a parent, and unbinds the world if so.
2117c478bd9Sstevel@tonic-gate  *
2127c478bd9Sstevel@tonic-gate  * MT-safe because it is only invoked from __yp_dobind(), which serializes
2137c478bd9Sstevel@tonic-gate  * all requests.
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate static void
216*61961e0fSrobinson newborn(void)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	static pid_t mypid;	/* Cached to detect forks */
2197c478bd9Sstevel@tonic-gate 	pid_t testpid;
2207c478bd9Sstevel@tonic-gate 	struct dom_binding *p, *q;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ((testpid = getpid()) != mypid) {
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		mypid = testpid;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		for (p = bound_domains;  p != 0;  p = q) {
2277c478bd9Sstevel@tonic-gate 			q = p->dom_pnext;
2287c478bd9Sstevel@tonic-gate 			free_dom_binding(p);
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 		bound_domains = 0;
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * This checks that the socket for a domain which has already been bound
2367c478bd9Sstevel@tonic-gate  * hasn't been closed or changed under us.  If it has, unbind the domain
2377c478bd9Sstevel@tonic-gate  * without closing the socket, which may be in use by some higher level
2387c478bd9Sstevel@tonic-gate  * code.  This returns TRUE and points the binding parameter at the found
2397c478bd9Sstevel@tonic-gate  * dom_binding if the binding is found and the socket looks OK, and FALSE
2407c478bd9Sstevel@tonic-gate  * otherwise.
2417c478bd9Sstevel@tonic-gate  *
2427c478bd9Sstevel@tonic-gate  * MT-safe because it is only invoked from __yp_dobind(), which serializes
2437c478bd9Sstevel@tonic-gate  * all requests.
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate static bool
246*61961e0fSrobinson check_binding(char *domain, struct dom_binding **binding)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
2497c478bd9Sstevel@tonic-gate 	struct ypbind_resp *ypbind_resp;
2507c478bd9Sstevel@tonic-gate 	int status;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	for (pdomb = bound_domains; pdomb != NULL; pdomb = pdomb->dom_pnext) {
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		if (strcmp(domain, pdomb->dom_domain) == 0) {
2557c478bd9Sstevel@tonic-gate 		/*
2567c478bd9Sstevel@tonic-gate 		 * XXX How do we really make sure the udp connection hasn't
2577c478bd9Sstevel@tonic-gate 		 * changes under us ? If it happens and we can't detect it,
2587c478bd9Sstevel@tonic-gate 		 * the appliction is doomed !
2597c478bd9Sstevel@tonic-gate 		 * POLICY: Let nobody do a yp_bind or __yp_dobind explicitly
2607c478bd9Sstevel@tonic-gate 		 * and forget to to yp_unbind it. All apps should go
2617c478bd9Sstevel@tonic-gate 		 * through the standard yp_match/first etc. functions.
2627c478bd9Sstevel@tonic-gate 		 */
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 			*binding = pdomb;
2657c478bd9Sstevel@tonic-gate 			return (TRUE);
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 *  We check to see if we can do a quick bind to ypserv.
2717c478bd9Sstevel@tonic-gate 	 *  If we can, then we load the binding (i.e., add it to our
2727c478bd9Sstevel@tonic-gate 	 *  cache of bindings) and then return it.
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	if ((ypbind_resp = get_cached_domain(domain)) != 0) {
2757c478bd9Sstevel@tonic-gate 		pdomb = load_dom_binding(ypbind_resp, domain, &status);
276*61961e0fSrobinson 		if (pdomb == 0)
2777c478bd9Sstevel@tonic-gate 			return (FALSE);
2787c478bd9Sstevel@tonic-gate 		*binding = pdomb;
2797c478bd9Sstevel@tonic-gate 		return (TRUE);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	return (FALSE);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  *  This routine adds a binding for a particular server to our
2867c478bd9Sstevel@tonic-gate  *  list of bound domains.  We check to see if there is actually
2877c478bd9Sstevel@tonic-gate  *  a yp server at the given address.  If not, or if there is
2887c478bd9Sstevel@tonic-gate  *  any other error, we return 0.  We have to malloc the binding
2897c478bd9Sstevel@tonic-gate  *  structure because that is what a call to ypbind returns and
2907c478bd9Sstevel@tonic-gate  *  we are basically doing what a call to ypbind would do.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate #define	SOCKADDR_SIZE (sizeof (struct sockaddr_in6))
2947c478bd9Sstevel@tonic-gate static int
2957c478bd9Sstevel@tonic-gate __yp_add_binding_netid(char *domain, char *addr, char *netid)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	struct netconfig *nconf = 0;
2987c478bd9Sstevel@tonic-gate 	struct netbuf *svcaddr = 0;
2997c478bd9Sstevel@tonic-gate 	struct ypbind_binding *binding = 0;
3007c478bd9Sstevel@tonic-gate 	int status;
3017c478bd9Sstevel@tonic-gate 	struct ypbind_resp resp;
3027c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	nconf = getnetconfigent(netid);
3057c478bd9Sstevel@tonic-gate 	if (nconf == 0)
3067c478bd9Sstevel@tonic-gate 		goto err;
3077c478bd9Sstevel@tonic-gate 
308*61961e0fSrobinson 	svcaddr = malloc(sizeof (struct netbuf));
3097c478bd9Sstevel@tonic-gate 	if (svcaddr == 0)
3107c478bd9Sstevel@tonic-gate 		goto err;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	svcaddr->maxlen = SOCKADDR_SIZE;
313*61961e0fSrobinson 	svcaddr->buf = malloc(SOCKADDR_SIZE);
3147c478bd9Sstevel@tonic-gate 	if (svcaddr->buf == 0)
3157c478bd9Sstevel@tonic-gate 		goto err;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (!rpcb_getaddr(YPPROG, YPVERS, nconf, svcaddr, addr))
3187c478bd9Sstevel@tonic-gate 		goto err;
3197c478bd9Sstevel@tonic-gate 
320*61961e0fSrobinson 	binding = malloc(sizeof (struct ypbind_binding));
3217c478bd9Sstevel@tonic-gate 	if (binding == 0)
3227c478bd9Sstevel@tonic-gate 		goto err;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	binding->ypbind_hi_vers = YPVERS;
3257c478bd9Sstevel@tonic-gate 	binding->ypbind_lo_vers = YPVERS;
3267c478bd9Sstevel@tonic-gate 	binding->ypbind_nconf = nconf;
3277c478bd9Sstevel@tonic-gate 	binding->ypbind_svcaddr = svcaddr;
3287c478bd9Sstevel@tonic-gate 	binding->ypbind_servername = (char *)strdup(addr);
3297c478bd9Sstevel@tonic-gate 	if (binding->ypbind_servername == 0)
3307c478bd9Sstevel@tonic-gate 		goto err;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	resp.ypbind_status = YPBIND_SUCC_VAL;
3337c478bd9Sstevel@tonic-gate 	resp.ypbind_resp_u.ypbind_bindinfo = binding;
3347c478bd9Sstevel@tonic-gate 
335*61961e0fSrobinson 	(void) mutex_lock(&bound_domains_lock);
3367c478bd9Sstevel@tonic-gate 	newborn();
3377c478bd9Sstevel@tonic-gate 	pdomb = load_dom_binding(&resp, domain, &status);
338*61961e0fSrobinson 	(void) mutex_unlock(&bound_domains_lock);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	return (pdomb != 0);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate err:
3437c478bd9Sstevel@tonic-gate 	if (nconf)
3447c478bd9Sstevel@tonic-gate 		freenetconfigent(nconf);
3457c478bd9Sstevel@tonic-gate 	if (svcaddr) {
3467c478bd9Sstevel@tonic-gate 		if (svcaddr->buf)
347*61961e0fSrobinson 			free(svcaddr->buf);
348*61961e0fSrobinson 		free(svcaddr);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	if (binding) {
3517c478bd9Sstevel@tonic-gate 		if (binding->ypbind_servername)
3527c478bd9Sstevel@tonic-gate 			free(binding->ypbind_servername);
3537c478bd9Sstevel@tonic-gate 		free(binding);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 	return (0);
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate int
3607c478bd9Sstevel@tonic-gate __yp_add_binding(char *domain, char *addr) {
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	int ret = __yp_add_binding_netid(domain, addr, "udp6");
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (ret == 0)
3657c478bd9Sstevel@tonic-gate 		ret = __yp_add_binding_netid(domain, addr, "udp");
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	return (ret);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * This allocates some memory for a domain binding, initialize it, and
3737c478bd9Sstevel@tonic-gate  * returns a pointer to it.  Based on the program version we ended up
3747c478bd9Sstevel@tonic-gate  * talking to ypbind with, fill out an opvector of appropriate protocol
3757c478bd9Sstevel@tonic-gate  * modules.
3767c478bd9Sstevel@tonic-gate  *
3777c478bd9Sstevel@tonic-gate  * MT-safe because it is only invoked from __yp_dobind(), which serializes
3787c478bd9Sstevel@tonic-gate  * all requests.
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate static struct dom_binding *
381*61961e0fSrobinson load_dom_binding(struct ypbind_resp *ypbind_res, char *domain, int *err)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	int fd;
3847c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
3857c478bd9Sstevel@tonic-gate 
386*61961e0fSrobinson 	pdomb = NULL;
3877c478bd9Sstevel@tonic-gate 
388*61961e0fSrobinson 	if ((pdomb = malloc(sizeof (struct dom_binding))) == NULL) {
3897c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "load_dom_binding:  malloc failure.");
3907c478bd9Sstevel@tonic-gate 		*err = YPERR_RESRC;
391*61961e0fSrobinson 		return (NULL);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	pdomb->dom_binding = ypbind_res->ypbind_resp_u.ypbind_bindinfo;
3957c478bd9Sstevel@tonic-gate 	/*
3967c478bd9Sstevel@tonic-gate 	 * Open up a path to the server, which will remain active globally.
3977c478bd9Sstevel@tonic-gate 	 */
3987c478bd9Sstevel@tonic-gate 	pdomb->dom_client = clnt_tli_create(RPC_ANYFD,
3997c478bd9Sstevel@tonic-gate 					    pdomb->dom_binding->ypbind_nconf,
4007c478bd9Sstevel@tonic-gate 					    pdomb->dom_binding->ypbind_svcaddr,
4017c478bd9Sstevel@tonic-gate 					    YPPROG, YPVERS, __ypipbufsize,
4027c478bd9Sstevel@tonic-gate 					    __ypipbufsize);
4037c478bd9Sstevel@tonic-gate 	if (pdomb->dom_client == NULL) {
4047c478bd9Sstevel@tonic-gate 		clnt_pcreateerror("yp_bind: clnt_tli_create");
405*61961e0fSrobinson 		free(pdomb);
4067c478bd9Sstevel@tonic-gate 		*err = YPERR_RPC;
407*61961e0fSrobinson 		return (NULL);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate #ifdef DEBUG
4107c478bd9Sstevel@tonic-gate (void) printf("yp_bind: clnt_tli_create suceeded\n");
4117c478bd9Sstevel@tonic-gate #endif
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	pdomb->dom_pnext = bound_domains;	/* Link this to the list as */
414*61961e0fSrobinson 	pdomb->dom_domain = malloc(strlen(domain) + (unsigned)1);
4157c478bd9Sstevel@tonic-gate 	if (pdomb->dom_domain == NULL) {
4167c478bd9Sstevel@tonic-gate 		clnt_destroy(pdomb->dom_client);
417*61961e0fSrobinson 		free(pdomb);
4187c478bd9Sstevel@tonic-gate 		*err = YPERR_RESRC;
419*61961e0fSrobinson 		return (NULL);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 *  We may not have loaded from a cache file, but we assume the
4237c478bd9Sstevel@tonic-gate 	 *  cache is good until we find out otherwise.
4247c478bd9Sstevel@tonic-gate 	 */
4257c478bd9Sstevel@tonic-gate 	pdomb->cache_bad = 0;
4267c478bd9Sstevel@tonic-gate 	set_rdev(pdomb);
4277c478bd9Sstevel@tonic-gate 	if (clnt_control(pdomb->dom_client, CLGET_FD, (char *)&fd))
4287c478bd9Sstevel@tonic-gate 		_fcntl(fd, F_SETFD, 1);  /* make it "close on exec" */
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	(void) strcpy(pdomb->dom_domain, domain); /* Remember the domain name */
4317c478bd9Sstevel@tonic-gate 	pdomb->ref_count = 0;
4327c478bd9Sstevel@tonic-gate 	pdomb->need_free = 0;
433*61961e0fSrobinson 	(void) mutex_init(&pdomb->server_name_lock, USYNC_THREAD, 0);
4347c478bd9Sstevel@tonic-gate 	bound_domains = pdomb;			/* ... the head entry */
4357c478bd9Sstevel@tonic-gate 	return (pdomb);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * XXX special code for handling C2 (passwd.adjunct) lookups when we need
4407c478bd9Sstevel@tonic-gate  * a reserved port.
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate static int
443*61961e0fSrobinson tli_open_rsvdport(struct netconfig *nconf)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	int fd;
4467c478bd9Sstevel@tonic-gate 
447*61961e0fSrobinson 	if (nconf == NULL)
4487c478bd9Sstevel@tonic-gate 		return (-1);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	fd = t_open(nconf->nc_device, O_RDWR, NULL);
451*61961e0fSrobinson 	if (fd == -1)
4527c478bd9Sstevel@tonic-gate 		return (-1);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	if (netdir_options(nconf, ND_SET_RESERVEDPORT, fd, NULL) == -1) {
455*61961e0fSrobinson 		if (t_bind(fd, NULL, NULL) == -1) {
4567c478bd9Sstevel@tonic-gate 			(void) t_close(fd);
4577c478bd9Sstevel@tonic-gate 			return (-1);
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	return (fd);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * This allocates some memory for a domain binding, initialize it, and
4657c478bd9Sstevel@tonic-gate  * returns a pointer to it.  Based on the program version we ended up
4667c478bd9Sstevel@tonic-gate  * talking to ypbind with, fill out an opvector of appropriate protocol
4677c478bd9Sstevel@tonic-gate  * modules.
4687c478bd9Sstevel@tonic-gate  *
4697c478bd9Sstevel@tonic-gate  * MT-safe because it is only invoked from __yp_dobind(), which serializes
4707c478bd9Sstevel@tonic-gate  * all requests.
4717c478bd9Sstevel@tonic-gate  *
4727c478bd9Sstevel@tonic-gate  * XXX special version for handling C2 (passwd.adjunct) lookups when we need
4737c478bd9Sstevel@tonic-gate  * a reserved port.
4747c478bd9Sstevel@tonic-gate  *
4757c478bd9Sstevel@tonic-gate  * Note that the binding is not cached. The caller has to free the binding
4767c478bd9Sstevel@tonic-gate  * using free_dom_binding().
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate static struct dom_binding *
479*61961e0fSrobinson load_dom_binding_rsvdport(struct ypbind_binding *dom_binding, char *domain,
480*61961e0fSrobinson 								int *err)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
4837c478bd9Sstevel@tonic-gate 	int fd;
4847c478bd9Sstevel@tonic-gate 
485*61961e0fSrobinson 	pdomb = NULL;
4867c478bd9Sstevel@tonic-gate 
487*61961e0fSrobinson 	if ((pdomb = malloc(sizeof (struct dom_binding))) == NULL) {
488*61961e0fSrobinson 		syslog(LOG_ERR, "load_dom_binding_rsvdport:  malloc failure.");
4897c478bd9Sstevel@tonic-gate 		*err = YPERR_RESRC;
490*61961e0fSrobinson 		return (NULL);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	pdomb->dom_binding = dom_binding;
4947c478bd9Sstevel@tonic-gate 	/*
4957c478bd9Sstevel@tonic-gate 	 * Open up a path to the server, which will remain active globally.
4967c478bd9Sstevel@tonic-gate 	 */
4977c478bd9Sstevel@tonic-gate 	fd = tli_open_rsvdport(pdomb->dom_binding->ypbind_nconf);
4987c478bd9Sstevel@tonic-gate 	if (fd < 0) {
4997c478bd9Sstevel@tonic-gate 		clnt_pcreateerror("yp_bind: tli_open_rsvdport");
500*61961e0fSrobinson 		free(pdomb);
5017c478bd9Sstevel@tonic-gate 		*err = YPERR_RPC;
502*61961e0fSrobinson 		return (NULL);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	pdomb->dom_client = clnt_tli_create(fd,
5057c478bd9Sstevel@tonic-gate 					    pdomb->dom_binding->ypbind_nconf,
5067c478bd9Sstevel@tonic-gate 					    pdomb->dom_binding->ypbind_svcaddr,
5077c478bd9Sstevel@tonic-gate 					    YPPROG, YPVERS, __ypipbufsize,
5087c478bd9Sstevel@tonic-gate 					    __ypipbufsize);
5097c478bd9Sstevel@tonic-gate 	if (pdomb->dom_client == NULL) {
5107c478bd9Sstevel@tonic-gate 		clnt_pcreateerror("yp_bind: clnt_tli_create");
511*61961e0fSrobinson 		free(pdomb);
5127c478bd9Sstevel@tonic-gate 		*err = YPERR_RPC;
513*61961e0fSrobinson 		return (NULL);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate #ifdef DEBUG
5167c478bd9Sstevel@tonic-gate (void) printf("yp_bind: clnt_tli_create suceeded\n");
5177c478bd9Sstevel@tonic-gate #endif
518*61961e0fSrobinson 	(void) CLNT_CONTROL(pdomb->dom_client, CLSET_FD_CLOSE, NULL);
5197c478bd9Sstevel@tonic-gate 
520*61961e0fSrobinson 	pdomb->dom_domain = malloc(strlen(domain) + (unsigned)1);
5217c478bd9Sstevel@tonic-gate 	if (pdomb->dom_domain == NULL) {
5227c478bd9Sstevel@tonic-gate 		clnt_destroy(pdomb->dom_client);
523*61961e0fSrobinson 		free(pdomb);
5247c478bd9Sstevel@tonic-gate 		*err = YPERR_RESRC;
525*61961e0fSrobinson 		return (NULL);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	(void) strcpy(pdomb->dom_domain, domain); /* Remember the domain name */
5297c478bd9Sstevel@tonic-gate 	pdomb->ref_count = 0;
5307c478bd9Sstevel@tonic-gate 	pdomb->need_free = 0;
5317c478bd9Sstevel@tonic-gate 	set_rdev(pdomb);
532*61961e0fSrobinson 	(void) mutex_init(&pdomb->server_name_lock, USYNC_THREAD, 0);
5337c478bd9Sstevel@tonic-gate 	return (pdomb);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate /*
5377c478bd9Sstevel@tonic-gate  * Attempts to locate a yellow pages server that serves a passed domain.  If
5387c478bd9Sstevel@tonic-gate  * one is found, an entry is created on the static list of domain-server pairs
5397c478bd9Sstevel@tonic-gate  * pointed to by cell bound_domains, a udp path to the server is created and
5407c478bd9Sstevel@tonic-gate  * the function returns 0.  Otherwise, the function returns a defined errorcode
5417c478bd9Sstevel@tonic-gate  * YPERR_xxxx.
5427c478bd9Sstevel@tonic-gate  *
5437c478bd9Sstevel@tonic-gate  * MT-safe because it serializes on bound_domains_lock.
5447c478bd9Sstevel@tonic-gate  *
5457c478bd9Sstevel@tonic-gate  * If hardlookup is set then loop forever until success, else try 4
5467c478bd9Sstevel@tonic-gate  * times (each try is relatively short) max.
5477c478bd9Sstevel@tonic-gate  */
5487c478bd9Sstevel@tonic-gate int
5497c478bd9Sstevel@tonic-gate __yp_dobind_cflookup(
5507c478bd9Sstevel@tonic-gate 	char *domain,
5517c478bd9Sstevel@tonic-gate 	struct dom_binding **binding,	/* if result==0, ptr to dom_binding */
5527c478bd9Sstevel@tonic-gate 	int hardlookup)
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate {
5557c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;	/* Ptr to new domain binding */
5567c478bd9Sstevel@tonic-gate 	struct ypbind_resp *ypbind_resp; /* Response from local ypbinder */
5577c478bd9Sstevel@tonic-gate 	struct ypbind_domain ypbd;
5587c478bd9Sstevel@tonic-gate 	int status, err = YPERR_DOMAIN;
5597c478bd9Sstevel@tonic-gate 	int tries = 4; /* if not hardlookup, try 4 times max to bind */
5607c478bd9Sstevel@tonic-gate 	int first_try = 1;
561*61961e0fSrobinson 	CLIENT *tb = NULL;
5627c478bd9Sstevel@tonic-gate 
563*61961e0fSrobinson 	if ((domain == NULL) ||(strlen(domain) == 0))
5647c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
5657c478bd9Sstevel@tonic-gate 
566*61961e0fSrobinson 	(void) mutex_lock(&bound_domains_lock);
5677c478bd9Sstevel@tonic-gate 	/*
5687c478bd9Sstevel@tonic-gate 	 * ===>
5697c478bd9Sstevel@tonic-gate 	 * If someone managed to fork() while we were holding this lock,
5707c478bd9Sstevel@tonic-gate 	 *   we'll probably end up hanging on the lock.  Tant pis.
5717c478bd9Sstevel@tonic-gate 	 */
5727c478bd9Sstevel@tonic-gate 	newborn();
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	if (check_binding(domain, binding)) {
5757c478bd9Sstevel@tonic-gate 		/*
5767c478bd9Sstevel@tonic-gate 		 *  If the cache is okay and if the underlying file
5777c478bd9Sstevel@tonic-gate 		 *  descriptor is okay (application did not close it).
5787c478bd9Sstevel@tonic-gate 		 *  then use the binding.
5797c478bd9Sstevel@tonic-gate 		 */
5807c478bd9Sstevel@tonic-gate 		if (!(*binding)->cache_bad && check_rdev(*binding)) {
5817c478bd9Sstevel@tonic-gate 			(*binding)->ref_count += 1;
582*61961e0fSrobinson 			(void) mutex_unlock(&bound_domains_lock);
5837c478bd9Sstevel@tonic-gate 			return (0);		/* We are bound */
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		/*
5877c478bd9Sstevel@tonic-gate 		 *  If we get here, one of two things happened:  the
5887c478bd9Sstevel@tonic-gate 		 *  cache is bad, or the underlying file descriptor
5897c478bd9Sstevel@tonic-gate 		 *  had changed.
5907c478bd9Sstevel@tonic-gate 		 *
5917c478bd9Sstevel@tonic-gate 		 *  If the cache is bad, then we call yp_unbind to remove
5927c478bd9Sstevel@tonic-gate 		 *  the binding.
5937c478bd9Sstevel@tonic-gate 		 *
5947c478bd9Sstevel@tonic-gate 		 *  If the file descriptor has changed, then we call
5957c478bd9Sstevel@tonic-gate 		 *  yp_unbind to remove the binding (we set cache_bad
5967c478bd9Sstevel@tonic-gate 		 *  to force yp_unbind to do the remove), and then
5977c478bd9Sstevel@tonic-gate 		 *  call check_binding to reload the binding from the
5987c478bd9Sstevel@tonic-gate 		 *  cache again.
5997c478bd9Sstevel@tonic-gate 		 */
6007c478bd9Sstevel@tonic-gate 		if ((*binding)->cache_bad) {
6017c478bd9Sstevel@tonic-gate 			__yp_unbind_nolock(domain);
6027c478bd9Sstevel@tonic-gate 		} else {
6037c478bd9Sstevel@tonic-gate 			(*binding)->cache_bad = 1;
604*61961e0fSrobinson 			(void) mutex_unlock(&bound_domains_lock);
6057c478bd9Sstevel@tonic-gate 			yp_unbind(domain);
606*61961e0fSrobinson 			(void) mutex_lock(&bound_domains_lock);
6077c478bd9Sstevel@tonic-gate 			if (check_binding(domain, binding)) {
6087c478bd9Sstevel@tonic-gate 				(*binding)->ref_count += 1;
609*61961e0fSrobinson 				(void) mutex_unlock(&bound_domains_lock);
6107c478bd9Sstevel@tonic-gate 				return (0);
6117c478bd9Sstevel@tonic-gate 			}
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	while (hardlookup ? 1 : tries--) {
6167c478bd9Sstevel@tonic-gate 		if (first_try)
6177c478bd9Sstevel@tonic-gate 			first_try = 0;
6187c478bd9Sstevel@tonic-gate 		else {
6197c478bd9Sstevel@tonic-gate 			/*
6207c478bd9Sstevel@tonic-gate 			 * ===> sleep() -- Ugh.  And with the lock held, too.
6217c478bd9Sstevel@tonic-gate 			 */
6227c478bd9Sstevel@tonic-gate 			(void) sleep(_ypsleeptime);
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 		tb = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
6257c478bd9Sstevel@tonic-gate 		if (tb == NULL) {
6267c478bd9Sstevel@tonic-gate 			if (ypbind_running(err, rpc_createerr.cf_stat))
6277c478bd9Sstevel@tonic-gate 				continue;
6287c478bd9Sstevel@tonic-gate 			break;
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 		ypbd.ypbind_domainname = domain;
6317c478bd9Sstevel@tonic-gate 		ypbd.ypbind_vers = YPVERS;
6327c478bd9Sstevel@tonic-gate 		/*
6337c478bd9Sstevel@tonic-gate 		 * The interface to ypbindproc_domain_3 is MT-unsafe, but we're
6347c478bd9Sstevel@tonic-gate 		 *   OK as long as we're the only ones who call it and we
6357c478bd9Sstevel@tonic-gate 		 *   serialize all requests (for all domains).  Otherwise,
6367c478bd9Sstevel@tonic-gate 		 *   change the interface (pass in the ypbind_resp struct).
6377c478bd9Sstevel@tonic-gate 		 */
6387c478bd9Sstevel@tonic-gate 		ypbind_resp = ypbindproc_domain_3(&ypbd, tb);
6397c478bd9Sstevel@tonic-gate 		/*
6407c478bd9Sstevel@tonic-gate 		 * Although we talk to ypbind on loopback,
6417c478bd9Sstevel@tonic-gate 		 * it gives us a udp address for the ypserv.
6427c478bd9Sstevel@tonic-gate 		 */
6437c478bd9Sstevel@tonic-gate 		if (ypbind_resp == NULL) {
6447c478bd9Sstevel@tonic-gate 			/* lost ypbind? */
6457c478bd9Sstevel@tonic-gate 			clnt_perror(tb,
6467c478bd9Sstevel@tonic-gate 				"ypbindproc_domain_3: can't contact ypbind");
6477c478bd9Sstevel@tonic-gate 			clnt_destroy(tb);
648*61961e0fSrobinson 			tb = NULL;
6497c478bd9Sstevel@tonic-gate 			continue;
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		if (ypbind_resp->ypbind_status == YPBIND_SUCC_VAL) {
6527c478bd9Sstevel@tonic-gate 			/*
6537c478bd9Sstevel@tonic-gate 			 * Local ypbind has let us in on the ypserv's address,
6547c478bd9Sstevel@tonic-gate 			 * go get in touch with it !
6557c478bd9Sstevel@tonic-gate 			 */
6567c478bd9Sstevel@tonic-gate 			pdomb = load_dom_binding(ypbind_resp, domain, &status);
6577c478bd9Sstevel@tonic-gate 			if (pdomb == 0) {
6587c478bd9Sstevel@tonic-gate 				err = status;
6597c478bd9Sstevel@tonic-gate 				clnt_destroy(tb);
660*61961e0fSrobinson 				tb = NULL;
6617c478bd9Sstevel@tonic-gate 				continue;
6627c478bd9Sstevel@tonic-gate 			}
6637c478bd9Sstevel@tonic-gate 			clnt_destroy(tb);
6647c478bd9Sstevel@tonic-gate 			pdomb->ref_count += 1;
665*61961e0fSrobinson 			(void) mutex_unlock(&bound_domains_lock);
6667c478bd9Sstevel@tonic-gate 			*binding = pdomb; /* Return ptr to the binding entry */
6677c478bd9Sstevel@tonic-gate 			return (0);		/* This is the go path */
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 		if (ypbind_resp->ypbind_resp_u.ypbind_error ==
6707c478bd9Sstevel@tonic-gate 		    YPBIND_ERR_NOSERV)
6717c478bd9Sstevel@tonic-gate 			err = YPERR_DOMAIN;
6727c478bd9Sstevel@tonic-gate 		else
6737c478bd9Sstevel@tonic-gate 			err = YPERR_YPBIND;
6747c478bd9Sstevel@tonic-gate 		clnt_destroy(tb);
675*61961e0fSrobinson 		tb = NULL;
6767c478bd9Sstevel@tonic-gate 	}
677*61961e0fSrobinson 	if (tb != NULL)
6787c478bd9Sstevel@tonic-gate 		clnt_destroy(tb);
679*61961e0fSrobinson 	(void) mutex_unlock(&bound_domains_lock);
6807c478bd9Sstevel@tonic-gate 	if (err)
6817c478bd9Sstevel@tonic-gate 		return (err);
6827c478bd9Sstevel@tonic-gate 	return (YPERR_DOMAIN);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate int
6867c478bd9Sstevel@tonic-gate __yp_dobind(
6877c478bd9Sstevel@tonic-gate 	char *domain,
6887c478bd9Sstevel@tonic-gate 	struct dom_binding **binding)	/* if result == 0, ptr to dom_binding */
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	/* traditional __yp_dobind loops forever so set hardlookup */
6917c478bd9Sstevel@tonic-gate 	return (__yp_dobind_cflookup(domain, binding, 1));
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate void
695*61961e0fSrobinson __yp_rel_binding(struct dom_binding *binding)
6967c478bd9Sstevel@tonic-gate {
697*61961e0fSrobinson 	(void) mutex_lock(&bound_domains_lock);
6987c478bd9Sstevel@tonic-gate 	binding->ref_count -= 1;
6997c478bd9Sstevel@tonic-gate 	if (binding->need_free && binding->ref_count == 0)
7007c478bd9Sstevel@tonic-gate 		free_dom_binding(binding);
701*61961e0fSrobinson 	(void) mutex_unlock(&bound_domains_lock);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate  * Attempts to locate a yellow pages server that serves a passed domain.  If
7067c478bd9Sstevel@tonic-gate  * one is found, an entry is created on the static list of domain-server pairs
7077c478bd9Sstevel@tonic-gate  * pointed to by cell bound_domains, a udp path to the server is created and
7087c478bd9Sstevel@tonic-gate  * the function returns 0.  Otherwise, the function returns a defined errorcode
7097c478bd9Sstevel@tonic-gate  * YPERR_xxxx.
7107c478bd9Sstevel@tonic-gate  *
7117c478bd9Sstevel@tonic-gate  * MT-safe because it serializes on bound_domains_lock.
7127c478bd9Sstevel@tonic-gate  *
7137c478bd9Sstevel@tonic-gate  * XXX special version for handling C2 (passwd.adjunct) lookups when we need
7147c478bd9Sstevel@tonic-gate  * a reserved port.
7157c478bd9Sstevel@tonic-gate  * This returns an uncached binding which the caller has to free using
7167c478bd9Sstevel@tonic-gate  * free_dom_binding().
7177c478bd9Sstevel@tonic-gate  */
7187c478bd9Sstevel@tonic-gate int
7197c478bd9Sstevel@tonic-gate __yp_dobind_rsvdport_cflookup(
7207c478bd9Sstevel@tonic-gate 	char *domain,
7217c478bd9Sstevel@tonic-gate 	struct dom_binding **binding,	/* if result==0, ptr to dom_binding */
7227c478bd9Sstevel@tonic-gate 	int hardlookup)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;	/* Ptr to new domain binding */
7257c478bd9Sstevel@tonic-gate 	struct ypbind_resp *ypbind_resp; /* Response from local ypbinder */
7267c478bd9Sstevel@tonic-gate 	struct ypbind_domain ypbd;
7277c478bd9Sstevel@tonic-gate 	int status,  err = YPERR_DOMAIN;
7287c478bd9Sstevel@tonic-gate 	int tries = 4; /* if not hardlookup, try a few times to bind */
7297c478bd9Sstevel@tonic-gate 	int first_try = 1;
730*61961e0fSrobinson 	CLIENT *tb = NULL;
7317c478bd9Sstevel@tonic-gate 
732*61961e0fSrobinson 	if ((domain == NULL) ||(strlen(domain) == 0))
7337c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
7347c478bd9Sstevel@tonic-gate 
735*61961e0fSrobinson 	(void) mutex_lock(&bound_domains_lock);
7367c478bd9Sstevel@tonic-gate 	/*
7377c478bd9Sstevel@tonic-gate 	 * ===>
7387c478bd9Sstevel@tonic-gate 	 * If someone managed to fork() while we were holding this lock,
7397c478bd9Sstevel@tonic-gate 	 *   we'll probably end up hanging on the lock.  Tant pis.
7407c478bd9Sstevel@tonic-gate 	 */
7417c478bd9Sstevel@tonic-gate 	newborn();
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	/*
7447c478bd9Sstevel@tonic-gate 	 * Check for existing bindings and use the information in the binding
7457c478bd9Sstevel@tonic-gate 	 * to create a transport endpoint with a reserved port.
7467c478bd9Sstevel@tonic-gate 	 */
7477c478bd9Sstevel@tonic-gate 	if (check_binding(domain, binding)) {
7487c478bd9Sstevel@tonic-gate 		/*
7497c478bd9Sstevel@tonic-gate 		 * If the cache is bad, yp_unbind() the entry again and then
7507c478bd9Sstevel@tonic-gate 		 * talk to ypbind.
7517c478bd9Sstevel@tonic-gate 		 */
7527c478bd9Sstevel@tonic-gate 		if ((*binding)->cache_bad) {
7537c478bd9Sstevel@tonic-gate 			__yp_unbind_nolock(domain);
7547c478bd9Sstevel@tonic-gate 		} else {
7557c478bd9Sstevel@tonic-gate 			pdomb = load_dom_binding_rsvdport(
7567c478bd9Sstevel@tonic-gate 						(*binding)->dom_binding,
7577c478bd9Sstevel@tonic-gate 							domain, &status);
7587c478bd9Sstevel@tonic-gate 			if (pdomb == 0) {
759*61961e0fSrobinson 				(void) mutex_unlock(&bound_domains_lock);
7607c478bd9Sstevel@tonic-gate 				return (status);
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 			pdomb->ref_count += 1;
763*61961e0fSrobinson 			(void) mutex_unlock(&bound_domains_lock);
7647c478bd9Sstevel@tonic-gate 			*binding = pdomb; /* Return ptr to the binding entry */
7657c478bd9Sstevel@tonic-gate 			return (0);
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	while (hardlookup ? 1 : tries--) {
7707c478bd9Sstevel@tonic-gate 		if (first_try)
7717c478bd9Sstevel@tonic-gate 			first_try = 0;
7727c478bd9Sstevel@tonic-gate 		else {
7737c478bd9Sstevel@tonic-gate 			/*
7747c478bd9Sstevel@tonic-gate 			 * ===> sleep() -- Ugh.  And with the lock held, too.
7757c478bd9Sstevel@tonic-gate 			 */
7767c478bd9Sstevel@tonic-gate 			(void) sleep(_ypsleeptime*tries);
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 		tb = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
7797c478bd9Sstevel@tonic-gate 		if (tb == NULL) {
7807c478bd9Sstevel@tonic-gate 			if (ypbind_running(err, rpc_createerr.cf_stat))
7817c478bd9Sstevel@tonic-gate 				continue;
7827c478bd9Sstevel@tonic-gate 			break;
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 		ypbd.ypbind_domainname = domain;
7857c478bd9Sstevel@tonic-gate 		ypbd.ypbind_vers = YPVERS;
7867c478bd9Sstevel@tonic-gate 		/*
7877c478bd9Sstevel@tonic-gate 		 * The interface to ypbindproc_domain_3 is MT-unsafe, but we're
7887c478bd9Sstevel@tonic-gate 		 *   OK as long as we're the only ones who call it and we
7897c478bd9Sstevel@tonic-gate 		 *   serialize all requests (for all domains).  Otherwise,
7907c478bd9Sstevel@tonic-gate 		 *   change the interface (pass in the ypbind_resp struct).
7917c478bd9Sstevel@tonic-gate 		 */
7927c478bd9Sstevel@tonic-gate 		ypbind_resp = ypbindproc_domain_3(&ypbd, tb);
7937c478bd9Sstevel@tonic-gate 		/*
7947c478bd9Sstevel@tonic-gate 		 * Although we talk to ypbind on loopback,
7957c478bd9Sstevel@tonic-gate 		 * it gives us a udp address for the ypserv.
7967c478bd9Sstevel@tonic-gate 		 */
7977c478bd9Sstevel@tonic-gate 		if (ypbind_resp == NULL) {
7987c478bd9Sstevel@tonic-gate 			/* lost ypbind? */
7997c478bd9Sstevel@tonic-gate 			clnt_perror(tb,
8007c478bd9Sstevel@tonic-gate 				"ypbindproc_domain_3: can't contact ypbind");
8017c478bd9Sstevel@tonic-gate 			clnt_destroy(tb);
802*61961e0fSrobinson 			tb = NULL;
8037c478bd9Sstevel@tonic-gate 			continue;
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 		if (ypbind_resp->ypbind_status == YPBIND_SUCC_VAL) {
8067c478bd9Sstevel@tonic-gate 			/*
8077c478bd9Sstevel@tonic-gate 			 * Local ypbind has let us in on the ypserv's address,
8087c478bd9Sstevel@tonic-gate 			 * go get in touch with it !
8097c478bd9Sstevel@tonic-gate 			 */
8107c478bd9Sstevel@tonic-gate 			pdomb = load_dom_binding_rsvdport(
8117c478bd9Sstevel@tonic-gate 				    ypbind_resp->ypbind_resp_u.ypbind_bindinfo,
8127c478bd9Sstevel@tonic-gate 				    domain, &status);
8137c478bd9Sstevel@tonic-gate 			if (pdomb == 0) {
8147c478bd9Sstevel@tonic-gate 				err = status;
8157c478bd9Sstevel@tonic-gate 				clnt_destroy(tb);
816*61961e0fSrobinson 				tb = NULL;
8177c478bd9Sstevel@tonic-gate 				continue;
8187c478bd9Sstevel@tonic-gate 			}
8197c478bd9Sstevel@tonic-gate 			clnt_destroy(tb);
8207c478bd9Sstevel@tonic-gate 			pdomb->ref_count += 1;
821*61961e0fSrobinson 			(void) mutex_unlock(&bound_domains_lock);
8227c478bd9Sstevel@tonic-gate 			*binding = pdomb; /* Return ptr to the binding entry */
8237c478bd9Sstevel@tonic-gate 			return (0);		/* This is the go path */
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 		if (ypbind_resp->ypbind_resp_u.ypbind_error ==
8267c478bd9Sstevel@tonic-gate 		    YPBIND_ERR_NOSERV)
8277c478bd9Sstevel@tonic-gate 			err = YPERR_DOMAIN;
8287c478bd9Sstevel@tonic-gate 		else
8297c478bd9Sstevel@tonic-gate 			err = YPERR_YPBIND;
8307c478bd9Sstevel@tonic-gate 		clnt_destroy(tb);
831*61961e0fSrobinson 		tb = NULL;
8327c478bd9Sstevel@tonic-gate 	}
833*61961e0fSrobinson 	if (tb != NULL)
8347c478bd9Sstevel@tonic-gate 		clnt_destroy(tb);
835*61961e0fSrobinson 	(void) mutex_unlock(&bound_domains_lock);
8367c478bd9Sstevel@tonic-gate 	if (err)
8377c478bd9Sstevel@tonic-gate 		return (err);
8387c478bd9Sstevel@tonic-gate 	return (YPERR_DOMAIN);
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate int
8427c478bd9Sstevel@tonic-gate __yp_dobind_rsvdport(
8437c478bd9Sstevel@tonic-gate 	char *domain,
8447c478bd9Sstevel@tonic-gate 	struct dom_binding **binding)	/* if result==0, ptr to dom_binding */
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate 	/* traditional __yp_dobind_rsvdport loops forever so set hardlookup */
8477c478bd9Sstevel@tonic-gate 	return (__yp_dobind_rsvdport_cflookup(domain, binding, 1));
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate  * This is a "wrapper" function for __yp_dobind for vanilla user-level
8527c478bd9Sstevel@tonic-gate  * functions which neither know nor care about struct dom_bindings.
8537c478bd9Sstevel@tonic-gate  */
8547c478bd9Sstevel@tonic-gate int
855*61961e0fSrobinson yp_bind(char *domain)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	struct dom_binding *binding;
859*61961e0fSrobinson 	int    res;
8607c478bd9Sstevel@tonic-gate 
861*61961e0fSrobinson 	res = __yp_dobind(domain, &binding);
862*61961e0fSrobinson 	if (res == 0)
8637c478bd9Sstevel@tonic-gate 		__yp_rel_binding(binding);
864*61961e0fSrobinson 	return (res);
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate static char *
868*61961e0fSrobinson __default_domain(void)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	char temp[256];
8717c478bd9Sstevel@tonic-gate 
872*61961e0fSrobinson 	(void) mutex_lock(&default_domain_lock);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	if (default_domain) {
875*61961e0fSrobinson 		(void) mutex_unlock(&default_domain_lock);
8767c478bd9Sstevel@tonic-gate 		return (default_domain);
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 	if (getdomainname(temp, sizeof (temp)) < 0) {
879*61961e0fSrobinson 		(void) mutex_unlock(&default_domain_lock);
8807c478bd9Sstevel@tonic-gate 		return (0);
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 	if (strlen(temp) > 0) {
883*61961e0fSrobinson 		default_domain = malloc((strlen(temp) + 1));
8847c478bd9Sstevel@tonic-gate 		if (default_domain == 0) {
885*61961e0fSrobinson 			(void) mutex_unlock(&default_domain_lock);
8867c478bd9Sstevel@tonic-gate 			return (0);
8877c478bd9Sstevel@tonic-gate 		}
8887c478bd9Sstevel@tonic-gate 		(void) strcpy(default_domain, temp);
889*61961e0fSrobinson 		(void) mutex_unlock(&default_domain_lock);
8907c478bd9Sstevel@tonic-gate 		return (default_domain);
8917c478bd9Sstevel@tonic-gate 	}
892*61961e0fSrobinson 	(void) mutex_unlock(&default_domain_lock);
8937c478bd9Sstevel@tonic-gate 	return (0);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate /*
8977c478bd9Sstevel@tonic-gate  * This is a wrapper for the system call getdomainname which returns a
8987c478bd9Sstevel@tonic-gate  * ypclnt.h error code in the failure case.  It also checks to see that
8997c478bd9Sstevel@tonic-gate  * the domain name is non-null, knowing that the null string is going to
9007c478bd9Sstevel@tonic-gate  * get rejected elsewhere in the yp client package.
9017c478bd9Sstevel@tonic-gate  */
9027c478bd9Sstevel@tonic-gate int
903*61961e0fSrobinson yp_get_default_domain(char **domain)
9047c478bd9Sstevel@tonic-gate {
905*61961e0fSrobinson 	if ((*domain = __default_domain()) != 0)
9067c478bd9Sstevel@tonic-gate 		return (0);
9077c478bd9Sstevel@tonic-gate 	return (YPERR_YPERR);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate  * ===> Nobody uses this, do they?  Can we nuke it?
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate int
914*61961e0fSrobinson usingypmap(char **ddn, char *map)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	char in, *outval = NULL;
9177c478bd9Sstevel@tonic-gate 	int outvallen, stat;
9187c478bd9Sstevel@tonic-gate 	char *domain;
9197c478bd9Sstevel@tonic-gate 
920*61961e0fSrobinson 	if ((domain = __default_domain()) == 0)
9217c478bd9Sstevel@tonic-gate 		return (FALSE);
9227c478bd9Sstevel@tonic-gate 	*ddn = domain;
9237c478bd9Sstevel@tonic-gate 	/* does the map exist ? */
9247c478bd9Sstevel@tonic-gate 	in = (char)0xff;
9257c478bd9Sstevel@tonic-gate 	stat = yp_match(domain, map, &in, 1, &outval, &outvallen);
9267c478bd9Sstevel@tonic-gate 	if (outval != NULL)
9277c478bd9Sstevel@tonic-gate 		free(outval);
9287c478bd9Sstevel@tonic-gate 	switch (stat) {
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	case 0:  /* it actually succeeded! */
9317c478bd9Sstevel@tonic-gate 	case YPERR_KEY:  /* no such key in map */
9327c478bd9Sstevel@tonic-gate 	case YPERR_NOMORE:
9337c478bd9Sstevel@tonic-gate 	case YPERR_BUSY:
9347c478bd9Sstevel@tonic-gate 		return (TRUE);
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 	return (FALSE);
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate  * Creates a quick connection on a connection oriented loopback
9417c478bd9Sstevel@tonic-gate  * transport. Fails quickly without timeout. Only naming service
9427c478bd9Sstevel@tonic-gate  * it goes to is straddr.so.
9437c478bd9Sstevel@tonic-gate  */
9447c478bd9Sstevel@tonic-gate CLIENT *
945*61961e0fSrobinson __clnt_create_loopback(rpcprog_t prog, rpcvers_t vers, int *err)
9467c478bd9Sstevel@tonic-gate {
9477c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
9487c478bd9Sstevel@tonic-gate 	CLIENT *clnt = NULL;
9497c478bd9Sstevel@tonic-gate 	void *nc_handle;	/* Net config handle */
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	*err = 0;
9527c478bd9Sstevel@tonic-gate 	nc_handle = setnetconfig();
953*61961e0fSrobinson 	if (nc_handle == NULL) {
9547c478bd9Sstevel@tonic-gate 		/* fails to open netconfig file */
9557c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_FAILED;
9567c478bd9Sstevel@tonic-gate 		*err = YPERR_RPC;
957*61961e0fSrobinson 		return (NULL);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 	while (nconf = getnetconfig(nc_handle))
9607c478bd9Sstevel@tonic-gate 		/* Try only one connection oriented loopback transport */
9617c478bd9Sstevel@tonic-gate 		if ((strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) &&
9627c478bd9Sstevel@tonic-gate 			((nconf->nc_semantics == NC_TPI_COTS) ||
9637c478bd9Sstevel@tonic-gate 			(nconf->nc_semantics == NC_TPI_COTS_ORD))) {
9647c478bd9Sstevel@tonic-gate 			clnt = getclnt(prog, vers, nconf, err);
9657c478bd9Sstevel@tonic-gate 			break;
9667c478bd9Sstevel@tonic-gate 		}
967*61961e0fSrobinson 	(void) endnetconfig(nc_handle);
9687c478bd9Sstevel@tonic-gate 
969*61961e0fSrobinson 	if (clnt == NULL) {	/* no loopback transport available */
9707c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == 0)
9717c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
9727c478bd9Sstevel@tonic-gate 		if (*err == 0) *err = YPERR_RPC;
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 	return (clnt);
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate static CLIENT *
978*61961e0fSrobinson getclnt(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf, int *err)
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate 	int fd;
9817c478bd9Sstevel@tonic-gate 	struct netbuf *svcaddr;			/* servers address */
9827c478bd9Sstevel@tonic-gate 	CLIENT *cl;
9837c478bd9Sstevel@tonic-gate 	struct nd_addrlist *nas;
9847c478bd9Sstevel@tonic-gate 	struct nd_hostserv rpcbind_hs;
9857c478bd9Sstevel@tonic-gate 	struct t_call sndcall;
9867c478bd9Sstevel@tonic-gate 	char uaddress[1024]; /* XXX maxlen ?? */
9877c478bd9Sstevel@tonic-gate 	RPCB parms;
9887c478bd9Sstevel@tonic-gate 	enum clnt_stat clnt_st;
9897c478bd9Sstevel@tonic-gate 	char *ua;
9907c478bd9Sstevel@tonic-gate 	struct timeval tv = { 30, 0 };
9917c478bd9Sstevel@tonic-gate 
992*61961e0fSrobinson 	if (nconf == NULL) {
9937c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
9947c478bd9Sstevel@tonic-gate 		*err = YPERR_RPC;
995*61961e0fSrobinson 		return (NULL);
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/*
9997c478bd9Sstevel@tonic-gate 	 *  The ypbind process might cache its transport address.
10007c478bd9Sstevel@tonic-gate 	 *  If we can get at it, then we will use it and avoid
10017c478bd9Sstevel@tonic-gate 	 *  wasting time talking to rpcbind.
10027c478bd9Sstevel@tonic-gate 	 */
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	if (get_cached_transport(nconf, vers, uaddress, sizeof (uaddress))) {
10057c478bd9Sstevel@tonic-gate 		goto create_client;
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	/*
10097c478bd9Sstevel@tonic-gate 	 * Check to see if local rpcbind is up or not. If it
10107c478bd9Sstevel@tonic-gate 	 * isn't, it is best that the application should realize
10117c478bd9Sstevel@tonic-gate 	 * yp is not up and take a remedial action. This is to
10127c478bd9Sstevel@tonic-gate 	 * avoid the minute long timeout incurred by rpcbind_getaddr.
10137c478bd9Sstevel@tonic-gate 	 * Looks like the only way to accomplish this it is to unfold
10147c478bd9Sstevel@tonic-gate 	 * rpcb_getaddr and make a few changes. Alas !
10157c478bd9Sstevel@tonic-gate 	 */
10167c478bd9Sstevel@tonic-gate 	rpcbind_hs.h_host = HOST_SELF_CONNECT;
10177c478bd9Sstevel@tonic-gate 	rpcbind_hs.h_serv = "rpcbind";
10187c478bd9Sstevel@tonic-gate 	if (netdir_getbyname(nconf, &rpcbind_hs, &nas) != ND_OK) {
10197c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
10207c478bd9Sstevel@tonic-gate 		*err = YPERR_RPC;
1021*61961e0fSrobinson 		return (NULL);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
10247c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
10257c478bd9Sstevel@tonic-gate 		*err = YPERR_RPC;
1026*61961e0fSrobinson 		return (NULL);
10277c478bd9Sstevel@tonic-gate 	}
1028*61961e0fSrobinson 	if (t_bind(fd, NULL, NULL) == -1) {
10297c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
10307c478bd9Sstevel@tonic-gate 		*err = YPERR_RPC;
10317c478bd9Sstevel@tonic-gate 		(void) t_close(fd);
1032*61961e0fSrobinson 		return (NULL);
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 	sndcall.addr = *(nas->n_addrs);
10357c478bd9Sstevel@tonic-gate 	sndcall.opt.len = 0;
10367c478bd9Sstevel@tonic-gate 	sndcall.udata.len = 0;
10377c478bd9Sstevel@tonic-gate 	if (t_connect(fd, &sndcall, NULL) == -1) {
10387c478bd9Sstevel@tonic-gate 		netdir_free((char *)nas, ND_ADDRLIST);
10397c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
10407c478bd9Sstevel@tonic-gate 		(void) t_close(fd);
10417c478bd9Sstevel@tonic-gate 		*err = YPERR_PMAP;
1042*61961e0fSrobinson 		return (NULL);
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	/*
10467c478bd9Sstevel@tonic-gate 	 * Get the address of the server
10477c478bd9Sstevel@tonic-gate 	 */
10487c478bd9Sstevel@tonic-gate 	cl = clnt_tli_create(fd, nconf, nas->n_addrs,
10497c478bd9Sstevel@tonic-gate 		RPCBPROG, RPCBVERS, __ypipbufsize, __ypipbufsize);
10507c478bd9Sstevel@tonic-gate 	netdir_free((char *)nas, ND_ADDRLIST);
1051*61961e0fSrobinson 	if (cl == NULL) {
10527c478bd9Sstevel@tonic-gate 		(void) t_close(fd);
10537c478bd9Sstevel@tonic-gate 		*err = YPERR_PMAP;
1054*61961e0fSrobinson 		return (NULL);
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 	parms.r_prog = prog;
10577c478bd9Sstevel@tonic-gate 	parms.r_vers = vers;
10587c478bd9Sstevel@tonic-gate 	parms.r_netid = nconf->nc_netid;
10597c478bd9Sstevel@tonic-gate 	parms.r_addr = nullstring;
10607c478bd9Sstevel@tonic-gate 	parms.r_owner = nullstring;
10617c478bd9Sstevel@tonic-gate 	ua = uaddress;
10627c478bd9Sstevel@tonic-gate 	clnt_st = CLNT_CALL(cl, RPCBPROC_GETADDR, xdr_rpcb, (char *)&parms,
10637c478bd9Sstevel@tonic-gate 		xdr_wrapstring, (char *)&ua, tv);
10647c478bd9Sstevel@tonic-gate 	(void) t_close(fd);
10657c478bd9Sstevel@tonic-gate 	clnt_destroy(cl);
10667c478bd9Sstevel@tonic-gate 	if (clnt_st != RPC_SUCCESS) {
10677c478bd9Sstevel@tonic-gate 		*err = YPERR_YPBIND;
1068*61961e0fSrobinson 		return (NULL);
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 	if (strlen(uaddress) == 0) {
10717c478bd9Sstevel@tonic-gate 		*err = YPERR_YPBIND;
10727c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
1073*61961e0fSrobinson 		return (NULL);
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate create_client:
10777c478bd9Sstevel@tonic-gate 	svcaddr = uaddr2taddr(nconf, uaddress);
10787c478bd9Sstevel@tonic-gate 	cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr, prog, vers,
10797c478bd9Sstevel@tonic-gate 					__ypipbufsize, __ypipbufsize);
10807c478bd9Sstevel@tonic-gate 	netdir_free((char *)svcaddr, ND_ADDR);
1081*61961e0fSrobinson 	if (cl == NULL) {
10827c478bd9Sstevel@tonic-gate 		*err = YPERR_YPBIND;
1083*61961e0fSrobinson 		return (NULL);
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 	/*
10867c478bd9Sstevel@tonic-gate 	 * The fd should be closed while destroying the handle.
10877c478bd9Sstevel@tonic-gate 	 */
10887c478bd9Sstevel@tonic-gate 	return (cl);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
1091*61961e0fSrobinson static int
1092*61961e0fSrobinson get_cached_transport(struct netconfig *nconf, int vers, char *uaddress,
1093*61961e0fSrobinson 								int ulen)
10947c478bd9Sstevel@tonic-gate {
10957c478bd9Sstevel@tonic-gate 	ssize_t st;
10967c478bd9Sstevel@tonic-gate 	int fd;
10977c478bd9Sstevel@tonic-gate 
1098*61961e0fSrobinson 	(void) snprintf(uaddress, ulen,
1099*61961e0fSrobinson 		"%s/xprt.%s.%d", BINDING, nconf->nc_netid, vers);
11007c478bd9Sstevel@tonic-gate 	fd = open(uaddress, O_RDONLY);
11017c478bd9Sstevel@tonic-gate 	if (fd == -1)
11027c478bd9Sstevel@tonic-gate 		return (0);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	/* if first byte is not locked, then ypbind must not be running */
11057c478bd9Sstevel@tonic-gate 	st = lockf(fd, F_TEST, 1);
11067c478bd9Sstevel@tonic-gate 	if (st != -1 || (errno != EAGAIN && errno != EACCES)) {
11077c478bd9Sstevel@tonic-gate 		(void) close(fd);
11087c478bd9Sstevel@tonic-gate 		return (0);
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	st = read(fd, uaddress, ulen);
11127c478bd9Sstevel@tonic-gate 	if (st == -1) {
11137c478bd9Sstevel@tonic-gate 		(void) close(fd);
11147c478bd9Sstevel@tonic-gate 		return (0);
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	(void) close(fd);
11187c478bd9Sstevel@tonic-gate 	return (1);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
1121*61961e0fSrobinson static ypbind_resp *
1122*61961e0fSrobinson get_cached_domain(char *domain)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	__NSL_FILE *fp;
11257c478bd9Sstevel@tonic-gate 	int st;
11267c478bd9Sstevel@tonic-gate 	char filename[300];
11277c478bd9Sstevel@tonic-gate 	static ypbind_resp res;
11287c478bd9Sstevel@tonic-gate 	XDR xdrs;
11297c478bd9Sstevel@tonic-gate 
1130*61961e0fSrobinson 	(void) snprintf(filename, sizeof (filename),
1131*61961e0fSrobinson 					"%s/%s/cache_binding", BINDING, domain);
11327c478bd9Sstevel@tonic-gate 	fp = __nsl_fopen(filename, "r");
11337c478bd9Sstevel@tonic-gate 	if (fp == 0)
11347c478bd9Sstevel@tonic-gate 		return (0);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	/* if first byte is not locked, then ypbind must not be running */
11377c478bd9Sstevel@tonic-gate 	st = lockf(__nsl_fileno(fp), F_TEST, 1);
11387c478bd9Sstevel@tonic-gate 	if (st != -1 || (errno != EAGAIN && errno != EACCES)) {
11397c478bd9Sstevel@tonic-gate 		(void) __nsl_fclose(fp);
11407c478bd9Sstevel@tonic-gate 		return (0);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	__nsl_xdrstdio_create(&xdrs, fp, XDR_DECODE);
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	(void) memset((char *)&res, 0, sizeof (res));
11467c478bd9Sstevel@tonic-gate 	st = xdr_ypbind_resp(&xdrs, &res);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	xdr_destroy(&xdrs);
11497c478bd9Sstevel@tonic-gate 	(void) __nsl_fclose(fp);
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	if (st)
11527c478bd9Sstevel@tonic-gate 		return (&res);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	return (0);
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate 
1157*61961e0fSrobinson static int
1158*61961e0fSrobinson ypbind_running(int err, int status)
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate 	char filename[300];
11617c478bd9Sstevel@tonic-gate 	int st;
11627c478bd9Sstevel@tonic-gate 	int fd;
11637c478bd9Sstevel@tonic-gate 
1164*61961e0fSrobinson 	(void) snprintf(filename, sizeof (filename), "%s/ypbind.pid", BINDING);
11657c478bd9Sstevel@tonic-gate 	fd = open(filename, O_RDONLY);
11667c478bd9Sstevel@tonic-gate 	if (fd == -1) {
11677c478bd9Sstevel@tonic-gate 		if ((err == YPERR_YPBIND) && (status != RPC_PROGNOTREGISTERED))
11687c478bd9Sstevel@tonic-gate 			return (1);
11697c478bd9Sstevel@tonic-gate 		return (0);
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	/* if first byte is not locked, then ypbind must not be running */
11737c478bd9Sstevel@tonic-gate 	st = lockf(fd, F_TEST, 1);
11747c478bd9Sstevel@tonic-gate 	if (st != -1 || (errno != EAGAIN && errno != EACCES)) {
11757c478bd9Sstevel@tonic-gate 		(void) close(fd);
11767c478bd9Sstevel@tonic-gate 		return (0);
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	(void) close(fd);
11807c478bd9Sstevel@tonic-gate 	return (1);
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
1183*61961e0fSrobinson static void
1184*61961e0fSrobinson set_rdev(struct dom_binding *pdomb)
11857c478bd9Sstevel@tonic-gate {
11867c478bd9Sstevel@tonic-gate 	int fd;
11877c478bd9Sstevel@tonic-gate 	struct stat stbuf;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	if (clnt_control(pdomb->dom_client, CLGET_FD, (char *)&fd) != TRUE ||
11907c478bd9Sstevel@tonic-gate 	    _FSTAT(fd, &stbuf) == -1) {
11917c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG, "ypbind client:  can't get rdev");
11927c478bd9Sstevel@tonic-gate 		pdomb->fd = -1;
11937c478bd9Sstevel@tonic-gate 		return;
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 	pdomb->fd = fd;
11967c478bd9Sstevel@tonic-gate 	pdomb->rdev = stbuf.st_rdev;
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate 
1199*61961e0fSrobinson static int
1200*61961e0fSrobinson check_rdev(struct dom_binding *pdomb)
12017c478bd9Sstevel@tonic-gate {
12027c478bd9Sstevel@tonic-gate 	struct stat stbuf;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	if (pdomb->fd == -1)
12057c478bd9Sstevel@tonic-gate 		return (1);    /* can't check it, assume it is okay */
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	if (_FSTAT(pdomb->fd, &stbuf) == -1) {
12087c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG, "yp_bind client:  can't stat %d", pdomb->fd);
12097c478bd9Sstevel@tonic-gate 		/* could be because file descriptor was closed */
12107c478bd9Sstevel@tonic-gate 		/* it's not our file descriptor, so don't try to close it */
1211*61961e0fSrobinson 		clnt_control(pdomb->dom_client, CLSET_FD_NCLOSE, NULL);
12127c478bd9Sstevel@tonic-gate 		return (0);
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 	if (pdomb->rdev != stbuf.st_rdev) {
12157c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG,
12167c478bd9Sstevel@tonic-gate 		    "yp_bind client:  fd %d changed, old=0x%x, new=0x%x",
12177c478bd9Sstevel@tonic-gate 		    pdomb->fd, pdomb->rdev, stbuf.st_rdev);
12187c478bd9Sstevel@tonic-gate 		/* it's not our file descriptor, so don't try to close it */
1219*61961e0fSrobinson 		clnt_control(pdomb->dom_client, CLSET_FD_NCLOSE, NULL);
12207c478bd9Sstevel@tonic-gate 		return (0);
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 	return (1);    /* fd is okay */
12237c478bd9Sstevel@tonic-gate }
1224