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 /*
235800Sdougm  * Copyright 2008 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[] =
1173910Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL };
1183910Sdougm static char *service_list_logging[] =
1193910Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL };
1203910Sdougm 
1213910Sdougm /*
1223910Sdougm  * option definitions.  Make sure to keep the #define for the option
1233910Sdougm  * index just before the entry it is the index for. Changing the order
1243910Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1253910Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1263910Sdougm  */
1273910Sdougm 
1283910Sdougm struct option_defs optdefs[] = {
1293910Sdougm #define	OPT_RO		0
1303910Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1313910Sdougm #define	OPT_RW		1
1323910Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1333910Sdougm #define	OPT_ROOT	2
1343910Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1353910Sdougm #define	OPT_SECURE	3
1363910Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1373910Sdougm #define	OPT_ANON	4
1383910Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1393910Sdougm #define	OPT_WINDOW	5
1403910Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1413910Sdougm #define	OPT_NOSUID	6
1423910Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1433910Sdougm #define	OPT_ACLOK	7
1443910Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1453910Sdougm #define	OPT_NOSUB	8
1463910Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1473910Sdougm #define	OPT_SEC		9
1483910Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1493910Sdougm #define	OPT_PUBLIC	10
1503910Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1513910Sdougm #define	OPT_INDEX	11
1523910Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1533910Sdougm #define	OPT_LOG		12
1543910Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1553910Sdougm #define	OPT_CKSUM	13
1563910Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
1577961SNatalie.Li@Sun.COM #define	OPT_NONE	14
1587961SNatalie.Li@Sun.COM 	{SHOPT_NONE, OPT_NONE, OPT_TYPE_ACCLIST},
1597961SNatalie.Li@Sun.COM #define	OPT_ROOT_MAPPING	15
1607961SNatalie.Li@Sun.COM 	{SHOPT_ROOT_MAPPING, OPT_ROOT_MAPPING, OPT_TYPE_USER},
1617961SNatalie.Li@Sun.COM #define	OPT_CHARSET_MAP	16
1627961SNatalie.Li@Sun.COM 	{"", OPT_CHARSET_MAP, OPT_TYPE_ACCLIST},
1633910Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
1647961SNatalie.Li@Sun.COM #define	OPT_VOLFH	17
1653910Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1663910Sdougm #endif /* VOLATILE_FH_TEST */
1673910Sdougm 	NULL
1683910Sdougm };
1693910Sdougm 
1703910Sdougm /*
1717961SNatalie.Li@Sun.COM  * Codesets that may need to be converted to UTF-8 for file paths.
1727961SNatalie.Li@Sun.COM  * Add new names here to add new property support. If we ever get a
1737961SNatalie.Li@Sun.COM  * way to query the kernel for character sets, this should become
1747961SNatalie.Li@Sun.COM  * dynamically loaded. Make sure changes here are reflected in
1757961SNatalie.Li@Sun.COM  * cmd/fs.d/nfs/mountd/nfscmd.c
1767961SNatalie.Li@Sun.COM  */
1777961SNatalie.Li@Sun.COM 
1787961SNatalie.Li@Sun.COM static char *legal_conv[] = {
1797961SNatalie.Li@Sun.COM 	"euc-cn",
1807961SNatalie.Li@Sun.COM 	"euc-jp",
1817961SNatalie.Li@Sun.COM 	"euc-jpms",
1827961SNatalie.Li@Sun.COM 	"euc-kr",
1837961SNatalie.Li@Sun.COM 	"euc-tw",
1847961SNatalie.Li@Sun.COM 	"iso8859-1",
1857961SNatalie.Li@Sun.COM 	"iso8859-2",
1867961SNatalie.Li@Sun.COM 	"iso8859-5",
1877961SNatalie.Li@Sun.COM 	"iso8859-6",
1887961SNatalie.Li@Sun.COM 	"iso8859-7",
1897961SNatalie.Li@Sun.COM 	"iso8859-8",
1907961SNatalie.Li@Sun.COM 	"iso8859-9",
1917961SNatalie.Li@Sun.COM 	"iso8859-13",
1927961SNatalie.Li@Sun.COM 	"iso8859-15",
1937961SNatalie.Li@Sun.COM 	"koi8-r",
1947961SNatalie.Li@Sun.COM 	NULL
1957961SNatalie.Li@Sun.COM };
1967961SNatalie.Li@Sun.COM 
1977961SNatalie.Li@Sun.COM /*
1983910Sdougm  * list of properties that are related to security flavors.
1993910Sdougm  */
2003910Sdougm static char *seclist[] = {
2013910Sdougm 	SHOPT_RO,
2023910Sdougm 	SHOPT_RW,
2033910Sdougm 	SHOPT_ROOT,
2043910Sdougm 	SHOPT_WINDOW,
2057961SNatalie.Li@Sun.COM 	SHOPT_NONE,
2067961SNatalie.Li@Sun.COM 	SHOPT_ROOT_MAPPING,
2073910Sdougm 	NULL
2083910Sdougm };
2093910Sdougm 
2103910Sdougm /* structure for list of securities */
2113910Sdougm struct securities {
2123910Sdougm 	sa_security_t security;
2133910Sdougm 	struct securities *next;
2143910Sdougm };
2153910Sdougm 
2163910Sdougm /*
2177961SNatalie.Li@Sun.COM  * findcharset(charset)
2187961SNatalie.Li@Sun.COM  *
2197961SNatalie.Li@Sun.COM  * Returns B_TRUE if the charset is a legal conversion otherwise
2207961SNatalie.Li@Sun.COM  * B_FALSE. This will need to be rewritten to be more efficient when
2217961SNatalie.Li@Sun.COM  * we have a dynamic list of legal conversions.
2227961SNatalie.Li@Sun.COM  */
2237961SNatalie.Li@Sun.COM 
2247961SNatalie.Li@Sun.COM static boolean_t
2257961SNatalie.Li@Sun.COM findcharset(char *charset)
2267961SNatalie.Li@Sun.COM {
2277961SNatalie.Li@Sun.COM 	int i;
2287961SNatalie.Li@Sun.COM 
2297961SNatalie.Li@Sun.COM 	for (i = 0; legal_conv[i] != NULL; i++)
2307961SNatalie.Li@Sun.COM 		if (strcmp(charset, legal_conv[i]) == 0)
2317961SNatalie.Li@Sun.COM 			return (B_TRUE);
2327961SNatalie.Li@Sun.COM 	return (B_FALSE);
2337961SNatalie.Li@Sun.COM }
2347961SNatalie.Li@Sun.COM 
2357961SNatalie.Li@Sun.COM /*
2363910Sdougm  * findopt(name)
2373910Sdougm  *
2383910Sdougm  * Lookup option "name" in the option table and return the table
2393910Sdougm  * index.
2403910Sdougm  */
2413910Sdougm 
2423910Sdougm static int
2433910Sdougm findopt(char *name)
2443910Sdougm {
2453910Sdougm 	int i;
2463910Sdougm 	if (name != NULL) {
2474345Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
2484345Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
2494345Sdougm 				return (i);
2504345Sdougm 		}
2517961SNatalie.Li@Sun.COM 		if (findcharset(name))
2527961SNatalie.Li@Sun.COM 			return (OPT_CHARSET_MAP);
2533910Sdougm 	}
2543910Sdougm 	return (-1);
2553910Sdougm }
2563910Sdougm 
2573910Sdougm /*
2583910Sdougm  * gettype(name)
2593910Sdougm  *
2603910Sdougm  * Return the type of option "name".
2613910Sdougm  */
2623910Sdougm 
2633910Sdougm static int
2643910Sdougm gettype(char *name)
2653910Sdougm {
2663910Sdougm 	int optdef;
2673910Sdougm 
2683910Sdougm 	optdef = findopt(name);
2693910Sdougm 	if (optdef != -1)
2704345Sdougm 		return (optdefs[optdef].type);
2713910Sdougm 	return (OPT_TYPE_ANY);
2723910Sdougm }
2733910Sdougm 
2743910Sdougm /*
2753910Sdougm  * nfs_validate_security_mode(mode)
2763910Sdougm  *
2773910Sdougm  * is the specified mode string a valid one for use with NFS?
2783910Sdougm  */
2793910Sdougm 
2803910Sdougm static int
2813910Sdougm nfs_validate_security_mode(char *mode)
2823910Sdougm {
2833910Sdougm 	seconfig_t secinfo;
2843910Sdougm 	int err;
2853910Sdougm 
2863910Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2873910Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2883910Sdougm 	if (err == SC_NOERROR)
2894345Sdougm 		return (1);
2903910Sdougm 	return (0);
2913910Sdougm }
2923910Sdougm 
2933910Sdougm /*
2943910Sdougm  * nfs_is_security_opt(tok)
2953910Sdougm  *
2963910Sdougm  * check to see if tok represents an option that is only valid in some
2973910Sdougm  * security flavor.
2983910Sdougm  */
2993910Sdougm 
3003910Sdougm static int
3013910Sdougm nfs_is_security_opt(char *tok)
3023910Sdougm {
3033910Sdougm 	int i;
3043910Sdougm 
3053910Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
3064345Sdougm 		if (strcmp(tok, seclist[i]) == 0)
3074345Sdougm 			return (1);
3083910Sdougm 	}
3093910Sdougm 	return (0);
3103910Sdougm }
3113910Sdougm 
3123910Sdougm /*
3133910Sdougm  * find_security(seclist, sec)
3143910Sdougm  *
3153910Sdougm  * Walk the current list of security flavors and return true if it is
3163910Sdougm  * present, else return false.
3173910Sdougm  */
3183910Sdougm 
3193910Sdougm static int
3203910Sdougm find_security(struct securities *seclist, sa_security_t sec)
3213910Sdougm {
3223910Sdougm 	while (seclist != NULL) {
3234345Sdougm 		if (seclist->security == sec)
3244345Sdougm 			return (1);
3254345Sdougm 		seclist = seclist->next;
3263910Sdougm 	}
3273910Sdougm 	return (0);
3283910Sdougm }
3293910Sdougm 
3303910Sdougm /*
3313910Sdougm  * make_security_list(group, securitymodes, proto)
3323910Sdougm  *	go through the list of securitymodes and add them to the
3333910Sdougm  *	group's list of security optionsets. We also keep a list of
3343910Sdougm  *	those optionsets so we don't have to find them later. All of
3353910Sdougm  *	these will get copies of the same properties.
3363910Sdougm  */
3373910Sdougm 
3383910Sdougm static struct securities *
3393910Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
3403910Sdougm {
3413910Sdougm 	char *tok, *next = NULL;
3423910Sdougm 	struct securities *curp, *headp = NULL, *prev;
3433910Sdougm 	sa_security_t check;
3443910Sdougm 	int freetok = 0;
3453910Sdougm 
3463910Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
3474345Sdougm 		next = strchr(tok, ':');
3484345Sdougm 		if (next != NULL)
3494345Sdougm 			*next++ = '\0';
3504345Sdougm 		if (strcmp(tok, "default") == 0) {
3514345Sdougm 			/* resolve default into the real type */
3524345Sdougm 			tok = nfs_space_alias(tok);
3534345Sdougm 			freetok = 1;
3544345Sdougm 		}
3554345Sdougm 		check = sa_get_security(group, tok, proto);
3563910Sdougm 
3574345Sdougm 		/* add to the security list if it isn't there already */
3584345Sdougm 		if (check == NULL || !find_security(headp, check)) {
3594345Sdougm 			curp = (struct securities *)calloc(1,
3604345Sdougm 			    sizeof (struct securities));
3614345Sdougm 			if (curp != NULL) {
3624345Sdougm 				if (check == NULL) {
3634345Sdougm 					curp->security = sa_create_security(
3644345Sdougm 					    group, tok, proto);
3654345Sdougm 				} else {
3664345Sdougm 					curp->security = check;
3674345Sdougm 				}
3684345Sdougm 				/*
3694345Sdougm 				 * note that the first time through the loop,
3704345Sdougm 				 * headp will be NULL and prev will be
3714345Sdougm 				 * undefined.  Since headp is NULL, we set
3724345Sdougm 				 * both it and prev to the curp (first
3734345Sdougm 				 * structure to be allocated).
3744345Sdougm 				 *
3754345Sdougm 				 * later passes through the loop will have
3764345Sdougm 				 * headp not being NULL and prev will be used
3774345Sdougm 				 * to allocate at the end of the list.
3784345Sdougm 				 */
3794345Sdougm 				if (headp == NULL) {
3804345Sdougm 					headp = curp;
3814345Sdougm 					prev = curp;
3824345Sdougm 				} else {
3834345Sdougm 					prev->next = curp;
3844345Sdougm 					prev = curp;
3854345Sdougm 				}
3864345Sdougm 			}
3873910Sdougm 		}
3883910Sdougm 
3894345Sdougm 		if (freetok) {
3904345Sdougm 			freetok = 0;
3914345Sdougm 			sa_free_attr_string(tok);
3924345Sdougm 		}
3933910Sdougm 	}
3943910Sdougm 	return (headp);
3953910Sdougm }
3963910Sdougm 
3973910Sdougm static void
3983910Sdougm free_security_list(struct securities *sec)
3993910Sdougm {
4003910Sdougm 	struct securities *next;
4013910Sdougm 	if (sec != NULL) {
4024345Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
4034345Sdougm 			next = sec->next;
4044345Sdougm 			free(sec);
4054345Sdougm 		}
4063910Sdougm 	}
4073910Sdougm }
4083910Sdougm 
4093910Sdougm /*
4103910Sdougm  * nfs_alistcat(str1, str2, sep)
4113910Sdougm  *
4123910Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
4133910Sdougm  * character. If memory allocation fails, return NULL;
4143910Sdougm  */
4153910Sdougm 
4163910Sdougm static char *
4173910Sdougm nfs_alistcat(char *str1, char *str2, char sep)
4183910Sdougm {
4193910Sdougm 	char *newstr;
4203910Sdougm 	size_t len;
4213910Sdougm 
4223910Sdougm 	len = strlen(str1) + strlen(str2) + 2;
4233910Sdougm 	newstr = (char *)malloc(len);
4243910Sdougm 	if (newstr != NULL)
4254345Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
4263910Sdougm 	return (newstr);
4273910Sdougm }
4283910Sdougm 
4293910Sdougm /*
4303910Sdougm  * add_security_prop(sec, name, value, persist)
4313910Sdougm  *
4323910Sdougm  * Add the property to the securities structure. This accumulates
4333910Sdougm  * properties for as part of parsing legacy options.
4343910Sdougm  */
4353910Sdougm 
4363910Sdougm static int
4373910Sdougm add_security_prop(struct securities *sec, char *name, char *value,
4383910Sdougm 			int persist, int iszfs)
4393910Sdougm {
4403910Sdougm 	sa_property_t prop;
4413910Sdougm 	int ret = SA_OK;
4423910Sdougm 
4433910Sdougm 	for (; sec != NULL; sec = sec->next) {
4444345Sdougm 		if (value == NULL) {
4454345Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
4464345Sdougm 			    strcmp(name, SHOPT_RO) == 0)
4474345Sdougm 				value = "*";
4484345Sdougm 			else
4494345Sdougm 				value = "true";
4504345Sdougm 		}
4513910Sdougm 
4523910Sdougm 		/*
4533910Sdougm 		 * Get the existing property, if it exists, so we can
4543910Sdougm 		 * determine what to do with it. The ro/rw/root
4553910Sdougm 		 * properties can be merged if multiple instances of
4563910Sdougm 		 * these properies are given. For example, if "rw"
4573910Sdougm 		 * exists with a value "host1" and a later token of
4583910Sdougm 		 * rw="host2" is seen, the values are merged into a
4593910Sdougm 		 * single rw="host1:host2".
4603910Sdougm 		 */
4614345Sdougm 		prop = sa_get_property(sec->security, name);
4623910Sdougm 
4634345Sdougm 		if (prop != NULL) {
4644345Sdougm 			char *oldvalue;
4654345Sdougm 			char *newvalue;
4663910Sdougm 
4673910Sdougm 			/*
4684345Sdougm 			 * The security options of ro/rw/root might appear
4694345Sdougm 			 * multiple times. If they do, the values need to be
4704345Sdougm 			 * merged into an access list. If it was previously
4714345Sdougm 			 * empty, the new value alone is added.
4723910Sdougm 			 */
4734345Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
4744345Sdougm 			if (oldvalue != NULL) {
4754345Sdougm 				/*
4764345Sdougm 				 * The general case is to concatenate the new
4774345Sdougm 				 * value onto the old value for multiple
4784345Sdougm 				 * rw(ro/root) properties. A special case
4794345Sdougm 				 * exists when either the old or new is the
4804345Sdougm 				 * "all" case. In the special case, if both
4814345Sdougm 				 * are "all", then it is "all", else if one is
4824345Sdougm 				 * an access-list, that replaces the "all".
4834345Sdougm 				 */
4844345Sdougm 				if (strcmp(oldvalue, "*") == 0) {
4854345Sdougm 					/* Replace old value with new value. */
4864345Sdougm 					newvalue = strdup(value);
4875454Sdougm 				} else if (strcmp(value, "*") == 0 ||
4885454Sdougm 				    strcmp(oldvalue, value) == 0) {
4894345Sdougm 					/*
4904345Sdougm 					 * Keep old value and ignore
4914345Sdougm 					 * the new value.
4924345Sdougm 					 */
4934345Sdougm 					newvalue = NULL;
4944345Sdougm 				} else {
4954345Sdougm 					/*
4964345Sdougm 					 * Make a new list of old plus new
4974345Sdougm 					 * access-list.
4984345Sdougm 					 */
4994345Sdougm 					newvalue = nfs_alistcat(oldvalue,
5004345Sdougm 					    value, ':');
5014345Sdougm 				}
5023910Sdougm 
5034345Sdougm 				if (newvalue != NULL) {
5044345Sdougm 					(void) sa_remove_property(prop);
5054345Sdougm 					prop = sa_create_property(name,
5064345Sdougm 					    newvalue);
5074345Sdougm 					ret = sa_add_property(sec->security,
5084345Sdougm 					    prop);
5094345Sdougm 					free(newvalue);
5104345Sdougm 				}
5114345Sdougm 				if (oldvalue != NULL)
5124345Sdougm 					sa_free_attr_string(oldvalue);
5134345Sdougm 			}
5144345Sdougm 		} else {
5154345Sdougm 			prop = sa_create_property(name, value);
5163910Sdougm 			ret = sa_add_property(sec->security, prop);
5173910Sdougm 		}
5184345Sdougm 		if (ret == SA_OK && !iszfs) {
5194345Sdougm 			ret = sa_commit_properties(sec->security, !persist);
5204345Sdougm 		}
5213910Sdougm 	}
5223910Sdougm 	return (ret);
5233910Sdougm }
5243910Sdougm 
5253910Sdougm /*
5263910Sdougm  * check to see if group/share is persistent.
5273910Sdougm  */
5283910Sdougm static int
5293910Sdougm is_persistent(sa_group_t group)
5303910Sdougm {
5313910Sdougm 	char *type;
5323910Sdougm 	int persist = 1;
5333910Sdougm 
5343910Sdougm 	type = sa_get_group_attr(group, "type");
5353910Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
5364345Sdougm 		persist = 0;
5373910Sdougm 	if (type != NULL)
5384345Sdougm 		sa_free_attr_string(type);
5393910Sdougm 	return (persist);
5403910Sdougm }
5413910Sdougm 
5423910Sdougm /*
5433910Sdougm  * invalid_security(options)
5443910Sdougm  *
5453910Sdougm  * search option string for any invalid sec= type.
5463910Sdougm  * return true (1) if any are not valid else false (0)
5473910Sdougm  */
5483910Sdougm static int
5493910Sdougm invalid_security(char *options)
5503910Sdougm {
5513910Sdougm 	char *copy, *base, *token, *value;
5523910Sdougm 	int ret = 0;
5533910Sdougm 
5543910Sdougm 	copy = strdup(options);
5553910Sdougm 	token = base = copy;
5563910Sdougm 	while (token != NULL && ret == 0) {
5574345Sdougm 		token = strtok(base, ",");
5584345Sdougm 		base = NULL;
5594345Sdougm 		if (token != NULL) {
5604345Sdougm 			value = strchr(token, '=');
5614345Sdougm 			if (value != NULL)
5624345Sdougm 				*value++ = '\0';
5634345Sdougm 			if (strcmp(token, "sec") == 0) {
5644345Sdougm 				/* HAVE security flavors so check them */
5654345Sdougm 				char *tok, *next;
5664345Sdougm 				for (next = NULL, tok = value; tok != NULL;
5674345Sdougm 				    tok = next) {
5684345Sdougm 					next = strchr(tok, ':');
5694345Sdougm 					if (next != NULL)
5704345Sdougm 						*next++ = '\0';
5714345Sdougm 					ret = !nfs_validate_security_mode(tok);
5724345Sdougm 					if (ret)
5734345Sdougm 						break;
5744345Sdougm 				}
5754345Sdougm 			}
5763910Sdougm 		}
5773910Sdougm 	}
5783910Sdougm 	if (copy != NULL)
5794345Sdougm 		free(copy);
5803910Sdougm 	return (ret);
5813910Sdougm }
5823910Sdougm 
5833910Sdougm /*
5843910Sdougm  * nfs_parse_legacy_options(group, options)
5853910Sdougm  *
5863910Sdougm  * Parse the old style options into internal format and store on the
5873910Sdougm  * specified group.  Group could be a share for full legacy support.
5883910Sdougm  */
5893910Sdougm 
5903910Sdougm static int
5913910Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5923910Sdougm {
5934704Sdougm 	char *dup;
5943910Sdougm 	char *base;
5953910Sdougm 	char *token;
5963910Sdougm 	sa_optionset_t optionset;
5973910Sdougm 	struct securities *security_list = NULL;
5983910Sdougm 	sa_property_t prop;
5993910Sdougm 	int ret = SA_OK;
6003910Sdougm 	int iszfs = 0;
6013910Sdougm 	sa_group_t parent;
6023910Sdougm 	int persist = 0;
6033910Sdougm 	char *lasts;
6043910Sdougm 
6053910Sdougm 	/* do we have an existing optionset? */
6063910Sdougm 	optionset = sa_get_optionset(group, "nfs");
6073910Sdougm 	if (optionset == NULL) {
6084345Sdougm 		/* didn't find existing optionset so create one */
6094345Sdougm 		optionset = sa_create_optionset(group, "nfs");
6103910Sdougm 	} else {
6113910Sdougm 		/*
6125331Samw 		 * Have an existing optionset . Ideally, we would need
6135331Samw 		 * to compare options in order to detect errors. For
6145331Samw 		 * now, we assume that the first optionset is the
6155331Samw 		 * correct one and the others will be the same. An
6165331Samw 		 * empty optionset is the same as no optionset so we
6175331Samw 		 * don't want to exit in that case. Getting an empty
6185331Samw 		 * optionset can occur with ZFS property checking.
6193910Sdougm 		 */
6205331Samw 		if (sa_get_property(optionset, NULL) != NULL)
6215331Samw 			return (ret);
6223910Sdougm 	}
6233910Sdougm 
6243910Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
6253910Sdougm 		/*
6263910Sdougm 		 * there is a special case of only the option "rw"
6273910Sdougm 		 * being the default option. We don't have to do
6283910Sdougm 		 * anything.
6293910Sdougm 		 */
6304345Sdougm 		return (ret);
6313910Sdougm 	}
6323910Sdougm 
6333910Sdougm 	/*
6343910Sdougm 	 * check if security types are present and validate them. If
6353910Sdougm 	 * any are not legal, fail.
6363910Sdougm 	 */
6373910Sdougm 
6383910Sdougm 	if (invalid_security(options)) {
6394345Sdougm 		return (SA_INVALID_SECURITY);
6403910Sdougm 	}
6413910Sdougm 
6423910Sdougm 	/*
6433910Sdougm 	 * in order to not attempt to change ZFS properties unless
6443910Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
6453910Sdougm 	 */
6463910Sdougm 	if (sa_is_share(group)) {
6474345Sdougm 		char *zfs;
6484345Sdougm 		parent = sa_get_parent_group(group);
6494345Sdougm 		if (parent != NULL) {
6504345Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
6514345Sdougm 			if (zfs != NULL) {
6524345Sdougm 				sa_free_attr_string(zfs);
6534345Sdougm 				iszfs++;
6544345Sdougm 			}
6553910Sdougm 		}
6563910Sdougm 	} else {
6574345Sdougm 		iszfs = sa_group_is_zfs(group);
6583910Sdougm 	}
6593910Sdougm 
6604704Sdougm 	/* We need a copy of options for the next part. */
6614704Sdougm 	dup = strdup(options);
6624704Sdougm 	if (dup == NULL)
6634704Sdougm 		return (SA_NO_MEMORY);
6644704Sdougm 
6653910Sdougm 	/*
6663910Sdougm 	 * we need to step through each option in the string and then
6673910Sdougm 	 * add either the option or the security option as needed. If
6683910Sdougm 	 * this is not a persistent share, don't commit to the
6693910Sdougm 	 * repository. If there is an error, we also want to abort the
6703910Sdougm 	 * processing and report it.
6713910Sdougm 	 */
6723910Sdougm 	persist = is_persistent(group);
6733910Sdougm 	base = dup;
6743910Sdougm 	token = dup;
6753910Sdougm 	lasts = NULL;
6763910Sdougm 	while (token != NULL && ret == SA_OK) {
6774345Sdougm 		ret = SA_OK;
6784345Sdougm 		token = strtok_r(base, ",", &lasts);
6794345Sdougm 		base = NULL;
6804345Sdougm 		if (token != NULL) {
6814345Sdougm 			char *value;
6823910Sdougm 			/*
6834345Sdougm 			 * if the option has a value, it will have an '=' to
6844345Sdougm 			 * separate the name from the value. The following
6854345Sdougm 			 * code will result in value != NULL and token
6864345Sdougm 			 * pointing to just the name if there is a value.
6873910Sdougm 			 */
6884345Sdougm 			value = strchr(token, '=');
6894345Sdougm 			if (value != NULL) {
6904345Sdougm 				*value++ = '\0';
6914345Sdougm 			}
6924345Sdougm 			if (strcmp(token, "sec") == 0 ||
6934345Sdougm 			    strcmp(token, "secure") == 0) {
6943910Sdougm 				/*
6954345Sdougm 				 * Once in security parsing, we only
6964345Sdougm 				 * do security. We do need to move
6974345Sdougm 				 * between the security node and the
6984345Sdougm 				 * toplevel. The security tag goes on
6994345Sdougm 				 * the root while the following ones
7004345Sdougm 				 * go on the security.
7013910Sdougm 				 */
7024345Sdougm 				if (security_list != NULL) {
7034345Sdougm 					/*
7044345Sdougm 					 * have an old list so close it and
7054345Sdougm 					 * start the new
7064345Sdougm 					 */
7074345Sdougm 					free_security_list(security_list);
7084345Sdougm 				}
7094345Sdougm 				if (strcmp(token, "secure") == 0) {
7104345Sdougm 					value = "dh";
7114345Sdougm 				} else {
7124345Sdougm 					if (value == NULL) {
7134345Sdougm 						ret = SA_SYNTAX_ERR;
7144345Sdougm 						break;
7154345Sdougm 					}
7164345Sdougm 				}
7174345Sdougm 				security_list = make_security_list(group,
7184345Sdougm 				    value, "nfs");
7193910Sdougm 			} else {
7204345Sdougm 				/*
7214345Sdougm 				 * Note that the "old" syntax allowed a
7224345Sdougm 				 * default security model This must be
7234345Sdougm 				 * accounted for and internally converted to
7244345Sdougm 				 * "standard" security structure.
7254345Sdougm 				 */
7264345Sdougm 				if (nfs_is_security_opt(token)) {
7274345Sdougm 					if (security_list == NULL) {
7284345Sdougm 						/*
7294345Sdougm 						 * need to have a
7304345Sdougm 						 * security
7314345Sdougm 						 * option. This will
7324345Sdougm 						 * be "closed" when a
7334345Sdougm 						 * defined "sec="
7344345Sdougm 						 * option is
7354345Sdougm 						 * seen. This is
7364345Sdougm 						 * technically an
7374345Sdougm 						 * error but will be
7384345Sdougm 						 * allowed with
7394345Sdougm 						 * warning.
7404345Sdougm 						 */
7414345Sdougm 						security_list =
7424345Sdougm 						    make_security_list(group,
7434345Sdougm 						    "default",
7444345Sdougm 						    "nfs");
7454345Sdougm 					}
7464345Sdougm 					if (security_list != NULL) {
7474345Sdougm 						ret = add_security_prop(
7484345Sdougm 						    security_list, token,
7494345Sdougm 						    value, persist, iszfs);
7504345Sdougm 					} else {
7514345Sdougm 						ret = SA_NO_MEMORY;
7524345Sdougm 					}
7534345Sdougm 				} else {
7544345Sdougm 					/* regular options */
7554345Sdougm 					if (value == NULL) {
7564345Sdougm 						if (strcmp(token, SHOPT_RW) ==
7574345Sdougm 						    0 || strcmp(token,
7584345Sdougm 						    SHOPT_RO) == 0) {
7594345Sdougm 							value = "*";
7604345Sdougm 						} else {
7614345Sdougm 							value = "global";
7624345Sdougm 							if (strcmp(token,
7634345Sdougm 							    SHOPT_LOG) != 0) {
7644345Sdougm 								value = "true";
7654345Sdougm 							}
7664345Sdougm 						}
7674345Sdougm 					}
7684372Sdougm 					/*
7694372Sdougm 					 * In all cases, create the
7704372Sdougm 					 * property specified. If the
7714372Sdougm 					 * value was NULL, the default
7724372Sdougm 					 * value will have been
7734372Sdougm 					 * substituted.
7744372Sdougm 					 */
7754372Sdougm 					prop = sa_create_property(token, value);
7764372Sdougm 					ret =  sa_add_property(optionset, prop);
7774372Sdougm 					if (ret != SA_OK)
7784372Sdougm 						break;
7794372Sdougm 
7804345Sdougm 					if (!iszfs) {
7814345Sdougm 						ret = sa_commit_properties(
7824345Sdougm 						    optionset, !persist);
7834345Sdougm 					}
7844345Sdougm 				}
7853910Sdougm 			}
7863910Sdougm 		}
7873910Sdougm 	}
7883910Sdougm 	if (security_list != NULL)
7894345Sdougm 		free_security_list(security_list);
7904704Sdougm 
7914704Sdougm 	free(dup);
7923910Sdougm 	return (ret);
7933910Sdougm }
7943910Sdougm 
7953910Sdougm /*
7963910Sdougm  * is_a_number(number)
7973910Sdougm  *
7983910Sdougm  * is the string a number in one of the forms we want to use?
7993910Sdougm  */
8003910Sdougm 
8013910Sdougm static int
8023910Sdougm is_a_number(char *number)
8033910Sdougm {
8043910Sdougm 	int ret = 1;
8053910Sdougm 	int hex = 0;
8063910Sdougm 
8073910Sdougm 	if (strncmp(number, "0x", 2) == 0) {
8084345Sdougm 		number += 2;
8094345Sdougm 		hex = 1;
8104345Sdougm 	} else if (*number == '-') {
8114345Sdougm 		number++; /* skip the minus */
8124345Sdougm 	}
8133910Sdougm 	while (ret == 1 && *number != '\0') {
8144345Sdougm 		if (hex) {
8154345Sdougm 			ret = isxdigit(*number++);
8164345Sdougm 		} else {
8174345Sdougm 			ret = isdigit(*number++);
8184345Sdougm 		}
8193910Sdougm 	}
8203910Sdougm 	return (ret);
8213910Sdougm }
8223910Sdougm 
8233910Sdougm /*
8243910Sdougm  * Look for the specified tag in the configuration file. If it is found,
8253910Sdougm  * enable logging and set the logging configuration information for exp.
8263910Sdougm  */
8273910Sdougm static void
8283910Sdougm configlog(struct exportdata *exp, char *tag)
8293910Sdougm {
8303910Sdougm 	nfsl_config_t *configlist = NULL, *configp;
8313910Sdougm 	int error = 0;
8323910Sdougm 	char globaltag[] = DEFAULTTAG;
8333910Sdougm 
8343910Sdougm 	/*
8353910Sdougm 	 * Sends config errors to stderr
8363910Sdougm 	 */
8373910Sdougm 	nfsl_errs_to_syslog = B_FALSE;
8383910Sdougm 
8393910Sdougm 	/*
8403910Sdougm 	 * get the list of configuration settings
8413910Sdougm 	 */
8423910Sdougm 	error = nfsl_getconfig_list(&configlist);
8433910Sdougm 	if (error) {
8443910Sdougm 		(void) fprintf(stderr,
8454345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
8464345Sdougm 		    strerror(error));
8473910Sdougm 	}
8483910Sdougm 
8493910Sdougm 	if (tag == NULL)
8503910Sdougm 		tag = globaltag;
8513910Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
8523910Sdougm 		nfsl_freeconfig_list(&configlist);
8533910Sdougm 		(void) fprintf(stderr,
8544345Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
8553910Sdougm 		/* bad configuration */
8563910Sdougm 		error = ENOENT;
8573910Sdougm 		goto err;
8583910Sdougm 	}
8593910Sdougm 
8603910Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
8613910Sdougm 		error = ENOMEM;
8623910Sdougm 		goto out;
8633910Sdougm 	}
8643910Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
8653910Sdougm 		error = ENOMEM;
8663910Sdougm 		goto out;
8673910Sdougm 	}
8683910Sdougm 	exp->ex_flags |= EX_LOG;
8693910Sdougm 	if (configp->nc_rpclogpath != NULL)
8703910Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8713910Sdougm out:
8723910Sdougm 	if (configlist != NULL)
8734345Sdougm 		nfsl_freeconfig_list(&configlist);
8743910Sdougm 
8753910Sdougm err:
8763910Sdougm 	if (error != 0) {
8773910Sdougm 		if (exp->ex_flags != NULL)
8783910Sdougm 			free(exp->ex_tag);
8793910Sdougm 		if (exp->ex_log_buffer != NULL)
8803910Sdougm 			free(exp->ex_log_buffer);
8813910Sdougm 		(void) fprintf(stderr,
8824345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
8834345Sdougm 		    strerror(error));
8843910Sdougm 	}
8853910Sdougm }
8863910Sdougm 
8873910Sdougm /*
8883910Sdougm  * fill_export_from_optionset(export, optionset)
8893910Sdougm  *
8903910Sdougm  * In order to share, we need to set all the possible general options
8913910Sdougm  * into the export structure. Share info will be filled in by the
8923910Sdougm  * caller. Various property values get turned into structure specific
8933910Sdougm  * values.
8943910Sdougm  */
8953910Sdougm 
8963910Sdougm static int
8973910Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
8983910Sdougm {
8993910Sdougm 	sa_property_t option;
9003910Sdougm 	int ret = SA_OK;
9013910Sdougm 
9023910Sdougm 	for (option = sa_get_property(optionset, NULL);
9034345Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
9044345Sdougm 		char *name;
9054345Sdougm 		char *value;
9064345Sdougm 		uint32_t val;
9073910Sdougm 
9084345Sdougm 		/*
9094345Sdougm 		 * since options may be set/reset multiple times, always do an
9104345Sdougm 		 * explicit set or clear of the option. This allows defaults
9115331Samw 		 * to be set and then the protocol specific to override.
9124345Sdougm 		 */
9133910Sdougm 
9144345Sdougm 		name = sa_get_property_attr(option, "type");
9154345Sdougm 		value = sa_get_property_attr(option, "value");
9164345Sdougm 		switch (findopt(name)) {
9174345Sdougm 		case OPT_ANON:
9184345Sdougm 			if (value != NULL && is_a_number(value)) {
9194345Sdougm 				val = strtoul(value, NULL, 0);
9204345Sdougm 			} else {
9214345Sdougm 				struct passwd *pw;
9224345Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
9234345Sdougm 				if (pw != NULL) {
9244345Sdougm 					val = pw->pw_uid;
9254345Sdougm 				} else {
9264345Sdougm 					val = UID_NOBODY;
9274345Sdougm 				}
9284345Sdougm 				endpwent();
9294345Sdougm 			}
9304345Sdougm 			export->ex_anon = val;
9314345Sdougm 			break;
9324345Sdougm 		case OPT_NOSUID:
9334345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9344345Sdougm 			    strcmp(value, "1") == 0))
9354345Sdougm 				export->ex_flags |= EX_NOSUID;
9364345Sdougm 			else
9374345Sdougm 				export->ex_flags &= ~EX_NOSUID;
9384345Sdougm 			break;
9394345Sdougm 		case OPT_ACLOK:
9404345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9414345Sdougm 			    strcmp(value, "1") == 0))
9424345Sdougm 				export->ex_flags |= EX_ACLOK;
9434345Sdougm 			else
9444345Sdougm 				export->ex_flags &= ~EX_ACLOK;
9454345Sdougm 			break;
9464345Sdougm 		case OPT_NOSUB:
9474345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9484345Sdougm 			    strcmp(value, "1") == 0))
9494345Sdougm 				export->ex_flags |= EX_NOSUB;
9504345Sdougm 			else
9514345Sdougm 				export->ex_flags &= ~EX_NOSUB;
9524345Sdougm 			break;
9534345Sdougm 		case OPT_PUBLIC:
9544345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9554345Sdougm 			    strcmp(value, "1") == 0))
9564345Sdougm 				export->ex_flags |= EX_PUBLIC;
9574345Sdougm 			else
9584345Sdougm 				export->ex_flags &= ~EX_PUBLIC;
9594345Sdougm 			break;
9604345Sdougm 		case OPT_INDEX:
9614345Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
9624345Sdougm 			    strchr(value, '/') != NULL)) {
9634345Sdougm 				/* this is an error */
9644345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
9654345Sdougm 				    "NFS: index=\"%s\" not valid;"
9664345Sdougm 				    "must be a filename.\n"),
9674345Sdougm 				    value);
9684345Sdougm 				break;
9694345Sdougm 			}
9704345Sdougm 			if (value != NULL && *value != '\0' &&
9714345Sdougm 			    strcmp(value, ".") != 0) {
9724345Sdougm 				/* valid index file string */
9734345Sdougm 				if (export->ex_index != NULL) {
9744345Sdougm 					/* left over from "default" */
9754345Sdougm 					free(export->ex_index);
9764345Sdougm 				}
9774345Sdougm 				/* remember to free */
9784345Sdougm 				export->ex_index = strdup(value);
9794345Sdougm 				if (export->ex_index == NULL) {
9804345Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
9814345Sdougm 					    "NFS: out of memory setting "
9824345Sdougm 					    "index property\n"));
9834345Sdougm 					break;
9844345Sdougm 				}
9854345Sdougm 				export->ex_flags |= EX_INDEX;
9864345Sdougm 			}
9874345Sdougm 			break;
9884345Sdougm 		case OPT_LOG:
9894345Sdougm 			if (value == NULL)
9904345Sdougm 				value = strdup("global");
9914345Sdougm 			if (value != NULL)
9924345Sdougm 				configlog(export,
9934345Sdougm 				    strlen(value) ? value : "global");
9944345Sdougm 			break;
9957961SNatalie.Li@Sun.COM 		case OPT_CHARSET_MAP:
9967961SNatalie.Li@Sun.COM 			/*
9977961SNatalie.Li@Sun.COM 			 * Set EX_CHARMAP when there is at least one
9987961SNatalie.Li@Sun.COM 			 * charmap conversion property. This will get
9997961SNatalie.Li@Sun.COM 			 * checked by the nfs server when it needs to.
10007961SNatalie.Li@Sun.COM 			 */
10017961SNatalie.Li@Sun.COM 			export->ex_flags |= EX_CHARMAP;
10027961SNatalie.Li@Sun.COM 			break;
10034345Sdougm 		default:
10044345Sdougm 			/* have a syntactic error */
10054345Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
10064345Sdougm 			    "NFS: unrecognized option %s=%s\n"),
10074345Sdougm 			    name, value != NULL ? value : "");
10084345Sdougm 			break;
10093910Sdougm 		}
10104345Sdougm 		if (name != NULL)
10114345Sdougm 			sa_free_attr_string(name);
10123910Sdougm 		if (value != NULL)
10134345Sdougm 			sa_free_attr_string(value);
10143910Sdougm 	}
10153910Sdougm 	return (ret);
10163910Sdougm }
10173910Sdougm 
10183910Sdougm /*
10193910Sdougm  * cleanup_export(export)
10203910Sdougm  *
10213910Sdougm  * Cleanup the allocated areas so we don't leak memory
10223910Sdougm  */
10233910Sdougm 
10243910Sdougm static void
10253910Sdougm cleanup_export(struct exportdata *export)
10263910Sdougm {
10273910Sdougm 	int i;
10283910Sdougm 
10293910Sdougm 	if (export->ex_index != NULL)
10304345Sdougm 		free(export->ex_index);
10313910Sdougm 	if (export->ex_secinfo != NULL) {
10324345Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
10334345Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
10344345Sdougm 				free(export->ex_secinfo[i].s_rootnames);
10354345Sdougm 		free(export->ex_secinfo);
10363910Sdougm 	}
10373910Sdougm }
10383910Sdougm 
10393910Sdougm /*
10403910Sdougm  * Given a seconfig entry and a colon-separated
10413910Sdougm  * list of names, allocate an array big enough
10423910Sdougm  * to hold the root list, then convert each name to
10433910Sdougm  * a principal name according to the security
10443910Sdougm  * info and assign it to an array element.
10453910Sdougm  * Return the array and its size.
10463910Sdougm  */
10473910Sdougm static caddr_t *
10483910Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
10493910Sdougm {
10503910Sdougm 	caddr_t *a;
10513910Sdougm 	int c, i;
10523910Sdougm 	char *host, *p;
10533910Sdougm 
10543910Sdougm 	/*
10553910Sdougm 	 * Count the number of strings in the list.
10563910Sdougm 	 * This is the number of colon separators + 1.
10573910Sdougm 	 */
10583910Sdougm 	c = 1;
10593910Sdougm 	for (p = list; *p; p++)
10603910Sdougm 		if (*p == ':')
10613910Sdougm 			c++;
10623910Sdougm 	*count = c;
10633910Sdougm 
10643910Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
10653910Sdougm 	if (a == NULL) {
10663910Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
10674345Sdougm 		    "get_rootnames: no memory\n"));
10683910Sdougm 	} else {
10694345Sdougm 		for (i = 0; i < c; i++) {
10704345Sdougm 			host = strtok(list, ":");
10714345Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
10724345Sdougm 				free(a);
10734345Sdougm 				a = NULL;
10744345Sdougm 				break;
10754345Sdougm 			}
10764345Sdougm 			list = NULL;
10773910Sdougm 		}
10783910Sdougm 	}
10793910Sdougm 
10803910Sdougm 	return (a);
10813910Sdougm }
10823910Sdougm 
10833910Sdougm /*
10843910Sdougm  * fill_security_from_secopts(sp, secopts)
10853910Sdougm  *
10863910Sdougm  * Fill the secinfo structure from the secopts optionset.
10873910Sdougm  */
10883910Sdougm 
10893910Sdougm static int
10903910Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
10913910Sdougm {
10923910Sdougm 	sa_property_t prop;
10933910Sdougm 	char *type;
10943910Sdougm 	int longform;
10953910Sdougm 	int err = SC_NOERROR;
10967961SNatalie.Li@Sun.COM 	uint32_t val;
10973910Sdougm 
10983910Sdougm 	type = sa_get_security_attr(secopts, "sectype");
10993910Sdougm 	if (type != NULL) {
11004345Sdougm 		/* named security type needs secinfo to be filled in */
11014345Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
11024345Sdougm 		sa_free_attr_string(type);
11034345Sdougm 		if (err != SC_NOERROR)
11044345Sdougm 			return (err);
11053910Sdougm 	} else {
11064345Sdougm 		/* default case */
11074345Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
11084345Sdougm 		if (err != SC_NOERROR)
11094345Sdougm 			return (err);
11103910Sdougm 	}
11113910Sdougm 
11123910Sdougm 	err = SA_OK;
11133910Sdougm 	for (prop = sa_get_property(secopts, NULL);
11144345Sdougm 	    prop != NULL && err == SA_OK;
11154345Sdougm 	    prop = sa_get_next_property(prop)) {
11164345Sdougm 		char *name;
11174345Sdougm 		char *value;
11183910Sdougm 
11194345Sdougm 		name = sa_get_property_attr(prop, "type");
11204345Sdougm 		value = sa_get_property_attr(prop, "value");
11213910Sdougm 
11224345Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
11233910Sdougm 
11244345Sdougm 		switch (findopt(name)) {
11254345Sdougm 		case OPT_RO:
11264345Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
11274345Sdougm 			break;
11284345Sdougm 		case OPT_RW:
11294345Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
11304345Sdougm 			break;
11314345Sdougm 		case OPT_ROOT:
11324345Sdougm 			sp->s_flags |= M_ROOT;
11334345Sdougm 			/*
11344345Sdougm 			 * if we are using AUTH_UNIX, handle like other things
11354345Sdougm 			 * such as RO/RW
11364345Sdougm 			 */
11374345Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
11384345Sdougm 				continue;
11394345Sdougm 			/* not AUTH_UNIX */
11404345Sdougm 			if (value != NULL) {
11414345Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
11424345Sdougm 				    value, &sp->s_rootcnt);
11434345Sdougm 				if (sp->s_rootnames == NULL) {
11444345Sdougm 					err = SA_BAD_VALUE;
11454345Sdougm 					(void) fprintf(stderr,
11464345Sdougm 					    dgettext(TEXT_DOMAIN,
11474345Sdougm 					    "Bad root list\n"));
11484345Sdougm 				}
11494345Sdougm 			}
11504345Sdougm 			break;
11517961SNatalie.Li@Sun.COM 		case OPT_NONE:
11527961SNatalie.Li@Sun.COM 			sp->s_flags |= M_NONE;
11537961SNatalie.Li@Sun.COM 			break;
11544345Sdougm 		case OPT_WINDOW:
11554345Sdougm 			if (value != NULL) {
11564345Sdougm 				sp->s_window = atoi(value);
11574345Sdougm 				/* just in case */
11584345Sdougm 				if (sp->s_window < 0)
11594345Sdougm 					sp->s_window = DEF_WIN;
11604345Sdougm 			}
11614345Sdougm 			break;
11627961SNatalie.Li@Sun.COM 		case OPT_ROOT_MAPPING:
11637961SNatalie.Li@Sun.COM 			if (value != NULL && is_a_number(value)) {
11647961SNatalie.Li@Sun.COM 				val = strtoul(value, NULL, 0);
11657961SNatalie.Li@Sun.COM 			} else {
11667961SNatalie.Li@Sun.COM 				struct passwd *pw;
11677961SNatalie.Li@Sun.COM 				pw = getpwnam(value != NULL ? value : "nobody");
11687961SNatalie.Li@Sun.COM 				if (pw != NULL) {
11697961SNatalie.Li@Sun.COM 					val = pw->pw_uid;
11707961SNatalie.Li@Sun.COM 				} else {
11717961SNatalie.Li@Sun.COM 					val = UID_NOBODY;
11727961SNatalie.Li@Sun.COM 				}
11737961SNatalie.Li@Sun.COM 				endpwent();
11747961SNatalie.Li@Sun.COM 			}
11757961SNatalie.Li@Sun.COM 			sp->s_rootid = val;
11767961SNatalie.Li@Sun.COM 			break;
11774345Sdougm 		default:
11784345Sdougm 			break;
11793910Sdougm 		}
11804345Sdougm 		if (name != NULL)
11814345Sdougm 			sa_free_attr_string(name);
11824345Sdougm 		if (value != NULL)
11834345Sdougm 			sa_free_attr_string(value);
11843910Sdougm 	}
11853910Sdougm 	/* if rw/ro options not set, use default of RW */
11863910Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
11874345Sdougm 		sp->s_flags |= M_RW;
11883910Sdougm 	return (err);
11893910Sdougm }
11903910Sdougm 
11913910Sdougm /*
11923910Sdougm  * This is for testing only
11933910Sdougm  * It displays the export structure that
11943910Sdougm  * goes into the kernel.
11953910Sdougm  */
11963910Sdougm static void
11973910Sdougm printarg(char *path, struct exportdata *ep)
11983910Sdougm {
11993910Sdougm 	int i, j;
12003910Sdougm 	struct secinfo *sp;
12013910Sdougm 
12023910Sdougm 	if (debug == 0)
12034345Sdougm 		return;
12043910Sdougm 
12053910Sdougm 	(void) printf("%s:\n", path);
12063910Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
12073910Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
12083910Sdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
12093910Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
12103910Sdougm 	if (ep->ex_flags & EX_NOSUID)
12113910Sdougm 		(void) printf("NOSUID ");
12123910Sdougm 	if (ep->ex_flags & EX_ACLOK)
12133910Sdougm 		(void) printf("ACLOK ");
12143910Sdougm 	if (ep->ex_flags & EX_PUBLIC)
12153910Sdougm 		(void) printf("PUBLIC ");
12163910Sdougm 	if (ep->ex_flags & EX_NOSUB)
12173910Sdougm 		(void) printf("NOSUB ");
12183910Sdougm 	if (ep->ex_flags & EX_LOG)
12193910Sdougm 		(void) printf("LOG ");
12207961SNatalie.Li@Sun.COM 	if (ep->ex_flags & EX_CHARMAP)
12217961SNatalie.Li@Sun.COM 		(void) printf("CHARMAP ");
12223910Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
12233910Sdougm 		(void) printf("LOG_ALLOPS ");
12243910Sdougm 	if (ep->ex_flags == 0)
12253910Sdougm 		(void) printf("(none)");
12263910Sdougm 	(void) 	printf("\n");
12273910Sdougm 	if (ep->ex_flags & EX_LOG) {
12283910Sdougm 		(void) printf("\tex_log_buffer = %s\n",
12294345Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
12303910Sdougm 		(void) printf("\tex_tag = %s\n",
12314345Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
12323910Sdougm 	}
12333910Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
12343910Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
12353910Sdougm 	(void) printf("\n");
12363910Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
12373910Sdougm 		sp = &ep->ex_secinfo[i];
12383910Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
12393910Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
12403910Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
12413910Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
12423910Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
12433910Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
12443910Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
12457961SNatalie.Li@Sun.COM 		if (sp->s_flags & M_NONE) (void) printf("M_NONE ");
12463910Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
12473910Sdougm 		(void) printf("\n");
12483910Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
12497961SNatalie.Li@Sun.COM 		(void) printf("\t\ts_rootid = %d\n", sp->s_rootid);
12503910Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
12513910Sdougm 		(void) fflush(stdout);
12523910Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
12533910Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
12544345Sdougm 			    sp->s_rootnames[j] : "<null>");
12553910Sdougm 		(void) printf("\n\n");
12563910Sdougm 	}
12573910Sdougm }
12583910Sdougm 
12593910Sdougm /*
12603910Sdougm  * count_security(opts)
12613910Sdougm  *
12623910Sdougm  * Count the number of security types (flavors). The optionset has
12633910Sdougm  * been populated with the security flavors as a holding mechanism.
12643910Sdougm  * We later use this number to allocate data structures.
12653910Sdougm  */
12663910Sdougm 
12673910Sdougm static int
12683910Sdougm count_security(sa_optionset_t opts)
12693910Sdougm {
12703910Sdougm 	int count = 0;
12713910Sdougm 	sa_property_t prop;
12723910Sdougm 	if (opts != NULL) {
12734345Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
12744345Sdougm 		    prop = sa_get_next_property(prop)) {
12754345Sdougm 			count++;
12764345Sdougm 		}
12773910Sdougm 	}
12783910Sdougm 	return (count);
12793910Sdougm }
12803910Sdougm 
12813910Sdougm /*
12823910Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
12833910Sdougm  *
12843910Sdougm  * provides a mechanism to format NFS properties into legacy output
12853910Sdougm  * format. If the buffer would overflow, it is reallocated and grown
12863910Sdougm  * as appropriate. Special cases of converting internal form of values
12873910Sdougm  * to those used by "share" are done. this function does one property
12883910Sdougm  * at a time.
12893910Sdougm  */
12903910Sdougm 
12915179Sdougm static int
12923910Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
12933910Sdougm 			sa_property_t prop, int sep)
12943910Sdougm {
12953910Sdougm 	char *name;
12963910Sdougm 	char *value;
12973910Sdougm 	int curlen;
12983910Sdougm 	char *buff = *rbuff;
12993910Sdougm 	size_t buffsize = *rbuffsize;
13005179Sdougm 	int printed = B_FALSE;
13013910Sdougm 
13023910Sdougm 	name = sa_get_property_attr(prop, "type");
13033910Sdougm 	value = sa_get_property_attr(prop, "value");
13043910Sdougm 	if (buff != NULL)
13054345Sdougm 		curlen = strlen(buff);
13063910Sdougm 	else
13074345Sdougm 		curlen = 0;
13083910Sdougm 	if (name != NULL) {
13094345Sdougm 		int len;
13104345Sdougm 		len = strlen(name) + sep;
13113910Sdougm 
13123910Sdougm 		/*
13133910Sdougm 		 * A future RFE would be to replace this with more
13143910Sdougm 		 * generic code and to possibly handle more types.
13153910Sdougm 		 */
13164345Sdougm 		switch (gettype(name)) {
13174345Sdougm 		case OPT_TYPE_BOOLEAN:
13185179Sdougm 			/*
13195179Sdougm 			 * For NFS, boolean value of FALSE means it
13205179Sdougm 			 * doesn't show up in the option list at all.
13215179Sdougm 			 */
13224345Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
13235179Sdougm 				goto skip;
13245179Sdougm 			if (value != NULL) {
13254345Sdougm 				sa_free_attr_string(value);
13265179Sdougm 				value = NULL;
13275179Sdougm 			}
13284345Sdougm 			break;
13294345Sdougm 		case OPT_TYPE_ACCLIST:
13304345Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
13314345Sdougm 				sa_free_attr_string(value);
13324345Sdougm 				value = NULL;
13334345Sdougm 			} else {
13344345Sdougm 				if (value != NULL)
13354345Sdougm 					len += 1 + strlen(value);
13364345Sdougm 			}
13374345Sdougm 			break;
13384345Sdougm 		case OPT_TYPE_LOGTAG:
13394345Sdougm 			if (value != NULL && strlen(value) == 0) {
13404345Sdougm 				sa_free_attr_string(value);
13414345Sdougm 				value = NULL;
13424345Sdougm 			} else {
13434345Sdougm 				if (value != NULL)
13444345Sdougm 					len += 1 + strlen(value);
13454345Sdougm 			}
13464345Sdougm 			break;
13474345Sdougm 		default:
13484345Sdougm 			if (value != NULL)
13494345Sdougm 				len += 1 + strlen(value);
13504345Sdougm 			break;
13513910Sdougm 		}
13524345Sdougm 		while (buffsize <= (curlen + len)) {
13534345Sdougm 			/* need more room */
13544345Sdougm 			buffsize += incr;
13554345Sdougm 			buff = realloc(buff, buffsize);
13564345Sdougm 			if (buff == NULL) {
13574345Sdougm 				/* realloc failed so free everything */
13584345Sdougm 				if (*rbuff != NULL)
13594345Sdougm 					free(*rbuff);
13604345Sdougm 			}
13614345Sdougm 			*rbuff = buff;
13624345Sdougm 			*rbuffsize = buffsize;
13635179Sdougm 			if (buff == NULL)
13645179Sdougm 				goto skip;
13655179Sdougm 
13663910Sdougm 		}
13675179Sdougm 
13684345Sdougm 		if (buff == NULL)
13695179Sdougm 			goto skip;
13705179Sdougm 
13714345Sdougm 		if (value == NULL) {
13724345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
13734345Sdougm 			    "%s%s", sep ? "," : "",
13744345Sdougm 			    name, value != NULL ? value : "");
13754345Sdougm 		} else {
13764345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
13774345Sdougm 			    "%s%s=%s", sep ? "," : "",
13784345Sdougm 			    name, value != NULL ? value : "");
13793910Sdougm 		}
13805179Sdougm 		printed = B_TRUE;
13813910Sdougm 	}
13825179Sdougm skip:
13833910Sdougm 	if (name != NULL)
13844345Sdougm 		sa_free_attr_string(name);
13853910Sdougm 	if (value != NULL)
13864345Sdougm 		sa_free_attr_string(value);
13875179Sdougm 	return (printed);
13883910Sdougm }
13893910Sdougm 
13903910Sdougm /*
13913910Sdougm  * nfs_format_options(group, hier)
13923910Sdougm  *
13933910Sdougm  * format all the options on the group into an old-style option
13943910Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
13953910Sdougm  * options.
13963910Sdougm  */
13973910Sdougm 
13983910Sdougm static char *
13993910Sdougm nfs_format_options(sa_group_t group, int hier)
14003910Sdougm {
14013910Sdougm 	sa_optionset_t options = NULL;
14024345Sdougm 	sa_optionset_t secoptions = NULL;
14033910Sdougm 	sa_property_t prop, secprop;
14044345Sdougm 	sa_security_t security = NULL;
14053910Sdougm 	char *buff;
14063910Sdougm 	size_t buffsize;
14074345Sdougm 	char *sectype = NULL;
14084345Sdougm 	int sep = 0;
14094345Sdougm 
14104345Sdougm 
14114345Sdougm 	buff = malloc(OPT_CHUNK);
14124345Sdougm 	if (buff == NULL) {
14134345Sdougm 		return (NULL);
14144345Sdougm 	}
14154345Sdougm 
14164345Sdougm 	buff[0] = '\0';
14174345Sdougm 	buffsize = OPT_CHUNK;
14184345Sdougm 
14194345Sdougm 	/*
14204345Sdougm 	 * We may have a an optionset relative to this item. format
14214345Sdougm 	 * these if we find them and then add any security definitions.
14224345Sdougm 	 */
14233910Sdougm 
14243910Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
14253910Sdougm 
14263910Sdougm 	/*
14274345Sdougm 	 * do the default set first but skip any option that is also
14284345Sdougm 	 * in the protocol specific optionset.
14293910Sdougm 	 */
14304345Sdougm 	if (options != NULL) {
14314345Sdougm 		for (prop = sa_get_property(options, NULL);
14324345Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
14333910Sdougm 			/*
14344345Sdougm 			 * use this one since we skipped any
14354345Sdougm 			 * of these that were also in
14364345Sdougm 			 * optdefault
14373910Sdougm 			 */
14385179Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
14395179Sdougm 			    prop, sep))
14405179Sdougm 				sep = 1;
14414345Sdougm 			if (buff == NULL) {
14424345Sdougm 				/*
14434345Sdougm 				 * buff could become NULL if there
14444345Sdougm 				 * isn't enough memory for
14454345Sdougm 				 * nfs_sprint_option to realloc()
14464345Sdougm 				 * as necessary. We can't really
14474345Sdougm 				 * do anything about it at this
14484345Sdougm 				 * point so we return NULL.  The
14494345Sdougm 				 * caller should handle the
14504345Sdougm 				 * failure.
14514345Sdougm 				 */
14524345Sdougm 				if (options != NULL)
14534345Sdougm 					sa_free_derived_optionset(
14544345Sdougm 					    options);
14554345Sdougm 				return (buff);
14564345Sdougm 			}
14573910Sdougm 		}
14584345Sdougm 	}
14594345Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
14604345Sdougm 	    "nfs", hier);
14614345Sdougm 	if (secoptions != NULL) {
14623910Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
14634345Sdougm 		    secprop != NULL;
14644345Sdougm 		    secprop = sa_get_next_property(secprop)) {
14654345Sdougm 			sectype = sa_get_property_attr(secprop, "type");
14664345Sdougm 			security =
14674345Sdougm 			    (sa_security_t)sa_get_derived_security(
14684345Sdougm 			    group, sectype, "nfs", hier);
14694345Sdougm 			if (security != NULL) {
14704345Sdougm 				if (sectype != NULL) {
14714345Sdougm 					prop = sa_create_property(
14724345Sdougm 					    "sec", sectype);
14735179Sdougm 					if (prop == NULL)
14745179Sdougm 						goto err;
14755179Sdougm 					if (nfs_sprint_option(&buff,
14765179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
14775179Sdougm 						sep = 1;
14784345Sdougm 					(void) sa_remove_property(prop);
14795179Sdougm 					if (buff == NULL)
14805179Sdougm 						goto err;
14814345Sdougm 				}
14824345Sdougm 				for (prop = sa_get_property(security,
14834345Sdougm 				    NULL); prop != NULL;
14844345Sdougm 				    prop = sa_get_next_property(prop)) {
14855179Sdougm 					if (nfs_sprint_option(&buff,
14865179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
14875179Sdougm 						sep = 1;
14884345Sdougm 					if (buff == NULL)
14894345Sdougm 						goto err;
14904345Sdougm 				}
14914345Sdougm 				sa_free_derived_optionset(security);
14923910Sdougm 			}
14934345Sdougm 			if (sectype != NULL)
14944345Sdougm 				sa_free_attr_string(sectype);
14953910Sdougm 		}
14963910Sdougm 		sa_free_derived_optionset(secoptions);
14973910Sdougm 	}
14984345Sdougm 
14993910Sdougm 	if (options != NULL)
15004345Sdougm 		sa_free_derived_optionset(options);
15014345Sdougm 	return (buff);
15024345Sdougm 
15034345Sdougm err:
15044345Sdougm 	/*
15054345Sdougm 	 * If we couldn't allocate memory for option printing, we need
15064345Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
15074345Sdougm 	 */
15084345Sdougm 	if (secoptions != NULL)
15094345Sdougm 		sa_free_derived_optionset(secoptions);
15104345Sdougm 	if (security != NULL)
15114345Sdougm 		sa_free_derived_optionset(security);
15124345Sdougm 	if (sectype != NULL)
15134345Sdougm 		sa_free_attr_string(sectype);
15144345Sdougm 	if (options != NULL)
15154345Sdougm 		sa_free_derived_optionset(options);
1516*8334SJose.Borrego@Sun.COM 	return (NULL);
15173910Sdougm }
15184345Sdougm 
15193910Sdougm /*
15203910Sdougm  * Append an entry to the nfslogtab file
15213910Sdougm  */
15223910Sdougm static int
15233910Sdougm nfslogtab_add(dir, buffer, tag)
15243910Sdougm 	char *dir, *buffer, *tag;
15253910Sdougm {
15263910Sdougm 	FILE *f;
15273910Sdougm 	struct logtab_ent lep;
15283910Sdougm 	int error = 0;
15293910Sdougm 
15303910Sdougm 	/*
15313910Sdougm 	 * Open the file for update and create it if necessary.
15323910Sdougm 	 * This may leave the I/O offset at the end of the file,
15333910Sdougm 	 * so rewind back to the beginning of the file.
15343910Sdougm 	 */
15353910Sdougm 	f = fopen(NFSLOGTAB, "a+");
15363910Sdougm 	if (f == NULL) {
15373910Sdougm 		error = errno;
15383910Sdougm 		goto out;
15393910Sdougm 	}
15403910Sdougm 	rewind(f);
15413910Sdougm 
15423910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
15433910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15444345Sdougm 		    "share complete, however failed to lock %s "
15454345Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
15463910Sdougm 		error = -1;
15473910Sdougm 		goto out;
15483910Sdougm 	}
15493910Sdougm 
15503910Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
15513910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15524345Sdougm 		    "share complete, however could not deactivate "
15534345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15543910Sdougm 		error = -1;
15553910Sdougm 		goto out;
15563910Sdougm 	}
15573910Sdougm 
15583910Sdougm 	/*
15593910Sdougm 	 * Remove entries matching buffer and sharepoint since we're
15603910Sdougm 	 * going to replace it with perhaps an entry with a new tag.
15613910Sdougm 	 */
15623910Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
15633910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15644345Sdougm 		    "share complete, however could not remove matching "
15654345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15663910Sdougm 		error = -1;
15673910Sdougm 		goto out;
15683910Sdougm 	}
15693910Sdougm 
15703910Sdougm 	/*
15713910Sdougm 	 * Deactivate all active entries matching this sharepoint
15723910Sdougm 	 */
15733910Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
15743910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15754345Sdougm 		    "share complete, however could not deactivate matching "
15764345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15773910Sdougm 		error = -1;
15783910Sdougm 		goto out;
15793910Sdougm 	}
15803910Sdougm 
15813910Sdougm 	lep.le_buffer = buffer;
15823910Sdougm 	lep.le_path = dir;
15833910Sdougm 	lep.le_tag = tag;
15843910Sdougm 	lep.le_state = LES_ACTIVE;
15853910Sdougm 
15863910Sdougm 	/*
15873910Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
15883910Sdougm 	 */
15893910Sdougm 	if (logtab_putent(f, &lep) < 0) {
15903910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15914345Sdougm 		    "share complete, however could not add %s to %s\n"),
15924345Sdougm 		    dir, NFSLOGTAB);
15933910Sdougm 		error = -1;
15943910Sdougm 	}
15953910Sdougm 
15963910Sdougm out:
15973910Sdougm 	if (f != NULL)
15983910Sdougm 		(void) fclose(f);
15993910Sdougm 	return (error);
16003910Sdougm }
16013910Sdougm 
16023910Sdougm /*
16033910Sdougm  * Deactivate an entry from the nfslogtab file
16043910Sdougm  */
16053910Sdougm static int
16063910Sdougm nfslogtab_deactivate(path)
16073910Sdougm 	char *path;
16083910Sdougm {
16093910Sdougm 	FILE *f;
16103910Sdougm 	int error = 0;
16113910Sdougm 
16123910Sdougm 	f = fopen(NFSLOGTAB, "r+");
16133910Sdougm 	if (f == NULL) {
16143910Sdougm 		error = errno;
16153910Sdougm 		goto out;
16163910Sdougm 	}
16173910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
16183910Sdougm 		error = errno;
16193910Sdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
16204345Sdougm 		    "share complete, however could not lock %s for "
16214345Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
16223910Sdougm 		goto out;
16233910Sdougm 	}
16243910Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
16253910Sdougm 		error = -1;
16263910Sdougm 		(void) fprintf(stderr,
16274345Sdougm 		    dgettext(TEXT_DOMAIN,
16284345Sdougm 		    "share complete, however could not "
16294345Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
16303910Sdougm 		goto out;
16313910Sdougm 	}
16323910Sdougm 
16333910Sdougm out:	if (f != NULL)
16343910Sdougm 		(void) fclose(f);
16353910Sdougm 
16363910Sdougm 	return (error);
16373910Sdougm }
16383910Sdougm 
16393910Sdougm /*
16404524Sdougm  * check_public(group, skipshare)
16414524Sdougm  *
16424524Sdougm  * Check the group for any shares that have the public property
16434524Sdougm  * enabled. We skip "skipshare" since that is the one we are
16444524Sdougm  * working with. This is a separate function to make handling
16454524Sdougm  * subgroups simpler. Returns true if there is a share with public.
16464524Sdougm  */
16474524Sdougm static int
16484524Sdougm check_public(sa_group_t group, sa_share_t skipshare)
16494524Sdougm {
16504524Sdougm 	int exists = B_FALSE;
16514524Sdougm 	sa_share_t share;
16524524Sdougm 	sa_optionset_t opt;
16534524Sdougm 	sa_property_t prop;
16544524Sdougm 	char *shared;
16554524Sdougm 
16564524Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
16574524Sdougm 	    share = sa_get_next_share(share)) {
16584524Sdougm 		if (share == skipshare)
16594524Sdougm 			continue;
16604524Sdougm 
16614524Sdougm 		opt = sa_get_optionset(share, "nfs");
16624524Sdougm 		if (opt == NULL)
16634524Sdougm 			continue;
16644524Sdougm 		prop = sa_get_property(opt, "public");
16654524Sdougm 		if (prop == NULL)
16664524Sdougm 			continue;
16674524Sdougm 		shared = sa_get_share_attr(share, "shared");
16684524Sdougm 		if (shared != NULL) {
16694524Sdougm 			exists = strcmp(shared, "true") == 0;
16704524Sdougm 			sa_free_attr_string(shared);
16714524Sdougm 			if (exists == B_TRUE)
16724524Sdougm 				break;
16734524Sdougm 		}
16744524Sdougm 	}
16754524Sdougm 
16764524Sdougm 	return (exists);
16774524Sdougm }
16784524Sdougm 
16794524Sdougm /*
16806214Sdougm  * public_exists(handle, share)
16813910Sdougm  *
16823910Sdougm  * check to see if public option is set on any other share than the
16834524Sdougm  * one specified. Need to check zfs sub-groups as well as the top
16844524Sdougm  * level groups.
16853910Sdougm  */
16863910Sdougm static int
16876214Sdougm public_exists(sa_handle_t handle, sa_share_t skipshare)
16883910Sdougm {
16896214Sdougm 	sa_group_t group = NULL;
16903910Sdougm 
16916271Sdougm 	/*
16926271Sdougm 	 * If we don't have a handle, we can only do syntax check. We
16936271Sdougm 	 * can't check against other shares so we assume OK and will
16946271Sdougm 	 * catch the problem only when we actually try to apply it.
16956271Sdougm 	 */
16963910Sdougm 	if (handle == NULL)
16976271Sdougm 		return (SA_OK);
16983910Sdougm 
16996214Sdougm 	if (skipshare != NULL) {
17006214Sdougm 		group = sa_get_parent_group(skipshare);
17016214Sdougm 		if (group == NULL)
17026214Sdougm 			return (SA_NO_SUCH_GROUP);
17036214Sdougm 	}
17046214Sdougm 
17053910Sdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
17063910Sdougm 	    group = sa_get_next_group(group)) {
17074524Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
17084524Sdougm 		if (sa_group_is_zfs(group)) {
17094524Sdougm 			sa_group_t subgroup;
17104524Sdougm 			for (subgroup = sa_get_sub_group(group);
17114524Sdougm 			    subgroup != NULL;
17124524Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
17134524Sdougm 				if (check_public(subgroup, skipshare))
17144524Sdougm 					return (B_TRUE);
17153910Sdougm 			}
17164524Sdougm 		} else {
17174524Sdougm 			if (check_public(group, skipshare))
17184524Sdougm 				return (B_TRUE);
17193910Sdougm 		}
17203910Sdougm 	}
17214524Sdougm 	return (B_FALSE);
17223910Sdougm }
17233910Sdougm 
17243910Sdougm /*
17253910Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
17263910Sdougm  * implementation that it is to enable the share. This entails
17273910Sdougm  * converting the path and options into the appropriate ioctl
17283910Sdougm  * calls. It is assumed that all error checking of paths, etc. were
17293910Sdougm  * done earlier.
17303910Sdougm  */
17313910Sdougm static int
17323910Sdougm nfs_enable_share(sa_share_t share)
17333910Sdougm {
17343910Sdougm 	struct exportdata export;
17353910Sdougm 	sa_optionset_t secoptlist;
17363910Sdougm 	struct secinfo *sp;
17373910Sdougm 	int num_secinfo;
17383910Sdougm 	sa_optionset_t opt;
17393910Sdougm 	sa_security_t sec;
17403910Sdougm 	sa_property_t prop;
17413910Sdougm 	char *path;
17423910Sdougm 	int err = SA_OK;
17434524Sdougm 	int i;
17444543Smarks 	int iszfs;
17456214Sdougm 	sa_handle_t handle;
17463910Sdougm 
17473910Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
17483910Sdougm 	(void) signal(SIGSYS, SIG_IGN);
17493910Sdougm 
17503910Sdougm 	/* get the path since it is important in several places */
17513910Sdougm 	path = sa_get_share_attr(share, "path");
17523910Sdougm 	if (path == NULL)
17534345Sdougm 		return (SA_NO_SUCH_PATH);
17543910Sdougm 
17554543Smarks 	iszfs = sa_path_is_zfs(path);
17563910Sdougm 	/*
17573910Sdougm 	 * find the optionsets and security sets.  There may not be
17583910Sdougm 	 * any or there could be one or two for each of optionset and
17593910Sdougm 	 * security may have multiple, one per security type per
17603910Sdougm 	 * protocol type.
17613910Sdougm 	 */
17623910Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
17633910Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
17643910Sdougm 	if (secoptlist != NULL)
17654345Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
17663910Sdougm 	else
17674345Sdougm 		num_secinfo = 1;
17683910Sdougm 
17693910Sdougm 	/*
17703910Sdougm 	 * walk through the options and fill in the structure
17713910Sdougm 	 * appropriately.
17723910Sdougm 	 */
17733910Sdougm 
17743910Sdougm 	(void) memset(&export, '\0', sizeof (export));
17753910Sdougm 
17763910Sdougm 	/*
17773910Sdougm 	 * do non-security options first since there is only one after
17783910Sdougm 	 * the derived group is constructed.
17793910Sdougm 	 */
17803910Sdougm 	export.ex_version = EX_CURRENT_VERSION;
17813910Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
17823910Sdougm 	export.ex_index = NULL;
17833910Sdougm 	export.ex_path = path;
17843910Sdougm 	export.ex_pathlen = strlen(path) + 1;
17853910Sdougm 
17863910Sdougm 	if (opt != NULL)
17874345Sdougm 		err = fill_export_from_optionset(&export, opt);
17883910Sdougm 
17893910Sdougm 	/*
17903910Sdougm 	 * check to see if "public" is set. If it is, then make sure
17913910Sdougm 	 * no other share has it set. If it is already used, fail.
17923910Sdougm 	 */
17933910Sdougm 
17946214Sdougm 	handle = sa_find_group_handle((sa_group_t)share);
17956214Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
17964345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
17974345Sdougm 		    "NFS: Cannot share more than one file "
17984345Sdougm 		    "system with 'public' property\n"));
17994345Sdougm 		err = SA_NOT_ALLOWED;
18004345Sdougm 		goto out;
18013910Sdougm 	}
18023910Sdougm 
18034524Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
18043910Sdougm 	if (sp == NULL) {
18054345Sdougm 		err = SA_NO_MEMORY;
18064524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18074524Sdougm 		    "NFS: NFS: no memory for security\n"));
18084524Sdougm 		goto out;
18094524Sdougm 	}
18104524Sdougm 	export.ex_secinfo = sp;
18114524Sdougm 	/* get default secinfo */
18124524Sdougm 	export.ex_seccnt = num_secinfo;
18134524Sdougm 	/*
18144524Sdougm 	 * since we must have one security option defined, we
18154524Sdougm 	 * init to the default and then override as we find
18164524Sdougm 	 * defined security options. This handles the case
18174524Sdougm 	 * where we have no defined options but we need to set
18184524Sdougm 	 * up one.
18194524Sdougm 	 */
18204524Sdougm 	sp[0].s_window = DEF_WIN;
18214524Sdougm 	sp[0].s_rootnames = NULL;
18224524Sdougm 	/* setup a default in case no properties defined */
18234524Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
18244524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18254524Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
18264524Sdougm 		    "get default security mode\n"));
18274524Sdougm 		err = SA_CONFIG_ERR;
18284524Sdougm 	}
18294524Sdougm 	if (secoptlist != NULL) {
18304524Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
18314524Sdougm 		    prop != NULL && i < num_secinfo;
18324524Sdougm 		    prop = sa_get_next_property(prop), i++) {
18334524Sdougm 			char *sectype;
18344345Sdougm 				sectype = sa_get_property_attr(prop, "type");
18354524Sdougm 			/*
18364524Sdougm 			 * if sectype is NULL, we probably
18374524Sdougm 			 * have a memory problem and can't get
18384524Sdougm 			 * the correct values. Rather than
18394524Sdougm 			 * exporting with incorrect security,
18404524Sdougm 			 * don't share it.
18414524Sdougm 			 */
18424524Sdougm 			if (sectype == NULL) {
18434524Sdougm 				err = SA_NO_MEMORY;
18444524Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
18454524Sdougm 				    "NFS: Cannot share %s: "
18464524Sdougm 				    "no memory\n"), path);
18474524Sdougm 				goto out;
18484524Sdougm 			}
18494524Sdougm 			sec = (sa_security_t)sa_get_derived_security(
18504524Sdougm 			    share, sectype, "nfs", 1);
18514524Sdougm 			sp[i].s_window = DEF_WIN;
18524524Sdougm 			sp[i].s_rootcnt = 0;
18534524Sdougm 			sp[i].s_rootnames = NULL;
18544345Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
18554524Sdougm 			if (sec != NULL)
18564524Sdougm 				sa_free_derived_security(sec);
18574524Sdougm 			if (sectype != NULL)
18584524Sdougm 				sa_free_attr_string(sectype);
18593910Sdougm 		}
18604524Sdougm 	}
18614524Sdougm 	/*
18624524Sdougm 	 * when we get here, we can do the exportfs system call and
18634524Sdougm 	 * initiate thinsg. We probably want to enable the nfs.server
18644524Sdougm 	 * service first if it isn't running within SMF.
18654524Sdougm 	 */
18664524Sdougm 	/* check nfs.server status and start if needed */
18674524Sdougm 	/* now add the share to the internal tables */
18684524Sdougm 	printarg(path, &export);
18694524Sdougm 	/*
18704524Sdougm 	 * call the exportfs system call which is implemented
18714524Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
18724524Sdougm 	 */
18734543Smarks 	if (iszfs) {
18744543Smarks 		struct exportfs_args ea;
18754543Smarks 		share_t sh;
18764543Smarks 		char *str;
18774543Smarks 		priv_set_t *priv_effective;
18784543Smarks 		int privileged;
18794543Smarks 
18804543Smarks 		/*
18814543Smarks 		 * If we aren't a privileged user
18824543Smarks 		 * and NFS server service isn't running
18834543Smarks 		 * then print out an error message
18844543Smarks 		 * and return EPERM
18854543Smarks 		 */
18864543Smarks 
18874543Smarks 		priv_effective = priv_allocset();
18884543Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
18894543Smarks 
18904543Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
18914543Smarks 		priv_freeset(priv_effective);
18924543Smarks 
18934543Smarks 		if (!privileged &&
18944543Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
18954543Smarks 			err = 0;
18964543Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
18974543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
18984543Smarks 				    "NFS: Cannot share remote "
18994543Smarks 				    "filesystem: %s\n"), path);
19004543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
19014543Smarks 				    "NFS: Service needs to be enabled "
19024543Smarks 				    "by a privileged user\n"));
19034543Smarks 				err = SA_SYSTEM_ERR;
19044543Smarks 				errno = EPERM;
19054543Smarks 			}
19064543Smarks 			free(str);
19074543Smarks 		}
19084543Smarks 
19094543Smarks 		if (err == 0) {
19104543Smarks 			ea.dname = path;
19114543Smarks 			ea.uex = &export;
19124543Smarks 
19134543Smarks 			sa_sharetab_fill_zfs(share, &sh, "nfs");
19145331Samw 			err = sa_share_zfs(share, path, &sh,
19155331Samw 			    &ea, ZFS_SHARE_NFS);
19164543Smarks 			sa_emptyshare(&sh);
19174543Smarks 		}
19184543Smarks 	} else {
19194543Smarks 		err = exportfs(path, &export);
19204543Smarks 	}
19214543Smarks 
19224543Smarks 	if (err < 0) {
19234524Sdougm 		err = SA_SYSTEM_ERR;
19244524Sdougm 		switch (errno) {
19254524Sdougm 		case EREMOTE:
19264524Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
19274543Smarks 			    "NFS: Cannot share filesystems "
19284543Smarks 			    "in non-global zones: %s\n"), path);
19294543Smarks 			err = SA_NOT_SUPPORTED;
19304524Sdougm 			break;
19314524Sdougm 		case EPERM:
19324524Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
19334345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
19344543Smarks 				    "NFS: Cannot share file systems "
19354524Sdougm 				    "in non-global zones: %s\n"), path);
19364524Sdougm 				err = SA_NOT_SUPPORTED;
19374345Sdougm 				break;
19384345Sdougm 			}
19394524Sdougm 			err = SA_NO_PERMISSION;
19404524Sdougm 			/* FALLTHROUGH */
19414524Sdougm 		default:
19424524Sdougm 			break;
19433910Sdougm 		}
19444524Sdougm 	} else {
19454524Sdougm 		/* update sharetab with an add/modify */
19464543Smarks 		if (!iszfs) {
19474543Smarks 			(void) sa_update_sharetab(share, "nfs");
19484543Smarks 		}
19493910Sdougm 	}
19503910Sdougm 
19513910Sdougm 	if (err == SA_OK) {
19523910Sdougm 		/*
19533910Sdougm 		 * enable services as needed. This should probably be
19543910Sdougm 		 * done elsewhere in order to minimize the calls to
19553910Sdougm 		 * check services.
19563910Sdougm 		 */
19573910Sdougm 		/*
19583910Sdougm 		 * check to see if logging and other services need to
19593910Sdougm 		 * be triggered, but only if there wasn't an
19603910Sdougm 		 * error. This is probably where sharetab should be
19613910Sdougm 		 * updated with the NFS specific entry.
19623910Sdougm 		 */
19634345Sdougm 		if (export.ex_flags & EX_LOG) {
19644345Sdougm 			/* enable logging */
19654345Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
19664345Sdougm 			    export.ex_tag) != 0) {
19674345Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
19684345Sdougm 				    "Could not enable logging for %s\n"),
19694345Sdougm 				    path);
19704345Sdougm 			}
19714345Sdougm 			_check_services(service_list_logging);
19724345Sdougm 		} else {
19734345Sdougm 			/*
19744345Sdougm 			 * don't have logging so remove it from file. It might
19754345Sdougm 			 * not be thre, but that doesn't matter.
19764345Sdougm 			 */
19774345Sdougm 			(void) nfslogtab_deactivate(path);
19784345Sdougm 			_check_services(service_list_default);
19793910Sdougm 		}
19803910Sdougm 	}
19813910Sdougm 
19823910Sdougm out:
19833910Sdougm 	if (path != NULL)
19844345Sdougm 		free(path);
19853910Sdougm 
19863910Sdougm 	cleanup_export(&export);
19873910Sdougm 	if (opt != NULL)
19884345Sdougm 		sa_free_derived_optionset(opt);
19893910Sdougm 	if (secoptlist != NULL)
19904345Sdougm 		(void) sa_destroy_optionset(secoptlist);
19913910Sdougm 	return (err);
19923910Sdougm }
19933910Sdougm 
19943910Sdougm /*
19955800Sdougm  * nfs_disable_share(share, path)
19963910Sdougm  *
19975800Sdougm  * Unshare the specified share. Note that "path" is the same path as
19985800Sdougm  * what is in the "share" object. It is passed in to avoid an
19995800Sdougm  * additional lookup. A missing "path" value makes this a no-op
20005800Sdougm  * function.
20013910Sdougm  */
20023910Sdougm static int
20034543Smarks nfs_disable_share(sa_share_t share, char *path)
20043910Sdougm {
20053910Sdougm 	int err;
20063910Sdougm 	int ret = SA_OK;
20074543Smarks 	int iszfs;
20085800Sdougm 	sa_group_t parent;
20095951Sdougm 	sa_handle_t handle;
20104543Smarks 
20115800Sdougm 	if (path == NULL)
20125800Sdougm 		return (ret);
20134543Smarks 
20145800Sdougm 	/*
20155800Sdougm 	 * If the share is in a ZFS group we need to handle it
20165800Sdougm 	 * differently.  Just being on a ZFS file system isn't
20175800Sdougm 	 * enough since we may be in a legacy share case.
20185800Sdougm 	 */
20195800Sdougm 	parent = sa_get_parent_group(share);
20205800Sdougm 	iszfs = sa_group_is_zfs(parent);
20215800Sdougm 	if (iszfs) {
20225800Sdougm 		struct exportfs_args ea;
20235800Sdougm 		share_t sh = { 0 };
20245800Sdougm 		ea.dname = path;
20255800Sdougm 		ea.uex = NULL;
20265800Sdougm 		sh.sh_path = path;
20275800Sdougm 		sh.sh_fstype = "nfs";
20284543Smarks 
20295800Sdougm 		err = sa_share_zfs(share, path, &sh,
20305800Sdougm 		    &ea, ZFS_UNSHARE_NFS);
20315800Sdougm 	} else {
20325800Sdougm 		err = exportfs(path, NULL);
20335800Sdougm 	}
20345800Sdougm 	if (err < 0) {
20355800Sdougm 		/*
20365800Sdougm 		 * TBD: only an error in some
20375800Sdougm 		 * cases - need better analysis
20385800Sdougm 		 */
20395800Sdougm 		switch (errno) {
20405800Sdougm 		case EPERM:
20415800Sdougm 		case EACCES:
20425800Sdougm 			ret = SA_NO_PERMISSION;
20435800Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
20445800Sdougm 				ret = SA_NOT_SUPPORTED;
20455800Sdougm 			}
20465800Sdougm 			break;
20475800Sdougm 		case EINVAL:
20485800Sdougm 		case ENOENT:
20495800Sdougm 			ret = SA_NO_SUCH_PATH;
20504543Smarks 			break;
20514345Sdougm 			default:
20524345Sdougm 				ret = SA_SYSTEM_ERR;
20534543Smarks 			break;
20543910Sdougm 		}
20555800Sdougm 	}
20565800Sdougm 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
20575951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
20585800Sdougm 		if (!iszfs)
20595951Sdougm 			(void) sa_delete_sharetab(handle, path, "nfs");
20605800Sdougm 		/* just in case it was logged */
20615800Sdougm 		(void) nfslogtab_deactivate(path);
20623910Sdougm 	}
20633910Sdougm 	return (ret);
20643910Sdougm }
20653910Sdougm 
20663910Sdougm /*
20677961SNatalie.Li@Sun.COM  * check_rorwnone(v1, v2, v3)
20687961SNatalie.Li@Sun.COM  *
20697961SNatalie.Li@Sun.COM  * check ro vs rw vs none values.  Over time this may get beefed up.
20707961SNatalie.Li@Sun.COM  * for now it just does simple checks. v1 is never NULL but v2 or v3
20717961SNatalie.Li@Sun.COM  * could be.
20723910Sdougm  */
20733910Sdougm 
20743910Sdougm static int
20757961SNatalie.Li@Sun.COM check_rorwnone(char *v1, char *v2, char *v3)
20763910Sdougm {
20773910Sdougm 	int ret = SA_OK;
20787961SNatalie.Li@Sun.COM 	if (v2 != NULL && strcmp(v1, v2) == 0)
20794345Sdougm 		ret = SA_VALUE_CONFLICT;
20807961SNatalie.Li@Sun.COM 	else if (v3 != NULL && strcmp(v1, v3) == 0)
20817961SNatalie.Li@Sun.COM 		ret = SA_VALUE_CONFLICT;
20827961SNatalie.Li@Sun.COM 
20833910Sdougm 	return (ret);
20843910Sdougm }
20853910Sdougm 
20863910Sdougm /*
20876214Sdougm  * nfs_validate_property(handle, property, parent)
20883910Sdougm  *
20893910Sdougm  * Check that the property has a legitimate value for its type.
20903910Sdougm  */
20913910Sdougm 
20923910Sdougm static int
20936214Sdougm nfs_validate_property(sa_handle_t handle, sa_property_t property,
20946214Sdougm     sa_optionset_t parent)
20953910Sdougm {
20963910Sdougm 	int ret = SA_OK;
20973910Sdougm 	char *propname;
20987961SNatalie.Li@Sun.COM 	char *other1;
20997961SNatalie.Li@Sun.COM 	char *other2;
21003910Sdougm 	int optindex;
21013910Sdougm 	nfsl_config_t *configlist;
21023910Sdougm 	sa_group_t parent_group;
21033910Sdougm 	char *value;
21043910Sdougm 
21053910Sdougm 	propname = sa_get_property_attr(property, "type");
21063910Sdougm 
21073910Sdougm 	if ((optindex = findopt(propname)) < 0)
21084345Sdougm 		ret = SA_NO_SUCH_PROP;
21093910Sdougm 
21103910Sdougm 	/* need to validate value range here as well */
21113910Sdougm 
21123910Sdougm 	if (ret == SA_OK) {
21134345Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
21146214Sdougm 		if (optdefs[optindex].share && parent_group != NULL &&
21156214Sdougm 		    !sa_is_share(parent_group))
21164345Sdougm 			ret = SA_PROP_SHARE_ONLY;
21173910Sdougm 	}
21183910Sdougm 	if (ret == SA_OK) {
21196214Sdougm 		if (optdefs[optindex].index == OPT_PUBLIC) {
21206214Sdougm 			/*
21216214Sdougm 			 * Public is special in that only one instance can
21226214Sdougm 			 * be in the repository at the same time.
21236214Sdougm 			 */
21246214Sdougm 			if (public_exists(handle, parent_group)) {
21257961SNatalie.Li@Sun.COM 				sa_free_attr_string(propname);
21266214Sdougm 				return (SA_VALUE_CONFLICT);
21276214Sdougm 			}
21286214Sdougm 		}
21294345Sdougm 		value = sa_get_property_attr(property, "value");
21304345Sdougm 		if (value != NULL) {
21314345Sdougm 			/* first basic type checking */
21324345Sdougm 			switch (optdefs[optindex].type) {
21334345Sdougm 			case OPT_TYPE_NUMBER:
21344345Sdougm 				/* check that the value is all digits */
21354345Sdougm 				if (!is_a_number(value))
21364345Sdougm 					ret = SA_BAD_VALUE;
21374345Sdougm 				break;
21384345Sdougm 			case OPT_TYPE_BOOLEAN:
21394345Sdougm 				if (strlen(value) == 0 ||
21404345Sdougm 				    strcasecmp(value, "true") == 0 ||
21414345Sdougm 				    strcmp(value, "1") == 0 ||
21424345Sdougm 				    strcasecmp(value, "false") == 0 ||
21434345Sdougm 				    strcmp(value, "0") == 0) {
21444345Sdougm 					ret = SA_OK;
21454345Sdougm 				} else {
21464345Sdougm 					ret = SA_BAD_VALUE;
21474345Sdougm 				}
21484345Sdougm 				break;
21494345Sdougm 			case OPT_TYPE_USER:
21504345Sdougm 				if (!is_a_number(value)) {
21514345Sdougm 					struct passwd *pw;
21524345Sdougm 					/*
21534345Sdougm 					 * in this case it would have to be a
21544345Sdougm 					 * user name
21554345Sdougm 					 */
21564345Sdougm 					pw = getpwnam(value);
21574345Sdougm 					if (pw == NULL)
21584345Sdougm 						ret = SA_BAD_VALUE;
21594345Sdougm 					endpwent();
21604345Sdougm 				} else {
21614345Sdougm 					uint64_t intval;
21624345Sdougm 					intval = strtoull(value, NULL, 0);
21634345Sdougm 					if (intval > UID_MAX && intval != ~0)
21644345Sdougm 						ret = SA_BAD_VALUE;
21654345Sdougm 				}
21664345Sdougm 				break;
21674345Sdougm 			case OPT_TYPE_FILE:
21684345Sdougm 				if (strcmp(value, "..") == 0 ||
21694345Sdougm 				    strchr(value, '/') != NULL) {
21704345Sdougm 					ret = SA_BAD_VALUE;
21714345Sdougm 				}
21724345Sdougm 				break;
21737961SNatalie.Li@Sun.COM 			case OPT_TYPE_ACCLIST: {
21747961SNatalie.Li@Sun.COM 				sa_property_t oprop1;
21757961SNatalie.Li@Sun.COM 				sa_property_t oprop2;
21767961SNatalie.Li@Sun.COM 				char *ovalue1 = NULL;
21777961SNatalie.Li@Sun.COM 				char *ovalue2 = NULL;
21787961SNatalie.Li@Sun.COM 
21797961SNatalie.Li@Sun.COM 				if (parent == NULL)
21807961SNatalie.Li@Sun.COM 					break;
21814345Sdougm 				/*
21824345Sdougm 				 * access list handling. Should eventually
21834345Sdougm 				 * validate that all the values make sense.
21844345Sdougm 				 * Also, ro and rw may have cross value
21854345Sdougm 				 * conflicts.
21864345Sdougm 				 */
21877961SNatalie.Li@Sun.COM 				if (strcmp(propname, SHOPT_RO) == 0) {
21887961SNatalie.Li@Sun.COM 					other1 = SHOPT_RW;
21897961SNatalie.Li@Sun.COM 					other2 = SHOPT_NONE;
21907961SNatalie.Li@Sun.COM 				} else if (strcmp(propname, SHOPT_RW) == 0) {
21917961SNatalie.Li@Sun.COM 					other1 = SHOPT_RO;
21927961SNatalie.Li@Sun.COM 					other2 = SHOPT_NONE;
21937961SNatalie.Li@Sun.COM 				} else if (strcmp(propname, SHOPT_NONE) == 0) {
21947961SNatalie.Li@Sun.COM 					other1 = SHOPT_RO;
21957961SNatalie.Li@Sun.COM 					other2 = SHOPT_RW;
21967961SNatalie.Li@Sun.COM 				} else {
21977961SNatalie.Li@Sun.COM 					other1 = NULL;
21987961SNatalie.Li@Sun.COM 					other2 = NULL;
21997961SNatalie.Li@Sun.COM 				}
22007961SNatalie.Li@Sun.COM 				if (other1 == NULL && other2 == NULL)
22017961SNatalie.Li@Sun.COM 					break;
22027961SNatalie.Li@Sun.COM 
22037961SNatalie.Li@Sun.COM 				/* compare rw(ro) with ro(rw) */
22044345Sdougm 
22057961SNatalie.Li@Sun.COM 				oprop1 = sa_get_property(parent, other1);
22067961SNatalie.Li@Sun.COM 				oprop2 = sa_get_property(parent, other2);
22077961SNatalie.Li@Sun.COM 				if (oprop1 == NULL && oprop2 == NULL)
22087961SNatalie.Li@Sun.COM 					break;
22097961SNatalie.Li@Sun.COM 				/*
22107961SNatalie.Li@Sun.COM 				 * Only potential confusion if other1
22117961SNatalie.Li@Sun.COM 				 * or other2 exists. Check the values
22127961SNatalie.Li@Sun.COM 				 * and run the check if there is a
22137961SNatalie.Li@Sun.COM 				 * value other than the one we are
22147961SNatalie.Li@Sun.COM 				 * explicitly looking at.
22157961SNatalie.Li@Sun.COM 				 */
22167961SNatalie.Li@Sun.COM 				ovalue1 = sa_get_property_attr(oprop1, "value");
22177961SNatalie.Li@Sun.COM 				ovalue2 = sa_get_property_attr(oprop2, "value");
22187961SNatalie.Li@Sun.COM 				if (ovalue1 != NULL || ovalue2 != NULL)
22197961SNatalie.Li@Sun.COM 					ret = check_rorwnone(value, ovalue1,
22207961SNatalie.Li@Sun.COM 					    ovalue2);
22217961SNatalie.Li@Sun.COM 
22227961SNatalie.Li@Sun.COM 				if (ovalue1 != NULL)
22237961SNatalie.Li@Sun.COM 					sa_free_attr_string(ovalue1);
22247961SNatalie.Li@Sun.COM 				if (ovalue2 != NULL)
22257961SNatalie.Li@Sun.COM 					sa_free_attr_string(ovalue2);
22264345Sdougm 				break;
22277961SNatalie.Li@Sun.COM 			}
22284345Sdougm 			case OPT_TYPE_LOGTAG:
22294345Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
22304345Sdougm 					int error;
22314345Sdougm 					if (value == NULL ||
22324345Sdougm 					    strlen(value) == 0) {
22334345Sdougm 						if (value != NULL)
22344345Sdougm 							sa_free_attr_string(
22354345Sdougm 							    value);
22364345Sdougm 						value = strdup("global");
22374345Sdougm 					}
22384345Sdougm 					if (value != NULL &&
22394345Sdougm 					    nfsl_findconfig(configlist, value,
22404345Sdougm 					    &error) == NULL) {
22414345Sdougm 						ret = SA_BAD_VALUE;
22424345Sdougm 					}
22435179Sdougm 					/* Must always free when done */
22445179Sdougm 					nfsl_freeconfig_list(&configlist);
22454345Sdougm 				} else {
22464345Sdougm 					ret = SA_CONFIG_ERR;
22474345Sdougm 				}
22484345Sdougm 				break;
22494345Sdougm 			case OPT_TYPE_STRING:
22504345Sdougm 				/* whatever is here should be ok */
22514345Sdougm 				break;
22524345Sdougm 			case OPT_TYPE_SECURITY:
22534345Sdougm 				/*
22544345Sdougm 				 * The "sec" property isn't used in the
22554345Sdougm 				 * non-legacy parts of sharemgr. We need to
22564345Sdougm 				 * reject it here. For legacy, it is pulled
22574345Sdougm 				 * out well before we get here.
22584345Sdougm 				 */
22594345Sdougm 				ret = SA_NO_SUCH_PROP;
22604345Sdougm 				break;
22614345Sdougm 			default:
22624345Sdougm 				break;
22633910Sdougm 			}
22645179Sdougm 
22655179Sdougm 			if (value != NULL)
22665179Sdougm 				sa_free_attr_string(value);
22675179Sdougm 
22684345Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
22694345Sdougm 				/* do the property specific check */
22706214Sdougm 				ret = optdefs[optindex].check(handle, property);
22713910Sdougm 			}
22723910Sdougm 		}
22733910Sdougm 	}
22743910Sdougm 
22753910Sdougm 	if (propname != NULL)
22764345Sdougm 		sa_free_attr_string(propname);
22773910Sdougm 	return (ret);
22783910Sdougm }
22793910Sdougm 
22803910Sdougm /*
22813910Sdougm  * Protocol management functions
22823910Sdougm  *
22833910Sdougm  * Properties defined in the default files are defined in
22843910Sdougm  * proto_option_defs for parsing and validation. If "other" and
22853910Sdougm  * "compare" are set, then the value for this property should be
22863910Sdougm  * compared against the property specified in "other" using the
22873910Sdougm  * "compare" check (either <= or >=) in order to ensure that the
22883910Sdougm  * values are in the correct range.  E.g. setting server_versmin
22893910Sdougm  * higher than server_versmax should not be allowed.
22903910Sdougm  */
22913910Sdougm 
22923910Sdougm struct proto_option_defs {
22933910Sdougm 	char *tag;
22943910Sdougm 	char *name;	/* display name -- remove protocol identifier */
22953910Sdougm 	int index;
22963910Sdougm 	int type;
22973910Sdougm 	union {
22983910Sdougm 	    int intval;
22993910Sdougm 	    char *string;
23003910Sdougm 	} defvalue;
23013910Sdougm 	uint32_t svcs;
23023910Sdougm 	int32_t minval;
23033910Sdougm 	int32_t maxval;
23043910Sdougm 	char *file;
23053910Sdougm 	char *other;
23063910Sdougm 	int compare;
23073910Sdougm #define	OPT_CMP_GE	0
23083910Sdougm #define	OPT_CMP_LE	1
23093910Sdougm 	int (*check)(char *);
23103910Sdougm } proto_options[] = {
23113910Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
23123910Sdougm 	{"nfsd_servers",
23133910Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
23143910Sdougm 	    1, INT32_MAX, NFSADMIN},
23153910Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
23163910Sdougm 	{"lockd_listen_backlog",
23173910Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
23183910Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
23193910Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
23203910Sdougm 	{"lockd_servers",
23213910Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
23223910Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
23233910Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
23243910Sdougm 	{"lockd_retransmit_timeout",
23253910Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
23263910Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23273910Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
23283910Sdougm 	{"grace_period",
23293910Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
23303910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23313910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
23323910Sdougm 	{"nfs_server_versmin",
23333910Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
23343910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23353910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
23363910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
23373910Sdougm 	{"nfs_server_versmax",
23383910Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
23393910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23403910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
23413910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
23423910Sdougm 	{"nfs_client_versmin",
23433910Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
23443910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23453910Sdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
23463910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
23473910Sdougm 	{"nfs_client_versmax",
23483910Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
23493910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23503910Sdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
23513910Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
23523910Sdougm 	{"nfs_server_delegation",
23533910Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
23543910Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
23553910Sdougm 	    NFSADMIN},
23563910Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
23573910Sdougm 	{"nfsmapid_domain",
23583910Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
23593910Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
23603910Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
23613910Sdougm 	{"nfsd_max_connections",
23623910Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
23633910Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
23643910Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
23653910Sdougm 	{"nfsd_protocol",
23663910Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
23673910Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
23683910Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
23693910Sdougm 	{"nfsd_listen_backlog",
23703910Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
23713910Sdougm 	    OPT_TYPE_NUMBER, 0,
23723910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23733910Sdougm 	{NULL}
23743910Sdougm };
23753910Sdougm 
23763910Sdougm /*
23773910Sdougm  * the protoset holds the defined options so we don't have to read
23783910Sdougm  * them multiple times
23793910Sdougm  */
23805179Sdougm static sa_protocol_properties_t protoset;
23813910Sdougm 
23823910Sdougm static int
23833910Sdougm findprotoopt(char *name, int whichname)
23843910Sdougm {
23853910Sdougm 	int i;
23863910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
23874345Sdougm 		if (whichname == 1) {
23884345Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
23893910Sdougm 			return (i);
23904345Sdougm 		} else {
23914345Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
23924345Sdougm 				return (i);
23934345Sdougm 		}
23943910Sdougm 	}
23953910Sdougm 	return (-1);
23963910Sdougm }
23973910Sdougm 
23983910Sdougm /*
23993910Sdougm  * fixcaselower(str)
24003910Sdougm  *
24013910Sdougm  * convert a string to lower case (inplace).
24023910Sdougm  */
24033910Sdougm 
24043910Sdougm static void
24053910Sdougm fixcaselower(char *str)
24063910Sdougm {
24073910Sdougm 	while (*str) {
24084345Sdougm 		*str = tolower(*str);
24094345Sdougm 		str++;
24103910Sdougm 	}
24113910Sdougm }
24123910Sdougm 
24133910Sdougm /*
24143910Sdougm  * fixcaseupper(str)
24153910Sdougm  *
24163910Sdougm  * convert a string to upper case (inplace).
24173910Sdougm  */
24183910Sdougm 
24193910Sdougm static void
24203910Sdougm fixcaseupper(char *str)
24213910Sdougm {
24223910Sdougm 	while (*str) {
24234345Sdougm 		*str = toupper(*str);
24244345Sdougm 		str++;
24253910Sdougm 	}
24263910Sdougm }
24273910Sdougm 
24283910Sdougm /*
24294241Sdougm  * skipwhitespace(str)
24304241Sdougm  *
24314241Sdougm  * Skip leading white space. It is assumed that it is called with a
24324241Sdougm  * valid pointer.
24334241Sdougm  */
24344241Sdougm 
24354241Sdougm static char *
24364241Sdougm skipwhitespace(char *str)
24374241Sdougm {
24384241Sdougm 	while (*str && isspace(*str))
24394241Sdougm 		str++;
24404241Sdougm 
24414241Sdougm 	return (str);
24424241Sdougm }
24434241Sdougm 
24444241Sdougm /*
24454345Sdougm  * extractprop()
24464345Sdougm  *
24474345Sdougm  * Extract the property and value out of the line and create the
24484345Sdougm  * property in the optionset.
24494345Sdougm  */
24506019Sdougm static int
24514345Sdougm extractprop(char *name, char *value)
24524345Sdougm {
24534345Sdougm 	sa_property_t prop;
24544345Sdougm 	int index;
24556019Sdougm 	int ret = SA_OK;
24564345Sdougm 	/*
24574345Sdougm 	 * Remove any leading
24584345Sdougm 	 * white space.
24594345Sdougm 	 */
24604345Sdougm 	name = skipwhitespace(name);
24614345Sdougm 
24624345Sdougm 	index = findprotoopt(name, 0);
24634345Sdougm 	if (index >= 0) {
24644345Sdougm 		fixcaselower(name);
24654345Sdougm 		prop = sa_create_property(proto_options[index].name, value);
24664345Sdougm 		if (prop != NULL)
24676019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
24686019Sdougm 		else
24696019Sdougm 			ret = SA_NO_MEMORY;
24704345Sdougm 	}
24716019Sdougm 	return (ret);
24724345Sdougm }
24734345Sdougm 
24744345Sdougm /*
24753910Sdougm  * initprotofromdefault()
24763910Sdougm  *
24776162Sdougm  * Read the default file(s) and add the defined values to the
24783910Sdougm  * protoset.  Note that default values are known from the built in
24796162Sdougm  * table in case the file doesn't have a definition. Not having the
24806162Sdougm  * /etc/default/nfs file is OK since we have builtin default
24816162Sdougm  * values. The default file will get constructed as needed if values
24826162Sdougm  * are changed from the defaults.
24833910Sdougm  */
24843910Sdougm 
24853910Sdougm static int
24863910Sdougm initprotofromdefault()
24873910Sdougm {
24883910Sdougm 	FILE *nfs;
24893910Sdougm 	char buff[BUFSIZ];
24903910Sdougm 	char *name;
24913910Sdougm 	char *value;
24926019Sdougm 	int ret = SA_OK;
24933910Sdougm 
24943910Sdougm 	protoset = sa_create_protocol_properties("nfs");
24953910Sdougm 
24963910Sdougm 	if (protoset != NULL) {
24974345Sdougm 		nfs = fopen(NFSADMIN, "r");
24984345Sdougm 		if (nfs != NULL) {
24996019Sdougm 			while (ret == SA_OK &&
25006019Sdougm 			    fgets(buff, sizeof (buff), nfs) != NULL) {
25014345Sdougm 				switch (buff[0]) {
25024345Sdougm 				case '\n':
25034345Sdougm 				case '#':
25044345Sdougm 					/* skip */
25054345Sdougm 					break;
25064345Sdougm 				default:
25074345Sdougm 					name = buff;
25084345Sdougm 					buff[strlen(buff) - 1] = '\0';
25094345Sdougm 					value = strchr(name, '=');
25104345Sdougm 					if (value != NULL) {
25114345Sdougm 						*value++ = '\0';
25126019Sdougm 						ret = extractprop(name, value);
25134345Sdougm 					}
25144345Sdougm 				}
25153910Sdougm 			}
25166019Sdougm 			(void) fclose(nfs);
25176019Sdougm 		} else {
25186162Sdougm 			switch (errno) {
25196162Sdougm 			case EPERM:
25206162Sdougm 			case EACCES:
25216162Sdougm 				ret = SA_NO_PERMISSION;
25226162Sdougm 				break;
25236162Sdougm 			case ENOENT:
25246162Sdougm 				break;
25256162Sdougm 			default:
25266162Sdougm 				ret = SA_SYSTEM_ERR;
25276162Sdougm 				break;
25286162Sdougm 			}
25293910Sdougm 		}
25306019Sdougm 	} else {
25316019Sdougm 		ret = SA_NO_MEMORY;
25323910Sdougm 	}
25336019Sdougm 	return (ret);
25343910Sdougm }
25353910Sdougm 
25363910Sdougm /*
25374345Sdougm  * add_defaults()
25383910Sdougm  *
25393910Sdougm  * Add the default values for any property not defined in the parsing
25403910Sdougm  * of the default files. Values are set according to their defined
25413910Sdougm  * types.
25423910Sdougm  */
25433910Sdougm 
25443910Sdougm static void
25453910Sdougm add_defaults()
25463910Sdougm {
25473910Sdougm 	int i;
25483910Sdougm 	char number[MAXDIGITS];
25493910Sdougm 
25503910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
25514345Sdougm 		sa_property_t prop;
25524345Sdougm 		prop = sa_get_protocol_property(protoset,
25534345Sdougm 		    proto_options[i].name);
25544345Sdougm 		if (prop == NULL) {
25554345Sdougm 			/* add the default value */
25564345Sdougm 			switch (proto_options[i].type) {
25574345Sdougm 			case OPT_TYPE_NUMBER:
25584345Sdougm 				(void) snprintf(number, sizeof (number), "%d",
25594345Sdougm 				    proto_options[i].defvalue.intval);
25604345Sdougm 				prop = sa_create_property(proto_options[i].name,
25614345Sdougm 				    number);
25624345Sdougm 				break;
25633910Sdougm 
25644345Sdougm 			case OPT_TYPE_BOOLEAN:
25654345Sdougm 				prop = sa_create_property(proto_options[i].name,
25664345Sdougm 				    proto_options[i].defvalue.intval ?
25674345Sdougm 				    "true" : "false");
25684345Sdougm 				break;
25693910Sdougm 
25704345Sdougm 			case OPT_TYPE_ONOFF:
25714345Sdougm 				prop = sa_create_property(proto_options[i].name,
25724345Sdougm 				    proto_options[i].defvalue.intval ?
25734345Sdougm 				    "on" : "off");
25744345Sdougm 				break;
25753910Sdougm 
25764345Sdougm 			default:
25774345Sdougm 				/* treat as strings of zero length */
25784345Sdougm 				prop = sa_create_property(proto_options[i].name,
25794345Sdougm 				    "");
25804345Sdougm 				break;
25814345Sdougm 			}
25824345Sdougm 			if (prop != NULL)
25834345Sdougm 				(void) sa_add_protocol_property(protoset, prop);
25843910Sdougm 		}
25853910Sdougm 	}
25863910Sdougm }
25873910Sdougm 
25883910Sdougm static void
25893910Sdougm free_protoprops()
25903910Sdougm {
25915179Sdougm 	if (protoset != NULL) {
25925179Sdougm 		xmlFreeNode(protoset);
25935179Sdougm 		protoset = NULL;
25945179Sdougm 	}
25953910Sdougm }
25963910Sdougm 
25973910Sdougm /*
25983910Sdougm  * nfs_init()
25993910Sdougm  *
26003910Sdougm  * Initialize the NFS plugin.
26013910Sdougm  */
26023910Sdougm 
26033910Sdougm static int
26043910Sdougm nfs_init()
26053910Sdougm {
26063910Sdougm 	int ret = SA_OK;
26073910Sdougm 
26086162Sdougm 	if (sa_plugin_ops.sa_init != nfs_init) {
26094345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
26104345Sdougm 		    "NFS plugin not properly initialized\n"));
26116162Sdougm 		return (SA_CONFIG_ERR);
26126162Sdougm 	}
26133910Sdougm 
26143910Sdougm 	ret = initprotofromdefault();
26156162Sdougm 	if (ret != SA_OK) {
26166162Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
26176162Sdougm 		    "NFS plugin problem with default file: %s\n"),
26186162Sdougm 		    sa_errorstr(ret));
26196162Sdougm 		ret = SA_OK;
26206162Sdougm 	}
26216162Sdougm 	add_defaults();
26223910Sdougm 
26233910Sdougm 	return (ret);
26243910Sdougm }
26253910Sdougm 
26263910Sdougm /*
26273910Sdougm  * nfs_fini()
26283910Sdougm  *
26293910Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
26303910Sdougm  */
26313910Sdougm 
26323910Sdougm static void
26333910Sdougm nfs_fini()
26343910Sdougm {
26353910Sdougm 	free_protoprops();
26363910Sdougm }
26373910Sdougm 
26383910Sdougm /*
26393910Sdougm  * nfs_get_proto_set()
26403910Sdougm  *
26413910Sdougm  * Return an optionset with all the protocol specific properties in
26423910Sdougm  * it.
26433910Sdougm  */
26443910Sdougm 
26453910Sdougm static sa_protocol_properties_t
26463910Sdougm nfs_get_proto_set()
26473910Sdougm {
26483910Sdougm 	return (protoset);
26493910Sdougm }
26503910Sdougm 
26513910Sdougm struct deffile {
26523910Sdougm 	struct deffile *next;
26533910Sdougm 	char *line;
26543910Sdougm };
26553910Sdougm 
26563910Sdougm /*
26573910Sdougm  * read_default_file(fname)
26583910Sdougm  *
26593910Sdougm  * Read the specified default file. We return a list of entries. This
26603910Sdougm  * get used for adding or removing values.
26613910Sdougm  */
26623910Sdougm 
26633910Sdougm static struct deffile *
26643910Sdougm read_default_file(char *fname)
26653910Sdougm {
26663910Sdougm 	FILE *file;
26673910Sdougm 	struct deffile *defs = NULL;
26683910Sdougm 	struct deffile *newdef;
26693910Sdougm 	struct deffile *prevdef = NULL;
26703910Sdougm 	char buff[BUFSIZ * 2];
26713910Sdougm 
26723910Sdougm 	file = fopen(fname, "r");
26733910Sdougm 	if (file != NULL) {
26744345Sdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
26754345Sdougm 			newdef = (struct deffile *)calloc(1,
26764345Sdougm 			    sizeof (struct deffile));
26774345Sdougm 			if (newdef != NULL) {
26784345Sdougm 				/* Make sure we skip any leading whitespace. */
26794345Sdougm 				newdef->line = strdup(skipwhitespace(buff));
26804345Sdougm 				if (defs == NULL) {
26814345Sdougm 					prevdef = defs = newdef;
26824345Sdougm 				} else {
26834345Sdougm 					prevdef->next = newdef;
26844345Sdougm 					prevdef = newdef;
26854345Sdougm 				}
26864345Sdougm 			}
26873910Sdougm 		}
26886162Sdougm 		(void) fclose(file);
26896162Sdougm 	} else {
26906162Sdougm 		int ret = SA_OK;
26916162Sdougm 		switch (errno) {
26926162Sdougm 		case EPERM:
26936162Sdougm 		case EACCES:
26946162Sdougm 			ret = SA_NO_PERMISSION;
26956162Sdougm 			break;
26966162Sdougm 		case ENOENT:
26976162Sdougm 			break;
26986162Sdougm 		default:
26996162Sdougm 			ret = SA_SYSTEM_ERR;
27006162Sdougm 			break;
27016162Sdougm 		}
27026162Sdougm 		if (ret == SA_OK) {
27036162Sdougm 			/* Want at least one comment line */
27046162Sdougm 			defs = (struct deffile *)
27056162Sdougm 			    calloc(1, sizeof (struct deffile));
27066162Sdougm 			defs->line = strdup("# NFS default file\n");
27076162Sdougm 		}
27083910Sdougm 	}
27093910Sdougm 	return (defs);
27103910Sdougm }
27113910Sdougm 
27123910Sdougm static void
27133910Sdougm free_default_file(struct deffile *defs)
27143910Sdougm {
27153910Sdougm 	struct deffile *curdefs = NULL;
27163910Sdougm 
27173910Sdougm 	while (defs != NULL) {
27184345Sdougm 		curdefs = defs;
27194345Sdougm 		defs = defs->next;
27204345Sdougm 		if (curdefs->line != NULL)
27214345Sdougm 			free(curdefs->line);
27224345Sdougm 		free(curdefs);
27233910Sdougm 	}
27243910Sdougm }
27253910Sdougm 
27263910Sdougm /*
27273910Sdougm  * write_default_file(fname, defs)
27283910Sdougm  *
27293910Sdougm  * Write the default file back.
27303910Sdougm  */
27313910Sdougm 
27323910Sdougm static int
27333910Sdougm write_default_file(char *fname, struct deffile *defs)
27343910Sdougm {
27353910Sdougm 	FILE *file;
27363910Sdougm 	int ret = SA_OK;
27373910Sdougm 	sigset_t old, new;
27383910Sdougm 
27393910Sdougm 	file = fopen(fname, "w+");
27403910Sdougm 	if (file != NULL) {
27414345Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
27424345Sdougm 		(void) sigaddset(&new, SIGHUP);
27434345Sdougm 		(void) sigaddset(&new, SIGINT);
27444345Sdougm 		(void) sigaddset(&new, SIGQUIT);
27454345Sdougm 		(void) sigaddset(&new, SIGTSTP);
27464345Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
27474345Sdougm 		while (defs != NULL) {
27484345Sdougm 			(void) fputs(defs->line, file);
27494345Sdougm 			defs = defs->next;
27504345Sdougm 		}
27514345Sdougm 		(void) fsync(fileno(file));
27524345Sdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
27534345Sdougm 		(void) fclose(file);
27543910Sdougm 	} else {
27554345Sdougm 		switch (errno) {
27564345Sdougm 		case EPERM:
27574345Sdougm 		case EACCES:
27584345Sdougm 			ret = SA_NO_PERMISSION;
27594345Sdougm 			break;
27604345Sdougm 		default:
27614345Sdougm 			ret = SA_SYSTEM_ERR;
27624345Sdougm 		}
27633910Sdougm 	}
27643910Sdougm 	return (ret);
27653910Sdougm }
27663910Sdougm 
27673910Sdougm 
27683910Sdougm /*
27693910Sdougm  * set_default_file_value(tag, value)
27703910Sdougm  *
27713910Sdougm  * Set the default file value for tag to value. Then rewrite the file.
27723910Sdougm  * tag and value are always set.  The caller must ensure this.
27733910Sdougm  */
27743910Sdougm 
27753910Sdougm #define	MAX_STRING_LENGTH	256
27763910Sdougm static int
27773910Sdougm set_default_file_value(char *tag, char *value)
27783910Sdougm {
27793910Sdougm 	int ret = SA_OK;
27803910Sdougm 	struct deffile *root;
27813910Sdougm 	struct deffile *defs;
27823910Sdougm 	struct deffile *prev;
27833910Sdougm 	char string[MAX_STRING_LENGTH];
27843910Sdougm 	int len;
27856162Sdougm 	boolean_t update = B_FALSE;
27863910Sdougm 
27873910Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
27883910Sdougm 	len = strlen(string);
27893910Sdougm 
27903910Sdougm 	root = defs = read_default_file(NFSADMIN);
27913910Sdougm 	if (root == NULL) {
27926162Sdougm 		switch (errno) {
27936162Sdougm 		case EPERM:
27946162Sdougm 		case EACCES:
27954345Sdougm 			ret = SA_NO_PERMISSION;
27966162Sdougm 			break;
27976162Sdougm 		default:
27986162Sdougm 			ret = SA_NO_MEMORY;
27996162Sdougm 			break;
28006162Sdougm 		}
28016162Sdougm 		return (ret);
28026162Sdougm 	}
28036162Sdougm 
28046162Sdougm 	while (defs != NULL) {
28056162Sdougm 		if (defs->line != NULL &&
28066162Sdougm 		    strncasecmp(defs->line, string, len) == 0) {
28076162Sdougm 			/* replace with the new value */
28086162Sdougm 			free(defs->line);
28096162Sdougm 			fixcaseupper(tag);
28106162Sdougm 			(void) snprintf(string, sizeof (string),
28116162Sdougm 			    "%s=%s\n", tag, value);
28126162Sdougm 			string[MAX_STRING_LENGTH - 1] = '\0';
28136162Sdougm 			defs->line = strdup(string);
28146162Sdougm 			update = B_TRUE;
28156162Sdougm 			break;
28166162Sdougm 		}
28176162Sdougm 		defs = defs->next;
28186162Sdougm 	}
28196162Sdougm 	if (!update) {
28206162Sdougm 		defs = root;
28216162Sdougm 		/* didn't find, so see if it is a comment */
28226162Sdougm 		(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
28236162Sdougm 		len = strlen(string);
28243910Sdougm 		while (defs != NULL) {
28256162Sdougm 			if (strncasecmp(defs->line, string, len) == 0) {
28264345Sdougm 				/* replace with the new value */
28274345Sdougm 				free(defs->line);
28284345Sdougm 				fixcaseupper(tag);
28294345Sdougm 				(void) snprintf(string, sizeof (string),
28303910Sdougm 				    "%s=%s\n", tag, value);
28314345Sdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
28324345Sdougm 				defs->line = strdup(string);
28336162Sdougm 				update = B_TRUE;
28344345Sdougm 				break;
28354345Sdougm 			}
28364345Sdougm 			defs = defs->next;
28373910Sdougm 		}
28386162Sdougm 	}
28396162Sdougm 	if (!update) {
28406162Sdougm 		fixcaseupper(tag);
28416162Sdougm 		(void) snprintf(string, sizeof (string), "%s=%s\n",
28426162Sdougm 		    tag, value);
28436162Sdougm 		prev = root;
28446162Sdougm 		while (prev->next != NULL)
28456162Sdougm 			prev = prev->next;
28466162Sdougm 		defs = malloc(sizeof (struct deffile));
28476162Sdougm 		prev->next = defs;
28486162Sdougm 		if (defs != NULL) {
28496162Sdougm 			defs->next = NULL;
28506162Sdougm 			defs->line = strdup(string);
28516162Sdougm 			update = B_TRUE;
28523910Sdougm 		}
28533910Sdougm 	}
28546162Sdougm 	if (update) {
28556162Sdougm 		ret = write_default_file(NFSADMIN, root);
28566162Sdougm 	}
28576162Sdougm 	free_default_file(root);
28583910Sdougm 	return (ret);
28593910Sdougm }
28603910Sdougm 
28613910Sdougm /*
28623910Sdougm  * service_in_state(service, chkstate)
28633910Sdougm  *
28643910Sdougm  * Want to know if the specified service is in the desired state
28653910Sdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
28663910Sdougm  * isn't.
28673910Sdougm  */
28683910Sdougm static int
28693910Sdougm service_in_state(char *service, const char *chkstate)
28703910Sdougm {
28713910Sdougm 	char *state;
28723910Sdougm 	int ret = B_FALSE;
28733910Sdougm 
28743910Sdougm 	state = smf_get_state(service);
28753910Sdougm 	if (state != NULL) {
28764345Sdougm 		/* got the state so get the equality for the return value */
28774345Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
28784345Sdougm 		free(state);
28793910Sdougm 	}
28803910Sdougm 	return (ret);
28813910Sdougm }
28823910Sdougm 
28833910Sdougm /*
28843910Sdougm  * restart_service(svcs)
28853910Sdougm  *
28863910Sdougm  * Walk through the bit mask of services that need to be restarted in
28873910Sdougm  * order to use the new property values. Some properties affect
28883910Sdougm  * multiple daemons. Should only restart a service if it is currently
28893910Sdougm  * enabled (online).
28903910Sdougm  */
28913910Sdougm 
28923910Sdougm static void
28933910Sdougm restart_service(uint32_t svcs)
28943910Sdougm {
28953910Sdougm 	uint32_t mask;
28963910Sdougm 	int ret;
28973910Sdougm 	char *service;
28983910Sdougm 
28993910Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
29004345Sdougm 		switch (svcs & mask) {
29014345Sdougm 		case SVC_LOCKD:
29024345Sdougm 			service = LOCKD;
29034345Sdougm 			break;
29044345Sdougm 		case SVC_STATD:
29054345Sdougm 			service = STATD;
29064345Sdougm 			break;
29074345Sdougm 		case SVC_NFSD:
29084345Sdougm 			service = NFSD;
29094345Sdougm 			break;
29104345Sdougm 		case SVC_MOUNTD:
29114345Sdougm 			service = MOUNTD;
29124345Sdougm 			break;
29134345Sdougm 		case SVC_NFS4CBD:
29144345Sdougm 			service = NFS4CBD;
29154345Sdougm 			break;
29164345Sdougm 		case SVC_NFSMAPID:
29174345Sdougm 			service = NFSMAPID;
29184345Sdougm 			break;
29194345Sdougm 		case SVC_RQUOTAD:
29204345Sdougm 			service = RQUOTAD;
29214345Sdougm 			break;
29224345Sdougm 		case SVC_NFSLOGD:
29234345Sdougm 			service = NFSLOGD;
29244345Sdougm 			break;
29254345Sdougm 		default:
29264345Sdougm 			continue;
29274345Sdougm 		}
29283910Sdougm 
29293910Sdougm 		/*
29303910Sdougm 		 * Only attempt to restart the service if it is
29313910Sdougm 		 * currently running. In the future, it may be
29323910Sdougm 		 * desirable to use smf_refresh_instance if the NFS
29333910Sdougm 		 * services ever implement the refresh method.
29343910Sdougm 		 */
29354345Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
29364345Sdougm 			ret = smf_restart_instance(service);
29373910Sdougm 			/*
29384345Sdougm 			 * There are only a few SMF errors at this point, but
29394345Sdougm 			 * it is also possible that a bad value may have put
29404345Sdougm 			 * the service into maintenance if there wasn't an
29414345Sdougm 			 * SMF level error.
29423910Sdougm 			 */
29434345Sdougm 			if (ret != 0) {
29444345Sdougm 				(void) fprintf(stderr,
29454345Sdougm 				    dgettext(TEXT_DOMAIN,
29464345Sdougm 				    "%s failed to restart: %s\n"),
29474345Sdougm 				    scf_strerror(scf_error()));
29484345Sdougm 			} else {
29494345Sdougm 				/*
29504345Sdougm 				 * Check whether it has gone to "maintenance"
29514345Sdougm 				 * mode or not. Maintenance implies something
29524345Sdougm 				 * went wrong.
29534345Sdougm 				 */
29544345Sdougm 				if (service_in_state(service,
29554345Sdougm 				    SCF_STATE_STRING_MAINT)) {
29564345Sdougm 					(void) fprintf(stderr,
29574345Sdougm 					    dgettext(TEXT_DOMAIN,
29584345Sdougm 					    "%s failed to restart\n"),
29594345Sdougm 					    service);
29604345Sdougm 				}
29614345Sdougm 			}
29623910Sdougm 		}
29634345Sdougm 		svcs &= ~mask;
29643910Sdougm 	}
29653910Sdougm }
29663910Sdougm 
29673910Sdougm /*
29683910Sdougm  * nfs_minmax_check(name, value)
29693910Sdougm  *
29703910Sdougm  * Verify that the value for the property specified by index is valid
29713910Sdougm  * relative to the opposite value in the case of a min/max variable.
29723910Sdougm  * Currently, server_minvers/server_maxvers and
29733910Sdougm  * client_minvers/client_maxvers are the only ones to check.
29743910Sdougm  */
29753910Sdougm 
29763910Sdougm static int
29773910Sdougm nfs_minmax_check(int index, int value)
29783910Sdougm {
29793910Sdougm 	int val;
29803910Sdougm 	char *pval;
29813910Sdougm 	sa_property_t prop;
29823910Sdougm 	sa_optionset_t opts;
29833910Sdougm 	int ret = B_TRUE;
29843910Sdougm 
29853910Sdougm 	if (proto_options[index].other != NULL) {
29864345Sdougm 		/* have a property to compare against */
29874345Sdougm 		opts = nfs_get_proto_set();
29884345Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
29893910Sdougm 		/*
29903910Sdougm 		 * If we don't find the property, assume default
29913910Sdougm 		 * values which will work since the max will be at the
29923910Sdougm 		 * max and the min at the min.
29933910Sdougm 		 */
29944345Sdougm 		if (prop != NULL) {
29954345Sdougm 			pval = sa_get_property_attr(prop, "value");
29964345Sdougm 			if (pval != NULL) {
29974345Sdougm 				val = strtoul(pval, NULL, 0);
29984345Sdougm 				if (proto_options[index].compare ==
29994345Sdougm 				    OPT_CMP_LE) {
30004345Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
30014345Sdougm 				} else if (proto_options[index].compare ==
30024345Sdougm 				    OPT_CMP_GE) {
30034345Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
30044345Sdougm 				}
30054345Sdougm 			}
30063910Sdougm 		}
30073910Sdougm 	}
30083910Sdougm 	return (ret);
30093910Sdougm }
30103910Sdougm 
30113910Sdougm /*
30123910Sdougm  * nfs_validate_proto_prop(index, name, value)
30133910Sdougm  *
30145331Samw  * Verify that the property specified by name can take the new
30153910Sdougm  * value. This is a sanity check to prevent bad values getting into
30163910Sdougm  * the default files. All values need to be checked against what is
30173910Sdougm  * allowed by their defined type. If a type isn't explicitly defined
30183910Sdougm  * here, it is treated as a string.
30193910Sdougm  *
30203910Sdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
30213910Sdougm  * within the range specified and potentially against another property
30223910Sdougm  * value as well as specified in the proto_options members other and
30233910Sdougm  * compare.
30243910Sdougm  */
30253910Sdougm 
30263910Sdougm static int
30273910Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
30283910Sdougm {
30293910Sdougm 	int ret = SA_OK;
30303910Sdougm 	char *cp;
30313910Sdougm #ifdef lint
30323910Sdougm 	name = name;
30333910Sdougm #endif
30343910Sdougm 
30353910Sdougm 	switch (proto_options[index].type) {
30363910Sdougm 	case OPT_TYPE_NUMBER:
30374345Sdougm 		if (!is_a_number(value))
30384345Sdougm 			ret = SA_BAD_VALUE;
30394345Sdougm 		else {
30404345Sdougm 			int val;
30414345Sdougm 			val = strtoul(value, NULL, 0);
30424345Sdougm 			if (val < proto_options[index].minval ||
30434345Sdougm 			    val > proto_options[index].maxval)
30444345Sdougm 				ret = SA_BAD_VALUE;
30454345Sdougm 			/*
30464345Sdougm 			 * For server_versmin/server_versmax and
30474345Sdougm 			 * client_versmin/client_versmax, the value of the
30484345Sdougm 			 * min(max) should be checked to be correct relative
30494345Sdougm 			 * to the current max(min).
30504345Sdougm 			 */
30514345Sdougm 			if (!nfs_minmax_check(index, val)) {
30524345Sdougm 				ret = SA_BAD_VALUE;
30534345Sdougm 			}
30543910Sdougm 		}
30554345Sdougm 		break;
30563910Sdougm 
30573910Sdougm 	case OPT_TYPE_DOMAIN:
30583910Sdougm 		/*
30593910Sdougm 		 * needs to be a qualified domain so will have at
30603910Sdougm 		 * least one period and other characters on either
30613910Sdougm 		 * side of it.  A zero length string is also allowed
30623910Sdougm 		 * and is the way to turn off the override.
30633910Sdougm 		 */
30644345Sdougm 		if (strlen(value) == 0)
30654345Sdougm 			break;
30664345Sdougm 		cp = strchr(value, '.');
30674345Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
30684345Sdougm 			ret = SA_BAD_VALUE;
30693910Sdougm 		break;
30703910Sdougm 
30713910Sdougm 	case OPT_TYPE_BOOLEAN:
30724345Sdougm 		if (strlen(value) == 0 ||
30734345Sdougm 		    strcasecmp(value, "true") == 0 ||
30744345Sdougm 		    strcmp(value, "1") == 0 ||
30754345Sdougm 		    strcasecmp(value, "false") == 0 ||
30764345Sdougm 		    strcmp(value, "0") == 0) {
30774345Sdougm 			ret = SA_OK;
30784345Sdougm 		} else {
30794345Sdougm 			ret = SA_BAD_VALUE;
30804345Sdougm 		}
30814345Sdougm 		break;
30823910Sdougm 
30833910Sdougm 	case OPT_TYPE_ONOFF:
30844345Sdougm 		if (strcasecmp(value, "on") != 0 &&
30854345Sdougm 		    strcasecmp(value, "off") != 0) {
30864345Sdougm 			ret = SA_BAD_VALUE;
30874345Sdougm 		}
30884345Sdougm 		break;
30893910Sdougm 
30903910Sdougm 	case OPT_TYPE_PROTOCOL:
30916162Sdougm 		if (strlen(value) != 0 &&
30926162Sdougm 		    strcasecmp(value, "all") != 0 &&
30934345Sdougm 		    strcasecmp(value, "tcp") != 0 &&
30944345Sdougm 		    strcasecmp(value, "udp") != 0)
30954345Sdougm 			ret = SA_BAD_VALUE;
30964345Sdougm 		break;
30973910Sdougm 
30983910Sdougm 	default:
30994345Sdougm 		/* treat as a string */
31004345Sdougm 		break;
31013910Sdougm 	}
31023910Sdougm 	return (ret);
31033910Sdougm }
31043910Sdougm 
31053910Sdougm /*
31063910Sdougm  * nfs_set_proto_prop(prop)
31073910Sdougm  *
31083910Sdougm  * check that prop is valid.
31093910Sdougm  */
31103910Sdougm 
31113910Sdougm static int
31123910Sdougm nfs_set_proto_prop(sa_property_t prop)
31133910Sdougm {
31143910Sdougm 	int ret = SA_OK;
31153910Sdougm 	char *name;
31163910Sdougm 	char *value;
31173910Sdougm 
31183910Sdougm 	name = sa_get_property_attr(prop, "type");
31193910Sdougm 	value = sa_get_property_attr(prop, "value");
31203910Sdougm 	if (name != NULL && value != NULL) {
31214345Sdougm 		int index = findprotoopt(name, 1);
31224345Sdougm 		if (index >= 0) {
31234345Sdougm 			/* should test for valid value */
31244345Sdougm 			ret = nfs_validate_proto_prop(index, name, value);
31254345Sdougm 			if (ret == SA_OK)
31264345Sdougm 				ret = set_default_file_value(
31274345Sdougm 				    proto_options[index].tag, value);
31284345Sdougm 			if (ret == SA_OK)
31294345Sdougm 				restart_service(proto_options[index].svcs);
31304345Sdougm 		}
31313910Sdougm 	}
31323910Sdougm 	if (name != NULL)
31334345Sdougm 		sa_free_attr_string(name);
31343910Sdougm 	if (value != NULL)
31354345Sdougm 		sa_free_attr_string(value);
31363910Sdougm 	return (ret);
31373910Sdougm }
31383910Sdougm 
31393910Sdougm /*
31403910Sdougm  * nfs_get_status()
31413910Sdougm  *
31423910Sdougm  * What is the current status of the nfsd? We use the SMF state here.
31433910Sdougm  * Caller must free the returned value.
31443910Sdougm  */
31453910Sdougm 
31463910Sdougm static char *
31473910Sdougm nfs_get_status()
31483910Sdougm {
31493910Sdougm 	char *state;
31503910Sdougm 	state = smf_get_state(NFSD);
31513910Sdougm 	return (state != NULL ? state : strdup("-"));
31523910Sdougm }
31533910Sdougm 
31543910Sdougm /*
31553910Sdougm  * nfs_space_alias(alias)
31563910Sdougm  *
31573910Sdougm  * Lookup the space (security) name. If it is default, convert to the
31583910Sdougm  * real name.
31593910Sdougm  */
31603910Sdougm 
31613910Sdougm static char *
31623910Sdougm nfs_space_alias(char *space)
31633910Sdougm {
31643910Sdougm 	char *name = space;
31653910Sdougm 	seconfig_t secconf;
31663910Sdougm 
31673910Sdougm 	/*
31683910Sdougm 	 * Only the space named "default" is special. If it is used,
31693910Sdougm 	 * the default needs to be looked up and the real name used.
31703910Sdougm 	 * This is normally "sys" but could be changed.  We always
31713910Sdougm 	 * change defautl to the real name.
31723910Sdougm 	 */
31733910Sdougm 	if (strcmp(space, "default") == 0 &&
31743910Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
31754345Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
31764345Sdougm 			name = secconf.sc_name;
31773910Sdougm 	}
31783910Sdougm 	return (strdup(name));
31793910Sdougm }
31805331Samw 
31815331Samw /*
31825331Samw  * nfs_features()
31835331Samw  *
31845331Samw  * Return a mask of the features required.
31855331Samw  */
31865331Samw 
31875331Samw static uint64_t
31885331Samw nfs_features()
31895331Samw {
31906088Sdougm 	return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);
31915331Samw }
3192