16185db85Sdougm /*
26185db85Sdougm  * CDDL HEADER START
36185db85Sdougm  *
46185db85Sdougm  * The contents of this file are subject to the terms of the
56185db85Sdougm  * Common Development and Distribution License (the "License").
66185db85Sdougm  * You may not use this file except in compliance with the License.
76185db85Sdougm  *
86185db85Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96185db85Sdougm  * or http://www.opensolaris.org/os/licensing.
106185db85Sdougm  * See the License for the specific language governing permissions
116185db85Sdougm  * and limitations under the License.
126185db85Sdougm  *
136185db85Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
146185db85Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156185db85Sdougm  * If applicable, add the following below this CDDL HEADER, with the
166185db85Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
176185db85Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
186185db85Sdougm  *
196185db85Sdougm  * CDDL HEADER END
206185db85Sdougm  */
216185db85Sdougm 
226185db85Sdougm /*
233472f5dcSdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246185db85Sdougm  * Use is subject to license terms.
256185db85Sdougm  */
266185db85Sdougm 
276185db85Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
286185db85Sdougm 
296185db85Sdougm /*
306185db85Sdougm  * NFS specific functions
316185db85Sdougm  */
326185db85Sdougm #include <stdio.h>
336185db85Sdougm #include <string.h>
346185db85Sdougm #include <ctype.h>
356185db85Sdougm #include <stdlib.h>
366185db85Sdougm #include <unistd.h>
376185db85Sdougm #include <zone.h>
386185db85Sdougm #include <errno.h>
396185db85Sdougm #include <locale.h>
406185db85Sdougm #include <signal.h>
416185db85Sdougm #include "libshare.h"
426185db85Sdougm #include "libshare_impl.h"
436185db85Sdougm #include <nfs/export.h>
446185db85Sdougm #include <pwd.h>
456185db85Sdougm #include <limits.h>
466185db85Sdougm #include <libscf.h>
476185db85Sdougm #include "nfslog_config.h"
486185db85Sdougm #include "nfslogtab.h"
496185db85Sdougm #include "libshare_nfs.h"
506185db85Sdougm #include <rpcsvc/daemon_utils.h>
516185db85Sdougm #include <nfs/nfs.h>
526185db85Sdougm 
536185db85Sdougm /* should really be in some global place */
546185db85Sdougm #define	DEF_WIN	30000
556185db85Sdougm #define	OPT_CHUNK	1024
566185db85Sdougm 
576185db85Sdougm int debug = 0;
586185db85Sdougm 
596185db85Sdougm 
606185db85Sdougm /* internal functions */
616185db85Sdougm static int nfs_init();
626185db85Sdougm static void nfs_fini();
636185db85Sdougm static int nfs_enable_share(sa_share_t);
646185db85Sdougm static int nfs_disable_share(char *);
656185db85Sdougm static int nfs_validate_property(sa_property_t, sa_optionset_t);
666185db85Sdougm static int nfs_validate_security_mode(char *);
676185db85Sdougm static int nfs_is_security_opt(char *);
686185db85Sdougm static int nfs_parse_legacy_options(sa_group_t, char *);
696185db85Sdougm static char *nfs_format_options(sa_group_t, int);
706185db85Sdougm static int nfs_set_proto_prop(sa_property_t);
716185db85Sdougm static sa_protocol_properties_t nfs_get_proto_set();
726185db85Sdougm static char *nfs_get_status();
736185db85Sdougm static char *nfs_space_alias(char *);
746185db85Sdougm 
756185db85Sdougm /*
766185db85Sdougm  * ops vector that provides the protocol specific info and operations
776185db85Sdougm  * for share management.
786185db85Sdougm  */
796185db85Sdougm 
806185db85Sdougm struct sa_plugin_ops sa_plugin_ops = {
816185db85Sdougm 	SA_PLUGIN_VERSION,
826185db85Sdougm 	"nfs",
836185db85Sdougm 	nfs_init,
846185db85Sdougm 	nfs_fini,
856185db85Sdougm 	nfs_enable_share,
866185db85Sdougm 	nfs_disable_share,
876185db85Sdougm 	nfs_validate_property,
886185db85Sdougm 	nfs_validate_security_mode,
896185db85Sdougm 	nfs_is_security_opt,
906185db85Sdougm 	nfs_parse_legacy_options,
916185db85Sdougm 	nfs_format_options,
926185db85Sdougm 	nfs_set_proto_prop,
936185db85Sdougm 	nfs_get_proto_set,
946185db85Sdougm 	nfs_get_status,
956185db85Sdougm 	nfs_space_alias,
966185db85Sdougm 	NULL,
976185db85Sdougm 	NULL
986185db85Sdougm };
996185db85Sdougm 
1006185db85Sdougm /*
1016185db85Sdougm  * list of support services needed
1026185db85Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
1036185db85Sdougm  */
1046185db85Sdougm 
1056185db85Sdougm static char *service_list_default[] =
1066185db85Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL };
1076185db85Sdougm static char *service_list_logging[] =
1086185db85Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL };
1096185db85Sdougm 
1106185db85Sdougm /*
1116185db85Sdougm  * option definitions.  Make sure to keep the #define for the option
1126185db85Sdougm  * index just before the entry it is the index for. Changing the order
1136185db85Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1146185db85Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1156185db85Sdougm  */
1166185db85Sdougm 
1176185db85Sdougm struct option_defs optdefs[] = {
1186185db85Sdougm #define	OPT_RO		0
1196185db85Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1206185db85Sdougm #define	OPT_RW		1
1216185db85Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1226185db85Sdougm #define	OPT_ROOT	2
1236185db85Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1246185db85Sdougm #define	OPT_SECURE	3
1256185db85Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1266185db85Sdougm #define	OPT_ANON	4
1276185db85Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1286185db85Sdougm #define	OPT_WINDOW	5
1296185db85Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1306185db85Sdougm #define	OPT_NOSUID	6
1316185db85Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1326185db85Sdougm #define	OPT_ACLOK	7
1336185db85Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1346185db85Sdougm #define	OPT_NOSUB	8
1356185db85Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1366185db85Sdougm #define	OPT_SEC		9
1376185db85Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1386185db85Sdougm #define	OPT_PUBLIC	10
1396185db85Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1406185db85Sdougm #define	OPT_INDEX	11
1416185db85Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1426185db85Sdougm #define	OPT_LOG		12
1436185db85Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1446185db85Sdougm #define	OPT_CKSUM	13
1456185db85Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
1466185db85Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
1476185db85Sdougm #define	OPT_VOLFH	14
1486185db85Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1496185db85Sdougm #endif /* VOLATILE_FH_TEST */
1506185db85Sdougm 	NULL
1516185db85Sdougm };
1526185db85Sdougm 
1536185db85Sdougm /*
154a99982a7Sdougm  * list of properties that are related to security flavors.
1556185db85Sdougm  */
1566185db85Sdougm static char *seclist[] = {
1576185db85Sdougm 	SHOPT_RO,
1586185db85Sdougm 	SHOPT_RW,
1596185db85Sdougm 	SHOPT_ROOT,
1606185db85Sdougm 	SHOPT_WINDOW,
1616185db85Sdougm 	NULL
1626185db85Sdougm };
1636185db85Sdougm 
1646185db85Sdougm /* structure for list of securities */
1656185db85Sdougm struct securities {
166a99982a7Sdougm 	sa_security_t security;
167a99982a7Sdougm 	struct securities *next;
1686185db85Sdougm };
1696185db85Sdougm 
1706185db85Sdougm /*
1716185db85Sdougm  * findopt(name)
1726185db85Sdougm  *
1736185db85Sdougm  * Lookup option "name" in the option table and return the table
1746185db85Sdougm  * index.
1756185db85Sdougm  */
1766185db85Sdougm 
1776185db85Sdougm static int
1786185db85Sdougm findopt(char *name)
1796185db85Sdougm {
1806185db85Sdougm 	int i;
1816185db85Sdougm 	if (name != NULL) {
182a3351425Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
183a3351425Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
184a3351425Sdougm 				return (i);
185a3351425Sdougm 		}
1866185db85Sdougm 	}
1876185db85Sdougm 	return (-1);
1886185db85Sdougm }
1896185db85Sdougm 
1906185db85Sdougm /*
1916185db85Sdougm  * gettype(name)
1926185db85Sdougm  *
1936185db85Sdougm  * Return the type of option "name".
1946185db85Sdougm  */
1956185db85Sdougm 
1966185db85Sdougm static int
1976185db85Sdougm gettype(char *name)
1986185db85Sdougm {
1996185db85Sdougm 	int optdef;
2006185db85Sdougm 
2016185db85Sdougm 	optdef = findopt(name);
2026185db85Sdougm 	if (optdef != -1)
203a3351425Sdougm 		return (optdefs[optdef].type);
2046185db85Sdougm 	return (OPT_TYPE_ANY);
2056185db85Sdougm }
2066185db85Sdougm 
2076185db85Sdougm /*
2086185db85Sdougm  * nfs_validate_security_mode(mode)
2096185db85Sdougm  *
2106185db85Sdougm  * is the specified mode string a valid one for use with NFS?
2116185db85Sdougm  */
2126185db85Sdougm 
2136185db85Sdougm static int
2146185db85Sdougm nfs_validate_security_mode(char *mode)
2156185db85Sdougm {
2166185db85Sdougm 	seconfig_t secinfo;
2176185db85Sdougm 	int err;
2186185db85Sdougm 
2196185db85Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2206185db85Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2216185db85Sdougm 	if (err == SC_NOERROR)
222a3351425Sdougm 		return (1);
2236185db85Sdougm 	return (0);
2246185db85Sdougm }
2256185db85Sdougm 
2266185db85Sdougm /*
2276185db85Sdougm  * nfs_is_security_opt(tok)
2286185db85Sdougm  *
2296185db85Sdougm  * check to see if tok represents an option that is only valid in some
2306185db85Sdougm  * security flavor.
2316185db85Sdougm  */
2326185db85Sdougm 
2336185db85Sdougm static int
2346185db85Sdougm nfs_is_security_opt(char *tok)
2356185db85Sdougm {
2366185db85Sdougm 	int i;
2376185db85Sdougm 
2386185db85Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
239a3351425Sdougm 		if (strcmp(tok, seclist[i]) == 0)
240a3351425Sdougm 			return (1);
2416185db85Sdougm 	}
2426185db85Sdougm 	return (0);
2436185db85Sdougm }
2446185db85Sdougm 
2456185db85Sdougm /*
2466185db85Sdougm  * find_security(seclist, sec)
2476185db85Sdougm  *
2486185db85Sdougm  * Walk the current list of security flavors and return true if it is
2496185db85Sdougm  * present, else return false.
2506185db85Sdougm  */
2516185db85Sdougm 
2526185db85Sdougm static int
2536185db85Sdougm find_security(struct securities *seclist, sa_security_t sec)
2546185db85Sdougm {
2556185db85Sdougm 	while (seclist != NULL) {
256a3351425Sdougm 		if (seclist->security == sec)
257a3351425Sdougm 			return (1);
258a3351425Sdougm 		seclist = seclist->next;
2596185db85Sdougm 	}
2606185db85Sdougm 	return (0);
2616185db85Sdougm }
2626185db85Sdougm 
2636185db85Sdougm /*
2646185db85Sdougm  * make_security_list(group, securitymodes, proto)
2656185db85Sdougm  *	go through the list of securitymodes and add them to the
2666185db85Sdougm  *	group's list of security optionsets. We also keep a list of
2676185db85Sdougm  *	those optionsets so we don't have to find them later. All of
2686185db85Sdougm  *	these will get copies of the same properties.
2696185db85Sdougm  */
2706185db85Sdougm 
2716185db85Sdougm static struct securities *
2726185db85Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
2736185db85Sdougm {
2746185db85Sdougm 	char *tok, *next = NULL;
2756185db85Sdougm 	struct securities *curp, *headp = NULL, *prev;
2766185db85Sdougm 	sa_security_t check;
2776185db85Sdougm 	int freetok = 0;
2786185db85Sdougm 
2796185db85Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
280a3351425Sdougm 		next = strchr(tok, ':');
281a3351425Sdougm 		if (next != NULL)
282a3351425Sdougm 			*next++ = '\0';
283a3351425Sdougm 		if (strcmp(tok, "default") == 0) {
284a3351425Sdougm 			/* resolve default into the real type */
285a3351425Sdougm 			tok = nfs_space_alias(tok);
286a3351425Sdougm 			freetok = 1;
287a3351425Sdougm 		}
288a3351425Sdougm 		check = sa_get_security(group, tok, proto);
289a3351425Sdougm 
290a3351425Sdougm 		/* add to the security list if it isn't there already */
291a3351425Sdougm 		if (check == NULL || !find_security(headp, check)) {
292a3351425Sdougm 			curp = (struct securities *)calloc(1,
293a3351425Sdougm 			    sizeof (struct securities));
294a3351425Sdougm 			if (curp != NULL) {
295a3351425Sdougm 				if (check == NULL) {
296a3351425Sdougm 					curp->security = sa_create_security(
297a3351425Sdougm 					    group, tok, proto);
298a3351425Sdougm 				} else {
299a3351425Sdougm 					curp->security = check;
300a3351425Sdougm 				}
301a3351425Sdougm 				/*
302a3351425Sdougm 				 * note that the first time through the loop,
303a3351425Sdougm 				 * headp will be NULL and prev will be
304a3351425Sdougm 				 * undefined.  Since headp is NULL, we set
305a3351425Sdougm 				 * both it and prev to the curp (first
306a3351425Sdougm 				 * structure to be allocated).
307a3351425Sdougm 				 *
308a3351425Sdougm 				 * later passes through the loop will have
309a3351425Sdougm 				 * headp not being NULL and prev will be used
310a3351425Sdougm 				 * to allocate at the end of the list.
311a3351425Sdougm 				 */
312a3351425Sdougm 				if (headp == NULL) {
313a3351425Sdougm 					headp = curp;
314a3351425Sdougm 					prev = curp;
315a3351425Sdougm 				} else {
316a3351425Sdougm 					prev->next = curp;
317a3351425Sdougm 					prev = curp;
318a3351425Sdougm 				}
319a3351425Sdougm 			}
3206185db85Sdougm 		}
321a99982a7Sdougm 
322a3351425Sdougm 		if (freetok) {
323a3351425Sdougm 			freetok = 0;
324a3351425Sdougm 			sa_free_attr_string(tok);
325a3351425Sdougm 		}
3266185db85Sdougm 	}
3276185db85Sdougm 	return (headp);
3286185db85Sdougm }
3296185db85Sdougm 
3306185db85Sdougm static void
3316185db85Sdougm free_security_list(struct securities *sec)
3326185db85Sdougm {
3336185db85Sdougm 	struct securities *next;
3346185db85Sdougm 	if (sec != NULL) {
335a3351425Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
336a3351425Sdougm 			next = sec->next;
337a3351425Sdougm 			free(sec);
338a3351425Sdougm 		}
3396185db85Sdougm 	}
3406185db85Sdougm }
3416185db85Sdougm 
3426185db85Sdougm /*
3436185db85Sdougm  * nfs_alistcat(str1, str2, sep)
3446185db85Sdougm  *
3456185db85Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
3466185db85Sdougm  * character. If memory allocation fails, return NULL;
3476185db85Sdougm  */
3486185db85Sdougm 
3496185db85Sdougm static char *
3506185db85Sdougm nfs_alistcat(char *str1, char *str2, char sep)
3516185db85Sdougm {
3526185db85Sdougm 	char *newstr;
3536185db85Sdougm 	size_t len;
3546185db85Sdougm 
3556185db85Sdougm 	len = strlen(str1) + strlen(str2) + 2;
3566185db85Sdougm 	newstr = (char *)malloc(len);
3576185db85Sdougm 	if (newstr != NULL)
358a3351425Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
3596185db85Sdougm 	return (newstr);
3606185db85Sdougm }
3616185db85Sdougm 
3626185db85Sdougm /*
3636185db85Sdougm  * add_security_prop(sec, name, value, persist)
3646185db85Sdougm  *
3656185db85Sdougm  * Add the property to the securities structure. This accumulates
3666185db85Sdougm  * properties for as part of parsing legacy options.
3676185db85Sdougm  */
3686185db85Sdougm 
3696185db85Sdougm static int
3706185db85Sdougm add_security_prop(struct securities *sec, char *name, char *value,
3716185db85Sdougm 			int persist, int iszfs)
3726185db85Sdougm {
3736185db85Sdougm 	sa_property_t prop;
3746185db85Sdougm 	int ret = SA_OK;
3756185db85Sdougm 
3766185db85Sdougm 	for (; sec != NULL; sec = sec->next) {
377a3351425Sdougm 		if (value == NULL) {
378a3351425Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
379a3351425Sdougm 			    strcmp(name, SHOPT_RO) == 0)
380a3351425Sdougm 				value = "*";
381a3351425Sdougm 			else
382a3351425Sdougm 				value = "true";
383a3351425Sdougm 		}
384a99982a7Sdougm 
385a99982a7Sdougm 		/*
386a99982a7Sdougm 		 * Get the existing property, if it exists, so we can
387a99982a7Sdougm 		 * determine what to do with it. The ro/rw/root
388a99982a7Sdougm 		 * properties can be merged if multiple instances of
389a99982a7Sdougm 		 * these properies are given. For example, if "rw"
390a99982a7Sdougm 		 * exists with a value "host1" and a later token of
391a99982a7Sdougm 		 * rw="host2" is seen, the values are merged into a
392a99982a7Sdougm 		 * single rw="host1:host2".
393a99982a7Sdougm 		 */
394a3351425Sdougm 		prop = sa_get_property(sec->security, name);
395a99982a7Sdougm 
396a3351425Sdougm 		if (prop != NULL) {
397a3351425Sdougm 			char *oldvalue;
398a3351425Sdougm 			char *newvalue;
399a99982a7Sdougm 
400a99982a7Sdougm 			/*
401a3351425Sdougm 			 * The security options of ro/rw/root might appear
402a3351425Sdougm 			 * multiple times. If they do, the values need to be
403a3351425Sdougm 			 * merged into an access list. If it was previously
404a3351425Sdougm 			 * empty, the new value alone is added.
405a99982a7Sdougm 			 */
406a3351425Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
407a3351425Sdougm 			if (oldvalue != NULL) {
408a3351425Sdougm 				/*
409a3351425Sdougm 				 * The general case is to concatenate the new
410a3351425Sdougm 				 * value onto the old value for multiple
411a3351425Sdougm 				 * rw(ro/root) properties. A special case
412a3351425Sdougm 				 * exists when either the old or new is the
413a3351425Sdougm 				 * "all" case. In the special case, if both
414a3351425Sdougm 				 * are "all", then it is "all", else if one is
415a3351425Sdougm 				 * an access-list, that replaces the "all".
416a3351425Sdougm 				 */
417a3351425Sdougm 				if (strcmp(oldvalue, "*") == 0) {
418a3351425Sdougm 					/* Replace old value with new value. */
419a3351425Sdougm 					newvalue = strdup(value);
420a3351425Sdougm 				} else if (strcmp(value, "*") == 0) {
421a3351425Sdougm 					/*
422a3351425Sdougm 					 * Keep old value and ignore
423a3351425Sdougm 					 * the new value.
424a3351425Sdougm 					 */
425a3351425Sdougm 					newvalue = NULL;
426a3351425Sdougm 				} else {
427a3351425Sdougm 					/*
428a3351425Sdougm 					 * Make a new list of old plus new
429a3351425Sdougm 					 * access-list.
430a3351425Sdougm 					 */
431a3351425Sdougm 					newvalue = nfs_alistcat(oldvalue,
432a3351425Sdougm 					    value, ':');
433a3351425Sdougm 				}
434a3351425Sdougm 
435a3351425Sdougm 				if (newvalue != NULL) {
436a3351425Sdougm 					(void) sa_remove_property(prop);
437a3351425Sdougm 					prop = sa_create_property(name,
438a3351425Sdougm 					    newvalue);
439a3351425Sdougm 					ret = sa_add_property(sec->security,
440a3351425Sdougm 					    prop);
441a3351425Sdougm 					free(newvalue);
442a3351425Sdougm 				}
443a3351425Sdougm 				if (oldvalue != NULL)
444a3351425Sdougm 					sa_free_attr_string(oldvalue);
445a3351425Sdougm 			}
446a3351425Sdougm 		} else {
447a3351425Sdougm 			prop = sa_create_property(name, value);
448a99982a7Sdougm 			ret = sa_add_property(sec->security, prop);
4496185db85Sdougm 		}
450a3351425Sdougm 		if (ret == SA_OK && !iszfs) {
451a3351425Sdougm 			ret = sa_commit_properties(sec->security, !persist);
452a3351425Sdougm 		}
4536185db85Sdougm 	}
4546185db85Sdougm 	return (ret);
4556185db85Sdougm }
4566185db85Sdougm 
4576185db85Sdougm /*
4586185db85Sdougm  * check to see if group/share is persistent.
4596185db85Sdougm  */
4606185db85Sdougm static int
4616185db85Sdougm is_persistent(sa_group_t group)
4626185db85Sdougm {
4636185db85Sdougm 	char *type;
4646185db85Sdougm 	int persist = 1;
4656185db85Sdougm 
4666185db85Sdougm 	type = sa_get_group_attr(group, "type");
4676185db85Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
468a3351425Sdougm 		persist = 0;
4696185db85Sdougm 	if (type != NULL)
470a3351425Sdougm 		sa_free_attr_string(type);
4716185db85Sdougm 	return (persist);
4726185db85Sdougm }
4736185db85Sdougm 
4746185db85Sdougm /*
4756185db85Sdougm  * invalid_security(options)
4766185db85Sdougm  *
4776185db85Sdougm  * search option string for any invalid sec= type.
4786185db85Sdougm  * return true (1) if any are not valid else false (0)
4796185db85Sdougm  */
4806185db85Sdougm static int
4816185db85Sdougm invalid_security(char *options)
4826185db85Sdougm {
4836185db85Sdougm 	char *copy, *base, *token, *value;
4846185db85Sdougm 	int ret = 0;
4856185db85Sdougm 
4866185db85Sdougm 	copy = strdup(options);
4876185db85Sdougm 	token = base = copy;
4886185db85Sdougm 	while (token != NULL && ret == 0) {
489a3351425Sdougm 		token = strtok(base, ",");
490a3351425Sdougm 		base = NULL;
491a3351425Sdougm 		if (token != NULL) {
492a3351425Sdougm 			value = strchr(token, '=');
493a3351425Sdougm 			if (value != NULL)
494a3351425Sdougm 				*value++ = '\0';
495a3351425Sdougm 			if (strcmp(token, "sec") == 0) {
496a3351425Sdougm 				/* HAVE security flavors so check them */
497a3351425Sdougm 				char *tok, *next;
498a3351425Sdougm 				for (next = NULL, tok = value; tok != NULL;
499a3351425Sdougm 				    tok = next) {
500a3351425Sdougm 					next = strchr(tok, ':');
501a3351425Sdougm 					if (next != NULL)
502a3351425Sdougm 						*next++ = '\0';
503a3351425Sdougm 					ret = !nfs_validate_security_mode(tok);
504a3351425Sdougm 					if (ret)
505a3351425Sdougm 						break;
506a3351425Sdougm 				}
507a3351425Sdougm 			}
5086185db85Sdougm 		}
5096185db85Sdougm 	}
5106185db85Sdougm 	if (copy != NULL)
511a3351425Sdougm 		free(copy);
5126185db85Sdougm 	return (ret);
5136185db85Sdougm }
5146185db85Sdougm 
5156185db85Sdougm /*
5166185db85Sdougm  * nfs_parse_legacy_options(group, options)
5176185db85Sdougm  *
5186185db85Sdougm  * Parse the old style options into internal format and store on the
5196185db85Sdougm  * specified group.  Group could be a share for full legacy support.
5206185db85Sdougm  */
5216185db85Sdougm 
5226185db85Sdougm static int
5236185db85Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5246185db85Sdougm {
5256185db85Sdougm 	char *dup = strdup(options);
5266185db85Sdougm 	char *base;
5276185db85Sdougm 	char *token;
5286185db85Sdougm 	sa_optionset_t optionset;
5296185db85Sdougm 	struct securities *security_list = NULL;
5306185db85Sdougm 	sa_property_t prop;
5316185db85Sdougm 	int ret = SA_OK;
5326185db85Sdougm 	int iszfs = 0;
5336185db85Sdougm 	sa_group_t parent;
5346185db85Sdougm 	int persist = 0;
5356185db85Sdougm 	char *lasts;
5366185db85Sdougm 
5376185db85Sdougm 	/* do we have an existing optionset? */
5386185db85Sdougm 	optionset = sa_get_optionset(group, "nfs");
5396185db85Sdougm 	if (optionset == NULL) {
540a3351425Sdougm 		/* didn't find existing optionset so create one */
541a3351425Sdougm 		optionset = sa_create_optionset(group, "nfs");
5426185db85Sdougm 	} else {
5436185db85Sdougm 		/*
5446185db85Sdougm 		 * have an existing optionset so we need to compare
5456185db85Sdougm 		 * options in order to detect errors. For now, we
5466185db85Sdougm 		 * assume that the first optionset is the correct one
5476185db85Sdougm 		 * and the others will be the same. This needs to be
5486185db85Sdougm 		 * fixed before the final code is ready.
5496185db85Sdougm 		 */
550a3351425Sdougm 		return (ret);
5516185db85Sdougm 	}
5526185db85Sdougm 
5536185db85Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
5546185db85Sdougm 		/*
5556185db85Sdougm 		 * there is a special case of only the option "rw"
5566185db85Sdougm 		 * being the default option. We don't have to do
5576185db85Sdougm 		 * anything.
5586185db85Sdougm 		 */
559a3351425Sdougm 		return (ret);
5606185db85Sdougm 	}
5616185db85Sdougm 
5626185db85Sdougm 	/*
5636185db85Sdougm 	 * check if security types are present and validate them. If
5646185db85Sdougm 	 * any are not legal, fail.
5656185db85Sdougm 	 */
5666185db85Sdougm 
5676185db85Sdougm 	if (invalid_security(options)) {
568a3351425Sdougm 		return (SA_INVALID_SECURITY);
5696185db85Sdougm 	}
5706185db85Sdougm 
5716185db85Sdougm 	/*
5726185db85Sdougm 	 * in order to not attempt to change ZFS properties unless
5736185db85Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
5746185db85Sdougm 	 */
5756185db85Sdougm 	if (sa_is_share(group)) {
576a3351425Sdougm 		char *zfs;
577a3351425Sdougm 		parent = sa_get_parent_group(group);
578a3351425Sdougm 		if (parent != NULL) {
579a3351425Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
580a3351425Sdougm 			if (zfs != NULL) {
581a3351425Sdougm 				sa_free_attr_string(zfs);
582a3351425Sdougm 				iszfs++;
583a3351425Sdougm 			}
5846185db85Sdougm 		}
5856185db85Sdougm 	} else {
586a3351425Sdougm 		iszfs = sa_group_is_zfs(group);
5876185db85Sdougm 	}
5886185db85Sdougm 
5896185db85Sdougm 	/*
5906185db85Sdougm 	 * we need to step through each option in the string and then
5916185db85Sdougm 	 * add either the option or the security option as needed. If
5926185db85Sdougm 	 * this is not a persistent share, don't commit to the
593a99982a7Sdougm 	 * repository. If there is an error, we also want to abort the
594a99982a7Sdougm 	 * processing and report it.
5956185db85Sdougm 	 */
5966185db85Sdougm 	persist = is_persistent(group);
5976185db85Sdougm 	base = dup;
5986185db85Sdougm 	token = dup;
5996185db85Sdougm 	lasts = NULL;
600a99982a7Sdougm 	while (token != NULL && ret == SA_OK) {
601a3351425Sdougm 		ret = SA_OK;
602a3351425Sdougm 		token = strtok_r(base, ",", &lasts);
603a3351425Sdougm 		base = NULL;
604a3351425Sdougm 		if (token != NULL) {
605a3351425Sdougm 			char *value;
6066185db85Sdougm 			/*
607a3351425Sdougm 			 * if the option has a value, it will have an '=' to
608a3351425Sdougm 			 * separate the name from the value. The following
609a3351425Sdougm 			 * code will result in value != NULL and token
610a3351425Sdougm 			 * pointing to just the name if there is a value.
6116185db85Sdougm 			 */
612a3351425Sdougm 			value = strchr(token, '=');
613a3351425Sdougm 			if (value != NULL) {
614a3351425Sdougm 				*value++ = '\0';
6156185db85Sdougm 			}
616a3351425Sdougm 			if (strcmp(token, "sec") == 0 ||
617a3351425Sdougm 			    strcmp(token, "secure") == 0) {
6186185db85Sdougm 				/*
619a3351425Sdougm 				 * Once in security parsing, we only
620a3351425Sdougm 				 * do security. We do need to move
621a3351425Sdougm 				 * between the security node and the
622a3351425Sdougm 				 * toplevel. The security tag goes on
623a3351425Sdougm 				 * the root while the following ones
624a3351425Sdougm 				 * go on the security.
6256185db85Sdougm 				 */
626a3351425Sdougm 				if (security_list != NULL) {
627a3351425Sdougm 					/*
628a3351425Sdougm 					 * have an old list so close it and
629a3351425Sdougm 					 * start the new
630a3351425Sdougm 					 */
631a3351425Sdougm 					free_security_list(security_list);
632a3351425Sdougm 				}
633a3351425Sdougm 				if (strcmp(token, "secure") == 0) {
634a3351425Sdougm 					value = "dh";
635a3351425Sdougm 				} else {
636a3351425Sdougm 					if (value == NULL) {
637a3351425Sdougm 						ret = SA_SYNTAX_ERR;
638a3351425Sdougm 						break;
639a3351425Sdougm 					}
640a3351425Sdougm 				}
641a3351425Sdougm 				security_list = make_security_list(group,
642a3351425Sdougm 				    value, "nfs");
6436185db85Sdougm 			} else {
644a3351425Sdougm 				/*
645a3351425Sdougm 				 * Note that the "old" syntax allowed a
646a3351425Sdougm 				 * default security model This must be
647a3351425Sdougm 				 * accounted for and internally converted to
648a3351425Sdougm 				 * "standard" security structure.
649a3351425Sdougm 				 */
650a3351425Sdougm 				if (nfs_is_security_opt(token)) {
651a3351425Sdougm 					if (security_list == NULL) {
652a3351425Sdougm 						/*
653a3351425Sdougm 						 * need to have a
654a3351425Sdougm 						 * security
655a3351425Sdougm 						 * option. This will
656a3351425Sdougm 						 * be "closed" when a
657a3351425Sdougm 						 * defined "sec="
658a3351425Sdougm 						 * option is
659a3351425Sdougm 						 * seen. This is
660a3351425Sdougm 						 * technically an
661a3351425Sdougm 						 * error but will be
662a3351425Sdougm 						 * allowed with
663a3351425Sdougm 						 * warning.
664a3351425Sdougm 						 */
665a3351425Sdougm 						security_list =
666a3351425Sdougm 						    make_security_list(group,
667a3351425Sdougm 						    "default",
668a3351425Sdougm 						    "nfs");
669a3351425Sdougm 					}
670a3351425Sdougm 					if (security_list != NULL) {
671a3351425Sdougm 						ret = add_security_prop(
672a3351425Sdougm 						    security_list, token,
673a3351425Sdougm 						    value, persist, iszfs);
674a3351425Sdougm 					} else {
675a3351425Sdougm 						ret = SA_NO_MEMORY;
676a3351425Sdougm 					}
677a3351425Sdougm 				} else {
678a3351425Sdougm 					/* regular options */
679a3351425Sdougm 					if (value == NULL) {
680a3351425Sdougm 						if (strcmp(token, SHOPT_RW) ==
681a3351425Sdougm 						    0 || strcmp(token,
682a3351425Sdougm 						    SHOPT_RO) == 0) {
683a3351425Sdougm 							value = "*";
684a3351425Sdougm 						} else {
685a3351425Sdougm 							value = "global";
686a3351425Sdougm 							if (strcmp(token,
687a3351425Sdougm 							    SHOPT_LOG) != 0) {
688a3351425Sdougm 								value = "true";
689a3351425Sdougm 							}
690a3351425Sdougm 						}
691a3351425Sdougm 					}
692*d6405362Sdougm 					/*
693*d6405362Sdougm 					 * In all cases, create the
694*d6405362Sdougm 					 * property specified. If the
695*d6405362Sdougm 					 * value was NULL, the default
696*d6405362Sdougm 					 * value will have been
697*d6405362Sdougm 					 * substituted.
698*d6405362Sdougm 					 */
699*d6405362Sdougm 					prop = sa_create_property(token, value);
700*d6405362Sdougm 					ret =  sa_add_property(optionset, prop);
701*d6405362Sdougm 					if (ret != SA_OK)
702*d6405362Sdougm 						break;
703*d6405362Sdougm 
704a3351425Sdougm 					if (!iszfs) {
705a3351425Sdougm 						ret = sa_commit_properties(
706a3351425Sdougm 						    optionset, !persist);
707a3351425Sdougm 					}
708a3351425Sdougm 				}
7096185db85Sdougm 			}
7106185db85Sdougm 		}
7116185db85Sdougm 	}
7126185db85Sdougm 	if (security_list != NULL)
713a3351425Sdougm 		free_security_list(security_list);
7146185db85Sdougm 	if (dup != NULL)
715a3351425Sdougm 		free(dup);
7166185db85Sdougm 	return (ret);
7176185db85Sdougm }
7186185db85Sdougm 
7196185db85Sdougm /*
7206185db85Sdougm  * is_a_number(number)
7216185db85Sdougm  *
7226185db85Sdougm  * is the string a number in one of the forms we want to use?
7236185db85Sdougm  */
7246185db85Sdougm 
7256185db85Sdougm static int
7266185db85Sdougm is_a_number(char *number)
7276185db85Sdougm {
7286185db85Sdougm 	int ret = 1;
7296185db85Sdougm 	int hex = 0;
7306185db85Sdougm 
7316185db85Sdougm 	if (strncmp(number, "0x", 2) == 0) {
732a3351425Sdougm 		number += 2;
733a3351425Sdougm 		hex = 1;
734a3351425Sdougm 	} else if (*number == '-') {
735a3351425Sdougm 		number++; /* skip the minus */
736a3351425Sdougm 	}
7376185db85Sdougm 	while (ret == 1 && *number != '\0') {
738a3351425Sdougm 		if (hex) {
739a3351425Sdougm 			ret = isxdigit(*number++);
740a3351425Sdougm 		} else {
741a3351425Sdougm 			ret = isdigit(*number++);
742a3351425Sdougm 		}
7436185db85Sdougm 	}
7446185db85Sdougm 	return (ret);
7456185db85Sdougm }
7466185db85Sdougm 
7476185db85Sdougm /*
7486185db85Sdougm  * Look for the specified tag in the configuration file. If it is found,
7496185db85Sdougm  * enable logging and set the logging configuration information for exp.
7506185db85Sdougm  */
7516185db85Sdougm static void
7526185db85Sdougm configlog(struct exportdata *exp, char *tag)
7536185db85Sdougm {
7546185db85Sdougm 	nfsl_config_t *configlist = NULL, *configp;
7556185db85Sdougm 	int error = 0;
7566185db85Sdougm 	char globaltag[] = DEFAULTTAG;
7576185db85Sdougm 
7586185db85Sdougm 	/*
7596185db85Sdougm 	 * Sends config errors to stderr
7606185db85Sdougm 	 */
7616185db85Sdougm 	nfsl_errs_to_syslog = B_FALSE;
7626185db85Sdougm 
7636185db85Sdougm 	/*
7646185db85Sdougm 	 * get the list of configuration settings
7656185db85Sdougm 	 */
7666185db85Sdougm 	error = nfsl_getconfig_list(&configlist);
7676185db85Sdougm 	if (error) {
7686185db85Sdougm 		(void) fprintf(stderr,
769a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
770a3351425Sdougm 		    strerror(error));
7716185db85Sdougm 	}
7726185db85Sdougm 
7736185db85Sdougm 	if (tag == NULL)
7746185db85Sdougm 		tag = globaltag;
7756185db85Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
7766185db85Sdougm 		nfsl_freeconfig_list(&configlist);
7776185db85Sdougm 		(void) fprintf(stderr,
778a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
7796185db85Sdougm 		/* bad configuration */
7806185db85Sdougm 		error = ENOENT;
7816185db85Sdougm 		goto err;
7826185db85Sdougm 	}
7836185db85Sdougm 
7846185db85Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
7856185db85Sdougm 		error = ENOMEM;
7866185db85Sdougm 		goto out;
7876185db85Sdougm 	}
7886185db85Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
7896185db85Sdougm 		error = ENOMEM;
7906185db85Sdougm 		goto out;
7916185db85Sdougm 	}
7926185db85Sdougm 	exp->ex_flags |= EX_LOG;
7936185db85Sdougm 	if (configp->nc_rpclogpath != NULL)
7946185db85Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
7956185db85Sdougm out:
7966185db85Sdougm 	if (configlist != NULL)
797a3351425Sdougm 		nfsl_freeconfig_list(&configlist);
7986185db85Sdougm 
7996185db85Sdougm err:
8006185db85Sdougm 	if (error != 0) {
8016185db85Sdougm 		if (exp->ex_flags != NULL)
8026185db85Sdougm 			free(exp->ex_tag);
8036185db85Sdougm 		if (exp->ex_log_buffer != NULL)
8046185db85Sdougm 			free(exp->ex_log_buffer);
8056185db85Sdougm 		(void) fprintf(stderr,
806a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
807a3351425Sdougm 		    strerror(error));
8086185db85Sdougm 	}
8096185db85Sdougm }
8106185db85Sdougm 
8116185db85Sdougm /*
8126185db85Sdougm  * fill_export_from_optionset(export, optionset)
8136185db85Sdougm  *
8146185db85Sdougm  * In order to share, we need to set all the possible general options
8156185db85Sdougm  * into the export structure. Share info will be filled in by the
8166185db85Sdougm  * caller. Various property values get turned into structure specific
8176185db85Sdougm  * values.
8186185db85Sdougm  */
8196185db85Sdougm 
8206185db85Sdougm static int
8216185db85Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
8226185db85Sdougm {
8236185db85Sdougm 	sa_property_t option;
8246185db85Sdougm 	int ret = SA_OK;
8256185db85Sdougm 
8266185db85Sdougm 	for (option = sa_get_property(optionset, NULL);
827a3351425Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
828a3351425Sdougm 		char *name;
829a3351425Sdougm 		char *value;
830a3351425Sdougm 		uint32_t val;
8316185db85Sdougm 
832a3351425Sdougm 		/*
833a3351425Sdougm 		 * since options may be set/reset multiple times, always do an
834a3351425Sdougm 		 * explicit set or clear of the option. This allows defaults
835a3351425Sdougm 		 * to be set and then the protocol specifici to override.
836a3351425Sdougm 		 */
8376185db85Sdougm 
838a3351425Sdougm 		name = sa_get_property_attr(option, "type");
839a3351425Sdougm 		value = sa_get_property_attr(option, "value");
840a3351425Sdougm 		switch (findopt(name)) {
841a3351425Sdougm 		case OPT_ANON:
842a3351425Sdougm 			if (value != NULL && is_a_number(value)) {
843a3351425Sdougm 				val = strtoul(value, NULL, 0);
844a3351425Sdougm 			} else {
845a3351425Sdougm 				struct passwd *pw;
846a3351425Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
847a3351425Sdougm 				if (pw != NULL) {
848a3351425Sdougm 					val = pw->pw_uid;
849a3351425Sdougm 				} else {
850a3351425Sdougm 					val = UID_NOBODY;
851a3351425Sdougm 				}
852a3351425Sdougm 				endpwent();
853a3351425Sdougm 			}
854a3351425Sdougm 			export->ex_anon = val;
855a3351425Sdougm 			break;
856a3351425Sdougm 		case OPT_NOSUID:
857a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
858a3351425Sdougm 			    strcmp(value, "1") == 0))
859a3351425Sdougm 				export->ex_flags |= EX_NOSUID;
860a3351425Sdougm 			else
861a3351425Sdougm 				export->ex_flags &= ~EX_NOSUID;
862a3351425Sdougm 			break;
863a3351425Sdougm 		case OPT_ACLOK:
864a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
865a3351425Sdougm 			    strcmp(value, "1") == 0))
866a3351425Sdougm 				export->ex_flags |= EX_ACLOK;
867a3351425Sdougm 			else
868a3351425Sdougm 				export->ex_flags &= ~EX_ACLOK;
869a3351425Sdougm 			break;
870a3351425Sdougm 		case OPT_NOSUB:
871a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
872a3351425Sdougm 			    strcmp(value, "1") == 0))
873a3351425Sdougm 				export->ex_flags |= EX_NOSUB;
874a3351425Sdougm 			else
875a3351425Sdougm 				export->ex_flags &= ~EX_NOSUB;
876a3351425Sdougm 			break;
877a3351425Sdougm 		case OPT_PUBLIC:
878a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
879a3351425Sdougm 			    strcmp(value, "1") == 0))
880a3351425Sdougm 				export->ex_flags |= EX_PUBLIC;
881a3351425Sdougm 			else
882a3351425Sdougm 				export->ex_flags &= ~EX_PUBLIC;
883a3351425Sdougm 			break;
884a3351425Sdougm 		case OPT_INDEX:
885a3351425Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
886a3351425Sdougm 			    strchr(value, '/') != NULL)) {
887a3351425Sdougm 				/* this is an error */
888a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
889a3351425Sdougm 				    "NFS: index=\"%s\" not valid;"
890a3351425Sdougm 				    "must be a filename.\n"),
891a3351425Sdougm 				    value);
892a3351425Sdougm 				break;
893a3351425Sdougm 			}
894a3351425Sdougm 			if (value != NULL && *value != '\0' &&
895a3351425Sdougm 			    strcmp(value, ".") != 0) {
896a3351425Sdougm 				/* valid index file string */
897a3351425Sdougm 				if (export->ex_index != NULL) {
898a3351425Sdougm 					/* left over from "default" */
899a3351425Sdougm 					free(export->ex_index);
900a3351425Sdougm 				}
901a3351425Sdougm 				/* remember to free */
902a3351425Sdougm 				export->ex_index = strdup(value);
903a3351425Sdougm 				if (export->ex_index == NULL) {
904a3351425Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
905a3351425Sdougm 					    "NFS: out of memory setting "
906a3351425Sdougm 					    "index property\n"));
907a3351425Sdougm 					break;
908a3351425Sdougm 				}
909a3351425Sdougm 				export->ex_flags |= EX_INDEX;
910a3351425Sdougm 			}
911a3351425Sdougm 			break;
912a3351425Sdougm 		case OPT_LOG:
913a3351425Sdougm 			if (value == NULL)
914a3351425Sdougm 				value = strdup("global");
915a3351425Sdougm 			if (value != NULL)
916a3351425Sdougm 				configlog(export,
917a3351425Sdougm 				    strlen(value) ? value : "global");
918a3351425Sdougm 			break;
919a3351425Sdougm 		default:
920a3351425Sdougm 			/* have a syntactic error */
921549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
922a3351425Sdougm 			    "NFS: unrecognized option %s=%s\n"),
923a3351425Sdougm 			    name, value != NULL ? value : "");
9246185db85Sdougm 			break;
9256185db85Sdougm 		}
926a3351425Sdougm 		if (name != NULL)
927a3351425Sdougm 			sa_free_attr_string(name);
9286185db85Sdougm 		if (value != NULL)
929a3351425Sdougm 			sa_free_attr_string(value);
9306185db85Sdougm 	}
9316185db85Sdougm 	return (ret);
9326185db85Sdougm }
9336185db85Sdougm 
9346185db85Sdougm /*
9356185db85Sdougm  * cleanup_export(export)
9366185db85Sdougm  *
9376185db85Sdougm  * Cleanup the allocated areas so we don't leak memory
9386185db85Sdougm  */
9396185db85Sdougm 
9406185db85Sdougm static void
9416185db85Sdougm cleanup_export(struct exportdata *export)
9426185db85Sdougm {
9436185db85Sdougm 	int i;
9446185db85Sdougm 
9456185db85Sdougm 	if (export->ex_index != NULL)
946a3351425Sdougm 		free(export->ex_index);
9476185db85Sdougm 	if (export->ex_secinfo != NULL) {
948a3351425Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
949a3351425Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
950a3351425Sdougm 				free(export->ex_secinfo[i].s_rootnames);
951a3351425Sdougm 		free(export->ex_secinfo);
9526185db85Sdougm 	}
9536185db85Sdougm }
9546185db85Sdougm 
9556185db85Sdougm /*
9566185db85Sdougm  * Given a seconfig entry and a colon-separated
9576185db85Sdougm  * list of names, allocate an array big enough
9586185db85Sdougm  * to hold the root list, then convert each name to
9596185db85Sdougm  * a principal name according to the security
9606185db85Sdougm  * info and assign it to an array element.
9616185db85Sdougm  * Return the array and its size.
9626185db85Sdougm  */
9636185db85Sdougm static caddr_t *
9646185db85Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
9656185db85Sdougm {
9666185db85Sdougm 	caddr_t *a;
9676185db85Sdougm 	int c, i;
9686185db85Sdougm 	char *host, *p;
9696185db85Sdougm 
9706185db85Sdougm 	/*
9716185db85Sdougm 	 * Count the number of strings in the list.
9726185db85Sdougm 	 * This is the number of colon separators + 1.
9736185db85Sdougm 	 */
9746185db85Sdougm 	c = 1;
9756185db85Sdougm 	for (p = list; *p; p++)
9766185db85Sdougm 		if (*p == ':')
9776185db85Sdougm 			c++;
9786185db85Sdougm 	*count = c;
9796185db85Sdougm 
9806185db85Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
9816185db85Sdougm 	if (a == NULL) {
982549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
983a3351425Sdougm 		    "get_rootnames: no memory\n"));
9846185db85Sdougm 	} else {
985a3351425Sdougm 		for (i = 0; i < c; i++) {
986a3351425Sdougm 			host = strtok(list, ":");
987a3351425Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
988a3351425Sdougm 				free(a);
989a3351425Sdougm 				a = NULL;
990a3351425Sdougm 				break;
991a3351425Sdougm 			}
992a3351425Sdougm 			list = NULL;
9936185db85Sdougm 		}
9946185db85Sdougm 	}
9956185db85Sdougm 
9966185db85Sdougm 	return (a);
9976185db85Sdougm }
9986185db85Sdougm 
9996185db85Sdougm /*
10006185db85Sdougm  * fill_security_from_secopts(sp, secopts)
10016185db85Sdougm  *
10026185db85Sdougm  * Fill the secinfo structure from the secopts optionset.
10036185db85Sdougm  */
10046185db85Sdougm 
10056185db85Sdougm static int
10066185db85Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
10076185db85Sdougm {
10086185db85Sdougm 	sa_property_t prop;
10096185db85Sdougm 	char *type;
10106185db85Sdougm 	int longform;
1011a99982a7Sdougm 	int err = SC_NOERROR;
10126185db85Sdougm 
10136185db85Sdougm 	type = sa_get_security_attr(secopts, "sectype");
10146185db85Sdougm 	if (type != NULL) {
1015a3351425Sdougm 		/* named security type needs secinfo to be filled in */
1016a3351425Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
1017a3351425Sdougm 		sa_free_attr_string(type);
1018a3351425Sdougm 		if (err != SC_NOERROR)
1019a3351425Sdougm 			return (err);
10206185db85Sdougm 	} else {
1021a3351425Sdougm 		/* default case */
1022a3351425Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
1023a3351425Sdougm 		if (err != SC_NOERROR)
1024a3351425Sdougm 			return (err);
10256185db85Sdougm 	}
10266185db85Sdougm 
1027a99982a7Sdougm 	err = SA_OK;
10286185db85Sdougm 	for (prop = sa_get_property(secopts, NULL);
1029a3351425Sdougm 	    prop != NULL && err == SA_OK;
1030a3351425Sdougm 	    prop = sa_get_next_property(prop)) {
1031a3351425Sdougm 		char *name;
1032a3351425Sdougm 		char *value;
10336185db85Sdougm 
1034a3351425Sdougm 		name = sa_get_property_attr(prop, "type");
1035a3351425Sdougm 		value = sa_get_property_attr(prop, "value");
10366185db85Sdougm 
1037a3351425Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
10386185db85Sdougm 
1039a3351425Sdougm 		switch (findopt(name)) {
1040a3351425Sdougm 		case OPT_RO:
1041a3351425Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
1042a3351425Sdougm 			break;
1043a3351425Sdougm 		case OPT_RW:
1044a3351425Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
1045a3351425Sdougm 			break;
1046a3351425Sdougm 		case OPT_ROOT:
1047a3351425Sdougm 			sp->s_flags |= M_ROOT;
1048a3351425Sdougm 			/*
1049a3351425Sdougm 			 * if we are using AUTH_UNIX, handle like other things
1050a3351425Sdougm 			 * such as RO/RW
1051a3351425Sdougm 			 */
1052a3351425Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
1053a3351425Sdougm 				continue;
1054a3351425Sdougm 			/* not AUTH_UNIX */
1055a3351425Sdougm 			if (value != NULL) {
1056a3351425Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
1057a3351425Sdougm 				    value, &sp->s_rootcnt);
1058a3351425Sdougm 				if (sp->s_rootnames == NULL) {
1059a3351425Sdougm 					err = SA_BAD_VALUE;
1060a3351425Sdougm 					(void) fprintf(stderr,
1061a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
1062a3351425Sdougm 					    "Bad root list\n"));
1063a3351425Sdougm 				}
1064a3351425Sdougm 			}
1065a3351425Sdougm 			break;
1066a3351425Sdougm 		case OPT_WINDOW:
1067a3351425Sdougm 			if (value != NULL) {
1068a3351425Sdougm 				sp->s_window = atoi(value);
1069a3351425Sdougm 				/* just in case */
1070a3351425Sdougm 				if (sp->s_window < 0)
1071a3351425Sdougm 					sp->s_window = DEF_WIN;
1072a3351425Sdougm 			}
1073a3351425Sdougm 			break;
1074a3351425Sdougm 		default:
1075a3351425Sdougm 			break;
10766185db85Sdougm 		}
1077a3351425Sdougm 		if (name != NULL)
1078a3351425Sdougm 			sa_free_attr_string(name);
1079a3351425Sdougm 		if (value != NULL)
1080a3351425Sdougm 			sa_free_attr_string(value);
10816185db85Sdougm 	}
10826185db85Sdougm 	/* if rw/ro options not set, use default of RW */
10836185db85Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
1084a3351425Sdougm 		sp->s_flags |= M_RW;
10856185db85Sdougm 	return (err);
10866185db85Sdougm }
10876185db85Sdougm 
10886185db85Sdougm /*
10896185db85Sdougm  * This is for testing only
10906185db85Sdougm  * It displays the export structure that
10916185db85Sdougm  * goes into the kernel.
10926185db85Sdougm  */
10936185db85Sdougm static void
10946185db85Sdougm printarg(char *path, struct exportdata *ep)
10956185db85Sdougm {
10966185db85Sdougm 	int i, j;
10976185db85Sdougm 	struct secinfo *sp;
10986185db85Sdougm 
10996185db85Sdougm 	if (debug == 0)
1100a3351425Sdougm 		return;
11016185db85Sdougm 
11026185db85Sdougm 	(void) printf("%s:\n", path);
11036185db85Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
11046185db85Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
1105549ec3ffSdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
11066185db85Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
11076185db85Sdougm 	if (ep->ex_flags & EX_NOSUID)
11086185db85Sdougm 		(void) printf("NOSUID ");
11096185db85Sdougm 	if (ep->ex_flags & EX_ACLOK)
11106185db85Sdougm 		(void) printf("ACLOK ");
11116185db85Sdougm 	if (ep->ex_flags & EX_PUBLIC)
11126185db85Sdougm 		(void) printf("PUBLIC ");
11136185db85Sdougm 	if (ep->ex_flags & EX_NOSUB)
11146185db85Sdougm 		(void) printf("NOSUB ");
11156185db85Sdougm 	if (ep->ex_flags & EX_LOG)
11166185db85Sdougm 		(void) printf("LOG ");
11176185db85Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
11186185db85Sdougm 		(void) printf("LOG_ALLOPS ");
11196185db85Sdougm 	if (ep->ex_flags == 0)
11206185db85Sdougm 		(void) printf("(none)");
11216185db85Sdougm 	(void) 	printf("\n");
11226185db85Sdougm 	if (ep->ex_flags & EX_LOG) {
11236185db85Sdougm 		(void) printf("\tex_log_buffer = %s\n",
1124a3351425Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
11256185db85Sdougm 		(void) printf("\tex_tag = %s\n",
1126a3351425Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
11276185db85Sdougm 	}
11286185db85Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
11296185db85Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
11306185db85Sdougm 	(void) printf("\n");
11316185db85Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
11326185db85Sdougm 		sp = &ep->ex_secinfo[i];
11336185db85Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
11346185db85Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
11356185db85Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
11366185db85Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
11376185db85Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
11386185db85Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
11396185db85Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
11406185db85Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
11416185db85Sdougm 		(void) printf("\n");
11426185db85Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
11436185db85Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
11446185db85Sdougm 		(void) fflush(stdout);
11456185db85Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
11466185db85Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
1147a3351425Sdougm 			    sp->s_rootnames[j] : "<null>");
11486185db85Sdougm 		(void) printf("\n\n");
11496185db85Sdougm 	}
11506185db85Sdougm }
11516185db85Sdougm 
11526185db85Sdougm /*
11536185db85Sdougm  * count_security(opts)
11546185db85Sdougm  *
11556185db85Sdougm  * Count the number of security types (flavors). The optionset has
11566185db85Sdougm  * been populated with the security flavors as a holding mechanism.
11576185db85Sdougm  * We later use this number to allocate data structures.
11586185db85Sdougm  */
11596185db85Sdougm 
11606185db85Sdougm static int
11616185db85Sdougm count_security(sa_optionset_t opts)
11626185db85Sdougm {
11636185db85Sdougm 	int count = 0;
11646185db85Sdougm 	sa_property_t prop;
11656185db85Sdougm 	if (opts != NULL) {
1166a3351425Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
1167a3351425Sdougm 		    prop = sa_get_next_property(prop)) {
1168a3351425Sdougm 			count++;
1169a3351425Sdougm 		}
11706185db85Sdougm 	}
11716185db85Sdougm 	return (count);
11726185db85Sdougm }
11736185db85Sdougm 
11746185db85Sdougm /*
11756185db85Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
11766185db85Sdougm  *
11776185db85Sdougm  * provides a mechanism to format NFS properties into legacy output
11786185db85Sdougm  * format. If the buffer would overflow, it is reallocated and grown
11796185db85Sdougm  * as appropriate. Special cases of converting internal form of values
11806185db85Sdougm  * to those used by "share" are done. this function does one property
11816185db85Sdougm  * at a time.
11826185db85Sdougm  */
11836185db85Sdougm 
11846185db85Sdougm static void
11856185db85Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
11866185db85Sdougm 			sa_property_t prop, int sep)
11876185db85Sdougm {
11886185db85Sdougm 	char *name;
11896185db85Sdougm 	char *value;
11906185db85Sdougm 	int curlen;
11916185db85Sdougm 	char *buff = *rbuff;
11926185db85Sdougm 	size_t buffsize = *rbuffsize;
11936185db85Sdougm 
11946185db85Sdougm 	name = sa_get_property_attr(prop, "type");
11956185db85Sdougm 	value = sa_get_property_attr(prop, "value");
11966185db85Sdougm 	if (buff != NULL)
1197a3351425Sdougm 		curlen = strlen(buff);
11986185db85Sdougm 	else
1199a3351425Sdougm 		curlen = 0;
12006185db85Sdougm 	if (name != NULL) {
1201a3351425Sdougm 		int len;
1202a3351425Sdougm 		len = strlen(name) + sep;
12036185db85Sdougm 
12046185db85Sdougm 		/*
12056185db85Sdougm 		 * A future RFE would be to replace this with more
12066185db85Sdougm 		 * generic code and to possibly handle more types.
12076185db85Sdougm 		 */
1208a3351425Sdougm 		switch (gettype(name)) {
1209a3351425Sdougm 		case OPT_TYPE_BOOLEAN:
1210a3351425Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
1211a3351425Sdougm 				*name = '\0';
1212a3351425Sdougm 			if (value != NULL)
1213a3351425Sdougm 				sa_free_attr_string(value);
1214a3351425Sdougm 			value = NULL;
1215a3351425Sdougm 			break;
1216a3351425Sdougm 		case OPT_TYPE_ACCLIST:
1217a3351425Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
1218a3351425Sdougm 				sa_free_attr_string(value);
1219a3351425Sdougm 				value = NULL;
1220a3351425Sdougm 			} else {
1221a3351425Sdougm 				if (value != NULL)
1222a3351425Sdougm 					len += 1 + strlen(value);
1223a3351425Sdougm 			}
1224a3351425Sdougm 			break;
1225a3351425Sdougm 		case OPT_TYPE_LOGTAG:
1226a3351425Sdougm 			if (value != NULL && strlen(value) == 0) {
1227a3351425Sdougm 				sa_free_attr_string(value);
1228a3351425Sdougm 				value = NULL;
1229a3351425Sdougm 			} else {
1230a3351425Sdougm 				if (value != NULL)
1231a3351425Sdougm 					len += 1 + strlen(value);
1232a3351425Sdougm 			}
1233a3351425Sdougm 			break;
1234a3351425Sdougm 		default:
1235a3351425Sdougm 			if (value != NULL)
1236a3351425Sdougm 				len += 1 + strlen(value);
1237a3351425Sdougm 			break;
12386185db85Sdougm 		}
1239a3351425Sdougm 		while (buffsize <= (curlen + len)) {
1240a3351425Sdougm 			/* need more room */
1241a3351425Sdougm 			buffsize += incr;
1242a3351425Sdougm 			buff = realloc(buff, buffsize);
1243a3351425Sdougm 			if (buff == NULL) {
1244a3351425Sdougm 				/* realloc failed so free everything */
1245a3351425Sdougm 				if (*rbuff != NULL)
1246a3351425Sdougm 					free(*rbuff);
1247a3351425Sdougm 			}
1248a3351425Sdougm 			*rbuff = buff;
1249a3351425Sdougm 			*rbuffsize = buffsize;
1250a3351425Sdougm 			if (buff == NULL) {
1251a3351425Sdougm 				return;
1252a3351425Sdougm 			}
12536185db85Sdougm 		}
1254a3351425Sdougm 		if (buff == NULL)
1255a3351425Sdougm 			return;
1256a3351425Sdougm 		if (value == NULL) {
1257a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1258a3351425Sdougm 			    "%s%s", sep ? "," : "",
1259a3351425Sdougm 			    name, value != NULL ? value : "");
12606185db85Sdougm 		} else {
1261a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1262a3351425Sdougm 			    "%s%s=%s", sep ? "," : "",
1263a3351425Sdougm 			    name, value != NULL ? value : "");
12646185db85Sdougm 		}
12656185db85Sdougm 	}
12666185db85Sdougm 	if (name != NULL)
1267a3351425Sdougm 		sa_free_attr_string(name);
12686185db85Sdougm 	if (value != NULL)
1269a3351425Sdougm 		sa_free_attr_string(value);
12706185db85Sdougm }
12716185db85Sdougm 
12726185db85Sdougm /*
12736185db85Sdougm  * nfs_format_options(group, hier)
12746185db85Sdougm  *
12756185db85Sdougm  * format all the options on the group into an old-style option
12766185db85Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
12776185db85Sdougm  * options.
12786185db85Sdougm  */
12796185db85Sdougm 
12806185db85Sdougm static char *
12816185db85Sdougm nfs_format_options(sa_group_t group, int hier)
12826185db85Sdougm {
12836185db85Sdougm 	sa_optionset_t options = NULL;
1284a3351425Sdougm 	sa_optionset_t secoptions = NULL;
12856185db85Sdougm 	sa_property_t prop, secprop;
1286a3351425Sdougm 	sa_security_t security = NULL;
12876185db85Sdougm 	char *buff;
12886185db85Sdougm 	size_t buffsize;
1289a3351425Sdougm 	char *sectype = NULL;
1290a3351425Sdougm 	int sep = 0;
1291a3351425Sdougm 
1292a3351425Sdougm 
1293a3351425Sdougm 	buff = malloc(OPT_CHUNK);
1294a3351425Sdougm 	if (buff == NULL) {
1295a3351425Sdougm 		return (NULL);
1296a3351425Sdougm 	}
1297a3351425Sdougm 
1298a3351425Sdougm 	buff[0] = '\0';
1299a3351425Sdougm 	buffsize = OPT_CHUNK;
1300a3351425Sdougm 
1301a3351425Sdougm 	/*
1302a3351425Sdougm 	 * We may have a an optionset relative to this item. format
1303a3351425Sdougm 	 * these if we find them and then add any security definitions.
1304a3351425Sdougm 	 */
13056185db85Sdougm 
13066185db85Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
13076185db85Sdougm 
13086185db85Sdougm 	/*
1309a3351425Sdougm 	 * do the default set first but skip any option that is also
1310a3351425Sdougm 	 * in the protocol specific optionset.
13116185db85Sdougm 	 */
1312a3351425Sdougm 	if (options != NULL) {
1313a3351425Sdougm 		for (prop = sa_get_property(options, NULL);
1314a3351425Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
13156185db85Sdougm 			/*
1316a3351425Sdougm 			 * use this one since we skipped any
1317a3351425Sdougm 			 * of these that were also in
1318a3351425Sdougm 			 * optdefault
13196185db85Sdougm 			 */
1320a3351425Sdougm 			nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1321a3351425Sdougm 			    prop, sep);
1322a3351425Sdougm 			if (buff == NULL) {
1323a3351425Sdougm 				/*
1324a3351425Sdougm 				 * buff could become NULL if there
1325a3351425Sdougm 				 * isn't enough memory for
1326a3351425Sdougm 				 * nfs_sprint_option to realloc()
1327a3351425Sdougm 				 * as necessary. We can't really
1328a3351425Sdougm 				 * do anything about it at this
1329a3351425Sdougm 				 * point so we return NULL.  The
1330a3351425Sdougm 				 * caller should handle the
1331a3351425Sdougm 				 * failure.
1332a3351425Sdougm 				 */
13336185db85Sdougm 				if (options != NULL)
1334a3351425Sdougm 					sa_free_derived_optionset(
1335a3351425Sdougm 					    options);
13366185db85Sdougm 				return (buff);
13376185db85Sdougm 			}
1338a3351425Sdougm 			sep = 1;
1339a3351425Sdougm 		}
1340a3351425Sdougm 	}
1341a3351425Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1342a3351425Sdougm 	    "nfs", hier);
1343a3351425Sdougm 	if (secoptions != NULL) {
1344a3351425Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
1345a3351425Sdougm 		    secprop != NULL;
1346a3351425Sdougm 		    secprop = sa_get_next_property(secprop)) {
1347a3351425Sdougm 			sectype = sa_get_property_attr(secprop, "type");
1348a3351425Sdougm 			security =
1349a3351425Sdougm 			    (sa_security_t)sa_get_derived_security(
1350a3351425Sdougm 			    group, sectype, "nfs", hier);
1351a3351425Sdougm 			if (security != NULL) {
1352a3351425Sdougm 				if (sectype != NULL) {
1353a3351425Sdougm 					prop = sa_create_property(
1354a3351425Sdougm 					    "sec", sectype);
1355a3351425Sdougm 					nfs_sprint_option(&buff,
1356a3351425Sdougm 					    &buffsize, OPT_CHUNK,
1357a3351425Sdougm 					    prop, sep);
1358a3351425Sdougm 					(void) sa_remove_property(prop);
1359a3351425Sdougm 					sep = 1;
1360a3351425Sdougm 				}
1361a3351425Sdougm 				for (prop = sa_get_property(security,
1362a3351425Sdougm 				    NULL); prop != NULL;
1363a3351425Sdougm 				    prop = sa_get_next_property(prop)) {
1364a3351425Sdougm 					nfs_sprint_option(&buff,
1365a3351425Sdougm 					    &buffsize, OPT_CHUNK, prop,
1366a3351425Sdougm 					    sep);
1367a3351425Sdougm 					if (buff == NULL)
1368a3351425Sdougm 						goto err;
1369a3351425Sdougm 					sep = 1;
1370a3351425Sdougm 				}
1371a3351425Sdougm 				sa_free_derived_optionset(security);
1372a3351425Sdougm 			}
1373a3351425Sdougm 			if (sectype != NULL)
1374a3351425Sdougm 				sa_free_attr_string(sectype);
13756185db85Sdougm 		}
13766185db85Sdougm 		sa_free_derived_optionset(secoptions);
13776185db85Sdougm 	}
1378a3351425Sdougm 
13796185db85Sdougm 	if (options != NULL)
1380a3351425Sdougm 		sa_free_derived_optionset(options);
1381a3351425Sdougm 	return (buff);
1382a3351425Sdougm 
1383a3351425Sdougm err:
1384a3351425Sdougm 	/*
1385a3351425Sdougm 	 * If we couldn't allocate memory for option printing, we need
1386a3351425Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
1387a3351425Sdougm 	 */
1388a3351425Sdougm 	if (secoptions != NULL)
1389a3351425Sdougm 		sa_free_derived_optionset(secoptions);
1390a3351425Sdougm 	if (security != NULL)
1391a3351425Sdougm 		sa_free_derived_optionset(security);
1392a3351425Sdougm 	if (sectype != NULL)
1393a3351425Sdougm 		sa_free_attr_string(sectype);
1394a3351425Sdougm 	if (options != NULL)
1395a3351425Sdougm 		sa_free_derived_optionset(options);
13966185db85Sdougm 	return (buff);
13976185db85Sdougm }
1398a3351425Sdougm 
13996185db85Sdougm /*
14006185db85Sdougm  * Append an entry to the nfslogtab file
14016185db85Sdougm  */
14026185db85Sdougm static int
14036185db85Sdougm nfslogtab_add(dir, buffer, tag)
14046185db85Sdougm 	char *dir, *buffer, *tag;
14056185db85Sdougm {
14066185db85Sdougm 	FILE *f;
14076185db85Sdougm 	struct logtab_ent lep;
14086185db85Sdougm 	int error = 0;
14096185db85Sdougm 
14106185db85Sdougm 	/*
14116185db85Sdougm 	 * Open the file for update and create it if necessary.
14126185db85Sdougm 	 * This may leave the I/O offset at the end of the file,
14136185db85Sdougm 	 * so rewind back to the beginning of the file.
14146185db85Sdougm 	 */
14156185db85Sdougm 	f = fopen(NFSLOGTAB, "a+");
14166185db85Sdougm 	if (f == NULL) {
14176185db85Sdougm 		error = errno;
14186185db85Sdougm 		goto out;
14196185db85Sdougm 	}
14206185db85Sdougm 	rewind(f);
14216185db85Sdougm 
14226185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1423549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1424a3351425Sdougm 		    "share complete, however failed to lock %s "
1425a3351425Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
14266185db85Sdougm 		error = -1;
14276185db85Sdougm 		goto out;
14286185db85Sdougm 	}
14296185db85Sdougm 
14306185db85Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
1431549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1432a3351425Sdougm 		    "share complete, however could not deactivate "
1433a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14346185db85Sdougm 		error = -1;
14356185db85Sdougm 		goto out;
14366185db85Sdougm 	}
14376185db85Sdougm 
14386185db85Sdougm 	/*
14396185db85Sdougm 	 * Remove entries matching buffer and sharepoint since we're
14406185db85Sdougm 	 * going to replace it with perhaps an entry with a new tag.
14416185db85Sdougm 	 */
14426185db85Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
1443549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1444a3351425Sdougm 		    "share complete, however could not remove matching "
1445a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14466185db85Sdougm 		error = -1;
14476185db85Sdougm 		goto out;
14486185db85Sdougm 	}
14496185db85Sdougm 
14506185db85Sdougm 	/*
14516185db85Sdougm 	 * Deactivate all active entries matching this sharepoint
14526185db85Sdougm 	 */
14536185db85Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
1454549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1455a3351425Sdougm 		    "share complete, however could not deactivate matching "
1456a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
14576185db85Sdougm 		error = -1;
14586185db85Sdougm 		goto out;
14596185db85Sdougm 	}
14606185db85Sdougm 
14616185db85Sdougm 	lep.le_buffer = buffer;
14626185db85Sdougm 	lep.le_path = dir;
14636185db85Sdougm 	lep.le_tag = tag;
14646185db85Sdougm 	lep.le_state = LES_ACTIVE;
14656185db85Sdougm 
14666185db85Sdougm 	/*
14676185db85Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
14686185db85Sdougm 	 */
14696185db85Sdougm 	if (logtab_putent(f, &lep) < 0) {
1470549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1471a3351425Sdougm 		    "share complete, however could not add %s to %s\n"),
1472a3351425Sdougm 		    dir, NFSLOGTAB);
14736185db85Sdougm 		error = -1;
14746185db85Sdougm 	}
14756185db85Sdougm 
14766185db85Sdougm out:
14776185db85Sdougm 	if (f != NULL)
14786185db85Sdougm 		(void) fclose(f);
14796185db85Sdougm 	return (error);
14806185db85Sdougm }
14816185db85Sdougm 
14826185db85Sdougm /*
14836185db85Sdougm  * Deactivate an entry from the nfslogtab file
14846185db85Sdougm  */
14856185db85Sdougm static int
14866185db85Sdougm nfslogtab_deactivate(path)
14876185db85Sdougm 	char *path;
14886185db85Sdougm {
14896185db85Sdougm 	FILE *f;
14906185db85Sdougm 	int error = 0;
14916185db85Sdougm 
14926185db85Sdougm 	f = fopen(NFSLOGTAB, "r+");
14936185db85Sdougm 	if (f == NULL) {
14946185db85Sdougm 		error = errno;
14956185db85Sdougm 		goto out;
14966185db85Sdougm 	}
14976185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
14986185db85Sdougm 		error = errno;
1499549ec3ffSdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
1500a3351425Sdougm 		    "share complete, however could not lock %s for "
1501a3351425Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
15026185db85Sdougm 		goto out;
15036185db85Sdougm 	}
15046185db85Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
15056185db85Sdougm 		error = -1;
15066185db85Sdougm 		(void) fprintf(stderr,
1507a3351425Sdougm 		    dgettext(TEXT_DOMAIN,
1508a3351425Sdougm 		    "share complete, however could not "
1509a3351425Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
15106185db85Sdougm 		goto out;
15116185db85Sdougm 	}
15126185db85Sdougm 
15136185db85Sdougm out:	if (f != NULL)
15146185db85Sdougm 		(void) fclose(f);
15156185db85Sdougm 
15166185db85Sdougm 	return (error);
15176185db85Sdougm }
15186185db85Sdougm 
15196185db85Sdougm /*
15206185db85Sdougm  * public_exists(share)
15216185db85Sdougm  *
15226185db85Sdougm  * check to see if public option is set on any other share than the
15236185db85Sdougm  * one specified.
15246185db85Sdougm  */
15256185db85Sdougm static int
15266185db85Sdougm public_exists(sa_share_t skipshare)
15276185db85Sdougm {
15286185db85Sdougm 	sa_share_t share;
15296185db85Sdougm 	sa_group_t group;
15306185db85Sdougm 	sa_optionset_t opt;
15316185db85Sdougm 	sa_property_t prop;
15326185db85Sdougm 	int exists = 0;
1533549ec3ffSdougm 	sa_handle_t handle;
15346185db85Sdougm 
1535549ec3ffSdougm 	group = sa_get_parent_group(skipshare);
1536549ec3ffSdougm 	if (group == NULL)
1537a3351425Sdougm 		return (SA_NO_SUCH_GROUP);
1538549ec3ffSdougm 
1539549ec3ffSdougm 	handle = sa_find_group_handle(group);
1540549ec3ffSdougm 	if (handle == NULL)
1541a3351425Sdougm 		return (SA_SYSTEM_ERR);
1542549ec3ffSdougm 
1543549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
15446185db85Sdougm 	    group = sa_get_next_group(group)) {
1545a3351425Sdougm 		for (share = sa_get_share(group, NULL); share != NULL;
1546a3351425Sdougm 		    share = sa_get_next_share(share)) {
1547a3351425Sdougm 			if (share == skipshare)
1548a3351425Sdougm 				continue;
1549a3351425Sdougm 			opt = sa_get_optionset(share, "nfs");
1550a3351425Sdougm 			if (opt != NULL) {
1551a3351425Sdougm 				prop = sa_get_property(opt, "public");
1552a3351425Sdougm 				if (prop != NULL) {
1553a3351425Sdougm 					char *shared;
1554a3351425Sdougm 					shared = sa_get_share_attr(share,
1555a3351425Sdougm 					    "shared");
1556a3351425Sdougm 					if (shared != NULL) {
1557a3351425Sdougm 						exists = strcmp(shared,
1558a3351425Sdougm 						    "true") == 0;
1559a3351425Sdougm 						sa_free_attr_string(shared);
1560a3351425Sdougm 						goto out;
1561a3351425Sdougm 					}
1562a3351425Sdougm 				}
15636185db85Sdougm 			}
15646185db85Sdougm 		}
15656185db85Sdougm 	}
15666185db85Sdougm out:
15676185db85Sdougm 	return (exists);
15686185db85Sdougm }
15696185db85Sdougm 
15706185db85Sdougm /*
15716185db85Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
15726185db85Sdougm  * implementation that it is to enable the share. This entails
15736185db85Sdougm  * converting the path and options into the appropriate ioctl
15746185db85Sdougm  * calls. It is assumed that all error checking of paths, etc. were
15756185db85Sdougm  * done earlier.
15766185db85Sdougm  */
15776185db85Sdougm static int
15786185db85Sdougm nfs_enable_share(sa_share_t share)
15796185db85Sdougm {
15806185db85Sdougm 	struct exportdata export;
15816185db85Sdougm 	sa_optionset_t secoptlist;
15826185db85Sdougm 	struct secinfo *sp;
15836185db85Sdougm 	int num_secinfo;
15846185db85Sdougm 	sa_optionset_t opt;
15856185db85Sdougm 	sa_security_t sec;
15866185db85Sdougm 	sa_property_t prop;
15876185db85Sdougm 	char *path;
15886185db85Sdougm 	int err = SA_OK;
15896185db85Sdougm 
15906185db85Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
15916185db85Sdougm 	(void) signal(SIGSYS, SIG_IGN);
15926185db85Sdougm 
15936185db85Sdougm 	/* get the path since it is important in several places */
15946185db85Sdougm 	path = sa_get_share_attr(share, "path");
15956185db85Sdougm 	if (path == NULL)
1596a3351425Sdougm 		return (SA_NO_SUCH_PATH);
15976185db85Sdougm 
15986185db85Sdougm 	/*
15996185db85Sdougm 	 * find the optionsets and security sets.  There may not be
16006185db85Sdougm 	 * any or there could be one or two for each of optionset and
16016185db85Sdougm 	 * security may have multiple, one per security type per
16026185db85Sdougm 	 * protocol type.
16036185db85Sdougm 	 */
16046185db85Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
16056185db85Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
16066185db85Sdougm 	if (secoptlist != NULL)
1607a3351425Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
16086185db85Sdougm 	else
1609a3351425Sdougm 		num_secinfo = 1;
16106185db85Sdougm 
16116185db85Sdougm 	/*
16126185db85Sdougm 	 * walk through the options and fill in the structure
16136185db85Sdougm 	 * appropriately.
16146185db85Sdougm 	 */
16156185db85Sdougm 
16166185db85Sdougm 	(void) memset(&export, '\0', sizeof (export));
16176185db85Sdougm 
16186185db85Sdougm 	/*
16196185db85Sdougm 	 * do non-security options first since there is only one after
16206185db85Sdougm 	 * the derived group is constructed.
16216185db85Sdougm 	 */
16226185db85Sdougm 	export.ex_version = EX_CURRENT_VERSION;
16236185db85Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
16246185db85Sdougm 	export.ex_index = NULL;
16256185db85Sdougm 	export.ex_path = path;
16266185db85Sdougm 	export.ex_pathlen = strlen(path) + 1;
16276185db85Sdougm 
16286185db85Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
16296185db85Sdougm 
16306185db85Sdougm 	if (opt != NULL)
1631a3351425Sdougm 		err = fill_export_from_optionset(&export, opt);
16326185db85Sdougm 
16336185db85Sdougm 	/*
16346185db85Sdougm 	 * check to see if "public" is set. If it is, then make sure
16356185db85Sdougm 	 * no other share has it set. If it is already used, fail.
16366185db85Sdougm 	 */
16376185db85Sdougm 
16386185db85Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(share)) {
1639a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1640a3351425Sdougm 		    "NFS: Cannot share more than one file "
1641a3351425Sdougm 		    "system with 'public' property\n"));
1642a3351425Sdougm 		err = SA_NOT_ALLOWED;
1643a3351425Sdougm 		goto out;
16446185db85Sdougm 	}
16456185db85Sdougm 
16466185db85Sdougm 	if (sp == NULL) {
16476185db85Sdougm 		/* failed to alloc memory */
1648a3351425Sdougm 		(void) printf("NFS: no memory for security\n");
1649a3351425Sdougm 		err = SA_NO_MEMORY;
16506185db85Sdougm 	} else {
1651a3351425Sdougm 		int i;
1652a3351425Sdougm 		export.ex_secinfo = sp;
1653a3351425Sdougm 		/* get default secinfo */
1654a3351425Sdougm 		export.ex_seccnt = num_secinfo;
16556185db85Sdougm 		/*
16566185db85Sdougm 		 * since we must have one security option defined, we
16576185db85Sdougm 		 * init to the default and then override as we find
16586185db85Sdougm 		 * defined security options. This handles the case
16596185db85Sdougm 		 * where we have no defined options but we need to set
16606185db85Sdougm 		 * up one.
16616185db85Sdougm 		 */
1662a3351425Sdougm 		sp[0].s_window = DEF_WIN;
1663a3351425Sdougm 		sp[0].s_rootnames = NULL;
1664a3351425Sdougm 		/* setup a default in case no properties defined */
1665a3351425Sdougm 		if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1666a3351425Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1667a3351425Sdougm 			    "NFS: nfs_getseconfig_default: failed to "
1668a3351425Sdougm 			    "get default security mode\n"));
1669a3351425Sdougm 			err = SA_CONFIG_ERR;
1670a3351425Sdougm 		}
1671a3351425Sdougm 		if (secoptlist != NULL) {
1672a3351425Sdougm 			for (i = 0, prop = sa_get_property(secoptlist, NULL);
1673a3351425Sdougm 			    prop != NULL && i < num_secinfo;
1674a3351425Sdougm 			    prop = sa_get_next_property(prop), i++) {
1675a3351425Sdougm 				char *sectype;
1676a3351425Sdougm 
1677a3351425Sdougm 				sectype = sa_get_property_attr(prop, "type");
1678a3351425Sdougm 				/*
1679a3351425Sdougm 				 * if sectype is NULL, we probably
1680a3351425Sdougm 				 * have a memory problem and can't get
1681a3351425Sdougm 				 * the correct values. Rather than
1682a3351425Sdougm 				 * exporting with incorrect security,
1683a3351425Sdougm 				 * don't share it.
1684a3351425Sdougm 				 */
1685a3351425Sdougm 				if (sectype == NULL) {
1686a3351425Sdougm 					err = SA_NO_MEMORY;
1687a3351425Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
1688a3351425Sdougm 					    "NFS: Cannot share %s: "
1689a3351425Sdougm 					    "no memory\n"), path);
1690a3351425Sdougm 					goto out;
1691a3351425Sdougm 				}
1692a3351425Sdougm 				sec = (sa_security_t)sa_get_derived_security(
1693a3351425Sdougm 				    share, sectype, "nfs", 1);
1694a3351425Sdougm 				sp[i].s_window = DEF_WIN;
1695a3351425Sdougm 				sp[i].s_rootcnt = 0;
1696a3351425Sdougm 				sp[i].s_rootnames = NULL;
1697a3351425Sdougm 
1698a3351425Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
1699a3351425Sdougm 				if (sec != NULL)
1700a3351425Sdougm 					sa_free_derived_security(sec);
1701a3351425Sdougm 				if (sectype != NULL)
1702a3351425Sdougm 					sa_free_attr_string(sectype);
1703a3351425Sdougm 			}
17046185db85Sdougm 		}
17056185db85Sdougm 		/*
17066185db85Sdougm 		 * when we get here, we can do the exportfs system call and
17076185db85Sdougm 		 * initiate thinsg. We probably want to enable the nfs.server
17086185db85Sdougm 		 * service first if it isn't running within SMF.
17096185db85Sdougm 		 */
17106185db85Sdougm 		/* check nfs.server status and start if needed */
17116185db85Sdougm 
17126185db85Sdougm 		/* now add the share to the internal tables */
1713a3351425Sdougm 		printarg(path, &export);
17146185db85Sdougm 		/*
17156185db85Sdougm 		 * call the exportfs system call which is implemented
17166185db85Sdougm 		 * via the nfssys() call as the EXPORTFS subfunction.
17176185db85Sdougm 		 */
1718a3351425Sdougm 		if ((err = exportfs(path, &export)) < 0) {
1719a3351425Sdougm 			err = SA_SYSTEM_ERR;
1720a3351425Sdougm 			switch (errno) {
1721a3351425Sdougm 			case EREMOTE:
1722a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1723a3351425Sdougm 				    "NFS: Cannot share remote "
1724a3351425Sdougm 				    "filesystem: %s\n"), path);
1725a3351425Sdougm 				break;
1726a3351425Sdougm 			case EPERM:
1727a3351425Sdougm 				if (getzoneid() != GLOBAL_ZONEID) {
1728a3351425Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
1729a3351425Sdougm 					    "NFS: Cannot share filesystems "
1730a3351425Sdougm 					    "in non-global zones: %s\n"), path);
1731a3351425Sdougm 					err = SA_NOT_SUPPORTED;
1732a3351425Sdougm 					break;
1733a3351425Sdougm 				}
1734a3351425Sdougm 				err = SA_NO_PERMISSION;
1735a3351425Sdougm 				/* FALLTHROUGH */
1736a3351425Sdougm 			default:
1737a3351425Sdougm 				break;
1738a3351425Sdougm 			}
1739a3351425Sdougm 		} else {
1740a3351425Sdougm 			/* update sharetab with an add/modify */
1741a3351425Sdougm 			(void) sa_update_sharetab(share, "nfs");
17426185db85Sdougm 		}
17436185db85Sdougm 	}
17446185db85Sdougm 
17456185db85Sdougm 	if (err == SA_OK) {
17466185db85Sdougm 		/*
17476185db85Sdougm 		 * enable services as needed. This should probably be
17486185db85Sdougm 		 * done elsewhere in order to minimize the calls to
17496185db85Sdougm 		 * check services.
17506185db85Sdougm 		 */
17516185db85Sdougm 		/*
17526185db85Sdougm 		 * check to see if logging and other services need to
17536185db85Sdougm 		 * be triggered, but only if there wasn't an
17546185db85Sdougm 		 * error. This is probably where sharetab should be
17556185db85Sdougm 		 * updated with the NFS specific entry.
17566185db85Sdougm 		 */
1757a3351425Sdougm 		if (export.ex_flags & EX_LOG) {
1758a3351425Sdougm 			/* enable logging */
1759a3351425Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
1760a3351425Sdougm 			    export.ex_tag) != 0) {
1761a3351425Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1762a3351425Sdougm 				    "Could not enable logging for %s\n"),
1763a3351425Sdougm 				    path);
1764a3351425Sdougm 			}
1765a3351425Sdougm 			_check_services(service_list_logging);
1766a3351425Sdougm 		} else {
1767a3351425Sdougm 			/*
1768a3351425Sdougm 			 * don't have logging so remove it from file. It might
1769a3351425Sdougm 			 * not be thre, but that doesn't matter.
1770a3351425Sdougm 			 */
1771a3351425Sdougm 			(void) nfslogtab_deactivate(path);
1772a3351425Sdougm 			_check_services(service_list_default);
17736185db85Sdougm 		}
17746185db85Sdougm 	}
17756185db85Sdougm 
17766185db85Sdougm out:
17776185db85Sdougm 	if (path != NULL)
1778a3351425Sdougm 		free(path);
17796185db85Sdougm 
17806185db85Sdougm 	cleanup_export(&export);
17816185db85Sdougm 	if (opt != NULL)
1782a3351425Sdougm 		sa_free_derived_optionset(opt);
17836185db85Sdougm 	if (secoptlist != NULL)
1784a3351425Sdougm 		(void) sa_destroy_optionset(secoptlist);
17856185db85Sdougm 	return (err);
17866185db85Sdougm }
17876185db85Sdougm 
17886185db85Sdougm /*
17896185db85Sdougm  * nfs_disable_share(share)
17906185db85Sdougm  *
17916185db85Sdougm  * Unshare the specified share.  How much error checking should be
17926185db85Sdougm  * done? We only do basic errors for now.
17936185db85Sdougm  */
17946185db85Sdougm static int
17956185db85Sdougm nfs_disable_share(char *share)
17966185db85Sdougm {
17976185db85Sdougm 	int err;
17986185db85Sdougm 	int ret = SA_OK;
17996185db85Sdougm 
18006185db85Sdougm 	if (share != NULL) {
1801a3351425Sdougm 		err = exportfs(share, NULL);
1802a3351425Sdougm 		if (err < 0) {
1803a3351425Sdougm 			/*
1804a3351425Sdougm 			 * TBD: only an error in some cases - need
1805a3351425Sdougm 			 * better analysis
1806a3351425Sdougm 			 */
1807a3351425Sdougm 			switch (errno) {
1808a3351425Sdougm 			case EPERM:
1809a3351425Sdougm 			case EACCES:
1810a3351425Sdougm 				ret = SA_NO_PERMISSION;
1811a3351425Sdougm 				if (getzoneid() != GLOBAL_ZONEID)
1812a3351425Sdougm 					ret = SA_NOT_SUPPORTED;
1813a3351425Sdougm 				break;
1814a3351425Sdougm 			case EINVAL:
1815a3351425Sdougm 			case ENOENT:
1816a3351425Sdougm 				ret = SA_NO_SUCH_PATH;
1817a3351425Sdougm 				break;
1818a3351425Sdougm 			default:
1819a3351425Sdougm 				ret = SA_SYSTEM_ERR;
1820a3351425Sdougm 				break;
1821a3351425Sdougm 			}
1822a3351425Sdougm 		}
1823a3351425Sdougm 		if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
1824a3351425Sdougm 			(void) sa_delete_sharetab(share, "nfs");
1825a3351425Sdougm 			/* just in case it was logged */
1826a3351425Sdougm 			(void) nfslogtab_deactivate(share);
18276185db85Sdougm 		}
18286185db85Sdougm 	}
18296185db85Sdougm 	return (ret);
18306185db85Sdougm }
18316185db85Sdougm 
18326185db85Sdougm /*
18336185db85Sdougm  * check ro vs rw values.  Over time this may get beefed up.
18346185db85Sdougm  * for now it just does simple checks.
18356185db85Sdougm  */
18366185db85Sdougm 
18376185db85Sdougm static int
18386185db85Sdougm check_rorw(char *v1, char *v2)
18396185db85Sdougm {
18406185db85Sdougm 	int ret = SA_OK;
18416185db85Sdougm 	if (strcmp(v1, v2) == 0)
1842a3351425Sdougm 		ret = SA_VALUE_CONFLICT;
18436185db85Sdougm 	return (ret);
18446185db85Sdougm }
18456185db85Sdougm 
18466185db85Sdougm /*
18476185db85Sdougm  * nfs_validate_property(property, parent)
18486185db85Sdougm  *
18496185db85Sdougm  * Check that the property has a legitimate value for its type.
18506185db85Sdougm  */
18516185db85Sdougm 
18526185db85Sdougm static int
18536185db85Sdougm nfs_validate_property(sa_property_t property, sa_optionset_t parent)
18546185db85Sdougm {
18556185db85Sdougm 	int ret = SA_OK;
18566185db85Sdougm 	char *propname;
18576185db85Sdougm 	char *other;
18586185db85Sdougm 	int optindex;
18596185db85Sdougm 	nfsl_config_t *configlist;
18606185db85Sdougm 	sa_group_t parent_group;
18616185db85Sdougm 	char *value;
18626185db85Sdougm 
18636185db85Sdougm 	propname = sa_get_property_attr(property, "type");
18646185db85Sdougm 
18656185db85Sdougm 	if ((optindex = findopt(propname)) < 0)
1866a3351425Sdougm 		ret = SA_NO_SUCH_PROP;
18676185db85Sdougm 
18686185db85Sdougm 	/* need to validate value range here as well */
18696185db85Sdougm 
18706185db85Sdougm 	if (ret == SA_OK) {
1871a3351425Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
1872a3351425Sdougm 		if (optdefs[optindex].share && !sa_is_share(parent_group))
1873a3351425Sdougm 			ret = SA_PROP_SHARE_ONLY;
18746185db85Sdougm 	}
18756185db85Sdougm 	if (ret == SA_OK) {
1876a3351425Sdougm 		value = sa_get_property_attr(property, "value");
1877a3351425Sdougm 		if (value != NULL) {
1878a3351425Sdougm 			/* first basic type checking */
1879a3351425Sdougm 			switch (optdefs[optindex].type) {
1880a3351425Sdougm 			case OPT_TYPE_NUMBER:
1881a3351425Sdougm 				/* check that the value is all digits */
1882a3351425Sdougm 				if (!is_a_number(value))
1883a3351425Sdougm 					ret = SA_BAD_VALUE;
1884a3351425Sdougm 				break;
1885a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
1886a3351425Sdougm 				if (strlen(value) == 0 ||
1887a3351425Sdougm 				    strcasecmp(value, "true") == 0 ||
1888a3351425Sdougm 				    strcmp(value, "1") == 0 ||
1889a3351425Sdougm 				    strcasecmp(value, "false") == 0 ||
1890a3351425Sdougm 				    strcmp(value, "0") == 0) {
1891a3351425Sdougm 					ret = SA_OK;
1892a3351425Sdougm 				} else {
1893a3351425Sdougm 					ret = SA_BAD_VALUE;
1894a3351425Sdougm 				}
1895a3351425Sdougm 				break;
1896a3351425Sdougm 			case OPT_TYPE_USER:
1897a3351425Sdougm 				if (!is_a_number(value)) {
1898a3351425Sdougm 					struct passwd *pw;
1899a3351425Sdougm 					/*
1900a3351425Sdougm 					 * in this case it would have to be a
1901a3351425Sdougm 					 * user name
1902a3351425Sdougm 					 */
1903a3351425Sdougm 					pw = getpwnam(value);
1904a3351425Sdougm 					if (pw == NULL)
1905a3351425Sdougm 						ret = SA_BAD_VALUE;
1906a3351425Sdougm 					endpwent();
1907a3351425Sdougm 				} else {
1908a3351425Sdougm 					uint64_t intval;
1909a3351425Sdougm 					intval = strtoull(value, NULL, 0);
1910a3351425Sdougm 					if (intval > UID_MAX && intval != ~0)
1911a3351425Sdougm 						ret = SA_BAD_VALUE;
1912a3351425Sdougm 				}
1913a3351425Sdougm 				break;
1914a3351425Sdougm 			case OPT_TYPE_FILE:
1915a3351425Sdougm 				if (strcmp(value, "..") == 0 ||
1916a3351425Sdougm 				    strchr(value, '/') != NULL) {
1917a3351425Sdougm 					ret = SA_BAD_VALUE;
1918a3351425Sdougm 				}
1919a3351425Sdougm 				break;
1920a3351425Sdougm 			case OPT_TYPE_ACCLIST:
1921a3351425Sdougm 				/*
1922a3351425Sdougm 				 * access list handling. Should eventually
1923a3351425Sdougm 				 * validate that all the values make sense.
1924a3351425Sdougm 				 * Also, ro and rw may have cross value
1925a3351425Sdougm 				 * conflicts.
1926a3351425Sdougm 				 */
1927a3351425Sdougm 				if (strcmp(propname, SHOPT_RO) == 0)
1928a3351425Sdougm 					other = SHOPT_RW;
1929a3351425Sdougm 				else if (strcmp(propname, SHOPT_RW) == 0)
1930a3351425Sdougm 					other = SHOPT_RO;
1931a3351425Sdougm 				else
1932a3351425Sdougm 					other = NULL;
1933a3351425Sdougm 
1934a3351425Sdougm 				if (other != NULL && parent != NULL) {
1935a3351425Sdougm 					/* compare rw(ro) with ro(rw) */
1936a3351425Sdougm 					sa_property_t oprop;
1937a3351425Sdougm 					oprop = sa_get_property(parent, other);
1938a3351425Sdougm 					if (oprop != NULL) {
1939a3351425Sdougm 						/*
1940a3351425Sdougm 						 * only potential
1941a3351425Sdougm 						 * confusion if other
1942a3351425Sdougm 						 * exists
1943a3351425Sdougm 						 */
1944a3351425Sdougm 						char *ovalue;
1945a3351425Sdougm 						ovalue = sa_get_property_attr(
1946a3351425Sdougm 						    oprop, "value");
1947a3351425Sdougm 						if (ovalue != NULL) {
1948a3351425Sdougm 							ret = check_rorw(value,
1949a3351425Sdougm 							    ovalue);
1950a3351425Sdougm 							sa_free_attr_string(
1951a3351425Sdougm 							    ovalue);
1952a3351425Sdougm 						}
1953a3351425Sdougm 					}
1954a3351425Sdougm 				}
1955a3351425Sdougm 				break;
1956a3351425Sdougm 			case OPT_TYPE_LOGTAG:
1957a3351425Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
1958a3351425Sdougm 					int error;
1959a3351425Sdougm 					if (value == NULL ||
1960a3351425Sdougm 					    strlen(value) == 0) {
1961a3351425Sdougm 						if (value != NULL)
1962a3351425Sdougm 							sa_free_attr_string(
1963a3351425Sdougm 							    value);
1964a3351425Sdougm 						value = strdup("global");
1965a3351425Sdougm 					}
1966a3351425Sdougm 					if (value != NULL &&
1967a3351425Sdougm 					    nfsl_findconfig(configlist, value,
1968a3351425Sdougm 					    &error) == NULL) {
1969a3351425Sdougm 						ret = SA_BAD_VALUE;
1970a3351425Sdougm 					} else {
1971a3351425Sdougm 						nfsl_freeconfig_list(
1972a3351425Sdougm 						    &configlist);
1973a3351425Sdougm 					}
1974a3351425Sdougm 				} else {
1975a3351425Sdougm 					ret = SA_CONFIG_ERR;
1976a3351425Sdougm 				}
1977a3351425Sdougm 				break;
1978a3351425Sdougm 			case OPT_TYPE_STRING:
1979a3351425Sdougm 				/* whatever is here should be ok */
1980a3351425Sdougm 				break;
1981a3351425Sdougm 			case OPT_TYPE_SECURITY:
1982a3351425Sdougm 				/*
1983a3351425Sdougm 				 * The "sec" property isn't used in the
1984a3351425Sdougm 				 * non-legacy parts of sharemgr. We need to
1985a3351425Sdougm 				 * reject it here. For legacy, it is pulled
1986a3351425Sdougm 				 * out well before we get here.
1987a3351425Sdougm 				 */
1988a3351425Sdougm 				ret = SA_NO_SUCH_PROP;
1989a3351425Sdougm 				break;
1990a3351425Sdougm 			default:
1991a3351425Sdougm 				break;
19926185db85Sdougm 			}
1993a3351425Sdougm 			sa_free_attr_string(value);
1994a3351425Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
1995a3351425Sdougm 				/* do the property specific check */
1996a3351425Sdougm 				ret = optdefs[optindex].check(property);
19976185db85Sdougm 			}
19986185db85Sdougm 		}
19996185db85Sdougm 	}
20006185db85Sdougm 
20016185db85Sdougm 	if (propname != NULL)
2002a3351425Sdougm 		sa_free_attr_string(propname);
20036185db85Sdougm 	return (ret);
20046185db85Sdougm }
20056185db85Sdougm 
20066185db85Sdougm /*
20076185db85Sdougm  * Protocol management functions
20086185db85Sdougm  *
20093472f5dcSdougm  * Properties defined in the default files are defined in
20103472f5dcSdougm  * proto_option_defs for parsing and validation. If "other" and
20113472f5dcSdougm  * "compare" are set, then the value for this property should be
20123472f5dcSdougm  * compared against the property specified in "other" using the
20133472f5dcSdougm  * "compare" check (either <= or >=) in order to ensure that the
20143472f5dcSdougm  * values are in the correct range.  E.g. setting server_versmin
20153472f5dcSdougm  * higher than server_versmax should not be allowed.
20166185db85Sdougm  */
20176185db85Sdougm 
20186185db85Sdougm struct proto_option_defs {
20196185db85Sdougm 	char *tag;
20206185db85Sdougm 	char *name;	/* display name -- remove protocol identifier */
20216185db85Sdougm 	int index;
20226185db85Sdougm 	int type;
20236185db85Sdougm 	union {
20246185db85Sdougm 	    int intval;
20256185db85Sdougm 	    char *string;
20266185db85Sdougm 	} defvalue;
20276185db85Sdougm 	uint32_t svcs;
20286185db85Sdougm 	int32_t minval;
20296185db85Sdougm 	int32_t maxval;
20306185db85Sdougm 	char *file;
20313472f5dcSdougm 	char *other;
20323472f5dcSdougm 	int compare;
20333472f5dcSdougm #define	OPT_CMP_GE	0
20343472f5dcSdougm #define	OPT_CMP_LE	1
20356185db85Sdougm 	int (*check)(char *);
20366185db85Sdougm } proto_options[] = {
20376185db85Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
20386185db85Sdougm 	{"nfsd_servers",
20396185db85Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
20406185db85Sdougm 	    1, INT32_MAX, NFSADMIN},
20416185db85Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
20426185db85Sdougm 	{"lockd_listen_backlog",
20436185db85Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
20446185db85Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
20456185db85Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
20466185db85Sdougm 	{"lockd_servers",
20476185db85Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
20486185db85Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
20496185db85Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
20506185db85Sdougm 	{"lockd_retransmit_timeout",
20516185db85Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
20526185db85Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
20536185db85Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
20546185db85Sdougm 	{"grace_period",
20556185db85Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
20566185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
20576185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
20586185db85Sdougm 	{"nfs_server_versmin",
20596185db85Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
20606185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
20613472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
20626185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
20636185db85Sdougm 	{"nfs_server_versmax",
20646185db85Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
20656185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
20663472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
20676185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
20686185db85Sdougm 	{"nfs_client_versmin",
20696185db85Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
20706185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
20713472f5dcSdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
20726185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
20736185db85Sdougm 	{"nfs_client_versmax",
20746185db85Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
20756185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
20763472f5dcSdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
20776185db85Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
20786185db85Sdougm 	{"nfs_server_delegation",
20796185db85Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
20806185db85Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
20816185db85Sdougm 	    NFSADMIN},
20826185db85Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
20836185db85Sdougm 	{"nfsmapid_domain",
20846185db85Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
20856185db85Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
20866185db85Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
20876185db85Sdougm 	{"nfsd_max_connections",
20886185db85Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
20896185db85Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
20906185db85Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
20916185db85Sdougm 	{"nfsd_protocol",
20926185db85Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
20936185db85Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
20946185db85Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
20956185db85Sdougm 	{"nfsd_listen_backlog",
20966185db85Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
20976185db85Sdougm 	    OPT_TYPE_NUMBER, 0,
20986185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
20996185db85Sdougm 	{NULL}
21006185db85Sdougm };
21016185db85Sdougm 
21026185db85Sdougm /*
21036185db85Sdougm  * the protoset holds the defined options so we don't have to read
21046185db85Sdougm  * them multiple times
21056185db85Sdougm  */
21066185db85Sdougm sa_protocol_properties_t protoset;
21076185db85Sdougm 
21086185db85Sdougm static int
21096185db85Sdougm findprotoopt(char *name, int whichname)
21106185db85Sdougm {
21116185db85Sdougm 	int i;
21126185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2113a3351425Sdougm 		if (whichname == 1) {
2114a3351425Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
21156185db85Sdougm 			return (i);
2116a3351425Sdougm 		} else {
2117a3351425Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
2118a3351425Sdougm 				return (i);
2119a3351425Sdougm 		}
21206185db85Sdougm 	}
21216185db85Sdougm 	return (-1);
21226185db85Sdougm }
21236185db85Sdougm 
21246185db85Sdougm /*
21256185db85Sdougm  * fixcaselower(str)
21266185db85Sdougm  *
21276185db85Sdougm  * convert a string to lower case (inplace).
21286185db85Sdougm  */
21296185db85Sdougm 
21306185db85Sdougm static void
21316185db85Sdougm fixcaselower(char *str)
21326185db85Sdougm {
21336185db85Sdougm 	while (*str) {
2134a3351425Sdougm 		*str = tolower(*str);
2135a3351425Sdougm 		str++;
21366185db85Sdougm 	}
21376185db85Sdougm }
21386185db85Sdougm 
21396185db85Sdougm /*
21406185db85Sdougm  * fixcaseupper(str)
21416185db85Sdougm  *
21426185db85Sdougm  * convert a string to upper case (inplace).
21436185db85Sdougm  */
21446185db85Sdougm 
21456185db85Sdougm static void
21466185db85Sdougm fixcaseupper(char *str)
21476185db85Sdougm {
21486185db85Sdougm 	while (*str) {
2149a3351425Sdougm 		*str = toupper(*str);
2150a3351425Sdougm 		str++;
21516185db85Sdougm 	}
21526185db85Sdougm }
21536185db85Sdougm 
2154330ef417Sdougm /*
2155330ef417Sdougm  * skipwhitespace(str)
2156330ef417Sdougm  *
2157330ef417Sdougm  * Skip leading white space. It is assumed that it is called with a
2158330ef417Sdougm  * valid pointer.
2159330ef417Sdougm  */
2160330ef417Sdougm 
2161330ef417Sdougm static char *
2162330ef417Sdougm skipwhitespace(char *str)
2163330ef417Sdougm {
2164330ef417Sdougm 	while (*str && isspace(*str))
2165330ef417Sdougm 		str++;
2166330ef417Sdougm 
2167330ef417Sdougm 	return (str);
2168330ef417Sdougm }
2169330ef417Sdougm 
2170a3351425Sdougm /*
2171a3351425Sdougm  * extractprop()
2172a3351425Sdougm  *
2173a3351425Sdougm  * Extract the property and value out of the line and create the
2174a3351425Sdougm  * property in the optionset.
2175a3351425Sdougm  */
2176a3351425Sdougm static void
2177a3351425Sdougm extractprop(char *name, char *value)
2178a3351425Sdougm {
2179a3351425Sdougm 	sa_property_t prop;
2180a3351425Sdougm 	int index;
2181a3351425Sdougm 	/*
2182a3351425Sdougm 	 * Remove any leading
2183a3351425Sdougm 	 * white space.
2184a3351425Sdougm 	 */
2185a3351425Sdougm 	name = skipwhitespace(name);
2186a3351425Sdougm 
2187a3351425Sdougm 	index = findprotoopt(name, 0);
2188a3351425Sdougm 	if (index >= 0) {
2189a3351425Sdougm 		fixcaselower(name);
2190a3351425Sdougm 		prop = sa_create_property(proto_options[index].name, value);
2191a3351425Sdougm 		if (prop != NULL)
2192a3351425Sdougm 			(void) sa_add_protocol_property(protoset, prop);
2193a3351425Sdougm 	}
2194a3351425Sdougm }
2195a3351425Sdougm 
21966185db85Sdougm /*
21976185db85Sdougm  * initprotofromdefault()
21986185db85Sdougm  *
21996185db85Sdougm  * read the default file(s) and add the defined values to the
22006185db85Sdougm  * protoset.  Note that default values are known from the built in
22016185db85Sdougm  * table in case the file doesn't have a definition.
22026185db85Sdougm  */
22036185db85Sdougm 
22046185db85Sdougm static int
22056185db85Sdougm initprotofromdefault()
22066185db85Sdougm {
22076185db85Sdougm 	FILE *nfs;
22086185db85Sdougm 	char buff[BUFSIZ];
22096185db85Sdougm 	char *name;
22106185db85Sdougm 	char *value;
22116185db85Sdougm 
22126185db85Sdougm 	protoset = sa_create_protocol_properties("nfs");
22136185db85Sdougm 
22146185db85Sdougm 	if (protoset != NULL) {
2215a3351425Sdougm 		nfs = fopen(NFSADMIN, "r");
2216a3351425Sdougm 		if (nfs != NULL) {
2217a3351425Sdougm 			while (fgets(buff, sizeof (buff), nfs) != NULL) {
2218a3351425Sdougm 				switch (buff[0]) {
2219a3351425Sdougm 				case '\n':
2220a3351425Sdougm 				case '#':
2221a3351425Sdougm 					/* skip */
2222a3351425Sdougm 					break;
2223a3351425Sdougm 				default:
2224a3351425Sdougm 					name = buff;
2225a3351425Sdougm 					buff[strlen(buff) - 1] = '\0';
2226a3351425Sdougm 					value = strchr(name, '=');
2227a3351425Sdougm 					if (value != NULL) {
2228a3351425Sdougm 						*value++ = '\0';
2229a3351425Sdougm 						extractprop(name, value);
2230a3351425Sdougm 					}
2231a3351425Sdougm 				}
22326185db85Sdougm 			}
2233a3351425Sdougm 			if (nfs != NULL)
2234a3351425Sdougm 				(void) fclose(nfs);
22356185db85Sdougm 		}
22366185db85Sdougm 	}
22376185db85Sdougm 	if (protoset == NULL)
2238a3351425Sdougm 		return (SA_NO_MEMORY);
22396185db85Sdougm 	return (SA_OK);
22406185db85Sdougm }
22416185db85Sdougm 
22426185db85Sdougm /*
2243a3351425Sdougm  * add_defaults()
22446185db85Sdougm  *
22456185db85Sdougm  * Add the default values for any property not defined in the parsing
22463472f5dcSdougm  * of the default files. Values are set according to their defined
22473472f5dcSdougm  * types.
22486185db85Sdougm  */
22496185db85Sdougm 
22506185db85Sdougm static void
22516185db85Sdougm add_defaults()
22526185db85Sdougm {
22536185db85Sdougm 	int i;
22546185db85Sdougm 	char number[MAXDIGITS];
22556185db85Sdougm 
22566185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2257a3351425Sdougm 		sa_property_t prop;
2258a3351425Sdougm 		prop = sa_get_protocol_property(protoset,
2259a3351425Sdougm 		    proto_options[i].name);
2260a3351425Sdougm 		if (prop == NULL) {
2261a3351425Sdougm 			/* add the default value */
2262a3351425Sdougm 			switch (proto_options[i].type) {
2263a3351425Sdougm 			case OPT_TYPE_NUMBER:
2264a3351425Sdougm 				(void) snprintf(number, sizeof (number), "%d",
2265a3351425Sdougm 				    proto_options[i].defvalue.intval);
2266a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2267a3351425Sdougm 				    number);
2268a3351425Sdougm 				break;
2269a3351425Sdougm 
2270a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2271a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2272a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2273a3351425Sdougm 				    "true" : "false");
2274a3351425Sdougm 				break;
2275a3351425Sdougm 
2276a3351425Sdougm 			case OPT_TYPE_ONOFF:
2277a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2278a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2279a3351425Sdougm 				    "on" : "off");
2280a3351425Sdougm 				break;
2281a3351425Sdougm 
2282a3351425Sdougm 			default:
2283a3351425Sdougm 				/* treat as strings of zero length */
2284a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2285a3351425Sdougm 				    "");
2286a3351425Sdougm 				break;
2287a3351425Sdougm 			}
2288a3351425Sdougm 			if (prop != NULL)
2289a3351425Sdougm 				(void) sa_add_protocol_property(protoset, prop);
22906185db85Sdougm 		}
22916185db85Sdougm 	}
22926185db85Sdougm }
22936185db85Sdougm 
22946185db85Sdougm static void
22956185db85Sdougm free_protoprops()
22966185db85Sdougm {
22976185db85Sdougm 	xmlFreeNode(protoset);
22986185db85Sdougm }
22996185db85Sdougm 
23006185db85Sdougm /*
23016185db85Sdougm  * nfs_init()
23026185db85Sdougm  *
23036185db85Sdougm  * Initialize the NFS plugin.
23046185db85Sdougm  */
23056185db85Sdougm 
23066185db85Sdougm static int
23076185db85Sdougm nfs_init()
23086185db85Sdougm {
23096185db85Sdougm 	int ret = SA_OK;
23106185db85Sdougm 
23116185db85Sdougm 	if (sa_plugin_ops.sa_init != nfs_init)
2312a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2313a3351425Sdougm 		    "NFS plugin not properly initialized\n"));
23146185db85Sdougm 
23156185db85Sdougm 	ret = initprotofromdefault();
2316a3351425Sdougm 	if (ret == SA_OK)
2317a3351425Sdougm 		add_defaults();
23186185db85Sdougm 
23196185db85Sdougm 	return (ret);
23206185db85Sdougm }
23216185db85Sdougm 
23226185db85Sdougm /*
23236185db85Sdougm  * nfs_fini()
23246185db85Sdougm  *
23256185db85Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
23266185db85Sdougm  */
23276185db85Sdougm 
23286185db85Sdougm static void
23296185db85Sdougm nfs_fini()
23306185db85Sdougm {
23316185db85Sdougm 	free_protoprops();
23326185db85Sdougm }
23336185db85Sdougm 
23346185db85Sdougm /*
23356185db85Sdougm  * nfs_get_proto_set()
23366185db85Sdougm  *
23376185db85Sdougm  * Return an optionset with all the protocol specific properties in
23386185db85Sdougm  * it.
23396185db85Sdougm  */
23406185db85Sdougm 
23416185db85Sdougm static sa_protocol_properties_t
23426185db85Sdougm nfs_get_proto_set()
23436185db85Sdougm {
23446185db85Sdougm 	return (protoset);
23456185db85Sdougm }
23466185db85Sdougm 
23476185db85Sdougm struct deffile {
23486185db85Sdougm 	struct deffile *next;
23496185db85Sdougm 	char *line;
23506185db85Sdougm };
23516185db85Sdougm 
23526185db85Sdougm /*
23536185db85Sdougm  * read_default_file(fname)
23546185db85Sdougm  *
23556185db85Sdougm  * Read the specified default file. We return a list of entries. This
23566185db85Sdougm  * get used for adding or removing values.
23576185db85Sdougm  */
23586185db85Sdougm 
23596185db85Sdougm static struct deffile *
23606185db85Sdougm read_default_file(char *fname)
23616185db85Sdougm {
23626185db85Sdougm 	FILE *file;
23636185db85Sdougm 	struct deffile *defs = NULL;
23646185db85Sdougm 	struct deffile *newdef;
23656185db85Sdougm 	struct deffile *prevdef = NULL;
23666185db85Sdougm 	char buff[BUFSIZ * 2];
23676185db85Sdougm 
23686185db85Sdougm 	file = fopen(fname, "r");
23696185db85Sdougm 	if (file != NULL) {
2370a3351425Sdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
2371a3351425Sdougm 			newdef = (struct deffile *)calloc(1,
2372a3351425Sdougm 			    sizeof (struct deffile));
2373a3351425Sdougm 			if (newdef != NULL) {
2374a3351425Sdougm 				/* Make sure we skip any leading whitespace. */
2375a3351425Sdougm 				newdef->line = strdup(skipwhitespace(buff));
2376a3351425Sdougm 				if (defs == NULL) {
2377a3351425Sdougm 					prevdef = defs = newdef;
2378a3351425Sdougm 				} else {
2379a3351425Sdougm 					prevdef->next = newdef;
2380a3351425Sdougm 					prevdef = newdef;
2381a3351425Sdougm 				}
2382a3351425Sdougm 			}
23836185db85Sdougm 		}
23846185db85Sdougm 	}
23856185db85Sdougm 	(void) fclose(file);
23866185db85Sdougm 	return (defs);
23876185db85Sdougm }
23886185db85Sdougm 
23896185db85Sdougm static void
23906185db85Sdougm free_default_file(struct deffile *defs)
23916185db85Sdougm {
23926185db85Sdougm 	struct deffile *curdefs = NULL;
23936185db85Sdougm 
23946185db85Sdougm 	while (defs != NULL) {
2395a3351425Sdougm 		curdefs = defs;
2396a3351425Sdougm 		defs = defs->next;
2397a3351425Sdougm 		if (curdefs->line != NULL)
2398a3351425Sdougm 			free(curdefs->line);
2399a3351425Sdougm 		free(curdefs);
24006185db85Sdougm 	}
24016185db85Sdougm }
24026185db85Sdougm 
24036185db85Sdougm /*
24046185db85Sdougm  * write_default_file(fname, defs)
24056185db85Sdougm  *
24066185db85Sdougm  * Write the default file back.
24076185db85Sdougm  */
24086185db85Sdougm 
24096185db85Sdougm static int
24106185db85Sdougm write_default_file(char *fname, struct deffile *defs)
24116185db85Sdougm {
24126185db85Sdougm 	FILE *file;
24136185db85Sdougm 	int ret = SA_OK;
24146185db85Sdougm 	sigset_t old, new;
24156185db85Sdougm 
24166185db85Sdougm 	file = fopen(fname, "w+");
24176185db85Sdougm 	if (file != NULL) {
2418a3351425Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
2419a3351425Sdougm 		(void) sigaddset(&new, SIGHUP);
2420a3351425Sdougm 		(void) sigaddset(&new, SIGINT);
2421a3351425Sdougm 		(void) sigaddset(&new, SIGQUIT);
2422a3351425Sdougm 		(void) sigaddset(&new, SIGTSTP);
2423a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
2424a3351425Sdougm 		while (defs != NULL) {
2425a3351425Sdougm 			(void) fputs(defs->line, file);
2426a3351425Sdougm 			defs = defs->next;
2427a3351425Sdougm 		}
2428a3351425Sdougm 		(void) fsync(fileno(file));
2429a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
2430a3351425Sdougm 		(void) fclose(file);
24316185db85Sdougm 	} else {
2432a3351425Sdougm 		switch (errno) {
2433a3351425Sdougm 		case EPERM:
2434a3351425Sdougm 		case EACCES:
2435a3351425Sdougm 			ret = SA_NO_PERMISSION;
2436a3351425Sdougm 			break;
2437a3351425Sdougm 		default:
2438a3351425Sdougm 			ret = SA_SYSTEM_ERR;
2439a3351425Sdougm 		}
24406185db85Sdougm 	}
24416185db85Sdougm 	return (ret);
24426185db85Sdougm }
24436185db85Sdougm 
24446185db85Sdougm 
24456185db85Sdougm /*
24466185db85Sdougm  * set_default_file_value(tag, value)
24476185db85Sdougm  *
24486185db85Sdougm  * Set the default file value for tag to value. Then rewrite the file.
24496185db85Sdougm  * tag and value are always set.  The caller must ensure this.
24506185db85Sdougm  */
24516185db85Sdougm 
24526185db85Sdougm #define	MAX_STRING_LENGTH	256
24536185db85Sdougm static int
24546185db85Sdougm set_default_file_value(char *tag, char *value)
24556185db85Sdougm {
24566185db85Sdougm 	int ret = SA_OK;
24576185db85Sdougm 	struct deffile *root;
24586185db85Sdougm 	struct deffile *defs;
24596185db85Sdougm 	struct deffile *prev;
24606185db85Sdougm 	char string[MAX_STRING_LENGTH];
24616185db85Sdougm 	int len;
24626185db85Sdougm 	int update = 0;
24636185db85Sdougm 
24646185db85Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
24656185db85Sdougm 	len = strlen(string);
24666185db85Sdougm 
24676185db85Sdougm 	root = defs = read_default_file(NFSADMIN);
24686185db85Sdougm 	if (root == NULL) {
2469a3351425Sdougm 		if (errno == EPERM || errno == EACCES)
2470a3351425Sdougm 			ret = SA_NO_PERMISSION;
2471a3351425Sdougm 		else
2472a3351425Sdougm 			ret = SA_SYSTEM_ERR;
24736185db85Sdougm 	} else {
24746185db85Sdougm 		while (defs != NULL) {
2475a3351425Sdougm 			if (defs->line != NULL &&
2476a3351425Sdougm 			    strncasecmp(defs->line, string, len) == 0) {
2477a3351425Sdougm 				/* replace with the new value */
2478a3351425Sdougm 				free(defs->line);
2479a3351425Sdougm 				fixcaseupper(tag);
2480a3351425Sdougm 				(void) snprintf(string, sizeof (string),
24816185db85Sdougm 				    "%s=%s\n", tag, value);
2482a3351425Sdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
2483a3351425Sdougm 				defs->line = strdup(string);
2484a3351425Sdougm 				update = 1;
2485a3351425Sdougm 				break;
2486a3351425Sdougm 			}
2487a3351425Sdougm 			defs = defs->next;
2488a3351425Sdougm 		}
2489a3351425Sdougm 		if (!update) {
2490a3351425Sdougm 			defs = root;
2491a3351425Sdougm 			/* didn't find, so see if it is a comment */
2492a3351425Sdougm 			(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
2493a3351425Sdougm 			len = strlen(string);
2494a3351425Sdougm 			while (defs != NULL) {
2495a3351425Sdougm 				if (strncasecmp(defs->line, string, len) == 0) {
2496a3351425Sdougm 					/* replace with the new value */
2497a3351425Sdougm 					free(defs->line);
2498a3351425Sdougm 					fixcaseupper(tag);
2499a3351425Sdougm 					(void) snprintf(string, sizeof (string),
2500a3351425Sdougm 					    "%s=%s\n", tag, value);
2501a3351425Sdougm 					string[MAX_STRING_LENGTH - 1] = '\0';
2502a3351425Sdougm 					defs->line = strdup(string);
2503a3351425Sdougm 					update = 1;
2504a3351425Sdougm 					break;
2505a3351425Sdougm 				}
2506a3351425Sdougm 				defs = defs->next;
2507a3351425Sdougm 			}
25086185db85Sdougm 		}
2509a3351425Sdougm 		if (!update) {
2510a3351425Sdougm 			fixcaseupper(tag);
2511a3351425Sdougm 			(void) snprintf(string, sizeof (string), "%s=%s\n",
2512a3351425Sdougm 			    tag, value);
2513a3351425Sdougm 			prev = root;
2514a3351425Sdougm 			while (prev->next != NULL)
2515a3351425Sdougm 				prev = prev->next;
2516a3351425Sdougm 			defs = malloc(sizeof (struct deffile));
2517a3351425Sdougm 			prev->next = defs;
2518a3351425Sdougm 			if (defs != NULL) {
2519a3351425Sdougm 				defs->next = NULL;
2520a3351425Sdougm 				defs->line = strdup(string);
2521a3351425Sdougm 			}
25226185db85Sdougm 		}
2523a3351425Sdougm 		if (update) {
2524a3351425Sdougm 			ret = write_default_file(NFSADMIN, root);
2525a3351425Sdougm 		}
2526a3351425Sdougm 		free_default_file(root);
25276185db85Sdougm 	}
25286185db85Sdougm 	return (ret);
25296185db85Sdougm }
25306185db85Sdougm 
25313472f5dcSdougm /*
25323472f5dcSdougm  * service_in_state(service, chkstate)
25333472f5dcSdougm  *
25343472f5dcSdougm  * Want to know if the specified service is in the desired state
25353472f5dcSdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
25363472f5dcSdougm  * isn't.
25373472f5dcSdougm  */
25383472f5dcSdougm static int
25393472f5dcSdougm service_in_state(char *service, const char *chkstate)
25403472f5dcSdougm {
25413472f5dcSdougm 	char *state;
25423472f5dcSdougm 	int ret = B_FALSE;
25433472f5dcSdougm 
25443472f5dcSdougm 	state = smf_get_state(service);
25453472f5dcSdougm 	if (state != NULL) {
2546a3351425Sdougm 		/* got the state so get the equality for the return value */
2547a3351425Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2548a3351425Sdougm 		free(state);
25493472f5dcSdougm 	}
25503472f5dcSdougm 	return (ret);
25513472f5dcSdougm }
25523472f5dcSdougm 
25536185db85Sdougm /*
25546185db85Sdougm  * restart_service(svcs)
25556185db85Sdougm  *
25566185db85Sdougm  * Walk through the bit mask of services that need to be restarted in
25576185db85Sdougm  * order to use the new property values. Some properties affect
25583472f5dcSdougm  * multiple daemons. Should only restart a service if it is currently
25593472f5dcSdougm  * enabled (online).
25606185db85Sdougm  */
25616185db85Sdougm 
25626185db85Sdougm static void
25636185db85Sdougm restart_service(uint32_t svcs)
25646185db85Sdougm {
25656185db85Sdougm 	uint32_t mask;
25663472f5dcSdougm 	int ret;
25673472f5dcSdougm 	char *service;
25683472f5dcSdougm 
25696185db85Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
2570a3351425Sdougm 		switch (svcs & mask) {
2571a3351425Sdougm 		case SVC_LOCKD:
2572a3351425Sdougm 			service = LOCKD;
2573a3351425Sdougm 			break;
2574a3351425Sdougm 		case SVC_STATD:
2575a3351425Sdougm 			service = STATD;
2576a3351425Sdougm 			break;
2577a3351425Sdougm 		case SVC_NFSD:
2578a3351425Sdougm 			service = NFSD;
2579a3351425Sdougm 			break;
2580a3351425Sdougm 		case SVC_MOUNTD:
2581a3351425Sdougm 			service = MOUNTD;
2582a3351425Sdougm 			break;
2583a3351425Sdougm 		case SVC_NFS4CBD:
2584a3351425Sdougm 			service = NFS4CBD;
2585a3351425Sdougm 			break;
2586a3351425Sdougm 		case SVC_NFSMAPID:
2587a3351425Sdougm 			service = NFSMAPID;
2588a3351425Sdougm 			break;
2589a3351425Sdougm 		case SVC_RQUOTAD:
2590a3351425Sdougm 			service = RQUOTAD;
2591a3351425Sdougm 			break;
2592a3351425Sdougm 		case SVC_NFSLOGD:
2593a3351425Sdougm 			service = NFSLOGD;
2594a3351425Sdougm 			break;
2595a3351425Sdougm 		default:
2596a3351425Sdougm 			continue;
2597a3351425Sdougm 		}
25983472f5dcSdougm 
25993472f5dcSdougm 		/*
26003472f5dcSdougm 		 * Only attempt to restart the service if it is
26013472f5dcSdougm 		 * currently running. In the future, it may be
26023472f5dcSdougm 		 * desirable to use smf_refresh_instance if the NFS
26033472f5dcSdougm 		 * services ever implement the refresh method.
26043472f5dcSdougm 		 */
2605a3351425Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2606a3351425Sdougm 			ret = smf_restart_instance(service);
26073472f5dcSdougm 			/*
2608a3351425Sdougm 			 * There are only a few SMF errors at this point, but
2609a3351425Sdougm 			 * it is also possible that a bad value may have put
2610a3351425Sdougm 			 * the service into maintenance if there wasn't an
2611a3351425Sdougm 			 * SMF level error.
26123472f5dcSdougm 			 */
2613a3351425Sdougm 			if (ret != 0) {
2614a3351425Sdougm 				(void) fprintf(stderr,
2615a3351425Sdougm 				    dgettext(TEXT_DOMAIN,
2616a3351425Sdougm 				    "%s failed to restart: %s\n"),
2617a3351425Sdougm 				    scf_strerror(scf_error()));
2618a3351425Sdougm 			} else {
2619a3351425Sdougm 				/*
2620a3351425Sdougm 				 * Check whether it has gone to "maintenance"
2621a3351425Sdougm 				 * mode or not. Maintenance implies something
2622a3351425Sdougm 				 * went wrong.
2623a3351425Sdougm 				 */
2624a3351425Sdougm 				if (service_in_state(service,
2625a3351425Sdougm 				    SCF_STATE_STRING_MAINT)) {
2626a3351425Sdougm 					(void) fprintf(stderr,
2627a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
2628a3351425Sdougm 					    "%s failed to restart\n"),
2629a3351425Sdougm 					    service);
2630a3351425Sdougm 				}
2631a3351425Sdougm 			}
26323472f5dcSdougm 		}
2633a3351425Sdougm 		svcs &= ~mask;
26346185db85Sdougm 	}
26356185db85Sdougm }
26366185db85Sdougm 
26373472f5dcSdougm /*
26383472f5dcSdougm  * nfs_minmax_check(name, value)
26393472f5dcSdougm  *
26403472f5dcSdougm  * Verify that the value for the property specified by index is valid
26413472f5dcSdougm  * relative to the opposite value in the case of a min/max variable.
26423472f5dcSdougm  * Currently, server_minvers/server_maxvers and
26433472f5dcSdougm  * client_minvers/client_maxvers are the only ones to check.
26443472f5dcSdougm  */
26453472f5dcSdougm 
26463472f5dcSdougm static int
26473472f5dcSdougm nfs_minmax_check(int index, int value)
26483472f5dcSdougm {
26493472f5dcSdougm 	int val;
26503472f5dcSdougm 	char *pval;
26513472f5dcSdougm 	sa_property_t prop;
26523472f5dcSdougm 	sa_optionset_t opts;
26533472f5dcSdougm 	int ret = B_TRUE;
26543472f5dcSdougm 
26553472f5dcSdougm 	if (proto_options[index].other != NULL) {
2656a3351425Sdougm 		/* have a property to compare against */
2657a3351425Sdougm 		opts = nfs_get_proto_set();
2658a3351425Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
26593472f5dcSdougm 		/*
26603472f5dcSdougm 		 * If we don't find the property, assume default
26613472f5dcSdougm 		 * values which will work since the max will be at the
26623472f5dcSdougm 		 * max and the min at the min.
26633472f5dcSdougm 		 */
2664a3351425Sdougm 		if (prop != NULL) {
2665a3351425Sdougm 			pval = sa_get_property_attr(prop, "value");
2666a3351425Sdougm 			if (pval != NULL) {
2667a3351425Sdougm 				val = strtoul(pval, NULL, 0);
2668a3351425Sdougm 				if (proto_options[index].compare ==
2669a3351425Sdougm 				    OPT_CMP_LE) {
2670a3351425Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
2671a3351425Sdougm 				} else if (proto_options[index].compare ==
2672a3351425Sdougm 				    OPT_CMP_GE) {
2673a3351425Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
2674a3351425Sdougm 				}
2675a3351425Sdougm 			}
26763472f5dcSdougm 		}
26773472f5dcSdougm 	}
26783472f5dcSdougm 	return (ret);
26793472f5dcSdougm }
26803472f5dcSdougm 
26816185db85Sdougm /*
26826185db85Sdougm  * nfs_validate_proto_prop(index, name, value)
26836185db85Sdougm  *
26846185db85Sdougm  * Verify that the property specifed by name can take the new
26856185db85Sdougm  * value. This is a sanity check to prevent bad values getting into
26863472f5dcSdougm  * the default files. All values need to be checked against what is
26873472f5dcSdougm  * allowed by their defined type. If a type isn't explicitly defined
26883472f5dcSdougm  * here, it is treated as a string.
26893472f5dcSdougm  *
26903472f5dcSdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
26913472f5dcSdougm  * within the range specified and potentially against another property
26923472f5dcSdougm  * value as well as specified in the proto_options members other and
26933472f5dcSdougm  * compare.
26946185db85Sdougm  */
26956185db85Sdougm 
26966185db85Sdougm static int
26976185db85Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
26986185db85Sdougm {
26996185db85Sdougm 	int ret = SA_OK;
27006185db85Sdougm 	char *cp;
27016185db85Sdougm #ifdef lint
27026185db85Sdougm 	name = name;
27036185db85Sdougm #endif
27046185db85Sdougm 
27056185db85Sdougm 	switch (proto_options[index].type) {
27066185db85Sdougm 	case OPT_TYPE_NUMBER:
2707a3351425Sdougm 		if (!is_a_number(value))
2708a3351425Sdougm 			ret = SA_BAD_VALUE;
2709a3351425Sdougm 		else {
2710a3351425Sdougm 			int val;
2711a3351425Sdougm 			val = strtoul(value, NULL, 0);
2712a3351425Sdougm 			if (val < proto_options[index].minval ||
2713a3351425Sdougm 			    val > proto_options[index].maxval)
2714a3351425Sdougm 				ret = SA_BAD_VALUE;
2715a3351425Sdougm 			/*
2716a3351425Sdougm 			 * For server_versmin/server_versmax and
2717a3351425Sdougm 			 * client_versmin/client_versmax, the value of the
2718a3351425Sdougm 			 * min(max) should be checked to be correct relative
2719a3351425Sdougm 			 * to the current max(min).
2720a3351425Sdougm 			 */
2721a3351425Sdougm 			if (!nfs_minmax_check(index, val)) {
2722a3351425Sdougm 				ret = SA_BAD_VALUE;
2723a3351425Sdougm 			}
27243472f5dcSdougm 		}
2725a3351425Sdougm 		break;
27263472f5dcSdougm 
27276185db85Sdougm 	case OPT_TYPE_DOMAIN:
27286185db85Sdougm 		/*
27293472f5dcSdougm 		 * needs to be a qualified domain so will have at
27303472f5dcSdougm 		 * least one period and other characters on either
27313472f5dcSdougm 		 * side of it.  A zero length string is also allowed
27323472f5dcSdougm 		 * and is the way to turn off the override.
27336185db85Sdougm 		 */
2734a3351425Sdougm 		if (strlen(value) == 0)
2735a3351425Sdougm 			break;
2736a3351425Sdougm 		cp = strchr(value, '.');
2737a3351425Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
2738a3351425Sdougm 			ret = SA_BAD_VALUE;
27393472f5dcSdougm 		break;
27403472f5dcSdougm 
27416185db85Sdougm 	case OPT_TYPE_BOOLEAN:
2742a3351425Sdougm 		if (strlen(value) == 0 ||
2743a3351425Sdougm 		    strcasecmp(value, "true") == 0 ||
2744a3351425Sdougm 		    strcmp(value, "1") == 0 ||
2745a3351425Sdougm 		    strcasecmp(value, "false") == 0 ||
2746a3351425Sdougm 		    strcmp(value, "0") == 0) {
2747a3351425Sdougm 			ret = SA_OK;
2748a3351425Sdougm 		} else {
2749a3351425Sdougm 			ret = SA_BAD_VALUE;
2750a3351425Sdougm 		}
2751a3351425Sdougm 		break;
27523472f5dcSdougm 
27536185db85Sdougm 	case OPT_TYPE_ONOFF:
2754a3351425Sdougm 		if (strcasecmp(value, "on") != 0 &&
2755a3351425Sdougm 		    strcasecmp(value, "off") != 0) {
2756a3351425Sdougm 			ret = SA_BAD_VALUE;
2757a3351425Sdougm 		}
2758a3351425Sdougm 		break;
27593472f5dcSdougm 
27606185db85Sdougm 	case OPT_TYPE_PROTOCOL:
2761a3351425Sdougm 		if (strcasecmp(value, "all") != 0 &&
2762a3351425Sdougm 		    strcasecmp(value, "tcp") != 0 &&
2763a3351425Sdougm 		    strcasecmp(value, "udp") != 0)
2764a3351425Sdougm 			ret = SA_BAD_VALUE;
2765a3351425Sdougm 		break;
27663472f5dcSdougm 
27673472f5dcSdougm 	default:
2768a3351425Sdougm 		/* treat as a string */
2769a3351425Sdougm 		break;
27706185db85Sdougm 	}
27716185db85Sdougm 	return (ret);
27726185db85Sdougm }
27736185db85Sdougm 
27746185db85Sdougm /*
27756185db85Sdougm  * nfs_set_proto_prop(prop)
27766185db85Sdougm  *
27776185db85Sdougm  * check that prop is valid.
27786185db85Sdougm  */
27796185db85Sdougm 
27806185db85Sdougm static int
27816185db85Sdougm nfs_set_proto_prop(sa_property_t prop)
27826185db85Sdougm {
27836185db85Sdougm 	int ret = SA_OK;
27846185db85Sdougm 	char *name;
27856185db85Sdougm 	char *value;
27866185db85Sdougm 
27876185db85Sdougm 	name = sa_get_property_attr(prop, "type");
27886185db85Sdougm 	value = sa_get_property_attr(prop, "value");
27896185db85Sdougm 	if (name != NULL && value != NULL) {
2790a3351425Sdougm 		int index = findprotoopt(name, 1);
2791a3351425Sdougm 		if (index >= 0) {
2792a3351425Sdougm 			/* should test for valid value */
2793a3351425Sdougm 			ret = nfs_validate_proto_prop(index, name, value);
2794a3351425Sdougm 			if (ret == SA_OK)
2795a3351425Sdougm 				ret = set_default_file_value(
2796a3351425Sdougm 				    proto_options[index].tag, value);
2797a3351425Sdougm 			if (ret == SA_OK)
2798a3351425Sdougm 				restart_service(proto_options[index].svcs);
2799a3351425Sdougm 		}
28006185db85Sdougm 	}
28016185db85Sdougm 	if (name != NULL)
2802a3351425Sdougm 		sa_free_attr_string(name);
28036185db85Sdougm 	if (value != NULL)
2804a3351425Sdougm 		sa_free_attr_string(value);
28056185db85Sdougm 	return (ret);
28066185db85Sdougm }
28076185db85Sdougm 
28086185db85Sdougm /*
28096185db85Sdougm  * nfs_get_status()
28106185db85Sdougm  *
28116185db85Sdougm  * What is the current status of the nfsd? We use the SMF state here.
28126185db85Sdougm  * Caller must free the returned value.
28136185db85Sdougm  */
28146185db85Sdougm 
28156185db85Sdougm static char *
28166185db85Sdougm nfs_get_status()
28176185db85Sdougm {
28186185db85Sdougm 	char *state;
28196185db85Sdougm 	state = smf_get_state(NFSD);
28206185db85Sdougm 	return (state != NULL ? state : strdup("-"));
28216185db85Sdougm }
28226185db85Sdougm 
28236185db85Sdougm /*
28246185db85Sdougm  * nfs_space_alias(alias)
28256185db85Sdougm  *
28266185db85Sdougm  * Lookup the space (security) name. If it is default, convert to the
28276185db85Sdougm  * real name.
28286185db85Sdougm  */
28296185db85Sdougm 
28306185db85Sdougm static char *
28316185db85Sdougm nfs_space_alias(char *space)
28326185db85Sdougm {
28336185db85Sdougm 	char *name = space;
28346185db85Sdougm 	seconfig_t secconf;
2835a99982a7Sdougm 
2836a99982a7Sdougm 	/*
2837a99982a7Sdougm 	 * Only the space named "default" is special. If it is used,
2838a99982a7Sdougm 	 * the default needs to be looked up and the real name used.
2839a99982a7Sdougm 	 * This is normally "sys" but could be changed.  We always
2840a99982a7Sdougm 	 * change defautl to the real name.
2841a99982a7Sdougm 	 */
2842a99982a7Sdougm 	if (strcmp(space, "default") == 0 &&
2843a99982a7Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
2844a3351425Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
2845a3351425Sdougm 			name = secconf.sc_name;
28466185db85Sdougm 	}
28476185db85Sdougm 	return (strdup(name));
28486185db85Sdougm }
2849