16185db85Sdougm /*
26185db85Sdougm  * CDDL HEADER START
36185db85Sdougm  *
46185db85Sdougm  * The contents of this file are subject to the terms of the
56185db85Sdougm  * Common Development and Distribution License (the "License").
66185db85Sdougm  * You may not use this file except in compliance with the License.
76185db85Sdougm  *
86185db85Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96185db85Sdougm  * or http://www.opensolaris.org/os/licensing.
106185db85Sdougm  * See the License for the specific language governing permissions
116185db85Sdougm  * and limitations under the License.
126185db85Sdougm  *
136185db85Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
146185db85Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156185db85Sdougm  * If applicable, add the following below this CDDL HEADER, with the
166185db85Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
176185db85Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
186185db85Sdougm  *
196185db85Sdougm  * CDDL HEADER END
206185db85Sdougm  */
216185db85Sdougm 
226185db85Sdougm /*
233472f5dcSdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246185db85Sdougm  * Use is subject to license terms.
256185db85Sdougm  */
266185db85Sdougm 
276185db85Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
286185db85Sdougm 
296185db85Sdougm /*
306185db85Sdougm  * NFS specific functions
316185db85Sdougm  */
326185db85Sdougm #include <stdio.h>
336185db85Sdougm #include <string.h>
346185db85Sdougm #include <ctype.h>
356185db85Sdougm #include <stdlib.h>
366185db85Sdougm #include <unistd.h>
376185db85Sdougm #include <zone.h>
386185db85Sdougm #include <errno.h>
396185db85Sdougm #include <locale.h>
406185db85Sdougm #include <signal.h>
416185db85Sdougm #include "libshare.h"
426185db85Sdougm #include "libshare_impl.h"
436185db85Sdougm #include <nfs/export.h>
446185db85Sdougm #include <pwd.h>
456185db85Sdougm #include <limits.h>
466185db85Sdougm #include <libscf.h>
476185db85Sdougm #include "nfslog_config.h"
486185db85Sdougm #include "nfslogtab.h"
496185db85Sdougm #include "libshare_nfs.h"
506185db85Sdougm #include <rpcsvc/daemon_utils.h>
516185db85Sdougm #include <nfs/nfs.h>
52ecd6cf80Smarks #include <nfs/nfssys.h>
536185db85Sdougm 
546185db85Sdougm /* should really be in some global place */
556185db85Sdougm #define	DEF_WIN	30000
566185db85Sdougm #define	OPT_CHUNK	1024
576185db85Sdougm 
586185db85Sdougm int debug = 0;
596185db85Sdougm 
60ecd6cf80Smarks #define	NFS_SERVER_SVC	"svc:/network/nfs/server:default"
616185db85Sdougm 
626185db85Sdougm /* internal functions */
636185db85Sdougm static int nfs_init();
646185db85Sdougm static void nfs_fini();
656185db85Sdougm static int nfs_enable_share(sa_share_t);
66ecd6cf80Smarks static int nfs_disable_share(sa_share_t, char *);
676185db85Sdougm static int nfs_validate_property(sa_property_t, sa_optionset_t);
686185db85Sdougm static int nfs_validate_security_mode(char *);
696185db85Sdougm static int nfs_is_security_opt(char *);
706185db85Sdougm static int nfs_parse_legacy_options(sa_group_t, char *);
716185db85Sdougm static char *nfs_format_options(sa_group_t, int);
726185db85Sdougm static int nfs_set_proto_prop(sa_property_t);
736185db85Sdougm static sa_protocol_properties_t nfs_get_proto_set();
746185db85Sdougm static char *nfs_get_status();
756185db85Sdougm static char *nfs_space_alias(char *);
76da6c28aaSamw static uint64_t nfs_features();
776185db85Sdougm 
786185db85Sdougm /*
796185db85Sdougm  * ops vector that provides the protocol specific info and operations
806185db85Sdougm  * for share management.
816185db85Sdougm  */
826185db85Sdougm 
836185db85Sdougm struct sa_plugin_ops sa_plugin_ops = {
846185db85Sdougm 	SA_PLUGIN_VERSION,
856185db85Sdougm 	"nfs",
866185db85Sdougm 	nfs_init,
876185db85Sdougm 	nfs_fini,
886185db85Sdougm 	nfs_enable_share,
896185db85Sdougm 	nfs_disable_share,
906185db85Sdougm 	nfs_validate_property,
916185db85Sdougm 	nfs_validate_security_mode,
926185db85Sdougm 	nfs_is_security_opt,
936185db85Sdougm 	nfs_parse_legacy_options,
946185db85Sdougm 	nfs_format_options,
956185db85Sdougm 	nfs_set_proto_prop,
966185db85Sdougm 	nfs_get_proto_set,
976185db85Sdougm 	nfs_get_status,
986185db85Sdougm 	nfs_space_alias,
99da6c28aaSamw 	NULL,	/* update_legacy */
100da6c28aaSamw 	NULL,	/* delete_legacy */
101da6c28aaSamw 	NULL,	/* change_notify */
102da6c28aaSamw 	NULL,	/* enable_resource */
103da6c28aaSamw 	NULL,	/* disable_resource */
104da6c28aaSamw 	nfs_features,
105da6c28aaSamw 	NULL,	/* transient shares */
106da6c28aaSamw 	NULL,	/* notify resource */
1076185db85Sdougm 	NULL
1086185db85Sdougm };
1096185db85Sdougm 
1106185db85Sdougm /*
1116185db85Sdougm  * list of support services needed
1126185db85Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
1136185db85Sdougm  */
1146185db85Sdougm 
1156185db85Sdougm static char *service_list_default[] =
1166185db85Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL };
1176185db85Sdougm static char *service_list_logging[] =
1186185db85Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL };
1196185db85Sdougm 
1206185db85Sdougm /*
1216185db85Sdougm  * option definitions.  Make sure to keep the #define for the option
1226185db85Sdougm  * index just before the entry it is the index for. Changing the order
1236185db85Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1246185db85Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1256185db85Sdougm  */
1266185db85Sdougm 
1276185db85Sdougm struct option_defs optdefs[] = {
1286185db85Sdougm #define	OPT_RO		0
1296185db85Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1306185db85Sdougm #define	OPT_RW		1
1316185db85Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1326185db85Sdougm #define	OPT_ROOT	2
1336185db85Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1346185db85Sdougm #define	OPT_SECURE	3
1356185db85Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1366185db85Sdougm #define	OPT_ANON	4
1376185db85Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1386185db85Sdougm #define	OPT_WINDOW	5
1396185db85Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1406185db85Sdougm #define	OPT_NOSUID	6
1416185db85Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1426185db85Sdougm #define	OPT_ACLOK	7
1436185db85Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1446185db85Sdougm #define	OPT_NOSUB	8
1456185db85Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1466185db85Sdougm #define	OPT_SEC		9
1476185db85Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1486185db85Sdougm #define	OPT_PUBLIC	10
1496185db85Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1506185db85Sdougm #define	OPT_INDEX	11
1516185db85Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1526185db85Sdougm #define	OPT_LOG		12
1536185db85Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1546185db85Sdougm #define	OPT_CKSUM	13
1556185db85Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
1566185db85Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
1576185db85Sdougm #define	OPT_VOLFH	14
1586185db85Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1596185db85Sdougm #endif /* VOLATILE_FH_TEST */
1606185db85Sdougm 	NULL
1616185db85Sdougm };
1626185db85Sdougm 
1636185db85Sdougm /*
164a99982a7Sdougm  * list of properties that are related to security flavors.
1656185db85Sdougm  */
1666185db85Sdougm static char *seclist[] = {
1676185db85Sdougm 	SHOPT_RO,
1686185db85Sdougm 	SHOPT_RW,
1696185db85Sdougm 	SHOPT_ROOT,
1706185db85Sdougm 	SHOPT_WINDOW,
1716185db85Sdougm 	NULL
1726185db85Sdougm };
1736185db85Sdougm 
1746185db85Sdougm /* structure for list of securities */
1756185db85Sdougm struct securities {
176a99982a7Sdougm 	sa_security_t security;
177a99982a7Sdougm 	struct securities *next;
1786185db85Sdougm };
1796185db85Sdougm 
1806185db85Sdougm /*
1816185db85Sdougm  * findopt(name)
1826185db85Sdougm  *
1836185db85Sdougm  * Lookup option "name" in the option table and return the table
1846185db85Sdougm  * index.
1856185db85Sdougm  */
1866185db85Sdougm 
1876185db85Sdougm static int
1886185db85Sdougm findopt(char *name)
1896185db85Sdougm {
1906185db85Sdougm 	int i;
1916185db85Sdougm 	if (name != NULL) {
192a3351425Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
193a3351425Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
194a3351425Sdougm 				return (i);
195a3351425Sdougm 		}
1966185db85Sdougm 	}
1976185db85Sdougm 	return (-1);
1986185db85Sdougm }
1996185db85Sdougm 
2006185db85Sdougm /*
2016185db85Sdougm  * gettype(name)
2026185db85Sdougm  *
2036185db85Sdougm  * Return the type of option "name".
2046185db85Sdougm  */
2056185db85Sdougm 
2066185db85Sdougm static int
2076185db85Sdougm gettype(char *name)
2086185db85Sdougm {
2096185db85Sdougm 	int optdef;
2106185db85Sdougm 
2116185db85Sdougm 	optdef = findopt(name);
2126185db85Sdougm 	if (optdef != -1)
213a3351425Sdougm 		return (optdefs[optdef].type);
2146185db85Sdougm 	return (OPT_TYPE_ANY);
2156185db85Sdougm }
2166185db85Sdougm 
2176185db85Sdougm /*
2186185db85Sdougm  * nfs_validate_security_mode(mode)
2196185db85Sdougm  *
2206185db85Sdougm  * is the specified mode string a valid one for use with NFS?
2216185db85Sdougm  */
2226185db85Sdougm 
2236185db85Sdougm static int
2246185db85Sdougm nfs_validate_security_mode(char *mode)
2256185db85Sdougm {
2266185db85Sdougm 	seconfig_t secinfo;
2276185db85Sdougm 	int err;
2286185db85Sdougm 
2296185db85Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2306185db85Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2316185db85Sdougm 	if (err == SC_NOERROR)
232a3351425Sdougm 		return (1);
2336185db85Sdougm 	return (0);
2346185db85Sdougm }
2356185db85Sdougm 
2366185db85Sdougm /*
2376185db85Sdougm  * nfs_is_security_opt(tok)
2386185db85Sdougm  *
2396185db85Sdougm  * check to see if tok represents an option that is only valid in some
2406185db85Sdougm  * security flavor.
2416185db85Sdougm  */
2426185db85Sdougm 
2436185db85Sdougm static int
2446185db85Sdougm nfs_is_security_opt(char *tok)
2456185db85Sdougm {
2466185db85Sdougm 	int i;
2476185db85Sdougm 
2486185db85Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
249a3351425Sdougm 		if (strcmp(tok, seclist[i]) == 0)
250a3351425Sdougm 			return (1);
2516185db85Sdougm 	}
2526185db85Sdougm 	return (0);
2536185db85Sdougm }
2546185db85Sdougm 
2556185db85Sdougm /*
2566185db85Sdougm  * find_security(seclist, sec)
2576185db85Sdougm  *
2586185db85Sdougm  * Walk the current list of security flavors and return true if it is
2596185db85Sdougm  * present, else return false.
2606185db85Sdougm  */
2616185db85Sdougm 
2626185db85Sdougm static int
2636185db85Sdougm find_security(struct securities *seclist, sa_security_t sec)
2646185db85Sdougm {
2656185db85Sdougm 	while (seclist != NULL) {
266a3351425Sdougm 		if (seclist->security == sec)
267a3351425Sdougm 			return (1);
268a3351425Sdougm 		seclist = seclist->next;
2696185db85Sdougm 	}
2706185db85Sdougm 	return (0);
2716185db85Sdougm }
2726185db85Sdougm 
2736185db85Sdougm /*
2746185db85Sdougm  * make_security_list(group, securitymodes, proto)
2756185db85Sdougm  *	go through the list of securitymodes and add them to the
2766185db85Sdougm  *	group's list of security optionsets. We also keep a list of
2776185db85Sdougm  *	those optionsets so we don't have to find them later. All of
2786185db85Sdougm  *	these will get copies of the same properties.
2796185db85Sdougm  */
2806185db85Sdougm 
2816185db85Sdougm static struct securities *
2826185db85Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
2836185db85Sdougm {
2846185db85Sdougm 	char *tok, *next = NULL;
2856185db85Sdougm 	struct securities *curp, *headp = NULL, *prev;
2866185db85Sdougm 	sa_security_t check;
2876185db85Sdougm 	int freetok = 0;
2886185db85Sdougm 
2896185db85Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
290a3351425Sdougm 		next = strchr(tok, ':');
291a3351425Sdougm 		if (next != NULL)
292a3351425Sdougm 			*next++ = '\0';
293a3351425Sdougm 		if (strcmp(tok, "default") == 0) {
294a3351425Sdougm 			/* resolve default into the real type */
295a3351425Sdougm 			tok = nfs_space_alias(tok);
296a3351425Sdougm 			freetok = 1;
297a3351425Sdougm 		}
298a3351425Sdougm 		check = sa_get_security(group, tok, proto);
299a3351425Sdougm 
300a3351425Sdougm 		/* add to the security list if it isn't there already */
301a3351425Sdougm 		if (check == NULL || !find_security(headp, check)) {
302a3351425Sdougm 			curp = (struct securities *)calloc(1,
303a3351425Sdougm 			    sizeof (struct securities));
304a3351425Sdougm 			if (curp != NULL) {
305a3351425Sdougm 				if (check == NULL) {
306a3351425Sdougm 					curp->security = sa_create_security(
307a3351425Sdougm 					    group, tok, proto);
308a3351425Sdougm 				} else {
309a3351425Sdougm 					curp->security = check;
310a3351425Sdougm 				}
311a3351425Sdougm 				/*
312a3351425Sdougm 				 * note that the first time through the loop,
313a3351425Sdougm 				 * headp will be NULL and prev will be
314a3351425Sdougm 				 * undefined.  Since headp is NULL, we set
315a3351425Sdougm 				 * both it and prev to the curp (first
316a3351425Sdougm 				 * structure to be allocated).
317a3351425Sdougm 				 *
318a3351425Sdougm 				 * later passes through the loop will have
319a3351425Sdougm 				 * headp not being NULL and prev will be used
320a3351425Sdougm 				 * to allocate at the end of the list.
321a3351425Sdougm 				 */
322a3351425Sdougm 				if (headp == NULL) {
323a3351425Sdougm 					headp = curp;
324a3351425Sdougm 					prev = curp;
325a3351425Sdougm 				} else {
326a3351425Sdougm 					prev->next = curp;
327a3351425Sdougm 					prev = curp;
328a3351425Sdougm 				}
329a3351425Sdougm 			}
3306185db85Sdougm 		}
331a99982a7Sdougm 
332a3351425Sdougm 		if (freetok) {
333a3351425Sdougm 			freetok = 0;
334a3351425Sdougm 			sa_free_attr_string(tok);
335a3351425Sdougm 		}
3366185db85Sdougm 	}
3376185db85Sdougm 	return (headp);
3386185db85Sdougm }
3396185db85Sdougm 
3406185db85Sdougm static void
3416185db85Sdougm free_security_list(struct securities *sec)
3426185db85Sdougm {
3436185db85Sdougm 	struct securities *next;
3446185db85Sdougm 	if (sec != NULL) {
345a3351425Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
346a3351425Sdougm 			next = sec->next;
347a3351425Sdougm 			free(sec);
348a3351425Sdougm 		}
3496185db85Sdougm 	}
3506185db85Sdougm }
3516185db85Sdougm 
3526185db85Sdougm /*
3536185db85Sdougm  * nfs_alistcat(str1, str2, sep)
3546185db85Sdougm  *
3556185db85Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
3566185db85Sdougm  * character. If memory allocation fails, return NULL;
3576185db85Sdougm  */
3586185db85Sdougm 
3596185db85Sdougm static char *
3606185db85Sdougm nfs_alistcat(char *str1, char *str2, char sep)
3616185db85Sdougm {
3626185db85Sdougm 	char *newstr;
3636185db85Sdougm 	size_t len;
3646185db85Sdougm 
3656185db85Sdougm 	len = strlen(str1) + strlen(str2) + 2;
3666185db85Sdougm 	newstr = (char *)malloc(len);
3676185db85Sdougm 	if (newstr != NULL)
368a3351425Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
3696185db85Sdougm 	return (newstr);
3706185db85Sdougm }
3716185db85Sdougm 
3726185db85Sdougm /*
3736185db85Sdougm  * add_security_prop(sec, name, value, persist)
3746185db85Sdougm  *
3756185db85Sdougm  * Add the property to the securities structure. This accumulates
3766185db85Sdougm  * properties for as part of parsing legacy options.
3776185db85Sdougm  */
3786185db85Sdougm 
3796185db85Sdougm static int
3806185db85Sdougm add_security_prop(struct securities *sec, char *name, char *value,
3816185db85Sdougm 			int persist, int iszfs)
3826185db85Sdougm {
3836185db85Sdougm 	sa_property_t prop;
3846185db85Sdougm 	int ret = SA_OK;
3856185db85Sdougm 
3866185db85Sdougm 	for (; sec != NULL; sec = sec->next) {
387a3351425Sdougm 		if (value == NULL) {
388a3351425Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
389a3351425Sdougm 			    strcmp(name, SHOPT_RO) == 0)
390a3351425Sdougm 				value = "*";
391a3351425Sdougm 			else
392a3351425Sdougm 				value = "true";
393a3351425Sdougm 		}
394a99982a7Sdougm 
395a99982a7Sdougm 		/*
396a99982a7Sdougm 		 * Get the existing property, if it exists, so we can
397a99982a7Sdougm 		 * determine what to do with it. The ro/rw/root
398a99982a7Sdougm 		 * properties can be merged if multiple instances of
399a99982a7Sdougm 		 * these properies are given. For example, if "rw"
400a99982a7Sdougm 		 * exists with a value "host1" and a later token of
401a99982a7Sdougm 		 * rw="host2" is seen, the values are merged into a
402a99982a7Sdougm 		 * single rw="host1:host2".
403a99982a7Sdougm 		 */
404a3351425Sdougm 		prop = sa_get_property(sec->security, name);
405a99982a7Sdougm 
406a3351425Sdougm 		if (prop != NULL) {
407a3351425Sdougm 			char *oldvalue;
408a3351425Sdougm 			char *newvalue;
409a99982a7Sdougm 
410a99982a7Sdougm 			/*
411a3351425Sdougm 			 * The security options of ro/rw/root might appear
412a3351425Sdougm 			 * multiple times. If they do, the values need to be
413a3351425Sdougm 			 * merged into an access list. If it was previously
414a3351425Sdougm 			 * empty, the new value alone is added.
415a99982a7Sdougm 			 */
416a3351425Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
417a3351425Sdougm 			if (oldvalue != NULL) {
418a3351425Sdougm 				/*
419a3351425Sdougm 				 * The general case is to concatenate the new
420a3351425Sdougm 				 * value onto the old value for multiple
421a3351425Sdougm 				 * rw(ro/root) properties. A special case
422a3351425Sdougm 				 * exists when either the old or new is the
423a3351425Sdougm 				 * "all" case. In the special case, if both
424a3351425Sdougm 				 * are "all", then it is "all", else if one is
425a3351425Sdougm 				 * an access-list, that replaces the "all".
426a3351425Sdougm 				 */
427a3351425Sdougm 				if (strcmp(oldvalue, "*") == 0) {
428a3351425Sdougm 					/* Replace old value with new value. */
429a3351425Sdougm 					newvalue = strdup(value);
430*97df5ac9Sdougm 				} else if (strcmp(value, "*") == 0 ||
431*97df5ac9Sdougm 				    strcmp(oldvalue, value) == 0) {
432a3351425Sdougm 					/*
433a3351425Sdougm 					 * Keep old value and ignore
434a3351425Sdougm 					 * the new value.
435a3351425Sdougm 					 */
436a3351425Sdougm 					newvalue = NULL;
437a3351425Sdougm 				} else {
438a3351425Sdougm 					/*
439a3351425Sdougm 					 * Make a new list of old plus new
440a3351425Sdougm 					 * access-list.
441a3351425Sdougm 					 */
442a3351425Sdougm 					newvalue = nfs_alistcat(oldvalue,
443a3351425Sdougm 					    value, ':');
444a3351425Sdougm 				}
445a3351425Sdougm 
446a3351425Sdougm 				if (newvalue != NULL) {
447a3351425Sdougm 					(void) sa_remove_property(prop);
448a3351425Sdougm 					prop = sa_create_property(name,
449a3351425Sdougm 					    newvalue);
450a3351425Sdougm 					ret = sa_add_property(sec->security,
451a3351425Sdougm 					    prop);
452a3351425Sdougm 					free(newvalue);
453a3351425Sdougm 				}
454a3351425Sdougm 				if (oldvalue != NULL)
455a3351425Sdougm 					sa_free_attr_string(oldvalue);
456a3351425Sdougm 			}
457a3351425Sdougm 		} else {
458a3351425Sdougm 			prop = sa_create_property(name, value);
459a99982a7Sdougm 			ret = sa_add_property(sec->security, prop);
4606185db85Sdougm 		}
461a3351425Sdougm 		if (ret == SA_OK && !iszfs) {
462a3351425Sdougm 			ret = sa_commit_properties(sec->security, !persist);
463a3351425Sdougm 		}
4646185db85Sdougm 	}
4656185db85Sdougm 	return (ret);
4666185db85Sdougm }
4676185db85Sdougm 
4686185db85Sdougm /*
4696185db85Sdougm  * check to see if group/share is persistent.
4706185db85Sdougm  */
4716185db85Sdougm static int
4726185db85Sdougm is_persistent(sa_group_t group)
4736185db85Sdougm {
4746185db85Sdougm 	char *type;
4756185db85Sdougm 	int persist = 1;
4766185db85Sdougm 
4776185db85Sdougm 	type = sa_get_group_attr(group, "type");
4786185db85Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
479a3351425Sdougm 		persist = 0;
4806185db85Sdougm 	if (type != NULL)
481a3351425Sdougm 		sa_free_attr_string(type);
4826185db85Sdougm 	return (persist);
4836185db85Sdougm }
4846185db85Sdougm 
4856185db85Sdougm /*
4866185db85Sdougm  * invalid_security(options)
4876185db85Sdougm  *
4886185db85Sdougm  * search option string for any invalid sec= type.
4896185db85Sdougm  * return true (1) if any are not valid else false (0)
4906185db85Sdougm  */
4916185db85Sdougm static int
4926185db85Sdougm invalid_security(char *options)
4936185db85Sdougm {
4946185db85Sdougm 	char *copy, *base, *token, *value;
4956185db85Sdougm 	int ret = 0;
4966185db85Sdougm 
4976185db85Sdougm 	copy = strdup(options);
4986185db85Sdougm 	token = base = copy;
4996185db85Sdougm 	while (token != NULL && ret == 0) {
500a3351425Sdougm 		token = strtok(base, ",");
501a3351425Sdougm 		base = NULL;
502a3351425Sdougm 		if (token != NULL) {
503a3351425Sdougm 			value = strchr(token, '=');
504a3351425Sdougm 			if (value != NULL)
505a3351425Sdougm 				*value++ = '\0';
506a3351425Sdougm 			if (strcmp(token, "sec") == 0) {
507a3351425Sdougm 				/* HAVE security flavors so check them */
508a3351425Sdougm 				char *tok, *next;
509a3351425Sdougm 				for (next = NULL, tok = value; tok != NULL;
510a3351425Sdougm 				    tok = next) {
511a3351425Sdougm 					next = strchr(tok, ':');
512a3351425Sdougm 					if (next != NULL)
513a3351425Sdougm 						*next++ = '\0';
514a3351425Sdougm 					ret = !nfs_validate_security_mode(tok);
515a3351425Sdougm 					if (ret)
516a3351425Sdougm 						break;
517a3351425Sdougm 				}
518a3351425Sdougm 			}
5196185db85Sdougm 		}
5206185db85Sdougm 	}
5216185db85Sdougm 	if (copy != NULL)
522a3351425Sdougm 		free(copy);
5236185db85Sdougm 	return (ret);
5246185db85Sdougm }
5256185db85Sdougm 
5266185db85Sdougm /*
5276185db85Sdougm  * nfs_parse_legacy_options(group, options)
5286185db85Sdougm  *
5296185db85Sdougm  * Parse the old style options into internal format and store on the
5306185db85Sdougm  * specified group.  Group could be a share for full legacy support.
5316185db85Sdougm  */
5326185db85Sdougm 
5336185db85Sdougm static int
5346185db85Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5356185db85Sdougm {
536aba7a40bSdougm 	char *dup;
5376185db85Sdougm 	char *base;
5386185db85Sdougm 	char *token;
5396185db85Sdougm 	sa_optionset_t optionset;
5406185db85Sdougm 	struct securities *security_list = NULL;
5416185db85Sdougm 	sa_property_t prop;
5426185db85Sdougm 	int ret = SA_OK;
5436185db85Sdougm 	int iszfs = 0;
5446185db85Sdougm 	sa_group_t parent;
5456185db85Sdougm 	int persist = 0;
5466185db85Sdougm 	char *lasts;
5476185db85Sdougm 
5486185db85Sdougm 	/* do we have an existing optionset? */
5496185db85Sdougm 	optionset = sa_get_optionset(group, "nfs");
5506185db85Sdougm 	if (optionset == NULL) {
551a3351425Sdougm 		/* didn't find existing optionset so create one */
552a3351425Sdougm 		optionset = sa_create_optionset(group, "nfs");
5536185db85Sdougm 	} else {
5546185db85Sdougm 		/*
555da6c28aaSamw 		 * Have an existing optionset . Ideally, we would need
556da6c28aaSamw 		 * to compare options in order to detect errors. For
557da6c28aaSamw 		 * now, we assume that the first optionset is the
558da6c28aaSamw 		 * correct one and the others will be the same. An
559da6c28aaSamw 		 * empty optionset is the same as no optionset so we
560da6c28aaSamw 		 * don't want to exit in that case. Getting an empty
561da6c28aaSamw 		 * optionset can occur with ZFS property checking.
5626185db85Sdougm 		 */
563da6c28aaSamw 		if (sa_get_property(optionset, NULL) != NULL)
564da6c28aaSamw 			return (ret);
5656185db85Sdougm 	}
5666185db85Sdougm 
5676185db85Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
5686185db85Sdougm 		/*
5696185db85Sdougm 		 * there is a special case of only the option "rw"
5706185db85Sdougm 		 * being the default option. We don't have to do
5716185db85Sdougm 		 * anything.
5726185db85Sdougm 		 */
573a3351425Sdougm 		return (ret);
5746185db85Sdougm 	}
5756185db85Sdougm 
5766185db85Sdougm 	/*
5776185db85Sdougm 	 * check if security types are present and validate them. If
5786185db85Sdougm 	 * any are not legal, fail.
5796185db85Sdougm 	 */
5806185db85Sdougm 
5816185db85Sdougm 	if (invalid_security(options)) {
582a3351425Sdougm 		return (SA_INVALID_SECURITY);
5836185db85Sdougm 	}
5846185db85Sdougm 
5856185db85Sdougm 	/*
5866185db85Sdougm 	 * in order to not attempt to change ZFS properties unless
5876185db85Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
5886185db85Sdougm 	 */
5896185db85Sdougm 	if (sa_is_share(group)) {
590a3351425Sdougm 		char *zfs;
591a3351425Sdougm 		parent = sa_get_parent_group(group);
592a3351425Sdougm 		if (parent != NULL) {
593a3351425Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
594a3351425Sdougm 			if (zfs != NULL) {
595a3351425Sdougm 				sa_free_attr_string(zfs);
596a3351425Sdougm 				iszfs++;
597a3351425Sdougm 			}
5986185db85Sdougm 		}
5996185db85Sdougm 	} else {
600a3351425Sdougm 		iszfs = sa_group_is_zfs(group);
6016185db85Sdougm 	}
6026185db85Sdougm 
603aba7a40bSdougm 	/* We need a copy of options for the next part. */
604aba7a40bSdougm 	dup = strdup(options);
605aba7a40bSdougm 	if (dup == NULL)
606aba7a40bSdougm 		return (SA_NO_MEMORY);
607aba7a40bSdougm 
6086185db85Sdougm 	/*
6096185db85Sdougm 	 * we need to step through each option in the string and then
6106185db85Sdougm 	 * add either the option or the security option as needed. If
6116185db85Sdougm 	 * this is not a persistent share, don't commit to the
612a99982a7Sdougm 	 * repository. If there is an error, we also want to abort the
613a99982a7Sdougm 	 * processing and report it.
6146185db85Sdougm 	 */
6156185db85Sdougm 	persist = is_persistent(group);
6166185db85Sdougm 	base = dup;
6176185db85Sdougm 	token = dup;
6186185db85Sdougm 	lasts = NULL;
619a99982a7Sdougm 	while (token != NULL && ret == SA_OK) {
620a3351425Sdougm 		ret = SA_OK;
621a3351425Sdougm 		token = strtok_r(base, ",", &lasts);
622a3351425Sdougm 		base = NULL;
623a3351425Sdougm 		if (token != NULL) {
624a3351425Sdougm 			char *value;
6256185db85Sdougm 			/*
626a3351425Sdougm 			 * if the option has a value, it will have an '=' to
627a3351425Sdougm 			 * separate the name from the value. The following
628a3351425Sdougm 			 * code will result in value != NULL and token
629a3351425Sdougm 			 * pointing to just the name if there is a value.
6306185db85Sdougm 			 */
631a3351425Sdougm 			value = strchr(token, '=');
632a3351425Sdougm 			if (value != NULL) {
633a3351425Sdougm 				*value++ = '\0';
6346185db85Sdougm 			}
635a3351425Sdougm 			if (strcmp(token, "sec") == 0 ||
636a3351425Sdougm 			    strcmp(token, "secure") == 0) {
6376185db85Sdougm 				/*
638a3351425Sdougm 				 * Once in security parsing, we only
639a3351425Sdougm 				 * do security. We do need to move
640a3351425Sdougm 				 * between the security node and the
641a3351425Sdougm 				 * toplevel. The security tag goes on
642a3351425Sdougm 				 * the root while the following ones
643a3351425Sdougm 				 * go on the security.
6446185db85Sdougm 				 */
645a3351425Sdougm 				if (security_list != NULL) {
646a3351425Sdougm 					/*
647a3351425Sdougm 					 * have an old list so close it and
648a3351425Sdougm 					 * start the new
649a3351425Sdougm 					 */
650a3351425Sdougm 					free_security_list(security_list);
651a3351425Sdougm 				}
652a3351425Sdougm 				if (strcmp(token, "secure") == 0) {
653a3351425Sdougm 					value = "dh";
654a3351425Sdougm 				} else {
655a3351425Sdougm 					if (value == NULL) {
656a3351425Sdougm 						ret = SA_SYNTAX_ERR;
657a3351425Sdougm 						break;
658a3351425Sdougm 					}
659a3351425Sdougm 				}
660a3351425Sdougm 				security_list = make_security_list(group,
661a3351425Sdougm 				    value, "nfs");
6626185db85Sdougm 			} else {
663a3351425Sdougm 				/*
664a3351425Sdougm 				 * Note that the "old" syntax allowed a
665a3351425Sdougm 				 * default security model This must be
666a3351425Sdougm 				 * accounted for and internally converted to
667a3351425Sdougm 				 * "standard" security structure.
668a3351425Sdougm 				 */
669a3351425Sdougm 				if (nfs_is_security_opt(token)) {
670a3351425Sdougm 					if (security_list == NULL) {
671a3351425Sdougm 						/*
672a3351425Sdougm 						 * need to have a
673a3351425Sdougm 						 * security
674a3351425Sdougm 						 * option. This will
675a3351425Sdougm 						 * be "closed" when a
676a3351425Sdougm 						 * defined "sec="
677a3351425Sdougm 						 * option is
678a3351425Sdougm 						 * seen. This is
679a3351425Sdougm 						 * technically an
680a3351425Sdougm 						 * error but will be
681a3351425Sdougm 						 * allowed with
682a3351425Sdougm 						 * warning.
683a3351425Sdougm 						 */
684a3351425Sdougm 						security_list =
685a3351425Sdougm 						    make_security_list(group,
686a3351425Sdougm 						    "default",
687a3351425Sdougm 						    "nfs");
688a3351425Sdougm 					}
689a3351425Sdougm 					if (security_list != NULL) {
690a3351425Sdougm 						ret = add_security_prop(
691a3351425Sdougm 						    security_list, token,
692a3351425Sdougm 						    value, persist, iszfs);
693a3351425Sdougm 					} else {
694a3351425Sdougm 						ret = SA_NO_MEMORY;
695a3351425Sdougm 					}
696a3351425Sdougm 				} else {
697a3351425Sdougm 					/* regular options */
698a3351425Sdougm 					if (value == NULL) {
699a3351425Sdougm 						if (strcmp(token, SHOPT_RW) ==
700a3351425Sdougm 						    0 || strcmp(token,
701a3351425Sdougm 						    SHOPT_RO) == 0) {
702a3351425Sdougm 							value = "*";
703a3351425Sdougm 						} else {
704a3351425Sdougm 							value = "global";
705a3351425Sdougm 							if (strcmp(token,
706a3351425Sdougm 							    SHOPT_LOG) != 0) {
707a3351425Sdougm 								value = "true";
708a3351425Sdougm 							}
709a3351425Sdougm 						}
710a3351425Sdougm 					}
711d6405362Sdougm 					/*
712d6405362Sdougm 					 * In all cases, create the
713d6405362Sdougm 					 * property specified. If the
714d6405362Sdougm 					 * value was NULL, the default
715d6405362Sdougm 					 * value will have been
716d6405362Sdougm 					 * substituted.
717d6405362Sdougm 					 */
718d6405362Sdougm 					prop = sa_create_property(token, value);
719d6405362Sdougm 					ret =  sa_add_property(optionset, prop);
720d6405362Sdougm 					if (ret != SA_OK)
721d6405362Sdougm 						break;
722d6405362Sdougm 
723a3351425Sdougm 					if (!iszfs) {
724a3351425Sdougm 						ret = sa_commit_properties(
725a3351425Sdougm 						    optionset, !persist);
726a3351425Sdougm 					}
727a3351425Sdougm 				}
7286185db85Sdougm 			}
7296185db85Sdougm 		}
7306185db85Sdougm 	}
7316185db85Sdougm 	if (security_list != NULL)
732a3351425Sdougm 		free_security_list(security_list);
733aba7a40bSdougm 
734aba7a40bSdougm 	free(dup);
7356185db85Sdougm 	return (ret);
7366185db85Sdougm }
7376185db85Sdougm 
7386185db85Sdougm /*
7396185db85Sdougm  * is_a_number(number)
7406185db85Sdougm  *
7416185db85Sdougm  * is the string a number in one of the forms we want to use?
7426185db85Sdougm  */
7436185db85Sdougm 
7446185db85Sdougm static int
7456185db85Sdougm is_a_number(char *number)
7466185db85Sdougm {
7476185db85Sdougm 	int ret = 1;
7486185db85Sdougm 	int hex = 0;
7496185db85Sdougm 
7506185db85Sdougm 	if (strncmp(number, "0x", 2) == 0) {
751a3351425Sdougm 		number += 2;
752a3351425Sdougm 		hex = 1;
753a3351425Sdougm 	} else if (*number == '-') {
754a3351425Sdougm 		number++; /* skip the minus */
755a3351425Sdougm 	}
7566185db85Sdougm 	while (ret == 1 && *number != '\0') {
757a3351425Sdougm 		if (hex) {
758a3351425Sdougm 			ret = isxdigit(*number++);
759a3351425Sdougm 		} else {
760a3351425Sdougm 			ret = isdigit(*number++);
761a3351425Sdougm 		}
7626185db85Sdougm 	}
7636185db85Sdougm 	return (ret);
7646185db85Sdougm }
7656185db85Sdougm 
7666185db85Sdougm /*
7676185db85Sdougm  * Look for the specified tag in the configuration file. If it is found,
7686185db85Sdougm  * enable logging and set the logging configuration information for exp.
7696185db85Sdougm  */
7706185db85Sdougm static void
7716185db85Sdougm configlog(struct exportdata *exp, char *tag)
7726185db85Sdougm {
7736185db85Sdougm 	nfsl_config_t *configlist = NULL, *configp;
7746185db85Sdougm 	int error = 0;
7756185db85Sdougm 	char globaltag[] = DEFAULTTAG;
7766185db85Sdougm 
7776185db85Sdougm 	/*
7786185db85Sdougm 	 * Sends config errors to stderr
7796185db85Sdougm 	 */
7806185db85Sdougm 	nfsl_errs_to_syslog = B_FALSE;
7816185db85Sdougm 
7826185db85Sdougm 	/*
7836185db85Sdougm 	 * get the list of configuration settings
7846185db85Sdougm 	 */
7856185db85Sdougm 	error = nfsl_getconfig_list(&configlist);
7866185db85Sdougm 	if (error) {
7876185db85Sdougm 		(void) fprintf(stderr,
788a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
789a3351425Sdougm 		    strerror(error));
7906185db85Sdougm 	}
7916185db85Sdougm 
7926185db85Sdougm 	if (tag == NULL)
7936185db85Sdougm 		tag = globaltag;
7946185db85Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
7956185db85Sdougm 		nfsl_freeconfig_list(&configlist);
7966185db85Sdougm 		(void) fprintf(stderr,
797a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
7986185db85Sdougm 		/* bad configuration */
7996185db85Sdougm 		error = ENOENT;
8006185db85Sdougm 		goto err;
8016185db85Sdougm 	}
8026185db85Sdougm 
8036185db85Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
8046185db85Sdougm 		error = ENOMEM;
8056185db85Sdougm 		goto out;
8066185db85Sdougm 	}
8076185db85Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
8086185db85Sdougm 		error = ENOMEM;
8096185db85Sdougm 		goto out;
8106185db85Sdougm 	}
8116185db85Sdougm 	exp->ex_flags |= EX_LOG;
8126185db85Sdougm 	if (configp->nc_rpclogpath != NULL)
8136185db85Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8146185db85Sdougm out:
8156185db85Sdougm 	if (configlist != NULL)
816a3351425Sdougm 		nfsl_freeconfig_list(&configlist);
8176185db85Sdougm 
8186185db85Sdougm err:
8196185db85Sdougm 	if (error != 0) {
8206185db85Sdougm 		if (exp->ex_flags != NULL)
8216185db85Sdougm 			free(exp->ex_tag);
8226185db85Sdougm 		if (exp->ex_log_buffer != NULL)
8236185db85Sdougm 			free(exp->ex_log_buffer);
8246185db85Sdougm 		(void) fprintf(stderr,
825a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
826a3351425Sdougm 		    strerror(error));
8276185db85Sdougm 	}
8286185db85Sdougm }
8296185db85Sdougm 
8306185db85Sdougm /*
8316185db85Sdougm  * fill_export_from_optionset(export, optionset)
8326185db85Sdougm  *
8336185db85Sdougm  * In order to share, we need to set all the possible general options
8346185db85Sdougm  * into the export structure. Share info will be filled in by the
8356185db85Sdougm  * caller. Various property values get turned into structure specific
8366185db85Sdougm  * values.
8376185db85Sdougm  */
8386185db85Sdougm 
8396185db85Sdougm static int
8406185db85Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
8416185db85Sdougm {
8426185db85Sdougm 	sa_property_t option;
8436185db85Sdougm 	int ret = SA_OK;
8446185db85Sdougm 
8456185db85Sdougm 	for (option = sa_get_property(optionset, NULL);
846a3351425Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
847a3351425Sdougm 		char *name;
848a3351425Sdougm 		char *value;
849a3351425Sdougm 		uint32_t val;
8506185db85Sdougm 
851a3351425Sdougm 		/*
852a3351425Sdougm 		 * since options may be set/reset multiple times, always do an
853a3351425Sdougm 		 * explicit set or clear of the option. This allows defaults
854da6c28aaSamw 		 * to be set and then the protocol specific to override.
855a3351425Sdougm 		 */
8566185db85Sdougm 
857a3351425Sdougm 		name = sa_get_property_attr(option, "type");
858a3351425Sdougm 		value = sa_get_property_attr(option, "value");
859a3351425Sdougm 		switch (findopt(name)) {
860a3351425Sdougm 		case OPT_ANON:
861a3351425Sdougm 			if (value != NULL && is_a_number(value)) {
862a3351425Sdougm 				val = strtoul(value, NULL, 0);
863a3351425Sdougm 			} else {
864a3351425Sdougm 				struct passwd *pw;
865a3351425Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
866a3351425Sdougm 				if (pw != NULL) {
867a3351425Sdougm 					val = pw->pw_uid;
868a3351425Sdougm 				} else {
869a3351425Sdougm 					val = UID_NOBODY;
870a3351425Sdougm 				}
871a3351425Sdougm 				endpwent();
872a3351425Sdougm 			}
873a3351425Sdougm 			export->ex_anon = val;
874a3351425Sdougm 			break;
875a3351425Sdougm 		case OPT_NOSUID:
876a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
877a3351425Sdougm 			    strcmp(value, "1") == 0))
878a3351425Sdougm 				export->ex_flags |= EX_NOSUID;
879a3351425Sdougm 			else
880a3351425Sdougm 				export->ex_flags &= ~EX_NOSUID;
881a3351425Sdougm 			break;
882a3351425Sdougm 		case OPT_ACLOK:
883a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
884a3351425Sdougm 			    strcmp(value, "1") == 0))
885a3351425Sdougm 				export->ex_flags |= EX_ACLOK;
886a3351425Sdougm 			else
887a3351425Sdougm 				export->ex_flags &= ~EX_ACLOK;
888a3351425Sdougm 			break;
889a3351425Sdougm 		case OPT_NOSUB:
890a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
891a3351425Sdougm 			    strcmp(value, "1") == 0))
892a3351425Sdougm 				export->ex_flags |= EX_NOSUB;
893a3351425Sdougm 			else
894a3351425Sdougm 				export->ex_flags &= ~EX_NOSUB;
895a3351425Sdougm 			break;
896a3351425Sdougm 		case OPT_PUBLIC:
897a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
898a3351425Sdougm 			    strcmp(value, "1") == 0))
899a3351425Sdougm 				export->ex_flags |= EX_PUBLIC;
900a3351425Sdougm 			else
901a3351425Sdougm 				export->ex_flags &= ~EX_PUBLIC;
902a3351425Sdougm 			break;
903a3351425Sdougm 		case OPT_INDEX:
904a3351425Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
905a3351425Sdougm 			    strchr(value, '/') != NULL)) {
906a3351425Sdougm 				/* this is an error */
907a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
908a3351425Sdougm 				    "NFS: index=\"%s\" not valid;"
909a3351425Sdougm 				    "must be a filename.\n"),
910a3351425Sdougm 				    value);
911a3351425Sdougm 				break;
912a3351425Sdougm 			}
913a3351425Sdougm 			if (value != NULL && *value != '\0' &&
914a3351425Sdougm 			    strcmp(value, ".") != 0) {
915a3351425Sdougm 				/* valid index file string */
916a3351425Sdougm 				if (export->ex_index != NULL) {
917a3351425Sdougm 					/* left over from "default" */
918a3351425Sdougm 					free(export->ex_index);
919a3351425Sdougm 				}
920a3351425Sdougm 				/* remember to free */
921a3351425Sdougm 				export->ex_index = strdup(value);
922a3351425Sdougm 				if (export->ex_index == NULL) {
923a3351425Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
924a3351425Sdougm 					    "NFS: out of memory setting "
925a3351425Sdougm 					    "index property\n"));
926a3351425Sdougm 					break;
927a3351425Sdougm 				}
928a3351425Sdougm 				export->ex_flags |= EX_INDEX;
929a3351425Sdougm 			}
930a3351425Sdougm 			break;
931a3351425Sdougm 		case OPT_LOG:
932a3351425Sdougm 			if (value == NULL)
933a3351425Sdougm 				value = strdup("global");
934a3351425Sdougm 			if (value != NULL)
935a3351425Sdougm 				configlog(export,
936a3351425Sdougm 				    strlen(value) ? value : "global");
937a3351425Sdougm 			break;
938a3351425Sdougm 		default:
939a3351425Sdougm 			/* have a syntactic error */
940549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
941a3351425Sdougm 			    "NFS: unrecognized option %s=%s\n"),
942a3351425Sdougm 			    name, value != NULL ? value : "");
9436185db85Sdougm 			break;
9446185db85Sdougm 		}
945a3351425Sdougm 		if (name != NULL)
946a3351425Sdougm 			sa_free_attr_string(name);
9476185db85Sdougm 		if (value != NULL)
948a3351425Sdougm 			sa_free_attr_string(value);
9496185db85Sdougm 	}
9506185db85Sdougm 	return (ret);
9516185db85Sdougm }
9526185db85Sdougm 
9536185db85Sdougm /*
9546185db85Sdougm  * cleanup_export(export)
9556185db85Sdougm  *
9566185db85Sdougm  * Cleanup the allocated areas so we don't leak memory
9576185db85Sdougm  */
9586185db85Sdougm 
9596185db85Sdougm static void
9606185db85Sdougm cleanup_export(struct exportdata *export)
9616185db85Sdougm {
9626185db85Sdougm 	int i;
9636185db85Sdougm 
9646185db85Sdougm 	if (export->ex_index != NULL)
965a3351425Sdougm 		free(export->ex_index);
9666185db85Sdougm 	if (export->ex_secinfo != NULL) {
967a3351425Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
968a3351425Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
969a3351425Sdougm 				free(export->ex_secinfo[i].s_rootnames);
970a3351425Sdougm 		free(export->ex_secinfo);
9716185db85Sdougm 	}
9726185db85Sdougm }
9736185db85Sdougm 
9746185db85Sdougm /*
9756185db85Sdougm  * Given a seconfig entry and a colon-separated
9766185db85Sdougm  * list of names, allocate an array big enough
9776185db85Sdougm  * to hold the root list, then convert each name to
9786185db85Sdougm  * a principal name according to the security
9796185db85Sdougm  * info and assign it to an array element.
9806185db85Sdougm  * Return the array and its size.
9816185db85Sdougm  */
9826185db85Sdougm static caddr_t *
9836185db85Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
9846185db85Sdougm {
9856185db85Sdougm 	caddr_t *a;
9866185db85Sdougm 	int c, i;
9876185db85Sdougm 	char *host, *p;
9886185db85Sdougm 
9896185db85Sdougm 	/*
9906185db85Sdougm 	 * Count the number of strings in the list.
9916185db85Sdougm 	 * This is the number of colon separators + 1.
9926185db85Sdougm 	 */
9936185db85Sdougm 	c = 1;
9946185db85Sdougm 	for (p = list; *p; p++)
9956185db85Sdougm 		if (*p == ':')
9966185db85Sdougm 			c++;
9976185db85Sdougm 	*count = c;
9986185db85Sdougm 
9996185db85Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
10006185db85Sdougm 	if (a == NULL) {
1001549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1002a3351425Sdougm 		    "get_rootnames: no memory\n"));
10036185db85Sdougm 	} else {
1004a3351425Sdougm 		for (i = 0; i < c; i++) {
1005a3351425Sdougm 			host = strtok(list, ":");
1006a3351425Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
1007a3351425Sdougm 				free(a);
1008a3351425Sdougm 				a = NULL;
1009a3351425Sdougm 				break;
1010a3351425Sdougm 			}
1011a3351425Sdougm 			list = NULL;
10126185db85Sdougm 		}
10136185db85Sdougm 	}
10146185db85Sdougm 
10156185db85Sdougm 	return (a);
10166185db85Sdougm }
10176185db85Sdougm 
10186185db85Sdougm /*
10196185db85Sdougm  * fill_security_from_secopts(sp, secopts)
10206185db85Sdougm  *
10216185db85Sdougm  * Fill the secinfo structure from the secopts optionset.
10226185db85Sdougm  */
10236185db85Sdougm 
10246185db85Sdougm static int
10256185db85Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
10266185db85Sdougm {
10276185db85Sdougm 	sa_property_t prop;
10286185db85Sdougm 	char *type;
10296185db85Sdougm 	int longform;
1030a99982a7Sdougm 	int err = SC_NOERROR;
10316185db85Sdougm 
10326185db85Sdougm 	type = sa_get_security_attr(secopts, "sectype");
10336185db85Sdougm 	if (type != NULL) {
1034a3351425Sdougm 		/* named security type needs secinfo to be filled in */
1035a3351425Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
1036a3351425Sdougm 		sa_free_attr_string(type);
1037a3351425Sdougm 		if (err != SC_NOERROR)
1038a3351425Sdougm 			return (err);
10396185db85Sdougm 	} else {
1040a3351425Sdougm 		/* default case */
1041a3351425Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
1042a3351425Sdougm 		if (err != SC_NOERROR)
1043a3351425Sdougm 			return (err);
10446185db85Sdougm 	}
10456185db85Sdougm 
1046a99982a7Sdougm 	err = SA_OK;
10476185db85Sdougm 	for (prop = sa_get_property(secopts, NULL);
1048a3351425Sdougm 	    prop != NULL && err == SA_OK;
1049a3351425Sdougm 	    prop = sa_get_next_property(prop)) {
1050a3351425Sdougm 		char *name;
1051a3351425Sdougm 		char *value;
10526185db85Sdougm 
1053a3351425Sdougm 		name = sa_get_property_attr(prop, "type");
1054a3351425Sdougm 		value = sa_get_property_attr(prop, "value");
10556185db85Sdougm 
1056a3351425Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
10576185db85Sdougm 
1058a3351425Sdougm 		switch (findopt(name)) {
1059a3351425Sdougm 		case OPT_RO:
1060a3351425Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
1061a3351425Sdougm 			break;
1062a3351425Sdougm 		case OPT_RW:
1063a3351425Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
1064a3351425Sdougm 			break;
1065a3351425Sdougm 		case OPT_ROOT:
1066a3351425Sdougm 			sp->s_flags |= M_ROOT;
1067a3351425Sdougm 			/*
1068a3351425Sdougm 			 * if we are using AUTH_UNIX, handle like other things
1069a3351425Sdougm 			 * such as RO/RW
1070a3351425Sdougm 			 */
1071a3351425Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
1072a3351425Sdougm 				continue;
1073a3351425Sdougm 			/* not AUTH_UNIX */
1074a3351425Sdougm 			if (value != NULL) {
1075a3351425Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
1076a3351425Sdougm 				    value, &sp->s_rootcnt);
1077a3351425Sdougm 				if (sp->s_rootnames == NULL) {
1078a3351425Sdougm 					err = SA_BAD_VALUE;
1079a3351425Sdougm 					(void) fprintf(stderr,
1080a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
1081a3351425Sdougm 					    "Bad root list\n"));
1082a3351425Sdougm 				}
1083a3351425Sdougm 			}
1084a3351425Sdougm 			break;
1085a3351425Sdougm 		case OPT_WINDOW:
1086a3351425Sdougm 			if (value != NULL) {
1087a3351425Sdougm 				sp->s_window = atoi(value);
1088a3351425Sdougm 				/* just in case */
1089a3351425Sdougm 				if (sp->s_window < 0)
1090a3351425Sdougm 					sp->s_window = DEF_WIN;
1091a3351425Sdougm 			}
1092a3351425Sdougm 			break;
1093a3351425Sdougm 		default:
1094a3351425Sdougm 			break;
10956185db85Sdougm 		}
1096a3351425Sdougm 		if (name != NULL)
1097a3351425Sdougm 			sa_free_attr_string(name);
1098a3351425Sdougm 		if (value != NULL)
1099a3351425Sdougm 			sa_free_attr_string(value);
11006185db85Sdougm 	}
11016185db85Sdougm 	/* if rw/ro options not set, use default of RW */
11026185db85Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
1103a3351425Sdougm 		sp->s_flags |= M_RW;
11046185db85Sdougm 	return (err);
11056185db85Sdougm }
11066185db85Sdougm 
11076185db85Sdougm /*
11086185db85Sdougm  * This is for testing only
11096185db85Sdougm  * It displays the export structure that
11106185db85Sdougm  * goes into the kernel.
11116185db85Sdougm  */
11126185db85Sdougm static void
11136185db85Sdougm printarg(char *path, struct exportdata *ep)
11146185db85Sdougm {
11156185db85Sdougm 	int i, j;
11166185db85Sdougm 	struct secinfo *sp;
11176185db85Sdougm 
11186185db85Sdougm 	if (debug == 0)
1119a3351425Sdougm 		return;
11206185db85Sdougm 
11216185db85Sdougm 	(void) printf("%s:\n", path);
11226185db85Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
11236185db85Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
1124549ec3ffSdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
11256185db85Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
11266185db85Sdougm 	if (ep->ex_flags & EX_NOSUID)
11276185db85Sdougm 		(void) printf("NOSUID ");
11286185db85Sdougm 	if (ep->ex_flags & EX_ACLOK)
11296185db85Sdougm 		(void) printf("ACLOK ");
11306185db85Sdougm 	if (ep->ex_flags & EX_PUBLIC)
11316185db85Sdougm 		(void) printf("PUBLIC ");
11326185db85Sdougm 	if (ep->ex_flags & EX_NOSUB)
11336185db85Sdougm 		(void) printf("NOSUB ");
11346185db85Sdougm 	if (ep->ex_flags & EX_LOG)
11356185db85Sdougm 		(void) printf("LOG ");
11366185db85Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
11376185db85Sdougm 		(void) printf("LOG_ALLOPS ");
11386185db85Sdougm 	if (ep->ex_flags == 0)
11396185db85Sdougm 		(void) printf("(none)");
11406185db85Sdougm 	(void) 	printf("\n");
11416185db85Sdougm 	if (ep->ex_flags & EX_LOG) {
11426185db85Sdougm 		(void) printf("\tex_log_buffer = %s\n",
1143a3351425Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
11446185db85Sdougm 		(void) printf("\tex_tag = %s\n",
1145a3351425Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
11466185db85Sdougm 	}
11476185db85Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
11486185db85Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
11496185db85Sdougm 	(void) printf("\n");
11506185db85Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
11516185db85Sdougm 		sp = &ep->ex_secinfo[i];
11526185db85Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
11536185db85Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
11546185db85Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
11556185db85Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
11566185db85Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
11576185db85Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
11586185db85Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
11596185db85Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
11606185db85Sdougm 		(void) printf("\n");
11616185db85Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
11626185db85Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
11636185db85Sdougm 		(void) fflush(stdout);
11646185db85Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
11656185db85Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
1166a3351425Sdougm 			    sp->s_rootnames[j] : "<null>");
11676185db85Sdougm 		(void) printf("\n\n");
11686185db85Sdougm 	}
11696185db85Sdougm }
11706185db85Sdougm 
11716185db85Sdougm /*
11726185db85Sdougm  * count_security(opts)
11736185db85Sdougm  *
11746185db85Sdougm  * Count the number of security types (flavors). The optionset has
11756185db85Sdougm  * been populated with the security flavors as a holding mechanism.
11766185db85Sdougm  * We later use this number to allocate data structures.
11776185db85Sdougm  */
11786185db85Sdougm 
11796185db85Sdougm static int
11806185db85Sdougm count_security(sa_optionset_t opts)
11816185db85Sdougm {
11826185db85Sdougm 	int count = 0;
11836185db85Sdougm 	sa_property_t prop;
11846185db85Sdougm 	if (opts != NULL) {
1185a3351425Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
1186a3351425Sdougm 		    prop = sa_get_next_property(prop)) {
1187a3351425Sdougm 			count++;
1188a3351425Sdougm 		}
11896185db85Sdougm 	}
11906185db85Sdougm 	return (count);
11916185db85Sdougm }
11926185db85Sdougm 
11936185db85Sdougm /*
11946185db85Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
11956185db85Sdougm  *
11966185db85Sdougm  * provides a mechanism to format NFS properties into legacy output
11976185db85Sdougm  * format. If the buffer would overflow, it is reallocated and grown
11986185db85Sdougm  * as appropriate. Special cases of converting internal form of values
11996185db85Sdougm  * to those used by "share" are done. this function does one property
12006185db85Sdougm  * at a time.
12016185db85Sdougm  */
12026185db85Sdougm 
1203aed5d200Sdougm static int
12046185db85Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
12056185db85Sdougm 			sa_property_t prop, int sep)
12066185db85Sdougm {
12076185db85Sdougm 	char *name;
12086185db85Sdougm 	char *value;
12096185db85Sdougm 	int curlen;
12106185db85Sdougm 	char *buff = *rbuff;
12116185db85Sdougm 	size_t buffsize = *rbuffsize;
1212aed5d200Sdougm 	int printed = B_FALSE;
12136185db85Sdougm 
12146185db85Sdougm 	name = sa_get_property_attr(prop, "type");
12156185db85Sdougm 	value = sa_get_property_attr(prop, "value");
12166185db85Sdougm 	if (buff != NULL)
1217a3351425Sdougm 		curlen = strlen(buff);
12186185db85Sdougm 	else
1219a3351425Sdougm 		curlen = 0;
12206185db85Sdougm 	if (name != NULL) {
1221a3351425Sdougm 		int len;
1222a3351425Sdougm 		len = strlen(name) + sep;
12236185db85Sdougm 
12246185db85Sdougm 		/*
12256185db85Sdougm 		 * A future RFE would be to replace this with more
12266185db85Sdougm 		 * generic code and to possibly handle more types.
12276185db85Sdougm 		 */
1228a3351425Sdougm 		switch (gettype(name)) {
1229a3351425Sdougm 		case OPT_TYPE_BOOLEAN:
1230aed5d200Sdougm 			/*
1231aed5d200Sdougm 			 * For NFS, boolean value of FALSE means it
1232aed5d200Sdougm 			 * doesn't show up in the option list at all.
1233aed5d200Sdougm 			 */
1234a3351425Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
1235aed5d200Sdougm 				goto skip;
1236aed5d200Sdougm 			if (value != NULL) {
1237a3351425Sdougm 				sa_free_attr_string(value);
1238aed5d200Sdougm 				value = NULL;
1239aed5d200Sdougm 			}
1240a3351425Sdougm 			break;
1241a3351425Sdougm 		case OPT_TYPE_ACCLIST:
1242a3351425Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
1243a3351425Sdougm 				sa_free_attr_string(value);
1244a3351425Sdougm 				value = NULL;
1245a3351425Sdougm 			} else {
1246a3351425Sdougm 				if (value != NULL)
1247a3351425Sdougm 					len += 1 + strlen(value);
1248a3351425Sdougm 			}
1249a3351425Sdougm 			break;
1250a3351425Sdougm 		case OPT_TYPE_LOGTAG:
1251a3351425Sdougm 			if (value != NULL && strlen(value) == 0) {
1252a3351425Sdougm 				sa_free_attr_string(value);
1253a3351425Sdougm 				value = NULL;
1254a3351425Sdougm 			} else {
1255a3351425Sdougm 				if (value != NULL)
1256a3351425Sdougm 					len += 1 + strlen(value);
1257a3351425Sdougm 			}
1258a3351425Sdougm 			break;
1259a3351425Sdougm 		default:
1260a3351425Sdougm 			if (value != NULL)
1261a3351425Sdougm 				len += 1 + strlen(value);
1262a3351425Sdougm 			break;
12636185db85Sdougm 		}
1264a3351425Sdougm 		while (buffsize <= (curlen + len)) {
1265a3351425Sdougm 			/* need more room */
1266a3351425Sdougm 			buffsize += incr;
1267a3351425Sdougm 			buff = realloc(buff, buffsize);
1268a3351425Sdougm 			if (buff == NULL) {
1269a3351425Sdougm 				/* realloc failed so free everything */
1270a3351425Sdougm 				if (*rbuff != NULL)
1271a3351425Sdougm 					free(*rbuff);
1272a3351425Sdougm 			}
1273a3351425Sdougm 			*rbuff = buff;
1274a3351425Sdougm 			*rbuffsize = buffsize;
1275aed5d200Sdougm 			if (buff == NULL)
1276aed5d200Sdougm 				goto skip;
1277aed5d200Sdougm 
12786185db85Sdougm 		}
1279aed5d200Sdougm 
1280a3351425Sdougm 		if (buff == NULL)
1281aed5d200Sdougm 			goto skip;
1282aed5d200Sdougm 
1283a3351425Sdougm 		if (value == NULL) {
1284a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1285a3351425Sdougm 			    "%s%s", sep ? "," : "",
1286a3351425Sdougm 			    name, value != NULL ? value : "");
12876185db85Sdougm 		} else {
1288a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1289a3351425Sdougm 			    "%s%s=%s", sep ? "," : "",
1290a3351425Sdougm 			    name, value != NULL ? value : "");
12916185db85Sdougm 		}
1292aed5d200Sdougm 		printed = B_TRUE;
12936185db85Sdougm 	}
1294aed5d200Sdougm skip:
12956185db85Sdougm 	if (name != NULL)
1296a3351425Sdougm 		sa_free_attr_string(name);
12976185db85Sdougm 	if (value != NULL)
1298a3351425Sdougm 		sa_free_attr_string(value);
1299aed5d200Sdougm 	return (printed);
13006185db85Sdougm }
13016185db85Sdougm 
13026185db85Sdougm /*
13036185db85Sdougm  * nfs_format_options(group, hier)
13046185db85Sdougm  *
13056185db85Sdougm  * format all the options on the group into an old-style option
13066185db85Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
13076185db85Sdougm  * options.
13086185db85Sdougm  */
13096185db85Sdougm 
13106185db85Sdougm static char *
13116185db85Sdougm nfs_format_options(sa_group_t group, int hier)
13126185db85Sdougm {
13136185db85Sdougm 	sa_optionset_t options = NULL;
1314a3351425Sdougm 	sa_optionset_t secoptions = NULL;
13156185db85Sdougm 	sa_property_t prop, secprop;
1316a3351425Sdougm 	sa_security_t security = NULL;
13176185db85Sdougm 	char *buff;
13186185db85Sdougm 	size_t buffsize;
1319a3351425Sdougm 	char *sectype = NULL;
1320a3351425Sdougm 	int sep = 0;
1321a3351425Sdougm 
1322a3351425Sdougm 
1323a3351425Sdougm 	buff = malloc(OPT_CHUNK);
1324a3351425Sdougm 	if (buff == NULL) {
1325a3351425Sdougm 		return (NULL);
1326a3351425Sdougm 	}
1327a3351425Sdougm 
1328a3351425Sdougm 	buff[0] = '\0';
1329a3351425Sdougm 	buffsize = OPT_CHUNK;
1330a3351425Sdougm 
1331a3351425Sdougm 	/*
1332a3351425Sdougm 	 * We may have a an optionset relative to this item. format
1333a3351425Sdougm 	 * these if we find them and then add any security definitions.
1334a3351425Sdougm 	 */
13356185db85Sdougm 
13366185db85Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
13376185db85Sdougm 
13386185db85Sdougm 	/*
1339a3351425Sdougm 	 * do the default set first but skip any option that is also
1340a3351425Sdougm 	 * in the protocol specific optionset.
13416185db85Sdougm 	 */
1342a3351425Sdougm 	if (options != NULL) {
1343a3351425Sdougm 		for (prop = sa_get_property(options, NULL);
1344a3351425Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
13456185db85Sdougm 			/*
1346a3351425Sdougm 			 * use this one since we skipped any
1347a3351425Sdougm 			 * of these that were also in
1348a3351425Sdougm 			 * optdefault
13496185db85Sdougm 			 */
1350aed5d200Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1351aed5d200Sdougm 			    prop, sep))
1352aed5d200Sdougm 				sep = 1;
1353a3351425Sdougm 			if (buff == NULL) {
1354a3351425Sdougm 				/*
1355a3351425Sdougm 				 * buff could become NULL if there
1356a3351425Sdougm 				 * isn't enough memory for
1357a3351425Sdougm 				 * nfs_sprint_option to realloc()
1358a3351425Sdougm 				 * as necessary. We can't really
1359a3351425Sdougm 				 * do anything about it at this
1360a3351425Sdougm 				 * point so we return NULL.  The
1361a3351425Sdougm 				 * caller should handle the
1362a3351425Sdougm 				 * failure.
1363a3351425Sdougm 				 */
13646185db85Sdougm 				if (options != NULL)
1365a3351425Sdougm 					sa_free_derived_optionset(
1366a3351425Sdougm 					    options);
13676185db85Sdougm 				return (buff);
13686185db85Sdougm 			}
1369a3351425Sdougm 		}
1370a3351425Sdougm 	}
1371a3351425Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1372a3351425Sdougm 	    "nfs", hier);
1373a3351425Sdougm 	if (secoptions != NULL) {
1374a3351425Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
1375a3351425Sdougm 		    secprop != NULL;
1376a3351425Sdougm 		    secprop = sa_get_next_property(secprop)) {
1377a3351425Sdougm 			sectype = sa_get_property_attr(secprop, "type");
1378a3351425Sdougm 			security =
1379a3351425Sdougm 			    (sa_security_t)sa_get_derived_security(
1380a3351425Sdougm 			    group, sectype, "nfs", hier);
1381a3351425Sdougm 			if (security != NULL) {
1382a3351425Sdougm 				if (sectype != NULL) {
1383a3351425Sdougm 					prop = sa_create_property(
1384a3351425Sdougm 					    "sec", sectype);
1385aed5d200Sdougm 					if (prop == NULL)
1386aed5d200Sdougm 						goto err;
1387aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1388aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1389aed5d200Sdougm 						sep = 1;
1390a3351425Sdougm 					(void) sa_remove_property(prop);
1391aed5d200Sdougm 					if (buff == NULL)
1392aed5d200Sdougm 						goto err;
1393a3351425Sdougm 				}
1394a3351425Sdougm 				for (prop = sa_get_property(security,
1395a3351425Sdougm 				    NULL); prop != NULL;
1396a3351425Sdougm 				    prop = sa_get_next_property(prop)) {
1397aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1398aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1399aed5d200Sdougm 						sep = 1;
1400a3351425Sdougm 					if (buff == NULL)
1401a3351425Sdougm 						goto err;
1402a3351425Sdougm 				}
1403a3351425Sdougm 				sa_free_derived_optionset(security);
1404a3351425Sdougm 			}
1405a3351425Sdougm 			if (sectype != NULL)
1406a3351425Sdougm 				sa_free_attr_string(sectype);
14076185db85Sdougm 		}
14086185db85Sdougm 		sa_free_derived_optionset(secoptions);
14096185db85Sdougm 	}
1410a3351425Sdougm 
14116185db85Sdougm 	if (options != NULL)
1412a3351425Sdougm 		sa_free_derived_optionset(options);
1413a3351425Sdougm 	return (buff);
1414a3351425Sdougm 
1415a3351425Sdougm err:
1416a3351425Sdougm 	/*
1417a3351425Sdougm 	 * If we couldn't allocate memory for option printing, we need
1418a3351425Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
1419a3351425Sdougm 	 */
1420a3351425Sdougm 	if (secoptions != NULL)
1421a3351425Sdougm 		sa_free_derived_optionset(secoptions);
1422a3351425Sdougm 	if (security != NULL)
1423a3351425Sdougm 		sa_free_derived_optionset(security);
1424a3351425Sdougm 	if (sectype != NULL)
1425a3351425Sdougm 		sa_free_attr_string(sectype);
1426a3351425Sdougm 	if (options != NULL)
1427a3351425Sdougm 		sa_free_derived_optionset(options);
14286185db85Sdougm 	return (buff);
14296185db85Sdougm }
1430a3351425Sdougm 
14316185db85Sdougm /*
14326185db85Sdougm  * Append an entry to the nfslogtab file
14336185db85Sdougm  */
14346185db85Sdougm static int
14356185db85Sdougm nfslogtab_add(dir, buffer, tag)
14366185db85Sdougm 	char *dir, *buffer, *tag;
14376185db85Sdougm {
14386185db85Sdougm 	FILE *f;
14396185db85Sdougm 	struct logtab_ent lep;
14406185db85Sdougm 	int error = 0;
14416185db85Sdougm 
14426185db85Sdougm 	/*
14436185db85Sdougm 	 * Open the file for update and create it if necessary.
14446185db85Sdougm 	 * This may leave the I/O offset at the end of the file,
14456185db85Sdougm 	 * so rewind back to the beginning of the file.
14466185db85Sdougm 	 */
14476185db85Sdougm 	f = fopen(NFSLOGTAB, "a+");
14486185db85Sdougm 	if (f == NULL) {
14496185db85Sdougm 		error = errno;
14506185db85Sdougm 		goto out;
14516185db85Sdougm 	}
14526185db85Sdougm 	rewind(f);
14536185db85Sdougm 
14546185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1455549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1456a3351425Sdougm 		    "share complete, however failed to lock %s "
1457a3351425Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
14586185db85Sdougm 		error = -1;
14596185db85Sdougm 		goto out;
14606185db85Sdougm 	}
14616185db85Sdougm 
14626185db85Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
1463549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1464a3351425Sdougm 		    "share complete, however could not deactivate "
1465a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14666185db85Sdougm 		error = -1;
14676185db85Sdougm 		goto out;
14686185db85Sdougm 	}
14696185db85Sdougm 
14706185db85Sdougm 	/*
14716185db85Sdougm 	 * Remove entries matching buffer and sharepoint since we're
14726185db85Sdougm 	 * going to replace it with perhaps an entry with a new tag.
14736185db85Sdougm 	 */
14746185db85Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
1475549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1476a3351425Sdougm 		    "share complete, however could not remove matching "
1477a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14786185db85Sdougm 		error = -1;
14796185db85Sdougm 		goto out;
14806185db85Sdougm 	}
14816185db85Sdougm 
14826185db85Sdougm 	/*
14836185db85Sdougm 	 * Deactivate all active entries matching this sharepoint
14846185db85Sdougm 	 */
14856185db85Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
1486549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1487a3351425Sdougm 		    "share complete, however could not deactivate matching "
1488a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14896185db85Sdougm 		error = -1;
14906185db85Sdougm 		goto out;
14916185db85Sdougm 	}
14926185db85Sdougm 
14936185db85Sdougm 	lep.le_buffer = buffer;
14946185db85Sdougm 	lep.le_path = dir;
14956185db85Sdougm 	lep.le_tag = tag;
14966185db85Sdougm 	lep.le_state = LES_ACTIVE;
14976185db85Sdougm 
14986185db85Sdougm 	/*
14996185db85Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
15006185db85Sdougm 	 */
15016185db85Sdougm 	if (logtab_putent(f, &lep) < 0) {
1502549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1503a3351425Sdougm 		    "share complete, however could not add %s to %s\n"),
1504a3351425Sdougm 		    dir, NFSLOGTAB);
15056185db85Sdougm 		error = -1;
15066185db85Sdougm 	}
15076185db85Sdougm 
15086185db85Sdougm out:
15096185db85Sdougm 	if (f != NULL)
15106185db85Sdougm 		(void) fclose(f);
15116185db85Sdougm 	return (error);
15126185db85Sdougm }
15136185db85Sdougm 
15146185db85Sdougm /*
15156185db85Sdougm  * Deactivate an entry from the nfslogtab file
15166185db85Sdougm  */
15176185db85Sdougm static int
15186185db85Sdougm nfslogtab_deactivate(path)
15196185db85Sdougm 	char *path;
15206185db85Sdougm {
15216185db85Sdougm 	FILE *f;
15226185db85Sdougm 	int error = 0;
15236185db85Sdougm 
15246185db85Sdougm 	f = fopen(NFSLOGTAB, "r+");
15256185db85Sdougm 	if (f == NULL) {
15266185db85Sdougm 		error = errno;
15276185db85Sdougm 		goto out;
15286185db85Sdougm 	}
15296185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
15306185db85Sdougm 		error = errno;
1531549ec3ffSdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
1532a3351425Sdougm 		    "share complete, however could not lock %s for "
1533a3351425Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
15346185db85Sdougm 		goto out;
15356185db85Sdougm 	}
15366185db85Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
15376185db85Sdougm 		error = -1;
15386185db85Sdougm 		(void) fprintf(stderr,
1539a3351425Sdougm 		    dgettext(TEXT_DOMAIN,
1540a3351425Sdougm 		    "share complete, however could not "
1541a3351425Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
15426185db85Sdougm 		goto out;
15436185db85Sdougm 	}
15446185db85Sdougm 
15456185db85Sdougm out:	if (f != NULL)
15466185db85Sdougm 		(void) fclose(f);
15476185db85Sdougm 
15486185db85Sdougm 	return (error);
15496185db85Sdougm }
15506185db85Sdougm 
1551546405c3Sdougm /*
1552546405c3Sdougm  * check_public(group, skipshare)
1553546405c3Sdougm  *
1554546405c3Sdougm  * Check the group for any shares that have the public property
1555546405c3Sdougm  * enabled. We skip "skipshare" since that is the one we are
1556546405c3Sdougm  * working with. This is a separate function to make handling
1557546405c3Sdougm  * subgroups simpler. Returns true if there is a share with public.
1558546405c3Sdougm  */
1559546405c3Sdougm static int
1560546405c3Sdougm check_public(sa_group_t group, sa_share_t skipshare)
1561546405c3Sdougm {
1562546405c3Sdougm 	int exists = B_FALSE;
1563546405c3Sdougm 	sa_share_t share;
1564546405c3Sdougm 	sa_optionset_t opt;
1565546405c3Sdougm 	sa_property_t prop;
1566546405c3Sdougm 	char *shared;
1567546405c3Sdougm 
1568546405c3Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
1569546405c3Sdougm 	    share = sa_get_next_share(share)) {
1570546405c3Sdougm 		if (share == skipshare)
1571546405c3Sdougm 			continue;
1572546405c3Sdougm 
1573546405c3Sdougm 		opt = sa_get_optionset(share, "nfs");
1574546405c3Sdougm 		if (opt == NULL)
1575546405c3Sdougm 			continue;
1576546405c3Sdougm 		prop = sa_get_property(opt, "public");
1577546405c3Sdougm 		if (prop == NULL)
1578546405c3Sdougm 			continue;
1579546405c3Sdougm 		shared = sa_get_share_attr(share, "shared");
1580546405c3Sdougm 		if (shared != NULL) {
1581546405c3Sdougm 			exists = strcmp(shared, "true") == 0;
1582546405c3Sdougm 			sa_free_attr_string(shared);
1583546405c3Sdougm 			if (exists == B_TRUE)
1584546405c3Sdougm 				break;
1585546405c3Sdougm 		}
1586546405c3Sdougm 	}
1587546405c3Sdougm 
1588546405c3Sdougm 	return (exists);
1589546405c3Sdougm }
1590546405c3Sdougm 
15916185db85Sdougm /*
15926185db85Sdougm  * public_exists(share)
15936185db85Sdougm  *
15946185db85Sdougm  * check to see if public option is set on any other share than the
1595546405c3Sdougm  * one specified. Need to check zfs sub-groups as well as the top
1596546405c3Sdougm  * level groups.
15976185db85Sdougm  */
15986185db85Sdougm static int
15996185db85Sdougm public_exists(sa_share_t skipshare)
16006185db85Sdougm {
16016185db85Sdougm 	sa_group_t group;
1602549ec3ffSdougm 	sa_handle_t handle;
16036185db85Sdougm 
1604549ec3ffSdougm 	group = sa_get_parent_group(skipshare);
1605549ec3ffSdougm 	if (group == NULL)
1606a3351425Sdougm 		return (SA_NO_SUCH_GROUP);
1607549ec3ffSdougm 
1608549ec3ffSdougm 	handle = sa_find_group_handle(group);
1609549ec3ffSdougm 	if (handle == NULL)
1610a3351425Sdougm 		return (SA_SYSTEM_ERR);
1611549ec3ffSdougm 
1612549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
16136185db85Sdougm 	    group = sa_get_next_group(group)) {
1614546405c3Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
1615546405c3Sdougm 		if (sa_group_is_zfs(group)) {
1616546405c3Sdougm 			sa_group_t subgroup;
1617546405c3Sdougm 			for (subgroup = sa_get_sub_group(group);
1618546405c3Sdougm 			    subgroup != NULL;
1619546405c3Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
1620546405c3Sdougm 				if (check_public(subgroup, skipshare))
1621546405c3Sdougm 					return (B_TRUE);
16226185db85Sdougm 			}
1623546405c3Sdougm 		} else {
1624546405c3Sdougm 			if (check_public(group, skipshare))
1625546405c3Sdougm 				return (B_TRUE);
16266185db85Sdougm 		}
16276185db85Sdougm 	}
1628546405c3Sdougm 	return (B_FALSE);
16296185db85Sdougm }
16306185db85Sdougm 
16316185db85Sdougm /*
16326185db85Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
16336185db85Sdougm  * implementation that it is to enable the share. This entails
16346185db85Sdougm  * converting the path and options into the appropriate ioctl
16356185db85Sdougm  * calls. It is assumed that all error checking of paths, etc. were
16366185db85Sdougm  * done earlier.
16376185db85Sdougm  */
16386185db85Sdougm static int
16396185db85Sdougm nfs_enable_share(sa_share_t share)
16406185db85Sdougm {
16416185db85Sdougm 	struct exportdata export;
16426185db85Sdougm 	sa_optionset_t secoptlist;
16436185db85Sdougm 	struct secinfo *sp;
16446185db85Sdougm 	int num_secinfo;
16456185db85Sdougm 	sa_optionset_t opt;
16466185db85Sdougm 	sa_security_t sec;
16476185db85Sdougm 	sa_property_t prop;
16486185db85Sdougm 	char *path;
16496185db85Sdougm 	int err = SA_OK;
1650546405c3Sdougm 	int i;
1651ecd6cf80Smarks 	int iszfs;
16526185db85Sdougm 
16536185db85Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
16546185db85Sdougm 	(void) signal(SIGSYS, SIG_IGN);
16556185db85Sdougm 
16566185db85Sdougm 	/* get the path since it is important in several places */
16576185db85Sdougm 	path = sa_get_share_attr(share, "path");
16586185db85Sdougm 	if (path == NULL)
1659a3351425Sdougm 		return (SA_NO_SUCH_PATH);
16606185db85Sdougm 
1661ecd6cf80Smarks 	iszfs = sa_path_is_zfs(path);
16626185db85Sdougm 	/*
16636185db85Sdougm 	 * find the optionsets and security sets.  There may not be
16646185db85Sdougm 	 * any or there could be one or two for each of optionset and
16656185db85Sdougm 	 * security may have multiple, one per security type per
16666185db85Sdougm 	 * protocol type.
16676185db85Sdougm 	 */
16686185db85Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
16696185db85Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
16706185db85Sdougm 	if (secoptlist != NULL)
1671a3351425Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
16726185db85Sdougm 	else
1673a3351425Sdougm 		num_secinfo = 1;
16746185db85Sdougm 
16756185db85Sdougm 	/*
16766185db85Sdougm 	 * walk through the options and fill in the structure
16776185db85Sdougm 	 * appropriately.
16786185db85Sdougm 	 */
16796185db85Sdougm 
16806185db85Sdougm 	(void) memset(&export, '\0', sizeof (export));
16816185db85Sdougm 
16826185db85Sdougm 	/*
16836185db85Sdougm 	 * do non-security options first since there is only one after
16846185db85Sdougm 	 * the derived group is constructed.
16856185db85Sdougm 	 */
16866185db85Sdougm 	export.ex_version = EX_CURRENT_VERSION;
16876185db85Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
16886185db85Sdougm 	export.ex_index = NULL;
16896185db85Sdougm 	export.ex_path = path;
16906185db85Sdougm 	export.ex_pathlen = strlen(path) + 1;
16916185db85Sdougm 
16926185db85Sdougm 	if (opt != NULL)
1693a3351425Sdougm 		err = fill_export_from_optionset(&export, opt);
16946185db85Sdougm 
16956185db85Sdougm 	/*
16966185db85Sdougm 	 * check to see if "public" is set. If it is, then make sure
16976185db85Sdougm 	 * no other share has it set. If it is already used, fail.
16986185db85Sdougm 	 */
16996185db85Sdougm 
17006185db85Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(share)) {
1701a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1702a3351425Sdougm 		    "NFS: Cannot share more than one file "
1703a3351425Sdougm 		    "system with 'public' property\n"));
1704a3351425Sdougm 		err = SA_NOT_ALLOWED;
1705a3351425Sdougm 		goto out;
17066185db85Sdougm 	}
17076185db85Sdougm 
1708546405c3Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
17096185db85Sdougm 	if (sp == NULL) {
1710a3351425Sdougm 		err = SA_NO_MEMORY;
1711546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1712546405c3Sdougm 		    "NFS: NFS: no memory for security\n"));
1713546405c3Sdougm 		goto out;
1714546405c3Sdougm 	}
1715546405c3Sdougm 	export.ex_secinfo = sp;
1716546405c3Sdougm 	/* get default secinfo */
1717546405c3Sdougm 	export.ex_seccnt = num_secinfo;
1718546405c3Sdougm 	/*
1719546405c3Sdougm 	 * since we must have one security option defined, we
1720546405c3Sdougm 	 * init to the default and then override as we find
1721546405c3Sdougm 	 * defined security options. This handles the case
1722546405c3Sdougm 	 * where we have no defined options but we need to set
1723546405c3Sdougm 	 * up one.
1724546405c3Sdougm 	 */
1725546405c3Sdougm 	sp[0].s_window = DEF_WIN;
1726546405c3Sdougm 	sp[0].s_rootnames = NULL;
1727546405c3Sdougm 	/* setup a default in case no properties defined */
1728546405c3Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1729546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1730546405c3Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
1731546405c3Sdougm 		    "get default security mode\n"));
1732546405c3Sdougm 		err = SA_CONFIG_ERR;
1733546405c3Sdougm 	}
1734546405c3Sdougm 	if (secoptlist != NULL) {
1735546405c3Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
1736546405c3Sdougm 		    prop != NULL && i < num_secinfo;
1737546405c3Sdougm 		    prop = sa_get_next_property(prop), i++) {
1738546405c3Sdougm 			char *sectype;
1739a3351425Sdougm 				sectype = sa_get_property_attr(prop, "type");
1740546405c3Sdougm 			/*
1741546405c3Sdougm 			 * if sectype is NULL, we probably
1742546405c3Sdougm 			 * have a memory problem and can't get
1743546405c3Sdougm 			 * the correct values. Rather than
1744546405c3Sdougm 			 * exporting with incorrect security,
1745546405c3Sdougm 			 * don't share it.
1746546405c3Sdougm 			 */
1747546405c3Sdougm 			if (sectype == NULL) {
1748546405c3Sdougm 				err = SA_NO_MEMORY;
1749546405c3Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1750546405c3Sdougm 				    "NFS: Cannot share %s: "
1751546405c3Sdougm 				    "no memory\n"), path);
1752546405c3Sdougm 				goto out;
1753a3351425Sdougm 			}
1754546405c3Sdougm 			sec = (sa_security_t)sa_get_derived_security(
1755546405c3Sdougm 			    share, sectype, "nfs", 1);
1756546405c3Sdougm 			sp[i].s_window = DEF_WIN;
1757546405c3Sdougm 			sp[i].s_rootcnt = 0;
1758546405c3Sdougm 			sp[i].s_rootnames = NULL;
1759546405c3Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
1760546405c3Sdougm 			if (sec != NULL)
1761546405c3Sdougm 				sa_free_derived_security(sec);
1762546405c3Sdougm 			if (sectype != NULL)
1763546405c3Sdougm 				sa_free_attr_string(sectype);
17646185db85Sdougm 		}
1765546405c3Sdougm 	}
1766546405c3Sdougm 	/*
1767546405c3Sdougm 	 * when we get here, we can do the exportfs system call and
1768546405c3Sdougm 	 * initiate thinsg. We probably want to enable the nfs.server
1769546405c3Sdougm 	 * service first if it isn't running within SMF.
1770546405c3Sdougm 	 */
1771546405c3Sdougm 	/* check nfs.server status and start if needed */
1772546405c3Sdougm 	/* now add the share to the internal tables */
1773546405c3Sdougm 	printarg(path, &export);
1774546405c3Sdougm 	/*
1775546405c3Sdougm 	 * call the exportfs system call which is implemented
1776546405c3Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
1777546405c3Sdougm 	 */
1778ecd6cf80Smarks 	if (iszfs) {
1779ecd6cf80Smarks 		struct exportfs_args ea;
1780ecd6cf80Smarks 		share_t sh;
1781ecd6cf80Smarks 		char *str;
1782ecd6cf80Smarks 		priv_set_t *priv_effective;
1783ecd6cf80Smarks 		int privileged;
1784ecd6cf80Smarks 
1785ecd6cf80Smarks 		/*
1786ecd6cf80Smarks 		 * If we aren't a privileged user
1787ecd6cf80Smarks 		 * and NFS server service isn't running
1788ecd6cf80Smarks 		 * then print out an error message
1789ecd6cf80Smarks 		 * and return EPERM
1790ecd6cf80Smarks 		 */
1791ecd6cf80Smarks 
1792ecd6cf80Smarks 		priv_effective = priv_allocset();
1793ecd6cf80Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
1794ecd6cf80Smarks 
1795ecd6cf80Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
1796ecd6cf80Smarks 		priv_freeset(priv_effective);
1797ecd6cf80Smarks 
1798ecd6cf80Smarks 		if (!privileged &&
1799ecd6cf80Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
1800ecd6cf80Smarks 			err = 0;
1801ecd6cf80Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
1802ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1803ecd6cf80Smarks 				    "NFS: Cannot share remote "
1804ecd6cf80Smarks 				    "filesystem: %s\n"), path);
1805ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1806ecd6cf80Smarks 				    "NFS: Service needs to be enabled "
1807ecd6cf80Smarks 				    "by a privileged user\n"));
1808ecd6cf80Smarks 				err = SA_SYSTEM_ERR;
1809ecd6cf80Smarks 				errno = EPERM;
1810ecd6cf80Smarks 			}
1811ecd6cf80Smarks 			free(str);
1812ecd6cf80Smarks 		}
1813ecd6cf80Smarks 
1814ecd6cf80Smarks 		if (err == 0) {
1815ecd6cf80Smarks 			ea.dname = path;
1816ecd6cf80Smarks 			ea.uex = &export;
1817ecd6cf80Smarks 
1818ecd6cf80Smarks 			sa_sharetab_fill_zfs(share, &sh, "nfs");
1819da6c28aaSamw 			err = sa_share_zfs(share, path, &sh,
1820da6c28aaSamw 			    &ea, ZFS_SHARE_NFS);
1821ecd6cf80Smarks 			sa_emptyshare(&sh);
1822ecd6cf80Smarks 		}
1823ecd6cf80Smarks 	} else {
1824ecd6cf80Smarks 		err = exportfs(path, &export);
1825ecd6cf80Smarks 	}
1826ecd6cf80Smarks 
1827ecd6cf80Smarks 	if (err < 0) {
1828546405c3Sdougm 		err = SA_SYSTEM_ERR;
1829546405c3Sdougm 		switch (errno) {
1830546405c3Sdougm 		case EREMOTE:
1831546405c3Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1832ecd6cf80Smarks 			    "NFS: Cannot share filesystems "
1833ecd6cf80Smarks 			    "in non-global zones: %s\n"), path);
1834ecd6cf80Smarks 			err = SA_NOT_SUPPORTED;
1835546405c3Sdougm 			break;
1836546405c3Sdougm 		case EPERM:
1837546405c3Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
1838a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1839ecd6cf80Smarks 				    "NFS: Cannot share file systems "
1840546405c3Sdougm 				    "in non-global zones: %s\n"), path);
1841546405c3Sdougm 				err = SA_NOT_SUPPORTED;
1842a3351425Sdougm 				break;
1843a3351425Sdougm 			}
1844546405c3Sdougm 			err = SA_NO_PERMISSION;
1845546405c3Sdougm 			/* FALLTHROUGH */
1846546405c3Sdougm 		default:
1847546405c3Sdougm 			break;
18486185db85Sdougm 		}
1849546405c3Sdougm 	} else {
1850546405c3Sdougm 		/* update sharetab with an add/modify */
1851ecd6cf80Smarks 		if (!iszfs) {
1852ecd6cf80Smarks 			(void) sa_update_sharetab(share, "nfs");
1853ecd6cf80Smarks 		}
18546185db85Sdougm 	}
18556185db85Sdougm 
18566185db85Sdougm 	if (err == SA_OK) {
18576185db85Sdougm 		/*
18586185db85Sdougm 		 * enable services as needed. This should probably be
18596185db85Sdougm 		 * done elsewhere in order to minimize the calls to
18606185db85Sdougm 		 * check services.
18616185db85Sdougm 		 */
18626185db85Sdougm 		/*
18636185db85Sdougm 		 * check to see if logging and other services need to
18646185db85Sdougm 		 * be triggered, but only if there wasn't an
18656185db85Sdougm 		 * error. This is probably where sharetab should be
18666185db85Sdougm 		 * updated with the NFS specific entry.
18676185db85Sdougm 		 */
1868a3351425Sdougm 		if (export.ex_flags & EX_LOG) {
1869a3351425Sdougm 			/* enable logging */
1870a3351425Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
1871a3351425Sdougm 			    export.ex_tag) != 0) {
1872a3351425Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1873a3351425Sdougm 				    "Could not enable logging for %s\n"),
1874a3351425Sdougm 				    path);
1875a3351425Sdougm 			}
1876a3351425Sdougm 			_check_services(service_list_logging);
1877a3351425Sdougm 		} else {
1878a3351425Sdougm 			/*
1879a3351425Sdougm 			 * don't have logging so remove it from file. It might
1880a3351425Sdougm 			 * not be thre, but that doesn't matter.
1881a3351425Sdougm 			 */
1882a3351425Sdougm 			(void) nfslogtab_deactivate(path);
1883a3351425Sdougm 			_check_services(service_list_default);
18846185db85Sdougm 		}
18856185db85Sdougm 	}
18866185db85Sdougm 
18876185db85Sdougm out:
18886185db85Sdougm 	if (path != NULL)
1889a3351425Sdougm 		free(path);
18906185db85Sdougm 
18916185db85Sdougm 	cleanup_export(&export);
18926185db85Sdougm 	if (opt != NULL)
1893a3351425Sdougm 		sa_free_derived_optionset(opt);
18946185db85Sdougm 	if (secoptlist != NULL)
1895a3351425Sdougm 		(void) sa_destroy_optionset(secoptlist);
18966185db85Sdougm 	return (err);
18976185db85Sdougm }
18986185db85Sdougm 
18996185db85Sdougm /*
19006185db85Sdougm  * nfs_disable_share(share)
19016185db85Sdougm  *
19026185db85Sdougm  * Unshare the specified share.  How much error checking should be
19036185db85Sdougm  * done? We only do basic errors for now.
19046185db85Sdougm  */
19056185db85Sdougm static int
1906ecd6cf80Smarks nfs_disable_share(sa_share_t share, char *path)
19076185db85Sdougm {
19086185db85Sdougm 	int err;
19096185db85Sdougm 	int ret = SA_OK;
1910ecd6cf80Smarks 	int iszfs;
1911ecd6cf80Smarks 
1912ecd6cf80Smarks 
1913ecd6cf80Smarks 	if (path != NULL) {
1914ecd6cf80Smarks 		iszfs = sa_path_is_zfs(path);
1915ecd6cf80Smarks 
1916ecd6cf80Smarks 		if (iszfs) {
1917ecd6cf80Smarks 			struct exportfs_args ea;
1918ecd6cf80Smarks 			share_t sh = { 0 };
1919ecd6cf80Smarks 
1920ecd6cf80Smarks 			ea.dname = path;
1921ecd6cf80Smarks 			ea.uex = NULL;
1922ecd6cf80Smarks 			sh.sh_path = path;
1923ecd6cf80Smarks 			sh.sh_fstype = "nfs";
19246185db85Sdougm 
1925da6c28aaSamw 			err = sa_share_zfs(share, path, &sh,
1926da6c28aaSamw 			    &ea, ZFS_UNSHARE_NFS);
1927ecd6cf80Smarks 		} else
1928ecd6cf80Smarks 			err = exportfs(path, NULL);
1929a3351425Sdougm 		if (err < 0) {
1930a3351425Sdougm 			/*
1931ecd6cf80Smarks 			 * TBD: only an error in some
1932ecd6cf80Smarks 			 * cases - need better analysis
1933a3351425Sdougm 			 */
1934ecd6cf80Smarks 
1935a3351425Sdougm 			switch (errno) {
1936a3351425Sdougm 			case EPERM:
1937a3351425Sdougm 			case EACCES:
1938a3351425Sdougm 				ret = SA_NO_PERMISSION;
1939ecd6cf80Smarks 				if (getzoneid() != GLOBAL_ZONEID) {
1940a3351425Sdougm 					ret = SA_NOT_SUPPORTED;
1941ecd6cf80Smarks 				}
1942a3351425Sdougm 				break;
1943a3351425Sdougm 			case EINVAL:
1944a3351425Sdougm 			case ENOENT:
1945a3351425Sdougm 				ret = SA_NO_SUCH_PATH;
1946ecd6cf80Smarks 			break;
1947a3351425Sdougm 			default:
1948a3351425Sdougm 				ret = SA_SYSTEM_ERR;
1949ecd6cf80Smarks 			break;
1950a3351425Sdougm 			}
1951a3351425Sdougm 		}
1952a3351425Sdougm 		if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
1953ecd6cf80Smarks 			if (!iszfs)
1954ecd6cf80Smarks 				(void) sa_delete_sharetab(path, "nfs");
1955a3351425Sdougm 			/* just in case it was logged */
1956ecd6cf80Smarks 			(void) nfslogtab_deactivate(path);
19576185db85Sdougm 		}
19586185db85Sdougm 	}
19596185db85Sdougm 	return (ret);
19606185db85Sdougm }
19616185db85Sdougm 
19626185db85Sdougm /*
19636185db85Sdougm  * check ro vs rw values.  Over time this may get beefed up.
19646185db85Sdougm  * for now it just does simple checks.
19656185db85Sdougm  */
19666185db85Sdougm 
19676185db85Sdougm static int
19686185db85Sdougm check_rorw(char *v1, char *v2)
19696185db85Sdougm {
19706185db85Sdougm 	int ret = SA_OK;
19716185db85Sdougm 	if (strcmp(v1, v2) == 0)
1972a3351425Sdougm 		ret = SA_VALUE_CONFLICT;
19736185db85Sdougm 	return (ret);
19746185db85Sdougm }
19756185db85Sdougm 
19766185db85Sdougm /*
19776185db85Sdougm  * nfs_validate_property(property, parent)
19786185db85Sdougm  *
19796185db85Sdougm  * Check that the property has a legitimate value for its type.
19806185db85Sdougm  */
19816185db85Sdougm 
19826185db85Sdougm static int
19836185db85Sdougm nfs_validate_property(sa_property_t property, sa_optionset_t parent)
19846185db85Sdougm {
19856185db85Sdougm 	int ret = SA_OK;
19866185db85Sdougm 	char *propname;
19876185db85Sdougm 	char *other;
19886185db85Sdougm 	int optindex;
19896185db85Sdougm 	nfsl_config_t *configlist;
19906185db85Sdougm 	sa_group_t parent_group;
19916185db85Sdougm 	char *value;
19926185db85Sdougm 
19936185db85Sdougm 	propname = sa_get_property_attr(property, "type");
19946185db85Sdougm 
19956185db85Sdougm 	if ((optindex = findopt(propname)) < 0)
1996a3351425Sdougm 		ret = SA_NO_SUCH_PROP;
19976185db85Sdougm 
19986185db85Sdougm 	/* need to validate value range here as well */
19996185db85Sdougm 
20006185db85Sdougm 	if (ret == SA_OK) {
2001a3351425Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
2002a3351425Sdougm 		if (optdefs[optindex].share && !sa_is_share(parent_group))
2003a3351425Sdougm 			ret = SA_PROP_SHARE_ONLY;
20046185db85Sdougm 	}
20056185db85Sdougm 	if (ret == SA_OK) {
2006a3351425Sdougm 		value = sa_get_property_attr(property, "value");
2007a3351425Sdougm 		if (value != NULL) {
2008a3351425Sdougm 			/* first basic type checking */
2009a3351425Sdougm 			switch (optdefs[optindex].type) {
2010a3351425Sdougm 			case OPT_TYPE_NUMBER:
2011a3351425Sdougm 				/* check that the value is all digits */
2012a3351425Sdougm 				if (!is_a_number(value))
2013a3351425Sdougm 					ret = SA_BAD_VALUE;
2014a3351425Sdougm 				break;
2015a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2016a3351425Sdougm 				if (strlen(value) == 0 ||
2017a3351425Sdougm 				    strcasecmp(value, "true") == 0 ||
2018a3351425Sdougm 				    strcmp(value, "1") == 0 ||
2019a3351425Sdougm 				    strcasecmp(value, "false") == 0 ||
2020a3351425Sdougm 				    strcmp(value, "0") == 0) {
2021a3351425Sdougm 					ret = SA_OK;
2022a3351425Sdougm 				} else {
2023a3351425Sdougm 					ret = SA_BAD_VALUE;
2024a3351425Sdougm 				}
2025a3351425Sdougm 				break;
2026a3351425Sdougm 			case OPT_TYPE_USER:
2027a3351425Sdougm 				if (!is_a_number(value)) {
2028a3351425Sdougm 					struct passwd *pw;
2029a3351425Sdougm 					/*
2030a3351425Sdougm 					 * in this case it would have to be a
2031a3351425Sdougm 					 * user name
2032a3351425Sdougm 					 */
2033a3351425Sdougm 					pw = getpwnam(value);
2034a3351425Sdougm 					if (pw == NULL)
2035a3351425Sdougm 						ret = SA_BAD_VALUE;
2036a3351425Sdougm 					endpwent();
2037a3351425Sdougm 				} else {
2038a3351425Sdougm 					uint64_t intval;
2039a3351425Sdougm 					intval = strtoull(value, NULL, 0);
2040a3351425Sdougm 					if (intval > UID_MAX && intval != ~0)
2041a3351425Sdougm 						ret = SA_BAD_VALUE;
2042a3351425Sdougm 				}
2043a3351425Sdougm 				break;
2044a3351425Sdougm 			case OPT_TYPE_FILE:
2045a3351425Sdougm 				if (strcmp(value, "..") == 0 ||
2046a3351425Sdougm 				    strchr(value, '/') != NULL) {
2047a3351425Sdougm 					ret = SA_BAD_VALUE;
2048a3351425Sdougm 				}
2049a3351425Sdougm 				break;
2050a3351425Sdougm 			case OPT_TYPE_ACCLIST:
2051a3351425Sdougm 				/*
2052a3351425Sdougm 				 * access list handling. Should eventually
2053a3351425Sdougm 				 * validate that all the values make sense.
2054a3351425Sdougm 				 * Also, ro and rw may have cross value
2055a3351425Sdougm 				 * conflicts.
2056a3351425Sdougm 				 */
2057a3351425Sdougm 				if (strcmp(propname, SHOPT_RO) == 0)
2058a3351425Sdougm 					other = SHOPT_RW;
2059a3351425Sdougm 				else if (strcmp(propname, SHOPT_RW) == 0)
2060a3351425Sdougm 					other = SHOPT_RO;
2061a3351425Sdougm 				else
2062a3351425Sdougm 					other = NULL;
2063a3351425Sdougm 
2064a3351425Sdougm 				if (other != NULL && parent != NULL) {
2065a3351425Sdougm 					/* compare rw(ro) with ro(rw) */
2066a3351425Sdougm 					sa_property_t oprop;
2067a3351425Sdougm 					oprop = sa_get_property(parent, other);
2068a3351425Sdougm 					if (oprop != NULL) {
2069a3351425Sdougm 						/*
2070a3351425Sdougm 						 * only potential
2071a3351425Sdougm 						 * confusion if other
2072a3351425Sdougm 						 * exists
2073a3351425Sdougm 						 */
2074a3351425Sdougm 						char *ovalue;
2075a3351425Sdougm 						ovalue = sa_get_property_attr(
2076a3351425Sdougm 						    oprop, "value");
2077a3351425Sdougm 						if (ovalue != NULL) {
2078a3351425Sdougm 							ret = check_rorw(value,
2079a3351425Sdougm 							    ovalue);
2080a3351425Sdougm 							sa_free_attr_string(
2081a3351425Sdougm 							    ovalue);
2082a3351425Sdougm 						}
2083a3351425Sdougm 					}
2084a3351425Sdougm 				}
2085a3351425Sdougm 				break;
2086a3351425Sdougm 			case OPT_TYPE_LOGTAG:
2087a3351425Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
2088a3351425Sdougm 					int error;
2089a3351425Sdougm 					if (value == NULL ||
2090a3351425Sdougm 					    strlen(value) == 0) {
2091a3351425Sdougm 						if (value != NULL)
2092a3351425Sdougm 							sa_free_attr_string(
2093a3351425Sdougm 							    value);
2094a3351425Sdougm 						value = strdup("global");
2095a3351425Sdougm 					}
2096a3351425Sdougm 					if (value != NULL &&
2097a3351425Sdougm 					    nfsl_findconfig(configlist, value,
2098a3351425Sdougm 					    &error) == NULL) {
2099a3351425Sdougm 						ret = SA_BAD_VALUE;
2100a3351425Sdougm 					}
2101aed5d200Sdougm 					/* Must always free when done */
2102aed5d200Sdougm 					nfsl_freeconfig_list(&configlist);
2103a3351425Sdougm 				} else {
2104a3351425Sdougm 					ret = SA_CONFIG_ERR;
2105a3351425Sdougm 				}
2106a3351425Sdougm 				break;
2107a3351425Sdougm 			case OPT_TYPE_STRING:
2108a3351425Sdougm 				/* whatever is here should be ok */
2109a3351425Sdougm 				break;
2110a3351425Sdougm 			case OPT_TYPE_SECURITY:
2111a3351425Sdougm 				/*
2112a3351425Sdougm 				 * The "sec" property isn't used in the
2113a3351425Sdougm 				 * non-legacy parts of sharemgr. We need to
2114a3351425Sdougm 				 * reject it here. For legacy, it is pulled
2115a3351425Sdougm 				 * out well before we get here.
2116a3351425Sdougm 				 */
2117a3351425Sdougm 				ret = SA_NO_SUCH_PROP;
2118a3351425Sdougm 				break;
2119a3351425Sdougm 			default:
2120a3351425Sdougm 				break;
21216185db85Sdougm 			}
2122aed5d200Sdougm 
2123aed5d200Sdougm 			if (value != NULL)
2124aed5d200Sdougm 				sa_free_attr_string(value);
2125aed5d200Sdougm 
2126a3351425Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
2127a3351425Sdougm 				/* do the property specific check */
2128a3351425Sdougm 				ret = optdefs[optindex].check(property);
21296185db85Sdougm 			}
21306185db85Sdougm 		}
21316185db85Sdougm 	}
21326185db85Sdougm 
21336185db85Sdougm 	if (propname != NULL)
2134a3351425Sdougm 		sa_free_attr_string(propname);
21356185db85Sdougm 	return (ret);
21366185db85Sdougm }
21376185db85Sdougm 
21386185db85Sdougm /*
21396185db85Sdougm  * Protocol management functions
21406185db85Sdougm  *
21413472f5dcSdougm  * Properties defined in the default files are defined in
21423472f5dcSdougm  * proto_option_defs for parsing and validation. If "other" and
21433472f5dcSdougm  * "compare" are set, then the value for this property should be
21443472f5dcSdougm  * compared against the property specified in "other" using the
21453472f5dcSdougm  * "compare" check (either <= or >=) in order to ensure that the
21463472f5dcSdougm  * values are in the correct range.  E.g. setting server_versmin
21473472f5dcSdougm  * higher than server_versmax should not be allowed.
21486185db85Sdougm  */
21496185db85Sdougm 
21506185db85Sdougm struct proto_option_defs {
21516185db85Sdougm 	char *tag;
21526185db85Sdougm 	char *name;	/* display name -- remove protocol identifier */
21536185db85Sdougm 	int index;
21546185db85Sdougm 	int type;
21556185db85Sdougm 	union {
21566185db85Sdougm 	    int intval;
21576185db85Sdougm 	    char *string;
21586185db85Sdougm 	} defvalue;
21596185db85Sdougm 	uint32_t svcs;
21606185db85Sdougm 	int32_t minval;
21616185db85Sdougm 	int32_t maxval;
21626185db85Sdougm 	char *file;
21633472f5dcSdougm 	char *other;
21643472f5dcSdougm 	int compare;
21653472f5dcSdougm #define	OPT_CMP_GE	0
21663472f5dcSdougm #define	OPT_CMP_LE	1
21676185db85Sdougm 	int (*check)(char *);
21686185db85Sdougm } proto_options[] = {
21696185db85Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
21706185db85Sdougm 	{"nfsd_servers",
21716185db85Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
21726185db85Sdougm 	    1, INT32_MAX, NFSADMIN},
21736185db85Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
21746185db85Sdougm 	{"lockd_listen_backlog",
21756185db85Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
21766185db85Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
21776185db85Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
21786185db85Sdougm 	{"lockd_servers",
21796185db85Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
21806185db85Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
21816185db85Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
21826185db85Sdougm 	{"lockd_retransmit_timeout",
21836185db85Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
21846185db85Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
21856185db85Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
21866185db85Sdougm 	{"grace_period",
21876185db85Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
21886185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
21896185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
21906185db85Sdougm 	{"nfs_server_versmin",
21916185db85Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
21926185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
21933472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
21946185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
21956185db85Sdougm 	{"nfs_server_versmax",
21966185db85Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
21976185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
21983472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
21996185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
22006185db85Sdougm 	{"nfs_client_versmin",
22016185db85Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
22026185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
22033472f5dcSdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
22046185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
22056185db85Sdougm 	{"nfs_client_versmax",
22066185db85Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
22076185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
22083472f5dcSdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
22096185db85Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
22106185db85Sdougm 	{"nfs_server_delegation",
22116185db85Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
22126185db85Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
22136185db85Sdougm 	    NFSADMIN},
22146185db85Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
22156185db85Sdougm 	{"nfsmapid_domain",
22166185db85Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
22176185db85Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
22186185db85Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
22196185db85Sdougm 	{"nfsd_max_connections",
22206185db85Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
22216185db85Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
22226185db85Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
22236185db85Sdougm 	{"nfsd_protocol",
22246185db85Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
22256185db85Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
22266185db85Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
22276185db85Sdougm 	{"nfsd_listen_backlog",
22286185db85Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
22296185db85Sdougm 	    OPT_TYPE_NUMBER, 0,
22306185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
22316185db85Sdougm 	{NULL}
22326185db85Sdougm };
22336185db85Sdougm 
22346185db85Sdougm /*
22356185db85Sdougm  * the protoset holds the defined options so we don't have to read
22366185db85Sdougm  * them multiple times
22376185db85Sdougm  */
2238aed5d200Sdougm static sa_protocol_properties_t protoset;
22396185db85Sdougm 
22406185db85Sdougm static int
22416185db85Sdougm findprotoopt(char *name, int whichname)
22426185db85Sdougm {
22436185db85Sdougm 	int i;
22446185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2245a3351425Sdougm 		if (whichname == 1) {
2246a3351425Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
22476185db85Sdougm 			return (i);
2248a3351425Sdougm 		} else {
2249a3351425Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
2250a3351425Sdougm 				return (i);
2251a3351425Sdougm 		}
22526185db85Sdougm 	}
22536185db85Sdougm 	return (-1);
22546185db85Sdougm }
22556185db85Sdougm 
22566185db85Sdougm /*
22576185db85Sdougm  * fixcaselower(str)
22586185db85Sdougm  *
22596185db85Sdougm  * convert a string to lower case (inplace).
22606185db85Sdougm  */
22616185db85Sdougm 
22626185db85Sdougm static void
22636185db85Sdougm fixcaselower(char *str)
22646185db85Sdougm {
22656185db85Sdougm 	while (*str) {
2266a3351425Sdougm 		*str = tolower(*str);
2267a3351425Sdougm 		str++;
22686185db85Sdougm 	}
22696185db85Sdougm }
22706185db85Sdougm 
22716185db85Sdougm /*
22726185db85Sdougm  * fixcaseupper(str)
22736185db85Sdougm  *
22746185db85Sdougm  * convert a string to upper case (inplace).
22756185db85Sdougm  */
22766185db85Sdougm 
22776185db85Sdougm static void
22786185db85Sdougm fixcaseupper(char *str)
22796185db85Sdougm {
22806185db85Sdougm 	while (*str) {
2281a3351425Sdougm 		*str = toupper(*str);
2282a3351425Sdougm 		str++;
22836185db85Sdougm 	}
22846185db85Sdougm }
22856185db85Sdougm 
2286330ef417Sdougm /*
2287330ef417Sdougm  * skipwhitespace(str)
2288330ef417Sdougm  *
2289330ef417Sdougm  * Skip leading white space. It is assumed that it is called with a
2290330ef417Sdougm  * valid pointer.
2291330ef417Sdougm  */
2292330ef417Sdougm 
2293330ef417Sdougm static char *
2294330ef417Sdougm skipwhitespace(char *str)
2295330ef417Sdougm {
2296330ef417Sdougm 	while (*str && isspace(*str))
2297330ef417Sdougm 		str++;
2298330ef417Sdougm 
2299330ef417Sdougm 	return (str);
2300330ef417Sdougm }
2301330ef417Sdougm 
2302a3351425Sdougm /*
2303a3351425Sdougm  * extractprop()
2304a3351425Sdougm  *
2305a3351425Sdougm  * Extract the property and value out of the line and create the
2306a3351425Sdougm  * property in the optionset.
2307a3351425Sdougm  */
2308a3351425Sdougm static void
2309a3351425Sdougm extractprop(char *name, char *value)
2310a3351425Sdougm {
2311a3351425Sdougm 	sa_property_t prop;
2312a3351425Sdougm 	int index;
2313a3351425Sdougm 	/*
2314a3351425Sdougm 	 * Remove any leading
2315a3351425Sdougm 	 * white space.
2316a3351425Sdougm 	 */
2317a3351425Sdougm 	name = skipwhitespace(name);
2318a3351425Sdougm 
2319a3351425Sdougm 	index = findprotoopt(name, 0);
2320a3351425Sdougm 	if (index >= 0) {
2321a3351425Sdougm 		fixcaselower(name);
2322a3351425Sdougm 		prop = sa_create_property(proto_options[index].name, value);
2323a3351425Sdougm 		if (prop != NULL)
2324a3351425Sdougm 			(void) sa_add_protocol_property(protoset, prop);
2325a3351425Sdougm 	}
2326a3351425Sdougm }
2327a3351425Sdougm 
23286185db85Sdougm /*
23296185db85Sdougm  * initprotofromdefault()
23306185db85Sdougm  *
23316185db85Sdougm  * read the default file(s) and add the defined values to the
23326185db85Sdougm  * protoset.  Note that default values are known from the built in
23336185db85Sdougm  * table in case the file doesn't have a definition.
23346185db85Sdougm  */
23356185db85Sdougm 
23366185db85Sdougm static int
23376185db85Sdougm initprotofromdefault()
23386185db85Sdougm {
23396185db85Sdougm 	FILE *nfs;
23406185db85Sdougm 	char buff[BUFSIZ];
23416185db85Sdougm 	char *name;
23426185db85Sdougm 	char *value;
23436185db85Sdougm 
23446185db85Sdougm 	protoset = sa_create_protocol_properties("nfs");
23456185db85Sdougm 
23466185db85Sdougm 	if (protoset != NULL) {
2347a3351425Sdougm 		nfs = fopen(NFSADMIN, "r");
2348a3351425Sdougm 		if (nfs != NULL) {
2349a3351425Sdougm 			while (fgets(buff, sizeof (buff), nfs) != NULL) {
2350a3351425Sdougm 				switch (buff[0]) {
2351a3351425Sdougm 				case '\n':
2352a3351425Sdougm 				case '#':
2353a3351425Sdougm 					/* skip */
2354a3351425Sdougm 					break;
2355a3351425Sdougm 				default:
2356a3351425Sdougm 					name = buff;
2357a3351425Sdougm 					buff[strlen(buff) - 1] = '\0';
2358a3351425Sdougm 					value = strchr(name, '=');
2359a3351425Sdougm 					if (value != NULL) {
2360a3351425Sdougm 						*value++ = '\0';
2361a3351425Sdougm 						extractprop(name, value);
2362a3351425Sdougm 					}
2363a3351425Sdougm 				}
23646185db85Sdougm 			}
2365a3351425Sdougm 			if (nfs != NULL)
2366a3351425Sdougm 				(void) fclose(nfs);
23676185db85Sdougm 		}
23686185db85Sdougm 	}
23696185db85Sdougm 	if (protoset == NULL)
2370a3351425Sdougm 		return (SA_NO_MEMORY);
23716185db85Sdougm 	return (SA_OK);
23726185db85Sdougm }
23736185db85Sdougm 
23746185db85Sdougm /*
2375a3351425Sdougm  * add_defaults()
23766185db85Sdougm  *
23776185db85Sdougm  * Add the default values for any property not defined in the parsing
23783472f5dcSdougm  * of the default files. Values are set according to their defined
23793472f5dcSdougm  * types.
23806185db85Sdougm  */
23816185db85Sdougm 
23826185db85Sdougm static void
23836185db85Sdougm add_defaults()
23846185db85Sdougm {
23856185db85Sdougm 	int i;
23866185db85Sdougm 	char number[MAXDIGITS];
23876185db85Sdougm 
23886185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2389a3351425Sdougm 		sa_property_t prop;
2390a3351425Sdougm 		prop = sa_get_protocol_property(protoset,
2391a3351425Sdougm 		    proto_options[i].name);
2392a3351425Sdougm 		if (prop == NULL) {
2393a3351425Sdougm 			/* add the default value */
2394a3351425Sdougm 			switch (proto_options[i].type) {
2395a3351425Sdougm 			case OPT_TYPE_NUMBER:
2396a3351425Sdougm 				(void) snprintf(number, sizeof (number), "%d",
2397a3351425Sdougm 				    proto_options[i].defvalue.intval);
2398a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2399a3351425Sdougm 				    number);
2400a3351425Sdougm 				break;
2401a3351425Sdougm 
2402a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2403a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2404a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2405a3351425Sdougm 				    "true" : "false");
2406a3351425Sdougm 				break;
2407a3351425Sdougm 
2408a3351425Sdougm 			case OPT_TYPE_ONOFF:
2409a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2410a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2411a3351425Sdougm 				    "on" : "off");
2412a3351425Sdougm 				break;
2413a3351425Sdougm 
2414a3351425Sdougm 			default:
2415a3351425Sdougm 				/* treat as strings of zero length */
2416a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2417a3351425Sdougm 				    "");
2418a3351425Sdougm 				break;
2419a3351425Sdougm 			}
2420a3351425Sdougm 			if (prop != NULL)
2421a3351425Sdougm 				(void) sa_add_protocol_property(protoset, prop);
24226185db85Sdougm 		}
24236185db85Sdougm 	}
24246185db85Sdougm }
24256185db85Sdougm 
24266185db85Sdougm static void
24276185db85Sdougm free_protoprops()
24286185db85Sdougm {
2429aed5d200Sdougm 	if (protoset != NULL) {
2430aed5d200Sdougm 		xmlFreeNode(protoset);
2431aed5d200Sdougm 		protoset = NULL;
2432aed5d200Sdougm 	}
24336185db85Sdougm }
24346185db85Sdougm 
24356185db85Sdougm /*
24366185db85Sdougm  * nfs_init()
24376185db85Sdougm  *
24386185db85Sdougm  * Initialize the NFS plugin.
24396185db85Sdougm  */
24406185db85Sdougm 
24416185db85Sdougm static int
24426185db85Sdougm nfs_init()
24436185db85Sdougm {
24446185db85Sdougm 	int ret = SA_OK;
24456185db85Sdougm 
24466185db85Sdougm 	if (sa_plugin_ops.sa_init != nfs_init)
2447a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2448a3351425Sdougm 		    "NFS plugin not properly initialized\n"));
24496185db85Sdougm 
24506185db85Sdougm 	ret = initprotofromdefault();
2451a3351425Sdougm 	if (ret == SA_OK)
2452a3351425Sdougm 		add_defaults();
24536185db85Sdougm 
24546185db85Sdougm 	return (ret);
24556185db85Sdougm }
24566185db85Sdougm 
24576185db85Sdougm /*
24586185db85Sdougm  * nfs_fini()
24596185db85Sdougm  *
24606185db85Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
24616185db85Sdougm  */
24626185db85Sdougm 
24636185db85Sdougm static void
24646185db85Sdougm nfs_fini()
24656185db85Sdougm {
24666185db85Sdougm 	free_protoprops();
24676185db85Sdougm }
24686185db85Sdougm 
24696185db85Sdougm /*
24706185db85Sdougm  * nfs_get_proto_set()
24716185db85Sdougm  *
24726185db85Sdougm  * Return an optionset with all the protocol specific properties in
24736185db85Sdougm  * it.
24746185db85Sdougm  */
24756185db85Sdougm 
24766185db85Sdougm static sa_protocol_properties_t
24776185db85Sdougm nfs_get_proto_set()
24786185db85Sdougm {
24796185db85Sdougm 	return (protoset);
24806185db85Sdougm }
24816185db85Sdougm 
24826185db85Sdougm struct deffile {
24836185db85Sdougm 	struct deffile *next;
24846185db85Sdougm 	char *line;
24856185db85Sdougm };
24866185db85Sdougm 
24876185db85Sdougm /*
24886185db85Sdougm  * read_default_file(fname)
24896185db85Sdougm  *
24906185db85Sdougm  * Read the specified default file. We return a list of entries. This
24916185db85Sdougm  * get used for adding or removing values.
24926185db85Sdougm  */
24936185db85Sdougm 
24946185db85Sdougm static struct deffile *
24956185db85Sdougm read_default_file(char *fname)
24966185db85Sdougm {
24976185db85Sdougm 	FILE *file;
24986185db85Sdougm 	struct deffile *defs = NULL;
24996185db85Sdougm 	struct deffile *newdef;
25006185db85Sdougm 	struct deffile *prevdef = NULL;
25016185db85Sdougm 	char buff[BUFSIZ * 2];
25026185db85Sdougm 
25036185db85Sdougm 	file = fopen(fname, "r");
25046185db85Sdougm 	if (file != NULL) {
2505a3351425Sdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
2506a3351425Sdougm 			newdef = (struct deffile *)calloc(1,
2507a3351425Sdougm 			    sizeof (struct deffile));
2508a3351425Sdougm 			if (newdef != NULL) {
2509a3351425Sdougm 				/* Make sure we skip any leading whitespace. */
2510a3351425Sdougm 				newdef->line = strdup(skipwhitespace(buff));
2511a3351425Sdougm 				if (defs == NULL) {
2512a3351425Sdougm 					prevdef = defs = newdef;
2513a3351425Sdougm 				} else {
2514a3351425Sdougm 					prevdef->next = newdef;
2515a3351425Sdougm 					prevdef = newdef;
2516a3351425Sdougm 				}
2517a3351425Sdougm 			}
25186185db85Sdougm 		}
25196185db85Sdougm 	}
25206185db85Sdougm 	(void) fclose(file);
25216185db85Sdougm 	return (defs);
25226185db85Sdougm }
25236185db85Sdougm 
25246185db85Sdougm static void
25256185db85Sdougm free_default_file(struct deffile *defs)
25266185db85Sdougm {
25276185db85Sdougm 	struct deffile *curdefs = NULL;
25286185db85Sdougm 
25296185db85Sdougm 	while (defs != NULL) {
2530a3351425Sdougm 		curdefs = defs;
2531a3351425Sdougm 		defs = defs->next;
2532a3351425Sdougm 		if (curdefs->line != NULL)
2533a3351425Sdougm 			free(curdefs->line);
2534a3351425Sdougm 		free(curdefs);
25356185db85Sdougm 	}
25366185db85Sdougm }
25376185db85Sdougm 
25386185db85Sdougm /*
25396185db85Sdougm  * write_default_file(fname, defs)
25406185db85Sdougm  *
25416185db85Sdougm  * Write the default file back.
25426185db85Sdougm  */
25436185db85Sdougm 
25446185db85Sdougm static int
25456185db85Sdougm write_default_file(char *fname, struct deffile *defs)
25466185db85Sdougm {
25476185db85Sdougm 	FILE *file;
25486185db85Sdougm 	int ret = SA_OK;
25496185db85Sdougm 	sigset_t old, new;
25506185db85Sdougm 
25516185db85Sdougm 	file = fopen(fname, "w+");
25526185db85Sdougm 	if (file != NULL) {
2553a3351425Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
2554a3351425Sdougm 		(void) sigaddset(&new, SIGHUP);
2555a3351425Sdougm 		(void) sigaddset(&new, SIGINT);
2556a3351425Sdougm 		(void) sigaddset(&new, SIGQUIT);
2557a3351425Sdougm 		(void) sigaddset(&new, SIGTSTP);
2558a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
2559a3351425Sdougm 		while (defs != NULL) {
2560a3351425Sdougm 			(void) fputs(defs->line, file);
2561a3351425Sdougm 			defs = defs->next;
2562a3351425Sdougm 		}
2563a3351425Sdougm 		(void) fsync(fileno(file));
2564a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
2565a3351425Sdougm 		(void) fclose(file);
25666185db85Sdougm 	} else {
2567a3351425Sdougm 		switch (errno) {
2568a3351425Sdougm 		case EPERM:
2569a3351425Sdougm 		case EACCES:
2570a3351425Sdougm 			ret = SA_NO_PERMISSION;
2571a3351425Sdougm 			break;
2572a3351425Sdougm 		default:
2573a3351425Sdougm 			ret = SA_SYSTEM_ERR;
2574a3351425Sdougm 		}
25756185db85Sdougm 	}
25766185db85Sdougm 	return (ret);
25776185db85Sdougm }
25786185db85Sdougm 
25796185db85Sdougm 
25806185db85Sdougm /*
25816185db85Sdougm  * set_default_file_value(tag, value)
25826185db85Sdougm  *
25836185db85Sdougm  * Set the default file value for tag to value. Then rewrite the file.
25846185db85Sdougm  * tag and value are always set.  The caller must ensure this.
25856185db85Sdougm  */
25866185db85Sdougm 
25876185db85Sdougm #define	MAX_STRING_LENGTH	256
25886185db85Sdougm static int
25896185db85Sdougm set_default_file_value(char *tag, char *value)
25906185db85Sdougm {
25916185db85Sdougm 	int ret = SA_OK;
25926185db85Sdougm 	struct deffile *root;
25936185db85Sdougm 	struct deffile *defs;
25946185db85Sdougm 	struct deffile *prev;
25956185db85Sdougm 	char string[MAX_STRING_LENGTH];
25966185db85Sdougm 	int len;
25976185db85Sdougm 	int update = 0;
25986185db85Sdougm 
25996185db85Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
26006185db85Sdougm 	len = strlen(string);
26016185db85Sdougm 
26026185db85Sdougm 	root = defs = read_default_file(NFSADMIN);
26036185db85Sdougm 	if (root == NULL) {
2604a3351425Sdougm 		if (errno == EPERM || errno == EACCES)
2605a3351425Sdougm 			ret = SA_NO_PERMISSION;
2606a3351425Sdougm 		else
2607a3351425Sdougm 			ret = SA_SYSTEM_ERR;
26086185db85Sdougm 	} else {
26096185db85Sdougm 		while (defs != NULL) {
2610a3351425Sdougm 			if (defs->line != NULL &&
2611a3351425Sdougm 			    strncasecmp(defs->line, string, len) == 0) {
2612a3351425Sdougm 				/* replace with the new value */
2613a3351425Sdougm 				free(defs->line);
2614a3351425Sdougm 				fixcaseupper(tag);
2615a3351425Sdougm 				(void) snprintf(string, sizeof (string),
26166185db85Sdougm 				    "%s=%s\n", tag, value);
2617a3351425Sdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
2618a3351425Sdougm 				defs->line = strdup(string);
2619a3351425Sdougm 				update = 1;
2620a3351425Sdougm 				break;
2621a3351425Sdougm 			}
2622a3351425Sdougm 			defs = defs->next;
2623a3351425Sdougm 		}
2624a3351425Sdougm 		if (!update) {
2625a3351425Sdougm 			defs = root;
2626a3351425Sdougm 			/* didn't find, so see if it is a comment */
2627a3351425Sdougm 			(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
2628a3351425Sdougm 			len = strlen(string);
2629a3351425Sdougm 			while (defs != NULL) {
2630a3351425Sdougm 				if (strncasecmp(defs->line, string, len) == 0) {
2631a3351425Sdougm 					/* replace with the new value */
2632a3351425Sdougm 					free(defs->line);
2633a3351425Sdougm 					fixcaseupper(tag);
2634a3351425Sdougm 					(void) snprintf(string, sizeof (string),
2635a3351425Sdougm 					    "%s=%s\n", tag, value);
2636a3351425Sdougm 					string[MAX_STRING_LENGTH - 1] = '\0';
2637a3351425Sdougm 					defs->line = strdup(string);
2638a3351425Sdougm 					update = 1;
2639a3351425Sdougm 					break;
2640a3351425Sdougm 				}
2641a3351425Sdougm 				defs = defs->next;
2642a3351425Sdougm 			}
26436185db85Sdougm 		}
2644a3351425Sdougm 		if (!update) {
2645a3351425Sdougm 			fixcaseupper(tag);
2646a3351425Sdougm 			(void) snprintf(string, sizeof (string), "%s=%s\n",
2647a3351425Sdougm 			    tag, value);
2648a3351425Sdougm 			prev = root;
2649a3351425Sdougm 			while (prev->next != NULL)
2650a3351425Sdougm 				prev = prev->next;
2651a3351425Sdougm 			defs = malloc(sizeof (struct deffile));
2652a3351425Sdougm 			prev->next = defs;
2653a3351425Sdougm 			if (defs != NULL) {
2654a3351425Sdougm 				defs->next = NULL;
2655a3351425Sdougm 				defs->line = strdup(string);
2656a3351425Sdougm 			}
26576185db85Sdougm 		}
2658a3351425Sdougm 		if (update) {
2659a3351425Sdougm 			ret = write_default_file(NFSADMIN, root);
2660a3351425Sdougm 		}
2661a3351425Sdougm 		free_default_file(root);
26626185db85Sdougm 	}
26636185db85Sdougm 	return (ret);
26646185db85Sdougm }
26656185db85Sdougm 
26663472f5dcSdougm /*
26673472f5dcSdougm  * service_in_state(service, chkstate)
26683472f5dcSdougm  *
26693472f5dcSdougm  * Want to know if the specified service is in the desired state
26703472f5dcSdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
26713472f5dcSdougm  * isn't.
26723472f5dcSdougm  */
26733472f5dcSdougm static int
26743472f5dcSdougm service_in_state(char *service, const char *chkstate)
26753472f5dcSdougm {
26763472f5dcSdougm 	char *state;
26773472f5dcSdougm 	int ret = B_FALSE;
26783472f5dcSdougm 
26793472f5dcSdougm 	state = smf_get_state(service);
26803472f5dcSdougm 	if (state != NULL) {
2681a3351425Sdougm 		/* got the state so get the equality for the return value */
2682a3351425Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2683a3351425Sdougm 		free(state);
26843472f5dcSdougm 	}
26853472f5dcSdougm 	return (ret);
26863472f5dcSdougm }
26873472f5dcSdougm 
26886185db85Sdougm /*
26896185db85Sdougm  * restart_service(svcs)
26906185db85Sdougm  *
26916185db85Sdougm  * Walk through the bit mask of services that need to be restarted in
26926185db85Sdougm  * order to use the new property values. Some properties affect
26933472f5dcSdougm  * multiple daemons. Should only restart a service if it is currently
26943472f5dcSdougm  * enabled (online).
26956185db85Sdougm  */
26966185db85Sdougm 
26976185db85Sdougm static void
26986185db85Sdougm restart_service(uint32_t svcs)
26996185db85Sdougm {
27006185db85Sdougm 	uint32_t mask;
27013472f5dcSdougm 	int ret;
27023472f5dcSdougm 	char *service;
27033472f5dcSdougm 
27046185db85Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
2705a3351425Sdougm 		switch (svcs & mask) {
2706a3351425Sdougm 		case SVC_LOCKD:
2707a3351425Sdougm 			service = LOCKD;
2708a3351425Sdougm 			break;
2709a3351425Sdougm 		case SVC_STATD:
2710a3351425Sdougm 			service = STATD;
2711a3351425Sdougm 			break;
2712a3351425Sdougm 		case SVC_NFSD:
2713a3351425Sdougm 			service = NFSD;
2714a3351425Sdougm 			break;
2715a3351425Sdougm 		case SVC_MOUNTD:
2716a3351425Sdougm 			service = MOUNTD;
2717a3351425Sdougm 			break;
2718a3351425Sdougm 		case SVC_NFS4CBD:
2719a3351425Sdougm 			service = NFS4CBD;
2720a3351425Sdougm 			break;
2721a3351425Sdougm 		case SVC_NFSMAPID:
2722a3351425Sdougm 			service = NFSMAPID;
2723a3351425Sdougm 			break;
2724a3351425Sdougm 		case SVC_RQUOTAD:
2725a3351425Sdougm 			service = RQUOTAD;
2726a3351425Sdougm 			break;
2727a3351425Sdougm 		case SVC_NFSLOGD:
2728a3351425Sdougm 			service = NFSLOGD;
2729a3351425Sdougm 			break;
2730a3351425Sdougm 		default:
2731a3351425Sdougm 			continue;
2732a3351425Sdougm 		}
27333472f5dcSdougm 
27343472f5dcSdougm 		/*
27353472f5dcSdougm 		 * Only attempt to restart the service if it is
27363472f5dcSdougm 		 * currently running. In the future, it may be
27373472f5dcSdougm 		 * desirable to use smf_refresh_instance if the NFS
27383472f5dcSdougm 		 * services ever implement the refresh method.
27393472f5dcSdougm 		 */
2740a3351425Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2741a3351425Sdougm 			ret = smf_restart_instance(service);
27423472f5dcSdougm 			/*
2743a3351425Sdougm 			 * There are only a few SMF errors at this point, but
2744a3351425Sdougm 			 * it is also possible that a bad value may have put
2745a3351425Sdougm 			 * the service into maintenance if there wasn't an
2746a3351425Sdougm 			 * SMF level error.
27473472f5dcSdougm 			 */
2748a3351425Sdougm 			if (ret != 0) {
2749a3351425Sdougm 				(void) fprintf(stderr,
2750a3351425Sdougm 				    dgettext(TEXT_DOMAIN,
2751a3351425Sdougm 				    "%s failed to restart: %s\n"),
2752a3351425Sdougm 				    scf_strerror(scf_error()));
2753a3351425Sdougm 			} else {
2754a3351425Sdougm 				/*
2755a3351425Sdougm 				 * Check whether it has gone to "maintenance"
2756a3351425Sdougm 				 * mode or not. Maintenance implies something
2757a3351425Sdougm 				 * went wrong.
2758a3351425Sdougm 				 */
2759a3351425Sdougm 				if (service_in_state(service,
2760a3351425Sdougm 				    SCF_STATE_STRING_MAINT)) {
2761a3351425Sdougm 					(void) fprintf(stderr,
2762a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
2763a3351425Sdougm 					    "%s failed to restart\n"),
2764a3351425Sdougm 					    service);
2765a3351425Sdougm 				}
2766a3351425Sdougm 			}
27673472f5dcSdougm 		}
2768a3351425Sdougm 		svcs &= ~mask;
27696185db85Sdougm 	}
27706185db85Sdougm }
27716185db85Sdougm 
27723472f5dcSdougm /*
27733472f5dcSdougm  * nfs_minmax_check(name, value)
27743472f5dcSdougm  *
27753472f5dcSdougm  * Verify that the value for the property specified by index is valid
27763472f5dcSdougm  * relative to the opposite value in the case of a min/max variable.
27773472f5dcSdougm  * Currently, server_minvers/server_maxvers and
27783472f5dcSdougm  * client_minvers/client_maxvers are the only ones to check.
27793472f5dcSdougm  */
27803472f5dcSdougm 
27813472f5dcSdougm static int
27823472f5dcSdougm nfs_minmax_check(int index, int value)
27833472f5dcSdougm {
27843472f5dcSdougm 	int val;
27853472f5dcSdougm 	char *pval;
27863472f5dcSdougm 	sa_property_t prop;
27873472f5dcSdougm 	sa_optionset_t opts;
27883472f5dcSdougm 	int ret = B_TRUE;
27893472f5dcSdougm 
27903472f5dcSdougm 	if (proto_options[index].other != NULL) {
2791a3351425Sdougm 		/* have a property to compare against */
2792a3351425Sdougm 		opts = nfs_get_proto_set();
2793a3351425Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
27943472f5dcSdougm 		/*
27953472f5dcSdougm 		 * If we don't find the property, assume default
27963472f5dcSdougm 		 * values which will work since the max will be at the
27973472f5dcSdougm 		 * max and the min at the min.
27983472f5dcSdougm 		 */
2799a3351425Sdougm 		if (prop != NULL) {
2800a3351425Sdougm 			pval = sa_get_property_attr(prop, "value");
2801a3351425Sdougm 			if (pval != NULL) {
2802a3351425Sdougm 				val = strtoul(pval, NULL, 0);
2803a3351425Sdougm 				if (proto_options[index].compare ==
2804a3351425Sdougm 				    OPT_CMP_LE) {
2805a3351425Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
2806a3351425Sdougm 				} else if (proto_options[index].compare ==
2807a3351425Sdougm 				    OPT_CMP_GE) {
2808a3351425Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
2809a3351425Sdougm 				}
2810a3351425Sdougm 			}
28113472f5dcSdougm 		}
28123472f5dcSdougm 	}
28133472f5dcSdougm 	return (ret);
28143472f5dcSdougm }
28153472f5dcSdougm 
28166185db85Sdougm /*
28176185db85Sdougm  * nfs_validate_proto_prop(index, name, value)
28186185db85Sdougm  *
2819da6c28aaSamw  * Verify that the property specified by name can take the new
28206185db85Sdougm  * value. This is a sanity check to prevent bad values getting into
28213472f5dcSdougm  * the default files. All values need to be checked against what is
28223472f5dcSdougm  * allowed by their defined type. If a type isn't explicitly defined
28233472f5dcSdougm  * here, it is treated as a string.
28243472f5dcSdougm  *
28253472f5dcSdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
28263472f5dcSdougm  * within the range specified and potentially against another property
28273472f5dcSdougm  * value as well as specified in the proto_options members other and
28283472f5dcSdougm  * compare.
28296185db85Sdougm  */
28306185db85Sdougm 
28316185db85Sdougm static int
28326185db85Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
28336185db85Sdougm {
28346185db85Sdougm 	int ret = SA_OK;
28356185db85Sdougm 	char *cp;
28366185db85Sdougm #ifdef lint
28376185db85Sdougm 	name = name;
28386185db85Sdougm #endif
28396185db85Sdougm 
28406185db85Sdougm 	switch (proto_options[index].type) {
28416185db85Sdougm 	case OPT_TYPE_NUMBER:
2842a3351425Sdougm 		if (!is_a_number(value))
2843a3351425Sdougm 			ret = SA_BAD_VALUE;
2844a3351425Sdougm 		else {
2845a3351425Sdougm 			int val;
2846a3351425Sdougm 			val = strtoul(value, NULL, 0);
2847a3351425Sdougm 			if (val < proto_options[index].minval ||
2848a3351425Sdougm 			    val > proto_options[index].maxval)
2849a3351425Sdougm 				ret = SA_BAD_VALUE;
2850a3351425Sdougm 			/*
2851a3351425Sdougm 			 * For server_versmin/server_versmax and
2852a3351425Sdougm 			 * client_versmin/client_versmax, the value of the
2853a3351425Sdougm 			 * min(max) should be checked to be correct relative
2854a3351425Sdougm 			 * to the current max(min).
2855a3351425Sdougm 			 */
2856a3351425Sdougm 			if (!nfs_minmax_check(index, val)) {
2857a3351425Sdougm 				ret = SA_BAD_VALUE;
2858a3351425Sdougm 			}
28593472f5dcSdougm 		}
2860a3351425Sdougm 		break;
28613472f5dcSdougm 
28626185db85Sdougm 	case OPT_TYPE_DOMAIN:
28636185db85Sdougm 		/*
28643472f5dcSdougm 		 * needs to be a qualified domain so will have at
28653472f5dcSdougm 		 * least one period and other characters on either
28663472f5dcSdougm 		 * side of it.  A zero length string is also allowed
28673472f5dcSdougm 		 * and is the way to turn off the override.
28686185db85Sdougm 		 */
2869a3351425Sdougm 		if (strlen(value) == 0)
2870a3351425Sdougm 			break;
2871a3351425Sdougm 		cp = strchr(value, '.');
2872a3351425Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
2873a3351425Sdougm 			ret = SA_BAD_VALUE;
28743472f5dcSdougm 		break;
28753472f5dcSdougm 
28766185db85Sdougm 	case OPT_TYPE_BOOLEAN:
2877a3351425Sdougm 		if (strlen(value) == 0 ||
2878a3351425Sdougm 		    strcasecmp(value, "true") == 0 ||
2879a3351425Sdougm 		    strcmp(value, "1") == 0 ||
2880a3351425Sdougm 		    strcasecmp(value, "false") == 0 ||
2881a3351425Sdougm 		    strcmp(value, "0") == 0) {
2882a3351425Sdougm 			ret = SA_OK;
2883a3351425Sdougm 		} else {
2884a3351425Sdougm 			ret = SA_BAD_VALUE;
2885a3351425Sdougm 		}
2886a3351425Sdougm 		break;
28873472f5dcSdougm 
28886185db85Sdougm 	case OPT_TYPE_ONOFF:
2889a3351425Sdougm 		if (strcasecmp(value, "on") != 0 &&
2890a3351425Sdougm 		    strcasecmp(value, "off") != 0) {
2891a3351425Sdougm 			ret = SA_BAD_VALUE;
2892a3351425Sdougm 		}
2893a3351425Sdougm 		break;
28943472f5dcSdougm 
28956185db85Sdougm 	case OPT_TYPE_PROTOCOL:
2896a3351425Sdougm 		if (strcasecmp(value, "all") != 0 &&
2897a3351425Sdougm 		    strcasecmp(value, "tcp") != 0 &&
2898a3351425Sdougm 		    strcasecmp(value, "udp") != 0)
2899a3351425Sdougm 			ret = SA_BAD_VALUE;
2900a3351425Sdougm 		break;
29013472f5dcSdougm 
29023472f5dcSdougm 	default:
2903a3351425Sdougm 		/* treat as a string */
2904a3351425Sdougm 		break;
29056185db85Sdougm 	}
29066185db85Sdougm 	return (ret);
29076185db85Sdougm }
29086185db85Sdougm 
29096185db85Sdougm /*
29106185db85Sdougm  * nfs_set_proto_prop(prop)
29116185db85Sdougm  *
29126185db85Sdougm  * check that prop is valid.
29136185db85Sdougm  */
29146185db85Sdougm 
29156185db85Sdougm static int
29166185db85Sdougm nfs_set_proto_prop(sa_property_t prop)
29176185db85Sdougm {
29186185db85Sdougm 	int ret = SA_OK;
29196185db85Sdougm 	char *name;
29206185db85Sdougm 	char *value;
29216185db85Sdougm 
29226185db85Sdougm 	name = sa_get_property_attr(prop, "type");
29236185db85Sdougm 	value = sa_get_property_attr(prop, "value");
29246185db85Sdougm 	if (name != NULL && value != NULL) {
2925a3351425Sdougm 		int index = findprotoopt(name, 1);
2926a3351425Sdougm 		if (index >= 0) {
2927a3351425Sdougm 			/* should test for valid value */
2928a3351425Sdougm 			ret = nfs_validate_proto_prop(index, name, value);
2929a3351425Sdougm 			if (ret == SA_OK)
2930a3351425Sdougm 				ret = set_default_file_value(
2931a3351425Sdougm 				    proto_options[index].tag, value);
2932a3351425Sdougm 			if (ret == SA_OK)
2933a3351425Sdougm 				restart_service(proto_options[index].svcs);
2934a3351425Sdougm 		}
29356185db85Sdougm 	}
29366185db85Sdougm 	if (name != NULL)
2937a3351425Sdougm 		sa_free_attr_string(name);
29386185db85Sdougm 	if (value != NULL)
2939a3351425Sdougm 		sa_free_attr_string(value);
29406185db85Sdougm 	return (ret);
29416185db85Sdougm }
29426185db85Sdougm 
29436185db85Sdougm /*
29446185db85Sdougm  * nfs_get_status()
29456185db85Sdougm  *
29466185db85Sdougm  * What is the current status of the nfsd? We use the SMF state here.
29476185db85Sdougm  * Caller must free the returned value.
29486185db85Sdougm  */
29496185db85Sdougm 
29506185db85Sdougm static char *
29516185db85Sdougm nfs_get_status()
29526185db85Sdougm {
29536185db85Sdougm 	char *state;
29546185db85Sdougm 	state = smf_get_state(NFSD);
29556185db85Sdougm 	return (state != NULL ? state : strdup("-"));
29566185db85Sdougm }
29576185db85Sdougm 
29586185db85Sdougm /*
29596185db85Sdougm  * nfs_space_alias(alias)
29606185db85Sdougm  *
29616185db85Sdougm  * Lookup the space (security) name. If it is default, convert to the
29626185db85Sdougm  * real name.
29636185db85Sdougm  */
29646185db85Sdougm 
29656185db85Sdougm static char *
29666185db85Sdougm nfs_space_alias(char *space)
29676185db85Sdougm {
29686185db85Sdougm 	char *name = space;
29696185db85Sdougm 	seconfig_t secconf;
2970a99982a7Sdougm 
2971a99982a7Sdougm 	/*
2972a99982a7Sdougm 	 * Only the space named "default" is special. If it is used,
2973a99982a7Sdougm 	 * the default needs to be looked up and the real name used.
2974a99982a7Sdougm 	 * This is normally "sys" but could be changed.  We always
2975a99982a7Sdougm 	 * change defautl to the real name.
2976a99982a7Sdougm 	 */
2977a99982a7Sdougm 	if (strcmp(space, "default") == 0 &&
2978a99982a7Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
2979a3351425Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
2980a3351425Sdougm 			name = secconf.sc_name;
29816185db85Sdougm 	}
29826185db85Sdougm 	return (strdup(name));
29836185db85Sdougm }
2984da6c28aaSamw 
2985da6c28aaSamw /*
2986da6c28aaSamw  * nfs_features()
2987da6c28aaSamw  *
2988da6c28aaSamw  * Return a mask of the features required.
2989da6c28aaSamw  */
2990da6c28aaSamw 
2991da6c28aaSamw static uint64_t
2992da6c28aaSamw nfs_features()
2993da6c28aaSamw {
2994da6c28aaSamw 	return ((uint64_t)SA_FEATURE_DFSTAB);
2995da6c28aaSamw }
2996