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[] =
117*11291SRobert.Thurlow@Sun.COM 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, REPARSED, NULL };
1183910Sdougm static char *service_list_logging[] =
119*11291SRobert.Thurlow@Sun.COM 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, REPARSED,
120*11291SRobert.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},
1643910Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
1657961SNatalie.Li@Sun.COM #define	OPT_VOLFH	17
1663910Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1673910Sdougm #endif /* VOLATILE_FH_TEST */
1683910Sdougm 	NULL
1693910Sdougm };
1703910Sdougm 
1713910Sdougm /*
1727961SNatalie.Li@Sun.COM  * Codesets that may need to be converted to UTF-8 for file paths.
1737961SNatalie.Li@Sun.COM  * Add new names here to add new property support. If we ever get a
1747961SNatalie.Li@Sun.COM  * way to query the kernel for character sets, this should become
1757961SNatalie.Li@Sun.COM  * dynamically loaded. Make sure changes here are reflected in
1767961SNatalie.Li@Sun.COM  * cmd/fs.d/nfs/mountd/nfscmd.c
1777961SNatalie.Li@Sun.COM  */
1787961SNatalie.Li@Sun.COM 
1797961SNatalie.Li@Sun.COM static char *legal_conv[] = {
1807961SNatalie.Li@Sun.COM 	"euc-cn",
1817961SNatalie.Li@Sun.COM 	"euc-jp",
1827961SNatalie.Li@Sun.COM 	"euc-jpms",
1837961SNatalie.Li@Sun.COM 	"euc-kr",
1847961SNatalie.Li@Sun.COM 	"euc-tw",
1857961SNatalie.Li@Sun.COM 	"iso8859-1",
1867961SNatalie.Li@Sun.COM 	"iso8859-2",
1877961SNatalie.Li@Sun.COM 	"iso8859-5",
1887961SNatalie.Li@Sun.COM 	"iso8859-6",
1897961SNatalie.Li@Sun.COM 	"iso8859-7",
1907961SNatalie.Li@Sun.COM 	"iso8859-8",
1917961SNatalie.Li@Sun.COM 	"iso8859-9",
1927961SNatalie.Li@Sun.COM 	"iso8859-13",
1937961SNatalie.Li@Sun.COM 	"iso8859-15",
1947961SNatalie.Li@Sun.COM 	"koi8-r",
1957961SNatalie.Li@Sun.COM 	NULL
1967961SNatalie.Li@Sun.COM };
1977961SNatalie.Li@Sun.COM 
1987961SNatalie.Li@Sun.COM /*
1993910Sdougm  * list of properties that are related to security flavors.
2003910Sdougm  */
2013910Sdougm static char *seclist[] = {
2023910Sdougm 	SHOPT_RO,
2033910Sdougm 	SHOPT_RW,
2043910Sdougm 	SHOPT_ROOT,
2053910Sdougm 	SHOPT_WINDOW,
2067961SNatalie.Li@Sun.COM 	SHOPT_NONE,
2077961SNatalie.Li@Sun.COM 	SHOPT_ROOT_MAPPING,
2083910Sdougm 	NULL
2093910Sdougm };
2103910Sdougm 
2113910Sdougm /* structure for list of securities */
2123910Sdougm struct securities {
2133910Sdougm 	sa_security_t security;
2143910Sdougm 	struct securities *next;
2153910Sdougm };
2163910Sdougm 
2173910Sdougm /*
2187961SNatalie.Li@Sun.COM  * findcharset(charset)
2197961SNatalie.Li@Sun.COM  *
2207961SNatalie.Li@Sun.COM  * Returns B_TRUE if the charset is a legal conversion otherwise
2217961SNatalie.Li@Sun.COM  * B_FALSE. This will need to be rewritten to be more efficient when
2227961SNatalie.Li@Sun.COM  * we have a dynamic list of legal conversions.
2237961SNatalie.Li@Sun.COM  */
2247961SNatalie.Li@Sun.COM 
2257961SNatalie.Li@Sun.COM static boolean_t
2267961SNatalie.Li@Sun.COM findcharset(char *charset)
2277961SNatalie.Li@Sun.COM {
2287961SNatalie.Li@Sun.COM 	int i;
2297961SNatalie.Li@Sun.COM 
2307961SNatalie.Li@Sun.COM 	for (i = 0; legal_conv[i] != NULL; i++)
2317961SNatalie.Li@Sun.COM 		if (strcmp(charset, legal_conv[i]) == 0)
2327961SNatalie.Li@Sun.COM 			return (B_TRUE);
2337961SNatalie.Li@Sun.COM 	return (B_FALSE);
2347961SNatalie.Li@Sun.COM }
2357961SNatalie.Li@Sun.COM 
2367961SNatalie.Li@Sun.COM /*
2373910Sdougm  * findopt(name)
2383910Sdougm  *
2393910Sdougm  * Lookup option "name" in the option table and return the table
2403910Sdougm  * index.
2413910Sdougm  */
2423910Sdougm 
2433910Sdougm static int
2443910Sdougm findopt(char *name)
2453910Sdougm {
2463910Sdougm 	int i;
2473910Sdougm 	if (name != NULL) {
2484345Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
2494345Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
2504345Sdougm 				return (i);
2514345Sdougm 		}
2527961SNatalie.Li@Sun.COM 		if (findcharset(name))
2537961SNatalie.Li@Sun.COM 			return (OPT_CHARSET_MAP);
2543910Sdougm 	}
2553910Sdougm 	return (-1);
2563910Sdougm }
2573910Sdougm 
2583910Sdougm /*
2593910Sdougm  * gettype(name)
2603910Sdougm  *
2613910Sdougm  * Return the type of option "name".
2623910Sdougm  */
2633910Sdougm 
2643910Sdougm static int
2653910Sdougm gettype(char *name)
2663910Sdougm {
2673910Sdougm 	int optdef;
2683910Sdougm 
2693910Sdougm 	optdef = findopt(name);
2703910Sdougm 	if (optdef != -1)
2714345Sdougm 		return (optdefs[optdef].type);
2723910Sdougm 	return (OPT_TYPE_ANY);
2733910Sdougm }
2743910Sdougm 
2753910Sdougm /*
2763910Sdougm  * nfs_validate_security_mode(mode)
2773910Sdougm  *
2783910Sdougm  * is the specified mode string a valid one for use with NFS?
2793910Sdougm  */
2803910Sdougm 
2813910Sdougm static int
2823910Sdougm nfs_validate_security_mode(char *mode)
2833910Sdougm {
2843910Sdougm 	seconfig_t secinfo;
2853910Sdougm 	int err;
2863910Sdougm 
2873910Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2883910Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2893910Sdougm 	if (err == SC_NOERROR)
2904345Sdougm 		return (1);
2913910Sdougm 	return (0);
2923910Sdougm }
2933910Sdougm 
2943910Sdougm /*
2953910Sdougm  * nfs_is_security_opt(tok)
2963910Sdougm  *
2973910Sdougm  * check to see if tok represents an option that is only valid in some
2983910Sdougm  * security flavor.
2993910Sdougm  */
3003910Sdougm 
3013910Sdougm static int
3023910Sdougm nfs_is_security_opt(char *tok)
3033910Sdougm {
3043910Sdougm 	int i;
3053910Sdougm 
3063910Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
3074345Sdougm 		if (strcmp(tok, seclist[i]) == 0)
3084345Sdougm 			return (1);
3093910Sdougm 	}
3103910Sdougm 	return (0);
3113910Sdougm }
3123910Sdougm 
3133910Sdougm /*
3143910Sdougm  * find_security(seclist, sec)
3153910Sdougm  *
3163910Sdougm  * Walk the current list of security flavors and return true if it is
3173910Sdougm  * present, else return false.
3183910Sdougm  */
3193910Sdougm 
3203910Sdougm static int
3213910Sdougm find_security(struct securities *seclist, sa_security_t sec)
3223910Sdougm {
3233910Sdougm 	while (seclist != NULL) {
3244345Sdougm 		if (seclist->security == sec)
3254345Sdougm 			return (1);
3264345Sdougm 		seclist = seclist->next;
3273910Sdougm 	}
3283910Sdougm 	return (0);
3293910Sdougm }
3303910Sdougm 
3313910Sdougm /*
3323910Sdougm  * make_security_list(group, securitymodes, proto)
3333910Sdougm  *	go through the list of securitymodes and add them to the
3343910Sdougm  *	group's list of security optionsets. We also keep a list of
3353910Sdougm  *	those optionsets so we don't have to find them later. All of
3363910Sdougm  *	these will get copies of the same properties.
3373910Sdougm  */
3383910Sdougm 
3393910Sdougm static struct securities *
3403910Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
3413910Sdougm {
3423910Sdougm 	char *tok, *next = NULL;
3433910Sdougm 	struct securities *curp, *headp = NULL, *prev;
3443910Sdougm 	sa_security_t check;
3453910Sdougm 	int freetok = 0;
3463910Sdougm 
3473910Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
3484345Sdougm 		next = strchr(tok, ':');
3494345Sdougm 		if (next != NULL)
3504345Sdougm 			*next++ = '\0';
3514345Sdougm 		if (strcmp(tok, "default") == 0) {
3524345Sdougm 			/* resolve default into the real type */
3534345Sdougm 			tok = nfs_space_alias(tok);
3544345Sdougm 			freetok = 1;
3554345Sdougm 		}
3564345Sdougm 		check = sa_get_security(group, tok, proto);
3573910Sdougm 
3584345Sdougm 		/* add to the security list if it isn't there already */
3594345Sdougm 		if (check == NULL || !find_security(headp, check)) {
3604345Sdougm 			curp = (struct securities *)calloc(1,
3614345Sdougm 			    sizeof (struct securities));
3624345Sdougm 			if (curp != NULL) {
3634345Sdougm 				if (check == NULL) {
3644345Sdougm 					curp->security = sa_create_security(
3654345Sdougm 					    group, tok, proto);
3664345Sdougm 				} else {
3674345Sdougm 					curp->security = check;
3684345Sdougm 				}
3694345Sdougm 				/*
3704345Sdougm 				 * note that the first time through the loop,
3714345Sdougm 				 * headp will be NULL and prev will be
3724345Sdougm 				 * undefined.  Since headp is NULL, we set
3734345Sdougm 				 * both it and prev to the curp (first
3744345Sdougm 				 * structure to be allocated).
3754345Sdougm 				 *
3764345Sdougm 				 * later passes through the loop will have
3774345Sdougm 				 * headp not being NULL and prev will be used
3784345Sdougm 				 * to allocate at the end of the list.
3794345Sdougm 				 */
3804345Sdougm 				if (headp == NULL) {
3814345Sdougm 					headp = curp;
3824345Sdougm 					prev = curp;
3834345Sdougm 				} else {
3844345Sdougm 					prev->next = curp;
3854345Sdougm 					prev = curp;
3864345Sdougm 				}
3874345Sdougm 			}
3883910Sdougm 		}
3893910Sdougm 
3904345Sdougm 		if (freetok) {
3914345Sdougm 			freetok = 0;
3924345Sdougm 			sa_free_attr_string(tok);
3934345Sdougm 		}
3943910Sdougm 	}
3953910Sdougm 	return (headp);
3963910Sdougm }
3973910Sdougm 
3983910Sdougm static void
3993910Sdougm free_security_list(struct securities *sec)
4003910Sdougm {
4013910Sdougm 	struct securities *next;
4023910Sdougm 	if (sec != NULL) {
4034345Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
4044345Sdougm 			next = sec->next;
4054345Sdougm 			free(sec);
4064345Sdougm 		}
4073910Sdougm 	}
4083910Sdougm }
4093910Sdougm 
4103910Sdougm /*
4113910Sdougm  * nfs_alistcat(str1, str2, sep)
4123910Sdougm  *
4133910Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
4143910Sdougm  * character. If memory allocation fails, return NULL;
4153910Sdougm  */
4163910Sdougm 
4173910Sdougm static char *
4183910Sdougm nfs_alistcat(char *str1, char *str2, char sep)
4193910Sdougm {
4203910Sdougm 	char *newstr;
4213910Sdougm 	size_t len;
4223910Sdougm 
4233910Sdougm 	len = strlen(str1) + strlen(str2) + 2;
4243910Sdougm 	newstr = (char *)malloc(len);
4253910Sdougm 	if (newstr != NULL)
4264345Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
4273910Sdougm 	return (newstr);
4283910Sdougm }
4293910Sdougm 
4303910Sdougm /*
4313910Sdougm  * add_security_prop(sec, name, value, persist)
4323910Sdougm  *
4333910Sdougm  * Add the property to the securities structure. This accumulates
4343910Sdougm  * properties for as part of parsing legacy options.
4353910Sdougm  */
4363910Sdougm 
4373910Sdougm static int
4383910Sdougm add_security_prop(struct securities *sec, char *name, char *value,
4393910Sdougm 			int persist, int iszfs)
4403910Sdougm {
4413910Sdougm 	sa_property_t prop;
4423910Sdougm 	int ret = SA_OK;
4433910Sdougm 
4443910Sdougm 	for (; sec != NULL; sec = sec->next) {
4454345Sdougm 		if (value == NULL) {
4464345Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
4474345Sdougm 			    strcmp(name, SHOPT_RO) == 0)
4484345Sdougm 				value = "*";
4494345Sdougm 			else
4504345Sdougm 				value = "true";
4514345Sdougm 		}
4523910Sdougm 
4533910Sdougm 		/*
4543910Sdougm 		 * Get the existing property, if it exists, so we can
4553910Sdougm 		 * determine what to do with it. The ro/rw/root
4563910Sdougm 		 * properties can be merged if multiple instances of
4573910Sdougm 		 * these properies are given. For example, if "rw"
4583910Sdougm 		 * exists with a value "host1" and a later token of
4593910Sdougm 		 * rw="host2" is seen, the values are merged into a
4603910Sdougm 		 * single rw="host1:host2".
4613910Sdougm 		 */
4624345Sdougm 		prop = sa_get_property(sec->security, name);
4633910Sdougm 
4644345Sdougm 		if (prop != NULL) {
4654345Sdougm 			char *oldvalue;
4664345Sdougm 			char *newvalue;
4673910Sdougm 
4683910Sdougm 			/*
4694345Sdougm 			 * The security options of ro/rw/root might appear
4704345Sdougm 			 * multiple times. If they do, the values need to be
4714345Sdougm 			 * merged into an access list. If it was previously
4724345Sdougm 			 * empty, the new value alone is added.
4733910Sdougm 			 */
4744345Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
4754345Sdougm 			if (oldvalue != NULL) {
4764345Sdougm 				/*
4774345Sdougm 				 * The general case is to concatenate the new
4784345Sdougm 				 * value onto the old value for multiple
4794345Sdougm 				 * rw(ro/root) properties. A special case
4804345Sdougm 				 * exists when either the old or new is the
4814345Sdougm 				 * "all" case. In the special case, if both
4824345Sdougm 				 * are "all", then it is "all", else if one is
4834345Sdougm 				 * an access-list, that replaces the "all".
4844345Sdougm 				 */
4854345Sdougm 				if (strcmp(oldvalue, "*") == 0) {
4864345Sdougm 					/* Replace old value with new value. */
4874345Sdougm 					newvalue = strdup(value);
4885454Sdougm 				} else if (strcmp(value, "*") == 0 ||
4895454Sdougm 				    strcmp(oldvalue, value) == 0) {
4904345Sdougm 					/*
4914345Sdougm 					 * Keep old value and ignore
4924345Sdougm 					 * the new value.
4934345Sdougm 					 */
4944345Sdougm 					newvalue = NULL;
4954345Sdougm 				} else {
4964345Sdougm 					/*
4974345Sdougm 					 * Make a new list of old plus new
4984345Sdougm 					 * access-list.
4994345Sdougm 					 */
5004345Sdougm 					newvalue = nfs_alistcat(oldvalue,
5014345Sdougm 					    value, ':');
5024345Sdougm 				}
5033910Sdougm 
5044345Sdougm 				if (newvalue != NULL) {
5054345Sdougm 					(void) sa_remove_property(prop);
5064345Sdougm 					prop = sa_create_property(name,
5074345Sdougm 					    newvalue);
5084345Sdougm 					ret = sa_add_property(sec->security,
5094345Sdougm 					    prop);
5104345Sdougm 					free(newvalue);
5114345Sdougm 				}
5124345Sdougm 				if (oldvalue != NULL)
5134345Sdougm 					sa_free_attr_string(oldvalue);
5144345Sdougm 			}
5154345Sdougm 		} else {
5164345Sdougm 			prop = sa_create_property(name, value);
5173910Sdougm 			ret = sa_add_property(sec->security, prop);
5183910Sdougm 		}
5194345Sdougm 		if (ret == SA_OK && !iszfs) {
5204345Sdougm 			ret = sa_commit_properties(sec->security, !persist);
5214345Sdougm 		}
5223910Sdougm 	}
5233910Sdougm 	return (ret);
5243910Sdougm }
5253910Sdougm 
5263910Sdougm /*
5273910Sdougm  * check to see if group/share is persistent.
5283910Sdougm  */
5293910Sdougm static int
5303910Sdougm is_persistent(sa_group_t group)
5313910Sdougm {
5323910Sdougm 	char *type;
5333910Sdougm 	int persist = 1;
5343910Sdougm 
5353910Sdougm 	type = sa_get_group_attr(group, "type");
5363910Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
5374345Sdougm 		persist = 0;
5383910Sdougm 	if (type != NULL)
5394345Sdougm 		sa_free_attr_string(type);
5403910Sdougm 	return (persist);
5413910Sdougm }
5423910Sdougm 
5433910Sdougm /*
5443910Sdougm  * invalid_security(options)
5453910Sdougm  *
5463910Sdougm  * search option string for any invalid sec= type.
5473910Sdougm  * return true (1) if any are not valid else false (0)
5483910Sdougm  */
5493910Sdougm static int
5503910Sdougm invalid_security(char *options)
5513910Sdougm {
5523910Sdougm 	char *copy, *base, *token, *value;
5533910Sdougm 	int ret = 0;
5543910Sdougm 
5553910Sdougm 	copy = strdup(options);
5563910Sdougm 	token = base = copy;
5573910Sdougm 	while (token != NULL && ret == 0) {
5584345Sdougm 		token = strtok(base, ",");
5594345Sdougm 		base = NULL;
5604345Sdougm 		if (token != NULL) {
5614345Sdougm 			value = strchr(token, '=');
5624345Sdougm 			if (value != NULL)
5634345Sdougm 				*value++ = '\0';
5644345Sdougm 			if (strcmp(token, "sec") == 0) {
5654345Sdougm 				/* HAVE security flavors so check them */
5664345Sdougm 				char *tok, *next;
5674345Sdougm 				for (next = NULL, tok = value; tok != NULL;
5684345Sdougm 				    tok = next) {
5694345Sdougm 					next = strchr(tok, ':');
5704345Sdougm 					if (next != NULL)
5714345Sdougm 						*next++ = '\0';
5724345Sdougm 					ret = !nfs_validate_security_mode(tok);
5734345Sdougm 					if (ret)
5744345Sdougm 						break;
5754345Sdougm 				}
5764345Sdougm 			}
5773910Sdougm 		}
5783910Sdougm 	}
5793910Sdougm 	if (copy != NULL)
5804345Sdougm 		free(copy);
5813910Sdougm 	return (ret);
5823910Sdougm }
5833910Sdougm 
5843910Sdougm /*
5853910Sdougm  * nfs_parse_legacy_options(group, options)
5863910Sdougm  *
5873910Sdougm  * Parse the old style options into internal format and store on the
5883910Sdougm  * specified group.  Group could be a share for full legacy support.
5893910Sdougm  */
5903910Sdougm 
5913910Sdougm static int
5923910Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5933910Sdougm {
5944704Sdougm 	char *dup;
5953910Sdougm 	char *base;
5963910Sdougm 	char *token;
5973910Sdougm 	sa_optionset_t optionset;
5983910Sdougm 	struct securities *security_list = NULL;
5993910Sdougm 	sa_property_t prop;
6003910Sdougm 	int ret = SA_OK;
6013910Sdougm 	int iszfs = 0;
6023910Sdougm 	sa_group_t parent;
6033910Sdougm 	int persist = 0;
6043910Sdougm 	char *lasts;
6053910Sdougm 
6063910Sdougm 	/* do we have an existing optionset? */
6073910Sdougm 	optionset = sa_get_optionset(group, "nfs");
6083910Sdougm 	if (optionset == NULL) {
6094345Sdougm 		/* didn't find existing optionset so create one */
6104345Sdougm 		optionset = sa_create_optionset(group, "nfs");
6113910Sdougm 	} else {
6123910Sdougm 		/*
6135331Samw 		 * Have an existing optionset . Ideally, we would need
6145331Samw 		 * to compare options in order to detect errors. For
6155331Samw 		 * now, we assume that the first optionset is the
6165331Samw 		 * correct one and the others will be the same. An
6175331Samw 		 * empty optionset is the same as no optionset so we
6185331Samw 		 * don't want to exit in that case. Getting an empty
6195331Samw 		 * optionset can occur with ZFS property checking.
6203910Sdougm 		 */
6215331Samw 		if (sa_get_property(optionset, NULL) != NULL)
6225331Samw 			return (ret);
6233910Sdougm 	}
6243910Sdougm 
6253910Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
6263910Sdougm 		/*
6273910Sdougm 		 * there is a special case of only the option "rw"
6283910Sdougm 		 * being the default option. We don't have to do
6293910Sdougm 		 * anything.
6303910Sdougm 		 */
6314345Sdougm 		return (ret);
6323910Sdougm 	}
6333910Sdougm 
6343910Sdougm 	/*
6353910Sdougm 	 * check if security types are present and validate them. If
6363910Sdougm 	 * any are not legal, fail.
6373910Sdougm 	 */
6383910Sdougm 
6393910Sdougm 	if (invalid_security(options)) {
6404345Sdougm 		return (SA_INVALID_SECURITY);
6413910Sdougm 	}
6423910Sdougm 
6433910Sdougm 	/*
6443910Sdougm 	 * in order to not attempt to change ZFS properties unless
6453910Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
6463910Sdougm 	 */
6473910Sdougm 	if (sa_is_share(group)) {
6484345Sdougm 		char *zfs;
6494345Sdougm 		parent = sa_get_parent_group(group);
6504345Sdougm 		if (parent != NULL) {
6514345Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
6524345Sdougm 			if (zfs != NULL) {
6534345Sdougm 				sa_free_attr_string(zfs);
6544345Sdougm 				iszfs++;
6554345Sdougm 			}
6563910Sdougm 		}
6573910Sdougm 	} else {
6584345Sdougm 		iszfs = sa_group_is_zfs(group);
6593910Sdougm 	}
6603910Sdougm 
6614704Sdougm 	/* We need a copy of options for the next part. */
6624704Sdougm 	dup = strdup(options);
6634704Sdougm 	if (dup == NULL)
6644704Sdougm 		return (SA_NO_MEMORY);
6654704Sdougm 
6663910Sdougm 	/*
6673910Sdougm 	 * we need to step through each option in the string and then
6683910Sdougm 	 * add either the option or the security option as needed. If
6693910Sdougm 	 * this is not a persistent share, don't commit to the
6703910Sdougm 	 * repository. If there is an error, we also want to abort the
6713910Sdougm 	 * processing and report it.
6723910Sdougm 	 */
6733910Sdougm 	persist = is_persistent(group);
6743910Sdougm 	base = dup;
6753910Sdougm 	token = dup;
6763910Sdougm 	lasts = NULL;
6773910Sdougm 	while (token != NULL && ret == SA_OK) {
6784345Sdougm 		ret = SA_OK;
6794345Sdougm 		token = strtok_r(base, ",", &lasts);
6804345Sdougm 		base = NULL;
6814345Sdougm 		if (token != NULL) {
6824345Sdougm 			char *value;
6833910Sdougm 			/*
6844345Sdougm 			 * if the option has a value, it will have an '=' to
6854345Sdougm 			 * separate the name from the value. The following
6864345Sdougm 			 * code will result in value != NULL and token
6874345Sdougm 			 * pointing to just the name if there is a value.
6883910Sdougm 			 */
6894345Sdougm 			value = strchr(token, '=');
6904345Sdougm 			if (value != NULL) {
6914345Sdougm 				*value++ = '\0';
6924345Sdougm 			}
6934345Sdougm 			if (strcmp(token, "sec") == 0 ||
6944345Sdougm 			    strcmp(token, "secure") == 0) {
6953910Sdougm 				/*
6964345Sdougm 				 * Once in security parsing, we only
6974345Sdougm 				 * do security. We do need to move
6984345Sdougm 				 * between the security node and the
6994345Sdougm 				 * toplevel. The security tag goes on
7004345Sdougm 				 * the root while the following ones
7014345Sdougm 				 * go on the security.
7023910Sdougm 				 */
7034345Sdougm 				if (security_list != NULL) {
7044345Sdougm 					/*
7054345Sdougm 					 * have an old list so close it and
7064345Sdougm 					 * start the new
7074345Sdougm 					 */
7084345Sdougm 					free_security_list(security_list);
7094345Sdougm 				}
7104345Sdougm 				if (strcmp(token, "secure") == 0) {
7114345Sdougm 					value = "dh";
7124345Sdougm 				} else {
7134345Sdougm 					if (value == NULL) {
7144345Sdougm 						ret = SA_SYNTAX_ERR;
7154345Sdougm 						break;
7164345Sdougm 					}
7174345Sdougm 				}
7184345Sdougm 				security_list = make_security_list(group,
7194345Sdougm 				    value, "nfs");
7203910Sdougm 			} else {
7214345Sdougm 				/*
7224345Sdougm 				 * Note that the "old" syntax allowed a
7234345Sdougm 				 * default security model This must be
7244345Sdougm 				 * accounted for and internally converted to
7254345Sdougm 				 * "standard" security structure.
7264345Sdougm 				 */
7274345Sdougm 				if (nfs_is_security_opt(token)) {
7284345Sdougm 					if (security_list == NULL) {
7294345Sdougm 						/*
7304345Sdougm 						 * need to have a
7314345Sdougm 						 * security
7324345Sdougm 						 * option. This will
7334345Sdougm 						 * be "closed" when a
7344345Sdougm 						 * defined "sec="
7354345Sdougm 						 * option is
7364345Sdougm 						 * seen. This is
7374345Sdougm 						 * technically an
7384345Sdougm 						 * error but will be
7394345Sdougm 						 * allowed with
7404345Sdougm 						 * warning.
7414345Sdougm 						 */
7424345Sdougm 						security_list =
7434345Sdougm 						    make_security_list(group,
7444345Sdougm 						    "default",
7454345Sdougm 						    "nfs");
7464345Sdougm 					}
7474345Sdougm 					if (security_list != NULL) {
7484345Sdougm 						ret = add_security_prop(
7494345Sdougm 						    security_list, token,
7504345Sdougm 						    value, persist, iszfs);
7514345Sdougm 					} else {
7524345Sdougm 						ret = SA_NO_MEMORY;
7534345Sdougm 					}
7544345Sdougm 				} else {
7554345Sdougm 					/* regular options */
7564345Sdougm 					if (value == NULL) {
7574345Sdougm 						if (strcmp(token, SHOPT_RW) ==
7584345Sdougm 						    0 || strcmp(token,
7594345Sdougm 						    SHOPT_RO) == 0) {
7604345Sdougm 							value = "*";
7614345Sdougm 						} else {
7624345Sdougm 							value = "global";
7634345Sdougm 							if (strcmp(token,
7644345Sdougm 							    SHOPT_LOG) != 0) {
7654345Sdougm 								value = "true";
7664345Sdougm 							}
7674345Sdougm 						}
7684345Sdougm 					}
7694372Sdougm 					/*
7704372Sdougm 					 * In all cases, create the
7714372Sdougm 					 * property specified. If the
7724372Sdougm 					 * value was NULL, the default
7734372Sdougm 					 * value will have been
7744372Sdougm 					 * substituted.
7754372Sdougm 					 */
7764372Sdougm 					prop = sa_create_property(token, value);
7774372Sdougm 					ret =  sa_add_property(optionset, prop);
7784372Sdougm 					if (ret != SA_OK)
7794372Sdougm 						break;
7804372Sdougm 
7814345Sdougm 					if (!iszfs) {
7824345Sdougm 						ret = sa_commit_properties(
7834345Sdougm 						    optionset, !persist);
7844345Sdougm 					}
7854345Sdougm 				}
7863910Sdougm 			}
7873910Sdougm 		}
7883910Sdougm 	}
7893910Sdougm 	if (security_list != NULL)
7904345Sdougm 		free_security_list(security_list);
7914704Sdougm 
7924704Sdougm 	free(dup);
7933910Sdougm 	return (ret);
7943910Sdougm }
7953910Sdougm 
7963910Sdougm /*
7973910Sdougm  * is_a_number(number)
7983910Sdougm  *
7993910Sdougm  * is the string a number in one of the forms we want to use?
8003910Sdougm  */
8013910Sdougm 
8023910Sdougm static int
8033910Sdougm is_a_number(char *number)
8043910Sdougm {
8053910Sdougm 	int ret = 1;
8063910Sdougm 	int hex = 0;
8073910Sdougm 
8083910Sdougm 	if (strncmp(number, "0x", 2) == 0) {
8094345Sdougm 		number += 2;
8104345Sdougm 		hex = 1;
8114345Sdougm 	} else if (*number == '-') {
8124345Sdougm 		number++; /* skip the minus */
8134345Sdougm 	}
8143910Sdougm 	while (ret == 1 && *number != '\0') {
8154345Sdougm 		if (hex) {
8164345Sdougm 			ret = isxdigit(*number++);
8174345Sdougm 		} else {
8184345Sdougm 			ret = isdigit(*number++);
8194345Sdougm 		}
8203910Sdougm 	}
8213910Sdougm 	return (ret);
8223910Sdougm }
8233910Sdougm 
8243910Sdougm /*
8253910Sdougm  * Look for the specified tag in the configuration file. If it is found,
8263910Sdougm  * enable logging and set the logging configuration information for exp.
8273910Sdougm  */
8283910Sdougm static void
8293910Sdougm configlog(struct exportdata *exp, char *tag)
8303910Sdougm {
8313910Sdougm 	nfsl_config_t *configlist = NULL, *configp;
8323910Sdougm 	int error = 0;
8333910Sdougm 	char globaltag[] = DEFAULTTAG;
8343910Sdougm 
8353910Sdougm 	/*
8363910Sdougm 	 * Sends config errors to stderr
8373910Sdougm 	 */
8383910Sdougm 	nfsl_errs_to_syslog = B_FALSE;
8393910Sdougm 
8403910Sdougm 	/*
8413910Sdougm 	 * get the list of configuration settings
8423910Sdougm 	 */
8433910Sdougm 	error = nfsl_getconfig_list(&configlist);
8443910Sdougm 	if (error) {
8453910Sdougm 		(void) fprintf(stderr,
8464345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
8474345Sdougm 		    strerror(error));
8483910Sdougm 	}
8493910Sdougm 
8503910Sdougm 	if (tag == NULL)
8513910Sdougm 		tag = globaltag;
8523910Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
8533910Sdougm 		nfsl_freeconfig_list(&configlist);
8543910Sdougm 		(void) fprintf(stderr,
8554345Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
8563910Sdougm 		/* bad configuration */
8573910Sdougm 		error = ENOENT;
8583910Sdougm 		goto err;
8593910Sdougm 	}
8603910Sdougm 
8613910Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
8623910Sdougm 		error = ENOMEM;
8633910Sdougm 		goto out;
8643910Sdougm 	}
8653910Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
8663910Sdougm 		error = ENOMEM;
8673910Sdougm 		goto out;
8683910Sdougm 	}
8693910Sdougm 	exp->ex_flags |= EX_LOG;
8703910Sdougm 	if (configp->nc_rpclogpath != NULL)
8713910Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8723910Sdougm out:
8733910Sdougm 	if (configlist != NULL)
8744345Sdougm 		nfsl_freeconfig_list(&configlist);
8753910Sdougm 
8763910Sdougm err:
8773910Sdougm 	if (error != 0) {
8783910Sdougm 		if (exp->ex_flags != NULL)
8793910Sdougm 			free(exp->ex_tag);
8803910Sdougm 		if (exp->ex_log_buffer != NULL)
8813910Sdougm 			free(exp->ex_log_buffer);
8823910Sdougm 		(void) fprintf(stderr,
8834345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
8844345Sdougm 		    strerror(error));
8853910Sdougm 	}
8863910Sdougm }
8873910Sdougm 
8883910Sdougm /*
8893910Sdougm  * fill_export_from_optionset(export, optionset)
8903910Sdougm  *
8913910Sdougm  * In order to share, we need to set all the possible general options
8923910Sdougm  * into the export structure. Share info will be filled in by the
8933910Sdougm  * caller. Various property values get turned into structure specific
8943910Sdougm  * values.
8953910Sdougm  */
8963910Sdougm 
8973910Sdougm static int
8983910Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
8993910Sdougm {
9003910Sdougm 	sa_property_t option;
9013910Sdougm 	int ret = SA_OK;
9023910Sdougm 
9033910Sdougm 	for (option = sa_get_property(optionset, NULL);
9044345Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
9054345Sdougm 		char *name;
9064345Sdougm 		char *value;
9074345Sdougm 		uint32_t val;
9083910Sdougm 
9094345Sdougm 		/*
9104345Sdougm 		 * since options may be set/reset multiple times, always do an
9114345Sdougm 		 * explicit set or clear of the option. This allows defaults
9125331Samw 		 * to be set and then the protocol specific to override.
9134345Sdougm 		 */
9143910Sdougm 
9154345Sdougm 		name = sa_get_property_attr(option, "type");
9164345Sdougm 		value = sa_get_property_attr(option, "value");
9174345Sdougm 		switch (findopt(name)) {
9184345Sdougm 		case OPT_ANON:
9194345Sdougm 			if (value != NULL && is_a_number(value)) {
9204345Sdougm 				val = strtoul(value, NULL, 0);
9214345Sdougm 			} else {
9224345Sdougm 				struct passwd *pw;
9234345Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
9244345Sdougm 				if (pw != NULL) {
9254345Sdougm 					val = pw->pw_uid;
9264345Sdougm 				} else {
9274345Sdougm 					val = UID_NOBODY;
9284345Sdougm 				}
9294345Sdougm 				endpwent();
9304345Sdougm 			}
9314345Sdougm 			export->ex_anon = val;
9324345Sdougm 			break;
9334345Sdougm 		case OPT_NOSUID:
9344345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9354345Sdougm 			    strcmp(value, "1") == 0))
9364345Sdougm 				export->ex_flags |= EX_NOSUID;
9374345Sdougm 			else
9384345Sdougm 				export->ex_flags &= ~EX_NOSUID;
9394345Sdougm 			break;
9404345Sdougm 		case OPT_ACLOK:
9414345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9424345Sdougm 			    strcmp(value, "1") == 0))
9434345Sdougm 				export->ex_flags |= EX_ACLOK;
9444345Sdougm 			else
9454345Sdougm 				export->ex_flags &= ~EX_ACLOK;
9464345Sdougm 			break;
9474345Sdougm 		case OPT_NOSUB:
9484345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9494345Sdougm 			    strcmp(value, "1") == 0))
9504345Sdougm 				export->ex_flags |= EX_NOSUB;
9514345Sdougm 			else
9524345Sdougm 				export->ex_flags &= ~EX_NOSUB;
9534345Sdougm 			break;
9544345Sdougm 		case OPT_PUBLIC:
9554345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9564345Sdougm 			    strcmp(value, "1") == 0))
9574345Sdougm 				export->ex_flags |= EX_PUBLIC;
9584345Sdougm 			else
9594345Sdougm 				export->ex_flags &= ~EX_PUBLIC;
9604345Sdougm 			break;
9614345Sdougm 		case OPT_INDEX:
9624345Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
9634345Sdougm 			    strchr(value, '/') != NULL)) {
9644345Sdougm 				/* this is an error */
9654345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
9664345Sdougm 				    "NFS: index=\"%s\" not valid;"
9674345Sdougm 				    "must be a filename.\n"),
9684345Sdougm 				    value);
9694345Sdougm 				break;
9704345Sdougm 			}
9714345Sdougm 			if (value != NULL && *value != '\0' &&
9724345Sdougm 			    strcmp(value, ".") != 0) {
9734345Sdougm 				/* valid index file string */
9744345Sdougm 				if (export->ex_index != NULL) {
9754345Sdougm 					/* left over from "default" */
9764345Sdougm 					free(export->ex_index);
9774345Sdougm 				}
9784345Sdougm 				/* remember to free */
9794345Sdougm 				export->ex_index = strdup(value);
9804345Sdougm 				if (export->ex_index == NULL) {
9814345Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
9824345Sdougm 					    "NFS: out of memory setting "
9834345Sdougm 					    "index property\n"));
9844345Sdougm 					break;
9854345Sdougm 				}
9864345Sdougm 				export->ex_flags |= EX_INDEX;
9874345Sdougm 			}
9884345Sdougm 			break;
9894345Sdougm 		case OPT_LOG:
9904345Sdougm 			if (value == NULL)
9914345Sdougm 				value = strdup("global");
9924345Sdougm 			if (value != NULL)
9934345Sdougm 				configlog(export,
9944345Sdougm 				    strlen(value) ? value : "global");
9954345Sdougm 			break;
9967961SNatalie.Li@Sun.COM 		case OPT_CHARSET_MAP:
9977961SNatalie.Li@Sun.COM 			/*
9987961SNatalie.Li@Sun.COM 			 * Set EX_CHARMAP when there is at least one
9997961SNatalie.Li@Sun.COM 			 * charmap conversion property. This will get
10007961SNatalie.Li@Sun.COM 			 * checked by the nfs server when it needs to.
10017961SNatalie.Li@Sun.COM 			 */
10027961SNatalie.Li@Sun.COM 			export->ex_flags |= EX_CHARMAP;
10037961SNatalie.Li@Sun.COM 			break;
10044345Sdougm 		default:
10054345Sdougm 			/* have a syntactic error */
10064345Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
10074345Sdougm 			    "NFS: unrecognized option %s=%s\n"),
10084345Sdougm 			    name, value != NULL ? value : "");
10094345Sdougm 			break;
10103910Sdougm 		}
10114345Sdougm 		if (name != NULL)
10124345Sdougm 			sa_free_attr_string(name);
10133910Sdougm 		if (value != NULL)
10144345Sdougm 			sa_free_attr_string(value);
10153910Sdougm 	}
10163910Sdougm 	return (ret);
10173910Sdougm }
10183910Sdougm 
10193910Sdougm /*
10203910Sdougm  * cleanup_export(export)
10213910Sdougm  *
10223910Sdougm  * Cleanup the allocated areas so we don't leak memory
10233910Sdougm  */
10243910Sdougm 
10253910Sdougm static void
10263910Sdougm cleanup_export(struct exportdata *export)
10273910Sdougm {
10283910Sdougm 	int i;
10293910Sdougm 
10303910Sdougm 	if (export->ex_index != NULL)
10314345Sdougm 		free(export->ex_index);
10323910Sdougm 	if (export->ex_secinfo != NULL) {
10334345Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
10344345Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
10354345Sdougm 				free(export->ex_secinfo[i].s_rootnames);
10364345Sdougm 		free(export->ex_secinfo);
10373910Sdougm 	}
10383910Sdougm }
10393910Sdougm 
10403910Sdougm /*
10413910Sdougm  * Given a seconfig entry and a colon-separated
10423910Sdougm  * list of names, allocate an array big enough
10433910Sdougm  * to hold the root list, then convert each name to
10443910Sdougm  * a principal name according to the security
10453910Sdougm  * info and assign it to an array element.
10463910Sdougm  * Return the array and its size.
10473910Sdougm  */
10483910Sdougm static caddr_t *
10493910Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
10503910Sdougm {
10513910Sdougm 	caddr_t *a;
10523910Sdougm 	int c, i;
10533910Sdougm 	char *host, *p;
10543910Sdougm 
10553910Sdougm 	/*
10563910Sdougm 	 * Count the number of strings in the list.
10573910Sdougm 	 * This is the number of colon separators + 1.
10583910Sdougm 	 */
10593910Sdougm 	c = 1;
10603910Sdougm 	for (p = list; *p; p++)
10613910Sdougm 		if (*p == ':')
10623910Sdougm 			c++;
10633910Sdougm 	*count = c;
10643910Sdougm 
10653910Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
10663910Sdougm 	if (a == NULL) {
10673910Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
10684345Sdougm 		    "get_rootnames: no memory\n"));
10693910Sdougm 	} else {
10704345Sdougm 		for (i = 0; i < c; i++) {
10714345Sdougm 			host = strtok(list, ":");
10724345Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
10734345Sdougm 				free(a);
10744345Sdougm 				a = NULL;
10754345Sdougm 				break;
10764345Sdougm 			}
10774345Sdougm 			list = NULL;
10783910Sdougm 		}
10793910Sdougm 	}
10803910Sdougm 
10813910Sdougm 	return (a);
10823910Sdougm }
10833910Sdougm 
10843910Sdougm /*
10853910Sdougm  * fill_security_from_secopts(sp, secopts)
10863910Sdougm  *
10873910Sdougm  * Fill the secinfo structure from the secopts optionset.
10883910Sdougm  */
10893910Sdougm 
10903910Sdougm static int
10913910Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
10923910Sdougm {
10933910Sdougm 	sa_property_t prop;
10943910Sdougm 	char *type;
10953910Sdougm 	int longform;
10963910Sdougm 	int err = SC_NOERROR;
10977961SNatalie.Li@Sun.COM 	uint32_t val;
10983910Sdougm 
10993910Sdougm 	type = sa_get_security_attr(secopts, "sectype");
11003910Sdougm 	if (type != NULL) {
11014345Sdougm 		/* named security type needs secinfo to be filled in */
11024345Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
11034345Sdougm 		sa_free_attr_string(type);
11044345Sdougm 		if (err != SC_NOERROR)
11054345Sdougm 			return (err);
11063910Sdougm 	} else {
11074345Sdougm 		/* default case */
11084345Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
11094345Sdougm 		if (err != SC_NOERROR)
11104345Sdougm 			return (err);
11113910Sdougm 	}
11123910Sdougm 
11133910Sdougm 	err = SA_OK;
11143910Sdougm 	for (prop = sa_get_property(secopts, NULL);
11154345Sdougm 	    prop != NULL && err == SA_OK;
11164345Sdougm 	    prop = sa_get_next_property(prop)) {
11174345Sdougm 		char *name;
11184345Sdougm 		char *value;
11193910Sdougm 
11204345Sdougm 		name = sa_get_property_attr(prop, "type");
11214345Sdougm 		value = sa_get_property_attr(prop, "value");
11223910Sdougm 
11234345Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
11243910Sdougm 
11254345Sdougm 		switch (findopt(name)) {
11264345Sdougm 		case OPT_RO:
11274345Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
11284345Sdougm 			break;
11294345Sdougm 		case OPT_RW:
11304345Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
11314345Sdougm 			break;
11324345Sdougm 		case OPT_ROOT:
11334345Sdougm 			sp->s_flags |= M_ROOT;
11344345Sdougm 			/*
11354345Sdougm 			 * if we are using AUTH_UNIX, handle like other things
11364345Sdougm 			 * such as RO/RW
11374345Sdougm 			 */
11384345Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
11394345Sdougm 				continue;
11404345Sdougm 			/* not AUTH_UNIX */
11414345Sdougm 			if (value != NULL) {
11424345Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
11434345Sdougm 				    value, &sp->s_rootcnt);
11444345Sdougm 				if (sp->s_rootnames == NULL) {
11454345Sdougm 					err = SA_BAD_VALUE;
11464345Sdougm 					(void) fprintf(stderr,
11474345Sdougm 					    dgettext(TEXT_DOMAIN,
11484345Sdougm 					    "Bad root list\n"));
11494345Sdougm 				}
11504345Sdougm 			}
11514345Sdougm 			break;
11527961SNatalie.Li@Sun.COM 		case OPT_NONE:
11537961SNatalie.Li@Sun.COM 			sp->s_flags |= M_NONE;
11547961SNatalie.Li@Sun.COM 			break;
11554345Sdougm 		case OPT_WINDOW:
11564345Sdougm 			if (value != NULL) {
11574345Sdougm 				sp->s_window = atoi(value);
11584345Sdougm 				/* just in case */
11594345Sdougm 				if (sp->s_window < 0)
11604345Sdougm 					sp->s_window = DEF_WIN;
11614345Sdougm 			}
11624345Sdougm 			break;
11637961SNatalie.Li@Sun.COM 		case OPT_ROOT_MAPPING:
11647961SNatalie.Li@Sun.COM 			if (value != NULL && is_a_number(value)) {
11657961SNatalie.Li@Sun.COM 				val = strtoul(value, NULL, 0);
11667961SNatalie.Li@Sun.COM 			} else {
11677961SNatalie.Li@Sun.COM 				struct passwd *pw;
11687961SNatalie.Li@Sun.COM 				pw = getpwnam(value != NULL ? value : "nobody");
11697961SNatalie.Li@Sun.COM 				if (pw != NULL) {
11707961SNatalie.Li@Sun.COM 					val = pw->pw_uid;
11717961SNatalie.Li@Sun.COM 				} else {
11727961SNatalie.Li@Sun.COM 					val = UID_NOBODY;
11737961SNatalie.Li@Sun.COM 				}
11747961SNatalie.Li@Sun.COM 				endpwent();
11757961SNatalie.Li@Sun.COM 			}
11767961SNatalie.Li@Sun.COM 			sp->s_rootid = val;
11777961SNatalie.Li@Sun.COM 			break;
11784345Sdougm 		default:
11794345Sdougm 			break;
11803910Sdougm 		}
11814345Sdougm 		if (name != NULL)
11824345Sdougm 			sa_free_attr_string(name);
11834345Sdougm 		if (value != NULL)
11844345Sdougm 			sa_free_attr_string(value);
11853910Sdougm 	}
11863910Sdougm 	/* if rw/ro options not set, use default of RW */
11873910Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
11884345Sdougm 		sp->s_flags |= M_RW;
11893910Sdougm 	return (err);
11903910Sdougm }
11913910Sdougm 
11923910Sdougm /*
11933910Sdougm  * This is for testing only
11943910Sdougm  * It displays the export structure that
11953910Sdougm  * goes into the kernel.
11963910Sdougm  */
11973910Sdougm static void
11983910Sdougm printarg(char *path, struct exportdata *ep)
11993910Sdougm {
12003910Sdougm 	int i, j;
12013910Sdougm 	struct secinfo *sp;
12023910Sdougm 
12033910Sdougm 	if (debug == 0)
12044345Sdougm 		return;
12053910Sdougm 
12063910Sdougm 	(void) printf("%s:\n", path);
12073910Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
12083910Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
12093910Sdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
12103910Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
12113910Sdougm 	if (ep->ex_flags & EX_NOSUID)
12123910Sdougm 		(void) printf("NOSUID ");
12133910Sdougm 	if (ep->ex_flags & EX_ACLOK)
12143910Sdougm 		(void) printf("ACLOK ");
12153910Sdougm 	if (ep->ex_flags & EX_PUBLIC)
12163910Sdougm 		(void) printf("PUBLIC ");
12173910Sdougm 	if (ep->ex_flags & EX_NOSUB)
12183910Sdougm 		(void) printf("NOSUB ");
12193910Sdougm 	if (ep->ex_flags & EX_LOG)
12203910Sdougm 		(void) printf("LOG ");
12217961SNatalie.Li@Sun.COM 	if (ep->ex_flags & EX_CHARMAP)
12227961SNatalie.Li@Sun.COM 		(void) printf("CHARMAP ");
12233910Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
12243910Sdougm 		(void) printf("LOG_ALLOPS ");
12253910Sdougm 	if (ep->ex_flags == 0)
12263910Sdougm 		(void) printf("(none)");
12273910Sdougm 	(void) 	printf("\n");
12283910Sdougm 	if (ep->ex_flags & EX_LOG) {
12293910Sdougm 		(void) printf("\tex_log_buffer = %s\n",
12304345Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
12313910Sdougm 		(void) printf("\tex_tag = %s\n",
12324345Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
12333910Sdougm 	}
12343910Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
12353910Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
12363910Sdougm 	(void) printf("\n");
12373910Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
12383910Sdougm 		sp = &ep->ex_secinfo[i];
12393910Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
12403910Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
12413910Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
12423910Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
12433910Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
12443910Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
12453910Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
12467961SNatalie.Li@Sun.COM 		if (sp->s_flags & M_NONE) (void) printf("M_NONE ");
12473910Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
12483910Sdougm 		(void) printf("\n");
12493910Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
12507961SNatalie.Li@Sun.COM 		(void) printf("\t\ts_rootid = %d\n", sp->s_rootid);
12513910Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
12523910Sdougm 		(void) fflush(stdout);
12533910Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
12543910Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
12554345Sdougm 			    sp->s_rootnames[j] : "<null>");
12563910Sdougm 		(void) printf("\n\n");
12573910Sdougm 	}
12583910Sdougm }
12593910Sdougm 
12603910Sdougm /*
12613910Sdougm  * count_security(opts)
12623910Sdougm  *
12633910Sdougm  * Count the number of security types (flavors). The optionset has
12643910Sdougm  * been populated with the security flavors as a holding mechanism.
12653910Sdougm  * We later use this number to allocate data structures.
12663910Sdougm  */
12673910Sdougm 
12683910Sdougm static int
12693910Sdougm count_security(sa_optionset_t opts)
12703910Sdougm {
12713910Sdougm 	int count = 0;
12723910Sdougm 	sa_property_t prop;
12733910Sdougm 	if (opts != NULL) {
12744345Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
12754345Sdougm 		    prop = sa_get_next_property(prop)) {
12764345Sdougm 			count++;
12774345Sdougm 		}
12783910Sdougm 	}
12793910Sdougm 	return (count);
12803910Sdougm }
12813910Sdougm 
12823910Sdougm /*
12833910Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
12843910Sdougm  *
12853910Sdougm  * provides a mechanism to format NFS properties into legacy output
12863910Sdougm  * format. If the buffer would overflow, it is reallocated and grown
12873910Sdougm  * as appropriate. Special cases of converting internal form of values
12883910Sdougm  * to those used by "share" are done. this function does one property
12893910Sdougm  * at a time.
12903910Sdougm  */
12913910Sdougm 
12925179Sdougm static int
12933910Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
12943910Sdougm 			sa_property_t prop, int sep)
12953910Sdougm {
12963910Sdougm 	char *name;
12973910Sdougm 	char *value;
12983910Sdougm 	int curlen;
12993910Sdougm 	char *buff = *rbuff;
13003910Sdougm 	size_t buffsize = *rbuffsize;
13015179Sdougm 	int printed = B_FALSE;
13023910Sdougm 
13033910Sdougm 	name = sa_get_property_attr(prop, "type");
13043910Sdougm 	value = sa_get_property_attr(prop, "value");
13053910Sdougm 	if (buff != NULL)
13064345Sdougm 		curlen = strlen(buff);
13073910Sdougm 	else
13084345Sdougm 		curlen = 0;
13093910Sdougm 	if (name != NULL) {
13104345Sdougm 		int len;
13114345Sdougm 		len = strlen(name) + sep;
13123910Sdougm 
13133910Sdougm 		/*
13143910Sdougm 		 * A future RFE would be to replace this with more
13153910Sdougm 		 * generic code and to possibly handle more types.
13163910Sdougm 		 */
13174345Sdougm 		switch (gettype(name)) {
13184345Sdougm 		case OPT_TYPE_BOOLEAN:
13195179Sdougm 			/*
13205179Sdougm 			 * For NFS, boolean value of FALSE means it
13215179Sdougm 			 * doesn't show up in the option list at all.
13225179Sdougm 			 */
13234345Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
13245179Sdougm 				goto skip;
13255179Sdougm 			if (value != NULL) {
13264345Sdougm 				sa_free_attr_string(value);
13275179Sdougm 				value = NULL;
13285179Sdougm 			}
13294345Sdougm 			break;
13304345Sdougm 		case OPT_TYPE_ACCLIST:
13314345Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
13324345Sdougm 				sa_free_attr_string(value);
13334345Sdougm 				value = NULL;
13344345Sdougm 			} else {
13354345Sdougm 				if (value != NULL)
13364345Sdougm 					len += 1 + strlen(value);
13374345Sdougm 			}
13384345Sdougm 			break;
13394345Sdougm 		case OPT_TYPE_LOGTAG:
13404345Sdougm 			if (value != NULL && strlen(value) == 0) {
13414345Sdougm 				sa_free_attr_string(value);
13424345Sdougm 				value = NULL;
13434345Sdougm 			} else {
13444345Sdougm 				if (value != NULL)
13454345Sdougm 					len += 1 + strlen(value);
13464345Sdougm 			}
13474345Sdougm 			break;
13484345Sdougm 		default:
13494345Sdougm 			if (value != NULL)
13504345Sdougm 				len += 1 + strlen(value);
13514345Sdougm 			break;
13523910Sdougm 		}
13534345Sdougm 		while (buffsize <= (curlen + len)) {
13544345Sdougm 			/* need more room */
13554345Sdougm 			buffsize += incr;
13564345Sdougm 			buff = realloc(buff, buffsize);
13574345Sdougm 			if (buff == NULL) {
13584345Sdougm 				/* realloc failed so free everything */
13594345Sdougm 				if (*rbuff != NULL)
13604345Sdougm 					free(*rbuff);
13614345Sdougm 			}
13624345Sdougm 			*rbuff = buff;
13634345Sdougm 			*rbuffsize = buffsize;
13645179Sdougm 			if (buff == NULL)
13655179Sdougm 				goto skip;
13665179Sdougm 
13673910Sdougm 		}
13685179Sdougm 
13694345Sdougm 		if (buff == NULL)
13705179Sdougm 			goto skip;
13715179Sdougm 
13724345Sdougm 		if (value == NULL) {
13734345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
13744345Sdougm 			    "%s%s", sep ? "," : "",
13754345Sdougm 			    name, value != NULL ? value : "");
13764345Sdougm 		} else {
13774345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
13784345Sdougm 			    "%s%s=%s", sep ? "," : "",
13794345Sdougm 			    name, value != NULL ? value : "");
13803910Sdougm 		}
13815179Sdougm 		printed = B_TRUE;
13823910Sdougm 	}
13835179Sdougm skip:
13843910Sdougm 	if (name != NULL)
13854345Sdougm 		sa_free_attr_string(name);
13863910Sdougm 	if (value != NULL)
13874345Sdougm 		sa_free_attr_string(value);
13885179Sdougm 	return (printed);
13893910Sdougm }
13903910Sdougm 
13913910Sdougm /*
13923910Sdougm  * nfs_format_options(group, hier)
13933910Sdougm  *
13943910Sdougm  * format all the options on the group into an old-style option
13953910Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
13963910Sdougm  * options.
13973910Sdougm  */
13983910Sdougm 
13993910Sdougm static char *
14003910Sdougm nfs_format_options(sa_group_t group, int hier)
14013910Sdougm {
14023910Sdougm 	sa_optionset_t options = NULL;
14034345Sdougm 	sa_optionset_t secoptions = NULL;
14043910Sdougm 	sa_property_t prop, secprop;
14054345Sdougm 	sa_security_t security = NULL;
14063910Sdougm 	char *buff;
14073910Sdougm 	size_t buffsize;
14084345Sdougm 	char *sectype = NULL;
14094345Sdougm 	int sep = 0;
14104345Sdougm 
14114345Sdougm 
14124345Sdougm 	buff = malloc(OPT_CHUNK);
14134345Sdougm 	if (buff == NULL) {
14144345Sdougm 		return (NULL);
14154345Sdougm 	}
14164345Sdougm 
14174345Sdougm 	buff[0] = '\0';
14184345Sdougm 	buffsize = OPT_CHUNK;
14194345Sdougm 
14204345Sdougm 	/*
14214345Sdougm 	 * We may have a an optionset relative to this item. format
14224345Sdougm 	 * these if we find them and then add any security definitions.
14234345Sdougm 	 */
14243910Sdougm 
14253910Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
14263910Sdougm 
14273910Sdougm 	/*
14284345Sdougm 	 * do the default set first but skip any option that is also
14294345Sdougm 	 * in the protocol specific optionset.
14303910Sdougm 	 */
14314345Sdougm 	if (options != NULL) {
14324345Sdougm 		for (prop = sa_get_property(options, NULL);
14334345Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
14343910Sdougm 			/*
14354345Sdougm 			 * use this one since we skipped any
14364345Sdougm 			 * of these that were also in
14374345Sdougm 			 * optdefault
14383910Sdougm 			 */
14395179Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
14405179Sdougm 			    prop, sep))
14415179Sdougm 				sep = 1;
14424345Sdougm 			if (buff == NULL) {
14434345Sdougm 				/*
14444345Sdougm 				 * buff could become NULL if there
14454345Sdougm 				 * isn't enough memory for
14464345Sdougm 				 * nfs_sprint_option to realloc()
14474345Sdougm 				 * as necessary. We can't really
14484345Sdougm 				 * do anything about it at this
14494345Sdougm 				 * point so we return NULL.  The
14504345Sdougm 				 * caller should handle the
14514345Sdougm 				 * failure.
14524345Sdougm 				 */
14534345Sdougm 				if (options != NULL)
14544345Sdougm 					sa_free_derived_optionset(
14554345Sdougm 					    options);
14564345Sdougm 				return (buff);
14574345Sdougm 			}
14583910Sdougm 		}
14594345Sdougm 	}
14604345Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
14614345Sdougm 	    "nfs", hier);
14624345Sdougm 	if (secoptions != NULL) {
14633910Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
14644345Sdougm 		    secprop != NULL;
14654345Sdougm 		    secprop = sa_get_next_property(secprop)) {
14664345Sdougm 			sectype = sa_get_property_attr(secprop, "type");
14674345Sdougm 			security =
14684345Sdougm 			    (sa_security_t)sa_get_derived_security(
14694345Sdougm 			    group, sectype, "nfs", hier);
14704345Sdougm 			if (security != NULL) {
14714345Sdougm 				if (sectype != NULL) {
14724345Sdougm 					prop = sa_create_property(
14734345Sdougm 					    "sec", sectype);
14745179Sdougm 					if (prop == NULL)
14755179Sdougm 						goto err;
14765179Sdougm 					if (nfs_sprint_option(&buff,
14775179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
14785179Sdougm 						sep = 1;
14794345Sdougm 					(void) sa_remove_property(prop);
14805179Sdougm 					if (buff == NULL)
14815179Sdougm 						goto err;
14824345Sdougm 				}
14834345Sdougm 				for (prop = sa_get_property(security,
14844345Sdougm 				    NULL); prop != NULL;
14854345Sdougm 				    prop = sa_get_next_property(prop)) {
14865179Sdougm 					if (nfs_sprint_option(&buff,
14875179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
14885179Sdougm 						sep = 1;
14894345Sdougm 					if (buff == NULL)
14904345Sdougm 						goto err;
14914345Sdougm 				}
14924345Sdougm 				sa_free_derived_optionset(security);
14933910Sdougm 			}
14944345Sdougm 			if (sectype != NULL)
14954345Sdougm 				sa_free_attr_string(sectype);
14963910Sdougm 		}
14973910Sdougm 		sa_free_derived_optionset(secoptions);
14983910Sdougm 	}
14994345Sdougm 
15003910Sdougm 	if (options != NULL)
15014345Sdougm 		sa_free_derived_optionset(options);
15024345Sdougm 	return (buff);
15034345Sdougm 
15044345Sdougm err:
15054345Sdougm 	/*
15064345Sdougm 	 * If we couldn't allocate memory for option printing, we need
15074345Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
15084345Sdougm 	 */
15094345Sdougm 	if (secoptions != NULL)
15104345Sdougm 		sa_free_derived_optionset(secoptions);
15114345Sdougm 	if (security != NULL)
15124345Sdougm 		sa_free_derived_optionset(security);
15134345Sdougm 	if (sectype != NULL)
15144345Sdougm 		sa_free_attr_string(sectype);
15154345Sdougm 	if (options != NULL)
15164345Sdougm 		sa_free_derived_optionset(options);
15178334SJose.Borrego@Sun.COM 	return (NULL);
15183910Sdougm }
15194345Sdougm 
15203910Sdougm /*
15213910Sdougm  * Append an entry to the nfslogtab file
15223910Sdougm  */
15233910Sdougm static int
15243910Sdougm nfslogtab_add(dir, buffer, tag)
15253910Sdougm 	char *dir, *buffer, *tag;
15263910Sdougm {
15273910Sdougm 	FILE *f;
15283910Sdougm 	struct logtab_ent lep;
15293910Sdougm 	int error = 0;
15303910Sdougm 
15313910Sdougm 	/*
15323910Sdougm 	 * Open the file for update and create it if necessary.
15333910Sdougm 	 * This may leave the I/O offset at the end of the file,
15343910Sdougm 	 * so rewind back to the beginning of the file.
15353910Sdougm 	 */
15363910Sdougm 	f = fopen(NFSLOGTAB, "a+");
15373910Sdougm 	if (f == NULL) {
15383910Sdougm 		error = errno;
15393910Sdougm 		goto out;
15403910Sdougm 	}
15413910Sdougm 	rewind(f);
15423910Sdougm 
15433910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
15443910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15454345Sdougm 		    "share complete, however failed to lock %s "
15464345Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
15473910Sdougm 		error = -1;
15483910Sdougm 		goto out;
15493910Sdougm 	}
15503910Sdougm 
15513910Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
15523910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15534345Sdougm 		    "share complete, however could not deactivate "
15544345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15553910Sdougm 		error = -1;
15563910Sdougm 		goto out;
15573910Sdougm 	}
15583910Sdougm 
15593910Sdougm 	/*
15603910Sdougm 	 * Remove entries matching buffer and sharepoint since we're
15613910Sdougm 	 * going to replace it with perhaps an entry with a new tag.
15623910Sdougm 	 */
15633910Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
15643910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15654345Sdougm 		    "share complete, however could not remove matching "
15664345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15673910Sdougm 		error = -1;
15683910Sdougm 		goto out;
15693910Sdougm 	}
15703910Sdougm 
15713910Sdougm 	/*
15723910Sdougm 	 * Deactivate all active entries matching this sharepoint
15733910Sdougm 	 */
15743910Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
15753910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15764345Sdougm 		    "share complete, however could not deactivate matching "
15774345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15783910Sdougm 		error = -1;
15793910Sdougm 		goto out;
15803910Sdougm 	}
15813910Sdougm 
15823910Sdougm 	lep.le_buffer = buffer;
15833910Sdougm 	lep.le_path = dir;
15843910Sdougm 	lep.le_tag = tag;
15853910Sdougm 	lep.le_state = LES_ACTIVE;
15863910Sdougm 
15873910Sdougm 	/*
15883910Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
15893910Sdougm 	 */
15903910Sdougm 	if (logtab_putent(f, &lep) < 0) {
15913910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15924345Sdougm 		    "share complete, however could not add %s to %s\n"),
15934345Sdougm 		    dir, NFSLOGTAB);
15943910Sdougm 		error = -1;
15953910Sdougm 	}
15963910Sdougm 
15973910Sdougm out:
15983910Sdougm 	if (f != NULL)
15993910Sdougm 		(void) fclose(f);
16003910Sdougm 	return (error);
16013910Sdougm }
16023910Sdougm 
16033910Sdougm /*
16043910Sdougm  * Deactivate an entry from the nfslogtab file
16053910Sdougm  */
16063910Sdougm static int
16073910Sdougm nfslogtab_deactivate(path)
16083910Sdougm 	char *path;
16093910Sdougm {
16103910Sdougm 	FILE *f;
16113910Sdougm 	int error = 0;
16123910Sdougm 
16133910Sdougm 	f = fopen(NFSLOGTAB, "r+");
16143910Sdougm 	if (f == NULL) {
16153910Sdougm 		error = errno;
16163910Sdougm 		goto out;
16173910Sdougm 	}
16183910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
16193910Sdougm 		error = errno;
16203910Sdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
16214345Sdougm 		    "share complete, however could not lock %s for "
16224345Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
16233910Sdougm 		goto out;
16243910Sdougm 	}
16253910Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
16263910Sdougm 		error = -1;
16273910Sdougm 		(void) fprintf(stderr,
16284345Sdougm 		    dgettext(TEXT_DOMAIN,
16294345Sdougm 		    "share complete, however could not "
16304345Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
16313910Sdougm 		goto out;
16323910Sdougm 	}
16333910Sdougm 
16343910Sdougm out:	if (f != NULL)
16353910Sdougm 		(void) fclose(f);
16363910Sdougm 
16373910Sdougm 	return (error);
16383910Sdougm }
16393910Sdougm 
16403910Sdougm /*
16414524Sdougm  * check_public(group, skipshare)
16424524Sdougm  *
16434524Sdougm  * Check the group for any shares that have the public property
16444524Sdougm  * enabled. We skip "skipshare" since that is the one we are
16454524Sdougm  * working with. This is a separate function to make handling
16464524Sdougm  * subgroups simpler. Returns true if there is a share with public.
16474524Sdougm  */
16484524Sdougm static int
16494524Sdougm check_public(sa_group_t group, sa_share_t skipshare)
16504524Sdougm {
16514524Sdougm 	int exists = B_FALSE;
16524524Sdougm 	sa_share_t share;
16534524Sdougm 	sa_optionset_t opt;
16544524Sdougm 	sa_property_t prop;
16554524Sdougm 	char *shared;
16564524Sdougm 
16574524Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
16584524Sdougm 	    share = sa_get_next_share(share)) {
16594524Sdougm 		if (share == skipshare)
16604524Sdougm 			continue;
16614524Sdougm 
16624524Sdougm 		opt = sa_get_optionset(share, "nfs");
16634524Sdougm 		if (opt == NULL)
16644524Sdougm 			continue;
16654524Sdougm 		prop = sa_get_property(opt, "public");
16664524Sdougm 		if (prop == NULL)
16674524Sdougm 			continue;
16684524Sdougm 		shared = sa_get_share_attr(share, "shared");
16694524Sdougm 		if (shared != NULL) {
16704524Sdougm 			exists = strcmp(shared, "true") == 0;
16714524Sdougm 			sa_free_attr_string(shared);
16724524Sdougm 			if (exists == B_TRUE)
16734524Sdougm 				break;
16744524Sdougm 		}
16754524Sdougm 	}
16764524Sdougm 
16774524Sdougm 	return (exists);
16784524Sdougm }
16794524Sdougm 
16804524Sdougm /*
16816214Sdougm  * public_exists(handle, share)
16823910Sdougm  *
16833910Sdougm  * check to see if public option is set on any other share than the
16844524Sdougm  * one specified. Need to check zfs sub-groups as well as the top
16854524Sdougm  * level groups.
16863910Sdougm  */
16873910Sdougm static int
16886214Sdougm public_exists(sa_handle_t handle, sa_share_t skipshare)
16893910Sdougm {
16906214Sdougm 	sa_group_t group = NULL;
16913910Sdougm 
16926271Sdougm 	/*
16936271Sdougm 	 * If we don't have a handle, we can only do syntax check. We
16946271Sdougm 	 * can't check against other shares so we assume OK and will
16956271Sdougm 	 * catch the problem only when we actually try to apply it.
16966271Sdougm 	 */
16973910Sdougm 	if (handle == NULL)
16986271Sdougm 		return (SA_OK);
16993910Sdougm 
17006214Sdougm 	if (skipshare != NULL) {
17016214Sdougm 		group = sa_get_parent_group(skipshare);
17026214Sdougm 		if (group == NULL)
17036214Sdougm 			return (SA_NO_SUCH_GROUP);
17046214Sdougm 	}
17056214Sdougm 
17063910Sdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
17073910Sdougm 	    group = sa_get_next_group(group)) {
17084524Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
17094524Sdougm 		if (sa_group_is_zfs(group)) {
17104524Sdougm 			sa_group_t subgroup;
17114524Sdougm 			for (subgroup = sa_get_sub_group(group);
17124524Sdougm 			    subgroup != NULL;
17134524Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
17144524Sdougm 				if (check_public(subgroup, skipshare))
17154524Sdougm 					return (B_TRUE);
17163910Sdougm 			}
17174524Sdougm 		} else {
17184524Sdougm 			if (check_public(group, skipshare))
17194524Sdougm 				return (B_TRUE);
17203910Sdougm 		}
17213910Sdougm 	}
17224524Sdougm 	return (B_FALSE);
17233910Sdougm }
17243910Sdougm 
17253910Sdougm /*
17263910Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
17273910Sdougm  * implementation that it is to enable the share. This entails
17283910Sdougm  * converting the path and options into the appropriate ioctl
17293910Sdougm  * calls. It is assumed that all error checking of paths, etc. were
17303910Sdougm  * done earlier.
17313910Sdougm  */
17323910Sdougm static int
17333910Sdougm nfs_enable_share(sa_share_t share)
17343910Sdougm {
17353910Sdougm 	struct exportdata export;
17363910Sdougm 	sa_optionset_t secoptlist;
17373910Sdougm 	struct secinfo *sp;
17383910Sdougm 	int num_secinfo;
17393910Sdougm 	sa_optionset_t opt;
17403910Sdougm 	sa_security_t sec;
17413910Sdougm 	sa_property_t prop;
17423910Sdougm 	char *path;
17433910Sdougm 	int err = SA_OK;
17444524Sdougm 	int i;
17454543Smarks 	int iszfs;
17466214Sdougm 	sa_handle_t handle;
17473910Sdougm 
17483910Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
17493910Sdougm 	(void) signal(SIGSYS, SIG_IGN);
17503910Sdougm 
17513910Sdougm 	/* get the path since it is important in several places */
17523910Sdougm 	path = sa_get_share_attr(share, "path");
17533910Sdougm 	if (path == NULL)
17544345Sdougm 		return (SA_NO_SUCH_PATH);
17553910Sdougm 
17564543Smarks 	iszfs = sa_path_is_zfs(path);
17573910Sdougm 	/*
17583910Sdougm 	 * find the optionsets and security sets.  There may not be
17593910Sdougm 	 * any or there could be one or two for each of optionset and
17603910Sdougm 	 * security may have multiple, one per security type per
17613910Sdougm 	 * protocol type.
17623910Sdougm 	 */
17633910Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
17643910Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
17653910Sdougm 	if (secoptlist != NULL)
17664345Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
17673910Sdougm 	else
17684345Sdougm 		num_secinfo = 1;
17693910Sdougm 
17703910Sdougm 	/*
17713910Sdougm 	 * walk through the options and fill in the structure
17723910Sdougm 	 * appropriately.
17733910Sdougm 	 */
17743910Sdougm 
17753910Sdougm 	(void) memset(&export, '\0', sizeof (export));
17763910Sdougm 
17773910Sdougm 	/*
17783910Sdougm 	 * do non-security options first since there is only one after
17793910Sdougm 	 * the derived group is constructed.
17803910Sdougm 	 */
17813910Sdougm 	export.ex_version = EX_CURRENT_VERSION;
17823910Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
17833910Sdougm 	export.ex_index = NULL;
17843910Sdougm 	export.ex_path = path;
17853910Sdougm 	export.ex_pathlen = strlen(path) + 1;
17863910Sdougm 
17873910Sdougm 	if (opt != NULL)
17884345Sdougm 		err = fill_export_from_optionset(&export, opt);
17893910Sdougm 
17903910Sdougm 	/*
17913910Sdougm 	 * check to see if "public" is set. If it is, then make sure
17923910Sdougm 	 * no other share has it set. If it is already used, fail.
17933910Sdougm 	 */
17943910Sdougm 
17956214Sdougm 	handle = sa_find_group_handle((sa_group_t)share);
17966214Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
17974345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
17984345Sdougm 		    "NFS: Cannot share more than one file "
17994345Sdougm 		    "system with 'public' property\n"));
18004345Sdougm 		err = SA_NOT_ALLOWED;
18014345Sdougm 		goto out;
18023910Sdougm 	}
18033910Sdougm 
18044524Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
18053910Sdougm 	if (sp == NULL) {
18064345Sdougm 		err = SA_NO_MEMORY;
18074524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18084524Sdougm 		    "NFS: NFS: no memory for security\n"));
18094524Sdougm 		goto out;
18104524Sdougm 	}
18114524Sdougm 	export.ex_secinfo = sp;
18124524Sdougm 	/* get default secinfo */
18134524Sdougm 	export.ex_seccnt = num_secinfo;
18144524Sdougm 	/*
18154524Sdougm 	 * since we must have one security option defined, we
18164524Sdougm 	 * init to the default and then override as we find
18174524Sdougm 	 * defined security options. This handles the case
18184524Sdougm 	 * where we have no defined options but we need to set
18194524Sdougm 	 * up one.
18204524Sdougm 	 */
18214524Sdougm 	sp[0].s_window = DEF_WIN;
18224524Sdougm 	sp[0].s_rootnames = NULL;
18234524Sdougm 	/* setup a default in case no properties defined */
18244524Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
18254524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18264524Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
18274524Sdougm 		    "get default security mode\n"));
18284524Sdougm 		err = SA_CONFIG_ERR;
18294524Sdougm 	}
18304524Sdougm 	if (secoptlist != NULL) {
18314524Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
18324524Sdougm 		    prop != NULL && i < num_secinfo;
18334524Sdougm 		    prop = sa_get_next_property(prop), i++) {
18344524Sdougm 			char *sectype;
18354345Sdougm 				sectype = sa_get_property_attr(prop, "type");
18364524Sdougm 			/*
18374524Sdougm 			 * if sectype is NULL, we probably
18384524Sdougm 			 * have a memory problem and can't get
18394524Sdougm 			 * the correct values. Rather than
18404524Sdougm 			 * exporting with incorrect security,
18414524Sdougm 			 * don't share it.
18424524Sdougm 			 */
18434524Sdougm 			if (sectype == NULL) {
18444524Sdougm 				err = SA_NO_MEMORY;
18454524Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
18464524Sdougm 				    "NFS: Cannot share %s: "
18474524Sdougm 				    "no memory\n"), path);
18484524Sdougm 				goto out;
18494524Sdougm 			}
18504524Sdougm 			sec = (sa_security_t)sa_get_derived_security(
18514524Sdougm 			    share, sectype, "nfs", 1);
18524524Sdougm 			sp[i].s_window = DEF_WIN;
18534524Sdougm 			sp[i].s_rootcnt = 0;
18544524Sdougm 			sp[i].s_rootnames = NULL;
18554345Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
18564524Sdougm 			if (sec != NULL)
18574524Sdougm 				sa_free_derived_security(sec);
18584524Sdougm 			if (sectype != NULL)
18594524Sdougm 				sa_free_attr_string(sectype);
18603910Sdougm 		}
18614524Sdougm 	}
18624524Sdougm 	/*
18634524Sdougm 	 * when we get here, we can do the exportfs system call and
18644524Sdougm 	 * initiate thinsg. We probably want to enable the nfs.server
18654524Sdougm 	 * service first if it isn't running within SMF.
18664524Sdougm 	 */
18674524Sdougm 	/* check nfs.server status and start if needed */
18684524Sdougm 	/* now add the share to the internal tables */
18694524Sdougm 	printarg(path, &export);
18704524Sdougm 	/*
18714524Sdougm 	 * call the exportfs system call which is implemented
18724524Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
18734524Sdougm 	 */
18744543Smarks 	if (iszfs) {
18754543Smarks 		struct exportfs_args ea;
18764543Smarks 		share_t sh;
18774543Smarks 		char *str;
18784543Smarks 		priv_set_t *priv_effective;
18794543Smarks 		int privileged;
18804543Smarks 
18814543Smarks 		/*
18824543Smarks 		 * If we aren't a privileged user
18834543Smarks 		 * and NFS server service isn't running
18844543Smarks 		 * then print out an error message
18854543Smarks 		 * and return EPERM
18864543Smarks 		 */
18874543Smarks 
18884543Smarks 		priv_effective = priv_allocset();
18894543Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
18904543Smarks 
18914543Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
18924543Smarks 		priv_freeset(priv_effective);
18934543Smarks 
18944543Smarks 		if (!privileged &&
18954543Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
18964543Smarks 			err = 0;
18974543Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
18984543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
18994543Smarks 				    "NFS: Cannot share remote "
19004543Smarks 				    "filesystem: %s\n"), path);
19014543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
19024543Smarks 				    "NFS: Service needs to be enabled "
19034543Smarks 				    "by a privileged user\n"));
19044543Smarks 				err = SA_SYSTEM_ERR;
19054543Smarks 				errno = EPERM;
19064543Smarks 			}
19074543Smarks 			free(str);
19084543Smarks 		}
19094543Smarks 
19104543Smarks 		if (err == 0) {
19114543Smarks 			ea.dname = path;
19124543Smarks 			ea.uex = &export;
19134543Smarks 
19144543Smarks 			sa_sharetab_fill_zfs(share, &sh, "nfs");
19158845Samw@Sun.COM 			err = sa_share_zfs(share, NULL, path, &sh,
19165331Samw 			    &ea, ZFS_SHARE_NFS);
191710761SWilliam.Krier@Sun.COM 			if (err != SA_OK) {
191810761SWilliam.Krier@Sun.COM 				errno = err;
191910761SWilliam.Krier@Sun.COM 				err = -1;
192010761SWilliam.Krier@Sun.COM 			}
19214543Smarks 			sa_emptyshare(&sh);
19224543Smarks 		}
19234543Smarks 	} else {
19244543Smarks 		err = exportfs(path, &export);
19254543Smarks 	}
19264543Smarks 
19274543Smarks 	if (err < 0) {
19284524Sdougm 		err = SA_SYSTEM_ERR;
19294524Sdougm 		switch (errno) {
19304524Sdougm 		case EREMOTE:
19314524Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
19324543Smarks 			    "NFS: Cannot share filesystems "
19334543Smarks 			    "in non-global zones: %s\n"), path);
19344543Smarks 			err = SA_NOT_SUPPORTED;
19354524Sdougm 			break;
19364524Sdougm 		case EPERM:
19374524Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
19384345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
19394543Smarks 				    "NFS: Cannot share file systems "
19404524Sdougm 				    "in non-global zones: %s\n"), path);
19414524Sdougm 				err = SA_NOT_SUPPORTED;
19424345Sdougm 				break;
19434345Sdougm 			}
19444524Sdougm 			err = SA_NO_PERMISSION;
19454524Sdougm 			/* FALLTHROUGH */
19464524Sdougm 		default:
19474524Sdougm 			break;
19483910Sdougm 		}
19494524Sdougm 	} else {
19504524Sdougm 		/* update sharetab with an add/modify */
19514543Smarks 		if (!iszfs) {
19524543Smarks 			(void) sa_update_sharetab(share, "nfs");
19534543Smarks 		}
19543910Sdougm 	}
19553910Sdougm 
19563910Sdougm 	if (err == SA_OK) {
19573910Sdougm 		/*
19583910Sdougm 		 * enable services as needed. This should probably be
19593910Sdougm 		 * done elsewhere in order to minimize the calls to
19603910Sdougm 		 * check services.
19613910Sdougm 		 */
19623910Sdougm 		/*
19633910Sdougm 		 * check to see if logging and other services need to
19643910Sdougm 		 * be triggered, but only if there wasn't an
19653910Sdougm 		 * error. This is probably where sharetab should be
19663910Sdougm 		 * updated with the NFS specific entry.
19673910Sdougm 		 */
19684345Sdougm 		if (export.ex_flags & EX_LOG) {
19694345Sdougm 			/* enable logging */
19704345Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
19714345Sdougm 			    export.ex_tag) != 0) {
19724345Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
19734345Sdougm 				    "Could not enable logging for %s\n"),
19744345Sdougm 				    path);
19754345Sdougm 			}
19764345Sdougm 			_check_services(service_list_logging);
19774345Sdougm 		} else {
19784345Sdougm 			/*
19794345Sdougm 			 * don't have logging so remove it from file. It might
19804345Sdougm 			 * not be thre, but that doesn't matter.
19814345Sdougm 			 */
19824345Sdougm 			(void) nfslogtab_deactivate(path);
19834345Sdougm 			_check_services(service_list_default);
19843910Sdougm 		}
19853910Sdougm 	}
19863910Sdougm 
19873910Sdougm out:
19883910Sdougm 	if (path != NULL)
19894345Sdougm 		free(path);
19903910Sdougm 
19913910Sdougm 	cleanup_export(&export);
19923910Sdougm 	if (opt != NULL)
19934345Sdougm 		sa_free_derived_optionset(opt);
19943910Sdougm 	if (secoptlist != NULL)
19954345Sdougm 		(void) sa_destroy_optionset(secoptlist);
19963910Sdougm 	return (err);
19973910Sdougm }
19983910Sdougm 
19993910Sdougm /*
20005800Sdougm  * nfs_disable_share(share, path)
20013910Sdougm  *
20025800Sdougm  * Unshare the specified share. Note that "path" is the same path as
20035800Sdougm  * what is in the "share" object. It is passed in to avoid an
20045800Sdougm  * additional lookup. A missing "path" value makes this a no-op
20055800Sdougm  * function.
20063910Sdougm  */
20073910Sdougm static int
20084543Smarks nfs_disable_share(sa_share_t share, char *path)
20093910Sdougm {
20103910Sdougm 	int err;
20113910Sdougm 	int ret = SA_OK;
20124543Smarks 	int iszfs;
20135800Sdougm 	sa_group_t parent;
20145951Sdougm 	sa_handle_t handle;
20154543Smarks 
20165800Sdougm 	if (path == NULL)
20175800Sdougm 		return (ret);
20184543Smarks 
20195800Sdougm 	/*
20205800Sdougm 	 * If the share is in a ZFS group we need to handle it
20215800Sdougm 	 * differently.  Just being on a ZFS file system isn't
20225800Sdougm 	 * enough since we may be in a legacy share case.
20235800Sdougm 	 */
20245800Sdougm 	parent = sa_get_parent_group(share);
20255800Sdougm 	iszfs = sa_group_is_zfs(parent);
20265800Sdougm 	if (iszfs) {
20275800Sdougm 		struct exportfs_args ea;
20285800Sdougm 		share_t sh = { 0 };
20295800Sdougm 		ea.dname = path;
20305800Sdougm 		ea.uex = NULL;
20315800Sdougm 		sh.sh_path = path;
20325800Sdougm 		sh.sh_fstype = "nfs";
20334543Smarks 
20348845Samw@Sun.COM 		err = sa_share_zfs(share, NULL, path, &sh,
20355800Sdougm 		    &ea, ZFS_UNSHARE_NFS);
203610761SWilliam.Krier@Sun.COM 		if (err != SA_OK) {
203710761SWilliam.Krier@Sun.COM 			errno = err;
203810761SWilliam.Krier@Sun.COM 			err = -1;
203910761SWilliam.Krier@Sun.COM 		}
20405800Sdougm 	} else {
20415800Sdougm 		err = exportfs(path, NULL);
20425800Sdougm 	}
20435800Sdougm 	if (err < 0) {
20445800Sdougm 		/*
20455800Sdougm 		 * TBD: only an error in some
20465800Sdougm 		 * cases - need better analysis
20475800Sdougm 		 */
20485800Sdougm 		switch (errno) {
20495800Sdougm 		case EPERM:
20505800Sdougm 		case EACCES:
20515800Sdougm 			ret = SA_NO_PERMISSION;
20525800Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
20535800Sdougm 				ret = SA_NOT_SUPPORTED;
20545800Sdougm 			}
20555800Sdougm 			break;
20565800Sdougm 		case EINVAL:
20575800Sdougm 		case ENOENT:
20585800Sdougm 			ret = SA_NO_SUCH_PATH;
20594543Smarks 			break;
206010761SWilliam.Krier@Sun.COM 		default:
206110761SWilliam.Krier@Sun.COM 			ret = SA_SYSTEM_ERR;
20624543Smarks 			break;
20633910Sdougm 		}
20645800Sdougm 	}
20655800Sdougm 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
20665951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
20675800Sdougm 		if (!iszfs)
20685951Sdougm 			(void) sa_delete_sharetab(handle, path, "nfs");
20695800Sdougm 		/* just in case it was logged */
20705800Sdougm 		(void) nfslogtab_deactivate(path);
20713910Sdougm 	}
20723910Sdougm 	return (ret);
20733910Sdougm }
20743910Sdougm 
20753910Sdougm /*
20767961SNatalie.Li@Sun.COM  * check_rorwnone(v1, v2, v3)
20777961SNatalie.Li@Sun.COM  *
20787961SNatalie.Li@Sun.COM  * check ro vs rw vs none values.  Over time this may get beefed up.
20797961SNatalie.Li@Sun.COM  * for now it just does simple checks. v1 is never NULL but v2 or v3
20807961SNatalie.Li@Sun.COM  * could be.
20813910Sdougm  */
20823910Sdougm 
20833910Sdougm static int
20847961SNatalie.Li@Sun.COM check_rorwnone(char *v1, char *v2, char *v3)
20853910Sdougm {
20863910Sdougm 	int ret = SA_OK;
20877961SNatalie.Li@Sun.COM 	if (v2 != NULL && strcmp(v1, v2) == 0)
20884345Sdougm 		ret = SA_VALUE_CONFLICT;
20897961SNatalie.Li@Sun.COM 	else if (v3 != NULL && strcmp(v1, v3) == 0)
20907961SNatalie.Li@Sun.COM 		ret = SA_VALUE_CONFLICT;
20917961SNatalie.Li@Sun.COM 
20923910Sdougm 	return (ret);
20933910Sdougm }
20943910Sdougm 
20953910Sdougm /*
20966214Sdougm  * nfs_validate_property(handle, property, parent)
20973910Sdougm  *
20983910Sdougm  * Check that the property has a legitimate value for its type.
20993910Sdougm  */
21003910Sdougm 
21013910Sdougm static int
21026214Sdougm nfs_validate_property(sa_handle_t handle, sa_property_t property,
21036214Sdougm     sa_optionset_t parent)
21043910Sdougm {
21053910Sdougm 	int ret = SA_OK;
21063910Sdougm 	char *propname;
21077961SNatalie.Li@Sun.COM 	char *other1;
21087961SNatalie.Li@Sun.COM 	char *other2;
21093910Sdougm 	int optindex;
21103910Sdougm 	nfsl_config_t *configlist;
21113910Sdougm 	sa_group_t parent_group;
21123910Sdougm 	char *value;
21133910Sdougm 
21143910Sdougm 	propname = sa_get_property_attr(property, "type");
21153910Sdougm 
21163910Sdougm 	if ((optindex = findopt(propname)) < 0)
21174345Sdougm 		ret = SA_NO_SUCH_PROP;
21183910Sdougm 
21193910Sdougm 	/* need to validate value range here as well */
21203910Sdougm 
21213910Sdougm 	if (ret == SA_OK) {
21224345Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
21236214Sdougm 		if (optdefs[optindex].share && parent_group != NULL &&
21246214Sdougm 		    !sa_is_share(parent_group))
21254345Sdougm 			ret = SA_PROP_SHARE_ONLY;
21263910Sdougm 	}
21273910Sdougm 	if (ret == SA_OK) {
21286214Sdougm 		if (optdefs[optindex].index == OPT_PUBLIC) {
21296214Sdougm 			/*
21306214Sdougm 			 * Public is special in that only one instance can
21316214Sdougm 			 * be in the repository at the same time.
21326214Sdougm 			 */
21336214Sdougm 			if (public_exists(handle, parent_group)) {
21347961SNatalie.Li@Sun.COM 				sa_free_attr_string(propname);
21356214Sdougm 				return (SA_VALUE_CONFLICT);
21366214Sdougm 			}
21376214Sdougm 		}
21384345Sdougm 		value = sa_get_property_attr(property, "value");
21394345Sdougm 		if (value != NULL) {
21404345Sdougm 			/* first basic type checking */
21414345Sdougm 			switch (optdefs[optindex].type) {
21424345Sdougm 			case OPT_TYPE_NUMBER:
21434345Sdougm 				/* check that the value is all digits */
21444345Sdougm 				if (!is_a_number(value))
21454345Sdougm 					ret = SA_BAD_VALUE;
21464345Sdougm 				break;
21474345Sdougm 			case OPT_TYPE_BOOLEAN:
21484345Sdougm 				if (strlen(value) == 0 ||
21494345Sdougm 				    strcasecmp(value, "true") == 0 ||
21504345Sdougm 				    strcmp(value, "1") == 0 ||
21514345Sdougm 				    strcasecmp(value, "false") == 0 ||
21524345Sdougm 				    strcmp(value, "0") == 0) {
21534345Sdougm 					ret = SA_OK;
21544345Sdougm 				} else {
21554345Sdougm 					ret = SA_BAD_VALUE;
21564345Sdougm 				}
21574345Sdougm 				break;
21584345Sdougm 			case OPT_TYPE_USER:
21594345Sdougm 				if (!is_a_number(value)) {
21604345Sdougm 					struct passwd *pw;
21614345Sdougm 					/*
21624345Sdougm 					 * in this case it would have to be a
21634345Sdougm 					 * user name
21644345Sdougm 					 */
21654345Sdougm 					pw = getpwnam(value);
21664345Sdougm 					if (pw == NULL)
21674345Sdougm 						ret = SA_BAD_VALUE;
21684345Sdougm 					endpwent();
21694345Sdougm 				} else {
21704345Sdougm 					uint64_t intval;
21714345Sdougm 					intval = strtoull(value, NULL, 0);
21724345Sdougm 					if (intval > UID_MAX && intval != ~0)
21734345Sdougm 						ret = SA_BAD_VALUE;
21744345Sdougm 				}
21754345Sdougm 				break;
21764345Sdougm 			case OPT_TYPE_FILE:
21774345Sdougm 				if (strcmp(value, "..") == 0 ||
21784345Sdougm 				    strchr(value, '/') != NULL) {
21794345Sdougm 					ret = SA_BAD_VALUE;
21804345Sdougm 				}
21814345Sdougm 				break;
21827961SNatalie.Li@Sun.COM 			case OPT_TYPE_ACCLIST: {
21837961SNatalie.Li@Sun.COM 				sa_property_t oprop1;
21847961SNatalie.Li@Sun.COM 				sa_property_t oprop2;
21857961SNatalie.Li@Sun.COM 				char *ovalue1 = NULL;
21867961SNatalie.Li@Sun.COM 				char *ovalue2 = NULL;
21877961SNatalie.Li@Sun.COM 
21887961SNatalie.Li@Sun.COM 				if (parent == NULL)
21897961SNatalie.Li@Sun.COM 					break;
21904345Sdougm 				/*
21914345Sdougm 				 * access list handling. Should eventually
21924345Sdougm 				 * validate that all the values make sense.
21934345Sdougm 				 * Also, ro and rw may have cross value
21944345Sdougm 				 * conflicts.
21954345Sdougm 				 */
21967961SNatalie.Li@Sun.COM 				if (strcmp(propname, SHOPT_RO) == 0) {
21977961SNatalie.Li@Sun.COM 					other1 = SHOPT_RW;
21987961SNatalie.Li@Sun.COM 					other2 = SHOPT_NONE;
21997961SNatalie.Li@Sun.COM 				} else if (strcmp(propname, SHOPT_RW) == 0) {
22007961SNatalie.Li@Sun.COM 					other1 = SHOPT_RO;
22017961SNatalie.Li@Sun.COM 					other2 = SHOPT_NONE;
22027961SNatalie.Li@Sun.COM 				} else if (strcmp(propname, SHOPT_NONE) == 0) {
22037961SNatalie.Li@Sun.COM 					other1 = SHOPT_RO;
22047961SNatalie.Li@Sun.COM 					other2 = SHOPT_RW;
22057961SNatalie.Li@Sun.COM 				} else {
22067961SNatalie.Li@Sun.COM 					other1 = NULL;
22077961SNatalie.Li@Sun.COM 					other2 = NULL;
22087961SNatalie.Li@Sun.COM 				}
22097961SNatalie.Li@Sun.COM 				if (other1 == NULL && other2 == NULL)
22107961SNatalie.Li@Sun.COM 					break;
22117961SNatalie.Li@Sun.COM 
22127961SNatalie.Li@Sun.COM 				/* compare rw(ro) with ro(rw) */
22134345Sdougm 
22147961SNatalie.Li@Sun.COM 				oprop1 = sa_get_property(parent, other1);
22157961SNatalie.Li@Sun.COM 				oprop2 = sa_get_property(parent, other2);
22167961SNatalie.Li@Sun.COM 				if (oprop1 == NULL && oprop2 == NULL)
22177961SNatalie.Li@Sun.COM 					break;
22187961SNatalie.Li@Sun.COM 				/*
22197961SNatalie.Li@Sun.COM 				 * Only potential confusion if other1
22207961SNatalie.Li@Sun.COM 				 * or other2 exists. Check the values
22217961SNatalie.Li@Sun.COM 				 * and run the check if there is a
22227961SNatalie.Li@Sun.COM 				 * value other than the one we are
22237961SNatalie.Li@Sun.COM 				 * explicitly looking at.
22247961SNatalie.Li@Sun.COM 				 */
22257961SNatalie.Li@Sun.COM 				ovalue1 = sa_get_property_attr(oprop1, "value");
22267961SNatalie.Li@Sun.COM 				ovalue2 = sa_get_property_attr(oprop2, "value");
22277961SNatalie.Li@Sun.COM 				if (ovalue1 != NULL || ovalue2 != NULL)
22287961SNatalie.Li@Sun.COM 					ret = check_rorwnone(value, ovalue1,
22297961SNatalie.Li@Sun.COM 					    ovalue2);
22307961SNatalie.Li@Sun.COM 
22317961SNatalie.Li@Sun.COM 				if (ovalue1 != NULL)
22327961SNatalie.Li@Sun.COM 					sa_free_attr_string(ovalue1);
22337961SNatalie.Li@Sun.COM 				if (ovalue2 != NULL)
22347961SNatalie.Li@Sun.COM 					sa_free_attr_string(ovalue2);
22354345Sdougm 				break;
22367961SNatalie.Li@Sun.COM 			}
22374345Sdougm 			case OPT_TYPE_LOGTAG:
22384345Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
22394345Sdougm 					int error;
22404345Sdougm 					if (value == NULL ||
22414345Sdougm 					    strlen(value) == 0) {
22424345Sdougm 						if (value != NULL)
22434345Sdougm 							sa_free_attr_string(
22444345Sdougm 							    value);
22454345Sdougm 						value = strdup("global");
22464345Sdougm 					}
22474345Sdougm 					if (value != NULL &&
22484345Sdougm 					    nfsl_findconfig(configlist, value,
22494345Sdougm 					    &error) == NULL) {
22504345Sdougm 						ret = SA_BAD_VALUE;
22514345Sdougm 					}
22525179Sdougm 					/* Must always free when done */
22535179Sdougm 					nfsl_freeconfig_list(&configlist);
22544345Sdougm 				} else {
22554345Sdougm 					ret = SA_CONFIG_ERR;
22564345Sdougm 				}
22574345Sdougm 				break;
22584345Sdougm 			case OPT_TYPE_STRING:
22594345Sdougm 				/* whatever is here should be ok */
22604345Sdougm 				break;
22614345Sdougm 			case OPT_TYPE_SECURITY:
22624345Sdougm 				/*
22634345Sdougm 				 * The "sec" property isn't used in the
22644345Sdougm 				 * non-legacy parts of sharemgr. We need to
22654345Sdougm 				 * reject it here. For legacy, it is pulled
22664345Sdougm 				 * out well before we get here.
22674345Sdougm 				 */
22684345Sdougm 				ret = SA_NO_SUCH_PROP;
22694345Sdougm 				break;
22704345Sdougm 			default:
22714345Sdougm 				break;
22723910Sdougm 			}
22735179Sdougm 
22745179Sdougm 			if (value != NULL)
22755179Sdougm 				sa_free_attr_string(value);
22765179Sdougm 
22774345Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
22784345Sdougm 				/* do the property specific check */
22796214Sdougm 				ret = optdefs[optindex].check(handle, property);
22803910Sdougm 			}
22813910Sdougm 		}
22823910Sdougm 	}
22833910Sdougm 
22843910Sdougm 	if (propname != NULL)
22854345Sdougm 		sa_free_attr_string(propname);
22863910Sdougm 	return (ret);
22873910Sdougm }
22883910Sdougm 
22893910Sdougm /*
22903910Sdougm  * Protocol management functions
22913910Sdougm  *
22923910Sdougm  * Properties defined in the default files are defined in
22933910Sdougm  * proto_option_defs for parsing and validation. If "other" and
22943910Sdougm  * "compare" are set, then the value for this property should be
22953910Sdougm  * compared against the property specified in "other" using the
22963910Sdougm  * "compare" check (either <= or >=) in order to ensure that the
22973910Sdougm  * values are in the correct range.  E.g. setting server_versmin
22983910Sdougm  * higher than server_versmax should not be allowed.
22993910Sdougm  */
23003910Sdougm 
23013910Sdougm struct proto_option_defs {
23023910Sdougm 	char *tag;
23033910Sdougm 	char *name;	/* display name -- remove protocol identifier */
23043910Sdougm 	int index;
23053910Sdougm 	int type;
23063910Sdougm 	union {
23073910Sdougm 	    int intval;
23083910Sdougm 	    char *string;
23093910Sdougm 	} defvalue;
23103910Sdougm 	uint32_t svcs;
23113910Sdougm 	int32_t minval;
23123910Sdougm 	int32_t maxval;
23133910Sdougm 	char *file;
23143910Sdougm 	char *other;
23153910Sdougm 	int compare;
23163910Sdougm #define	OPT_CMP_GE	0
23173910Sdougm #define	OPT_CMP_LE	1
23183910Sdougm 	int (*check)(char *);
23193910Sdougm } proto_options[] = {
23203910Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
23213910Sdougm 	{"nfsd_servers",
23223910Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
23233910Sdougm 	    1, INT32_MAX, NFSADMIN},
23243910Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
23253910Sdougm 	{"lockd_listen_backlog",
23263910Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
23273910Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
23283910Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
23293910Sdougm 	{"lockd_servers",
23303910Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
23313910Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
23323910Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
23333910Sdougm 	{"lockd_retransmit_timeout",
23343910Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
23353910Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23363910Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
23373910Sdougm 	{"grace_period",
23383910Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
23393910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23403910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
23413910Sdougm 	{"nfs_server_versmin",
23423910Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
23433910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23443910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
23453910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
23463910Sdougm 	{"nfs_server_versmax",
23473910Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
23483910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23493910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
23503910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
23513910Sdougm 	{"nfs_client_versmin",
23523910Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
23533910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23543910Sdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
23553910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
23563910Sdougm 	{"nfs_client_versmax",
23573910Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
23583910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23593910Sdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
23603910Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
23613910Sdougm 	{"nfs_server_delegation",
23623910Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
23633910Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
23643910Sdougm 	    NFSADMIN},
23653910Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
23663910Sdougm 	{"nfsmapid_domain",
23673910Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
23683910Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
23693910Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
23703910Sdougm 	{"nfsd_max_connections",
23713910Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
23723910Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
23733910Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
23743910Sdougm 	{"nfsd_protocol",
23753910Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
23763910Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
23773910Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
23783910Sdougm 	{"nfsd_listen_backlog",
23793910Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
23803910Sdougm 	    OPT_TYPE_NUMBER, 0,
23813910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23823910Sdougm 	{NULL}
23833910Sdougm };
23843910Sdougm 
23853910Sdougm /*
23863910Sdougm  * the protoset holds the defined options so we don't have to read
23873910Sdougm  * them multiple times
23883910Sdougm  */
23895179Sdougm static sa_protocol_properties_t protoset;
23903910Sdougm 
23913910Sdougm static int
23923910Sdougm findprotoopt(char *name, int whichname)
23933910Sdougm {
23943910Sdougm 	int i;
23953910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
23964345Sdougm 		if (whichname == 1) {
23974345Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
23983910Sdougm 			return (i);
23994345Sdougm 		} else {
24004345Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
24014345Sdougm 				return (i);
24024345Sdougm 		}
24033910Sdougm 	}
24043910Sdougm 	return (-1);
24053910Sdougm }
24063910Sdougm 
24073910Sdougm /*
24083910Sdougm  * fixcaselower(str)
24093910Sdougm  *
24103910Sdougm  * convert a string to lower case (inplace).
24113910Sdougm  */
24123910Sdougm 
24133910Sdougm static void
24143910Sdougm fixcaselower(char *str)
24153910Sdougm {
24163910Sdougm 	while (*str) {
24174345Sdougm 		*str = tolower(*str);
24184345Sdougm 		str++;
24193910Sdougm 	}
24203910Sdougm }
24213910Sdougm 
24223910Sdougm /*
24233910Sdougm  * fixcaseupper(str)
24243910Sdougm  *
24253910Sdougm  * convert a string to upper case (inplace).
24263910Sdougm  */
24273910Sdougm 
24283910Sdougm static void
24293910Sdougm fixcaseupper(char *str)
24303910Sdougm {
24313910Sdougm 	while (*str) {
24324345Sdougm 		*str = toupper(*str);
24334345Sdougm 		str++;
24343910Sdougm 	}
24353910Sdougm }
24363910Sdougm 
24373910Sdougm /*
24384241Sdougm  * skipwhitespace(str)
24394241Sdougm  *
24404241Sdougm  * Skip leading white space. It is assumed that it is called with a
24414241Sdougm  * valid pointer.
24424241Sdougm  */
24434241Sdougm 
24444241Sdougm static char *
24454241Sdougm skipwhitespace(char *str)
24464241Sdougm {
24474241Sdougm 	while (*str && isspace(*str))
24484241Sdougm 		str++;
24494241Sdougm 
24504241Sdougm 	return (str);
24514241Sdougm }
24524241Sdougm 
24534241Sdougm /*
24544345Sdougm  * extractprop()
24554345Sdougm  *
24564345Sdougm  * Extract the property and value out of the line and create the
24574345Sdougm  * property in the optionset.
24584345Sdougm  */
24596019Sdougm static int
24604345Sdougm extractprop(char *name, char *value)
24614345Sdougm {
24624345Sdougm 	sa_property_t prop;
24634345Sdougm 	int index;
24646019Sdougm 	int ret = SA_OK;
24654345Sdougm 	/*
24664345Sdougm 	 * Remove any leading
24674345Sdougm 	 * white space.
24684345Sdougm 	 */
24694345Sdougm 	name = skipwhitespace(name);
24704345Sdougm 
24714345Sdougm 	index = findprotoopt(name, 0);
24724345Sdougm 	if (index >= 0) {
24734345Sdougm 		fixcaselower(name);
24744345Sdougm 		prop = sa_create_property(proto_options[index].name, value);
24754345Sdougm 		if (prop != NULL)
24766019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
24776019Sdougm 		else
24786019Sdougm 			ret = SA_NO_MEMORY;
24794345Sdougm 	}
24806019Sdougm 	return (ret);
24814345Sdougm }
24824345Sdougm 
24834345Sdougm /*
24843910Sdougm  * initprotofromdefault()
24853910Sdougm  *
24866162Sdougm  * Read the default file(s) and add the defined values to the
24873910Sdougm  * protoset.  Note that default values are known from the built in
24886162Sdougm  * table in case the file doesn't have a definition. Not having the
24896162Sdougm  * /etc/default/nfs file is OK since we have builtin default
24906162Sdougm  * values. The default file will get constructed as needed if values
24916162Sdougm  * are changed from the defaults.
24923910Sdougm  */
24933910Sdougm 
24943910Sdougm static int
24953910Sdougm initprotofromdefault()
24963910Sdougm {
24973910Sdougm 	FILE *nfs;
24983910Sdougm 	char buff[BUFSIZ];
24993910Sdougm 	char *name;
25003910Sdougm 	char *value;
25016019Sdougm 	int ret = SA_OK;
25023910Sdougm 
25033910Sdougm 	protoset = sa_create_protocol_properties("nfs");
25043910Sdougm 
25053910Sdougm 	if (protoset != NULL) {
25064345Sdougm 		nfs = fopen(NFSADMIN, "r");
25074345Sdougm 		if (nfs != NULL) {
25086019Sdougm 			while (ret == SA_OK &&
25096019Sdougm 			    fgets(buff, sizeof (buff), nfs) != NULL) {
25104345Sdougm 				switch (buff[0]) {
25114345Sdougm 				case '\n':
25124345Sdougm 				case '#':
25134345Sdougm 					/* skip */
25144345Sdougm 					break;
25154345Sdougm 				default:
25164345Sdougm 					name = buff;
25174345Sdougm 					buff[strlen(buff) - 1] = '\0';
25184345Sdougm 					value = strchr(name, '=');
25194345Sdougm 					if (value != NULL) {
25204345Sdougm 						*value++ = '\0';
25216019Sdougm 						ret = extractprop(name, value);
25224345Sdougm 					}
25234345Sdougm 				}
25243910Sdougm 			}
25256019Sdougm 			(void) fclose(nfs);
25266019Sdougm 		} else {
25276162Sdougm 			switch (errno) {
25286162Sdougm 			case EPERM:
25296162Sdougm 			case EACCES:
25306162Sdougm 				ret = SA_NO_PERMISSION;
25316162Sdougm 				break;
25326162Sdougm 			case ENOENT:
25336162Sdougm 				break;
25346162Sdougm 			default:
25356162Sdougm 				ret = SA_SYSTEM_ERR;
25366162Sdougm 				break;
25376162Sdougm 			}
25383910Sdougm 		}
25396019Sdougm 	} else {
25406019Sdougm 		ret = SA_NO_MEMORY;
25413910Sdougm 	}
25426019Sdougm 	return (ret);
25433910Sdougm }
25443910Sdougm 
25453910Sdougm /*
25464345Sdougm  * add_defaults()
25473910Sdougm  *
25483910Sdougm  * Add the default values for any property not defined in the parsing
25493910Sdougm  * of the default files. Values are set according to their defined
25503910Sdougm  * types.
25513910Sdougm  */
25523910Sdougm 
25533910Sdougm static void
25543910Sdougm add_defaults()
25553910Sdougm {
25563910Sdougm 	int i;
25573910Sdougm 	char number[MAXDIGITS];
25583910Sdougm 
25593910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
25604345Sdougm 		sa_property_t prop;
25614345Sdougm 		prop = sa_get_protocol_property(protoset,
25624345Sdougm 		    proto_options[i].name);
25634345Sdougm 		if (prop == NULL) {
25644345Sdougm 			/* add the default value */
25654345Sdougm 			switch (proto_options[i].type) {
25664345Sdougm 			case OPT_TYPE_NUMBER:
25674345Sdougm 				(void) snprintf(number, sizeof (number), "%d",
25684345Sdougm 				    proto_options[i].defvalue.intval);
25694345Sdougm 				prop = sa_create_property(proto_options[i].name,
25704345Sdougm 				    number);
25714345Sdougm 				break;
25723910Sdougm 
25734345Sdougm 			case OPT_TYPE_BOOLEAN:
25744345Sdougm 				prop = sa_create_property(proto_options[i].name,
25754345Sdougm 				    proto_options[i].defvalue.intval ?
25764345Sdougm 				    "true" : "false");
25774345Sdougm 				break;
25783910Sdougm 
25794345Sdougm 			case OPT_TYPE_ONOFF:
25804345Sdougm 				prop = sa_create_property(proto_options[i].name,
25814345Sdougm 				    proto_options[i].defvalue.intval ?
25824345Sdougm 				    "on" : "off");
25834345Sdougm 				break;
25843910Sdougm 
25854345Sdougm 			default:
25864345Sdougm 				/* treat as strings of zero length */
25874345Sdougm 				prop = sa_create_property(proto_options[i].name,
25884345Sdougm 				    "");
25894345Sdougm 				break;
25904345Sdougm 			}
25914345Sdougm 			if (prop != NULL)
25924345Sdougm 				(void) sa_add_protocol_property(protoset, prop);
25933910Sdougm 		}
25943910Sdougm 	}
25953910Sdougm }
25963910Sdougm 
25973910Sdougm static void
25983910Sdougm free_protoprops()
25993910Sdougm {
26005179Sdougm 	if (protoset != NULL) {
26015179Sdougm 		xmlFreeNode(protoset);
26025179Sdougm 		protoset = NULL;
26035179Sdougm 	}
26043910Sdougm }
26053910Sdougm 
26063910Sdougm /*
26073910Sdougm  * nfs_init()
26083910Sdougm  *
26093910Sdougm  * Initialize the NFS plugin.
26103910Sdougm  */
26113910Sdougm 
26123910Sdougm static int
26133910Sdougm nfs_init()
26143910Sdougm {
26153910Sdougm 	int ret = SA_OK;
26163910Sdougm 
26176162Sdougm 	if (sa_plugin_ops.sa_init != nfs_init) {
26184345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
26194345Sdougm 		    "NFS plugin not properly initialized\n"));
26206162Sdougm 		return (SA_CONFIG_ERR);
26216162Sdougm 	}
26223910Sdougm 
26233910Sdougm 	ret = initprotofromdefault();
26246162Sdougm 	if (ret != SA_OK) {
26256162Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
26266162Sdougm 		    "NFS plugin problem with default file: %s\n"),
26276162Sdougm 		    sa_errorstr(ret));
26286162Sdougm 		ret = SA_OK;
26296162Sdougm 	}
26306162Sdougm 	add_defaults();
26313910Sdougm 
26323910Sdougm 	return (ret);
26333910Sdougm }
26343910Sdougm 
26353910Sdougm /*
26363910Sdougm  * nfs_fini()
26373910Sdougm  *
26383910Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
26393910Sdougm  */
26403910Sdougm 
26413910Sdougm static void
26423910Sdougm nfs_fini()
26433910Sdougm {
26443910Sdougm 	free_protoprops();
26453910Sdougm }
26463910Sdougm 
26473910Sdougm /*
26483910Sdougm  * nfs_get_proto_set()
26493910Sdougm  *
26503910Sdougm  * Return an optionset with all the protocol specific properties in
26513910Sdougm  * it.
26523910Sdougm  */
26533910Sdougm 
26543910Sdougm static sa_protocol_properties_t
26553910Sdougm nfs_get_proto_set()
26563910Sdougm {
26573910Sdougm 	return (protoset);
26583910Sdougm }
26593910Sdougm 
26603910Sdougm struct deffile {
26613910Sdougm 	struct deffile *next;
26623910Sdougm 	char *line;
26633910Sdougm };
26643910Sdougm 
26653910Sdougm /*
26663910Sdougm  * read_default_file(fname)
26673910Sdougm  *
26683910Sdougm  * Read the specified default file. We return a list of entries. This
26693910Sdougm  * get used for adding or removing values.
26703910Sdougm  */
26713910Sdougm 
26723910Sdougm static struct deffile *
26733910Sdougm read_default_file(char *fname)
26743910Sdougm {
26753910Sdougm 	FILE *file;
26763910Sdougm 	struct deffile *defs = NULL;
26773910Sdougm 	struct deffile *newdef;
26783910Sdougm 	struct deffile *prevdef = NULL;
26793910Sdougm 	char buff[BUFSIZ * 2];
26803910Sdougm 
26813910Sdougm 	file = fopen(fname, "r");
26823910Sdougm 	if (file != NULL) {
26834345Sdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
26844345Sdougm 			newdef = (struct deffile *)calloc(1,
26854345Sdougm 			    sizeof (struct deffile));
26864345Sdougm 			if (newdef != NULL) {
26874345Sdougm 				/* Make sure we skip any leading whitespace. */
26884345Sdougm 				newdef->line = strdup(skipwhitespace(buff));
26894345Sdougm 				if (defs == NULL) {
26904345Sdougm 					prevdef = defs = newdef;
26914345Sdougm 				} else {
26924345Sdougm 					prevdef->next = newdef;
26934345Sdougm 					prevdef = newdef;
26944345Sdougm 				}
26954345Sdougm 			}
26963910Sdougm 		}
26976162Sdougm 		(void) fclose(file);
26986162Sdougm 	} else {
26996162Sdougm 		int ret = SA_OK;
27006162Sdougm 		switch (errno) {
27016162Sdougm 		case EPERM:
27026162Sdougm 		case EACCES:
27036162Sdougm 			ret = SA_NO_PERMISSION;
27046162Sdougm 			break;
27056162Sdougm 		case ENOENT:
27066162Sdougm 			break;
27076162Sdougm 		default:
27086162Sdougm 			ret = SA_SYSTEM_ERR;
27096162Sdougm 			break;
27106162Sdougm 		}
27116162Sdougm 		if (ret == SA_OK) {
27126162Sdougm 			/* Want at least one comment line */
27136162Sdougm 			defs = (struct deffile *)
27146162Sdougm 			    calloc(1, sizeof (struct deffile));
27156162Sdougm 			defs->line = strdup("# NFS default file\n");
27166162Sdougm 		}
27173910Sdougm 	}
27183910Sdougm 	return (defs);
27193910Sdougm }
27203910Sdougm 
27213910Sdougm static void
27223910Sdougm free_default_file(struct deffile *defs)
27233910Sdougm {
27243910Sdougm 	struct deffile *curdefs = NULL;
27253910Sdougm 
27263910Sdougm 	while (defs != NULL) {
27274345Sdougm 		curdefs = defs;
27284345Sdougm 		defs = defs->next;
27294345Sdougm 		if (curdefs->line != NULL)
27304345Sdougm 			free(curdefs->line);
27314345Sdougm 		free(curdefs);
27323910Sdougm 	}
27333910Sdougm }
27343910Sdougm 
27353910Sdougm /*
27363910Sdougm  * write_default_file(fname, defs)
27373910Sdougm  *
27383910Sdougm  * Write the default file back.
27393910Sdougm  */
27403910Sdougm 
27413910Sdougm static int
27423910Sdougm write_default_file(char *fname, struct deffile *defs)
27433910Sdougm {
27443910Sdougm 	FILE *file;
27453910Sdougm 	int ret = SA_OK;
27463910Sdougm 	sigset_t old, new;
27473910Sdougm 
27483910Sdougm 	file = fopen(fname, "w+");
27493910Sdougm 	if (file != NULL) {
27504345Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
27514345Sdougm 		(void) sigaddset(&new, SIGHUP);
27524345Sdougm 		(void) sigaddset(&new, SIGINT);
27534345Sdougm 		(void) sigaddset(&new, SIGQUIT);
27544345Sdougm 		(void) sigaddset(&new, SIGTSTP);
27554345Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
27564345Sdougm 		while (defs != NULL) {
27574345Sdougm 			(void) fputs(defs->line, file);
27584345Sdougm 			defs = defs->next;
27594345Sdougm 		}
27604345Sdougm 		(void) fsync(fileno(file));
27614345Sdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
27624345Sdougm 		(void) fclose(file);
27633910Sdougm 	} else {
27644345Sdougm 		switch (errno) {
27654345Sdougm 		case EPERM:
27664345Sdougm 		case EACCES:
27674345Sdougm 			ret = SA_NO_PERMISSION;
27684345Sdougm 			break;
27694345Sdougm 		default:
27704345Sdougm 			ret = SA_SYSTEM_ERR;
27714345Sdougm 		}
27723910Sdougm 	}
27733910Sdougm 	return (ret);
27743910Sdougm }
27753910Sdougm 
27763910Sdougm 
27773910Sdougm /*
27783910Sdougm  * set_default_file_value(tag, value)
27793910Sdougm  *
27803910Sdougm  * Set the default file value for tag to value. Then rewrite the file.
27813910Sdougm  * tag and value are always set.  The caller must ensure this.
27823910Sdougm  */
27833910Sdougm 
27843910Sdougm #define	MAX_STRING_LENGTH	256
27853910Sdougm static int
27863910Sdougm set_default_file_value(char *tag, char *value)
27873910Sdougm {
27883910Sdougm 	int ret = SA_OK;
27893910Sdougm 	struct deffile *root;
27903910Sdougm 	struct deffile *defs;
27913910Sdougm 	struct deffile *prev;
27923910Sdougm 	char string[MAX_STRING_LENGTH];
27933910Sdougm 	int len;
27946162Sdougm 	boolean_t update = B_FALSE;
27953910Sdougm 
27963910Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
27973910Sdougm 	len = strlen(string);
27983910Sdougm 
27993910Sdougm 	root = defs = read_default_file(NFSADMIN);
28003910Sdougm 	if (root == NULL) {
28016162Sdougm 		switch (errno) {
28026162Sdougm 		case EPERM:
28036162Sdougm 		case EACCES:
28044345Sdougm 			ret = SA_NO_PERMISSION;
28056162Sdougm 			break;
28066162Sdougm 		default:
28076162Sdougm 			ret = SA_NO_MEMORY;
28086162Sdougm 			break;
28096162Sdougm 		}
28106162Sdougm 		return (ret);
28116162Sdougm 	}
28126162Sdougm 
28136162Sdougm 	while (defs != NULL) {
28146162Sdougm 		if (defs->line != NULL &&
28156162Sdougm 		    strncasecmp(defs->line, string, len) == 0) {
28166162Sdougm 			/* replace with the new value */
28176162Sdougm 			free(defs->line);
28186162Sdougm 			fixcaseupper(tag);
28196162Sdougm 			(void) snprintf(string, sizeof (string),
28206162Sdougm 			    "%s=%s\n", tag, value);
28216162Sdougm 			string[MAX_STRING_LENGTH - 1] = '\0';
28226162Sdougm 			defs->line = strdup(string);
28236162Sdougm 			update = B_TRUE;
28246162Sdougm 			break;
28256162Sdougm 		}
28266162Sdougm 		defs = defs->next;
28276162Sdougm 	}
28286162Sdougm 	if (!update) {
28296162Sdougm 		defs = root;
28306162Sdougm 		/* didn't find, so see if it is a comment */
28316162Sdougm 		(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
28326162Sdougm 		len = strlen(string);
28333910Sdougm 		while (defs != NULL) {
28346162Sdougm 			if (strncasecmp(defs->line, string, len) == 0) {
28354345Sdougm 				/* replace with the new value */
28364345Sdougm 				free(defs->line);
28374345Sdougm 				fixcaseupper(tag);
28384345Sdougm 				(void) snprintf(string, sizeof (string),
28393910Sdougm 				    "%s=%s\n", tag, value);
28404345Sdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
28414345Sdougm 				defs->line = strdup(string);
28426162Sdougm 				update = B_TRUE;
28434345Sdougm 				break;
28444345Sdougm 			}
28454345Sdougm 			defs = defs->next;
28463910Sdougm 		}
28476162Sdougm 	}
28486162Sdougm 	if (!update) {
28496162Sdougm 		fixcaseupper(tag);
28506162Sdougm 		(void) snprintf(string, sizeof (string), "%s=%s\n",
28516162Sdougm 		    tag, value);
28526162Sdougm 		prev = root;
28536162Sdougm 		while (prev->next != NULL)
28546162Sdougm 			prev = prev->next;
28556162Sdougm 		defs = malloc(sizeof (struct deffile));
28566162Sdougm 		prev->next = defs;
28576162Sdougm 		if (defs != NULL) {
28586162Sdougm 			defs->next = NULL;
28596162Sdougm 			defs->line = strdup(string);
28606162Sdougm 			update = B_TRUE;
28613910Sdougm 		}
28623910Sdougm 	}
28636162Sdougm 	if (update) {
28646162Sdougm 		ret = write_default_file(NFSADMIN, root);
28656162Sdougm 	}
28666162Sdougm 	free_default_file(root);
28673910Sdougm 	return (ret);
28683910Sdougm }
28693910Sdougm 
28703910Sdougm /*
28713910Sdougm  * service_in_state(service, chkstate)
28723910Sdougm  *
28733910Sdougm  * Want to know if the specified service is in the desired state
28743910Sdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
28753910Sdougm  * isn't.
28763910Sdougm  */
28773910Sdougm static int
28783910Sdougm service_in_state(char *service, const char *chkstate)
28793910Sdougm {
28803910Sdougm 	char *state;
28813910Sdougm 	int ret = B_FALSE;
28823910Sdougm 
28833910Sdougm 	state = smf_get_state(service);
28843910Sdougm 	if (state != NULL) {
28854345Sdougm 		/* got the state so get the equality for the return value */
28864345Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
28874345Sdougm 		free(state);
28883910Sdougm 	}
28893910Sdougm 	return (ret);
28903910Sdougm }
28913910Sdougm 
28923910Sdougm /*
28933910Sdougm  * restart_service(svcs)
28943910Sdougm  *
28953910Sdougm  * Walk through the bit mask of services that need to be restarted in
28963910Sdougm  * order to use the new property values. Some properties affect
28973910Sdougm  * multiple daemons. Should only restart a service if it is currently
28983910Sdougm  * enabled (online).
28993910Sdougm  */
29003910Sdougm 
29013910Sdougm static void
29023910Sdougm restart_service(uint32_t svcs)
29033910Sdougm {
29043910Sdougm 	uint32_t mask;
29053910Sdougm 	int ret;
29063910Sdougm 	char *service;
29073910Sdougm 
29083910Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
29094345Sdougm 		switch (svcs & mask) {
29104345Sdougm 		case SVC_LOCKD:
29114345Sdougm 			service = LOCKD;
29124345Sdougm 			break;
29134345Sdougm 		case SVC_STATD:
29144345Sdougm 			service = STATD;
29154345Sdougm 			break;
29164345Sdougm 		case SVC_NFSD:
29174345Sdougm 			service = NFSD;
29184345Sdougm 			break;
29194345Sdougm 		case SVC_MOUNTD:
29204345Sdougm 			service = MOUNTD;
29214345Sdougm 			break;
29224345Sdougm 		case SVC_NFS4CBD:
29234345Sdougm 			service = NFS4CBD;
29244345Sdougm 			break;
29254345Sdougm 		case SVC_NFSMAPID:
29264345Sdougm 			service = NFSMAPID;
29274345Sdougm 			break;
29284345Sdougm 		case SVC_RQUOTAD:
29294345Sdougm 			service = RQUOTAD;
29304345Sdougm 			break;
29314345Sdougm 		case SVC_NFSLOGD:
29324345Sdougm 			service = NFSLOGD;
29334345Sdougm 			break;
2934*11291SRobert.Thurlow@Sun.COM 		case SVC_REPARSED:
2935*11291SRobert.Thurlow@Sun.COM 			service = REPARSED;
2936*11291SRobert.Thurlow@Sun.COM 			break;
29374345Sdougm 		default:
29384345Sdougm 			continue;
29394345Sdougm 		}
29403910Sdougm 
29413910Sdougm 		/*
29423910Sdougm 		 * Only attempt to restart the service if it is
29433910Sdougm 		 * currently running. In the future, it may be
29443910Sdougm 		 * desirable to use smf_refresh_instance if the NFS
29453910Sdougm 		 * services ever implement the refresh method.
29463910Sdougm 		 */
29474345Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
29484345Sdougm 			ret = smf_restart_instance(service);
29493910Sdougm 			/*
29504345Sdougm 			 * There are only a few SMF errors at this point, but
29514345Sdougm 			 * it is also possible that a bad value may have put
29524345Sdougm 			 * the service into maintenance if there wasn't an
29534345Sdougm 			 * SMF level error.
29543910Sdougm 			 */
29554345Sdougm 			if (ret != 0) {
29564345Sdougm 				(void) fprintf(stderr,
29574345Sdougm 				    dgettext(TEXT_DOMAIN,
29584345Sdougm 				    "%s failed to restart: %s\n"),
29594345Sdougm 				    scf_strerror(scf_error()));
29604345Sdougm 			} else {
29614345Sdougm 				/*
29624345Sdougm 				 * Check whether it has gone to "maintenance"
29634345Sdougm 				 * mode or not. Maintenance implies something
29644345Sdougm 				 * went wrong.
29654345Sdougm 				 */
29664345Sdougm 				if (service_in_state(service,
29674345Sdougm 				    SCF_STATE_STRING_MAINT)) {
29684345Sdougm 					(void) fprintf(stderr,
29694345Sdougm 					    dgettext(TEXT_DOMAIN,
29704345Sdougm 					    "%s failed to restart\n"),
29714345Sdougm 					    service);
29724345Sdougm 				}
29734345Sdougm 			}
29743910Sdougm 		}
29754345Sdougm 		svcs &= ~mask;
29763910Sdougm 	}
29773910Sdougm }
29783910Sdougm 
29793910Sdougm /*
29803910Sdougm  * nfs_minmax_check(name, value)
29813910Sdougm  *
29823910Sdougm  * Verify that the value for the property specified by index is valid
29833910Sdougm  * relative to the opposite value in the case of a min/max variable.
29843910Sdougm  * Currently, server_minvers/server_maxvers and
29853910Sdougm  * client_minvers/client_maxvers are the only ones to check.
29863910Sdougm  */
29873910Sdougm 
29883910Sdougm static int
29893910Sdougm nfs_minmax_check(int index, int value)
29903910Sdougm {
29913910Sdougm 	int val;
29923910Sdougm 	char *pval;
29933910Sdougm 	sa_property_t prop;
29943910Sdougm 	sa_optionset_t opts;
29953910Sdougm 	int ret = B_TRUE;
29963910Sdougm 
29973910Sdougm 	if (proto_options[index].other != NULL) {
29984345Sdougm 		/* have a property to compare against */
29994345Sdougm 		opts = nfs_get_proto_set();
30004345Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
30013910Sdougm 		/*
30023910Sdougm 		 * If we don't find the property, assume default
30033910Sdougm 		 * values which will work since the max will be at the
30043910Sdougm 		 * max and the min at the min.
30053910Sdougm 		 */
30064345Sdougm 		if (prop != NULL) {
30074345Sdougm 			pval = sa_get_property_attr(prop, "value");
30084345Sdougm 			if (pval != NULL) {
30094345Sdougm 				val = strtoul(pval, NULL, 0);
30104345Sdougm 				if (proto_options[index].compare ==
30114345Sdougm 				    OPT_CMP_LE) {
30124345Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
30134345Sdougm 				} else if (proto_options[index].compare ==
30144345Sdougm 				    OPT_CMP_GE) {
30154345Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
30164345Sdougm 				}
30174345Sdougm 			}
30183910Sdougm 		}
30193910Sdougm 	}
30203910Sdougm 	return (ret);
30213910Sdougm }
30223910Sdougm 
30233910Sdougm /*
30243910Sdougm  * nfs_validate_proto_prop(index, name, value)
30253910Sdougm  *
30265331Samw  * Verify that the property specified by name can take the new
30273910Sdougm  * value. This is a sanity check to prevent bad values getting into
30283910Sdougm  * the default files. All values need to be checked against what is
30293910Sdougm  * allowed by their defined type. If a type isn't explicitly defined
30303910Sdougm  * here, it is treated as a string.
30313910Sdougm  *
30323910Sdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
30333910Sdougm  * within the range specified and potentially against another property
30343910Sdougm  * value as well as specified in the proto_options members other and
30353910Sdougm  * compare.
30363910Sdougm  */
30373910Sdougm 
30383910Sdougm static int
30393910Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
30403910Sdougm {
30413910Sdougm 	int ret = SA_OK;
30423910Sdougm 	char *cp;
30433910Sdougm #ifdef lint
30443910Sdougm 	name = name;
30453910Sdougm #endif
30463910Sdougm 
30473910Sdougm 	switch (proto_options[index].type) {
30483910Sdougm 	case OPT_TYPE_NUMBER:
30494345Sdougm 		if (!is_a_number(value))
30504345Sdougm 			ret = SA_BAD_VALUE;
30514345Sdougm 		else {
30524345Sdougm 			int val;
30534345Sdougm 			val = strtoul(value, NULL, 0);
30544345Sdougm 			if (val < proto_options[index].minval ||
30554345Sdougm 			    val > proto_options[index].maxval)
30564345Sdougm 				ret = SA_BAD_VALUE;
30574345Sdougm 			/*
30584345Sdougm 			 * For server_versmin/server_versmax and
30594345Sdougm 			 * client_versmin/client_versmax, the value of the
30604345Sdougm 			 * min(max) should be checked to be correct relative
30614345Sdougm 			 * to the current max(min).
30624345Sdougm 			 */
30634345Sdougm 			if (!nfs_minmax_check(index, val)) {
30644345Sdougm 				ret = SA_BAD_VALUE;
30654345Sdougm 			}
30663910Sdougm 		}
30674345Sdougm 		break;
30683910Sdougm 
30693910Sdougm 	case OPT_TYPE_DOMAIN:
30703910Sdougm 		/*
30713910Sdougm 		 * needs to be a qualified domain so will have at
30723910Sdougm 		 * least one period and other characters on either
30733910Sdougm 		 * side of it.  A zero length string is also allowed
30743910Sdougm 		 * and is the way to turn off the override.
30753910Sdougm 		 */
30764345Sdougm 		if (strlen(value) == 0)
30774345Sdougm 			break;
30784345Sdougm 		cp = strchr(value, '.');
30794345Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
30804345Sdougm 			ret = SA_BAD_VALUE;
30813910Sdougm 		break;
30823910Sdougm 
30833910Sdougm 	case OPT_TYPE_BOOLEAN:
30844345Sdougm 		if (strlen(value) == 0 ||
30854345Sdougm 		    strcasecmp(value, "true") == 0 ||
30864345Sdougm 		    strcmp(value, "1") == 0 ||
30874345Sdougm 		    strcasecmp(value, "false") == 0 ||
30884345Sdougm 		    strcmp(value, "0") == 0) {
30894345Sdougm 			ret = SA_OK;
30904345Sdougm 		} else {
30914345Sdougm 			ret = SA_BAD_VALUE;
30924345Sdougm 		}
30934345Sdougm 		break;
30943910Sdougm 
30953910Sdougm 	case OPT_TYPE_ONOFF:
30964345Sdougm 		if (strcasecmp(value, "on") != 0 &&
30974345Sdougm 		    strcasecmp(value, "off") != 0) {
30984345Sdougm 			ret = SA_BAD_VALUE;
30994345Sdougm 		}
31004345Sdougm 		break;
31013910Sdougm 
31023910Sdougm 	case OPT_TYPE_PROTOCOL:
31036162Sdougm 		if (strlen(value) != 0 &&
31046162Sdougm 		    strcasecmp(value, "all") != 0 &&
31054345Sdougm 		    strcasecmp(value, "tcp") != 0 &&
31064345Sdougm 		    strcasecmp(value, "udp") != 0)
31074345Sdougm 			ret = SA_BAD_VALUE;
31084345Sdougm 		break;
31093910Sdougm 
31103910Sdougm 	default:
31114345Sdougm 		/* treat as a string */
31124345Sdougm 		break;
31133910Sdougm 	}
31143910Sdougm 	return (ret);
31153910Sdougm }
31163910Sdougm 
31173910Sdougm /*
31183910Sdougm  * nfs_set_proto_prop(prop)
31193910Sdougm  *
31203910Sdougm  * check that prop is valid.
31213910Sdougm  */
31223910Sdougm 
31233910Sdougm static int
31243910Sdougm nfs_set_proto_prop(sa_property_t prop)
31253910Sdougm {
31263910Sdougm 	int ret = SA_OK;
31273910Sdougm 	char *name;
31283910Sdougm 	char *value;
31293910Sdougm 
31303910Sdougm 	name = sa_get_property_attr(prop, "type");
31313910Sdougm 	value = sa_get_property_attr(prop, "value");
31323910Sdougm 	if (name != NULL && value != NULL) {
31334345Sdougm 		int index = findprotoopt(name, 1);
31344345Sdougm 		if (index >= 0) {
31354345Sdougm 			/* should test for valid value */
31364345Sdougm 			ret = nfs_validate_proto_prop(index, name, value);
31374345Sdougm 			if (ret == SA_OK)
31384345Sdougm 				ret = set_default_file_value(
31394345Sdougm 				    proto_options[index].tag, value);
31404345Sdougm 			if (ret == SA_OK)
31414345Sdougm 				restart_service(proto_options[index].svcs);
31424345Sdougm 		}
31433910Sdougm 	}
31443910Sdougm 	if (name != NULL)
31454345Sdougm 		sa_free_attr_string(name);
31463910Sdougm 	if (value != NULL)
31474345Sdougm 		sa_free_attr_string(value);
31483910Sdougm 	return (ret);
31493910Sdougm }
31503910Sdougm 
31513910Sdougm /*
31523910Sdougm  * nfs_get_status()
31533910Sdougm  *
31543910Sdougm  * What is the current status of the nfsd? We use the SMF state here.
31553910Sdougm  * Caller must free the returned value.
31563910Sdougm  */
31573910Sdougm 
31583910Sdougm static char *
31593910Sdougm nfs_get_status()
31603910Sdougm {
31613910Sdougm 	char *state;
31623910Sdougm 	state = smf_get_state(NFSD);
31633910Sdougm 	return (state != NULL ? state : strdup("-"));
31643910Sdougm }
31653910Sdougm 
31663910Sdougm /*
31673910Sdougm  * nfs_space_alias(alias)
31683910Sdougm  *
31693910Sdougm  * Lookup the space (security) name. If it is default, convert to the
31703910Sdougm  * real name.
31713910Sdougm  */
31723910Sdougm 
31733910Sdougm static char *
31743910Sdougm nfs_space_alias(char *space)
31753910Sdougm {
31763910Sdougm 	char *name = space;
31773910Sdougm 	seconfig_t secconf;
31783910Sdougm 
31793910Sdougm 	/*
31803910Sdougm 	 * Only the space named "default" is special. If it is used,
31813910Sdougm 	 * the default needs to be looked up and the real name used.
31823910Sdougm 	 * This is normally "sys" but could be changed.  We always
31833910Sdougm 	 * change defautl to the real name.
31843910Sdougm 	 */
31853910Sdougm 	if (strcmp(space, "default") == 0 &&
31863910Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
31874345Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
31884345Sdougm 			name = secconf.sc_name;
31893910Sdougm 	}
31903910Sdougm 	return (strdup(name));
31913910Sdougm }
31925331Samw 
31935331Samw /*
31945331Samw  * nfs_features()
31955331Samw  *
31965331Samw  * Return a mask of the features required.
31975331Samw  */
31985331Samw 
31995331Samw static uint64_t
32005331Samw nfs_features()
32015331Samw {
32026088Sdougm 	return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);
32035331Samw }
3204