xref: /titanic_51/usr/src/cmd/rexd/mount_nfs.c (revision 16ab6e0b56ccd36f9a870ff0b87e64c55599a6f0)
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
217c478bd9Sstevel@tonic-gate  *
22*16ab6e0bSsm26363  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
267c478bd9Sstevel@tonic-gate /* All Rights Reserved */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  *  mount_nfs.c - procedural interface to the NFS mount operation
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #define	NFSCLIENT
357c478bd9Sstevel@tonic-gate #include	<sys/types.h>
367c478bd9Sstevel@tonic-gate #include	<memory.h>
377c478bd9Sstevel@tonic-gate #include	<netconfig.h>
387c478bd9Sstevel@tonic-gate #include	<netdb.h>
397c478bd9Sstevel@tonic-gate #include	<netdir.h>
407c478bd9Sstevel@tonic-gate #include	<netinet/in.h>
417c478bd9Sstevel@tonic-gate #include	<stdarg.h>
427c478bd9Sstevel@tonic-gate #include	<stdio.h>
437c478bd9Sstevel@tonic-gate #include	<stdlib.h>
447c478bd9Sstevel@tonic-gate #include	<string.h>
457c478bd9Sstevel@tonic-gate #include	<unistd.h>
467c478bd9Sstevel@tonic-gate #include	<syslog.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include	<rpc/rpc.h>
497c478bd9Sstevel@tonic-gate #include	<rpc/clnt_soc.h>
507c478bd9Sstevel@tonic-gate #include	<rpc/pmap_prot.h>
517c478bd9Sstevel@tonic-gate #include	<nfs/nfs.h>
527c478bd9Sstevel@tonic-gate #include	<nfs/mount.h>
537c478bd9Sstevel@tonic-gate #include	<rpcsvc/mount.h>
547c478bd9Sstevel@tonic-gate #include	<errno.h>
557c478bd9Sstevel@tonic-gate #include	<sys/mntent.h>
567c478bd9Sstevel@tonic-gate #include	<sys/mnttab.h>
577c478bd9Sstevel@tonic-gate #include	<sys/mount.h>
587c478bd9Sstevel@tonic-gate #include	<sys/param.h>
597c478bd9Sstevel@tonic-gate #include	<sys/socket.h>
607c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
617c478bd9Sstevel@tonic-gate 
62*16ab6e0bSsm26363 static struct knetconfig *get_knconf(struct netconfig *nconf);
63*16ab6e0bSsm26363 static int bindudp_resvport(CLIENT *client);
64*16ab6e0bSsm26363 static void free_knconf(struct knetconfig *k);
65*16ab6e0bSsm26363 static void freemnttab(struct mnttab *mnt);
66*16ab6e0bSsm26363 static enum clnt_stat pingnfs(char *hostname, rpcvers_t *versp);
67*16ab6e0bSsm26363 static void netbuf_free(struct netbuf *nb);
68*16ab6e0bSsm26363 static struct netbuf *get_addr(char *hostname, int prog, int vers,
69*16ab6e0bSsm26363     struct netconfig **nconfp);
70*16ab6e0bSsm26363 
717c478bd9Sstevel@tonic-gate #define	TIME_MAX	16
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate extern	int	Debug;
747c478bd9Sstevel@tonic-gate extern  time_t	time_now;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate extern	FILE	*setmntent(char *, char *);
777c478bd9Sstevel@tonic-gate extern	void	errprintf(char *, char *, ...);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate FILE		*setmntent(char *, char *);
807c478bd9Sstevel@tonic-gate void		endmntent(FILE *);
817c478bd9Sstevel@tonic-gate enum clnt_stat	pingnfs(char *, rpcvers_t *);
827c478bd9Sstevel@tonic-gate struct netbuf	*get_addr(char *, int, int, struct netconfig **);
837c478bd9Sstevel@tonic-gate struct knetconfig *get_knconf(struct netconfig *);
847c478bd9Sstevel@tonic-gate void		netbuf_free(struct netbuf *);
857c478bd9Sstevel@tonic-gate void		free_knconf(struct knetconfig *);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * mount_nfs - mount a file system using NFS
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  * Returns: 0 if OK, 1 if error.
917c478bd9Sstevel@tonic-gate  * 	The "error" string returns the error message.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate int
947c478bd9Sstevel@tonic-gate mount_nfs(fsname, dir, error)
957c478bd9Sstevel@tonic-gate 	char *fsname;
967c478bd9Sstevel@tonic-gate 	char *dir;
977c478bd9Sstevel@tonic-gate 	char *error;
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin;
1007c478bd9Sstevel@tonic-gate 	struct hostent *hp;
1017c478bd9Sstevel@tonic-gate 	struct fhstatus fhs;
1027c478bd9Sstevel@tonic-gate 	char host[256];
1037c478bd9Sstevel@tonic-gate 	char *path;
1047c478bd9Sstevel@tonic-gate 	char opts[32];
1057c478bd9Sstevel@tonic-gate 	struct stat st;
1067c478bd9Sstevel@tonic-gate 	int s = -1;
1077c478bd9Sstevel@tonic-gate 	struct timeval timeout;
1087c478bd9Sstevel@tonic-gate 	CLIENT *client;
1097c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
1107c478bd9Sstevel@tonic-gate 	int printed1 = 0;
1117c478bd9Sstevel@tonic-gate 	int printed2 = 0;
1127c478bd9Sstevel@tonic-gate 	unsigned winks = 1;	/* seconds of sleep time */
1137c478bd9Sstevel@tonic-gate 	struct mnttab mnt;
1147c478bd9Sstevel@tonic-gate 	FILE *mnted;
1157c478bd9Sstevel@tonic-gate 	int flags;
1167c478bd9Sstevel@tonic-gate 	struct nfs_args args;
1177c478bd9Sstevel@tonic-gate 	struct netconfig *nconf, *udpnconf;
1187c478bd9Sstevel@tonic-gate 	char tbuf[TIME_MAX];
1197c478bd9Sstevel@tonic-gate 	rpcvers_t vers;
1207c478bd9Sstevel@tonic-gate 	rpcvers_t nfsvers;
1217c478bd9Sstevel@tonic-gate 	char *fstype;
1227c478bd9Sstevel@tonic-gate 	struct mountres3 res3;
1237c478bd9Sstevel@tonic-gate 	nfs_fh3 fh3;
1247c478bd9Sstevel@tonic-gate 	int *auths;
1257c478bd9Sstevel@tonic-gate 	int count;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (Debug)
1287c478bd9Sstevel@tonic-gate 		printf("mount_nfs request: mount %s\tdir %s\n", fsname, dir);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (Debug && errno)
1317c478bd9Sstevel@tonic-gate 		printf("ERRNO set on mount_nfs entry: %d\n", errno);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	path = strchr(fsname, ':');
1347c478bd9Sstevel@tonic-gate 	if (path == NULL) {
1357c478bd9Sstevel@tonic-gate 		errprintf(error, "No host name in %s\n", fsname);
1367c478bd9Sstevel@tonic-gate 		return (1);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	*path = '\0';
1397c478bd9Sstevel@tonic-gate 	strcpy(host, fsname);
1407c478bd9Sstevel@tonic-gate 	*path++ = ':';
1417c478bd9Sstevel@tonic-gate 	if (*path == '\0') {
1427c478bd9Sstevel@tonic-gate 		/*
1437c478bd9Sstevel@tonic-gate 		 * handle the special case of importing a root file system
1447c478bd9Sstevel@tonic-gate 		 */
1457c478bd9Sstevel@tonic-gate 		strcpy(path, "/");
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (Debug) {
1497c478bd9Sstevel@tonic-gate 		printf("mount_nfs:\tpath == %s\n", path);
1507c478bd9Sstevel@tonic-gate 		printf("\t\tdir == %s\n", dir);
1517c478bd9Sstevel@tonic-gate 		printf("\t\tgethostbyname host == %s\n", host);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * Get server's address
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	if ((hp = gethostbyname(host)) == NULL) {
1587c478bd9Sstevel@tonic-gate 		errprintf(error, "mount %s: %s not in hosts database\n",
1597c478bd9Sstevel@tonic-gate 			fsname, host);
1607c478bd9Sstevel@tonic-gate 		return (1);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (Debug && errno)
1647c478bd9Sstevel@tonic-gate 		printf("ERRNO set after gethostbyname: %d\n", errno);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (Debug) {
1677c478bd9Sstevel@tonic-gate 		fprintf(stderr, "gethostbyname:\n\th_name %s\n\t", hp->h_name);
1687c478bd9Sstevel@tonic-gate 		if (hp->h_aliases[0] && *hp->h_aliases[0])
1697c478bd9Sstevel@tonic-gate 			fprintf(stderr, "h_aliases %s\n\t", hp->h_aliases[0]);
1707c478bd9Sstevel@tonic-gate 		else
1717c478bd9Sstevel@tonic-gate 			fprintf(stderr, "h_aliases %s\n\t", "<none>");
1727c478bd9Sstevel@tonic-gate 		if (hp->h_addrtype == AF_INET)
1737c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1747c478bd9Sstevel@tonic-gate 				"h_addrtype AF_INET\n\th_adth_length %u\n\t",
1757c478bd9Sstevel@tonic-gate 				hp->h_length);
1767c478bd9Sstevel@tonic-gate 		else
1777c478bd9Sstevel@tonic-gate 			fprintf(stderr, "h_addrtype %u\n\th_adth_length %u\n\t",
1787c478bd9Sstevel@tonic-gate 				hp->h_addrtype, hp->h_length);
1797c478bd9Sstevel@tonic-gate 		if (hp->h_addr_list[0] && *hp->h_addr_list[0])
1807c478bd9Sstevel@tonic-gate 			fprintf(stderr, "h_addr_list <apparent list>\n");
1817c478bd9Sstevel@tonic-gate 		else
1827c478bd9Sstevel@tonic-gate 			fprintf(stderr, "h_addr_list %s\n", "<none>");
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (pingnfs(host, &nfsvers) != RPC_SUCCESS) {
1867c478bd9Sstevel@tonic-gate 		errprintf(error, "host %s not responding to ping\n", host);
1877c478bd9Sstevel@tonic-gate 		return (1);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (Debug)
1917c478bd9Sstevel@tonic-gate 		printf("pingnfs: succeeds.\n");
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	vers = nfsvers;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (Debug)
1967c478bd9Sstevel@tonic-gate 		printf("clnt_create for mountproc (%d)\n", errno);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	client = clnt_create_vers(host, MOUNTPROG, &vers, MOUNTVERS, vers,
1997c478bd9Sstevel@tonic-gate 				"udp");
2007c478bd9Sstevel@tonic-gate 	if (client == NULL) {
2017c478bd9Sstevel@tonic-gate 		errprintf(error, "%s %s\n", host,
2027c478bd9Sstevel@tonic-gate 			  clnt_spcreateerror("mount server not responding"));
2037c478bd9Sstevel@tonic-gate 		return (1);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (Debug)
2077c478bd9Sstevel@tonic-gate 		printf("call bindudp_resvport for mountproc (%d)\n", errno);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate  	if (bindudp_resvport(client) < 0) {
2107c478bd9Sstevel@tonic-gate 		errprintf(error, "mount %s:%s: %s\n", host, path,
2117c478bd9Sstevel@tonic-gate 			  "Couldn't bind to reserved port");
2127c478bd9Sstevel@tonic-gate 		if (Debug)
2137c478bd9Sstevel@tonic-gate 			printf("could not bind to reserved port\n");
2147c478bd9Sstevel@tonic-gate 		clnt_destroy(client);
2157c478bd9Sstevel@tonic-gate 		return (1);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (client->cl_auth)
2197c478bd9Sstevel@tonic-gate 		auth_destroy(client->cl_auth);
2207c478bd9Sstevel@tonic-gate 	if ((client->cl_auth = authsys_create_default()) == NULL) {
2217c478bd9Sstevel@tonic-gate 		errprintf(error, "mount %s:%s: %s\n", host, path,
2227c478bd9Sstevel@tonic-gate 			  "Couldn't create authsys structure");
2237c478bd9Sstevel@tonic-gate 		if (Debug)
2247c478bd9Sstevel@tonic-gate 			printf("could not create authsys structure\n");
2257c478bd9Sstevel@tonic-gate 		clnt_destroy(client);
2267c478bd9Sstevel@tonic-gate 		return (1);
2277c478bd9Sstevel@tonic-gate 	}
228*16ab6e0bSsm26363 /*
229*16ab6e0bSsm26363  * #ifdef	NOWAY
230*16ab6e0bSsm26363  *	if (Debug)
231*16ab6e0bSsm26363  *		printf("authsys_create_default called for mountproc\n");
232*16ab6e0bSsm26363  *	client->cl_auth = authsys_create_default();
233*16ab6e0bSsm26363  * #endif
234*16ab6e0bSsm26363  */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/* set mount args */
2377c478bd9Sstevel@tonic-gate 	memset(&args, 0, sizeof(args));
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/* Get fhandle of remote path from server's mountd */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	timeout.tv_usec = 0;
2427c478bd9Sstevel@tonic-gate 	timeout.tv_sec = 25;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	switch (vers) {
2457c478bd9Sstevel@tonic-gate 	case MOUNTVERS:
2467c478bd9Sstevel@tonic-gate 	case MOUNTVERS_POSIX:
2477c478bd9Sstevel@tonic-gate 		rpc_stat = clnt_call(client, MOUNTPROC_MNT,
2487c478bd9Sstevel@tonic-gate 				xdr_dirpath, (caddr_t)&path,
2497c478bd9Sstevel@tonic-gate 				xdr_fhstatus, (caddr_t)&fhs, timeout);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		if (rpc_stat != RPC_SUCCESS) {
2527c478bd9Sstevel@tonic-gate 			/* Given the way "clnt_sperror" works, the "%s" */
2537c478bd9Sstevel@tonic-gate 			/* following the "not responding" is correct. */
2547c478bd9Sstevel@tonic-gate 			errprintf(error, "mount server %s not responding %s\n",
2557c478bd9Sstevel@tonic-gate 				host, clnt_sperror(client, ""));
2567c478bd9Sstevel@tonic-gate 			clnt_destroy(client);
2577c478bd9Sstevel@tonic-gate 			return (1);
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		clnt_destroy(client);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		if ((errno = fhs.fhs_status) != MNT_OK) {
2637c478bd9Sstevel@tonic-gate 			if (errno == EACCES) {
2647c478bd9Sstevel@tonic-gate 				errprintf(error,
2657c478bd9Sstevel@tonic-gate 				  "rexd mount: not in EXPORT list for %s\n",
2667c478bd9Sstevel@tonic-gate 				  fsname);
2677c478bd9Sstevel@tonic-gate 			} else {
2687c478bd9Sstevel@tonic-gate 				errprintf(error, "rexd mount: error %d %s\n",
2697c478bd9Sstevel@tonic-gate 					errno, strerror(errno));
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 			return (1);
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		args.fh = (caddr_t)&fhs.fhstatus_u.fhs_fhandle;
2757c478bd9Sstevel@tonic-gate 		fstype = MNTTYPE_NFS;
2767c478bd9Sstevel@tonic-gate 		break;
2777c478bd9Sstevel@tonic-gate 	case MOUNTVERS3:
2787c478bd9Sstevel@tonic-gate 		memset((char *)&res3, '\0', sizeof (res3));
2797c478bd9Sstevel@tonic-gate 		rpc_stat = clnt_call(client, MOUNTPROC_MNT,
2807c478bd9Sstevel@tonic-gate 				xdr_dirpath, (char *)&path,
2817c478bd9Sstevel@tonic-gate 				xdr_mountres3, (char *)&res3, timeout);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		if (rpc_stat != RPC_SUCCESS) {
2847c478bd9Sstevel@tonic-gate 			/* Given the way "clnt_sperror" works, the "%s" */
2857c478bd9Sstevel@tonic-gate 			/* following the "not responding" is correct. */
2867c478bd9Sstevel@tonic-gate 			errprintf(error, "mount server %s not responding %s\n",
2877c478bd9Sstevel@tonic-gate 				host, clnt_sperror(client, ""));
2887c478bd9Sstevel@tonic-gate 			clnt_destroy(client);
2897c478bd9Sstevel@tonic-gate 			return (1);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		clnt_destroy(client);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		if ((errno = res3.fhs_status) != MNT_OK) {
2957c478bd9Sstevel@tonic-gate 			if (errno == EACCES) {
2967c478bd9Sstevel@tonic-gate 				errprintf(error,
2977c478bd9Sstevel@tonic-gate 				  "rexd mount: not in EXPORT list for %s\n",
2987c478bd9Sstevel@tonic-gate 				  fsname);
2997c478bd9Sstevel@tonic-gate 			} else {
3007c478bd9Sstevel@tonic-gate 				errprintf(error, "rexd mount: error %d %s\n",
3017c478bd9Sstevel@tonic-gate 					errno, strerror(errno));
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 			return (1);
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		auths =
3077c478bd9Sstevel@tonic-gate 		    res3.mountres3_u.mountinfo.auth_flavors.auth_flavors_val;
3087c478bd9Sstevel@tonic-gate 		count =
3097c478bd9Sstevel@tonic-gate 		    res3.mountres3_u.mountinfo.auth_flavors.auth_flavors_len;
3107c478bd9Sstevel@tonic-gate 		if (count > 0) {
3117c478bd9Sstevel@tonic-gate 			if (auths[0] == AUTH_DES)
3127c478bd9Sstevel@tonic-gate 				args.flags |= NFSMNT_SECURE;
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		fh3.fh3_length =
3167c478bd9Sstevel@tonic-gate 		    res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
3177c478bd9Sstevel@tonic-gate 		memcpy(fh3.fh3_u.data,
3187c478bd9Sstevel@tonic-gate 		    res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
3197c478bd9Sstevel@tonic-gate 		    fh3.fh3_length);
3207c478bd9Sstevel@tonic-gate 		args.fh = (caddr_t)&fh3;
3217c478bd9Sstevel@tonic-gate 		fstype = MNTTYPE_NFS3;
3227c478bd9Sstevel@tonic-gate 		break;
3237c478bd9Sstevel@tonic-gate 	default:
3247c478bd9Sstevel@tonic-gate 		errprintf(error, "rexd mount: unknown MOUNT version %ld\n",
3257c478bd9Sstevel@tonic-gate 			vers);
3267c478bd9Sstevel@tonic-gate 		return (1);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * remote mount the fhandle on the local path.
3327c478bd9Sstevel@tonic-gate 	 */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	args.hostname = host;
3357c478bd9Sstevel@tonic-gate 	args.flags = NFSMNT_HOSTNAME;
3367c478bd9Sstevel@tonic-gate 	args.flags |= NFSMNT_INT; /* default is "intr" */
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	args.addr = get_addr(host, NFS_PROGRAM, nfsvers, &nconf);
3397c478bd9Sstevel@tonic-gate 	if (args.addr == NULL) {
3407c478bd9Sstevel@tonic-gate 		errprintf(error, "%s: no NFS service", host);
3417c478bd9Sstevel@tonic-gate 		return (1);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	args.flags |=  NFSMNT_KNCONF;
3457c478bd9Sstevel@tonic-gate 	args.knconf = get_knconf(nconf);
3467c478bd9Sstevel@tonic-gate 	if (args.knconf == NULL) {
3477c478bd9Sstevel@tonic-gate 		netbuf_free(args.addr);
3487c478bd9Sstevel@tonic-gate 		return (1);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (Debug)
3527c478bd9Sstevel@tonic-gate 		printf("start mount system call (%d)\n", errno);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	flags = MS_NOSUID | MS_DATA;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* Provide the mounted resource name when mounting. */
3577c478bd9Sstevel@tonic-gate 	if (mount(fsname, dir, flags, fstype, &args, sizeof(args)) < 0) {
3587c478bd9Sstevel@tonic-gate 		netbuf_free(args.addr);
3597c478bd9Sstevel@tonic-gate 		free_knconf(args.knconf);
3607c478bd9Sstevel@tonic-gate 		errprintf(error, "unable to mount %s on %s: %s\n",
3617c478bd9Sstevel@tonic-gate 			fsname, dir, strerror(errno));
3627c478bd9Sstevel@tonic-gate 		return (1);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (Debug)
3667c478bd9Sstevel@tonic-gate 		printf("end mount system call (%d)\n", errno);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * stat the new mount and get its dev
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	if (stat(dir, &st) < 0) {
3727c478bd9Sstevel@tonic-gate 		errprintf(error, "couldn't stat %s\n", dir);
3737c478bd9Sstevel@tonic-gate 		return (1);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (Debug)
3777c478bd9Sstevel@tonic-gate 		printf("stat of new mount (%d)\n", errno);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	(void) sprintf(opts, "rw,noquota,hard,intr,dev=%x", st.st_dev);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * update /etc/mtab
3837c478bd9Sstevel@tonic-gate 	 */
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	mnt.mnt_special = fsname;
3867c478bd9Sstevel@tonic-gate 	mnt.mnt_mountp = dir;
3877c478bd9Sstevel@tonic-gate 	mnt.mnt_fstype = MNTTYPE_NFS;
3887c478bd9Sstevel@tonic-gate 	mnt.mnt_mntopts = opts;
3897c478bd9Sstevel@tonic-gate 	(void) sprintf(tbuf, "%ld", time(0L));
3907c478bd9Sstevel@tonic-gate 	mnt.mnt_time = tbuf;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	return (0);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate #define	UNMOUNTTRIES 6
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * umount_nfs - unmount a file system when finished
3987c478bd9Sstevel@tonic-gate  */
3997c478bd9Sstevel@tonic-gate int
4007c478bd9Sstevel@tonic-gate umount_nfs(fsname, dir)
4017c478bd9Sstevel@tonic-gate char *fsname, *dir;
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	char *p;
4047c478bd9Sstevel@tonic-gate 	char *hostname;
4057c478bd9Sstevel@tonic-gate 	int s = -1;
4067c478bd9Sstevel@tonic-gate 	struct timeval timeout;
4077c478bd9Sstevel@tonic-gate 	CLIENT *client;
4087c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
4097c478bd9Sstevel@tonic-gate 	int count = 0;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (Debug)
4127c478bd9Sstevel@tonic-gate 		printf("umount: fsname %s dir %s\n", fsname, dir);
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * Give the filesystem time to become un-busy when unmounting.
4157c478bd9Sstevel@tonic-gate 	 * If child aborted and is takes a core dump, we may receive the
4167c478bd9Sstevel@tonic-gate 	 * SIGCHLD before the core dump is completed.
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	while (umount(dir) == -1) {
4197c478bd9Sstevel@tonic-gate 		if (errno != EBUSY) {
4207c478bd9Sstevel@tonic-gate 			perror(dir);
4217c478bd9Sstevel@tonic-gate 			return (1);
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		if (++count > UNMOUNTTRIES)
4257c478bd9Sstevel@tonic-gate 			return (1);
4267c478bd9Sstevel@tonic-gate 		sleep(10);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (Debug)
4307c478bd9Sstevel@tonic-gate 		printf("umount_nfs: unmounting %s\n", dir);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if ((p = strchr(fsname, ':')) == NULL)
4337c478bd9Sstevel@tonic-gate 		return (1);
4347c478bd9Sstevel@tonic-gate 	*p++ = 0;
4357c478bd9Sstevel@tonic-gate 	hostname = fsname;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if ((client = clnt_create(hostname, MOUNTPROG, MOUNTVERS, "udp"))
4397c478bd9Sstevel@tonic-gate 	    == NULL) {
4407c478bd9Sstevel@tonic-gate 		clnt_spcreateerror("Warning on umount create:");
4417c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\n\r");
4427c478bd9Sstevel@tonic-gate 		return (1);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	if (bindudp_resvport(client) < 0) {
4457c478bd9Sstevel@tonic-gate 		errprintf(NULL, "umount %s:%s:%s", hostname, p,
4467c478bd9Sstevel@tonic-gate 			  "Could not bind to reserved port\n");
4477c478bd9Sstevel@tonic-gate 		clnt_destroy(client);
4487c478bd9Sstevel@tonic-gate 		return (1);
4497c478bd9Sstevel@tonic-gate 	}
450*16ab6e0bSsm26363 /*
451*16ab6e0bSsm26363  * #ifdef		NOWAWY
452*16ab6e0bSsm26363  * 	client->cl_auth = authunix_create_default();
453*16ab6e0bSsm26363  * #endif
454*16ab6e0bSsm26363  */
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	timeout.tv_usec = 0;
4577c478bd9Sstevel@tonic-gate 	timeout.tv_sec = 25;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(client, MOUNTPROC_UMNT, xdr_dirpath, (caddr_t) &p,
4607c478bd9Sstevel@tonic-gate 			     xdr_void, (char *)NULL, timeout);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	clnt_destroy(client);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
4657c478bd9Sstevel@tonic-gate 		clnt_perror(client, "Warning: umount:");
4667c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\n\r");
4677c478bd9Sstevel@tonic-gate 		return (1);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	return (0);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate static struct mnttab *
4777c478bd9Sstevel@tonic-gate dupmnttab(mnt)
4787c478bd9Sstevel@tonic-gate struct mnttab *mnt;
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	struct mnttab *new;
4817c478bd9Sstevel@tonic-gate 	void freemnttab();
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	new = (struct mnttab *)malloc(sizeof (*new));
4847c478bd9Sstevel@tonic-gate 	if (new == NULL)
4857c478bd9Sstevel@tonic-gate 		goto alloc_failed;
4867c478bd9Sstevel@tonic-gate 	memset((char *)new, 0, sizeof (*new));
4877c478bd9Sstevel@tonic-gate 	new->mnt_special = strdup(mnt->mnt_special);
4887c478bd9Sstevel@tonic-gate 	if (new->mnt_special == NULL)
4897c478bd9Sstevel@tonic-gate 		goto alloc_failed;
4907c478bd9Sstevel@tonic-gate 	new->mnt_mountp = strdup(mnt->mnt_mountp);
4917c478bd9Sstevel@tonic-gate 	if (new->mnt_mountp == NULL)
4927c478bd9Sstevel@tonic-gate 		goto alloc_failed;
4937c478bd9Sstevel@tonic-gate 	new->mnt_fstype = strdup(mnt->mnt_fstype);
4947c478bd9Sstevel@tonic-gate 	if (new->mnt_fstype == NULL)
4957c478bd9Sstevel@tonic-gate 		goto alloc_failed;
4967c478bd9Sstevel@tonic-gate 	if (mnt->mnt_mntopts != NULL)
4977c478bd9Sstevel@tonic-gate 		if ((new->mnt_mntopts = strdup(mnt->mnt_mntopts)) == NULL)
4987c478bd9Sstevel@tonic-gate 			goto alloc_failed;
4997c478bd9Sstevel@tonic-gate 	if (mnt->mnt_time != NULL)
5007c478bd9Sstevel@tonic-gate 		if ((new->mnt_time = strdup(mnt->mnt_time)) == NULL)
5017c478bd9Sstevel@tonic-gate 			goto alloc_failed;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	return (new);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate       alloc_failed:
5067c478bd9Sstevel@tonic-gate 	errprintf(NULL, "dupmnttab: memory allocation failed\n");
5077c478bd9Sstevel@tonic-gate 	freemnttab(new);
5087c478bd9Sstevel@tonic-gate 	return (NULL);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate  * Free a single mnttab structure
5157c478bd9Sstevel@tonic-gate  */
5167c478bd9Sstevel@tonic-gate static void
5177c478bd9Sstevel@tonic-gate freemnttab(mnt)
5187c478bd9Sstevel@tonic-gate struct mnttab *mnt;
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	if (mnt) {
5217c478bd9Sstevel@tonic-gate 		if (mnt->mnt_special)
5227c478bd9Sstevel@tonic-gate 			free(mnt->mnt_special);
5237c478bd9Sstevel@tonic-gate 		if (mnt->mnt_mountp)
5247c478bd9Sstevel@tonic-gate 			free(mnt->mnt_mountp);
5257c478bd9Sstevel@tonic-gate 		if (mnt->mnt_fstype)
5267c478bd9Sstevel@tonic-gate 			free(mnt->mnt_fstype);
5277c478bd9Sstevel@tonic-gate 		if (mnt->mnt_mntopts)
5287c478bd9Sstevel@tonic-gate 			free(mnt->mnt_mntopts);
5297c478bd9Sstevel@tonic-gate 		if (mnt->mnt_time)
5307c478bd9Sstevel@tonic-gate 			free(mnt->mnt_time);
5317c478bd9Sstevel@tonic-gate 		free(mnt);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate /* the following structure is used to build a list of */
5377c478bd9Sstevel@tonic-gate /* mnttab structures from /etc/mnttab. */
5387c478bd9Sstevel@tonic-gate struct mntlist {
5397c478bd9Sstevel@tonic-gate 	struct mnttab *mntl_mnt;
5407c478bd9Sstevel@tonic-gate 	struct mntlist *mntl_next;
5417c478bd9Sstevel@tonic-gate };
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  * Free a list of mnttab structures
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate static void
5487c478bd9Sstevel@tonic-gate freemntlist(mntl)
5497c478bd9Sstevel@tonic-gate struct mntlist *mntl;
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	struct mntlist *mntl_tmp;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	while (mntl) {
5547c478bd9Sstevel@tonic-gate 		freemnttab(mntl->mntl_mnt);
5557c478bd9Sstevel@tonic-gate 		mntl_tmp = mntl;
5567c478bd9Sstevel@tonic-gate 		mntl = mntl->mntl_next;
5577c478bd9Sstevel@tonic-gate 		free(mntl_tmp);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate  * parsefs - given a name of the form host:/path/name/for/file
5647c478bd9Sstevel@tonic-gate  *	connect to the give host and look for the exported file system
5657c478bd9Sstevel@tonic-gate  *	that matches.
5667c478bd9Sstevel@tonic-gate  * Returns: pointer to string containing the part of the pathname
5677c478bd9Sstevel@tonic-gate  *	within the exported directory.
5687c478bd9Sstevel@tonic-gate  *	Returns NULL on errors.
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate char *
5717c478bd9Sstevel@tonic-gate parsefs(fullname, error)
5727c478bd9Sstevel@tonic-gate char	*fullname;
5737c478bd9Sstevel@tonic-gate char	*error;
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate 	char	*dir,
5767c478bd9Sstevel@tonic-gate 	*subdir;
5777c478bd9Sstevel@tonic-gate 	struct exportnode	*ex = NULL;
5787c478bd9Sstevel@tonic-gate 	int	err;
5797c478bd9Sstevel@tonic-gate 	int	bestlen = 0;
5807c478bd9Sstevel@tonic-gate 	int	len,
5817c478bd9Sstevel@tonic-gate 	dirlen;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (Debug && errno)
5847c478bd9Sstevel@tonic-gate 		printf("parsefs of %s entered with errno %d %s\n",
5857c478bd9Sstevel@tonic-gate 			fullname, errno, strerror(errno));
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	dir = strchr(fullname, ':');
5887c478bd9Sstevel@tonic-gate 	if (dir == NULL) {
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		errprintf(error, "No host name in %s\n", fullname);
5917c478bd9Sstevel@tonic-gate 		return (NULL);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 	*dir++ = '\0';
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if (Debug)
5967c478bd9Sstevel@tonic-gate 		printf("parsefs before rpc_call: ERRNO:%d\n", errno);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if (err = rpc_call(fullname, MOUNTPROG, MOUNTVERS, MOUNTPROC_EXPORT,
5997c478bd9Sstevel@tonic-gate 			xdr_void, 0, xdr_exports, (char *)&ex, "udp")) {
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		if (err == (int)RPC_TIMEDOUT)
6027c478bd9Sstevel@tonic-gate 			errprintf(error, "Host %s is not running mountd\n",
6037c478bd9Sstevel@tonic-gate 				fullname);
6047c478bd9Sstevel@tonic-gate 		else
6057c478bd9Sstevel@tonic-gate 			errprintf(error, "RPC error %d with host %s (%s)\n",
6067c478bd9Sstevel@tonic-gate 				err, fullname, clnt_sperrno(err));
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		if (Debug && errno) {
6097c478bd9Sstevel@tonic-gate 			printf("parsefs: mount call to %s returned %d %s\n",
6107c478bd9Sstevel@tonic-gate 				fullname,
6117c478bd9Sstevel@tonic-gate 				err,
6127c478bd9Sstevel@tonic-gate 				clnt_sperrno(err));
6137c478bd9Sstevel@tonic-gate 			printf("with errno %d:\t%s\n",	errno, strerror(errno));
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 		return (NULL);
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (Debug)
6197c478bd9Sstevel@tonic-gate 		printf("parsefs after rpc_call: ERRNO:%d\n", errno);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	dirlen = strlen(dir);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (Debug && errno) {
6247c478bd9Sstevel@tonic-gate 		printf("parsefs: mount call to %s returned %d %s\n",
6257c478bd9Sstevel@tonic-gate 			fullname,
6267c478bd9Sstevel@tonic-gate 			err,
6277c478bd9Sstevel@tonic-gate 			clnt_sperrno(err));
6287c478bd9Sstevel@tonic-gate 		printf("with errno %d:\t%s\n", errno, strerror(errno));
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (Debug)
6327c478bd9Sstevel@tonic-gate 		printf("parsefs: checking export list:\n");
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	for (; ex; ex = ex->ex_next) {
6357c478bd9Sstevel@tonic-gate 		len = strlen(ex->ex_dir);
6367c478bd9Sstevel@tonic-gate 		if (len > bestlen && len <= dirlen &&
6377c478bd9Sstevel@tonic-gate 		    strncmp(dir, ex->ex_dir, len) == 0 &&
6387c478bd9Sstevel@tonic-gate 		    (dir[len] == '/' || dir[len] == '\0'))
6397c478bd9Sstevel@tonic-gate 			bestlen = len;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		if (Debug)
6427c478bd9Sstevel@tonic-gate 			printf("\t%d\t%s\n", bestlen, ex->ex_dir);
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if (bestlen == 0) {
6467c478bd9Sstevel@tonic-gate 		errprintf(error, "%s not exported by %s\n",
6477c478bd9Sstevel@tonic-gate 			dir, fullname);
6487c478bd9Sstevel@tonic-gate 		return (NULL);
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (dir[bestlen] == '\0')
6527c478bd9Sstevel@tonic-gate 		subdir = &dir[bestlen];
6537c478bd9Sstevel@tonic-gate 	else {
6547c478bd9Sstevel@tonic-gate 		dir[bestlen] = '\0';
6557c478bd9Sstevel@tonic-gate 		subdir = &dir[bestlen+1];
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	*--dir = ':';
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	return (subdir);
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate  * Get the network address for the service identified by "prog"
6657c478bd9Sstevel@tonic-gate  * and "vers" on "hostname".  The netconfig address is returned
6667c478bd9Sstevel@tonic-gate  * in the value of "nconfp".
6677c478bd9Sstevel@tonic-gate  * If the hostname is the same as the last call, then the same
6687c478bd9Sstevel@tonic-gate  * transport is used as last time (same netconfig entry).
6697c478bd9Sstevel@tonic-gate  */
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate static struct netbuf *
6737c478bd9Sstevel@tonic-gate get_addr(hostname, prog, vers, nconfp)
6747c478bd9Sstevel@tonic-gate char *hostname;
6757c478bd9Sstevel@tonic-gate int prog, vers;
6767c478bd9Sstevel@tonic-gate struct netconfig **nconfp;
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 	static char prevhost[MAXHOSTNAMELEN+1];
6797c478bd9Sstevel@tonic-gate 	static struct netconfig *nconf;
6807c478bd9Sstevel@tonic-gate 	static NCONF_HANDLE *nc = NULL;
6817c478bd9Sstevel@tonic-gate 	struct netbuf *nb = NULL;
6827c478bd9Sstevel@tonic-gate 	struct t_bind *tbind = NULL;
6837c478bd9Sstevel@tonic-gate 	struct netconfig *getnetconfig();
6847c478bd9Sstevel@tonic-gate 	struct netconfig *getnetconfigent();
6857c478bd9Sstevel@tonic-gate 	enum clnt_stat cs;
6867c478bd9Sstevel@tonic-gate 	struct timeval tv;
6877c478bd9Sstevel@tonic-gate 	int fd = -1;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if (strcmp(hostname, prevhost) != 0) {
6907c478bd9Sstevel@tonic-gate 		if (nc)
6917c478bd9Sstevel@tonic-gate 			endnetconfig(nc);
6927c478bd9Sstevel@tonic-gate 		nc = setnetconfig();
6937c478bd9Sstevel@tonic-gate 		if (nc == NULL)
6947c478bd9Sstevel@tonic-gate 			goto done;
6957c478bd9Sstevel@tonic-gate 	retry:
6967c478bd9Sstevel@tonic-gate 		/*
6977c478bd9Sstevel@tonic-gate 		 * If the port number is specified then UDP is needed.
6987c478bd9Sstevel@tonic-gate 		 * Otherwise any connectionless transport will do.
6997c478bd9Sstevel@tonic-gate 		 */
7007c478bd9Sstevel@tonic-gate 		while (nconf = getnetconfig(nc)) {
7017c478bd9Sstevel@tonic-gate 			if ((nconf->nc_flag & NC_VISIBLE) &&
7027c478bd9Sstevel@tonic-gate 			    nconf->nc_semantics == NC_TPI_CLTS) {
7037c478bd9Sstevel@tonic-gate 				break;
7047c478bd9Sstevel@tonic-gate 			}
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 		if (nconf == NULL)
7077c478bd9Sstevel@tonic-gate 			goto done;
7087c478bd9Sstevel@tonic-gate 		(void) strcpy(prevhost, hostname);
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	fd = t_open(nconf->nc_device, O_RDWR, NULL);
7127c478bd9Sstevel@tonic-gate 	if (fd < 0)
7137c478bd9Sstevel@tonic-gate 		goto done;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	tbind = (struct t_bind *) t_alloc(fd, T_BIND, T_ADDR);
7167c478bd9Sstevel@tonic-gate 	if (tbind == NULL)
7177c478bd9Sstevel@tonic-gate 		goto done;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if (rpcb_getaddr(prog, vers, nconf, &tbind->addr, hostname) == 0) {
7207c478bd9Sstevel@tonic-gate 		t_free((char *) tbind, T_BIND);
7217c478bd9Sstevel@tonic-gate 		tbind = NULL;
7227c478bd9Sstevel@tonic-gate 		goto retry;
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 	*nconfp = nconf;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	/*
7277c478bd9Sstevel@tonic-gate 	 * Make a copy of the netbuf to return
7287c478bd9Sstevel@tonic-gate 	 */
7297c478bd9Sstevel@tonic-gate 	nb = (struct netbuf *) malloc(sizeof (struct netbuf));
7307c478bd9Sstevel@tonic-gate 	if (nb == NULL) {
7317c478bd9Sstevel@tonic-gate 		errprintf(NULL, "no memory");
7327c478bd9Sstevel@tonic-gate 		goto done;
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	*nb = tbind->addr;
7357c478bd9Sstevel@tonic-gate 	nb->buf = (char *)malloc(nb->len);
7367c478bd9Sstevel@tonic-gate 	if (nb->buf == NULL) {
7377c478bd9Sstevel@tonic-gate 		errprintf(NULL, "no memory");
7387c478bd9Sstevel@tonic-gate 		free(nb);
7397c478bd9Sstevel@tonic-gate 		nb = NULL;
7407c478bd9Sstevel@tonic-gate 		goto done;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate done:
7457c478bd9Sstevel@tonic-gate 	if (tbind)
7467c478bd9Sstevel@tonic-gate 		t_free((char *) tbind, T_BIND);
7477c478bd9Sstevel@tonic-gate 	if (fd >= 0)
7487c478bd9Sstevel@tonic-gate 		(void) t_close(fd);
7497c478bd9Sstevel@tonic-gate 	return (nb);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate static struct knetconfig *
7537c478bd9Sstevel@tonic-gate get_knconf(nconf)
7547c478bd9Sstevel@tonic-gate struct netconfig *nconf;
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	struct stat stbuf;
7577c478bd9Sstevel@tonic-gate 	struct knetconfig *k;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (stat(nconf->nc_device, &stbuf) < 0) {
7607c478bd9Sstevel@tonic-gate 		errprintf(NULL, "get_knconf: stat %s: %m", nconf->nc_device);
7617c478bd9Sstevel@tonic-gate 		return (NULL);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 	k = (struct knetconfig *) malloc(sizeof (*k));
7647c478bd9Sstevel@tonic-gate 	if (k == NULL)
7657c478bd9Sstevel@tonic-gate 		goto nomem;
7667c478bd9Sstevel@tonic-gate 	k->knc_semantics = nconf->nc_semantics;
7677c478bd9Sstevel@tonic-gate 	k->knc_protofmly = strdup(nconf->nc_protofmly);
7687c478bd9Sstevel@tonic-gate 	if (k->knc_protofmly == NULL)
7697c478bd9Sstevel@tonic-gate 		goto nomem;
7707c478bd9Sstevel@tonic-gate 	k->knc_proto = strdup(nconf->nc_proto);
7717c478bd9Sstevel@tonic-gate 	if (k->knc_proto == NULL)
7727c478bd9Sstevel@tonic-gate 		goto nomem;
7737c478bd9Sstevel@tonic-gate 	k->knc_rdev = stbuf.st_rdev;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	return (k);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate nomem:
7787c478bd9Sstevel@tonic-gate 	errprintf(NULL, "get_knconf: no memory");
7797c478bd9Sstevel@tonic-gate 	free_knconf(k);
7807c478bd9Sstevel@tonic-gate 	return (NULL);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate static void
7847c478bd9Sstevel@tonic-gate free_knconf(k)
7857c478bd9Sstevel@tonic-gate struct knetconfig *k;
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	if (k == NULL)
7887c478bd9Sstevel@tonic-gate 		return;
7897c478bd9Sstevel@tonic-gate 	if (k->knc_protofmly)
7907c478bd9Sstevel@tonic-gate 		free(k->knc_protofmly);
7917c478bd9Sstevel@tonic-gate 	if (k->knc_proto)
7927c478bd9Sstevel@tonic-gate 		free(k->knc_proto);
7937c478bd9Sstevel@tonic-gate 	free(k);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate static void
7977c478bd9Sstevel@tonic-gate netbuf_free(nb)
7987c478bd9Sstevel@tonic-gate struct netbuf *nb;
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	if (nb == NULL)
8017c478bd9Sstevel@tonic-gate 		return;
8027c478bd9Sstevel@tonic-gate 	if (nb->buf)
8037c478bd9Sstevel@tonic-gate 		free(nb->buf);
8047c478bd9Sstevel@tonic-gate 	free(nb);
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate static enum clnt_stat
8097c478bd9Sstevel@tonic-gate pingnfs(hostname, versp)
8107c478bd9Sstevel@tonic-gate char *hostname;
8117c478bd9Sstevel@tonic-gate rpcvers_t *versp;
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	CLIENT *cl;
8147c478bd9Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
8157c478bd9Sstevel@tonic-gate 	static char goodhost[MAXHOSTNAMELEN+1];
8167c478bd9Sstevel@tonic-gate 	static char deadhost[MAXHOSTNAMELEN+1];
8177c478bd9Sstevel@tonic-gate 	static time_t goodtime, deadtime;
8187c478bd9Sstevel@tonic-gate 	int cache_time = 60;	/* sec */
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (goodtime > time_now && strcmp(hostname, goodhost) == 0)
8217c478bd9Sstevel@tonic-gate 		return (RPC_SUCCESS);
8227c478bd9Sstevel@tonic-gate 	if (deadtime > time_now && strcmp(hostname, deadhost) == 0)
8237c478bd9Sstevel@tonic-gate 		return (RPC_TIMEDOUT);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if (Debug)
8267c478bd9Sstevel@tonic-gate 		printf("ping %s ", hostname);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/* ping the NFS nullproc on the server */
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	cl = clnt_create_vers(hostname, NFS_PROGRAM, versp, NFS_VERSMIN,
8317c478bd9Sstevel@tonic-gate 			NFS_VERSMAX, "udp");
8327c478bd9Sstevel@tonic-gate 	if (cl == NULL) {
8337c478bd9Sstevel@tonic-gate 		errprintf(NULL, "pingnfs: %s%s",
8347c478bd9Sstevel@tonic-gate 			  hostname, clnt_spcreateerror(""));
8357c478bd9Sstevel@tonic-gate 		if (Debug)
8367c478bd9Sstevel@tonic-gate 			printf("clnt_create failed\n");
8377c478bd9Sstevel@tonic-gate 		clnt_stat = RPC_TIMEDOUT;
8387c478bd9Sstevel@tonic-gate 	} else {
8397c478bd9Sstevel@tonic-gate 		clnt_stat = RPC_SUCCESS;
8407c478bd9Sstevel@tonic-gate 		clnt_destroy(cl);
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (clnt_stat == RPC_SUCCESS) {
8447c478bd9Sstevel@tonic-gate 		(void) strcpy(goodhost, hostname);
8457c478bd9Sstevel@tonic-gate 		goodtime = time_now + cache_time;
8467c478bd9Sstevel@tonic-gate 	} else {
8477c478bd9Sstevel@tonic-gate 		(void) strcpy(deadhost, hostname);
8487c478bd9Sstevel@tonic-gate 		deadtime = time_now + cache_time;
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	if (Debug)
8527c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", clnt_stat == RPC_SUCCESS ?
8537c478bd9Sstevel@tonic-gate 				"OK" : "NO RESPONSE");
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	return (clnt_stat);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate static int bindudp_resvport(client)
8597c478bd9Sstevel@tonic-gate CLIENT *client;
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate 	struct netconfig *udpnconf;
8627c478bd9Sstevel@tonic-gate 	int clfd;
8637c478bd9Sstevel@tonic-gate 	int rv;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/* check for superuser as reserved ports are for superuser only */
8667c478bd9Sstevel@tonic-gate 	if (geteuid()) {
8677c478bd9Sstevel@tonic-gate 		errno = EACCES;
8687c478bd9Sstevel@tonic-gate 		return (-1);
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if (clnt_control(client, CLGET_FD, (char *) &clfd) == FALSE) {
8727c478bd9Sstevel@tonic-gate 		errprintf(NULL, "Could not get file dscriptor for client handle\n");
8737c478bd9Sstevel@tonic-gate 		return (-1);
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	if (Debug)
8777c478bd9Sstevel@tonic-gate 		printf("Clnt_control success, clfd = %d\n", clfd);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	if (t_getstate(clfd) != T_UNBND) {
8807c478bd9Sstevel@tonic-gate 		if (t_unbind(clfd) < 0) {
8817c478bd9Sstevel@tonic-gate 			return (-1);
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	if ((udpnconf = getnetconfigent("udp")) == (struct netconfig *) NULL) {
8867c478bd9Sstevel@tonic-gate 		errprintf(NULL, "no netconfig information about \"udp\"\n");
8877c478bd9Sstevel@tonic-gate 		return (-1);
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (Debug) {
8917c478bd9Sstevel@tonic-gate 		printf("getnetconfigent success\n");
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if ((rv = netdir_options(udpnconf, ND_SET_RESERVEDPORT, clfd,
8957c478bd9Sstevel@tonic-gate 				(char *) NULL)) == -1) {
8967c478bd9Sstevel@tonic-gate 		if (Debug) {
8977c478bd9Sstevel@tonic-gate 			printf("netdir_options fails rv=%d\n", rv);
8987c478bd9Sstevel@tonic-gate 		}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		errprintf(NULL, netdir_sperror());
9017c478bd9Sstevel@tonic-gate 		return (-1);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (Debug)
9057c478bd9Sstevel@tonic-gate 		printf("netdir_options success rv = %d\n", rv);
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	freenetconfigent(udpnconf);
9087c478bd9Sstevel@tonic-gate 	return (0);
9097c478bd9Sstevel@tonic-gate }
910