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 /*
234b92079bSAlexander Eremin  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24*822e6387SMarcel Telka  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2587da98afSVitaliy Gusev  */
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>
39dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include <strings.h>
406185db85Sdougm #include "libshare.h"
416185db85Sdougm #include "libshare_impl.h"
426185db85Sdougm #include <nfs/export.h>
436185db85Sdougm #include <pwd.h>
445cb0d679SMarcel Telka #include <grp.h>
456185db85Sdougm #include <limits.h>
466185db85Sdougm #include <libscf.h>
47dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include <syslog.h>
48dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include <rpcsvc/daemon_utils.h>
496185db85Sdougm #include "nfslog_config.h"
506185db85Sdougm #include "nfslogtab.h"
516185db85Sdougm #include "libshare_nfs.h"
526185db85Sdougm #include <nfs/nfs.h>
53ecd6cf80Smarks #include <nfs/nfssys.h>
54dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include "smfcfg.h"
556185db85Sdougm 
566185db85Sdougm /* should really be in some global place */
576185db85Sdougm #define	DEF_WIN	30000
586185db85Sdougm #define	OPT_CHUNK	1024
596185db85Sdougm 
606185db85Sdougm int debug = 0;
616185db85Sdougm 
62ecd6cf80Smarks #define	NFS_SERVER_SVC	"svc:/network/nfs/server:default"
63dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #define	NFS_CLIENT_SVC	(char *)"svc:/network/nfs/client:default"
646185db85Sdougm 
656185db85Sdougm /* internal functions */
666185db85Sdougm static int nfs_init();
676185db85Sdougm static void nfs_fini();
686185db85Sdougm static int nfs_enable_share(sa_share_t);
69ecd6cf80Smarks static int nfs_disable_share(sa_share_t, char *);
70687915e9Sdougm static int nfs_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
716185db85Sdougm static int nfs_validate_security_mode(char *);
726185db85Sdougm static int nfs_is_security_opt(char *);
736185db85Sdougm static int nfs_parse_legacy_options(sa_group_t, char *);
746185db85Sdougm static char *nfs_format_options(sa_group_t, int);
756185db85Sdougm static int nfs_set_proto_prop(sa_property_t);
766185db85Sdougm static sa_protocol_properties_t nfs_get_proto_set();
776185db85Sdougm static char *nfs_get_status();
786185db85Sdougm static char *nfs_space_alias(char *);
79da6c28aaSamw static uint64_t nfs_features();
806185db85Sdougm 
816185db85Sdougm /*
826185db85Sdougm  * ops vector that provides the protocol specific info and operations
836185db85Sdougm  * for share management.
846185db85Sdougm  */
856185db85Sdougm 
866185db85Sdougm struct sa_plugin_ops sa_plugin_ops = {
876185db85Sdougm 	SA_PLUGIN_VERSION,
886185db85Sdougm 	"nfs",
896185db85Sdougm 	nfs_init,
906185db85Sdougm 	nfs_fini,
916185db85Sdougm 	nfs_enable_share,
926185db85Sdougm 	nfs_disable_share,
936185db85Sdougm 	nfs_validate_property,
946185db85Sdougm 	nfs_validate_security_mode,
956185db85Sdougm 	nfs_is_security_opt,
966185db85Sdougm 	nfs_parse_legacy_options,
976185db85Sdougm 	nfs_format_options,
986185db85Sdougm 	nfs_set_proto_prop,
996185db85Sdougm 	nfs_get_proto_set,
1006185db85Sdougm 	nfs_get_status,
1016185db85Sdougm 	nfs_space_alias,
102da6c28aaSamw 	NULL,	/* update_legacy */
103da6c28aaSamw 	NULL,	/* delete_legacy */
104da6c28aaSamw 	NULL,	/* change_notify */
105da6c28aaSamw 	NULL,	/* enable_resource */
106da6c28aaSamw 	NULL,	/* disable_resource */
107da6c28aaSamw 	nfs_features,
108da6c28aaSamw 	NULL,	/* transient shares */
109da6c28aaSamw 	NULL,	/* notify resource */
1104bff34e3Sthurlow 	NULL,	/* rename_resource */
1114bff34e3Sthurlow 	NULL,	/* run_command */
1124bff34e3Sthurlow 	NULL,	/* command_help */
1134bff34e3Sthurlow 	NULL	/* delete_proto_section */
1146185db85Sdougm };
1156185db85Sdougm 
1166185db85Sdougm /*
1176185db85Sdougm  * list of support services needed
1186185db85Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
1196185db85Sdougm  */
1206185db85Sdougm 
1216185db85Sdougm static char *service_list_default[] =
1222f172c55SRobert Thurlow 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, REPARSED, NULL };
1236185db85Sdougm static char *service_list_logging[] =
1242f172c55SRobert Thurlow 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, REPARSED,
1252f172c55SRobert Thurlow 	    NULL };
1266185db85Sdougm 
1276185db85Sdougm /*
1286185db85Sdougm  * option definitions.  Make sure to keep the #define for the option
1296185db85Sdougm  * index just before the entry it is the index for. Changing the order
1306185db85Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1316185db85Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1326185db85Sdougm  */
1336185db85Sdougm 
1346185db85Sdougm struct option_defs optdefs[] = {
1356185db85Sdougm #define	OPT_RO		0
1366185db85Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1376185db85Sdougm #define	OPT_RW		1
1386185db85Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1396185db85Sdougm #define	OPT_ROOT	2
1406185db85Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1416185db85Sdougm #define	OPT_SECURE	3
1426185db85Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1436185db85Sdougm #define	OPT_ANON	4
1446185db85Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1456185db85Sdougm #define	OPT_WINDOW	5
1466185db85Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1476185db85Sdougm #define	OPT_NOSUID	6
1486185db85Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1496185db85Sdougm #define	OPT_ACLOK	7
1506185db85Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1516185db85Sdougm #define	OPT_NOSUB	8
1526185db85Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1536185db85Sdougm #define	OPT_SEC		9
1546185db85Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1556185db85Sdougm #define	OPT_PUBLIC	10
1566185db85Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1576185db85Sdougm #define	OPT_INDEX	11
1586185db85Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1596185db85Sdougm #define	OPT_LOG		12
1606185db85Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1616185db85Sdougm #define	OPT_CKSUM	13
1626185db85Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
163b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_NONE	14
164b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{SHOPT_NONE, OPT_NONE, OPT_TYPE_ACCLIST},
165b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_ROOT_MAPPING	15
166b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{SHOPT_ROOT_MAPPING, OPT_ROOT_MAPPING, OPT_TYPE_USER},
167b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_CHARSET_MAP	16
168b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{"", OPT_CHARSET_MAP, OPT_TYPE_ACCLIST},
1696764e83bSVallish Vaidyeshwara #define	OPT_NOACLFAB	17
1706764e83bSVallish Vaidyeshwara 	{SHOPT_NOACLFAB, OPT_NOACLFAB, OPT_TYPE_BOOLEAN},
1715cb0d679SMarcel Telka #define	OPT_UIDMAP	18
1725cb0d679SMarcel Telka 	{SHOPT_UIDMAP, OPT_UIDMAP, OPT_TYPE_MAPPING},
1735cb0d679SMarcel Telka #define	OPT_GIDMAP	19
1745cb0d679SMarcel Telka 	{SHOPT_GIDMAP, OPT_GIDMAP, OPT_TYPE_MAPPING},
1756185db85Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
1765cb0d679SMarcel Telka #define	OPT_VOLFH	20
1776185db85Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1786185db85Sdougm #endif /* VOLATILE_FH_TEST */
1796185db85Sdougm 	NULL
1806185db85Sdougm };
1816185db85Sdougm 
182b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
183b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Codesets that may need to be converted to UTF-8 for file paths.
184b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Add new names here to add new property support. If we ever get a
185b89a8333Snatalie li - Sun Microsystems - Irvine United States  * way to query the kernel for character sets, this should become
186b89a8333Snatalie li - Sun Microsystems - Irvine United States  * dynamically loaded. Make sure changes here are reflected in
187b89a8333Snatalie li - Sun Microsystems - Irvine United States  * cmd/fs.d/nfs/mountd/nfscmd.c
188b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
189b89a8333Snatalie li - Sun Microsystems - Irvine United States 
190b89a8333Snatalie li - Sun Microsystems - Irvine United States static char *legal_conv[] = {
191b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-cn",
192b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-jp",
193b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-jpms",
194b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-kr",
195b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-tw",
196b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-1",
197b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-2",
198b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-5",
199b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-6",
200b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-7",
201b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-8",
202b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-9",
203b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-13",
204b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-15",
205b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"koi8-r",
206b89a8333Snatalie li - Sun Microsystems - Irvine United States 	NULL
207b89a8333Snatalie li - Sun Microsystems - Irvine United States };
208b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2096185db85Sdougm /*
210a99982a7Sdougm  * list of properties that are related to security flavors.
2116185db85Sdougm  */
2126185db85Sdougm static char *seclist[] = {
2136185db85Sdougm 	SHOPT_RO,
2146185db85Sdougm 	SHOPT_RW,
2156185db85Sdougm 	SHOPT_ROOT,
2166185db85Sdougm 	SHOPT_WINDOW,
217b89a8333Snatalie li - Sun Microsystems - Irvine United States 	SHOPT_NONE,
218b89a8333Snatalie li - Sun Microsystems - Irvine United States 	SHOPT_ROOT_MAPPING,
2195cb0d679SMarcel Telka 	SHOPT_UIDMAP,
2205cb0d679SMarcel Telka 	SHOPT_GIDMAP,
2216185db85Sdougm 	NULL
2226185db85Sdougm };
2236185db85Sdougm 
2246185db85Sdougm /* structure for list of securities */
2256185db85Sdougm struct securities {
226a99982a7Sdougm 	sa_security_t security;
227a99982a7Sdougm 	struct securities *next;
2286185db85Sdougm };
2296185db85Sdougm 
230b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
231b89a8333Snatalie li - Sun Microsystems - Irvine United States  * findcharset(charset)
232b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
233b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Returns B_TRUE if the charset is a legal conversion otherwise
234b89a8333Snatalie li - Sun Microsystems - Irvine United States  * B_FALSE. This will need to be rewritten to be more efficient when
235b89a8333Snatalie li - Sun Microsystems - Irvine United States  * we have a dynamic list of legal conversions.
236b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
237b89a8333Snatalie li - Sun Microsystems - Irvine United States 
238b89a8333Snatalie li - Sun Microsystems - Irvine United States static boolean_t
239b89a8333Snatalie li - Sun Microsystems - Irvine United States findcharset(char *charset)
240b89a8333Snatalie li - Sun Microsystems - Irvine United States {
241b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int i;
242b89a8333Snatalie li - Sun Microsystems - Irvine United States 
243b89a8333Snatalie li - Sun Microsystems - Irvine United States 	for (i = 0; legal_conv[i] != NULL; i++)
244b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (strcmp(charset, legal_conv[i]) == 0)
245b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (B_TRUE);
246b89a8333Snatalie li - Sun Microsystems - Irvine United States 	return (B_FALSE);
247b89a8333Snatalie li - Sun Microsystems - Irvine United States }
248b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2496185db85Sdougm /*
2506185db85Sdougm  * findopt(name)
2516185db85Sdougm  *
2526185db85Sdougm  * Lookup option "name" in the option table and return the table
2536185db85Sdougm  * index.
2546185db85Sdougm  */
2556185db85Sdougm 
2566185db85Sdougm static int
2576185db85Sdougm findopt(char *name)
2586185db85Sdougm {
2596185db85Sdougm 	int i;
2606185db85Sdougm 	if (name != NULL) {
261a3351425Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
262a3351425Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
26387da98afSVitaliy Gusev 				return (optdefs[i].index);
264a3351425Sdougm 		}
265b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (findcharset(name))
266b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (OPT_CHARSET_MAP);
2676185db85Sdougm 	}
2686185db85Sdougm 	return (-1);
2696185db85Sdougm }
2706185db85Sdougm 
2716185db85Sdougm /*
2726185db85Sdougm  * gettype(name)
2736185db85Sdougm  *
2746185db85Sdougm  * Return the type of option "name".
2756185db85Sdougm  */
2766185db85Sdougm 
2776185db85Sdougm static int
2786185db85Sdougm gettype(char *name)
2796185db85Sdougm {
2806185db85Sdougm 	int optdef;
2816185db85Sdougm 
2826185db85Sdougm 	optdef = findopt(name);
2836185db85Sdougm 	if (optdef != -1)
284a3351425Sdougm 		return (optdefs[optdef].type);
2856185db85Sdougm 	return (OPT_TYPE_ANY);
2866185db85Sdougm }
2876185db85Sdougm 
2886185db85Sdougm /*
2896185db85Sdougm  * nfs_validate_security_mode(mode)
2906185db85Sdougm  *
2916185db85Sdougm  * is the specified mode string a valid one for use with NFS?
2926185db85Sdougm  */
2936185db85Sdougm 
2946185db85Sdougm static int
2956185db85Sdougm nfs_validate_security_mode(char *mode)
2966185db85Sdougm {
2976185db85Sdougm 	seconfig_t secinfo;
2986185db85Sdougm 	int err;
2996185db85Sdougm 
3006185db85Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
3016185db85Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
3026185db85Sdougm 	if (err == SC_NOERROR)
303a3351425Sdougm 		return (1);
3046185db85Sdougm 	return (0);
3056185db85Sdougm }
3066185db85Sdougm 
3076185db85Sdougm /*
3086185db85Sdougm  * nfs_is_security_opt(tok)
3096185db85Sdougm  *
3106185db85Sdougm  * check to see if tok represents an option that is only valid in some
3116185db85Sdougm  * security flavor.
3126185db85Sdougm  */
3136185db85Sdougm 
3146185db85Sdougm static int
3156185db85Sdougm nfs_is_security_opt(char *tok)
3166185db85Sdougm {
3176185db85Sdougm 	int i;
3186185db85Sdougm 
3196185db85Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
320a3351425Sdougm 		if (strcmp(tok, seclist[i]) == 0)
321a3351425Sdougm 			return (1);
3226185db85Sdougm 	}
3236185db85Sdougm 	return (0);
3246185db85Sdougm }
3256185db85Sdougm 
3266185db85Sdougm /*
3276185db85Sdougm  * find_security(seclist, sec)
3286185db85Sdougm  *
3296185db85Sdougm  * Walk the current list of security flavors and return true if it is
3306185db85Sdougm  * present, else return false.
3316185db85Sdougm  */
3326185db85Sdougm 
3336185db85Sdougm static int
3346185db85Sdougm find_security(struct securities *seclist, sa_security_t sec)
3356185db85Sdougm {
3366185db85Sdougm 	while (seclist != NULL) {
337a3351425Sdougm 		if (seclist->security == sec)
338a3351425Sdougm 			return (1);
339a3351425Sdougm 		seclist = seclist->next;
3406185db85Sdougm 	}
3416185db85Sdougm 	return (0);
3426185db85Sdougm }
3436185db85Sdougm 
3446185db85Sdougm /*
3456185db85Sdougm  * make_security_list(group, securitymodes, proto)
3466185db85Sdougm  *	go through the list of securitymodes and add them to the
3476185db85Sdougm  *	group's list of security optionsets. We also keep a list of
3486185db85Sdougm  *	those optionsets so we don't have to find them later. All of
3496185db85Sdougm  *	these will get copies of the same properties.
3506185db85Sdougm  */
3516185db85Sdougm 
3526185db85Sdougm static struct securities *
3536185db85Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
3546185db85Sdougm {
3556185db85Sdougm 	char *tok, *next = NULL;
3566185db85Sdougm 	struct securities *curp, *headp = NULL, *prev;
3576185db85Sdougm 	sa_security_t check;
3586185db85Sdougm 	int freetok = 0;
3596185db85Sdougm 
3606185db85Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
361a3351425Sdougm 		next = strchr(tok, ':');
362a3351425Sdougm 		if (next != NULL)
363a3351425Sdougm 			*next++ = '\0';
364a3351425Sdougm 		if (strcmp(tok, "default") == 0) {
365a3351425Sdougm 			/* resolve default into the real type */
366a3351425Sdougm 			tok = nfs_space_alias(tok);
367a3351425Sdougm 			freetok = 1;
368a3351425Sdougm 		}
369a3351425Sdougm 		check = sa_get_security(group, tok, proto);
370a3351425Sdougm 
371a3351425Sdougm 		/* add to the security list if it isn't there already */
372a3351425Sdougm 		if (check == NULL || !find_security(headp, check)) {
373a3351425Sdougm 			curp = (struct securities *)calloc(1,
374a3351425Sdougm 			    sizeof (struct securities));
375a3351425Sdougm 			if (curp != NULL) {
376a3351425Sdougm 				if (check == NULL) {
377a3351425Sdougm 					curp->security = sa_create_security(
378a3351425Sdougm 					    group, tok, proto);
379a3351425Sdougm 				} else {
380a3351425Sdougm 					curp->security = check;
381a3351425Sdougm 				}
382a3351425Sdougm 				/*
383a3351425Sdougm 				 * note that the first time through the loop,
384a3351425Sdougm 				 * headp will be NULL and prev will be
385a3351425Sdougm 				 * undefined.  Since headp is NULL, we set
386a3351425Sdougm 				 * both it and prev to the curp (first
387a3351425Sdougm 				 * structure to be allocated).
388a3351425Sdougm 				 *
389a3351425Sdougm 				 * later passes through the loop will have
390a3351425Sdougm 				 * headp not being NULL and prev will be used
391a3351425Sdougm 				 * to allocate at the end of the list.
392a3351425Sdougm 				 */
393a3351425Sdougm 				if (headp == NULL) {
394a3351425Sdougm 					headp = curp;
395a3351425Sdougm 					prev = curp;
396a3351425Sdougm 				} else {
397a3351425Sdougm 					prev->next = curp;
398a3351425Sdougm 					prev = curp;
399a3351425Sdougm 				}
400a3351425Sdougm 			}
4016185db85Sdougm 		}
402a99982a7Sdougm 
403a3351425Sdougm 		if (freetok) {
404a3351425Sdougm 			freetok = 0;
405a3351425Sdougm 			sa_free_attr_string(tok);
406a3351425Sdougm 		}
4076185db85Sdougm 	}
4086185db85Sdougm 	return (headp);
4096185db85Sdougm }
4106185db85Sdougm 
4116185db85Sdougm static void
4126185db85Sdougm free_security_list(struct securities *sec)
4136185db85Sdougm {
4146185db85Sdougm 	struct securities *next;
4156185db85Sdougm 	if (sec != NULL) {
416a3351425Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
417a3351425Sdougm 			next = sec->next;
418a3351425Sdougm 			free(sec);
419a3351425Sdougm 		}
4206185db85Sdougm 	}
4216185db85Sdougm }
4226185db85Sdougm 
4236185db85Sdougm /*
4246185db85Sdougm  * nfs_alistcat(str1, str2, sep)
4256185db85Sdougm  *
4266185db85Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
4276185db85Sdougm  * character. If memory allocation fails, return NULL;
4286185db85Sdougm  */
4296185db85Sdougm 
4306185db85Sdougm static char *
4316185db85Sdougm nfs_alistcat(char *str1, char *str2, char sep)
4326185db85Sdougm {
4336185db85Sdougm 	char *newstr;
4346185db85Sdougm 	size_t len;
4356185db85Sdougm 
4366185db85Sdougm 	len = strlen(str1) + strlen(str2) + 2;
4376185db85Sdougm 	newstr = (char *)malloc(len);
4386185db85Sdougm 	if (newstr != NULL)
439a3351425Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
4406185db85Sdougm 	return (newstr);
4416185db85Sdougm }
4426185db85Sdougm 
4436185db85Sdougm /*
4445cb0d679SMarcel Telka  * add_security_prop(sec, name, value, persist, iszfs)
4456185db85Sdougm  *
4466185db85Sdougm  * Add the property to the securities structure. This accumulates
4476185db85Sdougm  * properties for as part of parsing legacy options.
4486185db85Sdougm  */
4496185db85Sdougm 
4506185db85Sdougm static int
4516185db85Sdougm add_security_prop(struct securities *sec, char *name, char *value,
4526185db85Sdougm 			int persist, int iszfs)
4536185db85Sdougm {
4546185db85Sdougm 	sa_property_t prop;
4556185db85Sdougm 	int ret = SA_OK;
4566185db85Sdougm 
4576185db85Sdougm 	for (; sec != NULL; sec = sec->next) {
458a3351425Sdougm 		if (value == NULL) {
459a3351425Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
460a3351425Sdougm 			    strcmp(name, SHOPT_RO) == 0)
461a3351425Sdougm 				value = "*";
462a3351425Sdougm 			else
463a3351425Sdougm 				value = "true";
464a3351425Sdougm 		}
465a99982a7Sdougm 
466a99982a7Sdougm 		/*
467a99982a7Sdougm 		 * Get the existing property, if it exists, so we can
468a99982a7Sdougm 		 * determine what to do with it. The ro/rw/root
469a99982a7Sdougm 		 * properties can be merged if multiple instances of
470a99982a7Sdougm 		 * these properies are given. For example, if "rw"
471a99982a7Sdougm 		 * exists with a value "host1" and a later token of
472a99982a7Sdougm 		 * rw="host2" is seen, the values are merged into a
473a99982a7Sdougm 		 * single rw="host1:host2".
474a99982a7Sdougm 		 */
475a3351425Sdougm 		prop = sa_get_property(sec->security, name);
476a99982a7Sdougm 
477a3351425Sdougm 		if (prop != NULL) {
478a3351425Sdougm 			char *oldvalue;
479a3351425Sdougm 			char *newvalue;
480a99982a7Sdougm 
481a99982a7Sdougm 			/*
4825cb0d679SMarcel Telka 			 * The security options of ro/rw/root/uidmap/gidmap
4835cb0d679SMarcel Telka 			 * might appear multiple times.  If they do, the values
4845cb0d679SMarcel Telka 			 * need to be merged.  If it was previously empty, the
4855cb0d679SMarcel Telka 			 * new value alone is added.
486a99982a7Sdougm 			 */
487a3351425Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
488a3351425Sdougm 			if (oldvalue != NULL) {
4895cb0d679SMarcel Telka 				char sep = ':';
4905cb0d679SMarcel Telka 
4915cb0d679SMarcel Telka 				if (strcmp(name, SHOPT_UIDMAP) == 0 ||
4925cb0d679SMarcel Telka 				    strcmp(name, SHOPT_GIDMAP) == 0)
4935cb0d679SMarcel Telka 					sep = '~';
4945cb0d679SMarcel Telka 
495a3351425Sdougm 				/*
496a3351425Sdougm 				 * The general case is to concatenate the new
497a3351425Sdougm 				 * value onto the old value for multiple
4985cb0d679SMarcel Telka 				 * rw(ro/root/uidmap/gidmap) properties.  For
4995cb0d679SMarcel Telka 				 * rw/ro/root a special case exists when either
5005cb0d679SMarcel Telka 				 * the old or new is the "all" case.  In the
5015cb0d679SMarcel Telka 				 * special case, if both are "all", then it is
5025cb0d679SMarcel Telka 				 * "all", else if one is an access-list, that
5035cb0d679SMarcel Telka 				 * replaces the "all".
504a3351425Sdougm 				 */
505a3351425Sdougm 				if (strcmp(oldvalue, "*") == 0) {
506a3351425Sdougm 					/* Replace old value with new value. */
507a3351425Sdougm 					newvalue = strdup(value);
50897df5ac9Sdougm 				} else if (strcmp(value, "*") == 0 ||
50997df5ac9Sdougm 				    strcmp(oldvalue, value) == 0) {
510a3351425Sdougm 					/*
511a3351425Sdougm 					 * Keep old value and ignore
512a3351425Sdougm 					 * the new value.
513a3351425Sdougm 					 */
514a3351425Sdougm 					newvalue = NULL;
515a3351425Sdougm 				} else {
516a3351425Sdougm 					/*
517a3351425Sdougm 					 * Make a new list of old plus new
518a3351425Sdougm 					 * access-list.
519a3351425Sdougm 					 */
520a3351425Sdougm 					newvalue = nfs_alistcat(oldvalue,
5215cb0d679SMarcel Telka 					    value, sep);
522a3351425Sdougm 				}
523a3351425Sdougm 
524a3351425Sdougm 				if (newvalue != NULL) {
525a3351425Sdougm 					(void) sa_remove_property(prop);
526a3351425Sdougm 					prop = sa_create_property(name,
527a3351425Sdougm 					    newvalue);
528a3351425Sdougm 					ret = sa_add_property(sec->security,
529a3351425Sdougm 					    prop);
530a3351425Sdougm 					free(newvalue);
531a3351425Sdougm 				}
5325cb0d679SMarcel Telka 
5335cb0d679SMarcel Telka 				sa_free_attr_string(oldvalue);
534a3351425Sdougm 			}
535a3351425Sdougm 		} else {
536a3351425Sdougm 			prop = sa_create_property(name, value);
537a99982a7Sdougm 			ret = sa_add_property(sec->security, prop);
5386185db85Sdougm 		}
539a3351425Sdougm 		if (ret == SA_OK && !iszfs) {
540a3351425Sdougm 			ret = sa_commit_properties(sec->security, !persist);
541a3351425Sdougm 		}
5426185db85Sdougm 	}
5436185db85Sdougm 	return (ret);
5446185db85Sdougm }
5456185db85Sdougm 
5466185db85Sdougm /*
5476185db85Sdougm  * check to see if group/share is persistent.
5486185db85Sdougm  */
5496185db85Sdougm static int
5506185db85Sdougm is_persistent(sa_group_t group)
5516185db85Sdougm {
5526185db85Sdougm 	char *type;
5536185db85Sdougm 	int persist = 1;
5546185db85Sdougm 
5556185db85Sdougm 	type = sa_get_group_attr(group, "type");
5566185db85Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
557a3351425Sdougm 		persist = 0;
5586185db85Sdougm 	if (type != NULL)
559a3351425Sdougm 		sa_free_attr_string(type);
5606185db85Sdougm 	return (persist);
5616185db85Sdougm }
5626185db85Sdougm 
5636185db85Sdougm /*
5646185db85Sdougm  * invalid_security(options)
5656185db85Sdougm  *
5666185db85Sdougm  * search option string for any invalid sec= type.
5676185db85Sdougm  * return true (1) if any are not valid else false (0)
5686185db85Sdougm  */
5696185db85Sdougm static int
5706185db85Sdougm invalid_security(char *options)
5716185db85Sdougm {
5726185db85Sdougm 	char *copy, *base, *token, *value;
5736185db85Sdougm 	int ret = 0;
5746185db85Sdougm 
5756185db85Sdougm 	copy = strdup(options);
5766185db85Sdougm 	token = base = copy;
5776185db85Sdougm 	while (token != NULL && ret == 0) {
578a3351425Sdougm 		token = strtok(base, ",");
579a3351425Sdougm 		base = NULL;
580a3351425Sdougm 		if (token != NULL) {
581a3351425Sdougm 			value = strchr(token, '=');
582a3351425Sdougm 			if (value != NULL)
583a3351425Sdougm 				*value++ = '\0';
5845cb0d679SMarcel Telka 			if (strcmp(token, SHOPT_SEC) == 0) {
585a3351425Sdougm 				/* HAVE security flavors so check them */
586a3351425Sdougm 				char *tok, *next;
587a3351425Sdougm 				for (next = NULL, tok = value; tok != NULL;
588a3351425Sdougm 				    tok = next) {
589a3351425Sdougm 					next = strchr(tok, ':');
590a3351425Sdougm 					if (next != NULL)
591a3351425Sdougm 						*next++ = '\0';
592a3351425Sdougm 					ret = !nfs_validate_security_mode(tok);
593a3351425Sdougm 					if (ret)
594a3351425Sdougm 						break;
595a3351425Sdougm 				}
596a3351425Sdougm 			}
5976185db85Sdougm 		}
5986185db85Sdougm 	}
5996185db85Sdougm 	if (copy != NULL)
600a3351425Sdougm 		free(copy);
6016185db85Sdougm 	return (ret);
6026185db85Sdougm }
6036185db85Sdougm 
6046185db85Sdougm /*
6056185db85Sdougm  * nfs_parse_legacy_options(group, options)
6066185db85Sdougm  *
6076185db85Sdougm  * Parse the old style options into internal format and store on the
6086185db85Sdougm  * specified group.  Group could be a share for full legacy support.
6096185db85Sdougm  */
6106185db85Sdougm 
6116185db85Sdougm static int
6126185db85Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
6136185db85Sdougm {
614aba7a40bSdougm 	char *dup;
6156185db85Sdougm 	char *base;
6166185db85Sdougm 	char *token;
6176185db85Sdougm 	sa_optionset_t optionset;
6186185db85Sdougm 	struct securities *security_list = NULL;
6196185db85Sdougm 	sa_property_t prop;
6206185db85Sdougm 	int ret = SA_OK;
6216185db85Sdougm 	int iszfs = 0;
6226185db85Sdougm 	sa_group_t parent;
6236185db85Sdougm 	int persist = 0;
6246185db85Sdougm 	char *lasts;
6256185db85Sdougm 
6266185db85Sdougm 	/* do we have an existing optionset? */
6276185db85Sdougm 	optionset = sa_get_optionset(group, "nfs");
6286185db85Sdougm 	if (optionset == NULL) {
629a3351425Sdougm 		/* didn't find existing optionset so create one */
630a3351425Sdougm 		optionset = sa_create_optionset(group, "nfs");
6316185db85Sdougm 	} else {
6326185db85Sdougm 		/*
633da6c28aaSamw 		 * Have an existing optionset . Ideally, we would need
634da6c28aaSamw 		 * to compare options in order to detect errors. For
635da6c28aaSamw 		 * now, we assume that the first optionset is the
636da6c28aaSamw 		 * correct one and the others will be the same. An
637da6c28aaSamw 		 * empty optionset is the same as no optionset so we
638da6c28aaSamw 		 * don't want to exit in that case. Getting an empty
639da6c28aaSamw 		 * optionset can occur with ZFS property checking.
6406185db85Sdougm 		 */
641da6c28aaSamw 		if (sa_get_property(optionset, NULL) != NULL)
642da6c28aaSamw 			return (ret);
6436185db85Sdougm 	}
6446185db85Sdougm 
6456185db85Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
6466185db85Sdougm 		/*
6476185db85Sdougm 		 * there is a special case of only the option "rw"
6486185db85Sdougm 		 * being the default option. We don't have to do
6496185db85Sdougm 		 * anything.
6506185db85Sdougm 		 */
651a3351425Sdougm 		return (ret);
6526185db85Sdougm 	}
6536185db85Sdougm 
6546185db85Sdougm 	/*
6556185db85Sdougm 	 * check if security types are present and validate them. If
6566185db85Sdougm 	 * any are not legal, fail.
6576185db85Sdougm 	 */
6586185db85Sdougm 
6596185db85Sdougm 	if (invalid_security(options)) {
660a3351425Sdougm 		return (SA_INVALID_SECURITY);
6616185db85Sdougm 	}
6626185db85Sdougm 
6636185db85Sdougm 	/*
6646185db85Sdougm 	 * in order to not attempt to change ZFS properties unless
6656185db85Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
6666185db85Sdougm 	 */
6676185db85Sdougm 	if (sa_is_share(group)) {
668a3351425Sdougm 		char *zfs;
669a3351425Sdougm 		parent = sa_get_parent_group(group);
670a3351425Sdougm 		if (parent != NULL) {
671a3351425Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
672a3351425Sdougm 			if (zfs != NULL) {
673a3351425Sdougm 				sa_free_attr_string(zfs);
674a3351425Sdougm 				iszfs++;
675a3351425Sdougm 			}
6766185db85Sdougm 		}
6776185db85Sdougm 	} else {
678a3351425Sdougm 		iszfs = sa_group_is_zfs(group);
6796185db85Sdougm 	}
6806185db85Sdougm 
681aba7a40bSdougm 	/* We need a copy of options for the next part. */
682aba7a40bSdougm 	dup = strdup(options);
683aba7a40bSdougm 	if (dup == NULL)
684aba7a40bSdougm 		return (SA_NO_MEMORY);
685aba7a40bSdougm 
6866185db85Sdougm 	/*
6876185db85Sdougm 	 * we need to step through each option in the string and then
6886185db85Sdougm 	 * add either the option or the security option as needed. If
6896185db85Sdougm 	 * this is not a persistent share, don't commit to the
690a99982a7Sdougm 	 * repository. If there is an error, we also want to abort the
691a99982a7Sdougm 	 * processing and report it.
6926185db85Sdougm 	 */
6936185db85Sdougm 	persist = is_persistent(group);
6946185db85Sdougm 	base = dup;
6956185db85Sdougm 	token = dup;
6966185db85Sdougm 	lasts = NULL;
697a99982a7Sdougm 	while (token != NULL && ret == SA_OK) {
698a3351425Sdougm 		token = strtok_r(base, ",", &lasts);
699a3351425Sdougm 		base = NULL;
700a3351425Sdougm 		if (token != NULL) {
701a3351425Sdougm 			char *value;
7026185db85Sdougm 			/*
703a3351425Sdougm 			 * if the option has a value, it will have an '=' to
704a3351425Sdougm 			 * separate the name from the value. The following
705a3351425Sdougm 			 * code will result in value != NULL and token
706a3351425Sdougm 			 * pointing to just the name if there is a value.
7076185db85Sdougm 			 */
708a3351425Sdougm 			value = strchr(token, '=');
709a3351425Sdougm 			if (value != NULL) {
710a3351425Sdougm 				*value++ = '\0';
7116185db85Sdougm 			}
7125cb0d679SMarcel Telka 			if (strcmp(token, SHOPT_SEC) == 0 ||
7135cb0d679SMarcel Telka 			    strcmp(token, SHOPT_SECURE) == 0) {
7146185db85Sdougm 				/*
715a3351425Sdougm 				 * Once in security parsing, we only
716a3351425Sdougm 				 * do security. We do need to move
717a3351425Sdougm 				 * between the security node and the
718a3351425Sdougm 				 * toplevel. The security tag goes on
719a3351425Sdougm 				 * the root while the following ones
720a3351425Sdougm 				 * go on the security.
7216185db85Sdougm 				 */
722a3351425Sdougm 				if (security_list != NULL) {
723a3351425Sdougm 					/*
724a3351425Sdougm 					 * have an old list so close it and
725a3351425Sdougm 					 * start the new
726a3351425Sdougm 					 */
727a3351425Sdougm 					free_security_list(security_list);
728a3351425Sdougm 				}
7295cb0d679SMarcel Telka 				if (strcmp(token, SHOPT_SECURE) == 0) {
730a3351425Sdougm 					value = "dh";
731a3351425Sdougm 				} else {
732a3351425Sdougm 					if (value == NULL) {
733a3351425Sdougm 						ret = SA_SYNTAX_ERR;
734a3351425Sdougm 						break;
735a3351425Sdougm 					}
736a3351425Sdougm 				}
737a3351425Sdougm 				security_list = make_security_list(group,
738a3351425Sdougm 				    value, "nfs");
7396185db85Sdougm 			} else {
740a3351425Sdougm 				/*
741a3351425Sdougm 				 * Note that the "old" syntax allowed a
7425cb0d679SMarcel Telka 				 * default security model.  This must be
743a3351425Sdougm 				 * accounted for and internally converted to
744a3351425Sdougm 				 * "standard" security structure.
745a3351425Sdougm 				 */
746a3351425Sdougm 				if (nfs_is_security_opt(token)) {
747a3351425Sdougm 					if (security_list == NULL) {
748a3351425Sdougm 						/*
749a3351425Sdougm 						 * need to have a
750a3351425Sdougm 						 * security
751a3351425Sdougm 						 * option. This will
752a3351425Sdougm 						 * be "closed" when a
753a3351425Sdougm 						 * defined "sec="
754a3351425Sdougm 						 * option is
755a3351425Sdougm 						 * seen. This is
756a3351425Sdougm 						 * technically an
757a3351425Sdougm 						 * error but will be
758a3351425Sdougm 						 * allowed with
759a3351425Sdougm 						 * warning.
760a3351425Sdougm 						 */
761a3351425Sdougm 						security_list =
762a3351425Sdougm 						    make_security_list(group,
763a3351425Sdougm 						    "default",
764a3351425Sdougm 						    "nfs");
765a3351425Sdougm 					}
766a3351425Sdougm 					if (security_list != NULL) {
767a3351425Sdougm 						ret = add_security_prop(
768a3351425Sdougm 						    security_list, token,
769a3351425Sdougm 						    value, persist, iszfs);
770a3351425Sdougm 					} else {
771a3351425Sdougm 						ret = SA_NO_MEMORY;
772a3351425Sdougm 					}
773a3351425Sdougm 				} else {
774a3351425Sdougm 					/* regular options */
775a3351425Sdougm 					if (value == NULL) {
776a3351425Sdougm 						if (strcmp(token, SHOPT_RW) ==
777a3351425Sdougm 						    0 || strcmp(token,
778a3351425Sdougm 						    SHOPT_RO) == 0) {
779a3351425Sdougm 							value = "*";
780a3351425Sdougm 						} else {
781a3351425Sdougm 							value = "global";
782a3351425Sdougm 							if (strcmp(token,
783a3351425Sdougm 							    SHOPT_LOG) != 0) {
784a3351425Sdougm 								value = "true";
785a3351425Sdougm 							}
786a3351425Sdougm 						}
787a3351425Sdougm 					}
788d6405362Sdougm 					/*
789d6405362Sdougm 					 * In all cases, create the
790d6405362Sdougm 					 * property specified. If the
791d6405362Sdougm 					 * value was NULL, the default
792d6405362Sdougm 					 * value will have been
793d6405362Sdougm 					 * substituted.
794d6405362Sdougm 					 */
795d6405362Sdougm 					prop = sa_create_property(token, value);
796d6405362Sdougm 					ret =  sa_add_property(optionset, prop);
797d6405362Sdougm 					if (ret != SA_OK)
798d6405362Sdougm 						break;
799d6405362Sdougm 
800a3351425Sdougm 					if (!iszfs) {
801a3351425Sdougm 						ret = sa_commit_properties(
802a3351425Sdougm 						    optionset, !persist);
803a3351425Sdougm 					}
804a3351425Sdougm 				}
8056185db85Sdougm 			}
8066185db85Sdougm 		}
8076185db85Sdougm 	}
8086185db85Sdougm 	if (security_list != NULL)
809a3351425Sdougm 		free_security_list(security_list);
810aba7a40bSdougm 
811aba7a40bSdougm 	free(dup);
8126185db85Sdougm 	return (ret);
8136185db85Sdougm }
8146185db85Sdougm 
8156185db85Sdougm /*
8166185db85Sdougm  * is_a_number(number)
8176185db85Sdougm  *
8186185db85Sdougm  * is the string a number in one of the forms we want to use?
8196185db85Sdougm  */
8206185db85Sdougm 
8216185db85Sdougm static int
8226185db85Sdougm is_a_number(char *number)
8236185db85Sdougm {
8246185db85Sdougm 	int ret = 1;
8256185db85Sdougm 	int hex = 0;
8266185db85Sdougm 
8276185db85Sdougm 	if (strncmp(number, "0x", 2) == 0) {
828a3351425Sdougm 		number += 2;
829a3351425Sdougm 		hex = 1;
830a3351425Sdougm 	} else if (*number == '-') {
831a3351425Sdougm 		number++; /* skip the minus */
832a3351425Sdougm 	}
8336185db85Sdougm 	while (ret == 1 && *number != '\0') {
834a3351425Sdougm 		if (hex) {
835a3351425Sdougm 			ret = isxdigit(*number++);
836a3351425Sdougm 		} else {
837a3351425Sdougm 			ret = isdigit(*number++);
838a3351425Sdougm 		}
8396185db85Sdougm 	}
8406185db85Sdougm 	return (ret);
8416185db85Sdougm }
8426185db85Sdougm 
8436185db85Sdougm /*
8446185db85Sdougm  * Look for the specified tag in the configuration file. If it is found,
8456185db85Sdougm  * enable logging and set the logging configuration information for exp.
8466185db85Sdougm  */
8476185db85Sdougm static void
8486185db85Sdougm configlog(struct exportdata *exp, char *tag)
8496185db85Sdougm {
8506185db85Sdougm 	nfsl_config_t *configlist = NULL, *configp;
8516185db85Sdougm 	int error = 0;
8526185db85Sdougm 	char globaltag[] = DEFAULTTAG;
8536185db85Sdougm 
8546185db85Sdougm 	/*
8556185db85Sdougm 	 * Sends config errors to stderr
8566185db85Sdougm 	 */
8576185db85Sdougm 	nfsl_errs_to_syslog = B_FALSE;
8586185db85Sdougm 
8596185db85Sdougm 	/*
8606185db85Sdougm 	 * get the list of configuration settings
8616185db85Sdougm 	 */
8626185db85Sdougm 	error = nfsl_getconfig_list(&configlist);
8636185db85Sdougm 	if (error) {
8646185db85Sdougm 		(void) fprintf(stderr,
865a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
866a3351425Sdougm 		    strerror(error));
8676185db85Sdougm 	}
8686185db85Sdougm 
8696185db85Sdougm 	if (tag == NULL)
8706185db85Sdougm 		tag = globaltag;
8716185db85Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
8726185db85Sdougm 		nfsl_freeconfig_list(&configlist);
8736185db85Sdougm 		(void) fprintf(stderr,
874a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
8756185db85Sdougm 		/* bad configuration */
8766185db85Sdougm 		error = ENOENT;
8776185db85Sdougm 		goto err;
8786185db85Sdougm 	}
8796185db85Sdougm 
8806185db85Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
8816185db85Sdougm 		error = ENOMEM;
8826185db85Sdougm 		goto out;
8836185db85Sdougm 	}
8846185db85Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
8856185db85Sdougm 		error = ENOMEM;
8866185db85Sdougm 		goto out;
8876185db85Sdougm 	}
8886185db85Sdougm 	exp->ex_flags |= EX_LOG;
8896185db85Sdougm 	if (configp->nc_rpclogpath != NULL)
8906185db85Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8916185db85Sdougm out:
8926185db85Sdougm 	if (configlist != NULL)
893a3351425Sdougm 		nfsl_freeconfig_list(&configlist);
8946185db85Sdougm 
8956185db85Sdougm err:
8966185db85Sdougm 	if (error != 0) {
897*822e6387SMarcel Telka 		free(exp->ex_tag);
898*822e6387SMarcel Telka 		free(exp->ex_log_buffer);
8996185db85Sdougm 		(void) fprintf(stderr,
900a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
901a3351425Sdougm 		    strerror(error));
9026185db85Sdougm 	}
9036185db85Sdougm }
9046185db85Sdougm 
9056185db85Sdougm /*
9066185db85Sdougm  * fill_export_from_optionset(export, optionset)
9076185db85Sdougm  *
9086185db85Sdougm  * In order to share, we need to set all the possible general options
9096185db85Sdougm  * into the export structure. Share info will be filled in by the
9106185db85Sdougm  * caller. Various property values get turned into structure specific
9116185db85Sdougm  * values.
9126185db85Sdougm  */
9136185db85Sdougm 
9146185db85Sdougm static int
9156185db85Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
9166185db85Sdougm {
9176185db85Sdougm 	sa_property_t option;
9186185db85Sdougm 	int ret = SA_OK;
9196185db85Sdougm 
9206185db85Sdougm 	for (option = sa_get_property(optionset, NULL);
921a3351425Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
922a3351425Sdougm 		char *name;
923a3351425Sdougm 		char *value;
924a3351425Sdougm 		uint32_t val;
9256185db85Sdougm 
926a3351425Sdougm 		/*
927a3351425Sdougm 		 * since options may be set/reset multiple times, always do an
928a3351425Sdougm 		 * explicit set or clear of the option. This allows defaults
929da6c28aaSamw 		 * to be set and then the protocol specific to override.
930a3351425Sdougm 		 */
9316185db85Sdougm 
932a3351425Sdougm 		name = sa_get_property_attr(option, "type");
933a3351425Sdougm 		value = sa_get_property_attr(option, "value");
934a3351425Sdougm 		switch (findopt(name)) {
935a3351425Sdougm 		case OPT_ANON:
936a3351425Sdougm 			if (value != NULL && is_a_number(value)) {
937a3351425Sdougm 				val = strtoul(value, NULL, 0);
938a3351425Sdougm 			} else {
939a3351425Sdougm 				struct passwd *pw;
940a3351425Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
941a3351425Sdougm 				if (pw != NULL) {
942a3351425Sdougm 					val = pw->pw_uid;
943a3351425Sdougm 				} else {
944a3351425Sdougm 					val = UID_NOBODY;
945a3351425Sdougm 				}
946a3351425Sdougm 				endpwent();
947a3351425Sdougm 			}
948a3351425Sdougm 			export->ex_anon = val;
949a3351425Sdougm 			break;
950a3351425Sdougm 		case OPT_NOSUID:
951a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
952a3351425Sdougm 			    strcmp(value, "1") == 0))
953a3351425Sdougm 				export->ex_flags |= EX_NOSUID;
954a3351425Sdougm 			else
955a3351425Sdougm 				export->ex_flags &= ~EX_NOSUID;
956a3351425Sdougm 			break;
957a3351425Sdougm 		case OPT_ACLOK:
958a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
959a3351425Sdougm 			    strcmp(value, "1") == 0))
960a3351425Sdougm 				export->ex_flags |= EX_ACLOK;
961a3351425Sdougm 			else
962a3351425Sdougm 				export->ex_flags &= ~EX_ACLOK;
963a3351425Sdougm 			break;
964a3351425Sdougm 		case OPT_NOSUB:
965a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
966a3351425Sdougm 			    strcmp(value, "1") == 0))
967a3351425Sdougm 				export->ex_flags |= EX_NOSUB;
968a3351425Sdougm 			else
969a3351425Sdougm 				export->ex_flags &= ~EX_NOSUB;
970a3351425Sdougm 			break;
971a3351425Sdougm 		case OPT_PUBLIC:
972a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
973a3351425Sdougm 			    strcmp(value, "1") == 0))
974a3351425Sdougm 				export->ex_flags |= EX_PUBLIC;
975a3351425Sdougm 			else
976a3351425Sdougm 				export->ex_flags &= ~EX_PUBLIC;
977a3351425Sdougm 			break;
978a3351425Sdougm 		case OPT_INDEX:
979a3351425Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
980a3351425Sdougm 			    strchr(value, '/') != NULL)) {
981a3351425Sdougm 				/* this is an error */
982a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
983a3351425Sdougm 				    "NFS: index=\"%s\" not valid;"
984a3351425Sdougm 				    "must be a filename.\n"),
985a3351425Sdougm 				    value);
986a3351425Sdougm 				break;
987a3351425Sdougm 			}
988a3351425Sdougm 			if (value != NULL && *value != '\0' &&
989a3351425Sdougm 			    strcmp(value, ".") != 0) {
990a3351425Sdougm 				/* valid index file string */
991a3351425Sdougm 				if (export->ex_index != NULL) {
992a3351425Sdougm 					/* left over from "default" */
993a3351425Sdougm 					free(export->ex_index);
994a3351425Sdougm 				}
995a3351425Sdougm 				/* remember to free */
996a3351425Sdougm 				export->ex_index = strdup(value);
997a3351425Sdougm 				if (export->ex_index == NULL) {
998a3351425Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
999a3351425Sdougm 					    "NFS: out of memory setting "
1000a3351425Sdougm 					    "index property\n"));
1001a3351425Sdougm 					break;
1002a3351425Sdougm 				}
1003a3351425Sdougm 				export->ex_flags |= EX_INDEX;
1004a3351425Sdougm 			}
1005a3351425Sdougm 			break;
1006a3351425Sdougm 		case OPT_LOG:
1007a3351425Sdougm 			if (value == NULL)
1008a3351425Sdougm 				value = strdup("global");
1009a3351425Sdougm 			if (value != NULL)
1010a3351425Sdougm 				configlog(export,
1011a3351425Sdougm 				    strlen(value) ? value : "global");
1012a3351425Sdougm 			break;
1013b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_CHARSET_MAP:
1014b89a8333Snatalie li - Sun Microsystems - Irvine United States 			/*
1015b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * Set EX_CHARMAP when there is at least one
1016b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * charmap conversion property. This will get
1017b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * checked by the nfs server when it needs to.
1018b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 */
1019b89a8333Snatalie li - Sun Microsystems - Irvine United States 			export->ex_flags |= EX_CHARMAP;
1020b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
10216764e83bSVallish Vaidyeshwara 		case OPT_NOACLFAB:
10226764e83bSVallish Vaidyeshwara 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
10236764e83bSVallish Vaidyeshwara 			    strcmp(value, "1") == 0))
10246764e83bSVallish Vaidyeshwara 				export->ex_flags |= EX_NOACLFAB;
10256764e83bSVallish Vaidyeshwara 			else
10266764e83bSVallish Vaidyeshwara 				export->ex_flags &= ~EX_NOACLFAB;
10276764e83bSVallish Vaidyeshwara 			break;
1028a3351425Sdougm 		default:
1029a3351425Sdougm 			/* have a syntactic error */
1030549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1031a3351425Sdougm 			    "NFS: unrecognized option %s=%s\n"),
1032fe1c642dSBill Krier 			    name != NULL ? name : "",
1033fe1c642dSBill Krier 			    value != NULL ? value : "");
10346185db85Sdougm 			break;
10356185db85Sdougm 		}
1036a3351425Sdougm 		if (name != NULL)
1037a3351425Sdougm 			sa_free_attr_string(name);
10386185db85Sdougm 		if (value != NULL)
1039a3351425Sdougm 			sa_free_attr_string(value);
10406185db85Sdougm 	}
10416185db85Sdougm 	return (ret);
10426185db85Sdougm }
10436185db85Sdougm 
10446185db85Sdougm /*
10456185db85Sdougm  * cleanup_export(export)
10466185db85Sdougm  *
10476185db85Sdougm  * Cleanup the allocated areas so we don't leak memory
10486185db85Sdougm  */
10496185db85Sdougm 
10506185db85Sdougm static void
10516185db85Sdougm cleanup_export(struct exportdata *export)
10526185db85Sdougm {
10536185db85Sdougm 	int i;
10546185db85Sdougm 
10553253d86fSMarcel Telka 	free(export->ex_index);
10563253d86fSMarcel Telka 
10573253d86fSMarcel Telka 	for (i = 0; i < export->ex_seccnt; i++) {
10583253d86fSMarcel Telka 		struct secinfo *s = &export->ex_secinfo[i];
10593253d86fSMarcel Telka 
10603253d86fSMarcel Telka 		while (s->s_rootcnt > 0)
10613253d86fSMarcel Telka 			free(s->s_rootnames[--s->s_rootcnt]);
10623253d86fSMarcel Telka 
10633253d86fSMarcel Telka 		free(s->s_rootnames);
10646185db85Sdougm 	}
10653253d86fSMarcel Telka 	free(export->ex_secinfo);
10666185db85Sdougm }
10676185db85Sdougm 
10686185db85Sdougm /*
10696185db85Sdougm  * Given a seconfig entry and a colon-separated
10706185db85Sdougm  * list of names, allocate an array big enough
10716185db85Sdougm  * to hold the root list, then convert each name to
10726185db85Sdougm  * a principal name according to the security
10736185db85Sdougm  * info and assign it to an array element.
10746185db85Sdougm  * Return the array and its size.
10756185db85Sdougm  */
10766185db85Sdougm static caddr_t *
10776185db85Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
10786185db85Sdougm {
10796185db85Sdougm 	caddr_t *a;
10806185db85Sdougm 	int c, i;
10816185db85Sdougm 	char *host, *p;
10826185db85Sdougm 
10836185db85Sdougm 	/*
10846185db85Sdougm 	 * Count the number of strings in the list.
10856185db85Sdougm 	 * This is the number of colon separators + 1.
10866185db85Sdougm 	 */
10876185db85Sdougm 	c = 1;
10886185db85Sdougm 	for (p = list; *p; p++)
10896185db85Sdougm 		if (*p == ':')
10906185db85Sdougm 			c++;
10916185db85Sdougm 	*count = c;
10926185db85Sdougm 
10936185db85Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
10946185db85Sdougm 	if (a == NULL) {
1095549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1096a3351425Sdougm 		    "get_rootnames: no memory\n"));
10976185db85Sdougm 	} else {
1098a3351425Sdougm 		for (i = 0; i < c; i++) {
1099a3351425Sdougm 			host = strtok(list, ":");
1100a3351425Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
11013253d86fSMarcel Telka 				while (i > 0)
11023253d86fSMarcel Telka 					free(a[--i]);
1103a3351425Sdougm 				free(a);
1104a3351425Sdougm 				a = NULL;
1105a3351425Sdougm 				break;
1106a3351425Sdougm 			}
1107a3351425Sdougm 			list = NULL;
11086185db85Sdougm 		}
11096185db85Sdougm 	}
11106185db85Sdougm 
11116185db85Sdougm 	return (a);
11126185db85Sdougm }
11136185db85Sdougm 
11146185db85Sdougm /*
11156185db85Sdougm  * fill_security_from_secopts(sp, secopts)
11166185db85Sdougm  *
11176185db85Sdougm  * Fill the secinfo structure from the secopts optionset.
11186185db85Sdougm  */
11196185db85Sdougm 
11206185db85Sdougm static int
11216185db85Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
11226185db85Sdougm {
11236185db85Sdougm 	sa_property_t prop;
11246185db85Sdougm 	char *type;
11256185db85Sdougm 	int longform;
1126a99982a7Sdougm 	int err = SC_NOERROR;
1127b89a8333Snatalie li - Sun Microsystems - Irvine United States 	uint32_t val;
11286185db85Sdougm 
11296185db85Sdougm 	type = sa_get_security_attr(secopts, "sectype");
11306185db85Sdougm 	if (type != NULL) {
1131a3351425Sdougm 		/* named security type needs secinfo to be filled in */
1132a3351425Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
1133a3351425Sdougm 		sa_free_attr_string(type);
1134a3351425Sdougm 		if (err != SC_NOERROR)
1135a3351425Sdougm 			return (err);
11366185db85Sdougm 	} else {
1137a3351425Sdougm 		/* default case */
1138a3351425Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
1139a3351425Sdougm 		if (err != SC_NOERROR)
1140a3351425Sdougm 			return (err);
11416185db85Sdougm 	}
11426185db85Sdougm 
1143a99982a7Sdougm 	err = SA_OK;
11446185db85Sdougm 	for (prop = sa_get_property(secopts, NULL);
1145a3351425Sdougm 	    prop != NULL && err == SA_OK;
1146a3351425Sdougm 	    prop = sa_get_next_property(prop)) {
1147a3351425Sdougm 		char *name;
1148a3351425Sdougm 		char *value;
11496185db85Sdougm 
1150a3351425Sdougm 		name = sa_get_property_attr(prop, "type");
1151a3351425Sdougm 		value = sa_get_property_attr(prop, "value");
11526185db85Sdougm 
1153a3351425Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
11546185db85Sdougm 
1155a3351425Sdougm 		switch (findopt(name)) {
1156a3351425Sdougm 		case OPT_RO:
1157a3351425Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
1158a3351425Sdougm 			break;
1159a3351425Sdougm 		case OPT_RW:
1160a3351425Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
1161a3351425Sdougm 			break;
1162a3351425Sdougm 		case OPT_ROOT:
1163a3351425Sdougm 			sp->s_flags |= M_ROOT;
1164a3351425Sdougm 			/*
1165a3351425Sdougm 			 * if we are using AUTH_UNIX, handle like other things
1166a3351425Sdougm 			 * such as RO/RW
1167a3351425Sdougm 			 */
1168a3351425Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
11696020a1b7SMarcel Telka 				break;
1170a3351425Sdougm 			/* not AUTH_UNIX */
1171a3351425Sdougm 			if (value != NULL) {
1172a3351425Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
1173a3351425Sdougm 				    value, &sp->s_rootcnt);
1174a3351425Sdougm 				if (sp->s_rootnames == NULL) {
1175a3351425Sdougm 					err = SA_BAD_VALUE;
1176a3351425Sdougm 					(void) fprintf(stderr,
1177a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
1178a3351425Sdougm 					    "Bad root list\n"));
1179a3351425Sdougm 				}
1180a3351425Sdougm 			}
1181a3351425Sdougm 			break;
1182b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_NONE:
1183b89a8333Snatalie li - Sun Microsystems - Irvine United States 			sp->s_flags |= M_NONE;
1184b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
1185a3351425Sdougm 		case OPT_WINDOW:
1186a3351425Sdougm 			if (value != NULL) {
1187a3351425Sdougm 				sp->s_window = atoi(value);
1188a3351425Sdougm 				/* just in case */
1189a3351425Sdougm 				if (sp->s_window < 0)
1190a3351425Sdougm 					sp->s_window = DEF_WIN;
1191a3351425Sdougm 			}
1192a3351425Sdougm 			break;
1193b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_ROOT_MAPPING:
1194b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (value != NULL && is_a_number(value)) {
1195b89a8333Snatalie li - Sun Microsystems - Irvine United States 				val = strtoul(value, NULL, 0);
1196b89a8333Snatalie li - Sun Microsystems - Irvine United States 			} else {
1197b89a8333Snatalie li - Sun Microsystems - Irvine United States 				struct passwd *pw;
1198b89a8333Snatalie li - Sun Microsystems - Irvine United States 				pw = getpwnam(value != NULL ? value : "nobody");
1199b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (pw != NULL) {
1200b89a8333Snatalie li - Sun Microsystems - Irvine United States 					val = pw->pw_uid;
1201b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else {
1202b89a8333Snatalie li - Sun Microsystems - Irvine United States 					val = UID_NOBODY;
1203b89a8333Snatalie li - Sun Microsystems - Irvine United States 				}
1204b89a8333Snatalie li - Sun Microsystems - Irvine United States 				endpwent();
1205b89a8333Snatalie li - Sun Microsystems - Irvine United States 			}
1206b89a8333Snatalie li - Sun Microsystems - Irvine United States 			sp->s_rootid = val;
1207b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
12085cb0d679SMarcel Telka 		case OPT_UIDMAP:
12095cb0d679SMarcel Telka 		case OPT_GIDMAP:
12105cb0d679SMarcel Telka 			sp->s_flags |= M_MAP;
12115cb0d679SMarcel Telka 			break;
1212a3351425Sdougm 		default:
1213a3351425Sdougm 			break;
12146185db85Sdougm 		}
1215a3351425Sdougm 		if (name != NULL)
1216a3351425Sdougm 			sa_free_attr_string(name);
1217a3351425Sdougm 		if (value != NULL)
1218a3351425Sdougm 			sa_free_attr_string(value);
12196185db85Sdougm 	}
12206185db85Sdougm 	/* if rw/ro options not set, use default of RW */
12216185db85Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
1222a3351425Sdougm 		sp->s_flags |= M_RW;
12236185db85Sdougm 	return (err);
12246185db85Sdougm }
12256185db85Sdougm 
12266185db85Sdougm /*
12276185db85Sdougm  * This is for testing only
12286185db85Sdougm  * It displays the export structure that
12296185db85Sdougm  * goes into the kernel.
12306185db85Sdougm  */
12316185db85Sdougm static void
12326185db85Sdougm printarg(char *path, struct exportdata *ep)
12336185db85Sdougm {
12346185db85Sdougm 	int i, j;
12356185db85Sdougm 	struct secinfo *sp;
12366185db85Sdougm 
12376185db85Sdougm 	if (debug == 0)
1238a3351425Sdougm 		return;
12396185db85Sdougm 
12406185db85Sdougm 	(void) printf("%s:\n", path);
12416185db85Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
12426185db85Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
1243549ec3ffSdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
12446185db85Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
12456185db85Sdougm 	if (ep->ex_flags & EX_NOSUID)
12466185db85Sdougm 		(void) printf("NOSUID ");
12476185db85Sdougm 	if (ep->ex_flags & EX_ACLOK)
12486185db85Sdougm 		(void) printf("ACLOK ");
12496185db85Sdougm 	if (ep->ex_flags & EX_PUBLIC)
12506185db85Sdougm 		(void) printf("PUBLIC ");
12516185db85Sdougm 	if (ep->ex_flags & EX_NOSUB)
12526185db85Sdougm 		(void) printf("NOSUB ");
12536185db85Sdougm 	if (ep->ex_flags & EX_LOG)
12546185db85Sdougm 		(void) printf("LOG ");
1255b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (ep->ex_flags & EX_CHARMAP)
1256b89a8333Snatalie li - Sun Microsystems - Irvine United States 		(void) printf("CHARMAP ");
12576185db85Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
12586185db85Sdougm 		(void) printf("LOG_ALLOPS ");
12596185db85Sdougm 	if (ep->ex_flags == 0)
12606185db85Sdougm 		(void) printf("(none)");
12616185db85Sdougm 	(void) 	printf("\n");
12626185db85Sdougm 	if (ep->ex_flags & EX_LOG) {
12636185db85Sdougm 		(void) printf("\tex_log_buffer = %s\n",
1264a3351425Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
12656185db85Sdougm 		(void) printf("\tex_tag = %s\n",
1266a3351425Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
12676185db85Sdougm 	}
12686185db85Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
12696185db85Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
12706185db85Sdougm 	(void) printf("\n");
12716185db85Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
12726185db85Sdougm 		sp = &ep->ex_secinfo[i];
12736185db85Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
12746185db85Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
12756185db85Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
12766185db85Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
12776185db85Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
12786185db85Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
12796185db85Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
1280b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (sp->s_flags & M_NONE) (void) printf("M_NONE ");
12815cb0d679SMarcel Telka 		if (sp->s_flags & M_MAP) (void) printf("M_MAP ");
12826185db85Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
12836185db85Sdougm 		(void) printf("\n");
12846185db85Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
1285b89a8333Snatalie li - Sun Microsystems - Irvine United States 		(void) printf("\t\ts_rootid = %d\n", sp->s_rootid);
12866185db85Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
12876185db85Sdougm 		(void) fflush(stdout);
12886185db85Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
12896185db85Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
1290a3351425Sdougm 			    sp->s_rootnames[j] : "<null>");
12916185db85Sdougm 		(void) printf("\n\n");
12926185db85Sdougm 	}
12936185db85Sdougm }
12946185db85Sdougm 
12956185db85Sdougm /*
12966185db85Sdougm  * count_security(opts)
12976185db85Sdougm  *
12986185db85Sdougm  * Count the number of security types (flavors). The optionset has
12996185db85Sdougm  * been populated with the security flavors as a holding mechanism.
13006185db85Sdougm  * We later use this number to allocate data structures.
13016185db85Sdougm  */
13026185db85Sdougm 
13036185db85Sdougm static int
13046185db85Sdougm count_security(sa_optionset_t opts)
13056185db85Sdougm {
13066185db85Sdougm 	int count = 0;
13076185db85Sdougm 	sa_property_t prop;
13086185db85Sdougm 	if (opts != NULL) {
1309a3351425Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
1310a3351425Sdougm 		    prop = sa_get_next_property(prop)) {
1311a3351425Sdougm 			count++;
1312a3351425Sdougm 		}
13136185db85Sdougm 	}
13146185db85Sdougm 	return (count);
13156185db85Sdougm }
13166185db85Sdougm 
13176185db85Sdougm /*
13186185db85Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
13196185db85Sdougm  *
13206185db85Sdougm  * provides a mechanism to format NFS properties into legacy output
13216185db85Sdougm  * format. If the buffer would overflow, it is reallocated and grown
13226185db85Sdougm  * as appropriate. Special cases of converting internal form of values
13236185db85Sdougm  * to those used by "share" are done. this function does one property
13246185db85Sdougm  * at a time.
13256185db85Sdougm  */
13266185db85Sdougm 
1327aed5d200Sdougm static int
13286185db85Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
13296185db85Sdougm 			sa_property_t prop, int sep)
13306185db85Sdougm {
13316185db85Sdougm 	char *name;
13326185db85Sdougm 	char *value;
13336185db85Sdougm 	int curlen;
13346185db85Sdougm 	char *buff = *rbuff;
13356185db85Sdougm 	size_t buffsize = *rbuffsize;
1336aed5d200Sdougm 	int printed = B_FALSE;
13376185db85Sdougm 
13386185db85Sdougm 	name = sa_get_property_attr(prop, "type");
13396185db85Sdougm 	value = sa_get_property_attr(prop, "value");
13406185db85Sdougm 	if (buff != NULL)
1341a3351425Sdougm 		curlen = strlen(buff);
13426185db85Sdougm 	else
1343a3351425Sdougm 		curlen = 0;
13446185db85Sdougm 	if (name != NULL) {
1345a3351425Sdougm 		int len;
1346a3351425Sdougm 		len = strlen(name) + sep;
13476185db85Sdougm 
13486185db85Sdougm 		/*
13496185db85Sdougm 		 * A future RFE would be to replace this with more
13506185db85Sdougm 		 * generic code and to possibly handle more types.
13516185db85Sdougm 		 */
1352a3351425Sdougm 		switch (gettype(name)) {
1353a3351425Sdougm 		case OPT_TYPE_BOOLEAN:
1354aed5d200Sdougm 			/*
1355aed5d200Sdougm 			 * For NFS, boolean value of FALSE means it
1356aed5d200Sdougm 			 * doesn't show up in the option list at all.
1357aed5d200Sdougm 			 */
1358a3351425Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
1359aed5d200Sdougm 				goto skip;
1360aed5d200Sdougm 			if (value != NULL) {
1361a3351425Sdougm 				sa_free_attr_string(value);
1362aed5d200Sdougm 				value = NULL;
1363aed5d200Sdougm 			}
1364a3351425Sdougm 			break;
1365a3351425Sdougm 		case OPT_TYPE_ACCLIST:
1366a3351425Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
1367a3351425Sdougm 				sa_free_attr_string(value);
1368a3351425Sdougm 				value = NULL;
1369a3351425Sdougm 			} else {
1370a3351425Sdougm 				if (value != NULL)
1371a3351425Sdougm 					len += 1 + strlen(value);
1372a3351425Sdougm 			}
1373a3351425Sdougm 			break;
1374a3351425Sdougm 		case OPT_TYPE_LOGTAG:
1375a3351425Sdougm 			if (value != NULL && strlen(value) == 0) {
1376a3351425Sdougm 				sa_free_attr_string(value);
1377a3351425Sdougm 				value = NULL;
1378a3351425Sdougm 			} else {
1379a3351425Sdougm 				if (value != NULL)
1380a3351425Sdougm 					len += 1 + strlen(value);
1381a3351425Sdougm 			}
1382a3351425Sdougm 			break;
1383a3351425Sdougm 		default:
1384a3351425Sdougm 			if (value != NULL)
1385a3351425Sdougm 				len += 1 + strlen(value);
1386a3351425Sdougm 			break;
13876185db85Sdougm 		}
1388a3351425Sdougm 		while (buffsize <= (curlen + len)) {
1389a3351425Sdougm 			/* need more room */
1390a3351425Sdougm 			buffsize += incr;
1391a3351425Sdougm 			buff = realloc(buff, buffsize);
1392a3351425Sdougm 			if (buff == NULL) {
1393a3351425Sdougm 				/* realloc failed so free everything */
1394a3351425Sdougm 				if (*rbuff != NULL)
1395a3351425Sdougm 					free(*rbuff);
1396a3351425Sdougm 			}
1397a3351425Sdougm 			*rbuff = buff;
1398a3351425Sdougm 			*rbuffsize = buffsize;
1399aed5d200Sdougm 			if (buff == NULL)
1400aed5d200Sdougm 				goto skip;
1401aed5d200Sdougm 
14026185db85Sdougm 		}
1403aed5d200Sdougm 
1404a3351425Sdougm 		if (buff == NULL)
1405aed5d200Sdougm 			goto skip;
1406aed5d200Sdougm 
1407a3351425Sdougm 		if (value == NULL) {
1408a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
14095cb0d679SMarcel Telka 			    "%s%s", sep ? "," : "", name);
14106185db85Sdougm 		} else {
1411a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1412a3351425Sdougm 			    "%s%s=%s", sep ? "," : "",
1413a3351425Sdougm 			    name, value != NULL ? value : "");
14146185db85Sdougm 		}
1415aed5d200Sdougm 		printed = B_TRUE;
14166185db85Sdougm 	}
1417aed5d200Sdougm skip:
14186185db85Sdougm 	if (name != NULL)
1419a3351425Sdougm 		sa_free_attr_string(name);
14206185db85Sdougm 	if (value != NULL)
1421a3351425Sdougm 		sa_free_attr_string(value);
1422aed5d200Sdougm 	return (printed);
14236185db85Sdougm }
14246185db85Sdougm 
14256185db85Sdougm /*
14266185db85Sdougm  * nfs_format_options(group, hier)
14276185db85Sdougm  *
14286185db85Sdougm  * format all the options on the group into an old-style option
14296185db85Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
14306185db85Sdougm  * options.
14316185db85Sdougm  */
14326185db85Sdougm 
14336185db85Sdougm static char *
14346185db85Sdougm nfs_format_options(sa_group_t group, int hier)
14356185db85Sdougm {
14366185db85Sdougm 	sa_optionset_t options = NULL;
1437a3351425Sdougm 	sa_optionset_t secoptions = NULL;
14386185db85Sdougm 	sa_property_t prop, secprop;
1439a3351425Sdougm 	sa_security_t security = NULL;
14406185db85Sdougm 	char *buff;
14416185db85Sdougm 	size_t buffsize;
1442a3351425Sdougm 	char *sectype = NULL;
1443a3351425Sdougm 	int sep = 0;
1444a3351425Sdougm 
1445a3351425Sdougm 
1446a3351425Sdougm 	buff = malloc(OPT_CHUNK);
1447a3351425Sdougm 	if (buff == NULL) {
1448a3351425Sdougm 		return (NULL);
1449a3351425Sdougm 	}
1450a3351425Sdougm 
1451a3351425Sdougm 	buff[0] = '\0';
1452a3351425Sdougm 	buffsize = OPT_CHUNK;
1453a3351425Sdougm 
1454a3351425Sdougm 	/*
1455a3351425Sdougm 	 * We may have a an optionset relative to this item. format
1456a3351425Sdougm 	 * these if we find them and then add any security definitions.
1457a3351425Sdougm 	 */
14586185db85Sdougm 
14596185db85Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
14606185db85Sdougm 
14616185db85Sdougm 	/*
1462a3351425Sdougm 	 * do the default set first but skip any option that is also
1463a3351425Sdougm 	 * in the protocol specific optionset.
14646185db85Sdougm 	 */
1465a3351425Sdougm 	if (options != NULL) {
1466a3351425Sdougm 		for (prop = sa_get_property(options, NULL);
1467a3351425Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
14686185db85Sdougm 			/*
1469a3351425Sdougm 			 * use this one since we skipped any
1470a3351425Sdougm 			 * of these that were also in
1471a3351425Sdougm 			 * optdefault
14726185db85Sdougm 			 */
1473aed5d200Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1474aed5d200Sdougm 			    prop, sep))
1475aed5d200Sdougm 				sep = 1;
1476a3351425Sdougm 			if (buff == NULL) {
1477a3351425Sdougm 				/*
1478a3351425Sdougm 				 * buff could become NULL if there
1479a3351425Sdougm 				 * isn't enough memory for
1480a3351425Sdougm 				 * nfs_sprint_option to realloc()
1481a3351425Sdougm 				 * as necessary. We can't really
1482a3351425Sdougm 				 * do anything about it at this
1483a3351425Sdougm 				 * point so we return NULL.  The
1484a3351425Sdougm 				 * caller should handle the
1485a3351425Sdougm 				 * failure.
1486a3351425Sdougm 				 */
14876185db85Sdougm 				if (options != NULL)
1488a3351425Sdougm 					sa_free_derived_optionset(
1489a3351425Sdougm 					    options);
14906185db85Sdougm 				return (buff);
14916185db85Sdougm 			}
1492a3351425Sdougm 		}
1493a3351425Sdougm 	}
1494a3351425Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1495a3351425Sdougm 	    "nfs", hier);
1496a3351425Sdougm 	if (secoptions != NULL) {
1497a3351425Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
1498a3351425Sdougm 		    secprop != NULL;
1499a3351425Sdougm 		    secprop = sa_get_next_property(secprop)) {
1500a3351425Sdougm 			sectype = sa_get_property_attr(secprop, "type");
1501a3351425Sdougm 			security =
1502a3351425Sdougm 			    (sa_security_t)sa_get_derived_security(
1503a3351425Sdougm 			    group, sectype, "nfs", hier);
1504a3351425Sdougm 			if (security != NULL) {
1505a3351425Sdougm 				if (sectype != NULL) {
1506a3351425Sdougm 					prop = sa_create_property(
1507a3351425Sdougm 					    "sec", sectype);
1508aed5d200Sdougm 					if (prop == NULL)
1509aed5d200Sdougm 						goto err;
1510aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1511aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1512aed5d200Sdougm 						sep = 1;
1513a3351425Sdougm 					(void) sa_remove_property(prop);
1514aed5d200Sdougm 					if (buff == NULL)
1515aed5d200Sdougm 						goto err;
1516a3351425Sdougm 				}
1517a3351425Sdougm 				for (prop = sa_get_property(security,
1518a3351425Sdougm 				    NULL); prop != NULL;
1519a3351425Sdougm 				    prop = sa_get_next_property(prop)) {
1520aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1521aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1522aed5d200Sdougm 						sep = 1;
1523a3351425Sdougm 					if (buff == NULL)
1524a3351425Sdougm 						goto err;
1525a3351425Sdougm 				}
1526a3351425Sdougm 				sa_free_derived_optionset(security);
1527a3351425Sdougm 			}
1528a3351425Sdougm 			if (sectype != NULL)
1529a3351425Sdougm 				sa_free_attr_string(sectype);
15306185db85Sdougm 		}
15316185db85Sdougm 		sa_free_derived_optionset(secoptions);
15326185db85Sdougm 	}
1533a3351425Sdougm 
15346185db85Sdougm 	if (options != NULL)
1535a3351425Sdougm 		sa_free_derived_optionset(options);
1536a3351425Sdougm 	return (buff);
1537a3351425Sdougm 
1538a3351425Sdougm err:
1539a3351425Sdougm 	/*
1540a3351425Sdougm 	 * If we couldn't allocate memory for option printing, we need
1541a3351425Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
1542a3351425Sdougm 	 */
1543a3351425Sdougm 	if (secoptions != NULL)
1544a3351425Sdougm 		sa_free_derived_optionset(secoptions);
1545a3351425Sdougm 	if (security != NULL)
1546a3351425Sdougm 		sa_free_derived_optionset(security);
1547a3351425Sdougm 	if (sectype != NULL)
1548a3351425Sdougm 		sa_free_attr_string(sectype);
1549a3351425Sdougm 	if (options != NULL)
1550a3351425Sdougm 		sa_free_derived_optionset(options);
1551dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	return (buff);
15526185db85Sdougm }
1553a3351425Sdougm 
15546185db85Sdougm /*
15556185db85Sdougm  * Append an entry to the nfslogtab file
15566185db85Sdougm  */
15576185db85Sdougm static int
15586185db85Sdougm nfslogtab_add(dir, buffer, tag)
15596185db85Sdougm 	char *dir, *buffer, *tag;
15606185db85Sdougm {
15616185db85Sdougm 	FILE *f;
15626185db85Sdougm 	struct logtab_ent lep;
15636185db85Sdougm 	int error = 0;
15646185db85Sdougm 
15656185db85Sdougm 	/*
15666185db85Sdougm 	 * Open the file for update and create it if necessary.
15676185db85Sdougm 	 * This may leave the I/O offset at the end of the file,
15686185db85Sdougm 	 * so rewind back to the beginning of the file.
15696185db85Sdougm 	 */
15706185db85Sdougm 	f = fopen(NFSLOGTAB, "a+");
15716185db85Sdougm 	if (f == NULL) {
15726185db85Sdougm 		error = errno;
15736185db85Sdougm 		goto out;
15746185db85Sdougm 	}
15756185db85Sdougm 	rewind(f);
15766185db85Sdougm 
15776185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1578549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1579a3351425Sdougm 		    "share complete, however failed to lock %s "
1580a3351425Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
15816185db85Sdougm 		error = -1;
15826185db85Sdougm 		goto out;
15836185db85Sdougm 	}
15846185db85Sdougm 
15856185db85Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
1586549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1587a3351425Sdougm 		    "share complete, however could not deactivate "
1588a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15896185db85Sdougm 		error = -1;
15906185db85Sdougm 		goto out;
15916185db85Sdougm 	}
15926185db85Sdougm 
15936185db85Sdougm 	/*
15946185db85Sdougm 	 * Remove entries matching buffer and sharepoint since we're
15956185db85Sdougm 	 * going to replace it with perhaps an entry with a new tag.
15966185db85Sdougm 	 */
15976185db85Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
1598549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1599a3351425Sdougm 		    "share complete, however could not remove matching "
1600a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
16016185db85Sdougm 		error = -1;
16026185db85Sdougm 		goto out;
16036185db85Sdougm 	}
16046185db85Sdougm 
16056185db85Sdougm 	/*
16066185db85Sdougm 	 * Deactivate all active entries matching this sharepoint
16076185db85Sdougm 	 */
16086185db85Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
1609549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1610a3351425Sdougm 		    "share complete, however could not deactivate matching "
1611a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
16126185db85Sdougm 		error = -1;
16136185db85Sdougm 		goto out;
16146185db85Sdougm 	}
16156185db85Sdougm 
16166185db85Sdougm 	lep.le_buffer = buffer;
16176185db85Sdougm 	lep.le_path = dir;
16186185db85Sdougm 	lep.le_tag = tag;
16196185db85Sdougm 	lep.le_state = LES_ACTIVE;
16206185db85Sdougm 
16216185db85Sdougm 	/*
16226185db85Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
16236185db85Sdougm 	 */
16246185db85Sdougm 	if (logtab_putent(f, &lep) < 0) {
1625549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1626a3351425Sdougm 		    "share complete, however could not add %s to %s\n"),
1627a3351425Sdougm 		    dir, NFSLOGTAB);
16286185db85Sdougm 		error = -1;
16296185db85Sdougm 	}
16306185db85Sdougm 
16316185db85Sdougm out:
16326185db85Sdougm 	if (f != NULL)
16336185db85Sdougm 		(void) fclose(f);
16346185db85Sdougm 	return (error);
16356185db85Sdougm }
16366185db85Sdougm 
16376185db85Sdougm /*
16386185db85Sdougm  * Deactivate an entry from the nfslogtab file
16396185db85Sdougm  */
16406185db85Sdougm static int
16416185db85Sdougm nfslogtab_deactivate(path)
16426185db85Sdougm 	char *path;
16436185db85Sdougm {
16446185db85Sdougm 	FILE *f;
16456185db85Sdougm 	int error = 0;
16466185db85Sdougm 
16476185db85Sdougm 	f = fopen(NFSLOGTAB, "r+");
16486185db85Sdougm 	if (f == NULL) {
16496185db85Sdougm 		error = errno;
16506185db85Sdougm 		goto out;
16516185db85Sdougm 	}
16526185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
16536185db85Sdougm 		error = errno;
1654549ec3ffSdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
1655a3351425Sdougm 		    "share complete, however could not lock %s for "
1656a3351425Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
16576185db85Sdougm 		goto out;
16586185db85Sdougm 	}
16596185db85Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
16606185db85Sdougm 		error = -1;
16616185db85Sdougm 		(void) fprintf(stderr,
1662a3351425Sdougm 		    dgettext(TEXT_DOMAIN,
1663a3351425Sdougm 		    "share complete, however could not "
1664a3351425Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
16656185db85Sdougm 		goto out;
16666185db85Sdougm 	}
16676185db85Sdougm 
16686185db85Sdougm out:	if (f != NULL)
16696185db85Sdougm 		(void) fclose(f);
16706185db85Sdougm 
16716185db85Sdougm 	return (error);
16726185db85Sdougm }
16736185db85Sdougm 
1674546405c3Sdougm /*
1675546405c3Sdougm  * check_public(group, skipshare)
1676546405c3Sdougm  *
1677546405c3Sdougm  * Check the group for any shares that have the public property
1678546405c3Sdougm  * enabled. We skip "skipshare" since that is the one we are
1679546405c3Sdougm  * working with. This is a separate function to make handling
1680546405c3Sdougm  * subgroups simpler. Returns true if there is a share with public.
1681546405c3Sdougm  */
1682546405c3Sdougm static int
1683546405c3Sdougm check_public(sa_group_t group, sa_share_t skipshare)
1684546405c3Sdougm {
1685546405c3Sdougm 	int exists = B_FALSE;
1686546405c3Sdougm 	sa_share_t share;
1687546405c3Sdougm 	sa_optionset_t opt;
1688546405c3Sdougm 	sa_property_t prop;
1689546405c3Sdougm 	char *shared;
1690546405c3Sdougm 
1691546405c3Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
1692546405c3Sdougm 	    share = sa_get_next_share(share)) {
1693546405c3Sdougm 		if (share == skipshare)
1694546405c3Sdougm 			continue;
1695546405c3Sdougm 
1696546405c3Sdougm 		opt = sa_get_optionset(share, "nfs");
1697546405c3Sdougm 		if (opt == NULL)
1698546405c3Sdougm 			continue;
1699546405c3Sdougm 		prop = sa_get_property(opt, "public");
1700546405c3Sdougm 		if (prop == NULL)
1701546405c3Sdougm 			continue;
1702546405c3Sdougm 		shared = sa_get_share_attr(share, "shared");
1703546405c3Sdougm 		if (shared != NULL) {
1704546405c3Sdougm 			exists = strcmp(shared, "true") == 0;
1705546405c3Sdougm 			sa_free_attr_string(shared);
1706546405c3Sdougm 			if (exists == B_TRUE)
1707546405c3Sdougm 				break;
1708546405c3Sdougm 		}
1709546405c3Sdougm 	}
1710546405c3Sdougm 
1711546405c3Sdougm 	return (exists);
1712546405c3Sdougm }
1713546405c3Sdougm 
17146185db85Sdougm /*
17155cb0d679SMarcel Telka  * public_exists(handle, skipshare)
17166185db85Sdougm  *
17176185db85Sdougm  * check to see if public option is set on any other share than the
1718546405c3Sdougm  * one specified. Need to check zfs sub-groups as well as the top
1719546405c3Sdougm  * level groups.
17206185db85Sdougm  */
17216185db85Sdougm static int
1722687915e9Sdougm public_exists(sa_handle_t handle, sa_share_t skipshare)
17236185db85Sdougm {
1724687915e9Sdougm 	sa_group_t group = NULL;
1725549ec3ffSdougm 
17263b61b335Sdougm 	/*
17273b61b335Sdougm 	 * If we don't have a handle, we can only do syntax check. We
17283b61b335Sdougm 	 * can't check against other shares so we assume OK and will
17293b61b335Sdougm 	 * catch the problem only when we actually try to apply it.
17303b61b335Sdougm 	 */
1731549ec3ffSdougm 	if (handle == NULL)
17323b61b335Sdougm 		return (SA_OK);
1733549ec3ffSdougm 
1734687915e9Sdougm 	if (skipshare != NULL) {
1735687915e9Sdougm 		group = sa_get_parent_group(skipshare);
1736687915e9Sdougm 		if (group == NULL)
1737687915e9Sdougm 			return (SA_NO_SUCH_GROUP);
1738687915e9Sdougm 	}
1739687915e9Sdougm 
1740549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
17416185db85Sdougm 	    group = sa_get_next_group(group)) {
1742546405c3Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
1743546405c3Sdougm 		if (sa_group_is_zfs(group)) {
1744546405c3Sdougm 			sa_group_t subgroup;
1745546405c3Sdougm 			for (subgroup = sa_get_sub_group(group);
1746546405c3Sdougm 			    subgroup != NULL;
1747546405c3Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
1748546405c3Sdougm 				if (check_public(subgroup, skipshare))
1749546405c3Sdougm 					return (B_TRUE);
17506185db85Sdougm 			}
1751546405c3Sdougm 		} else {
1752546405c3Sdougm 			if (check_public(group, skipshare))
1753546405c3Sdougm 				return (B_TRUE);
17546185db85Sdougm 		}
17556185db85Sdougm 	}
1756546405c3Sdougm 	return (B_FALSE);
17576185db85Sdougm }
17586185db85Sdougm 
17596185db85Sdougm /*
17606185db85Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
17616185db85Sdougm  * implementation that it is to enable the share. This entails
17626185db85Sdougm  * converting the path and options into the appropriate ioctl
17636185db85Sdougm  * calls. It is assumed that all error checking of paths, etc. were
17646185db85Sdougm  * done earlier.
17656185db85Sdougm  */
17666185db85Sdougm static int
17676185db85Sdougm nfs_enable_share(sa_share_t share)
17686185db85Sdougm {
17696185db85Sdougm 	struct exportdata export;
17706185db85Sdougm 	sa_optionset_t secoptlist;
17716185db85Sdougm 	struct secinfo *sp;
17726185db85Sdougm 	int num_secinfo;
17736185db85Sdougm 	sa_optionset_t opt;
17746185db85Sdougm 	sa_security_t sec;
17756185db85Sdougm 	sa_property_t prop;
17766185db85Sdougm 	char *path;
17776185db85Sdougm 	int err = SA_OK;
1778546405c3Sdougm 	int i;
1779ecd6cf80Smarks 	int iszfs;
1780687915e9Sdougm 	sa_handle_t handle;
17816185db85Sdougm 
17826185db85Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
17836185db85Sdougm 	(void) signal(SIGSYS, SIG_IGN);
17846185db85Sdougm 
17856185db85Sdougm 	/* get the path since it is important in several places */
17866185db85Sdougm 	path = sa_get_share_attr(share, "path");
17876185db85Sdougm 	if (path == NULL)
1788a3351425Sdougm 		return (SA_NO_SUCH_PATH);
17896185db85Sdougm 
1790ecd6cf80Smarks 	iszfs = sa_path_is_zfs(path);
17916185db85Sdougm 	/*
17926185db85Sdougm 	 * find the optionsets and security sets.  There may not be
17936185db85Sdougm 	 * any or there could be one or two for each of optionset and
17946185db85Sdougm 	 * security may have multiple, one per security type per
17956185db85Sdougm 	 * protocol type.
17966185db85Sdougm 	 */
17976185db85Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
17986185db85Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
17996185db85Sdougm 	if (secoptlist != NULL)
1800a3351425Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
18016185db85Sdougm 	else
1802a3351425Sdougm 		num_secinfo = 1;
18036185db85Sdougm 
18046185db85Sdougm 	/*
18056185db85Sdougm 	 * walk through the options and fill in the structure
18066185db85Sdougm 	 * appropriately.
18076185db85Sdougm 	 */
18086185db85Sdougm 
18096185db85Sdougm 	(void) memset(&export, '\0', sizeof (export));
18106185db85Sdougm 
18116185db85Sdougm 	/*
18126185db85Sdougm 	 * do non-security options first since there is only one after
18136185db85Sdougm 	 * the derived group is constructed.
18146185db85Sdougm 	 */
18156185db85Sdougm 	export.ex_version = EX_CURRENT_VERSION;
18166185db85Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
18176185db85Sdougm 	export.ex_index = NULL;
18186185db85Sdougm 	export.ex_path = path;
18196185db85Sdougm 	export.ex_pathlen = strlen(path) + 1;
18206185db85Sdougm 
18216185db85Sdougm 	if (opt != NULL)
1822a3351425Sdougm 		err = fill_export_from_optionset(&export, opt);
18236185db85Sdougm 
18246185db85Sdougm 	/*
18256185db85Sdougm 	 * check to see if "public" is set. If it is, then make sure
18266185db85Sdougm 	 * no other share has it set. If it is already used, fail.
18276185db85Sdougm 	 */
18286185db85Sdougm 
1829687915e9Sdougm 	handle = sa_find_group_handle((sa_group_t)share);
1830687915e9Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
1831a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1832a3351425Sdougm 		    "NFS: Cannot share more than one file "
1833a3351425Sdougm 		    "system with 'public' property\n"));
1834a3351425Sdougm 		err = SA_NOT_ALLOWED;
1835a3351425Sdougm 		goto out;
18366185db85Sdougm 	}
18376185db85Sdougm 
1838546405c3Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
18396185db85Sdougm 	if (sp == NULL) {
1840a3351425Sdougm 		err = SA_NO_MEMORY;
1841546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1842546405c3Sdougm 		    "NFS: NFS: no memory for security\n"));
1843546405c3Sdougm 		goto out;
1844546405c3Sdougm 	}
1845546405c3Sdougm 	export.ex_secinfo = sp;
1846546405c3Sdougm 	/* get default secinfo */
1847546405c3Sdougm 	export.ex_seccnt = num_secinfo;
1848546405c3Sdougm 	/*
1849546405c3Sdougm 	 * since we must have one security option defined, we
1850546405c3Sdougm 	 * init to the default and then override as we find
1851546405c3Sdougm 	 * defined security options. This handles the case
1852546405c3Sdougm 	 * where we have no defined options but we need to set
1853546405c3Sdougm 	 * up one.
1854546405c3Sdougm 	 */
1855546405c3Sdougm 	sp[0].s_window = DEF_WIN;
1856546405c3Sdougm 	sp[0].s_rootnames = NULL;
1857546405c3Sdougm 	/* setup a default in case no properties defined */
1858546405c3Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1859546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1860546405c3Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
1861546405c3Sdougm 		    "get default security mode\n"));
1862546405c3Sdougm 		err = SA_CONFIG_ERR;
1863546405c3Sdougm 	}
1864546405c3Sdougm 	if (secoptlist != NULL) {
1865546405c3Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
1866546405c3Sdougm 		    prop != NULL && i < num_secinfo;
1867546405c3Sdougm 		    prop = sa_get_next_property(prop), i++) {
1868546405c3Sdougm 			char *sectype;
1869fe1c642dSBill Krier 			sectype = sa_get_property_attr(prop, "type");
1870546405c3Sdougm 			/*
1871546405c3Sdougm 			 * if sectype is NULL, we probably
1872546405c3Sdougm 			 * have a memory problem and can't get
1873546405c3Sdougm 			 * the correct values. Rather than
1874546405c3Sdougm 			 * exporting with incorrect security,
1875546405c3Sdougm 			 * don't share it.
1876546405c3Sdougm 			 */
1877546405c3Sdougm 			if (sectype == NULL) {
1878546405c3Sdougm 				err = SA_NO_MEMORY;
1879546405c3Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1880546405c3Sdougm 				    "NFS: Cannot share %s: "
1881546405c3Sdougm 				    "no memory\n"), path);
1882546405c3Sdougm 				goto out;
1883a3351425Sdougm 			}
1884546405c3Sdougm 			sec = (sa_security_t)sa_get_derived_security(
1885546405c3Sdougm 			    share, sectype, "nfs", 1);
1886546405c3Sdougm 			sp[i].s_window = DEF_WIN;
1887546405c3Sdougm 			sp[i].s_rootcnt = 0;
1888546405c3Sdougm 			sp[i].s_rootnames = NULL;
18895cb0d679SMarcel Telka 			(void) fill_security_from_secopts(&sp[i], sec);
1890546405c3Sdougm 			if (sec != NULL)
1891546405c3Sdougm 				sa_free_derived_security(sec);
1892546405c3Sdougm 			if (sectype != NULL)
1893546405c3Sdougm 				sa_free_attr_string(sectype);
18946185db85Sdougm 		}
1895546405c3Sdougm 	}
1896546405c3Sdougm 	/*
1897546405c3Sdougm 	 * when we get here, we can do the exportfs system call and
18984b92079bSAlexander Eremin 	 * initiate things. We probably want to enable the
18994b92079bSAlexander Eremin 	 * svc:/network/nfs/server service first if it isn't running.
1900546405c3Sdougm 	 */
19014b92079bSAlexander Eremin 	/* check svc:/network/nfs/server status and start if needed */
1902546405c3Sdougm 	/* now add the share to the internal tables */
1903546405c3Sdougm 	printarg(path, &export);
1904546405c3Sdougm 	/*
1905546405c3Sdougm 	 * call the exportfs system call which is implemented
1906546405c3Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
1907546405c3Sdougm 	 */
1908ecd6cf80Smarks 	if (iszfs) {
1909ecd6cf80Smarks 		struct exportfs_args ea;
1910ecd6cf80Smarks 		share_t sh;
1911ecd6cf80Smarks 		char *str;
1912ecd6cf80Smarks 		priv_set_t *priv_effective;
1913ecd6cf80Smarks 		int privileged;
1914ecd6cf80Smarks 
1915ecd6cf80Smarks 		/*
1916ecd6cf80Smarks 		 * If we aren't a privileged user
1917ecd6cf80Smarks 		 * and NFS server service isn't running
1918ecd6cf80Smarks 		 * then print out an error message
1919ecd6cf80Smarks 		 * and return EPERM
1920ecd6cf80Smarks 		 */
1921ecd6cf80Smarks 
1922ecd6cf80Smarks 		priv_effective = priv_allocset();
1923ecd6cf80Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
1924ecd6cf80Smarks 
1925ecd6cf80Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
1926ecd6cf80Smarks 		priv_freeset(priv_effective);
1927ecd6cf80Smarks 
1928ecd6cf80Smarks 		if (!privileged &&
1929ecd6cf80Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
1930ecd6cf80Smarks 			err = 0;
1931ecd6cf80Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
1932ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1933ecd6cf80Smarks 				    "NFS: Cannot share remote "
1934ecd6cf80Smarks 				    "filesystem: %s\n"), path);
1935ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1936ecd6cf80Smarks 				    "NFS: Service needs to be enabled "
1937ecd6cf80Smarks 				    "by a privileged user\n"));
1938ecd6cf80Smarks 				err = SA_SYSTEM_ERR;
1939ecd6cf80Smarks 				errno = EPERM;
1940ecd6cf80Smarks 			}
1941ecd6cf80Smarks 			free(str);
1942ecd6cf80Smarks 		}
1943ecd6cf80Smarks 
1944ecd6cf80Smarks 		if (err == 0) {
1945ecd6cf80Smarks 			ea.dname = path;
1946ecd6cf80Smarks 			ea.uex = &export;
1947ecd6cf80Smarks 
19485ad42b1bSSurya Prakki 			(void) sa_sharetab_fill_zfs(share, &sh, "nfs");
1949743a77edSAlan Wright 			err = sa_share_zfs(share, NULL, path, &sh,
1950da6c28aaSamw 			    &ea, ZFS_SHARE_NFS);
1951a63214d6SBill Krier 			if (err != SA_OK) {
1952a63214d6SBill Krier 				errno = err;
1953a63214d6SBill Krier 				err = -1;
1954a63214d6SBill Krier 			}
1955ecd6cf80Smarks 			sa_emptyshare(&sh);
1956ecd6cf80Smarks 		}
1957ecd6cf80Smarks 	} else {
1958ecd6cf80Smarks 		err = exportfs(path, &export);
1959ecd6cf80Smarks 	}
1960ecd6cf80Smarks 
1961ecd6cf80Smarks 	if (err < 0) {
1962546405c3Sdougm 		err = SA_SYSTEM_ERR;
1963546405c3Sdougm 		switch (errno) {
1964546405c3Sdougm 		case EREMOTE:
1965546405c3Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1966ecd6cf80Smarks 			    "NFS: Cannot share filesystems "
1967ecd6cf80Smarks 			    "in non-global zones: %s\n"), path);
1968ecd6cf80Smarks 			err = SA_NOT_SUPPORTED;
1969546405c3Sdougm 			break;
1970546405c3Sdougm 		case EPERM:
1971546405c3Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
1972a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1973ecd6cf80Smarks 				    "NFS: Cannot share file systems "
1974546405c3Sdougm 				    "in non-global zones: %s\n"), path);
1975546405c3Sdougm 				err = SA_NOT_SUPPORTED;
1976a3351425Sdougm 				break;
1977a3351425Sdougm 			}
1978546405c3Sdougm 			err = SA_NO_PERMISSION;
19790616fd7fSPavel Filipensky 			break;
19800616fd7fSPavel Filipensky 		case EEXIST:
19810616fd7fSPavel Filipensky 			err = SA_SHARE_EXISTS;
19820616fd7fSPavel Filipensky 			break;
1983546405c3Sdougm 		default:
1984546405c3Sdougm 			break;
19856185db85Sdougm 		}
1986546405c3Sdougm 	} else {
1987546405c3Sdougm 		/* update sharetab with an add/modify */
1988ecd6cf80Smarks 		if (!iszfs) {
1989ecd6cf80Smarks 			(void) sa_update_sharetab(share, "nfs");
1990ecd6cf80Smarks 		}
19916185db85Sdougm 	}
19926185db85Sdougm 
19936185db85Sdougm 	if (err == SA_OK) {
19946185db85Sdougm 		/*
19956185db85Sdougm 		 * enable services as needed. This should probably be
19966185db85Sdougm 		 * done elsewhere in order to minimize the calls to
19976185db85Sdougm 		 * check services.
19986185db85Sdougm 		 */
19996185db85Sdougm 		/*
20006185db85Sdougm 		 * check to see if logging and other services need to
20016185db85Sdougm 		 * be triggered, but only if there wasn't an
20026185db85Sdougm 		 * error. This is probably where sharetab should be
20036185db85Sdougm 		 * updated with the NFS specific entry.
20046185db85Sdougm 		 */
2005a3351425Sdougm 		if (export.ex_flags & EX_LOG) {
2006a3351425Sdougm 			/* enable logging */
2007a3351425Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
2008a3351425Sdougm 			    export.ex_tag) != 0) {
2009a3351425Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2010a3351425Sdougm 				    "Could not enable logging for %s\n"),
2011a3351425Sdougm 				    path);
2012a3351425Sdougm 			}
2013a3351425Sdougm 			_check_services(service_list_logging);
2014a3351425Sdougm 		} else {
2015a3351425Sdougm 			/*
2016a3351425Sdougm 			 * don't have logging so remove it from file. It might
2017a3351425Sdougm 			 * not be thre, but that doesn't matter.
2018a3351425Sdougm 			 */
2019a3351425Sdougm 			(void) nfslogtab_deactivate(path);
2020a3351425Sdougm 			_check_services(service_list_default);
20216185db85Sdougm 		}
20226185db85Sdougm 	}
20236185db85Sdougm 
20246185db85Sdougm out:
20256185db85Sdougm 	if (path != NULL)
2026a3351425Sdougm 		free(path);
20276185db85Sdougm 
20286185db85Sdougm 	cleanup_export(&export);
20296185db85Sdougm 	if (opt != NULL)
2030a3351425Sdougm 		sa_free_derived_optionset(opt);
20316185db85Sdougm 	if (secoptlist != NULL)
2032a3351425Sdougm 		(void) sa_destroy_optionset(secoptlist);
20336185db85Sdougm 	return (err);
20346185db85Sdougm }
20356185db85Sdougm 
20366185db85Sdougm /*
20378e314a44Sdougm  * nfs_disable_share(share, path)
20386185db85Sdougm  *
20398e314a44Sdougm  * Unshare the specified share. Note that "path" is the same path as
20408e314a44Sdougm  * what is in the "share" object. It is passed in to avoid an
20418e314a44Sdougm  * additional lookup. A missing "path" value makes this a no-op
20428e314a44Sdougm  * function.
20436185db85Sdougm  */
20446185db85Sdougm static int
2045ecd6cf80Smarks nfs_disable_share(sa_share_t share, char *path)
20466185db85Sdougm {
20476185db85Sdougm 	int err;
20486185db85Sdougm 	int ret = SA_OK;
2049ecd6cf80Smarks 	int iszfs;
20508e314a44Sdougm 	sa_group_t parent;
20515b6e0c46Sdougm 	sa_handle_t handle;
2052ecd6cf80Smarks 
20538e314a44Sdougm 	if (path == NULL)
20548e314a44Sdougm 		return (ret);
2055ecd6cf80Smarks 
20568e314a44Sdougm 	/*
20578e314a44Sdougm 	 * If the share is in a ZFS group we need to handle it
20588e314a44Sdougm 	 * differently.  Just being on a ZFS file system isn't
20598e314a44Sdougm 	 * enough since we may be in a legacy share case.
20608e314a44Sdougm 	 */
20618e314a44Sdougm 	parent = sa_get_parent_group(share);
20628e314a44Sdougm 	iszfs = sa_group_is_zfs(parent);
20638e314a44Sdougm 	if (iszfs) {
20648e314a44Sdougm 		struct exportfs_args ea;
20658e314a44Sdougm 		share_t sh = { 0 };
20668e314a44Sdougm 		ea.dname = path;
20678e314a44Sdougm 		ea.uex = NULL;
20688e314a44Sdougm 		sh.sh_path = path;
20698e314a44Sdougm 		sh.sh_fstype = "nfs";
20708e314a44Sdougm 
2071743a77edSAlan Wright 		err = sa_share_zfs(share, NULL, path, &sh,
20728e314a44Sdougm 		    &ea, ZFS_UNSHARE_NFS);
2073a63214d6SBill Krier 		if (err != SA_OK) {
2074a63214d6SBill Krier 			errno = err;
2075a63214d6SBill Krier 			err = -1;
2076a63214d6SBill Krier 		}
20778e314a44Sdougm 	} else {
20788e314a44Sdougm 		err = exportfs(path, NULL);
20798e314a44Sdougm 	}
20808e314a44Sdougm 	if (err < 0) {
20818e314a44Sdougm 		/*
20828e314a44Sdougm 		 * TBD: only an error in some
20838e314a44Sdougm 		 * cases - need better analysis
20848e314a44Sdougm 		 */
20858e314a44Sdougm 		switch (errno) {
20868e314a44Sdougm 		case EPERM:
20878e314a44Sdougm 		case EACCES:
20888e314a44Sdougm 			ret = SA_NO_PERMISSION;
20898e314a44Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
20908e314a44Sdougm 				ret = SA_NOT_SUPPORTED;
20918e314a44Sdougm 			}
20928e314a44Sdougm 			break;
20938e314a44Sdougm 		case EINVAL:
20948e314a44Sdougm 		case ENOENT:
20958e314a44Sdougm 			ret = SA_NO_SUCH_PATH;
2096ecd6cf80Smarks 			break;
2097a63214d6SBill Krier 		default:
2098a63214d6SBill Krier 			ret = SA_SYSTEM_ERR;
2099ecd6cf80Smarks 			break;
21006185db85Sdougm 		}
21016185db85Sdougm 	}
21028e314a44Sdougm 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
21035b6e0c46Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
21048e314a44Sdougm 		if (!iszfs)
21055b6e0c46Sdougm 			(void) sa_delete_sharetab(handle, path, "nfs");
21068e314a44Sdougm 		/* just in case it was logged */
21078e314a44Sdougm 		(void) nfslogtab_deactivate(path);
21088e314a44Sdougm 	}
21096185db85Sdougm 	return (ret);
21106185db85Sdougm }
21116185db85Sdougm 
21125cb0d679SMarcel Telka static int
21135cb0d679SMarcel Telka check_user(char *value)
21145cb0d679SMarcel Telka {
21155cb0d679SMarcel Telka 	int ret = SA_OK;
21165cb0d679SMarcel Telka 
21175cb0d679SMarcel Telka 	if (!is_a_number(value)) {
21185cb0d679SMarcel Telka 		struct passwd *pw;
21195cb0d679SMarcel Telka 		/*
21205cb0d679SMarcel Telka 		 * in this case it would have to be a
21215cb0d679SMarcel Telka 		 * user name
21225cb0d679SMarcel Telka 		 */
21235cb0d679SMarcel Telka 		pw = getpwnam(value);
21245cb0d679SMarcel Telka 		if (pw == NULL)
21255cb0d679SMarcel Telka 			ret = SA_BAD_VALUE;
21265cb0d679SMarcel Telka 		endpwent();
21275cb0d679SMarcel Telka 	} else {
21285cb0d679SMarcel Telka 		uint64_t intval;
21295cb0d679SMarcel Telka 		intval = strtoull(value, NULL, 0);
21305cb0d679SMarcel Telka 		if (intval > UID_MAX && intval != -1)
21315cb0d679SMarcel Telka 			ret = SA_BAD_VALUE;
21325cb0d679SMarcel Telka 	}
21335cb0d679SMarcel Telka 
21345cb0d679SMarcel Telka 	return (ret);
21355cb0d679SMarcel Telka }
21365cb0d679SMarcel Telka 
21375cb0d679SMarcel Telka static int
21385cb0d679SMarcel Telka check_group(char *value)
21395cb0d679SMarcel Telka {
21405cb0d679SMarcel Telka 	int ret = SA_OK;
21415cb0d679SMarcel Telka 
21425cb0d679SMarcel Telka 	if (!is_a_number(value)) {
21435cb0d679SMarcel Telka 		struct group *gr;
21445cb0d679SMarcel Telka 		/*
21455cb0d679SMarcel Telka 		 * in this case it would have to be a
21465cb0d679SMarcel Telka 		 * group name
21475cb0d679SMarcel Telka 		 */
21485cb0d679SMarcel Telka 		gr = getgrnam(value);
21495cb0d679SMarcel Telka 		if (gr == NULL)
21505cb0d679SMarcel Telka 			ret = SA_BAD_VALUE;
21515cb0d679SMarcel Telka 		endgrent();
21525cb0d679SMarcel Telka 	} else {
21535cb0d679SMarcel Telka 		uint64_t intval;
21545cb0d679SMarcel Telka 		intval = strtoull(value, NULL, 0);
21555cb0d679SMarcel Telka 		if (intval > UID_MAX && intval != -1)
21565cb0d679SMarcel Telka 			ret = SA_BAD_VALUE;
21575cb0d679SMarcel Telka 	}
21585cb0d679SMarcel Telka 
21595cb0d679SMarcel Telka 	return (ret);
21605cb0d679SMarcel Telka }
21615cb0d679SMarcel Telka 
21626185db85Sdougm /*
2163b89a8333Snatalie li - Sun Microsystems - Irvine United States  * check_rorwnone(v1, v2, v3)
2164b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
2165b89a8333Snatalie li - Sun Microsystems - Irvine United States  * check ro vs rw vs none values.  Over time this may get beefed up.
2166b89a8333Snatalie li - Sun Microsystems - Irvine United States  * for now it just does simple checks. v1 is never NULL but v2 or v3
2167b89a8333Snatalie li - Sun Microsystems - Irvine United States  * could be.
21686185db85Sdougm  */
21696185db85Sdougm 
21706185db85Sdougm static int
2171b89a8333Snatalie li - Sun Microsystems - Irvine United States check_rorwnone(char *v1, char *v2, char *v3)
21726185db85Sdougm {
21736185db85Sdougm 	int ret = SA_OK;
2174b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (v2 != NULL && strcmp(v1, v2) == 0)
2175b89a8333Snatalie li - Sun Microsystems - Irvine United States 		ret = SA_VALUE_CONFLICT;
2176b89a8333Snatalie li - Sun Microsystems - Irvine United States 	else if (v3 != NULL && strcmp(v1, v3) == 0)
2177a3351425Sdougm 		ret = SA_VALUE_CONFLICT;
2178b89a8333Snatalie li - Sun Microsystems - Irvine United States 
21796185db85Sdougm 	return (ret);
21806185db85Sdougm }
21816185db85Sdougm 
21826185db85Sdougm /*
2183687915e9Sdougm  * nfs_validate_property(handle, property, parent)
21846185db85Sdougm  *
21856185db85Sdougm  * Check that the property has a legitimate value for its type.
21866185db85Sdougm  */
21876185db85Sdougm 
21886185db85Sdougm static int
2189687915e9Sdougm nfs_validate_property(sa_handle_t handle, sa_property_t property,
2190687915e9Sdougm     sa_optionset_t parent)
21916185db85Sdougm {
21926185db85Sdougm 	int ret = SA_OK;
21936185db85Sdougm 	char *propname;
2194b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *other1;
2195b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *other2;
21966185db85Sdougm 	int optindex;
21976185db85Sdougm 	nfsl_config_t *configlist;
21986185db85Sdougm 	sa_group_t parent_group;
21996185db85Sdougm 	char *value;
22006185db85Sdougm 
22016185db85Sdougm 	propname = sa_get_property_attr(property, "type");
22026185db85Sdougm 
22036185db85Sdougm 	if ((optindex = findopt(propname)) < 0)
2204a3351425Sdougm 		ret = SA_NO_SUCH_PROP;
22056185db85Sdougm 
22066185db85Sdougm 	/* need to validate value range here as well */
22076185db85Sdougm 
22086185db85Sdougm 	if (ret == SA_OK) {
2209a3351425Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
2210687915e9Sdougm 		if (optdefs[optindex].share && parent_group != NULL &&
2211687915e9Sdougm 		    !sa_is_share(parent_group))
2212a3351425Sdougm 			ret = SA_PROP_SHARE_ONLY;
22136185db85Sdougm 	}
22146185db85Sdougm 	if (ret == SA_OK) {
2215687915e9Sdougm 		if (optdefs[optindex].index == OPT_PUBLIC) {
2216687915e9Sdougm 			/*
2217687915e9Sdougm 			 * Public is special in that only one instance can
2218687915e9Sdougm 			 * be in the repository at the same time.
2219687915e9Sdougm 			 */
2220687915e9Sdougm 			if (public_exists(handle, parent_group)) {
2221b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_free_attr_string(propname);
2222687915e9Sdougm 				return (SA_VALUE_CONFLICT);
2223687915e9Sdougm 			}
2224687915e9Sdougm 		}
2225a3351425Sdougm 		value = sa_get_property_attr(property, "value");
2226a3351425Sdougm 		if (value != NULL) {
2227a3351425Sdougm 			/* first basic type checking */
2228a3351425Sdougm 			switch (optdefs[optindex].type) {
22295cb0d679SMarcel Telka 
2230a3351425Sdougm 			case OPT_TYPE_NUMBER:
2231a3351425Sdougm 				/* check that the value is all digits */
2232a3351425Sdougm 				if (!is_a_number(value))
2233a3351425Sdougm 					ret = SA_BAD_VALUE;
2234a3351425Sdougm 				break;
22355cb0d679SMarcel Telka 
2236a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2237a3351425Sdougm 				if (strlen(value) == 0 ||
2238a3351425Sdougm 				    strcasecmp(value, "true") == 0 ||
2239a3351425Sdougm 				    strcmp(value, "1") == 0 ||
2240a3351425Sdougm 				    strcasecmp(value, "false") == 0 ||
2241a3351425Sdougm 				    strcmp(value, "0") == 0) {
2242a3351425Sdougm 					ret = SA_OK;
2243a3351425Sdougm 				} else {
2244a3351425Sdougm 					ret = SA_BAD_VALUE;
2245a3351425Sdougm 				}
2246a3351425Sdougm 				break;
22475cb0d679SMarcel Telka 
2248a3351425Sdougm 			case OPT_TYPE_USER:
22495cb0d679SMarcel Telka 				ret = check_user(value);
2250a3351425Sdougm 				break;
22515cb0d679SMarcel Telka 
2252a3351425Sdougm 			case OPT_TYPE_FILE:
2253a3351425Sdougm 				if (strcmp(value, "..") == 0 ||
2254a3351425Sdougm 				    strchr(value, '/') != NULL) {
2255a3351425Sdougm 					ret = SA_BAD_VALUE;
2256a3351425Sdougm 				}
2257a3351425Sdougm 				break;
22585cb0d679SMarcel Telka 
2259b89a8333Snatalie li - Sun Microsystems - Irvine United States 			case OPT_TYPE_ACCLIST: {
2260b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_property_t oprop1;
2261b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_property_t oprop2;
2262b89a8333Snatalie li - Sun Microsystems - Irvine United States 				char *ovalue1 = NULL;
2263b89a8333Snatalie li - Sun Microsystems - Irvine United States 				char *ovalue2 = NULL;
2264b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2265b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (parent == NULL)
2266b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2267a3351425Sdougm 				/*
2268a3351425Sdougm 				 * access list handling. Should eventually
2269a3351425Sdougm 				 * validate that all the values make sense.
2270a3351425Sdougm 				 * Also, ro and rw may have cross value
2271a3351425Sdougm 				 * conflicts.
2272a3351425Sdougm 				 */
2273b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (strcmp(propname, SHOPT_RO) == 0) {
2274b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RW;
2275b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_NONE;
2276b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else if (strcmp(propname, SHOPT_RW) == 0) {
2277b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RO;
2278b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_NONE;
2279b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else if (strcmp(propname, SHOPT_NONE) == 0) {
2280b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RO;
2281b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_RW;
2282b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else {
2283b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = NULL;
2284b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = NULL;
2285a3351425Sdougm 				}
2286b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (other1 == NULL && other2 == NULL)
2287b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2288b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2289b89a8333Snatalie li - Sun Microsystems - Irvine United States 				/* compare rw(ro) with ro(rw) */
2290b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2291b89a8333Snatalie li - Sun Microsystems - Irvine United States 				oprop1 = sa_get_property(parent, other1);
2292b89a8333Snatalie li - Sun Microsystems - Irvine United States 				oprop2 = sa_get_property(parent, other2);
2293b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (oprop1 == NULL && oprop2 == NULL)
2294b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2295b89a8333Snatalie li - Sun Microsystems - Irvine United States 				/*
2296b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * Only potential confusion if other1
2297b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * or other2 exists. Check the values
2298b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * and run the check if there is a
2299b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * value other than the one we are
2300b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * explicitly looking at.
2301b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 */
2302b89a8333Snatalie li - Sun Microsystems - Irvine United States 				ovalue1 = sa_get_property_attr(oprop1, "value");
2303b89a8333Snatalie li - Sun Microsystems - Irvine United States 				ovalue2 = sa_get_property_attr(oprop2, "value");
2304b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue1 != NULL || ovalue2 != NULL)
2305b89a8333Snatalie li - Sun Microsystems - Irvine United States 					ret = check_rorwnone(value, ovalue1,
2306b89a8333Snatalie li - Sun Microsystems - Irvine United States 					    ovalue2);
2307b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2308b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue1 != NULL)
2309b89a8333Snatalie li - Sun Microsystems - Irvine United States 					sa_free_attr_string(ovalue1);
2310b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue2 != NULL)
2311b89a8333Snatalie li - Sun Microsystems - Irvine United States 					sa_free_attr_string(ovalue2);
2312a3351425Sdougm 				break;
2313b89a8333Snatalie li - Sun Microsystems - Irvine United States 			}
23145cb0d679SMarcel Telka 
2315a3351425Sdougm 			case OPT_TYPE_LOGTAG:
2316a3351425Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
2317a3351425Sdougm 					int error;
2318a3351425Sdougm 					if (value == NULL ||
2319a3351425Sdougm 					    strlen(value) == 0) {
2320a3351425Sdougm 						if (value != NULL)
2321a3351425Sdougm 							sa_free_attr_string(
2322a3351425Sdougm 							    value);
2323a3351425Sdougm 						value = strdup("global");
2324a3351425Sdougm 					}
2325a3351425Sdougm 					if (value != NULL &&
2326a3351425Sdougm 					    nfsl_findconfig(configlist, value,
2327a3351425Sdougm 					    &error) == NULL) {
2328a3351425Sdougm 						ret = SA_BAD_VALUE;
2329a3351425Sdougm 					}
2330aed5d200Sdougm 					/* Must always free when done */
2331aed5d200Sdougm 					nfsl_freeconfig_list(&configlist);
2332a3351425Sdougm 				} else {
2333a3351425Sdougm 					ret = SA_CONFIG_ERR;
2334a3351425Sdougm 				}
2335a3351425Sdougm 				break;
23365cb0d679SMarcel Telka 
2337a3351425Sdougm 			case OPT_TYPE_STRING:
2338a3351425Sdougm 				/* whatever is here should be ok */
2339a3351425Sdougm 				break;
23405cb0d679SMarcel Telka 
2341a3351425Sdougm 			case OPT_TYPE_SECURITY:
2342a3351425Sdougm 				/*
2343a3351425Sdougm 				 * The "sec" property isn't used in the
2344a3351425Sdougm 				 * non-legacy parts of sharemgr. We need to
2345a3351425Sdougm 				 * reject it here. For legacy, it is pulled
2346a3351425Sdougm 				 * out well before we get here.
2347a3351425Sdougm 				 */
2348a3351425Sdougm 				ret = SA_NO_SUCH_PROP;
2349a3351425Sdougm 				break;
23505cb0d679SMarcel Telka 
23515cb0d679SMarcel Telka 			case OPT_TYPE_MAPPING: {
23525cb0d679SMarcel Telka 				char *p;
23535cb0d679SMarcel Telka 				char *n;
23545cb0d679SMarcel Telka 				char *c;
23555cb0d679SMarcel Telka 				int (*f)(char *);
23565cb0d679SMarcel Telka 
23575cb0d679SMarcel Telka 				sa_security_t security;
23585cb0d679SMarcel Telka 
23595cb0d679SMarcel Telka 				/*
23605cb0d679SMarcel Telka 				 * mapping is only supported for sec=sys
23615cb0d679SMarcel Telka 				 */
23625cb0d679SMarcel Telka 				ret = SA_CONFIG_ERR;
23635cb0d679SMarcel Telka 				if (parent_group == NULL)
23645cb0d679SMarcel Telka 					break;
23655cb0d679SMarcel Telka 
23665cb0d679SMarcel Telka 				for (security = sa_get_security(parent_group,
23675cb0d679SMarcel Telka 				    NULL, NULL); security != NULL;
23685cb0d679SMarcel Telka 				    security = sa_get_next_security(security)) {
23695cb0d679SMarcel Telka 					char *type;
23705cb0d679SMarcel Telka 					char *sectype;
23715cb0d679SMarcel Telka 
23725cb0d679SMarcel Telka 					type = sa_get_security_attr(security,
23735cb0d679SMarcel Telka 					    "type");
23745cb0d679SMarcel Telka 					if (type == NULL)
23755cb0d679SMarcel Telka 						continue;
23765cb0d679SMarcel Telka 
23775cb0d679SMarcel Telka 					if (strcmp(type, "nfs") != 0) {
23785cb0d679SMarcel Telka 						sa_free_attr_string(type);
23795cb0d679SMarcel Telka 						continue;
23805cb0d679SMarcel Telka 					}
23815cb0d679SMarcel Telka 					sa_free_attr_string(type);
23825cb0d679SMarcel Telka 
23835cb0d679SMarcel Telka 					sectype = sa_get_security_attr(security,
23845cb0d679SMarcel Telka 					    "sectype");
23855cb0d679SMarcel Telka 					if (sectype == NULL)
23865cb0d679SMarcel Telka 						continue;
23875cb0d679SMarcel Telka 
23885cb0d679SMarcel Telka 					if (strcmp(sectype, "sys") != 0) {
23895cb0d679SMarcel Telka 						sa_free_attr_string(sectype);
23905cb0d679SMarcel Telka 						ret = SA_CONFIG_ERR;
23915cb0d679SMarcel Telka 						break;
23925cb0d679SMarcel Telka 					}
23935cb0d679SMarcel Telka 					sa_free_attr_string(sectype);
23945cb0d679SMarcel Telka 					ret = SA_OK;
23955cb0d679SMarcel Telka 				}
23965cb0d679SMarcel Telka 
23975cb0d679SMarcel Telka 				if (ret != SA_OK)
23985cb0d679SMarcel Telka 					break;
23995cb0d679SMarcel Telka 
24005cb0d679SMarcel Telka 				assert(optindex == OPT_UIDMAP ||
24015cb0d679SMarcel Telka 				    optindex == OPT_GIDMAP);
24025cb0d679SMarcel Telka 				f = optindex == OPT_UIDMAP ? check_user :
24035cb0d679SMarcel Telka 				    check_group;
24045cb0d679SMarcel Telka 
24055cb0d679SMarcel Telka 
24065cb0d679SMarcel Telka 				p = strdup(value);
24075cb0d679SMarcel Telka 				if (p == NULL)
24085cb0d679SMarcel Telka 					ret = SA_BAD_VALUE;
24095cb0d679SMarcel Telka 
24105cb0d679SMarcel Telka 				for (c = p; ret == SA_OK && c != NULL; c = n) {
24115cb0d679SMarcel Telka 					char *s;
24125cb0d679SMarcel Telka 					char *t;
24135cb0d679SMarcel Telka 
24145cb0d679SMarcel Telka 					n = strchr(c, '~');
24155cb0d679SMarcel Telka 					if (n != NULL)
24165cb0d679SMarcel Telka 						*n++ = '\0';
24175cb0d679SMarcel Telka 
24185cb0d679SMarcel Telka 					s = strchr(c, ':');
24195cb0d679SMarcel Telka 					if (s != NULL) {
24205cb0d679SMarcel Telka 						*s++ = '\0';
24215cb0d679SMarcel Telka 						t = strchr(s, ':');
24225cb0d679SMarcel Telka 						if (t != NULL)
24235cb0d679SMarcel Telka 							*t = '\0';
24245cb0d679SMarcel Telka 					}
24255cb0d679SMarcel Telka 
24265cb0d679SMarcel Telka 					if (s == NULL || t == NULL)
24275cb0d679SMarcel Telka 						ret = SA_BAD_VALUE;
24285cb0d679SMarcel Telka 
24295cb0d679SMarcel Telka 					if (ret == SA_OK && *c != '\0' &&
24305cb0d679SMarcel Telka 					    strcmp(c, "*") != 0)
24315cb0d679SMarcel Telka 						ret = f(c);
24325cb0d679SMarcel Telka 
24335cb0d679SMarcel Telka 					if (ret == SA_OK && *s != '\0' &&
24345cb0d679SMarcel Telka 					    strcmp(s, "-1") != 0)
24355cb0d679SMarcel Telka 						ret = f(s);
24365cb0d679SMarcel Telka 				}
24375cb0d679SMarcel Telka 
24385cb0d679SMarcel Telka 				free(p);
24395cb0d679SMarcel Telka 
24405cb0d679SMarcel Telka 				break;
24415cb0d679SMarcel Telka 			}
24425cb0d679SMarcel Telka 
2443a3351425Sdougm 			default:
2444a3351425Sdougm 				break;
24456185db85Sdougm 			}
2446aed5d200Sdougm 
2447aed5d200Sdougm 			if (value != NULL)
2448aed5d200Sdougm 				sa_free_attr_string(value);
2449aed5d200Sdougm 
2450a3351425Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
2451a3351425Sdougm 				/* do the property specific check */
2452687915e9Sdougm 				ret = optdefs[optindex].check(handle, property);
24536185db85Sdougm 			}
24546185db85Sdougm 		}
24556185db85Sdougm 	}
24566185db85Sdougm 
24576185db85Sdougm 	if (propname != NULL)
2458a3351425Sdougm 		sa_free_attr_string(propname);
24596185db85Sdougm 	return (ret);
24606185db85Sdougm }
24616185db85Sdougm 
24626185db85Sdougm /*
24636185db85Sdougm  * Protocol management functions
24646185db85Sdougm  *
24653472f5dcSdougm  * Properties defined in the default files are defined in
24663472f5dcSdougm  * proto_option_defs for parsing and validation. If "other" and
24673472f5dcSdougm  * "compare" are set, then the value for this property should be
24683472f5dcSdougm  * compared against the property specified in "other" using the
24693472f5dcSdougm  * "compare" check (either <= or >=) in order to ensure that the
24703472f5dcSdougm  * values are in the correct range.  E.g. setting server_versmin
24713472f5dcSdougm  * higher than server_versmax should not be allowed.
24726185db85Sdougm  */
24736185db85Sdougm 
24746185db85Sdougm struct proto_option_defs {
24756185db85Sdougm 	char *tag;
24766185db85Sdougm 	char *name;	/* display name -- remove protocol identifier */
24776185db85Sdougm 	int index;
24786185db85Sdougm 	int type;
24796185db85Sdougm 	union {
24806185db85Sdougm 	    int intval;
24816185db85Sdougm 	    char *string;
24826185db85Sdougm 	} defvalue;
24836185db85Sdougm 	uint32_t svcs;
24846185db85Sdougm 	int32_t minval;
24856185db85Sdougm 	int32_t maxval;
24863472f5dcSdougm 	char *other;
24873472f5dcSdougm 	int compare;
24883472f5dcSdougm #define	OPT_CMP_GE	0
24893472f5dcSdougm #define	OPT_CMP_LE	1
24906185db85Sdougm 	int (*check)(char *);
24916185db85Sdougm } proto_options[] = {
24926185db85Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
24936185db85Sdougm 	{"nfsd_servers",
24946185db85Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
2495dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    1, INT32_MAX},
24966185db85Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
24976185db85Sdougm 	{"lockd_listen_backlog",
24986185db85Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
2499dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX},
25006185db85Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
25016185db85Sdougm 	{"lockd_servers",
25026185db85Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
2503dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SVC_LOCKD, 1, INT32_MAX},
25046185db85Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
25056185db85Sdougm 	{"lockd_retransmit_timeout",
25066185db85Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
2507dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX},
25086185db85Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
25096185db85Sdougm 	{"grace_period",
25106185db85Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
2511dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SVC_LOCKD, 0, INT32_MAX},
25126185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
25136185db85Sdougm 	{"nfs_server_versmin",
25146185db85Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
25156185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2516dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    NFS_VERSMAX, "server_versmax", OPT_CMP_LE},
25176185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
25186185db85Sdougm 	{"nfs_server_versmax",
25196185db85Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
25206185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2521dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    NFS_VERSMAX, "server_versmin", OPT_CMP_GE},
25226185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
25236185db85Sdougm 	{"nfs_client_versmin",
25246185db85Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
2525dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    (int)NFS_VERSMIN_DEFAULT, SVC_CLIENT, NFS_VERSMIN, NFS_VERSMAX,
2526dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    "client_versmax", OPT_CMP_LE},
25276185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
25286185db85Sdougm 	{"nfs_client_versmax",
25296185db85Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
2530dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    (int)NFS_VERSMAX_DEFAULT, SVC_CLIENT, NFS_VERSMIN, NFS_VERSMAX,
2531dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    "client_versmin", OPT_CMP_GE},
25326185db85Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
25336185db85Sdougm 	{"nfs_server_delegation",
25346185db85Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
2535dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0},
25366185db85Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
25376185db85Sdougm 	{"nfsmapid_domain",
25386185db85Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
2539dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    NULL, SVC_NFSMAPID, 0, 0},
25406185db85Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
25416185db85Sdougm 	{"nfsd_max_connections",
25426185db85Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
2543dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX},
25446185db85Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
25456185db85Sdougm 	{"nfsd_protocol",
25466185db85Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
2547dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SVC_NFSD, 0, 0},
25486185db85Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
25496185db85Sdougm 	{"nfsd_listen_backlog",
25506185db85Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
2551dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_NUMBER, 0, SVC_NFSD, 0, INT32_MAX},
2552dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #define	PROTO_OPT_NFSD_DEVICE			14
2553dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	{"nfsd_device",
2554dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    "device", PROTO_OPT_NFSD_DEVICE,
2555dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_STRING, NULL, SVC_NFSD, 0, 0},
2556361f55a5SMarcel Telka #define	PROTO_OPT_MOUNTD_LISTEN_BACKLOG		15
2557361f55a5SMarcel Telka 	{"mountd_listen_backlog",
2558361f55a5SMarcel Telka 	    "mountd_listen_backlog", PROTO_OPT_MOUNTD_LISTEN_BACKLOG,
2559361f55a5SMarcel Telka 	    OPT_TYPE_NUMBER, 64, SVC_NFSD|SVC_MOUNTD, 1, INT32_MAX},
2560361f55a5SMarcel Telka #define	PROTO_OPT_MOUNTD_MAX_THREADS		16
2561361f55a5SMarcel Telka 	{"mountd_max_threads",
2562361f55a5SMarcel Telka 	    "mountd_max_threads", PROTO_OPT_MOUNTD_MAX_THREADS,
2563361f55a5SMarcel Telka 	    OPT_TYPE_NUMBER, 16, SVC_NFSD|SVC_MOUNTD, 1, INT32_MAX},
25646185db85Sdougm 	{NULL}
25656185db85Sdougm };
25666185db85Sdougm 
25676185db85Sdougm /*
25686185db85Sdougm  * the protoset holds the defined options so we don't have to read
25696185db85Sdougm  * them multiple times
25706185db85Sdougm  */
2571aed5d200Sdougm static sa_protocol_properties_t protoset;
25726185db85Sdougm 
25736185db85Sdougm static int
25746185db85Sdougm findprotoopt(char *name, int whichname)
25756185db85Sdougm {
25766185db85Sdougm 	int i;
25776185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2578a3351425Sdougm 		if (whichname == 1) {
2579a3351425Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
25806185db85Sdougm 			return (i);
2581a3351425Sdougm 		} else {
2582a3351425Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
2583a3351425Sdougm 				return (i);
2584a3351425Sdougm 		}
25856185db85Sdougm 	}
25866185db85Sdougm 	return (-1);
25876185db85Sdougm }
25886185db85Sdougm 
25896185db85Sdougm /*
25906185db85Sdougm  * fixcaselower(str)
25916185db85Sdougm  *
25926185db85Sdougm  * convert a string to lower case (inplace).
25936185db85Sdougm  */
25946185db85Sdougm 
25956185db85Sdougm static void
25966185db85Sdougm fixcaselower(char *str)
25976185db85Sdougm {
25986185db85Sdougm 	while (*str) {
2599a3351425Sdougm 		*str = tolower(*str);
2600a3351425Sdougm 		str++;
26016185db85Sdougm 	}
26026185db85Sdougm }
26036185db85Sdougm 
2604330ef417Sdougm /*
2605330ef417Sdougm  * skipwhitespace(str)
2606330ef417Sdougm  *
2607330ef417Sdougm  * Skip leading white space. It is assumed that it is called with a
2608330ef417Sdougm  * valid pointer.
2609330ef417Sdougm  */
2610330ef417Sdougm 
2611330ef417Sdougm static char *
2612330ef417Sdougm skipwhitespace(char *str)
2613330ef417Sdougm {
2614330ef417Sdougm 	while (*str && isspace(*str))
2615330ef417Sdougm 		str++;
2616330ef417Sdougm 
2617330ef417Sdougm 	return (str);
2618330ef417Sdougm }
2619330ef417Sdougm 
2620a3351425Sdougm /*
2621a3351425Sdougm  * extractprop()
2622a3351425Sdougm  *
2623a3351425Sdougm  * Extract the property and value out of the line and create the
2624a3351425Sdougm  * property in the optionset.
2625a3351425Sdougm  */
2626e7bab347Sdougm static int
2627a3351425Sdougm extractprop(char *name, char *value)
2628a3351425Sdougm {
2629a3351425Sdougm 	sa_property_t prop;
2630a3351425Sdougm 	int index;
2631e7bab347Sdougm 	int ret = SA_OK;
2632a3351425Sdougm 	/*
2633a3351425Sdougm 	 * Remove any leading
2634a3351425Sdougm 	 * white space.
2635a3351425Sdougm 	 */
2636a3351425Sdougm 	name = skipwhitespace(name);
2637a3351425Sdougm 
2638dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	index = findprotoopt(name, 1);
2639a3351425Sdougm 	if (index >= 0) {
2640a3351425Sdougm 		fixcaselower(name);
2641a3351425Sdougm 		prop = sa_create_property(proto_options[index].name, value);
2642a3351425Sdougm 		if (prop != NULL)
2643e7bab347Sdougm 			ret = sa_add_protocol_property(protoset, prop);
2644e7bab347Sdougm 		else
2645e7bab347Sdougm 			ret = SA_NO_MEMORY;
2646a3351425Sdougm 	}
2647e7bab347Sdougm 	return (ret);
2648a3351425Sdougm }
2649a3351425Sdougm 
2650dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States scf_type_t
2651dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States getscftype(int type)
2652dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States {
2653dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	scf_type_t ret;
2654dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
2655dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	switch (type) {
2656dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	case OPT_TYPE_NUMBER:
2657dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		ret = SCF_TYPE_INTEGER;
2658dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	break;
2659dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	case OPT_TYPE_BOOLEAN:
2660dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		ret = SCF_TYPE_BOOLEAN;
2661dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	break;
2662dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	default:
2663dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		ret = SCF_TYPE_ASTRING;
2664dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	}
2665dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	return (ret);
2666dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States }
2667dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
2668dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States char *
2669dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States getsvcname(uint32_t svcs)
2670dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States {
2671dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	char *service;
2672dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	switch (svcs) {
2673dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_LOCKD:
2674dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = LOCKD;
2675dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2676dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_STATD:
2677dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = STATD;
2678dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2679dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_NFSD:
2680dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFSD;
2681dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2682dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_CLIENT:
2683dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFS_CLIENT_SVC;
2684dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2685dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_NFS4CBD:
2686dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFS4CBD;
2687dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2688dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_NFSMAPID:
2689dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFSMAPID;
2690dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2691dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_RQUOTAD:
2692dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = RQUOTAD;
2693dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2694dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_NFSLOGD:
2695dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFSLOGD;
2696dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2697dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_REPARSED:
2698dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = REPARSED;
2699dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2700dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		default:
2701dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFSD;
2702dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	}
2703dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	return (service);
2704dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States }
2705dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
27066185db85Sdougm /*
2707dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * initprotofromsmf()
27086185db85Sdougm  *
2709dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * Read NFS SMF properties and add the defined values to the
27106185db85Sdougm  * protoset.  Note that default values are known from the built in
2711dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * table in case SMF doesn't have a definition. Not having
2712dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * SMF properties is OK since we have builtin default
2713dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * values.
27146185db85Sdougm  */
27156185db85Sdougm static int
2716dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States initprotofromsmf()
27176185db85Sdougm {
2718dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	char name[PATH_MAX];
2719dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	char value[PATH_MAX];
2720dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	int ret = SA_OK, bufsz = 0, i;
27216185db85Sdougm 
27226185db85Sdougm 	protoset = sa_create_protocol_properties("nfs");
27236185db85Sdougm 	if (protoset != NULL) {
2724dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		for (i = 0; proto_options[i].tag != NULL; i++) {
2725dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			scf_type_t ptype;
2726dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			char *svc_name;
2727dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
2728dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			bzero(value, PATH_MAX);
2729dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			(void) strncpy(name, proto_options[i].name, PATH_MAX);
2730dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			/* Replace NULL with the correct instance */
2731dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			ptype = getscftype(proto_options[i].type);
2732dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			svc_name = getsvcname(proto_options[i].svcs);
2733dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			bufsz = PATH_MAX;
2734dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			ret = nfs_smf_get_prop(name, value,
2735dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			    (char *)DEFAULT_INSTANCE, ptype,
2736dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			    svc_name, &bufsz);
2737dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			if (ret == SA_OK) {
2738dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				ret = extractprop(name, value);
2739917c27c8Sdougm 			}
27406185db85Sdougm 		}
2741e7bab347Sdougm 	} else {
2742e7bab347Sdougm 		ret = SA_NO_MEMORY;
27436185db85Sdougm 	}
2744dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
2745e7bab347Sdougm 	return (ret);
27466185db85Sdougm }
27476185db85Sdougm 
27486185db85Sdougm /*
2749a3351425Sdougm  * add_defaults()
27506185db85Sdougm  *
2751dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * Add the default values for any property not defined
2752dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * in NFS SMF repository.
2753dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * Values are set according to their defined types.
27546185db85Sdougm  */
27556185db85Sdougm 
27566185db85Sdougm static void
27576185db85Sdougm add_defaults()
27586185db85Sdougm {
27596185db85Sdougm 	int i;
27606185db85Sdougm 	char number[MAXDIGITS];
27616185db85Sdougm 
27626185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2763a3351425Sdougm 		sa_property_t prop;
2764a3351425Sdougm 		prop = sa_get_protocol_property(protoset,
2765a3351425Sdougm 		    proto_options[i].name);
2766a3351425Sdougm 		if (prop == NULL) {
2767a3351425Sdougm 			/* add the default value */
2768a3351425Sdougm 			switch (proto_options[i].type) {
2769a3351425Sdougm 			case OPT_TYPE_NUMBER:
2770a3351425Sdougm 				(void) snprintf(number, sizeof (number), "%d",
2771a3351425Sdougm 				    proto_options[i].defvalue.intval);
2772a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2773a3351425Sdougm 				    number);
2774a3351425Sdougm 				break;
2775a3351425Sdougm 
2776a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2777a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2778a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2779a3351425Sdougm 				    "true" : "false");
2780a3351425Sdougm 				break;
2781a3351425Sdougm 
2782a3351425Sdougm 			case OPT_TYPE_ONOFF:
2783a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2784a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2785a3351425Sdougm 				    "on" : "off");
2786a3351425Sdougm 				break;
2787a3351425Sdougm 
2788a3351425Sdougm 			default:
2789a3351425Sdougm 				/* treat as strings of zero length */
2790a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2791a3351425Sdougm 				    "");
2792a3351425Sdougm 				break;
2793a3351425Sdougm 			}
2794a3351425Sdougm 			if (prop != NULL)
2795a3351425Sdougm 				(void) sa_add_protocol_property(protoset, prop);
27966185db85Sdougm 		}
27976185db85Sdougm 	}
27986185db85Sdougm }
27996185db85Sdougm 
28006185db85Sdougm static void
28016185db85Sdougm free_protoprops()
28026185db85Sdougm {
2803aed5d200Sdougm 	if (protoset != NULL) {
2804aed5d200Sdougm 		xmlFreeNode(protoset);
2805aed5d200Sdougm 		protoset = NULL;
2806aed5d200Sdougm 	}
28076185db85Sdougm }
28086185db85Sdougm 
28096185db85Sdougm /*
28106185db85Sdougm  * nfs_init()
28116185db85Sdougm  *
28126185db85Sdougm  * Initialize the NFS plugin.
28136185db85Sdougm  */
28146185db85Sdougm 
28156185db85Sdougm static int
28166185db85Sdougm nfs_init()
28176185db85Sdougm {
28186185db85Sdougm 	int ret = SA_OK;
28196185db85Sdougm 
2820917c27c8Sdougm 	if (sa_plugin_ops.sa_init != nfs_init) {
2821a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2822a3351425Sdougm 		    "NFS plugin not properly initialized\n"));
2823917c27c8Sdougm 		return (SA_CONFIG_ERR);
2824917c27c8Sdougm 	}
28256185db85Sdougm 
2826dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	ret = initprotofromsmf();
2827917c27c8Sdougm 	if (ret != SA_OK) {
2828917c27c8Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2829dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		    "NFS plugin problem with SMF repository: %s\n"),
2830917c27c8Sdougm 		    sa_errorstr(ret));
2831917c27c8Sdougm 		ret = SA_OK;
2832917c27c8Sdougm 	}
2833917c27c8Sdougm 	add_defaults();
28346185db85Sdougm 
28356185db85Sdougm 	return (ret);
28366185db85Sdougm }
28376185db85Sdougm 
28386185db85Sdougm /*
28396185db85Sdougm  * nfs_fini()
28406185db85Sdougm  *
28416185db85Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
28426185db85Sdougm  */
28436185db85Sdougm 
28446185db85Sdougm static void
28456185db85Sdougm nfs_fini()
28466185db85Sdougm {
28476185db85Sdougm 	free_protoprops();
28486185db85Sdougm }
28496185db85Sdougm 
28506185db85Sdougm /*
28516185db85Sdougm  * nfs_get_proto_set()
28526185db85Sdougm  *
28536185db85Sdougm  * Return an optionset with all the protocol specific properties in
28546185db85Sdougm  * it.
28556185db85Sdougm  */
28566185db85Sdougm 
28576185db85Sdougm static sa_protocol_properties_t
28586185db85Sdougm nfs_get_proto_set()
28596185db85Sdougm {
28606185db85Sdougm 	return (protoset);
28616185db85Sdougm }
28626185db85Sdougm 
28633472f5dcSdougm /*
28643472f5dcSdougm  * service_in_state(service, chkstate)
28653472f5dcSdougm  *
28663472f5dcSdougm  * Want to know if the specified service is in the desired state
28673472f5dcSdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
28683472f5dcSdougm  * isn't.
28693472f5dcSdougm  */
28703472f5dcSdougm static int
28713472f5dcSdougm service_in_state(char *service, const char *chkstate)
28723472f5dcSdougm {
28733472f5dcSdougm 	char *state;
28743472f5dcSdougm 	int ret = B_FALSE;
28753472f5dcSdougm 
28763472f5dcSdougm 	state = smf_get_state(service);
28773472f5dcSdougm 	if (state != NULL) {
2878a3351425Sdougm 		/* got the state so get the equality for the return value */
2879a3351425Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2880a3351425Sdougm 		free(state);
28813472f5dcSdougm 	}
28823472f5dcSdougm 	return (ret);
28833472f5dcSdougm }
28843472f5dcSdougm 
28856185db85Sdougm /*
28866185db85Sdougm  * restart_service(svcs)
28876185db85Sdougm  *
28886185db85Sdougm  * Walk through the bit mask of services that need to be restarted in
28896185db85Sdougm  * order to use the new property values. Some properties affect
28903472f5dcSdougm  * multiple daemons. Should only restart a service if it is currently
28913472f5dcSdougm  * enabled (online).
28926185db85Sdougm  */
28936185db85Sdougm 
28946185db85Sdougm static void
28956185db85Sdougm restart_service(uint32_t svcs)
28966185db85Sdougm {
28976185db85Sdougm 	uint32_t mask;
28983472f5dcSdougm 	int ret;
28993472f5dcSdougm 	char *service;
29003472f5dcSdougm 
29016185db85Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
2902a3351425Sdougm 		switch (svcs & mask) {
2903a3351425Sdougm 		case SVC_LOCKD:
2904a3351425Sdougm 			service = LOCKD;
2905a3351425Sdougm 			break;
2906a3351425Sdougm 		case SVC_STATD:
2907a3351425Sdougm 			service = STATD;
2908a3351425Sdougm 			break;
2909a3351425Sdougm 		case SVC_NFSD:
2910a3351425Sdougm 			service = NFSD;
2911a3351425Sdougm 			break;
2912a3351425Sdougm 		case SVC_MOUNTD:
2913a3351425Sdougm 			service = MOUNTD;
2914a3351425Sdougm 			break;
2915a3351425Sdougm 		case SVC_NFS4CBD:
2916a3351425Sdougm 			service = NFS4CBD;
2917a3351425Sdougm 			break;
2918a3351425Sdougm 		case SVC_NFSMAPID:
2919a3351425Sdougm 			service = NFSMAPID;
2920a3351425Sdougm 			break;
2921a3351425Sdougm 		case SVC_RQUOTAD:
2922a3351425Sdougm 			service = RQUOTAD;
2923a3351425Sdougm 			break;
2924a3351425Sdougm 		case SVC_NFSLOGD:
2925a3351425Sdougm 			service = NFSLOGD;
2926a3351425Sdougm 			break;
29272f172c55SRobert Thurlow 		case SVC_REPARSED:
29282f172c55SRobert Thurlow 			service = REPARSED;
29292f172c55SRobert Thurlow 			break;
2930dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_CLIENT:
2931dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFS_CLIENT_SVC;
2932dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2933a3351425Sdougm 		default:
2934a3351425Sdougm 			continue;
2935a3351425Sdougm 		}
29363472f5dcSdougm 
29373472f5dcSdougm 		/*
29383472f5dcSdougm 		 * Only attempt to restart the service if it is
29393472f5dcSdougm 		 * currently running. In the future, it may be
29403472f5dcSdougm 		 * desirable to use smf_refresh_instance if the NFS
29413472f5dcSdougm 		 * services ever implement the refresh method.
29423472f5dcSdougm 		 */
2943a3351425Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2944a3351425Sdougm 			ret = smf_restart_instance(service);
29453472f5dcSdougm 			/*
2946a3351425Sdougm 			 * There are only a few SMF errors at this point, but
2947a3351425Sdougm 			 * it is also possible that a bad value may have put
2948a3351425Sdougm 			 * the service into maintenance if there wasn't an
2949a3351425Sdougm 			 * SMF level error.
29503472f5dcSdougm 			 */
2951a3351425Sdougm 			if (ret != 0) {
2952a3351425Sdougm 				(void) fprintf(stderr,
2953a3351425Sdougm 				    dgettext(TEXT_DOMAIN,
2954a3351425Sdougm 				    "%s failed to restart: %s\n"),
295503205638SMarcel Telka 				    service, scf_strerror(scf_error()));
2956a3351425Sdougm 			} else {
2957a3351425Sdougm 				/*
2958a3351425Sdougm 				 * Check whether it has gone to "maintenance"
2959a3351425Sdougm 				 * mode or not. Maintenance implies something
2960a3351425Sdougm 				 * went wrong.
2961a3351425Sdougm 				 */
2962a3351425Sdougm 				if (service_in_state(service,
2963a3351425Sdougm 				    SCF_STATE_STRING_MAINT)) {
2964a3351425Sdougm 					(void) fprintf(stderr,
2965a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
2966a3351425Sdougm 					    "%s failed to restart\n"),
2967a3351425Sdougm 					    service);
2968a3351425Sdougm 				}
2969a3351425Sdougm 			}
29703472f5dcSdougm 		}
2971a3351425Sdougm 		svcs &= ~mask;
29726185db85Sdougm 	}
29736185db85Sdougm }
29746185db85Sdougm 
29753472f5dcSdougm /*
29763472f5dcSdougm  * nfs_minmax_check(name, value)
29773472f5dcSdougm  *
29783472f5dcSdougm  * Verify that the value for the property specified by index is valid
29793472f5dcSdougm  * relative to the opposite value in the case of a min/max variable.
29803472f5dcSdougm  * Currently, server_minvers/server_maxvers and
29813472f5dcSdougm  * client_minvers/client_maxvers are the only ones to check.
29823472f5dcSdougm  */
29833472f5dcSdougm 
29843472f5dcSdougm static int
29853472f5dcSdougm nfs_minmax_check(int index, int value)
29863472f5dcSdougm {
29873472f5dcSdougm 	int val;
29883472f5dcSdougm 	char *pval;
29893472f5dcSdougm 	sa_property_t prop;
29903472f5dcSdougm 	sa_optionset_t opts;
29913472f5dcSdougm 	int ret = B_TRUE;
29923472f5dcSdougm 
29933472f5dcSdougm 	if (proto_options[index].other != NULL) {
2994a3351425Sdougm 		/* have a property to compare against */
2995a3351425Sdougm 		opts = nfs_get_proto_set();
2996a3351425Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
29973472f5dcSdougm 		/*
29983472f5dcSdougm 		 * If we don't find the property, assume default
29993472f5dcSdougm 		 * values which will work since the max will be at the
30003472f5dcSdougm 		 * max and the min at the min.
30013472f5dcSdougm 		 */
3002a3351425Sdougm 		if (prop != NULL) {
3003a3351425Sdougm 			pval = sa_get_property_attr(prop, "value");
3004a3351425Sdougm 			if (pval != NULL) {
3005a3351425Sdougm 				val = strtoul(pval, NULL, 0);
3006a3351425Sdougm 				if (proto_options[index].compare ==
3007a3351425Sdougm 				    OPT_CMP_LE) {
3008a3351425Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
3009a3351425Sdougm 				} else if (proto_options[index].compare ==
3010a3351425Sdougm 				    OPT_CMP_GE) {
3011a3351425Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
3012a3351425Sdougm 				}
3013fe1c642dSBill Krier 				sa_free_attr_string(pval);
3014a3351425Sdougm 			}
30153472f5dcSdougm 		}
30163472f5dcSdougm 	}
30173472f5dcSdougm 	return (ret);
30183472f5dcSdougm }
30193472f5dcSdougm 
30206185db85Sdougm /*
30216185db85Sdougm  * nfs_validate_proto_prop(index, name, value)
30226185db85Sdougm  *
3023da6c28aaSamw  * Verify that the property specified by name can take the new
30246185db85Sdougm  * value. This is a sanity check to prevent bad values getting into
30253472f5dcSdougm  * the default files. All values need to be checked against what is
30263472f5dcSdougm  * allowed by their defined type. If a type isn't explicitly defined
30273472f5dcSdougm  * here, it is treated as a string.
30283472f5dcSdougm  *
30293472f5dcSdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
30303472f5dcSdougm  * within the range specified and potentially against another property
30313472f5dcSdougm  * value as well as specified in the proto_options members other and
30323472f5dcSdougm  * compare.
30336185db85Sdougm  */
30346185db85Sdougm 
30356185db85Sdougm static int
30366185db85Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
30376185db85Sdougm {
30386185db85Sdougm 	int ret = SA_OK;
30396185db85Sdougm 	char *cp;
30406185db85Sdougm #ifdef lint
30416185db85Sdougm 	name = name;
30426185db85Sdougm #endif
30436185db85Sdougm 	switch (proto_options[index].type) {
30446185db85Sdougm 	case OPT_TYPE_NUMBER:
3045a3351425Sdougm 		if (!is_a_number(value))
3046a3351425Sdougm 			ret = SA_BAD_VALUE;
3047a3351425Sdougm 		else {
3048a3351425Sdougm 			int val;
3049a3351425Sdougm 			val = strtoul(value, NULL, 0);
3050a3351425Sdougm 			if (val < proto_options[index].minval ||
3051a3351425Sdougm 			    val > proto_options[index].maxval)
3052a3351425Sdougm 				ret = SA_BAD_VALUE;
3053a3351425Sdougm 			/*
3054a3351425Sdougm 			 * For server_versmin/server_versmax and
3055a3351425Sdougm 			 * client_versmin/client_versmax, the value of the
3056a3351425Sdougm 			 * min(max) should be checked to be correct relative
3057a3351425Sdougm 			 * to the current max(min).
3058a3351425Sdougm 			 */
3059a3351425Sdougm 			if (!nfs_minmax_check(index, val)) {
3060a3351425Sdougm 				ret = SA_BAD_VALUE;
3061a3351425Sdougm 			}
30623472f5dcSdougm 		}
3063a3351425Sdougm 		break;
30643472f5dcSdougm 
30656185db85Sdougm 	case OPT_TYPE_DOMAIN:
30666185db85Sdougm 		/*
30673472f5dcSdougm 		 * needs to be a qualified domain so will have at
30683472f5dcSdougm 		 * least one period and other characters on either
30693472f5dcSdougm 		 * side of it.  A zero length string is also allowed
30703472f5dcSdougm 		 * and is the way to turn off the override.
30716185db85Sdougm 		 */
3072a3351425Sdougm 		if (strlen(value) == 0)
3073a3351425Sdougm 			break;
3074a3351425Sdougm 		cp = strchr(value, '.');
3075a3351425Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
3076a3351425Sdougm 			ret = SA_BAD_VALUE;
30773472f5dcSdougm 		break;
30783472f5dcSdougm 
30796185db85Sdougm 	case OPT_TYPE_BOOLEAN:
3080a3351425Sdougm 		if (strlen(value) == 0 ||
3081a3351425Sdougm 		    strcasecmp(value, "true") == 0 ||
3082a3351425Sdougm 		    strcmp(value, "1") == 0 ||
3083a3351425Sdougm 		    strcasecmp(value, "false") == 0 ||
3084a3351425Sdougm 		    strcmp(value, "0") == 0) {
3085a3351425Sdougm 			ret = SA_OK;
3086a3351425Sdougm 		} else {
3087a3351425Sdougm 			ret = SA_BAD_VALUE;
3088a3351425Sdougm 		}
3089a3351425Sdougm 		break;
30903472f5dcSdougm 
30916185db85Sdougm 	case OPT_TYPE_ONOFF:
3092a3351425Sdougm 		if (strcasecmp(value, "on") != 0 &&
3093a3351425Sdougm 		    strcasecmp(value, "off") != 0) {
3094a3351425Sdougm 			ret = SA_BAD_VALUE;
3095a3351425Sdougm 		}
3096a3351425Sdougm 		break;
30973472f5dcSdougm 
30986185db85Sdougm 	case OPT_TYPE_PROTOCOL:
3099917c27c8Sdougm 		if (strlen(value) != 0 &&
3100917c27c8Sdougm 		    strcasecmp(value, "all") != 0 &&
3101a3351425Sdougm 		    strcasecmp(value, "tcp") != 0 &&
3102a3351425Sdougm 		    strcasecmp(value, "udp") != 0)
3103a3351425Sdougm 			ret = SA_BAD_VALUE;
3104a3351425Sdougm 		break;
31053472f5dcSdougm 
31063472f5dcSdougm 	default:
3107a3351425Sdougm 		/* treat as a string */
3108a3351425Sdougm 		break;
31096185db85Sdougm 	}
31106185db85Sdougm 	return (ret);
31116185db85Sdougm }
31126185db85Sdougm 
31136185db85Sdougm /*
31146185db85Sdougm  * nfs_set_proto_prop(prop)
31156185db85Sdougm  *
31166185db85Sdougm  * check that prop is valid.
31176185db85Sdougm  */
31186185db85Sdougm 
31196185db85Sdougm static int
31206185db85Sdougm nfs_set_proto_prop(sa_property_t prop)
31216185db85Sdougm {
31226185db85Sdougm 	int ret = SA_OK;
31236185db85Sdougm 	char *name;
31246185db85Sdougm 	char *value;
31256185db85Sdougm 
31266185db85Sdougm 	name = sa_get_property_attr(prop, "type");
31276185db85Sdougm 	value = sa_get_property_attr(prop, "value");
31286185db85Sdougm 	if (name != NULL && value != NULL) {
3129dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		scf_type_t sctype;
3130dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		char *svc_name;
3131dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		char *instance = NULL;
3132a3351425Sdougm 		int index = findprotoopt(name, 1);
3133dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
3134dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		ret = nfs_validate_proto_prop(index, name, value);
3135dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		if (ret == SA_OK) {
3136dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			sctype = getscftype(proto_options[index].type);
3137dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			svc_name = getsvcname(proto_options[index].svcs);
3138dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			if (sctype == SCF_TYPE_BOOLEAN) {
3139dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				if (value != NULL)
3140dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 					sa_free_attr_string(value);
3141dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				if (string_to_boolean(value) == 0)
3142dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 					value = strdup("0");
3143dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				else
3144dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 					value = strdup("1");
3145dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			}
3146dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			ret = nfs_smf_set_prop(name, value, instance, sctype,
3147dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			    svc_name);
3148dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			if (ret == SA_OK) {
3149a3351425Sdougm 				restart_service(proto_options[index].svcs);
3150dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			} else {
3151dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				(void) printf(dgettext(TEXT_DOMAIN,
3152dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				    "Cannot restart NFS services : %s\n"),
3153dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				    sa_errorstr(ret));
3154dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			}
3155a3351425Sdougm 		}
31566185db85Sdougm 	}
31576185db85Sdougm 	if (name != NULL)
3158a3351425Sdougm 		sa_free_attr_string(name);
31596185db85Sdougm 	if (value != NULL)
3160a3351425Sdougm 		sa_free_attr_string(value);
31616185db85Sdougm 	return (ret);
31626185db85Sdougm }
31636185db85Sdougm 
31646185db85Sdougm /*
31656185db85Sdougm  * nfs_get_status()
31666185db85Sdougm  *
31676185db85Sdougm  * What is the current status of the nfsd? We use the SMF state here.
31686185db85Sdougm  * Caller must free the returned value.
31696185db85Sdougm  */
31706185db85Sdougm 
31716185db85Sdougm static char *
31726185db85Sdougm nfs_get_status()
31736185db85Sdougm {
3174652601fbSRobert Mustacchi 	return (smf_get_state(NFSD));
31756185db85Sdougm }
31766185db85Sdougm 
31776185db85Sdougm /*
31786185db85Sdougm  * nfs_space_alias(alias)
31796185db85Sdougm  *
31806185db85Sdougm  * Lookup the space (security) name. If it is default, convert to the
31816185db85Sdougm  * real name.
31826185db85Sdougm  */
31836185db85Sdougm 
31846185db85Sdougm static char *
31856185db85Sdougm nfs_space_alias(char *space)
31866185db85Sdougm {
31876185db85Sdougm 	char *name = space;
31886185db85Sdougm 	seconfig_t secconf;
3189a99982a7Sdougm 
3190a99982a7Sdougm 	/*
3191a99982a7Sdougm 	 * Only the space named "default" is special. If it is used,
3192a99982a7Sdougm 	 * the default needs to be looked up and the real name used.
3193a99982a7Sdougm 	 * This is normally "sys" but could be changed.  We always
31945cb0d679SMarcel Telka 	 * change default to the real name.
3195a99982a7Sdougm 	 */
3196a99982a7Sdougm 	if (strcmp(space, "default") == 0 &&
3197a99982a7Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
3198a3351425Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
3199a3351425Sdougm 			name = secconf.sc_name;
32006185db85Sdougm 	}
32016185db85Sdougm 	return (strdup(name));
32026185db85Sdougm }
3203da6c28aaSamw 
3204da6c28aaSamw /*
3205da6c28aaSamw  * nfs_features()
3206da6c28aaSamw  *
3207da6c28aaSamw  * Return a mask of the features required.
3208da6c28aaSamw  */
3209da6c28aaSamw 
3210da6c28aaSamw static uint64_t
3211da6c28aaSamw nfs_features()
3212da6c28aaSamw {
32139e5da854Sdougm 	return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);
3214da6c28aaSamw }
3215