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 /*
233910Sdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
243910Sdougm  * Use is subject to license terms.
253910Sdougm  */
263910Sdougm 
273910Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
283910Sdougm 
293910Sdougm /*
303910Sdougm  * NFS specific functions
313910Sdougm  */
323910Sdougm #include <stdio.h>
333910Sdougm #include <string.h>
343910Sdougm #include <ctype.h>
353910Sdougm #include <stdlib.h>
363910Sdougm #include <unistd.h>
373910Sdougm #include <zone.h>
383910Sdougm #include <errno.h>
393910Sdougm #include <locale.h>
403910Sdougm #include <signal.h>
413910Sdougm #include "libshare.h"
423910Sdougm #include "libshare_impl.h"
433910Sdougm #include <nfs/export.h>
443910Sdougm #include <pwd.h>
453910Sdougm #include <limits.h>
463910Sdougm #include <libscf.h>
473910Sdougm #include "nfslog_config.h"
483910Sdougm #include "nfslogtab.h"
493910Sdougm #include "libshare_nfs.h"
503910Sdougm #include <rpcsvc/daemon_utils.h>
513910Sdougm #include <nfs/nfs.h>
524543Smarks #include <nfs/nfssys.h>
533910Sdougm 
543910Sdougm /* should really be in some global place */
553910Sdougm #define	DEF_WIN	30000
563910Sdougm #define	OPT_CHUNK	1024
573910Sdougm 
583910Sdougm int debug = 0;
593910Sdougm 
604543Smarks #define	NFS_SERVER_SVC	"svc:/network/nfs/server:default"
613910Sdougm 
623910Sdougm /* internal functions */
633910Sdougm static int nfs_init();
643910Sdougm static void nfs_fini();
653910Sdougm static int nfs_enable_share(sa_share_t);
664543Smarks static int nfs_disable_share(sa_share_t, char *);
673910Sdougm static int nfs_validate_property(sa_property_t, sa_optionset_t);
683910Sdougm static int nfs_validate_security_mode(char *);
693910Sdougm static int nfs_is_security_opt(char *);
703910Sdougm static int nfs_parse_legacy_options(sa_group_t, char *);
713910Sdougm static char *nfs_format_options(sa_group_t, int);
723910Sdougm static int nfs_set_proto_prop(sa_property_t);
733910Sdougm static sa_protocol_properties_t nfs_get_proto_set();
743910Sdougm static char *nfs_get_status();
753910Sdougm static char *nfs_space_alias(char *);
763910Sdougm 
773910Sdougm /*
783910Sdougm  * ops vector that provides the protocol specific info and operations
793910Sdougm  * for share management.
803910Sdougm  */
813910Sdougm 
823910Sdougm struct sa_plugin_ops sa_plugin_ops = {
833910Sdougm 	SA_PLUGIN_VERSION,
843910Sdougm 	"nfs",
853910Sdougm 	nfs_init,
863910Sdougm 	nfs_fini,
873910Sdougm 	nfs_enable_share,
883910Sdougm 	nfs_disable_share,
893910Sdougm 	nfs_validate_property,
903910Sdougm 	nfs_validate_security_mode,
913910Sdougm 	nfs_is_security_opt,
923910Sdougm 	nfs_parse_legacy_options,
933910Sdougm 	nfs_format_options,
943910Sdougm 	nfs_set_proto_prop,
953910Sdougm 	nfs_get_proto_set,
963910Sdougm 	nfs_get_status,
973910Sdougm 	nfs_space_alias,
983910Sdougm 	NULL,
993910Sdougm 	NULL
1003910Sdougm };
1013910Sdougm 
1023910Sdougm /*
1033910Sdougm  * list of support services needed
1043910Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
1053910Sdougm  */
1063910Sdougm 
1073910Sdougm static char *service_list_default[] =
1083910Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL };
1093910Sdougm static char *service_list_logging[] =
1103910Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL };
1113910Sdougm 
1123910Sdougm /*
1133910Sdougm  * option definitions.  Make sure to keep the #define for the option
1143910Sdougm  * index just before the entry it is the index for. Changing the order
1153910Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1163910Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1173910Sdougm  */
1183910Sdougm 
1193910Sdougm struct option_defs optdefs[] = {
1203910Sdougm #define	OPT_RO		0
1213910Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1223910Sdougm #define	OPT_RW		1
1233910Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1243910Sdougm #define	OPT_ROOT	2
1253910Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1263910Sdougm #define	OPT_SECURE	3
1273910Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1283910Sdougm #define	OPT_ANON	4
1293910Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1303910Sdougm #define	OPT_WINDOW	5
1313910Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1323910Sdougm #define	OPT_NOSUID	6
1333910Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1343910Sdougm #define	OPT_ACLOK	7
1353910Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1363910Sdougm #define	OPT_NOSUB	8
1373910Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1383910Sdougm #define	OPT_SEC		9
1393910Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1403910Sdougm #define	OPT_PUBLIC	10
1413910Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1423910Sdougm #define	OPT_INDEX	11
1433910Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1443910Sdougm #define	OPT_LOG		12
1453910Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1463910Sdougm #define	OPT_CKSUM	13
1473910Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
1483910Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
1493910Sdougm #define	OPT_VOLFH	14
1503910Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1513910Sdougm #endif /* VOLATILE_FH_TEST */
1523910Sdougm 	NULL
1533910Sdougm };
1543910Sdougm 
1553910Sdougm /*
1563910Sdougm  * list of properties that are related to security flavors.
1573910Sdougm  */
1583910Sdougm static char *seclist[] = {
1593910Sdougm 	SHOPT_RO,
1603910Sdougm 	SHOPT_RW,
1613910Sdougm 	SHOPT_ROOT,
1623910Sdougm 	SHOPT_WINDOW,
1633910Sdougm 	NULL
1643910Sdougm };
1653910Sdougm 
1663910Sdougm /* structure for list of securities */
1673910Sdougm struct securities {
1683910Sdougm 	sa_security_t security;
1693910Sdougm 	struct securities *next;
1703910Sdougm };
1713910Sdougm 
1723910Sdougm /*
1733910Sdougm  * findopt(name)
1743910Sdougm  *
1753910Sdougm  * Lookup option "name" in the option table and return the table
1763910Sdougm  * index.
1773910Sdougm  */
1783910Sdougm 
1793910Sdougm static int
1803910Sdougm findopt(char *name)
1813910Sdougm {
1823910Sdougm 	int i;
1833910Sdougm 	if (name != NULL) {
1844345Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
1854345Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
1864345Sdougm 				return (i);
1874345Sdougm 		}
1883910Sdougm 	}
1893910Sdougm 	return (-1);
1903910Sdougm }
1913910Sdougm 
1923910Sdougm /*
1933910Sdougm  * gettype(name)
1943910Sdougm  *
1953910Sdougm  * Return the type of option "name".
1963910Sdougm  */
1973910Sdougm 
1983910Sdougm static int
1993910Sdougm gettype(char *name)
2003910Sdougm {
2013910Sdougm 	int optdef;
2023910Sdougm 
2033910Sdougm 	optdef = findopt(name);
2043910Sdougm 	if (optdef != -1)
2054345Sdougm 		return (optdefs[optdef].type);
2063910Sdougm 	return (OPT_TYPE_ANY);
2073910Sdougm }
2083910Sdougm 
2093910Sdougm /*
2103910Sdougm  * nfs_validate_security_mode(mode)
2113910Sdougm  *
2123910Sdougm  * is the specified mode string a valid one for use with NFS?
2133910Sdougm  */
2143910Sdougm 
2153910Sdougm static int
2163910Sdougm nfs_validate_security_mode(char *mode)
2173910Sdougm {
2183910Sdougm 	seconfig_t secinfo;
2193910Sdougm 	int err;
2203910Sdougm 
2213910Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2223910Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2233910Sdougm 	if (err == SC_NOERROR)
2244345Sdougm 		return (1);
2253910Sdougm 	return (0);
2263910Sdougm }
2273910Sdougm 
2283910Sdougm /*
2293910Sdougm  * nfs_is_security_opt(tok)
2303910Sdougm  *
2313910Sdougm  * check to see if tok represents an option that is only valid in some
2323910Sdougm  * security flavor.
2333910Sdougm  */
2343910Sdougm 
2353910Sdougm static int
2363910Sdougm nfs_is_security_opt(char *tok)
2373910Sdougm {
2383910Sdougm 	int i;
2393910Sdougm 
2403910Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
2414345Sdougm 		if (strcmp(tok, seclist[i]) == 0)
2424345Sdougm 			return (1);
2433910Sdougm 	}
2443910Sdougm 	return (0);
2453910Sdougm }
2463910Sdougm 
2473910Sdougm /*
2483910Sdougm  * find_security(seclist, sec)
2493910Sdougm  *
2503910Sdougm  * Walk the current list of security flavors and return true if it is
2513910Sdougm  * present, else return false.
2523910Sdougm  */
2533910Sdougm 
2543910Sdougm static int
2553910Sdougm find_security(struct securities *seclist, sa_security_t sec)
2563910Sdougm {
2573910Sdougm 	while (seclist != NULL) {
2584345Sdougm 		if (seclist->security == sec)
2594345Sdougm 			return (1);
2604345Sdougm 		seclist = seclist->next;
2613910Sdougm 	}
2623910Sdougm 	return (0);
2633910Sdougm }
2643910Sdougm 
2653910Sdougm /*
2663910Sdougm  * make_security_list(group, securitymodes, proto)
2673910Sdougm  *	go through the list of securitymodes and add them to the
2683910Sdougm  *	group's list of security optionsets. We also keep a list of
2693910Sdougm  *	those optionsets so we don't have to find them later. All of
2703910Sdougm  *	these will get copies of the same properties.
2713910Sdougm  */
2723910Sdougm 
2733910Sdougm static struct securities *
2743910Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
2753910Sdougm {
2763910Sdougm 	char *tok, *next = NULL;
2773910Sdougm 	struct securities *curp, *headp = NULL, *prev;
2783910Sdougm 	sa_security_t check;
2793910Sdougm 	int freetok = 0;
2803910Sdougm 
2813910Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
2824345Sdougm 		next = strchr(tok, ':');
2834345Sdougm 		if (next != NULL)
2844345Sdougm 			*next++ = '\0';
2854345Sdougm 		if (strcmp(tok, "default") == 0) {
2864345Sdougm 			/* resolve default into the real type */
2874345Sdougm 			tok = nfs_space_alias(tok);
2884345Sdougm 			freetok = 1;
2894345Sdougm 		}
2904345Sdougm 		check = sa_get_security(group, tok, proto);
2913910Sdougm 
2924345Sdougm 		/* add to the security list if it isn't there already */
2934345Sdougm 		if (check == NULL || !find_security(headp, check)) {
2944345Sdougm 			curp = (struct securities *)calloc(1,
2954345Sdougm 			    sizeof (struct securities));
2964345Sdougm 			if (curp != NULL) {
2974345Sdougm 				if (check == NULL) {
2984345Sdougm 					curp->security = sa_create_security(
2994345Sdougm 					    group, tok, proto);
3004345Sdougm 				} else {
3014345Sdougm 					curp->security = check;
3024345Sdougm 				}
3034345Sdougm 				/*
3044345Sdougm 				 * note that the first time through the loop,
3054345Sdougm 				 * headp will be NULL and prev will be
3064345Sdougm 				 * undefined.  Since headp is NULL, we set
3074345Sdougm 				 * both it and prev to the curp (first
3084345Sdougm 				 * structure to be allocated).
3094345Sdougm 				 *
3104345Sdougm 				 * later passes through the loop will have
3114345Sdougm 				 * headp not being NULL and prev will be used
3124345Sdougm 				 * to allocate at the end of the list.
3134345Sdougm 				 */
3144345Sdougm 				if (headp == NULL) {
3154345Sdougm 					headp = curp;
3164345Sdougm 					prev = curp;
3174345Sdougm 				} else {
3184345Sdougm 					prev->next = curp;
3194345Sdougm 					prev = curp;
3204345Sdougm 				}
3214345Sdougm 			}
3223910Sdougm 		}
3233910Sdougm 
3244345Sdougm 		if (freetok) {
3254345Sdougm 			freetok = 0;
3264345Sdougm 			sa_free_attr_string(tok);
3274345Sdougm 		}
3283910Sdougm 	}
3293910Sdougm 	return (headp);
3303910Sdougm }
3313910Sdougm 
3323910Sdougm static void
3333910Sdougm free_security_list(struct securities *sec)
3343910Sdougm {
3353910Sdougm 	struct securities *next;
3363910Sdougm 	if (sec != NULL) {
3374345Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
3384345Sdougm 			next = sec->next;
3394345Sdougm 			free(sec);
3404345Sdougm 		}
3413910Sdougm 	}
3423910Sdougm }
3433910Sdougm 
3443910Sdougm /*
3453910Sdougm  * nfs_alistcat(str1, str2, sep)
3463910Sdougm  *
3473910Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
3483910Sdougm  * character. If memory allocation fails, return NULL;
3493910Sdougm  */
3503910Sdougm 
3513910Sdougm static char *
3523910Sdougm nfs_alistcat(char *str1, char *str2, char sep)
3533910Sdougm {
3543910Sdougm 	char *newstr;
3553910Sdougm 	size_t len;
3563910Sdougm 
3573910Sdougm 	len = strlen(str1) + strlen(str2) + 2;
3583910Sdougm 	newstr = (char *)malloc(len);
3593910Sdougm 	if (newstr != NULL)
3604345Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
3613910Sdougm 	return (newstr);
3623910Sdougm }
3633910Sdougm 
3643910Sdougm /*
3653910Sdougm  * add_security_prop(sec, name, value, persist)
3663910Sdougm  *
3673910Sdougm  * Add the property to the securities structure. This accumulates
3683910Sdougm  * properties for as part of parsing legacy options.
3693910Sdougm  */
3703910Sdougm 
3713910Sdougm static int
3723910Sdougm add_security_prop(struct securities *sec, char *name, char *value,
3733910Sdougm 			int persist, int iszfs)
3743910Sdougm {
3753910Sdougm 	sa_property_t prop;
3763910Sdougm 	int ret = SA_OK;
3773910Sdougm 
3783910Sdougm 	for (; sec != NULL; sec = sec->next) {
3794345Sdougm 		if (value == NULL) {
3804345Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
3814345Sdougm 			    strcmp(name, SHOPT_RO) == 0)
3824345Sdougm 				value = "*";
3834345Sdougm 			else
3844345Sdougm 				value = "true";
3854345Sdougm 		}
3863910Sdougm 
3873910Sdougm 		/*
3883910Sdougm 		 * Get the existing property, if it exists, so we can
3893910Sdougm 		 * determine what to do with it. The ro/rw/root
3903910Sdougm 		 * properties can be merged if multiple instances of
3913910Sdougm 		 * these properies are given. For example, if "rw"
3923910Sdougm 		 * exists with a value "host1" and a later token of
3933910Sdougm 		 * rw="host2" is seen, the values are merged into a
3943910Sdougm 		 * single rw="host1:host2".
3953910Sdougm 		 */
3964345Sdougm 		prop = sa_get_property(sec->security, name);
3973910Sdougm 
3984345Sdougm 		if (prop != NULL) {
3994345Sdougm 			char *oldvalue;
4004345Sdougm 			char *newvalue;
4013910Sdougm 
4023910Sdougm 			/*
4034345Sdougm 			 * The security options of ro/rw/root might appear
4044345Sdougm 			 * multiple times. If they do, the values need to be
4054345Sdougm 			 * merged into an access list. If it was previously
4064345Sdougm 			 * empty, the new value alone is added.
4073910Sdougm 			 */
4084345Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
4094345Sdougm 			if (oldvalue != NULL) {
4104345Sdougm 				/*
4114345Sdougm 				 * The general case is to concatenate the new
4124345Sdougm 				 * value onto the old value for multiple
4134345Sdougm 				 * rw(ro/root) properties. A special case
4144345Sdougm 				 * exists when either the old or new is the
4154345Sdougm 				 * "all" case. In the special case, if both
4164345Sdougm 				 * are "all", then it is "all", else if one is
4174345Sdougm 				 * an access-list, that replaces the "all".
4184345Sdougm 				 */
4194345Sdougm 				if (strcmp(oldvalue, "*") == 0) {
4204345Sdougm 					/* Replace old value with new value. */
4214345Sdougm 					newvalue = strdup(value);
4224345Sdougm 				} else if (strcmp(value, "*") == 0) {
4234345Sdougm 					/*
4244345Sdougm 					 * Keep old value and ignore
4254345Sdougm 					 * the new value.
4264345Sdougm 					 */
4274345Sdougm 					newvalue = NULL;
4284345Sdougm 				} else {
4294345Sdougm 					/*
4304345Sdougm 					 * Make a new list of old plus new
4314345Sdougm 					 * access-list.
4324345Sdougm 					 */
4334345Sdougm 					newvalue = nfs_alistcat(oldvalue,
4344345Sdougm 					    value, ':');
4354345Sdougm 				}
4363910Sdougm 
4374345Sdougm 				if (newvalue != NULL) {
4384345Sdougm 					(void) sa_remove_property(prop);
4394345Sdougm 					prop = sa_create_property(name,
4404345Sdougm 					    newvalue);
4414345Sdougm 					ret = sa_add_property(sec->security,
4424345Sdougm 					    prop);
4434345Sdougm 					free(newvalue);
4444345Sdougm 				}
4454345Sdougm 				if (oldvalue != NULL)
4464345Sdougm 					sa_free_attr_string(oldvalue);
4474345Sdougm 			}
4484345Sdougm 		} else {
4494345Sdougm 			prop = sa_create_property(name, value);
4503910Sdougm 			ret = sa_add_property(sec->security, prop);
4513910Sdougm 		}
4524345Sdougm 		if (ret == SA_OK && !iszfs) {
4534345Sdougm 			ret = sa_commit_properties(sec->security, !persist);
4544345Sdougm 		}
4553910Sdougm 	}
4563910Sdougm 	return (ret);
4573910Sdougm }
4583910Sdougm 
4593910Sdougm /*
4603910Sdougm  * check to see if group/share is persistent.
4613910Sdougm  */
4623910Sdougm static int
4633910Sdougm is_persistent(sa_group_t group)
4643910Sdougm {
4653910Sdougm 	char *type;
4663910Sdougm 	int persist = 1;
4673910Sdougm 
4683910Sdougm 	type = sa_get_group_attr(group, "type");
4693910Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
4704345Sdougm 		persist = 0;
4713910Sdougm 	if (type != NULL)
4724345Sdougm 		sa_free_attr_string(type);
4733910Sdougm 	return (persist);
4743910Sdougm }
4753910Sdougm 
4763910Sdougm /*
4773910Sdougm  * invalid_security(options)
4783910Sdougm  *
4793910Sdougm  * search option string for any invalid sec= type.
4803910Sdougm  * return true (1) if any are not valid else false (0)
4813910Sdougm  */
4823910Sdougm static int
4833910Sdougm invalid_security(char *options)
4843910Sdougm {
4853910Sdougm 	char *copy, *base, *token, *value;
4863910Sdougm 	int ret = 0;
4873910Sdougm 
4883910Sdougm 	copy = strdup(options);
4893910Sdougm 	token = base = copy;
4903910Sdougm 	while (token != NULL && ret == 0) {
4914345Sdougm 		token = strtok(base, ",");
4924345Sdougm 		base = NULL;
4934345Sdougm 		if (token != NULL) {
4944345Sdougm 			value = strchr(token, '=');
4954345Sdougm 			if (value != NULL)
4964345Sdougm 				*value++ = '\0';
4974345Sdougm 			if (strcmp(token, "sec") == 0) {
4984345Sdougm 				/* HAVE security flavors so check them */
4994345Sdougm 				char *tok, *next;
5004345Sdougm 				for (next = NULL, tok = value; tok != NULL;
5014345Sdougm 				    tok = next) {
5024345Sdougm 					next = strchr(tok, ':');
5034345Sdougm 					if (next != NULL)
5044345Sdougm 						*next++ = '\0';
5054345Sdougm 					ret = !nfs_validate_security_mode(tok);
5064345Sdougm 					if (ret)
5074345Sdougm 						break;
5084345Sdougm 				}
5094345Sdougm 			}
5103910Sdougm 		}
5113910Sdougm 	}
5123910Sdougm 	if (copy != NULL)
5134345Sdougm 		free(copy);
5143910Sdougm 	return (ret);
5153910Sdougm }
5163910Sdougm 
5173910Sdougm /*
5183910Sdougm  * nfs_parse_legacy_options(group, options)
5193910Sdougm  *
5203910Sdougm  * Parse the old style options into internal format and store on the
5213910Sdougm  * specified group.  Group could be a share for full legacy support.
5223910Sdougm  */
5233910Sdougm 
5243910Sdougm static int
5253910Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5263910Sdougm {
5274704Sdougm 	char *dup;
5283910Sdougm 	char *base;
5293910Sdougm 	char *token;
5303910Sdougm 	sa_optionset_t optionset;
5313910Sdougm 	struct securities *security_list = NULL;
5323910Sdougm 	sa_property_t prop;
5333910Sdougm 	int ret = SA_OK;
5343910Sdougm 	int iszfs = 0;
5353910Sdougm 	sa_group_t parent;
5363910Sdougm 	int persist = 0;
5373910Sdougm 	char *lasts;
5383910Sdougm 
5393910Sdougm 	/* do we have an existing optionset? */
5403910Sdougm 	optionset = sa_get_optionset(group, "nfs");
5413910Sdougm 	if (optionset == NULL) {
5424345Sdougm 		/* didn't find existing optionset so create one */
5434345Sdougm 		optionset = sa_create_optionset(group, "nfs");
5443910Sdougm 	} else {
5453910Sdougm 		/*
5463910Sdougm 		 * have an existing optionset so we need to compare
5473910Sdougm 		 * options in order to detect errors. For now, we
5483910Sdougm 		 * assume that the first optionset is the correct one
5493910Sdougm 		 * and the others will be the same. This needs to be
5503910Sdougm 		 * fixed before the final code is ready.
5513910Sdougm 		 */
5524345Sdougm 		return (ret);
5533910Sdougm 	}
5543910Sdougm 
5553910Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
5563910Sdougm 		/*
5573910Sdougm 		 * there is a special case of only the option "rw"
5583910Sdougm 		 * being the default option. We don't have to do
5593910Sdougm 		 * anything.
5603910Sdougm 		 */
5614345Sdougm 		return (ret);
5623910Sdougm 	}
5633910Sdougm 
5643910Sdougm 	/*
5653910Sdougm 	 * check if security types are present and validate them. If
5663910Sdougm 	 * any are not legal, fail.
5673910Sdougm 	 */
5683910Sdougm 
5693910Sdougm 	if (invalid_security(options)) {
5704345Sdougm 		return (SA_INVALID_SECURITY);
5713910Sdougm 	}
5723910Sdougm 
5733910Sdougm 	/*
5743910Sdougm 	 * in order to not attempt to change ZFS properties unless
5753910Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
5763910Sdougm 	 */
5773910Sdougm 	if (sa_is_share(group)) {
5784345Sdougm 		char *zfs;
5794345Sdougm 		parent = sa_get_parent_group(group);
5804345Sdougm 		if (parent != NULL) {
5814345Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
5824345Sdougm 			if (zfs != NULL) {
5834345Sdougm 				sa_free_attr_string(zfs);
5844345Sdougm 				iszfs++;
5854345Sdougm 			}
5863910Sdougm 		}
5873910Sdougm 	} else {
5884345Sdougm 		iszfs = sa_group_is_zfs(group);
5893910Sdougm 	}
5903910Sdougm 
5914704Sdougm 	/* We need a copy of options for the next part. */
5924704Sdougm 	dup = strdup(options);
5934704Sdougm 	if (dup == NULL)
5944704Sdougm 		return (SA_NO_MEMORY);
5954704Sdougm 
5963910Sdougm 	/*
5973910Sdougm 	 * we need to step through each option in the string and then
5983910Sdougm 	 * add either the option or the security option as needed. If
5993910Sdougm 	 * this is not a persistent share, don't commit to the
6003910Sdougm 	 * repository. If there is an error, we also want to abort the
6013910Sdougm 	 * processing and report it.
6023910Sdougm 	 */
6033910Sdougm 	persist = is_persistent(group);
6043910Sdougm 	base = dup;
6053910Sdougm 	token = dup;
6063910Sdougm 	lasts = NULL;
6073910Sdougm 	while (token != NULL && ret == SA_OK) {
6084345Sdougm 		ret = SA_OK;
6094345Sdougm 		token = strtok_r(base, ",", &lasts);
6104345Sdougm 		base = NULL;
6114345Sdougm 		if (token != NULL) {
6124345Sdougm 			char *value;
6133910Sdougm 			/*
6144345Sdougm 			 * if the option has a value, it will have an '=' to
6154345Sdougm 			 * separate the name from the value. The following
6164345Sdougm 			 * code will result in value != NULL and token
6174345Sdougm 			 * pointing to just the name if there is a value.
6183910Sdougm 			 */
6194345Sdougm 			value = strchr(token, '=');
6204345Sdougm 			if (value != NULL) {
6214345Sdougm 				*value++ = '\0';
6224345Sdougm 			}
6234345Sdougm 			if (strcmp(token, "sec") == 0 ||
6244345Sdougm 			    strcmp(token, "secure") == 0) {
6253910Sdougm 				/*
6264345Sdougm 				 * Once in security parsing, we only
6274345Sdougm 				 * do security. We do need to move
6284345Sdougm 				 * between the security node and the
6294345Sdougm 				 * toplevel. The security tag goes on
6304345Sdougm 				 * the root while the following ones
6314345Sdougm 				 * go on the security.
6323910Sdougm 				 */
6334345Sdougm 				if (security_list != NULL) {
6344345Sdougm 					/*
6354345Sdougm 					 * have an old list so close it and
6364345Sdougm 					 * start the new
6374345Sdougm 					 */
6384345Sdougm 					free_security_list(security_list);
6394345Sdougm 				}
6404345Sdougm 				if (strcmp(token, "secure") == 0) {
6414345Sdougm 					value = "dh";
6424345Sdougm 				} else {
6434345Sdougm 					if (value == NULL) {
6444345Sdougm 						ret = SA_SYNTAX_ERR;
6454345Sdougm 						break;
6464345Sdougm 					}
6474345Sdougm 				}
6484345Sdougm 				security_list = make_security_list(group,
6494345Sdougm 				    value, "nfs");
6503910Sdougm 			} else {
6514345Sdougm 				/*
6524345Sdougm 				 * Note that the "old" syntax allowed a
6534345Sdougm 				 * default security model This must be
6544345Sdougm 				 * accounted for and internally converted to
6554345Sdougm 				 * "standard" security structure.
6564345Sdougm 				 */
6574345Sdougm 				if (nfs_is_security_opt(token)) {
6584345Sdougm 					if (security_list == NULL) {
6594345Sdougm 						/*
6604345Sdougm 						 * need to have a
6614345Sdougm 						 * security
6624345Sdougm 						 * option. This will
6634345Sdougm 						 * be "closed" when a
6644345Sdougm 						 * defined "sec="
6654345Sdougm 						 * option is
6664345Sdougm 						 * seen. This is
6674345Sdougm 						 * technically an
6684345Sdougm 						 * error but will be
6694345Sdougm 						 * allowed with
6704345Sdougm 						 * warning.
6714345Sdougm 						 */
6724345Sdougm 						security_list =
6734345Sdougm 						    make_security_list(group,
6744345Sdougm 						    "default",
6754345Sdougm 						    "nfs");
6764345Sdougm 					}
6774345Sdougm 					if (security_list != NULL) {
6784345Sdougm 						ret = add_security_prop(
6794345Sdougm 						    security_list, token,
6804345Sdougm 						    value, persist, iszfs);
6814345Sdougm 					} else {
6824345Sdougm 						ret = SA_NO_MEMORY;
6834345Sdougm 					}
6844345Sdougm 				} else {
6854345Sdougm 					/* regular options */
6864345Sdougm 					if (value == NULL) {
6874345Sdougm 						if (strcmp(token, SHOPT_RW) ==
6884345Sdougm 						    0 || strcmp(token,
6894345Sdougm 						    SHOPT_RO) == 0) {
6904345Sdougm 							value = "*";
6914345Sdougm 						} else {
6924345Sdougm 							value = "global";
6934345Sdougm 							if (strcmp(token,
6944345Sdougm 							    SHOPT_LOG) != 0) {
6954345Sdougm 								value = "true";
6964345Sdougm 							}
6974345Sdougm 						}
6984345Sdougm 					}
6994372Sdougm 					/*
7004372Sdougm 					 * In all cases, create the
7014372Sdougm 					 * property specified. If the
7024372Sdougm 					 * value was NULL, the default
7034372Sdougm 					 * value will have been
7044372Sdougm 					 * substituted.
7054372Sdougm 					 */
7064372Sdougm 					prop = sa_create_property(token, value);
7074372Sdougm 					ret =  sa_add_property(optionset, prop);
7084372Sdougm 					if (ret != SA_OK)
7094372Sdougm 						break;
7104372Sdougm 
7114345Sdougm 					if (!iszfs) {
7124345Sdougm 						ret = sa_commit_properties(
7134345Sdougm 						    optionset, !persist);
7144345Sdougm 					}
7154345Sdougm 				}
7163910Sdougm 			}
7173910Sdougm 		}
7183910Sdougm 	}
7193910Sdougm 	if (security_list != NULL)
7204345Sdougm 		free_security_list(security_list);
7214704Sdougm 
7224704Sdougm 	free(dup);
7233910Sdougm 	return (ret);
7243910Sdougm }
7253910Sdougm 
7263910Sdougm /*
7273910Sdougm  * is_a_number(number)
7283910Sdougm  *
7293910Sdougm  * is the string a number in one of the forms we want to use?
7303910Sdougm  */
7313910Sdougm 
7323910Sdougm static int
7333910Sdougm is_a_number(char *number)
7343910Sdougm {
7353910Sdougm 	int ret = 1;
7363910Sdougm 	int hex = 0;
7373910Sdougm 
7383910Sdougm 	if (strncmp(number, "0x", 2) == 0) {
7394345Sdougm 		number += 2;
7404345Sdougm 		hex = 1;
7414345Sdougm 	} else if (*number == '-') {
7424345Sdougm 		number++; /* skip the minus */
7434345Sdougm 	}
7443910Sdougm 	while (ret == 1 && *number != '\0') {
7454345Sdougm 		if (hex) {
7464345Sdougm 			ret = isxdigit(*number++);
7474345Sdougm 		} else {
7484345Sdougm 			ret = isdigit(*number++);
7494345Sdougm 		}
7503910Sdougm 	}
7513910Sdougm 	return (ret);
7523910Sdougm }
7533910Sdougm 
7543910Sdougm /*
7553910Sdougm  * Look for the specified tag in the configuration file. If it is found,
7563910Sdougm  * enable logging and set the logging configuration information for exp.
7573910Sdougm  */
7583910Sdougm static void
7593910Sdougm configlog(struct exportdata *exp, char *tag)
7603910Sdougm {
7613910Sdougm 	nfsl_config_t *configlist = NULL, *configp;
7623910Sdougm 	int error = 0;
7633910Sdougm 	char globaltag[] = DEFAULTTAG;
7643910Sdougm 
7653910Sdougm 	/*
7663910Sdougm 	 * Sends config errors to stderr
7673910Sdougm 	 */
7683910Sdougm 	nfsl_errs_to_syslog = B_FALSE;
7693910Sdougm 
7703910Sdougm 	/*
7713910Sdougm 	 * get the list of configuration settings
7723910Sdougm 	 */
7733910Sdougm 	error = nfsl_getconfig_list(&configlist);
7743910Sdougm 	if (error) {
7753910Sdougm 		(void) fprintf(stderr,
7764345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
7774345Sdougm 		    strerror(error));
7783910Sdougm 	}
7793910Sdougm 
7803910Sdougm 	if (tag == NULL)
7813910Sdougm 		tag = globaltag;
7823910Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
7833910Sdougm 		nfsl_freeconfig_list(&configlist);
7843910Sdougm 		(void) fprintf(stderr,
7854345Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
7863910Sdougm 		/* bad configuration */
7873910Sdougm 		error = ENOENT;
7883910Sdougm 		goto err;
7893910Sdougm 	}
7903910Sdougm 
7913910Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
7923910Sdougm 		error = ENOMEM;
7933910Sdougm 		goto out;
7943910Sdougm 	}
7953910Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
7963910Sdougm 		error = ENOMEM;
7973910Sdougm 		goto out;
7983910Sdougm 	}
7993910Sdougm 	exp->ex_flags |= EX_LOG;
8003910Sdougm 	if (configp->nc_rpclogpath != NULL)
8013910Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8023910Sdougm out:
8033910Sdougm 	if (configlist != NULL)
8044345Sdougm 		nfsl_freeconfig_list(&configlist);
8053910Sdougm 
8063910Sdougm err:
8073910Sdougm 	if (error != 0) {
8083910Sdougm 		if (exp->ex_flags != NULL)
8093910Sdougm 			free(exp->ex_tag);
8103910Sdougm 		if (exp->ex_log_buffer != NULL)
8113910Sdougm 			free(exp->ex_log_buffer);
8123910Sdougm 		(void) fprintf(stderr,
8134345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
8144345Sdougm 		    strerror(error));
8153910Sdougm 	}
8163910Sdougm }
8173910Sdougm 
8183910Sdougm /*
8193910Sdougm  * fill_export_from_optionset(export, optionset)
8203910Sdougm  *
8213910Sdougm  * In order to share, we need to set all the possible general options
8223910Sdougm  * into the export structure. Share info will be filled in by the
8233910Sdougm  * caller. Various property values get turned into structure specific
8243910Sdougm  * values.
8253910Sdougm  */
8263910Sdougm 
8273910Sdougm static int
8283910Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
8293910Sdougm {
8303910Sdougm 	sa_property_t option;
8313910Sdougm 	int ret = SA_OK;
8323910Sdougm 
8333910Sdougm 	for (option = sa_get_property(optionset, NULL);
8344345Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
8354345Sdougm 		char *name;
8364345Sdougm 		char *value;
8374345Sdougm 		uint32_t val;
8383910Sdougm 
8394345Sdougm 		/*
8404345Sdougm 		 * since options may be set/reset multiple times, always do an
8414345Sdougm 		 * explicit set or clear of the option. This allows defaults
8424345Sdougm 		 * to be set and then the protocol specifici to override.
8434345Sdougm 		 */
8443910Sdougm 
8454345Sdougm 		name = sa_get_property_attr(option, "type");
8464345Sdougm 		value = sa_get_property_attr(option, "value");
8474345Sdougm 		switch (findopt(name)) {
8484345Sdougm 		case OPT_ANON:
8494345Sdougm 			if (value != NULL && is_a_number(value)) {
8504345Sdougm 				val = strtoul(value, NULL, 0);
8514345Sdougm 			} else {
8524345Sdougm 				struct passwd *pw;
8534345Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
8544345Sdougm 				if (pw != NULL) {
8554345Sdougm 					val = pw->pw_uid;
8564345Sdougm 				} else {
8574345Sdougm 					val = UID_NOBODY;
8584345Sdougm 				}
8594345Sdougm 				endpwent();
8604345Sdougm 			}
8614345Sdougm 			export->ex_anon = val;
8624345Sdougm 			break;
8634345Sdougm 		case OPT_NOSUID:
8644345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
8654345Sdougm 			    strcmp(value, "1") == 0))
8664345Sdougm 				export->ex_flags |= EX_NOSUID;
8674345Sdougm 			else
8684345Sdougm 				export->ex_flags &= ~EX_NOSUID;
8694345Sdougm 			break;
8704345Sdougm 		case OPT_ACLOK:
8714345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
8724345Sdougm 			    strcmp(value, "1") == 0))
8734345Sdougm 				export->ex_flags |= EX_ACLOK;
8744345Sdougm 			else
8754345Sdougm 				export->ex_flags &= ~EX_ACLOK;
8764345Sdougm 			break;
8774345Sdougm 		case OPT_NOSUB:
8784345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
8794345Sdougm 			    strcmp(value, "1") == 0))
8804345Sdougm 				export->ex_flags |= EX_NOSUB;
8814345Sdougm 			else
8824345Sdougm 				export->ex_flags &= ~EX_NOSUB;
8834345Sdougm 			break;
8844345Sdougm 		case OPT_PUBLIC:
8854345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
8864345Sdougm 			    strcmp(value, "1") == 0))
8874345Sdougm 				export->ex_flags |= EX_PUBLIC;
8884345Sdougm 			else
8894345Sdougm 				export->ex_flags &= ~EX_PUBLIC;
8904345Sdougm 			break;
8914345Sdougm 		case OPT_INDEX:
8924345Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
8934345Sdougm 			    strchr(value, '/') != NULL)) {
8944345Sdougm 				/* this is an error */
8954345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
8964345Sdougm 				    "NFS: index=\"%s\" not valid;"
8974345Sdougm 				    "must be a filename.\n"),
8984345Sdougm 				    value);
8994345Sdougm 				break;
9004345Sdougm 			}
9014345Sdougm 			if (value != NULL && *value != '\0' &&
9024345Sdougm 			    strcmp(value, ".") != 0) {
9034345Sdougm 				/* valid index file string */
9044345Sdougm 				if (export->ex_index != NULL) {
9054345Sdougm 					/* left over from "default" */
9064345Sdougm 					free(export->ex_index);
9074345Sdougm 				}
9084345Sdougm 				/* remember to free */
9094345Sdougm 				export->ex_index = strdup(value);
9104345Sdougm 				if (export->ex_index == NULL) {
9114345Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
9124345Sdougm 					    "NFS: out of memory setting "
9134345Sdougm 					    "index property\n"));
9144345Sdougm 					break;
9154345Sdougm 				}
9164345Sdougm 				export->ex_flags |= EX_INDEX;
9174345Sdougm 			}
9184345Sdougm 			break;
9194345Sdougm 		case OPT_LOG:
9204345Sdougm 			if (value == NULL)
9214345Sdougm 				value = strdup("global");
9224345Sdougm 			if (value != NULL)
9234345Sdougm 				configlog(export,
9244345Sdougm 				    strlen(value) ? value : "global");
9254345Sdougm 			break;
9264345Sdougm 		default:
9274345Sdougm 			/* have a syntactic error */
9284345Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
9294345Sdougm 			    "NFS: unrecognized option %s=%s\n"),
9304345Sdougm 			    name, value != NULL ? value : "");
9314345Sdougm 			break;
9323910Sdougm 		}
9334345Sdougm 		if (name != NULL)
9344345Sdougm 			sa_free_attr_string(name);
9353910Sdougm 		if (value != NULL)
9364345Sdougm 			sa_free_attr_string(value);
9373910Sdougm 	}
9383910Sdougm 	return (ret);
9393910Sdougm }
9403910Sdougm 
9413910Sdougm /*
9423910Sdougm  * cleanup_export(export)
9433910Sdougm  *
9443910Sdougm  * Cleanup the allocated areas so we don't leak memory
9453910Sdougm  */
9463910Sdougm 
9473910Sdougm static void
9483910Sdougm cleanup_export(struct exportdata *export)
9493910Sdougm {
9503910Sdougm 	int i;
9513910Sdougm 
9523910Sdougm 	if (export->ex_index != NULL)
9534345Sdougm 		free(export->ex_index);
9543910Sdougm 	if (export->ex_secinfo != NULL) {
9554345Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
9564345Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
9574345Sdougm 				free(export->ex_secinfo[i].s_rootnames);
9584345Sdougm 		free(export->ex_secinfo);
9593910Sdougm 	}
9603910Sdougm }
9613910Sdougm 
9623910Sdougm /*
9633910Sdougm  * Given a seconfig entry and a colon-separated
9643910Sdougm  * list of names, allocate an array big enough
9653910Sdougm  * to hold the root list, then convert each name to
9663910Sdougm  * a principal name according to the security
9673910Sdougm  * info and assign it to an array element.
9683910Sdougm  * Return the array and its size.
9693910Sdougm  */
9703910Sdougm static caddr_t *
9713910Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
9723910Sdougm {
9733910Sdougm 	caddr_t *a;
9743910Sdougm 	int c, i;
9753910Sdougm 	char *host, *p;
9763910Sdougm 
9773910Sdougm 	/*
9783910Sdougm 	 * Count the number of strings in the list.
9793910Sdougm 	 * This is the number of colon separators + 1.
9803910Sdougm 	 */
9813910Sdougm 	c = 1;
9823910Sdougm 	for (p = list; *p; p++)
9833910Sdougm 		if (*p == ':')
9843910Sdougm 			c++;
9853910Sdougm 	*count = c;
9863910Sdougm 
9873910Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
9883910Sdougm 	if (a == NULL) {
9893910Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
9904345Sdougm 		    "get_rootnames: no memory\n"));
9913910Sdougm 	} else {
9924345Sdougm 		for (i = 0; i < c; i++) {
9934345Sdougm 			host = strtok(list, ":");
9944345Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
9954345Sdougm 				free(a);
9964345Sdougm 				a = NULL;
9974345Sdougm 				break;
9984345Sdougm 			}
9994345Sdougm 			list = NULL;
10003910Sdougm 		}
10013910Sdougm 	}
10023910Sdougm 
10033910Sdougm 	return (a);
10043910Sdougm }
10053910Sdougm 
10063910Sdougm /*
10073910Sdougm  * fill_security_from_secopts(sp, secopts)
10083910Sdougm  *
10093910Sdougm  * Fill the secinfo structure from the secopts optionset.
10103910Sdougm  */
10113910Sdougm 
10123910Sdougm static int
10133910Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
10143910Sdougm {
10153910Sdougm 	sa_property_t prop;
10163910Sdougm 	char *type;
10173910Sdougm 	int longform;
10183910Sdougm 	int err = SC_NOERROR;
10193910Sdougm 
10203910Sdougm 	type = sa_get_security_attr(secopts, "sectype");
10213910Sdougm 	if (type != NULL) {
10224345Sdougm 		/* named security type needs secinfo to be filled in */
10234345Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
10244345Sdougm 		sa_free_attr_string(type);
10254345Sdougm 		if (err != SC_NOERROR)
10264345Sdougm 			return (err);
10273910Sdougm 	} else {
10284345Sdougm 		/* default case */
10294345Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
10304345Sdougm 		if (err != SC_NOERROR)
10314345Sdougm 			return (err);
10323910Sdougm 	}
10333910Sdougm 
10343910Sdougm 	err = SA_OK;
10353910Sdougm 	for (prop = sa_get_property(secopts, NULL);
10364345Sdougm 	    prop != NULL && err == SA_OK;
10374345Sdougm 	    prop = sa_get_next_property(prop)) {
10384345Sdougm 		char *name;
10394345Sdougm 		char *value;
10403910Sdougm 
10414345Sdougm 		name = sa_get_property_attr(prop, "type");
10424345Sdougm 		value = sa_get_property_attr(prop, "value");
10433910Sdougm 
10444345Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
10453910Sdougm 
10464345Sdougm 		switch (findopt(name)) {
10474345Sdougm 		case OPT_RO:
10484345Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
10494345Sdougm 			break;
10504345Sdougm 		case OPT_RW:
10514345Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
10524345Sdougm 			break;
10534345Sdougm 		case OPT_ROOT:
10544345Sdougm 			sp->s_flags |= M_ROOT;
10554345Sdougm 			/*
10564345Sdougm 			 * if we are using AUTH_UNIX, handle like other things
10574345Sdougm 			 * such as RO/RW
10584345Sdougm 			 */
10594345Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
10604345Sdougm 				continue;
10614345Sdougm 			/* not AUTH_UNIX */
10624345Sdougm 			if (value != NULL) {
10634345Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
10644345Sdougm 				    value, &sp->s_rootcnt);
10654345Sdougm 				if (sp->s_rootnames == NULL) {
10664345Sdougm 					err = SA_BAD_VALUE;
10674345Sdougm 					(void) fprintf(stderr,
10684345Sdougm 					    dgettext(TEXT_DOMAIN,
10694345Sdougm 					    "Bad root list\n"));
10704345Sdougm 				}
10714345Sdougm 			}
10724345Sdougm 			break;
10734345Sdougm 		case OPT_WINDOW:
10744345Sdougm 			if (value != NULL) {
10754345Sdougm 				sp->s_window = atoi(value);
10764345Sdougm 				/* just in case */
10774345Sdougm 				if (sp->s_window < 0)
10784345Sdougm 					sp->s_window = DEF_WIN;
10794345Sdougm 			}
10804345Sdougm 			break;
10814345Sdougm 		default:
10824345Sdougm 			break;
10833910Sdougm 		}
10844345Sdougm 		if (name != NULL)
10854345Sdougm 			sa_free_attr_string(name);
10864345Sdougm 		if (value != NULL)
10874345Sdougm 			sa_free_attr_string(value);
10883910Sdougm 	}
10893910Sdougm 	/* if rw/ro options not set, use default of RW */
10903910Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
10914345Sdougm 		sp->s_flags |= M_RW;
10923910Sdougm 	return (err);
10933910Sdougm }
10943910Sdougm 
10953910Sdougm /*
10963910Sdougm  * This is for testing only
10973910Sdougm  * It displays the export structure that
10983910Sdougm  * goes into the kernel.
10993910Sdougm  */
11003910Sdougm static void
11013910Sdougm printarg(char *path, struct exportdata *ep)
11023910Sdougm {
11033910Sdougm 	int i, j;
11043910Sdougm 	struct secinfo *sp;
11053910Sdougm 
11063910Sdougm 	if (debug == 0)
11074345Sdougm 		return;
11083910Sdougm 
11093910Sdougm 	(void) printf("%s:\n", path);
11103910Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
11113910Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
11123910Sdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
11133910Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
11143910Sdougm 	if (ep->ex_flags & EX_NOSUID)
11153910Sdougm 		(void) printf("NOSUID ");
11163910Sdougm 	if (ep->ex_flags & EX_ACLOK)
11173910Sdougm 		(void) printf("ACLOK ");
11183910Sdougm 	if (ep->ex_flags & EX_PUBLIC)
11193910Sdougm 		(void) printf("PUBLIC ");
11203910Sdougm 	if (ep->ex_flags & EX_NOSUB)
11213910Sdougm 		(void) printf("NOSUB ");
11223910Sdougm 	if (ep->ex_flags & EX_LOG)
11233910Sdougm 		(void) printf("LOG ");
11243910Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
11253910Sdougm 		(void) printf("LOG_ALLOPS ");
11263910Sdougm 	if (ep->ex_flags == 0)
11273910Sdougm 		(void) printf("(none)");
11283910Sdougm 	(void) 	printf("\n");
11293910Sdougm 	if (ep->ex_flags & EX_LOG) {
11303910Sdougm 		(void) printf("\tex_log_buffer = %s\n",
11314345Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
11323910Sdougm 		(void) printf("\tex_tag = %s\n",
11334345Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
11343910Sdougm 	}
11353910Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
11363910Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
11373910Sdougm 	(void) printf("\n");
11383910Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
11393910Sdougm 		sp = &ep->ex_secinfo[i];
11403910Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
11413910Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
11423910Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
11433910Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
11443910Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
11453910Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
11463910Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
11473910Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
11483910Sdougm 		(void) printf("\n");
11493910Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
11503910Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
11513910Sdougm 		(void) fflush(stdout);
11523910Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
11533910Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
11544345Sdougm 			    sp->s_rootnames[j] : "<null>");
11553910Sdougm 		(void) printf("\n\n");
11563910Sdougm 	}
11573910Sdougm }
11583910Sdougm 
11593910Sdougm /*
11603910Sdougm  * count_security(opts)
11613910Sdougm  *
11623910Sdougm  * Count the number of security types (flavors). The optionset has
11633910Sdougm  * been populated with the security flavors as a holding mechanism.
11643910Sdougm  * We later use this number to allocate data structures.
11653910Sdougm  */
11663910Sdougm 
11673910Sdougm static int
11683910Sdougm count_security(sa_optionset_t opts)
11693910Sdougm {
11703910Sdougm 	int count = 0;
11713910Sdougm 	sa_property_t prop;
11723910Sdougm 	if (opts != NULL) {
11734345Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
11744345Sdougm 		    prop = sa_get_next_property(prop)) {
11754345Sdougm 			count++;
11764345Sdougm 		}
11773910Sdougm 	}
11783910Sdougm 	return (count);
11793910Sdougm }
11803910Sdougm 
11813910Sdougm /*
11823910Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
11833910Sdougm  *
11843910Sdougm  * provides a mechanism to format NFS properties into legacy output
11853910Sdougm  * format. If the buffer would overflow, it is reallocated and grown
11863910Sdougm  * as appropriate. Special cases of converting internal form of values
11873910Sdougm  * to those used by "share" are done. this function does one property
11883910Sdougm  * at a time.
11893910Sdougm  */
11903910Sdougm 
1191*5179Sdougm static int
11923910Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
11933910Sdougm 			sa_property_t prop, int sep)
11943910Sdougm {
11953910Sdougm 	char *name;
11963910Sdougm 	char *value;
11973910Sdougm 	int curlen;
11983910Sdougm 	char *buff = *rbuff;
11993910Sdougm 	size_t buffsize = *rbuffsize;
1200*5179Sdougm 	int printed = B_FALSE;
12013910Sdougm 
12023910Sdougm 	name = sa_get_property_attr(prop, "type");
12033910Sdougm 	value = sa_get_property_attr(prop, "value");
12043910Sdougm 	if (buff != NULL)
12054345Sdougm 		curlen = strlen(buff);
12063910Sdougm 	else
12074345Sdougm 		curlen = 0;
12083910Sdougm 	if (name != NULL) {
12094345Sdougm 		int len;
12104345Sdougm 		len = strlen(name) + sep;
12113910Sdougm 
12123910Sdougm 		/*
12133910Sdougm 		 * A future RFE would be to replace this with more
12143910Sdougm 		 * generic code and to possibly handle more types.
12153910Sdougm 		 */
12164345Sdougm 		switch (gettype(name)) {
12174345Sdougm 		case OPT_TYPE_BOOLEAN:
1218*5179Sdougm 			/*
1219*5179Sdougm 			 * For NFS, boolean value of FALSE means it
1220*5179Sdougm 			 * doesn't show up in the option list at all.
1221*5179Sdougm 			 */
12224345Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
1223*5179Sdougm 				goto skip;
1224*5179Sdougm 			if (value != NULL) {
12254345Sdougm 				sa_free_attr_string(value);
1226*5179Sdougm 				value = NULL;
1227*5179Sdougm 			}
12284345Sdougm 			break;
12294345Sdougm 		case OPT_TYPE_ACCLIST:
12304345Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
12314345Sdougm 				sa_free_attr_string(value);
12324345Sdougm 				value = NULL;
12334345Sdougm 			} else {
12344345Sdougm 				if (value != NULL)
12354345Sdougm 					len += 1 + strlen(value);
12364345Sdougm 			}
12374345Sdougm 			break;
12384345Sdougm 		case OPT_TYPE_LOGTAG:
12394345Sdougm 			if (value != NULL && strlen(value) == 0) {
12404345Sdougm 				sa_free_attr_string(value);
12414345Sdougm 				value = NULL;
12424345Sdougm 			} else {
12434345Sdougm 				if (value != NULL)
12444345Sdougm 					len += 1 + strlen(value);
12454345Sdougm 			}
12464345Sdougm 			break;
12474345Sdougm 		default:
12484345Sdougm 			if (value != NULL)
12494345Sdougm 				len += 1 + strlen(value);
12504345Sdougm 			break;
12513910Sdougm 		}
12524345Sdougm 		while (buffsize <= (curlen + len)) {
12534345Sdougm 			/* need more room */
12544345Sdougm 			buffsize += incr;
12554345Sdougm 			buff = realloc(buff, buffsize);
12564345Sdougm 			if (buff == NULL) {
12574345Sdougm 				/* realloc failed so free everything */
12584345Sdougm 				if (*rbuff != NULL)
12594345Sdougm 					free(*rbuff);
12604345Sdougm 			}
12614345Sdougm 			*rbuff = buff;
12624345Sdougm 			*rbuffsize = buffsize;
1263*5179Sdougm 			if (buff == NULL)
1264*5179Sdougm 				goto skip;
1265*5179Sdougm 
12663910Sdougm 		}
1267*5179Sdougm 
12684345Sdougm 		if (buff == NULL)
1269*5179Sdougm 			goto skip;
1270*5179Sdougm 
12714345Sdougm 		if (value == NULL) {
12724345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
12734345Sdougm 			    "%s%s", sep ? "," : "",
12744345Sdougm 			    name, value != NULL ? value : "");
12754345Sdougm 		} else {
12764345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
12774345Sdougm 			    "%s%s=%s", sep ? "," : "",
12784345Sdougm 			    name, value != NULL ? value : "");
12793910Sdougm 		}
1280*5179Sdougm 		printed = B_TRUE;
12813910Sdougm 	}
1282*5179Sdougm skip:
12833910Sdougm 	if (name != NULL)
12844345Sdougm 		sa_free_attr_string(name);
12853910Sdougm 	if (value != NULL)
12864345Sdougm 		sa_free_attr_string(value);
1287*5179Sdougm 	return (printed);
12883910Sdougm }
12893910Sdougm 
12903910Sdougm /*
12913910Sdougm  * nfs_format_options(group, hier)
12923910Sdougm  *
12933910Sdougm  * format all the options on the group into an old-style option
12943910Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
12953910Sdougm  * options.
12963910Sdougm  */
12973910Sdougm 
12983910Sdougm static char *
12993910Sdougm nfs_format_options(sa_group_t group, int hier)
13003910Sdougm {
13013910Sdougm 	sa_optionset_t options = NULL;
13024345Sdougm 	sa_optionset_t secoptions = NULL;
13033910Sdougm 	sa_property_t prop, secprop;
13044345Sdougm 	sa_security_t security = NULL;
13053910Sdougm 	char *buff;
13063910Sdougm 	size_t buffsize;
13074345Sdougm 	char *sectype = NULL;
13084345Sdougm 	int sep = 0;
13094345Sdougm 
13104345Sdougm 
13114345Sdougm 	buff = malloc(OPT_CHUNK);
13124345Sdougm 	if (buff == NULL) {
13134345Sdougm 		return (NULL);
13144345Sdougm 	}
13154345Sdougm 
13164345Sdougm 	buff[0] = '\0';
13174345Sdougm 	buffsize = OPT_CHUNK;
13184345Sdougm 
13194345Sdougm 	/*
13204345Sdougm 	 * We may have a an optionset relative to this item. format
13214345Sdougm 	 * these if we find them and then add any security definitions.
13224345Sdougm 	 */
13233910Sdougm 
13243910Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
13253910Sdougm 
13263910Sdougm 	/*
13274345Sdougm 	 * do the default set first but skip any option that is also
13284345Sdougm 	 * in the protocol specific optionset.
13293910Sdougm 	 */
13304345Sdougm 	if (options != NULL) {
13314345Sdougm 		for (prop = sa_get_property(options, NULL);
13324345Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
13333910Sdougm 			/*
13344345Sdougm 			 * use this one since we skipped any
13354345Sdougm 			 * of these that were also in
13364345Sdougm 			 * optdefault
13373910Sdougm 			 */
1338*5179Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1339*5179Sdougm 			    prop, sep))
1340*5179Sdougm 				sep = 1;
13414345Sdougm 			if (buff == NULL) {
13424345Sdougm 				/*
13434345Sdougm 				 * buff could become NULL if there
13444345Sdougm 				 * isn't enough memory for
13454345Sdougm 				 * nfs_sprint_option to realloc()
13464345Sdougm 				 * as necessary. We can't really
13474345Sdougm 				 * do anything about it at this
13484345Sdougm 				 * point so we return NULL.  The
13494345Sdougm 				 * caller should handle the
13504345Sdougm 				 * failure.
13514345Sdougm 				 */
13524345Sdougm 				if (options != NULL)
13534345Sdougm 					sa_free_derived_optionset(
13544345Sdougm 					    options);
13554345Sdougm 				return (buff);
13564345Sdougm 			}
13573910Sdougm 		}
13584345Sdougm 	}
13594345Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
13604345Sdougm 	    "nfs", hier);
13614345Sdougm 	if (secoptions != NULL) {
13623910Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
13634345Sdougm 		    secprop != NULL;
13644345Sdougm 		    secprop = sa_get_next_property(secprop)) {
13654345Sdougm 			sectype = sa_get_property_attr(secprop, "type");
13664345Sdougm 			security =
13674345Sdougm 			    (sa_security_t)sa_get_derived_security(
13684345Sdougm 			    group, sectype, "nfs", hier);
13694345Sdougm 			if (security != NULL) {
13704345Sdougm 				if (sectype != NULL) {
13714345Sdougm 					prop = sa_create_property(
13724345Sdougm 					    "sec", sectype);
1373*5179Sdougm 					if (prop == NULL)
1374*5179Sdougm 						goto err;
1375*5179Sdougm 					if (nfs_sprint_option(&buff,
1376*5179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1377*5179Sdougm 						sep = 1;
13784345Sdougm 					(void) sa_remove_property(prop);
1379*5179Sdougm 					if (buff == NULL)
1380*5179Sdougm 						goto err;
13814345Sdougm 				}
13824345Sdougm 				for (prop = sa_get_property(security,
13834345Sdougm 				    NULL); prop != NULL;
13844345Sdougm 				    prop = sa_get_next_property(prop)) {
1385*5179Sdougm 					if (nfs_sprint_option(&buff,
1386*5179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1387*5179Sdougm 						sep = 1;
13884345Sdougm 					if (buff == NULL)
13894345Sdougm 						goto err;
13904345Sdougm 				}
13914345Sdougm 				sa_free_derived_optionset(security);
13923910Sdougm 			}
13934345Sdougm 			if (sectype != NULL)
13944345Sdougm 				sa_free_attr_string(sectype);
13953910Sdougm 		}
13963910Sdougm 		sa_free_derived_optionset(secoptions);
13973910Sdougm 	}
13984345Sdougm 
13993910Sdougm 	if (options != NULL)
14004345Sdougm 		sa_free_derived_optionset(options);
14014345Sdougm 	return (buff);
14024345Sdougm 
14034345Sdougm err:
14044345Sdougm 	/*
14054345Sdougm 	 * If we couldn't allocate memory for option printing, we need
14064345Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
14074345Sdougm 	 */
14084345Sdougm 	if (secoptions != NULL)
14094345Sdougm 		sa_free_derived_optionset(secoptions);
14104345Sdougm 	if (security != NULL)
14114345Sdougm 		sa_free_derived_optionset(security);
14124345Sdougm 	if (sectype != NULL)
14134345Sdougm 		sa_free_attr_string(sectype);
14144345Sdougm 	if (options != NULL)
14154345Sdougm 		sa_free_derived_optionset(options);
14163910Sdougm 	return (buff);
14173910Sdougm }
14184345Sdougm 
14193910Sdougm /*
14203910Sdougm  * Append an entry to the nfslogtab file
14213910Sdougm  */
14223910Sdougm static int
14233910Sdougm nfslogtab_add(dir, buffer, tag)
14243910Sdougm 	char *dir, *buffer, *tag;
14253910Sdougm {
14263910Sdougm 	FILE *f;
14273910Sdougm 	struct logtab_ent lep;
14283910Sdougm 	int error = 0;
14293910Sdougm 
14303910Sdougm 	/*
14313910Sdougm 	 * Open the file for update and create it if necessary.
14323910Sdougm 	 * This may leave the I/O offset at the end of the file,
14333910Sdougm 	 * so rewind back to the beginning of the file.
14343910Sdougm 	 */
14353910Sdougm 	f = fopen(NFSLOGTAB, "a+");
14363910Sdougm 	if (f == NULL) {
14373910Sdougm 		error = errno;
14383910Sdougm 		goto out;
14393910Sdougm 	}
14403910Sdougm 	rewind(f);
14413910Sdougm 
14423910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
14433910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
14444345Sdougm 		    "share complete, however failed to lock %s "
14454345Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
14463910Sdougm 		error = -1;
14473910Sdougm 		goto out;
14483910Sdougm 	}
14493910Sdougm 
14503910Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
14513910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
14524345Sdougm 		    "share complete, however could not deactivate "
14534345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14543910Sdougm 		error = -1;
14553910Sdougm 		goto out;
14563910Sdougm 	}
14573910Sdougm 
14583910Sdougm 	/*
14593910Sdougm 	 * Remove entries matching buffer and sharepoint since we're
14603910Sdougm 	 * going to replace it with perhaps an entry with a new tag.
14613910Sdougm 	 */
14623910Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
14633910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
14644345Sdougm 		    "share complete, however could not remove matching "
14654345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14663910Sdougm 		error = -1;
14673910Sdougm 		goto out;
14683910Sdougm 	}
14693910Sdougm 
14703910Sdougm 	/*
14713910Sdougm 	 * Deactivate all active entries matching this sharepoint
14723910Sdougm 	 */
14733910Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
14743910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
14754345Sdougm 		    "share complete, however could not deactivate matching "
14764345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14773910Sdougm 		error = -1;
14783910Sdougm 		goto out;
14793910Sdougm 	}
14803910Sdougm 
14813910Sdougm 	lep.le_buffer = buffer;
14823910Sdougm 	lep.le_path = dir;
14833910Sdougm 	lep.le_tag = tag;
14843910Sdougm 	lep.le_state = LES_ACTIVE;
14853910Sdougm 
14863910Sdougm 	/*
14873910Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
14883910Sdougm 	 */
14893910Sdougm 	if (logtab_putent(f, &lep) < 0) {
14903910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
14914345Sdougm 		    "share complete, however could not add %s to %s\n"),
14924345Sdougm 		    dir, NFSLOGTAB);
14933910Sdougm 		error = -1;
14943910Sdougm 	}
14953910Sdougm 
14963910Sdougm out:
14973910Sdougm 	if (f != NULL)
14983910Sdougm 		(void) fclose(f);
14993910Sdougm 	return (error);
15003910Sdougm }
15013910Sdougm 
15023910Sdougm /*
15033910Sdougm  * Deactivate an entry from the nfslogtab file
15043910Sdougm  */
15053910Sdougm static int
15063910Sdougm nfslogtab_deactivate(path)
15073910Sdougm 	char *path;
15083910Sdougm {
15093910Sdougm 	FILE *f;
15103910Sdougm 	int error = 0;
15113910Sdougm 
15123910Sdougm 	f = fopen(NFSLOGTAB, "r+");
15133910Sdougm 	if (f == NULL) {
15143910Sdougm 		error = errno;
15153910Sdougm 		goto out;
15163910Sdougm 	}
15173910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
15183910Sdougm 		error = errno;
15193910Sdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
15204345Sdougm 		    "share complete, however could not lock %s for "
15214345Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
15223910Sdougm 		goto out;
15233910Sdougm 	}
15243910Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
15253910Sdougm 		error = -1;
15263910Sdougm 		(void) fprintf(stderr,
15274345Sdougm 		    dgettext(TEXT_DOMAIN,
15284345Sdougm 		    "share complete, however could not "
15294345Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
15303910Sdougm 		goto out;
15313910Sdougm 	}
15323910Sdougm 
15333910Sdougm out:	if (f != NULL)
15343910Sdougm 		(void) fclose(f);
15353910Sdougm 
15363910Sdougm 	return (error);
15373910Sdougm }
15383910Sdougm 
15393910Sdougm /*
15404524Sdougm  * check_public(group, skipshare)
15414524Sdougm  *
15424524Sdougm  * Check the group for any shares that have the public property
15434524Sdougm  * enabled. We skip "skipshare" since that is the one we are
15444524Sdougm  * working with. This is a separate function to make handling
15454524Sdougm  * subgroups simpler. Returns true if there is a share with public.
15464524Sdougm  */
15474524Sdougm static int
15484524Sdougm check_public(sa_group_t group, sa_share_t skipshare)
15494524Sdougm {
15504524Sdougm 	int exists = B_FALSE;
15514524Sdougm 	sa_share_t share;
15524524Sdougm 	sa_optionset_t opt;
15534524Sdougm 	sa_property_t prop;
15544524Sdougm 	char *shared;
15554524Sdougm 
15564524Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
15574524Sdougm 	    share = sa_get_next_share(share)) {
15584524Sdougm 		if (share == skipshare)
15594524Sdougm 			continue;
15604524Sdougm 
15614524Sdougm 		opt = sa_get_optionset(share, "nfs");
15624524Sdougm 		if (opt == NULL)
15634524Sdougm 			continue;
15644524Sdougm 		prop = sa_get_property(opt, "public");
15654524Sdougm 		if (prop == NULL)
15664524Sdougm 			continue;
15674524Sdougm 		shared = sa_get_share_attr(share, "shared");
15684524Sdougm 		if (shared != NULL) {
15694524Sdougm 			exists = strcmp(shared, "true") == 0;
15704524Sdougm 			sa_free_attr_string(shared);
15714524Sdougm 			if (exists == B_TRUE)
15724524Sdougm 				break;
15734524Sdougm 		}
15744524Sdougm 	}
15754524Sdougm 
15764524Sdougm 	return (exists);
15774524Sdougm }
15784524Sdougm 
15794524Sdougm /*
15803910Sdougm  * public_exists(share)
15813910Sdougm  *
15823910Sdougm  * check to see if public option is set on any other share than the
15834524Sdougm  * one specified. Need to check zfs sub-groups as well as the top
15844524Sdougm  * level groups.
15853910Sdougm  */
15863910Sdougm static int
15873910Sdougm public_exists(sa_share_t skipshare)
15883910Sdougm {
15893910Sdougm 	sa_group_t group;
15903910Sdougm 	sa_handle_t handle;
15913910Sdougm 
15923910Sdougm 	group = sa_get_parent_group(skipshare);
15933910Sdougm 	if (group == NULL)
15944345Sdougm 		return (SA_NO_SUCH_GROUP);
15953910Sdougm 
15963910Sdougm 	handle = sa_find_group_handle(group);
15973910Sdougm 	if (handle == NULL)
15984345Sdougm 		return (SA_SYSTEM_ERR);
15993910Sdougm 
16003910Sdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
16013910Sdougm 	    group = sa_get_next_group(group)) {
16024524Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
16034524Sdougm 		if (sa_group_is_zfs(group)) {
16044524Sdougm 			sa_group_t subgroup;
16054524Sdougm 			for (subgroup = sa_get_sub_group(group);
16064524Sdougm 			    subgroup != NULL;
16074524Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
16084524Sdougm 				if (check_public(subgroup, skipshare))
16094524Sdougm 					return (B_TRUE);
16103910Sdougm 			}
16114524Sdougm 		} else {
16124524Sdougm 			if (check_public(group, skipshare))
16134524Sdougm 				return (B_TRUE);
16143910Sdougm 		}
16153910Sdougm 	}
16164524Sdougm 	return (B_FALSE);
16173910Sdougm }
16183910Sdougm 
16193910Sdougm /*
16203910Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
16213910Sdougm  * implementation that it is to enable the share. This entails
16223910Sdougm  * converting the path and options into the appropriate ioctl
16233910Sdougm  * calls. It is assumed that all error checking of paths, etc. were
16243910Sdougm  * done earlier.
16253910Sdougm  */
16263910Sdougm static int
16273910Sdougm nfs_enable_share(sa_share_t share)
16283910Sdougm {
16293910Sdougm 	struct exportdata export;
16303910Sdougm 	sa_optionset_t secoptlist;
16313910Sdougm 	struct secinfo *sp;
16323910Sdougm 	int num_secinfo;
16333910Sdougm 	sa_optionset_t opt;
16343910Sdougm 	sa_security_t sec;
16353910Sdougm 	sa_property_t prop;
16363910Sdougm 	char *path;
16373910Sdougm 	int err = SA_OK;
16384524Sdougm 	int i;
16394543Smarks 	int iszfs;
16403910Sdougm 
16413910Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
16423910Sdougm 	(void) signal(SIGSYS, SIG_IGN);
16433910Sdougm 
16443910Sdougm 	/* get the path since it is important in several places */
16453910Sdougm 	path = sa_get_share_attr(share, "path");
16463910Sdougm 	if (path == NULL)
16474345Sdougm 		return (SA_NO_SUCH_PATH);
16483910Sdougm 
16494543Smarks 	iszfs = sa_path_is_zfs(path);
16503910Sdougm 	/*
16513910Sdougm 	 * find the optionsets and security sets.  There may not be
16523910Sdougm 	 * any or there could be one or two for each of optionset and
16533910Sdougm 	 * security may have multiple, one per security type per
16543910Sdougm 	 * protocol type.
16553910Sdougm 	 */
16563910Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
16573910Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
16583910Sdougm 	if (secoptlist != NULL)
16594345Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
16603910Sdougm 	else
16614345Sdougm 		num_secinfo = 1;
16623910Sdougm 
16633910Sdougm 	/*
16643910Sdougm 	 * walk through the options and fill in the structure
16653910Sdougm 	 * appropriately.
16663910Sdougm 	 */
16673910Sdougm 
16683910Sdougm 	(void) memset(&export, '\0', sizeof (export));
16693910Sdougm 
16703910Sdougm 	/*
16713910Sdougm 	 * do non-security options first since there is only one after
16723910Sdougm 	 * the derived group is constructed.
16733910Sdougm 	 */
16743910Sdougm 	export.ex_version = EX_CURRENT_VERSION;
16753910Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
16763910Sdougm 	export.ex_index = NULL;
16773910Sdougm 	export.ex_path = path;
16783910Sdougm 	export.ex_pathlen = strlen(path) + 1;
16793910Sdougm 
16803910Sdougm 	if (opt != NULL)
16814345Sdougm 		err = fill_export_from_optionset(&export, opt);
16823910Sdougm 
16833910Sdougm 	/*
16843910Sdougm 	 * check to see if "public" is set. If it is, then make sure
16853910Sdougm 	 * no other share has it set. If it is already used, fail.
16863910Sdougm 	 */
16873910Sdougm 
16883910Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(share)) {
16894345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
16904345Sdougm 		    "NFS: Cannot share more than one file "
16914345Sdougm 		    "system with 'public' property\n"));
16924345Sdougm 		err = SA_NOT_ALLOWED;
16934345Sdougm 		goto out;
16943910Sdougm 	}
16953910Sdougm 
16964524Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
16973910Sdougm 	if (sp == NULL) {
16984345Sdougm 		err = SA_NO_MEMORY;
16994524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
17004524Sdougm 		    "NFS: NFS: no memory for security\n"));
17014524Sdougm 		goto out;
17024524Sdougm 	}
17034524Sdougm 	export.ex_secinfo = sp;
17044524Sdougm 	/* get default secinfo */
17054524Sdougm 	export.ex_seccnt = num_secinfo;
17064524Sdougm 	/*
17074524Sdougm 	 * since we must have one security option defined, we
17084524Sdougm 	 * init to the default and then override as we find
17094524Sdougm 	 * defined security options. This handles the case
17104524Sdougm 	 * where we have no defined options but we need to set
17114524Sdougm 	 * up one.
17124524Sdougm 	 */
17134524Sdougm 	sp[0].s_window = DEF_WIN;
17144524Sdougm 	sp[0].s_rootnames = NULL;
17154524Sdougm 	/* setup a default in case no properties defined */
17164524Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
17174524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
17184524Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
17194524Sdougm 		    "get default security mode\n"));
17204524Sdougm 		err = SA_CONFIG_ERR;
17214524Sdougm 	}
17224524Sdougm 	if (secoptlist != NULL) {
17234524Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
17244524Sdougm 		    prop != NULL && i < num_secinfo;
17254524Sdougm 		    prop = sa_get_next_property(prop), i++) {
17264524Sdougm 			char *sectype;
17274345Sdougm 				sectype = sa_get_property_attr(prop, "type");
17284524Sdougm 			/*
17294524Sdougm 			 * if sectype is NULL, we probably
17304524Sdougm 			 * have a memory problem and can't get
17314524Sdougm 			 * the correct values. Rather than
17324524Sdougm 			 * exporting with incorrect security,
17334524Sdougm 			 * don't share it.
17344524Sdougm 			 */
17354524Sdougm 			if (sectype == NULL) {
17364524Sdougm 				err = SA_NO_MEMORY;
17374524Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
17384524Sdougm 				    "NFS: Cannot share %s: "
17394524Sdougm 				    "no memory\n"), path);
17404524Sdougm 				goto out;
17414524Sdougm 			}
17424524Sdougm 			sec = (sa_security_t)sa_get_derived_security(
17434524Sdougm 			    share, sectype, "nfs", 1);
17444524Sdougm 			sp[i].s_window = DEF_WIN;
17454524Sdougm 			sp[i].s_rootcnt = 0;
17464524Sdougm 			sp[i].s_rootnames = NULL;
17474345Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
17484524Sdougm 			if (sec != NULL)
17494524Sdougm 				sa_free_derived_security(sec);
17504524Sdougm 			if (sectype != NULL)
17514524Sdougm 				sa_free_attr_string(sectype);
17523910Sdougm 		}
17534524Sdougm 	}
17544524Sdougm 	/*
17554524Sdougm 	 * when we get here, we can do the exportfs system call and
17564524Sdougm 	 * initiate thinsg. We probably want to enable the nfs.server
17574524Sdougm 	 * service first if it isn't running within SMF.
17584524Sdougm 	 */
17594524Sdougm 	/* check nfs.server status and start if needed */
17604524Sdougm 	/* now add the share to the internal tables */
17614524Sdougm 	printarg(path, &export);
17624524Sdougm 	/*
17634524Sdougm 	 * call the exportfs system call which is implemented
17644524Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
17654524Sdougm 	 */
17664543Smarks 	if (iszfs) {
17674543Smarks 		struct exportfs_args ea;
17684543Smarks 		share_t sh;
17694543Smarks 		char *str;
17704543Smarks 		priv_set_t *priv_effective;
17714543Smarks 		int privileged;
17724543Smarks 
17734543Smarks 		/*
17744543Smarks 		 * If we aren't a privileged user
17754543Smarks 		 * and NFS server service isn't running
17764543Smarks 		 * then print out an error message
17774543Smarks 		 * and return EPERM
17784543Smarks 		 */
17794543Smarks 
17804543Smarks 		priv_effective = priv_allocset();
17814543Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
17824543Smarks 
17834543Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
17844543Smarks 		priv_freeset(priv_effective);
17854543Smarks 
17864543Smarks 		if (!privileged &&
17874543Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
17884543Smarks 			err = 0;
17894543Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
17904543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
17914543Smarks 				    "NFS: Cannot share remote "
17924543Smarks 				    "filesystem: %s\n"), path);
17934543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
17944543Smarks 				    "NFS: Service needs to be enabled "
17954543Smarks 				    "by a privileged user\n"));
17964543Smarks 				err = SA_SYSTEM_ERR;
17974543Smarks 				errno = EPERM;
17984543Smarks 			}
17994543Smarks 			free(str);
18004543Smarks 		}
18014543Smarks 
18024543Smarks 		if (err == 0) {
18034543Smarks 			ea.dname = path;
18044543Smarks 			ea.uex = &export;
18054543Smarks 
18064543Smarks 			sa_sharetab_fill_zfs(share, &sh, "nfs");
18074543Smarks 			err = sa_share_zfs(share, path, &sh, &ea, B_TRUE);
18084543Smarks 			sa_emptyshare(&sh);
18094543Smarks 		}
18104543Smarks 	} else {
18114543Smarks 		err = exportfs(path, &export);
18124543Smarks 	}
18134543Smarks 
18144543Smarks 	if (err < 0) {
18154524Sdougm 		err = SA_SYSTEM_ERR;
18164524Sdougm 		switch (errno) {
18174524Sdougm 		case EREMOTE:
18184524Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
18194543Smarks 			    "NFS: Cannot share filesystems "
18204543Smarks 			    "in non-global zones: %s\n"), path);
18214543Smarks 			err = SA_NOT_SUPPORTED;
18224524Sdougm 			break;
18234524Sdougm 		case EPERM:
18244524Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
18254345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
18264543Smarks 				    "NFS: Cannot share file systems "
18274524Sdougm 				    "in non-global zones: %s\n"), path);
18284524Sdougm 				err = SA_NOT_SUPPORTED;
18294345Sdougm 				break;
18304345Sdougm 			}
18314524Sdougm 			err = SA_NO_PERMISSION;
18324524Sdougm 			/* FALLTHROUGH */
18334524Sdougm 		default:
18344524Sdougm 			break;
18353910Sdougm 		}
18364524Sdougm 	} else {
18374524Sdougm 		/* update sharetab with an add/modify */
18384543Smarks 		if (!iszfs) {
18394543Smarks 			(void) sa_update_sharetab(share, "nfs");
18404543Smarks 		}
18413910Sdougm 	}
18423910Sdougm 
18433910Sdougm 	if (err == SA_OK) {
18443910Sdougm 		/*
18453910Sdougm 		 * enable services as needed. This should probably be
18463910Sdougm 		 * done elsewhere in order to minimize the calls to
18473910Sdougm 		 * check services.
18483910Sdougm 		 */
18493910Sdougm 		/*
18503910Sdougm 		 * check to see if logging and other services need to
18513910Sdougm 		 * be triggered, but only if there wasn't an
18523910Sdougm 		 * error. This is probably where sharetab should be
18533910Sdougm 		 * updated with the NFS specific entry.
18543910Sdougm 		 */
18554345Sdougm 		if (export.ex_flags & EX_LOG) {
18564345Sdougm 			/* enable logging */
18574345Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
18584345Sdougm 			    export.ex_tag) != 0) {
18594345Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
18604345Sdougm 				    "Could not enable logging for %s\n"),
18614345Sdougm 				    path);
18624345Sdougm 			}
18634345Sdougm 			_check_services(service_list_logging);
18644345Sdougm 		} else {
18654345Sdougm 			/*
18664345Sdougm 			 * don't have logging so remove it from file. It might
18674345Sdougm 			 * not be thre, but that doesn't matter.
18684345Sdougm 			 */
18694345Sdougm 			(void) nfslogtab_deactivate(path);
18704345Sdougm 			_check_services(service_list_default);
18713910Sdougm 		}
18723910Sdougm 	}
18733910Sdougm 
18743910Sdougm out:
18753910Sdougm 	if (path != NULL)
18764345Sdougm 		free(path);
18773910Sdougm 
18783910Sdougm 	cleanup_export(&export);
18793910Sdougm 	if (opt != NULL)
18804345Sdougm 		sa_free_derived_optionset(opt);
18813910Sdougm 	if (secoptlist != NULL)
18824345Sdougm 		(void) sa_destroy_optionset(secoptlist);
18833910Sdougm 	return (err);
18843910Sdougm }
18853910Sdougm 
18863910Sdougm /*
18873910Sdougm  * nfs_disable_share(share)
18883910Sdougm  *
18893910Sdougm  * Unshare the specified share.  How much error checking should be
18903910Sdougm  * done? We only do basic errors for now.
18913910Sdougm  */
18923910Sdougm static int
18934543Smarks nfs_disable_share(sa_share_t share, char *path)
18943910Sdougm {
18953910Sdougm 	int err;
18963910Sdougm 	int ret = SA_OK;
18974543Smarks 	int iszfs;
18983910Sdougm 
18994543Smarks 
19004543Smarks 	if (path != NULL) {
19014543Smarks 		iszfs = sa_path_is_zfs(path);
19024543Smarks 
19034543Smarks 		if (iszfs) {
19044543Smarks 			struct exportfs_args ea;
19054543Smarks 			share_t sh = { 0 };
19064543Smarks 
19074543Smarks 			ea.dname = path;
19084543Smarks 			ea.uex = NULL;
19094543Smarks 			sh.sh_path = path;
19104543Smarks 			sh.sh_fstype = "nfs";
19114543Smarks 
19124543Smarks 			err = sa_share_zfs(share, path, &sh, &ea, B_FALSE);
19134543Smarks 		} else
19144543Smarks 			err = exportfs(path, NULL);
19154345Sdougm 		if (err < 0) {
19164345Sdougm 			/*
19174543Smarks 			 * TBD: only an error in some
19184543Smarks 			 * cases - need better analysis
19194345Sdougm 			 */
19204543Smarks 
19214345Sdougm 			switch (errno) {
19224345Sdougm 			case EPERM:
19234345Sdougm 			case EACCES:
19244345Sdougm 				ret = SA_NO_PERMISSION;
19254543Smarks 				if (getzoneid() != GLOBAL_ZONEID) {
19264345Sdougm 					ret = SA_NOT_SUPPORTED;
19274543Smarks 				}
19284345Sdougm 				break;
19294345Sdougm 			case EINVAL:
19304345Sdougm 			case ENOENT:
19314345Sdougm 				ret = SA_NO_SUCH_PATH;
19324543Smarks 			break;
19334345Sdougm 			default:
19344345Sdougm 				ret = SA_SYSTEM_ERR;
19354543Smarks 			break;
19364345Sdougm 			}
19373910Sdougm 		}
19384345Sdougm 		if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
19394543Smarks 			if (!iszfs)
19404543Smarks 				(void) sa_delete_sharetab(path, "nfs");
19414345Sdougm 			/* just in case it was logged */
19424543Smarks 			(void) nfslogtab_deactivate(path);
19434345Sdougm 		}
19443910Sdougm 	}
19453910Sdougm 	return (ret);
19463910Sdougm }
19473910Sdougm 
19483910Sdougm /*
19493910Sdougm  * check ro vs rw values.  Over time this may get beefed up.
19503910Sdougm  * for now it just does simple checks.
19513910Sdougm  */
19523910Sdougm 
19533910Sdougm static int
19543910Sdougm check_rorw(char *v1, char *v2)
19553910Sdougm {
19563910Sdougm 	int ret = SA_OK;
19573910Sdougm 	if (strcmp(v1, v2) == 0)
19584345Sdougm 		ret = SA_VALUE_CONFLICT;
19593910Sdougm 	return (ret);
19603910Sdougm }
19613910Sdougm 
19623910Sdougm /*
19633910Sdougm  * nfs_validate_property(property, parent)
19643910Sdougm  *
19653910Sdougm  * Check that the property has a legitimate value for its type.
19663910Sdougm  */
19673910Sdougm 
19683910Sdougm static int
19693910Sdougm nfs_validate_property(sa_property_t property, sa_optionset_t parent)
19703910Sdougm {
19713910Sdougm 	int ret = SA_OK;
19723910Sdougm 	char *propname;
19733910Sdougm 	char *other;
19743910Sdougm 	int optindex;
19753910Sdougm 	nfsl_config_t *configlist;
19763910Sdougm 	sa_group_t parent_group;
19773910Sdougm 	char *value;
19783910Sdougm 
19793910Sdougm 	propname = sa_get_property_attr(property, "type");
19803910Sdougm 
19813910Sdougm 	if ((optindex = findopt(propname)) < 0)
19824345Sdougm 		ret = SA_NO_SUCH_PROP;
19833910Sdougm 
19843910Sdougm 	/* need to validate value range here as well */
19853910Sdougm 
19863910Sdougm 	if (ret == SA_OK) {
19874345Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
19884345Sdougm 		if (optdefs[optindex].share && !sa_is_share(parent_group))
19894345Sdougm 			ret = SA_PROP_SHARE_ONLY;
19903910Sdougm 	}
19913910Sdougm 	if (ret == SA_OK) {
19924345Sdougm 		value = sa_get_property_attr(property, "value");
19934345Sdougm 		if (value != NULL) {
19944345Sdougm 			/* first basic type checking */
19954345Sdougm 			switch (optdefs[optindex].type) {
19964345Sdougm 			case OPT_TYPE_NUMBER:
19974345Sdougm 				/* check that the value is all digits */
19984345Sdougm 				if (!is_a_number(value))
19994345Sdougm 					ret = SA_BAD_VALUE;
20004345Sdougm 				break;
20014345Sdougm 			case OPT_TYPE_BOOLEAN:
20024345Sdougm 				if (strlen(value) == 0 ||
20034345Sdougm 				    strcasecmp(value, "true") == 0 ||
20044345Sdougm 				    strcmp(value, "1") == 0 ||
20054345Sdougm 				    strcasecmp(value, "false") == 0 ||
20064345Sdougm 				    strcmp(value, "0") == 0) {
20074345Sdougm 					ret = SA_OK;
20084345Sdougm 				} else {
20094345Sdougm 					ret = SA_BAD_VALUE;
20104345Sdougm 				}
20114345Sdougm 				break;
20124345Sdougm 			case OPT_TYPE_USER:
20134345Sdougm 				if (!is_a_number(value)) {
20144345Sdougm 					struct passwd *pw;
20154345Sdougm 					/*
20164345Sdougm 					 * in this case it would have to be a
20174345Sdougm 					 * user name
20184345Sdougm 					 */
20194345Sdougm 					pw = getpwnam(value);
20204345Sdougm 					if (pw == NULL)
20214345Sdougm 						ret = SA_BAD_VALUE;
20224345Sdougm 					endpwent();
20234345Sdougm 				} else {
20244345Sdougm 					uint64_t intval;
20254345Sdougm 					intval = strtoull(value, NULL, 0);
20264345Sdougm 					if (intval > UID_MAX && intval != ~0)
20274345Sdougm 						ret = SA_BAD_VALUE;
20284345Sdougm 				}
20294345Sdougm 				break;
20304345Sdougm 			case OPT_TYPE_FILE:
20314345Sdougm 				if (strcmp(value, "..") == 0 ||
20324345Sdougm 				    strchr(value, '/') != NULL) {
20334345Sdougm 					ret = SA_BAD_VALUE;
20344345Sdougm 				}
20354345Sdougm 				break;
20364345Sdougm 			case OPT_TYPE_ACCLIST:
20374345Sdougm 				/*
20384345Sdougm 				 * access list handling. Should eventually
20394345Sdougm 				 * validate that all the values make sense.
20404345Sdougm 				 * Also, ro and rw may have cross value
20414345Sdougm 				 * conflicts.
20424345Sdougm 				 */
20434345Sdougm 				if (strcmp(propname, SHOPT_RO) == 0)
20444345Sdougm 					other = SHOPT_RW;
20454345Sdougm 				else if (strcmp(propname, SHOPT_RW) == 0)
20464345Sdougm 					other = SHOPT_RO;
20474345Sdougm 				else
20484345Sdougm 					other = NULL;
20494345Sdougm 
20504345Sdougm 				if (other != NULL && parent != NULL) {
20514345Sdougm 					/* compare rw(ro) with ro(rw) */
20524345Sdougm 					sa_property_t oprop;
20534345Sdougm 					oprop = sa_get_property(parent, other);
20544345Sdougm 					if (oprop != NULL) {
20554345Sdougm 						/*
20564345Sdougm 						 * only potential
20574345Sdougm 						 * confusion if other
20584345Sdougm 						 * exists
20594345Sdougm 						 */
20604345Sdougm 						char *ovalue;
20614345Sdougm 						ovalue = sa_get_property_attr(
20624345Sdougm 						    oprop, "value");
20634345Sdougm 						if (ovalue != NULL) {
20644345Sdougm 							ret = check_rorw(value,
20654345Sdougm 							    ovalue);
20664345Sdougm 							sa_free_attr_string(
20674345Sdougm 							    ovalue);
20684345Sdougm 						}
20694345Sdougm 					}
20704345Sdougm 				}
20714345Sdougm 				break;
20724345Sdougm 			case OPT_TYPE_LOGTAG:
20734345Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
20744345Sdougm 					int error;
20754345Sdougm 					if (value == NULL ||
20764345Sdougm 					    strlen(value) == 0) {
20774345Sdougm 						if (value != NULL)
20784345Sdougm 							sa_free_attr_string(
20794345Sdougm 							    value);
20804345Sdougm 						value = strdup("global");
20814345Sdougm 					}
20824345Sdougm 					if (value != NULL &&
20834345Sdougm 					    nfsl_findconfig(configlist, value,
20844345Sdougm 					    &error) == NULL) {
20854345Sdougm 						ret = SA_BAD_VALUE;
20864345Sdougm 					}
2087*5179Sdougm 					/* Must always free when done */
2088*5179Sdougm 					nfsl_freeconfig_list(&configlist);
20894345Sdougm 				} else {
20904345Sdougm 					ret = SA_CONFIG_ERR;
20914345Sdougm 				}
20924345Sdougm 				break;
20934345Sdougm 			case OPT_TYPE_STRING:
20944345Sdougm 				/* whatever is here should be ok */
20954345Sdougm 				break;
20964345Sdougm 			case OPT_TYPE_SECURITY:
20974345Sdougm 				/*
20984345Sdougm 				 * The "sec" property isn't used in the
20994345Sdougm 				 * non-legacy parts of sharemgr. We need to
21004345Sdougm 				 * reject it here. For legacy, it is pulled
21014345Sdougm 				 * out well before we get here.
21024345Sdougm 				 */
21034345Sdougm 				ret = SA_NO_SUCH_PROP;
21044345Sdougm 				break;
21054345Sdougm 			default:
21064345Sdougm 				break;
21073910Sdougm 			}
2108*5179Sdougm 
2109*5179Sdougm 			if (value != NULL)
2110*5179Sdougm 				sa_free_attr_string(value);
2111*5179Sdougm 
21124345Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
21134345Sdougm 				/* do the property specific check */
21144345Sdougm 				ret = optdefs[optindex].check(property);
21153910Sdougm 			}
21163910Sdougm 		}
21173910Sdougm 	}
21183910Sdougm 
21193910Sdougm 	if (propname != NULL)
21204345Sdougm 		sa_free_attr_string(propname);
21213910Sdougm 	return (ret);
21223910Sdougm }
21233910Sdougm 
21243910Sdougm /*
21253910Sdougm  * Protocol management functions
21263910Sdougm  *
21273910Sdougm  * Properties defined in the default files are defined in
21283910Sdougm  * proto_option_defs for parsing and validation. If "other" and
21293910Sdougm  * "compare" are set, then the value for this property should be
21303910Sdougm  * compared against the property specified in "other" using the
21313910Sdougm  * "compare" check (either <= or >=) in order to ensure that the
21323910Sdougm  * values are in the correct range.  E.g. setting server_versmin
21333910Sdougm  * higher than server_versmax should not be allowed.
21343910Sdougm  */
21353910Sdougm 
21363910Sdougm struct proto_option_defs {
21373910Sdougm 	char *tag;
21383910Sdougm 	char *name;	/* display name -- remove protocol identifier */
21393910Sdougm 	int index;
21403910Sdougm 	int type;
21413910Sdougm 	union {
21423910Sdougm 	    int intval;
21433910Sdougm 	    char *string;
21443910Sdougm 	} defvalue;
21453910Sdougm 	uint32_t svcs;
21463910Sdougm 	int32_t minval;
21473910Sdougm 	int32_t maxval;
21483910Sdougm 	char *file;
21493910Sdougm 	char *other;
21503910Sdougm 	int compare;
21513910Sdougm #define	OPT_CMP_GE	0
21523910Sdougm #define	OPT_CMP_LE	1
21533910Sdougm 	int (*check)(char *);
21543910Sdougm } proto_options[] = {
21553910Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
21563910Sdougm 	{"nfsd_servers",
21573910Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
21583910Sdougm 	    1, INT32_MAX, NFSADMIN},
21593910Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
21603910Sdougm 	{"lockd_listen_backlog",
21613910Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
21623910Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
21633910Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
21643910Sdougm 	{"lockd_servers",
21653910Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
21663910Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
21673910Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
21683910Sdougm 	{"lockd_retransmit_timeout",
21693910Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
21703910Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
21713910Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
21723910Sdougm 	{"grace_period",
21733910Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
21743910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
21753910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
21763910Sdougm 	{"nfs_server_versmin",
21773910Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
21783910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
21793910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
21803910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
21813910Sdougm 	{"nfs_server_versmax",
21823910Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
21833910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
21843910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
21853910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
21863910Sdougm 	{"nfs_client_versmin",
21873910Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
21883910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
21893910Sdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
21903910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
21913910Sdougm 	{"nfs_client_versmax",
21923910Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
21933910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
21943910Sdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
21953910Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
21963910Sdougm 	{"nfs_server_delegation",
21973910Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
21983910Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
21993910Sdougm 	    NFSADMIN},
22003910Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
22013910Sdougm 	{"nfsmapid_domain",
22023910Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
22033910Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
22043910Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
22053910Sdougm 	{"nfsd_max_connections",
22063910Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
22073910Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
22083910Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
22093910Sdougm 	{"nfsd_protocol",
22103910Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
22113910Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
22123910Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
22133910Sdougm 	{"nfsd_listen_backlog",
22143910Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
22153910Sdougm 	    OPT_TYPE_NUMBER, 0,
22163910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
22173910Sdougm 	{NULL}
22183910Sdougm };
22193910Sdougm 
22203910Sdougm /*
22213910Sdougm  * the protoset holds the defined options so we don't have to read
22223910Sdougm  * them multiple times
22233910Sdougm  */
2224*5179Sdougm static sa_protocol_properties_t protoset;
22253910Sdougm 
22263910Sdougm static int
22273910Sdougm findprotoopt(char *name, int whichname)
22283910Sdougm {
22293910Sdougm 	int i;
22303910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
22314345Sdougm 		if (whichname == 1) {
22324345Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
22333910Sdougm 			return (i);
22344345Sdougm 		} else {
22354345Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
22364345Sdougm 				return (i);
22374345Sdougm 		}
22383910Sdougm 	}
22393910Sdougm 	return (-1);
22403910Sdougm }
22413910Sdougm 
22423910Sdougm /*
22433910Sdougm  * fixcaselower(str)
22443910Sdougm  *
22453910Sdougm  * convert a string to lower case (inplace).
22463910Sdougm  */
22473910Sdougm 
22483910Sdougm static void
22493910Sdougm fixcaselower(char *str)
22503910Sdougm {
22513910Sdougm 	while (*str) {
22524345Sdougm 		*str = tolower(*str);
22534345Sdougm 		str++;
22543910Sdougm 	}
22553910Sdougm }
22563910Sdougm 
22573910Sdougm /*
22583910Sdougm  * fixcaseupper(str)
22593910Sdougm  *
22603910Sdougm  * convert a string to upper case (inplace).
22613910Sdougm  */
22623910Sdougm 
22633910Sdougm static void
22643910Sdougm fixcaseupper(char *str)
22653910Sdougm {
22663910Sdougm 	while (*str) {
22674345Sdougm 		*str = toupper(*str);
22684345Sdougm 		str++;
22693910Sdougm 	}
22703910Sdougm }
22713910Sdougm 
22723910Sdougm /*
22734241Sdougm  * skipwhitespace(str)
22744241Sdougm  *
22754241Sdougm  * Skip leading white space. It is assumed that it is called with a
22764241Sdougm  * valid pointer.
22774241Sdougm  */
22784241Sdougm 
22794241Sdougm static char *
22804241Sdougm skipwhitespace(char *str)
22814241Sdougm {
22824241Sdougm 	while (*str && isspace(*str))
22834241Sdougm 		str++;
22844241Sdougm 
22854241Sdougm 	return (str);
22864241Sdougm }
22874241Sdougm 
22884241Sdougm /*
22894345Sdougm  * extractprop()
22904345Sdougm  *
22914345Sdougm  * Extract the property and value out of the line and create the
22924345Sdougm  * property in the optionset.
22934345Sdougm  */
22944345Sdougm static void
22954345Sdougm extractprop(char *name, char *value)
22964345Sdougm {
22974345Sdougm 	sa_property_t prop;
22984345Sdougm 	int index;
22994345Sdougm 	/*
23004345Sdougm 	 * Remove any leading
23014345Sdougm 	 * white space.
23024345Sdougm 	 */
23034345Sdougm 	name = skipwhitespace(name);
23044345Sdougm 
23054345Sdougm 	index = findprotoopt(name, 0);
23064345Sdougm 	if (index >= 0) {
23074345Sdougm 		fixcaselower(name);
23084345Sdougm 		prop = sa_create_property(proto_options[index].name, value);
23094345Sdougm 		if (prop != NULL)
23104345Sdougm 			(void) sa_add_protocol_property(protoset, prop);
23114345Sdougm 	}
23124345Sdougm }
23134345Sdougm 
23144345Sdougm /*
23153910Sdougm  * initprotofromdefault()
23163910Sdougm  *
23173910Sdougm  * read the default file(s) and add the defined values to the
23183910Sdougm  * protoset.  Note that default values are known from the built in
23193910Sdougm  * table in case the file doesn't have a definition.
23203910Sdougm  */
23213910Sdougm 
23223910Sdougm static int
23233910Sdougm initprotofromdefault()
23243910Sdougm {
23253910Sdougm 	FILE *nfs;
23263910Sdougm 	char buff[BUFSIZ];
23273910Sdougm 	char *name;
23283910Sdougm 	char *value;
23293910Sdougm 
23303910Sdougm 	protoset = sa_create_protocol_properties("nfs");
23313910Sdougm 
23323910Sdougm 	if (protoset != NULL) {
23334345Sdougm 		nfs = fopen(NFSADMIN, "r");
23344345Sdougm 		if (nfs != NULL) {
23354345Sdougm 			while (fgets(buff, sizeof (buff), nfs) != NULL) {
23364345Sdougm 				switch (buff[0]) {
23374345Sdougm 				case '\n':
23384345Sdougm 				case '#':
23394345Sdougm 					/* skip */
23404345Sdougm 					break;
23414345Sdougm 				default:
23424345Sdougm 					name = buff;
23434345Sdougm 					buff[strlen(buff) - 1] = '\0';
23444345Sdougm 					value = strchr(name, '=');
23454345Sdougm 					if (value != NULL) {
23464345Sdougm 						*value++ = '\0';
23474345Sdougm 						extractprop(name, value);
23484345Sdougm 					}
23494345Sdougm 				}
23503910Sdougm 			}
23514345Sdougm 			if (nfs != NULL)
23524345Sdougm 				(void) fclose(nfs);
23533910Sdougm 		}
23543910Sdougm 	}
23553910Sdougm 	if (protoset == NULL)
23564345Sdougm 		return (SA_NO_MEMORY);
23573910Sdougm 	return (SA_OK);
23583910Sdougm }
23593910Sdougm 
23603910Sdougm /*
23614345Sdougm  * add_defaults()
23623910Sdougm  *
23633910Sdougm  * Add the default values for any property not defined in the parsing
23643910Sdougm  * of the default files. Values are set according to their defined
23653910Sdougm  * types.
23663910Sdougm  */
23673910Sdougm 
23683910Sdougm static void
23693910Sdougm add_defaults()
23703910Sdougm {
23713910Sdougm 	int i;
23723910Sdougm 	char number[MAXDIGITS];
23733910Sdougm 
23743910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
23754345Sdougm 		sa_property_t prop;
23764345Sdougm 		prop = sa_get_protocol_property(protoset,
23774345Sdougm 		    proto_options[i].name);
23784345Sdougm 		if (prop == NULL) {
23794345Sdougm 			/* add the default value */
23804345Sdougm 			switch (proto_options[i].type) {
23814345Sdougm 			case OPT_TYPE_NUMBER:
23824345Sdougm 				(void) snprintf(number, sizeof (number), "%d",
23834345Sdougm 				    proto_options[i].defvalue.intval);
23844345Sdougm 				prop = sa_create_property(proto_options[i].name,
23854345Sdougm 				    number);
23864345Sdougm 				break;
23873910Sdougm 
23884345Sdougm 			case OPT_TYPE_BOOLEAN:
23894345Sdougm 				prop = sa_create_property(proto_options[i].name,
23904345Sdougm 				    proto_options[i].defvalue.intval ?
23914345Sdougm 				    "true" : "false");
23924345Sdougm 				break;
23933910Sdougm 
23944345Sdougm 			case OPT_TYPE_ONOFF:
23954345Sdougm 				prop = sa_create_property(proto_options[i].name,
23964345Sdougm 				    proto_options[i].defvalue.intval ?
23974345Sdougm 				    "on" : "off");
23984345Sdougm 				break;
23993910Sdougm 
24004345Sdougm 			default:
24014345Sdougm 				/* treat as strings of zero length */
24024345Sdougm 				prop = sa_create_property(proto_options[i].name,
24034345Sdougm 				    "");
24044345Sdougm 				break;
24054345Sdougm 			}
24064345Sdougm 			if (prop != NULL)
24074345Sdougm 				(void) sa_add_protocol_property(protoset, prop);
24083910Sdougm 		}
24093910Sdougm 	}
24103910Sdougm }
24113910Sdougm 
24123910Sdougm static void
24133910Sdougm free_protoprops()
24143910Sdougm {
2415*5179Sdougm 	if (protoset != NULL) {
2416*5179Sdougm 		xmlFreeNode(protoset);
2417*5179Sdougm 		protoset = NULL;
2418*5179Sdougm 	}
24193910Sdougm }
24203910Sdougm 
24213910Sdougm /*
24223910Sdougm  * nfs_init()
24233910Sdougm  *
24243910Sdougm  * Initialize the NFS plugin.
24253910Sdougm  */
24263910Sdougm 
24273910Sdougm static int
24283910Sdougm nfs_init()
24293910Sdougm {
24303910Sdougm 	int ret = SA_OK;
24313910Sdougm 
24323910Sdougm 	if (sa_plugin_ops.sa_init != nfs_init)
24334345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
24344345Sdougm 		    "NFS plugin not properly initialized\n"));
24353910Sdougm 
24363910Sdougm 	ret = initprotofromdefault();
24374345Sdougm 	if (ret == SA_OK)
24384345Sdougm 		add_defaults();
24393910Sdougm 
24403910Sdougm 	return (ret);
24413910Sdougm }
24423910Sdougm 
24433910Sdougm /*
24443910Sdougm  * nfs_fini()
24453910Sdougm  *
24463910Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
24473910Sdougm  */
24483910Sdougm 
24493910Sdougm static void
24503910Sdougm nfs_fini()
24513910Sdougm {
24523910Sdougm 	free_protoprops();
24533910Sdougm }
24543910Sdougm 
24553910Sdougm /*
24563910Sdougm  * nfs_get_proto_set()
24573910Sdougm  *
24583910Sdougm  * Return an optionset with all the protocol specific properties in
24593910Sdougm  * it.
24603910Sdougm  */
24613910Sdougm 
24623910Sdougm static sa_protocol_properties_t
24633910Sdougm nfs_get_proto_set()
24643910Sdougm {
24653910Sdougm 	return (protoset);
24663910Sdougm }
24673910Sdougm 
24683910Sdougm struct deffile {
24693910Sdougm 	struct deffile *next;
24703910Sdougm 	char *line;
24713910Sdougm };
24723910Sdougm 
24733910Sdougm /*
24743910Sdougm  * read_default_file(fname)
24753910Sdougm  *
24763910Sdougm  * Read the specified default file. We return a list of entries. This
24773910Sdougm  * get used for adding or removing values.
24783910Sdougm  */
24793910Sdougm 
24803910Sdougm static struct deffile *
24813910Sdougm read_default_file(char *fname)
24823910Sdougm {
24833910Sdougm 	FILE *file;
24843910Sdougm 	struct deffile *defs = NULL;
24853910Sdougm 	struct deffile *newdef;
24863910Sdougm 	struct deffile *prevdef = NULL;
24873910Sdougm 	char buff[BUFSIZ * 2];
24883910Sdougm 
24893910Sdougm 	file = fopen(fname, "r");
24903910Sdougm 	if (file != NULL) {
24914345Sdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
24924345Sdougm 			newdef = (struct deffile *)calloc(1,
24934345Sdougm 			    sizeof (struct deffile));
24944345Sdougm 			if (newdef != NULL) {
24954345Sdougm 				/* Make sure we skip any leading whitespace. */
24964345Sdougm 				newdef->line = strdup(skipwhitespace(buff));
24974345Sdougm 				if (defs == NULL) {
24984345Sdougm 					prevdef = defs = newdef;
24994345Sdougm 				} else {
25004345Sdougm 					prevdef->next = newdef;
25014345Sdougm 					prevdef = newdef;
25024345Sdougm 				}
25034345Sdougm 			}
25043910Sdougm 		}
25053910Sdougm 	}
25063910Sdougm 	(void) fclose(file);
25073910Sdougm 	return (defs);
25083910Sdougm }
25093910Sdougm 
25103910Sdougm static void
25113910Sdougm free_default_file(struct deffile *defs)
25123910Sdougm {
25133910Sdougm 	struct deffile *curdefs = NULL;
25143910Sdougm 
25153910Sdougm 	while (defs != NULL) {
25164345Sdougm 		curdefs = defs;
25174345Sdougm 		defs = defs->next;
25184345Sdougm 		if (curdefs->line != NULL)
25194345Sdougm 			free(curdefs->line);
25204345Sdougm 		free(curdefs);
25213910Sdougm 	}
25223910Sdougm }
25233910Sdougm 
25243910Sdougm /*
25253910Sdougm  * write_default_file(fname, defs)
25263910Sdougm  *
25273910Sdougm  * Write the default file back.
25283910Sdougm  */
25293910Sdougm 
25303910Sdougm static int
25313910Sdougm write_default_file(char *fname, struct deffile *defs)
25323910Sdougm {
25333910Sdougm 	FILE *file;
25343910Sdougm 	int ret = SA_OK;
25353910Sdougm 	sigset_t old, new;
25363910Sdougm 
25373910Sdougm 	file = fopen(fname, "w+");
25383910Sdougm 	if (file != NULL) {
25394345Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
25404345Sdougm 		(void) sigaddset(&new, SIGHUP);
25414345Sdougm 		(void) sigaddset(&new, SIGINT);
25424345Sdougm 		(void) sigaddset(&new, SIGQUIT);
25434345Sdougm 		(void) sigaddset(&new, SIGTSTP);
25444345Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
25454345Sdougm 		while (defs != NULL) {
25464345Sdougm 			(void) fputs(defs->line, file);
25474345Sdougm 			defs = defs->next;
25484345Sdougm 		}
25494345Sdougm 		(void) fsync(fileno(file));
25504345Sdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
25514345Sdougm 		(void) fclose(file);
25523910Sdougm 	} else {
25534345Sdougm 		switch (errno) {
25544345Sdougm 		case EPERM:
25554345Sdougm 		case EACCES:
25564345Sdougm 			ret = SA_NO_PERMISSION;
25574345Sdougm 			break;
25584345Sdougm 		default:
25594345Sdougm 			ret = SA_SYSTEM_ERR;
25604345Sdougm 		}
25613910Sdougm 	}
25623910Sdougm 	return (ret);
25633910Sdougm }
25643910Sdougm 
25653910Sdougm 
25663910Sdougm /*
25673910Sdougm  * set_default_file_value(tag, value)
25683910Sdougm  *
25693910Sdougm  * Set the default file value for tag to value. Then rewrite the file.
25703910Sdougm  * tag and value are always set.  The caller must ensure this.
25713910Sdougm  */
25723910Sdougm 
25733910Sdougm #define	MAX_STRING_LENGTH	256
25743910Sdougm static int
25753910Sdougm set_default_file_value(char *tag, char *value)
25763910Sdougm {
25773910Sdougm 	int ret = SA_OK;
25783910Sdougm 	struct deffile *root;
25793910Sdougm 	struct deffile *defs;
25803910Sdougm 	struct deffile *prev;
25813910Sdougm 	char string[MAX_STRING_LENGTH];
25823910Sdougm 	int len;
25833910Sdougm 	int update = 0;
25843910Sdougm 
25853910Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
25863910Sdougm 	len = strlen(string);
25873910Sdougm 
25883910Sdougm 	root = defs = read_default_file(NFSADMIN);
25893910Sdougm 	if (root == NULL) {
25904345Sdougm 		if (errno == EPERM || errno == EACCES)
25914345Sdougm 			ret = SA_NO_PERMISSION;
25924345Sdougm 		else
25934345Sdougm 			ret = SA_SYSTEM_ERR;
25943910Sdougm 	} else {
25953910Sdougm 		while (defs != NULL) {
25964345Sdougm 			if (defs->line != NULL &&
25974345Sdougm 			    strncasecmp(defs->line, string, len) == 0) {
25984345Sdougm 				/* replace with the new value */
25994345Sdougm 				free(defs->line);
26004345Sdougm 				fixcaseupper(tag);
26014345Sdougm 				(void) snprintf(string, sizeof (string),
26023910Sdougm 				    "%s=%s\n", tag, value);
26034345Sdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
26044345Sdougm 				defs->line = strdup(string);
26054345Sdougm 				update = 1;
26064345Sdougm 				break;
26074345Sdougm 			}
26084345Sdougm 			defs = defs->next;
26093910Sdougm 		}
26104345Sdougm 		if (!update) {
26114345Sdougm 			defs = root;
26124345Sdougm 			/* didn't find, so see if it is a comment */
26134345Sdougm 			(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
26144345Sdougm 			len = strlen(string);
26154345Sdougm 			while (defs != NULL) {
26164345Sdougm 				if (strncasecmp(defs->line, string, len) == 0) {
26174345Sdougm 					/* replace with the new value */
26184345Sdougm 					free(defs->line);
26194345Sdougm 					fixcaseupper(tag);
26204345Sdougm 					(void) snprintf(string, sizeof (string),
26214345Sdougm 					    "%s=%s\n", tag, value);
26224345Sdougm 					string[MAX_STRING_LENGTH - 1] = '\0';
26234345Sdougm 					defs->line = strdup(string);
26244345Sdougm 					update = 1;
26254345Sdougm 					break;
26264345Sdougm 				}
26274345Sdougm 				defs = defs->next;
26284345Sdougm 			}
26293910Sdougm 		}
26304345Sdougm 		if (!update) {
26314345Sdougm 			fixcaseupper(tag);
26324345Sdougm 			(void) snprintf(string, sizeof (string), "%s=%s\n",
26334345Sdougm 			    tag, value);
26344345Sdougm 			prev = root;
26354345Sdougm 			while (prev->next != NULL)
26364345Sdougm 				prev = prev->next;
26374345Sdougm 			defs = malloc(sizeof (struct deffile));
26384345Sdougm 			prev->next = defs;
26394345Sdougm 			if (defs != NULL) {
26404345Sdougm 				defs->next = NULL;
26414345Sdougm 				defs->line = strdup(string);
26424345Sdougm 			}
26434345Sdougm 		}
26444345Sdougm 		if (update) {
26454345Sdougm 			ret = write_default_file(NFSADMIN, root);
26464345Sdougm 		}
26474345Sdougm 		free_default_file(root);
26483910Sdougm 	}
26493910Sdougm 	return (ret);
26503910Sdougm }
26513910Sdougm 
26523910Sdougm /*
26533910Sdougm  * service_in_state(service, chkstate)
26543910Sdougm  *
26553910Sdougm  * Want to know if the specified service is in the desired state
26563910Sdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
26573910Sdougm  * isn't.
26583910Sdougm  */
26593910Sdougm static int
26603910Sdougm service_in_state(char *service, const char *chkstate)
26613910Sdougm {
26623910Sdougm 	char *state;
26633910Sdougm 	int ret = B_FALSE;
26643910Sdougm 
26653910Sdougm 	state = smf_get_state(service);
26663910Sdougm 	if (state != NULL) {
26674345Sdougm 		/* got the state so get the equality for the return value */
26684345Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
26694345Sdougm 		free(state);
26703910Sdougm 	}
26713910Sdougm 	return (ret);
26723910Sdougm }
26733910Sdougm 
26743910Sdougm /*
26753910Sdougm  * restart_service(svcs)
26763910Sdougm  *
26773910Sdougm  * Walk through the bit mask of services that need to be restarted in
26783910Sdougm  * order to use the new property values. Some properties affect
26793910Sdougm  * multiple daemons. Should only restart a service if it is currently
26803910Sdougm  * enabled (online).
26813910Sdougm  */
26823910Sdougm 
26833910Sdougm static void
26843910Sdougm restart_service(uint32_t svcs)
26853910Sdougm {
26863910Sdougm 	uint32_t mask;
26873910Sdougm 	int ret;
26883910Sdougm 	char *service;
26893910Sdougm 
26903910Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
26914345Sdougm 		switch (svcs & mask) {
26924345Sdougm 		case SVC_LOCKD:
26934345Sdougm 			service = LOCKD;
26944345Sdougm 			break;
26954345Sdougm 		case SVC_STATD:
26964345Sdougm 			service = STATD;
26974345Sdougm 			break;
26984345Sdougm 		case SVC_NFSD:
26994345Sdougm 			service = NFSD;
27004345Sdougm 			break;
27014345Sdougm 		case SVC_MOUNTD:
27024345Sdougm 			service = MOUNTD;
27034345Sdougm 			break;
27044345Sdougm 		case SVC_NFS4CBD:
27054345Sdougm 			service = NFS4CBD;
27064345Sdougm 			break;
27074345Sdougm 		case SVC_NFSMAPID:
27084345Sdougm 			service = NFSMAPID;
27094345Sdougm 			break;
27104345Sdougm 		case SVC_RQUOTAD:
27114345Sdougm 			service = RQUOTAD;
27124345Sdougm 			break;
27134345Sdougm 		case SVC_NFSLOGD:
27144345Sdougm 			service = NFSLOGD;
27154345Sdougm 			break;
27164345Sdougm 		default:
27174345Sdougm 			continue;
27184345Sdougm 		}
27193910Sdougm 
27203910Sdougm 		/*
27213910Sdougm 		 * Only attempt to restart the service if it is
27223910Sdougm 		 * currently running. In the future, it may be
27233910Sdougm 		 * desirable to use smf_refresh_instance if the NFS
27243910Sdougm 		 * services ever implement the refresh method.
27253910Sdougm 		 */
27264345Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
27274345Sdougm 			ret = smf_restart_instance(service);
27283910Sdougm 			/*
27294345Sdougm 			 * There are only a few SMF errors at this point, but
27304345Sdougm 			 * it is also possible that a bad value may have put
27314345Sdougm 			 * the service into maintenance if there wasn't an
27324345Sdougm 			 * SMF level error.
27333910Sdougm 			 */
27344345Sdougm 			if (ret != 0) {
27354345Sdougm 				(void) fprintf(stderr,
27364345Sdougm 				    dgettext(TEXT_DOMAIN,
27374345Sdougm 				    "%s failed to restart: %s\n"),
27384345Sdougm 				    scf_strerror(scf_error()));
27394345Sdougm 			} else {
27404345Sdougm 				/*
27414345Sdougm 				 * Check whether it has gone to "maintenance"
27424345Sdougm 				 * mode or not. Maintenance implies something
27434345Sdougm 				 * went wrong.
27444345Sdougm 				 */
27454345Sdougm 				if (service_in_state(service,
27464345Sdougm 				    SCF_STATE_STRING_MAINT)) {
27474345Sdougm 					(void) fprintf(stderr,
27484345Sdougm 					    dgettext(TEXT_DOMAIN,
27494345Sdougm 					    "%s failed to restart\n"),
27504345Sdougm 					    service);
27514345Sdougm 				}
27524345Sdougm 			}
27533910Sdougm 		}
27544345Sdougm 		svcs &= ~mask;
27553910Sdougm 	}
27563910Sdougm }
27573910Sdougm 
27583910Sdougm /*
27593910Sdougm  * nfs_minmax_check(name, value)
27603910Sdougm  *
27613910Sdougm  * Verify that the value for the property specified by index is valid
27623910Sdougm  * relative to the opposite value in the case of a min/max variable.
27633910Sdougm  * Currently, server_minvers/server_maxvers and
27643910Sdougm  * client_minvers/client_maxvers are the only ones to check.
27653910Sdougm  */
27663910Sdougm 
27673910Sdougm static int
27683910Sdougm nfs_minmax_check(int index, int value)
27693910Sdougm {
27703910Sdougm 	int val;
27713910Sdougm 	char *pval;
27723910Sdougm 	sa_property_t prop;
27733910Sdougm 	sa_optionset_t opts;
27743910Sdougm 	int ret = B_TRUE;
27753910Sdougm 
27763910Sdougm 	if (proto_options[index].other != NULL) {
27774345Sdougm 		/* have a property to compare against */
27784345Sdougm 		opts = nfs_get_proto_set();
27794345Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
27803910Sdougm 		/*
27813910Sdougm 		 * If we don't find the property, assume default
27823910Sdougm 		 * values which will work since the max will be at the
27833910Sdougm 		 * max and the min at the min.
27843910Sdougm 		 */
27854345Sdougm 		if (prop != NULL) {
27864345Sdougm 			pval = sa_get_property_attr(prop, "value");
27874345Sdougm 			if (pval != NULL) {
27884345Sdougm 				val = strtoul(pval, NULL, 0);
27894345Sdougm 				if (proto_options[index].compare ==
27904345Sdougm 				    OPT_CMP_LE) {
27914345Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
27924345Sdougm 				} else if (proto_options[index].compare ==
27934345Sdougm 				    OPT_CMP_GE) {
27944345Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
27954345Sdougm 				}
27964345Sdougm 			}
27973910Sdougm 		}
27983910Sdougm 	}
27993910Sdougm 	return (ret);
28003910Sdougm }
28013910Sdougm 
28023910Sdougm /*
28033910Sdougm  * nfs_validate_proto_prop(index, name, value)
28043910Sdougm  *
28053910Sdougm  * Verify that the property specifed by name can take the new
28063910Sdougm  * value. This is a sanity check to prevent bad values getting into
28073910Sdougm  * the default files. All values need to be checked against what is
28083910Sdougm  * allowed by their defined type. If a type isn't explicitly defined
28093910Sdougm  * here, it is treated as a string.
28103910Sdougm  *
28113910Sdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
28123910Sdougm  * within the range specified and potentially against another property
28133910Sdougm  * value as well as specified in the proto_options members other and
28143910Sdougm  * compare.
28153910Sdougm  */
28163910Sdougm 
28173910Sdougm static int
28183910Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
28193910Sdougm {
28203910Sdougm 	int ret = SA_OK;
28213910Sdougm 	char *cp;
28223910Sdougm #ifdef lint
28233910Sdougm 	name = name;
28243910Sdougm #endif
28253910Sdougm 
28263910Sdougm 	switch (proto_options[index].type) {
28273910Sdougm 	case OPT_TYPE_NUMBER:
28284345Sdougm 		if (!is_a_number(value))
28294345Sdougm 			ret = SA_BAD_VALUE;
28304345Sdougm 		else {
28314345Sdougm 			int val;
28324345Sdougm 			val = strtoul(value, NULL, 0);
28334345Sdougm 			if (val < proto_options[index].minval ||
28344345Sdougm 			    val > proto_options[index].maxval)
28354345Sdougm 				ret = SA_BAD_VALUE;
28364345Sdougm 			/*
28374345Sdougm 			 * For server_versmin/server_versmax and
28384345Sdougm 			 * client_versmin/client_versmax, the value of the
28394345Sdougm 			 * min(max) should be checked to be correct relative
28404345Sdougm 			 * to the current max(min).
28414345Sdougm 			 */
28424345Sdougm 			if (!nfs_minmax_check(index, val)) {
28434345Sdougm 				ret = SA_BAD_VALUE;
28444345Sdougm 			}
28453910Sdougm 		}
28464345Sdougm 		break;
28473910Sdougm 
28483910Sdougm 	case OPT_TYPE_DOMAIN:
28493910Sdougm 		/*
28503910Sdougm 		 * needs to be a qualified domain so will have at
28513910Sdougm 		 * least one period and other characters on either
28523910Sdougm 		 * side of it.  A zero length string is also allowed
28533910Sdougm 		 * and is the way to turn off the override.
28543910Sdougm 		 */
28554345Sdougm 		if (strlen(value) == 0)
28564345Sdougm 			break;
28574345Sdougm 		cp = strchr(value, '.');
28584345Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
28594345Sdougm 			ret = SA_BAD_VALUE;
28603910Sdougm 		break;
28613910Sdougm 
28623910Sdougm 	case OPT_TYPE_BOOLEAN:
28634345Sdougm 		if (strlen(value) == 0 ||
28644345Sdougm 		    strcasecmp(value, "true") == 0 ||
28654345Sdougm 		    strcmp(value, "1") == 0 ||
28664345Sdougm 		    strcasecmp(value, "false") == 0 ||
28674345Sdougm 		    strcmp(value, "0") == 0) {
28684345Sdougm 			ret = SA_OK;
28694345Sdougm 		} else {
28704345Sdougm 			ret = SA_BAD_VALUE;
28714345Sdougm 		}
28724345Sdougm 		break;
28733910Sdougm 
28743910Sdougm 	case OPT_TYPE_ONOFF:
28754345Sdougm 		if (strcasecmp(value, "on") != 0 &&
28764345Sdougm 		    strcasecmp(value, "off") != 0) {
28774345Sdougm 			ret = SA_BAD_VALUE;
28784345Sdougm 		}
28794345Sdougm 		break;
28803910Sdougm 
28813910Sdougm 	case OPT_TYPE_PROTOCOL:
28824345Sdougm 		if (strcasecmp(value, "all") != 0 &&
28834345Sdougm 		    strcasecmp(value, "tcp") != 0 &&
28844345Sdougm 		    strcasecmp(value, "udp") != 0)
28854345Sdougm 			ret = SA_BAD_VALUE;
28864345Sdougm 		break;
28873910Sdougm 
28883910Sdougm 	default:
28894345Sdougm 		/* treat as a string */
28904345Sdougm 		break;
28913910Sdougm 	}
28923910Sdougm 	return (ret);
28933910Sdougm }
28943910Sdougm 
28953910Sdougm /*
28963910Sdougm  * nfs_set_proto_prop(prop)
28973910Sdougm  *
28983910Sdougm  * check that prop is valid.
28993910Sdougm  */
29003910Sdougm 
29013910Sdougm static int
29023910Sdougm nfs_set_proto_prop(sa_property_t prop)
29033910Sdougm {
29043910Sdougm 	int ret = SA_OK;
29053910Sdougm 	char *name;
29063910Sdougm 	char *value;
29073910Sdougm 
29083910Sdougm 	name = sa_get_property_attr(prop, "type");
29093910Sdougm 	value = sa_get_property_attr(prop, "value");
29103910Sdougm 	if (name != NULL && value != NULL) {
29114345Sdougm 		int index = findprotoopt(name, 1);
29124345Sdougm 		if (index >= 0) {
29134345Sdougm 			/* should test for valid value */
29144345Sdougm 			ret = nfs_validate_proto_prop(index, name, value);
29154345Sdougm 			if (ret == SA_OK)
29164345Sdougm 				ret = set_default_file_value(
29174345Sdougm 				    proto_options[index].tag, value);
29184345Sdougm 			if (ret == SA_OK)
29194345Sdougm 				restart_service(proto_options[index].svcs);
29204345Sdougm 		}
29213910Sdougm 	}
29223910Sdougm 	if (name != NULL)
29234345Sdougm 		sa_free_attr_string(name);
29243910Sdougm 	if (value != NULL)
29254345Sdougm 		sa_free_attr_string(value);
29263910Sdougm 	return (ret);
29273910Sdougm }
29283910Sdougm 
29293910Sdougm /*
29303910Sdougm  * nfs_get_status()
29313910Sdougm  *
29323910Sdougm  * What is the current status of the nfsd? We use the SMF state here.
29333910Sdougm  * Caller must free the returned value.
29343910Sdougm  */
29353910Sdougm 
29363910Sdougm static char *
29373910Sdougm nfs_get_status()
29383910Sdougm {
29393910Sdougm 	char *state;
29403910Sdougm 	state = smf_get_state(NFSD);
29413910Sdougm 	return (state != NULL ? state : strdup("-"));
29423910Sdougm }
29433910Sdougm 
29443910Sdougm /*
29453910Sdougm  * nfs_space_alias(alias)
29463910Sdougm  *
29473910Sdougm  * Lookup the space (security) name. If it is default, convert to the
29483910Sdougm  * real name.
29493910Sdougm  */
29503910Sdougm 
29513910Sdougm static char *
29523910Sdougm nfs_space_alias(char *space)
29533910Sdougm {
29543910Sdougm 	char *name = space;
29553910Sdougm 	seconfig_t secconf;
29563910Sdougm 
29573910Sdougm 	/*
29583910Sdougm 	 * Only the space named "default" is special. If it is used,
29593910Sdougm 	 * the default needs to be looked up and the real name used.
29603910Sdougm 	 * This is normally "sys" but could be changed.  We always
29613910Sdougm 	 * change defautl to the real name.
29623910Sdougm 	 */
29633910Sdougm 	if (strcmp(space, "default") == 0 &&
29643910Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
29654345Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
29664345Sdougm 			name = secconf.sc_name;
29673910Sdougm 	}
29683910Sdougm 	return (strdup(name));
29693910Sdougm }
2970