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 /*
238e314a44Sdougm  * Copyright 2008 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 */
107*4bff34e3Sthurlow 	NULL,	/* rename_resource */
108*4bff34e3Sthurlow 	NULL,	/* run_command */
109*4bff34e3Sthurlow 	NULL,	/* command_help */
110*4bff34e3Sthurlow 	NULL	/* delete_proto_section */
1116185db85Sdougm };
1126185db85Sdougm 
1136185db85Sdougm /*
1146185db85Sdougm  * list of support services needed
1156185db85Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
1166185db85Sdougm  */
1176185db85Sdougm 
1186185db85Sdougm static char *service_list_default[] =
1196185db85Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL };
1206185db85Sdougm static char *service_list_logging[] =
1216185db85Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL };
1226185db85Sdougm 
1236185db85Sdougm /*
1246185db85Sdougm  * option definitions.  Make sure to keep the #define for the option
1256185db85Sdougm  * index just before the entry it is the index for. Changing the order
1266185db85Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1276185db85Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1286185db85Sdougm  */
1296185db85Sdougm 
1306185db85Sdougm struct option_defs optdefs[] = {
1316185db85Sdougm #define	OPT_RO		0
1326185db85Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1336185db85Sdougm #define	OPT_RW		1
1346185db85Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1356185db85Sdougm #define	OPT_ROOT	2
1366185db85Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1376185db85Sdougm #define	OPT_SECURE	3
1386185db85Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1396185db85Sdougm #define	OPT_ANON	4
1406185db85Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1416185db85Sdougm #define	OPT_WINDOW	5
1426185db85Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1436185db85Sdougm #define	OPT_NOSUID	6
1446185db85Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1456185db85Sdougm #define	OPT_ACLOK	7
1466185db85Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1476185db85Sdougm #define	OPT_NOSUB	8
1486185db85Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1496185db85Sdougm #define	OPT_SEC		9
1506185db85Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1516185db85Sdougm #define	OPT_PUBLIC	10
1526185db85Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1536185db85Sdougm #define	OPT_INDEX	11
1546185db85Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1556185db85Sdougm #define	OPT_LOG		12
1566185db85Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1576185db85Sdougm #define	OPT_CKSUM	13
1586185db85Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
1596185db85Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
1606185db85Sdougm #define	OPT_VOLFH	14
1616185db85Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1626185db85Sdougm #endif /* VOLATILE_FH_TEST */
1636185db85Sdougm 	NULL
1646185db85Sdougm };
1656185db85Sdougm 
1666185db85Sdougm /*
167a99982a7Sdougm  * list of properties that are related to security flavors.
1686185db85Sdougm  */
1696185db85Sdougm static char *seclist[] = {
1706185db85Sdougm 	SHOPT_RO,
1716185db85Sdougm 	SHOPT_RW,
1726185db85Sdougm 	SHOPT_ROOT,
1736185db85Sdougm 	SHOPT_WINDOW,
1746185db85Sdougm 	NULL
1756185db85Sdougm };
1766185db85Sdougm 
1776185db85Sdougm /* structure for list of securities */
1786185db85Sdougm struct securities {
179a99982a7Sdougm 	sa_security_t security;
180a99982a7Sdougm 	struct securities *next;
1816185db85Sdougm };
1826185db85Sdougm 
1836185db85Sdougm /*
1846185db85Sdougm  * findopt(name)
1856185db85Sdougm  *
1866185db85Sdougm  * Lookup option "name" in the option table and return the table
1876185db85Sdougm  * index.
1886185db85Sdougm  */
1896185db85Sdougm 
1906185db85Sdougm static int
1916185db85Sdougm findopt(char *name)
1926185db85Sdougm {
1936185db85Sdougm 	int i;
1946185db85Sdougm 	if (name != NULL) {
195a3351425Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
196a3351425Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
197a3351425Sdougm 				return (i);
198a3351425Sdougm 		}
1996185db85Sdougm 	}
2006185db85Sdougm 	return (-1);
2016185db85Sdougm }
2026185db85Sdougm 
2036185db85Sdougm /*
2046185db85Sdougm  * gettype(name)
2056185db85Sdougm  *
2066185db85Sdougm  * Return the type of option "name".
2076185db85Sdougm  */
2086185db85Sdougm 
2096185db85Sdougm static int
2106185db85Sdougm gettype(char *name)
2116185db85Sdougm {
2126185db85Sdougm 	int optdef;
2136185db85Sdougm 
2146185db85Sdougm 	optdef = findopt(name);
2156185db85Sdougm 	if (optdef != -1)
216a3351425Sdougm 		return (optdefs[optdef].type);
2176185db85Sdougm 	return (OPT_TYPE_ANY);
2186185db85Sdougm }
2196185db85Sdougm 
2206185db85Sdougm /*
2216185db85Sdougm  * nfs_validate_security_mode(mode)
2226185db85Sdougm  *
2236185db85Sdougm  * is the specified mode string a valid one for use with NFS?
2246185db85Sdougm  */
2256185db85Sdougm 
2266185db85Sdougm static int
2276185db85Sdougm nfs_validate_security_mode(char *mode)
2286185db85Sdougm {
2296185db85Sdougm 	seconfig_t secinfo;
2306185db85Sdougm 	int err;
2316185db85Sdougm 
2326185db85Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2336185db85Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2346185db85Sdougm 	if (err == SC_NOERROR)
235a3351425Sdougm 		return (1);
2366185db85Sdougm 	return (0);
2376185db85Sdougm }
2386185db85Sdougm 
2396185db85Sdougm /*
2406185db85Sdougm  * nfs_is_security_opt(tok)
2416185db85Sdougm  *
2426185db85Sdougm  * check to see if tok represents an option that is only valid in some
2436185db85Sdougm  * security flavor.
2446185db85Sdougm  */
2456185db85Sdougm 
2466185db85Sdougm static int
2476185db85Sdougm nfs_is_security_opt(char *tok)
2486185db85Sdougm {
2496185db85Sdougm 	int i;
2506185db85Sdougm 
2516185db85Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
252a3351425Sdougm 		if (strcmp(tok, seclist[i]) == 0)
253a3351425Sdougm 			return (1);
2546185db85Sdougm 	}
2556185db85Sdougm 	return (0);
2566185db85Sdougm }
2576185db85Sdougm 
2586185db85Sdougm /*
2596185db85Sdougm  * find_security(seclist, sec)
2606185db85Sdougm  *
2616185db85Sdougm  * Walk the current list of security flavors and return true if it is
2626185db85Sdougm  * present, else return false.
2636185db85Sdougm  */
2646185db85Sdougm 
2656185db85Sdougm static int
2666185db85Sdougm find_security(struct securities *seclist, sa_security_t sec)
2676185db85Sdougm {
2686185db85Sdougm 	while (seclist != NULL) {
269a3351425Sdougm 		if (seclist->security == sec)
270a3351425Sdougm 			return (1);
271a3351425Sdougm 		seclist = seclist->next;
2726185db85Sdougm 	}
2736185db85Sdougm 	return (0);
2746185db85Sdougm }
2756185db85Sdougm 
2766185db85Sdougm /*
2776185db85Sdougm  * make_security_list(group, securitymodes, proto)
2786185db85Sdougm  *	go through the list of securitymodes and add them to the
2796185db85Sdougm  *	group's list of security optionsets. We also keep a list of
2806185db85Sdougm  *	those optionsets so we don't have to find them later. All of
2816185db85Sdougm  *	these will get copies of the same properties.
2826185db85Sdougm  */
2836185db85Sdougm 
2846185db85Sdougm static struct securities *
2856185db85Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
2866185db85Sdougm {
2876185db85Sdougm 	char *tok, *next = NULL;
2886185db85Sdougm 	struct securities *curp, *headp = NULL, *prev;
2896185db85Sdougm 	sa_security_t check;
2906185db85Sdougm 	int freetok = 0;
2916185db85Sdougm 
2926185db85Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
293a3351425Sdougm 		next = strchr(tok, ':');
294a3351425Sdougm 		if (next != NULL)
295a3351425Sdougm 			*next++ = '\0';
296a3351425Sdougm 		if (strcmp(tok, "default") == 0) {
297a3351425Sdougm 			/* resolve default into the real type */
298a3351425Sdougm 			tok = nfs_space_alias(tok);
299a3351425Sdougm 			freetok = 1;
300a3351425Sdougm 		}
301a3351425Sdougm 		check = sa_get_security(group, tok, proto);
302a3351425Sdougm 
303a3351425Sdougm 		/* add to the security list if it isn't there already */
304a3351425Sdougm 		if (check == NULL || !find_security(headp, check)) {
305a3351425Sdougm 			curp = (struct securities *)calloc(1,
306a3351425Sdougm 			    sizeof (struct securities));
307a3351425Sdougm 			if (curp != NULL) {
308a3351425Sdougm 				if (check == NULL) {
309a3351425Sdougm 					curp->security = sa_create_security(
310a3351425Sdougm 					    group, tok, proto);
311a3351425Sdougm 				} else {
312a3351425Sdougm 					curp->security = check;
313a3351425Sdougm 				}
314a3351425Sdougm 				/*
315a3351425Sdougm 				 * note that the first time through the loop,
316a3351425Sdougm 				 * headp will be NULL and prev will be
317a3351425Sdougm 				 * undefined.  Since headp is NULL, we set
318a3351425Sdougm 				 * both it and prev to the curp (first
319a3351425Sdougm 				 * structure to be allocated).
320a3351425Sdougm 				 *
321a3351425Sdougm 				 * later passes through the loop will have
322a3351425Sdougm 				 * headp not being NULL and prev will be used
323a3351425Sdougm 				 * to allocate at the end of the list.
324a3351425Sdougm 				 */
325a3351425Sdougm 				if (headp == NULL) {
326a3351425Sdougm 					headp = curp;
327a3351425Sdougm 					prev = curp;
328a3351425Sdougm 				} else {
329a3351425Sdougm 					prev->next = curp;
330a3351425Sdougm 					prev = curp;
331a3351425Sdougm 				}
332a3351425Sdougm 			}
3336185db85Sdougm 		}
334a99982a7Sdougm 
335a3351425Sdougm 		if (freetok) {
336a3351425Sdougm 			freetok = 0;
337a3351425Sdougm 			sa_free_attr_string(tok);
338a3351425Sdougm 		}
3396185db85Sdougm 	}
3406185db85Sdougm 	return (headp);
3416185db85Sdougm }
3426185db85Sdougm 
3436185db85Sdougm static void
3446185db85Sdougm free_security_list(struct securities *sec)
3456185db85Sdougm {
3466185db85Sdougm 	struct securities *next;
3476185db85Sdougm 	if (sec != NULL) {
348a3351425Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
349a3351425Sdougm 			next = sec->next;
350a3351425Sdougm 			free(sec);
351a3351425Sdougm 		}
3526185db85Sdougm 	}
3536185db85Sdougm }
3546185db85Sdougm 
3556185db85Sdougm /*
3566185db85Sdougm  * nfs_alistcat(str1, str2, sep)
3576185db85Sdougm  *
3586185db85Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
3596185db85Sdougm  * character. If memory allocation fails, return NULL;
3606185db85Sdougm  */
3616185db85Sdougm 
3626185db85Sdougm static char *
3636185db85Sdougm nfs_alistcat(char *str1, char *str2, char sep)
3646185db85Sdougm {
3656185db85Sdougm 	char *newstr;
3666185db85Sdougm 	size_t len;
3676185db85Sdougm 
3686185db85Sdougm 	len = strlen(str1) + strlen(str2) + 2;
3696185db85Sdougm 	newstr = (char *)malloc(len);
3706185db85Sdougm 	if (newstr != NULL)
371a3351425Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
3726185db85Sdougm 	return (newstr);
3736185db85Sdougm }
3746185db85Sdougm 
3756185db85Sdougm /*
3766185db85Sdougm  * add_security_prop(sec, name, value, persist)
3776185db85Sdougm  *
3786185db85Sdougm  * Add the property to the securities structure. This accumulates
3796185db85Sdougm  * properties for as part of parsing legacy options.
3806185db85Sdougm  */
3816185db85Sdougm 
3826185db85Sdougm static int
3836185db85Sdougm add_security_prop(struct securities *sec, char *name, char *value,
3846185db85Sdougm 			int persist, int iszfs)
3856185db85Sdougm {
3866185db85Sdougm 	sa_property_t prop;
3876185db85Sdougm 	int ret = SA_OK;
3886185db85Sdougm 
3896185db85Sdougm 	for (; sec != NULL; sec = sec->next) {
390a3351425Sdougm 		if (value == NULL) {
391a3351425Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
392a3351425Sdougm 			    strcmp(name, SHOPT_RO) == 0)
393a3351425Sdougm 				value = "*";
394a3351425Sdougm 			else
395a3351425Sdougm 				value = "true";
396a3351425Sdougm 		}
397a99982a7Sdougm 
398a99982a7Sdougm 		/*
399a99982a7Sdougm 		 * Get the existing property, if it exists, so we can
400a99982a7Sdougm 		 * determine what to do with it. The ro/rw/root
401a99982a7Sdougm 		 * properties can be merged if multiple instances of
402a99982a7Sdougm 		 * these properies are given. For example, if "rw"
403a99982a7Sdougm 		 * exists with a value "host1" and a later token of
404a99982a7Sdougm 		 * rw="host2" is seen, the values are merged into a
405a99982a7Sdougm 		 * single rw="host1:host2".
406a99982a7Sdougm 		 */
407a3351425Sdougm 		prop = sa_get_property(sec->security, name);
408a99982a7Sdougm 
409a3351425Sdougm 		if (prop != NULL) {
410a3351425Sdougm 			char *oldvalue;
411a3351425Sdougm 			char *newvalue;
412a99982a7Sdougm 
413a99982a7Sdougm 			/*
414a3351425Sdougm 			 * The security options of ro/rw/root might appear
415a3351425Sdougm 			 * multiple times. If they do, the values need to be
416a3351425Sdougm 			 * merged into an access list. If it was previously
417a3351425Sdougm 			 * empty, the new value alone is added.
418a99982a7Sdougm 			 */
419a3351425Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
420a3351425Sdougm 			if (oldvalue != NULL) {
421a3351425Sdougm 				/*
422a3351425Sdougm 				 * The general case is to concatenate the new
423a3351425Sdougm 				 * value onto the old value for multiple
424a3351425Sdougm 				 * rw(ro/root) properties. A special case
425a3351425Sdougm 				 * exists when either the old or new is the
426a3351425Sdougm 				 * "all" case. In the special case, if both
427a3351425Sdougm 				 * are "all", then it is "all", else if one is
428a3351425Sdougm 				 * an access-list, that replaces the "all".
429a3351425Sdougm 				 */
430a3351425Sdougm 				if (strcmp(oldvalue, "*") == 0) {
431a3351425Sdougm 					/* Replace old value with new value. */
432a3351425Sdougm 					newvalue = strdup(value);
43397df5ac9Sdougm 				} else if (strcmp(value, "*") == 0 ||
43497df5ac9Sdougm 				    strcmp(oldvalue, value) == 0) {
435a3351425Sdougm 					/*
436a3351425Sdougm 					 * Keep old value and ignore
437a3351425Sdougm 					 * the new value.
438a3351425Sdougm 					 */
439a3351425Sdougm 					newvalue = NULL;
440a3351425Sdougm 				} else {
441a3351425Sdougm 					/*
442a3351425Sdougm 					 * Make a new list of old plus new
443a3351425Sdougm 					 * access-list.
444a3351425Sdougm 					 */
445a3351425Sdougm 					newvalue = nfs_alistcat(oldvalue,
446a3351425Sdougm 					    value, ':');
447a3351425Sdougm 				}
448a3351425Sdougm 
449a3351425Sdougm 				if (newvalue != NULL) {
450a3351425Sdougm 					(void) sa_remove_property(prop);
451a3351425Sdougm 					prop = sa_create_property(name,
452a3351425Sdougm 					    newvalue);
453a3351425Sdougm 					ret = sa_add_property(sec->security,
454a3351425Sdougm 					    prop);
455a3351425Sdougm 					free(newvalue);
456a3351425Sdougm 				}
457a3351425Sdougm 				if (oldvalue != NULL)
458a3351425Sdougm 					sa_free_attr_string(oldvalue);
459a3351425Sdougm 			}
460a3351425Sdougm 		} else {
461a3351425Sdougm 			prop = sa_create_property(name, value);
462a99982a7Sdougm 			ret = sa_add_property(sec->security, prop);
4636185db85Sdougm 		}
464a3351425Sdougm 		if (ret == SA_OK && !iszfs) {
465a3351425Sdougm 			ret = sa_commit_properties(sec->security, !persist);
466a3351425Sdougm 		}
4676185db85Sdougm 	}
4686185db85Sdougm 	return (ret);
4696185db85Sdougm }
4706185db85Sdougm 
4716185db85Sdougm /*
4726185db85Sdougm  * check to see if group/share is persistent.
4736185db85Sdougm  */
4746185db85Sdougm static int
4756185db85Sdougm is_persistent(sa_group_t group)
4766185db85Sdougm {
4776185db85Sdougm 	char *type;
4786185db85Sdougm 	int persist = 1;
4796185db85Sdougm 
4806185db85Sdougm 	type = sa_get_group_attr(group, "type");
4816185db85Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
482a3351425Sdougm 		persist = 0;
4836185db85Sdougm 	if (type != NULL)
484a3351425Sdougm 		sa_free_attr_string(type);
4856185db85Sdougm 	return (persist);
4866185db85Sdougm }
4876185db85Sdougm 
4886185db85Sdougm /*
4896185db85Sdougm  * invalid_security(options)
4906185db85Sdougm  *
4916185db85Sdougm  * search option string for any invalid sec= type.
4926185db85Sdougm  * return true (1) if any are not valid else false (0)
4936185db85Sdougm  */
4946185db85Sdougm static int
4956185db85Sdougm invalid_security(char *options)
4966185db85Sdougm {
4976185db85Sdougm 	char *copy, *base, *token, *value;
4986185db85Sdougm 	int ret = 0;
4996185db85Sdougm 
5006185db85Sdougm 	copy = strdup(options);
5016185db85Sdougm 	token = base = copy;
5026185db85Sdougm 	while (token != NULL && ret == 0) {
503a3351425Sdougm 		token = strtok(base, ",");
504a3351425Sdougm 		base = NULL;
505a3351425Sdougm 		if (token != NULL) {
506a3351425Sdougm 			value = strchr(token, '=');
507a3351425Sdougm 			if (value != NULL)
508a3351425Sdougm 				*value++ = '\0';
509a3351425Sdougm 			if (strcmp(token, "sec") == 0) {
510a3351425Sdougm 				/* HAVE security flavors so check them */
511a3351425Sdougm 				char *tok, *next;
512a3351425Sdougm 				for (next = NULL, tok = value; tok != NULL;
513a3351425Sdougm 				    tok = next) {
514a3351425Sdougm 					next = strchr(tok, ':');
515a3351425Sdougm 					if (next != NULL)
516a3351425Sdougm 						*next++ = '\0';
517a3351425Sdougm 					ret = !nfs_validate_security_mode(tok);
518a3351425Sdougm 					if (ret)
519a3351425Sdougm 						break;
520a3351425Sdougm 				}
521a3351425Sdougm 			}
5226185db85Sdougm 		}
5236185db85Sdougm 	}
5246185db85Sdougm 	if (copy != NULL)
525a3351425Sdougm 		free(copy);
5266185db85Sdougm 	return (ret);
5276185db85Sdougm }
5286185db85Sdougm 
5296185db85Sdougm /*
5306185db85Sdougm  * nfs_parse_legacy_options(group, options)
5316185db85Sdougm  *
5326185db85Sdougm  * Parse the old style options into internal format and store on the
5336185db85Sdougm  * specified group.  Group could be a share for full legacy support.
5346185db85Sdougm  */
5356185db85Sdougm 
5366185db85Sdougm static int
5376185db85Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5386185db85Sdougm {
539aba7a40bSdougm 	char *dup;
5406185db85Sdougm 	char *base;
5416185db85Sdougm 	char *token;
5426185db85Sdougm 	sa_optionset_t optionset;
5436185db85Sdougm 	struct securities *security_list = NULL;
5446185db85Sdougm 	sa_property_t prop;
5456185db85Sdougm 	int ret = SA_OK;
5466185db85Sdougm 	int iszfs = 0;
5476185db85Sdougm 	sa_group_t parent;
5486185db85Sdougm 	int persist = 0;
5496185db85Sdougm 	char *lasts;
5506185db85Sdougm 
5516185db85Sdougm 	/* do we have an existing optionset? */
5526185db85Sdougm 	optionset = sa_get_optionset(group, "nfs");
5536185db85Sdougm 	if (optionset == NULL) {
554a3351425Sdougm 		/* didn't find existing optionset so create one */
555a3351425Sdougm 		optionset = sa_create_optionset(group, "nfs");
5566185db85Sdougm 	} else {
5576185db85Sdougm 		/*
558da6c28aaSamw 		 * Have an existing optionset . Ideally, we would need
559da6c28aaSamw 		 * to compare options in order to detect errors. For
560da6c28aaSamw 		 * now, we assume that the first optionset is the
561da6c28aaSamw 		 * correct one and the others will be the same. An
562da6c28aaSamw 		 * empty optionset is the same as no optionset so we
563da6c28aaSamw 		 * don't want to exit in that case. Getting an empty
564da6c28aaSamw 		 * optionset can occur with ZFS property checking.
5656185db85Sdougm 		 */
566da6c28aaSamw 		if (sa_get_property(optionset, NULL) != NULL)
567da6c28aaSamw 			return (ret);
5686185db85Sdougm 	}
5696185db85Sdougm 
5706185db85Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
5716185db85Sdougm 		/*
5726185db85Sdougm 		 * there is a special case of only the option "rw"
5736185db85Sdougm 		 * being the default option. We don't have to do
5746185db85Sdougm 		 * anything.
5756185db85Sdougm 		 */
576a3351425Sdougm 		return (ret);
5776185db85Sdougm 	}
5786185db85Sdougm 
5796185db85Sdougm 	/*
5806185db85Sdougm 	 * check if security types are present and validate them. If
5816185db85Sdougm 	 * any are not legal, fail.
5826185db85Sdougm 	 */
5836185db85Sdougm 
5846185db85Sdougm 	if (invalid_security(options)) {
585a3351425Sdougm 		return (SA_INVALID_SECURITY);
5866185db85Sdougm 	}
5876185db85Sdougm 
5886185db85Sdougm 	/*
5896185db85Sdougm 	 * in order to not attempt to change ZFS properties unless
5906185db85Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
5916185db85Sdougm 	 */
5926185db85Sdougm 	if (sa_is_share(group)) {
593a3351425Sdougm 		char *zfs;
594a3351425Sdougm 		parent = sa_get_parent_group(group);
595a3351425Sdougm 		if (parent != NULL) {
596a3351425Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
597a3351425Sdougm 			if (zfs != NULL) {
598a3351425Sdougm 				sa_free_attr_string(zfs);
599a3351425Sdougm 				iszfs++;
600a3351425Sdougm 			}
6016185db85Sdougm 		}
6026185db85Sdougm 	} else {
603a3351425Sdougm 		iszfs = sa_group_is_zfs(group);
6046185db85Sdougm 	}
6056185db85Sdougm 
606aba7a40bSdougm 	/* We need a copy of options for the next part. */
607aba7a40bSdougm 	dup = strdup(options);
608aba7a40bSdougm 	if (dup == NULL)
609aba7a40bSdougm 		return (SA_NO_MEMORY);
610aba7a40bSdougm 
6116185db85Sdougm 	/*
6126185db85Sdougm 	 * we need to step through each option in the string and then
6136185db85Sdougm 	 * add either the option or the security option as needed. If
6146185db85Sdougm 	 * this is not a persistent share, don't commit to the
615a99982a7Sdougm 	 * repository. If there is an error, we also want to abort the
616a99982a7Sdougm 	 * processing and report it.
6176185db85Sdougm 	 */
6186185db85Sdougm 	persist = is_persistent(group);
6196185db85Sdougm 	base = dup;
6206185db85Sdougm 	token = dup;
6216185db85Sdougm 	lasts = NULL;
622a99982a7Sdougm 	while (token != NULL && ret == SA_OK) {
623a3351425Sdougm 		ret = SA_OK;
624a3351425Sdougm 		token = strtok_r(base, ",", &lasts);
625a3351425Sdougm 		base = NULL;
626a3351425Sdougm 		if (token != NULL) {
627a3351425Sdougm 			char *value;
6286185db85Sdougm 			/*
629a3351425Sdougm 			 * if the option has a value, it will have an '=' to
630a3351425Sdougm 			 * separate the name from the value. The following
631a3351425Sdougm 			 * code will result in value != NULL and token
632a3351425Sdougm 			 * pointing to just the name if there is a value.
6336185db85Sdougm 			 */
634a3351425Sdougm 			value = strchr(token, '=');
635a3351425Sdougm 			if (value != NULL) {
636a3351425Sdougm 				*value++ = '\0';
6376185db85Sdougm 			}
638a3351425Sdougm 			if (strcmp(token, "sec") == 0 ||
639a3351425Sdougm 			    strcmp(token, "secure") == 0) {
6406185db85Sdougm 				/*
641a3351425Sdougm 				 * Once in security parsing, we only
642a3351425Sdougm 				 * do security. We do need to move
643a3351425Sdougm 				 * between the security node and the
644a3351425Sdougm 				 * toplevel. The security tag goes on
645a3351425Sdougm 				 * the root while the following ones
646a3351425Sdougm 				 * go on the security.
6476185db85Sdougm 				 */
648a3351425Sdougm 				if (security_list != NULL) {
649a3351425Sdougm 					/*
650a3351425Sdougm 					 * have an old list so close it and
651a3351425Sdougm 					 * start the new
652a3351425Sdougm 					 */
653a3351425Sdougm 					free_security_list(security_list);
654a3351425Sdougm 				}
655a3351425Sdougm 				if (strcmp(token, "secure") == 0) {
656a3351425Sdougm 					value = "dh";
657a3351425Sdougm 				} else {
658a3351425Sdougm 					if (value == NULL) {
659a3351425Sdougm 						ret = SA_SYNTAX_ERR;
660a3351425Sdougm 						break;
661a3351425Sdougm 					}
662a3351425Sdougm 				}
663a3351425Sdougm 				security_list = make_security_list(group,
664a3351425Sdougm 				    value, "nfs");
6656185db85Sdougm 			} else {
666a3351425Sdougm 				/*
667a3351425Sdougm 				 * Note that the "old" syntax allowed a
668a3351425Sdougm 				 * default security model This must be
669a3351425Sdougm 				 * accounted for and internally converted to
670a3351425Sdougm 				 * "standard" security structure.
671a3351425Sdougm 				 */
672a3351425Sdougm 				if (nfs_is_security_opt(token)) {
673a3351425Sdougm 					if (security_list == NULL) {
674a3351425Sdougm 						/*
675a3351425Sdougm 						 * need to have a
676a3351425Sdougm 						 * security
677a3351425Sdougm 						 * option. This will
678a3351425Sdougm 						 * be "closed" when a
679a3351425Sdougm 						 * defined "sec="
680a3351425Sdougm 						 * option is
681a3351425Sdougm 						 * seen. This is
682a3351425Sdougm 						 * technically an
683a3351425Sdougm 						 * error but will be
684a3351425Sdougm 						 * allowed with
685a3351425Sdougm 						 * warning.
686a3351425Sdougm 						 */
687a3351425Sdougm 						security_list =
688a3351425Sdougm 						    make_security_list(group,
689a3351425Sdougm 						    "default",
690a3351425Sdougm 						    "nfs");
691a3351425Sdougm 					}
692a3351425Sdougm 					if (security_list != NULL) {
693a3351425Sdougm 						ret = add_security_prop(
694a3351425Sdougm 						    security_list, token,
695a3351425Sdougm 						    value, persist, iszfs);
696a3351425Sdougm 					} else {
697a3351425Sdougm 						ret = SA_NO_MEMORY;
698a3351425Sdougm 					}
699a3351425Sdougm 				} else {
700a3351425Sdougm 					/* regular options */
701a3351425Sdougm 					if (value == NULL) {
702a3351425Sdougm 						if (strcmp(token, SHOPT_RW) ==
703a3351425Sdougm 						    0 || strcmp(token,
704a3351425Sdougm 						    SHOPT_RO) == 0) {
705a3351425Sdougm 							value = "*";
706a3351425Sdougm 						} else {
707a3351425Sdougm 							value = "global";
708a3351425Sdougm 							if (strcmp(token,
709a3351425Sdougm 							    SHOPT_LOG) != 0) {
710a3351425Sdougm 								value = "true";
711a3351425Sdougm 							}
712a3351425Sdougm 						}
713a3351425Sdougm 					}
714d6405362Sdougm 					/*
715d6405362Sdougm 					 * In all cases, create the
716d6405362Sdougm 					 * property specified. If the
717d6405362Sdougm 					 * value was NULL, the default
718d6405362Sdougm 					 * value will have been
719d6405362Sdougm 					 * substituted.
720d6405362Sdougm 					 */
721d6405362Sdougm 					prop = sa_create_property(token, value);
722d6405362Sdougm 					ret =  sa_add_property(optionset, prop);
723d6405362Sdougm 					if (ret != SA_OK)
724d6405362Sdougm 						break;
725d6405362Sdougm 
726a3351425Sdougm 					if (!iszfs) {
727a3351425Sdougm 						ret = sa_commit_properties(
728a3351425Sdougm 						    optionset, !persist);
729a3351425Sdougm 					}
730a3351425Sdougm 				}
7316185db85Sdougm 			}
7326185db85Sdougm 		}
7336185db85Sdougm 	}
7346185db85Sdougm 	if (security_list != NULL)
735a3351425Sdougm 		free_security_list(security_list);
736aba7a40bSdougm 
737aba7a40bSdougm 	free(dup);
7386185db85Sdougm 	return (ret);
7396185db85Sdougm }
7406185db85Sdougm 
7416185db85Sdougm /*
7426185db85Sdougm  * is_a_number(number)
7436185db85Sdougm  *
7446185db85Sdougm  * is the string a number in one of the forms we want to use?
7456185db85Sdougm  */
7466185db85Sdougm 
7476185db85Sdougm static int
7486185db85Sdougm is_a_number(char *number)
7496185db85Sdougm {
7506185db85Sdougm 	int ret = 1;
7516185db85Sdougm 	int hex = 0;
7526185db85Sdougm 
7536185db85Sdougm 	if (strncmp(number, "0x", 2) == 0) {
754a3351425Sdougm 		number += 2;
755a3351425Sdougm 		hex = 1;
756a3351425Sdougm 	} else if (*number == '-') {
757a3351425Sdougm 		number++; /* skip the minus */
758a3351425Sdougm 	}
7596185db85Sdougm 	while (ret == 1 && *number != '\0') {
760a3351425Sdougm 		if (hex) {
761a3351425Sdougm 			ret = isxdigit(*number++);
762a3351425Sdougm 		} else {
763a3351425Sdougm 			ret = isdigit(*number++);
764a3351425Sdougm 		}
7656185db85Sdougm 	}
7666185db85Sdougm 	return (ret);
7676185db85Sdougm }
7686185db85Sdougm 
7696185db85Sdougm /*
7706185db85Sdougm  * Look for the specified tag in the configuration file. If it is found,
7716185db85Sdougm  * enable logging and set the logging configuration information for exp.
7726185db85Sdougm  */
7736185db85Sdougm static void
7746185db85Sdougm configlog(struct exportdata *exp, char *tag)
7756185db85Sdougm {
7766185db85Sdougm 	nfsl_config_t *configlist = NULL, *configp;
7776185db85Sdougm 	int error = 0;
7786185db85Sdougm 	char globaltag[] = DEFAULTTAG;
7796185db85Sdougm 
7806185db85Sdougm 	/*
7816185db85Sdougm 	 * Sends config errors to stderr
7826185db85Sdougm 	 */
7836185db85Sdougm 	nfsl_errs_to_syslog = B_FALSE;
7846185db85Sdougm 
7856185db85Sdougm 	/*
7866185db85Sdougm 	 * get the list of configuration settings
7876185db85Sdougm 	 */
7886185db85Sdougm 	error = nfsl_getconfig_list(&configlist);
7896185db85Sdougm 	if (error) {
7906185db85Sdougm 		(void) fprintf(stderr,
791a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
792a3351425Sdougm 		    strerror(error));
7936185db85Sdougm 	}
7946185db85Sdougm 
7956185db85Sdougm 	if (tag == NULL)
7966185db85Sdougm 		tag = globaltag;
7976185db85Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
7986185db85Sdougm 		nfsl_freeconfig_list(&configlist);
7996185db85Sdougm 		(void) fprintf(stderr,
800a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
8016185db85Sdougm 		/* bad configuration */
8026185db85Sdougm 		error = ENOENT;
8036185db85Sdougm 		goto err;
8046185db85Sdougm 	}
8056185db85Sdougm 
8066185db85Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
8076185db85Sdougm 		error = ENOMEM;
8086185db85Sdougm 		goto out;
8096185db85Sdougm 	}
8106185db85Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
8116185db85Sdougm 		error = ENOMEM;
8126185db85Sdougm 		goto out;
8136185db85Sdougm 	}
8146185db85Sdougm 	exp->ex_flags |= EX_LOG;
8156185db85Sdougm 	if (configp->nc_rpclogpath != NULL)
8166185db85Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8176185db85Sdougm out:
8186185db85Sdougm 	if (configlist != NULL)
819a3351425Sdougm 		nfsl_freeconfig_list(&configlist);
8206185db85Sdougm 
8216185db85Sdougm err:
8226185db85Sdougm 	if (error != 0) {
8236185db85Sdougm 		if (exp->ex_flags != NULL)
8246185db85Sdougm 			free(exp->ex_tag);
8256185db85Sdougm 		if (exp->ex_log_buffer != NULL)
8266185db85Sdougm 			free(exp->ex_log_buffer);
8276185db85Sdougm 		(void) fprintf(stderr,
828a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
829a3351425Sdougm 		    strerror(error));
8306185db85Sdougm 	}
8316185db85Sdougm }
8326185db85Sdougm 
8336185db85Sdougm /*
8346185db85Sdougm  * fill_export_from_optionset(export, optionset)
8356185db85Sdougm  *
8366185db85Sdougm  * In order to share, we need to set all the possible general options
8376185db85Sdougm  * into the export structure. Share info will be filled in by the
8386185db85Sdougm  * caller. Various property values get turned into structure specific
8396185db85Sdougm  * values.
8406185db85Sdougm  */
8416185db85Sdougm 
8426185db85Sdougm static int
8436185db85Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
8446185db85Sdougm {
8456185db85Sdougm 	sa_property_t option;
8466185db85Sdougm 	int ret = SA_OK;
8476185db85Sdougm 
8486185db85Sdougm 	for (option = sa_get_property(optionset, NULL);
849a3351425Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
850a3351425Sdougm 		char *name;
851a3351425Sdougm 		char *value;
852a3351425Sdougm 		uint32_t val;
8536185db85Sdougm 
854a3351425Sdougm 		/*
855a3351425Sdougm 		 * since options may be set/reset multiple times, always do an
856a3351425Sdougm 		 * explicit set or clear of the option. This allows defaults
857da6c28aaSamw 		 * to be set and then the protocol specific to override.
858a3351425Sdougm 		 */
8596185db85Sdougm 
860a3351425Sdougm 		name = sa_get_property_attr(option, "type");
861a3351425Sdougm 		value = sa_get_property_attr(option, "value");
862a3351425Sdougm 		switch (findopt(name)) {
863a3351425Sdougm 		case OPT_ANON:
864a3351425Sdougm 			if (value != NULL && is_a_number(value)) {
865a3351425Sdougm 				val = strtoul(value, NULL, 0);
866a3351425Sdougm 			} else {
867a3351425Sdougm 				struct passwd *pw;
868a3351425Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
869a3351425Sdougm 				if (pw != NULL) {
870a3351425Sdougm 					val = pw->pw_uid;
871a3351425Sdougm 				} else {
872a3351425Sdougm 					val = UID_NOBODY;
873a3351425Sdougm 				}
874a3351425Sdougm 				endpwent();
875a3351425Sdougm 			}
876a3351425Sdougm 			export->ex_anon = val;
877a3351425Sdougm 			break;
878a3351425Sdougm 		case OPT_NOSUID:
879a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
880a3351425Sdougm 			    strcmp(value, "1") == 0))
881a3351425Sdougm 				export->ex_flags |= EX_NOSUID;
882a3351425Sdougm 			else
883a3351425Sdougm 				export->ex_flags &= ~EX_NOSUID;
884a3351425Sdougm 			break;
885a3351425Sdougm 		case OPT_ACLOK:
886a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
887a3351425Sdougm 			    strcmp(value, "1") == 0))
888a3351425Sdougm 				export->ex_flags |= EX_ACLOK;
889a3351425Sdougm 			else
890a3351425Sdougm 				export->ex_flags &= ~EX_ACLOK;
891a3351425Sdougm 			break;
892a3351425Sdougm 		case OPT_NOSUB:
893a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
894a3351425Sdougm 			    strcmp(value, "1") == 0))
895a3351425Sdougm 				export->ex_flags |= EX_NOSUB;
896a3351425Sdougm 			else
897a3351425Sdougm 				export->ex_flags &= ~EX_NOSUB;
898a3351425Sdougm 			break;
899a3351425Sdougm 		case OPT_PUBLIC:
900a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
901a3351425Sdougm 			    strcmp(value, "1") == 0))
902a3351425Sdougm 				export->ex_flags |= EX_PUBLIC;
903a3351425Sdougm 			else
904a3351425Sdougm 				export->ex_flags &= ~EX_PUBLIC;
905a3351425Sdougm 			break;
906a3351425Sdougm 		case OPT_INDEX:
907a3351425Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
908a3351425Sdougm 			    strchr(value, '/') != NULL)) {
909a3351425Sdougm 				/* this is an error */
910a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
911a3351425Sdougm 				    "NFS: index=\"%s\" not valid;"
912a3351425Sdougm 				    "must be a filename.\n"),
913a3351425Sdougm 				    value);
914a3351425Sdougm 				break;
915a3351425Sdougm 			}
916a3351425Sdougm 			if (value != NULL && *value != '\0' &&
917a3351425Sdougm 			    strcmp(value, ".") != 0) {
918a3351425Sdougm 				/* valid index file string */
919a3351425Sdougm 				if (export->ex_index != NULL) {
920a3351425Sdougm 					/* left over from "default" */
921a3351425Sdougm 					free(export->ex_index);
922a3351425Sdougm 				}
923a3351425Sdougm 				/* remember to free */
924a3351425Sdougm 				export->ex_index = strdup(value);
925a3351425Sdougm 				if (export->ex_index == NULL) {
926a3351425Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
927a3351425Sdougm 					    "NFS: out of memory setting "
928a3351425Sdougm 					    "index property\n"));
929a3351425Sdougm 					break;
930a3351425Sdougm 				}
931a3351425Sdougm 				export->ex_flags |= EX_INDEX;
932a3351425Sdougm 			}
933a3351425Sdougm 			break;
934a3351425Sdougm 		case OPT_LOG:
935a3351425Sdougm 			if (value == NULL)
936a3351425Sdougm 				value = strdup("global");
937a3351425Sdougm 			if (value != NULL)
938a3351425Sdougm 				configlog(export,
939a3351425Sdougm 				    strlen(value) ? value : "global");
940a3351425Sdougm 			break;
941a3351425Sdougm 		default:
942a3351425Sdougm 			/* have a syntactic error */
943549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
944a3351425Sdougm 			    "NFS: unrecognized option %s=%s\n"),
945a3351425Sdougm 			    name, value != NULL ? value : "");
9466185db85Sdougm 			break;
9476185db85Sdougm 		}
948a3351425Sdougm 		if (name != NULL)
949a3351425Sdougm 			sa_free_attr_string(name);
9506185db85Sdougm 		if (value != NULL)
951a3351425Sdougm 			sa_free_attr_string(value);
9526185db85Sdougm 	}
9536185db85Sdougm 	return (ret);
9546185db85Sdougm }
9556185db85Sdougm 
9566185db85Sdougm /*
9576185db85Sdougm  * cleanup_export(export)
9586185db85Sdougm  *
9596185db85Sdougm  * Cleanup the allocated areas so we don't leak memory
9606185db85Sdougm  */
9616185db85Sdougm 
9626185db85Sdougm static void
9636185db85Sdougm cleanup_export(struct exportdata *export)
9646185db85Sdougm {
9656185db85Sdougm 	int i;
9666185db85Sdougm 
9676185db85Sdougm 	if (export->ex_index != NULL)
968a3351425Sdougm 		free(export->ex_index);
9696185db85Sdougm 	if (export->ex_secinfo != NULL) {
970a3351425Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
971a3351425Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
972a3351425Sdougm 				free(export->ex_secinfo[i].s_rootnames);
973a3351425Sdougm 		free(export->ex_secinfo);
9746185db85Sdougm 	}
9756185db85Sdougm }
9766185db85Sdougm 
9776185db85Sdougm /*
9786185db85Sdougm  * Given a seconfig entry and a colon-separated
9796185db85Sdougm  * list of names, allocate an array big enough
9806185db85Sdougm  * to hold the root list, then convert each name to
9816185db85Sdougm  * a principal name according to the security
9826185db85Sdougm  * info and assign it to an array element.
9836185db85Sdougm  * Return the array and its size.
9846185db85Sdougm  */
9856185db85Sdougm static caddr_t *
9866185db85Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
9876185db85Sdougm {
9886185db85Sdougm 	caddr_t *a;
9896185db85Sdougm 	int c, i;
9906185db85Sdougm 	char *host, *p;
9916185db85Sdougm 
9926185db85Sdougm 	/*
9936185db85Sdougm 	 * Count the number of strings in the list.
9946185db85Sdougm 	 * This is the number of colon separators + 1.
9956185db85Sdougm 	 */
9966185db85Sdougm 	c = 1;
9976185db85Sdougm 	for (p = list; *p; p++)
9986185db85Sdougm 		if (*p == ':')
9996185db85Sdougm 			c++;
10006185db85Sdougm 	*count = c;
10016185db85Sdougm 
10026185db85Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
10036185db85Sdougm 	if (a == NULL) {
1004549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1005a3351425Sdougm 		    "get_rootnames: no memory\n"));
10066185db85Sdougm 	} else {
1007a3351425Sdougm 		for (i = 0; i < c; i++) {
1008a3351425Sdougm 			host = strtok(list, ":");
1009a3351425Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
1010a3351425Sdougm 				free(a);
1011a3351425Sdougm 				a = NULL;
1012a3351425Sdougm 				break;
1013a3351425Sdougm 			}
1014a3351425Sdougm 			list = NULL;
10156185db85Sdougm 		}
10166185db85Sdougm 	}
10176185db85Sdougm 
10186185db85Sdougm 	return (a);
10196185db85Sdougm }
10206185db85Sdougm 
10216185db85Sdougm /*
10226185db85Sdougm  * fill_security_from_secopts(sp, secopts)
10236185db85Sdougm  *
10246185db85Sdougm  * Fill the secinfo structure from the secopts optionset.
10256185db85Sdougm  */
10266185db85Sdougm 
10276185db85Sdougm static int
10286185db85Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
10296185db85Sdougm {
10306185db85Sdougm 	sa_property_t prop;
10316185db85Sdougm 	char *type;
10326185db85Sdougm 	int longform;
1033a99982a7Sdougm 	int err = SC_NOERROR;
10346185db85Sdougm 
10356185db85Sdougm 	type = sa_get_security_attr(secopts, "sectype");
10366185db85Sdougm 	if (type != NULL) {
1037a3351425Sdougm 		/* named security type needs secinfo to be filled in */
1038a3351425Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
1039a3351425Sdougm 		sa_free_attr_string(type);
1040a3351425Sdougm 		if (err != SC_NOERROR)
1041a3351425Sdougm 			return (err);
10426185db85Sdougm 	} else {
1043a3351425Sdougm 		/* default case */
1044a3351425Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
1045a3351425Sdougm 		if (err != SC_NOERROR)
1046a3351425Sdougm 			return (err);
10476185db85Sdougm 	}
10486185db85Sdougm 
1049a99982a7Sdougm 	err = SA_OK;
10506185db85Sdougm 	for (prop = sa_get_property(secopts, NULL);
1051a3351425Sdougm 	    prop != NULL && err == SA_OK;
1052a3351425Sdougm 	    prop = sa_get_next_property(prop)) {
1053a3351425Sdougm 		char *name;
1054a3351425Sdougm 		char *value;
10556185db85Sdougm 
1056a3351425Sdougm 		name = sa_get_property_attr(prop, "type");
1057a3351425Sdougm 		value = sa_get_property_attr(prop, "value");
10586185db85Sdougm 
1059a3351425Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
10606185db85Sdougm 
1061a3351425Sdougm 		switch (findopt(name)) {
1062a3351425Sdougm 		case OPT_RO:
1063a3351425Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
1064a3351425Sdougm 			break;
1065a3351425Sdougm 		case OPT_RW:
1066a3351425Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
1067a3351425Sdougm 			break;
1068a3351425Sdougm 		case OPT_ROOT:
1069a3351425Sdougm 			sp->s_flags |= M_ROOT;
1070a3351425Sdougm 			/*
1071a3351425Sdougm 			 * if we are using AUTH_UNIX, handle like other things
1072a3351425Sdougm 			 * such as RO/RW
1073a3351425Sdougm 			 */
1074a3351425Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
1075a3351425Sdougm 				continue;
1076a3351425Sdougm 			/* not AUTH_UNIX */
1077a3351425Sdougm 			if (value != NULL) {
1078a3351425Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
1079a3351425Sdougm 				    value, &sp->s_rootcnt);
1080a3351425Sdougm 				if (sp->s_rootnames == NULL) {
1081a3351425Sdougm 					err = SA_BAD_VALUE;
1082a3351425Sdougm 					(void) fprintf(stderr,
1083a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
1084a3351425Sdougm 					    "Bad root list\n"));
1085a3351425Sdougm 				}
1086a3351425Sdougm 			}
1087a3351425Sdougm 			break;
1088a3351425Sdougm 		case OPT_WINDOW:
1089a3351425Sdougm 			if (value != NULL) {
1090a3351425Sdougm 				sp->s_window = atoi(value);
1091a3351425Sdougm 				/* just in case */
1092a3351425Sdougm 				if (sp->s_window < 0)
1093a3351425Sdougm 					sp->s_window = DEF_WIN;
1094a3351425Sdougm 			}
1095a3351425Sdougm 			break;
1096a3351425Sdougm 		default:
1097a3351425Sdougm 			break;
10986185db85Sdougm 		}
1099a3351425Sdougm 		if (name != NULL)
1100a3351425Sdougm 			sa_free_attr_string(name);
1101a3351425Sdougm 		if (value != NULL)
1102a3351425Sdougm 			sa_free_attr_string(value);
11036185db85Sdougm 	}
11046185db85Sdougm 	/* if rw/ro options not set, use default of RW */
11056185db85Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
1106a3351425Sdougm 		sp->s_flags |= M_RW;
11076185db85Sdougm 	return (err);
11086185db85Sdougm }
11096185db85Sdougm 
11106185db85Sdougm /*
11116185db85Sdougm  * This is for testing only
11126185db85Sdougm  * It displays the export structure that
11136185db85Sdougm  * goes into the kernel.
11146185db85Sdougm  */
11156185db85Sdougm static void
11166185db85Sdougm printarg(char *path, struct exportdata *ep)
11176185db85Sdougm {
11186185db85Sdougm 	int i, j;
11196185db85Sdougm 	struct secinfo *sp;
11206185db85Sdougm 
11216185db85Sdougm 	if (debug == 0)
1122a3351425Sdougm 		return;
11236185db85Sdougm 
11246185db85Sdougm 	(void) printf("%s:\n", path);
11256185db85Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
11266185db85Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
1127549ec3ffSdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
11286185db85Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
11296185db85Sdougm 	if (ep->ex_flags & EX_NOSUID)
11306185db85Sdougm 		(void) printf("NOSUID ");
11316185db85Sdougm 	if (ep->ex_flags & EX_ACLOK)
11326185db85Sdougm 		(void) printf("ACLOK ");
11336185db85Sdougm 	if (ep->ex_flags & EX_PUBLIC)
11346185db85Sdougm 		(void) printf("PUBLIC ");
11356185db85Sdougm 	if (ep->ex_flags & EX_NOSUB)
11366185db85Sdougm 		(void) printf("NOSUB ");
11376185db85Sdougm 	if (ep->ex_flags & EX_LOG)
11386185db85Sdougm 		(void) printf("LOG ");
11396185db85Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
11406185db85Sdougm 		(void) printf("LOG_ALLOPS ");
11416185db85Sdougm 	if (ep->ex_flags == 0)
11426185db85Sdougm 		(void) printf("(none)");
11436185db85Sdougm 	(void) 	printf("\n");
11446185db85Sdougm 	if (ep->ex_flags & EX_LOG) {
11456185db85Sdougm 		(void) printf("\tex_log_buffer = %s\n",
1146a3351425Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
11476185db85Sdougm 		(void) printf("\tex_tag = %s\n",
1148a3351425Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
11496185db85Sdougm 	}
11506185db85Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
11516185db85Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
11526185db85Sdougm 	(void) printf("\n");
11536185db85Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
11546185db85Sdougm 		sp = &ep->ex_secinfo[i];
11556185db85Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
11566185db85Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
11576185db85Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
11586185db85Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
11596185db85Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
11606185db85Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
11616185db85Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
11626185db85Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
11636185db85Sdougm 		(void) printf("\n");
11646185db85Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
11656185db85Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
11666185db85Sdougm 		(void) fflush(stdout);
11676185db85Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
11686185db85Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
1169a3351425Sdougm 			    sp->s_rootnames[j] : "<null>");
11706185db85Sdougm 		(void) printf("\n\n");
11716185db85Sdougm 	}
11726185db85Sdougm }
11736185db85Sdougm 
11746185db85Sdougm /*
11756185db85Sdougm  * count_security(opts)
11766185db85Sdougm  *
11776185db85Sdougm  * Count the number of security types (flavors). The optionset has
11786185db85Sdougm  * been populated with the security flavors as a holding mechanism.
11796185db85Sdougm  * We later use this number to allocate data structures.
11806185db85Sdougm  */
11816185db85Sdougm 
11826185db85Sdougm static int
11836185db85Sdougm count_security(sa_optionset_t opts)
11846185db85Sdougm {
11856185db85Sdougm 	int count = 0;
11866185db85Sdougm 	sa_property_t prop;
11876185db85Sdougm 	if (opts != NULL) {
1188a3351425Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
1189a3351425Sdougm 		    prop = sa_get_next_property(prop)) {
1190a3351425Sdougm 			count++;
1191a3351425Sdougm 		}
11926185db85Sdougm 	}
11936185db85Sdougm 	return (count);
11946185db85Sdougm }
11956185db85Sdougm 
11966185db85Sdougm /*
11976185db85Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
11986185db85Sdougm  *
11996185db85Sdougm  * provides a mechanism to format NFS properties into legacy output
12006185db85Sdougm  * format. If the buffer would overflow, it is reallocated and grown
12016185db85Sdougm  * as appropriate. Special cases of converting internal form of values
12026185db85Sdougm  * to those used by "share" are done. this function does one property
12036185db85Sdougm  * at a time.
12046185db85Sdougm  */
12056185db85Sdougm 
1206aed5d200Sdougm static int
12076185db85Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
12086185db85Sdougm 			sa_property_t prop, int sep)
12096185db85Sdougm {
12106185db85Sdougm 	char *name;
12116185db85Sdougm 	char *value;
12126185db85Sdougm 	int curlen;
12136185db85Sdougm 	char *buff = *rbuff;
12146185db85Sdougm 	size_t buffsize = *rbuffsize;
1215aed5d200Sdougm 	int printed = B_FALSE;
12166185db85Sdougm 
12176185db85Sdougm 	name = sa_get_property_attr(prop, "type");
12186185db85Sdougm 	value = sa_get_property_attr(prop, "value");
12196185db85Sdougm 	if (buff != NULL)
1220a3351425Sdougm 		curlen = strlen(buff);
12216185db85Sdougm 	else
1222a3351425Sdougm 		curlen = 0;
12236185db85Sdougm 	if (name != NULL) {
1224a3351425Sdougm 		int len;
1225a3351425Sdougm 		len = strlen(name) + sep;
12266185db85Sdougm 
12276185db85Sdougm 		/*
12286185db85Sdougm 		 * A future RFE would be to replace this with more
12296185db85Sdougm 		 * generic code and to possibly handle more types.
12306185db85Sdougm 		 */
1231a3351425Sdougm 		switch (gettype(name)) {
1232a3351425Sdougm 		case OPT_TYPE_BOOLEAN:
1233aed5d200Sdougm 			/*
1234aed5d200Sdougm 			 * For NFS, boolean value of FALSE means it
1235aed5d200Sdougm 			 * doesn't show up in the option list at all.
1236aed5d200Sdougm 			 */
1237a3351425Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
1238aed5d200Sdougm 				goto skip;
1239aed5d200Sdougm 			if (value != NULL) {
1240a3351425Sdougm 				sa_free_attr_string(value);
1241aed5d200Sdougm 				value = NULL;
1242aed5d200Sdougm 			}
1243a3351425Sdougm 			break;
1244a3351425Sdougm 		case OPT_TYPE_ACCLIST:
1245a3351425Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
1246a3351425Sdougm 				sa_free_attr_string(value);
1247a3351425Sdougm 				value = NULL;
1248a3351425Sdougm 			} else {
1249a3351425Sdougm 				if (value != NULL)
1250a3351425Sdougm 					len += 1 + strlen(value);
1251a3351425Sdougm 			}
1252a3351425Sdougm 			break;
1253a3351425Sdougm 		case OPT_TYPE_LOGTAG:
1254a3351425Sdougm 			if (value != NULL && strlen(value) == 0) {
1255a3351425Sdougm 				sa_free_attr_string(value);
1256a3351425Sdougm 				value = NULL;
1257a3351425Sdougm 			} else {
1258a3351425Sdougm 				if (value != NULL)
1259a3351425Sdougm 					len += 1 + strlen(value);
1260a3351425Sdougm 			}
1261a3351425Sdougm 			break;
1262a3351425Sdougm 		default:
1263a3351425Sdougm 			if (value != NULL)
1264a3351425Sdougm 				len += 1 + strlen(value);
1265a3351425Sdougm 			break;
12666185db85Sdougm 		}
1267a3351425Sdougm 		while (buffsize <= (curlen + len)) {
1268a3351425Sdougm 			/* need more room */
1269a3351425Sdougm 			buffsize += incr;
1270a3351425Sdougm 			buff = realloc(buff, buffsize);
1271a3351425Sdougm 			if (buff == NULL) {
1272a3351425Sdougm 				/* realloc failed so free everything */
1273a3351425Sdougm 				if (*rbuff != NULL)
1274a3351425Sdougm 					free(*rbuff);
1275a3351425Sdougm 			}
1276a3351425Sdougm 			*rbuff = buff;
1277a3351425Sdougm 			*rbuffsize = buffsize;
1278aed5d200Sdougm 			if (buff == NULL)
1279aed5d200Sdougm 				goto skip;
1280aed5d200Sdougm 
12816185db85Sdougm 		}
1282aed5d200Sdougm 
1283a3351425Sdougm 		if (buff == NULL)
1284aed5d200Sdougm 			goto skip;
1285aed5d200Sdougm 
1286a3351425Sdougm 		if (value == NULL) {
1287a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1288a3351425Sdougm 			    "%s%s", sep ? "," : "",
1289a3351425Sdougm 			    name, value != NULL ? value : "");
12906185db85Sdougm 		} else {
1291a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1292a3351425Sdougm 			    "%s%s=%s", sep ? "," : "",
1293a3351425Sdougm 			    name, value != NULL ? value : "");
12946185db85Sdougm 		}
1295aed5d200Sdougm 		printed = B_TRUE;
12966185db85Sdougm 	}
1297aed5d200Sdougm skip:
12986185db85Sdougm 	if (name != NULL)
1299a3351425Sdougm 		sa_free_attr_string(name);
13006185db85Sdougm 	if (value != NULL)
1301a3351425Sdougm 		sa_free_attr_string(value);
1302aed5d200Sdougm 	return (printed);
13036185db85Sdougm }
13046185db85Sdougm 
13056185db85Sdougm /*
13066185db85Sdougm  * nfs_format_options(group, hier)
13076185db85Sdougm  *
13086185db85Sdougm  * format all the options on the group into an old-style option
13096185db85Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
13106185db85Sdougm  * options.
13116185db85Sdougm  */
13126185db85Sdougm 
13136185db85Sdougm static char *
13146185db85Sdougm nfs_format_options(sa_group_t group, int hier)
13156185db85Sdougm {
13166185db85Sdougm 	sa_optionset_t options = NULL;
1317a3351425Sdougm 	sa_optionset_t secoptions = NULL;
13186185db85Sdougm 	sa_property_t prop, secprop;
1319a3351425Sdougm 	sa_security_t security = NULL;
13206185db85Sdougm 	char *buff;
13216185db85Sdougm 	size_t buffsize;
1322a3351425Sdougm 	char *sectype = NULL;
1323a3351425Sdougm 	int sep = 0;
1324a3351425Sdougm 
1325a3351425Sdougm 
1326a3351425Sdougm 	buff = malloc(OPT_CHUNK);
1327a3351425Sdougm 	if (buff == NULL) {
1328a3351425Sdougm 		return (NULL);
1329a3351425Sdougm 	}
1330a3351425Sdougm 
1331a3351425Sdougm 	buff[0] = '\0';
1332a3351425Sdougm 	buffsize = OPT_CHUNK;
1333a3351425Sdougm 
1334a3351425Sdougm 	/*
1335a3351425Sdougm 	 * We may have a an optionset relative to this item. format
1336a3351425Sdougm 	 * these if we find them and then add any security definitions.
1337a3351425Sdougm 	 */
13386185db85Sdougm 
13396185db85Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
13406185db85Sdougm 
13416185db85Sdougm 	/*
1342a3351425Sdougm 	 * do the default set first but skip any option that is also
1343a3351425Sdougm 	 * in the protocol specific optionset.
13446185db85Sdougm 	 */
1345a3351425Sdougm 	if (options != NULL) {
1346a3351425Sdougm 		for (prop = sa_get_property(options, NULL);
1347a3351425Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
13486185db85Sdougm 			/*
1349a3351425Sdougm 			 * use this one since we skipped any
1350a3351425Sdougm 			 * of these that were also in
1351a3351425Sdougm 			 * optdefault
13526185db85Sdougm 			 */
1353aed5d200Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1354aed5d200Sdougm 			    prop, sep))
1355aed5d200Sdougm 				sep = 1;
1356a3351425Sdougm 			if (buff == NULL) {
1357a3351425Sdougm 				/*
1358a3351425Sdougm 				 * buff could become NULL if there
1359a3351425Sdougm 				 * isn't enough memory for
1360a3351425Sdougm 				 * nfs_sprint_option to realloc()
1361a3351425Sdougm 				 * as necessary. We can't really
1362a3351425Sdougm 				 * do anything about it at this
1363a3351425Sdougm 				 * point so we return NULL.  The
1364a3351425Sdougm 				 * caller should handle the
1365a3351425Sdougm 				 * failure.
1366a3351425Sdougm 				 */
13676185db85Sdougm 				if (options != NULL)
1368a3351425Sdougm 					sa_free_derived_optionset(
1369a3351425Sdougm 					    options);
13706185db85Sdougm 				return (buff);
13716185db85Sdougm 			}
1372a3351425Sdougm 		}
1373a3351425Sdougm 	}
1374a3351425Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1375a3351425Sdougm 	    "nfs", hier);
1376a3351425Sdougm 	if (secoptions != NULL) {
1377a3351425Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
1378a3351425Sdougm 		    secprop != NULL;
1379a3351425Sdougm 		    secprop = sa_get_next_property(secprop)) {
1380a3351425Sdougm 			sectype = sa_get_property_attr(secprop, "type");
1381a3351425Sdougm 			security =
1382a3351425Sdougm 			    (sa_security_t)sa_get_derived_security(
1383a3351425Sdougm 			    group, sectype, "nfs", hier);
1384a3351425Sdougm 			if (security != NULL) {
1385a3351425Sdougm 				if (sectype != NULL) {
1386a3351425Sdougm 					prop = sa_create_property(
1387a3351425Sdougm 					    "sec", sectype);
1388aed5d200Sdougm 					if (prop == NULL)
1389aed5d200Sdougm 						goto err;
1390aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1391aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1392aed5d200Sdougm 						sep = 1;
1393a3351425Sdougm 					(void) sa_remove_property(prop);
1394aed5d200Sdougm 					if (buff == NULL)
1395aed5d200Sdougm 						goto err;
1396a3351425Sdougm 				}
1397a3351425Sdougm 				for (prop = sa_get_property(security,
1398a3351425Sdougm 				    NULL); prop != NULL;
1399a3351425Sdougm 				    prop = sa_get_next_property(prop)) {
1400aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1401aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1402aed5d200Sdougm 						sep = 1;
1403a3351425Sdougm 					if (buff == NULL)
1404a3351425Sdougm 						goto err;
1405a3351425Sdougm 				}
1406a3351425Sdougm 				sa_free_derived_optionset(security);
1407a3351425Sdougm 			}
1408a3351425Sdougm 			if (sectype != NULL)
1409a3351425Sdougm 				sa_free_attr_string(sectype);
14106185db85Sdougm 		}
14116185db85Sdougm 		sa_free_derived_optionset(secoptions);
14126185db85Sdougm 	}
1413a3351425Sdougm 
14146185db85Sdougm 	if (options != NULL)
1415a3351425Sdougm 		sa_free_derived_optionset(options);
1416a3351425Sdougm 	return (buff);
1417a3351425Sdougm 
1418a3351425Sdougm err:
1419a3351425Sdougm 	/*
1420a3351425Sdougm 	 * If we couldn't allocate memory for option printing, we need
1421a3351425Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
1422a3351425Sdougm 	 */
1423a3351425Sdougm 	if (secoptions != NULL)
1424a3351425Sdougm 		sa_free_derived_optionset(secoptions);
1425a3351425Sdougm 	if (security != NULL)
1426a3351425Sdougm 		sa_free_derived_optionset(security);
1427a3351425Sdougm 	if (sectype != NULL)
1428a3351425Sdougm 		sa_free_attr_string(sectype);
1429a3351425Sdougm 	if (options != NULL)
1430a3351425Sdougm 		sa_free_derived_optionset(options);
14316185db85Sdougm 	return (buff);
14326185db85Sdougm }
1433a3351425Sdougm 
14346185db85Sdougm /*
14356185db85Sdougm  * Append an entry to the nfslogtab file
14366185db85Sdougm  */
14376185db85Sdougm static int
14386185db85Sdougm nfslogtab_add(dir, buffer, tag)
14396185db85Sdougm 	char *dir, *buffer, *tag;
14406185db85Sdougm {
14416185db85Sdougm 	FILE *f;
14426185db85Sdougm 	struct logtab_ent lep;
14436185db85Sdougm 	int error = 0;
14446185db85Sdougm 
14456185db85Sdougm 	/*
14466185db85Sdougm 	 * Open the file for update and create it if necessary.
14476185db85Sdougm 	 * This may leave the I/O offset at the end of the file,
14486185db85Sdougm 	 * so rewind back to the beginning of the file.
14496185db85Sdougm 	 */
14506185db85Sdougm 	f = fopen(NFSLOGTAB, "a+");
14516185db85Sdougm 	if (f == NULL) {
14526185db85Sdougm 		error = errno;
14536185db85Sdougm 		goto out;
14546185db85Sdougm 	}
14556185db85Sdougm 	rewind(f);
14566185db85Sdougm 
14576185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1458549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1459a3351425Sdougm 		    "share complete, however failed to lock %s "
1460a3351425Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
14616185db85Sdougm 		error = -1;
14626185db85Sdougm 		goto out;
14636185db85Sdougm 	}
14646185db85Sdougm 
14656185db85Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
1466549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1467a3351425Sdougm 		    "share complete, however could not deactivate "
1468a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14696185db85Sdougm 		error = -1;
14706185db85Sdougm 		goto out;
14716185db85Sdougm 	}
14726185db85Sdougm 
14736185db85Sdougm 	/*
14746185db85Sdougm 	 * Remove entries matching buffer and sharepoint since we're
14756185db85Sdougm 	 * going to replace it with perhaps an entry with a new tag.
14766185db85Sdougm 	 */
14776185db85Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
1478549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1479a3351425Sdougm 		    "share complete, however could not remove matching "
1480a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14816185db85Sdougm 		error = -1;
14826185db85Sdougm 		goto out;
14836185db85Sdougm 	}
14846185db85Sdougm 
14856185db85Sdougm 	/*
14866185db85Sdougm 	 * Deactivate all active entries matching this sharepoint
14876185db85Sdougm 	 */
14886185db85Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
1489549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1490a3351425Sdougm 		    "share complete, however could not deactivate matching "
1491a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14926185db85Sdougm 		error = -1;
14936185db85Sdougm 		goto out;
14946185db85Sdougm 	}
14956185db85Sdougm 
14966185db85Sdougm 	lep.le_buffer = buffer;
14976185db85Sdougm 	lep.le_path = dir;
14986185db85Sdougm 	lep.le_tag = tag;
14996185db85Sdougm 	lep.le_state = LES_ACTIVE;
15006185db85Sdougm 
15016185db85Sdougm 	/*
15026185db85Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
15036185db85Sdougm 	 */
15046185db85Sdougm 	if (logtab_putent(f, &lep) < 0) {
1505549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1506a3351425Sdougm 		    "share complete, however could not add %s to %s\n"),
1507a3351425Sdougm 		    dir, NFSLOGTAB);
15086185db85Sdougm 		error = -1;
15096185db85Sdougm 	}
15106185db85Sdougm 
15116185db85Sdougm out:
15126185db85Sdougm 	if (f != NULL)
15136185db85Sdougm 		(void) fclose(f);
15146185db85Sdougm 	return (error);
15156185db85Sdougm }
15166185db85Sdougm 
15176185db85Sdougm /*
15186185db85Sdougm  * Deactivate an entry from the nfslogtab file
15196185db85Sdougm  */
15206185db85Sdougm static int
15216185db85Sdougm nfslogtab_deactivate(path)
15226185db85Sdougm 	char *path;
15236185db85Sdougm {
15246185db85Sdougm 	FILE *f;
15256185db85Sdougm 	int error = 0;
15266185db85Sdougm 
15276185db85Sdougm 	f = fopen(NFSLOGTAB, "r+");
15286185db85Sdougm 	if (f == NULL) {
15296185db85Sdougm 		error = errno;
15306185db85Sdougm 		goto out;
15316185db85Sdougm 	}
15326185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
15336185db85Sdougm 		error = errno;
1534549ec3ffSdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
1535a3351425Sdougm 		    "share complete, however could not lock %s for "
1536a3351425Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
15376185db85Sdougm 		goto out;
15386185db85Sdougm 	}
15396185db85Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
15406185db85Sdougm 		error = -1;
15416185db85Sdougm 		(void) fprintf(stderr,
1542a3351425Sdougm 		    dgettext(TEXT_DOMAIN,
1543a3351425Sdougm 		    "share complete, however could not "
1544a3351425Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
15456185db85Sdougm 		goto out;
15466185db85Sdougm 	}
15476185db85Sdougm 
15486185db85Sdougm out:	if (f != NULL)
15496185db85Sdougm 		(void) fclose(f);
15506185db85Sdougm 
15516185db85Sdougm 	return (error);
15526185db85Sdougm }
15536185db85Sdougm 
1554546405c3Sdougm /*
1555546405c3Sdougm  * check_public(group, skipshare)
1556546405c3Sdougm  *
1557546405c3Sdougm  * Check the group for any shares that have the public property
1558546405c3Sdougm  * enabled. We skip "skipshare" since that is the one we are
1559546405c3Sdougm  * working with. This is a separate function to make handling
1560546405c3Sdougm  * subgroups simpler. Returns true if there is a share with public.
1561546405c3Sdougm  */
1562546405c3Sdougm static int
1563546405c3Sdougm check_public(sa_group_t group, sa_share_t skipshare)
1564546405c3Sdougm {
1565546405c3Sdougm 	int exists = B_FALSE;
1566546405c3Sdougm 	sa_share_t share;
1567546405c3Sdougm 	sa_optionset_t opt;
1568546405c3Sdougm 	sa_property_t prop;
1569546405c3Sdougm 	char *shared;
1570546405c3Sdougm 
1571546405c3Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
1572546405c3Sdougm 	    share = sa_get_next_share(share)) {
1573546405c3Sdougm 		if (share == skipshare)
1574546405c3Sdougm 			continue;
1575546405c3Sdougm 
1576546405c3Sdougm 		opt = sa_get_optionset(share, "nfs");
1577546405c3Sdougm 		if (opt == NULL)
1578546405c3Sdougm 			continue;
1579546405c3Sdougm 		prop = sa_get_property(opt, "public");
1580546405c3Sdougm 		if (prop == NULL)
1581546405c3Sdougm 			continue;
1582546405c3Sdougm 		shared = sa_get_share_attr(share, "shared");
1583546405c3Sdougm 		if (shared != NULL) {
1584546405c3Sdougm 			exists = strcmp(shared, "true") == 0;
1585546405c3Sdougm 			sa_free_attr_string(shared);
1586546405c3Sdougm 			if (exists == B_TRUE)
1587546405c3Sdougm 				break;
1588546405c3Sdougm 		}
1589546405c3Sdougm 	}
1590546405c3Sdougm 
1591546405c3Sdougm 	return (exists);
1592546405c3Sdougm }
1593546405c3Sdougm 
15946185db85Sdougm /*
15956185db85Sdougm  * public_exists(share)
15966185db85Sdougm  *
15976185db85Sdougm  * check to see if public option is set on any other share than the
1598546405c3Sdougm  * one specified. Need to check zfs sub-groups as well as the top
1599546405c3Sdougm  * level groups.
16006185db85Sdougm  */
16016185db85Sdougm static int
16026185db85Sdougm public_exists(sa_share_t skipshare)
16036185db85Sdougm {
16046185db85Sdougm 	sa_group_t group;
1605549ec3ffSdougm 	sa_handle_t handle;
16066185db85Sdougm 
1607549ec3ffSdougm 	group = sa_get_parent_group(skipshare);
1608549ec3ffSdougm 	if (group == NULL)
1609a3351425Sdougm 		return (SA_NO_SUCH_GROUP);
1610549ec3ffSdougm 
1611549ec3ffSdougm 	handle = sa_find_group_handle(group);
1612549ec3ffSdougm 	if (handle == NULL)
1613a3351425Sdougm 		return (SA_SYSTEM_ERR);
1614549ec3ffSdougm 
1615549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
16166185db85Sdougm 	    group = sa_get_next_group(group)) {
1617546405c3Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
1618546405c3Sdougm 		if (sa_group_is_zfs(group)) {
1619546405c3Sdougm 			sa_group_t subgroup;
1620546405c3Sdougm 			for (subgroup = sa_get_sub_group(group);
1621546405c3Sdougm 			    subgroup != NULL;
1622546405c3Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
1623546405c3Sdougm 				if (check_public(subgroup, skipshare))
1624546405c3Sdougm 					return (B_TRUE);
16256185db85Sdougm 			}
1626546405c3Sdougm 		} else {
1627546405c3Sdougm 			if (check_public(group, skipshare))
1628546405c3Sdougm 				return (B_TRUE);
16296185db85Sdougm 		}
16306185db85Sdougm 	}
1631546405c3Sdougm 	return (B_FALSE);
16326185db85Sdougm }
16336185db85Sdougm 
16346185db85Sdougm /*
16356185db85Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
16366185db85Sdougm  * implementation that it is to enable the share. This entails
16376185db85Sdougm  * converting the path and options into the appropriate ioctl
16386185db85Sdougm  * calls. It is assumed that all error checking of paths, etc. were
16396185db85Sdougm  * done earlier.
16406185db85Sdougm  */
16416185db85Sdougm static int
16426185db85Sdougm nfs_enable_share(sa_share_t share)
16436185db85Sdougm {
16446185db85Sdougm 	struct exportdata export;
16456185db85Sdougm 	sa_optionset_t secoptlist;
16466185db85Sdougm 	struct secinfo *sp;
16476185db85Sdougm 	int num_secinfo;
16486185db85Sdougm 	sa_optionset_t opt;
16496185db85Sdougm 	sa_security_t sec;
16506185db85Sdougm 	sa_property_t prop;
16516185db85Sdougm 	char *path;
16526185db85Sdougm 	int err = SA_OK;
1653546405c3Sdougm 	int i;
1654ecd6cf80Smarks 	int iszfs;
16556185db85Sdougm 
16566185db85Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
16576185db85Sdougm 	(void) signal(SIGSYS, SIG_IGN);
16586185db85Sdougm 
16596185db85Sdougm 	/* get the path since it is important in several places */
16606185db85Sdougm 	path = sa_get_share_attr(share, "path");
16616185db85Sdougm 	if (path == NULL)
1662a3351425Sdougm 		return (SA_NO_SUCH_PATH);
16636185db85Sdougm 
1664ecd6cf80Smarks 	iszfs = sa_path_is_zfs(path);
16656185db85Sdougm 	/*
16666185db85Sdougm 	 * find the optionsets and security sets.  There may not be
16676185db85Sdougm 	 * any or there could be one or two for each of optionset and
16686185db85Sdougm 	 * security may have multiple, one per security type per
16696185db85Sdougm 	 * protocol type.
16706185db85Sdougm 	 */
16716185db85Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
16726185db85Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
16736185db85Sdougm 	if (secoptlist != NULL)
1674a3351425Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
16756185db85Sdougm 	else
1676a3351425Sdougm 		num_secinfo = 1;
16776185db85Sdougm 
16786185db85Sdougm 	/*
16796185db85Sdougm 	 * walk through the options and fill in the structure
16806185db85Sdougm 	 * appropriately.
16816185db85Sdougm 	 */
16826185db85Sdougm 
16836185db85Sdougm 	(void) memset(&export, '\0', sizeof (export));
16846185db85Sdougm 
16856185db85Sdougm 	/*
16866185db85Sdougm 	 * do non-security options first since there is only one after
16876185db85Sdougm 	 * the derived group is constructed.
16886185db85Sdougm 	 */
16896185db85Sdougm 	export.ex_version = EX_CURRENT_VERSION;
16906185db85Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
16916185db85Sdougm 	export.ex_index = NULL;
16926185db85Sdougm 	export.ex_path = path;
16936185db85Sdougm 	export.ex_pathlen = strlen(path) + 1;
16946185db85Sdougm 
16956185db85Sdougm 	if (opt != NULL)
1696a3351425Sdougm 		err = fill_export_from_optionset(&export, opt);
16976185db85Sdougm 
16986185db85Sdougm 	/*
16996185db85Sdougm 	 * check to see if "public" is set. If it is, then make sure
17006185db85Sdougm 	 * no other share has it set. If it is already used, fail.
17016185db85Sdougm 	 */
17026185db85Sdougm 
17036185db85Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(share)) {
1704a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1705a3351425Sdougm 		    "NFS: Cannot share more than one file "
1706a3351425Sdougm 		    "system with 'public' property\n"));
1707a3351425Sdougm 		err = SA_NOT_ALLOWED;
1708a3351425Sdougm 		goto out;
17096185db85Sdougm 	}
17106185db85Sdougm 
1711546405c3Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
17126185db85Sdougm 	if (sp == NULL) {
1713a3351425Sdougm 		err = SA_NO_MEMORY;
1714546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1715546405c3Sdougm 		    "NFS: NFS: no memory for security\n"));
1716546405c3Sdougm 		goto out;
1717546405c3Sdougm 	}
1718546405c3Sdougm 	export.ex_secinfo = sp;
1719546405c3Sdougm 	/* get default secinfo */
1720546405c3Sdougm 	export.ex_seccnt = num_secinfo;
1721546405c3Sdougm 	/*
1722546405c3Sdougm 	 * since we must have one security option defined, we
1723546405c3Sdougm 	 * init to the default and then override as we find
1724546405c3Sdougm 	 * defined security options. This handles the case
1725546405c3Sdougm 	 * where we have no defined options but we need to set
1726546405c3Sdougm 	 * up one.
1727546405c3Sdougm 	 */
1728546405c3Sdougm 	sp[0].s_window = DEF_WIN;
1729546405c3Sdougm 	sp[0].s_rootnames = NULL;
1730546405c3Sdougm 	/* setup a default in case no properties defined */
1731546405c3Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1732546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1733546405c3Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
1734546405c3Sdougm 		    "get default security mode\n"));
1735546405c3Sdougm 		err = SA_CONFIG_ERR;
1736546405c3Sdougm 	}
1737546405c3Sdougm 	if (secoptlist != NULL) {
1738546405c3Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
1739546405c3Sdougm 		    prop != NULL && i < num_secinfo;
1740546405c3Sdougm 		    prop = sa_get_next_property(prop), i++) {
1741546405c3Sdougm 			char *sectype;
1742a3351425Sdougm 				sectype = sa_get_property_attr(prop, "type");
1743546405c3Sdougm 			/*
1744546405c3Sdougm 			 * if sectype is NULL, we probably
1745546405c3Sdougm 			 * have a memory problem and can't get
1746546405c3Sdougm 			 * the correct values. Rather than
1747546405c3Sdougm 			 * exporting with incorrect security,
1748546405c3Sdougm 			 * don't share it.
1749546405c3Sdougm 			 */
1750546405c3Sdougm 			if (sectype == NULL) {
1751546405c3Sdougm 				err = SA_NO_MEMORY;
1752546405c3Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1753546405c3Sdougm 				    "NFS: Cannot share %s: "
1754546405c3Sdougm 				    "no memory\n"), path);
1755546405c3Sdougm 				goto out;
1756a3351425Sdougm 			}
1757546405c3Sdougm 			sec = (sa_security_t)sa_get_derived_security(
1758546405c3Sdougm 			    share, sectype, "nfs", 1);
1759546405c3Sdougm 			sp[i].s_window = DEF_WIN;
1760546405c3Sdougm 			sp[i].s_rootcnt = 0;
1761546405c3Sdougm 			sp[i].s_rootnames = NULL;
1762546405c3Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
1763546405c3Sdougm 			if (sec != NULL)
1764546405c3Sdougm 				sa_free_derived_security(sec);
1765546405c3Sdougm 			if (sectype != NULL)
1766546405c3Sdougm 				sa_free_attr_string(sectype);
17676185db85Sdougm 		}
1768546405c3Sdougm 	}
1769546405c3Sdougm 	/*
1770546405c3Sdougm 	 * when we get here, we can do the exportfs system call and
1771546405c3Sdougm 	 * initiate thinsg. We probably want to enable the nfs.server
1772546405c3Sdougm 	 * service first if it isn't running within SMF.
1773546405c3Sdougm 	 */
1774546405c3Sdougm 	/* check nfs.server status and start if needed */
1775546405c3Sdougm 	/* now add the share to the internal tables */
1776546405c3Sdougm 	printarg(path, &export);
1777546405c3Sdougm 	/*
1778546405c3Sdougm 	 * call the exportfs system call which is implemented
1779546405c3Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
1780546405c3Sdougm 	 */
1781ecd6cf80Smarks 	if (iszfs) {
1782ecd6cf80Smarks 		struct exportfs_args ea;
1783ecd6cf80Smarks 		share_t sh;
1784ecd6cf80Smarks 		char *str;
1785ecd6cf80Smarks 		priv_set_t *priv_effective;
1786ecd6cf80Smarks 		int privileged;
1787ecd6cf80Smarks 
1788ecd6cf80Smarks 		/*
1789ecd6cf80Smarks 		 * If we aren't a privileged user
1790ecd6cf80Smarks 		 * and NFS server service isn't running
1791ecd6cf80Smarks 		 * then print out an error message
1792ecd6cf80Smarks 		 * and return EPERM
1793ecd6cf80Smarks 		 */
1794ecd6cf80Smarks 
1795ecd6cf80Smarks 		priv_effective = priv_allocset();
1796ecd6cf80Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
1797ecd6cf80Smarks 
1798ecd6cf80Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
1799ecd6cf80Smarks 		priv_freeset(priv_effective);
1800ecd6cf80Smarks 
1801ecd6cf80Smarks 		if (!privileged &&
1802ecd6cf80Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
1803ecd6cf80Smarks 			err = 0;
1804ecd6cf80Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
1805ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1806ecd6cf80Smarks 				    "NFS: Cannot share remote "
1807ecd6cf80Smarks 				    "filesystem: %s\n"), path);
1808ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1809ecd6cf80Smarks 				    "NFS: Service needs to be enabled "
1810ecd6cf80Smarks 				    "by a privileged user\n"));
1811ecd6cf80Smarks 				err = SA_SYSTEM_ERR;
1812ecd6cf80Smarks 				errno = EPERM;
1813ecd6cf80Smarks 			}
1814ecd6cf80Smarks 			free(str);
1815ecd6cf80Smarks 		}
1816ecd6cf80Smarks 
1817ecd6cf80Smarks 		if (err == 0) {
1818ecd6cf80Smarks 			ea.dname = path;
1819ecd6cf80Smarks 			ea.uex = &export;
1820ecd6cf80Smarks 
1821ecd6cf80Smarks 			sa_sharetab_fill_zfs(share, &sh, "nfs");
1822da6c28aaSamw 			err = sa_share_zfs(share, path, &sh,
1823da6c28aaSamw 			    &ea, ZFS_SHARE_NFS);
1824ecd6cf80Smarks 			sa_emptyshare(&sh);
1825ecd6cf80Smarks 		}
1826ecd6cf80Smarks 	} else {
1827ecd6cf80Smarks 		err = exportfs(path, &export);
1828ecd6cf80Smarks 	}
1829ecd6cf80Smarks 
1830ecd6cf80Smarks 	if (err < 0) {
1831546405c3Sdougm 		err = SA_SYSTEM_ERR;
1832546405c3Sdougm 		switch (errno) {
1833546405c3Sdougm 		case EREMOTE:
1834546405c3Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1835ecd6cf80Smarks 			    "NFS: Cannot share filesystems "
1836ecd6cf80Smarks 			    "in non-global zones: %s\n"), path);
1837ecd6cf80Smarks 			err = SA_NOT_SUPPORTED;
1838546405c3Sdougm 			break;
1839546405c3Sdougm 		case EPERM:
1840546405c3Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
1841a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1842ecd6cf80Smarks 				    "NFS: Cannot share file systems "
1843546405c3Sdougm 				    "in non-global zones: %s\n"), path);
1844546405c3Sdougm 				err = SA_NOT_SUPPORTED;
1845a3351425Sdougm 				break;
1846a3351425Sdougm 			}
1847546405c3Sdougm 			err = SA_NO_PERMISSION;
1848546405c3Sdougm 			/* FALLTHROUGH */
1849546405c3Sdougm 		default:
1850546405c3Sdougm 			break;
18516185db85Sdougm 		}
1852546405c3Sdougm 	} else {
1853546405c3Sdougm 		/* update sharetab with an add/modify */
1854ecd6cf80Smarks 		if (!iszfs) {
1855ecd6cf80Smarks 			(void) sa_update_sharetab(share, "nfs");
1856ecd6cf80Smarks 		}
18576185db85Sdougm 	}
18586185db85Sdougm 
18596185db85Sdougm 	if (err == SA_OK) {
18606185db85Sdougm 		/*
18616185db85Sdougm 		 * enable services as needed. This should probably be
18626185db85Sdougm 		 * done elsewhere in order to minimize the calls to
18636185db85Sdougm 		 * check services.
18646185db85Sdougm 		 */
18656185db85Sdougm 		/*
18666185db85Sdougm 		 * check to see if logging and other services need to
18676185db85Sdougm 		 * be triggered, but only if there wasn't an
18686185db85Sdougm 		 * error. This is probably where sharetab should be
18696185db85Sdougm 		 * updated with the NFS specific entry.
18706185db85Sdougm 		 */
1871a3351425Sdougm 		if (export.ex_flags & EX_LOG) {
1872a3351425Sdougm 			/* enable logging */
1873a3351425Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
1874a3351425Sdougm 			    export.ex_tag) != 0) {
1875a3351425Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1876a3351425Sdougm 				    "Could not enable logging for %s\n"),
1877a3351425Sdougm 				    path);
1878a3351425Sdougm 			}
1879a3351425Sdougm 			_check_services(service_list_logging);
1880a3351425Sdougm 		} else {
1881a3351425Sdougm 			/*
1882a3351425Sdougm 			 * don't have logging so remove it from file. It might
1883a3351425Sdougm 			 * not be thre, but that doesn't matter.
1884a3351425Sdougm 			 */
1885a3351425Sdougm 			(void) nfslogtab_deactivate(path);
1886a3351425Sdougm 			_check_services(service_list_default);
18876185db85Sdougm 		}
18886185db85Sdougm 	}
18896185db85Sdougm 
18906185db85Sdougm out:
18916185db85Sdougm 	if (path != NULL)
1892a3351425Sdougm 		free(path);
18936185db85Sdougm 
18946185db85Sdougm 	cleanup_export(&export);
18956185db85Sdougm 	if (opt != NULL)
1896a3351425Sdougm 		sa_free_derived_optionset(opt);
18976185db85Sdougm 	if (secoptlist != NULL)
1898a3351425Sdougm 		(void) sa_destroy_optionset(secoptlist);
18996185db85Sdougm 	return (err);
19006185db85Sdougm }
19016185db85Sdougm 
19026185db85Sdougm /*
19038e314a44Sdougm  * nfs_disable_share(share, path)
19046185db85Sdougm  *
19058e314a44Sdougm  * Unshare the specified share. Note that "path" is the same path as
19068e314a44Sdougm  * what is in the "share" object. It is passed in to avoid an
19078e314a44Sdougm  * additional lookup. A missing "path" value makes this a no-op
19088e314a44Sdougm  * function.
19096185db85Sdougm  */
19106185db85Sdougm static int
1911ecd6cf80Smarks nfs_disable_share(sa_share_t share, char *path)
19126185db85Sdougm {
19136185db85Sdougm 	int err;
19146185db85Sdougm 	int ret = SA_OK;
1915ecd6cf80Smarks 	int iszfs;
19168e314a44Sdougm 	sa_group_t parent;
19175b6e0c46Sdougm 	sa_handle_t handle;
1918ecd6cf80Smarks 
19198e314a44Sdougm 	if (path == NULL)
19208e314a44Sdougm 		return (ret);
1921ecd6cf80Smarks 
19228e314a44Sdougm 	/*
19238e314a44Sdougm 	 * If the share is in a ZFS group we need to handle it
19248e314a44Sdougm 	 * differently.  Just being on a ZFS file system isn't
19258e314a44Sdougm 	 * enough since we may be in a legacy share case.
19268e314a44Sdougm 	 */
19278e314a44Sdougm 	parent = sa_get_parent_group(share);
19288e314a44Sdougm 	iszfs = sa_group_is_zfs(parent);
19298e314a44Sdougm 	if (iszfs) {
19308e314a44Sdougm 		struct exportfs_args ea;
19318e314a44Sdougm 		share_t sh = { 0 };
19328e314a44Sdougm 		ea.dname = path;
19338e314a44Sdougm 		ea.uex = NULL;
19348e314a44Sdougm 		sh.sh_path = path;
19358e314a44Sdougm 		sh.sh_fstype = "nfs";
19368e314a44Sdougm 
19378e314a44Sdougm 		err = sa_share_zfs(share, path, &sh,
19388e314a44Sdougm 		    &ea, ZFS_UNSHARE_NFS);
19398e314a44Sdougm 	} else {
19408e314a44Sdougm 		err = exportfs(path, NULL);
19418e314a44Sdougm 	}
19428e314a44Sdougm 	if (err < 0) {
19438e314a44Sdougm 		/*
19448e314a44Sdougm 		 * TBD: only an error in some
19458e314a44Sdougm 		 * cases - need better analysis
19468e314a44Sdougm 		 */
19478e314a44Sdougm 		switch (errno) {
19488e314a44Sdougm 		case EPERM:
19498e314a44Sdougm 		case EACCES:
19508e314a44Sdougm 			ret = SA_NO_PERMISSION;
19518e314a44Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
19528e314a44Sdougm 				ret = SA_NOT_SUPPORTED;
19538e314a44Sdougm 			}
19548e314a44Sdougm 			break;
19558e314a44Sdougm 		case EINVAL:
19568e314a44Sdougm 		case ENOENT:
19578e314a44Sdougm 			ret = SA_NO_SUCH_PATH;
1958ecd6cf80Smarks 			break;
1959a3351425Sdougm 			default:
1960a3351425Sdougm 				ret = SA_SYSTEM_ERR;
1961ecd6cf80Smarks 			break;
19626185db85Sdougm 		}
19636185db85Sdougm 	}
19648e314a44Sdougm 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
19655b6e0c46Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
19668e314a44Sdougm 		if (!iszfs)
19675b6e0c46Sdougm 			(void) sa_delete_sharetab(handle, path, "nfs");
19688e314a44Sdougm 		/* just in case it was logged */
19698e314a44Sdougm 		(void) nfslogtab_deactivate(path);
19708e314a44Sdougm 	}
19716185db85Sdougm 	return (ret);
19726185db85Sdougm }
19736185db85Sdougm 
19746185db85Sdougm /*
19756185db85Sdougm  * check ro vs rw values.  Over time this may get beefed up.
19766185db85Sdougm  * for now it just does simple checks.
19776185db85Sdougm  */
19786185db85Sdougm 
19796185db85Sdougm static int
19806185db85Sdougm check_rorw(char *v1, char *v2)
19816185db85Sdougm {
19826185db85Sdougm 	int ret = SA_OK;
19836185db85Sdougm 	if (strcmp(v1, v2) == 0)
1984a3351425Sdougm 		ret = SA_VALUE_CONFLICT;
19856185db85Sdougm 	return (ret);
19866185db85Sdougm }
19876185db85Sdougm 
19886185db85Sdougm /*
19896185db85Sdougm  * nfs_validate_property(property, parent)
19906185db85Sdougm  *
19916185db85Sdougm  * Check that the property has a legitimate value for its type.
19926185db85Sdougm  */
19936185db85Sdougm 
19946185db85Sdougm static int
19956185db85Sdougm nfs_validate_property(sa_property_t property, sa_optionset_t parent)
19966185db85Sdougm {
19976185db85Sdougm 	int ret = SA_OK;
19986185db85Sdougm 	char *propname;
19996185db85Sdougm 	char *other;
20006185db85Sdougm 	int optindex;
20016185db85Sdougm 	nfsl_config_t *configlist;
20026185db85Sdougm 	sa_group_t parent_group;
20036185db85Sdougm 	char *value;
20046185db85Sdougm 
20056185db85Sdougm 	propname = sa_get_property_attr(property, "type");
20066185db85Sdougm 
20076185db85Sdougm 	if ((optindex = findopt(propname)) < 0)
2008a3351425Sdougm 		ret = SA_NO_SUCH_PROP;
20096185db85Sdougm 
20106185db85Sdougm 	/* need to validate value range here as well */
20116185db85Sdougm 
20126185db85Sdougm 	if (ret == SA_OK) {
2013a3351425Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
2014a3351425Sdougm 		if (optdefs[optindex].share && !sa_is_share(parent_group))
2015a3351425Sdougm 			ret = SA_PROP_SHARE_ONLY;
20166185db85Sdougm 	}
20176185db85Sdougm 	if (ret == SA_OK) {
2018a3351425Sdougm 		value = sa_get_property_attr(property, "value");
2019a3351425Sdougm 		if (value != NULL) {
2020a3351425Sdougm 			/* first basic type checking */
2021a3351425Sdougm 			switch (optdefs[optindex].type) {
2022a3351425Sdougm 			case OPT_TYPE_NUMBER:
2023a3351425Sdougm 				/* check that the value is all digits */
2024a3351425Sdougm 				if (!is_a_number(value))
2025a3351425Sdougm 					ret = SA_BAD_VALUE;
2026a3351425Sdougm 				break;
2027a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2028a3351425Sdougm 				if (strlen(value) == 0 ||
2029a3351425Sdougm 				    strcasecmp(value, "true") == 0 ||
2030a3351425Sdougm 				    strcmp(value, "1") == 0 ||
2031a3351425Sdougm 				    strcasecmp(value, "false") == 0 ||
2032a3351425Sdougm 				    strcmp(value, "0") == 0) {
2033a3351425Sdougm 					ret = SA_OK;
2034a3351425Sdougm 				} else {
2035a3351425Sdougm 					ret = SA_BAD_VALUE;
2036a3351425Sdougm 				}
2037a3351425Sdougm 				break;
2038a3351425Sdougm 			case OPT_TYPE_USER:
2039a3351425Sdougm 				if (!is_a_number(value)) {
2040a3351425Sdougm 					struct passwd *pw;
2041a3351425Sdougm 					/*
2042a3351425Sdougm 					 * in this case it would have to be a
2043a3351425Sdougm 					 * user name
2044a3351425Sdougm 					 */
2045a3351425Sdougm 					pw = getpwnam(value);
2046a3351425Sdougm 					if (pw == NULL)
2047a3351425Sdougm 						ret = SA_BAD_VALUE;
2048a3351425Sdougm 					endpwent();
2049a3351425Sdougm 				} else {
2050a3351425Sdougm 					uint64_t intval;
2051a3351425Sdougm 					intval = strtoull(value, NULL, 0);
2052a3351425Sdougm 					if (intval > UID_MAX && intval != ~0)
2053a3351425Sdougm 						ret = SA_BAD_VALUE;
2054a3351425Sdougm 				}
2055a3351425Sdougm 				break;
2056a3351425Sdougm 			case OPT_TYPE_FILE:
2057a3351425Sdougm 				if (strcmp(value, "..") == 0 ||
2058a3351425Sdougm 				    strchr(value, '/') != NULL) {
2059a3351425Sdougm 					ret = SA_BAD_VALUE;
2060a3351425Sdougm 				}
2061a3351425Sdougm 				break;
2062a3351425Sdougm 			case OPT_TYPE_ACCLIST:
2063a3351425Sdougm 				/*
2064a3351425Sdougm 				 * access list handling. Should eventually
2065a3351425Sdougm 				 * validate that all the values make sense.
2066a3351425Sdougm 				 * Also, ro and rw may have cross value
2067a3351425Sdougm 				 * conflicts.
2068a3351425Sdougm 				 */
2069a3351425Sdougm 				if (strcmp(propname, SHOPT_RO) == 0)
2070a3351425Sdougm 					other = SHOPT_RW;
2071a3351425Sdougm 				else if (strcmp(propname, SHOPT_RW) == 0)
2072a3351425Sdougm 					other = SHOPT_RO;
2073a3351425Sdougm 				else
2074a3351425Sdougm 					other = NULL;
2075a3351425Sdougm 
2076a3351425Sdougm 				if (other != NULL && parent != NULL) {
2077a3351425Sdougm 					/* compare rw(ro) with ro(rw) */
2078a3351425Sdougm 					sa_property_t oprop;
2079a3351425Sdougm 					oprop = sa_get_property(parent, other);
2080a3351425Sdougm 					if (oprop != NULL) {
2081a3351425Sdougm 						/*
2082a3351425Sdougm 						 * only potential
2083a3351425Sdougm 						 * confusion if other
2084a3351425Sdougm 						 * exists
2085a3351425Sdougm 						 */
2086a3351425Sdougm 						char *ovalue;
2087a3351425Sdougm 						ovalue = sa_get_property_attr(
2088a3351425Sdougm 						    oprop, "value");
2089a3351425Sdougm 						if (ovalue != NULL) {
2090a3351425Sdougm 							ret = check_rorw(value,
2091a3351425Sdougm 							    ovalue);
2092a3351425Sdougm 							sa_free_attr_string(
2093a3351425Sdougm 							    ovalue);
2094a3351425Sdougm 						}
2095a3351425Sdougm 					}
2096a3351425Sdougm 				}
2097a3351425Sdougm 				break;
2098a3351425Sdougm 			case OPT_TYPE_LOGTAG:
2099a3351425Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
2100a3351425Sdougm 					int error;
2101a3351425Sdougm 					if (value == NULL ||
2102a3351425Sdougm 					    strlen(value) == 0) {
2103a3351425Sdougm 						if (value != NULL)
2104a3351425Sdougm 							sa_free_attr_string(
2105a3351425Sdougm 							    value);
2106a3351425Sdougm 						value = strdup("global");
2107a3351425Sdougm 					}
2108a3351425Sdougm 					if (value != NULL &&
2109a3351425Sdougm 					    nfsl_findconfig(configlist, value,
2110a3351425Sdougm 					    &error) == NULL) {
2111a3351425Sdougm 						ret = SA_BAD_VALUE;
2112a3351425Sdougm 					}
2113aed5d200Sdougm 					/* Must always free when done */
2114aed5d200Sdougm 					nfsl_freeconfig_list(&configlist);
2115a3351425Sdougm 				} else {
2116a3351425Sdougm 					ret = SA_CONFIG_ERR;
2117a3351425Sdougm 				}
2118a3351425Sdougm 				break;
2119a3351425Sdougm 			case OPT_TYPE_STRING:
2120a3351425Sdougm 				/* whatever is here should be ok */
2121a3351425Sdougm 				break;
2122a3351425Sdougm 			case OPT_TYPE_SECURITY:
2123a3351425Sdougm 				/*
2124a3351425Sdougm 				 * The "sec" property isn't used in the
2125a3351425Sdougm 				 * non-legacy parts of sharemgr. We need to
2126a3351425Sdougm 				 * reject it here. For legacy, it is pulled
2127a3351425Sdougm 				 * out well before we get here.
2128a3351425Sdougm 				 */
2129a3351425Sdougm 				ret = SA_NO_SUCH_PROP;
2130a3351425Sdougm 				break;
2131a3351425Sdougm 			default:
2132a3351425Sdougm 				break;
21336185db85Sdougm 			}
2134aed5d200Sdougm 
2135aed5d200Sdougm 			if (value != NULL)
2136aed5d200Sdougm 				sa_free_attr_string(value);
2137aed5d200Sdougm 
2138a3351425Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
2139a3351425Sdougm 				/* do the property specific check */
2140a3351425Sdougm 				ret = optdefs[optindex].check(property);
21416185db85Sdougm 			}
21426185db85Sdougm 		}
21436185db85Sdougm 	}
21446185db85Sdougm 
21456185db85Sdougm 	if (propname != NULL)
2146a3351425Sdougm 		sa_free_attr_string(propname);
21476185db85Sdougm 	return (ret);
21486185db85Sdougm }
21496185db85Sdougm 
21506185db85Sdougm /*
21516185db85Sdougm  * Protocol management functions
21526185db85Sdougm  *
21533472f5dcSdougm  * Properties defined in the default files are defined in
21543472f5dcSdougm  * proto_option_defs for parsing and validation. If "other" and
21553472f5dcSdougm  * "compare" are set, then the value for this property should be
21563472f5dcSdougm  * compared against the property specified in "other" using the
21573472f5dcSdougm  * "compare" check (either <= or >=) in order to ensure that the
21583472f5dcSdougm  * values are in the correct range.  E.g. setting server_versmin
21593472f5dcSdougm  * higher than server_versmax should not be allowed.
21606185db85Sdougm  */
21616185db85Sdougm 
21626185db85Sdougm struct proto_option_defs {
21636185db85Sdougm 	char *tag;
21646185db85Sdougm 	char *name;	/* display name -- remove protocol identifier */
21656185db85Sdougm 	int index;
21666185db85Sdougm 	int type;
21676185db85Sdougm 	union {
21686185db85Sdougm 	    int intval;
21696185db85Sdougm 	    char *string;
21706185db85Sdougm 	} defvalue;
21716185db85Sdougm 	uint32_t svcs;
21726185db85Sdougm 	int32_t minval;
21736185db85Sdougm 	int32_t maxval;
21746185db85Sdougm 	char *file;
21753472f5dcSdougm 	char *other;
21763472f5dcSdougm 	int compare;
21773472f5dcSdougm #define	OPT_CMP_GE	0
21783472f5dcSdougm #define	OPT_CMP_LE	1
21796185db85Sdougm 	int (*check)(char *);
21806185db85Sdougm } proto_options[] = {
21816185db85Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
21826185db85Sdougm 	{"nfsd_servers",
21836185db85Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
21846185db85Sdougm 	    1, INT32_MAX, NFSADMIN},
21856185db85Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
21866185db85Sdougm 	{"lockd_listen_backlog",
21876185db85Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
21886185db85Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
21896185db85Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
21906185db85Sdougm 	{"lockd_servers",
21916185db85Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
21926185db85Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
21936185db85Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
21946185db85Sdougm 	{"lockd_retransmit_timeout",
21956185db85Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
21966185db85Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
21976185db85Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
21986185db85Sdougm 	{"grace_period",
21996185db85Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
22006185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
22016185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
22026185db85Sdougm 	{"nfs_server_versmin",
22036185db85Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
22046185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
22053472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
22066185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
22076185db85Sdougm 	{"nfs_server_versmax",
22086185db85Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
22096185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
22103472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
22116185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
22126185db85Sdougm 	{"nfs_client_versmin",
22136185db85Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
22146185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
22153472f5dcSdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
22166185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
22176185db85Sdougm 	{"nfs_client_versmax",
22186185db85Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
22196185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
22203472f5dcSdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
22216185db85Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
22226185db85Sdougm 	{"nfs_server_delegation",
22236185db85Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
22246185db85Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
22256185db85Sdougm 	    NFSADMIN},
22266185db85Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
22276185db85Sdougm 	{"nfsmapid_domain",
22286185db85Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
22296185db85Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
22306185db85Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
22316185db85Sdougm 	{"nfsd_max_connections",
22326185db85Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
22336185db85Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
22346185db85Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
22356185db85Sdougm 	{"nfsd_protocol",
22366185db85Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
22376185db85Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
22386185db85Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
22396185db85Sdougm 	{"nfsd_listen_backlog",
22406185db85Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
22416185db85Sdougm 	    OPT_TYPE_NUMBER, 0,
22426185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
22436185db85Sdougm 	{NULL}
22446185db85Sdougm };
22456185db85Sdougm 
22466185db85Sdougm /*
22476185db85Sdougm  * the protoset holds the defined options so we don't have to read
22486185db85Sdougm  * them multiple times
22496185db85Sdougm  */
2250aed5d200Sdougm static sa_protocol_properties_t protoset;
22516185db85Sdougm 
22526185db85Sdougm static int
22536185db85Sdougm findprotoopt(char *name, int whichname)
22546185db85Sdougm {
22556185db85Sdougm 	int i;
22566185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2257a3351425Sdougm 		if (whichname == 1) {
2258a3351425Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
22596185db85Sdougm 			return (i);
2260a3351425Sdougm 		} else {
2261a3351425Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
2262a3351425Sdougm 				return (i);
2263a3351425Sdougm 		}
22646185db85Sdougm 	}
22656185db85Sdougm 	return (-1);
22666185db85Sdougm }
22676185db85Sdougm 
22686185db85Sdougm /*
22696185db85Sdougm  * fixcaselower(str)
22706185db85Sdougm  *
22716185db85Sdougm  * convert a string to lower case (inplace).
22726185db85Sdougm  */
22736185db85Sdougm 
22746185db85Sdougm static void
22756185db85Sdougm fixcaselower(char *str)
22766185db85Sdougm {
22776185db85Sdougm 	while (*str) {
2278a3351425Sdougm 		*str = tolower(*str);
2279a3351425Sdougm 		str++;
22806185db85Sdougm 	}
22816185db85Sdougm }
22826185db85Sdougm 
22836185db85Sdougm /*
22846185db85Sdougm  * fixcaseupper(str)
22856185db85Sdougm  *
22866185db85Sdougm  * convert a string to upper case (inplace).
22876185db85Sdougm  */
22886185db85Sdougm 
22896185db85Sdougm static void
22906185db85Sdougm fixcaseupper(char *str)
22916185db85Sdougm {
22926185db85Sdougm 	while (*str) {
2293a3351425Sdougm 		*str = toupper(*str);
2294a3351425Sdougm 		str++;
22956185db85Sdougm 	}
22966185db85Sdougm }
22976185db85Sdougm 
2298330ef417Sdougm /*
2299330ef417Sdougm  * skipwhitespace(str)
2300330ef417Sdougm  *
2301330ef417Sdougm  * Skip leading white space. It is assumed that it is called with a
2302330ef417Sdougm  * valid pointer.
2303330ef417Sdougm  */
2304330ef417Sdougm 
2305330ef417Sdougm static char *
2306330ef417Sdougm skipwhitespace(char *str)
2307330ef417Sdougm {
2308330ef417Sdougm 	while (*str && isspace(*str))
2309330ef417Sdougm 		str++;
2310330ef417Sdougm 
2311330ef417Sdougm 	return (str);
2312330ef417Sdougm }
2313330ef417Sdougm 
2314a3351425Sdougm /*
2315a3351425Sdougm  * extractprop()
2316a3351425Sdougm  *
2317a3351425Sdougm  * Extract the property and value out of the line and create the
2318a3351425Sdougm  * property in the optionset.
2319a3351425Sdougm  */
2320a3351425Sdougm static void
2321a3351425Sdougm extractprop(char *name, char *value)
2322a3351425Sdougm {
2323a3351425Sdougm 	sa_property_t prop;
2324a3351425Sdougm 	int index;
2325a3351425Sdougm 	/*
2326a3351425Sdougm 	 * Remove any leading
2327a3351425Sdougm 	 * white space.
2328a3351425Sdougm 	 */
2329a3351425Sdougm 	name = skipwhitespace(name);
2330a3351425Sdougm 
2331a3351425Sdougm 	index = findprotoopt(name, 0);
2332a3351425Sdougm 	if (index >= 0) {
2333a3351425Sdougm 		fixcaselower(name);
2334a3351425Sdougm 		prop = sa_create_property(proto_options[index].name, value);
2335a3351425Sdougm 		if (prop != NULL)
2336a3351425Sdougm 			(void) sa_add_protocol_property(protoset, prop);
2337a3351425Sdougm 	}
2338a3351425Sdougm }
2339a3351425Sdougm 
23406185db85Sdougm /*
23416185db85Sdougm  * initprotofromdefault()
23426185db85Sdougm  *
23436185db85Sdougm  * read the default file(s) and add the defined values to the
23446185db85Sdougm  * protoset.  Note that default values are known from the built in
23456185db85Sdougm  * table in case the file doesn't have a definition.
23466185db85Sdougm  */
23476185db85Sdougm 
23486185db85Sdougm static int
23496185db85Sdougm initprotofromdefault()
23506185db85Sdougm {
23516185db85Sdougm 	FILE *nfs;
23526185db85Sdougm 	char buff[BUFSIZ];
23536185db85Sdougm 	char *name;
23546185db85Sdougm 	char *value;
23556185db85Sdougm 
23566185db85Sdougm 	protoset = sa_create_protocol_properties("nfs");
23576185db85Sdougm 
23586185db85Sdougm 	if (protoset != NULL) {
2359a3351425Sdougm 		nfs = fopen(NFSADMIN, "r");
2360a3351425Sdougm 		if (nfs != NULL) {
2361a3351425Sdougm 			while (fgets(buff, sizeof (buff), nfs) != NULL) {
2362a3351425Sdougm 				switch (buff[0]) {
2363a3351425Sdougm 				case '\n':
2364a3351425Sdougm 				case '#':
2365a3351425Sdougm 					/* skip */
2366a3351425Sdougm 					break;
2367a3351425Sdougm 				default:
2368a3351425Sdougm 					name = buff;
2369a3351425Sdougm 					buff[strlen(buff) - 1] = '\0';
2370a3351425Sdougm 					value = strchr(name, '=');
2371a3351425Sdougm 					if (value != NULL) {
2372a3351425Sdougm 						*value++ = '\0';
2373a3351425Sdougm 						extractprop(name, value);
2374a3351425Sdougm 					}
2375a3351425Sdougm 				}
23766185db85Sdougm 			}
2377a3351425Sdougm 			if (nfs != NULL)
2378a3351425Sdougm 				(void) fclose(nfs);
23796185db85Sdougm 		}
23806185db85Sdougm 	}
23816185db85Sdougm 	if (protoset == NULL)
2382a3351425Sdougm 		return (SA_NO_MEMORY);
23836185db85Sdougm 	return (SA_OK);
23846185db85Sdougm }
23856185db85Sdougm 
23866185db85Sdougm /*
2387a3351425Sdougm  * add_defaults()
23886185db85Sdougm  *
23896185db85Sdougm  * Add the default values for any property not defined in the parsing
23903472f5dcSdougm  * of the default files. Values are set according to their defined
23913472f5dcSdougm  * types.
23926185db85Sdougm  */
23936185db85Sdougm 
23946185db85Sdougm static void
23956185db85Sdougm add_defaults()
23966185db85Sdougm {
23976185db85Sdougm 	int i;
23986185db85Sdougm 	char number[MAXDIGITS];
23996185db85Sdougm 
24006185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2401a3351425Sdougm 		sa_property_t prop;
2402a3351425Sdougm 		prop = sa_get_protocol_property(protoset,
2403a3351425Sdougm 		    proto_options[i].name);
2404a3351425Sdougm 		if (prop == NULL) {
2405a3351425Sdougm 			/* add the default value */
2406a3351425Sdougm 			switch (proto_options[i].type) {
2407a3351425Sdougm 			case OPT_TYPE_NUMBER:
2408a3351425Sdougm 				(void) snprintf(number, sizeof (number), "%d",
2409a3351425Sdougm 				    proto_options[i].defvalue.intval);
2410a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2411a3351425Sdougm 				    number);
2412a3351425Sdougm 				break;
2413a3351425Sdougm 
2414a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2415a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2416a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2417a3351425Sdougm 				    "true" : "false");
2418a3351425Sdougm 				break;
2419a3351425Sdougm 
2420a3351425Sdougm 			case OPT_TYPE_ONOFF:
2421a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2422a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2423a3351425Sdougm 				    "on" : "off");
2424a3351425Sdougm 				break;
2425a3351425Sdougm 
2426a3351425Sdougm 			default:
2427a3351425Sdougm 				/* treat as strings of zero length */
2428a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2429a3351425Sdougm 				    "");
2430a3351425Sdougm 				break;
2431a3351425Sdougm 			}
2432a3351425Sdougm 			if (prop != NULL)
2433a3351425Sdougm 				(void) sa_add_protocol_property(protoset, prop);
24346185db85Sdougm 		}
24356185db85Sdougm 	}
24366185db85Sdougm }
24376185db85Sdougm 
24386185db85Sdougm static void
24396185db85Sdougm free_protoprops()
24406185db85Sdougm {
2441aed5d200Sdougm 	if (protoset != NULL) {
2442aed5d200Sdougm 		xmlFreeNode(protoset);
2443aed5d200Sdougm 		protoset = NULL;
2444aed5d200Sdougm 	}
24456185db85Sdougm }
24466185db85Sdougm 
24476185db85Sdougm /*
24486185db85Sdougm  * nfs_init()
24496185db85Sdougm  *
24506185db85Sdougm  * Initialize the NFS plugin.
24516185db85Sdougm  */
24526185db85Sdougm 
24536185db85Sdougm static int
24546185db85Sdougm nfs_init()
24556185db85Sdougm {
24566185db85Sdougm 	int ret = SA_OK;
24576185db85Sdougm 
24586185db85Sdougm 	if (sa_plugin_ops.sa_init != nfs_init)
2459a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2460a3351425Sdougm 		    "NFS plugin not properly initialized\n"));
24616185db85Sdougm 
24626185db85Sdougm 	ret = initprotofromdefault();
2463a3351425Sdougm 	if (ret == SA_OK)
2464a3351425Sdougm 		add_defaults();
24656185db85Sdougm 
24666185db85Sdougm 	return (ret);
24676185db85Sdougm }
24686185db85Sdougm 
24696185db85Sdougm /*
24706185db85Sdougm  * nfs_fini()
24716185db85Sdougm  *
24726185db85Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
24736185db85Sdougm  */
24746185db85Sdougm 
24756185db85Sdougm static void
24766185db85Sdougm nfs_fini()
24776185db85Sdougm {
24786185db85Sdougm 	free_protoprops();
24796185db85Sdougm }
24806185db85Sdougm 
24816185db85Sdougm /*
24826185db85Sdougm  * nfs_get_proto_set()
24836185db85Sdougm  *
24846185db85Sdougm  * Return an optionset with all the protocol specific properties in
24856185db85Sdougm  * it.
24866185db85Sdougm  */
24876185db85Sdougm 
24886185db85Sdougm static sa_protocol_properties_t
24896185db85Sdougm nfs_get_proto_set()
24906185db85Sdougm {
24916185db85Sdougm 	return (protoset);
24926185db85Sdougm }
24936185db85Sdougm 
24946185db85Sdougm struct deffile {
24956185db85Sdougm 	struct deffile *next;
24966185db85Sdougm 	char *line;
24976185db85Sdougm };
24986185db85Sdougm 
24996185db85Sdougm /*
25006185db85Sdougm  * read_default_file(fname)
25016185db85Sdougm  *
25026185db85Sdougm  * Read the specified default file. We return a list of entries. This
25036185db85Sdougm  * get used for adding or removing values.
25046185db85Sdougm  */
25056185db85Sdougm 
25066185db85Sdougm static struct deffile *
25076185db85Sdougm read_default_file(char *fname)
25086185db85Sdougm {
25096185db85Sdougm 	FILE *file;
25106185db85Sdougm 	struct deffile *defs = NULL;
25116185db85Sdougm 	struct deffile *newdef;
25126185db85Sdougm 	struct deffile *prevdef = NULL;
25136185db85Sdougm 	char buff[BUFSIZ * 2];
25146185db85Sdougm 
25156185db85Sdougm 	file = fopen(fname, "r");
25166185db85Sdougm 	if (file != NULL) {
2517a3351425Sdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
2518a3351425Sdougm 			newdef = (struct deffile *)calloc(1,
2519a3351425Sdougm 			    sizeof (struct deffile));
2520a3351425Sdougm 			if (newdef != NULL) {
2521a3351425Sdougm 				/* Make sure we skip any leading whitespace. */
2522a3351425Sdougm 				newdef->line = strdup(skipwhitespace(buff));
2523a3351425Sdougm 				if (defs == NULL) {
2524a3351425Sdougm 					prevdef = defs = newdef;
2525a3351425Sdougm 				} else {
2526a3351425Sdougm 					prevdef->next = newdef;
2527a3351425Sdougm 					prevdef = newdef;
2528a3351425Sdougm 				}
2529a3351425Sdougm 			}
25306185db85Sdougm 		}
25316185db85Sdougm 	}
25326185db85Sdougm 	(void) fclose(file);
25336185db85Sdougm 	return (defs);
25346185db85Sdougm }
25356185db85Sdougm 
25366185db85Sdougm static void
25376185db85Sdougm free_default_file(struct deffile *defs)
25386185db85Sdougm {
25396185db85Sdougm 	struct deffile *curdefs = NULL;
25406185db85Sdougm 
25416185db85Sdougm 	while (defs != NULL) {
2542a3351425Sdougm 		curdefs = defs;
2543a3351425Sdougm 		defs = defs->next;
2544a3351425Sdougm 		if (curdefs->line != NULL)
2545a3351425Sdougm 			free(curdefs->line);
2546a3351425Sdougm 		free(curdefs);
25476185db85Sdougm 	}
25486185db85Sdougm }
25496185db85Sdougm 
25506185db85Sdougm /*
25516185db85Sdougm  * write_default_file(fname, defs)
25526185db85Sdougm  *
25536185db85Sdougm  * Write the default file back.
25546185db85Sdougm  */
25556185db85Sdougm 
25566185db85Sdougm static int
25576185db85Sdougm write_default_file(char *fname, struct deffile *defs)
25586185db85Sdougm {
25596185db85Sdougm 	FILE *file;
25606185db85Sdougm 	int ret = SA_OK;
25616185db85Sdougm 	sigset_t old, new;
25626185db85Sdougm 
25636185db85Sdougm 	file = fopen(fname, "w+");
25646185db85Sdougm 	if (file != NULL) {
2565a3351425Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
2566a3351425Sdougm 		(void) sigaddset(&new, SIGHUP);
2567a3351425Sdougm 		(void) sigaddset(&new, SIGINT);
2568a3351425Sdougm 		(void) sigaddset(&new, SIGQUIT);
2569a3351425Sdougm 		(void) sigaddset(&new, SIGTSTP);
2570a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
2571a3351425Sdougm 		while (defs != NULL) {
2572a3351425Sdougm 			(void) fputs(defs->line, file);
2573a3351425Sdougm 			defs = defs->next;
2574a3351425Sdougm 		}
2575a3351425Sdougm 		(void) fsync(fileno(file));
2576a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
2577a3351425Sdougm 		(void) fclose(file);
25786185db85Sdougm 	} else {
2579a3351425Sdougm 		switch (errno) {
2580a3351425Sdougm 		case EPERM:
2581a3351425Sdougm 		case EACCES:
2582a3351425Sdougm 			ret = SA_NO_PERMISSION;
2583a3351425Sdougm 			break;
2584a3351425Sdougm 		default:
2585a3351425Sdougm 			ret = SA_SYSTEM_ERR;
2586a3351425Sdougm 		}
25876185db85Sdougm 	}
25886185db85Sdougm 	return (ret);
25896185db85Sdougm }
25906185db85Sdougm 
25916185db85Sdougm 
25926185db85Sdougm /*
25936185db85Sdougm  * set_default_file_value(tag, value)
25946185db85Sdougm  *
25956185db85Sdougm  * Set the default file value for tag to value. Then rewrite the file.
25966185db85Sdougm  * tag and value are always set.  The caller must ensure this.
25976185db85Sdougm  */
25986185db85Sdougm 
25996185db85Sdougm #define	MAX_STRING_LENGTH	256
26006185db85Sdougm static int
26016185db85Sdougm set_default_file_value(char *tag, char *value)
26026185db85Sdougm {
26036185db85Sdougm 	int ret = SA_OK;
26046185db85Sdougm 	struct deffile *root;
26056185db85Sdougm 	struct deffile *defs;
26066185db85Sdougm 	struct deffile *prev;
26076185db85Sdougm 	char string[MAX_STRING_LENGTH];
26086185db85Sdougm 	int len;
26096185db85Sdougm 	int update = 0;
26106185db85Sdougm 
26116185db85Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
26126185db85Sdougm 	len = strlen(string);
26136185db85Sdougm 
26146185db85Sdougm 	root = defs = read_default_file(NFSADMIN);
26156185db85Sdougm 	if (root == NULL) {
2616a3351425Sdougm 		if (errno == EPERM || errno == EACCES)
2617a3351425Sdougm 			ret = SA_NO_PERMISSION;
2618a3351425Sdougm 		else
2619a3351425Sdougm 			ret = SA_SYSTEM_ERR;
26206185db85Sdougm 	} else {
26216185db85Sdougm 		while (defs != NULL) {
2622a3351425Sdougm 			if (defs->line != NULL &&
2623a3351425Sdougm 			    strncasecmp(defs->line, string, len) == 0) {
2624a3351425Sdougm 				/* replace with the new value */
2625a3351425Sdougm 				free(defs->line);
2626a3351425Sdougm 				fixcaseupper(tag);
2627a3351425Sdougm 				(void) snprintf(string, sizeof (string),
26286185db85Sdougm 				    "%s=%s\n", tag, value);
2629a3351425Sdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
2630a3351425Sdougm 				defs->line = strdup(string);
2631a3351425Sdougm 				update = 1;
2632a3351425Sdougm 				break;
2633a3351425Sdougm 			}
2634a3351425Sdougm 			defs = defs->next;
2635a3351425Sdougm 		}
2636a3351425Sdougm 		if (!update) {
2637a3351425Sdougm 			defs = root;
2638a3351425Sdougm 			/* didn't find, so see if it is a comment */
2639a3351425Sdougm 			(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
2640a3351425Sdougm 			len = strlen(string);
2641a3351425Sdougm 			while (defs != NULL) {
2642a3351425Sdougm 				if (strncasecmp(defs->line, string, len) == 0) {
2643a3351425Sdougm 					/* replace with the new value */
2644a3351425Sdougm 					free(defs->line);
2645a3351425Sdougm 					fixcaseupper(tag);
2646a3351425Sdougm 					(void) snprintf(string, sizeof (string),
2647a3351425Sdougm 					    "%s=%s\n", tag, value);
2648a3351425Sdougm 					string[MAX_STRING_LENGTH - 1] = '\0';
2649a3351425Sdougm 					defs->line = strdup(string);
2650a3351425Sdougm 					update = 1;
2651a3351425Sdougm 					break;
2652a3351425Sdougm 				}
2653a3351425Sdougm 				defs = defs->next;
2654a3351425Sdougm 			}
26556185db85Sdougm 		}
2656a3351425Sdougm 		if (!update) {
2657a3351425Sdougm 			fixcaseupper(tag);
2658a3351425Sdougm 			(void) snprintf(string, sizeof (string), "%s=%s\n",
2659a3351425Sdougm 			    tag, value);
2660a3351425Sdougm 			prev = root;
2661a3351425Sdougm 			while (prev->next != NULL)
2662a3351425Sdougm 				prev = prev->next;
2663a3351425Sdougm 			defs = malloc(sizeof (struct deffile));
2664a3351425Sdougm 			prev->next = defs;
2665a3351425Sdougm 			if (defs != NULL) {
2666a3351425Sdougm 				defs->next = NULL;
2667a3351425Sdougm 				defs->line = strdup(string);
2668a3351425Sdougm 			}
26696185db85Sdougm 		}
2670a3351425Sdougm 		if (update) {
2671a3351425Sdougm 			ret = write_default_file(NFSADMIN, root);
2672a3351425Sdougm 		}
2673a3351425Sdougm 		free_default_file(root);
26746185db85Sdougm 	}
26756185db85Sdougm 	return (ret);
26766185db85Sdougm }
26776185db85Sdougm 
26783472f5dcSdougm /*
26793472f5dcSdougm  * service_in_state(service, chkstate)
26803472f5dcSdougm  *
26813472f5dcSdougm  * Want to know if the specified service is in the desired state
26823472f5dcSdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
26833472f5dcSdougm  * isn't.
26843472f5dcSdougm  */
26853472f5dcSdougm static int
26863472f5dcSdougm service_in_state(char *service, const char *chkstate)
26873472f5dcSdougm {
26883472f5dcSdougm 	char *state;
26893472f5dcSdougm 	int ret = B_FALSE;
26903472f5dcSdougm 
26913472f5dcSdougm 	state = smf_get_state(service);
26923472f5dcSdougm 	if (state != NULL) {
2693a3351425Sdougm 		/* got the state so get the equality for the return value */
2694a3351425Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2695a3351425Sdougm 		free(state);
26963472f5dcSdougm 	}
26973472f5dcSdougm 	return (ret);
26983472f5dcSdougm }
26993472f5dcSdougm 
27006185db85Sdougm /*
27016185db85Sdougm  * restart_service(svcs)
27026185db85Sdougm  *
27036185db85Sdougm  * Walk through the bit mask of services that need to be restarted in
27046185db85Sdougm  * order to use the new property values. Some properties affect
27053472f5dcSdougm  * multiple daemons. Should only restart a service if it is currently
27063472f5dcSdougm  * enabled (online).
27076185db85Sdougm  */
27086185db85Sdougm 
27096185db85Sdougm static void
27106185db85Sdougm restart_service(uint32_t svcs)
27116185db85Sdougm {
27126185db85Sdougm 	uint32_t mask;
27133472f5dcSdougm 	int ret;
27143472f5dcSdougm 	char *service;
27153472f5dcSdougm 
27166185db85Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
2717a3351425Sdougm 		switch (svcs & mask) {
2718a3351425Sdougm 		case SVC_LOCKD:
2719a3351425Sdougm 			service = LOCKD;
2720a3351425Sdougm 			break;
2721a3351425Sdougm 		case SVC_STATD:
2722a3351425Sdougm 			service = STATD;
2723a3351425Sdougm 			break;
2724a3351425Sdougm 		case SVC_NFSD:
2725a3351425Sdougm 			service = NFSD;
2726a3351425Sdougm 			break;
2727a3351425Sdougm 		case SVC_MOUNTD:
2728a3351425Sdougm 			service = MOUNTD;
2729a3351425Sdougm 			break;
2730a3351425Sdougm 		case SVC_NFS4CBD:
2731a3351425Sdougm 			service = NFS4CBD;
2732a3351425Sdougm 			break;
2733a3351425Sdougm 		case SVC_NFSMAPID:
2734a3351425Sdougm 			service = NFSMAPID;
2735a3351425Sdougm 			break;
2736a3351425Sdougm 		case SVC_RQUOTAD:
2737a3351425Sdougm 			service = RQUOTAD;
2738a3351425Sdougm 			break;
2739a3351425Sdougm 		case SVC_NFSLOGD:
2740a3351425Sdougm 			service = NFSLOGD;
2741a3351425Sdougm 			break;
2742a3351425Sdougm 		default:
2743a3351425Sdougm 			continue;
2744a3351425Sdougm 		}
27453472f5dcSdougm 
27463472f5dcSdougm 		/*
27473472f5dcSdougm 		 * Only attempt to restart the service if it is
27483472f5dcSdougm 		 * currently running. In the future, it may be
27493472f5dcSdougm 		 * desirable to use smf_refresh_instance if the NFS
27503472f5dcSdougm 		 * services ever implement the refresh method.
27513472f5dcSdougm 		 */
2752a3351425Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2753a3351425Sdougm 			ret = smf_restart_instance(service);
27543472f5dcSdougm 			/*
2755a3351425Sdougm 			 * There are only a few SMF errors at this point, but
2756a3351425Sdougm 			 * it is also possible that a bad value may have put
2757a3351425Sdougm 			 * the service into maintenance if there wasn't an
2758a3351425Sdougm 			 * SMF level error.
27593472f5dcSdougm 			 */
2760a3351425Sdougm 			if (ret != 0) {
2761a3351425Sdougm 				(void) fprintf(stderr,
2762a3351425Sdougm 				    dgettext(TEXT_DOMAIN,
2763a3351425Sdougm 				    "%s failed to restart: %s\n"),
2764a3351425Sdougm 				    scf_strerror(scf_error()));
2765a3351425Sdougm 			} else {
2766a3351425Sdougm 				/*
2767a3351425Sdougm 				 * Check whether it has gone to "maintenance"
2768a3351425Sdougm 				 * mode or not. Maintenance implies something
2769a3351425Sdougm 				 * went wrong.
2770a3351425Sdougm 				 */
2771a3351425Sdougm 				if (service_in_state(service,
2772a3351425Sdougm 				    SCF_STATE_STRING_MAINT)) {
2773a3351425Sdougm 					(void) fprintf(stderr,
2774a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
2775a3351425Sdougm 					    "%s failed to restart\n"),
2776a3351425Sdougm 					    service);
2777a3351425Sdougm 				}
2778a3351425Sdougm 			}
27793472f5dcSdougm 		}
2780a3351425Sdougm 		svcs &= ~mask;
27816185db85Sdougm 	}
27826185db85Sdougm }
27836185db85Sdougm 
27843472f5dcSdougm /*
27853472f5dcSdougm  * nfs_minmax_check(name, value)
27863472f5dcSdougm  *
27873472f5dcSdougm  * Verify that the value for the property specified by index is valid
27883472f5dcSdougm  * relative to the opposite value in the case of a min/max variable.
27893472f5dcSdougm  * Currently, server_minvers/server_maxvers and
27903472f5dcSdougm  * client_minvers/client_maxvers are the only ones to check.
27913472f5dcSdougm  */
27923472f5dcSdougm 
27933472f5dcSdougm static int
27943472f5dcSdougm nfs_minmax_check(int index, int value)
27953472f5dcSdougm {
27963472f5dcSdougm 	int val;
27973472f5dcSdougm 	char *pval;
27983472f5dcSdougm 	sa_property_t prop;
27993472f5dcSdougm 	sa_optionset_t opts;
28003472f5dcSdougm 	int ret = B_TRUE;
28013472f5dcSdougm 
28023472f5dcSdougm 	if (proto_options[index].other != NULL) {
2803a3351425Sdougm 		/* have a property to compare against */
2804a3351425Sdougm 		opts = nfs_get_proto_set();
2805a3351425Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
28063472f5dcSdougm 		/*
28073472f5dcSdougm 		 * If we don't find the property, assume default
28083472f5dcSdougm 		 * values which will work since the max will be at the
28093472f5dcSdougm 		 * max and the min at the min.
28103472f5dcSdougm 		 */
2811a3351425Sdougm 		if (prop != NULL) {
2812a3351425Sdougm 			pval = sa_get_property_attr(prop, "value");
2813a3351425Sdougm 			if (pval != NULL) {
2814a3351425Sdougm 				val = strtoul(pval, NULL, 0);
2815a3351425Sdougm 				if (proto_options[index].compare ==
2816a3351425Sdougm 				    OPT_CMP_LE) {
2817a3351425Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
2818a3351425Sdougm 				} else if (proto_options[index].compare ==
2819a3351425Sdougm 				    OPT_CMP_GE) {
2820a3351425Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
2821a3351425Sdougm 				}
2822a3351425Sdougm 			}
28233472f5dcSdougm 		}
28243472f5dcSdougm 	}
28253472f5dcSdougm 	return (ret);
28263472f5dcSdougm }
28273472f5dcSdougm 
28286185db85Sdougm /*
28296185db85Sdougm  * nfs_validate_proto_prop(index, name, value)
28306185db85Sdougm  *
2831da6c28aaSamw  * Verify that the property specified by name can take the new
28326185db85Sdougm  * value. This is a sanity check to prevent bad values getting into
28333472f5dcSdougm  * the default files. All values need to be checked against what is
28343472f5dcSdougm  * allowed by their defined type. If a type isn't explicitly defined
28353472f5dcSdougm  * here, it is treated as a string.
28363472f5dcSdougm  *
28373472f5dcSdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
28383472f5dcSdougm  * within the range specified and potentially against another property
28393472f5dcSdougm  * value as well as specified in the proto_options members other and
28403472f5dcSdougm  * compare.
28416185db85Sdougm  */
28426185db85Sdougm 
28436185db85Sdougm static int
28446185db85Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
28456185db85Sdougm {
28466185db85Sdougm 	int ret = SA_OK;
28476185db85Sdougm 	char *cp;
28486185db85Sdougm #ifdef lint
28496185db85Sdougm 	name = name;
28506185db85Sdougm #endif
28516185db85Sdougm 
28526185db85Sdougm 	switch (proto_options[index].type) {
28536185db85Sdougm 	case OPT_TYPE_NUMBER:
2854a3351425Sdougm 		if (!is_a_number(value))
2855a3351425Sdougm 			ret = SA_BAD_VALUE;
2856a3351425Sdougm 		else {
2857a3351425Sdougm 			int val;
2858a3351425Sdougm 			val = strtoul(value, NULL, 0);
2859a3351425Sdougm 			if (val < proto_options[index].minval ||
2860a3351425Sdougm 			    val > proto_options[index].maxval)
2861a3351425Sdougm 				ret = SA_BAD_VALUE;
2862a3351425Sdougm 			/*
2863a3351425Sdougm 			 * For server_versmin/server_versmax and
2864a3351425Sdougm 			 * client_versmin/client_versmax, the value of the
2865a3351425Sdougm 			 * min(max) should be checked to be correct relative
2866a3351425Sdougm 			 * to the current max(min).
2867a3351425Sdougm 			 */
2868a3351425Sdougm 			if (!nfs_minmax_check(index, val)) {
2869a3351425Sdougm 				ret = SA_BAD_VALUE;
2870a3351425Sdougm 			}
28713472f5dcSdougm 		}
2872a3351425Sdougm 		break;
28733472f5dcSdougm 
28746185db85Sdougm 	case OPT_TYPE_DOMAIN:
28756185db85Sdougm 		/*
28763472f5dcSdougm 		 * needs to be a qualified domain so will have at
28773472f5dcSdougm 		 * least one period and other characters on either
28783472f5dcSdougm 		 * side of it.  A zero length string is also allowed
28793472f5dcSdougm 		 * and is the way to turn off the override.
28806185db85Sdougm 		 */
2881a3351425Sdougm 		if (strlen(value) == 0)
2882a3351425Sdougm 			break;
2883a3351425Sdougm 		cp = strchr(value, '.');
2884a3351425Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
2885a3351425Sdougm 			ret = SA_BAD_VALUE;
28863472f5dcSdougm 		break;
28873472f5dcSdougm 
28886185db85Sdougm 	case OPT_TYPE_BOOLEAN:
2889a3351425Sdougm 		if (strlen(value) == 0 ||
2890a3351425Sdougm 		    strcasecmp(value, "true") == 0 ||
2891a3351425Sdougm 		    strcmp(value, "1") == 0 ||
2892a3351425Sdougm 		    strcasecmp(value, "false") == 0 ||
2893a3351425Sdougm 		    strcmp(value, "0") == 0) {
2894a3351425Sdougm 			ret = SA_OK;
2895a3351425Sdougm 		} else {
2896a3351425Sdougm 			ret = SA_BAD_VALUE;
2897a3351425Sdougm 		}
2898a3351425Sdougm 		break;
28993472f5dcSdougm 
29006185db85Sdougm 	case OPT_TYPE_ONOFF:
2901a3351425Sdougm 		if (strcasecmp(value, "on") != 0 &&
2902a3351425Sdougm 		    strcasecmp(value, "off") != 0) {
2903a3351425Sdougm 			ret = SA_BAD_VALUE;
2904a3351425Sdougm 		}
2905a3351425Sdougm 		break;
29063472f5dcSdougm 
29076185db85Sdougm 	case OPT_TYPE_PROTOCOL:
2908a3351425Sdougm 		if (strcasecmp(value, "all") != 0 &&
2909a3351425Sdougm 		    strcasecmp(value, "tcp") != 0 &&
2910a3351425Sdougm 		    strcasecmp(value, "udp") != 0)
2911a3351425Sdougm 			ret = SA_BAD_VALUE;
2912a3351425Sdougm 		break;
29133472f5dcSdougm 
29143472f5dcSdougm 	default:
2915a3351425Sdougm 		/* treat as a string */
2916a3351425Sdougm 		break;
29176185db85Sdougm 	}
29186185db85Sdougm 	return (ret);
29196185db85Sdougm }
29206185db85Sdougm 
29216185db85Sdougm /*
29226185db85Sdougm  * nfs_set_proto_prop(prop)
29236185db85Sdougm  *
29246185db85Sdougm  * check that prop is valid.
29256185db85Sdougm  */
29266185db85Sdougm 
29276185db85Sdougm static int
29286185db85Sdougm nfs_set_proto_prop(sa_property_t prop)
29296185db85Sdougm {
29306185db85Sdougm 	int ret = SA_OK;
29316185db85Sdougm 	char *name;
29326185db85Sdougm 	char *value;
29336185db85Sdougm 
29346185db85Sdougm 	name = sa_get_property_attr(prop, "type");
29356185db85Sdougm 	value = sa_get_property_attr(prop, "value");
29366185db85Sdougm 	if (name != NULL && value != NULL) {
2937a3351425Sdougm 		int index = findprotoopt(name, 1);
2938a3351425Sdougm 		if (index >= 0) {
2939a3351425Sdougm 			/* should test for valid value */
2940a3351425Sdougm 			ret = nfs_validate_proto_prop(index, name, value);
2941a3351425Sdougm 			if (ret == SA_OK)
2942a3351425Sdougm 				ret = set_default_file_value(
2943a3351425Sdougm 				    proto_options[index].tag, value);
2944a3351425Sdougm 			if (ret == SA_OK)
2945a3351425Sdougm 				restart_service(proto_options[index].svcs);
2946a3351425Sdougm 		}
29476185db85Sdougm 	}
29486185db85Sdougm 	if (name != NULL)
2949a3351425Sdougm 		sa_free_attr_string(name);
29506185db85Sdougm 	if (value != NULL)
2951a3351425Sdougm 		sa_free_attr_string(value);
29526185db85Sdougm 	return (ret);
29536185db85Sdougm }
29546185db85Sdougm 
29556185db85Sdougm /*
29566185db85Sdougm  * nfs_get_status()
29576185db85Sdougm  *
29586185db85Sdougm  * What is the current status of the nfsd? We use the SMF state here.
29596185db85Sdougm  * Caller must free the returned value.
29606185db85Sdougm  */
29616185db85Sdougm 
29626185db85Sdougm static char *
29636185db85Sdougm nfs_get_status()
29646185db85Sdougm {
29656185db85Sdougm 	char *state;
29666185db85Sdougm 	state = smf_get_state(NFSD);
29676185db85Sdougm 	return (state != NULL ? state : strdup("-"));
29686185db85Sdougm }
29696185db85Sdougm 
29706185db85Sdougm /*
29716185db85Sdougm  * nfs_space_alias(alias)
29726185db85Sdougm  *
29736185db85Sdougm  * Lookup the space (security) name. If it is default, convert to the
29746185db85Sdougm  * real name.
29756185db85Sdougm  */
29766185db85Sdougm 
29776185db85Sdougm static char *
29786185db85Sdougm nfs_space_alias(char *space)
29796185db85Sdougm {
29806185db85Sdougm 	char *name = space;
29816185db85Sdougm 	seconfig_t secconf;
2982a99982a7Sdougm 
2983a99982a7Sdougm 	/*
2984a99982a7Sdougm 	 * Only the space named "default" is special. If it is used,
2985a99982a7Sdougm 	 * the default needs to be looked up and the real name used.
2986a99982a7Sdougm 	 * This is normally "sys" but could be changed.  We always
2987a99982a7Sdougm 	 * change defautl to the real name.
2988a99982a7Sdougm 	 */
2989a99982a7Sdougm 	if (strcmp(space, "default") == 0 &&
2990a99982a7Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
2991a3351425Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
2992a3351425Sdougm 			name = secconf.sc_name;
29936185db85Sdougm 	}
29946185db85Sdougm 	return (strdup(name));
29956185db85Sdougm }
2996da6c28aaSamw 
2997da6c28aaSamw /*
2998da6c28aaSamw  * nfs_features()
2999da6c28aaSamw  *
3000da6c28aaSamw  * Return a mask of the features required.
3001da6c28aaSamw  */
3002da6c28aaSamw 
3003da6c28aaSamw static uint64_t
3004da6c28aaSamw nfs_features()
3005da6c28aaSamw {
3006da6c28aaSamw 	return ((uint64_t)SA_FEATURE_DFSTAB);
3007da6c28aaSamw }
3008