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
5004388ebScasper * Common Development and Distribution License (the "License").
6004388ebScasper * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
2061961e0fSrobinson */
2161961e0fSrobinson
2261961e0fSrobinson /*
23*17074b3aSGirish Moodalbail * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate * under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate * California.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate #include "mt.h"
357c478bd9Sstevel@tonic-gate #include "../rpc/rpc_mt.h"
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
447c478bd9Sstevel@tonic-gate #include <netconfig.h>
457c478bd9Sstevel@tonic-gate #include <netdir.h>
467c478bd9Sstevel@tonic-gate #include <syslog.h>
477c478bd9Sstevel@tonic-gate #include "yp_b.h"
487c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
497c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
507c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #define BFSIZE (YPMAXDOMAIN + 32) /* size of binding file */
537c478bd9Sstevel@tonic-gate int __ypipbufsize = 8192; /* size used for clnt_tli_create */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate /* This should match the one in ypbind.c */
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate extern int getdomainname(char *, int);
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate static CLIENT *getclnt(rpcprog_t, rpcvers_t, struct netconfig *, int *);
607c478bd9Sstevel@tonic-gate static struct dom_binding *load_dom_binding(struct ypbind_resp *, char *,
617c478bd9Sstevel@tonic-gate int *);
627c478bd9Sstevel@tonic-gate static ypbind_resp *get_cached_domain(char *);
637c478bd9Sstevel@tonic-gate static int get_cached_transport(struct netconfig *, int, char *, int);
647c478bd9Sstevel@tonic-gate static int ypbind_running(int, int);
657c478bd9Sstevel@tonic-gate static void set_rdev(struct dom_binding *);
667c478bd9Sstevel@tonic-gate static int check_rdev(struct dom_binding *);
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate static char nullstring[] = "";
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate * Time parameters when talking to the ypbind and pmap processes
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate #define YPSLEEPTIME 5 /* Time to sleep between tries */
747c478bd9Sstevel@tonic-gate unsigned int _ypsleeptime = YPSLEEPTIME;
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate * Time parameters when talking to the ypserv process
787c478bd9Sstevel@tonic-gate */
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate #ifdef DEBUG
817c478bd9Sstevel@tonic-gate #define YPTIMEOUT 120 /* Total seconds for timeout */
827c478bd9Sstevel@tonic-gate #define YPINTER_TRY 60 /* Seconds between tries */
837c478bd9Sstevel@tonic-gate #else
847c478bd9Sstevel@tonic-gate #define YPTIMEOUT 20 /* Total seconds for timeout */
857c478bd9Sstevel@tonic-gate #define YPINTER_TRY 5 /* Seconds between tries */
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate #define MAX_TRIES_FOR_NEW_YP 1 /* Number of times we'll try to */
897c478bd9Sstevel@tonic-gate /* get a new YP server before */
907c478bd9Sstevel@tonic-gate /* we'll settle for an old one. */
917c478bd9Sstevel@tonic-gate struct timeval _ypserv_timeout = {
927c478bd9Sstevel@tonic-gate YPTIMEOUT, /* Seconds */
937c478bd9Sstevel@tonic-gate 0 /* Microseconds */
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate static mutex_t default_domain_lock = DEFAULTMUTEX;
977c478bd9Sstevel@tonic-gate static char *default_domain;
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate * The bound_domains_lock serializes all action in yp_unbind(), __yp_dobind(),
1017c478bd9Sstevel@tonic-gate * newborn(), check_binding() and laod_dom_binding(), not just the direct
1027c478bd9Sstevel@tonic-gate * manipulation of the bound_domains list.
1037c478bd9Sstevel@tonic-gate * It also protects all of the fields within a domain binding except
1047c478bd9Sstevel@tonic-gate * the server_name field (which is protected by the server_name_lock).
1057c478bd9Sstevel@tonic-gate * A better implementation might try to serialize each domain separately,
1067c478bd9Sstevel@tonic-gate * but normally we're only dealing with one domain (the default) anyway.
1077c478bd9Sstevel@tonic-gate * To avoid one thread freeing a domain binding while another is using
1087c478bd9Sstevel@tonic-gate * the binding, we maintain a reference count for each binding. The
1097c478bd9Sstevel@tonic-gate * reference count is incremented in __yp_dobind. The thread calls
1107c478bd9Sstevel@tonic-gate * __yp_rel_binding() when it has finished using the binding (which
1117c478bd9Sstevel@tonic-gate * decrements the reference count). If the reference count is non-zero
1127c478bd9Sstevel@tonic-gate * when a thread tries to free a binding, the need_free flag is set and
1137c478bd9Sstevel@tonic-gate * the free is delayed. The __yp_rel_binding() routine checks the flag
1147c478bd9Sstevel@tonic-gate * and calls the free routine if the flag is set and the reference
1157c478bd9Sstevel@tonic-gate * count is zero.
1167c478bd9Sstevel@tonic-gate */
1177c478bd9Sstevel@tonic-gate static mutex_t bound_domains_lock = DEFAULTMUTEX;
1187c478bd9Sstevel@tonic-gate static struct dom_binding *bound_domains; /* List of bound domains */
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * Must be called with bound_domains_lock held or with a dom_binding
1237c478bd9Sstevel@tonic-gate * that cannot be referenced by another thread.
1247c478bd9Sstevel@tonic-gate */
1257c478bd9Sstevel@tonic-gate void
free_dom_binding(struct dom_binding * p)12661961e0fSrobinson free_dom_binding(struct dom_binding *p)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate if (p->ref_count != 0) {
1297c478bd9Sstevel@tonic-gate p->need_free = 1;
1307c478bd9Sstevel@tonic-gate return;
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate (void) check_rdev(p);
1337c478bd9Sstevel@tonic-gate clnt_destroy(p->dom_client);
1347c478bd9Sstevel@tonic-gate free(p->dom_domain);
13561961e0fSrobinson free(p);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Attempts to find a dom_binding in the list at bound_domains having the
1407c478bd9Sstevel@tonic-gate * domain name field equal to the passed domain name, and removes it if found.
1417c478bd9Sstevel@tonic-gate * The domain-server binding will not exist after the call to this function.
1427c478bd9Sstevel@tonic-gate * All resources associated with the binding will be freed.
1437c478bd9Sstevel@tonic-gate *
1447c478bd9Sstevel@tonic-gate * yp_unbind is MT-safe because it serializes on bound_domains_lock.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate static void
__yp_unbind_nolock(char * domain)14861961e0fSrobinson __yp_unbind_nolock(char *domain)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate struct dom_binding *p;
1517c478bd9Sstevel@tonic-gate struct dom_binding **prev;
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate if ((domain == NULL) || (strlen(domain) == 0)) {
1547c478bd9Sstevel@tonic-gate return;
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate * If we used a cache file to bind, then we will mark the
1597c478bd9Sstevel@tonic-gate * cache bad. This will cause a subsequent call to __yp_dobind
1607c478bd9Sstevel@tonic-gate * to ignore the cache and talk to ypbind. Otherwise, we
1617c478bd9Sstevel@tonic-gate * have already gotten a binding by talking to ypbind and
1627c478bd9Sstevel@tonic-gate * the binding is not good.
1637c478bd9Sstevel@tonic-gate *
1647c478bd9Sstevel@tonic-gate * An optimization could be to check to see if the cache
1657c478bd9Sstevel@tonic-gate * file has changed (ypbind is pointing at a new server) and
1667c478bd9Sstevel@tonic-gate * reload the binding from it. But that is too much work
1677c478bd9Sstevel@tonic-gate * for now.
1687c478bd9Sstevel@tonic-gate */
1697c478bd9Sstevel@tonic-gate for (prev = &bound_domains; (p = *prev) != 0; prev = &p->dom_pnext) {
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate if (strcmp(domain, p->dom_domain) == 0) {
1727c478bd9Sstevel@tonic-gate if (!p->cache_bad) {
1737c478bd9Sstevel@tonic-gate p->cache_bad = 1;
1747c478bd9Sstevel@tonic-gate break;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate *prev = p->dom_pnext;
1777c478bd9Sstevel@tonic-gate free_dom_binding(p);
1787c478bd9Sstevel@tonic-gate break;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate void
yp_unbind(char * domain)18661961e0fSrobinson yp_unbind(char *domain)
1877c478bd9Sstevel@tonic-gate {
18861961e0fSrobinson (void) mutex_lock(&bound_domains_lock);
1897c478bd9Sstevel@tonic-gate __yp_unbind_nolock(domain);
19061961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate * This checks to see if this is a new process incarnation which has
1967c478bd9Sstevel@tonic-gate * inherited bindings from a parent, and unbinds the world if so.
1977c478bd9Sstevel@tonic-gate *
1987c478bd9Sstevel@tonic-gate * MT-safe because it is only invoked from __yp_dobind(), which serializes
1997c478bd9Sstevel@tonic-gate * all requests.
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate static void
newborn(void)20261961e0fSrobinson newborn(void)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate static pid_t mypid; /* Cached to detect forks */
2057c478bd9Sstevel@tonic-gate pid_t testpid;
2067c478bd9Sstevel@tonic-gate struct dom_binding *p, *q;
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate if ((testpid = getpid()) != mypid) {
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate mypid = testpid;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate for (p = bound_domains; p != 0; p = q) {
2137c478bd9Sstevel@tonic-gate q = p->dom_pnext;
2147c478bd9Sstevel@tonic-gate free_dom_binding(p);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate bound_domains = 0;
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate * This checks that the socket for a domain which has already been bound
2227c478bd9Sstevel@tonic-gate * hasn't been closed or changed under us. If it has, unbind the domain
2237c478bd9Sstevel@tonic-gate * without closing the socket, which may be in use by some higher level
2247c478bd9Sstevel@tonic-gate * code. This returns TRUE and points the binding parameter at the found
2257c478bd9Sstevel@tonic-gate * dom_binding if the binding is found and the socket looks OK, and FALSE
2267c478bd9Sstevel@tonic-gate * otherwise.
2277c478bd9Sstevel@tonic-gate *
2287c478bd9Sstevel@tonic-gate * MT-safe because it is only invoked from __yp_dobind(), which serializes
2297c478bd9Sstevel@tonic-gate * all requests.
2307c478bd9Sstevel@tonic-gate */
2317c478bd9Sstevel@tonic-gate static bool
check_binding(char * domain,struct dom_binding ** binding)23261961e0fSrobinson check_binding(char *domain, struct dom_binding **binding)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate struct dom_binding *pdomb;
2357c478bd9Sstevel@tonic-gate struct ypbind_resp *ypbind_resp;
2367c478bd9Sstevel@tonic-gate int status;
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate for (pdomb = bound_domains; pdomb != NULL; pdomb = pdomb->dom_pnext) {
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate if (strcmp(domain, pdomb->dom_domain) == 0) {
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate * XXX How do we really make sure the udp connection hasn't
2437c478bd9Sstevel@tonic-gate * changes under us ? If it happens and we can't detect it,
2447c478bd9Sstevel@tonic-gate * the appliction is doomed !
2457c478bd9Sstevel@tonic-gate * POLICY: Let nobody do a yp_bind or __yp_dobind explicitly
2467c478bd9Sstevel@tonic-gate * and forget to to yp_unbind it. All apps should go
2477c478bd9Sstevel@tonic-gate * through the standard yp_match/first etc. functions.
2487c478bd9Sstevel@tonic-gate */
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate *binding = pdomb;
2517c478bd9Sstevel@tonic-gate return (TRUE);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate * We check to see if we can do a quick bind to ypserv.
2577c478bd9Sstevel@tonic-gate * If we can, then we load the binding (i.e., add it to our
2587c478bd9Sstevel@tonic-gate * cache of bindings) and then return it.
2597c478bd9Sstevel@tonic-gate */
2607c478bd9Sstevel@tonic-gate if ((ypbind_resp = get_cached_domain(domain)) != 0) {
2617c478bd9Sstevel@tonic-gate pdomb = load_dom_binding(ypbind_resp, domain, &status);
26261961e0fSrobinson if (pdomb == 0)
2637c478bd9Sstevel@tonic-gate return (FALSE);
2647c478bd9Sstevel@tonic-gate *binding = pdomb;
2657c478bd9Sstevel@tonic-gate return (TRUE);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate return (FALSE);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate * This routine adds a binding for a particular server to our
2727c478bd9Sstevel@tonic-gate * list of bound domains. We check to see if there is actually
2737c478bd9Sstevel@tonic-gate * a yp server at the given address. If not, or if there is
2747c478bd9Sstevel@tonic-gate * any other error, we return 0. We have to malloc the binding
2757c478bd9Sstevel@tonic-gate * structure because that is what a call to ypbind returns and
2767c478bd9Sstevel@tonic-gate * we are basically doing what a call to ypbind would do.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate #define SOCKADDR_SIZE (sizeof (struct sockaddr_in6))
2807c478bd9Sstevel@tonic-gate static int
__yp_add_binding_netid(char * domain,char * addr,char * netid)2817c478bd9Sstevel@tonic-gate __yp_add_binding_netid(char *domain, char *addr, char *netid)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate struct netconfig *nconf = 0;
2847c478bd9Sstevel@tonic-gate struct netbuf *svcaddr = 0;
2857c478bd9Sstevel@tonic-gate struct ypbind_binding *binding = 0;
2867c478bd9Sstevel@tonic-gate int status;
2877c478bd9Sstevel@tonic-gate struct ypbind_resp resp;
2887c478bd9Sstevel@tonic-gate struct dom_binding *pdomb;
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate nconf = getnetconfigent(netid);
2917c478bd9Sstevel@tonic-gate if (nconf == 0)
2927c478bd9Sstevel@tonic-gate goto err;
2937c478bd9Sstevel@tonic-gate
29461961e0fSrobinson svcaddr = malloc(sizeof (struct netbuf));
2957c478bd9Sstevel@tonic-gate if (svcaddr == 0)
2967c478bd9Sstevel@tonic-gate goto err;
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate svcaddr->maxlen = SOCKADDR_SIZE;
29961961e0fSrobinson svcaddr->buf = malloc(SOCKADDR_SIZE);
3007c478bd9Sstevel@tonic-gate if (svcaddr->buf == 0)
3017c478bd9Sstevel@tonic-gate goto err;
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate if (!rpcb_getaddr(YPPROG, YPVERS, nconf, svcaddr, addr))
3047c478bd9Sstevel@tonic-gate goto err;
3057c478bd9Sstevel@tonic-gate
30661961e0fSrobinson binding = malloc(sizeof (struct ypbind_binding));
3077c478bd9Sstevel@tonic-gate if (binding == 0)
3087c478bd9Sstevel@tonic-gate goto err;
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate binding->ypbind_hi_vers = YPVERS;
3117c478bd9Sstevel@tonic-gate binding->ypbind_lo_vers = YPVERS;
3127c478bd9Sstevel@tonic-gate binding->ypbind_nconf = nconf;
3137c478bd9Sstevel@tonic-gate binding->ypbind_svcaddr = svcaddr;
3147c478bd9Sstevel@tonic-gate binding->ypbind_servername = (char *)strdup(addr);
3157c478bd9Sstevel@tonic-gate if (binding->ypbind_servername == 0)
3167c478bd9Sstevel@tonic-gate goto err;
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate resp.ypbind_status = YPBIND_SUCC_VAL;
3197c478bd9Sstevel@tonic-gate resp.ypbind_resp_u.ypbind_bindinfo = binding;
3207c478bd9Sstevel@tonic-gate
32161961e0fSrobinson (void) mutex_lock(&bound_domains_lock);
3227c478bd9Sstevel@tonic-gate newborn();
3237c478bd9Sstevel@tonic-gate pdomb = load_dom_binding(&resp, domain, &status);
32461961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate return (pdomb != 0);
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate err:
3297c478bd9Sstevel@tonic-gate if (nconf)
3307c478bd9Sstevel@tonic-gate freenetconfigent(nconf);
3317c478bd9Sstevel@tonic-gate if (svcaddr) {
3327c478bd9Sstevel@tonic-gate if (svcaddr->buf)
33361961e0fSrobinson free(svcaddr->buf);
33461961e0fSrobinson free(svcaddr);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate if (binding) {
3377c478bd9Sstevel@tonic-gate if (binding->ypbind_servername)
3387c478bd9Sstevel@tonic-gate free(binding->ypbind_servername);
3397c478bd9Sstevel@tonic-gate free(binding);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate return (0);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate int
__yp_add_binding(char * domain,char * addr)3467c478bd9Sstevel@tonic-gate __yp_add_binding(char *domain, char *addr) {
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate int ret = __yp_add_binding_netid(domain, addr, "udp6");
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate if (ret == 0)
3517c478bd9Sstevel@tonic-gate ret = __yp_add_binding_netid(domain, addr, "udp");
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate return (ret);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate * This allocates some memory for a domain binding, initialize it, and
3597c478bd9Sstevel@tonic-gate * returns a pointer to it. Based on the program version we ended up
3607c478bd9Sstevel@tonic-gate * talking to ypbind with, fill out an opvector of appropriate protocol
3617c478bd9Sstevel@tonic-gate * modules.
3627c478bd9Sstevel@tonic-gate *
3637c478bd9Sstevel@tonic-gate * MT-safe because it is only invoked from __yp_dobind(), which serializes
3647c478bd9Sstevel@tonic-gate * all requests.
3657c478bd9Sstevel@tonic-gate */
3667c478bd9Sstevel@tonic-gate static struct dom_binding *
load_dom_binding(struct ypbind_resp * ypbind_res,char * domain,int * err)36761961e0fSrobinson load_dom_binding(struct ypbind_resp *ypbind_res, char *domain, int *err)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate int fd;
3707c478bd9Sstevel@tonic-gate struct dom_binding *pdomb;
3717c478bd9Sstevel@tonic-gate
37261961e0fSrobinson pdomb = NULL;
3737c478bd9Sstevel@tonic-gate
37461961e0fSrobinson if ((pdomb = malloc(sizeof (struct dom_binding))) == NULL) {
3757c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "load_dom_binding: malloc failure.");
3767c478bd9Sstevel@tonic-gate *err = YPERR_RESRC;
37761961e0fSrobinson return (NULL);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate pdomb->dom_binding = ypbind_res->ypbind_resp_u.ypbind_bindinfo;
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate * Open up a path to the server, which will remain active globally.
3837c478bd9Sstevel@tonic-gate */
3847c478bd9Sstevel@tonic-gate pdomb->dom_client = clnt_tli_create(RPC_ANYFD,
3857c478bd9Sstevel@tonic-gate pdomb->dom_binding->ypbind_nconf,
3867c478bd9Sstevel@tonic-gate pdomb->dom_binding->ypbind_svcaddr,
3877c478bd9Sstevel@tonic-gate YPPROG, YPVERS, __ypipbufsize,
3887c478bd9Sstevel@tonic-gate __ypipbufsize);
3897c478bd9Sstevel@tonic-gate if (pdomb->dom_client == NULL) {
3907c478bd9Sstevel@tonic-gate clnt_pcreateerror("yp_bind: clnt_tli_create");
39161961e0fSrobinson free(pdomb);
3927c478bd9Sstevel@tonic-gate *err = YPERR_RPC;
39361961e0fSrobinson return (NULL);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate #ifdef DEBUG
3967c478bd9Sstevel@tonic-gate (void) printf("yp_bind: clnt_tli_create suceeded\n");
3977c478bd9Sstevel@tonic-gate #endif
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate pdomb->dom_pnext = bound_domains; /* Link this to the list as */
4007c478bd9Sstevel@tonic-gate pdomb->dom_domain = malloc(strlen(domain) + (unsigned)1);
4017c478bd9Sstevel@tonic-gate if (pdomb->dom_domain == NULL) {
4027c478bd9Sstevel@tonic-gate clnt_destroy(pdomb->dom_client);
40361961e0fSrobinson free(pdomb);
4047c478bd9Sstevel@tonic-gate *err = YPERR_RESRC;
40561961e0fSrobinson return (NULL);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * We may not have loaded from a cache file, but we assume the
4097c478bd9Sstevel@tonic-gate * cache is good until we find out otherwise.
4107c478bd9Sstevel@tonic-gate */
4117c478bd9Sstevel@tonic-gate pdomb->cache_bad = 0;
4127c478bd9Sstevel@tonic-gate set_rdev(pdomb);
4137c478bd9Sstevel@tonic-gate if (clnt_control(pdomb->dom_client, CLGET_FD, (char *)&fd))
414e8031f0aSraf (void) fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate (void) strcpy(pdomb->dom_domain, domain); /* Remember the domain name */
4177c478bd9Sstevel@tonic-gate pdomb->ref_count = 0;
4187c478bd9Sstevel@tonic-gate pdomb->need_free = 0;
41961961e0fSrobinson (void) mutex_init(&pdomb->server_name_lock, USYNC_THREAD, 0);
4207c478bd9Sstevel@tonic-gate bound_domains = pdomb; /* ... the head entry */
4217c478bd9Sstevel@tonic-gate return (pdomb);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate * XXX special code for handling C2 (passwd.adjunct) lookups when we need
4267c478bd9Sstevel@tonic-gate * a reserved port.
4277c478bd9Sstevel@tonic-gate */
4287c478bd9Sstevel@tonic-gate static int
tli_open_rsvdport(struct netconfig * nconf)42961961e0fSrobinson tli_open_rsvdport(struct netconfig *nconf)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate int fd;
4327c478bd9Sstevel@tonic-gate
43361961e0fSrobinson if (nconf == NULL)
4347c478bd9Sstevel@tonic-gate return (-1);
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate fd = t_open(nconf->nc_device, O_RDWR, NULL);
43761961e0fSrobinson if (fd == -1)
4387c478bd9Sstevel@tonic-gate return (-1);
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate if (netdir_options(nconf, ND_SET_RESERVEDPORT, fd, NULL) == -1) {
44161961e0fSrobinson if (t_bind(fd, NULL, NULL) == -1) {
4427c478bd9Sstevel@tonic-gate (void) t_close(fd);
4437c478bd9Sstevel@tonic-gate return (-1);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate return (fd);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate * This allocates some memory for a domain binding, initialize it, and
4517c478bd9Sstevel@tonic-gate * returns a pointer to it. Based on the program version we ended up
4527c478bd9Sstevel@tonic-gate * talking to ypbind with, fill out an opvector of appropriate protocol
4537c478bd9Sstevel@tonic-gate * modules.
4547c478bd9Sstevel@tonic-gate *
4557c478bd9Sstevel@tonic-gate * MT-safe because it is only invoked from __yp_dobind(), which serializes
4567c478bd9Sstevel@tonic-gate * all requests.
4577c478bd9Sstevel@tonic-gate *
4587c478bd9Sstevel@tonic-gate * XXX special version for handling C2 (passwd.adjunct) lookups when we need
4597c478bd9Sstevel@tonic-gate * a reserved port.
4607c478bd9Sstevel@tonic-gate *
4617c478bd9Sstevel@tonic-gate * Note that the binding is not cached. The caller has to free the binding
4627c478bd9Sstevel@tonic-gate * using free_dom_binding().
4637c478bd9Sstevel@tonic-gate */
4647c478bd9Sstevel@tonic-gate static struct dom_binding *
load_dom_binding_rsvdport(struct ypbind_binding * dom_binding,char * domain,int * err)46561961e0fSrobinson load_dom_binding_rsvdport(struct ypbind_binding *dom_binding, char *domain,
46661961e0fSrobinson int *err)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate struct dom_binding *pdomb;
4697c478bd9Sstevel@tonic-gate int fd;
4707c478bd9Sstevel@tonic-gate
47161961e0fSrobinson pdomb = NULL;
4727c478bd9Sstevel@tonic-gate
47361961e0fSrobinson if ((pdomb = malloc(sizeof (struct dom_binding))) == NULL) {
47461961e0fSrobinson syslog(LOG_ERR, "load_dom_binding_rsvdport: malloc failure.");
4757c478bd9Sstevel@tonic-gate *err = YPERR_RESRC;
47661961e0fSrobinson return (NULL);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate pdomb->dom_binding = dom_binding;
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate * Open up a path to the server, which will remain active globally.
4827c478bd9Sstevel@tonic-gate */
4837c478bd9Sstevel@tonic-gate fd = tli_open_rsvdport(pdomb->dom_binding->ypbind_nconf);
4847c478bd9Sstevel@tonic-gate if (fd < 0) {
4857c478bd9Sstevel@tonic-gate clnt_pcreateerror("yp_bind: tli_open_rsvdport");
48661961e0fSrobinson free(pdomb);
4877c478bd9Sstevel@tonic-gate *err = YPERR_RPC;
48861961e0fSrobinson return (NULL);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate pdomb->dom_client = clnt_tli_create(fd,
4917c478bd9Sstevel@tonic-gate pdomb->dom_binding->ypbind_nconf,
4927c478bd9Sstevel@tonic-gate pdomb->dom_binding->ypbind_svcaddr,
4937c478bd9Sstevel@tonic-gate YPPROG, YPVERS, __ypipbufsize,
4947c478bd9Sstevel@tonic-gate __ypipbufsize);
4957c478bd9Sstevel@tonic-gate if (pdomb->dom_client == NULL) {
4967c478bd9Sstevel@tonic-gate clnt_pcreateerror("yp_bind: clnt_tli_create");
49761961e0fSrobinson free(pdomb);
4987c478bd9Sstevel@tonic-gate *err = YPERR_RPC;
49961961e0fSrobinson return (NULL);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate #ifdef DEBUG
5027c478bd9Sstevel@tonic-gate (void) printf("yp_bind: clnt_tli_create suceeded\n");
5037c478bd9Sstevel@tonic-gate #endif
50461961e0fSrobinson (void) CLNT_CONTROL(pdomb->dom_client, CLSET_FD_CLOSE, NULL);
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate pdomb->dom_domain = malloc(strlen(domain) + (unsigned)1);
5077c478bd9Sstevel@tonic-gate if (pdomb->dom_domain == NULL) {
5087c478bd9Sstevel@tonic-gate clnt_destroy(pdomb->dom_client);
50961961e0fSrobinson free(pdomb);
5107c478bd9Sstevel@tonic-gate *err = YPERR_RESRC;
51161961e0fSrobinson return (NULL);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate (void) strcpy(pdomb->dom_domain, domain); /* Remember the domain name */
5157c478bd9Sstevel@tonic-gate pdomb->ref_count = 0;
5167c478bd9Sstevel@tonic-gate pdomb->need_free = 0;
5177c478bd9Sstevel@tonic-gate set_rdev(pdomb);
51861961e0fSrobinson (void) mutex_init(&pdomb->server_name_lock, USYNC_THREAD, 0);
5197c478bd9Sstevel@tonic-gate return (pdomb);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate * Attempts to locate a yellow pages server that serves a passed domain. If
5247c478bd9Sstevel@tonic-gate * one is found, an entry is created on the static list of domain-server pairs
5257c478bd9Sstevel@tonic-gate * pointed to by cell bound_domains, a udp path to the server is created and
5267c478bd9Sstevel@tonic-gate * the function returns 0. Otherwise, the function returns a defined errorcode
5277c478bd9Sstevel@tonic-gate * YPERR_xxxx.
5287c478bd9Sstevel@tonic-gate *
5297c478bd9Sstevel@tonic-gate * MT-safe because it serializes on bound_domains_lock.
5307c478bd9Sstevel@tonic-gate *
5317c478bd9Sstevel@tonic-gate * If hardlookup is set then loop forever until success, else try 4
5327c478bd9Sstevel@tonic-gate * times (each try is relatively short) max.
5337c478bd9Sstevel@tonic-gate */
5347c478bd9Sstevel@tonic-gate int
__yp_dobind_cflookup(char * domain,struct dom_binding ** binding,int hardlookup)5357c478bd9Sstevel@tonic-gate __yp_dobind_cflookup(
5367c478bd9Sstevel@tonic-gate char *domain,
5377c478bd9Sstevel@tonic-gate struct dom_binding **binding, /* if result==0, ptr to dom_binding */
5387c478bd9Sstevel@tonic-gate int hardlookup)
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate struct dom_binding *pdomb; /* Ptr to new domain binding */
5427c478bd9Sstevel@tonic-gate struct ypbind_resp *ypbind_resp; /* Response from local ypbinder */
5437c478bd9Sstevel@tonic-gate struct ypbind_domain ypbd;
5447c478bd9Sstevel@tonic-gate int status, err = YPERR_DOMAIN;
5457c478bd9Sstevel@tonic-gate int first_try = 1;
54661961e0fSrobinson CLIENT *tb = NULL;
5477c478bd9Sstevel@tonic-gate
54861961e0fSrobinson if ((domain == NULL) ||(strlen(domain) == 0))
5497c478bd9Sstevel@tonic-gate return (YPERR_BADARGS);
5507c478bd9Sstevel@tonic-gate
55161961e0fSrobinson (void) mutex_lock(&bound_domains_lock);
5527c478bd9Sstevel@tonic-gate /*
5537c478bd9Sstevel@tonic-gate * ===>
5547c478bd9Sstevel@tonic-gate * If someone managed to fork() while we were holding this lock,
5557c478bd9Sstevel@tonic-gate * we'll probably end up hanging on the lock. Tant pis.
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate newborn();
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate if (check_binding(domain, binding)) {
5607c478bd9Sstevel@tonic-gate /*
5617c478bd9Sstevel@tonic-gate * If the cache is okay and if the underlying file
5627c478bd9Sstevel@tonic-gate * descriptor is okay (application did not close it).
5637c478bd9Sstevel@tonic-gate * then use the binding.
5647c478bd9Sstevel@tonic-gate */
5657c478bd9Sstevel@tonic-gate if (!(*binding)->cache_bad && check_rdev(*binding)) {
5667c478bd9Sstevel@tonic-gate (*binding)->ref_count += 1;
56761961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
5687c478bd9Sstevel@tonic-gate return (0); /* We are bound */
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate * If we get here, one of two things happened: the
5737c478bd9Sstevel@tonic-gate * cache is bad, or the underlying file descriptor
5747c478bd9Sstevel@tonic-gate * had changed.
5757c478bd9Sstevel@tonic-gate *
5767c478bd9Sstevel@tonic-gate * If the cache is bad, then we call yp_unbind to remove
5777c478bd9Sstevel@tonic-gate * the binding.
5787c478bd9Sstevel@tonic-gate *
5797c478bd9Sstevel@tonic-gate * If the file descriptor has changed, then we call
5807c478bd9Sstevel@tonic-gate * yp_unbind to remove the binding (we set cache_bad
5817c478bd9Sstevel@tonic-gate * to force yp_unbind to do the remove), and then
5827c478bd9Sstevel@tonic-gate * call check_binding to reload the binding from the
5837c478bd9Sstevel@tonic-gate * cache again.
5847c478bd9Sstevel@tonic-gate */
5857c478bd9Sstevel@tonic-gate if ((*binding)->cache_bad) {
5867c478bd9Sstevel@tonic-gate __yp_unbind_nolock(domain);
5877c478bd9Sstevel@tonic-gate } else {
5887c478bd9Sstevel@tonic-gate (*binding)->cache_bad = 1;
58961961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
5907c478bd9Sstevel@tonic-gate yp_unbind(domain);
59161961e0fSrobinson (void) mutex_lock(&bound_domains_lock);
5927c478bd9Sstevel@tonic-gate if (check_binding(domain, binding)) {
5937c478bd9Sstevel@tonic-gate (*binding)->ref_count += 1;
59461961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
5957c478bd9Sstevel@tonic-gate return (0);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate
600*17074b3aSGirish Moodalbail do {
6017c478bd9Sstevel@tonic-gate if (first_try)
6027c478bd9Sstevel@tonic-gate first_try = 0;
6037c478bd9Sstevel@tonic-gate else {
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate * ===> sleep() -- Ugh. And with the lock held, too.
6067c478bd9Sstevel@tonic-gate */
6077c478bd9Sstevel@tonic-gate (void) sleep(_ypsleeptime);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate tb = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
6107c478bd9Sstevel@tonic-gate if (tb == NULL) {
6117c478bd9Sstevel@tonic-gate if (ypbind_running(err, rpc_createerr.cf_stat))
6127c478bd9Sstevel@tonic-gate continue;
6137c478bd9Sstevel@tonic-gate break;
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate ypbd.ypbind_domainname = domain;
6167c478bd9Sstevel@tonic-gate ypbd.ypbind_vers = YPVERS;
6177c478bd9Sstevel@tonic-gate /*
6187c478bd9Sstevel@tonic-gate * The interface to ypbindproc_domain_3 is MT-unsafe, but we're
6197c478bd9Sstevel@tonic-gate * OK as long as we're the only ones who call it and we
6207c478bd9Sstevel@tonic-gate * serialize all requests (for all domains). Otherwise,
6217c478bd9Sstevel@tonic-gate * change the interface (pass in the ypbind_resp struct).
6227c478bd9Sstevel@tonic-gate */
6237c478bd9Sstevel@tonic-gate ypbind_resp = ypbindproc_domain_3(&ypbd, tb);
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate * Although we talk to ypbind on loopback,
6267c478bd9Sstevel@tonic-gate * it gives us a udp address for the ypserv.
6277c478bd9Sstevel@tonic-gate */
6287c478bd9Sstevel@tonic-gate if (ypbind_resp == NULL) {
6297c478bd9Sstevel@tonic-gate /* lost ypbind? */
6307c478bd9Sstevel@tonic-gate clnt_perror(tb,
6317c478bd9Sstevel@tonic-gate "ypbindproc_domain_3: can't contact ypbind");
6327c478bd9Sstevel@tonic-gate clnt_destroy(tb);
63361961e0fSrobinson tb = NULL;
6347c478bd9Sstevel@tonic-gate continue;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate if (ypbind_resp->ypbind_status == YPBIND_SUCC_VAL) {
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate * Local ypbind has let us in on the ypserv's address,
6397c478bd9Sstevel@tonic-gate * go get in touch with it !
6407c478bd9Sstevel@tonic-gate */
6417c478bd9Sstevel@tonic-gate pdomb = load_dom_binding(ypbind_resp, domain, &status);
6427c478bd9Sstevel@tonic-gate if (pdomb == 0) {
6437c478bd9Sstevel@tonic-gate err = status;
6447c478bd9Sstevel@tonic-gate clnt_destroy(tb);
64561961e0fSrobinson tb = NULL;
6467c478bd9Sstevel@tonic-gate continue;
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate clnt_destroy(tb);
6497c478bd9Sstevel@tonic-gate pdomb->ref_count += 1;
65061961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
6517c478bd9Sstevel@tonic-gate *binding = pdomb; /* Return ptr to the binding entry */
6527c478bd9Sstevel@tonic-gate return (0); /* This is the go path */
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate if (ypbind_resp->ypbind_resp_u.ypbind_error ==
6557c478bd9Sstevel@tonic-gate YPBIND_ERR_NOSERV)
6567c478bd9Sstevel@tonic-gate err = YPERR_DOMAIN;
6577c478bd9Sstevel@tonic-gate else
6587c478bd9Sstevel@tonic-gate err = YPERR_YPBIND;
6597c478bd9Sstevel@tonic-gate clnt_destroy(tb);
66061961e0fSrobinson tb = NULL;
661*17074b3aSGirish Moodalbail } while (hardlookup);
662*17074b3aSGirish Moodalbail
66361961e0fSrobinson if (tb != NULL)
6647c478bd9Sstevel@tonic-gate clnt_destroy(tb);
66561961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
6667c478bd9Sstevel@tonic-gate if (err)
6677c478bd9Sstevel@tonic-gate return (err);
6687c478bd9Sstevel@tonic-gate return (YPERR_DOMAIN);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate int
__yp_dobind(char * domain,struct dom_binding ** binding)6727c478bd9Sstevel@tonic-gate __yp_dobind(
6737c478bd9Sstevel@tonic-gate char *domain,
6747c478bd9Sstevel@tonic-gate struct dom_binding **binding) /* if result == 0, ptr to dom_binding */
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate /* traditional __yp_dobind loops forever so set hardlookup */
6777c478bd9Sstevel@tonic-gate return (__yp_dobind_cflookup(domain, binding, 1));
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate void
__yp_rel_binding(struct dom_binding * binding)68161961e0fSrobinson __yp_rel_binding(struct dom_binding *binding)
6827c478bd9Sstevel@tonic-gate {
68361961e0fSrobinson (void) mutex_lock(&bound_domains_lock);
6847c478bd9Sstevel@tonic-gate binding->ref_count -= 1;
6857c478bd9Sstevel@tonic-gate if (binding->need_free && binding->ref_count == 0)
6867c478bd9Sstevel@tonic-gate free_dom_binding(binding);
68761961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate * Attempts to locate a yellow pages server that serves a passed domain. If
6927c478bd9Sstevel@tonic-gate * one is found, an entry is created on the static list of domain-server pairs
6937c478bd9Sstevel@tonic-gate * pointed to by cell bound_domains, a udp path to the server is created and
6947c478bd9Sstevel@tonic-gate * the function returns 0. Otherwise, the function returns a defined errorcode
6957c478bd9Sstevel@tonic-gate * YPERR_xxxx.
6967c478bd9Sstevel@tonic-gate *
6977c478bd9Sstevel@tonic-gate * MT-safe because it serializes on bound_domains_lock.
6987c478bd9Sstevel@tonic-gate *
6997c478bd9Sstevel@tonic-gate * XXX special version for handling C2 (passwd.adjunct) lookups when we need
7007c478bd9Sstevel@tonic-gate * a reserved port.
7017c478bd9Sstevel@tonic-gate * This returns an uncached binding which the caller has to free using
7027c478bd9Sstevel@tonic-gate * free_dom_binding().
7037c478bd9Sstevel@tonic-gate */
7047c478bd9Sstevel@tonic-gate int
__yp_dobind_rsvdport_cflookup(char * domain,struct dom_binding ** binding,int hardlookup)7057c478bd9Sstevel@tonic-gate __yp_dobind_rsvdport_cflookup(
7067c478bd9Sstevel@tonic-gate char *domain,
7077c478bd9Sstevel@tonic-gate struct dom_binding **binding, /* if result==0, ptr to dom_binding */
7087c478bd9Sstevel@tonic-gate int hardlookup)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate struct dom_binding *pdomb; /* Ptr to new domain binding */
7117c478bd9Sstevel@tonic-gate struct ypbind_resp *ypbind_resp; /* Response from local ypbinder */
7127c478bd9Sstevel@tonic-gate struct ypbind_domain ypbd;
7137c478bd9Sstevel@tonic-gate int status, err = YPERR_DOMAIN;
7147c478bd9Sstevel@tonic-gate int first_try = 1;
71561961e0fSrobinson CLIENT *tb = NULL;
7167c478bd9Sstevel@tonic-gate
71761961e0fSrobinson if ((domain == NULL) ||(strlen(domain) == 0))
7187c478bd9Sstevel@tonic-gate return (YPERR_BADARGS);
7197c478bd9Sstevel@tonic-gate
72061961e0fSrobinson (void) mutex_lock(&bound_domains_lock);
7217c478bd9Sstevel@tonic-gate /*
7227c478bd9Sstevel@tonic-gate * ===>
7237c478bd9Sstevel@tonic-gate * If someone managed to fork() while we were holding this lock,
7247c478bd9Sstevel@tonic-gate * we'll probably end up hanging on the lock. Tant pis.
7257c478bd9Sstevel@tonic-gate */
7267c478bd9Sstevel@tonic-gate newborn();
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate /*
7297c478bd9Sstevel@tonic-gate * Check for existing bindings and use the information in the binding
7307c478bd9Sstevel@tonic-gate * to create a transport endpoint with a reserved port.
7317c478bd9Sstevel@tonic-gate */
7327c478bd9Sstevel@tonic-gate if (check_binding(domain, binding)) {
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate * If the cache is bad, yp_unbind() the entry again and then
7357c478bd9Sstevel@tonic-gate * talk to ypbind.
7367c478bd9Sstevel@tonic-gate */
7377c478bd9Sstevel@tonic-gate if ((*binding)->cache_bad) {
7387c478bd9Sstevel@tonic-gate __yp_unbind_nolock(domain);
7397c478bd9Sstevel@tonic-gate } else {
7407c478bd9Sstevel@tonic-gate pdomb = load_dom_binding_rsvdport(
7417c478bd9Sstevel@tonic-gate (*binding)->dom_binding,
7427c478bd9Sstevel@tonic-gate domain, &status);
7437c478bd9Sstevel@tonic-gate if (pdomb == 0) {
74461961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
7457c478bd9Sstevel@tonic-gate return (status);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate pdomb->ref_count += 1;
74861961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
7497c478bd9Sstevel@tonic-gate *binding = pdomb; /* Return ptr to the binding entry */
7507c478bd9Sstevel@tonic-gate return (0);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate
754*17074b3aSGirish Moodalbail do {
7557c478bd9Sstevel@tonic-gate if (first_try)
7567c478bd9Sstevel@tonic-gate first_try = 0;
7577c478bd9Sstevel@tonic-gate else {
7587c478bd9Sstevel@tonic-gate /*
7597c478bd9Sstevel@tonic-gate * ===> sleep() -- Ugh. And with the lock held, too.
7607c478bd9Sstevel@tonic-gate */
761*17074b3aSGirish Moodalbail (void) sleep(_ypsleeptime);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate tb = __clnt_create_loopback(YPBINDPROG, YPBINDVERS, &err);
7647c478bd9Sstevel@tonic-gate if (tb == NULL) {
7657c478bd9Sstevel@tonic-gate if (ypbind_running(err, rpc_createerr.cf_stat))
7667c478bd9Sstevel@tonic-gate continue;
7677c478bd9Sstevel@tonic-gate break;
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate ypbd.ypbind_domainname = domain;
7707c478bd9Sstevel@tonic-gate ypbd.ypbind_vers = YPVERS;
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate * The interface to ypbindproc_domain_3 is MT-unsafe, but we're
7737c478bd9Sstevel@tonic-gate * OK as long as we're the only ones who call it and we
7747c478bd9Sstevel@tonic-gate * serialize all requests (for all domains). Otherwise,
7757c478bd9Sstevel@tonic-gate * change the interface (pass in the ypbind_resp struct).
7767c478bd9Sstevel@tonic-gate */
7777c478bd9Sstevel@tonic-gate ypbind_resp = ypbindproc_domain_3(&ypbd, tb);
7787c478bd9Sstevel@tonic-gate /*
7797c478bd9Sstevel@tonic-gate * Although we talk to ypbind on loopback,
7807c478bd9Sstevel@tonic-gate * it gives us a udp address for the ypserv.
7817c478bd9Sstevel@tonic-gate */
7827c478bd9Sstevel@tonic-gate if (ypbind_resp == NULL) {
7837c478bd9Sstevel@tonic-gate /* lost ypbind? */
7847c478bd9Sstevel@tonic-gate clnt_perror(tb,
7857c478bd9Sstevel@tonic-gate "ypbindproc_domain_3: can't contact ypbind");
7867c478bd9Sstevel@tonic-gate clnt_destroy(tb);
78761961e0fSrobinson tb = NULL;
7887c478bd9Sstevel@tonic-gate continue;
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate if (ypbind_resp->ypbind_status == YPBIND_SUCC_VAL) {
7917c478bd9Sstevel@tonic-gate /*
7927c478bd9Sstevel@tonic-gate * Local ypbind has let us in on the ypserv's address,
7937c478bd9Sstevel@tonic-gate * go get in touch with it !
7947c478bd9Sstevel@tonic-gate */
7957c478bd9Sstevel@tonic-gate pdomb = load_dom_binding_rsvdport(
7967c478bd9Sstevel@tonic-gate ypbind_resp->ypbind_resp_u.ypbind_bindinfo,
7977c478bd9Sstevel@tonic-gate domain, &status);
7987c478bd9Sstevel@tonic-gate if (pdomb == 0) {
7997c478bd9Sstevel@tonic-gate err = status;
8007c478bd9Sstevel@tonic-gate clnt_destroy(tb);
80161961e0fSrobinson tb = NULL;
8027c478bd9Sstevel@tonic-gate continue;
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate clnt_destroy(tb);
8057c478bd9Sstevel@tonic-gate pdomb->ref_count += 1;
80661961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
8077c478bd9Sstevel@tonic-gate *binding = pdomb; /* Return ptr to the binding entry */
8087c478bd9Sstevel@tonic-gate return (0); /* This is the go path */
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate if (ypbind_resp->ypbind_resp_u.ypbind_error ==
8117c478bd9Sstevel@tonic-gate YPBIND_ERR_NOSERV)
8127c478bd9Sstevel@tonic-gate err = YPERR_DOMAIN;
8137c478bd9Sstevel@tonic-gate else
8147c478bd9Sstevel@tonic-gate err = YPERR_YPBIND;
8157c478bd9Sstevel@tonic-gate clnt_destroy(tb);
81661961e0fSrobinson tb = NULL;
817*17074b3aSGirish Moodalbail } while (hardlookup);
818*17074b3aSGirish Moodalbail
81961961e0fSrobinson if (tb != NULL)
8207c478bd9Sstevel@tonic-gate clnt_destroy(tb);
82161961e0fSrobinson (void) mutex_unlock(&bound_domains_lock);
8227c478bd9Sstevel@tonic-gate if (err)
8237c478bd9Sstevel@tonic-gate return (err);
8247c478bd9Sstevel@tonic-gate return (YPERR_DOMAIN);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate int
__yp_dobind_rsvdport(char * domain,struct dom_binding ** binding)8287c478bd9Sstevel@tonic-gate __yp_dobind_rsvdport(
8297c478bd9Sstevel@tonic-gate char *domain,
8307c478bd9Sstevel@tonic-gate struct dom_binding **binding) /* if result==0, ptr to dom_binding */
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate /* traditional __yp_dobind_rsvdport loops forever so set hardlookup */
8337c478bd9Sstevel@tonic-gate return (__yp_dobind_rsvdport_cflookup(domain, binding, 1));
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate
8367c478bd9Sstevel@tonic-gate /*
8377c478bd9Sstevel@tonic-gate * This is a "wrapper" function for __yp_dobind for vanilla user-level
8387c478bd9Sstevel@tonic-gate * functions which neither know nor care about struct dom_bindings.
8397c478bd9Sstevel@tonic-gate */
8407c478bd9Sstevel@tonic-gate int
yp_bind(char * domain)84161961e0fSrobinson yp_bind(char *domain)
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate
8447c478bd9Sstevel@tonic-gate struct dom_binding *binding;
84561961e0fSrobinson int res;
8467c478bd9Sstevel@tonic-gate
84761961e0fSrobinson res = __yp_dobind(domain, &binding);
84861961e0fSrobinson if (res == 0)
8497c478bd9Sstevel@tonic-gate __yp_rel_binding(binding);
85061961e0fSrobinson return (res);
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate
8537c478bd9Sstevel@tonic-gate static char *
__default_domain(void)85461961e0fSrobinson __default_domain(void)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate char temp[256];
8577c478bd9Sstevel@tonic-gate
85861961e0fSrobinson (void) mutex_lock(&default_domain_lock);
8597c478bd9Sstevel@tonic-gate
8607c478bd9Sstevel@tonic-gate if (default_domain) {
86161961e0fSrobinson (void) mutex_unlock(&default_domain_lock);
8627c478bd9Sstevel@tonic-gate return (default_domain);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate if (getdomainname(temp, sizeof (temp)) < 0) {
86561961e0fSrobinson (void) mutex_unlock(&default_domain_lock);
8667c478bd9Sstevel@tonic-gate return (0);
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate if (strlen(temp) > 0) {
86961961e0fSrobinson default_domain = malloc((strlen(temp) + 1));
8707c478bd9Sstevel@tonic-gate if (default_domain == 0) {
87161961e0fSrobinson (void) mutex_unlock(&default_domain_lock);
8727c478bd9Sstevel@tonic-gate return (0);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate (void) strcpy(default_domain, temp);
87561961e0fSrobinson (void) mutex_unlock(&default_domain_lock);
8767c478bd9Sstevel@tonic-gate return (default_domain);
8777c478bd9Sstevel@tonic-gate }
87861961e0fSrobinson (void) mutex_unlock(&default_domain_lock);
8797c478bd9Sstevel@tonic-gate return (0);
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate /*
8837c478bd9Sstevel@tonic-gate * This is a wrapper for the system call getdomainname which returns a
8847c478bd9Sstevel@tonic-gate * ypclnt.h error code in the failure case. It also checks to see that
8857c478bd9Sstevel@tonic-gate * the domain name is non-null, knowing that the null string is going to
8867c478bd9Sstevel@tonic-gate * get rejected elsewhere in the yp client package.
8877c478bd9Sstevel@tonic-gate */
8887c478bd9Sstevel@tonic-gate int
yp_get_default_domain(char ** domain)88961961e0fSrobinson yp_get_default_domain(char **domain)
8907c478bd9Sstevel@tonic-gate {
89161961e0fSrobinson if ((*domain = __default_domain()) != 0)
8927c478bd9Sstevel@tonic-gate return (0);
8937c478bd9Sstevel@tonic-gate return (YPERR_YPERR);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate
8967c478bd9Sstevel@tonic-gate /*
8977c478bd9Sstevel@tonic-gate * ===> Nobody uses this, do they? Can we nuke it?
8987c478bd9Sstevel@tonic-gate */
8997c478bd9Sstevel@tonic-gate int
usingypmap(char ** ddn,char * map)90061961e0fSrobinson usingypmap(char **ddn, char *map)
9017c478bd9Sstevel@tonic-gate {
9027c478bd9Sstevel@tonic-gate char in, *outval = NULL;
9037c478bd9Sstevel@tonic-gate int outvallen, stat;
9047c478bd9Sstevel@tonic-gate char *domain;
9057c478bd9Sstevel@tonic-gate
90661961e0fSrobinson if ((domain = __default_domain()) == 0)
9077c478bd9Sstevel@tonic-gate return (FALSE);
9087c478bd9Sstevel@tonic-gate *ddn = domain;
9097c478bd9Sstevel@tonic-gate /* does the map exist ? */
9107c478bd9Sstevel@tonic-gate in = (char)0xff;
9117c478bd9Sstevel@tonic-gate stat = yp_match(domain, map, &in, 1, &outval, &outvallen);
9127c478bd9Sstevel@tonic-gate if (outval != NULL)
9137c478bd9Sstevel@tonic-gate free(outval);
9147c478bd9Sstevel@tonic-gate switch (stat) {
9157c478bd9Sstevel@tonic-gate
9167c478bd9Sstevel@tonic-gate case 0: /* it actually succeeded! */
9177c478bd9Sstevel@tonic-gate case YPERR_KEY: /* no such key in map */
9187c478bd9Sstevel@tonic-gate case YPERR_NOMORE:
9197c478bd9Sstevel@tonic-gate case YPERR_BUSY:
9207c478bd9Sstevel@tonic-gate return (TRUE);
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate return (FALSE);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate * Creates a quick connection on a connection oriented loopback
9277c478bd9Sstevel@tonic-gate * transport. Fails quickly without timeout. Only naming service
9287c478bd9Sstevel@tonic-gate * it goes to is straddr.so.
9297c478bd9Sstevel@tonic-gate */
9307c478bd9Sstevel@tonic-gate CLIENT *
__clnt_create_loopback(rpcprog_t prog,rpcvers_t vers,int * err)93161961e0fSrobinson __clnt_create_loopback(rpcprog_t prog, rpcvers_t vers, int *err)
9327c478bd9Sstevel@tonic-gate {
9337c478bd9Sstevel@tonic-gate struct netconfig *nconf;
9347c478bd9Sstevel@tonic-gate CLIENT *clnt = NULL;
9357c478bd9Sstevel@tonic-gate void *nc_handle; /* Net config handle */
9367c478bd9Sstevel@tonic-gate
9377c478bd9Sstevel@tonic-gate *err = 0;
9387c478bd9Sstevel@tonic-gate nc_handle = setnetconfig();
93961961e0fSrobinson if (nc_handle == NULL) {
9407c478bd9Sstevel@tonic-gate /* fails to open netconfig file */
9417c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_FAILED;
9427c478bd9Sstevel@tonic-gate *err = YPERR_RPC;
94361961e0fSrobinson return (NULL);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate while (nconf = getnetconfig(nc_handle))
9467c478bd9Sstevel@tonic-gate /* Try only one connection oriented loopback transport */
9477c478bd9Sstevel@tonic-gate if ((strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) &&
9487c478bd9Sstevel@tonic-gate ((nconf->nc_semantics == NC_TPI_COTS) ||
9497c478bd9Sstevel@tonic-gate (nconf->nc_semantics == NC_TPI_COTS_ORD))) {
9507c478bd9Sstevel@tonic-gate clnt = getclnt(prog, vers, nconf, err);
9517c478bd9Sstevel@tonic-gate break;
9527c478bd9Sstevel@tonic-gate }
95361961e0fSrobinson (void) endnetconfig(nc_handle);
9547c478bd9Sstevel@tonic-gate
95561961e0fSrobinson if (clnt == NULL) { /* no loopback transport available */
9567c478bd9Sstevel@tonic-gate if (rpc_createerr.cf_stat == 0)
9577c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
9587c478bd9Sstevel@tonic-gate if (*err == 0) *err = YPERR_RPC;
9597c478bd9Sstevel@tonic-gate }
9607c478bd9Sstevel@tonic-gate return (clnt);
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate
9637c478bd9Sstevel@tonic-gate static CLIENT *
getclnt(rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,int * err)96461961e0fSrobinson getclnt(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf, int *err)
9657c478bd9Sstevel@tonic-gate {
9667c478bd9Sstevel@tonic-gate int fd;
9677c478bd9Sstevel@tonic-gate struct netbuf *svcaddr; /* servers address */
9687c478bd9Sstevel@tonic-gate CLIENT *cl;
9697c478bd9Sstevel@tonic-gate struct nd_addrlist *nas;
9707c478bd9Sstevel@tonic-gate struct nd_hostserv rpcbind_hs;
9717c478bd9Sstevel@tonic-gate struct t_call sndcall;
9727c478bd9Sstevel@tonic-gate char uaddress[1024]; /* XXX maxlen ?? */
9737c478bd9Sstevel@tonic-gate RPCB parms;
9747c478bd9Sstevel@tonic-gate enum clnt_stat clnt_st;
9757c478bd9Sstevel@tonic-gate char *ua;
9767c478bd9Sstevel@tonic-gate struct timeval tv = { 30, 0 };
9777c478bd9Sstevel@tonic-gate
97861961e0fSrobinson if (nconf == NULL) {
9797c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_TLIERROR;
9807c478bd9Sstevel@tonic-gate *err = YPERR_RPC;
98161961e0fSrobinson return (NULL);
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate
9847c478bd9Sstevel@tonic-gate /*
9857c478bd9Sstevel@tonic-gate * The ypbind process might cache its transport address.
9867c478bd9Sstevel@tonic-gate * If we can get at it, then we will use it and avoid
9877c478bd9Sstevel@tonic-gate * wasting time talking to rpcbind.
9887c478bd9Sstevel@tonic-gate */
9897c478bd9Sstevel@tonic-gate
9907c478bd9Sstevel@tonic-gate if (get_cached_transport(nconf, vers, uaddress, sizeof (uaddress))) {
9917c478bd9Sstevel@tonic-gate goto create_client;
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate
9947c478bd9Sstevel@tonic-gate /*
9957c478bd9Sstevel@tonic-gate * Check to see if local rpcbind is up or not. If it
9967c478bd9Sstevel@tonic-gate * isn't, it is best that the application should realize
9977c478bd9Sstevel@tonic-gate * yp is not up and take a remedial action. This is to
9987c478bd9Sstevel@tonic-gate * avoid the minute long timeout incurred by rpcbind_getaddr.
9997c478bd9Sstevel@tonic-gate * Looks like the only way to accomplish this it is to unfold
10007c478bd9Sstevel@tonic-gate * rpcb_getaddr and make a few changes. Alas !
10017c478bd9Sstevel@tonic-gate */
10027c478bd9Sstevel@tonic-gate rpcbind_hs.h_host = HOST_SELF_CONNECT;
10037c478bd9Sstevel@tonic-gate rpcbind_hs.h_serv = "rpcbind";
10047c478bd9Sstevel@tonic-gate if (netdir_getbyname(nconf, &rpcbind_hs, &nas) != ND_OK) {
10057c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
10067c478bd9Sstevel@tonic-gate *err = YPERR_RPC;
100761961e0fSrobinson return (NULL);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
10107c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_TLIERROR;
10117c478bd9Sstevel@tonic-gate *err = YPERR_RPC;
101261961e0fSrobinson return (NULL);
10137c478bd9Sstevel@tonic-gate }
101461961e0fSrobinson if (t_bind(fd, NULL, NULL) == -1) {
10157c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_TLIERROR;
10167c478bd9Sstevel@tonic-gate *err = YPERR_RPC;
10177c478bd9Sstevel@tonic-gate (void) t_close(fd);
101861961e0fSrobinson return (NULL);
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate sndcall.addr = *(nas->n_addrs);
10217c478bd9Sstevel@tonic-gate sndcall.opt.len = 0;
10227c478bd9Sstevel@tonic-gate sndcall.udata.len = 0;
10237c478bd9Sstevel@tonic-gate if (t_connect(fd, &sndcall, NULL) == -1) {
10247c478bd9Sstevel@tonic-gate netdir_free((char *)nas, ND_ADDRLIST);
10257c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_TLIERROR;
10267c478bd9Sstevel@tonic-gate (void) t_close(fd);
10277c478bd9Sstevel@tonic-gate *err = YPERR_PMAP;
102861961e0fSrobinson return (NULL);
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate
10317c478bd9Sstevel@tonic-gate /*
10327c478bd9Sstevel@tonic-gate * Get the address of the server
10337c478bd9Sstevel@tonic-gate */
10347c478bd9Sstevel@tonic-gate cl = clnt_tli_create(fd, nconf, nas->n_addrs,
10357c478bd9Sstevel@tonic-gate RPCBPROG, RPCBVERS, __ypipbufsize, __ypipbufsize);
10367c478bd9Sstevel@tonic-gate netdir_free((char *)nas, ND_ADDRLIST);
103761961e0fSrobinson if (cl == NULL) {
10387c478bd9Sstevel@tonic-gate (void) t_close(fd);
10397c478bd9Sstevel@tonic-gate *err = YPERR_PMAP;
104061961e0fSrobinson return (NULL);
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate parms.r_prog = prog;
10437c478bd9Sstevel@tonic-gate parms.r_vers = vers;
10447c478bd9Sstevel@tonic-gate parms.r_netid = nconf->nc_netid;
10457c478bd9Sstevel@tonic-gate parms.r_addr = nullstring;
10467c478bd9Sstevel@tonic-gate parms.r_owner = nullstring;
10477c478bd9Sstevel@tonic-gate ua = uaddress;
10487c478bd9Sstevel@tonic-gate clnt_st = CLNT_CALL(cl, RPCBPROC_GETADDR, xdr_rpcb, (char *)&parms,
10497c478bd9Sstevel@tonic-gate xdr_wrapstring, (char *)&ua, tv);
10507c478bd9Sstevel@tonic-gate (void) t_close(fd);
10517c478bd9Sstevel@tonic-gate clnt_destroy(cl);
10527c478bd9Sstevel@tonic-gate if (clnt_st != RPC_SUCCESS) {
10537c478bd9Sstevel@tonic-gate *err = YPERR_YPBIND;
105461961e0fSrobinson return (NULL);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate if (strlen(uaddress) == 0) {
10577c478bd9Sstevel@tonic-gate *err = YPERR_YPBIND;
10587c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
105961961e0fSrobinson return (NULL);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate create_client:
10637c478bd9Sstevel@tonic-gate svcaddr = uaddr2taddr(nconf, uaddress);
10647c478bd9Sstevel@tonic-gate cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr, prog, vers,
10657c478bd9Sstevel@tonic-gate __ypipbufsize, __ypipbufsize);
10667c478bd9Sstevel@tonic-gate netdir_free((char *)svcaddr, ND_ADDR);
106761961e0fSrobinson if (cl == NULL) {
10687c478bd9Sstevel@tonic-gate *err = YPERR_YPBIND;
106961961e0fSrobinson return (NULL);
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate /*
10727c478bd9Sstevel@tonic-gate * The fd should be closed while destroying the handle.
10737c478bd9Sstevel@tonic-gate */
10747c478bd9Sstevel@tonic-gate return (cl);
10757c478bd9Sstevel@tonic-gate }
10767c478bd9Sstevel@tonic-gate
107761961e0fSrobinson static int
get_cached_transport(struct netconfig * nconf,int vers,char * uaddress,int ulen)107861961e0fSrobinson get_cached_transport(struct netconfig *nconf, int vers, char *uaddress,
107961961e0fSrobinson int ulen)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate ssize_t st;
10827c478bd9Sstevel@tonic-gate int fd;
10837c478bd9Sstevel@tonic-gate
108461961e0fSrobinson (void) snprintf(uaddress, ulen,
108561961e0fSrobinson "%s/xprt.%s.%d", BINDING, nconf->nc_netid, vers);
10867c478bd9Sstevel@tonic-gate fd = open(uaddress, O_RDONLY);
10877c478bd9Sstevel@tonic-gate if (fd == -1)
10887c478bd9Sstevel@tonic-gate return (0);
10897c478bd9Sstevel@tonic-gate
10907c478bd9Sstevel@tonic-gate /* if first byte is not locked, then ypbind must not be running */
10917c478bd9Sstevel@tonic-gate st = lockf(fd, F_TEST, 1);
10927c478bd9Sstevel@tonic-gate if (st != -1 || (errno != EAGAIN && errno != EACCES)) {
10937c478bd9Sstevel@tonic-gate (void) close(fd);
10947c478bd9Sstevel@tonic-gate return (0);
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate
10977c478bd9Sstevel@tonic-gate st = read(fd, uaddress, ulen);
10987c478bd9Sstevel@tonic-gate if (st == -1) {
10997c478bd9Sstevel@tonic-gate (void) close(fd);
11007c478bd9Sstevel@tonic-gate return (0);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate (void) close(fd);
11047c478bd9Sstevel@tonic-gate return (1);
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate
110761961e0fSrobinson static ypbind_resp *
get_cached_domain(char * domain)110861961e0fSrobinson get_cached_domain(char *domain)
11097c478bd9Sstevel@tonic-gate {
1110004388ebScasper FILE *fp;
11117c478bd9Sstevel@tonic-gate int st;
11127c478bd9Sstevel@tonic-gate char filename[300];
11137c478bd9Sstevel@tonic-gate static ypbind_resp res;
11147c478bd9Sstevel@tonic-gate XDR xdrs;
11157c478bd9Sstevel@tonic-gate
111661961e0fSrobinson (void) snprintf(filename, sizeof (filename),
111761961e0fSrobinson "%s/%s/cache_binding", BINDING, domain);
1118004388ebScasper fp = fopen(filename, "rF");
11197c478bd9Sstevel@tonic-gate if (fp == 0)
11207c478bd9Sstevel@tonic-gate return (0);
11217c478bd9Sstevel@tonic-gate
11227c478bd9Sstevel@tonic-gate /* if first byte is not locked, then ypbind must not be running */
1123004388ebScasper st = lockf(fileno(fp), F_TEST, 1);
11247c478bd9Sstevel@tonic-gate if (st != -1 || (errno != EAGAIN && errno != EACCES)) {
1125004388ebScasper (void) fclose(fp);
11267c478bd9Sstevel@tonic-gate return (0);
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate
1129004388ebScasper xdrstdio_create(&xdrs, fp, XDR_DECODE);
11307c478bd9Sstevel@tonic-gate
11317c478bd9Sstevel@tonic-gate (void) memset((char *)&res, 0, sizeof (res));
11327c478bd9Sstevel@tonic-gate st = xdr_ypbind_resp(&xdrs, &res);
11337c478bd9Sstevel@tonic-gate
11347c478bd9Sstevel@tonic-gate xdr_destroy(&xdrs);
1135004388ebScasper (void) fclose(fp);
11367c478bd9Sstevel@tonic-gate
11377c478bd9Sstevel@tonic-gate if (st)
11387c478bd9Sstevel@tonic-gate return (&res);
11397c478bd9Sstevel@tonic-gate
11407c478bd9Sstevel@tonic-gate return (0);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate
114361961e0fSrobinson static int
ypbind_running(int err,int status)114461961e0fSrobinson ypbind_running(int err, int status)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate char filename[300];
11477c478bd9Sstevel@tonic-gate int st;
11487c478bd9Sstevel@tonic-gate int fd;
11497c478bd9Sstevel@tonic-gate
115061961e0fSrobinson (void) snprintf(filename, sizeof (filename), "%s/ypbind.pid", BINDING);
11517c478bd9Sstevel@tonic-gate fd = open(filename, O_RDONLY);
11527c478bd9Sstevel@tonic-gate if (fd == -1) {
11537c478bd9Sstevel@tonic-gate if ((err == YPERR_YPBIND) && (status != RPC_PROGNOTREGISTERED))
11547c478bd9Sstevel@tonic-gate return (1);
11557c478bd9Sstevel@tonic-gate return (0);
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate
11587c478bd9Sstevel@tonic-gate /* if first byte is not locked, then ypbind must not be running */
11597c478bd9Sstevel@tonic-gate st = lockf(fd, F_TEST, 1);
11607c478bd9Sstevel@tonic-gate if (st != -1 || (errno != EAGAIN && errno != EACCES)) {
11617c478bd9Sstevel@tonic-gate (void) close(fd);
11627c478bd9Sstevel@tonic-gate return (0);
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate
11657c478bd9Sstevel@tonic-gate (void) close(fd);
11667c478bd9Sstevel@tonic-gate return (1);
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate
116961961e0fSrobinson static void
set_rdev(struct dom_binding * pdomb)117061961e0fSrobinson set_rdev(struct dom_binding *pdomb)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate int fd;
11737c478bd9Sstevel@tonic-gate struct stat stbuf;
11747c478bd9Sstevel@tonic-gate
11757c478bd9Sstevel@tonic-gate if (clnt_control(pdomb->dom_client, CLGET_FD, (char *)&fd) != TRUE ||
1176e8031f0aSraf fstat(fd, &stbuf) == -1) {
11777c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "ypbind client: can't get rdev");
11787c478bd9Sstevel@tonic-gate pdomb->fd = -1;
11797c478bd9Sstevel@tonic-gate return;
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate pdomb->fd = fd;
11827c478bd9Sstevel@tonic-gate pdomb->rdev = stbuf.st_rdev;
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate
118561961e0fSrobinson static int
check_rdev(struct dom_binding * pdomb)118661961e0fSrobinson check_rdev(struct dom_binding *pdomb)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate struct stat stbuf;
11897c478bd9Sstevel@tonic-gate
11907c478bd9Sstevel@tonic-gate if (pdomb->fd == -1)
11917c478bd9Sstevel@tonic-gate return (1); /* can't check it, assume it is okay */
11927c478bd9Sstevel@tonic-gate
1193e8031f0aSraf if (fstat(pdomb->fd, &stbuf) == -1) {
11947c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "yp_bind client: can't stat %d", pdomb->fd);
11957c478bd9Sstevel@tonic-gate /* could be because file descriptor was closed */
11967c478bd9Sstevel@tonic-gate /* it's not our file descriptor, so don't try to close it */
119761961e0fSrobinson clnt_control(pdomb->dom_client, CLSET_FD_NCLOSE, NULL);
11987c478bd9Sstevel@tonic-gate return (0);
11997c478bd9Sstevel@tonic-gate }
12007c478bd9Sstevel@tonic-gate if (pdomb->rdev != stbuf.st_rdev) {
12017c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG,
12027c478bd9Sstevel@tonic-gate "yp_bind client: fd %d changed, old=0x%x, new=0x%x",
12037c478bd9Sstevel@tonic-gate pdomb->fd, pdomb->rdev, stbuf.st_rdev);
12047c478bd9Sstevel@tonic-gate /* it's not our file descriptor, so don't try to close it */
120561961e0fSrobinson clnt_control(pdomb->dom_client, CLSET_FD_NCLOSE, NULL);
12067c478bd9Sstevel@tonic-gate return (0);
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate return (1); /* fd is okay */
12097c478bd9Sstevel@tonic-gate }
1210