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