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 /*
23743a77edSAlan Wright  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
246185db85Sdougm  * Use is subject to license terms.
256185db85Sdougm  */
266185db85Sdougm 
276185db85Sdougm /*
286185db85Sdougm  * NFS specific functions
296185db85Sdougm  */
306185db85Sdougm #include <stdio.h>
316185db85Sdougm #include <string.h>
326185db85Sdougm #include <ctype.h>
336185db85Sdougm #include <stdlib.h>
346185db85Sdougm #include <unistd.h>
356185db85Sdougm #include <zone.h>
366185db85Sdougm #include <errno.h>
376185db85Sdougm #include <locale.h>
386185db85Sdougm #include <signal.h>
396185db85Sdougm #include "libshare.h"
406185db85Sdougm #include "libshare_impl.h"
416185db85Sdougm #include <nfs/export.h>
426185db85Sdougm #include <pwd.h>
436185db85Sdougm #include <limits.h>
446185db85Sdougm #include <libscf.h>
456185db85Sdougm #include "nfslog_config.h"
466185db85Sdougm #include "nfslogtab.h"
476185db85Sdougm #include "libshare_nfs.h"
486185db85Sdougm #include <rpcsvc/daemon_utils.h>
496185db85Sdougm #include <nfs/nfs.h>
50ecd6cf80Smarks #include <nfs/nfssys.h>
516185db85Sdougm 
526185db85Sdougm /* should really be in some global place */
536185db85Sdougm #define	DEF_WIN	30000
546185db85Sdougm #define	OPT_CHUNK	1024
556185db85Sdougm 
566185db85Sdougm int debug = 0;
576185db85Sdougm 
58ecd6cf80Smarks #define	NFS_SERVER_SVC	"svc:/network/nfs/server:default"
596185db85Sdougm 
606185db85Sdougm /* internal functions */
616185db85Sdougm static int nfs_init();
626185db85Sdougm static void nfs_fini();
636185db85Sdougm static int nfs_enable_share(sa_share_t);
64ecd6cf80Smarks static int nfs_disable_share(sa_share_t, char *);
65687915e9Sdougm static int nfs_validate_property(sa_handle_t, 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 *);
74da6c28aaSamw static uint64_t nfs_features();
756185db85Sdougm 
766185db85Sdougm /*
776185db85Sdougm  * ops vector that provides the protocol specific info and operations
786185db85Sdougm  * for share management.
796185db85Sdougm  */
806185db85Sdougm 
816185db85Sdougm struct sa_plugin_ops sa_plugin_ops = {
826185db85Sdougm 	SA_PLUGIN_VERSION,
836185db85Sdougm 	"nfs",
846185db85Sdougm 	nfs_init,
856185db85Sdougm 	nfs_fini,
866185db85Sdougm 	nfs_enable_share,
876185db85Sdougm 	nfs_disable_share,
886185db85Sdougm 	nfs_validate_property,
896185db85Sdougm 	nfs_validate_security_mode,
906185db85Sdougm 	nfs_is_security_opt,
916185db85Sdougm 	nfs_parse_legacy_options,
926185db85Sdougm 	nfs_format_options,
936185db85Sdougm 	nfs_set_proto_prop,
946185db85Sdougm 	nfs_get_proto_set,
956185db85Sdougm 	nfs_get_status,
966185db85Sdougm 	nfs_space_alias,
97da6c28aaSamw 	NULL,	/* update_legacy */
98da6c28aaSamw 	NULL,	/* delete_legacy */
99da6c28aaSamw 	NULL,	/* change_notify */
100da6c28aaSamw 	NULL,	/* enable_resource */
101da6c28aaSamw 	NULL,	/* disable_resource */
102da6c28aaSamw 	nfs_features,
103da6c28aaSamw 	NULL,	/* transient shares */
104da6c28aaSamw 	NULL,	/* notify resource */
1054bff34e3Sthurlow 	NULL,	/* rename_resource */
1064bff34e3Sthurlow 	NULL,	/* run_command */
1074bff34e3Sthurlow 	NULL,	/* command_help */
1084bff34e3Sthurlow 	NULL	/* delete_proto_section */
1096185db85Sdougm };
1106185db85Sdougm 
1116185db85Sdougm /*
1126185db85Sdougm  * list of support services needed
1136185db85Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
1146185db85Sdougm  */
1156185db85Sdougm 
1166185db85Sdougm static char *service_list_default[] =
117*2f172c55SRobert Thurlow 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, REPARSED, NULL };
1186185db85Sdougm static char *service_list_logging[] =
119*2f172c55SRobert Thurlow 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, REPARSED,
120*2f172c55SRobert Thurlow 	    NULL };
1216185db85Sdougm 
1226185db85Sdougm /*
1236185db85Sdougm  * option definitions.  Make sure to keep the #define for the option
1246185db85Sdougm  * index just before the entry it is the index for. Changing the order
1256185db85Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1266185db85Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1276185db85Sdougm  */
1286185db85Sdougm 
1296185db85Sdougm struct option_defs optdefs[] = {
1306185db85Sdougm #define	OPT_RO		0
1316185db85Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1326185db85Sdougm #define	OPT_RW		1
1336185db85Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1346185db85Sdougm #define	OPT_ROOT	2
1356185db85Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1366185db85Sdougm #define	OPT_SECURE	3
1376185db85Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1386185db85Sdougm #define	OPT_ANON	4
1396185db85Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1406185db85Sdougm #define	OPT_WINDOW	5
1416185db85Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1426185db85Sdougm #define	OPT_NOSUID	6
1436185db85Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1446185db85Sdougm #define	OPT_ACLOK	7
1456185db85Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1466185db85Sdougm #define	OPT_NOSUB	8
1476185db85Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1486185db85Sdougm #define	OPT_SEC		9
1496185db85Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1506185db85Sdougm #define	OPT_PUBLIC	10
1516185db85Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1526185db85Sdougm #define	OPT_INDEX	11
1536185db85Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1546185db85Sdougm #define	OPT_LOG		12
1556185db85Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1566185db85Sdougm #define	OPT_CKSUM	13
1576185db85Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
158b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_NONE	14
159b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{SHOPT_NONE, OPT_NONE, OPT_TYPE_ACCLIST},
160b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_ROOT_MAPPING	15
161b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{SHOPT_ROOT_MAPPING, OPT_ROOT_MAPPING, OPT_TYPE_USER},
162b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_CHARSET_MAP	16
163b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{"", OPT_CHARSET_MAP, OPT_TYPE_ACCLIST},
1646185db85Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
165b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_VOLFH	17
1666185db85Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1676185db85Sdougm #endif /* VOLATILE_FH_TEST */
1686185db85Sdougm 	NULL
1696185db85Sdougm };
1706185db85Sdougm 
171b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
172b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Codesets that may need to be converted to UTF-8 for file paths.
173b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Add new names here to add new property support. If we ever get a
174b89a8333Snatalie li - Sun Microsystems - Irvine United States  * way to query the kernel for character sets, this should become
175b89a8333Snatalie li - Sun Microsystems - Irvine United States  * dynamically loaded. Make sure changes here are reflected in
176b89a8333Snatalie li - Sun Microsystems - Irvine United States  * cmd/fs.d/nfs/mountd/nfscmd.c
177b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
178b89a8333Snatalie li - Sun Microsystems - Irvine United States 
179b89a8333Snatalie li - Sun Microsystems - Irvine United States static char *legal_conv[] = {
180b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-cn",
181b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-jp",
182b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-jpms",
183b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-kr",
184b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-tw",
185b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-1",
186b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-2",
187b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-5",
188b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-6",
189b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-7",
190b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-8",
191b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-9",
192b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-13",
193b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-15",
194b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"koi8-r",
195b89a8333Snatalie li - Sun Microsystems - Irvine United States 	NULL
196b89a8333Snatalie li - Sun Microsystems - Irvine United States };
197b89a8333Snatalie li - Sun Microsystems - Irvine United States 
1986185db85Sdougm /*
199a99982a7Sdougm  * list of properties that are related to security flavors.
2006185db85Sdougm  */
2016185db85Sdougm static char *seclist[] = {
2026185db85Sdougm 	SHOPT_RO,
2036185db85Sdougm 	SHOPT_RW,
2046185db85Sdougm 	SHOPT_ROOT,
2056185db85Sdougm 	SHOPT_WINDOW,
206b89a8333Snatalie li - Sun Microsystems - Irvine United States 	SHOPT_NONE,
207b89a8333Snatalie li - Sun Microsystems - Irvine United States 	SHOPT_ROOT_MAPPING,
2086185db85Sdougm 	NULL
2096185db85Sdougm };
2106185db85Sdougm 
2116185db85Sdougm /* structure for list of securities */
2126185db85Sdougm struct securities {
213a99982a7Sdougm 	sa_security_t security;
214a99982a7Sdougm 	struct securities *next;
2156185db85Sdougm };
2166185db85Sdougm 
217b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
218b89a8333Snatalie li - Sun Microsystems - Irvine United States  * findcharset(charset)
219b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
220b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Returns B_TRUE if the charset is a legal conversion otherwise
221b89a8333Snatalie li - Sun Microsystems - Irvine United States  * B_FALSE. This will need to be rewritten to be more efficient when
222b89a8333Snatalie li - Sun Microsystems - Irvine United States  * we have a dynamic list of legal conversions.
223b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
224b89a8333Snatalie li - Sun Microsystems - Irvine United States 
225b89a8333Snatalie li - Sun Microsystems - Irvine United States static boolean_t
226b89a8333Snatalie li - Sun Microsystems - Irvine United States findcharset(char *charset)
227b89a8333Snatalie li - Sun Microsystems - Irvine United States {
228b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int i;
229b89a8333Snatalie li - Sun Microsystems - Irvine United States 
230b89a8333Snatalie li - Sun Microsystems - Irvine United States 	for (i = 0; legal_conv[i] != NULL; i++)
231b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (strcmp(charset, legal_conv[i]) == 0)
232b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (B_TRUE);
233b89a8333Snatalie li - Sun Microsystems - Irvine United States 	return (B_FALSE);
234b89a8333Snatalie li - Sun Microsystems - Irvine United States }
235b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2366185db85Sdougm /*
2376185db85Sdougm  * findopt(name)
2386185db85Sdougm  *
2396185db85Sdougm  * Lookup option "name" in the option table and return the table
2406185db85Sdougm  * index.
2416185db85Sdougm  */
2426185db85Sdougm 
2436185db85Sdougm static int
2446185db85Sdougm findopt(char *name)
2456185db85Sdougm {
2466185db85Sdougm 	int i;
2476185db85Sdougm 	if (name != NULL) {
248a3351425Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
249a3351425Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
250a3351425Sdougm 				return (i);
251a3351425Sdougm 		}
252b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (findcharset(name))
253b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (OPT_CHARSET_MAP);
2546185db85Sdougm 	}
2556185db85Sdougm 	return (-1);
2566185db85Sdougm }
2576185db85Sdougm 
2586185db85Sdougm /*
2596185db85Sdougm  * gettype(name)
2606185db85Sdougm  *
2616185db85Sdougm  * Return the type of option "name".
2626185db85Sdougm  */
2636185db85Sdougm 
2646185db85Sdougm static int
2656185db85Sdougm gettype(char *name)
2666185db85Sdougm {
2676185db85Sdougm 	int optdef;
2686185db85Sdougm 
2696185db85Sdougm 	optdef = findopt(name);
2706185db85Sdougm 	if (optdef != -1)
271a3351425Sdougm 		return (optdefs[optdef].type);
2726185db85Sdougm 	return (OPT_TYPE_ANY);
2736185db85Sdougm }
2746185db85Sdougm 
2756185db85Sdougm /*
2766185db85Sdougm  * nfs_validate_security_mode(mode)
2776185db85Sdougm  *
2786185db85Sdougm  * is the specified mode string a valid one for use with NFS?
2796185db85Sdougm  */
2806185db85Sdougm 
2816185db85Sdougm static int
2826185db85Sdougm nfs_validate_security_mode(char *mode)
2836185db85Sdougm {
2846185db85Sdougm 	seconfig_t secinfo;
2856185db85Sdougm 	int err;
2866185db85Sdougm 
2876185db85Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2886185db85Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2896185db85Sdougm 	if (err == SC_NOERROR)
290a3351425Sdougm 		return (1);
2916185db85Sdougm 	return (0);
2926185db85Sdougm }
2936185db85Sdougm 
2946185db85Sdougm /*
2956185db85Sdougm  * nfs_is_security_opt(tok)
2966185db85Sdougm  *
2976185db85Sdougm  * check to see if tok represents an option that is only valid in some
2986185db85Sdougm  * security flavor.
2996185db85Sdougm  */
3006185db85Sdougm 
3016185db85Sdougm static int
3026185db85Sdougm nfs_is_security_opt(char *tok)
3036185db85Sdougm {
3046185db85Sdougm 	int i;
3056185db85Sdougm 
3066185db85Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
307a3351425Sdougm 		if (strcmp(tok, seclist[i]) == 0)
308a3351425Sdougm 			return (1);
3096185db85Sdougm 	}
3106185db85Sdougm 	return (0);
3116185db85Sdougm }
3126185db85Sdougm 
3136185db85Sdougm /*
3146185db85Sdougm  * find_security(seclist, sec)
3156185db85Sdougm  *
3166185db85Sdougm  * Walk the current list of security flavors and return true if it is
3176185db85Sdougm  * present, else return false.
3186185db85Sdougm  */
3196185db85Sdougm 
3206185db85Sdougm static int
3216185db85Sdougm find_security(struct securities *seclist, sa_security_t sec)
3226185db85Sdougm {
3236185db85Sdougm 	while (seclist != NULL) {
324a3351425Sdougm 		if (seclist->security == sec)
325a3351425Sdougm 			return (1);
326a3351425Sdougm 		seclist = seclist->next;
3276185db85Sdougm 	}
3286185db85Sdougm 	return (0);
3296185db85Sdougm }
3306185db85Sdougm 
3316185db85Sdougm /*
3326185db85Sdougm  * make_security_list(group, securitymodes, proto)
3336185db85Sdougm  *	go through the list of securitymodes and add them to the
3346185db85Sdougm  *	group's list of security optionsets. We also keep a list of
3356185db85Sdougm  *	those optionsets so we don't have to find them later. All of
3366185db85Sdougm  *	these will get copies of the same properties.
3376185db85Sdougm  */
3386185db85Sdougm 
3396185db85Sdougm static struct securities *
3406185db85Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
3416185db85Sdougm {
3426185db85Sdougm 	char *tok, *next = NULL;
3436185db85Sdougm 	struct securities *curp, *headp = NULL, *prev;
3446185db85Sdougm 	sa_security_t check;
3456185db85Sdougm 	int freetok = 0;
3466185db85Sdougm 
3476185db85Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
348a3351425Sdougm 		next = strchr(tok, ':');
349a3351425Sdougm 		if (next != NULL)
350a3351425Sdougm 			*next++ = '\0';
351a3351425Sdougm 		if (strcmp(tok, "default") == 0) {
352a3351425Sdougm 			/* resolve default into the real type */
353a3351425Sdougm 			tok = nfs_space_alias(tok);
354a3351425Sdougm 			freetok = 1;
355a3351425Sdougm 		}
356a3351425Sdougm 		check = sa_get_security(group, tok, proto);
357a3351425Sdougm 
358a3351425Sdougm 		/* add to the security list if it isn't there already */
359a3351425Sdougm 		if (check == NULL || !find_security(headp, check)) {
360a3351425Sdougm 			curp = (struct securities *)calloc(1,
361a3351425Sdougm 			    sizeof (struct securities));
362a3351425Sdougm 			if (curp != NULL) {
363a3351425Sdougm 				if (check == NULL) {
364a3351425Sdougm 					curp->security = sa_create_security(
365a3351425Sdougm 					    group, tok, proto);
366a3351425Sdougm 				} else {
367a3351425Sdougm 					curp->security = check;
368a3351425Sdougm 				}
369a3351425Sdougm 				/*
370a3351425Sdougm 				 * note that the first time through the loop,
371a3351425Sdougm 				 * headp will be NULL and prev will be
372a3351425Sdougm 				 * undefined.  Since headp is NULL, we set
373a3351425Sdougm 				 * both it and prev to the curp (first
374a3351425Sdougm 				 * structure to be allocated).
375a3351425Sdougm 				 *
376a3351425Sdougm 				 * later passes through the loop will have
377a3351425Sdougm 				 * headp not being NULL and prev will be used
378a3351425Sdougm 				 * to allocate at the end of the list.
379a3351425Sdougm 				 */
380a3351425Sdougm 				if (headp == NULL) {
381a3351425Sdougm 					headp = curp;
382a3351425Sdougm 					prev = curp;
383a3351425Sdougm 				} else {
384a3351425Sdougm 					prev->next = curp;
385a3351425Sdougm 					prev = curp;
386a3351425Sdougm 				}
387a3351425Sdougm 			}
3886185db85Sdougm 		}
389a99982a7Sdougm 
390a3351425Sdougm 		if (freetok) {
391a3351425Sdougm 			freetok = 0;
392a3351425Sdougm 			sa_free_attr_string(tok);
393a3351425Sdougm 		}
3946185db85Sdougm 	}
3956185db85Sdougm 	return (headp);
3966185db85Sdougm }
3976185db85Sdougm 
3986185db85Sdougm static void
3996185db85Sdougm free_security_list(struct securities *sec)
4006185db85Sdougm {
4016185db85Sdougm 	struct securities *next;
4026185db85Sdougm 	if (sec != NULL) {
403a3351425Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
404a3351425Sdougm 			next = sec->next;
405a3351425Sdougm 			free(sec);
406a3351425Sdougm 		}
4076185db85Sdougm 	}
4086185db85Sdougm }
4096185db85Sdougm 
4106185db85Sdougm /*
4116185db85Sdougm  * nfs_alistcat(str1, str2, sep)
4126185db85Sdougm  *
4136185db85Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
4146185db85Sdougm  * character. If memory allocation fails, return NULL;
4156185db85Sdougm  */
4166185db85Sdougm 
4176185db85Sdougm static char *
4186185db85Sdougm nfs_alistcat(char *str1, char *str2, char sep)
4196185db85Sdougm {
4206185db85Sdougm 	char *newstr;
4216185db85Sdougm 	size_t len;
4226185db85Sdougm 
4236185db85Sdougm 	len = strlen(str1) + strlen(str2) + 2;
4246185db85Sdougm 	newstr = (char *)malloc(len);
4256185db85Sdougm 	if (newstr != NULL)
426a3351425Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
4276185db85Sdougm 	return (newstr);
4286185db85Sdougm }
4296185db85Sdougm 
4306185db85Sdougm /*
4316185db85Sdougm  * add_security_prop(sec, name, value, persist)
4326185db85Sdougm  *
4336185db85Sdougm  * Add the property to the securities structure. This accumulates
4346185db85Sdougm  * properties for as part of parsing legacy options.
4356185db85Sdougm  */
4366185db85Sdougm 
4376185db85Sdougm static int
4386185db85Sdougm add_security_prop(struct securities *sec, char *name, char *value,
4396185db85Sdougm 			int persist, int iszfs)
4406185db85Sdougm {
4416185db85Sdougm 	sa_property_t prop;
4426185db85Sdougm 	int ret = SA_OK;
4436185db85Sdougm 
4446185db85Sdougm 	for (; sec != NULL; sec = sec->next) {
445a3351425Sdougm 		if (value == NULL) {
446a3351425Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
447a3351425Sdougm 			    strcmp(name, SHOPT_RO) == 0)
448a3351425Sdougm 				value = "*";
449a3351425Sdougm 			else
450a3351425Sdougm 				value = "true";
451a3351425Sdougm 		}
452a99982a7Sdougm 
453a99982a7Sdougm 		/*
454a99982a7Sdougm 		 * Get the existing property, if it exists, so we can
455a99982a7Sdougm 		 * determine what to do with it. The ro/rw/root
456a99982a7Sdougm 		 * properties can be merged if multiple instances of
457a99982a7Sdougm 		 * these properies are given. For example, if "rw"
458a99982a7Sdougm 		 * exists with a value "host1" and a later token of
459a99982a7Sdougm 		 * rw="host2" is seen, the values are merged into a
460a99982a7Sdougm 		 * single rw="host1:host2".
461a99982a7Sdougm 		 */
462a3351425Sdougm 		prop = sa_get_property(sec->security, name);
463a99982a7Sdougm 
464a3351425Sdougm 		if (prop != NULL) {
465a3351425Sdougm 			char *oldvalue;
466a3351425Sdougm 			char *newvalue;
467a99982a7Sdougm 
468a99982a7Sdougm 			/*
469a3351425Sdougm 			 * The security options of ro/rw/root might appear
470a3351425Sdougm 			 * multiple times. If they do, the values need to be
471a3351425Sdougm 			 * merged into an access list. If it was previously
472a3351425Sdougm 			 * empty, the new value alone is added.
473a99982a7Sdougm 			 */
474a3351425Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
475a3351425Sdougm 			if (oldvalue != NULL) {
476a3351425Sdougm 				/*
477a3351425Sdougm 				 * The general case is to concatenate the new
478a3351425Sdougm 				 * value onto the old value for multiple
479a3351425Sdougm 				 * rw(ro/root) properties. A special case
480a3351425Sdougm 				 * exists when either the old or new is the
481a3351425Sdougm 				 * "all" case. In the special case, if both
482a3351425Sdougm 				 * are "all", then it is "all", else if one is
483a3351425Sdougm 				 * an access-list, that replaces the "all".
484a3351425Sdougm 				 */
485a3351425Sdougm 				if (strcmp(oldvalue, "*") == 0) {
486a3351425Sdougm 					/* Replace old value with new value. */
487a3351425Sdougm 					newvalue = strdup(value);
48897df5ac9Sdougm 				} else if (strcmp(value, "*") == 0 ||
48997df5ac9Sdougm 				    strcmp(oldvalue, value) == 0) {
490a3351425Sdougm 					/*
491a3351425Sdougm 					 * Keep old value and ignore
492a3351425Sdougm 					 * the new value.
493a3351425Sdougm 					 */
494a3351425Sdougm 					newvalue = NULL;
495a3351425Sdougm 				} else {
496a3351425Sdougm 					/*
497a3351425Sdougm 					 * Make a new list of old plus new
498a3351425Sdougm 					 * access-list.
499a3351425Sdougm 					 */
500a3351425Sdougm 					newvalue = nfs_alistcat(oldvalue,
501a3351425Sdougm 					    value, ':');
502a3351425Sdougm 				}
503a3351425Sdougm 
504a3351425Sdougm 				if (newvalue != NULL) {
505a3351425Sdougm 					(void) sa_remove_property(prop);
506a3351425Sdougm 					prop = sa_create_property(name,
507a3351425Sdougm 					    newvalue);
508a3351425Sdougm 					ret = sa_add_property(sec->security,
509a3351425Sdougm 					    prop);
510a3351425Sdougm 					free(newvalue);
511a3351425Sdougm 				}
512a3351425Sdougm 				if (oldvalue != NULL)
513a3351425Sdougm 					sa_free_attr_string(oldvalue);
514a3351425Sdougm 			}
515a3351425Sdougm 		} else {
516a3351425Sdougm 			prop = sa_create_property(name, value);
517a99982a7Sdougm 			ret = sa_add_property(sec->security, prop);
5186185db85Sdougm 		}
519a3351425Sdougm 		if (ret == SA_OK && !iszfs) {
520a3351425Sdougm 			ret = sa_commit_properties(sec->security, !persist);
521a3351425Sdougm 		}
5226185db85Sdougm 	}
5236185db85Sdougm 	return (ret);
5246185db85Sdougm }
5256185db85Sdougm 
5266185db85Sdougm /*
5276185db85Sdougm  * check to see if group/share is persistent.
5286185db85Sdougm  */
5296185db85Sdougm static int
5306185db85Sdougm is_persistent(sa_group_t group)
5316185db85Sdougm {
5326185db85Sdougm 	char *type;
5336185db85Sdougm 	int persist = 1;
5346185db85Sdougm 
5356185db85Sdougm 	type = sa_get_group_attr(group, "type");
5366185db85Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
537a3351425Sdougm 		persist = 0;
5386185db85Sdougm 	if (type != NULL)
539a3351425Sdougm 		sa_free_attr_string(type);
5406185db85Sdougm 	return (persist);
5416185db85Sdougm }
5426185db85Sdougm 
5436185db85Sdougm /*
5446185db85Sdougm  * invalid_security(options)
5456185db85Sdougm  *
5466185db85Sdougm  * search option string for any invalid sec= type.
5476185db85Sdougm  * return true (1) if any are not valid else false (0)
5486185db85Sdougm  */
5496185db85Sdougm static int
5506185db85Sdougm invalid_security(char *options)
5516185db85Sdougm {
5526185db85Sdougm 	char *copy, *base, *token, *value;
5536185db85Sdougm 	int ret = 0;
5546185db85Sdougm 
5556185db85Sdougm 	copy = strdup(options);
5566185db85Sdougm 	token = base = copy;
5576185db85Sdougm 	while (token != NULL && ret == 0) {
558a3351425Sdougm 		token = strtok(base, ",");
559a3351425Sdougm 		base = NULL;
560a3351425Sdougm 		if (token != NULL) {
561a3351425Sdougm 			value = strchr(token, '=');
562a3351425Sdougm 			if (value != NULL)
563a3351425Sdougm 				*value++ = '\0';
564a3351425Sdougm 			if (strcmp(token, "sec") == 0) {
565a3351425Sdougm 				/* HAVE security flavors so check them */
566a3351425Sdougm 				char *tok, *next;
567a3351425Sdougm 				for (next = NULL, tok = value; tok != NULL;
568a3351425Sdougm 				    tok = next) {
569a3351425Sdougm 					next = strchr(tok, ':');
570a3351425Sdougm 					if (next != NULL)
571a3351425Sdougm 						*next++ = '\0';
572a3351425Sdougm 					ret = !nfs_validate_security_mode(tok);
573a3351425Sdougm 					if (ret)
574a3351425Sdougm 						break;
575a3351425Sdougm 				}
576a3351425Sdougm 			}
5776185db85Sdougm 		}
5786185db85Sdougm 	}
5796185db85Sdougm 	if (copy != NULL)
580a3351425Sdougm 		free(copy);
5816185db85Sdougm 	return (ret);
5826185db85Sdougm }
5836185db85Sdougm 
5846185db85Sdougm /*
5856185db85Sdougm  * nfs_parse_legacy_options(group, options)
5866185db85Sdougm  *
5876185db85Sdougm  * Parse the old style options into internal format and store on the
5886185db85Sdougm  * specified group.  Group could be a share for full legacy support.
5896185db85Sdougm  */
5906185db85Sdougm 
5916185db85Sdougm static int
5926185db85Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5936185db85Sdougm {
594aba7a40bSdougm 	char *dup;
5956185db85Sdougm 	char *base;
5966185db85Sdougm 	char *token;
5976185db85Sdougm 	sa_optionset_t optionset;
5986185db85Sdougm 	struct securities *security_list = NULL;
5996185db85Sdougm 	sa_property_t prop;
6006185db85Sdougm 	int ret = SA_OK;
6016185db85Sdougm 	int iszfs = 0;
6026185db85Sdougm 	sa_group_t parent;
6036185db85Sdougm 	int persist = 0;
6046185db85Sdougm 	char *lasts;
6056185db85Sdougm 
6066185db85Sdougm 	/* do we have an existing optionset? */
6076185db85Sdougm 	optionset = sa_get_optionset(group, "nfs");
6086185db85Sdougm 	if (optionset == NULL) {
609a3351425Sdougm 		/* didn't find existing optionset so create one */
610a3351425Sdougm 		optionset = sa_create_optionset(group, "nfs");
6116185db85Sdougm 	} else {
6126185db85Sdougm 		/*
613da6c28aaSamw 		 * Have an existing optionset . Ideally, we would need
614da6c28aaSamw 		 * to compare options in order to detect errors. For
615da6c28aaSamw 		 * now, we assume that the first optionset is the
616da6c28aaSamw 		 * correct one and the others will be the same. An
617da6c28aaSamw 		 * empty optionset is the same as no optionset so we
618da6c28aaSamw 		 * don't want to exit in that case. Getting an empty
619da6c28aaSamw 		 * optionset can occur with ZFS property checking.
6206185db85Sdougm 		 */
621da6c28aaSamw 		if (sa_get_property(optionset, NULL) != NULL)
622da6c28aaSamw 			return (ret);
6236185db85Sdougm 	}
6246185db85Sdougm 
6256185db85Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
6266185db85Sdougm 		/*
6276185db85Sdougm 		 * there is a special case of only the option "rw"
6286185db85Sdougm 		 * being the default option. We don't have to do
6296185db85Sdougm 		 * anything.
6306185db85Sdougm 		 */
631a3351425Sdougm 		return (ret);
6326185db85Sdougm 	}
6336185db85Sdougm 
6346185db85Sdougm 	/*
6356185db85Sdougm 	 * check if security types are present and validate them. If
6366185db85Sdougm 	 * any are not legal, fail.
6376185db85Sdougm 	 */
6386185db85Sdougm 
6396185db85Sdougm 	if (invalid_security(options)) {
640a3351425Sdougm 		return (SA_INVALID_SECURITY);
6416185db85Sdougm 	}
6426185db85Sdougm 
6436185db85Sdougm 	/*
6446185db85Sdougm 	 * in order to not attempt to change ZFS properties unless
6456185db85Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
6466185db85Sdougm 	 */
6476185db85Sdougm 	if (sa_is_share(group)) {
648a3351425Sdougm 		char *zfs;
649a3351425Sdougm 		parent = sa_get_parent_group(group);
650a3351425Sdougm 		if (parent != NULL) {
651a3351425Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
652a3351425Sdougm 			if (zfs != NULL) {
653a3351425Sdougm 				sa_free_attr_string(zfs);
654a3351425Sdougm 				iszfs++;
655a3351425Sdougm 			}
6566185db85Sdougm 		}
6576185db85Sdougm 	} else {
658a3351425Sdougm 		iszfs = sa_group_is_zfs(group);
6596185db85Sdougm 	}
6606185db85Sdougm 
661aba7a40bSdougm 	/* We need a copy of options for the next part. */
662aba7a40bSdougm 	dup = strdup(options);
663aba7a40bSdougm 	if (dup == NULL)
664aba7a40bSdougm 		return (SA_NO_MEMORY);
665aba7a40bSdougm 
6666185db85Sdougm 	/*
6676185db85Sdougm 	 * we need to step through each option in the string and then
6686185db85Sdougm 	 * add either the option or the security option as needed. If
6696185db85Sdougm 	 * this is not a persistent share, don't commit to the
670a99982a7Sdougm 	 * repository. If there is an error, we also want to abort the
671a99982a7Sdougm 	 * processing and report it.
6726185db85Sdougm 	 */
6736185db85Sdougm 	persist = is_persistent(group);
6746185db85Sdougm 	base = dup;
6756185db85Sdougm 	token = dup;
6766185db85Sdougm 	lasts = NULL;
677a99982a7Sdougm 	while (token != NULL && ret == SA_OK) {
678a3351425Sdougm 		ret = SA_OK;
679a3351425Sdougm 		token = strtok_r(base, ",", &lasts);
680a3351425Sdougm 		base = NULL;
681a3351425Sdougm 		if (token != NULL) {
682a3351425Sdougm 			char *value;
6836185db85Sdougm 			/*
684a3351425Sdougm 			 * if the option has a value, it will have an '=' to
685a3351425Sdougm 			 * separate the name from the value. The following
686a3351425Sdougm 			 * code will result in value != NULL and token
687a3351425Sdougm 			 * pointing to just the name if there is a value.
6886185db85Sdougm 			 */
689a3351425Sdougm 			value = strchr(token, '=');
690a3351425Sdougm 			if (value != NULL) {
691a3351425Sdougm 				*value++ = '\0';
6926185db85Sdougm 			}
693a3351425Sdougm 			if (strcmp(token, "sec") == 0 ||
694a3351425Sdougm 			    strcmp(token, "secure") == 0) {
6956185db85Sdougm 				/*
696a3351425Sdougm 				 * Once in security parsing, we only
697a3351425Sdougm 				 * do security. We do need to move
698a3351425Sdougm 				 * between the security node and the
699a3351425Sdougm 				 * toplevel. The security tag goes on
700a3351425Sdougm 				 * the root while the following ones
701a3351425Sdougm 				 * go on the security.
7026185db85Sdougm 				 */
703a3351425Sdougm 				if (security_list != NULL) {
704a3351425Sdougm 					/*
705a3351425Sdougm 					 * have an old list so close it and
706a3351425Sdougm 					 * start the new
707a3351425Sdougm 					 */
708a3351425Sdougm 					free_security_list(security_list);
709a3351425Sdougm 				}
710a3351425Sdougm 				if (strcmp(token, "secure") == 0) {
711a3351425Sdougm 					value = "dh";
712a3351425Sdougm 				} else {
713a3351425Sdougm 					if (value == NULL) {
714a3351425Sdougm 						ret = SA_SYNTAX_ERR;
715a3351425Sdougm 						break;
716a3351425Sdougm 					}
717a3351425Sdougm 				}
718a3351425Sdougm 				security_list = make_security_list(group,
719a3351425Sdougm 				    value, "nfs");
7206185db85Sdougm 			} else {
721a3351425Sdougm 				/*
722a3351425Sdougm 				 * Note that the "old" syntax allowed a
723a3351425Sdougm 				 * default security model This must be
724a3351425Sdougm 				 * accounted for and internally converted to
725a3351425Sdougm 				 * "standard" security structure.
726a3351425Sdougm 				 */
727a3351425Sdougm 				if (nfs_is_security_opt(token)) {
728a3351425Sdougm 					if (security_list == NULL) {
729a3351425Sdougm 						/*
730a3351425Sdougm 						 * need to have a
731a3351425Sdougm 						 * security
732a3351425Sdougm 						 * option. This will
733a3351425Sdougm 						 * be "closed" when a
734a3351425Sdougm 						 * defined "sec="
735a3351425Sdougm 						 * option is
736a3351425Sdougm 						 * seen. This is
737a3351425Sdougm 						 * technically an
738a3351425Sdougm 						 * error but will be
739a3351425Sdougm 						 * allowed with
740a3351425Sdougm 						 * warning.
741a3351425Sdougm 						 */
742a3351425Sdougm 						security_list =
743a3351425Sdougm 						    make_security_list(group,
744a3351425Sdougm 						    "default",
745a3351425Sdougm 						    "nfs");
746a3351425Sdougm 					}
747a3351425Sdougm 					if (security_list != NULL) {
748a3351425Sdougm 						ret = add_security_prop(
749a3351425Sdougm 						    security_list, token,
750a3351425Sdougm 						    value, persist, iszfs);
751a3351425Sdougm 					} else {
752a3351425Sdougm 						ret = SA_NO_MEMORY;
753a3351425Sdougm 					}
754a3351425Sdougm 				} else {
755a3351425Sdougm 					/* regular options */
756a3351425Sdougm 					if (value == NULL) {
757a3351425Sdougm 						if (strcmp(token, SHOPT_RW) ==
758a3351425Sdougm 						    0 || strcmp(token,
759a3351425Sdougm 						    SHOPT_RO) == 0) {
760a3351425Sdougm 							value = "*";
761a3351425Sdougm 						} else {
762a3351425Sdougm 							value = "global";
763a3351425Sdougm 							if (strcmp(token,
764a3351425Sdougm 							    SHOPT_LOG) != 0) {
765a3351425Sdougm 								value = "true";
766a3351425Sdougm 							}
767a3351425Sdougm 						}
768a3351425Sdougm 					}
769d6405362Sdougm 					/*
770d6405362Sdougm 					 * In all cases, create the
771d6405362Sdougm 					 * property specified. If the
772d6405362Sdougm 					 * value was NULL, the default
773d6405362Sdougm 					 * value will have been
774d6405362Sdougm 					 * substituted.
775d6405362Sdougm 					 */
776d6405362Sdougm 					prop = sa_create_property(token, value);
777d6405362Sdougm 					ret =  sa_add_property(optionset, prop);
778d6405362Sdougm 					if (ret != SA_OK)
779d6405362Sdougm 						break;
780d6405362Sdougm 
781a3351425Sdougm 					if (!iszfs) {
782a3351425Sdougm 						ret = sa_commit_properties(
783a3351425Sdougm 						    optionset, !persist);
784a3351425Sdougm 					}
785a3351425Sdougm 				}
7866185db85Sdougm 			}
7876185db85Sdougm 		}
7886185db85Sdougm 	}
7896185db85Sdougm 	if (security_list != NULL)
790a3351425Sdougm 		free_security_list(security_list);
791aba7a40bSdougm 
792aba7a40bSdougm 	free(dup);
7936185db85Sdougm 	return (ret);
7946185db85Sdougm }
7956185db85Sdougm 
7966185db85Sdougm /*
7976185db85Sdougm  * is_a_number(number)
7986185db85Sdougm  *
7996185db85Sdougm  * is the string a number in one of the forms we want to use?
8006185db85Sdougm  */
8016185db85Sdougm 
8026185db85Sdougm static int
8036185db85Sdougm is_a_number(char *number)
8046185db85Sdougm {
8056185db85Sdougm 	int ret = 1;
8066185db85Sdougm 	int hex = 0;
8076185db85Sdougm 
8086185db85Sdougm 	if (strncmp(number, "0x", 2) == 0) {
809a3351425Sdougm 		number += 2;
810a3351425Sdougm 		hex = 1;
811a3351425Sdougm 	} else if (*number == '-') {
812a3351425Sdougm 		number++; /* skip the minus */
813a3351425Sdougm 	}
8146185db85Sdougm 	while (ret == 1 && *number != '\0') {
815a3351425Sdougm 		if (hex) {
816a3351425Sdougm 			ret = isxdigit(*number++);
817a3351425Sdougm 		} else {
818a3351425Sdougm 			ret = isdigit(*number++);
819a3351425Sdougm 		}
8206185db85Sdougm 	}
8216185db85Sdougm 	return (ret);
8226185db85Sdougm }
8236185db85Sdougm 
8246185db85Sdougm /*
8256185db85Sdougm  * Look for the specified tag in the configuration file. If it is found,
8266185db85Sdougm  * enable logging and set the logging configuration information for exp.
8276185db85Sdougm  */
8286185db85Sdougm static void
8296185db85Sdougm configlog(struct exportdata *exp, char *tag)
8306185db85Sdougm {
8316185db85Sdougm 	nfsl_config_t *configlist = NULL, *configp;
8326185db85Sdougm 	int error = 0;
8336185db85Sdougm 	char globaltag[] = DEFAULTTAG;
8346185db85Sdougm 
8356185db85Sdougm 	/*
8366185db85Sdougm 	 * Sends config errors to stderr
8376185db85Sdougm 	 */
8386185db85Sdougm 	nfsl_errs_to_syslog = B_FALSE;
8396185db85Sdougm 
8406185db85Sdougm 	/*
8416185db85Sdougm 	 * get the list of configuration settings
8426185db85Sdougm 	 */
8436185db85Sdougm 	error = nfsl_getconfig_list(&configlist);
8446185db85Sdougm 	if (error) {
8456185db85Sdougm 		(void) fprintf(stderr,
846a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
847a3351425Sdougm 		    strerror(error));
8486185db85Sdougm 	}
8496185db85Sdougm 
8506185db85Sdougm 	if (tag == NULL)
8516185db85Sdougm 		tag = globaltag;
8526185db85Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
8536185db85Sdougm 		nfsl_freeconfig_list(&configlist);
8546185db85Sdougm 		(void) fprintf(stderr,
855a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
8566185db85Sdougm 		/* bad configuration */
8576185db85Sdougm 		error = ENOENT;
8586185db85Sdougm 		goto err;
8596185db85Sdougm 	}
8606185db85Sdougm 
8616185db85Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
8626185db85Sdougm 		error = ENOMEM;
8636185db85Sdougm 		goto out;
8646185db85Sdougm 	}
8656185db85Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
8666185db85Sdougm 		error = ENOMEM;
8676185db85Sdougm 		goto out;
8686185db85Sdougm 	}
8696185db85Sdougm 	exp->ex_flags |= EX_LOG;
8706185db85Sdougm 	if (configp->nc_rpclogpath != NULL)
8716185db85Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8726185db85Sdougm out:
8736185db85Sdougm 	if (configlist != NULL)
874a3351425Sdougm 		nfsl_freeconfig_list(&configlist);
8756185db85Sdougm 
8766185db85Sdougm err:
8776185db85Sdougm 	if (error != 0) {
8786185db85Sdougm 		if (exp->ex_flags != NULL)
8796185db85Sdougm 			free(exp->ex_tag);
8806185db85Sdougm 		if (exp->ex_log_buffer != NULL)
8816185db85Sdougm 			free(exp->ex_log_buffer);
8826185db85Sdougm 		(void) fprintf(stderr,
883a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
884a3351425Sdougm 		    strerror(error));
8856185db85Sdougm 	}
8866185db85Sdougm }
8876185db85Sdougm 
8886185db85Sdougm /*
8896185db85Sdougm  * fill_export_from_optionset(export, optionset)
8906185db85Sdougm  *
8916185db85Sdougm  * In order to share, we need to set all the possible general options
8926185db85Sdougm  * into the export structure. Share info will be filled in by the
8936185db85Sdougm  * caller. Various property values get turned into structure specific
8946185db85Sdougm  * values.
8956185db85Sdougm  */
8966185db85Sdougm 
8976185db85Sdougm static int
8986185db85Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
8996185db85Sdougm {
9006185db85Sdougm 	sa_property_t option;
9016185db85Sdougm 	int ret = SA_OK;
9026185db85Sdougm 
9036185db85Sdougm 	for (option = sa_get_property(optionset, NULL);
904a3351425Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
905a3351425Sdougm 		char *name;
906a3351425Sdougm 		char *value;
907a3351425Sdougm 		uint32_t val;
9086185db85Sdougm 
909a3351425Sdougm 		/*
910a3351425Sdougm 		 * since options may be set/reset multiple times, always do an
911a3351425Sdougm 		 * explicit set or clear of the option. This allows defaults
912da6c28aaSamw 		 * to be set and then the protocol specific to override.
913a3351425Sdougm 		 */
9146185db85Sdougm 
915a3351425Sdougm 		name = sa_get_property_attr(option, "type");
916a3351425Sdougm 		value = sa_get_property_attr(option, "value");
917a3351425Sdougm 		switch (findopt(name)) {
918a3351425Sdougm 		case OPT_ANON:
919a3351425Sdougm 			if (value != NULL && is_a_number(value)) {
920a3351425Sdougm 				val = strtoul(value, NULL, 0);
921a3351425Sdougm 			} else {
922a3351425Sdougm 				struct passwd *pw;
923a3351425Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
924a3351425Sdougm 				if (pw != NULL) {
925a3351425Sdougm 					val = pw->pw_uid;
926a3351425Sdougm 				} else {
927a3351425Sdougm 					val = UID_NOBODY;
928a3351425Sdougm 				}
929a3351425Sdougm 				endpwent();
930a3351425Sdougm 			}
931a3351425Sdougm 			export->ex_anon = val;
932a3351425Sdougm 			break;
933a3351425Sdougm 		case OPT_NOSUID:
934a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
935a3351425Sdougm 			    strcmp(value, "1") == 0))
936a3351425Sdougm 				export->ex_flags |= EX_NOSUID;
937a3351425Sdougm 			else
938a3351425Sdougm 				export->ex_flags &= ~EX_NOSUID;
939a3351425Sdougm 			break;
940a3351425Sdougm 		case OPT_ACLOK:
941a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
942a3351425Sdougm 			    strcmp(value, "1") == 0))
943a3351425Sdougm 				export->ex_flags |= EX_ACLOK;
944a3351425Sdougm 			else
945a3351425Sdougm 				export->ex_flags &= ~EX_ACLOK;
946a3351425Sdougm 			break;
947a3351425Sdougm 		case OPT_NOSUB:
948a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
949a3351425Sdougm 			    strcmp(value, "1") == 0))
950a3351425Sdougm 				export->ex_flags |= EX_NOSUB;
951a3351425Sdougm 			else
952a3351425Sdougm 				export->ex_flags &= ~EX_NOSUB;
953a3351425Sdougm 			break;
954a3351425Sdougm 		case OPT_PUBLIC:
955a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
956a3351425Sdougm 			    strcmp(value, "1") == 0))
957a3351425Sdougm 				export->ex_flags |= EX_PUBLIC;
958a3351425Sdougm 			else
959a3351425Sdougm 				export->ex_flags &= ~EX_PUBLIC;
960a3351425Sdougm 			break;
961a3351425Sdougm 		case OPT_INDEX:
962a3351425Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
963a3351425Sdougm 			    strchr(value, '/') != NULL)) {
964a3351425Sdougm 				/* this is an error */
965a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
966a3351425Sdougm 				    "NFS: index=\"%s\" not valid;"
967a3351425Sdougm 				    "must be a filename.\n"),
968a3351425Sdougm 				    value);
969a3351425Sdougm 				break;
970a3351425Sdougm 			}
971a3351425Sdougm 			if (value != NULL && *value != '\0' &&
972a3351425Sdougm 			    strcmp(value, ".") != 0) {
973a3351425Sdougm 				/* valid index file string */
974a3351425Sdougm 				if (export->ex_index != NULL) {
975a3351425Sdougm 					/* left over from "default" */
976a3351425Sdougm 					free(export->ex_index);
977a3351425Sdougm 				}
978a3351425Sdougm 				/* remember to free */
979a3351425Sdougm 				export->ex_index = strdup(value);
980a3351425Sdougm 				if (export->ex_index == NULL) {
981a3351425Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
982a3351425Sdougm 					    "NFS: out of memory setting "
983a3351425Sdougm 					    "index property\n"));
984a3351425Sdougm 					break;
985a3351425Sdougm 				}
986a3351425Sdougm 				export->ex_flags |= EX_INDEX;
987a3351425Sdougm 			}
988a3351425Sdougm 			break;
989a3351425Sdougm 		case OPT_LOG:
990a3351425Sdougm 			if (value == NULL)
991a3351425Sdougm 				value = strdup("global");
992a3351425Sdougm 			if (value != NULL)
993a3351425Sdougm 				configlog(export,
994a3351425Sdougm 				    strlen(value) ? value : "global");
995a3351425Sdougm 			break;
996b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_CHARSET_MAP:
997b89a8333Snatalie li - Sun Microsystems - Irvine United States 			/*
998b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * Set EX_CHARMAP when there is at least one
999b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * charmap conversion property. This will get
1000b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * checked by the nfs server when it needs to.
1001b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 */
1002b89a8333Snatalie li - Sun Microsystems - Irvine United States 			export->ex_flags |= EX_CHARMAP;
1003b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
1004a3351425Sdougm 		default:
1005a3351425Sdougm 			/* have a syntactic error */
1006549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1007a3351425Sdougm 			    "NFS: unrecognized option %s=%s\n"),
1008a3351425Sdougm 			    name, value != NULL ? value : "");
10096185db85Sdougm 			break;
10106185db85Sdougm 		}
1011a3351425Sdougm 		if (name != NULL)
1012a3351425Sdougm 			sa_free_attr_string(name);
10136185db85Sdougm 		if (value != NULL)
1014a3351425Sdougm 			sa_free_attr_string(value);
10156185db85Sdougm 	}
10166185db85Sdougm 	return (ret);
10176185db85Sdougm }
10186185db85Sdougm 
10196185db85Sdougm /*
10206185db85Sdougm  * cleanup_export(export)
10216185db85Sdougm  *
10226185db85Sdougm  * Cleanup the allocated areas so we don't leak memory
10236185db85Sdougm  */
10246185db85Sdougm 
10256185db85Sdougm static void
10266185db85Sdougm cleanup_export(struct exportdata *export)
10276185db85Sdougm {
10286185db85Sdougm 	int i;
10296185db85Sdougm 
10306185db85Sdougm 	if (export->ex_index != NULL)
1031a3351425Sdougm 		free(export->ex_index);
10326185db85Sdougm 	if (export->ex_secinfo != NULL) {
1033a3351425Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
1034a3351425Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
1035a3351425Sdougm 				free(export->ex_secinfo[i].s_rootnames);
1036a3351425Sdougm 		free(export->ex_secinfo);
10376185db85Sdougm 	}
10386185db85Sdougm }
10396185db85Sdougm 
10406185db85Sdougm /*
10416185db85Sdougm  * Given a seconfig entry and a colon-separated
10426185db85Sdougm  * list of names, allocate an array big enough
10436185db85Sdougm  * to hold the root list, then convert each name to
10446185db85Sdougm  * a principal name according to the security
10456185db85Sdougm  * info and assign it to an array element.
10466185db85Sdougm  * Return the array and its size.
10476185db85Sdougm  */
10486185db85Sdougm static caddr_t *
10496185db85Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
10506185db85Sdougm {
10516185db85Sdougm 	caddr_t *a;
10526185db85Sdougm 	int c, i;
10536185db85Sdougm 	char *host, *p;
10546185db85Sdougm 
10556185db85Sdougm 	/*
10566185db85Sdougm 	 * Count the number of strings in the list.
10576185db85Sdougm 	 * This is the number of colon separators + 1.
10586185db85Sdougm 	 */
10596185db85Sdougm 	c = 1;
10606185db85Sdougm 	for (p = list; *p; p++)
10616185db85Sdougm 		if (*p == ':')
10626185db85Sdougm 			c++;
10636185db85Sdougm 	*count = c;
10646185db85Sdougm 
10656185db85Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
10666185db85Sdougm 	if (a == NULL) {
1067549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1068a3351425Sdougm 		    "get_rootnames: no memory\n"));
10696185db85Sdougm 	} else {
1070a3351425Sdougm 		for (i = 0; i < c; i++) {
1071a3351425Sdougm 			host = strtok(list, ":");
1072a3351425Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
1073a3351425Sdougm 				free(a);
1074a3351425Sdougm 				a = NULL;
1075a3351425Sdougm 				break;
1076a3351425Sdougm 			}
1077a3351425Sdougm 			list = NULL;
10786185db85Sdougm 		}
10796185db85Sdougm 	}
10806185db85Sdougm 
10816185db85Sdougm 	return (a);
10826185db85Sdougm }
10836185db85Sdougm 
10846185db85Sdougm /*
10856185db85Sdougm  * fill_security_from_secopts(sp, secopts)
10866185db85Sdougm  *
10876185db85Sdougm  * Fill the secinfo structure from the secopts optionset.
10886185db85Sdougm  */
10896185db85Sdougm 
10906185db85Sdougm static int
10916185db85Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
10926185db85Sdougm {
10936185db85Sdougm 	sa_property_t prop;
10946185db85Sdougm 	char *type;
10956185db85Sdougm 	int longform;
1096a99982a7Sdougm 	int err = SC_NOERROR;
1097b89a8333Snatalie li - Sun Microsystems - Irvine United States 	uint32_t val;
10986185db85Sdougm 
10996185db85Sdougm 	type = sa_get_security_attr(secopts, "sectype");
11006185db85Sdougm 	if (type != NULL) {
1101a3351425Sdougm 		/* named security type needs secinfo to be filled in */
1102a3351425Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
1103a3351425Sdougm 		sa_free_attr_string(type);
1104a3351425Sdougm 		if (err != SC_NOERROR)
1105a3351425Sdougm 			return (err);
11066185db85Sdougm 	} else {
1107a3351425Sdougm 		/* default case */
1108a3351425Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
1109a3351425Sdougm 		if (err != SC_NOERROR)
1110a3351425Sdougm 			return (err);
11116185db85Sdougm 	}
11126185db85Sdougm 
1113a99982a7Sdougm 	err = SA_OK;
11146185db85Sdougm 	for (prop = sa_get_property(secopts, NULL);
1115a3351425Sdougm 	    prop != NULL && err == SA_OK;
1116a3351425Sdougm 	    prop = sa_get_next_property(prop)) {
1117a3351425Sdougm 		char *name;
1118a3351425Sdougm 		char *value;
11196185db85Sdougm 
1120a3351425Sdougm 		name = sa_get_property_attr(prop, "type");
1121a3351425Sdougm 		value = sa_get_property_attr(prop, "value");
11226185db85Sdougm 
1123a3351425Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
11246185db85Sdougm 
1125a3351425Sdougm 		switch (findopt(name)) {
1126a3351425Sdougm 		case OPT_RO:
1127a3351425Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
1128a3351425Sdougm 			break;
1129a3351425Sdougm 		case OPT_RW:
1130a3351425Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
1131a3351425Sdougm 			break;
1132a3351425Sdougm 		case OPT_ROOT:
1133a3351425Sdougm 			sp->s_flags |= M_ROOT;
1134a3351425Sdougm 			/*
1135a3351425Sdougm 			 * if we are using AUTH_UNIX, handle like other things
1136a3351425Sdougm 			 * such as RO/RW
1137a3351425Sdougm 			 */
1138a3351425Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
1139a3351425Sdougm 				continue;
1140a3351425Sdougm 			/* not AUTH_UNIX */
1141a3351425Sdougm 			if (value != NULL) {
1142a3351425Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
1143a3351425Sdougm 				    value, &sp->s_rootcnt);
1144a3351425Sdougm 				if (sp->s_rootnames == NULL) {
1145a3351425Sdougm 					err = SA_BAD_VALUE;
1146a3351425Sdougm 					(void) fprintf(stderr,
1147a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
1148a3351425Sdougm 					    "Bad root list\n"));
1149a3351425Sdougm 				}
1150a3351425Sdougm 			}
1151a3351425Sdougm 			break;
1152b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_NONE:
1153b89a8333Snatalie li - Sun Microsystems - Irvine United States 			sp->s_flags |= M_NONE;
1154b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
1155a3351425Sdougm 		case OPT_WINDOW:
1156a3351425Sdougm 			if (value != NULL) {
1157a3351425Sdougm 				sp->s_window = atoi(value);
1158a3351425Sdougm 				/* just in case */
1159a3351425Sdougm 				if (sp->s_window < 0)
1160a3351425Sdougm 					sp->s_window = DEF_WIN;
1161a3351425Sdougm 			}
1162a3351425Sdougm 			break;
1163b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_ROOT_MAPPING:
1164b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (value != NULL && is_a_number(value)) {
1165b89a8333Snatalie li - Sun Microsystems - Irvine United States 				val = strtoul(value, NULL, 0);
1166b89a8333Snatalie li - Sun Microsystems - Irvine United States 			} else {
1167b89a8333Snatalie li - Sun Microsystems - Irvine United States 				struct passwd *pw;
1168b89a8333Snatalie li - Sun Microsystems - Irvine United States 				pw = getpwnam(value != NULL ? value : "nobody");
1169b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (pw != NULL) {
1170b89a8333Snatalie li - Sun Microsystems - Irvine United States 					val = pw->pw_uid;
1171b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else {
1172b89a8333Snatalie li - Sun Microsystems - Irvine United States 					val = UID_NOBODY;
1173b89a8333Snatalie li - Sun Microsystems - Irvine United States 				}
1174b89a8333Snatalie li - Sun Microsystems - Irvine United States 				endpwent();
1175b89a8333Snatalie li - Sun Microsystems - Irvine United States 			}
1176b89a8333Snatalie li - Sun Microsystems - Irvine United States 			sp->s_rootid = val;
1177b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
1178a3351425Sdougm 		default:
1179a3351425Sdougm 			break;
11806185db85Sdougm 		}
1181a3351425Sdougm 		if (name != NULL)
1182a3351425Sdougm 			sa_free_attr_string(name);
1183a3351425Sdougm 		if (value != NULL)
1184a3351425Sdougm 			sa_free_attr_string(value);
11856185db85Sdougm 	}
11866185db85Sdougm 	/* if rw/ro options not set, use default of RW */
11876185db85Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
1188a3351425Sdougm 		sp->s_flags |= M_RW;
11896185db85Sdougm 	return (err);
11906185db85Sdougm }
11916185db85Sdougm 
11926185db85Sdougm /*
11936185db85Sdougm  * This is for testing only
11946185db85Sdougm  * It displays the export structure that
11956185db85Sdougm  * goes into the kernel.
11966185db85Sdougm  */
11976185db85Sdougm static void
11986185db85Sdougm printarg(char *path, struct exportdata *ep)
11996185db85Sdougm {
12006185db85Sdougm 	int i, j;
12016185db85Sdougm 	struct secinfo *sp;
12026185db85Sdougm 
12036185db85Sdougm 	if (debug == 0)
1204a3351425Sdougm 		return;
12056185db85Sdougm 
12066185db85Sdougm 	(void) printf("%s:\n", path);
12076185db85Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
12086185db85Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
1209549ec3ffSdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
12106185db85Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
12116185db85Sdougm 	if (ep->ex_flags & EX_NOSUID)
12126185db85Sdougm 		(void) printf("NOSUID ");
12136185db85Sdougm 	if (ep->ex_flags & EX_ACLOK)
12146185db85Sdougm 		(void) printf("ACLOK ");
12156185db85Sdougm 	if (ep->ex_flags & EX_PUBLIC)
12166185db85Sdougm 		(void) printf("PUBLIC ");
12176185db85Sdougm 	if (ep->ex_flags & EX_NOSUB)
12186185db85Sdougm 		(void) printf("NOSUB ");
12196185db85Sdougm 	if (ep->ex_flags & EX_LOG)
12206185db85Sdougm 		(void) printf("LOG ");
1221b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (ep->ex_flags & EX_CHARMAP)
1222b89a8333Snatalie li - Sun Microsystems - Irvine United States 		(void) printf("CHARMAP ");
12236185db85Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
12246185db85Sdougm 		(void) printf("LOG_ALLOPS ");
12256185db85Sdougm 	if (ep->ex_flags == 0)
12266185db85Sdougm 		(void) printf("(none)");
12276185db85Sdougm 	(void) 	printf("\n");
12286185db85Sdougm 	if (ep->ex_flags & EX_LOG) {
12296185db85Sdougm 		(void) printf("\tex_log_buffer = %s\n",
1230a3351425Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
12316185db85Sdougm 		(void) printf("\tex_tag = %s\n",
1232a3351425Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
12336185db85Sdougm 	}
12346185db85Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
12356185db85Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
12366185db85Sdougm 	(void) printf("\n");
12376185db85Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
12386185db85Sdougm 		sp = &ep->ex_secinfo[i];
12396185db85Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
12406185db85Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
12416185db85Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
12426185db85Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
12436185db85Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
12446185db85Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
12456185db85Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
1246b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (sp->s_flags & M_NONE) (void) printf("M_NONE ");
12476185db85Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
12486185db85Sdougm 		(void) printf("\n");
12496185db85Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
1250b89a8333Snatalie li - Sun Microsystems - Irvine United States 		(void) printf("\t\ts_rootid = %d\n", sp->s_rootid);
12516185db85Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
12526185db85Sdougm 		(void) fflush(stdout);
12536185db85Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
12546185db85Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
1255a3351425Sdougm 			    sp->s_rootnames[j] : "<null>");
12566185db85Sdougm 		(void) printf("\n\n");
12576185db85Sdougm 	}
12586185db85Sdougm }
12596185db85Sdougm 
12606185db85Sdougm /*
12616185db85Sdougm  * count_security(opts)
12626185db85Sdougm  *
12636185db85Sdougm  * Count the number of security types (flavors). The optionset has
12646185db85Sdougm  * been populated with the security flavors as a holding mechanism.
12656185db85Sdougm  * We later use this number to allocate data structures.
12666185db85Sdougm  */
12676185db85Sdougm 
12686185db85Sdougm static int
12696185db85Sdougm count_security(sa_optionset_t opts)
12706185db85Sdougm {
12716185db85Sdougm 	int count = 0;
12726185db85Sdougm 	sa_property_t prop;
12736185db85Sdougm 	if (opts != NULL) {
1274a3351425Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
1275a3351425Sdougm 		    prop = sa_get_next_property(prop)) {
1276a3351425Sdougm 			count++;
1277a3351425Sdougm 		}
12786185db85Sdougm 	}
12796185db85Sdougm 	return (count);
12806185db85Sdougm }
12816185db85Sdougm 
12826185db85Sdougm /*
12836185db85Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
12846185db85Sdougm  *
12856185db85Sdougm  * provides a mechanism to format NFS properties into legacy output
12866185db85Sdougm  * format. If the buffer would overflow, it is reallocated and grown
12876185db85Sdougm  * as appropriate. Special cases of converting internal form of values
12886185db85Sdougm  * to those used by "share" are done. this function does one property
12896185db85Sdougm  * at a time.
12906185db85Sdougm  */
12916185db85Sdougm 
1292aed5d200Sdougm static int
12936185db85Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
12946185db85Sdougm 			sa_property_t prop, int sep)
12956185db85Sdougm {
12966185db85Sdougm 	char *name;
12976185db85Sdougm 	char *value;
12986185db85Sdougm 	int curlen;
12996185db85Sdougm 	char *buff = *rbuff;
13006185db85Sdougm 	size_t buffsize = *rbuffsize;
1301aed5d200Sdougm 	int printed = B_FALSE;
13026185db85Sdougm 
13036185db85Sdougm 	name = sa_get_property_attr(prop, "type");
13046185db85Sdougm 	value = sa_get_property_attr(prop, "value");
13056185db85Sdougm 	if (buff != NULL)
1306a3351425Sdougm 		curlen = strlen(buff);
13076185db85Sdougm 	else
1308a3351425Sdougm 		curlen = 0;
13096185db85Sdougm 	if (name != NULL) {
1310a3351425Sdougm 		int len;
1311a3351425Sdougm 		len = strlen(name) + sep;
13126185db85Sdougm 
13136185db85Sdougm 		/*
13146185db85Sdougm 		 * A future RFE would be to replace this with more
13156185db85Sdougm 		 * generic code and to possibly handle more types.
13166185db85Sdougm 		 */
1317a3351425Sdougm 		switch (gettype(name)) {
1318a3351425Sdougm 		case OPT_TYPE_BOOLEAN:
1319aed5d200Sdougm 			/*
1320aed5d200Sdougm 			 * For NFS, boolean value of FALSE means it
1321aed5d200Sdougm 			 * doesn't show up in the option list at all.
1322aed5d200Sdougm 			 */
1323a3351425Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
1324aed5d200Sdougm 				goto skip;
1325aed5d200Sdougm 			if (value != NULL) {
1326a3351425Sdougm 				sa_free_attr_string(value);
1327aed5d200Sdougm 				value = NULL;
1328aed5d200Sdougm 			}
1329a3351425Sdougm 			break;
1330a3351425Sdougm 		case OPT_TYPE_ACCLIST:
1331a3351425Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
1332a3351425Sdougm 				sa_free_attr_string(value);
1333a3351425Sdougm 				value = NULL;
1334a3351425Sdougm 			} else {
1335a3351425Sdougm 				if (value != NULL)
1336a3351425Sdougm 					len += 1 + strlen(value);
1337a3351425Sdougm 			}
1338a3351425Sdougm 			break;
1339a3351425Sdougm 		case OPT_TYPE_LOGTAG:
1340a3351425Sdougm 			if (value != NULL && strlen(value) == 0) {
1341a3351425Sdougm 				sa_free_attr_string(value);
1342a3351425Sdougm 				value = NULL;
1343a3351425Sdougm 			} else {
1344a3351425Sdougm 				if (value != NULL)
1345a3351425Sdougm 					len += 1 + strlen(value);
1346a3351425Sdougm 			}
1347a3351425Sdougm 			break;
1348a3351425Sdougm 		default:
1349a3351425Sdougm 			if (value != NULL)
1350a3351425Sdougm 				len += 1 + strlen(value);
1351a3351425Sdougm 			break;
13526185db85Sdougm 		}
1353a3351425Sdougm 		while (buffsize <= (curlen + len)) {
1354a3351425Sdougm 			/* need more room */
1355a3351425Sdougm 			buffsize += incr;
1356a3351425Sdougm 			buff = realloc(buff, buffsize);
1357a3351425Sdougm 			if (buff == NULL) {
1358a3351425Sdougm 				/* realloc failed so free everything */
1359a3351425Sdougm 				if (*rbuff != NULL)
1360a3351425Sdougm 					free(*rbuff);
1361a3351425Sdougm 			}
1362a3351425Sdougm 			*rbuff = buff;
1363a3351425Sdougm 			*rbuffsize = buffsize;
1364aed5d200Sdougm 			if (buff == NULL)
1365aed5d200Sdougm 				goto skip;
1366aed5d200Sdougm 
13676185db85Sdougm 		}
1368aed5d200Sdougm 
1369a3351425Sdougm 		if (buff == NULL)
1370aed5d200Sdougm 			goto skip;
1371aed5d200Sdougm 
1372a3351425Sdougm 		if (value == NULL) {
1373a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1374a3351425Sdougm 			    "%s%s", sep ? "," : "",
1375a3351425Sdougm 			    name, value != NULL ? value : "");
13766185db85Sdougm 		} else {
1377a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1378a3351425Sdougm 			    "%s%s=%s", sep ? "," : "",
1379a3351425Sdougm 			    name, value != NULL ? value : "");
13806185db85Sdougm 		}
1381aed5d200Sdougm 		printed = B_TRUE;
13826185db85Sdougm 	}
1383aed5d200Sdougm skip:
13846185db85Sdougm 	if (name != NULL)
1385a3351425Sdougm 		sa_free_attr_string(name);
13866185db85Sdougm 	if (value != NULL)
1387a3351425Sdougm 		sa_free_attr_string(value);
1388aed5d200Sdougm 	return (printed);
13896185db85Sdougm }
13906185db85Sdougm 
13916185db85Sdougm /*
13926185db85Sdougm  * nfs_format_options(group, hier)
13936185db85Sdougm  *
13946185db85Sdougm  * format all the options on the group into an old-style option
13956185db85Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
13966185db85Sdougm  * options.
13976185db85Sdougm  */
13986185db85Sdougm 
13996185db85Sdougm static char *
14006185db85Sdougm nfs_format_options(sa_group_t group, int hier)
14016185db85Sdougm {
14026185db85Sdougm 	sa_optionset_t options = NULL;
1403a3351425Sdougm 	sa_optionset_t secoptions = NULL;
14046185db85Sdougm 	sa_property_t prop, secprop;
1405a3351425Sdougm 	sa_security_t security = NULL;
14066185db85Sdougm 	char *buff;
14076185db85Sdougm 	size_t buffsize;
1408a3351425Sdougm 	char *sectype = NULL;
1409a3351425Sdougm 	int sep = 0;
1410a3351425Sdougm 
1411a3351425Sdougm 
1412a3351425Sdougm 	buff = malloc(OPT_CHUNK);
1413a3351425Sdougm 	if (buff == NULL) {
1414a3351425Sdougm 		return (NULL);
1415a3351425Sdougm 	}
1416a3351425Sdougm 
1417a3351425Sdougm 	buff[0] = '\0';
1418a3351425Sdougm 	buffsize = OPT_CHUNK;
1419a3351425Sdougm 
1420a3351425Sdougm 	/*
1421a3351425Sdougm 	 * We may have a an optionset relative to this item. format
1422a3351425Sdougm 	 * these if we find them and then add any security definitions.
1423a3351425Sdougm 	 */
14246185db85Sdougm 
14256185db85Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
14266185db85Sdougm 
14276185db85Sdougm 	/*
1428a3351425Sdougm 	 * do the default set first but skip any option that is also
1429a3351425Sdougm 	 * in the protocol specific optionset.
14306185db85Sdougm 	 */
1431a3351425Sdougm 	if (options != NULL) {
1432a3351425Sdougm 		for (prop = sa_get_property(options, NULL);
1433a3351425Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
14346185db85Sdougm 			/*
1435a3351425Sdougm 			 * use this one since we skipped any
1436a3351425Sdougm 			 * of these that were also in
1437a3351425Sdougm 			 * optdefault
14386185db85Sdougm 			 */
1439aed5d200Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1440aed5d200Sdougm 			    prop, sep))
1441aed5d200Sdougm 				sep = 1;
1442a3351425Sdougm 			if (buff == NULL) {
1443a3351425Sdougm 				/*
1444a3351425Sdougm 				 * buff could become NULL if there
1445a3351425Sdougm 				 * isn't enough memory for
1446a3351425Sdougm 				 * nfs_sprint_option to realloc()
1447a3351425Sdougm 				 * as necessary. We can't really
1448a3351425Sdougm 				 * do anything about it at this
1449a3351425Sdougm 				 * point so we return NULL.  The
1450a3351425Sdougm 				 * caller should handle the
1451a3351425Sdougm 				 * failure.
1452a3351425Sdougm 				 */
14536185db85Sdougm 				if (options != NULL)
1454a3351425Sdougm 					sa_free_derived_optionset(
1455a3351425Sdougm 					    options);
14566185db85Sdougm 				return (buff);
14576185db85Sdougm 			}
1458a3351425Sdougm 		}
1459a3351425Sdougm 	}
1460a3351425Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1461a3351425Sdougm 	    "nfs", hier);
1462a3351425Sdougm 	if (secoptions != NULL) {
1463a3351425Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
1464a3351425Sdougm 		    secprop != NULL;
1465a3351425Sdougm 		    secprop = sa_get_next_property(secprop)) {
1466a3351425Sdougm 			sectype = sa_get_property_attr(secprop, "type");
1467a3351425Sdougm 			security =
1468a3351425Sdougm 			    (sa_security_t)sa_get_derived_security(
1469a3351425Sdougm 			    group, sectype, "nfs", hier);
1470a3351425Sdougm 			if (security != NULL) {
1471a3351425Sdougm 				if (sectype != NULL) {
1472a3351425Sdougm 					prop = sa_create_property(
1473a3351425Sdougm 					    "sec", sectype);
1474aed5d200Sdougm 					if (prop == NULL)
1475aed5d200Sdougm 						goto err;
1476aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1477aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1478aed5d200Sdougm 						sep = 1;
1479a3351425Sdougm 					(void) sa_remove_property(prop);
1480aed5d200Sdougm 					if (buff == NULL)
1481aed5d200Sdougm 						goto err;
1482a3351425Sdougm 				}
1483a3351425Sdougm 				for (prop = sa_get_property(security,
1484a3351425Sdougm 				    NULL); prop != NULL;
1485a3351425Sdougm 				    prop = sa_get_next_property(prop)) {
1486aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1487aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1488aed5d200Sdougm 						sep = 1;
1489a3351425Sdougm 					if (buff == NULL)
1490a3351425Sdougm 						goto err;
1491a3351425Sdougm 				}
1492a3351425Sdougm 				sa_free_derived_optionset(security);
1493a3351425Sdougm 			}
1494a3351425Sdougm 			if (sectype != NULL)
1495a3351425Sdougm 				sa_free_attr_string(sectype);
14966185db85Sdougm 		}
14976185db85Sdougm 		sa_free_derived_optionset(secoptions);
14986185db85Sdougm 	}
1499a3351425Sdougm 
15006185db85Sdougm 	if (options != NULL)
1501a3351425Sdougm 		sa_free_derived_optionset(options);
1502a3351425Sdougm 	return (buff);
1503a3351425Sdougm 
1504a3351425Sdougm err:
1505a3351425Sdougm 	/*
1506a3351425Sdougm 	 * If we couldn't allocate memory for option printing, we need
1507a3351425Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
1508a3351425Sdougm 	 */
1509a3351425Sdougm 	if (secoptions != NULL)
1510a3351425Sdougm 		sa_free_derived_optionset(secoptions);
1511a3351425Sdougm 	if (security != NULL)
1512a3351425Sdougm 		sa_free_derived_optionset(security);
1513a3351425Sdougm 	if (sectype != NULL)
1514a3351425Sdougm 		sa_free_attr_string(sectype);
1515a3351425Sdougm 	if (options != NULL)
1516a3351425Sdougm 		sa_free_derived_optionset(options);
15178d7e4166Sjose borrego 	return (NULL);
15186185db85Sdougm }
1519a3351425Sdougm 
15206185db85Sdougm /*
15216185db85Sdougm  * Append an entry to the nfslogtab file
15226185db85Sdougm  */
15236185db85Sdougm static int
15246185db85Sdougm nfslogtab_add(dir, buffer, tag)
15256185db85Sdougm 	char *dir, *buffer, *tag;
15266185db85Sdougm {
15276185db85Sdougm 	FILE *f;
15286185db85Sdougm 	struct logtab_ent lep;
15296185db85Sdougm 	int error = 0;
15306185db85Sdougm 
15316185db85Sdougm 	/*
15326185db85Sdougm 	 * Open the file for update and create it if necessary.
15336185db85Sdougm 	 * This may leave the I/O offset at the end of the file,
15346185db85Sdougm 	 * so rewind back to the beginning of the file.
15356185db85Sdougm 	 */
15366185db85Sdougm 	f = fopen(NFSLOGTAB, "a+");
15376185db85Sdougm 	if (f == NULL) {
15386185db85Sdougm 		error = errno;
15396185db85Sdougm 		goto out;
15406185db85Sdougm 	}
15416185db85Sdougm 	rewind(f);
15426185db85Sdougm 
15436185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1544549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1545a3351425Sdougm 		    "share complete, however failed to lock %s "
1546a3351425Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
15476185db85Sdougm 		error = -1;
15486185db85Sdougm 		goto out;
15496185db85Sdougm 	}
15506185db85Sdougm 
15516185db85Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
1552549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1553a3351425Sdougm 		    "share complete, however could not deactivate "
1554a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15556185db85Sdougm 		error = -1;
15566185db85Sdougm 		goto out;
15576185db85Sdougm 	}
15586185db85Sdougm 
15596185db85Sdougm 	/*
15606185db85Sdougm 	 * Remove entries matching buffer and sharepoint since we're
15616185db85Sdougm 	 * going to replace it with perhaps an entry with a new tag.
15626185db85Sdougm 	 */
15636185db85Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
1564549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1565a3351425Sdougm 		    "share complete, however could not remove matching "
1566a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15676185db85Sdougm 		error = -1;
15686185db85Sdougm 		goto out;
15696185db85Sdougm 	}
15706185db85Sdougm 
15716185db85Sdougm 	/*
15726185db85Sdougm 	 * Deactivate all active entries matching this sharepoint
15736185db85Sdougm 	 */
15746185db85Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
1575549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1576a3351425Sdougm 		    "share complete, however could not deactivate matching "
1577a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15786185db85Sdougm 		error = -1;
15796185db85Sdougm 		goto out;
15806185db85Sdougm 	}
15816185db85Sdougm 
15826185db85Sdougm 	lep.le_buffer = buffer;
15836185db85Sdougm 	lep.le_path = dir;
15846185db85Sdougm 	lep.le_tag = tag;
15856185db85Sdougm 	lep.le_state = LES_ACTIVE;
15866185db85Sdougm 
15876185db85Sdougm 	/*
15886185db85Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
15896185db85Sdougm 	 */
15906185db85Sdougm 	if (logtab_putent(f, &lep) < 0) {
1591549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1592a3351425Sdougm 		    "share complete, however could not add %s to %s\n"),
1593a3351425Sdougm 		    dir, NFSLOGTAB);
15946185db85Sdougm 		error = -1;
15956185db85Sdougm 	}
15966185db85Sdougm 
15976185db85Sdougm out:
15986185db85Sdougm 	if (f != NULL)
15996185db85Sdougm 		(void) fclose(f);
16006185db85Sdougm 	return (error);
16016185db85Sdougm }
16026185db85Sdougm 
16036185db85Sdougm /*
16046185db85Sdougm  * Deactivate an entry from the nfslogtab file
16056185db85Sdougm  */
16066185db85Sdougm static int
16076185db85Sdougm nfslogtab_deactivate(path)
16086185db85Sdougm 	char *path;
16096185db85Sdougm {
16106185db85Sdougm 	FILE *f;
16116185db85Sdougm 	int error = 0;
16126185db85Sdougm 
16136185db85Sdougm 	f = fopen(NFSLOGTAB, "r+");
16146185db85Sdougm 	if (f == NULL) {
16156185db85Sdougm 		error = errno;
16166185db85Sdougm 		goto out;
16176185db85Sdougm 	}
16186185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
16196185db85Sdougm 		error = errno;
1620549ec3ffSdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
1621a3351425Sdougm 		    "share complete, however could not lock %s for "
1622a3351425Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
16236185db85Sdougm 		goto out;
16246185db85Sdougm 	}
16256185db85Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
16266185db85Sdougm 		error = -1;
16276185db85Sdougm 		(void) fprintf(stderr,
1628a3351425Sdougm 		    dgettext(TEXT_DOMAIN,
1629a3351425Sdougm 		    "share complete, however could not "
1630a3351425Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
16316185db85Sdougm 		goto out;
16326185db85Sdougm 	}
16336185db85Sdougm 
16346185db85Sdougm out:	if (f != NULL)
16356185db85Sdougm 		(void) fclose(f);
16366185db85Sdougm 
16376185db85Sdougm 	return (error);
16386185db85Sdougm }
16396185db85Sdougm 
1640546405c3Sdougm /*
1641546405c3Sdougm  * check_public(group, skipshare)
1642546405c3Sdougm  *
1643546405c3Sdougm  * Check the group for any shares that have the public property
1644546405c3Sdougm  * enabled. We skip "skipshare" since that is the one we are
1645546405c3Sdougm  * working with. This is a separate function to make handling
1646546405c3Sdougm  * subgroups simpler. Returns true if there is a share with public.
1647546405c3Sdougm  */
1648546405c3Sdougm static int
1649546405c3Sdougm check_public(sa_group_t group, sa_share_t skipshare)
1650546405c3Sdougm {
1651546405c3Sdougm 	int exists = B_FALSE;
1652546405c3Sdougm 	sa_share_t share;
1653546405c3Sdougm 	sa_optionset_t opt;
1654546405c3Sdougm 	sa_property_t prop;
1655546405c3Sdougm 	char *shared;
1656546405c3Sdougm 
1657546405c3Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
1658546405c3Sdougm 	    share = sa_get_next_share(share)) {
1659546405c3Sdougm 		if (share == skipshare)
1660546405c3Sdougm 			continue;
1661546405c3Sdougm 
1662546405c3Sdougm 		opt = sa_get_optionset(share, "nfs");
1663546405c3Sdougm 		if (opt == NULL)
1664546405c3Sdougm 			continue;
1665546405c3Sdougm 		prop = sa_get_property(opt, "public");
1666546405c3Sdougm 		if (prop == NULL)
1667546405c3Sdougm 			continue;
1668546405c3Sdougm 		shared = sa_get_share_attr(share, "shared");
1669546405c3Sdougm 		if (shared != NULL) {
1670546405c3Sdougm 			exists = strcmp(shared, "true") == 0;
1671546405c3Sdougm 			sa_free_attr_string(shared);
1672546405c3Sdougm 			if (exists == B_TRUE)
1673546405c3Sdougm 				break;
1674546405c3Sdougm 		}
1675546405c3Sdougm 	}
1676546405c3Sdougm 
1677546405c3Sdougm 	return (exists);
1678546405c3Sdougm }
1679546405c3Sdougm 
16806185db85Sdougm /*
1681687915e9Sdougm  * public_exists(handle, share)
16826185db85Sdougm  *
16836185db85Sdougm  * check to see if public option is set on any other share than the
1684546405c3Sdougm  * one specified. Need to check zfs sub-groups as well as the top
1685546405c3Sdougm  * level groups.
16866185db85Sdougm  */
16876185db85Sdougm static int
1688687915e9Sdougm public_exists(sa_handle_t handle, sa_share_t skipshare)
16896185db85Sdougm {
1690687915e9Sdougm 	sa_group_t group = NULL;
1691549ec3ffSdougm 
16923b61b335Sdougm 	/*
16933b61b335Sdougm 	 * If we don't have a handle, we can only do syntax check. We
16943b61b335Sdougm 	 * can't check against other shares so we assume OK and will
16953b61b335Sdougm 	 * catch the problem only when we actually try to apply it.
16963b61b335Sdougm 	 */
1697549ec3ffSdougm 	if (handle == NULL)
16983b61b335Sdougm 		return (SA_OK);
1699549ec3ffSdougm 
1700687915e9Sdougm 	if (skipshare != NULL) {
1701687915e9Sdougm 		group = sa_get_parent_group(skipshare);
1702687915e9Sdougm 		if (group == NULL)
1703687915e9Sdougm 			return (SA_NO_SUCH_GROUP);
1704687915e9Sdougm 	}
1705687915e9Sdougm 
1706549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
17076185db85Sdougm 	    group = sa_get_next_group(group)) {
1708546405c3Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
1709546405c3Sdougm 		if (sa_group_is_zfs(group)) {
1710546405c3Sdougm 			sa_group_t subgroup;
1711546405c3Sdougm 			for (subgroup = sa_get_sub_group(group);
1712546405c3Sdougm 			    subgroup != NULL;
1713546405c3Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
1714546405c3Sdougm 				if (check_public(subgroup, skipshare))
1715546405c3Sdougm 					return (B_TRUE);
17166185db85Sdougm 			}
1717546405c3Sdougm 		} else {
1718546405c3Sdougm 			if (check_public(group, skipshare))
1719546405c3Sdougm 				return (B_TRUE);
17206185db85Sdougm 		}
17216185db85Sdougm 	}
1722546405c3Sdougm 	return (B_FALSE);
17236185db85Sdougm }
17246185db85Sdougm 
17256185db85Sdougm /*
17266185db85Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
17276185db85Sdougm  * implementation that it is to enable the share. This entails
17286185db85Sdougm  * converting the path and options into the appropriate ioctl
17296185db85Sdougm  * calls. It is assumed that all error checking of paths, etc. were
17306185db85Sdougm  * done earlier.
17316185db85Sdougm  */
17326185db85Sdougm static int
17336185db85Sdougm nfs_enable_share(sa_share_t share)
17346185db85Sdougm {
17356185db85Sdougm 	struct exportdata export;
17366185db85Sdougm 	sa_optionset_t secoptlist;
17376185db85Sdougm 	struct secinfo *sp;
17386185db85Sdougm 	int num_secinfo;
17396185db85Sdougm 	sa_optionset_t opt;
17406185db85Sdougm 	sa_security_t sec;
17416185db85Sdougm 	sa_property_t prop;
17426185db85Sdougm 	char *path;
17436185db85Sdougm 	int err = SA_OK;
1744546405c3Sdougm 	int i;
1745ecd6cf80Smarks 	int iszfs;
1746687915e9Sdougm 	sa_handle_t handle;
17476185db85Sdougm 
17486185db85Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
17496185db85Sdougm 	(void) signal(SIGSYS, SIG_IGN);
17506185db85Sdougm 
17516185db85Sdougm 	/* get the path since it is important in several places */
17526185db85Sdougm 	path = sa_get_share_attr(share, "path");
17536185db85Sdougm 	if (path == NULL)
1754a3351425Sdougm 		return (SA_NO_SUCH_PATH);
17556185db85Sdougm 
1756ecd6cf80Smarks 	iszfs = sa_path_is_zfs(path);
17576185db85Sdougm 	/*
17586185db85Sdougm 	 * find the optionsets and security sets.  There may not be
17596185db85Sdougm 	 * any or there could be one or two for each of optionset and
17606185db85Sdougm 	 * security may have multiple, one per security type per
17616185db85Sdougm 	 * protocol type.
17626185db85Sdougm 	 */
17636185db85Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
17646185db85Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
17656185db85Sdougm 	if (secoptlist != NULL)
1766a3351425Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
17676185db85Sdougm 	else
1768a3351425Sdougm 		num_secinfo = 1;
17696185db85Sdougm 
17706185db85Sdougm 	/*
17716185db85Sdougm 	 * walk through the options and fill in the structure
17726185db85Sdougm 	 * appropriately.
17736185db85Sdougm 	 */
17746185db85Sdougm 
17756185db85Sdougm 	(void) memset(&export, '\0', sizeof (export));
17766185db85Sdougm 
17776185db85Sdougm 	/*
17786185db85Sdougm 	 * do non-security options first since there is only one after
17796185db85Sdougm 	 * the derived group is constructed.
17806185db85Sdougm 	 */
17816185db85Sdougm 	export.ex_version = EX_CURRENT_VERSION;
17826185db85Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
17836185db85Sdougm 	export.ex_index = NULL;
17846185db85Sdougm 	export.ex_path = path;
17856185db85Sdougm 	export.ex_pathlen = strlen(path) + 1;
17866185db85Sdougm 
17876185db85Sdougm 	if (opt != NULL)
1788a3351425Sdougm 		err = fill_export_from_optionset(&export, opt);
17896185db85Sdougm 
17906185db85Sdougm 	/*
17916185db85Sdougm 	 * check to see if "public" is set. If it is, then make sure
17926185db85Sdougm 	 * no other share has it set. If it is already used, fail.
17936185db85Sdougm 	 */
17946185db85Sdougm 
1795687915e9Sdougm 	handle = sa_find_group_handle((sa_group_t)share);
1796687915e9Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
1797a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1798a3351425Sdougm 		    "NFS: Cannot share more than one file "
1799a3351425Sdougm 		    "system with 'public' property\n"));
1800a3351425Sdougm 		err = SA_NOT_ALLOWED;
1801a3351425Sdougm 		goto out;
18026185db85Sdougm 	}
18036185db85Sdougm 
1804546405c3Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
18056185db85Sdougm 	if (sp == NULL) {
1806a3351425Sdougm 		err = SA_NO_MEMORY;
1807546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1808546405c3Sdougm 		    "NFS: NFS: no memory for security\n"));
1809546405c3Sdougm 		goto out;
1810546405c3Sdougm 	}
1811546405c3Sdougm 	export.ex_secinfo = sp;
1812546405c3Sdougm 	/* get default secinfo */
1813546405c3Sdougm 	export.ex_seccnt = num_secinfo;
1814546405c3Sdougm 	/*
1815546405c3Sdougm 	 * since we must have one security option defined, we
1816546405c3Sdougm 	 * init to the default and then override as we find
1817546405c3Sdougm 	 * defined security options. This handles the case
1818546405c3Sdougm 	 * where we have no defined options but we need to set
1819546405c3Sdougm 	 * up one.
1820546405c3Sdougm 	 */
1821546405c3Sdougm 	sp[0].s_window = DEF_WIN;
1822546405c3Sdougm 	sp[0].s_rootnames = NULL;
1823546405c3Sdougm 	/* setup a default in case no properties defined */
1824546405c3Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1825546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1826546405c3Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
1827546405c3Sdougm 		    "get default security mode\n"));
1828546405c3Sdougm 		err = SA_CONFIG_ERR;
1829546405c3Sdougm 	}
1830546405c3Sdougm 	if (secoptlist != NULL) {
1831546405c3Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
1832546405c3Sdougm 		    prop != NULL && i < num_secinfo;
1833546405c3Sdougm 		    prop = sa_get_next_property(prop), i++) {
1834546405c3Sdougm 			char *sectype;
1835a3351425Sdougm 				sectype = sa_get_property_attr(prop, "type");
1836546405c3Sdougm 			/*
1837546405c3Sdougm 			 * if sectype is NULL, we probably
1838546405c3Sdougm 			 * have a memory problem and can't get
1839546405c3Sdougm 			 * the correct values. Rather than
1840546405c3Sdougm 			 * exporting with incorrect security,
1841546405c3Sdougm 			 * don't share it.
1842546405c3Sdougm 			 */
1843546405c3Sdougm 			if (sectype == NULL) {
1844546405c3Sdougm 				err = SA_NO_MEMORY;
1845546405c3Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1846546405c3Sdougm 				    "NFS: Cannot share %s: "
1847546405c3Sdougm 				    "no memory\n"), path);
1848546405c3Sdougm 				goto out;
1849a3351425Sdougm 			}
1850546405c3Sdougm 			sec = (sa_security_t)sa_get_derived_security(
1851546405c3Sdougm 			    share, sectype, "nfs", 1);
1852546405c3Sdougm 			sp[i].s_window = DEF_WIN;
1853546405c3Sdougm 			sp[i].s_rootcnt = 0;
1854546405c3Sdougm 			sp[i].s_rootnames = NULL;
1855546405c3Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
1856546405c3Sdougm 			if (sec != NULL)
1857546405c3Sdougm 				sa_free_derived_security(sec);
1858546405c3Sdougm 			if (sectype != NULL)
1859546405c3Sdougm 				sa_free_attr_string(sectype);
18606185db85Sdougm 		}
1861546405c3Sdougm 	}
1862546405c3Sdougm 	/*
1863546405c3Sdougm 	 * when we get here, we can do the exportfs system call and
1864546405c3Sdougm 	 * initiate thinsg. We probably want to enable the nfs.server
1865546405c3Sdougm 	 * service first if it isn't running within SMF.
1866546405c3Sdougm 	 */
1867546405c3Sdougm 	/* check nfs.server status and start if needed */
1868546405c3Sdougm 	/* now add the share to the internal tables */
1869546405c3Sdougm 	printarg(path, &export);
1870546405c3Sdougm 	/*
1871546405c3Sdougm 	 * call the exportfs system call which is implemented
1872546405c3Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
1873546405c3Sdougm 	 */
1874ecd6cf80Smarks 	if (iszfs) {
1875ecd6cf80Smarks 		struct exportfs_args ea;
1876ecd6cf80Smarks 		share_t sh;
1877ecd6cf80Smarks 		char *str;
1878ecd6cf80Smarks 		priv_set_t *priv_effective;
1879ecd6cf80Smarks 		int privileged;
1880ecd6cf80Smarks 
1881ecd6cf80Smarks 		/*
1882ecd6cf80Smarks 		 * If we aren't a privileged user
1883ecd6cf80Smarks 		 * and NFS server service isn't running
1884ecd6cf80Smarks 		 * then print out an error message
1885ecd6cf80Smarks 		 * and return EPERM
1886ecd6cf80Smarks 		 */
1887ecd6cf80Smarks 
1888ecd6cf80Smarks 		priv_effective = priv_allocset();
1889ecd6cf80Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
1890ecd6cf80Smarks 
1891ecd6cf80Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
1892ecd6cf80Smarks 		priv_freeset(priv_effective);
1893ecd6cf80Smarks 
1894ecd6cf80Smarks 		if (!privileged &&
1895ecd6cf80Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
1896ecd6cf80Smarks 			err = 0;
1897ecd6cf80Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
1898ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1899ecd6cf80Smarks 				    "NFS: Cannot share remote "
1900ecd6cf80Smarks 				    "filesystem: %s\n"), path);
1901ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1902ecd6cf80Smarks 				    "NFS: Service needs to be enabled "
1903ecd6cf80Smarks 				    "by a privileged user\n"));
1904ecd6cf80Smarks 				err = SA_SYSTEM_ERR;
1905ecd6cf80Smarks 				errno = EPERM;
1906ecd6cf80Smarks 			}
1907ecd6cf80Smarks 			free(str);
1908ecd6cf80Smarks 		}
1909ecd6cf80Smarks 
1910ecd6cf80Smarks 		if (err == 0) {
1911ecd6cf80Smarks 			ea.dname = path;
1912ecd6cf80Smarks 			ea.uex = &export;
1913ecd6cf80Smarks 
1914ecd6cf80Smarks 			sa_sharetab_fill_zfs(share, &sh, "nfs");
1915743a77edSAlan Wright 			err = sa_share_zfs(share, NULL, path, &sh,
1916da6c28aaSamw 			    &ea, ZFS_SHARE_NFS);
1917a63214d6SBill Krier 			if (err != SA_OK) {
1918a63214d6SBill Krier 				errno = err;
1919a63214d6SBill Krier 				err = -1;
1920a63214d6SBill Krier 			}
1921ecd6cf80Smarks 			sa_emptyshare(&sh);
1922ecd6cf80Smarks 		}
1923ecd6cf80Smarks 	} else {
1924ecd6cf80Smarks 		err = exportfs(path, &export);
1925ecd6cf80Smarks 	}
1926ecd6cf80Smarks 
1927ecd6cf80Smarks 	if (err < 0) {
1928546405c3Sdougm 		err = SA_SYSTEM_ERR;
1929546405c3Sdougm 		switch (errno) {
1930546405c3Sdougm 		case EREMOTE:
1931546405c3Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1932ecd6cf80Smarks 			    "NFS: Cannot share filesystems "
1933ecd6cf80Smarks 			    "in non-global zones: %s\n"), path);
1934ecd6cf80Smarks 			err = SA_NOT_SUPPORTED;
1935546405c3Sdougm 			break;
1936546405c3Sdougm 		case EPERM:
1937546405c3Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
1938a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1939ecd6cf80Smarks 				    "NFS: Cannot share file systems "
1940546405c3Sdougm 				    "in non-global zones: %s\n"), path);
1941546405c3Sdougm 				err = SA_NOT_SUPPORTED;
1942a3351425Sdougm 				break;
1943a3351425Sdougm 			}
1944546405c3Sdougm 			err = SA_NO_PERMISSION;
1945546405c3Sdougm 			/* FALLTHROUGH */
1946546405c3Sdougm 		default:
1947546405c3Sdougm 			break;
19486185db85Sdougm 		}
1949546405c3Sdougm 	} else {
1950546405c3Sdougm 		/* update sharetab with an add/modify */
1951ecd6cf80Smarks 		if (!iszfs) {
1952ecd6cf80Smarks 			(void) sa_update_sharetab(share, "nfs");
1953ecd6cf80Smarks 		}
19546185db85Sdougm 	}
19556185db85Sdougm 
19566185db85Sdougm 	if (err == SA_OK) {
19576185db85Sdougm 		/*
19586185db85Sdougm 		 * enable services as needed. This should probably be
19596185db85Sdougm 		 * done elsewhere in order to minimize the calls to
19606185db85Sdougm 		 * check services.
19616185db85Sdougm 		 */
19626185db85Sdougm 		/*
19636185db85Sdougm 		 * check to see if logging and other services need to
19646185db85Sdougm 		 * be triggered, but only if there wasn't an
19656185db85Sdougm 		 * error. This is probably where sharetab should be
19666185db85Sdougm 		 * updated with the NFS specific entry.
19676185db85Sdougm 		 */
1968a3351425Sdougm 		if (export.ex_flags & EX_LOG) {
1969a3351425Sdougm 			/* enable logging */
1970a3351425Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
1971a3351425Sdougm 			    export.ex_tag) != 0) {
1972a3351425Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1973a3351425Sdougm 				    "Could not enable logging for %s\n"),
1974a3351425Sdougm 				    path);
1975a3351425Sdougm 			}
1976a3351425Sdougm 			_check_services(service_list_logging);
1977a3351425Sdougm 		} else {
1978a3351425Sdougm 			/*
1979a3351425Sdougm 			 * don't have logging so remove it from file. It might
1980a3351425Sdougm 			 * not be thre, but that doesn't matter.
1981a3351425Sdougm 			 */
1982a3351425Sdougm 			(void) nfslogtab_deactivate(path);
1983a3351425Sdougm 			_check_services(service_list_default);
19846185db85Sdougm 		}
19856185db85Sdougm 	}
19866185db85Sdougm 
19876185db85Sdougm out:
19886185db85Sdougm 	if (path != NULL)
1989a3351425Sdougm 		free(path);
19906185db85Sdougm 
19916185db85Sdougm 	cleanup_export(&export);
19926185db85Sdougm 	if (opt != NULL)
1993a3351425Sdougm 		sa_free_derived_optionset(opt);
19946185db85Sdougm 	if (secoptlist != NULL)
1995a3351425Sdougm 		(void) sa_destroy_optionset(secoptlist);
19966185db85Sdougm 	return (err);
19976185db85Sdougm }
19986185db85Sdougm 
19996185db85Sdougm /*
20008e314a44Sdougm  * nfs_disable_share(share, path)
20016185db85Sdougm  *
20028e314a44Sdougm  * Unshare the specified share. Note that "path" is the same path as
20038e314a44Sdougm  * what is in the "share" object. It is passed in to avoid an
20048e314a44Sdougm  * additional lookup. A missing "path" value makes this a no-op
20058e314a44Sdougm  * function.
20066185db85Sdougm  */
20076185db85Sdougm static int
2008ecd6cf80Smarks nfs_disable_share(sa_share_t share, char *path)
20096185db85Sdougm {
20106185db85Sdougm 	int err;
20116185db85Sdougm 	int ret = SA_OK;
2012ecd6cf80Smarks 	int iszfs;
20138e314a44Sdougm 	sa_group_t parent;
20145b6e0c46Sdougm 	sa_handle_t handle;
2015ecd6cf80Smarks 
20168e314a44Sdougm 	if (path == NULL)
20178e314a44Sdougm 		return (ret);
2018ecd6cf80Smarks 
20198e314a44Sdougm 	/*
20208e314a44Sdougm 	 * If the share is in a ZFS group we need to handle it
20218e314a44Sdougm 	 * differently.  Just being on a ZFS file system isn't
20228e314a44Sdougm 	 * enough since we may be in a legacy share case.
20238e314a44Sdougm 	 */
20248e314a44Sdougm 	parent = sa_get_parent_group(share);
20258e314a44Sdougm 	iszfs = sa_group_is_zfs(parent);
20268e314a44Sdougm 	if (iszfs) {
20278e314a44Sdougm 		struct exportfs_args ea;
20288e314a44Sdougm 		share_t sh = { 0 };
20298e314a44Sdougm 		ea.dname = path;
20308e314a44Sdougm 		ea.uex = NULL;
20318e314a44Sdougm 		sh.sh_path = path;
20328e314a44Sdougm 		sh.sh_fstype = "nfs";
20338e314a44Sdougm 
2034743a77edSAlan Wright 		err = sa_share_zfs(share, NULL, path, &sh,
20358e314a44Sdougm 		    &ea, ZFS_UNSHARE_NFS);
2036a63214d6SBill Krier 		if (err != SA_OK) {
2037a63214d6SBill Krier 			errno = err;
2038a63214d6SBill Krier 			err = -1;
2039a63214d6SBill Krier 		}
20408e314a44Sdougm 	} else {
20418e314a44Sdougm 		err = exportfs(path, NULL);
20428e314a44Sdougm 	}
20438e314a44Sdougm 	if (err < 0) {
20448e314a44Sdougm 		/*
20458e314a44Sdougm 		 * TBD: only an error in some
20468e314a44Sdougm 		 * cases - need better analysis
20478e314a44Sdougm 		 */
20488e314a44Sdougm 		switch (errno) {
20498e314a44Sdougm 		case EPERM:
20508e314a44Sdougm 		case EACCES:
20518e314a44Sdougm 			ret = SA_NO_PERMISSION;
20528e314a44Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
20538e314a44Sdougm 				ret = SA_NOT_SUPPORTED;
20548e314a44Sdougm 			}
20558e314a44Sdougm 			break;
20568e314a44Sdougm 		case EINVAL:
20578e314a44Sdougm 		case ENOENT:
20588e314a44Sdougm 			ret = SA_NO_SUCH_PATH;
2059ecd6cf80Smarks 			break;
2060a63214d6SBill Krier 		default:
2061a63214d6SBill Krier 			ret = SA_SYSTEM_ERR;
2062ecd6cf80Smarks 			break;
20636185db85Sdougm 		}
20646185db85Sdougm 	}
20658e314a44Sdougm 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
20665b6e0c46Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
20678e314a44Sdougm 		if (!iszfs)
20685b6e0c46Sdougm 			(void) sa_delete_sharetab(handle, path, "nfs");
20698e314a44Sdougm 		/* just in case it was logged */
20708e314a44Sdougm 		(void) nfslogtab_deactivate(path);
20718e314a44Sdougm 	}
20726185db85Sdougm 	return (ret);
20736185db85Sdougm }
20746185db85Sdougm 
20756185db85Sdougm /*
2076b89a8333Snatalie li - Sun Microsystems - Irvine United States  * check_rorwnone(v1, v2, v3)
2077b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
2078b89a8333Snatalie li - Sun Microsystems - Irvine United States  * check ro vs rw vs none values.  Over time this may get beefed up.
2079b89a8333Snatalie li - Sun Microsystems - Irvine United States  * for now it just does simple checks. v1 is never NULL but v2 or v3
2080b89a8333Snatalie li - Sun Microsystems - Irvine United States  * could be.
20816185db85Sdougm  */
20826185db85Sdougm 
20836185db85Sdougm static int
2084b89a8333Snatalie li - Sun Microsystems - Irvine United States check_rorwnone(char *v1, char *v2, char *v3)
20856185db85Sdougm {
20866185db85Sdougm 	int ret = SA_OK;
2087b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (v2 != NULL && strcmp(v1, v2) == 0)
2088b89a8333Snatalie li - Sun Microsystems - Irvine United States 		ret = SA_VALUE_CONFLICT;
2089b89a8333Snatalie li - Sun Microsystems - Irvine United States 	else if (v3 != NULL && strcmp(v1, v3) == 0)
2090a3351425Sdougm 		ret = SA_VALUE_CONFLICT;
2091b89a8333Snatalie li - Sun Microsystems - Irvine United States 
20926185db85Sdougm 	return (ret);
20936185db85Sdougm }
20946185db85Sdougm 
20956185db85Sdougm /*
2096687915e9Sdougm  * nfs_validate_property(handle, property, parent)
20976185db85Sdougm  *
20986185db85Sdougm  * Check that the property has a legitimate value for its type.
20996185db85Sdougm  */
21006185db85Sdougm 
21016185db85Sdougm static int
2102687915e9Sdougm nfs_validate_property(sa_handle_t handle, sa_property_t property,
2103687915e9Sdougm     sa_optionset_t parent)
21046185db85Sdougm {
21056185db85Sdougm 	int ret = SA_OK;
21066185db85Sdougm 	char *propname;
2107b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *other1;
2108b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *other2;
21096185db85Sdougm 	int optindex;
21106185db85Sdougm 	nfsl_config_t *configlist;
21116185db85Sdougm 	sa_group_t parent_group;
21126185db85Sdougm 	char *value;
21136185db85Sdougm 
21146185db85Sdougm 	propname = sa_get_property_attr(property, "type");
21156185db85Sdougm 
21166185db85Sdougm 	if ((optindex = findopt(propname)) < 0)
2117a3351425Sdougm 		ret = SA_NO_SUCH_PROP;
21186185db85Sdougm 
21196185db85Sdougm 	/* need to validate value range here as well */
21206185db85Sdougm 
21216185db85Sdougm 	if (ret == SA_OK) {
2122a3351425Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
2123687915e9Sdougm 		if (optdefs[optindex].share && parent_group != NULL &&
2124687915e9Sdougm 		    !sa_is_share(parent_group))
2125a3351425Sdougm 			ret = SA_PROP_SHARE_ONLY;
21266185db85Sdougm 	}
21276185db85Sdougm 	if (ret == SA_OK) {
2128687915e9Sdougm 		if (optdefs[optindex].index == OPT_PUBLIC) {
2129687915e9Sdougm 			/*
2130687915e9Sdougm 			 * Public is special in that only one instance can
2131687915e9Sdougm 			 * be in the repository at the same time.
2132687915e9Sdougm 			 */
2133687915e9Sdougm 			if (public_exists(handle, parent_group)) {
2134b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_free_attr_string(propname);
2135687915e9Sdougm 				return (SA_VALUE_CONFLICT);
2136687915e9Sdougm 			}
2137687915e9Sdougm 		}
2138a3351425Sdougm 		value = sa_get_property_attr(property, "value");
2139a3351425Sdougm 		if (value != NULL) {
2140a3351425Sdougm 			/* first basic type checking */
2141a3351425Sdougm 			switch (optdefs[optindex].type) {
2142a3351425Sdougm 			case OPT_TYPE_NUMBER:
2143a3351425Sdougm 				/* check that the value is all digits */
2144a3351425Sdougm 				if (!is_a_number(value))
2145a3351425Sdougm 					ret = SA_BAD_VALUE;
2146a3351425Sdougm 				break;
2147a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2148a3351425Sdougm 				if (strlen(value) == 0 ||
2149a3351425Sdougm 				    strcasecmp(value, "true") == 0 ||
2150a3351425Sdougm 				    strcmp(value, "1") == 0 ||
2151a3351425Sdougm 				    strcasecmp(value, "false") == 0 ||
2152a3351425Sdougm 				    strcmp(value, "0") == 0) {
2153a3351425Sdougm 					ret = SA_OK;
2154a3351425Sdougm 				} else {
2155a3351425Sdougm 					ret = SA_BAD_VALUE;
2156a3351425Sdougm 				}
2157a3351425Sdougm 				break;
2158a3351425Sdougm 			case OPT_TYPE_USER:
2159a3351425Sdougm 				if (!is_a_number(value)) {
2160a3351425Sdougm 					struct passwd *pw;
2161a3351425Sdougm 					/*
2162a3351425Sdougm 					 * in this case it would have to be a
2163a3351425Sdougm 					 * user name
2164a3351425Sdougm 					 */
2165a3351425Sdougm 					pw = getpwnam(value);
2166a3351425Sdougm 					if (pw == NULL)
2167a3351425Sdougm 						ret = SA_BAD_VALUE;
2168a3351425Sdougm 					endpwent();
2169a3351425Sdougm 				} else {
2170a3351425Sdougm 					uint64_t intval;
2171a3351425Sdougm 					intval = strtoull(value, NULL, 0);
2172a3351425Sdougm 					if (intval > UID_MAX && intval != ~0)
2173a3351425Sdougm 						ret = SA_BAD_VALUE;
2174a3351425Sdougm 				}
2175a3351425Sdougm 				break;
2176a3351425Sdougm 			case OPT_TYPE_FILE:
2177a3351425Sdougm 				if (strcmp(value, "..") == 0 ||
2178a3351425Sdougm 				    strchr(value, '/') != NULL) {
2179a3351425Sdougm 					ret = SA_BAD_VALUE;
2180a3351425Sdougm 				}
2181a3351425Sdougm 				break;
2182b89a8333Snatalie li - Sun Microsystems - Irvine United States 			case OPT_TYPE_ACCLIST: {
2183b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_property_t oprop1;
2184b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_property_t oprop2;
2185b89a8333Snatalie li - Sun Microsystems - Irvine United States 				char *ovalue1 = NULL;
2186b89a8333Snatalie li - Sun Microsystems - Irvine United States 				char *ovalue2 = NULL;
2187b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2188b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (parent == NULL)
2189b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2190a3351425Sdougm 				/*
2191a3351425Sdougm 				 * access list handling. Should eventually
2192a3351425Sdougm 				 * validate that all the values make sense.
2193a3351425Sdougm 				 * Also, ro and rw may have cross value
2194a3351425Sdougm 				 * conflicts.
2195a3351425Sdougm 				 */
2196b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (strcmp(propname, SHOPT_RO) == 0) {
2197b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RW;
2198b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_NONE;
2199b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else if (strcmp(propname, SHOPT_RW) == 0) {
2200b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RO;
2201b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_NONE;
2202b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else if (strcmp(propname, SHOPT_NONE) == 0) {
2203b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RO;
2204b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_RW;
2205b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else {
2206b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = NULL;
2207b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = NULL;
2208a3351425Sdougm 				}
2209b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (other1 == NULL && other2 == NULL)
2210b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2211b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2212b89a8333Snatalie li - Sun Microsystems - Irvine United States 				/* compare rw(ro) with ro(rw) */
2213b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2214b89a8333Snatalie li - Sun Microsystems - Irvine United States 				oprop1 = sa_get_property(parent, other1);
2215b89a8333Snatalie li - Sun Microsystems - Irvine United States 				oprop2 = sa_get_property(parent, other2);
2216b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (oprop1 == NULL && oprop2 == NULL)
2217b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2218b89a8333Snatalie li - Sun Microsystems - Irvine United States 				/*
2219b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * Only potential confusion if other1
2220b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * or other2 exists. Check the values
2221b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * and run the check if there is a
2222b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * value other than the one we are
2223b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * explicitly looking at.
2224b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 */
2225b89a8333Snatalie li - Sun Microsystems - Irvine United States 				ovalue1 = sa_get_property_attr(oprop1, "value");
2226b89a8333Snatalie li - Sun Microsystems - Irvine United States 				ovalue2 = sa_get_property_attr(oprop2, "value");
2227b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue1 != NULL || ovalue2 != NULL)
2228b89a8333Snatalie li - Sun Microsystems - Irvine United States 					ret = check_rorwnone(value, ovalue1,
2229b89a8333Snatalie li - Sun Microsystems - Irvine United States 					    ovalue2);
2230b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2231b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue1 != NULL)
2232b89a8333Snatalie li - Sun Microsystems - Irvine United States 					sa_free_attr_string(ovalue1);
2233b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue2 != NULL)
2234b89a8333Snatalie li - Sun Microsystems - Irvine United States 					sa_free_attr_string(ovalue2);
2235a3351425Sdougm 				break;
2236b89a8333Snatalie li - Sun Microsystems - Irvine United States 			}
2237a3351425Sdougm 			case OPT_TYPE_LOGTAG:
2238a3351425Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
2239a3351425Sdougm 					int error;
2240a3351425Sdougm 					if (value == NULL ||
2241a3351425Sdougm 					    strlen(value) == 0) {
2242a3351425Sdougm 						if (value != NULL)
2243a3351425Sdougm 							sa_free_attr_string(
2244a3351425Sdougm 							    value);
2245a3351425Sdougm 						value = strdup("global");
2246a3351425Sdougm 					}
2247a3351425Sdougm 					if (value != NULL &&
2248a3351425Sdougm 					    nfsl_findconfig(configlist, value,
2249a3351425Sdougm 					    &error) == NULL) {
2250a3351425Sdougm 						ret = SA_BAD_VALUE;
2251a3351425Sdougm 					}
2252aed5d200Sdougm 					/* Must always free when done */
2253aed5d200Sdougm 					nfsl_freeconfig_list(&configlist);
2254a3351425Sdougm 				} else {
2255a3351425Sdougm 					ret = SA_CONFIG_ERR;
2256a3351425Sdougm 				}
2257a3351425Sdougm 				break;
2258a3351425Sdougm 			case OPT_TYPE_STRING:
2259a3351425Sdougm 				/* whatever is here should be ok */
2260a3351425Sdougm 				break;
2261a3351425Sdougm 			case OPT_TYPE_SECURITY:
2262a3351425Sdougm 				/*
2263a3351425Sdougm 				 * The "sec" property isn't used in the
2264a3351425Sdougm 				 * non-legacy parts of sharemgr. We need to
2265a3351425Sdougm 				 * reject it here. For legacy, it is pulled
2266a3351425Sdougm 				 * out well before we get here.
2267a3351425Sdougm 				 */
2268a3351425Sdougm 				ret = SA_NO_SUCH_PROP;
2269a3351425Sdougm 				break;
2270a3351425Sdougm 			default:
2271a3351425Sdougm 				break;
22726185db85Sdougm 			}
2273aed5d200Sdougm 
2274aed5d200Sdougm 			if (value != NULL)
2275aed5d200Sdougm 				sa_free_attr_string(value);
2276aed5d200Sdougm 
2277a3351425Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
2278a3351425Sdougm 				/* do the property specific check */
2279687915e9Sdougm 				ret = optdefs[optindex].check(handle, property);
22806185db85Sdougm 			}
22816185db85Sdougm 		}
22826185db85Sdougm 	}
22836185db85Sdougm 
22846185db85Sdougm 	if (propname != NULL)
2285a3351425Sdougm 		sa_free_attr_string(propname);
22866185db85Sdougm 	return (ret);
22876185db85Sdougm }
22886185db85Sdougm 
22896185db85Sdougm /*
22906185db85Sdougm  * Protocol management functions
22916185db85Sdougm  *
22923472f5dcSdougm  * Properties defined in the default files are defined in
22933472f5dcSdougm  * proto_option_defs for parsing and validation. If "other" and
22943472f5dcSdougm  * "compare" are set, then the value for this property should be
22953472f5dcSdougm  * compared against the property specified in "other" using the
22963472f5dcSdougm  * "compare" check (either <= or >=) in order to ensure that the
22973472f5dcSdougm  * values are in the correct range.  E.g. setting server_versmin
22983472f5dcSdougm  * higher than server_versmax should not be allowed.
22996185db85Sdougm  */
23006185db85Sdougm 
23016185db85Sdougm struct proto_option_defs {
23026185db85Sdougm 	char *tag;
23036185db85Sdougm 	char *name;	/* display name -- remove protocol identifier */
23046185db85Sdougm 	int index;
23056185db85Sdougm 	int type;
23066185db85Sdougm 	union {
23076185db85Sdougm 	    int intval;
23086185db85Sdougm 	    char *string;
23096185db85Sdougm 	} defvalue;
23106185db85Sdougm 	uint32_t svcs;
23116185db85Sdougm 	int32_t minval;
23126185db85Sdougm 	int32_t maxval;
23136185db85Sdougm 	char *file;
23143472f5dcSdougm 	char *other;
23153472f5dcSdougm 	int compare;
23163472f5dcSdougm #define	OPT_CMP_GE	0
23173472f5dcSdougm #define	OPT_CMP_LE	1
23186185db85Sdougm 	int (*check)(char *);
23196185db85Sdougm } proto_options[] = {
23206185db85Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
23216185db85Sdougm 	{"nfsd_servers",
23226185db85Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
23236185db85Sdougm 	    1, INT32_MAX, NFSADMIN},
23246185db85Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
23256185db85Sdougm 	{"lockd_listen_backlog",
23266185db85Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
23276185db85Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
23286185db85Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
23296185db85Sdougm 	{"lockd_servers",
23306185db85Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
23316185db85Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
23326185db85Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
23336185db85Sdougm 	{"lockd_retransmit_timeout",
23346185db85Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
23356185db85Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23366185db85Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
23376185db85Sdougm 	{"grace_period",
23386185db85Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
23396185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23406185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
23416185db85Sdougm 	{"nfs_server_versmin",
23426185db85Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
23436185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23443472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
23456185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
23466185db85Sdougm 	{"nfs_server_versmax",
23476185db85Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
23486185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23493472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
23506185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
23516185db85Sdougm 	{"nfs_client_versmin",
23526185db85Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
23536185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23543472f5dcSdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
23556185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
23566185db85Sdougm 	{"nfs_client_versmax",
23576185db85Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
23586185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23593472f5dcSdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
23606185db85Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
23616185db85Sdougm 	{"nfs_server_delegation",
23626185db85Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
23636185db85Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
23646185db85Sdougm 	    NFSADMIN},
23656185db85Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
23666185db85Sdougm 	{"nfsmapid_domain",
23676185db85Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
23686185db85Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
23696185db85Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
23706185db85Sdougm 	{"nfsd_max_connections",
23716185db85Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
23726185db85Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
23736185db85Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
23746185db85Sdougm 	{"nfsd_protocol",
23756185db85Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
23766185db85Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
23776185db85Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
23786185db85Sdougm 	{"nfsd_listen_backlog",
23796185db85Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
23806185db85Sdougm 	    OPT_TYPE_NUMBER, 0,
23816185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23826185db85Sdougm 	{NULL}
23836185db85Sdougm };
23846185db85Sdougm 
23856185db85Sdougm /*
23866185db85Sdougm  * the protoset holds the defined options so we don't have to read
23876185db85Sdougm  * them multiple times
23886185db85Sdougm  */
2389aed5d200Sdougm static sa_protocol_properties_t protoset;
23906185db85Sdougm 
23916185db85Sdougm static int
23926185db85Sdougm findprotoopt(char *name, int whichname)
23936185db85Sdougm {
23946185db85Sdougm 	int i;
23956185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2396a3351425Sdougm 		if (whichname == 1) {
2397a3351425Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
23986185db85Sdougm 			return (i);
2399a3351425Sdougm 		} else {
2400a3351425Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
2401a3351425Sdougm 				return (i);
2402a3351425Sdougm 		}
24036185db85Sdougm 	}
24046185db85Sdougm 	return (-1);
24056185db85Sdougm }
24066185db85Sdougm 
24076185db85Sdougm /*
24086185db85Sdougm  * fixcaselower(str)
24096185db85Sdougm  *
24106185db85Sdougm  * convert a string to lower case (inplace).
24116185db85Sdougm  */
24126185db85Sdougm 
24136185db85Sdougm static void
24146185db85Sdougm fixcaselower(char *str)
24156185db85Sdougm {
24166185db85Sdougm 	while (*str) {
2417a3351425Sdougm 		*str = tolower(*str);
2418a3351425Sdougm 		str++;
24196185db85Sdougm 	}
24206185db85Sdougm }
24216185db85Sdougm 
24226185db85Sdougm /*
24236185db85Sdougm  * fixcaseupper(str)
24246185db85Sdougm  *
24256185db85Sdougm  * convert a string to upper case (inplace).
24266185db85Sdougm  */
24276185db85Sdougm 
24286185db85Sdougm static void
24296185db85Sdougm fixcaseupper(char *str)
24306185db85Sdougm {
24316185db85Sdougm 	while (*str) {
2432a3351425Sdougm 		*str = toupper(*str);
2433a3351425Sdougm 		str++;
24346185db85Sdougm 	}
24356185db85Sdougm }
24366185db85Sdougm 
2437330ef417Sdougm /*
2438330ef417Sdougm  * skipwhitespace(str)
2439330ef417Sdougm  *
2440330ef417Sdougm  * Skip leading white space. It is assumed that it is called with a
2441330ef417Sdougm  * valid pointer.
2442330ef417Sdougm  */
2443330ef417Sdougm 
2444330ef417Sdougm static char *
2445330ef417Sdougm skipwhitespace(char *str)
2446330ef417Sdougm {
2447330ef417Sdougm 	while (*str && isspace(*str))
2448330ef417Sdougm 		str++;
2449330ef417Sdougm 
2450330ef417Sdougm 	return (str);
2451330ef417Sdougm }
2452330ef417Sdougm 
2453a3351425Sdougm /*
2454a3351425Sdougm  * extractprop()
2455a3351425Sdougm  *
2456a3351425Sdougm  * Extract the property and value out of the line and create the
2457a3351425Sdougm  * property in the optionset.
2458a3351425Sdougm  */
2459e7bab347Sdougm static int
2460a3351425Sdougm extractprop(char *name, char *value)
2461a3351425Sdougm {
2462a3351425Sdougm 	sa_property_t prop;
2463a3351425Sdougm 	int index;
2464e7bab347Sdougm 	int ret = SA_OK;
2465a3351425Sdougm 	/*
2466a3351425Sdougm 	 * Remove any leading
2467a3351425Sdougm 	 * white space.
2468a3351425Sdougm 	 */
2469a3351425Sdougm 	name = skipwhitespace(name);
2470a3351425Sdougm 
2471a3351425Sdougm 	index = findprotoopt(name, 0);
2472a3351425Sdougm 	if (index >= 0) {
2473a3351425Sdougm 		fixcaselower(name);
2474a3351425Sdougm 		prop = sa_create_property(proto_options[index].name, value);
2475a3351425Sdougm 		if (prop != NULL)
2476e7bab347Sdougm 			ret = sa_add_protocol_property(protoset, prop);
2477e7bab347Sdougm 		else
2478e7bab347Sdougm 			ret = SA_NO_MEMORY;
2479a3351425Sdougm 	}
2480e7bab347Sdougm 	return (ret);
2481a3351425Sdougm }
2482a3351425Sdougm 
24836185db85Sdougm /*
24846185db85Sdougm  * initprotofromdefault()
24856185db85Sdougm  *
2486917c27c8Sdougm  * Read the default file(s) and add the defined values to the
24876185db85Sdougm  * protoset.  Note that default values are known from the built in
2488917c27c8Sdougm  * table in case the file doesn't have a definition. Not having the
2489917c27c8Sdougm  * /etc/default/nfs file is OK since we have builtin default
2490917c27c8Sdougm  * values. The default file will get constructed as needed if values
2491917c27c8Sdougm  * are changed from the defaults.
24926185db85Sdougm  */
24936185db85Sdougm 
24946185db85Sdougm static int
24956185db85Sdougm initprotofromdefault()
24966185db85Sdougm {
24976185db85Sdougm 	FILE *nfs;
24986185db85Sdougm 	char buff[BUFSIZ];
24996185db85Sdougm 	char *name;
25006185db85Sdougm 	char *value;
2501e7bab347Sdougm 	int ret = SA_OK;
25026185db85Sdougm 
25036185db85Sdougm 	protoset = sa_create_protocol_properties("nfs");
25046185db85Sdougm 
25056185db85Sdougm 	if (protoset != NULL) {
2506a3351425Sdougm 		nfs = fopen(NFSADMIN, "r");
2507a3351425Sdougm 		if (nfs != NULL) {
2508e7bab347Sdougm 			while (ret == SA_OK &&
2509e7bab347Sdougm 			    fgets(buff, sizeof (buff), nfs) != NULL) {
2510a3351425Sdougm 				switch (buff[0]) {
2511a3351425Sdougm 				case '\n':
2512a3351425Sdougm 				case '#':
2513a3351425Sdougm 					/* skip */
2514a3351425Sdougm 					break;
2515a3351425Sdougm 				default:
2516a3351425Sdougm 					name = buff;
2517a3351425Sdougm 					buff[strlen(buff) - 1] = '\0';
2518a3351425Sdougm 					value = strchr(name, '=');
2519a3351425Sdougm 					if (value != NULL) {
2520a3351425Sdougm 						*value++ = '\0';
2521e7bab347Sdougm 						ret = extractprop(name, value);
2522a3351425Sdougm 					}
2523a3351425Sdougm 				}
25246185db85Sdougm 			}
2525e7bab347Sdougm 			(void) fclose(nfs);
2526e7bab347Sdougm 		} else {
2527917c27c8Sdougm 			switch (errno) {
2528917c27c8Sdougm 			case EPERM:
2529917c27c8Sdougm 			case EACCES:
2530917c27c8Sdougm 				ret = SA_NO_PERMISSION;
2531917c27c8Sdougm 				break;
2532917c27c8Sdougm 			case ENOENT:
2533917c27c8Sdougm 				break;
2534917c27c8Sdougm 			default:
2535917c27c8Sdougm 				ret = SA_SYSTEM_ERR;
2536917c27c8Sdougm 				break;
2537917c27c8Sdougm 			}
25386185db85Sdougm 		}
2539e7bab347Sdougm 	} else {
2540e7bab347Sdougm 		ret = SA_NO_MEMORY;
25416185db85Sdougm 	}
2542e7bab347Sdougm 	return (ret);
25436185db85Sdougm }
25446185db85Sdougm 
25456185db85Sdougm /*
2546a3351425Sdougm  * add_defaults()
25476185db85Sdougm  *
25486185db85Sdougm  * Add the default values for any property not defined in the parsing
25493472f5dcSdougm  * of the default files. Values are set according to their defined
25503472f5dcSdougm  * types.
25516185db85Sdougm  */
25526185db85Sdougm 
25536185db85Sdougm static void
25546185db85Sdougm add_defaults()
25556185db85Sdougm {
25566185db85Sdougm 	int i;
25576185db85Sdougm 	char number[MAXDIGITS];
25586185db85Sdougm 
25596185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2560a3351425Sdougm 		sa_property_t prop;
2561a3351425Sdougm 		prop = sa_get_protocol_property(protoset,
2562a3351425Sdougm 		    proto_options[i].name);
2563a3351425Sdougm 		if (prop == NULL) {
2564a3351425Sdougm 			/* add the default value */
2565a3351425Sdougm 			switch (proto_options[i].type) {
2566a3351425Sdougm 			case OPT_TYPE_NUMBER:
2567a3351425Sdougm 				(void) snprintf(number, sizeof (number), "%d",
2568a3351425Sdougm 				    proto_options[i].defvalue.intval);
2569a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2570a3351425Sdougm 				    number);
2571a3351425Sdougm 				break;
2572a3351425Sdougm 
2573a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2574a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2575a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2576a3351425Sdougm 				    "true" : "false");
2577a3351425Sdougm 				break;
2578a3351425Sdougm 
2579a3351425Sdougm 			case OPT_TYPE_ONOFF:
2580a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2581a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2582a3351425Sdougm 				    "on" : "off");
2583a3351425Sdougm 				break;
2584a3351425Sdougm 
2585a3351425Sdougm 			default:
2586a3351425Sdougm 				/* treat as strings of zero length */
2587a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2588a3351425Sdougm 				    "");
2589a3351425Sdougm 				break;
2590a3351425Sdougm 			}
2591a3351425Sdougm 			if (prop != NULL)
2592a3351425Sdougm 				(void) sa_add_protocol_property(protoset, prop);
25936185db85Sdougm 		}
25946185db85Sdougm 	}
25956185db85Sdougm }
25966185db85Sdougm 
25976185db85Sdougm static void
25986185db85Sdougm free_protoprops()
25996185db85Sdougm {
2600aed5d200Sdougm 	if (protoset != NULL) {
2601aed5d200Sdougm 		xmlFreeNode(protoset);
2602aed5d200Sdougm 		protoset = NULL;
2603aed5d200Sdougm 	}
26046185db85Sdougm }
26056185db85Sdougm 
26066185db85Sdougm /*
26076185db85Sdougm  * nfs_init()
26086185db85Sdougm  *
26096185db85Sdougm  * Initialize the NFS plugin.
26106185db85Sdougm  */
26116185db85Sdougm 
26126185db85Sdougm static int
26136185db85Sdougm nfs_init()
26146185db85Sdougm {
26156185db85Sdougm 	int ret = SA_OK;
26166185db85Sdougm 
2617917c27c8Sdougm 	if (sa_plugin_ops.sa_init != nfs_init) {
2618a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2619a3351425Sdougm 		    "NFS plugin not properly initialized\n"));
2620917c27c8Sdougm 		return (SA_CONFIG_ERR);
2621917c27c8Sdougm 	}
26226185db85Sdougm 
26236185db85Sdougm 	ret = initprotofromdefault();
2624917c27c8Sdougm 	if (ret != SA_OK) {
2625917c27c8Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2626917c27c8Sdougm 		    "NFS plugin problem with default file: %s\n"),
2627917c27c8Sdougm 		    sa_errorstr(ret));
2628917c27c8Sdougm 		ret = SA_OK;
2629917c27c8Sdougm 	}
2630917c27c8Sdougm 	add_defaults();
26316185db85Sdougm 
26326185db85Sdougm 	return (ret);
26336185db85Sdougm }
26346185db85Sdougm 
26356185db85Sdougm /*
26366185db85Sdougm  * nfs_fini()
26376185db85Sdougm  *
26386185db85Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
26396185db85Sdougm  */
26406185db85Sdougm 
26416185db85Sdougm static void
26426185db85Sdougm nfs_fini()
26436185db85Sdougm {
26446185db85Sdougm 	free_protoprops();
26456185db85Sdougm }
26466185db85Sdougm 
26476185db85Sdougm /*
26486185db85Sdougm  * nfs_get_proto_set()
26496185db85Sdougm  *
26506185db85Sdougm  * Return an optionset with all the protocol specific properties in
26516185db85Sdougm  * it.
26526185db85Sdougm  */
26536185db85Sdougm 
26546185db85Sdougm static sa_protocol_properties_t
26556185db85Sdougm nfs_get_proto_set()
26566185db85Sdougm {
26576185db85Sdougm 	return (protoset);
26586185db85Sdougm }
26596185db85Sdougm 
26606185db85Sdougm struct deffile {
26616185db85Sdougm 	struct deffile *next;
26626185db85Sdougm 	char *line;
26636185db85Sdougm };
26646185db85Sdougm 
26656185db85Sdougm /*
26666185db85Sdougm  * read_default_file(fname)
26676185db85Sdougm  *
26686185db85Sdougm  * Read the specified default file. We return a list of entries. This
26696185db85Sdougm  * get used for adding or removing values.
26706185db85Sdougm  */
26716185db85Sdougm 
26726185db85Sdougm static struct deffile *
26736185db85Sdougm read_default_file(char *fname)
26746185db85Sdougm {
26756185db85Sdougm 	FILE *file;
26766185db85Sdougm 	struct deffile *defs = NULL;
26776185db85Sdougm 	struct deffile *newdef;
26786185db85Sdougm 	struct deffile *prevdef = NULL;
26796185db85Sdougm 	char buff[BUFSIZ * 2];
26806185db85Sdougm 
26816185db85Sdougm 	file = fopen(fname, "r");
26826185db85Sdougm 	if (file != NULL) {
2683a3351425Sdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
2684a3351425Sdougm 			newdef = (struct deffile *)calloc(1,
2685a3351425Sdougm 			    sizeof (struct deffile));
2686a3351425Sdougm 			if (newdef != NULL) {
2687a3351425Sdougm 				/* Make sure we skip any leading whitespace. */
2688a3351425Sdougm 				newdef->line = strdup(skipwhitespace(buff));
2689a3351425Sdougm 				if (defs == NULL) {
2690a3351425Sdougm 					prevdef = defs = newdef;
2691a3351425Sdougm 				} else {
2692a3351425Sdougm 					prevdef->next = newdef;
2693a3351425Sdougm 					prevdef = newdef;
2694a3351425Sdougm 				}
2695a3351425Sdougm 			}
26966185db85Sdougm 		}
2697917c27c8Sdougm 		(void) fclose(file);
2698917c27c8Sdougm 	} else {
2699917c27c8Sdougm 		int ret = SA_OK;
2700917c27c8Sdougm 		switch (errno) {
2701917c27c8Sdougm 		case EPERM:
2702917c27c8Sdougm 		case EACCES:
2703917c27c8Sdougm 			ret = SA_NO_PERMISSION;
2704917c27c8Sdougm 			break;
2705917c27c8Sdougm 		case ENOENT:
2706917c27c8Sdougm 			break;
2707917c27c8Sdougm 		default:
2708917c27c8Sdougm 			ret = SA_SYSTEM_ERR;
2709917c27c8Sdougm 			break;
2710917c27c8Sdougm 		}
2711917c27c8Sdougm 		if (ret == SA_OK) {
2712917c27c8Sdougm 			/* Want at least one comment line */
2713917c27c8Sdougm 			defs = (struct deffile *)
2714917c27c8Sdougm 			    calloc(1, sizeof (struct deffile));
2715917c27c8Sdougm 			defs->line = strdup("# NFS default file\n");
2716917c27c8Sdougm 		}
27176185db85Sdougm 	}
27186185db85Sdougm 	return (defs);
27196185db85Sdougm }
27206185db85Sdougm 
27216185db85Sdougm static void
27226185db85Sdougm free_default_file(struct deffile *defs)
27236185db85Sdougm {
27246185db85Sdougm 	struct deffile *curdefs = NULL;
27256185db85Sdougm 
27266185db85Sdougm 	while (defs != NULL) {
2727a3351425Sdougm 		curdefs = defs;
2728a3351425Sdougm 		defs = defs->next;
2729a3351425Sdougm 		if (curdefs->line != NULL)
2730a3351425Sdougm 			free(curdefs->line);
2731a3351425Sdougm 		free(curdefs);
27326185db85Sdougm 	}
27336185db85Sdougm }
27346185db85Sdougm 
27356185db85Sdougm /*
27366185db85Sdougm  * write_default_file(fname, defs)
27376185db85Sdougm  *
27386185db85Sdougm  * Write the default file back.
27396185db85Sdougm  */
27406185db85Sdougm 
27416185db85Sdougm static int
27426185db85Sdougm write_default_file(char *fname, struct deffile *defs)
27436185db85Sdougm {
27446185db85Sdougm 	FILE *file;
27456185db85Sdougm 	int ret = SA_OK;
27466185db85Sdougm 	sigset_t old, new;
27476185db85Sdougm 
27486185db85Sdougm 	file = fopen(fname, "w+");
27496185db85Sdougm 	if (file != NULL) {
2750a3351425Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
2751a3351425Sdougm 		(void) sigaddset(&new, SIGHUP);
2752a3351425Sdougm 		(void) sigaddset(&new, SIGINT);
2753a3351425Sdougm 		(void) sigaddset(&new, SIGQUIT);
2754a3351425Sdougm 		(void) sigaddset(&new, SIGTSTP);
2755a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
2756a3351425Sdougm 		while (defs != NULL) {
2757a3351425Sdougm 			(void) fputs(defs->line, file);
2758a3351425Sdougm 			defs = defs->next;
2759a3351425Sdougm 		}
2760a3351425Sdougm 		(void) fsync(fileno(file));
2761a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
2762a3351425Sdougm 		(void) fclose(file);
27636185db85Sdougm 	} else {
2764a3351425Sdougm 		switch (errno) {
2765a3351425Sdougm 		case EPERM:
2766a3351425Sdougm 		case EACCES:
2767a3351425Sdougm 			ret = SA_NO_PERMISSION;
2768a3351425Sdougm 			break;
2769a3351425Sdougm 		default:
2770a3351425Sdougm 			ret = SA_SYSTEM_ERR;
2771a3351425Sdougm 		}
27726185db85Sdougm 	}
27736185db85Sdougm 	return (ret);
27746185db85Sdougm }
27756185db85Sdougm 
27766185db85Sdougm 
27776185db85Sdougm /*
27786185db85Sdougm  * set_default_file_value(tag, value)
27796185db85Sdougm  *
27806185db85Sdougm  * Set the default file value for tag to value. Then rewrite the file.
27816185db85Sdougm  * tag and value are always set.  The caller must ensure this.
27826185db85Sdougm  */
27836185db85Sdougm 
27846185db85Sdougm #define	MAX_STRING_LENGTH	256
27856185db85Sdougm static int
27866185db85Sdougm set_default_file_value(char *tag, char *value)
27876185db85Sdougm {
27886185db85Sdougm 	int ret = SA_OK;
27896185db85Sdougm 	struct deffile *root;
27906185db85Sdougm 	struct deffile *defs;
27916185db85Sdougm 	struct deffile *prev;
27926185db85Sdougm 	char string[MAX_STRING_LENGTH];
27936185db85Sdougm 	int len;
2794917c27c8Sdougm 	boolean_t update = B_FALSE;
27956185db85Sdougm 
27966185db85Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
27976185db85Sdougm 	len = strlen(string);
27986185db85Sdougm 
27996185db85Sdougm 	root = defs = read_default_file(NFSADMIN);
28006185db85Sdougm 	if (root == NULL) {
2801917c27c8Sdougm 		switch (errno) {
2802917c27c8Sdougm 		case EPERM:
2803917c27c8Sdougm 		case EACCES:
2804a3351425Sdougm 			ret = SA_NO_PERMISSION;
2805917c27c8Sdougm 			break;
2806917c27c8Sdougm 		default:
2807917c27c8Sdougm 			ret = SA_NO_MEMORY;
2808917c27c8Sdougm 			break;
2809917c27c8Sdougm 		}
2810917c27c8Sdougm 		return (ret);
2811917c27c8Sdougm 	}
2812917c27c8Sdougm 
2813917c27c8Sdougm 	while (defs != NULL) {
2814917c27c8Sdougm 		if (defs->line != NULL &&
2815917c27c8Sdougm 		    strncasecmp(defs->line, string, len) == 0) {
2816917c27c8Sdougm 			/* replace with the new value */
2817917c27c8Sdougm 			free(defs->line);
2818917c27c8Sdougm 			fixcaseupper(tag);
2819917c27c8Sdougm 			(void) snprintf(string, sizeof (string),
2820917c27c8Sdougm 			    "%s=%s\n", tag, value);
2821917c27c8Sdougm 			string[MAX_STRING_LENGTH - 1] = '\0';
2822917c27c8Sdougm 			defs->line = strdup(string);
2823917c27c8Sdougm 			update = B_TRUE;
2824917c27c8Sdougm 			break;
2825917c27c8Sdougm 		}
2826917c27c8Sdougm 		defs = defs->next;
2827917c27c8Sdougm 	}
2828917c27c8Sdougm 	if (!update) {
2829917c27c8Sdougm 		defs = root;
2830917c27c8Sdougm 		/* didn't find, so see if it is a comment */
2831917c27c8Sdougm 		(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
2832917c27c8Sdougm 		len = strlen(string);
28336185db85Sdougm 		while (defs != NULL) {
2834917c27c8Sdougm 			if (strncasecmp(defs->line, string, len) == 0) {
2835a3351425Sdougm 				/* replace with the new value */
2836a3351425Sdougm 				free(defs->line);
2837a3351425Sdougm 				fixcaseupper(tag);
2838a3351425Sdougm 				(void) snprintf(string, sizeof (string),
28396185db85Sdougm 				    "%s=%s\n", tag, value);
2840a3351425Sdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
2841a3351425Sdougm 				defs->line = strdup(string);
2842917c27c8Sdougm 				update = B_TRUE;
2843a3351425Sdougm 				break;
2844a3351425Sdougm 			}
2845a3351425Sdougm 			defs = defs->next;
2846a3351425Sdougm 		}
2847917c27c8Sdougm 	}
2848917c27c8Sdougm 	if (!update) {
2849917c27c8Sdougm 		fixcaseupper(tag);
2850917c27c8Sdougm 		(void) snprintf(string, sizeof (string), "%s=%s\n",
2851917c27c8Sdougm 		    tag, value);
2852917c27c8Sdougm 		prev = root;
2853917c27c8Sdougm 		while (prev->next != NULL)
2854917c27c8Sdougm 			prev = prev->next;
2855917c27c8Sdougm 		defs = malloc(sizeof (struct deffile));
2856917c27c8Sdougm 		prev->next = defs;
2857917c27c8Sdougm 		if (defs != NULL) {
2858917c27c8Sdougm 			defs->next = NULL;
2859917c27c8Sdougm 			defs->line = strdup(string);
2860917c27c8Sdougm 			update = B_TRUE;
2861a3351425Sdougm 		}
28626185db85Sdougm 	}
2863917c27c8Sdougm 	if (update) {
2864917c27c8Sdougm 		ret = write_default_file(NFSADMIN, root);
2865917c27c8Sdougm 	}
2866917c27c8Sdougm 	free_default_file(root);
28676185db85Sdougm 	return (ret);
28686185db85Sdougm }
28696185db85Sdougm 
28703472f5dcSdougm /*
28713472f5dcSdougm  * service_in_state(service, chkstate)
28723472f5dcSdougm  *
28733472f5dcSdougm  * Want to know if the specified service is in the desired state
28743472f5dcSdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
28753472f5dcSdougm  * isn't.
28763472f5dcSdougm  */
28773472f5dcSdougm static int
28783472f5dcSdougm service_in_state(char *service, const char *chkstate)
28793472f5dcSdougm {
28803472f5dcSdougm 	char *state;
28813472f5dcSdougm 	int ret = B_FALSE;
28823472f5dcSdougm 
28833472f5dcSdougm 	state = smf_get_state(service);
28843472f5dcSdougm 	if (state != NULL) {
2885a3351425Sdougm 		/* got the state so get the equality for the return value */
2886a3351425Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2887a3351425Sdougm 		free(state);
28883472f5dcSdougm 	}
28893472f5dcSdougm 	return (ret);
28903472f5dcSdougm }
28913472f5dcSdougm 
28926185db85Sdougm /*
28936185db85Sdougm  * restart_service(svcs)
28946185db85Sdougm  *
28956185db85Sdougm  * Walk through the bit mask of services that need to be restarted in
28966185db85Sdougm  * order to use the new property values. Some properties affect
28973472f5dcSdougm  * multiple daemons. Should only restart a service if it is currently
28983472f5dcSdougm  * enabled (online).
28996185db85Sdougm  */
29006185db85Sdougm 
29016185db85Sdougm static void
29026185db85Sdougm restart_service(uint32_t svcs)
29036185db85Sdougm {
29046185db85Sdougm 	uint32_t mask;
29053472f5dcSdougm 	int ret;
29063472f5dcSdougm 	char *service;
29073472f5dcSdougm 
29086185db85Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
2909a3351425Sdougm 		switch (svcs & mask) {
2910a3351425Sdougm 		case SVC_LOCKD:
2911a3351425Sdougm 			service = LOCKD;
2912a3351425Sdougm 			break;
2913a3351425Sdougm 		case SVC_STATD:
2914a3351425Sdougm 			service = STATD;
2915a3351425Sdougm 			break;
2916a3351425Sdougm 		case SVC_NFSD:
2917a3351425Sdougm 			service = NFSD;
2918a3351425Sdougm 			break;
2919a3351425Sdougm 		case SVC_MOUNTD:
2920a3351425Sdougm 			service = MOUNTD;
2921a3351425Sdougm 			break;
2922a3351425Sdougm 		case SVC_NFS4CBD:
2923a3351425Sdougm 			service = NFS4CBD;
2924a3351425Sdougm 			break;
2925a3351425Sdougm 		case SVC_NFSMAPID:
2926a3351425Sdougm 			service = NFSMAPID;
2927a3351425Sdougm 			break;
2928a3351425Sdougm 		case SVC_RQUOTAD:
2929a3351425Sdougm 			service = RQUOTAD;
2930a3351425Sdougm 			break;
2931a3351425Sdougm 		case SVC_NFSLOGD:
2932a3351425Sdougm 			service = NFSLOGD;
2933a3351425Sdougm 			break;
2934*2f172c55SRobert Thurlow 		case SVC_REPARSED:
2935*2f172c55SRobert Thurlow 			service = REPARSED;
2936*2f172c55SRobert Thurlow 			break;
2937a3351425Sdougm 		default:
2938a3351425Sdougm 			continue;
2939a3351425Sdougm 		}
29403472f5dcSdougm 
29413472f5dcSdougm 		/*
29423472f5dcSdougm 		 * Only attempt to restart the service if it is
29433472f5dcSdougm 		 * currently running. In the future, it may be
29443472f5dcSdougm 		 * desirable to use smf_refresh_instance if the NFS
29453472f5dcSdougm 		 * services ever implement the refresh method.
29463472f5dcSdougm 		 */
2947a3351425Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2948a3351425Sdougm 			ret = smf_restart_instance(service);
29493472f5dcSdougm 			/*
2950a3351425Sdougm 			 * There are only a few SMF errors at this point, but
2951a3351425Sdougm 			 * it is also possible that a bad value may have put
2952a3351425Sdougm 			 * the service into maintenance if there wasn't an
2953a3351425Sdougm 			 * SMF level error.
29543472f5dcSdougm 			 */
2955a3351425Sdougm 			if (ret != 0) {
2956a3351425Sdougm 				(void) fprintf(stderr,
2957a3351425Sdougm 				    dgettext(TEXT_DOMAIN,
2958a3351425Sdougm 				    "%s failed to restart: %s\n"),
2959a3351425Sdougm 				    scf_strerror(scf_error()));
2960a3351425Sdougm 			} else {
2961a3351425Sdougm 				/*
2962a3351425Sdougm 				 * Check whether it has gone to "maintenance"
2963a3351425Sdougm 				 * mode or not. Maintenance implies something
2964a3351425Sdougm 				 * went wrong.
2965a3351425Sdougm 				 */
2966a3351425Sdougm 				if (service_in_state(service,
2967a3351425Sdougm 				    SCF_STATE_STRING_MAINT)) {
2968a3351425Sdougm 					(void) fprintf(stderr,
2969a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
2970a3351425Sdougm 					    "%s failed to restart\n"),
2971a3351425Sdougm 					    service);
2972a3351425Sdougm 				}
2973a3351425Sdougm 			}
29743472f5dcSdougm 		}
2975a3351425Sdougm 		svcs &= ~mask;
29766185db85Sdougm 	}
29776185db85Sdougm }
29786185db85Sdougm 
29793472f5dcSdougm /*
29803472f5dcSdougm  * nfs_minmax_check(name, value)
29813472f5dcSdougm  *
29823472f5dcSdougm  * Verify that the value for the property specified by index is valid
29833472f5dcSdougm  * relative to the opposite value in the case of a min/max variable.
29843472f5dcSdougm  * Currently, server_minvers/server_maxvers and
29853472f5dcSdougm  * client_minvers/client_maxvers are the only ones to check.
29863472f5dcSdougm  */
29873472f5dcSdougm 
29883472f5dcSdougm static int
29893472f5dcSdougm nfs_minmax_check(int index, int value)
29903472f5dcSdougm {
29913472f5dcSdougm 	int val;
29923472f5dcSdougm 	char *pval;
29933472f5dcSdougm 	sa_property_t prop;
29943472f5dcSdougm 	sa_optionset_t opts;
29953472f5dcSdougm 	int ret = B_TRUE;
29963472f5dcSdougm 
29973472f5dcSdougm 	if (proto_options[index].other != NULL) {
2998a3351425Sdougm 		/* have a property to compare against */
2999a3351425Sdougm 		opts = nfs_get_proto_set();
3000a3351425Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
30013472f5dcSdougm 		/*
30023472f5dcSdougm 		 * If we don't find the property, assume default
30033472f5dcSdougm 		 * values which will work since the max will be at the
30043472f5dcSdougm 		 * max and the min at the min.
30053472f5dcSdougm 		 */
3006a3351425Sdougm 		if (prop != NULL) {
3007a3351425Sdougm 			pval = sa_get_property_attr(prop, "value");
3008a3351425Sdougm 			if (pval != NULL) {
3009a3351425Sdougm 				val = strtoul(pval, NULL, 0);
3010a3351425Sdougm 				if (proto_options[index].compare ==
3011a3351425Sdougm 				    OPT_CMP_LE) {
3012a3351425Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
3013a3351425Sdougm 				} else if (proto_options[index].compare ==
3014a3351425Sdougm 				    OPT_CMP_GE) {
3015a3351425Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
3016a3351425Sdougm 				}
3017a3351425Sdougm 			}
30183472f5dcSdougm 		}
30193472f5dcSdougm 	}
30203472f5dcSdougm 	return (ret);
30213472f5dcSdougm }
30223472f5dcSdougm 
30236185db85Sdougm /*
30246185db85Sdougm  * nfs_validate_proto_prop(index, name, value)
30256185db85Sdougm  *
3026da6c28aaSamw  * Verify that the property specified by name can take the new
30276185db85Sdougm  * value. This is a sanity check to prevent bad values getting into
30283472f5dcSdougm  * the default files. All values need to be checked against what is
30293472f5dcSdougm  * allowed by their defined type. If a type isn't explicitly defined
30303472f5dcSdougm  * here, it is treated as a string.
30313472f5dcSdougm  *
30323472f5dcSdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
30333472f5dcSdougm  * within the range specified and potentially against another property
30343472f5dcSdougm  * value as well as specified in the proto_options members other and
30353472f5dcSdougm  * compare.
30366185db85Sdougm  */
30376185db85Sdougm 
30386185db85Sdougm static int
30396185db85Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
30406185db85Sdougm {
30416185db85Sdougm 	int ret = SA_OK;
30426185db85Sdougm 	char *cp;
30436185db85Sdougm #ifdef lint
30446185db85Sdougm 	name = name;
30456185db85Sdougm #endif
30466185db85Sdougm 
30476185db85Sdougm 	switch (proto_options[index].type) {
30486185db85Sdougm 	case OPT_TYPE_NUMBER:
3049a3351425Sdougm 		if (!is_a_number(value))
3050a3351425Sdougm 			ret = SA_BAD_VALUE;
3051a3351425Sdougm 		else {
3052a3351425Sdougm 			int val;
3053a3351425Sdougm 			val = strtoul(value, NULL, 0);
3054a3351425Sdougm 			if (val < proto_options[index].minval ||
3055a3351425Sdougm 			    val > proto_options[index].maxval)
3056a3351425Sdougm 				ret = SA_BAD_VALUE;
3057a3351425Sdougm 			/*
3058a3351425Sdougm 			 * For server_versmin/server_versmax and
3059a3351425Sdougm 			 * client_versmin/client_versmax, the value of the
3060a3351425Sdougm 			 * min(max) should be checked to be correct relative
3061a3351425Sdougm 			 * to the current max(min).
3062a3351425Sdougm 			 */
3063a3351425Sdougm 			if (!nfs_minmax_check(index, val)) {
3064a3351425Sdougm 				ret = SA_BAD_VALUE;
3065a3351425Sdougm 			}
30663472f5dcSdougm 		}
3067a3351425Sdougm 		break;
30683472f5dcSdougm 
30696185db85Sdougm 	case OPT_TYPE_DOMAIN:
30706185db85Sdougm 		/*
30713472f5dcSdougm 		 * needs to be a qualified domain so will have at
30723472f5dcSdougm 		 * least one period and other characters on either
30733472f5dcSdougm 		 * side of it.  A zero length string is also allowed
30743472f5dcSdougm 		 * and is the way to turn off the override.
30756185db85Sdougm 		 */
3076a3351425Sdougm 		if (strlen(value) == 0)
3077a3351425Sdougm 			break;
3078a3351425Sdougm 		cp = strchr(value, '.');
3079a3351425Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
3080a3351425Sdougm 			ret = SA_BAD_VALUE;
30813472f5dcSdougm 		break;
30823472f5dcSdougm 
30836185db85Sdougm 	case OPT_TYPE_BOOLEAN:
3084a3351425Sdougm 		if (strlen(value) == 0 ||
3085a3351425Sdougm 		    strcasecmp(value, "true") == 0 ||
3086a3351425Sdougm 		    strcmp(value, "1") == 0 ||
3087a3351425Sdougm 		    strcasecmp(value, "false") == 0 ||
3088a3351425Sdougm 		    strcmp(value, "0") == 0) {
3089a3351425Sdougm 			ret = SA_OK;
3090a3351425Sdougm 		} else {
3091a3351425Sdougm 			ret = SA_BAD_VALUE;
3092a3351425Sdougm 		}
3093a3351425Sdougm 		break;
30943472f5dcSdougm 
30956185db85Sdougm 	case OPT_TYPE_ONOFF:
3096a3351425Sdougm 		if (strcasecmp(value, "on") != 0 &&
3097a3351425Sdougm 		    strcasecmp(value, "off") != 0) {
3098a3351425Sdougm 			ret = SA_BAD_VALUE;
3099a3351425Sdougm 		}
3100a3351425Sdougm 		break;
31013472f5dcSdougm 
31026185db85Sdougm 	case OPT_TYPE_PROTOCOL:
3103917c27c8Sdougm 		if (strlen(value) != 0 &&
3104917c27c8Sdougm 		    strcasecmp(value, "all") != 0 &&
3105a3351425Sdougm 		    strcasecmp(value, "tcp") != 0 &&
3106a3351425Sdougm 		    strcasecmp(value, "udp") != 0)
3107a3351425Sdougm 			ret = SA_BAD_VALUE;
3108a3351425Sdougm 		break;
31093472f5dcSdougm 
31103472f5dcSdougm 	default:
3111a3351425Sdougm 		/* treat as a string */
3112a3351425Sdougm 		break;
31136185db85Sdougm 	}
31146185db85Sdougm 	return (ret);
31156185db85Sdougm }
31166185db85Sdougm 
31176185db85Sdougm /*
31186185db85Sdougm  * nfs_set_proto_prop(prop)
31196185db85Sdougm  *
31206185db85Sdougm  * check that prop is valid.
31216185db85Sdougm  */
31226185db85Sdougm 
31236185db85Sdougm static int
31246185db85Sdougm nfs_set_proto_prop(sa_property_t prop)
31256185db85Sdougm {
31266185db85Sdougm 	int ret = SA_OK;
31276185db85Sdougm 	char *name;
31286185db85Sdougm 	char *value;
31296185db85Sdougm 
31306185db85Sdougm 	name = sa_get_property_attr(prop, "type");
31316185db85Sdougm 	value = sa_get_property_attr(prop, "value");
31326185db85Sdougm 	if (name != NULL && value != NULL) {
3133a3351425Sdougm 		int index = findprotoopt(name, 1);
3134a3351425Sdougm 		if (index >= 0) {
3135a3351425Sdougm 			/* should test for valid value */
3136a3351425Sdougm 			ret = nfs_validate_proto_prop(index, name, value);
3137a3351425Sdougm 			if (ret == SA_OK)
3138a3351425Sdougm 				ret = set_default_file_value(
3139a3351425Sdougm 				    proto_options[index].tag, value);
3140a3351425Sdougm 			if (ret == SA_OK)
3141a3351425Sdougm 				restart_service(proto_options[index].svcs);
3142a3351425Sdougm 		}
31436185db85Sdougm 	}
31446185db85Sdougm 	if (name != NULL)
3145a3351425Sdougm 		sa_free_attr_string(name);
31466185db85Sdougm 	if (value != NULL)
3147a3351425Sdougm 		sa_free_attr_string(value);
31486185db85Sdougm 	return (ret);
31496185db85Sdougm }
31506185db85Sdougm 
31516185db85Sdougm /*
31526185db85Sdougm  * nfs_get_status()
31536185db85Sdougm  *
31546185db85Sdougm  * What is the current status of the nfsd? We use the SMF state here.
31556185db85Sdougm  * Caller must free the returned value.
31566185db85Sdougm  */
31576185db85Sdougm 
31586185db85Sdougm static char *
31596185db85Sdougm nfs_get_status()
31606185db85Sdougm {
31616185db85Sdougm 	char *state;
31626185db85Sdougm 	state = smf_get_state(NFSD);
31636185db85Sdougm 	return (state != NULL ? state : strdup("-"));
31646185db85Sdougm }
31656185db85Sdougm 
31666185db85Sdougm /*
31676185db85Sdougm  * nfs_space_alias(alias)
31686185db85Sdougm  *
31696185db85Sdougm  * Lookup the space (security) name. If it is default, convert to the
31706185db85Sdougm  * real name.
31716185db85Sdougm  */
31726185db85Sdougm 
31736185db85Sdougm static char *
31746185db85Sdougm nfs_space_alias(char *space)
31756185db85Sdougm {
31766185db85Sdougm 	char *name = space;
31776185db85Sdougm 	seconfig_t secconf;
3178a99982a7Sdougm 
3179a99982a7Sdougm 	/*
3180a99982a7Sdougm 	 * Only the space named "default" is special. If it is used,
3181a99982a7Sdougm 	 * the default needs to be looked up and the real name used.
3182a99982a7Sdougm 	 * This is normally "sys" but could be changed.  We always
3183a99982a7Sdougm 	 * change defautl to the real name.
3184a99982a7Sdougm 	 */
3185a99982a7Sdougm 	if (strcmp(space, "default") == 0 &&
3186a99982a7Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
3187a3351425Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
3188a3351425Sdougm 			name = secconf.sc_name;
31896185db85Sdougm 	}
31906185db85Sdougm 	return (strdup(name));
31916185db85Sdougm }
3192da6c28aaSamw 
3193da6c28aaSamw /*
3194da6c28aaSamw  * nfs_features()
3195da6c28aaSamw  *
3196da6c28aaSamw  * Return a mask of the features required.
3197da6c28aaSamw  */
3198da6c28aaSamw 
3199da6c28aaSamw static uint64_t
3200da6c28aaSamw nfs_features()
3201da6c28aaSamw {
32029e5da854Sdougm 	return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);
3203da6c28aaSamw }
3204