13910Sdougm /*
23910Sdougm  * CDDL HEADER START
33910Sdougm  *
43910Sdougm  * The contents of this file are subject to the terms of the
53910Sdougm  * Common Development and Distribution License (the "License").
63910Sdougm  * You may not use this file except in compliance with the License.
73910Sdougm  *
83910Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93910Sdougm  * or http://www.opensolaris.org/os/licensing.
103910Sdougm  * See the License for the specific language governing permissions
113910Sdougm  * and limitations under the License.
123910Sdougm  *
133910Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
143910Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153910Sdougm  * If applicable, add the following below this CDDL HEADER, with the
163910Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
173910Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
183910Sdougm  *
193910Sdougm  * CDDL HEADER END
203910Sdougm  */
213910Sdougm 
223910Sdougm /*
238845Samw@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
243910Sdougm  * Use is subject to license terms.
253910Sdougm  */
263910Sdougm 
273910Sdougm /*
283910Sdougm  * NFS specific functions
293910Sdougm  */
303910Sdougm #include <stdio.h>
313910Sdougm #include <string.h>
323910Sdougm #include <ctype.h>
333910Sdougm #include <stdlib.h>
343910Sdougm #include <unistd.h>
353910Sdougm #include <zone.h>
363910Sdougm #include <errno.h>
373910Sdougm #include <locale.h>
383910Sdougm #include <signal.h>
393910Sdougm #include "libshare.h"
403910Sdougm #include "libshare_impl.h"
413910Sdougm #include <nfs/export.h>
423910Sdougm #include <pwd.h>
433910Sdougm #include <limits.h>
443910Sdougm #include <libscf.h>
453910Sdougm #include "nfslog_config.h"
463910Sdougm #include "nfslogtab.h"
473910Sdougm #include "libshare_nfs.h"
483910Sdougm #include <rpcsvc/daemon_utils.h>
493910Sdougm #include <nfs/nfs.h>
504543Smarks #include <nfs/nfssys.h>
513910Sdougm 
523910Sdougm /* should really be in some global place */
533910Sdougm #define	DEF_WIN	30000
543910Sdougm #define	OPT_CHUNK	1024
553910Sdougm 
563910Sdougm int debug = 0;
573910Sdougm 
584543Smarks #define	NFS_SERVER_SVC	"svc:/network/nfs/server:default"
593910Sdougm 
603910Sdougm /* internal functions */
613910Sdougm static int nfs_init();
623910Sdougm static void nfs_fini();
633910Sdougm static int nfs_enable_share(sa_share_t);
644543Smarks static int nfs_disable_share(sa_share_t, char *);
656214Sdougm static int nfs_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
663910Sdougm static int nfs_validate_security_mode(char *);
673910Sdougm static int nfs_is_security_opt(char *);
683910Sdougm static int nfs_parse_legacy_options(sa_group_t, char *);
693910Sdougm static char *nfs_format_options(sa_group_t, int);
703910Sdougm static int nfs_set_proto_prop(sa_property_t);
713910Sdougm static sa_protocol_properties_t nfs_get_proto_set();
723910Sdougm static char *nfs_get_status();
733910Sdougm static char *nfs_space_alias(char *);
745331Samw static uint64_t nfs_features();
753910Sdougm 
763910Sdougm /*
773910Sdougm  * ops vector that provides the protocol specific info and operations
783910Sdougm  * for share management.
793910Sdougm  */
803910Sdougm 
813910Sdougm struct sa_plugin_ops sa_plugin_ops = {
823910Sdougm 	SA_PLUGIN_VERSION,
833910Sdougm 	"nfs",
843910Sdougm 	nfs_init,
853910Sdougm 	nfs_fini,
863910Sdougm 	nfs_enable_share,
873910Sdougm 	nfs_disable_share,
883910Sdougm 	nfs_validate_property,
893910Sdougm 	nfs_validate_security_mode,
903910Sdougm 	nfs_is_security_opt,
913910Sdougm 	nfs_parse_legacy_options,
923910Sdougm 	nfs_format_options,
933910Sdougm 	nfs_set_proto_prop,
943910Sdougm 	nfs_get_proto_set,
953910Sdougm 	nfs_get_status,
963910Sdougm 	nfs_space_alias,
975331Samw 	NULL,	/* update_legacy */
985331Samw 	NULL,	/* delete_legacy */
995331Samw 	NULL,	/* change_notify */
1005331Samw 	NULL,	/* enable_resource */
1015331Samw 	NULL,	/* disable_resource */
1025331Samw 	nfs_features,
1035331Samw 	NULL,	/* transient shares */
1045331Samw 	NULL,	/* notify resource */
1056007Sthurlow 	NULL,	/* rename_resource */
1066007Sthurlow 	NULL,	/* run_command */
1076007Sthurlow 	NULL,	/* command_help */
1086007Sthurlow 	NULL	/* delete_proto_section */
1093910Sdougm };
1103910Sdougm 
1113910Sdougm /*
1123910Sdougm  * list of support services needed
1133910Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
1143910Sdougm  */
1153910Sdougm 
1163910Sdougm static char *service_list_default[] =
11711291SRobert.Thurlow@Sun.COM 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, REPARSED, NULL };
1183910Sdougm static char *service_list_logging[] =
11911291SRobert.Thurlow@Sun.COM 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, REPARSED,
12011291SRobert.Thurlow@Sun.COM 	    NULL };
1213910Sdougm 
1223910Sdougm /*
1233910Sdougm  * option definitions.  Make sure to keep the #define for the option
1243910Sdougm  * index just before the entry it is the index for. Changing the order
1253910Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1263910Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1273910Sdougm  */
1283910Sdougm 
1293910Sdougm struct option_defs optdefs[] = {
1303910Sdougm #define	OPT_RO		0
1313910Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1323910Sdougm #define	OPT_RW		1
1333910Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1343910Sdougm #define	OPT_ROOT	2
1353910Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1363910Sdougm #define	OPT_SECURE	3
1373910Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1383910Sdougm #define	OPT_ANON	4
1393910Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1403910Sdougm #define	OPT_WINDOW	5
1413910Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1423910Sdougm #define	OPT_NOSUID	6
1433910Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1443910Sdougm #define	OPT_ACLOK	7
1453910Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1463910Sdougm #define	OPT_NOSUB	8
1473910Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1483910Sdougm #define	OPT_SEC		9
1493910Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1503910Sdougm #define	OPT_PUBLIC	10
1513910Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1523910Sdougm #define	OPT_INDEX	11
1533910Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1543910Sdougm #define	OPT_LOG		12
1553910Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1563910Sdougm #define	OPT_CKSUM	13
1573910Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
1587961SNatalie.Li@Sun.COM #define	OPT_NONE	14
1597961SNatalie.Li@Sun.COM 	{SHOPT_NONE, OPT_NONE, OPT_TYPE_ACCLIST},
1607961SNatalie.Li@Sun.COM #define	OPT_ROOT_MAPPING	15
1617961SNatalie.Li@Sun.COM 	{SHOPT_ROOT_MAPPING, OPT_ROOT_MAPPING, OPT_TYPE_USER},
1627961SNatalie.Li@Sun.COM #define	OPT_CHARSET_MAP	16
1637961SNatalie.Li@Sun.COM 	{"", OPT_CHARSET_MAP, OPT_TYPE_ACCLIST},
16411323SVallish.Vaidyeshwara@Sun.COM #define	OPT_NOACLFAB	17
16511323SVallish.Vaidyeshwara@Sun.COM 	{SHOPT_NOACLFAB, OPT_NOACLFAB, OPT_TYPE_BOOLEAN},
1663910Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
16711323SVallish.Vaidyeshwara@Sun.COM #define	OPT_VOLFH	18
1683910Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1693910Sdougm #endif /* VOLATILE_FH_TEST */
1703910Sdougm 	NULL
1713910Sdougm };
1723910Sdougm 
1733910Sdougm /*
1747961SNatalie.Li@Sun.COM  * Codesets that may need to be converted to UTF-8 for file paths.
1757961SNatalie.Li@Sun.COM  * Add new names here to add new property support. If we ever get a
1767961SNatalie.Li@Sun.COM  * way to query the kernel for character sets, this should become
1777961SNatalie.Li@Sun.COM  * dynamically loaded. Make sure changes here are reflected in
1787961SNatalie.Li@Sun.COM  * cmd/fs.d/nfs/mountd/nfscmd.c
1797961SNatalie.Li@Sun.COM  */
1807961SNatalie.Li@Sun.COM 
1817961SNatalie.Li@Sun.COM static char *legal_conv[] = {
1827961SNatalie.Li@Sun.COM 	"euc-cn",
1837961SNatalie.Li@Sun.COM 	"euc-jp",
1847961SNatalie.Li@Sun.COM 	"euc-jpms",
1857961SNatalie.Li@Sun.COM 	"euc-kr",
1867961SNatalie.Li@Sun.COM 	"euc-tw",
1877961SNatalie.Li@Sun.COM 	"iso8859-1",
1887961SNatalie.Li@Sun.COM 	"iso8859-2",
1897961SNatalie.Li@Sun.COM 	"iso8859-5",
1907961SNatalie.Li@Sun.COM 	"iso8859-6",
1917961SNatalie.Li@Sun.COM 	"iso8859-7",
1927961SNatalie.Li@Sun.COM 	"iso8859-8",
1937961SNatalie.Li@Sun.COM 	"iso8859-9",
1947961SNatalie.Li@Sun.COM 	"iso8859-13",
1957961SNatalie.Li@Sun.COM 	"iso8859-15",
1967961SNatalie.Li@Sun.COM 	"koi8-r",
1977961SNatalie.Li@Sun.COM 	NULL
1987961SNatalie.Li@Sun.COM };
1997961SNatalie.Li@Sun.COM 
2007961SNatalie.Li@Sun.COM /*
2013910Sdougm  * list of properties that are related to security flavors.
2023910Sdougm  */
2033910Sdougm static char *seclist[] = {
2043910Sdougm 	SHOPT_RO,
2053910Sdougm 	SHOPT_RW,
2063910Sdougm 	SHOPT_ROOT,
2073910Sdougm 	SHOPT_WINDOW,
2087961SNatalie.Li@Sun.COM 	SHOPT_NONE,
2097961SNatalie.Li@Sun.COM 	SHOPT_ROOT_MAPPING,
2103910Sdougm 	NULL
2113910Sdougm };
2123910Sdougm 
2133910Sdougm /* structure for list of securities */
2143910Sdougm struct securities {
2153910Sdougm 	sa_security_t security;
2163910Sdougm 	struct securities *next;
2173910Sdougm };
2183910Sdougm 
2193910Sdougm /*
2207961SNatalie.Li@Sun.COM  * findcharset(charset)
2217961SNatalie.Li@Sun.COM  *
2227961SNatalie.Li@Sun.COM  * Returns B_TRUE if the charset is a legal conversion otherwise
2237961SNatalie.Li@Sun.COM  * B_FALSE. This will need to be rewritten to be more efficient when
2247961SNatalie.Li@Sun.COM  * we have a dynamic list of legal conversions.
2257961SNatalie.Li@Sun.COM  */
2267961SNatalie.Li@Sun.COM 
2277961SNatalie.Li@Sun.COM static boolean_t
2287961SNatalie.Li@Sun.COM findcharset(char *charset)
2297961SNatalie.Li@Sun.COM {
2307961SNatalie.Li@Sun.COM 	int i;
2317961SNatalie.Li@Sun.COM 
2327961SNatalie.Li@Sun.COM 	for (i = 0; legal_conv[i] != NULL; i++)
2337961SNatalie.Li@Sun.COM 		if (strcmp(charset, legal_conv[i]) == 0)
2347961SNatalie.Li@Sun.COM 			return (B_TRUE);
2357961SNatalie.Li@Sun.COM 	return (B_FALSE);
2367961SNatalie.Li@Sun.COM }
2377961SNatalie.Li@Sun.COM 
2387961SNatalie.Li@Sun.COM /*
2393910Sdougm  * findopt(name)
2403910Sdougm  *
2413910Sdougm  * Lookup option "name" in the option table and return the table
2423910Sdougm  * index.
2433910Sdougm  */
2443910Sdougm 
2453910Sdougm static int
2463910Sdougm findopt(char *name)
2473910Sdougm {
2483910Sdougm 	int i;
2493910Sdougm 	if (name != NULL) {
2504345Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
2514345Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
2524345Sdougm 				return (i);
2534345Sdougm 		}
2547961SNatalie.Li@Sun.COM 		if (findcharset(name))
2557961SNatalie.Li@Sun.COM 			return (OPT_CHARSET_MAP);
2563910Sdougm 	}
2573910Sdougm 	return (-1);
2583910Sdougm }
2593910Sdougm 
2603910Sdougm /*
2613910Sdougm  * gettype(name)
2623910Sdougm  *
2633910Sdougm  * Return the type of option "name".
2643910Sdougm  */
2653910Sdougm 
2663910Sdougm static int
2673910Sdougm gettype(char *name)
2683910Sdougm {
2693910Sdougm 	int optdef;
2703910Sdougm 
2713910Sdougm 	optdef = findopt(name);
2723910Sdougm 	if (optdef != -1)
2734345Sdougm 		return (optdefs[optdef].type);
2743910Sdougm 	return (OPT_TYPE_ANY);
2753910Sdougm }
2763910Sdougm 
2773910Sdougm /*
2783910Sdougm  * nfs_validate_security_mode(mode)
2793910Sdougm  *
2803910Sdougm  * is the specified mode string a valid one for use with NFS?
2813910Sdougm  */
2823910Sdougm 
2833910Sdougm static int
2843910Sdougm nfs_validate_security_mode(char *mode)
2853910Sdougm {
2863910Sdougm 	seconfig_t secinfo;
2873910Sdougm 	int err;
2883910Sdougm 
2893910Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2903910Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2913910Sdougm 	if (err == SC_NOERROR)
2924345Sdougm 		return (1);
2933910Sdougm 	return (0);
2943910Sdougm }
2953910Sdougm 
2963910Sdougm /*
2973910Sdougm  * nfs_is_security_opt(tok)
2983910Sdougm  *
2993910Sdougm  * check to see if tok represents an option that is only valid in some
3003910Sdougm  * security flavor.
3013910Sdougm  */
3023910Sdougm 
3033910Sdougm static int
3043910Sdougm nfs_is_security_opt(char *tok)
3053910Sdougm {
3063910Sdougm 	int i;
3073910Sdougm 
3083910Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
3094345Sdougm 		if (strcmp(tok, seclist[i]) == 0)
3104345Sdougm 			return (1);
3113910Sdougm 	}
3123910Sdougm 	return (0);
3133910Sdougm }
3143910Sdougm 
3153910Sdougm /*
3163910Sdougm  * find_security(seclist, sec)
3173910Sdougm  *
3183910Sdougm  * Walk the current list of security flavors and return true if it is
3193910Sdougm  * present, else return false.
3203910Sdougm  */
3213910Sdougm 
3223910Sdougm static int
3233910Sdougm find_security(struct securities *seclist, sa_security_t sec)
3243910Sdougm {
3253910Sdougm 	while (seclist != NULL) {
3264345Sdougm 		if (seclist->security == sec)
3274345Sdougm 			return (1);
3284345Sdougm 		seclist = seclist->next;
3293910Sdougm 	}
3303910Sdougm 	return (0);
3313910Sdougm }
3323910Sdougm 
3333910Sdougm /*
3343910Sdougm  * make_security_list(group, securitymodes, proto)
3353910Sdougm  *	go through the list of securitymodes and add them to the
3363910Sdougm  *	group's list of security optionsets. We also keep a list of
3373910Sdougm  *	those optionsets so we don't have to find them later. All of
3383910Sdougm  *	these will get copies of the same properties.
3393910Sdougm  */
3403910Sdougm 
3413910Sdougm static struct securities *
3423910Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
3433910Sdougm {
3443910Sdougm 	char *tok, *next = NULL;
3453910Sdougm 	struct securities *curp, *headp = NULL, *prev;
3463910Sdougm 	sa_security_t check;
3473910Sdougm 	int freetok = 0;
3483910Sdougm 
3493910Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
3504345Sdougm 		next = strchr(tok, ':');
3514345Sdougm 		if (next != NULL)
3524345Sdougm 			*next++ = '\0';
3534345Sdougm 		if (strcmp(tok, "default") == 0) {
3544345Sdougm 			/* resolve default into the real type */
3554345Sdougm 			tok = nfs_space_alias(tok);
3564345Sdougm 			freetok = 1;
3574345Sdougm 		}
3584345Sdougm 		check = sa_get_security(group, tok, proto);
3593910Sdougm 
3604345Sdougm 		/* add to the security list if it isn't there already */
3614345Sdougm 		if (check == NULL || !find_security(headp, check)) {
3624345Sdougm 			curp = (struct securities *)calloc(1,
3634345Sdougm 			    sizeof (struct securities));
3644345Sdougm 			if (curp != NULL) {
3654345Sdougm 				if (check == NULL) {
3664345Sdougm 					curp->security = sa_create_security(
3674345Sdougm 					    group, tok, proto);
3684345Sdougm 				} else {
3694345Sdougm 					curp->security = check;
3704345Sdougm 				}
3714345Sdougm 				/*
3724345Sdougm 				 * note that the first time through the loop,
3734345Sdougm 				 * headp will be NULL and prev will be
3744345Sdougm 				 * undefined.  Since headp is NULL, we set
3754345Sdougm 				 * both it and prev to the curp (first
3764345Sdougm 				 * structure to be allocated).
3774345Sdougm 				 *
3784345Sdougm 				 * later passes through the loop will have
3794345Sdougm 				 * headp not being NULL and prev will be used
3804345Sdougm 				 * to allocate at the end of the list.
3814345Sdougm 				 */
3824345Sdougm 				if (headp == NULL) {
3834345Sdougm 					headp = curp;
3844345Sdougm 					prev = curp;
3854345Sdougm 				} else {
3864345Sdougm 					prev->next = curp;
3874345Sdougm 					prev = curp;
3884345Sdougm 				}
3894345Sdougm 			}
3903910Sdougm 		}
3913910Sdougm 
3924345Sdougm 		if (freetok) {
3934345Sdougm 			freetok = 0;
3944345Sdougm 			sa_free_attr_string(tok);
3954345Sdougm 		}
3963910Sdougm 	}
3973910Sdougm 	return (headp);
3983910Sdougm }
3993910Sdougm 
4003910Sdougm static void
4013910Sdougm free_security_list(struct securities *sec)
4023910Sdougm {
4033910Sdougm 	struct securities *next;
4043910Sdougm 	if (sec != NULL) {
4054345Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
4064345Sdougm 			next = sec->next;
4074345Sdougm 			free(sec);
4084345Sdougm 		}
4093910Sdougm 	}
4103910Sdougm }
4113910Sdougm 
4123910Sdougm /*
4133910Sdougm  * nfs_alistcat(str1, str2, sep)
4143910Sdougm  *
4153910Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
4163910Sdougm  * character. If memory allocation fails, return NULL;
4173910Sdougm  */
4183910Sdougm 
4193910Sdougm static char *
4203910Sdougm nfs_alistcat(char *str1, char *str2, char sep)
4213910Sdougm {
4223910Sdougm 	char *newstr;
4233910Sdougm 	size_t len;
4243910Sdougm 
4253910Sdougm 	len = strlen(str1) + strlen(str2) + 2;
4263910Sdougm 	newstr = (char *)malloc(len);
4273910Sdougm 	if (newstr != NULL)
4284345Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
4293910Sdougm 	return (newstr);
4303910Sdougm }
4313910Sdougm 
4323910Sdougm /*
4333910Sdougm  * add_security_prop(sec, name, value, persist)
4343910Sdougm  *
4353910Sdougm  * Add the property to the securities structure. This accumulates
4363910Sdougm  * properties for as part of parsing legacy options.
4373910Sdougm  */
4383910Sdougm 
4393910Sdougm static int
4403910Sdougm add_security_prop(struct securities *sec, char *name, char *value,
4413910Sdougm 			int persist, int iszfs)
4423910Sdougm {
4433910Sdougm 	sa_property_t prop;
4443910Sdougm 	int ret = SA_OK;
4453910Sdougm 
4463910Sdougm 	for (; sec != NULL; sec = sec->next) {
4474345Sdougm 		if (value == NULL) {
4484345Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
4494345Sdougm 			    strcmp(name, SHOPT_RO) == 0)
4504345Sdougm 				value = "*";
4514345Sdougm 			else
4524345Sdougm 				value = "true";
4534345Sdougm 		}
4543910Sdougm 
4553910Sdougm 		/*
4563910Sdougm 		 * Get the existing property, if it exists, so we can
4573910Sdougm 		 * determine what to do with it. The ro/rw/root
4583910Sdougm 		 * properties can be merged if multiple instances of
4593910Sdougm 		 * these properies are given. For example, if "rw"
4603910Sdougm 		 * exists with a value "host1" and a later token of
4613910Sdougm 		 * rw="host2" is seen, the values are merged into a
4623910Sdougm 		 * single rw="host1:host2".
4633910Sdougm 		 */
4644345Sdougm 		prop = sa_get_property(sec->security, name);
4653910Sdougm 
4664345Sdougm 		if (prop != NULL) {
4674345Sdougm 			char *oldvalue;
4684345Sdougm 			char *newvalue;
4693910Sdougm 
4703910Sdougm 			/*
4714345Sdougm 			 * The security options of ro/rw/root might appear
4724345Sdougm 			 * multiple times. If they do, the values need to be
4734345Sdougm 			 * merged into an access list. If it was previously
4744345Sdougm 			 * empty, the new value alone is added.
4753910Sdougm 			 */
4764345Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
4774345Sdougm 			if (oldvalue != NULL) {
4784345Sdougm 				/*
4794345Sdougm 				 * The general case is to concatenate the new
4804345Sdougm 				 * value onto the old value for multiple
4814345Sdougm 				 * rw(ro/root) properties. A special case
4824345Sdougm 				 * exists when either the old or new is the
4834345Sdougm 				 * "all" case. In the special case, if both
4844345Sdougm 				 * are "all", then it is "all", else if one is
4854345Sdougm 				 * an access-list, that replaces the "all".
4864345Sdougm 				 */
4874345Sdougm 				if (strcmp(oldvalue, "*") == 0) {
4884345Sdougm 					/* Replace old value with new value. */
4894345Sdougm 					newvalue = strdup(value);
4905454Sdougm 				} else if (strcmp(value, "*") == 0 ||
4915454Sdougm 				    strcmp(oldvalue, value) == 0) {
4924345Sdougm 					/*
4934345Sdougm 					 * Keep old value and ignore
4944345Sdougm 					 * the new value.
4954345Sdougm 					 */
4964345Sdougm 					newvalue = NULL;
4974345Sdougm 				} else {
4984345Sdougm 					/*
4994345Sdougm 					 * Make a new list of old plus new
5004345Sdougm 					 * access-list.
5014345Sdougm 					 */
5024345Sdougm 					newvalue = nfs_alistcat(oldvalue,
5034345Sdougm 					    value, ':');
5044345Sdougm 				}
5053910Sdougm 
5064345Sdougm 				if (newvalue != NULL) {
5074345Sdougm 					(void) sa_remove_property(prop);
5084345Sdougm 					prop = sa_create_property(name,
5094345Sdougm 					    newvalue);
5104345Sdougm 					ret = sa_add_property(sec->security,
5114345Sdougm 					    prop);
5124345Sdougm 					free(newvalue);
5134345Sdougm 				}
5144345Sdougm 				if (oldvalue != NULL)
5154345Sdougm 					sa_free_attr_string(oldvalue);
5164345Sdougm 			}
5174345Sdougm 		} else {
5184345Sdougm 			prop = sa_create_property(name, value);
5193910Sdougm 			ret = sa_add_property(sec->security, prop);
5203910Sdougm 		}
5214345Sdougm 		if (ret == SA_OK && !iszfs) {
5224345Sdougm 			ret = sa_commit_properties(sec->security, !persist);
5234345Sdougm 		}
5243910Sdougm 	}
5253910Sdougm 	return (ret);
5263910Sdougm }
5273910Sdougm 
5283910Sdougm /*
5293910Sdougm  * check to see if group/share is persistent.
5303910Sdougm  */
5313910Sdougm static int
5323910Sdougm is_persistent(sa_group_t group)
5333910Sdougm {
5343910Sdougm 	char *type;
5353910Sdougm 	int persist = 1;
5363910Sdougm 
5373910Sdougm 	type = sa_get_group_attr(group, "type");
5383910Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
5394345Sdougm 		persist = 0;
5403910Sdougm 	if (type != NULL)
5414345Sdougm 		sa_free_attr_string(type);
5423910Sdougm 	return (persist);
5433910Sdougm }
5443910Sdougm 
5453910Sdougm /*
5463910Sdougm  * invalid_security(options)
5473910Sdougm  *
5483910Sdougm  * search option string for any invalid sec= type.
5493910Sdougm  * return true (1) if any are not valid else false (0)
5503910Sdougm  */
5513910Sdougm static int
5523910Sdougm invalid_security(char *options)
5533910Sdougm {
5543910Sdougm 	char *copy, *base, *token, *value;
5553910Sdougm 	int ret = 0;
5563910Sdougm 
5573910Sdougm 	copy = strdup(options);
5583910Sdougm 	token = base = copy;
5593910Sdougm 	while (token != NULL && ret == 0) {
5604345Sdougm 		token = strtok(base, ",");
5614345Sdougm 		base = NULL;
5624345Sdougm 		if (token != NULL) {
5634345Sdougm 			value = strchr(token, '=');
5644345Sdougm 			if (value != NULL)
5654345Sdougm 				*value++ = '\0';
5664345Sdougm 			if (strcmp(token, "sec") == 0) {
5674345Sdougm 				/* HAVE security flavors so check them */
5684345Sdougm 				char *tok, *next;
5694345Sdougm 				for (next = NULL, tok = value; tok != NULL;
5704345Sdougm 				    tok = next) {
5714345Sdougm 					next = strchr(tok, ':');
5724345Sdougm 					if (next != NULL)
5734345Sdougm 						*next++ = '\0';
5744345Sdougm 					ret = !nfs_validate_security_mode(tok);
5754345Sdougm 					if (ret)
5764345Sdougm 						break;
5774345Sdougm 				}
5784345Sdougm 			}
5793910Sdougm 		}
5803910Sdougm 	}
5813910Sdougm 	if (copy != NULL)
5824345Sdougm 		free(copy);
5833910Sdougm 	return (ret);
5843910Sdougm }
5853910Sdougm 
5863910Sdougm /*
5873910Sdougm  * nfs_parse_legacy_options(group, options)
5883910Sdougm  *
5893910Sdougm  * Parse the old style options into internal format and store on the
5903910Sdougm  * specified group.  Group could be a share for full legacy support.
5913910Sdougm  */
5923910Sdougm 
5933910Sdougm static int
5943910Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5953910Sdougm {
5964704Sdougm 	char *dup;
5973910Sdougm 	char *base;
5983910Sdougm 	char *token;
5993910Sdougm 	sa_optionset_t optionset;
6003910Sdougm 	struct securities *security_list = NULL;
6013910Sdougm 	sa_property_t prop;
6023910Sdougm 	int ret = SA_OK;
6033910Sdougm 	int iszfs = 0;
6043910Sdougm 	sa_group_t parent;
6053910Sdougm 	int persist = 0;
6063910Sdougm 	char *lasts;
6073910Sdougm 
6083910Sdougm 	/* do we have an existing optionset? */
6093910Sdougm 	optionset = sa_get_optionset(group, "nfs");
6103910Sdougm 	if (optionset == NULL) {
6114345Sdougm 		/* didn't find existing optionset so create one */
6124345Sdougm 		optionset = sa_create_optionset(group, "nfs");
6133910Sdougm 	} else {
6143910Sdougm 		/*
6155331Samw 		 * Have an existing optionset . Ideally, we would need
6165331Samw 		 * to compare options in order to detect errors. For
6175331Samw 		 * now, we assume that the first optionset is the
6185331Samw 		 * correct one and the others will be the same. An
6195331Samw 		 * empty optionset is the same as no optionset so we
6205331Samw 		 * don't want to exit in that case. Getting an empty
6215331Samw 		 * optionset can occur with ZFS property checking.
6223910Sdougm 		 */
6235331Samw 		if (sa_get_property(optionset, NULL) != NULL)
6245331Samw 			return (ret);
6253910Sdougm 	}
6263910Sdougm 
6273910Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
6283910Sdougm 		/*
6293910Sdougm 		 * there is a special case of only the option "rw"
6303910Sdougm 		 * being the default option. We don't have to do
6313910Sdougm 		 * anything.
6323910Sdougm 		 */
6334345Sdougm 		return (ret);
6343910Sdougm 	}
6353910Sdougm 
6363910Sdougm 	/*
6373910Sdougm 	 * check if security types are present and validate them. If
6383910Sdougm 	 * any are not legal, fail.
6393910Sdougm 	 */
6403910Sdougm 
6413910Sdougm 	if (invalid_security(options)) {
6424345Sdougm 		return (SA_INVALID_SECURITY);
6433910Sdougm 	}
6443910Sdougm 
6453910Sdougm 	/*
6463910Sdougm 	 * in order to not attempt to change ZFS properties unless
6473910Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
6483910Sdougm 	 */
6493910Sdougm 	if (sa_is_share(group)) {
6504345Sdougm 		char *zfs;
6514345Sdougm 		parent = sa_get_parent_group(group);
6524345Sdougm 		if (parent != NULL) {
6534345Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
6544345Sdougm 			if (zfs != NULL) {
6554345Sdougm 				sa_free_attr_string(zfs);
6564345Sdougm 				iszfs++;
6574345Sdougm 			}
6583910Sdougm 		}
6593910Sdougm 	} else {
6604345Sdougm 		iszfs = sa_group_is_zfs(group);
6613910Sdougm 	}
6623910Sdougm 
6634704Sdougm 	/* We need a copy of options for the next part. */
6644704Sdougm 	dup = strdup(options);
6654704Sdougm 	if (dup == NULL)
6664704Sdougm 		return (SA_NO_MEMORY);
6674704Sdougm 
6683910Sdougm 	/*
6693910Sdougm 	 * we need to step through each option in the string and then
6703910Sdougm 	 * add either the option or the security option as needed. If
6713910Sdougm 	 * this is not a persistent share, don't commit to the
6723910Sdougm 	 * repository. If there is an error, we also want to abort the
6733910Sdougm 	 * processing and report it.
6743910Sdougm 	 */
6753910Sdougm 	persist = is_persistent(group);
6763910Sdougm 	base = dup;
6773910Sdougm 	token = dup;
6783910Sdougm 	lasts = NULL;
6793910Sdougm 	while (token != NULL && ret == SA_OK) {
6804345Sdougm 		ret = SA_OK;
6814345Sdougm 		token = strtok_r(base, ",", &lasts);
6824345Sdougm 		base = NULL;
6834345Sdougm 		if (token != NULL) {
6844345Sdougm 			char *value;
6853910Sdougm 			/*
6864345Sdougm 			 * if the option has a value, it will have an '=' to
6874345Sdougm 			 * separate the name from the value. The following
6884345Sdougm 			 * code will result in value != NULL and token
6894345Sdougm 			 * pointing to just the name if there is a value.
6903910Sdougm 			 */
6914345Sdougm 			value = strchr(token, '=');
6924345Sdougm 			if (value != NULL) {
6934345Sdougm 				*value++ = '\0';
6944345Sdougm 			}
6954345Sdougm 			if (strcmp(token, "sec") == 0 ||
6964345Sdougm 			    strcmp(token, "secure") == 0) {
6973910Sdougm 				/*
6984345Sdougm 				 * Once in security parsing, we only
6994345Sdougm 				 * do security. We do need to move
7004345Sdougm 				 * between the security node and the
7014345Sdougm 				 * toplevel. The security tag goes on
7024345Sdougm 				 * the root while the following ones
7034345Sdougm 				 * go on the security.
7043910Sdougm 				 */
7054345Sdougm 				if (security_list != NULL) {
7064345Sdougm 					/*
7074345Sdougm 					 * have an old list so close it and
7084345Sdougm 					 * start the new
7094345Sdougm 					 */
7104345Sdougm 					free_security_list(security_list);
7114345Sdougm 				}
7124345Sdougm 				if (strcmp(token, "secure") == 0) {
7134345Sdougm 					value = "dh";
7144345Sdougm 				} else {
7154345Sdougm 					if (value == NULL) {
7164345Sdougm 						ret = SA_SYNTAX_ERR;
7174345Sdougm 						break;
7184345Sdougm 					}
7194345Sdougm 				}
7204345Sdougm 				security_list = make_security_list(group,
7214345Sdougm 				    value, "nfs");
7223910Sdougm 			} else {
7234345Sdougm 				/*
7244345Sdougm 				 * Note that the "old" syntax allowed a
7254345Sdougm 				 * default security model This must be
7264345Sdougm 				 * accounted for and internally converted to
7274345Sdougm 				 * "standard" security structure.
7284345Sdougm 				 */
7294345Sdougm 				if (nfs_is_security_opt(token)) {
7304345Sdougm 					if (security_list == NULL) {
7314345Sdougm 						/*
7324345Sdougm 						 * need to have a
7334345Sdougm 						 * security
7344345Sdougm 						 * option. This will
7354345Sdougm 						 * be "closed" when a
7364345Sdougm 						 * defined "sec="
7374345Sdougm 						 * option is
7384345Sdougm 						 * seen. This is
7394345Sdougm 						 * technically an
7404345Sdougm 						 * error but will be
7414345Sdougm 						 * allowed with
7424345Sdougm 						 * warning.
7434345Sdougm 						 */
7444345Sdougm 						security_list =
7454345Sdougm 						    make_security_list(group,
7464345Sdougm 						    "default",
7474345Sdougm 						    "nfs");
7484345Sdougm 					}
7494345Sdougm 					if (security_list != NULL) {
7504345Sdougm 						ret = add_security_prop(
7514345Sdougm 						    security_list, token,
7524345Sdougm 						    value, persist, iszfs);
7534345Sdougm 					} else {
7544345Sdougm 						ret = SA_NO_MEMORY;
7554345Sdougm 					}
7564345Sdougm 				} else {
7574345Sdougm 					/* regular options */
7584345Sdougm 					if (value == NULL) {
7594345Sdougm 						if (strcmp(token, SHOPT_RW) ==
7604345Sdougm 						    0 || strcmp(token,
7614345Sdougm 						    SHOPT_RO) == 0) {
7624345Sdougm 							value = "*";
7634345Sdougm 						} else {
7644345Sdougm 							value = "global";
7654345Sdougm 							if (strcmp(token,
7664345Sdougm 							    SHOPT_LOG) != 0) {
7674345Sdougm 								value = "true";
7684345Sdougm 							}
7694345Sdougm 						}
7704345Sdougm 					}
7714372Sdougm 					/*
7724372Sdougm 					 * In all cases, create the
7734372Sdougm 					 * property specified. If the
7744372Sdougm 					 * value was NULL, the default
7754372Sdougm 					 * value will have been
7764372Sdougm 					 * substituted.
7774372Sdougm 					 */
7784372Sdougm 					prop = sa_create_property(token, value);
7794372Sdougm 					ret =  sa_add_property(optionset, prop);
7804372Sdougm 					if (ret != SA_OK)
7814372Sdougm 						break;
7824372Sdougm 
7834345Sdougm 					if (!iszfs) {
7844345Sdougm 						ret = sa_commit_properties(
7854345Sdougm 						    optionset, !persist);
7864345Sdougm 					}
7874345Sdougm 				}
7883910Sdougm 			}
7893910Sdougm 		}
7903910Sdougm 	}
7913910Sdougm 	if (security_list != NULL)
7924345Sdougm 		free_security_list(security_list);
7934704Sdougm 
7944704Sdougm 	free(dup);
7953910Sdougm 	return (ret);
7963910Sdougm }
7973910Sdougm 
7983910Sdougm /*
7993910Sdougm  * is_a_number(number)
8003910Sdougm  *
8013910Sdougm  * is the string a number in one of the forms we want to use?
8023910Sdougm  */
8033910Sdougm 
8043910Sdougm static int
8053910Sdougm is_a_number(char *number)
8063910Sdougm {
8073910Sdougm 	int ret = 1;
8083910Sdougm 	int hex = 0;
8093910Sdougm 
8103910Sdougm 	if (strncmp(number, "0x", 2) == 0) {
8114345Sdougm 		number += 2;
8124345Sdougm 		hex = 1;
8134345Sdougm 	} else if (*number == '-') {
8144345Sdougm 		number++; /* skip the minus */
8154345Sdougm 	}
8163910Sdougm 	while (ret == 1 && *number != '\0') {
8174345Sdougm 		if (hex) {
8184345Sdougm 			ret = isxdigit(*number++);
8194345Sdougm 		} else {
8204345Sdougm 			ret = isdigit(*number++);
8214345Sdougm 		}
8223910Sdougm 	}
8233910Sdougm 	return (ret);
8243910Sdougm }
8253910Sdougm 
8263910Sdougm /*
8273910Sdougm  * Look for the specified tag in the configuration file. If it is found,
8283910Sdougm  * enable logging and set the logging configuration information for exp.
8293910Sdougm  */
8303910Sdougm static void
8313910Sdougm configlog(struct exportdata *exp, char *tag)
8323910Sdougm {
8333910Sdougm 	nfsl_config_t *configlist = NULL, *configp;
8343910Sdougm 	int error = 0;
8353910Sdougm 	char globaltag[] = DEFAULTTAG;
8363910Sdougm 
8373910Sdougm 	/*
8383910Sdougm 	 * Sends config errors to stderr
8393910Sdougm 	 */
8403910Sdougm 	nfsl_errs_to_syslog = B_FALSE;
8413910Sdougm 
8423910Sdougm 	/*
8433910Sdougm 	 * get the list of configuration settings
8443910Sdougm 	 */
8453910Sdougm 	error = nfsl_getconfig_list(&configlist);
8463910Sdougm 	if (error) {
8473910Sdougm 		(void) fprintf(stderr,
8484345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
8494345Sdougm 		    strerror(error));
8503910Sdougm 	}
8513910Sdougm 
8523910Sdougm 	if (tag == NULL)
8533910Sdougm 		tag = globaltag;
8543910Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
8553910Sdougm 		nfsl_freeconfig_list(&configlist);
8563910Sdougm 		(void) fprintf(stderr,
8574345Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
8583910Sdougm 		/* bad configuration */
8593910Sdougm 		error = ENOENT;
8603910Sdougm 		goto err;
8613910Sdougm 	}
8623910Sdougm 
8633910Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
8643910Sdougm 		error = ENOMEM;
8653910Sdougm 		goto out;
8663910Sdougm 	}
8673910Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
8683910Sdougm 		error = ENOMEM;
8693910Sdougm 		goto out;
8703910Sdougm 	}
8713910Sdougm 	exp->ex_flags |= EX_LOG;
8723910Sdougm 	if (configp->nc_rpclogpath != NULL)
8733910Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8743910Sdougm out:
8753910Sdougm 	if (configlist != NULL)
8764345Sdougm 		nfsl_freeconfig_list(&configlist);
8773910Sdougm 
8783910Sdougm err:
8793910Sdougm 	if (error != 0) {
8803910Sdougm 		if (exp->ex_flags != NULL)
8813910Sdougm 			free(exp->ex_tag);
8823910Sdougm 		if (exp->ex_log_buffer != NULL)
8833910Sdougm 			free(exp->ex_log_buffer);
8843910Sdougm 		(void) fprintf(stderr,
8854345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
8864345Sdougm 		    strerror(error));
8873910Sdougm 	}
8883910Sdougm }
8893910Sdougm 
8903910Sdougm /*
8913910Sdougm  * fill_export_from_optionset(export, optionset)
8923910Sdougm  *
8933910Sdougm  * In order to share, we need to set all the possible general options
8943910Sdougm  * into the export structure. Share info will be filled in by the
8953910Sdougm  * caller. Various property values get turned into structure specific
8963910Sdougm  * values.
8973910Sdougm  */
8983910Sdougm 
8993910Sdougm static int
9003910Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
9013910Sdougm {
9023910Sdougm 	sa_property_t option;
9033910Sdougm 	int ret = SA_OK;
9043910Sdougm 
9053910Sdougm 	for (option = sa_get_property(optionset, NULL);
9064345Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
9074345Sdougm 		char *name;
9084345Sdougm 		char *value;
9094345Sdougm 		uint32_t val;
9103910Sdougm 
9114345Sdougm 		/*
9124345Sdougm 		 * since options may be set/reset multiple times, always do an
9134345Sdougm 		 * explicit set or clear of the option. This allows defaults
9145331Samw 		 * to be set and then the protocol specific to override.
9154345Sdougm 		 */
9163910Sdougm 
9174345Sdougm 		name = sa_get_property_attr(option, "type");
9184345Sdougm 		value = sa_get_property_attr(option, "value");
9194345Sdougm 		switch (findopt(name)) {
9204345Sdougm 		case OPT_ANON:
9214345Sdougm 			if (value != NULL && is_a_number(value)) {
9224345Sdougm 				val = strtoul(value, NULL, 0);
9234345Sdougm 			} else {
9244345Sdougm 				struct passwd *pw;
9254345Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
9264345Sdougm 				if (pw != NULL) {
9274345Sdougm 					val = pw->pw_uid;
9284345Sdougm 				} else {
9294345Sdougm 					val = UID_NOBODY;
9304345Sdougm 				}
9314345Sdougm 				endpwent();
9324345Sdougm 			}
9334345Sdougm 			export->ex_anon = val;
9344345Sdougm 			break;
9354345Sdougm 		case OPT_NOSUID:
9364345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9374345Sdougm 			    strcmp(value, "1") == 0))
9384345Sdougm 				export->ex_flags |= EX_NOSUID;
9394345Sdougm 			else
9404345Sdougm 				export->ex_flags &= ~EX_NOSUID;
9414345Sdougm 			break;
9424345Sdougm 		case OPT_ACLOK:
9434345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9444345Sdougm 			    strcmp(value, "1") == 0))
9454345Sdougm 				export->ex_flags |= EX_ACLOK;
9464345Sdougm 			else
9474345Sdougm 				export->ex_flags &= ~EX_ACLOK;
9484345Sdougm 			break;
9494345Sdougm 		case OPT_NOSUB:
9504345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9514345Sdougm 			    strcmp(value, "1") == 0))
9524345Sdougm 				export->ex_flags |= EX_NOSUB;
9534345Sdougm 			else
9544345Sdougm 				export->ex_flags &= ~EX_NOSUB;
9554345Sdougm 			break;
9564345Sdougm 		case OPT_PUBLIC:
9574345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9584345Sdougm 			    strcmp(value, "1") == 0))
9594345Sdougm 				export->ex_flags |= EX_PUBLIC;
9604345Sdougm 			else
9614345Sdougm 				export->ex_flags &= ~EX_PUBLIC;
9624345Sdougm 			break;
9634345Sdougm 		case OPT_INDEX:
9644345Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
9654345Sdougm 			    strchr(value, '/') != NULL)) {
9664345Sdougm 				/* this is an error */
9674345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
9684345Sdougm 				    "NFS: index=\"%s\" not valid;"
9694345Sdougm 				    "must be a filename.\n"),
9704345Sdougm 				    value);
9714345Sdougm 				break;
9724345Sdougm 			}
9734345Sdougm 			if (value != NULL && *value != '\0' &&
9744345Sdougm 			    strcmp(value, ".") != 0) {
9754345Sdougm 				/* valid index file string */
9764345Sdougm 				if (export->ex_index != NULL) {
9774345Sdougm 					/* left over from "default" */
9784345Sdougm 					free(export->ex_index);
9794345Sdougm 				}
9804345Sdougm 				/* remember to free */
9814345Sdougm 				export->ex_index = strdup(value);
9824345Sdougm 				if (export->ex_index == NULL) {
9834345Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
9844345Sdougm 					    "NFS: out of memory setting "
9854345Sdougm 					    "index property\n"));
9864345Sdougm 					break;
9874345Sdougm 				}
9884345Sdougm 				export->ex_flags |= EX_INDEX;
9894345Sdougm 			}
9904345Sdougm 			break;
9914345Sdougm 		case OPT_LOG:
9924345Sdougm 			if (value == NULL)
9934345Sdougm 				value = strdup("global");
9944345Sdougm 			if (value != NULL)
9954345Sdougm 				configlog(export,
9964345Sdougm 				    strlen(value) ? value : "global");
9974345Sdougm 			break;
9987961SNatalie.Li@Sun.COM 		case OPT_CHARSET_MAP:
9997961SNatalie.Li@Sun.COM 			/*
10007961SNatalie.Li@Sun.COM 			 * Set EX_CHARMAP when there is at least one
10017961SNatalie.Li@Sun.COM 			 * charmap conversion property. This will get
10027961SNatalie.Li@Sun.COM 			 * checked by the nfs server when it needs to.
10037961SNatalie.Li@Sun.COM 			 */
10047961SNatalie.Li@Sun.COM 			export->ex_flags |= EX_CHARMAP;
10057961SNatalie.Li@Sun.COM 			break;
100611323SVallish.Vaidyeshwara@Sun.COM 		case OPT_NOACLFAB:
100711323SVallish.Vaidyeshwara@Sun.COM 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
100811323SVallish.Vaidyeshwara@Sun.COM 			    strcmp(value, "1") == 0))
100911323SVallish.Vaidyeshwara@Sun.COM 				export->ex_flags |= EX_NOACLFAB;
101011323SVallish.Vaidyeshwara@Sun.COM 			else
101111323SVallish.Vaidyeshwara@Sun.COM 				export->ex_flags &= ~EX_NOACLFAB;
101211323SVallish.Vaidyeshwara@Sun.COM 			break;
10134345Sdougm 		default:
10144345Sdougm 			/* have a syntactic error */
10154345Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
10164345Sdougm 			    "NFS: unrecognized option %s=%s\n"),
1017*11337SWilliam.Krier@Sun.COM 			    name != NULL ? name : "",
1018*11337SWilliam.Krier@Sun.COM 			    value != NULL ? value : "");
10194345Sdougm 			break;
10203910Sdougm 		}
10214345Sdougm 		if (name != NULL)
10224345Sdougm 			sa_free_attr_string(name);
10233910Sdougm 		if (value != NULL)
10244345Sdougm 			sa_free_attr_string(value);
10253910Sdougm 	}
10263910Sdougm 	return (ret);
10273910Sdougm }
10283910Sdougm 
10293910Sdougm /*
10303910Sdougm  * cleanup_export(export)
10313910Sdougm  *
10323910Sdougm  * Cleanup the allocated areas so we don't leak memory
10333910Sdougm  */
10343910Sdougm 
10353910Sdougm static void
10363910Sdougm cleanup_export(struct exportdata *export)
10373910Sdougm {
10383910Sdougm 	int i;
10393910Sdougm 
10403910Sdougm 	if (export->ex_index != NULL)
10414345Sdougm 		free(export->ex_index);
10423910Sdougm 	if (export->ex_secinfo != NULL) {
10434345Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
10444345Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
10454345Sdougm 				free(export->ex_secinfo[i].s_rootnames);
10464345Sdougm 		free(export->ex_secinfo);
10473910Sdougm 	}
10483910Sdougm }
10493910Sdougm 
10503910Sdougm /*
10513910Sdougm  * Given a seconfig entry and a colon-separated
10523910Sdougm  * list of names, allocate an array big enough
10533910Sdougm  * to hold the root list, then convert each name to
10543910Sdougm  * a principal name according to the security
10553910Sdougm  * info and assign it to an array element.
10563910Sdougm  * Return the array and its size.
10573910Sdougm  */
10583910Sdougm static caddr_t *
10593910Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
10603910Sdougm {
10613910Sdougm 	caddr_t *a;
10623910Sdougm 	int c, i;
10633910Sdougm 	char *host, *p;
10643910Sdougm 
10653910Sdougm 	/*
10663910Sdougm 	 * Count the number of strings in the list.
10673910Sdougm 	 * This is the number of colon separators + 1.
10683910Sdougm 	 */
10693910Sdougm 	c = 1;
10703910Sdougm 	for (p = list; *p; p++)
10713910Sdougm 		if (*p == ':')
10723910Sdougm 			c++;
10733910Sdougm 	*count = c;
10743910Sdougm 
10753910Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
10763910Sdougm 	if (a == NULL) {
10773910Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
10784345Sdougm 		    "get_rootnames: no memory\n"));
10793910Sdougm 	} else {
10804345Sdougm 		for (i = 0; i < c; i++) {
10814345Sdougm 			host = strtok(list, ":");
10824345Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
10834345Sdougm 				free(a);
10844345Sdougm 				a = NULL;
10854345Sdougm 				break;
10864345Sdougm 			}
10874345Sdougm 			list = NULL;
10883910Sdougm 		}
10893910Sdougm 	}
10903910Sdougm 
10913910Sdougm 	return (a);
10923910Sdougm }
10933910Sdougm 
10943910Sdougm /*
10953910Sdougm  * fill_security_from_secopts(sp, secopts)
10963910Sdougm  *
10973910Sdougm  * Fill the secinfo structure from the secopts optionset.
10983910Sdougm  */
10993910Sdougm 
11003910Sdougm static int
11013910Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
11023910Sdougm {
11033910Sdougm 	sa_property_t prop;
11043910Sdougm 	char *type;
11053910Sdougm 	int longform;
11063910Sdougm 	int err = SC_NOERROR;
11077961SNatalie.Li@Sun.COM 	uint32_t val;
11083910Sdougm 
11093910Sdougm 	type = sa_get_security_attr(secopts, "sectype");
11103910Sdougm 	if (type != NULL) {
11114345Sdougm 		/* named security type needs secinfo to be filled in */
11124345Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
11134345Sdougm 		sa_free_attr_string(type);
11144345Sdougm 		if (err != SC_NOERROR)
11154345Sdougm 			return (err);
11163910Sdougm 	} else {
11174345Sdougm 		/* default case */
11184345Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
11194345Sdougm 		if (err != SC_NOERROR)
11204345Sdougm 			return (err);
11213910Sdougm 	}
11223910Sdougm 
11233910Sdougm 	err = SA_OK;
11243910Sdougm 	for (prop = sa_get_property(secopts, NULL);
11254345Sdougm 	    prop != NULL && err == SA_OK;
11264345Sdougm 	    prop = sa_get_next_property(prop)) {
11274345Sdougm 		char *name;
11284345Sdougm 		char *value;
11293910Sdougm 
11304345Sdougm 		name = sa_get_property_attr(prop, "type");
11314345Sdougm 		value = sa_get_property_attr(prop, "value");
11323910Sdougm 
11334345Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
11343910Sdougm 
11354345Sdougm 		switch (findopt(name)) {
11364345Sdougm 		case OPT_RO:
11374345Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
11384345Sdougm 			break;
11394345Sdougm 		case OPT_RW:
11404345Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
11414345Sdougm 			break;
11424345Sdougm 		case OPT_ROOT:
11434345Sdougm 			sp->s_flags |= M_ROOT;
11444345Sdougm 			/*
11454345Sdougm 			 * if we are using AUTH_UNIX, handle like other things
11464345Sdougm 			 * such as RO/RW
11474345Sdougm 			 */
11484345Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
11494345Sdougm 				continue;
11504345Sdougm 			/* not AUTH_UNIX */
11514345Sdougm 			if (value != NULL) {
11524345Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
11534345Sdougm 				    value, &sp->s_rootcnt);
11544345Sdougm 				if (sp->s_rootnames == NULL) {
11554345Sdougm 					err = SA_BAD_VALUE;
11564345Sdougm 					(void) fprintf(stderr,
11574345Sdougm 					    dgettext(TEXT_DOMAIN,
11584345Sdougm 					    "Bad root list\n"));
11594345Sdougm 				}
11604345Sdougm 			}
11614345Sdougm 			break;
11627961SNatalie.Li@Sun.COM 		case OPT_NONE:
11637961SNatalie.Li@Sun.COM 			sp->s_flags |= M_NONE;
11647961SNatalie.Li@Sun.COM 			break;
11654345Sdougm 		case OPT_WINDOW:
11664345Sdougm 			if (value != NULL) {
11674345Sdougm 				sp->s_window = atoi(value);
11684345Sdougm 				/* just in case */
11694345Sdougm 				if (sp->s_window < 0)
11704345Sdougm 					sp->s_window = DEF_WIN;
11714345Sdougm 			}
11724345Sdougm 			break;
11737961SNatalie.Li@Sun.COM 		case OPT_ROOT_MAPPING:
11747961SNatalie.Li@Sun.COM 			if (value != NULL && is_a_number(value)) {
11757961SNatalie.Li@Sun.COM 				val = strtoul(value, NULL, 0);
11767961SNatalie.Li@Sun.COM 			} else {
11777961SNatalie.Li@Sun.COM 				struct passwd *pw;
11787961SNatalie.Li@Sun.COM 				pw = getpwnam(value != NULL ? value : "nobody");
11797961SNatalie.Li@Sun.COM 				if (pw != NULL) {
11807961SNatalie.Li@Sun.COM 					val = pw->pw_uid;
11817961SNatalie.Li@Sun.COM 				} else {
11827961SNatalie.Li@Sun.COM 					val = UID_NOBODY;
11837961SNatalie.Li@Sun.COM 				}
11847961SNatalie.Li@Sun.COM 				endpwent();
11857961SNatalie.Li@Sun.COM 			}
11867961SNatalie.Li@Sun.COM 			sp->s_rootid = val;
11877961SNatalie.Li@Sun.COM 			break;
11884345Sdougm 		default:
11894345Sdougm 			break;
11903910Sdougm 		}
11914345Sdougm 		if (name != NULL)
11924345Sdougm 			sa_free_attr_string(name);
11934345Sdougm 		if (value != NULL)
11944345Sdougm 			sa_free_attr_string(value);
11953910Sdougm 	}
11963910Sdougm 	/* if rw/ro options not set, use default of RW */
11973910Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
11984345Sdougm 		sp->s_flags |= M_RW;
11993910Sdougm 	return (err);
12003910Sdougm }
12013910Sdougm 
12023910Sdougm /*
12033910Sdougm  * This is for testing only
12043910Sdougm  * It displays the export structure that
12053910Sdougm  * goes into the kernel.
12063910Sdougm  */
12073910Sdougm static void
12083910Sdougm printarg(char *path, struct exportdata *ep)
12093910Sdougm {
12103910Sdougm 	int i, j;
12113910Sdougm 	struct secinfo *sp;
12123910Sdougm 
12133910Sdougm 	if (debug == 0)
12144345Sdougm 		return;
12153910Sdougm 
12163910Sdougm 	(void) printf("%s:\n", path);
12173910Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
12183910Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
12193910Sdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
12203910Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
12213910Sdougm 	if (ep->ex_flags & EX_NOSUID)
12223910Sdougm 		(void) printf("NOSUID ");
12233910Sdougm 	if (ep->ex_flags & EX_ACLOK)
12243910Sdougm 		(void) printf("ACLOK ");
12253910Sdougm 	if (ep->ex_flags & EX_PUBLIC)
12263910Sdougm 		(void) printf("PUBLIC ");
12273910Sdougm 	if (ep->ex_flags & EX_NOSUB)
12283910Sdougm 		(void) printf("NOSUB ");
12293910Sdougm 	if (ep->ex_flags & EX_LOG)
12303910Sdougm 		(void) printf("LOG ");
12317961SNatalie.Li@Sun.COM 	if (ep->ex_flags & EX_CHARMAP)
12327961SNatalie.Li@Sun.COM 		(void) printf("CHARMAP ");
12333910Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
12343910Sdougm 		(void) printf("LOG_ALLOPS ");
12353910Sdougm 	if (ep->ex_flags == 0)
12363910Sdougm 		(void) printf("(none)");
12373910Sdougm 	(void) 	printf("\n");
12383910Sdougm 	if (ep->ex_flags & EX_LOG) {
12393910Sdougm 		(void) printf("\tex_log_buffer = %s\n",
12404345Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
12413910Sdougm 		(void) printf("\tex_tag = %s\n",
12424345Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
12433910Sdougm 	}
12443910Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
12453910Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
12463910Sdougm 	(void) printf("\n");
12473910Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
12483910Sdougm 		sp = &ep->ex_secinfo[i];
12493910Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
12503910Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
12513910Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
12523910Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
12533910Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
12543910Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
12553910Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
12567961SNatalie.Li@Sun.COM 		if (sp->s_flags & M_NONE) (void) printf("M_NONE ");
12573910Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
12583910Sdougm 		(void) printf("\n");
12593910Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
12607961SNatalie.Li@Sun.COM 		(void) printf("\t\ts_rootid = %d\n", sp->s_rootid);
12613910Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
12623910Sdougm 		(void) fflush(stdout);
12633910Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
12643910Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
12654345Sdougm 			    sp->s_rootnames[j] : "<null>");
12663910Sdougm 		(void) printf("\n\n");
12673910Sdougm 	}
12683910Sdougm }
12693910Sdougm 
12703910Sdougm /*
12713910Sdougm  * count_security(opts)
12723910Sdougm  *
12733910Sdougm  * Count the number of security types (flavors). The optionset has
12743910Sdougm  * been populated with the security flavors as a holding mechanism.
12753910Sdougm  * We later use this number to allocate data structures.
12763910Sdougm  */
12773910Sdougm 
12783910Sdougm static int
12793910Sdougm count_security(sa_optionset_t opts)
12803910Sdougm {
12813910Sdougm 	int count = 0;
12823910Sdougm 	sa_property_t prop;
12833910Sdougm 	if (opts != NULL) {
12844345Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
12854345Sdougm 		    prop = sa_get_next_property(prop)) {
12864345Sdougm 			count++;
12874345Sdougm 		}
12883910Sdougm 	}
12893910Sdougm 	return (count);
12903910Sdougm }
12913910Sdougm 
12923910Sdougm /*
12933910Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
12943910Sdougm  *
12953910Sdougm  * provides a mechanism to format NFS properties into legacy output
12963910Sdougm  * format. If the buffer would overflow, it is reallocated and grown
12973910Sdougm  * as appropriate. Special cases of converting internal form of values
12983910Sdougm  * to those used by "share" are done. this function does one property
12993910Sdougm  * at a time.
13003910Sdougm  */
13013910Sdougm 
13025179Sdougm static int
13033910Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
13043910Sdougm 			sa_property_t prop, int sep)
13053910Sdougm {
13063910Sdougm 	char *name;
13073910Sdougm 	char *value;
13083910Sdougm 	int curlen;
13093910Sdougm 	char *buff = *rbuff;
13103910Sdougm 	size_t buffsize = *rbuffsize;
13115179Sdougm 	int printed = B_FALSE;
13123910Sdougm 
13133910Sdougm 	name = sa_get_property_attr(prop, "type");
13143910Sdougm 	value = sa_get_property_attr(prop, "value");
13153910Sdougm 	if (buff != NULL)
13164345Sdougm 		curlen = strlen(buff);
13173910Sdougm 	else
13184345Sdougm 		curlen = 0;
13193910Sdougm 	if (name != NULL) {
13204345Sdougm 		int len;
13214345Sdougm 		len = strlen(name) + sep;
13223910Sdougm 
13233910Sdougm 		/*
13243910Sdougm 		 * A future RFE would be to replace this with more
13253910Sdougm 		 * generic code and to possibly handle more types.
13263910Sdougm 		 */
13274345Sdougm 		switch (gettype(name)) {
13284345Sdougm 		case OPT_TYPE_BOOLEAN:
13295179Sdougm 			/*
13305179Sdougm 			 * For NFS, boolean value of FALSE means it
13315179Sdougm 			 * doesn't show up in the option list at all.
13325179Sdougm 			 */
13334345Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
13345179Sdougm 				goto skip;
13355179Sdougm 			if (value != NULL) {
13364345Sdougm 				sa_free_attr_string(value);
13375179Sdougm 				value = NULL;
13385179Sdougm 			}
13394345Sdougm 			break;
13404345Sdougm 		case OPT_TYPE_ACCLIST:
13414345Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
13424345Sdougm 				sa_free_attr_string(value);
13434345Sdougm 				value = NULL;
13444345Sdougm 			} else {
13454345Sdougm 				if (value != NULL)
13464345Sdougm 					len += 1 + strlen(value);
13474345Sdougm 			}
13484345Sdougm 			break;
13494345Sdougm 		case OPT_TYPE_LOGTAG:
13504345Sdougm 			if (value != NULL && strlen(value) == 0) {
13514345Sdougm 				sa_free_attr_string(value);
13524345Sdougm 				value = NULL;
13534345Sdougm 			} else {
13544345Sdougm 				if (value != NULL)
13554345Sdougm 					len += 1 + strlen(value);
13564345Sdougm 			}
13574345Sdougm 			break;
13584345Sdougm 		default:
13594345Sdougm 			if (value != NULL)
13604345Sdougm 				len += 1 + strlen(value);
13614345Sdougm 			break;
13623910Sdougm 		}
13634345Sdougm 		while (buffsize <= (curlen + len)) {
13644345Sdougm 			/* need more room */
13654345Sdougm 			buffsize += incr;
13664345Sdougm 			buff = realloc(buff, buffsize);
13674345Sdougm 			if (buff == NULL) {
13684345Sdougm 				/* realloc failed so free everything */
13694345Sdougm 				if (*rbuff != NULL)
13704345Sdougm 					free(*rbuff);
13714345Sdougm 			}
13724345Sdougm 			*rbuff = buff;
13734345Sdougm 			*rbuffsize = buffsize;
13745179Sdougm 			if (buff == NULL)
13755179Sdougm 				goto skip;
13765179Sdougm 
13773910Sdougm 		}
13785179Sdougm 
13794345Sdougm 		if (buff == NULL)
13805179Sdougm 			goto skip;
13815179Sdougm 
13824345Sdougm 		if (value == NULL) {
13834345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
13844345Sdougm 			    "%s%s", sep ? "," : "",
13854345Sdougm 			    name, value != NULL ? value : "");
13864345Sdougm 		} else {
13874345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
13884345Sdougm 			    "%s%s=%s", sep ? "," : "",
13894345Sdougm 			    name, value != NULL ? value : "");
13903910Sdougm 		}
13915179Sdougm 		printed = B_TRUE;
13923910Sdougm 	}
13935179Sdougm skip:
13943910Sdougm 	if (name != NULL)
13954345Sdougm 		sa_free_attr_string(name);
13963910Sdougm 	if (value != NULL)
13974345Sdougm 		sa_free_attr_string(value);
13985179Sdougm 	return (printed);
13993910Sdougm }
14003910Sdougm 
14013910Sdougm /*
14023910Sdougm  * nfs_format_options(group, hier)
14033910Sdougm  *
14043910Sdougm  * format all the options on the group into an old-style option
14053910Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
14063910Sdougm  * options.
14073910Sdougm  */
14083910Sdougm 
14093910Sdougm static char *
14103910Sdougm nfs_format_options(sa_group_t group, int hier)
14113910Sdougm {
14123910Sdougm 	sa_optionset_t options = NULL;
14134345Sdougm 	sa_optionset_t secoptions = NULL;
14143910Sdougm 	sa_property_t prop, secprop;
14154345Sdougm 	sa_security_t security = NULL;
14163910Sdougm 	char *buff;
14173910Sdougm 	size_t buffsize;
14184345Sdougm 	char *sectype = NULL;
14194345Sdougm 	int sep = 0;
14204345Sdougm 
14214345Sdougm 
14224345Sdougm 	buff = malloc(OPT_CHUNK);
14234345Sdougm 	if (buff == NULL) {
14244345Sdougm 		return (NULL);
14254345Sdougm 	}
14264345Sdougm 
14274345Sdougm 	buff[0] = '\0';
14284345Sdougm 	buffsize = OPT_CHUNK;
14294345Sdougm 
14304345Sdougm 	/*
14314345Sdougm 	 * We may have a an optionset relative to this item. format
14324345Sdougm 	 * these if we find them and then add any security definitions.
14334345Sdougm 	 */
14343910Sdougm 
14353910Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
14363910Sdougm 
14373910Sdougm 	/*
14384345Sdougm 	 * do the default set first but skip any option that is also
14394345Sdougm 	 * in the protocol specific optionset.
14403910Sdougm 	 */
14414345Sdougm 	if (options != NULL) {
14424345Sdougm 		for (prop = sa_get_property(options, NULL);
14434345Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
14443910Sdougm 			/*
14454345Sdougm 			 * use this one since we skipped any
14464345Sdougm 			 * of these that were also in
14474345Sdougm 			 * optdefault
14483910Sdougm 			 */
14495179Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
14505179Sdougm 			    prop, sep))
14515179Sdougm 				sep = 1;
14524345Sdougm 			if (buff == NULL) {
14534345Sdougm 				/*
14544345Sdougm 				 * buff could become NULL if there
14554345Sdougm 				 * isn't enough memory for
14564345Sdougm 				 * nfs_sprint_option to realloc()
14574345Sdougm 				 * as necessary. We can't really
14584345Sdougm 				 * do anything about it at this
14594345Sdougm 				 * point so we return NULL.  The
14604345Sdougm 				 * caller should handle the
14614345Sdougm 				 * failure.
14624345Sdougm 				 */
14634345Sdougm 				if (options != NULL)
14644345Sdougm 					sa_free_derived_optionset(
14654345Sdougm 					    options);
14664345Sdougm 				return (buff);
14674345Sdougm 			}
14683910Sdougm 		}
14694345Sdougm 	}
14704345Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
14714345Sdougm 	    "nfs", hier);
14724345Sdougm 	if (secoptions != NULL) {
14733910Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
14744345Sdougm 		    secprop != NULL;
14754345Sdougm 		    secprop = sa_get_next_property(secprop)) {
14764345Sdougm 			sectype = sa_get_property_attr(secprop, "type");
14774345Sdougm 			security =
14784345Sdougm 			    (sa_security_t)sa_get_derived_security(
14794345Sdougm 			    group, sectype, "nfs", hier);
14804345Sdougm 			if (security != NULL) {
14814345Sdougm 				if (sectype != NULL) {
14824345Sdougm 					prop = sa_create_property(
14834345Sdougm 					    "sec", sectype);
14845179Sdougm 					if (prop == NULL)
14855179Sdougm 						goto err;
14865179Sdougm 					if (nfs_sprint_option(&buff,
14875179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
14885179Sdougm 						sep = 1;
14894345Sdougm 					(void) sa_remove_property(prop);
14905179Sdougm 					if (buff == NULL)
14915179Sdougm 						goto err;
14924345Sdougm 				}
14934345Sdougm 				for (prop = sa_get_property(security,
14944345Sdougm 				    NULL); prop != NULL;
14954345Sdougm 				    prop = sa_get_next_property(prop)) {
14965179Sdougm 					if (nfs_sprint_option(&buff,
14975179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
14985179Sdougm 						sep = 1;
14994345Sdougm 					if (buff == NULL)
15004345Sdougm 						goto err;
15014345Sdougm 				}
15024345Sdougm 				sa_free_derived_optionset(security);
15033910Sdougm 			}
15044345Sdougm 			if (sectype != NULL)
15054345Sdougm 				sa_free_attr_string(sectype);
15063910Sdougm 		}
15073910Sdougm 		sa_free_derived_optionset(secoptions);
15083910Sdougm 	}
15094345Sdougm 
15103910Sdougm 	if (options != NULL)
15114345Sdougm 		sa_free_derived_optionset(options);
15124345Sdougm 	return (buff);
15134345Sdougm 
15144345Sdougm err:
15154345Sdougm 	/*
15164345Sdougm 	 * If we couldn't allocate memory for option printing, we need
15174345Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
15184345Sdougm 	 */
15194345Sdougm 	if (secoptions != NULL)
15204345Sdougm 		sa_free_derived_optionset(secoptions);
15214345Sdougm 	if (security != NULL)
15224345Sdougm 		sa_free_derived_optionset(security);
15234345Sdougm 	if (sectype != NULL)
15244345Sdougm 		sa_free_attr_string(sectype);
15254345Sdougm 	if (options != NULL)
15264345Sdougm 		sa_free_derived_optionset(options);
15278334SJose.Borrego@Sun.COM 	return (NULL);
15283910Sdougm }
15294345Sdougm 
15303910Sdougm /*
15313910Sdougm  * Append an entry to the nfslogtab file
15323910Sdougm  */
15333910Sdougm static int
15343910Sdougm nfslogtab_add(dir, buffer, tag)
15353910Sdougm 	char *dir, *buffer, *tag;
15363910Sdougm {
15373910Sdougm 	FILE *f;
15383910Sdougm 	struct logtab_ent lep;
15393910Sdougm 	int error = 0;
15403910Sdougm 
15413910Sdougm 	/*
15423910Sdougm 	 * Open the file for update and create it if necessary.
15433910Sdougm 	 * This may leave the I/O offset at the end of the file,
15443910Sdougm 	 * so rewind back to the beginning of the file.
15453910Sdougm 	 */
15463910Sdougm 	f = fopen(NFSLOGTAB, "a+");
15473910Sdougm 	if (f == NULL) {
15483910Sdougm 		error = errno;
15493910Sdougm 		goto out;
15503910Sdougm 	}
15513910Sdougm 	rewind(f);
15523910Sdougm 
15533910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
15543910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15554345Sdougm 		    "share complete, however failed to lock %s "
15564345Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
15573910Sdougm 		error = -1;
15583910Sdougm 		goto out;
15593910Sdougm 	}
15603910Sdougm 
15613910Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
15623910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15634345Sdougm 		    "share complete, however could not deactivate "
15644345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15653910Sdougm 		error = -1;
15663910Sdougm 		goto out;
15673910Sdougm 	}
15683910Sdougm 
15693910Sdougm 	/*
15703910Sdougm 	 * Remove entries matching buffer and sharepoint since we're
15713910Sdougm 	 * going to replace it with perhaps an entry with a new tag.
15723910Sdougm 	 */
15733910Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
15743910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15754345Sdougm 		    "share complete, however could not remove matching "
15764345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15773910Sdougm 		error = -1;
15783910Sdougm 		goto out;
15793910Sdougm 	}
15803910Sdougm 
15813910Sdougm 	/*
15823910Sdougm 	 * Deactivate all active entries matching this sharepoint
15833910Sdougm 	 */
15843910Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
15853910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15864345Sdougm 		    "share complete, however could not deactivate matching "
15874345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15883910Sdougm 		error = -1;
15893910Sdougm 		goto out;
15903910Sdougm 	}
15913910Sdougm 
15923910Sdougm 	lep.le_buffer = buffer;
15933910Sdougm 	lep.le_path = dir;
15943910Sdougm 	lep.le_tag = tag;
15953910Sdougm 	lep.le_state = LES_ACTIVE;
15963910Sdougm 
15973910Sdougm 	/*
15983910Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
15993910Sdougm 	 */
16003910Sdougm 	if (logtab_putent(f, &lep) < 0) {
16013910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
16024345Sdougm 		    "share complete, however could not add %s to %s\n"),
16034345Sdougm 		    dir, NFSLOGTAB);
16043910Sdougm 		error = -1;
16053910Sdougm 	}
16063910Sdougm 
16073910Sdougm out:
16083910Sdougm 	if (f != NULL)
16093910Sdougm 		(void) fclose(f);
16103910Sdougm 	return (error);
16113910Sdougm }
16123910Sdougm 
16133910Sdougm /*
16143910Sdougm  * Deactivate an entry from the nfslogtab file
16153910Sdougm  */
16163910Sdougm static int
16173910Sdougm nfslogtab_deactivate(path)
16183910Sdougm 	char *path;
16193910Sdougm {
16203910Sdougm 	FILE *f;
16213910Sdougm 	int error = 0;
16223910Sdougm 
16233910Sdougm 	f = fopen(NFSLOGTAB, "r+");
16243910Sdougm 	if (f == NULL) {
16253910Sdougm 		error = errno;
16263910Sdougm 		goto out;
16273910Sdougm 	}
16283910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
16293910Sdougm 		error = errno;
16303910Sdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
16314345Sdougm 		    "share complete, however could not lock %s for "
16324345Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
16333910Sdougm 		goto out;
16343910Sdougm 	}
16353910Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
16363910Sdougm 		error = -1;
16373910Sdougm 		(void) fprintf(stderr,
16384345Sdougm 		    dgettext(TEXT_DOMAIN,
16394345Sdougm 		    "share complete, however could not "
16404345Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
16413910Sdougm 		goto out;
16423910Sdougm 	}
16433910Sdougm 
16443910Sdougm out:	if (f != NULL)
16453910Sdougm 		(void) fclose(f);
16463910Sdougm 
16473910Sdougm 	return (error);
16483910Sdougm }
16493910Sdougm 
16503910Sdougm /*
16514524Sdougm  * check_public(group, skipshare)
16524524Sdougm  *
16534524Sdougm  * Check the group for any shares that have the public property
16544524Sdougm  * enabled. We skip "skipshare" since that is the one we are
16554524Sdougm  * working with. This is a separate function to make handling
16564524Sdougm  * subgroups simpler. Returns true if there is a share with public.
16574524Sdougm  */
16584524Sdougm static int
16594524Sdougm check_public(sa_group_t group, sa_share_t skipshare)
16604524Sdougm {
16614524Sdougm 	int exists = B_FALSE;
16624524Sdougm 	sa_share_t share;
16634524Sdougm 	sa_optionset_t opt;
16644524Sdougm 	sa_property_t prop;
16654524Sdougm 	char *shared;
16664524Sdougm 
16674524Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
16684524Sdougm 	    share = sa_get_next_share(share)) {
16694524Sdougm 		if (share == skipshare)
16704524Sdougm 			continue;
16714524Sdougm 
16724524Sdougm 		opt = sa_get_optionset(share, "nfs");
16734524Sdougm 		if (opt == NULL)
16744524Sdougm 			continue;
16754524Sdougm 		prop = sa_get_property(opt, "public");
16764524Sdougm 		if (prop == NULL)
16774524Sdougm 			continue;
16784524Sdougm 		shared = sa_get_share_attr(share, "shared");
16794524Sdougm 		if (shared != NULL) {
16804524Sdougm 			exists = strcmp(shared, "true") == 0;
16814524Sdougm 			sa_free_attr_string(shared);
16824524Sdougm 			if (exists == B_TRUE)
16834524Sdougm 				break;
16844524Sdougm 		}
16854524Sdougm 	}
16864524Sdougm 
16874524Sdougm 	return (exists);
16884524Sdougm }
16894524Sdougm 
16904524Sdougm /*
16916214Sdougm  * public_exists(handle, share)
16923910Sdougm  *
16933910Sdougm  * check to see if public option is set on any other share than the
16944524Sdougm  * one specified. Need to check zfs sub-groups as well as the top
16954524Sdougm  * level groups.
16963910Sdougm  */
16973910Sdougm static int
16986214Sdougm public_exists(sa_handle_t handle, sa_share_t skipshare)
16993910Sdougm {
17006214Sdougm 	sa_group_t group = NULL;
17013910Sdougm 
17026271Sdougm 	/*
17036271Sdougm 	 * If we don't have a handle, we can only do syntax check. We
17046271Sdougm 	 * can't check against other shares so we assume OK and will
17056271Sdougm 	 * catch the problem only when we actually try to apply it.
17066271Sdougm 	 */
17073910Sdougm 	if (handle == NULL)
17086271Sdougm 		return (SA_OK);
17093910Sdougm 
17106214Sdougm 	if (skipshare != NULL) {
17116214Sdougm 		group = sa_get_parent_group(skipshare);
17126214Sdougm 		if (group == NULL)
17136214Sdougm 			return (SA_NO_SUCH_GROUP);
17146214Sdougm 	}
17156214Sdougm 
17163910Sdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
17173910Sdougm 	    group = sa_get_next_group(group)) {
17184524Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
17194524Sdougm 		if (sa_group_is_zfs(group)) {
17204524Sdougm 			sa_group_t subgroup;
17214524Sdougm 			for (subgroup = sa_get_sub_group(group);
17224524Sdougm 			    subgroup != NULL;
17234524Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
17244524Sdougm 				if (check_public(subgroup, skipshare))
17254524Sdougm 					return (B_TRUE);
17263910Sdougm 			}
17274524Sdougm 		} else {
17284524Sdougm 			if (check_public(group, skipshare))
17294524Sdougm 				return (B_TRUE);
17303910Sdougm 		}
17313910Sdougm 	}
17324524Sdougm 	return (B_FALSE);
17333910Sdougm }
17343910Sdougm 
17353910Sdougm /*
17363910Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
17373910Sdougm  * implementation that it is to enable the share. This entails
17383910Sdougm  * converting the path and options into the appropriate ioctl
17393910Sdougm  * calls. It is assumed that all error checking of paths, etc. were
17403910Sdougm  * done earlier.
17413910Sdougm  */
17423910Sdougm static int
17433910Sdougm nfs_enable_share(sa_share_t share)
17443910Sdougm {
17453910Sdougm 	struct exportdata export;
17463910Sdougm 	sa_optionset_t secoptlist;
17473910Sdougm 	struct secinfo *sp;
17483910Sdougm 	int num_secinfo;
17493910Sdougm 	sa_optionset_t opt;
17503910Sdougm 	sa_security_t sec;
17513910Sdougm 	sa_property_t prop;
17523910Sdougm 	char *path;
17533910Sdougm 	int err = SA_OK;
17544524Sdougm 	int i;
17554543Smarks 	int iszfs;
17566214Sdougm 	sa_handle_t handle;
17573910Sdougm 
17583910Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
17593910Sdougm 	(void) signal(SIGSYS, SIG_IGN);
17603910Sdougm 
17613910Sdougm 	/* get the path since it is important in several places */
17623910Sdougm 	path = sa_get_share_attr(share, "path");
17633910Sdougm 	if (path == NULL)
17644345Sdougm 		return (SA_NO_SUCH_PATH);
17653910Sdougm 
17664543Smarks 	iszfs = sa_path_is_zfs(path);
17673910Sdougm 	/*
17683910Sdougm 	 * find the optionsets and security sets.  There may not be
17693910Sdougm 	 * any or there could be one or two for each of optionset and
17703910Sdougm 	 * security may have multiple, one per security type per
17713910Sdougm 	 * protocol type.
17723910Sdougm 	 */
17733910Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
17743910Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
17753910Sdougm 	if (secoptlist != NULL)
17764345Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
17773910Sdougm 	else
17784345Sdougm 		num_secinfo = 1;
17793910Sdougm 
17803910Sdougm 	/*
17813910Sdougm 	 * walk through the options and fill in the structure
17823910Sdougm 	 * appropriately.
17833910Sdougm 	 */
17843910Sdougm 
17853910Sdougm 	(void) memset(&export, '\0', sizeof (export));
17863910Sdougm 
17873910Sdougm 	/*
17883910Sdougm 	 * do non-security options first since there is only one after
17893910Sdougm 	 * the derived group is constructed.
17903910Sdougm 	 */
17913910Sdougm 	export.ex_version = EX_CURRENT_VERSION;
17923910Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
17933910Sdougm 	export.ex_index = NULL;
17943910Sdougm 	export.ex_path = path;
17953910Sdougm 	export.ex_pathlen = strlen(path) + 1;
17963910Sdougm 
17973910Sdougm 	if (opt != NULL)
17984345Sdougm 		err = fill_export_from_optionset(&export, opt);
17993910Sdougm 
18003910Sdougm 	/*
18013910Sdougm 	 * check to see if "public" is set. If it is, then make sure
18023910Sdougm 	 * no other share has it set. If it is already used, fail.
18033910Sdougm 	 */
18043910Sdougm 
18056214Sdougm 	handle = sa_find_group_handle((sa_group_t)share);
18066214Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
18074345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18084345Sdougm 		    "NFS: Cannot share more than one file "
18094345Sdougm 		    "system with 'public' property\n"));
18104345Sdougm 		err = SA_NOT_ALLOWED;
18114345Sdougm 		goto out;
18123910Sdougm 	}
18133910Sdougm 
18144524Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
18153910Sdougm 	if (sp == NULL) {
18164345Sdougm 		err = SA_NO_MEMORY;
18174524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18184524Sdougm 		    "NFS: NFS: no memory for security\n"));
18194524Sdougm 		goto out;
18204524Sdougm 	}
18214524Sdougm 	export.ex_secinfo = sp;
18224524Sdougm 	/* get default secinfo */
18234524Sdougm 	export.ex_seccnt = num_secinfo;
18244524Sdougm 	/*
18254524Sdougm 	 * since we must have one security option defined, we
18264524Sdougm 	 * init to the default and then override as we find
18274524Sdougm 	 * defined security options. This handles the case
18284524Sdougm 	 * where we have no defined options but we need to set
18294524Sdougm 	 * up one.
18304524Sdougm 	 */
18314524Sdougm 	sp[0].s_window = DEF_WIN;
18324524Sdougm 	sp[0].s_rootnames = NULL;
18334524Sdougm 	/* setup a default in case no properties defined */
18344524Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
18354524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18364524Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
18374524Sdougm 		    "get default security mode\n"));
18384524Sdougm 		err = SA_CONFIG_ERR;
18394524Sdougm 	}
18404524Sdougm 	if (secoptlist != NULL) {
18414524Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
18424524Sdougm 		    prop != NULL && i < num_secinfo;
18434524Sdougm 		    prop = sa_get_next_property(prop), i++) {
18444524Sdougm 			char *sectype;
1845*11337SWilliam.Krier@Sun.COM 			sectype = sa_get_property_attr(prop, "type");
18464524Sdougm 			/*
18474524Sdougm 			 * if sectype is NULL, we probably
18484524Sdougm 			 * have a memory problem and can't get
18494524Sdougm 			 * the correct values. Rather than
18504524Sdougm 			 * exporting with incorrect security,
18514524Sdougm 			 * don't share it.
18524524Sdougm 			 */
18534524Sdougm 			if (sectype == NULL) {
18544524Sdougm 				err = SA_NO_MEMORY;
18554524Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
18564524Sdougm 				    "NFS: Cannot share %s: "
18574524Sdougm 				    "no memory\n"), path);
18584524Sdougm 				goto out;
18594524Sdougm 			}
18604524Sdougm 			sec = (sa_security_t)sa_get_derived_security(
18614524Sdougm 			    share, sectype, "nfs", 1);
18624524Sdougm 			sp[i].s_window = DEF_WIN;
18634524Sdougm 			sp[i].s_rootcnt = 0;
18644524Sdougm 			sp[i].s_rootnames = NULL;
18654345Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
18664524Sdougm 			if (sec != NULL)
18674524Sdougm 				sa_free_derived_security(sec);
18684524Sdougm 			if (sectype != NULL)
18694524Sdougm 				sa_free_attr_string(sectype);
18703910Sdougm 		}
18714524Sdougm 	}
18724524Sdougm 	/*
18734524Sdougm 	 * when we get here, we can do the exportfs system call and
18744524Sdougm 	 * initiate thinsg. We probably want to enable the nfs.server
18754524Sdougm 	 * service first if it isn't running within SMF.
18764524Sdougm 	 */
18774524Sdougm 	/* check nfs.server status and start if needed */
18784524Sdougm 	/* now add the share to the internal tables */
18794524Sdougm 	printarg(path, &export);
18804524Sdougm 	/*
18814524Sdougm 	 * call the exportfs system call which is implemented
18824524Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
18834524Sdougm 	 */
18844543Smarks 	if (iszfs) {
18854543Smarks 		struct exportfs_args ea;
18864543Smarks 		share_t sh;
18874543Smarks 		char *str;
18884543Smarks 		priv_set_t *priv_effective;
18894543Smarks 		int privileged;
18904543Smarks 
18914543Smarks 		/*
18924543Smarks 		 * If we aren't a privileged user
18934543Smarks 		 * and NFS server service isn't running
18944543Smarks 		 * then print out an error message
18954543Smarks 		 * and return EPERM
18964543Smarks 		 */
18974543Smarks 
18984543Smarks 		priv_effective = priv_allocset();
18994543Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
19004543Smarks 
19014543Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
19024543Smarks 		priv_freeset(priv_effective);
19034543Smarks 
19044543Smarks 		if (!privileged &&
19054543Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
19064543Smarks 			err = 0;
19074543Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
19084543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
19094543Smarks 				    "NFS: Cannot share remote "
19104543Smarks 				    "filesystem: %s\n"), path);
19114543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
19124543Smarks 				    "NFS: Service needs to be enabled "
19134543Smarks 				    "by a privileged user\n"));
19144543Smarks 				err = SA_SYSTEM_ERR;
19154543Smarks 				errno = EPERM;
19164543Smarks 			}
19174543Smarks 			free(str);
19184543Smarks 		}
19194543Smarks 
19204543Smarks 		if (err == 0) {
19214543Smarks 			ea.dname = path;
19224543Smarks 			ea.uex = &export;
19234543Smarks 
19244543Smarks 			sa_sharetab_fill_zfs(share, &sh, "nfs");
19258845Samw@Sun.COM 			err = sa_share_zfs(share, NULL, path, &sh,
19265331Samw 			    &ea, ZFS_SHARE_NFS);
192710761SWilliam.Krier@Sun.COM 			if (err != SA_OK) {
192810761SWilliam.Krier@Sun.COM 				errno = err;
192910761SWilliam.Krier@Sun.COM 				err = -1;
193010761SWilliam.Krier@Sun.COM 			}
19314543Smarks 			sa_emptyshare(&sh);
19324543Smarks 		}
19334543Smarks 	} else {
19344543Smarks 		err = exportfs(path, &export);
19354543Smarks 	}
19364543Smarks 
19374543Smarks 	if (err < 0) {
19384524Sdougm 		err = SA_SYSTEM_ERR;
19394524Sdougm 		switch (errno) {
19404524Sdougm 		case EREMOTE:
19414524Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
19424543Smarks 			    "NFS: Cannot share filesystems "
19434543Smarks 			    "in non-global zones: %s\n"), path);
19444543Smarks 			err = SA_NOT_SUPPORTED;
19454524Sdougm 			break;
19464524Sdougm 		case EPERM:
19474524Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
19484345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
19494543Smarks 				    "NFS: Cannot share file systems "
19504524Sdougm 				    "in non-global zones: %s\n"), path);
19514524Sdougm 				err = SA_NOT_SUPPORTED;
19524345Sdougm 				break;
19534345Sdougm 			}
19544524Sdougm 			err = SA_NO_PERMISSION;
19554524Sdougm 			/* FALLTHROUGH */
19564524Sdougm 		default:
19574524Sdougm 			break;
19583910Sdougm 		}
19594524Sdougm 	} else {
19604524Sdougm 		/* update sharetab with an add/modify */
19614543Smarks 		if (!iszfs) {
19624543Smarks 			(void) sa_update_sharetab(share, "nfs");
19634543Smarks 		}
19643910Sdougm 	}
19653910Sdougm 
19663910Sdougm 	if (err == SA_OK) {
19673910Sdougm 		/*
19683910Sdougm 		 * enable services as needed. This should probably be
19693910Sdougm 		 * done elsewhere in order to minimize the calls to
19703910Sdougm 		 * check services.
19713910Sdougm 		 */
19723910Sdougm 		/*
19733910Sdougm 		 * check to see if logging and other services need to
19743910Sdougm 		 * be triggered, but only if there wasn't an
19753910Sdougm 		 * error. This is probably where sharetab should be
19763910Sdougm 		 * updated with the NFS specific entry.
19773910Sdougm 		 */
19784345Sdougm 		if (export.ex_flags & EX_LOG) {
19794345Sdougm 			/* enable logging */
19804345Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
19814345Sdougm 			    export.ex_tag) != 0) {
19824345Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
19834345Sdougm 				    "Could not enable logging for %s\n"),
19844345Sdougm 				    path);
19854345Sdougm 			}
19864345Sdougm 			_check_services(service_list_logging);
19874345Sdougm 		} else {
19884345Sdougm 			/*
19894345Sdougm 			 * don't have logging so remove it from file. It might
19904345Sdougm 			 * not be thre, but that doesn't matter.
19914345Sdougm 			 */
19924345Sdougm 			(void) nfslogtab_deactivate(path);
19934345Sdougm 			_check_services(service_list_default);
19943910Sdougm 		}
19953910Sdougm 	}
19963910Sdougm 
19973910Sdougm out:
19983910Sdougm 	if (path != NULL)
19994345Sdougm 		free(path);
20003910Sdougm 
20013910Sdougm 	cleanup_export(&export);
20023910Sdougm 	if (opt != NULL)
20034345Sdougm 		sa_free_derived_optionset(opt);
20043910Sdougm 	if (secoptlist != NULL)
20054345Sdougm 		(void) sa_destroy_optionset(secoptlist);
20063910Sdougm 	return (err);
20073910Sdougm }
20083910Sdougm 
20093910Sdougm /*
20105800Sdougm  * nfs_disable_share(share, path)
20113910Sdougm  *
20125800Sdougm  * Unshare the specified share. Note that "path" is the same path as
20135800Sdougm  * what is in the "share" object. It is passed in to avoid an
20145800Sdougm  * additional lookup. A missing "path" value makes this a no-op
20155800Sdougm  * function.
20163910Sdougm  */
20173910Sdougm static int
20184543Smarks nfs_disable_share(sa_share_t share, char *path)
20193910Sdougm {
20203910Sdougm 	int err;
20213910Sdougm 	int ret = SA_OK;
20224543Smarks 	int iszfs;
20235800Sdougm 	sa_group_t parent;
20245951Sdougm 	sa_handle_t handle;
20254543Smarks 
20265800Sdougm 	if (path == NULL)
20275800Sdougm 		return (ret);
20284543Smarks 
20295800Sdougm 	/*
20305800Sdougm 	 * If the share is in a ZFS group we need to handle it
20315800Sdougm 	 * differently.  Just being on a ZFS file system isn't
20325800Sdougm 	 * enough since we may be in a legacy share case.
20335800Sdougm 	 */
20345800Sdougm 	parent = sa_get_parent_group(share);
20355800Sdougm 	iszfs = sa_group_is_zfs(parent);
20365800Sdougm 	if (iszfs) {
20375800Sdougm 		struct exportfs_args ea;
20385800Sdougm 		share_t sh = { 0 };
20395800Sdougm 		ea.dname = path;
20405800Sdougm 		ea.uex = NULL;
20415800Sdougm 		sh.sh_path = path;
20425800Sdougm 		sh.sh_fstype = "nfs";
20434543Smarks 
20448845Samw@Sun.COM 		err = sa_share_zfs(share, NULL, path, &sh,
20455800Sdougm 		    &ea, ZFS_UNSHARE_NFS);
204610761SWilliam.Krier@Sun.COM 		if (err != SA_OK) {
204710761SWilliam.Krier@Sun.COM 			errno = err;
204810761SWilliam.Krier@Sun.COM 			err = -1;
204910761SWilliam.Krier@Sun.COM 		}
20505800Sdougm 	} else {
20515800Sdougm 		err = exportfs(path, NULL);
20525800Sdougm 	}
20535800Sdougm 	if (err < 0) {
20545800Sdougm 		/*
20555800Sdougm 		 * TBD: only an error in some
20565800Sdougm 		 * cases - need better analysis
20575800Sdougm 		 */
20585800Sdougm 		switch (errno) {
20595800Sdougm 		case EPERM:
20605800Sdougm 		case EACCES:
20615800Sdougm 			ret = SA_NO_PERMISSION;
20625800Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
20635800Sdougm 				ret = SA_NOT_SUPPORTED;
20645800Sdougm 			}
20655800Sdougm 			break;
20665800Sdougm 		case EINVAL:
20675800Sdougm 		case ENOENT:
20685800Sdougm 			ret = SA_NO_SUCH_PATH;
20694543Smarks 			break;
207010761SWilliam.Krier@Sun.COM 		default:
207110761SWilliam.Krier@Sun.COM 			ret = SA_SYSTEM_ERR;
20724543Smarks 			break;
20733910Sdougm 		}
20745800Sdougm 	}
20755800Sdougm 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
20765951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
20775800Sdougm 		if (!iszfs)
20785951Sdougm 			(void) sa_delete_sharetab(handle, path, "nfs");
20795800Sdougm 		/* just in case it was logged */
20805800Sdougm 		(void) nfslogtab_deactivate(path);
20813910Sdougm 	}
20823910Sdougm 	return (ret);
20833910Sdougm }
20843910Sdougm 
20853910Sdougm /*
20867961SNatalie.Li@Sun.COM  * check_rorwnone(v1, v2, v3)
20877961SNatalie.Li@Sun.COM  *
20887961SNatalie.Li@Sun.COM  * check ro vs rw vs none values.  Over time this may get beefed up.
20897961SNatalie.Li@Sun.COM  * for now it just does simple checks. v1 is never NULL but v2 or v3
20907961SNatalie.Li@Sun.COM  * could be.
20913910Sdougm  */
20923910Sdougm 
20933910Sdougm static int
20947961SNatalie.Li@Sun.COM check_rorwnone(char *v1, char *v2, char *v3)
20953910Sdougm {
20963910Sdougm 	int ret = SA_OK;
20977961SNatalie.Li@Sun.COM 	if (v2 != NULL && strcmp(v1, v2) == 0)
20984345Sdougm 		ret = SA_VALUE_CONFLICT;
20997961SNatalie.Li@Sun.COM 	else if (v3 != NULL && strcmp(v1, v3) == 0)
21007961SNatalie.Li@Sun.COM 		ret = SA_VALUE_CONFLICT;
21017961SNatalie.Li@Sun.COM 
21023910Sdougm 	return (ret);
21033910Sdougm }
21043910Sdougm 
21053910Sdougm /*
21066214Sdougm  * nfs_validate_property(handle, property, parent)
21073910Sdougm  *
21083910Sdougm  * Check that the property has a legitimate value for its type.
21093910Sdougm  */
21103910Sdougm 
21113910Sdougm static int
21126214Sdougm nfs_validate_property(sa_handle_t handle, sa_property_t property,
21136214Sdougm     sa_optionset_t parent)
21143910Sdougm {
21153910Sdougm 	int ret = SA_OK;
21163910Sdougm 	char *propname;
21177961SNatalie.Li@Sun.COM 	char *other1;
21187961SNatalie.Li@Sun.COM 	char *other2;
21193910Sdougm 	int optindex;
21203910Sdougm 	nfsl_config_t *configlist;
21213910Sdougm 	sa_group_t parent_group;
21223910Sdougm 	char *value;
21233910Sdougm 
21243910Sdougm 	propname = sa_get_property_attr(property, "type");
21253910Sdougm 
21263910Sdougm 	if ((optindex = findopt(propname)) < 0)
21274345Sdougm 		ret = SA_NO_SUCH_PROP;
21283910Sdougm 
21293910Sdougm 	/* need to validate value range here as well */
21303910Sdougm 
21313910Sdougm 	if (ret == SA_OK) {
21324345Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
21336214Sdougm 		if (optdefs[optindex].share && parent_group != NULL &&
21346214Sdougm 		    !sa_is_share(parent_group))
21354345Sdougm 			ret = SA_PROP_SHARE_ONLY;
21363910Sdougm 	}
21373910Sdougm 	if (ret == SA_OK) {
21386214Sdougm 		if (optdefs[optindex].index == OPT_PUBLIC) {
21396214Sdougm 			/*
21406214Sdougm 			 * Public is special in that only one instance can
21416214Sdougm 			 * be in the repository at the same time.
21426214Sdougm 			 */
21436214Sdougm 			if (public_exists(handle, parent_group)) {
21447961SNatalie.Li@Sun.COM 				sa_free_attr_string(propname);
21456214Sdougm 				return (SA_VALUE_CONFLICT);
21466214Sdougm 			}
21476214Sdougm 		}
21484345Sdougm 		value = sa_get_property_attr(property, "value");
21494345Sdougm 		if (value != NULL) {
21504345Sdougm 			/* first basic type checking */
21514345Sdougm 			switch (optdefs[optindex].type) {
21524345Sdougm 			case OPT_TYPE_NUMBER:
21534345Sdougm 				/* check that the value is all digits */
21544345Sdougm 				if (!is_a_number(value))
21554345Sdougm 					ret = SA_BAD_VALUE;
21564345Sdougm 				break;
21574345Sdougm 			case OPT_TYPE_BOOLEAN:
21584345Sdougm 				if (strlen(value) == 0 ||
21594345Sdougm 				    strcasecmp(value, "true") == 0 ||
21604345Sdougm 				    strcmp(value, "1") == 0 ||
21614345Sdougm 				    strcasecmp(value, "false") == 0 ||
21624345Sdougm 				    strcmp(value, "0") == 0) {
21634345Sdougm 					ret = SA_OK;
21644345Sdougm 				} else {
21654345Sdougm 					ret = SA_BAD_VALUE;
21664345Sdougm 				}
21674345Sdougm 				break;
21684345Sdougm 			case OPT_TYPE_USER:
21694345Sdougm 				if (!is_a_number(value)) {
21704345Sdougm 					struct passwd *pw;
21714345Sdougm 					/*
21724345Sdougm 					 * in this case it would have to be a
21734345Sdougm 					 * user name
21744345Sdougm 					 */
21754345Sdougm 					pw = getpwnam(value);
21764345Sdougm 					if (pw == NULL)
21774345Sdougm 						ret = SA_BAD_VALUE;
21784345Sdougm 					endpwent();
21794345Sdougm 				} else {
21804345Sdougm 					uint64_t intval;
21814345Sdougm 					intval = strtoull(value, NULL, 0);
21824345Sdougm 					if (intval > UID_MAX && intval != ~0)
21834345Sdougm 						ret = SA_BAD_VALUE;
21844345Sdougm 				}
21854345Sdougm 				break;
21864345Sdougm 			case OPT_TYPE_FILE:
21874345Sdougm 				if (strcmp(value, "..") == 0 ||
21884345Sdougm 				    strchr(value, '/') != NULL) {
21894345Sdougm 					ret = SA_BAD_VALUE;
21904345Sdougm 				}
21914345Sdougm 				break;
21927961SNatalie.Li@Sun.COM 			case OPT_TYPE_ACCLIST: {
21937961SNatalie.Li@Sun.COM 				sa_property_t oprop1;
21947961SNatalie.Li@Sun.COM 				sa_property_t oprop2;
21957961SNatalie.Li@Sun.COM 				char *ovalue1 = NULL;
21967961SNatalie.Li@Sun.COM 				char *ovalue2 = NULL;
21977961SNatalie.Li@Sun.COM 
21987961SNatalie.Li@Sun.COM 				if (parent == NULL)
21997961SNatalie.Li@Sun.COM 					break;
22004345Sdougm 				/*
22014345Sdougm 				 * access list handling. Should eventually
22024345Sdougm 				 * validate that all the values make sense.
22034345Sdougm 				 * Also, ro and rw may have cross value
22044345Sdougm 				 * conflicts.
22054345Sdougm 				 */
22067961SNatalie.Li@Sun.COM 				if (strcmp(propname, SHOPT_RO) == 0) {
22077961SNatalie.Li@Sun.COM 					other1 = SHOPT_RW;
22087961SNatalie.Li@Sun.COM 					other2 = SHOPT_NONE;
22097961SNatalie.Li@Sun.COM 				} else if (strcmp(propname, SHOPT_RW) == 0) {
22107961SNatalie.Li@Sun.COM 					other1 = SHOPT_RO;
22117961SNatalie.Li@Sun.COM 					other2 = SHOPT_NONE;
22127961SNatalie.Li@Sun.COM 				} else if (strcmp(propname, SHOPT_NONE) == 0) {
22137961SNatalie.Li@Sun.COM 					other1 = SHOPT_RO;
22147961SNatalie.Li@Sun.COM 					other2 = SHOPT_RW;
22157961SNatalie.Li@Sun.COM 				} else {
22167961SNatalie.Li@Sun.COM 					other1 = NULL;
22177961SNatalie.Li@Sun.COM 					other2 = NULL;
22187961SNatalie.Li@Sun.COM 				}
22197961SNatalie.Li@Sun.COM 				if (other1 == NULL && other2 == NULL)
22207961SNatalie.Li@Sun.COM 					break;
22217961SNatalie.Li@Sun.COM 
22227961SNatalie.Li@Sun.COM 				/* compare rw(ro) with ro(rw) */
22234345Sdougm 
22247961SNatalie.Li@Sun.COM 				oprop1 = sa_get_property(parent, other1);
22257961SNatalie.Li@Sun.COM 				oprop2 = sa_get_property(parent, other2);
22267961SNatalie.Li@Sun.COM 				if (oprop1 == NULL && oprop2 == NULL)
22277961SNatalie.Li@Sun.COM 					break;
22287961SNatalie.Li@Sun.COM 				/*
22297961SNatalie.Li@Sun.COM 				 * Only potential confusion if other1
22307961SNatalie.Li@Sun.COM 				 * or other2 exists. Check the values
22317961SNatalie.Li@Sun.COM 				 * and run the check if there is a
22327961SNatalie.Li@Sun.COM 				 * value other than the one we are
22337961SNatalie.Li@Sun.COM 				 * explicitly looking at.
22347961SNatalie.Li@Sun.COM 				 */
22357961SNatalie.Li@Sun.COM 				ovalue1 = sa_get_property_attr(oprop1, "value");
22367961SNatalie.Li@Sun.COM 				ovalue2 = sa_get_property_attr(oprop2, "value");
22377961SNatalie.Li@Sun.COM 				if (ovalue1 != NULL || ovalue2 != NULL)
22387961SNatalie.Li@Sun.COM 					ret = check_rorwnone(value, ovalue1,
22397961SNatalie.Li@Sun.COM 					    ovalue2);
22407961SNatalie.Li@Sun.COM 
22417961SNatalie.Li@Sun.COM 				if (ovalue1 != NULL)
22427961SNatalie.Li@Sun.COM 					sa_free_attr_string(ovalue1);
22437961SNatalie.Li@Sun.COM 				if (ovalue2 != NULL)
22447961SNatalie.Li@Sun.COM 					sa_free_attr_string(ovalue2);
22454345Sdougm 				break;
22467961SNatalie.Li@Sun.COM 			}
22474345Sdougm 			case OPT_TYPE_LOGTAG:
22484345Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
22494345Sdougm 					int error;
22504345Sdougm 					if (value == NULL ||
22514345Sdougm 					    strlen(value) == 0) {
22524345Sdougm 						if (value != NULL)
22534345Sdougm 							sa_free_attr_string(
22544345Sdougm 							    value);
22554345Sdougm 						value = strdup("global");
22564345Sdougm 					}
22574345Sdougm 					if (value != NULL &&
22584345Sdougm 					    nfsl_findconfig(configlist, value,
22594345Sdougm 					    &error) == NULL) {
22604345Sdougm 						ret = SA_BAD_VALUE;
22614345Sdougm 					}
22625179Sdougm 					/* Must always free when done */
22635179Sdougm 					nfsl_freeconfig_list(&configlist);
22644345Sdougm 				} else {
22654345Sdougm 					ret = SA_CONFIG_ERR;
22664345Sdougm 				}
22674345Sdougm 				break;
22684345Sdougm 			case OPT_TYPE_STRING:
22694345Sdougm 				/* whatever is here should be ok */
22704345Sdougm 				break;
22714345Sdougm 			case OPT_TYPE_SECURITY:
22724345Sdougm 				/*
22734345Sdougm 				 * The "sec" property isn't used in the
22744345Sdougm 				 * non-legacy parts of sharemgr. We need to
22754345Sdougm 				 * reject it here. For legacy, it is pulled
22764345Sdougm 				 * out well before we get here.
22774345Sdougm 				 */
22784345Sdougm 				ret = SA_NO_SUCH_PROP;
22794345Sdougm 				break;
22804345Sdougm 			default:
22814345Sdougm 				break;
22823910Sdougm 			}
22835179Sdougm 
22845179Sdougm 			if (value != NULL)
22855179Sdougm 				sa_free_attr_string(value);
22865179Sdougm 
22874345Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
22884345Sdougm 				/* do the property specific check */
22896214Sdougm 				ret = optdefs[optindex].check(handle, property);
22903910Sdougm 			}
22913910Sdougm 		}
22923910Sdougm 	}
22933910Sdougm 
22943910Sdougm 	if (propname != NULL)
22954345Sdougm 		sa_free_attr_string(propname);
22963910Sdougm 	return (ret);
22973910Sdougm }
22983910Sdougm 
22993910Sdougm /*
23003910Sdougm  * Protocol management functions
23013910Sdougm  *
23023910Sdougm  * Properties defined in the default files are defined in
23033910Sdougm  * proto_option_defs for parsing and validation. If "other" and
23043910Sdougm  * "compare" are set, then the value for this property should be
23053910Sdougm  * compared against the property specified in "other" using the
23063910Sdougm  * "compare" check (either <= or >=) in order to ensure that the
23073910Sdougm  * values are in the correct range.  E.g. setting server_versmin
23083910Sdougm  * higher than server_versmax should not be allowed.
23093910Sdougm  */
23103910Sdougm 
23113910Sdougm struct proto_option_defs {
23123910Sdougm 	char *tag;
23133910Sdougm 	char *name;	/* display name -- remove protocol identifier */
23143910Sdougm 	int index;
23153910Sdougm 	int type;
23163910Sdougm 	union {
23173910Sdougm 	    int intval;
23183910Sdougm 	    char *string;
23193910Sdougm 	} defvalue;
23203910Sdougm 	uint32_t svcs;
23213910Sdougm 	int32_t minval;
23223910Sdougm 	int32_t maxval;
23233910Sdougm 	char *file;
23243910Sdougm 	char *other;
23253910Sdougm 	int compare;
23263910Sdougm #define	OPT_CMP_GE	0
23273910Sdougm #define	OPT_CMP_LE	1
23283910Sdougm 	int (*check)(char *);
23293910Sdougm } proto_options[] = {
23303910Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
23313910Sdougm 	{"nfsd_servers",
23323910Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
23333910Sdougm 	    1, INT32_MAX, NFSADMIN},
23343910Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
23353910Sdougm 	{"lockd_listen_backlog",
23363910Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
23373910Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
23383910Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
23393910Sdougm 	{"lockd_servers",
23403910Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
23413910Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
23423910Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
23433910Sdougm 	{"lockd_retransmit_timeout",
23443910Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
23453910Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23463910Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
23473910Sdougm 	{"grace_period",
23483910Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
23493910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23503910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
23513910Sdougm 	{"nfs_server_versmin",
23523910Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
23533910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23543910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
23553910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
23563910Sdougm 	{"nfs_server_versmax",
23573910Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
23583910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23593910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
23603910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
23613910Sdougm 	{"nfs_client_versmin",
23623910Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
23633910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23643910Sdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
23653910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
23663910Sdougm 	{"nfs_client_versmax",
23673910Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
23683910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23693910Sdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
23703910Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
23713910Sdougm 	{"nfs_server_delegation",
23723910Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
23733910Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
23743910Sdougm 	    NFSADMIN},
23753910Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
23763910Sdougm 	{"nfsmapid_domain",
23773910Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
23783910Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
23793910Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
23803910Sdougm 	{"nfsd_max_connections",
23813910Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
23823910Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
23833910Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
23843910Sdougm 	{"nfsd_protocol",
23853910Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
23863910Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
23873910Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
23883910Sdougm 	{"nfsd_listen_backlog",
23893910Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
23903910Sdougm 	    OPT_TYPE_NUMBER, 0,
23913910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23923910Sdougm 	{NULL}
23933910Sdougm };
23943910Sdougm 
23953910Sdougm /*
23963910Sdougm  * the protoset holds the defined options so we don't have to read
23973910Sdougm  * them multiple times
23983910Sdougm  */
23995179Sdougm static sa_protocol_properties_t protoset;
24003910Sdougm 
24013910Sdougm static int
24023910Sdougm findprotoopt(char *name, int whichname)
24033910Sdougm {
24043910Sdougm 	int i;
24053910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
24064345Sdougm 		if (whichname == 1) {
24074345Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
24083910Sdougm 			return (i);
24094345Sdougm 		} else {
24104345Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
24114345Sdougm 				return (i);
24124345Sdougm 		}
24133910Sdougm 	}
24143910Sdougm 	return (-1);
24153910Sdougm }
24163910Sdougm 
24173910Sdougm /*
24183910Sdougm  * fixcaselower(str)
24193910Sdougm  *
24203910Sdougm  * convert a string to lower case (inplace).
24213910Sdougm  */
24223910Sdougm 
24233910Sdougm static void
24243910Sdougm fixcaselower(char *str)
24253910Sdougm {
24263910Sdougm 	while (*str) {
24274345Sdougm 		*str = tolower(*str);
24284345Sdougm 		str++;
24293910Sdougm 	}
24303910Sdougm }
24313910Sdougm 
24323910Sdougm /*
24333910Sdougm  * fixcaseupper(str)
24343910Sdougm  *
24353910Sdougm  * convert a string to upper case (inplace).
24363910Sdougm  */
24373910Sdougm 
24383910Sdougm static void
24393910Sdougm fixcaseupper(char *str)
24403910Sdougm {
24413910Sdougm 	while (*str) {
24424345Sdougm 		*str = toupper(*str);
24434345Sdougm 		str++;
24443910Sdougm 	}
24453910Sdougm }
24463910Sdougm 
24473910Sdougm /*
24484241Sdougm  * skipwhitespace(str)
24494241Sdougm  *
24504241Sdougm  * Skip leading white space. It is assumed that it is called with a
24514241Sdougm  * valid pointer.
24524241Sdougm  */
24534241Sdougm 
24544241Sdougm static char *
24554241Sdougm skipwhitespace(char *str)
24564241Sdougm {
24574241Sdougm 	while (*str && isspace(*str))
24584241Sdougm 		str++;
24594241Sdougm 
24604241Sdougm 	return (str);
24614241Sdougm }
24624241Sdougm 
24634241Sdougm /*
24644345Sdougm  * extractprop()
24654345Sdougm  *
24664345Sdougm  * Extract the property and value out of the line and create the
24674345Sdougm  * property in the optionset.
24684345Sdougm  */
24696019Sdougm static int
24704345Sdougm extractprop(char *name, char *value)
24714345Sdougm {
24724345Sdougm 	sa_property_t prop;
24734345Sdougm 	int index;
24746019Sdougm 	int ret = SA_OK;
24754345Sdougm 	/*
24764345Sdougm 	 * Remove any leading
24774345Sdougm 	 * white space.
24784345Sdougm 	 */
24794345Sdougm 	name = skipwhitespace(name);
24804345Sdougm 
24814345Sdougm 	index = findprotoopt(name, 0);
24824345Sdougm 	if (index >= 0) {
24834345Sdougm 		fixcaselower(name);
24844345Sdougm 		prop = sa_create_property(proto_options[index].name, value);
24854345Sdougm 		if (prop != NULL)
24866019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
24876019Sdougm 		else
24886019Sdougm 			ret = SA_NO_MEMORY;
24894345Sdougm 	}
24906019Sdougm 	return (ret);
24914345Sdougm }
24924345Sdougm 
24934345Sdougm /*
24943910Sdougm  * initprotofromdefault()
24953910Sdougm  *
24966162Sdougm  * Read the default file(s) and add the defined values to the
24973910Sdougm  * protoset.  Note that default values are known from the built in
24986162Sdougm  * table in case the file doesn't have a definition. Not having the
24996162Sdougm  * /etc/default/nfs file is OK since we have builtin default
25006162Sdougm  * values. The default file will get constructed as needed if values
25016162Sdougm  * are changed from the defaults.
25023910Sdougm  */
25033910Sdougm 
25043910Sdougm static int
25053910Sdougm initprotofromdefault()
25063910Sdougm {
25073910Sdougm 	FILE *nfs;
25083910Sdougm 	char buff[BUFSIZ];
25093910Sdougm 	char *name;
25103910Sdougm 	char *value;
25116019Sdougm 	int ret = SA_OK;
25123910Sdougm 
25133910Sdougm 	protoset = sa_create_protocol_properties("nfs");
25143910Sdougm 
25153910Sdougm 	if (protoset != NULL) {
25164345Sdougm 		nfs = fopen(NFSADMIN, "r");
25174345Sdougm 		if (nfs != NULL) {
25186019Sdougm 			while (ret == SA_OK &&
25196019Sdougm 			    fgets(buff, sizeof (buff), nfs) != NULL) {
25204345Sdougm 				switch (buff[0]) {
25214345Sdougm 				case '\n':
25224345Sdougm 				case '#':
25234345Sdougm 					/* skip */
25244345Sdougm 					break;
25254345Sdougm 				default:
25264345Sdougm 					name = buff;
25274345Sdougm 					buff[strlen(buff) - 1] = '\0';
25284345Sdougm 					value = strchr(name, '=');
25294345Sdougm 					if (value != NULL) {
25304345Sdougm 						*value++ = '\0';
25316019Sdougm 						ret = extractprop(name, value);
25324345Sdougm 					}
25334345Sdougm 				}
25343910Sdougm 			}
25356019Sdougm 			(void) fclose(nfs);
25366019Sdougm 		} else {
25376162Sdougm 			switch (errno) {
25386162Sdougm 			case EPERM:
25396162Sdougm 			case EACCES:
25406162Sdougm 				ret = SA_NO_PERMISSION;
25416162Sdougm 				break;
25426162Sdougm 			case ENOENT:
25436162Sdougm 				break;
25446162Sdougm 			default:
25456162Sdougm 				ret = SA_SYSTEM_ERR;
25466162Sdougm 				break;
25476162Sdougm 			}
25483910Sdougm 		}
25496019Sdougm 	} else {
25506019Sdougm 		ret = SA_NO_MEMORY;
25513910Sdougm 	}
25526019Sdougm 	return (ret);
25533910Sdougm }
25543910Sdougm 
25553910Sdougm /*
25564345Sdougm  * add_defaults()
25573910Sdougm  *
25583910Sdougm  * Add the default values for any property not defined in the parsing
25593910Sdougm  * of the default files. Values are set according to their defined
25603910Sdougm  * types.
25613910Sdougm  */
25623910Sdougm 
25633910Sdougm static void
25643910Sdougm add_defaults()
25653910Sdougm {
25663910Sdougm 	int i;
25673910Sdougm 	char number[MAXDIGITS];
25683910Sdougm 
25693910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
25704345Sdougm 		sa_property_t prop;
25714345Sdougm 		prop = sa_get_protocol_property(protoset,
25724345Sdougm 		    proto_options[i].name);
25734345Sdougm 		if (prop == NULL) {
25744345Sdougm 			/* add the default value */
25754345Sdougm 			switch (proto_options[i].type) {
25764345Sdougm 			case OPT_TYPE_NUMBER:
25774345Sdougm 				(void) snprintf(number, sizeof (number), "%d",
25784345Sdougm 				    proto_options[i].defvalue.intval);
25794345Sdougm 				prop = sa_create_property(proto_options[i].name,
25804345Sdougm 				    number);
25814345Sdougm 				break;
25823910Sdougm 
25834345Sdougm 			case OPT_TYPE_BOOLEAN:
25844345Sdougm 				prop = sa_create_property(proto_options[i].name,
25854345Sdougm 				    proto_options[i].defvalue.intval ?
25864345Sdougm 				    "true" : "false");
25874345Sdougm 				break;
25883910Sdougm 
25894345Sdougm 			case OPT_TYPE_ONOFF:
25904345Sdougm 				prop = sa_create_property(proto_options[i].name,
25914345Sdougm 				    proto_options[i].defvalue.intval ?
25924345Sdougm 				    "on" : "off");
25934345Sdougm 				break;
25943910Sdougm 
25954345Sdougm 			default:
25964345Sdougm 				/* treat as strings of zero length */
25974345Sdougm 				prop = sa_create_property(proto_options[i].name,
25984345Sdougm 				    "");
25994345Sdougm 				break;
26004345Sdougm 			}
26014345Sdougm 			if (prop != NULL)
26024345Sdougm 				(void) sa_add_protocol_property(protoset, prop);
26033910Sdougm 		}
26043910Sdougm 	}
26053910Sdougm }
26063910Sdougm 
26073910Sdougm static void
26083910Sdougm free_protoprops()
26093910Sdougm {
26105179Sdougm 	if (protoset != NULL) {
26115179Sdougm 		xmlFreeNode(protoset);
26125179Sdougm 		protoset = NULL;
26135179Sdougm 	}
26143910Sdougm }
26153910Sdougm 
26163910Sdougm /*
26173910Sdougm  * nfs_init()
26183910Sdougm  *
26193910Sdougm  * Initialize the NFS plugin.
26203910Sdougm  */
26213910Sdougm 
26223910Sdougm static int
26233910Sdougm nfs_init()
26243910Sdougm {
26253910Sdougm 	int ret = SA_OK;
26263910Sdougm 
26276162Sdougm 	if (sa_plugin_ops.sa_init != nfs_init) {
26284345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
26294345Sdougm 		    "NFS plugin not properly initialized\n"));
26306162Sdougm 		return (SA_CONFIG_ERR);
26316162Sdougm 	}
26323910Sdougm 
26333910Sdougm 	ret = initprotofromdefault();
26346162Sdougm 	if (ret != SA_OK) {
26356162Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
26366162Sdougm 		    "NFS plugin problem with default file: %s\n"),
26376162Sdougm 		    sa_errorstr(ret));
26386162Sdougm 		ret = SA_OK;
26396162Sdougm 	}
26406162Sdougm 	add_defaults();
26413910Sdougm 
26423910Sdougm 	return (ret);
26433910Sdougm }
26443910Sdougm 
26453910Sdougm /*
26463910Sdougm  * nfs_fini()
26473910Sdougm  *
26483910Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
26493910Sdougm  */
26503910Sdougm 
26513910Sdougm static void
26523910Sdougm nfs_fini()
26533910Sdougm {
26543910Sdougm 	free_protoprops();
26553910Sdougm }
26563910Sdougm 
26573910Sdougm /*
26583910Sdougm  * nfs_get_proto_set()
26593910Sdougm  *
26603910Sdougm  * Return an optionset with all the protocol specific properties in
26613910Sdougm  * it.
26623910Sdougm  */
26633910Sdougm 
26643910Sdougm static sa_protocol_properties_t
26653910Sdougm nfs_get_proto_set()
26663910Sdougm {
26673910Sdougm 	return (protoset);
26683910Sdougm }
26693910Sdougm 
26703910Sdougm struct deffile {
26713910Sdougm 	struct deffile *next;
26723910Sdougm 	char *line;
26733910Sdougm };
26743910Sdougm 
26753910Sdougm /*
26763910Sdougm  * read_default_file(fname)
26773910Sdougm  *
26783910Sdougm  * Read the specified default file. We return a list of entries. This
26793910Sdougm  * get used for adding or removing values.
26803910Sdougm  */
26813910Sdougm 
26823910Sdougm static struct deffile *
26833910Sdougm read_default_file(char *fname)
26843910Sdougm {
26853910Sdougm 	FILE *file;
26863910Sdougm 	struct deffile *defs = NULL;
26873910Sdougm 	struct deffile *newdef;
26883910Sdougm 	struct deffile *prevdef = NULL;
26893910Sdougm 	char buff[BUFSIZ * 2];
26903910Sdougm 
26913910Sdougm 	file = fopen(fname, "r");
26923910Sdougm 	if (file != NULL) {
26934345Sdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
26944345Sdougm 			newdef = (struct deffile *)calloc(1,
26954345Sdougm 			    sizeof (struct deffile));
26964345Sdougm 			if (newdef != NULL) {
26974345Sdougm 				/* Make sure we skip any leading whitespace. */
26984345Sdougm 				newdef->line = strdup(skipwhitespace(buff));
26994345Sdougm 				if (defs == NULL) {
27004345Sdougm 					prevdef = defs = newdef;
27014345Sdougm 				} else {
27024345Sdougm 					prevdef->next = newdef;
27034345Sdougm 					prevdef = newdef;
27044345Sdougm 				}
27054345Sdougm 			}
27063910Sdougm 		}
27076162Sdougm 		(void) fclose(file);
27086162Sdougm 	} else {
27096162Sdougm 		int ret = SA_OK;
27106162Sdougm 		switch (errno) {
27116162Sdougm 		case EPERM:
27126162Sdougm 		case EACCES:
27136162Sdougm 			ret = SA_NO_PERMISSION;
27146162Sdougm 			break;
27156162Sdougm 		case ENOENT:
27166162Sdougm 			break;
27176162Sdougm 		default:
27186162Sdougm 			ret = SA_SYSTEM_ERR;
27196162Sdougm 			break;
27206162Sdougm 		}
27216162Sdougm 		if (ret == SA_OK) {
27226162Sdougm 			/* Want at least one comment line */
27236162Sdougm 			defs = (struct deffile *)
27246162Sdougm 			    calloc(1, sizeof (struct deffile));
27256162Sdougm 			defs->line = strdup("# NFS default file\n");
27266162Sdougm 		}
27273910Sdougm 	}
27283910Sdougm 	return (defs);
27293910Sdougm }
27303910Sdougm 
27313910Sdougm static void
27323910Sdougm free_default_file(struct deffile *defs)
27333910Sdougm {
27343910Sdougm 	struct deffile *curdefs = NULL;
27353910Sdougm 
27363910Sdougm 	while (defs != NULL) {
27374345Sdougm 		curdefs = defs;
27384345Sdougm 		defs = defs->next;
27394345Sdougm 		if (curdefs->line != NULL)
27404345Sdougm 			free(curdefs->line);
27414345Sdougm 		free(curdefs);
27423910Sdougm 	}
27433910Sdougm }
27443910Sdougm 
27453910Sdougm /*
27463910Sdougm  * write_default_file(fname, defs)
27473910Sdougm  *
27483910Sdougm  * Write the default file back.
27493910Sdougm  */
27503910Sdougm 
27513910Sdougm static int
27523910Sdougm write_default_file(char *fname, struct deffile *defs)
27533910Sdougm {
27543910Sdougm 	FILE *file;
27553910Sdougm 	int ret = SA_OK;
27563910Sdougm 	sigset_t old, new;
27573910Sdougm 
27583910Sdougm 	file = fopen(fname, "w+");
27593910Sdougm 	if (file != NULL) {
27604345Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
27614345Sdougm 		(void) sigaddset(&new, SIGHUP);
27624345Sdougm 		(void) sigaddset(&new, SIGINT);
27634345Sdougm 		(void) sigaddset(&new, SIGQUIT);
27644345Sdougm 		(void) sigaddset(&new, SIGTSTP);
27654345Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
27664345Sdougm 		while (defs != NULL) {
27674345Sdougm 			(void) fputs(defs->line, file);
27684345Sdougm 			defs = defs->next;
27694345Sdougm 		}
27704345Sdougm 		(void) fsync(fileno(file));
27714345Sdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
27724345Sdougm 		(void) fclose(file);
27733910Sdougm 	} else {
27744345Sdougm 		switch (errno) {
27754345Sdougm 		case EPERM:
27764345Sdougm 		case EACCES:
27774345Sdougm 			ret = SA_NO_PERMISSION;
27784345Sdougm 			break;
27794345Sdougm 		default:
27804345Sdougm 			ret = SA_SYSTEM_ERR;
27814345Sdougm 		}
27823910Sdougm 	}
27833910Sdougm 	return (ret);
27843910Sdougm }
27853910Sdougm 
27863910Sdougm 
27873910Sdougm /*
27883910Sdougm  * set_default_file_value(tag, value)
27893910Sdougm  *
27903910Sdougm  * Set the default file value for tag to value. Then rewrite the file.
27913910Sdougm  * tag and value are always set.  The caller must ensure this.
27923910Sdougm  */
27933910Sdougm 
27943910Sdougm #define	MAX_STRING_LENGTH	256
27953910Sdougm static int
27963910Sdougm set_default_file_value(char *tag, char *value)
27973910Sdougm {
27983910Sdougm 	int ret = SA_OK;
27993910Sdougm 	struct deffile *root;
28003910Sdougm 	struct deffile *defs;
28013910Sdougm 	struct deffile *prev;
28023910Sdougm 	char string[MAX_STRING_LENGTH];
28033910Sdougm 	int len;
28046162Sdougm 	boolean_t update = B_FALSE;
28053910Sdougm 
28063910Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
28073910Sdougm 	len = strlen(string);
28083910Sdougm 
28093910Sdougm 	root = defs = read_default_file(NFSADMIN);
28103910Sdougm 	if (root == NULL) {
28116162Sdougm 		switch (errno) {
28126162Sdougm 		case EPERM:
28136162Sdougm 		case EACCES:
28144345Sdougm 			ret = SA_NO_PERMISSION;
28156162Sdougm 			break;
28166162Sdougm 		default:
28176162Sdougm 			ret = SA_NO_MEMORY;
28186162Sdougm 			break;
28196162Sdougm 		}
28206162Sdougm 		return (ret);
28216162Sdougm 	}
28226162Sdougm 
28236162Sdougm 	while (defs != NULL) {
28246162Sdougm 		if (defs->line != NULL &&
28256162Sdougm 		    strncasecmp(defs->line, string, len) == 0) {
28266162Sdougm 			/* replace with the new value */
28276162Sdougm 			free(defs->line);
28286162Sdougm 			fixcaseupper(tag);
28296162Sdougm 			(void) snprintf(string, sizeof (string),
28306162Sdougm 			    "%s=%s\n", tag, value);
28316162Sdougm 			string[MAX_STRING_LENGTH - 1] = '\0';
28326162Sdougm 			defs->line = strdup(string);
28336162Sdougm 			update = B_TRUE;
28346162Sdougm 			break;
28356162Sdougm 		}
28366162Sdougm 		defs = defs->next;
28376162Sdougm 	}
28386162Sdougm 	if (!update) {
28396162Sdougm 		defs = root;
28406162Sdougm 		/* didn't find, so see if it is a comment */
28416162Sdougm 		(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
28426162Sdougm 		len = strlen(string);
28433910Sdougm 		while (defs != NULL) {
28446162Sdougm 			if (strncasecmp(defs->line, string, len) == 0) {
28454345Sdougm 				/* replace with the new value */
28464345Sdougm 				free(defs->line);
28474345Sdougm 				fixcaseupper(tag);
28484345Sdougm 				(void) snprintf(string, sizeof (string),
28493910Sdougm 				    "%s=%s\n", tag, value);
28504345Sdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
28514345Sdougm 				defs->line = strdup(string);
28526162Sdougm 				update = B_TRUE;
28534345Sdougm 				break;
28544345Sdougm 			}
28554345Sdougm 			defs = defs->next;
28563910Sdougm 		}
28576162Sdougm 	}
28586162Sdougm 	if (!update) {
28596162Sdougm 		fixcaseupper(tag);
28606162Sdougm 		(void) snprintf(string, sizeof (string), "%s=%s\n",
28616162Sdougm 		    tag, value);
28626162Sdougm 		prev = root;
28636162Sdougm 		while (prev->next != NULL)
28646162Sdougm 			prev = prev->next;
28656162Sdougm 		defs = malloc(sizeof (struct deffile));
28666162Sdougm 		prev->next = defs;
28676162Sdougm 		if (defs != NULL) {
28686162Sdougm 			defs->next = NULL;
28696162Sdougm 			defs->line = strdup(string);
28706162Sdougm 			update = B_TRUE;
28713910Sdougm 		}
28723910Sdougm 	}
28736162Sdougm 	if (update) {
28746162Sdougm 		ret = write_default_file(NFSADMIN, root);
28756162Sdougm 	}
28766162Sdougm 	free_default_file(root);
28773910Sdougm 	return (ret);
28783910Sdougm }
28793910Sdougm 
28803910Sdougm /*
28813910Sdougm  * service_in_state(service, chkstate)
28823910Sdougm  *
28833910Sdougm  * Want to know if the specified service is in the desired state
28843910Sdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
28853910Sdougm  * isn't.
28863910Sdougm  */
28873910Sdougm static int
28883910Sdougm service_in_state(char *service, const char *chkstate)
28893910Sdougm {
28903910Sdougm 	char *state;
28913910Sdougm 	int ret = B_FALSE;
28923910Sdougm 
28933910Sdougm 	state = smf_get_state(service);
28943910Sdougm 	if (state != NULL) {
28954345Sdougm 		/* got the state so get the equality for the return value */
28964345Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
28974345Sdougm 		free(state);
28983910Sdougm 	}
28993910Sdougm 	return (ret);
29003910Sdougm }
29013910Sdougm 
29023910Sdougm /*
29033910Sdougm  * restart_service(svcs)
29043910Sdougm  *
29053910Sdougm  * Walk through the bit mask of services that need to be restarted in
29063910Sdougm  * order to use the new property values. Some properties affect
29073910Sdougm  * multiple daemons. Should only restart a service if it is currently
29083910Sdougm  * enabled (online).
29093910Sdougm  */
29103910Sdougm 
29113910Sdougm static void
29123910Sdougm restart_service(uint32_t svcs)
29133910Sdougm {
29143910Sdougm 	uint32_t mask;
29153910Sdougm 	int ret;
29163910Sdougm 	char *service;
29173910Sdougm 
29183910Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
29194345Sdougm 		switch (svcs & mask) {
29204345Sdougm 		case SVC_LOCKD:
29214345Sdougm 			service = LOCKD;
29224345Sdougm 			break;
29234345Sdougm 		case SVC_STATD:
29244345Sdougm 			service = STATD;
29254345Sdougm 			break;
29264345Sdougm 		case SVC_NFSD:
29274345Sdougm 			service = NFSD;
29284345Sdougm 			break;
29294345Sdougm 		case SVC_MOUNTD:
29304345Sdougm 			service = MOUNTD;
29314345Sdougm 			break;
29324345Sdougm 		case SVC_NFS4CBD:
29334345Sdougm 			service = NFS4CBD;
29344345Sdougm 			break;
29354345Sdougm 		case SVC_NFSMAPID:
29364345Sdougm 			service = NFSMAPID;
29374345Sdougm 			break;
29384345Sdougm 		case SVC_RQUOTAD:
29394345Sdougm 			service = RQUOTAD;
29404345Sdougm 			break;
29414345Sdougm 		case SVC_NFSLOGD:
29424345Sdougm 			service = NFSLOGD;
29434345Sdougm 			break;
294411291SRobert.Thurlow@Sun.COM 		case SVC_REPARSED:
294511291SRobert.Thurlow@Sun.COM 			service = REPARSED;
294611291SRobert.Thurlow@Sun.COM 			break;
29474345Sdougm 		default:
29484345Sdougm 			continue;
29494345Sdougm 		}
29503910Sdougm 
29513910Sdougm 		/*
29523910Sdougm 		 * Only attempt to restart the service if it is
29533910Sdougm 		 * currently running. In the future, it may be
29543910Sdougm 		 * desirable to use smf_refresh_instance if the NFS
29553910Sdougm 		 * services ever implement the refresh method.
29563910Sdougm 		 */
29574345Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
29584345Sdougm 			ret = smf_restart_instance(service);
29593910Sdougm 			/*
29604345Sdougm 			 * There are only a few SMF errors at this point, but
29614345Sdougm 			 * it is also possible that a bad value may have put
29624345Sdougm 			 * the service into maintenance if there wasn't an
29634345Sdougm 			 * SMF level error.
29643910Sdougm 			 */
29654345Sdougm 			if (ret != 0) {
29664345Sdougm 				(void) fprintf(stderr,
29674345Sdougm 				    dgettext(TEXT_DOMAIN,
29684345Sdougm 				    "%s failed to restart: %s\n"),
29694345Sdougm 				    scf_strerror(scf_error()));
29704345Sdougm 			} else {
29714345Sdougm 				/*
29724345Sdougm 				 * Check whether it has gone to "maintenance"
29734345Sdougm 				 * mode or not. Maintenance implies something
29744345Sdougm 				 * went wrong.
29754345Sdougm 				 */
29764345Sdougm 				if (service_in_state(service,
29774345Sdougm 				    SCF_STATE_STRING_MAINT)) {
29784345Sdougm 					(void) fprintf(stderr,
29794345Sdougm 					    dgettext(TEXT_DOMAIN,
29804345Sdougm 					    "%s failed to restart\n"),
29814345Sdougm 					    service);
29824345Sdougm 				}
29834345Sdougm 			}
29843910Sdougm 		}
29854345Sdougm 		svcs &= ~mask;
29863910Sdougm 	}
29873910Sdougm }
29883910Sdougm 
29893910Sdougm /*
29903910Sdougm  * nfs_minmax_check(name, value)
29913910Sdougm  *
29923910Sdougm  * Verify that the value for the property specified by index is valid
29933910Sdougm  * relative to the opposite value in the case of a min/max variable.
29943910Sdougm  * Currently, server_minvers/server_maxvers and
29953910Sdougm  * client_minvers/client_maxvers are the only ones to check.
29963910Sdougm  */
29973910Sdougm 
29983910Sdougm static int
29993910Sdougm nfs_minmax_check(int index, int value)
30003910Sdougm {
30013910Sdougm 	int val;
30023910Sdougm 	char *pval;
30033910Sdougm 	sa_property_t prop;
30043910Sdougm 	sa_optionset_t opts;
30053910Sdougm 	int ret = B_TRUE;
30063910Sdougm 
30073910Sdougm 	if (proto_options[index].other != NULL) {
30084345Sdougm 		/* have a property to compare against */
30094345Sdougm 		opts = nfs_get_proto_set();
30104345Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
30113910Sdougm 		/*
30123910Sdougm 		 * If we don't find the property, assume default
30133910Sdougm 		 * values which will work since the max will be at the
30143910Sdougm 		 * max and the min at the min.
30153910Sdougm 		 */
30164345Sdougm 		if (prop != NULL) {
30174345Sdougm 			pval = sa_get_property_attr(prop, "value");
30184345Sdougm 			if (pval != NULL) {
30194345Sdougm 				val = strtoul(pval, NULL, 0);
30204345Sdougm 				if (proto_options[index].compare ==
30214345Sdougm 				    OPT_CMP_LE) {
30224345Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
30234345Sdougm 				} else if (proto_options[index].compare ==
30244345Sdougm 				    OPT_CMP_GE) {
30254345Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
30264345Sdougm 				}
3027*11337SWilliam.Krier@Sun.COM 				sa_free_attr_string(pval);
30284345Sdougm 			}
30293910Sdougm 		}
30303910Sdougm 	}
30313910Sdougm 	return (ret);
30323910Sdougm }
30333910Sdougm 
30343910Sdougm /*
30353910Sdougm  * nfs_validate_proto_prop(index, name, value)
30363910Sdougm  *
30375331Samw  * Verify that the property specified by name can take the new
30383910Sdougm  * value. This is a sanity check to prevent bad values getting into
30393910Sdougm  * the default files. All values need to be checked against what is
30403910Sdougm  * allowed by their defined type. If a type isn't explicitly defined
30413910Sdougm  * here, it is treated as a string.
30423910Sdougm  *
30433910Sdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
30443910Sdougm  * within the range specified and potentially against another property
30453910Sdougm  * value as well as specified in the proto_options members other and
30463910Sdougm  * compare.
30473910Sdougm  */
30483910Sdougm 
30493910Sdougm static int
30503910Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
30513910Sdougm {
30523910Sdougm 	int ret = SA_OK;
30533910Sdougm 	char *cp;
30543910Sdougm #ifdef lint
30553910Sdougm 	name = name;
30563910Sdougm #endif
30573910Sdougm 
30583910Sdougm 	switch (proto_options[index].type) {
30593910Sdougm 	case OPT_TYPE_NUMBER:
30604345Sdougm 		if (!is_a_number(value))
30614345Sdougm 			ret = SA_BAD_VALUE;
30624345Sdougm 		else {
30634345Sdougm 			int val;
30644345Sdougm 			val = strtoul(value, NULL, 0);
30654345Sdougm 			if (val < proto_options[index].minval ||
30664345Sdougm 			    val > proto_options[index].maxval)
30674345Sdougm 				ret = SA_BAD_VALUE;
30684345Sdougm 			/*
30694345Sdougm 			 * For server_versmin/server_versmax and
30704345Sdougm 			 * client_versmin/client_versmax, the value of the
30714345Sdougm 			 * min(max) should be checked to be correct relative
30724345Sdougm 			 * to the current max(min).
30734345Sdougm 			 */
30744345Sdougm 			if (!nfs_minmax_check(index, val)) {
30754345Sdougm 				ret = SA_BAD_VALUE;
30764345Sdougm 			}
30773910Sdougm 		}
30784345Sdougm 		break;
30793910Sdougm 
30803910Sdougm 	case OPT_TYPE_DOMAIN:
30813910Sdougm 		/*
30823910Sdougm 		 * needs to be a qualified domain so will have at
30833910Sdougm 		 * least one period and other characters on either
30843910Sdougm 		 * side of it.  A zero length string is also allowed
30853910Sdougm 		 * and is the way to turn off the override.
30863910Sdougm 		 */
30874345Sdougm 		if (strlen(value) == 0)
30884345Sdougm 			break;
30894345Sdougm 		cp = strchr(value, '.');
30904345Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
30914345Sdougm 			ret = SA_BAD_VALUE;
30923910Sdougm 		break;
30933910Sdougm 
30943910Sdougm 	case OPT_TYPE_BOOLEAN:
30954345Sdougm 		if (strlen(value) == 0 ||
30964345Sdougm 		    strcasecmp(value, "true") == 0 ||
30974345Sdougm 		    strcmp(value, "1") == 0 ||
30984345Sdougm 		    strcasecmp(value, "false") == 0 ||
30994345Sdougm 		    strcmp(value, "0") == 0) {
31004345Sdougm 			ret = SA_OK;
31014345Sdougm 		} else {
31024345Sdougm 			ret = SA_BAD_VALUE;
31034345Sdougm 		}
31044345Sdougm 		break;
31053910Sdougm 
31063910Sdougm 	case OPT_TYPE_ONOFF:
31074345Sdougm 		if (strcasecmp(value, "on") != 0 &&
31084345Sdougm 		    strcasecmp(value, "off") != 0) {
31094345Sdougm 			ret = SA_BAD_VALUE;
31104345Sdougm 		}
31114345Sdougm 		break;
31123910Sdougm 
31133910Sdougm 	case OPT_TYPE_PROTOCOL:
31146162Sdougm 		if (strlen(value) != 0 &&
31156162Sdougm 		    strcasecmp(value, "all") != 0 &&
31164345Sdougm 		    strcasecmp(value, "tcp") != 0 &&
31174345Sdougm 		    strcasecmp(value, "udp") != 0)
31184345Sdougm 			ret = SA_BAD_VALUE;
31194345Sdougm 		break;
31203910Sdougm 
31213910Sdougm 	default:
31224345Sdougm 		/* treat as a string */
31234345Sdougm 		break;
31243910Sdougm 	}
31253910Sdougm 	return (ret);
31263910Sdougm }
31273910Sdougm 
31283910Sdougm /*
31293910Sdougm  * nfs_set_proto_prop(prop)
31303910Sdougm  *
31313910Sdougm  * check that prop is valid.
31323910Sdougm  */
31333910Sdougm 
31343910Sdougm static int
31353910Sdougm nfs_set_proto_prop(sa_property_t prop)
31363910Sdougm {
31373910Sdougm 	int ret = SA_OK;
31383910Sdougm 	char *name;
31393910Sdougm 	char *value;
31403910Sdougm 
31413910Sdougm 	name = sa_get_property_attr(prop, "type");
31423910Sdougm 	value = sa_get_property_attr(prop, "value");
31433910Sdougm 	if (name != NULL && value != NULL) {
31444345Sdougm 		int index = findprotoopt(name, 1);
31454345Sdougm 		if (index >= 0) {
31464345Sdougm 			/* should test for valid value */
31474345Sdougm 			ret = nfs_validate_proto_prop(index, name, value);
31484345Sdougm 			if (ret == SA_OK)
31494345Sdougm 				ret = set_default_file_value(
31504345Sdougm 				    proto_options[index].tag, value);
31514345Sdougm 			if (ret == SA_OK)
31524345Sdougm 				restart_service(proto_options[index].svcs);
31534345Sdougm 		}
31543910Sdougm 	}
31553910Sdougm 	if (name != NULL)
31564345Sdougm 		sa_free_attr_string(name);
31573910Sdougm 	if (value != NULL)
31584345Sdougm 		sa_free_attr_string(value);
31593910Sdougm 	return (ret);
31603910Sdougm }
31613910Sdougm 
31623910Sdougm /*
31633910Sdougm  * nfs_get_status()
31643910Sdougm  *
31653910Sdougm  * What is the current status of the nfsd? We use the SMF state here.
31663910Sdougm  * Caller must free the returned value.
31673910Sdougm  */
31683910Sdougm 
31693910Sdougm static char *
31703910Sdougm nfs_get_status()
31713910Sdougm {
31723910Sdougm 	char *state;
31733910Sdougm 	state = smf_get_state(NFSD);
31743910Sdougm 	return (state != NULL ? state : strdup("-"));
31753910Sdougm }
31763910Sdougm 
31773910Sdougm /*
31783910Sdougm  * nfs_space_alias(alias)
31793910Sdougm  *
31803910Sdougm  * Lookup the space (security) name. If it is default, convert to the
31813910Sdougm  * real name.
31823910Sdougm  */
31833910Sdougm 
31843910Sdougm static char *
31853910Sdougm nfs_space_alias(char *space)
31863910Sdougm {
31873910Sdougm 	char *name = space;
31883910Sdougm 	seconfig_t secconf;
31893910Sdougm 
31903910Sdougm 	/*
31913910Sdougm 	 * Only the space named "default" is special. If it is used,
31923910Sdougm 	 * the default needs to be looked up and the real name used.
31933910Sdougm 	 * This is normally "sys" but could be changed.  We always
31943910Sdougm 	 * change defautl to the real name.
31953910Sdougm 	 */
31963910Sdougm 	if (strcmp(space, "default") == 0 &&
31973910Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
31984345Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
31994345Sdougm 			name = secconf.sc_name;
32003910Sdougm 	}
32013910Sdougm 	return (strdup(name));
32023910Sdougm }
32035331Samw 
32045331Samw /*
32055331Samw  * nfs_features()
32065331Samw  *
32075331Samw  * Return a mask of the features required.
32085331Samw  */
32095331Samw 
32105331Samw static uint64_t
32115331Samw nfs_features()
32125331Samw {
32136088Sdougm 	return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);
32145331Samw }
3215