xref: /illumos-gate/usr/src/cmd/fs.d/nfs/lib/nfs_subr.c (revision b4509b05)
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
5b9238976Sth199096  * Common Development and Distribution License (the "License").
6b9238976Sth199096  * 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
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b9238976Sth199096  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23b9238976Sth199096  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <rpcsvc/nlm_prot.h>
307c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
317c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
327c478bd9Sstevel@tonic-gate #include "nfs_subr.h"
33b9238976Sth199096 #include <errno.h>
34b9238976Sth199096 #include <deflt.h>
35*b4509b05SMarcel Telka #include <rpcsvc/daemon_utils.h>
36*b4509b05SMarcel Telka #include "smfcfg.h"
37b9238976Sth199096 
38b9238976Sth199096 #include <nfs/nfssys.h>
39b9238976Sth199096 extern int _nfssys(enum nfssys_op, void *);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * This function is added to detect compatibility problem with SunOS4.x.
437c478bd9Sstevel@tonic-gate  * The compatibility problem exists when fshost cannot decode the request
447c478bd9Sstevel@tonic-gate  * arguments for NLM_GRANTED procedure.
457c478bd9Sstevel@tonic-gate  * Only in this case  we use local locking.
467c478bd9Sstevel@tonic-gate  * In any other case we use fshost's lockd for remote file locking.
477c478bd9Sstevel@tonic-gate  * Return value: 1 if we should use local locking, 0 if not.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate int
remote_lock(char * fshost,caddr_t fh)507c478bd9Sstevel@tonic-gate remote_lock(char *fshost, caddr_t fh)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	nlm_testargs rlm_args;
537c478bd9Sstevel@tonic-gate 	nlm_res rlm_res;
547c478bd9Sstevel@tonic-gate 	struct timeval timeout = { 5, 0};
557c478bd9Sstevel@tonic-gate 	CLIENT *cl;
567c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
577c478bd9Sstevel@tonic-gate 	struct utsname myid;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	(void) memset((char *)&rlm_args, 0, sizeof (nlm_testargs));
607c478bd9Sstevel@tonic-gate 	(void) memset((char *)&rlm_res, 0, sizeof (nlm_res));
617c478bd9Sstevel@tonic-gate 	/*
627c478bd9Sstevel@tonic-gate 	 * Assign the hostname and the file handle for the
637c478bd9Sstevel@tonic-gate 	 * NLM_GRANTED request below.  If for some reason the uname call fails,
647c478bd9Sstevel@tonic-gate 	 * list the server as the caller so that caller_name has some
657c478bd9Sstevel@tonic-gate 	 * reasonable value.
667c478bd9Sstevel@tonic-gate 	 */
677c478bd9Sstevel@tonic-gate 	if (uname(&myid) == -1)  {
687c478bd9Sstevel@tonic-gate 		rlm_args.alock.caller_name = fshost;
697c478bd9Sstevel@tonic-gate 	} else {
707c478bd9Sstevel@tonic-gate 		rlm_args.alock.caller_name = myid.nodename;
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 	rlm_args.alock.fh.n_len = sizeof (fhandle_t);
737c478bd9Sstevel@tonic-gate 	rlm_args.alock.fh.n_bytes = fh;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	cl = clnt_create(fshost, NLM_PROG, NLM_VERS, "datagram_v");
767c478bd9Sstevel@tonic-gate 	if (cl == NULL)
777c478bd9Sstevel@tonic-gate 		return (0);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(cl, NLM_GRANTED,
807c478bd9Sstevel@tonic-gate 	    xdr_nlm_testargs, (caddr_t)&rlm_args,
817c478bd9Sstevel@tonic-gate 	    xdr_nlm_res, (caddr_t)&rlm_res, timeout);
827c478bd9Sstevel@tonic-gate 	clnt_destroy(cl);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	return (rpc_stat == RPC_CANTDECODEARGS);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	fromhex(c)  ((c >= '0' && c <= '9') ? (c - '0') : \
887c478bd9Sstevel@tonic-gate 			((c >= 'A' && c <= 'F') ? (c - 'A' + 10) :\
897c478bd9Sstevel@tonic-gate 			((c >= 'a' && c <= 'f') ? (c - 'a' + 10) : 0)))
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * The implementation of URLparse guarantees that the final string will
937c478bd9Sstevel@tonic-gate  * fit in the original one. Replaces '%' occurrences followed by 2 characters
947c478bd9Sstevel@tonic-gate  * with its corresponding hexadecimal character.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate void
URLparse(char * str)977c478bd9Sstevel@tonic-gate URLparse(char *str)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	char *p, *q;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	p = q = str;
1027c478bd9Sstevel@tonic-gate 	while (*p) {
1037c478bd9Sstevel@tonic-gate 		*q = *p;
1047c478bd9Sstevel@tonic-gate 		if (*p++ == '%') {
1057c478bd9Sstevel@tonic-gate 			if (*p) {
1067c478bd9Sstevel@tonic-gate 				*q = fromhex(*p) * 16;
1077c478bd9Sstevel@tonic-gate 				p++;
1087c478bd9Sstevel@tonic-gate 				if (*p) {
1097c478bd9Sstevel@tonic-gate 					*q += fromhex(*p);
1107c478bd9Sstevel@tonic-gate 					p++;
1117c478bd9Sstevel@tonic-gate 				}
1127c478bd9Sstevel@tonic-gate 			}
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 		q++;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	*q = '\0';
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Convert from URL syntax to host:path syntax.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate int
convert_special(char ** specialp,char * host,char * oldpath,char * newpath,char * cur_special)1237c478bd9Sstevel@tonic-gate convert_special(char **specialp, char *host, char *oldpath, char *newpath,
1247c478bd9Sstevel@tonic-gate     char *cur_special)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	char *url;
1287c478bd9Sstevel@tonic-gate 	char *newspec;
1297c478bd9Sstevel@tonic-gate 	char *p;
1307c478bd9Sstevel@tonic-gate 	char *p1, *p2;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * Rebuild the URL. This is necessary because parse replica
1347c478bd9Sstevel@tonic-gate 	 * assumes that nfs: is the host name.
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	url = malloc(strlen("nfs:") + strlen(oldpath) + 1);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (url == NULL)
1397c478bd9Sstevel@tonic-gate 		return (-1);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	strcpy(url, "nfs:");
1427c478bd9Sstevel@tonic-gate 	strcat(url, oldpath);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * If we haven't done any conversion yet, allocate a buffer for it.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	if (*specialp == NULL) {
1487c478bd9Sstevel@tonic-gate 		newspec = *specialp = strdup(cur_special);
1497c478bd9Sstevel@tonic-gate 		if (newspec == NULL) {
1507c478bd9Sstevel@tonic-gate 			free(url);
1517c478bd9Sstevel@tonic-gate 			return (-1);
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	} else {
1557c478bd9Sstevel@tonic-gate 		newspec = *specialp;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * Now find the first occurence of the URL in the special string.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	p = strstr(newspec, url);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (p == NULL) {
1647c478bd9Sstevel@tonic-gate 		free(url);
1657c478bd9Sstevel@tonic-gate 		return (-1);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	p1 = p;
1697c478bd9Sstevel@tonic-gate 	p2 = host;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Overwrite the URL in the special.
1737c478bd9Sstevel@tonic-gate 	 *
1747c478bd9Sstevel@tonic-gate 	 * Begin with the host name.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	for (;;) {
1777c478bd9Sstevel@tonic-gate 		/*
1787c478bd9Sstevel@tonic-gate 		 * Sine URL's take more room than host:path, there is
1797c478bd9Sstevel@tonic-gate 		 * no way we should hit a null byte in the original special.
1807c478bd9Sstevel@tonic-gate 		 */
1817c478bd9Sstevel@tonic-gate 		if (*p1 == '\0') {
1827c478bd9Sstevel@tonic-gate 			free(url);
1837c478bd9Sstevel@tonic-gate 			free(*specialp);
1847c478bd9Sstevel@tonic-gate 			*specialp = NULL;
1857c478bd9Sstevel@tonic-gate 			return (-1);
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		if (*p2 == '\0') {
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		*p1 = *p2;
1937c478bd9Sstevel@tonic-gate 		p1++;
1947c478bd9Sstevel@tonic-gate 		p2++;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/*
1987c478bd9Sstevel@tonic-gate 	 * Add the : separator.
1997c478bd9Sstevel@tonic-gate 	 */
2007c478bd9Sstevel@tonic-gate 	*p1 = ':';
2017c478bd9Sstevel@tonic-gate 	p1++;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * Now over write into special the path portion of host:path in
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	p2 = newpath;
2077c478bd9Sstevel@tonic-gate 	for (;;) {
2087c478bd9Sstevel@tonic-gate 		if (*p1 == '\0') {
2097c478bd9Sstevel@tonic-gate 			free(url);
2107c478bd9Sstevel@tonic-gate 			free(*specialp);
2117c478bd9Sstevel@tonic-gate 			*specialp = NULL;
2127c478bd9Sstevel@tonic-gate 			return (-1);
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 		if (*p2 == '\0') {
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 		*p1 = *p2;
2187c478bd9Sstevel@tonic-gate 		p1++;
2197c478bd9Sstevel@tonic-gate 		p2++;
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * Now shift the rest of original special into the gap created
2247c478bd9Sstevel@tonic-gate 	 * by replacing nfs://host[:port]/path with host:path.
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	p2 = p + strlen(url);
2277c478bd9Sstevel@tonic-gate 	for (;;) {
2287c478bd9Sstevel@tonic-gate 		if (*p1 == '\0') {
2297c478bd9Sstevel@tonic-gate 			free(url);
2307c478bd9Sstevel@tonic-gate 			free(*specialp);
2317c478bd9Sstevel@tonic-gate 			*specialp = NULL;
2327c478bd9Sstevel@tonic-gate 			return (-1);
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		if (*p2 == '\0') {
2357c478bd9Sstevel@tonic-gate 			break;
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 		*p1 = *p2;
2387c478bd9Sstevel@tonic-gate 		p1++;
2397c478bd9Sstevel@tonic-gate 		p2++;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	*p1 = '\0';
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	free(url);
2457c478bd9Sstevel@tonic-gate 	return (0);
2467c478bd9Sstevel@tonic-gate }
247b9238976Sth199096 
248b9238976Sth199096 #define	AUTOFS_MOUNT_TIMEOUT	600	/* default min time mount will */
249b9238976Sth199096 
250b9238976Sth199096 void
set_nfsv4_ephemeral_mount_to(void)251b9238976Sth199096 set_nfsv4_ephemeral_mount_to(void)
252b9238976Sth199096 {
253*b4509b05SMarcel Telka 	char valbuf[6];
254*b4509b05SMarcel Telka 	int bufsz = sizeof (valbuf);
255b9238976Sth199096 
256b9238976Sth199096 	uint_t mount_to = AUTOFS_MOUNT_TIMEOUT;
257b9238976Sth199096 
258b9238976Sth199096 	/*
259*b4509b05SMarcel Telka 	 * Get the value from SMF
260b9238976Sth199096 	 */
261*b4509b05SMarcel Telka 	if (autofs_smf_get_prop("timeout", valbuf, DEFAULT_INSTANCE,
262*b4509b05SMarcel Telka 	    SCF_TYPE_INTEGER, AUTOMOUNTD, &bufsz) == SA_OK) {
263*b4509b05SMarcel Telka 		const char *errstr;
264*b4509b05SMarcel Telka 		uint_t val = strtonum(valbuf, 0, UINT_MAX, &errstr);
265*b4509b05SMarcel Telka 		if (errstr == NULL)
266*b4509b05SMarcel Telka 			mount_to = val;
267b9238976Sth199096 	}
268b9238976Sth199096 
269b9238976Sth199096 	(void) _nfssys(NFS4_EPHEMERAL_MOUNT_TO, &mount_to);
270b9238976Sth199096 }
271