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 /*
238e314a44Sdougm  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
246185db85Sdougm  * Use is subject to license terms.
256185db85Sdougm  */
266185db85Sdougm 
276185db85Sdougm /*
286185db85Sdougm  * NFS specific functions
296185db85Sdougm  */
306185db85Sdougm #include <stdio.h>
316185db85Sdougm #include <string.h>
326185db85Sdougm #include <ctype.h>
336185db85Sdougm #include <stdlib.h>
346185db85Sdougm #include <unistd.h>
356185db85Sdougm #include <zone.h>
366185db85Sdougm #include <errno.h>
376185db85Sdougm #include <locale.h>
386185db85Sdougm #include <signal.h>
396185db85Sdougm #include "libshare.h"
406185db85Sdougm #include "libshare_impl.h"
416185db85Sdougm #include <nfs/export.h>
426185db85Sdougm #include <pwd.h>
436185db85Sdougm #include <limits.h>
446185db85Sdougm #include <libscf.h>
456185db85Sdougm #include "nfslog_config.h"
466185db85Sdougm #include "nfslogtab.h"
476185db85Sdougm #include "libshare_nfs.h"
486185db85Sdougm #include <rpcsvc/daemon_utils.h>
496185db85Sdougm #include <nfs/nfs.h>
50ecd6cf80Smarks #include <nfs/nfssys.h>
516185db85Sdougm 
526185db85Sdougm /* should really be in some global place */
536185db85Sdougm #define	DEF_WIN	30000
546185db85Sdougm #define	OPT_CHUNK	1024
556185db85Sdougm 
566185db85Sdougm int debug = 0;
576185db85Sdougm 
58ecd6cf80Smarks #define	NFS_SERVER_SVC	"svc:/network/nfs/server:default"
596185db85Sdougm 
606185db85Sdougm /* internal functions */
616185db85Sdougm static int nfs_init();
626185db85Sdougm static void nfs_fini();
636185db85Sdougm static int nfs_enable_share(sa_share_t);
64ecd6cf80Smarks static int nfs_disable_share(sa_share_t, char *);
65687915e9Sdougm static int nfs_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
666185db85Sdougm static int nfs_validate_security_mode(char *);
676185db85Sdougm static int nfs_is_security_opt(char *);
686185db85Sdougm static int nfs_parse_legacy_options(sa_group_t, char *);
696185db85Sdougm static char *nfs_format_options(sa_group_t, int);
706185db85Sdougm static int nfs_set_proto_prop(sa_property_t);
716185db85Sdougm static sa_protocol_properties_t nfs_get_proto_set();
726185db85Sdougm static char *nfs_get_status();
736185db85Sdougm static char *nfs_space_alias(char *);
74da6c28aaSamw static uint64_t nfs_features();
756185db85Sdougm 
766185db85Sdougm /*
776185db85Sdougm  * ops vector that provides the protocol specific info and operations
786185db85Sdougm  * for share management.
796185db85Sdougm  */
806185db85Sdougm 
816185db85Sdougm struct sa_plugin_ops sa_plugin_ops = {
826185db85Sdougm 	SA_PLUGIN_VERSION,
836185db85Sdougm 	"nfs",
846185db85Sdougm 	nfs_init,
856185db85Sdougm 	nfs_fini,
866185db85Sdougm 	nfs_enable_share,
876185db85Sdougm 	nfs_disable_share,
886185db85Sdougm 	nfs_validate_property,
896185db85Sdougm 	nfs_validate_security_mode,
906185db85Sdougm 	nfs_is_security_opt,
916185db85Sdougm 	nfs_parse_legacy_options,
926185db85Sdougm 	nfs_format_options,
936185db85Sdougm 	nfs_set_proto_prop,
946185db85Sdougm 	nfs_get_proto_set,
956185db85Sdougm 	nfs_get_status,
966185db85Sdougm 	nfs_space_alias,
97da6c28aaSamw 	NULL,	/* update_legacy */
98da6c28aaSamw 	NULL,	/* delete_legacy */
99da6c28aaSamw 	NULL,	/* change_notify */
100da6c28aaSamw 	NULL,	/* enable_resource */
101da6c28aaSamw 	NULL,	/* disable_resource */
102da6c28aaSamw 	nfs_features,
103da6c28aaSamw 	NULL,	/* transient shares */
104da6c28aaSamw 	NULL,	/* notify resource */
1054bff34e3Sthurlow 	NULL,	/* rename_resource */
1064bff34e3Sthurlow 	NULL,	/* run_command */
1074bff34e3Sthurlow 	NULL,	/* command_help */
1084bff34e3Sthurlow 	NULL	/* delete_proto_section */
1096185db85Sdougm };
1106185db85Sdougm 
1116185db85Sdougm /*
1126185db85Sdougm  * list of support services needed
1136185db85Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
1146185db85Sdougm  */
1156185db85Sdougm 
1166185db85Sdougm static char *service_list_default[] =
1176185db85Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL };
1186185db85Sdougm static char *service_list_logging[] =
1196185db85Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL };
1206185db85Sdougm 
1216185db85Sdougm /*
1226185db85Sdougm  * option definitions.  Make sure to keep the #define for the option
1236185db85Sdougm  * index just before the entry it is the index for. Changing the order
1246185db85Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1256185db85Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1266185db85Sdougm  */
1276185db85Sdougm 
1286185db85Sdougm struct option_defs optdefs[] = {
1296185db85Sdougm #define	OPT_RO		0
1306185db85Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1316185db85Sdougm #define	OPT_RW		1
1326185db85Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1336185db85Sdougm #define	OPT_ROOT	2
1346185db85Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1356185db85Sdougm #define	OPT_SECURE	3
1366185db85Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1376185db85Sdougm #define	OPT_ANON	4
1386185db85Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1396185db85Sdougm #define	OPT_WINDOW	5
1406185db85Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1416185db85Sdougm #define	OPT_NOSUID	6
1426185db85Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1436185db85Sdougm #define	OPT_ACLOK	7
1446185db85Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1456185db85Sdougm #define	OPT_NOSUB	8
1466185db85Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1476185db85Sdougm #define	OPT_SEC		9
1486185db85Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1496185db85Sdougm #define	OPT_PUBLIC	10
1506185db85Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1516185db85Sdougm #define	OPT_INDEX	11
1526185db85Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1536185db85Sdougm #define	OPT_LOG		12
1546185db85Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1556185db85Sdougm #define	OPT_CKSUM	13
1566185db85Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
157*b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_NONE	14
158*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{SHOPT_NONE, OPT_NONE, OPT_TYPE_ACCLIST},
159*b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_ROOT_MAPPING	15
160*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{SHOPT_ROOT_MAPPING, OPT_ROOT_MAPPING, OPT_TYPE_USER},
161*b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_CHARSET_MAP	16
162*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{"", OPT_CHARSET_MAP, OPT_TYPE_ACCLIST},
1636185db85Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
164*b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_VOLFH	17
1656185db85Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1666185db85Sdougm #endif /* VOLATILE_FH_TEST */
1676185db85Sdougm 	NULL
1686185db85Sdougm };
1696185db85Sdougm 
170*b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
171*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Codesets that may need to be converted to UTF-8 for file paths.
172*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Add new names here to add new property support. If we ever get a
173*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * way to query the kernel for character sets, this should become
174*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * dynamically loaded. Make sure changes here are reflected in
175*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * cmd/fs.d/nfs/mountd/nfscmd.c
176*b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
177*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
178*b89a8333Snatalie li - Sun Microsystems - Irvine United States static char *legal_conv[] = {
179*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-cn",
180*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-jp",
181*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-jpms",
182*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-kr",
183*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-tw",
184*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-1",
185*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-2",
186*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-5",
187*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-6",
188*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-7",
189*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-8",
190*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-9",
191*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-13",
192*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-15",
193*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"koi8-r",
194*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	NULL
195*b89a8333Snatalie li - Sun Microsystems - Irvine United States };
196*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
1976185db85Sdougm /*
198a99982a7Sdougm  * list of properties that are related to security flavors.
1996185db85Sdougm  */
2006185db85Sdougm static char *seclist[] = {
2016185db85Sdougm 	SHOPT_RO,
2026185db85Sdougm 	SHOPT_RW,
2036185db85Sdougm 	SHOPT_ROOT,
2046185db85Sdougm 	SHOPT_WINDOW,
205*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	SHOPT_NONE,
206*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	SHOPT_ROOT_MAPPING,
2076185db85Sdougm 	NULL
2086185db85Sdougm };
2096185db85Sdougm 
2106185db85Sdougm /* structure for list of securities */
2116185db85Sdougm struct securities {
212a99982a7Sdougm 	sa_security_t security;
213a99982a7Sdougm 	struct securities *next;
2146185db85Sdougm };
2156185db85Sdougm 
216*b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
217*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * findcharset(charset)
218*b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
219*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Returns B_TRUE if the charset is a legal conversion otherwise
220*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * B_FALSE. This will need to be rewritten to be more efficient when
221*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * we have a dynamic list of legal conversions.
222*b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
223*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
224*b89a8333Snatalie li - Sun Microsystems - Irvine United States static boolean_t
225*b89a8333Snatalie li - Sun Microsystems - Irvine United States findcharset(char *charset)
226*b89a8333Snatalie li - Sun Microsystems - Irvine United States {
227*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int i;
228*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
229*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	for (i = 0; legal_conv[i] != NULL; i++)
230*b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (strcmp(charset, legal_conv[i]) == 0)
231*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (B_TRUE);
232*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	return (B_FALSE);
233*b89a8333Snatalie li - Sun Microsystems - Irvine United States }
234*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2356185db85Sdougm /*
2366185db85Sdougm  * findopt(name)
2376185db85Sdougm  *
2386185db85Sdougm  * Lookup option "name" in the option table and return the table
2396185db85Sdougm  * index.
2406185db85Sdougm  */
2416185db85Sdougm 
2426185db85Sdougm static int
2436185db85Sdougm findopt(char *name)
2446185db85Sdougm {
2456185db85Sdougm 	int i;
2466185db85Sdougm 	if (name != NULL) {
247a3351425Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
248a3351425Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
249a3351425Sdougm 				return (i);
250a3351425Sdougm 		}
251*b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (findcharset(name))
252*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (OPT_CHARSET_MAP);
2536185db85Sdougm 	}
2546185db85Sdougm 	return (-1);
2556185db85Sdougm }
2566185db85Sdougm 
2576185db85Sdougm /*
2586185db85Sdougm  * gettype(name)
2596185db85Sdougm  *
2606185db85Sdougm  * Return the type of option "name".
2616185db85Sdougm  */
2626185db85Sdougm 
2636185db85Sdougm static int
2646185db85Sdougm gettype(char *name)
2656185db85Sdougm {
2666185db85Sdougm 	int optdef;
2676185db85Sdougm 
2686185db85Sdougm 	optdef = findopt(name);
2696185db85Sdougm 	if (optdef != -1)
270a3351425Sdougm 		return (optdefs[optdef].type);
2716185db85Sdougm 	return (OPT_TYPE_ANY);
2726185db85Sdougm }
2736185db85Sdougm 
2746185db85Sdougm /*
2756185db85Sdougm  * nfs_validate_security_mode(mode)
2766185db85Sdougm  *
2776185db85Sdougm  * is the specified mode string a valid one for use with NFS?
2786185db85Sdougm  */
2796185db85Sdougm 
2806185db85Sdougm static int
2816185db85Sdougm nfs_validate_security_mode(char *mode)
2826185db85Sdougm {
2836185db85Sdougm 	seconfig_t secinfo;
2846185db85Sdougm 	int err;
2856185db85Sdougm 
2866185db85Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2876185db85Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2886185db85Sdougm 	if (err == SC_NOERROR)
289a3351425Sdougm 		return (1);
2906185db85Sdougm 	return (0);
2916185db85Sdougm }
2926185db85Sdougm 
2936185db85Sdougm /*
2946185db85Sdougm  * nfs_is_security_opt(tok)
2956185db85Sdougm  *
2966185db85Sdougm  * check to see if tok represents an option that is only valid in some
2976185db85Sdougm  * security flavor.
2986185db85Sdougm  */
2996185db85Sdougm 
3006185db85Sdougm static int
3016185db85Sdougm nfs_is_security_opt(char *tok)
3026185db85Sdougm {
3036185db85Sdougm 	int i;
3046185db85Sdougm 
3056185db85Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
306a3351425Sdougm 		if (strcmp(tok, seclist[i]) == 0)
307a3351425Sdougm 			return (1);
3086185db85Sdougm 	}
3096185db85Sdougm 	return (0);
3106185db85Sdougm }
3116185db85Sdougm 
3126185db85Sdougm /*
3136185db85Sdougm  * find_security(seclist, sec)
3146185db85Sdougm  *
3156185db85Sdougm  * Walk the current list of security flavors and return true if it is
3166185db85Sdougm  * present, else return false.
3176185db85Sdougm  */
3186185db85Sdougm 
3196185db85Sdougm static int
3206185db85Sdougm find_security(struct securities *seclist, sa_security_t sec)
3216185db85Sdougm {
3226185db85Sdougm 	while (seclist != NULL) {
323a3351425Sdougm 		if (seclist->security == sec)
324a3351425Sdougm 			return (1);
325a3351425Sdougm 		seclist = seclist->next;
3266185db85Sdougm 	}
3276185db85Sdougm 	return (0);
3286185db85Sdougm }
3296185db85Sdougm 
3306185db85Sdougm /*
3316185db85Sdougm  * make_security_list(group, securitymodes, proto)
3326185db85Sdougm  *	go through the list of securitymodes and add them to the
3336185db85Sdougm  *	group's list of security optionsets. We also keep a list of
3346185db85Sdougm  *	those optionsets so we don't have to find them later. All of
3356185db85Sdougm  *	these will get copies of the same properties.
3366185db85Sdougm  */
3376185db85Sdougm 
3386185db85Sdougm static struct securities *
3396185db85Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
3406185db85Sdougm {
3416185db85Sdougm 	char *tok, *next = NULL;
3426185db85Sdougm 	struct securities *curp, *headp = NULL, *prev;
3436185db85Sdougm 	sa_security_t check;
3446185db85Sdougm 	int freetok = 0;
3456185db85Sdougm 
3466185db85Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
347a3351425Sdougm 		next = strchr(tok, ':');
348a3351425Sdougm 		if (next != NULL)
349a3351425Sdougm 			*next++ = '\0';
350a3351425Sdougm 		if (strcmp(tok, "default") == 0) {
351a3351425Sdougm 			/* resolve default into the real type */
352a3351425Sdougm 			tok = nfs_space_alias(tok);
353a3351425Sdougm 			freetok = 1;
354a3351425Sdougm 		}
355a3351425Sdougm 		check = sa_get_security(group, tok, proto);
356a3351425Sdougm 
357a3351425Sdougm 		/* add to the security list if it isn't there already */
358a3351425Sdougm 		if (check == NULL || !find_security(headp, check)) {
359a3351425Sdougm 			curp = (struct securities *)calloc(1,
360a3351425Sdougm 			    sizeof (struct securities));
361a3351425Sdougm 			if (curp != NULL) {
362a3351425Sdougm 				if (check == NULL) {
363a3351425Sdougm 					curp->security = sa_create_security(
364a3351425Sdougm 					    group, tok, proto);
365a3351425Sdougm 				} else {
366a3351425Sdougm 					curp->security = check;
367a3351425Sdougm 				}
368a3351425Sdougm 				/*
369a3351425Sdougm 				 * note that the first time through the loop,
370a3351425Sdougm 				 * headp will be NULL and prev will be
371a3351425Sdougm 				 * undefined.  Since headp is NULL, we set
372a3351425Sdougm 				 * both it and prev to the curp (first
373a3351425Sdougm 				 * structure to be allocated).
374a3351425Sdougm 				 *
375a3351425Sdougm 				 * later passes through the loop will have
376a3351425Sdougm 				 * headp not being NULL and prev will be used
377a3351425Sdougm 				 * to allocate at the end of the list.
378a3351425Sdougm 				 */
379a3351425Sdougm 				if (headp == NULL) {
380a3351425Sdougm 					headp = curp;
381a3351425Sdougm 					prev = curp;
382a3351425Sdougm 				} else {
383a3351425Sdougm 					prev->next = curp;
384a3351425Sdougm 					prev = curp;
385a3351425Sdougm 				}
386a3351425Sdougm 			}
3876185db85Sdougm 		}
388a99982a7Sdougm 
389a3351425Sdougm 		if (freetok) {
390a3351425Sdougm 			freetok = 0;
391a3351425Sdougm 			sa_free_attr_string(tok);
392a3351425Sdougm 		}
3936185db85Sdougm 	}
3946185db85Sdougm 	return (headp);
3956185db85Sdougm }
3966185db85Sdougm 
3976185db85Sdougm static void
3986185db85Sdougm free_security_list(struct securities *sec)
3996185db85Sdougm {
4006185db85Sdougm 	struct securities *next;
4016185db85Sdougm 	if (sec != NULL) {
402a3351425Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
403a3351425Sdougm 			next = sec->next;
404a3351425Sdougm 			free(sec);
405a3351425Sdougm 		}
4066185db85Sdougm 	}
4076185db85Sdougm }
4086185db85Sdougm 
4096185db85Sdougm /*
4106185db85Sdougm  * nfs_alistcat(str1, str2, sep)
4116185db85Sdougm  *
4126185db85Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
4136185db85Sdougm  * character. If memory allocation fails, return NULL;
4146185db85Sdougm  */
4156185db85Sdougm 
4166185db85Sdougm static char *
4176185db85Sdougm nfs_alistcat(char *str1, char *str2, char sep)
4186185db85Sdougm {
4196185db85Sdougm 	char *newstr;
4206185db85Sdougm 	size_t len;
4216185db85Sdougm 
4226185db85Sdougm 	len = strlen(str1) + strlen(str2) + 2;
4236185db85Sdougm 	newstr = (char *)malloc(len);
4246185db85Sdougm 	if (newstr != NULL)
425a3351425Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
4266185db85Sdougm 	return (newstr);
4276185db85Sdougm }
4286185db85Sdougm 
4296185db85Sdougm /*
4306185db85Sdougm  * add_security_prop(sec, name, value, persist)
4316185db85Sdougm  *
4326185db85Sdougm  * Add the property to the securities structure. This accumulates
4336185db85Sdougm  * properties for as part of parsing legacy options.
4346185db85Sdougm  */
4356185db85Sdougm 
4366185db85Sdougm static int
4376185db85Sdougm add_security_prop(struct securities *sec, char *name, char *value,
4386185db85Sdougm 			int persist, int iszfs)
4396185db85Sdougm {
4406185db85Sdougm 	sa_property_t prop;
4416185db85Sdougm 	int ret = SA_OK;
4426185db85Sdougm 
4436185db85Sdougm 	for (; sec != NULL; sec = sec->next) {
444a3351425Sdougm 		if (value == NULL) {
445a3351425Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
446a3351425Sdougm 			    strcmp(name, SHOPT_RO) == 0)
447a3351425Sdougm 				value = "*";
448a3351425Sdougm 			else
449a3351425Sdougm 				value = "true";
450a3351425Sdougm 		}
451a99982a7Sdougm 
452a99982a7Sdougm 		/*
453a99982a7Sdougm 		 * Get the existing property, if it exists, so we can
454a99982a7Sdougm 		 * determine what to do with it. The ro/rw/root
455a99982a7Sdougm 		 * properties can be merged if multiple instances of
456a99982a7Sdougm 		 * these properies are given. For example, if "rw"
457a99982a7Sdougm 		 * exists with a value "host1" and a later token of
458a99982a7Sdougm 		 * rw="host2" is seen, the values are merged into a
459a99982a7Sdougm 		 * single rw="host1:host2".
460a99982a7Sdougm 		 */
461a3351425Sdougm 		prop = sa_get_property(sec->security, name);
462a99982a7Sdougm 
463a3351425Sdougm 		if (prop != NULL) {
464a3351425Sdougm 			char *oldvalue;
465a3351425Sdougm 			char *newvalue;
466a99982a7Sdougm 
467a99982a7Sdougm 			/*
468a3351425Sdougm 			 * The security options of ro/rw/root might appear
469a3351425Sdougm 			 * multiple times. If they do, the values need to be
470a3351425Sdougm 			 * merged into an access list. If it was previously
471a3351425Sdougm 			 * empty, the new value alone is added.
472a99982a7Sdougm 			 */
473a3351425Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
474a3351425Sdougm 			if (oldvalue != NULL) {
475a3351425Sdougm 				/*
476a3351425Sdougm 				 * The general case is to concatenate the new
477a3351425Sdougm 				 * value onto the old value for multiple
478a3351425Sdougm 				 * rw(ro/root) properties. A special case
479a3351425Sdougm 				 * exists when either the old or new is the
480a3351425Sdougm 				 * "all" case. In the special case, if both
481a3351425Sdougm 				 * are "all", then it is "all", else if one is
482a3351425Sdougm 				 * an access-list, that replaces the "all".
483a3351425Sdougm 				 */
484a3351425Sdougm 				if (strcmp(oldvalue, "*") == 0) {
485a3351425Sdougm 					/* Replace old value with new value. */
486a3351425Sdougm 					newvalue = strdup(value);
48797df5ac9Sdougm 				} else if (strcmp(value, "*") == 0 ||
48897df5ac9Sdougm 				    strcmp(oldvalue, value) == 0) {
489a3351425Sdougm 					/*
490a3351425Sdougm 					 * Keep old value and ignore
491a3351425Sdougm 					 * the new value.
492a3351425Sdougm 					 */
493a3351425Sdougm 					newvalue = NULL;
494a3351425Sdougm 				} else {
495a3351425Sdougm 					/*
496a3351425Sdougm 					 * Make a new list of old plus new
497a3351425Sdougm 					 * access-list.
498a3351425Sdougm 					 */
499a3351425Sdougm 					newvalue = nfs_alistcat(oldvalue,
500a3351425Sdougm 					    value, ':');
501a3351425Sdougm 				}
502a3351425Sdougm 
503a3351425Sdougm 				if (newvalue != NULL) {
504a3351425Sdougm 					(void) sa_remove_property(prop);
505a3351425Sdougm 					prop = sa_create_property(name,
506a3351425Sdougm 					    newvalue);
507a3351425Sdougm 					ret = sa_add_property(sec->security,
508a3351425Sdougm 					    prop);
509a3351425Sdougm 					free(newvalue);
510a3351425Sdougm 				}
511a3351425Sdougm 				if (oldvalue != NULL)
512a3351425Sdougm 					sa_free_attr_string(oldvalue);
513a3351425Sdougm 			}
514a3351425Sdougm 		} else {
515a3351425Sdougm 			prop = sa_create_property(name, value);
516a99982a7Sdougm 			ret = sa_add_property(sec->security, prop);
5176185db85Sdougm 		}
518a3351425Sdougm 		if (ret == SA_OK && !iszfs) {
519a3351425Sdougm 			ret = sa_commit_properties(sec->security, !persist);
520a3351425Sdougm 		}
5216185db85Sdougm 	}
5226185db85Sdougm 	return (ret);
5236185db85Sdougm }
5246185db85Sdougm 
5256185db85Sdougm /*
5266185db85Sdougm  * check to see if group/share is persistent.
5276185db85Sdougm  */
5286185db85Sdougm static int
5296185db85Sdougm is_persistent(sa_group_t group)
5306185db85Sdougm {
5316185db85Sdougm 	char *type;
5326185db85Sdougm 	int persist = 1;
5336185db85Sdougm 
5346185db85Sdougm 	type = sa_get_group_attr(group, "type");
5356185db85Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
536a3351425Sdougm 		persist = 0;
5376185db85Sdougm 	if (type != NULL)
538a3351425Sdougm 		sa_free_attr_string(type);
5396185db85Sdougm 	return (persist);
5406185db85Sdougm }
5416185db85Sdougm 
5426185db85Sdougm /*
5436185db85Sdougm  * invalid_security(options)
5446185db85Sdougm  *
5456185db85Sdougm  * search option string for any invalid sec= type.
5466185db85Sdougm  * return true (1) if any are not valid else false (0)
5476185db85Sdougm  */
5486185db85Sdougm static int
5496185db85Sdougm invalid_security(char *options)
5506185db85Sdougm {
5516185db85Sdougm 	char *copy, *base, *token, *value;
5526185db85Sdougm 	int ret = 0;
5536185db85Sdougm 
5546185db85Sdougm 	copy = strdup(options);
5556185db85Sdougm 	token = base = copy;
5566185db85Sdougm 	while (token != NULL && ret == 0) {
557a3351425Sdougm 		token = strtok(base, ",");
558a3351425Sdougm 		base = NULL;
559a3351425Sdougm 		if (token != NULL) {
560a3351425Sdougm 			value = strchr(token, '=');
561a3351425Sdougm 			if (value != NULL)
562a3351425Sdougm 				*value++ = '\0';
563a3351425Sdougm 			if (strcmp(token, "sec") == 0) {
564a3351425Sdougm 				/* HAVE security flavors so check them */
565a3351425Sdougm 				char *tok, *next;
566a3351425Sdougm 				for (next = NULL, tok = value; tok != NULL;
567a3351425Sdougm 				    tok = next) {
568a3351425Sdougm 					next = strchr(tok, ':');
569a3351425Sdougm 					if (next != NULL)
570a3351425Sdougm 						*next++ = '\0';
571a3351425Sdougm 					ret = !nfs_validate_security_mode(tok);
572a3351425Sdougm 					if (ret)
573a3351425Sdougm 						break;
574a3351425Sdougm 				}
575a3351425Sdougm 			}
5766185db85Sdougm 		}
5776185db85Sdougm 	}
5786185db85Sdougm 	if (copy != NULL)
579a3351425Sdougm 		free(copy);
5806185db85Sdougm 	return (ret);
5816185db85Sdougm }
5826185db85Sdougm 
5836185db85Sdougm /*
5846185db85Sdougm  * nfs_parse_legacy_options(group, options)
5856185db85Sdougm  *
5866185db85Sdougm  * Parse the old style options into internal format and store on the
5876185db85Sdougm  * specified group.  Group could be a share for full legacy support.
5886185db85Sdougm  */
5896185db85Sdougm 
5906185db85Sdougm static int
5916185db85Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5926185db85Sdougm {
593aba7a40bSdougm 	char *dup;
5946185db85Sdougm 	char *base;
5956185db85Sdougm 	char *token;
5966185db85Sdougm 	sa_optionset_t optionset;
5976185db85Sdougm 	struct securities *security_list = NULL;
5986185db85Sdougm 	sa_property_t prop;
5996185db85Sdougm 	int ret = SA_OK;
6006185db85Sdougm 	int iszfs = 0;
6016185db85Sdougm 	sa_group_t parent;
6026185db85Sdougm 	int persist = 0;
6036185db85Sdougm 	char *lasts;
6046185db85Sdougm 
6056185db85Sdougm 	/* do we have an existing optionset? */
6066185db85Sdougm 	optionset = sa_get_optionset(group, "nfs");
6076185db85Sdougm 	if (optionset == NULL) {
608a3351425Sdougm 		/* didn't find existing optionset so create one */
609a3351425Sdougm 		optionset = sa_create_optionset(group, "nfs");
6106185db85Sdougm 	} else {
6116185db85Sdougm 		/*
612da6c28aaSamw 		 * Have an existing optionset . Ideally, we would need
613da6c28aaSamw 		 * to compare options in order to detect errors. For
614da6c28aaSamw 		 * now, we assume that the first optionset is the
615da6c28aaSamw 		 * correct one and the others will be the same. An
616da6c28aaSamw 		 * empty optionset is the same as no optionset so we
617da6c28aaSamw 		 * don't want to exit in that case. Getting an empty
618da6c28aaSamw 		 * optionset can occur with ZFS property checking.
6196185db85Sdougm 		 */
620da6c28aaSamw 		if (sa_get_property(optionset, NULL) != NULL)
621da6c28aaSamw 			return (ret);
6226185db85Sdougm 	}
6236185db85Sdougm 
6246185db85Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
6256185db85Sdougm 		/*
6266185db85Sdougm 		 * there is a special case of only the option "rw"
6276185db85Sdougm 		 * being the default option. We don't have to do
6286185db85Sdougm 		 * anything.
6296185db85Sdougm 		 */
630a3351425Sdougm 		return (ret);
6316185db85Sdougm 	}
6326185db85Sdougm 
6336185db85Sdougm 	/*
6346185db85Sdougm 	 * check if security types are present and validate them. If
6356185db85Sdougm 	 * any are not legal, fail.
6366185db85Sdougm 	 */
6376185db85Sdougm 
6386185db85Sdougm 	if (invalid_security(options)) {
639a3351425Sdougm 		return (SA_INVALID_SECURITY);
6406185db85Sdougm 	}
6416185db85Sdougm 
6426185db85Sdougm 	/*
6436185db85Sdougm 	 * in order to not attempt to change ZFS properties unless
6446185db85Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
6456185db85Sdougm 	 */
6466185db85Sdougm 	if (sa_is_share(group)) {
647a3351425Sdougm 		char *zfs;
648a3351425Sdougm 		parent = sa_get_parent_group(group);
649a3351425Sdougm 		if (parent != NULL) {
650a3351425Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
651a3351425Sdougm 			if (zfs != NULL) {
652a3351425Sdougm 				sa_free_attr_string(zfs);
653a3351425Sdougm 				iszfs++;
654a3351425Sdougm 			}
6556185db85Sdougm 		}
6566185db85Sdougm 	} else {
657a3351425Sdougm 		iszfs = sa_group_is_zfs(group);
6586185db85Sdougm 	}
6596185db85Sdougm 
660aba7a40bSdougm 	/* We need a copy of options for the next part. */
661aba7a40bSdougm 	dup = strdup(options);
662aba7a40bSdougm 	if (dup == NULL)
663aba7a40bSdougm 		return (SA_NO_MEMORY);
664aba7a40bSdougm 
6656185db85Sdougm 	/*
6666185db85Sdougm 	 * we need to step through each option in the string and then
6676185db85Sdougm 	 * add either the option or the security option as needed. If
6686185db85Sdougm 	 * this is not a persistent share, don't commit to the
669a99982a7Sdougm 	 * repository. If there is an error, we also want to abort the
670a99982a7Sdougm 	 * processing and report it.
6716185db85Sdougm 	 */
6726185db85Sdougm 	persist = is_persistent(group);
6736185db85Sdougm 	base = dup;
6746185db85Sdougm 	token = dup;
6756185db85Sdougm 	lasts = NULL;
676a99982a7Sdougm 	while (token != NULL && ret == SA_OK) {
677a3351425Sdougm 		ret = SA_OK;
678a3351425Sdougm 		token = strtok_r(base, ",", &lasts);
679a3351425Sdougm 		base = NULL;
680a3351425Sdougm 		if (token != NULL) {
681a3351425Sdougm 			char *value;
6826185db85Sdougm 			/*
683a3351425Sdougm 			 * if the option has a value, it will have an '=' to
684a3351425Sdougm 			 * separate the name from the value. The following
685a3351425Sdougm 			 * code will result in value != NULL and token
686a3351425Sdougm 			 * pointing to just the name if there is a value.
6876185db85Sdougm 			 */
688a3351425Sdougm 			value = strchr(token, '=');
689a3351425Sdougm 			if (value != NULL) {
690a3351425Sdougm 				*value++ = '\0';
6916185db85Sdougm 			}
692a3351425Sdougm 			if (strcmp(token, "sec") == 0 ||
693a3351425Sdougm 			    strcmp(token, "secure") == 0) {
6946185db85Sdougm 				/*
695a3351425Sdougm 				 * Once in security parsing, we only
696a3351425Sdougm 				 * do security. We do need to move
697a3351425Sdougm 				 * between the security node and the
698a3351425Sdougm 				 * toplevel. The security tag goes on
699a3351425Sdougm 				 * the root while the following ones
700a3351425Sdougm 				 * go on the security.
7016185db85Sdougm 				 */
702a3351425Sdougm 				if (security_list != NULL) {
703a3351425Sdougm 					/*
704a3351425Sdougm 					 * have an old list so close it and
705a3351425Sdougm 					 * start the new
706a3351425Sdougm 					 */
707a3351425Sdougm 					free_security_list(security_list);
708a3351425Sdougm 				}
709a3351425Sdougm 				if (strcmp(token, "secure") == 0) {
710a3351425Sdougm 					value = "dh";
711a3351425Sdougm 				} else {
712a3351425Sdougm 					if (value == NULL) {
713a3351425Sdougm 						ret = SA_SYNTAX_ERR;
714a3351425Sdougm 						break;
715a3351425Sdougm 					}
716a3351425Sdougm 				}
717a3351425Sdougm 				security_list = make_security_list(group,
718a3351425Sdougm 				    value, "nfs");
7196185db85Sdougm 			} else {
720a3351425Sdougm 				/*
721a3351425Sdougm 				 * Note that the "old" syntax allowed a
722a3351425Sdougm 				 * default security model This must be
723a3351425Sdougm 				 * accounted for and internally converted to
724a3351425Sdougm 				 * "standard" security structure.
725a3351425Sdougm 				 */
726a3351425Sdougm 				if (nfs_is_security_opt(token)) {
727a3351425Sdougm 					if (security_list == NULL) {
728a3351425Sdougm 						/*
729a3351425Sdougm 						 * need to have a
730a3351425Sdougm 						 * security
731a3351425Sdougm 						 * option. This will
732a3351425Sdougm 						 * be "closed" when a
733a3351425Sdougm 						 * defined "sec="
734a3351425Sdougm 						 * option is
735a3351425Sdougm 						 * seen. This is
736a3351425Sdougm 						 * technically an
737a3351425Sdougm 						 * error but will be
738a3351425Sdougm 						 * allowed with
739a3351425Sdougm 						 * warning.
740a3351425Sdougm 						 */
741a3351425Sdougm 						security_list =
742a3351425Sdougm 						    make_security_list(group,
743a3351425Sdougm 						    "default",
744a3351425Sdougm 						    "nfs");
745a3351425Sdougm 					}
746a3351425Sdougm 					if (security_list != NULL) {
747a3351425Sdougm 						ret = add_security_prop(
748a3351425Sdougm 						    security_list, token,
749a3351425Sdougm 						    value, persist, iszfs);
750a3351425Sdougm 					} else {
751a3351425Sdougm 						ret = SA_NO_MEMORY;
752a3351425Sdougm 					}
753a3351425Sdougm 				} else {
754a3351425Sdougm 					/* regular options */
755a3351425Sdougm 					if (value == NULL) {
756a3351425Sdougm 						if (strcmp(token, SHOPT_RW) ==
757a3351425Sdougm 						    0 || strcmp(token,
758a3351425Sdougm 						    SHOPT_RO) == 0) {
759a3351425Sdougm 							value = "*";
760a3351425Sdougm 						} else {
761a3351425Sdougm 							value = "global";
762a3351425Sdougm 							if (strcmp(token,
763a3351425Sdougm 							    SHOPT_LOG) != 0) {
764a3351425Sdougm 								value = "true";
765a3351425Sdougm 							}
766a3351425Sdougm 						}
767a3351425Sdougm 					}
768d6405362Sdougm 					/*
769d6405362Sdougm 					 * In all cases, create the
770d6405362Sdougm 					 * property specified. If the
771d6405362Sdougm 					 * value was NULL, the default
772d6405362Sdougm 					 * value will have been
773d6405362Sdougm 					 * substituted.
774d6405362Sdougm 					 */
775d6405362Sdougm 					prop = sa_create_property(token, value);
776d6405362Sdougm 					ret =  sa_add_property(optionset, prop);
777d6405362Sdougm 					if (ret != SA_OK)
778d6405362Sdougm 						break;
779d6405362Sdougm 
780a3351425Sdougm 					if (!iszfs) {
781a3351425Sdougm 						ret = sa_commit_properties(
782a3351425Sdougm 						    optionset, !persist);
783a3351425Sdougm 					}
784a3351425Sdougm 				}
7856185db85Sdougm 			}
7866185db85Sdougm 		}
7876185db85Sdougm 	}
7886185db85Sdougm 	if (security_list != NULL)
789a3351425Sdougm 		free_security_list(security_list);
790aba7a40bSdougm 
791aba7a40bSdougm 	free(dup);
7926185db85Sdougm 	return (ret);
7936185db85Sdougm }
7946185db85Sdougm 
7956185db85Sdougm /*
7966185db85Sdougm  * is_a_number(number)
7976185db85Sdougm  *
7986185db85Sdougm  * is the string a number in one of the forms we want to use?
7996185db85Sdougm  */
8006185db85Sdougm 
8016185db85Sdougm static int
8026185db85Sdougm is_a_number(char *number)
8036185db85Sdougm {
8046185db85Sdougm 	int ret = 1;
8056185db85Sdougm 	int hex = 0;
8066185db85Sdougm 
8076185db85Sdougm 	if (strncmp(number, "0x", 2) == 0) {
808a3351425Sdougm 		number += 2;
809a3351425Sdougm 		hex = 1;
810a3351425Sdougm 	} else if (*number == '-') {
811a3351425Sdougm 		number++; /* skip the minus */
812a3351425Sdougm 	}
8136185db85Sdougm 	while (ret == 1 && *number != '\0') {
814a3351425Sdougm 		if (hex) {
815a3351425Sdougm 			ret = isxdigit(*number++);
816a3351425Sdougm 		} else {
817a3351425Sdougm 			ret = isdigit(*number++);
818a3351425Sdougm 		}
8196185db85Sdougm 	}
8206185db85Sdougm 	return (ret);
8216185db85Sdougm }
8226185db85Sdougm 
8236185db85Sdougm /*
8246185db85Sdougm  * Look for the specified tag in the configuration file. If it is found,
8256185db85Sdougm  * enable logging and set the logging configuration information for exp.
8266185db85Sdougm  */
8276185db85Sdougm static void
8286185db85Sdougm configlog(struct exportdata *exp, char *tag)
8296185db85Sdougm {
8306185db85Sdougm 	nfsl_config_t *configlist = NULL, *configp;
8316185db85Sdougm 	int error = 0;
8326185db85Sdougm 	char globaltag[] = DEFAULTTAG;
8336185db85Sdougm 
8346185db85Sdougm 	/*
8356185db85Sdougm 	 * Sends config errors to stderr
8366185db85Sdougm 	 */
8376185db85Sdougm 	nfsl_errs_to_syslog = B_FALSE;
8386185db85Sdougm 
8396185db85Sdougm 	/*
8406185db85Sdougm 	 * get the list of configuration settings
8416185db85Sdougm 	 */
8426185db85Sdougm 	error = nfsl_getconfig_list(&configlist);
8436185db85Sdougm 	if (error) {
8446185db85Sdougm 		(void) fprintf(stderr,
845a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
846a3351425Sdougm 		    strerror(error));
8476185db85Sdougm 	}
8486185db85Sdougm 
8496185db85Sdougm 	if (tag == NULL)
8506185db85Sdougm 		tag = globaltag;
8516185db85Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
8526185db85Sdougm 		nfsl_freeconfig_list(&configlist);
8536185db85Sdougm 		(void) fprintf(stderr,
854a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
8556185db85Sdougm 		/* bad configuration */
8566185db85Sdougm 		error = ENOENT;
8576185db85Sdougm 		goto err;
8586185db85Sdougm 	}
8596185db85Sdougm 
8606185db85Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
8616185db85Sdougm 		error = ENOMEM;
8626185db85Sdougm 		goto out;
8636185db85Sdougm 	}
8646185db85Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
8656185db85Sdougm 		error = ENOMEM;
8666185db85Sdougm 		goto out;
8676185db85Sdougm 	}
8686185db85Sdougm 	exp->ex_flags |= EX_LOG;
8696185db85Sdougm 	if (configp->nc_rpclogpath != NULL)
8706185db85Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8716185db85Sdougm out:
8726185db85Sdougm 	if (configlist != NULL)
873a3351425Sdougm 		nfsl_freeconfig_list(&configlist);
8746185db85Sdougm 
8756185db85Sdougm err:
8766185db85Sdougm 	if (error != 0) {
8776185db85Sdougm 		if (exp->ex_flags != NULL)
8786185db85Sdougm 			free(exp->ex_tag);
8796185db85Sdougm 		if (exp->ex_log_buffer != NULL)
8806185db85Sdougm 			free(exp->ex_log_buffer);
8816185db85Sdougm 		(void) fprintf(stderr,
882a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
883a3351425Sdougm 		    strerror(error));
8846185db85Sdougm 	}
8856185db85Sdougm }
8866185db85Sdougm 
8876185db85Sdougm /*
8886185db85Sdougm  * fill_export_from_optionset(export, optionset)
8896185db85Sdougm  *
8906185db85Sdougm  * In order to share, we need to set all the possible general options
8916185db85Sdougm  * into the export structure. Share info will be filled in by the
8926185db85Sdougm  * caller. Various property values get turned into structure specific
8936185db85Sdougm  * values.
8946185db85Sdougm  */
8956185db85Sdougm 
8966185db85Sdougm static int
8976185db85Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
8986185db85Sdougm {
8996185db85Sdougm 	sa_property_t option;
9006185db85Sdougm 	int ret = SA_OK;
9016185db85Sdougm 
9026185db85Sdougm 	for (option = sa_get_property(optionset, NULL);
903a3351425Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
904a3351425Sdougm 		char *name;
905a3351425Sdougm 		char *value;
906a3351425Sdougm 		uint32_t val;
9076185db85Sdougm 
908a3351425Sdougm 		/*
909a3351425Sdougm 		 * since options may be set/reset multiple times, always do an
910a3351425Sdougm 		 * explicit set or clear of the option. This allows defaults
911da6c28aaSamw 		 * to be set and then the protocol specific to override.
912a3351425Sdougm 		 */
9136185db85Sdougm 
914a3351425Sdougm 		name = sa_get_property_attr(option, "type");
915a3351425Sdougm 		value = sa_get_property_attr(option, "value");
916a3351425Sdougm 		switch (findopt(name)) {
917a3351425Sdougm 		case OPT_ANON:
918a3351425Sdougm 			if (value != NULL && is_a_number(value)) {
919a3351425Sdougm 				val = strtoul(value, NULL, 0);
920a3351425Sdougm 			} else {
921a3351425Sdougm 				struct passwd *pw;
922a3351425Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
923a3351425Sdougm 				if (pw != NULL) {
924a3351425Sdougm 					val = pw->pw_uid;
925a3351425Sdougm 				} else {
926a3351425Sdougm 					val = UID_NOBODY;
927a3351425Sdougm 				}
928a3351425Sdougm 				endpwent();
929a3351425Sdougm 			}
930a3351425Sdougm 			export->ex_anon = val;
931a3351425Sdougm 			break;
932a3351425Sdougm 		case OPT_NOSUID:
933a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
934a3351425Sdougm 			    strcmp(value, "1") == 0))
935a3351425Sdougm 				export->ex_flags |= EX_NOSUID;
936a3351425Sdougm 			else
937a3351425Sdougm 				export->ex_flags &= ~EX_NOSUID;
938a3351425Sdougm 			break;
939a3351425Sdougm 		case OPT_ACLOK:
940a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
941a3351425Sdougm 			    strcmp(value, "1") == 0))
942a3351425Sdougm 				export->ex_flags |= EX_ACLOK;
943a3351425Sdougm 			else
944a3351425Sdougm 				export->ex_flags &= ~EX_ACLOK;
945a3351425Sdougm 			break;
946a3351425Sdougm 		case OPT_NOSUB:
947a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
948a3351425Sdougm 			    strcmp(value, "1") == 0))
949a3351425Sdougm 				export->ex_flags |= EX_NOSUB;
950a3351425Sdougm 			else
951a3351425Sdougm 				export->ex_flags &= ~EX_NOSUB;
952a3351425Sdougm 			break;
953a3351425Sdougm 		case OPT_PUBLIC:
954a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
955a3351425Sdougm 			    strcmp(value, "1") == 0))
956a3351425Sdougm 				export->ex_flags |= EX_PUBLIC;
957a3351425Sdougm 			else
958a3351425Sdougm 				export->ex_flags &= ~EX_PUBLIC;
959a3351425Sdougm 			break;
960a3351425Sdougm 		case OPT_INDEX:
961a3351425Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
962a3351425Sdougm 			    strchr(value, '/') != NULL)) {
963a3351425Sdougm 				/* this is an error */
964a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
965a3351425Sdougm 				    "NFS: index=\"%s\" not valid;"
966a3351425Sdougm 				    "must be a filename.\n"),
967a3351425Sdougm 				    value);
968a3351425Sdougm 				break;
969a3351425Sdougm 			}
970a3351425Sdougm 			if (value != NULL && *value != '\0' &&
971a3351425Sdougm 			    strcmp(value, ".") != 0) {
972a3351425Sdougm 				/* valid index file string */
973a3351425Sdougm 				if (export->ex_index != NULL) {
974a3351425Sdougm 					/* left over from "default" */
975a3351425Sdougm 					free(export->ex_index);
976a3351425Sdougm 				}
977a3351425Sdougm 				/* remember to free */
978a3351425Sdougm 				export->ex_index = strdup(value);
979a3351425Sdougm 				if (export->ex_index == NULL) {
980a3351425Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
981a3351425Sdougm 					    "NFS: out of memory setting "
982a3351425Sdougm 					    "index property\n"));
983a3351425Sdougm 					break;
984a3351425Sdougm 				}
985a3351425Sdougm 				export->ex_flags |= EX_INDEX;
986a3351425Sdougm 			}
987a3351425Sdougm 			break;
988a3351425Sdougm 		case OPT_LOG:
989a3351425Sdougm 			if (value == NULL)
990a3351425Sdougm 				value = strdup("global");
991a3351425Sdougm 			if (value != NULL)
992a3351425Sdougm 				configlog(export,
993a3351425Sdougm 				    strlen(value) ? value : "global");
994a3351425Sdougm 			break;
995*b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_CHARSET_MAP:
996*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			/*
997*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * Set EX_CHARMAP when there is at least one
998*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * charmap conversion property. This will get
999*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * checked by the nfs server when it needs to.
1000*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 */
1001*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			export->ex_flags |= EX_CHARMAP;
1002*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
1003a3351425Sdougm 		default:
1004a3351425Sdougm 			/* have a syntactic error */
1005549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1006a3351425Sdougm 			    "NFS: unrecognized option %s=%s\n"),
1007a3351425Sdougm 			    name, value != NULL ? value : "");
10086185db85Sdougm 			break;
10096185db85Sdougm 		}
1010a3351425Sdougm 		if (name != NULL)
1011a3351425Sdougm 			sa_free_attr_string(name);
10126185db85Sdougm 		if (value != NULL)
1013a3351425Sdougm 			sa_free_attr_string(value);
10146185db85Sdougm 	}
10156185db85Sdougm 	return (ret);
10166185db85Sdougm }
10176185db85Sdougm 
10186185db85Sdougm /*
10196185db85Sdougm  * cleanup_export(export)
10206185db85Sdougm  *
10216185db85Sdougm  * Cleanup the allocated areas so we don't leak memory
10226185db85Sdougm  */
10236185db85Sdougm 
10246185db85Sdougm static void
10256185db85Sdougm cleanup_export(struct exportdata *export)
10266185db85Sdougm {
10276185db85Sdougm 	int i;
10286185db85Sdougm 
10296185db85Sdougm 	if (export->ex_index != NULL)
1030a3351425Sdougm 		free(export->ex_index);
10316185db85Sdougm 	if (export->ex_secinfo != NULL) {
1032a3351425Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
1033a3351425Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
1034a3351425Sdougm 				free(export->ex_secinfo[i].s_rootnames);
1035a3351425Sdougm 		free(export->ex_secinfo);
10366185db85Sdougm 	}
10376185db85Sdougm }
10386185db85Sdougm 
10396185db85Sdougm /*
10406185db85Sdougm  * Given a seconfig entry and a colon-separated
10416185db85Sdougm  * list of names, allocate an array big enough
10426185db85Sdougm  * to hold the root list, then convert each name to
10436185db85Sdougm  * a principal name according to the security
10446185db85Sdougm  * info and assign it to an array element.
10456185db85Sdougm  * Return the array and its size.
10466185db85Sdougm  */
10476185db85Sdougm static caddr_t *
10486185db85Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
10496185db85Sdougm {
10506185db85Sdougm 	caddr_t *a;
10516185db85Sdougm 	int c, i;
10526185db85Sdougm 	char *host, *p;
10536185db85Sdougm 
10546185db85Sdougm 	/*
10556185db85Sdougm 	 * Count the number of strings in the list.
10566185db85Sdougm 	 * This is the number of colon separators + 1.
10576185db85Sdougm 	 */
10586185db85Sdougm 	c = 1;
10596185db85Sdougm 	for (p = list; *p; p++)
10606185db85Sdougm 		if (*p == ':')
10616185db85Sdougm 			c++;
10626185db85Sdougm 	*count = c;
10636185db85Sdougm 
10646185db85Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
10656185db85Sdougm 	if (a == NULL) {
1066549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1067a3351425Sdougm 		    "get_rootnames: no memory\n"));
10686185db85Sdougm 	} else {
1069a3351425Sdougm 		for (i = 0; i < c; i++) {
1070a3351425Sdougm 			host = strtok(list, ":");
1071a3351425Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
1072a3351425Sdougm 				free(a);
1073a3351425Sdougm 				a = NULL;
1074a3351425Sdougm 				break;
1075a3351425Sdougm 			}
1076a3351425Sdougm 			list = NULL;
10776185db85Sdougm 		}
10786185db85Sdougm 	}
10796185db85Sdougm 
10806185db85Sdougm 	return (a);
10816185db85Sdougm }
10826185db85Sdougm 
10836185db85Sdougm /*
10846185db85Sdougm  * fill_security_from_secopts(sp, secopts)
10856185db85Sdougm  *
10866185db85Sdougm  * Fill the secinfo structure from the secopts optionset.
10876185db85Sdougm  */
10886185db85Sdougm 
10896185db85Sdougm static int
10906185db85Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
10916185db85Sdougm {
10926185db85Sdougm 	sa_property_t prop;
10936185db85Sdougm 	char *type;
10946185db85Sdougm 	int longform;
1095a99982a7Sdougm 	int err = SC_NOERROR;
1096*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	uint32_t val;
10976185db85Sdougm 
10986185db85Sdougm 	type = sa_get_security_attr(secopts, "sectype");
10996185db85Sdougm 	if (type != NULL) {
1100a3351425Sdougm 		/* named security type needs secinfo to be filled in */
1101a3351425Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
1102a3351425Sdougm 		sa_free_attr_string(type);
1103a3351425Sdougm 		if (err != SC_NOERROR)
1104a3351425Sdougm 			return (err);
11056185db85Sdougm 	} else {
1106a3351425Sdougm 		/* default case */
1107a3351425Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
1108a3351425Sdougm 		if (err != SC_NOERROR)
1109a3351425Sdougm 			return (err);
11106185db85Sdougm 	}
11116185db85Sdougm 
1112a99982a7Sdougm 	err = SA_OK;
11136185db85Sdougm 	for (prop = sa_get_property(secopts, NULL);
1114a3351425Sdougm 	    prop != NULL && err == SA_OK;
1115a3351425Sdougm 	    prop = sa_get_next_property(prop)) {
1116a3351425Sdougm 		char *name;
1117a3351425Sdougm 		char *value;
11186185db85Sdougm 
1119a3351425Sdougm 		name = sa_get_property_attr(prop, "type");
1120a3351425Sdougm 		value = sa_get_property_attr(prop, "value");
11216185db85Sdougm 
1122a3351425Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
11236185db85Sdougm 
1124a3351425Sdougm 		switch (findopt(name)) {
1125a3351425Sdougm 		case OPT_RO:
1126a3351425Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
1127a3351425Sdougm 			break;
1128a3351425Sdougm 		case OPT_RW:
1129a3351425Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
1130a3351425Sdougm 			break;
1131a3351425Sdougm 		case OPT_ROOT:
1132a3351425Sdougm 			sp->s_flags |= M_ROOT;
1133a3351425Sdougm 			/*
1134a3351425Sdougm 			 * if we are using AUTH_UNIX, handle like other things
1135a3351425Sdougm 			 * such as RO/RW
1136a3351425Sdougm 			 */
1137a3351425Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
1138a3351425Sdougm 				continue;
1139a3351425Sdougm 			/* not AUTH_UNIX */
1140a3351425Sdougm 			if (value != NULL) {
1141a3351425Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
1142a3351425Sdougm 				    value, &sp->s_rootcnt);
1143a3351425Sdougm 				if (sp->s_rootnames == NULL) {
1144a3351425Sdougm 					err = SA_BAD_VALUE;
1145a3351425Sdougm 					(void) fprintf(stderr,
1146a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
1147a3351425Sdougm 					    "Bad root list\n"));
1148a3351425Sdougm 				}
1149a3351425Sdougm 			}
1150a3351425Sdougm 			break;
1151*b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_NONE:
1152*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			sp->s_flags |= M_NONE;
1153*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
1154a3351425Sdougm 		case OPT_WINDOW:
1155a3351425Sdougm 			if (value != NULL) {
1156a3351425Sdougm 				sp->s_window = atoi(value);
1157a3351425Sdougm 				/* just in case */
1158a3351425Sdougm 				if (sp->s_window < 0)
1159a3351425Sdougm 					sp->s_window = DEF_WIN;
1160a3351425Sdougm 			}
1161a3351425Sdougm 			break;
1162*b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_ROOT_MAPPING:
1163*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (value != NULL && is_a_number(value)) {
1164*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				val = strtoul(value, NULL, 0);
1165*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			} else {
1166*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				struct passwd *pw;
1167*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				pw = getpwnam(value != NULL ? value : "nobody");
1168*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (pw != NULL) {
1169*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					val = pw->pw_uid;
1170*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else {
1171*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					val = UID_NOBODY;
1172*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				}
1173*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				endpwent();
1174*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			}
1175*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			sp->s_rootid = val;
1176*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
1177a3351425Sdougm 		default:
1178a3351425Sdougm 			break;
11796185db85Sdougm 		}
1180a3351425Sdougm 		if (name != NULL)
1181a3351425Sdougm 			sa_free_attr_string(name);
1182a3351425Sdougm 		if (value != NULL)
1183a3351425Sdougm 			sa_free_attr_string(value);
11846185db85Sdougm 	}
11856185db85Sdougm 	/* if rw/ro options not set, use default of RW */
11866185db85Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
1187a3351425Sdougm 		sp->s_flags |= M_RW;
11886185db85Sdougm 	return (err);
11896185db85Sdougm }
11906185db85Sdougm 
11916185db85Sdougm /*
11926185db85Sdougm  * This is for testing only
11936185db85Sdougm  * It displays the export structure that
11946185db85Sdougm  * goes into the kernel.
11956185db85Sdougm  */
11966185db85Sdougm static void
11976185db85Sdougm printarg(char *path, struct exportdata *ep)
11986185db85Sdougm {
11996185db85Sdougm 	int i, j;
12006185db85Sdougm 	struct secinfo *sp;
12016185db85Sdougm 
12026185db85Sdougm 	if (debug == 0)
1203a3351425Sdougm 		return;
12046185db85Sdougm 
12056185db85Sdougm 	(void) printf("%s:\n", path);
12066185db85Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
12076185db85Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
1208549ec3ffSdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
12096185db85Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
12106185db85Sdougm 	if (ep->ex_flags & EX_NOSUID)
12116185db85Sdougm 		(void) printf("NOSUID ");
12126185db85Sdougm 	if (ep->ex_flags & EX_ACLOK)
12136185db85Sdougm 		(void) printf("ACLOK ");
12146185db85Sdougm 	if (ep->ex_flags & EX_PUBLIC)
12156185db85Sdougm 		(void) printf("PUBLIC ");
12166185db85Sdougm 	if (ep->ex_flags & EX_NOSUB)
12176185db85Sdougm 		(void) printf("NOSUB ");
12186185db85Sdougm 	if (ep->ex_flags & EX_LOG)
12196185db85Sdougm 		(void) printf("LOG ");
1220*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (ep->ex_flags & EX_CHARMAP)
1221*b89a8333Snatalie li - Sun Microsystems - Irvine United States 		(void) printf("CHARMAP ");
12226185db85Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
12236185db85Sdougm 		(void) printf("LOG_ALLOPS ");
12246185db85Sdougm 	if (ep->ex_flags == 0)
12256185db85Sdougm 		(void) printf("(none)");
12266185db85Sdougm 	(void) 	printf("\n");
12276185db85Sdougm 	if (ep->ex_flags & EX_LOG) {
12286185db85Sdougm 		(void) printf("\tex_log_buffer = %s\n",
1229a3351425Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
12306185db85Sdougm 		(void) printf("\tex_tag = %s\n",
1231a3351425Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
12326185db85Sdougm 	}
12336185db85Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
12346185db85Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
12356185db85Sdougm 	(void) printf("\n");
12366185db85Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
12376185db85Sdougm 		sp = &ep->ex_secinfo[i];
12386185db85Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
12396185db85Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
12406185db85Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
12416185db85Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
12426185db85Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
12436185db85Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
12446185db85Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
1245*b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (sp->s_flags & M_NONE) (void) printf("M_NONE ");
12466185db85Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
12476185db85Sdougm 		(void) printf("\n");
12486185db85Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
1249*b89a8333Snatalie li - Sun Microsystems - Irvine United States 		(void) printf("\t\ts_rootid = %d\n", sp->s_rootid);
12506185db85Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
12516185db85Sdougm 		(void) fflush(stdout);
12526185db85Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
12536185db85Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
1254a3351425Sdougm 			    sp->s_rootnames[j] : "<null>");
12556185db85Sdougm 		(void) printf("\n\n");
12566185db85Sdougm 	}
12576185db85Sdougm }
12586185db85Sdougm 
12596185db85Sdougm /*
12606185db85Sdougm  * count_security(opts)
12616185db85Sdougm  *
12626185db85Sdougm  * Count the number of security types (flavors). The optionset has
12636185db85Sdougm  * been populated with the security flavors as a holding mechanism.
12646185db85Sdougm  * We later use this number to allocate data structures.
12656185db85Sdougm  */
12666185db85Sdougm 
12676185db85Sdougm static int
12686185db85Sdougm count_security(sa_optionset_t opts)
12696185db85Sdougm {
12706185db85Sdougm 	int count = 0;
12716185db85Sdougm 	sa_property_t prop;
12726185db85Sdougm 	if (opts != NULL) {
1273a3351425Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
1274a3351425Sdougm 		    prop = sa_get_next_property(prop)) {
1275a3351425Sdougm 			count++;
1276a3351425Sdougm 		}
12776185db85Sdougm 	}
12786185db85Sdougm 	return (count);
12796185db85Sdougm }
12806185db85Sdougm 
12816185db85Sdougm /*
12826185db85Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
12836185db85Sdougm  *
12846185db85Sdougm  * provides a mechanism to format NFS properties into legacy output
12856185db85Sdougm  * format. If the buffer would overflow, it is reallocated and grown
12866185db85Sdougm  * as appropriate. Special cases of converting internal form of values
12876185db85Sdougm  * to those used by "share" are done. this function does one property
12886185db85Sdougm  * at a time.
12896185db85Sdougm  */
12906185db85Sdougm 
1291aed5d200Sdougm static int
12926185db85Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
12936185db85Sdougm 			sa_property_t prop, int sep)
12946185db85Sdougm {
12956185db85Sdougm 	char *name;
12966185db85Sdougm 	char *value;
12976185db85Sdougm 	int curlen;
12986185db85Sdougm 	char *buff = *rbuff;
12996185db85Sdougm 	size_t buffsize = *rbuffsize;
1300aed5d200Sdougm 	int printed = B_FALSE;
13016185db85Sdougm 
13026185db85Sdougm 	name = sa_get_property_attr(prop, "type");
13036185db85Sdougm 	value = sa_get_property_attr(prop, "value");
13046185db85Sdougm 	if (buff != NULL)
1305a3351425Sdougm 		curlen = strlen(buff);
13066185db85Sdougm 	else
1307a3351425Sdougm 		curlen = 0;
13086185db85Sdougm 	if (name != NULL) {
1309a3351425Sdougm 		int len;
1310a3351425Sdougm 		len = strlen(name) + sep;
13116185db85Sdougm 
13126185db85Sdougm 		/*
13136185db85Sdougm 		 * A future RFE would be to replace this with more
13146185db85Sdougm 		 * generic code and to possibly handle more types.
13156185db85Sdougm 		 */
1316a3351425Sdougm 		switch (gettype(name)) {
1317a3351425Sdougm 		case OPT_TYPE_BOOLEAN:
1318aed5d200Sdougm 			/*
1319aed5d200Sdougm 			 * For NFS, boolean value of FALSE means it
1320aed5d200Sdougm 			 * doesn't show up in the option list at all.
1321aed5d200Sdougm 			 */
1322a3351425Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
1323aed5d200Sdougm 				goto skip;
1324aed5d200Sdougm 			if (value != NULL) {
1325a3351425Sdougm 				sa_free_attr_string(value);
1326aed5d200Sdougm 				value = NULL;
1327aed5d200Sdougm 			}
1328a3351425Sdougm 			break;
1329a3351425Sdougm 		case OPT_TYPE_ACCLIST:
1330a3351425Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
1331a3351425Sdougm 				sa_free_attr_string(value);
1332a3351425Sdougm 				value = NULL;
1333a3351425Sdougm 			} else {
1334a3351425Sdougm 				if (value != NULL)
1335a3351425Sdougm 					len += 1 + strlen(value);
1336a3351425Sdougm 			}
1337a3351425Sdougm 			break;
1338a3351425Sdougm 		case OPT_TYPE_LOGTAG:
1339a3351425Sdougm 			if (value != NULL && strlen(value) == 0) {
1340a3351425Sdougm 				sa_free_attr_string(value);
1341a3351425Sdougm 				value = NULL;
1342a3351425Sdougm 			} else {
1343a3351425Sdougm 				if (value != NULL)
1344a3351425Sdougm 					len += 1 + strlen(value);
1345a3351425Sdougm 			}
1346a3351425Sdougm 			break;
1347a3351425Sdougm 		default:
1348a3351425Sdougm 			if (value != NULL)
1349a3351425Sdougm 				len += 1 + strlen(value);
1350a3351425Sdougm 			break;
13516185db85Sdougm 		}
1352a3351425Sdougm 		while (buffsize <= (curlen + len)) {
1353a3351425Sdougm 			/* need more room */
1354a3351425Sdougm 			buffsize += incr;
1355a3351425Sdougm 			buff = realloc(buff, buffsize);
1356a3351425Sdougm 			if (buff == NULL) {
1357a3351425Sdougm 				/* realloc failed so free everything */
1358a3351425Sdougm 				if (*rbuff != NULL)
1359a3351425Sdougm 					free(*rbuff);
1360a3351425Sdougm 			}
1361a3351425Sdougm 			*rbuff = buff;
1362a3351425Sdougm 			*rbuffsize = buffsize;
1363aed5d200Sdougm 			if (buff == NULL)
1364aed5d200Sdougm 				goto skip;
1365aed5d200Sdougm 
13666185db85Sdougm 		}
1367aed5d200Sdougm 
1368a3351425Sdougm 		if (buff == NULL)
1369aed5d200Sdougm 			goto skip;
1370aed5d200Sdougm 
1371a3351425Sdougm 		if (value == NULL) {
1372a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1373a3351425Sdougm 			    "%s%s", sep ? "," : "",
1374a3351425Sdougm 			    name, value != NULL ? value : "");
13756185db85Sdougm 		} else {
1376a3351425Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1377a3351425Sdougm 			    "%s%s=%s", sep ? "," : "",
1378a3351425Sdougm 			    name, value != NULL ? value : "");
13796185db85Sdougm 		}
1380aed5d200Sdougm 		printed = B_TRUE;
13816185db85Sdougm 	}
1382aed5d200Sdougm skip:
13836185db85Sdougm 	if (name != NULL)
1384a3351425Sdougm 		sa_free_attr_string(name);
13856185db85Sdougm 	if (value != NULL)
1386a3351425Sdougm 		sa_free_attr_string(value);
1387aed5d200Sdougm 	return (printed);
13886185db85Sdougm }
13896185db85Sdougm 
13906185db85Sdougm /*
13916185db85Sdougm  * nfs_format_options(group, hier)
13926185db85Sdougm  *
13936185db85Sdougm  * format all the options on the group into an old-style option
13946185db85Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
13956185db85Sdougm  * options.
13966185db85Sdougm  */
13976185db85Sdougm 
13986185db85Sdougm static char *
13996185db85Sdougm nfs_format_options(sa_group_t group, int hier)
14006185db85Sdougm {
14016185db85Sdougm 	sa_optionset_t options = NULL;
1402a3351425Sdougm 	sa_optionset_t secoptions = NULL;
14036185db85Sdougm 	sa_property_t prop, secprop;
1404a3351425Sdougm 	sa_security_t security = NULL;
14056185db85Sdougm 	char *buff;
14066185db85Sdougm 	size_t buffsize;
1407a3351425Sdougm 	char *sectype = NULL;
1408a3351425Sdougm 	int sep = 0;
1409a3351425Sdougm 
1410a3351425Sdougm 
1411a3351425Sdougm 	buff = malloc(OPT_CHUNK);
1412a3351425Sdougm 	if (buff == NULL) {
1413a3351425Sdougm 		return (NULL);
1414a3351425Sdougm 	}
1415a3351425Sdougm 
1416a3351425Sdougm 	buff[0] = '\0';
1417a3351425Sdougm 	buffsize = OPT_CHUNK;
1418a3351425Sdougm 
1419a3351425Sdougm 	/*
1420a3351425Sdougm 	 * We may have a an optionset relative to this item. format
1421a3351425Sdougm 	 * these if we find them and then add any security definitions.
1422a3351425Sdougm 	 */
14236185db85Sdougm 
14246185db85Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
14256185db85Sdougm 
14266185db85Sdougm 	/*
1427a3351425Sdougm 	 * do the default set first but skip any option that is also
1428a3351425Sdougm 	 * in the protocol specific optionset.
14296185db85Sdougm 	 */
1430a3351425Sdougm 	if (options != NULL) {
1431a3351425Sdougm 		for (prop = sa_get_property(options, NULL);
1432a3351425Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
14336185db85Sdougm 			/*
1434a3351425Sdougm 			 * use this one since we skipped any
1435a3351425Sdougm 			 * of these that were also in
1436a3351425Sdougm 			 * optdefault
14376185db85Sdougm 			 */
1438aed5d200Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1439aed5d200Sdougm 			    prop, sep))
1440aed5d200Sdougm 				sep = 1;
1441a3351425Sdougm 			if (buff == NULL) {
1442a3351425Sdougm 				/*
1443a3351425Sdougm 				 * buff could become NULL if there
1444a3351425Sdougm 				 * isn't enough memory for
1445a3351425Sdougm 				 * nfs_sprint_option to realloc()
1446a3351425Sdougm 				 * as necessary. We can't really
1447a3351425Sdougm 				 * do anything about it at this
1448a3351425Sdougm 				 * point so we return NULL.  The
1449a3351425Sdougm 				 * caller should handle the
1450a3351425Sdougm 				 * failure.
1451a3351425Sdougm 				 */
14526185db85Sdougm 				if (options != NULL)
1453a3351425Sdougm 					sa_free_derived_optionset(
1454a3351425Sdougm 					    options);
14556185db85Sdougm 				return (buff);
14566185db85Sdougm 			}
1457a3351425Sdougm 		}
1458a3351425Sdougm 	}
1459a3351425Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1460a3351425Sdougm 	    "nfs", hier);
1461a3351425Sdougm 	if (secoptions != NULL) {
1462a3351425Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
1463a3351425Sdougm 		    secprop != NULL;
1464a3351425Sdougm 		    secprop = sa_get_next_property(secprop)) {
1465a3351425Sdougm 			sectype = sa_get_property_attr(secprop, "type");
1466a3351425Sdougm 			security =
1467a3351425Sdougm 			    (sa_security_t)sa_get_derived_security(
1468a3351425Sdougm 			    group, sectype, "nfs", hier);
1469a3351425Sdougm 			if (security != NULL) {
1470a3351425Sdougm 				if (sectype != NULL) {
1471a3351425Sdougm 					prop = sa_create_property(
1472a3351425Sdougm 					    "sec", sectype);
1473aed5d200Sdougm 					if (prop == NULL)
1474aed5d200Sdougm 						goto err;
1475aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1476aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1477aed5d200Sdougm 						sep = 1;
1478a3351425Sdougm 					(void) sa_remove_property(prop);
1479aed5d200Sdougm 					if (buff == NULL)
1480aed5d200Sdougm 						goto err;
1481a3351425Sdougm 				}
1482a3351425Sdougm 				for (prop = sa_get_property(security,
1483a3351425Sdougm 				    NULL); prop != NULL;
1484a3351425Sdougm 				    prop = sa_get_next_property(prop)) {
1485aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1486aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1487aed5d200Sdougm 						sep = 1;
1488a3351425Sdougm 					if (buff == NULL)
1489a3351425Sdougm 						goto err;
1490a3351425Sdougm 				}
1491a3351425Sdougm 				sa_free_derived_optionset(security);
1492a3351425Sdougm 			}
1493a3351425Sdougm 			if (sectype != NULL)
1494a3351425Sdougm 				sa_free_attr_string(sectype);
14956185db85Sdougm 		}
14966185db85Sdougm 		sa_free_derived_optionset(secoptions);
14976185db85Sdougm 	}
1498a3351425Sdougm 
14996185db85Sdougm 	if (options != NULL)
1500a3351425Sdougm 		sa_free_derived_optionset(options);
1501a3351425Sdougm 	return (buff);
1502a3351425Sdougm 
1503a3351425Sdougm err:
1504a3351425Sdougm 	/*
1505a3351425Sdougm 	 * If we couldn't allocate memory for option printing, we need
1506a3351425Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
1507a3351425Sdougm 	 */
1508a3351425Sdougm 	if (secoptions != NULL)
1509a3351425Sdougm 		sa_free_derived_optionset(secoptions);
1510a3351425Sdougm 	if (security != NULL)
1511a3351425Sdougm 		sa_free_derived_optionset(security);
1512a3351425Sdougm 	if (sectype != NULL)
1513a3351425Sdougm 		sa_free_attr_string(sectype);
1514a3351425Sdougm 	if (options != NULL)
1515a3351425Sdougm 		sa_free_derived_optionset(options);
15166185db85Sdougm 	return (buff);
15176185db85Sdougm }
1518a3351425Sdougm 
15196185db85Sdougm /*
15206185db85Sdougm  * Append an entry to the nfslogtab file
15216185db85Sdougm  */
15226185db85Sdougm static int
15236185db85Sdougm nfslogtab_add(dir, buffer, tag)
15246185db85Sdougm 	char *dir, *buffer, *tag;
15256185db85Sdougm {
15266185db85Sdougm 	FILE *f;
15276185db85Sdougm 	struct logtab_ent lep;
15286185db85Sdougm 	int error = 0;
15296185db85Sdougm 
15306185db85Sdougm 	/*
15316185db85Sdougm 	 * Open the file for update and create it if necessary.
15326185db85Sdougm 	 * This may leave the I/O offset at the end of the file,
15336185db85Sdougm 	 * so rewind back to the beginning of the file.
15346185db85Sdougm 	 */
15356185db85Sdougm 	f = fopen(NFSLOGTAB, "a+");
15366185db85Sdougm 	if (f == NULL) {
15376185db85Sdougm 		error = errno;
15386185db85Sdougm 		goto out;
15396185db85Sdougm 	}
15406185db85Sdougm 	rewind(f);
15416185db85Sdougm 
15426185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1543549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1544a3351425Sdougm 		    "share complete, however failed to lock %s "
1545a3351425Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
15466185db85Sdougm 		error = -1;
15476185db85Sdougm 		goto out;
15486185db85Sdougm 	}
15496185db85Sdougm 
15506185db85Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
1551549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1552a3351425Sdougm 		    "share complete, however could not deactivate "
1553a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15546185db85Sdougm 		error = -1;
15556185db85Sdougm 		goto out;
15566185db85Sdougm 	}
15576185db85Sdougm 
15586185db85Sdougm 	/*
15596185db85Sdougm 	 * Remove entries matching buffer and sharepoint since we're
15606185db85Sdougm 	 * going to replace it with perhaps an entry with a new tag.
15616185db85Sdougm 	 */
15626185db85Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
1563549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1564a3351425Sdougm 		    "share complete, however could not remove matching "
1565a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15666185db85Sdougm 		error = -1;
15676185db85Sdougm 		goto out;
15686185db85Sdougm 	}
15696185db85Sdougm 
15706185db85Sdougm 	/*
15716185db85Sdougm 	 * Deactivate all active entries matching this sharepoint
15726185db85Sdougm 	 */
15736185db85Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
1574549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1575a3351425Sdougm 		    "share complete, however could not deactivate matching "
1576a3351425Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15776185db85Sdougm 		error = -1;
15786185db85Sdougm 		goto out;
15796185db85Sdougm 	}
15806185db85Sdougm 
15816185db85Sdougm 	lep.le_buffer = buffer;
15826185db85Sdougm 	lep.le_path = dir;
15836185db85Sdougm 	lep.le_tag = tag;
15846185db85Sdougm 	lep.le_state = LES_ACTIVE;
15856185db85Sdougm 
15866185db85Sdougm 	/*
15876185db85Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
15886185db85Sdougm 	 */
15896185db85Sdougm 	if (logtab_putent(f, &lep) < 0) {
1590549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1591a3351425Sdougm 		    "share complete, however could not add %s to %s\n"),
1592a3351425Sdougm 		    dir, NFSLOGTAB);
15936185db85Sdougm 		error = -1;
15946185db85Sdougm 	}
15956185db85Sdougm 
15966185db85Sdougm out:
15976185db85Sdougm 	if (f != NULL)
15986185db85Sdougm 		(void) fclose(f);
15996185db85Sdougm 	return (error);
16006185db85Sdougm }
16016185db85Sdougm 
16026185db85Sdougm /*
16036185db85Sdougm  * Deactivate an entry from the nfslogtab file
16046185db85Sdougm  */
16056185db85Sdougm static int
16066185db85Sdougm nfslogtab_deactivate(path)
16076185db85Sdougm 	char *path;
16086185db85Sdougm {
16096185db85Sdougm 	FILE *f;
16106185db85Sdougm 	int error = 0;
16116185db85Sdougm 
16126185db85Sdougm 	f = fopen(NFSLOGTAB, "r+");
16136185db85Sdougm 	if (f == NULL) {
16146185db85Sdougm 		error = errno;
16156185db85Sdougm 		goto out;
16166185db85Sdougm 	}
16176185db85Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
16186185db85Sdougm 		error = errno;
1619549ec3ffSdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
1620a3351425Sdougm 		    "share complete, however could not lock %s for "
1621a3351425Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
16226185db85Sdougm 		goto out;
16236185db85Sdougm 	}
16246185db85Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
16256185db85Sdougm 		error = -1;
16266185db85Sdougm 		(void) fprintf(stderr,
1627a3351425Sdougm 		    dgettext(TEXT_DOMAIN,
1628a3351425Sdougm 		    "share complete, however could not "
1629a3351425Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
16306185db85Sdougm 		goto out;
16316185db85Sdougm 	}
16326185db85Sdougm 
16336185db85Sdougm out:	if (f != NULL)
16346185db85Sdougm 		(void) fclose(f);
16356185db85Sdougm 
16366185db85Sdougm 	return (error);
16376185db85Sdougm }
16386185db85Sdougm 
1639546405c3Sdougm /*
1640546405c3Sdougm  * check_public(group, skipshare)
1641546405c3Sdougm  *
1642546405c3Sdougm  * Check the group for any shares that have the public property
1643546405c3Sdougm  * enabled. We skip "skipshare" since that is the one we are
1644546405c3Sdougm  * working with. This is a separate function to make handling
1645546405c3Sdougm  * subgroups simpler. Returns true if there is a share with public.
1646546405c3Sdougm  */
1647546405c3Sdougm static int
1648546405c3Sdougm check_public(sa_group_t group, sa_share_t skipshare)
1649546405c3Sdougm {
1650546405c3Sdougm 	int exists = B_FALSE;
1651546405c3Sdougm 	sa_share_t share;
1652546405c3Sdougm 	sa_optionset_t opt;
1653546405c3Sdougm 	sa_property_t prop;
1654546405c3Sdougm 	char *shared;
1655546405c3Sdougm 
1656546405c3Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
1657546405c3Sdougm 	    share = sa_get_next_share(share)) {
1658546405c3Sdougm 		if (share == skipshare)
1659546405c3Sdougm 			continue;
1660546405c3Sdougm 
1661546405c3Sdougm 		opt = sa_get_optionset(share, "nfs");
1662546405c3Sdougm 		if (opt == NULL)
1663546405c3Sdougm 			continue;
1664546405c3Sdougm 		prop = sa_get_property(opt, "public");
1665546405c3Sdougm 		if (prop == NULL)
1666546405c3Sdougm 			continue;
1667546405c3Sdougm 		shared = sa_get_share_attr(share, "shared");
1668546405c3Sdougm 		if (shared != NULL) {
1669546405c3Sdougm 			exists = strcmp(shared, "true") == 0;
1670546405c3Sdougm 			sa_free_attr_string(shared);
1671546405c3Sdougm 			if (exists == B_TRUE)
1672546405c3Sdougm 				break;
1673546405c3Sdougm 		}
1674546405c3Sdougm 	}
1675546405c3Sdougm 
1676546405c3Sdougm 	return (exists);
1677546405c3Sdougm }
1678546405c3Sdougm 
16796185db85Sdougm /*
1680687915e9Sdougm  * public_exists(handle, share)
16816185db85Sdougm  *
16826185db85Sdougm  * check to see if public option is set on any other share than the
1683546405c3Sdougm  * one specified. Need to check zfs sub-groups as well as the top
1684546405c3Sdougm  * level groups.
16856185db85Sdougm  */
16866185db85Sdougm static int
1687687915e9Sdougm public_exists(sa_handle_t handle, sa_share_t skipshare)
16886185db85Sdougm {
1689687915e9Sdougm 	sa_group_t group = NULL;
1690549ec3ffSdougm 
16913b61b335Sdougm 	/*
16923b61b335Sdougm 	 * If we don't have a handle, we can only do syntax check. We
16933b61b335Sdougm 	 * can't check against other shares so we assume OK and will
16943b61b335Sdougm 	 * catch the problem only when we actually try to apply it.
16953b61b335Sdougm 	 */
1696549ec3ffSdougm 	if (handle == NULL)
16973b61b335Sdougm 		return (SA_OK);
1698549ec3ffSdougm 
1699687915e9Sdougm 	if (skipshare != NULL) {
1700687915e9Sdougm 		group = sa_get_parent_group(skipshare);
1701687915e9Sdougm 		if (group == NULL)
1702687915e9Sdougm 			return (SA_NO_SUCH_GROUP);
1703687915e9Sdougm 	}
1704687915e9Sdougm 
1705549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
17066185db85Sdougm 	    group = sa_get_next_group(group)) {
1707546405c3Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
1708546405c3Sdougm 		if (sa_group_is_zfs(group)) {
1709546405c3Sdougm 			sa_group_t subgroup;
1710546405c3Sdougm 			for (subgroup = sa_get_sub_group(group);
1711546405c3Sdougm 			    subgroup != NULL;
1712546405c3Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
1713546405c3Sdougm 				if (check_public(subgroup, skipshare))
1714546405c3Sdougm 					return (B_TRUE);
17156185db85Sdougm 			}
1716546405c3Sdougm 		} else {
1717546405c3Sdougm 			if (check_public(group, skipshare))
1718546405c3Sdougm 				return (B_TRUE);
17196185db85Sdougm 		}
17206185db85Sdougm 	}
1721546405c3Sdougm 	return (B_FALSE);
17226185db85Sdougm }
17236185db85Sdougm 
17246185db85Sdougm /*
17256185db85Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
17266185db85Sdougm  * implementation that it is to enable the share. This entails
17276185db85Sdougm  * converting the path and options into the appropriate ioctl
17286185db85Sdougm  * calls. It is assumed that all error checking of paths, etc. were
17296185db85Sdougm  * done earlier.
17306185db85Sdougm  */
17316185db85Sdougm static int
17326185db85Sdougm nfs_enable_share(sa_share_t share)
17336185db85Sdougm {
17346185db85Sdougm 	struct exportdata export;
17356185db85Sdougm 	sa_optionset_t secoptlist;
17366185db85Sdougm 	struct secinfo *sp;
17376185db85Sdougm 	int num_secinfo;
17386185db85Sdougm 	sa_optionset_t opt;
17396185db85Sdougm 	sa_security_t sec;
17406185db85Sdougm 	sa_property_t prop;
17416185db85Sdougm 	char *path;
17426185db85Sdougm 	int err = SA_OK;
1743546405c3Sdougm 	int i;
1744ecd6cf80Smarks 	int iszfs;
1745687915e9Sdougm 	sa_handle_t handle;
17466185db85Sdougm 
17476185db85Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
17486185db85Sdougm 	(void) signal(SIGSYS, SIG_IGN);
17496185db85Sdougm 
17506185db85Sdougm 	/* get the path since it is important in several places */
17516185db85Sdougm 	path = sa_get_share_attr(share, "path");
17526185db85Sdougm 	if (path == NULL)
1753a3351425Sdougm 		return (SA_NO_SUCH_PATH);
17546185db85Sdougm 
1755ecd6cf80Smarks 	iszfs = sa_path_is_zfs(path);
17566185db85Sdougm 	/*
17576185db85Sdougm 	 * find the optionsets and security sets.  There may not be
17586185db85Sdougm 	 * any or there could be one or two for each of optionset and
17596185db85Sdougm 	 * security may have multiple, one per security type per
17606185db85Sdougm 	 * protocol type.
17616185db85Sdougm 	 */
17626185db85Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
17636185db85Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
17646185db85Sdougm 	if (secoptlist != NULL)
1765a3351425Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
17666185db85Sdougm 	else
1767a3351425Sdougm 		num_secinfo = 1;
17686185db85Sdougm 
17696185db85Sdougm 	/*
17706185db85Sdougm 	 * walk through the options and fill in the structure
17716185db85Sdougm 	 * appropriately.
17726185db85Sdougm 	 */
17736185db85Sdougm 
17746185db85Sdougm 	(void) memset(&export, '\0', sizeof (export));
17756185db85Sdougm 
17766185db85Sdougm 	/*
17776185db85Sdougm 	 * do non-security options first since there is only one after
17786185db85Sdougm 	 * the derived group is constructed.
17796185db85Sdougm 	 */
17806185db85Sdougm 	export.ex_version = EX_CURRENT_VERSION;
17816185db85Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
17826185db85Sdougm 	export.ex_index = NULL;
17836185db85Sdougm 	export.ex_path = path;
17846185db85Sdougm 	export.ex_pathlen = strlen(path) + 1;
17856185db85Sdougm 
17866185db85Sdougm 	if (opt != NULL)
1787a3351425Sdougm 		err = fill_export_from_optionset(&export, opt);
17886185db85Sdougm 
17896185db85Sdougm 	/*
17906185db85Sdougm 	 * check to see if "public" is set. If it is, then make sure
17916185db85Sdougm 	 * no other share has it set. If it is already used, fail.
17926185db85Sdougm 	 */
17936185db85Sdougm 
1794687915e9Sdougm 	handle = sa_find_group_handle((sa_group_t)share);
1795687915e9Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
1796a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1797a3351425Sdougm 		    "NFS: Cannot share more than one file "
1798a3351425Sdougm 		    "system with 'public' property\n"));
1799a3351425Sdougm 		err = SA_NOT_ALLOWED;
1800a3351425Sdougm 		goto out;
18016185db85Sdougm 	}
18026185db85Sdougm 
1803546405c3Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
18046185db85Sdougm 	if (sp == NULL) {
1805a3351425Sdougm 		err = SA_NO_MEMORY;
1806546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1807546405c3Sdougm 		    "NFS: NFS: no memory for security\n"));
1808546405c3Sdougm 		goto out;
1809546405c3Sdougm 	}
1810546405c3Sdougm 	export.ex_secinfo = sp;
1811546405c3Sdougm 	/* get default secinfo */
1812546405c3Sdougm 	export.ex_seccnt = num_secinfo;
1813546405c3Sdougm 	/*
1814546405c3Sdougm 	 * since we must have one security option defined, we
1815546405c3Sdougm 	 * init to the default and then override as we find
1816546405c3Sdougm 	 * defined security options. This handles the case
1817546405c3Sdougm 	 * where we have no defined options but we need to set
1818546405c3Sdougm 	 * up one.
1819546405c3Sdougm 	 */
1820546405c3Sdougm 	sp[0].s_window = DEF_WIN;
1821546405c3Sdougm 	sp[0].s_rootnames = NULL;
1822546405c3Sdougm 	/* setup a default in case no properties defined */
1823546405c3Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1824546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1825546405c3Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
1826546405c3Sdougm 		    "get default security mode\n"));
1827546405c3Sdougm 		err = SA_CONFIG_ERR;
1828546405c3Sdougm 	}
1829546405c3Sdougm 	if (secoptlist != NULL) {
1830546405c3Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
1831546405c3Sdougm 		    prop != NULL && i < num_secinfo;
1832546405c3Sdougm 		    prop = sa_get_next_property(prop), i++) {
1833546405c3Sdougm 			char *sectype;
1834a3351425Sdougm 				sectype = sa_get_property_attr(prop, "type");
1835546405c3Sdougm 			/*
1836546405c3Sdougm 			 * if sectype is NULL, we probably
1837546405c3Sdougm 			 * have a memory problem and can't get
1838546405c3Sdougm 			 * the correct values. Rather than
1839546405c3Sdougm 			 * exporting with incorrect security,
1840546405c3Sdougm 			 * don't share it.
1841546405c3Sdougm 			 */
1842546405c3Sdougm 			if (sectype == NULL) {
1843546405c3Sdougm 				err = SA_NO_MEMORY;
1844546405c3Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1845546405c3Sdougm 				    "NFS: Cannot share %s: "
1846546405c3Sdougm 				    "no memory\n"), path);
1847546405c3Sdougm 				goto out;
1848a3351425Sdougm 			}
1849546405c3Sdougm 			sec = (sa_security_t)sa_get_derived_security(
1850546405c3Sdougm 			    share, sectype, "nfs", 1);
1851546405c3Sdougm 			sp[i].s_window = DEF_WIN;
1852546405c3Sdougm 			sp[i].s_rootcnt = 0;
1853546405c3Sdougm 			sp[i].s_rootnames = NULL;
1854546405c3Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
1855546405c3Sdougm 			if (sec != NULL)
1856546405c3Sdougm 				sa_free_derived_security(sec);
1857546405c3Sdougm 			if (sectype != NULL)
1858546405c3Sdougm 				sa_free_attr_string(sectype);
18596185db85Sdougm 		}
1860546405c3Sdougm 	}
1861546405c3Sdougm 	/*
1862546405c3Sdougm 	 * when we get here, we can do the exportfs system call and
1863546405c3Sdougm 	 * initiate thinsg. We probably want to enable the nfs.server
1864546405c3Sdougm 	 * service first if it isn't running within SMF.
1865546405c3Sdougm 	 */
1866546405c3Sdougm 	/* check nfs.server status and start if needed */
1867546405c3Sdougm 	/* now add the share to the internal tables */
1868546405c3Sdougm 	printarg(path, &export);
1869546405c3Sdougm 	/*
1870546405c3Sdougm 	 * call the exportfs system call which is implemented
1871546405c3Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
1872546405c3Sdougm 	 */
1873ecd6cf80Smarks 	if (iszfs) {
1874ecd6cf80Smarks 		struct exportfs_args ea;
1875ecd6cf80Smarks 		share_t sh;
1876ecd6cf80Smarks 		char *str;
1877ecd6cf80Smarks 		priv_set_t *priv_effective;
1878ecd6cf80Smarks 		int privileged;
1879ecd6cf80Smarks 
1880ecd6cf80Smarks 		/*
1881ecd6cf80Smarks 		 * If we aren't a privileged user
1882ecd6cf80Smarks 		 * and NFS server service isn't running
1883ecd6cf80Smarks 		 * then print out an error message
1884ecd6cf80Smarks 		 * and return EPERM
1885ecd6cf80Smarks 		 */
1886ecd6cf80Smarks 
1887ecd6cf80Smarks 		priv_effective = priv_allocset();
1888ecd6cf80Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
1889ecd6cf80Smarks 
1890ecd6cf80Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
1891ecd6cf80Smarks 		priv_freeset(priv_effective);
1892ecd6cf80Smarks 
1893ecd6cf80Smarks 		if (!privileged &&
1894ecd6cf80Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
1895ecd6cf80Smarks 			err = 0;
1896ecd6cf80Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
1897ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1898ecd6cf80Smarks 				    "NFS: Cannot share remote "
1899ecd6cf80Smarks 				    "filesystem: %s\n"), path);
1900ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1901ecd6cf80Smarks 				    "NFS: Service needs to be enabled "
1902ecd6cf80Smarks 				    "by a privileged user\n"));
1903ecd6cf80Smarks 				err = SA_SYSTEM_ERR;
1904ecd6cf80Smarks 				errno = EPERM;
1905ecd6cf80Smarks 			}
1906ecd6cf80Smarks 			free(str);
1907ecd6cf80Smarks 		}
1908ecd6cf80Smarks 
1909ecd6cf80Smarks 		if (err == 0) {
1910ecd6cf80Smarks 			ea.dname = path;
1911ecd6cf80Smarks 			ea.uex = &export;
1912ecd6cf80Smarks 
1913ecd6cf80Smarks 			sa_sharetab_fill_zfs(share, &sh, "nfs");
1914da6c28aaSamw 			err = sa_share_zfs(share, path, &sh,
1915da6c28aaSamw 			    &ea, ZFS_SHARE_NFS);
1916ecd6cf80Smarks 			sa_emptyshare(&sh);
1917ecd6cf80Smarks 		}
1918ecd6cf80Smarks 	} else {
1919ecd6cf80Smarks 		err = exportfs(path, &export);
1920ecd6cf80Smarks 	}
1921ecd6cf80Smarks 
1922ecd6cf80Smarks 	if (err < 0) {
1923546405c3Sdougm 		err = SA_SYSTEM_ERR;
1924546405c3Sdougm 		switch (errno) {
1925546405c3Sdougm 		case EREMOTE:
1926546405c3Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1927ecd6cf80Smarks 			    "NFS: Cannot share filesystems "
1928ecd6cf80Smarks 			    "in non-global zones: %s\n"), path);
1929ecd6cf80Smarks 			err = SA_NOT_SUPPORTED;
1930546405c3Sdougm 			break;
1931546405c3Sdougm 		case EPERM:
1932546405c3Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
1933a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1934ecd6cf80Smarks 				    "NFS: Cannot share file systems "
1935546405c3Sdougm 				    "in non-global zones: %s\n"), path);
1936546405c3Sdougm 				err = SA_NOT_SUPPORTED;
1937a3351425Sdougm 				break;
1938a3351425Sdougm 			}
1939546405c3Sdougm 			err = SA_NO_PERMISSION;
1940546405c3Sdougm 			/* FALLTHROUGH */
1941546405c3Sdougm 		default:
1942546405c3Sdougm 			break;
19436185db85Sdougm 		}
1944546405c3Sdougm 	} else {
1945546405c3Sdougm 		/* update sharetab with an add/modify */
1946ecd6cf80Smarks 		if (!iszfs) {
1947ecd6cf80Smarks 			(void) sa_update_sharetab(share, "nfs");
1948ecd6cf80Smarks 		}
19496185db85Sdougm 	}
19506185db85Sdougm 
19516185db85Sdougm 	if (err == SA_OK) {
19526185db85Sdougm 		/*
19536185db85Sdougm 		 * enable services as needed. This should probably be
19546185db85Sdougm 		 * done elsewhere in order to minimize the calls to
19556185db85Sdougm 		 * check services.
19566185db85Sdougm 		 */
19576185db85Sdougm 		/*
19586185db85Sdougm 		 * check to see if logging and other services need to
19596185db85Sdougm 		 * be triggered, but only if there wasn't an
19606185db85Sdougm 		 * error. This is probably where sharetab should be
19616185db85Sdougm 		 * updated with the NFS specific entry.
19626185db85Sdougm 		 */
1963a3351425Sdougm 		if (export.ex_flags & EX_LOG) {
1964a3351425Sdougm 			/* enable logging */
1965a3351425Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
1966a3351425Sdougm 			    export.ex_tag) != 0) {
1967a3351425Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1968a3351425Sdougm 				    "Could not enable logging for %s\n"),
1969a3351425Sdougm 				    path);
1970a3351425Sdougm 			}
1971a3351425Sdougm 			_check_services(service_list_logging);
1972a3351425Sdougm 		} else {
1973a3351425Sdougm 			/*
1974a3351425Sdougm 			 * don't have logging so remove it from file. It might
1975a3351425Sdougm 			 * not be thre, but that doesn't matter.
1976a3351425Sdougm 			 */
1977a3351425Sdougm 			(void) nfslogtab_deactivate(path);
1978a3351425Sdougm 			_check_services(service_list_default);
19796185db85Sdougm 		}
19806185db85Sdougm 	}
19816185db85Sdougm 
19826185db85Sdougm out:
19836185db85Sdougm 	if (path != NULL)
1984a3351425Sdougm 		free(path);
19856185db85Sdougm 
19866185db85Sdougm 	cleanup_export(&export);
19876185db85Sdougm 	if (opt != NULL)
1988a3351425Sdougm 		sa_free_derived_optionset(opt);
19896185db85Sdougm 	if (secoptlist != NULL)
1990a3351425Sdougm 		(void) sa_destroy_optionset(secoptlist);
19916185db85Sdougm 	return (err);
19926185db85Sdougm }
19936185db85Sdougm 
19946185db85Sdougm /*
19958e314a44Sdougm  * nfs_disable_share(share, path)
19966185db85Sdougm  *
19978e314a44Sdougm  * Unshare the specified share. Note that "path" is the same path as
19988e314a44Sdougm  * what is in the "share" object. It is passed in to avoid an
19998e314a44Sdougm  * additional lookup. A missing "path" value makes this a no-op
20008e314a44Sdougm  * function.
20016185db85Sdougm  */
20026185db85Sdougm static int
2003ecd6cf80Smarks nfs_disable_share(sa_share_t share, char *path)
20046185db85Sdougm {
20056185db85Sdougm 	int err;
20066185db85Sdougm 	int ret = SA_OK;
2007ecd6cf80Smarks 	int iszfs;
20088e314a44Sdougm 	sa_group_t parent;
20095b6e0c46Sdougm 	sa_handle_t handle;
2010ecd6cf80Smarks 
20118e314a44Sdougm 	if (path == NULL)
20128e314a44Sdougm 		return (ret);
2013ecd6cf80Smarks 
20148e314a44Sdougm 	/*
20158e314a44Sdougm 	 * If the share is in a ZFS group we need to handle it
20168e314a44Sdougm 	 * differently.  Just being on a ZFS file system isn't
20178e314a44Sdougm 	 * enough since we may be in a legacy share case.
20188e314a44Sdougm 	 */
20198e314a44Sdougm 	parent = sa_get_parent_group(share);
20208e314a44Sdougm 	iszfs = sa_group_is_zfs(parent);
20218e314a44Sdougm 	if (iszfs) {
20228e314a44Sdougm 		struct exportfs_args ea;
20238e314a44Sdougm 		share_t sh = { 0 };
20248e314a44Sdougm 		ea.dname = path;
20258e314a44Sdougm 		ea.uex = NULL;
20268e314a44Sdougm 		sh.sh_path = path;
20278e314a44Sdougm 		sh.sh_fstype = "nfs";
20288e314a44Sdougm 
20298e314a44Sdougm 		err = sa_share_zfs(share, path, &sh,
20308e314a44Sdougm 		    &ea, ZFS_UNSHARE_NFS);
20318e314a44Sdougm 	} else {
20328e314a44Sdougm 		err = exportfs(path, NULL);
20338e314a44Sdougm 	}
20348e314a44Sdougm 	if (err < 0) {
20358e314a44Sdougm 		/*
20368e314a44Sdougm 		 * TBD: only an error in some
20378e314a44Sdougm 		 * cases - need better analysis
20388e314a44Sdougm 		 */
20398e314a44Sdougm 		switch (errno) {
20408e314a44Sdougm 		case EPERM:
20418e314a44Sdougm 		case EACCES:
20428e314a44Sdougm 			ret = SA_NO_PERMISSION;
20438e314a44Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
20448e314a44Sdougm 				ret = SA_NOT_SUPPORTED;
20458e314a44Sdougm 			}
20468e314a44Sdougm 			break;
20478e314a44Sdougm 		case EINVAL:
20488e314a44Sdougm 		case ENOENT:
20498e314a44Sdougm 			ret = SA_NO_SUCH_PATH;
2050ecd6cf80Smarks 			break;
2051a3351425Sdougm 			default:
2052a3351425Sdougm 				ret = SA_SYSTEM_ERR;
2053ecd6cf80Smarks 			break;
20546185db85Sdougm 		}
20556185db85Sdougm 	}
20568e314a44Sdougm 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
20575b6e0c46Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
20588e314a44Sdougm 		if (!iszfs)
20595b6e0c46Sdougm 			(void) sa_delete_sharetab(handle, path, "nfs");
20608e314a44Sdougm 		/* just in case it was logged */
20618e314a44Sdougm 		(void) nfslogtab_deactivate(path);
20628e314a44Sdougm 	}
20636185db85Sdougm 	return (ret);
20646185db85Sdougm }
20656185db85Sdougm 
20666185db85Sdougm /*
2067*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * check_rorwnone(v1, v2, v3)
2068*b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
2069*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * check ro vs rw vs none values.  Over time this may get beefed up.
2070*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * for now it just does simple checks. v1 is never NULL but v2 or v3
2071*b89a8333Snatalie li - Sun Microsystems - Irvine United States  * could be.
20726185db85Sdougm  */
20736185db85Sdougm 
20746185db85Sdougm static int
2075*b89a8333Snatalie li - Sun Microsystems - Irvine United States check_rorwnone(char *v1, char *v2, char *v3)
20766185db85Sdougm {
20776185db85Sdougm 	int ret = SA_OK;
2078*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (v2 != NULL && strcmp(v1, v2) == 0)
2079*b89a8333Snatalie li - Sun Microsystems - Irvine United States 		ret = SA_VALUE_CONFLICT;
2080*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	else if (v3 != NULL && strcmp(v1, v3) == 0)
2081a3351425Sdougm 		ret = SA_VALUE_CONFLICT;
2082*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
20836185db85Sdougm 	return (ret);
20846185db85Sdougm }
20856185db85Sdougm 
20866185db85Sdougm /*
2087687915e9Sdougm  * nfs_validate_property(handle, property, parent)
20886185db85Sdougm  *
20896185db85Sdougm  * Check that the property has a legitimate value for its type.
20906185db85Sdougm  */
20916185db85Sdougm 
20926185db85Sdougm static int
2093687915e9Sdougm nfs_validate_property(sa_handle_t handle, sa_property_t property,
2094687915e9Sdougm     sa_optionset_t parent)
20956185db85Sdougm {
20966185db85Sdougm 	int ret = SA_OK;
20976185db85Sdougm 	char *propname;
2098*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *other1;
2099*b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *other2;
21006185db85Sdougm 	int optindex;
21016185db85Sdougm 	nfsl_config_t *configlist;
21026185db85Sdougm 	sa_group_t parent_group;
21036185db85Sdougm 	char *value;
21046185db85Sdougm 
21056185db85Sdougm 	propname = sa_get_property_attr(property, "type");
21066185db85Sdougm 
21076185db85Sdougm 	if ((optindex = findopt(propname)) < 0)
2108a3351425Sdougm 		ret = SA_NO_SUCH_PROP;
21096185db85Sdougm 
21106185db85Sdougm 	/* need to validate value range here as well */
21116185db85Sdougm 
21126185db85Sdougm 	if (ret == SA_OK) {
2113a3351425Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
2114687915e9Sdougm 		if (optdefs[optindex].share && parent_group != NULL &&
2115687915e9Sdougm 		    !sa_is_share(parent_group))
2116a3351425Sdougm 			ret = SA_PROP_SHARE_ONLY;
21176185db85Sdougm 	}
21186185db85Sdougm 	if (ret == SA_OK) {
2119687915e9Sdougm 		if (optdefs[optindex].index == OPT_PUBLIC) {
2120687915e9Sdougm 			/*
2121687915e9Sdougm 			 * Public is special in that only one instance can
2122687915e9Sdougm 			 * be in the repository at the same time.
2123687915e9Sdougm 			 */
2124687915e9Sdougm 			if (public_exists(handle, parent_group)) {
2125*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_free_attr_string(propname);
2126687915e9Sdougm 				return (SA_VALUE_CONFLICT);
2127687915e9Sdougm 			}
2128687915e9Sdougm 		}
2129a3351425Sdougm 		value = sa_get_property_attr(property, "value");
2130a3351425Sdougm 		if (value != NULL) {
2131a3351425Sdougm 			/* first basic type checking */
2132a3351425Sdougm 			switch (optdefs[optindex].type) {
2133a3351425Sdougm 			case OPT_TYPE_NUMBER:
2134a3351425Sdougm 				/* check that the value is all digits */
2135a3351425Sdougm 				if (!is_a_number(value))
2136a3351425Sdougm 					ret = SA_BAD_VALUE;
2137a3351425Sdougm 				break;
2138a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2139a3351425Sdougm 				if (strlen(value) == 0 ||
2140a3351425Sdougm 				    strcasecmp(value, "true") == 0 ||
2141a3351425Sdougm 				    strcmp(value, "1") == 0 ||
2142a3351425Sdougm 				    strcasecmp(value, "false") == 0 ||
2143a3351425Sdougm 				    strcmp(value, "0") == 0) {
2144a3351425Sdougm 					ret = SA_OK;
2145a3351425Sdougm 				} else {
2146a3351425Sdougm 					ret = SA_BAD_VALUE;
2147a3351425Sdougm 				}
2148a3351425Sdougm 				break;
2149a3351425Sdougm 			case OPT_TYPE_USER:
2150a3351425Sdougm 				if (!is_a_number(value)) {
2151a3351425Sdougm 					struct passwd *pw;
2152a3351425Sdougm 					/*
2153a3351425Sdougm 					 * in this case it would have to be a
2154a3351425Sdougm 					 * user name
2155a3351425Sdougm 					 */
2156a3351425Sdougm 					pw = getpwnam(value);
2157a3351425Sdougm 					if (pw == NULL)
2158a3351425Sdougm 						ret = SA_BAD_VALUE;
2159a3351425Sdougm 					endpwent();
2160a3351425Sdougm 				} else {
2161a3351425Sdougm 					uint64_t intval;
2162a3351425Sdougm 					intval = strtoull(value, NULL, 0);
2163a3351425Sdougm 					if (intval > UID_MAX && intval != ~0)
2164a3351425Sdougm 						ret = SA_BAD_VALUE;
2165a3351425Sdougm 				}
2166a3351425Sdougm 				break;
2167a3351425Sdougm 			case OPT_TYPE_FILE:
2168a3351425Sdougm 				if (strcmp(value, "..") == 0 ||
2169a3351425Sdougm 				    strchr(value, '/') != NULL) {
2170a3351425Sdougm 					ret = SA_BAD_VALUE;
2171a3351425Sdougm 				}
2172a3351425Sdougm 				break;
2173*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			case OPT_TYPE_ACCLIST: {
2174*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_property_t oprop1;
2175*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_property_t oprop2;
2176*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				char *ovalue1 = NULL;
2177*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				char *ovalue2 = NULL;
2178*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2179*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (parent == NULL)
2180*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2181a3351425Sdougm 				/*
2182a3351425Sdougm 				 * access list handling. Should eventually
2183a3351425Sdougm 				 * validate that all the values make sense.
2184a3351425Sdougm 				 * Also, ro and rw may have cross value
2185a3351425Sdougm 				 * conflicts.
2186a3351425Sdougm 				 */
2187*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (strcmp(propname, SHOPT_RO) == 0) {
2188*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RW;
2189*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_NONE;
2190*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else if (strcmp(propname, SHOPT_RW) == 0) {
2191*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RO;
2192*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_NONE;
2193*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else if (strcmp(propname, SHOPT_NONE) == 0) {
2194*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RO;
2195*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_RW;
2196*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else {
2197*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = NULL;
2198*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = NULL;
2199a3351425Sdougm 				}
2200*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (other1 == NULL && other2 == NULL)
2201*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2202*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2203*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				/* compare rw(ro) with ro(rw) */
2204*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2205*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				oprop1 = sa_get_property(parent, other1);
2206*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				oprop2 = sa_get_property(parent, other2);
2207*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (oprop1 == NULL && oprop2 == NULL)
2208*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2209*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				/*
2210*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * Only potential confusion if other1
2211*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * or other2 exists. Check the values
2212*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * and run the check if there is a
2213*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * value other than the one we are
2214*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * explicitly looking at.
2215*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 */
2216*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				ovalue1 = sa_get_property_attr(oprop1, "value");
2217*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				ovalue2 = sa_get_property_attr(oprop2, "value");
2218*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue1 != NULL || ovalue2 != NULL)
2219*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					ret = check_rorwnone(value, ovalue1,
2220*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					    ovalue2);
2221*b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2222*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue1 != NULL)
2223*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					sa_free_attr_string(ovalue1);
2224*b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue2 != NULL)
2225*b89a8333Snatalie li - Sun Microsystems - Irvine United States 					sa_free_attr_string(ovalue2);
2226a3351425Sdougm 				break;
2227*b89a8333Snatalie li - Sun Microsystems - Irvine United States 			}
2228a3351425Sdougm 			case OPT_TYPE_LOGTAG:
2229a3351425Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
2230a3351425Sdougm 					int error;
2231a3351425Sdougm 					if (value == NULL ||
2232a3351425Sdougm 					    strlen(value) == 0) {
2233a3351425Sdougm 						if (value != NULL)
2234a3351425Sdougm 							sa_free_attr_string(
2235a3351425Sdougm 							    value);
2236a3351425Sdougm 						value = strdup("global");
2237a3351425Sdougm 					}
2238a3351425Sdougm 					if (value != NULL &&
2239a3351425Sdougm 					    nfsl_findconfig(configlist, value,
2240a3351425Sdougm 					    &error) == NULL) {
2241a3351425Sdougm 						ret = SA_BAD_VALUE;
2242a3351425Sdougm 					}
2243aed5d200Sdougm 					/* Must always free when done */
2244aed5d200Sdougm 					nfsl_freeconfig_list(&configlist);
2245a3351425Sdougm 				} else {
2246a3351425Sdougm 					ret = SA_CONFIG_ERR;
2247a3351425Sdougm 				}
2248a3351425Sdougm 				break;
2249a3351425Sdougm 			case OPT_TYPE_STRING:
2250a3351425Sdougm 				/* whatever is here should be ok */
2251a3351425Sdougm 				break;
2252a3351425Sdougm 			case OPT_TYPE_SECURITY:
2253a3351425Sdougm 				/*
2254a3351425Sdougm 				 * The "sec" property isn't used in the
2255a3351425Sdougm 				 * non-legacy parts of sharemgr. We need to
2256a3351425Sdougm 				 * reject it here. For legacy, it is pulled
2257a3351425Sdougm 				 * out well before we get here.
2258a3351425Sdougm 				 */
2259a3351425Sdougm 				ret = SA_NO_SUCH_PROP;
2260a3351425Sdougm 				break;
2261a3351425Sdougm 			default:
2262a3351425Sdougm 				break;
22636185db85Sdougm 			}
2264aed5d200Sdougm 
2265aed5d200Sdougm 			if (value != NULL)
2266aed5d200Sdougm 				sa_free_attr_string(value);
2267aed5d200Sdougm 
2268a3351425Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
2269a3351425Sdougm 				/* do the property specific check */
2270687915e9Sdougm 				ret = optdefs[optindex].check(handle, property);
22716185db85Sdougm 			}
22726185db85Sdougm 		}
22736185db85Sdougm 	}
22746185db85Sdougm 
22756185db85Sdougm 	if (propname != NULL)
2276a3351425Sdougm 		sa_free_attr_string(propname);
22776185db85Sdougm 	return (ret);
22786185db85Sdougm }
22796185db85Sdougm 
22806185db85Sdougm /*
22816185db85Sdougm  * Protocol management functions
22826185db85Sdougm  *
22833472f5dcSdougm  * Properties defined in the default files are defined in
22843472f5dcSdougm  * proto_option_defs for parsing and validation. If "other" and
22853472f5dcSdougm  * "compare" are set, then the value for this property should be
22863472f5dcSdougm  * compared against the property specified in "other" using the
22873472f5dcSdougm  * "compare" check (either <= or >=) in order to ensure that the
22883472f5dcSdougm  * values are in the correct range.  E.g. setting server_versmin
22893472f5dcSdougm  * higher than server_versmax should not be allowed.
22906185db85Sdougm  */
22916185db85Sdougm 
22926185db85Sdougm struct proto_option_defs {
22936185db85Sdougm 	char *tag;
22946185db85Sdougm 	char *name;	/* display name -- remove protocol identifier */
22956185db85Sdougm 	int index;
22966185db85Sdougm 	int type;
22976185db85Sdougm 	union {
22986185db85Sdougm 	    int intval;
22996185db85Sdougm 	    char *string;
23006185db85Sdougm 	} defvalue;
23016185db85Sdougm 	uint32_t svcs;
23026185db85Sdougm 	int32_t minval;
23036185db85Sdougm 	int32_t maxval;
23046185db85Sdougm 	char *file;
23053472f5dcSdougm 	char *other;
23063472f5dcSdougm 	int compare;
23073472f5dcSdougm #define	OPT_CMP_GE	0
23083472f5dcSdougm #define	OPT_CMP_LE	1
23096185db85Sdougm 	int (*check)(char *);
23106185db85Sdougm } proto_options[] = {
23116185db85Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
23126185db85Sdougm 	{"nfsd_servers",
23136185db85Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
23146185db85Sdougm 	    1, INT32_MAX, NFSADMIN},
23156185db85Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
23166185db85Sdougm 	{"lockd_listen_backlog",
23176185db85Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
23186185db85Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
23196185db85Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
23206185db85Sdougm 	{"lockd_servers",
23216185db85Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
23226185db85Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
23236185db85Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
23246185db85Sdougm 	{"lockd_retransmit_timeout",
23256185db85Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
23266185db85Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23276185db85Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
23286185db85Sdougm 	{"grace_period",
23296185db85Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
23306185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23316185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
23326185db85Sdougm 	{"nfs_server_versmin",
23336185db85Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
23346185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23353472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
23366185db85Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
23376185db85Sdougm 	{"nfs_server_versmax",
23386185db85Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
23396185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
23403472f5dcSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
23416185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
23426185db85Sdougm 	{"nfs_client_versmin",
23436185db85Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
23446185db85Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23453472f5dcSdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
23466185db85Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
23476185db85Sdougm 	{"nfs_client_versmax",
23486185db85Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
23496185db85Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
23503472f5dcSdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
23516185db85Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
23526185db85Sdougm 	{"nfs_server_delegation",
23536185db85Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
23546185db85Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
23556185db85Sdougm 	    NFSADMIN},
23566185db85Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
23576185db85Sdougm 	{"nfsmapid_domain",
23586185db85Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
23596185db85Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
23606185db85Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
23616185db85Sdougm 	{"nfsd_max_connections",
23626185db85Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
23636185db85Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
23646185db85Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
23656185db85Sdougm 	{"nfsd_protocol",
23666185db85Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
23676185db85Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
23686185db85Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
23696185db85Sdougm 	{"nfsd_listen_backlog",
23706185db85Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
23716185db85Sdougm 	    OPT_TYPE_NUMBER, 0,
23726185db85Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
23736185db85Sdougm 	{NULL}
23746185db85Sdougm };
23756185db85Sdougm 
23766185db85Sdougm /*
23776185db85Sdougm  * the protoset holds the defined options so we don't have to read
23786185db85Sdougm  * them multiple times
23796185db85Sdougm  */
2380aed5d200Sdougm static sa_protocol_properties_t protoset;
23816185db85Sdougm 
23826185db85Sdougm static int
23836185db85Sdougm findprotoopt(char *name, int whichname)
23846185db85Sdougm {
23856185db85Sdougm 	int i;
23866185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2387a3351425Sdougm 		if (whichname == 1) {
2388a3351425Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
23896185db85Sdougm 			return (i);
2390a3351425Sdougm 		} else {
2391a3351425Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
2392a3351425Sdougm 				return (i);
2393a3351425Sdougm 		}
23946185db85Sdougm 	}
23956185db85Sdougm 	return (-1);
23966185db85Sdougm }
23976185db85Sdougm 
23986185db85Sdougm /*
23996185db85Sdougm  * fixcaselower(str)
24006185db85Sdougm  *
24016185db85Sdougm  * convert a string to lower case (inplace).
24026185db85Sdougm  */
24036185db85Sdougm 
24046185db85Sdougm static void
24056185db85Sdougm fixcaselower(char *str)
24066185db85Sdougm {
24076185db85Sdougm 	while (*str) {
2408a3351425Sdougm 		*str = tolower(*str);
2409a3351425Sdougm 		str++;
24106185db85Sdougm 	}
24116185db85Sdougm }
24126185db85Sdougm 
24136185db85Sdougm /*
24146185db85Sdougm  * fixcaseupper(str)
24156185db85Sdougm  *
24166185db85Sdougm  * convert a string to upper case (inplace).
24176185db85Sdougm  */
24186185db85Sdougm 
24196185db85Sdougm static void
24206185db85Sdougm fixcaseupper(char *str)
24216185db85Sdougm {
24226185db85Sdougm 	while (*str) {
2423a3351425Sdougm 		*str = toupper(*str);
2424a3351425Sdougm 		str++;
24256185db85Sdougm 	}
24266185db85Sdougm }
24276185db85Sdougm 
2428330ef417Sdougm /*
2429330ef417Sdougm  * skipwhitespace(str)
2430330ef417Sdougm  *
2431330ef417Sdougm  * Skip leading white space. It is assumed that it is called with a
2432330ef417Sdougm  * valid pointer.
2433330ef417Sdougm  */
2434330ef417Sdougm 
2435330ef417Sdougm static char *
2436330ef417Sdougm skipwhitespace(char *str)
2437330ef417Sdougm {
2438330ef417Sdougm 	while (*str && isspace(*str))
2439330ef417Sdougm 		str++;
2440330ef417Sdougm 
2441330ef417Sdougm 	return (str);
2442330ef417Sdougm }
2443330ef417Sdougm 
2444a3351425Sdougm /*
2445a3351425Sdougm  * extractprop()
2446a3351425Sdougm  *
2447a3351425Sdougm  * Extract the property and value out of the line and create the
2448a3351425Sdougm  * property in the optionset.
2449a3351425Sdougm  */
2450e7bab347Sdougm static int
2451a3351425Sdougm extractprop(char *name, char *value)
2452a3351425Sdougm {
2453a3351425Sdougm 	sa_property_t prop;
2454a3351425Sdougm 	int index;
2455e7bab347Sdougm 	int ret = SA_OK;
2456a3351425Sdougm 	/*
2457a3351425Sdougm 	 * Remove any leading
2458a3351425Sdougm 	 * white space.
2459a3351425Sdougm 	 */
2460a3351425Sdougm 	name = skipwhitespace(name);
2461a3351425Sdougm 
2462a3351425Sdougm 	index = findprotoopt(name, 0);
2463a3351425Sdougm 	if (index >= 0) {
2464a3351425Sdougm 		fixcaselower(name);
2465a3351425Sdougm 		prop = sa_create_property(proto_options[index].name, value);
2466a3351425Sdougm 		if (prop != NULL)
2467e7bab347Sdougm 			ret = sa_add_protocol_property(protoset, prop);
2468e7bab347Sdougm 		else
2469e7bab347Sdougm 			ret = SA_NO_MEMORY;
2470a3351425Sdougm 	}
2471e7bab347Sdougm 	return (ret);
2472a3351425Sdougm }
2473a3351425Sdougm 
24746185db85Sdougm /*
24756185db85Sdougm  * initprotofromdefault()
24766185db85Sdougm  *
2477917c27c8Sdougm  * Read the default file(s) and add the defined values to the
24786185db85Sdougm  * protoset.  Note that default values are known from the built in
2479917c27c8Sdougm  * table in case the file doesn't have a definition. Not having the
2480917c27c8Sdougm  * /etc/default/nfs file is OK since we have builtin default
2481917c27c8Sdougm  * values. The default file will get constructed as needed if values
2482917c27c8Sdougm  * are changed from the defaults.
24836185db85Sdougm  */
24846185db85Sdougm 
24856185db85Sdougm static int
24866185db85Sdougm initprotofromdefault()
24876185db85Sdougm {
24886185db85Sdougm 	FILE *nfs;
24896185db85Sdougm 	char buff[BUFSIZ];
24906185db85Sdougm 	char *name;
24916185db85Sdougm 	char *value;
2492e7bab347Sdougm 	int ret = SA_OK;
24936185db85Sdougm 
24946185db85Sdougm 	protoset = sa_create_protocol_properties("nfs");
24956185db85Sdougm 
24966185db85Sdougm 	if (protoset != NULL) {
2497a3351425Sdougm 		nfs = fopen(NFSADMIN, "r");
2498a3351425Sdougm 		if (nfs != NULL) {
2499e7bab347Sdougm 			while (ret == SA_OK &&
2500e7bab347Sdougm 			    fgets(buff, sizeof (buff), nfs) != NULL) {
2501a3351425Sdougm 				switch (buff[0]) {
2502a3351425Sdougm 				case '\n':
2503a3351425Sdougm 				case '#':
2504a3351425Sdougm 					/* skip */
2505a3351425Sdougm 					break;
2506a3351425Sdougm 				default:
2507a3351425Sdougm 					name = buff;
2508a3351425Sdougm 					buff[strlen(buff) - 1] = '\0';
2509a3351425Sdougm 					value = strchr(name, '=');
2510a3351425Sdougm 					if (value != NULL) {
2511a3351425Sdougm 						*value++ = '\0';
2512e7bab347Sdougm 						ret = extractprop(name, value);
2513a3351425Sdougm 					}
2514a3351425Sdougm 				}
25156185db85Sdougm 			}
2516e7bab347Sdougm 			(void) fclose(nfs);
2517e7bab347Sdougm 		} else {
2518917c27c8Sdougm 			switch (errno) {
2519917c27c8Sdougm 			case EPERM:
2520917c27c8Sdougm 			case EACCES:
2521917c27c8Sdougm 				ret = SA_NO_PERMISSION;
2522917c27c8Sdougm 				break;
2523917c27c8Sdougm 			case ENOENT:
2524917c27c8Sdougm 				break;
2525917c27c8Sdougm 			default:
2526917c27c8Sdougm 				ret = SA_SYSTEM_ERR;
2527917c27c8Sdougm 				break;
2528917c27c8Sdougm 			}
25296185db85Sdougm 		}
2530e7bab347Sdougm 	} else {
2531e7bab347Sdougm 		ret = SA_NO_MEMORY;
25326185db85Sdougm 	}
2533e7bab347Sdougm 	return (ret);
25346185db85Sdougm }
25356185db85Sdougm 
25366185db85Sdougm /*
2537a3351425Sdougm  * add_defaults()
25386185db85Sdougm  *
25396185db85Sdougm  * Add the default values for any property not defined in the parsing
25403472f5dcSdougm  * of the default files. Values are set according to their defined
25413472f5dcSdougm  * types.
25426185db85Sdougm  */
25436185db85Sdougm 
25446185db85Sdougm static void
25456185db85Sdougm add_defaults()
25466185db85Sdougm {
25476185db85Sdougm 	int i;
25486185db85Sdougm 	char number[MAXDIGITS];
25496185db85Sdougm 
25506185db85Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2551a3351425Sdougm 		sa_property_t prop;
2552a3351425Sdougm 		prop = sa_get_protocol_property(protoset,
2553a3351425Sdougm 		    proto_options[i].name);
2554a3351425Sdougm 		if (prop == NULL) {
2555a3351425Sdougm 			/* add the default value */
2556a3351425Sdougm 			switch (proto_options[i].type) {
2557a3351425Sdougm 			case OPT_TYPE_NUMBER:
2558a3351425Sdougm 				(void) snprintf(number, sizeof (number), "%d",
2559a3351425Sdougm 				    proto_options[i].defvalue.intval);
2560a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2561a3351425Sdougm 				    number);
2562a3351425Sdougm 				break;
2563a3351425Sdougm 
2564a3351425Sdougm 			case OPT_TYPE_BOOLEAN:
2565a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2566a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2567a3351425Sdougm 				    "true" : "false");
2568a3351425Sdougm 				break;
2569a3351425Sdougm 
2570a3351425Sdougm 			case OPT_TYPE_ONOFF:
2571a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2572a3351425Sdougm 				    proto_options[i].defvalue.intval ?
2573a3351425Sdougm 				    "on" : "off");
2574a3351425Sdougm 				break;
2575a3351425Sdougm 
2576a3351425Sdougm 			default:
2577a3351425Sdougm 				/* treat as strings of zero length */
2578a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2579a3351425Sdougm 				    "");
2580a3351425Sdougm 				break;
2581a3351425Sdougm 			}
2582a3351425Sdougm 			if (prop != NULL)
2583a3351425Sdougm 				(void) sa_add_protocol_property(protoset, prop);
25846185db85Sdougm 		}
25856185db85Sdougm 	}
25866185db85Sdougm }
25876185db85Sdougm 
25886185db85Sdougm static void
25896185db85Sdougm free_protoprops()
25906185db85Sdougm {
2591aed5d200Sdougm 	if (protoset != NULL) {
2592aed5d200Sdougm 		xmlFreeNode(protoset);
2593aed5d200Sdougm 		protoset = NULL;
2594aed5d200Sdougm 	}
25956185db85Sdougm }
25966185db85Sdougm 
25976185db85Sdougm /*
25986185db85Sdougm  * nfs_init()
25996185db85Sdougm  *
26006185db85Sdougm  * Initialize the NFS plugin.
26016185db85Sdougm  */
26026185db85Sdougm 
26036185db85Sdougm static int
26046185db85Sdougm nfs_init()
26056185db85Sdougm {
26066185db85Sdougm 	int ret = SA_OK;
26076185db85Sdougm 
2608917c27c8Sdougm 	if (sa_plugin_ops.sa_init != nfs_init) {
2609a3351425Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2610a3351425Sdougm 		    "NFS plugin not properly initialized\n"));
2611917c27c8Sdougm 		return (SA_CONFIG_ERR);
2612917c27c8Sdougm 	}
26136185db85Sdougm 
26146185db85Sdougm 	ret = initprotofromdefault();
2615917c27c8Sdougm 	if (ret != SA_OK) {
2616917c27c8Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2617917c27c8Sdougm 		    "NFS plugin problem with default file: %s\n"),
2618917c27c8Sdougm 		    sa_errorstr(ret));
2619917c27c8Sdougm 		ret = SA_OK;
2620917c27c8Sdougm 	}
2621917c27c8Sdougm 	add_defaults();
26226185db85Sdougm 
26236185db85Sdougm 	return (ret);
26246185db85Sdougm }
26256185db85Sdougm 
26266185db85Sdougm /*
26276185db85Sdougm  * nfs_fini()
26286185db85Sdougm  *
26296185db85Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
26306185db85Sdougm  */
26316185db85Sdougm 
26326185db85Sdougm static void
26336185db85Sdougm nfs_fini()
26346185db85Sdougm {
26356185db85Sdougm 	free_protoprops();
26366185db85Sdougm }
26376185db85Sdougm 
26386185db85Sdougm /*
26396185db85Sdougm  * nfs_get_proto_set()
26406185db85Sdougm  *
26416185db85Sdougm  * Return an optionset with all the protocol specific properties in
26426185db85Sdougm  * it.
26436185db85Sdougm  */
26446185db85Sdougm 
26456185db85Sdougm static sa_protocol_properties_t
26466185db85Sdougm nfs_get_proto_set()
26476185db85Sdougm {
26486185db85Sdougm 	return (protoset);
26496185db85Sdougm }
26506185db85Sdougm 
26516185db85Sdougm struct deffile {
26526185db85Sdougm 	struct deffile *next;
26536185db85Sdougm 	char *line;
26546185db85Sdougm };
26556185db85Sdougm 
26566185db85Sdougm /*
26576185db85Sdougm  * read_default_file(fname)
26586185db85Sdougm  *
26596185db85Sdougm  * Read the specified default file. We return a list of entries. This
26606185db85Sdougm  * get used for adding or removing values.
26616185db85Sdougm  */
26626185db85Sdougm 
26636185db85Sdougm static struct deffile *
26646185db85Sdougm read_default_file(char *fname)
26656185db85Sdougm {
26666185db85Sdougm 	FILE *file;
26676185db85Sdougm 	struct deffile *defs = NULL;
26686185db85Sdougm 	struct deffile *newdef;
26696185db85Sdougm 	struct deffile *prevdef = NULL;
26706185db85Sdougm 	char buff[BUFSIZ * 2];
26716185db85Sdougm 
26726185db85Sdougm 	file = fopen(fname, "r");
26736185db85Sdougm 	if (file != NULL) {
2674a3351425Sdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
2675a3351425Sdougm 			newdef = (struct deffile *)calloc(1,
2676a3351425Sdougm 			    sizeof (struct deffile));
2677a3351425Sdougm 			if (newdef != NULL) {
2678a3351425Sdougm 				/* Make sure we skip any leading whitespace. */
2679a3351425Sdougm 				newdef->line = strdup(skipwhitespace(buff));
2680a3351425Sdougm 				if (defs == NULL) {
2681a3351425Sdougm 					prevdef = defs = newdef;
2682a3351425Sdougm 				} else {
2683a3351425Sdougm 					prevdef->next = newdef;
2684a3351425Sdougm 					prevdef = newdef;
2685a3351425Sdougm 				}
2686a3351425Sdougm 			}
26876185db85Sdougm 		}
2688917c27c8Sdougm 		(void) fclose(file);
2689917c27c8Sdougm 	} else {
2690917c27c8Sdougm 		int ret = SA_OK;
2691917c27c8Sdougm 		switch (errno) {
2692917c27c8Sdougm 		case EPERM:
2693917c27c8Sdougm 		case EACCES:
2694917c27c8Sdougm 			ret = SA_NO_PERMISSION;
2695917c27c8Sdougm 			break;
2696917c27c8Sdougm 		case ENOENT:
2697917c27c8Sdougm 			break;
2698917c27c8Sdougm 		default:
2699917c27c8Sdougm 			ret = SA_SYSTEM_ERR;
2700917c27c8Sdougm 			break;
2701917c27c8Sdougm 		}
2702917c27c8Sdougm 		if (ret == SA_OK) {
2703917c27c8Sdougm 			/* Want at least one comment line */
2704917c27c8Sdougm 			defs = (struct deffile *)
2705917c27c8Sdougm 			    calloc(1, sizeof (struct deffile));
2706917c27c8Sdougm 			defs->line = strdup("# NFS default file\n");
2707917c27c8Sdougm 		}
27086185db85Sdougm 	}
27096185db85Sdougm 	return (defs);
27106185db85Sdougm }
27116185db85Sdougm 
27126185db85Sdougm static void
27136185db85Sdougm free_default_file(struct deffile *defs)
27146185db85Sdougm {
27156185db85Sdougm 	struct deffile *curdefs = NULL;
27166185db85Sdougm 
27176185db85Sdougm 	while (defs != NULL) {
2718a3351425Sdougm 		curdefs = defs;
2719a3351425Sdougm 		defs = defs->next;
2720a3351425Sdougm 		if (curdefs->line != NULL)
2721a3351425Sdougm 			free(curdefs->line);
2722a3351425Sdougm 		free(curdefs);
27236185db85Sdougm 	}
27246185db85Sdougm }
27256185db85Sdougm 
27266185db85Sdougm /*
27276185db85Sdougm  * write_default_file(fname, defs)
27286185db85Sdougm  *
27296185db85Sdougm  * Write the default file back.
27306185db85Sdougm  */
27316185db85Sdougm 
27326185db85Sdougm static int
27336185db85Sdougm write_default_file(char *fname, struct deffile *defs)
27346185db85Sdougm {
27356185db85Sdougm 	FILE *file;
27366185db85Sdougm 	int ret = SA_OK;
27376185db85Sdougm 	sigset_t old, new;
27386185db85Sdougm 
27396185db85Sdougm 	file = fopen(fname, "w+");
27406185db85Sdougm 	if (file != NULL) {
2741a3351425Sdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
2742a3351425Sdougm 		(void) sigaddset(&new, SIGHUP);
2743a3351425Sdougm 		(void) sigaddset(&new, SIGINT);
2744a3351425Sdougm 		(void) sigaddset(&new, SIGQUIT);
2745a3351425Sdougm 		(void) sigaddset(&new, SIGTSTP);
2746a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
2747a3351425Sdougm 		while (defs != NULL) {
2748a3351425Sdougm 			(void) fputs(defs->line, file);
2749a3351425Sdougm 			defs = defs->next;
2750a3351425Sdougm 		}
2751a3351425Sdougm 		(void) fsync(fileno(file));
2752a3351425Sdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
2753a3351425Sdougm 		(void) fclose(file);
27546185db85Sdougm 	} else {
2755a3351425Sdougm 		switch (errno) {
2756a3351425Sdougm 		case EPERM:
2757a3351425Sdougm 		case EACCES:
2758a3351425Sdougm 			ret = SA_NO_PERMISSION;
2759a3351425Sdougm 			break;
2760a3351425Sdougm 		default:
2761a3351425Sdougm 			ret = SA_SYSTEM_ERR;
2762a3351425Sdougm 		}
27636185db85Sdougm 	}
27646185db85Sdougm 	return (ret);
27656185db85Sdougm }
27666185db85Sdougm 
27676185db85Sdougm 
27686185db85Sdougm /*
27696185db85Sdougm  * set_default_file_value(tag, value)
27706185db85Sdougm  *
27716185db85Sdougm  * Set the default file value for tag to value. Then rewrite the file.
27726185db85Sdougm  * tag and value are always set.  The caller must ensure this.
27736185db85Sdougm  */
27746185db85Sdougm 
27756185db85Sdougm #define	MAX_STRING_LENGTH	256
27766185db85Sdougm static int
27776185db85Sdougm set_default_file_value(char *tag, char *value)
27786185db85Sdougm {
27796185db85Sdougm 	int ret = SA_OK;
27806185db85Sdougm 	struct deffile *root;
27816185db85Sdougm 	struct deffile *defs;
27826185db85Sdougm 	struct deffile *prev;
27836185db85Sdougm 	char string[MAX_STRING_LENGTH];
27846185db85Sdougm 	int len;
2785917c27c8Sdougm 	boolean_t update = B_FALSE;
27866185db85Sdougm 
27876185db85Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
27886185db85Sdougm 	len = strlen(string);
27896185db85Sdougm 
27906185db85Sdougm 	root = defs = read_default_file(NFSADMIN);
27916185db85Sdougm 	if (root == NULL) {
2792917c27c8Sdougm 		switch (errno) {
2793917c27c8Sdougm 		case EPERM:
2794917c27c8Sdougm 		case EACCES:
2795a3351425Sdougm 			ret = SA_NO_PERMISSION;
2796917c27c8Sdougm 			break;
2797917c27c8Sdougm 		default:
2798917c27c8Sdougm 			ret = SA_NO_MEMORY;
2799917c27c8Sdougm 			break;
2800917c27c8Sdougm 		}
2801917c27c8Sdougm 		return (ret);
2802917c27c8Sdougm 	}
2803917c27c8Sdougm 
2804917c27c8Sdougm 	while (defs != NULL) {
2805917c27c8Sdougm 		if (defs->line != NULL &&
2806917c27c8Sdougm 		    strncasecmp(defs->line, string, len) == 0) {
2807917c27c8Sdougm 			/* replace with the new value */
2808917c27c8Sdougm 			free(defs->line);
2809917c27c8Sdougm 			fixcaseupper(tag);
2810917c27c8Sdougm 			(void) snprintf(string, sizeof (string),
2811917c27c8Sdougm 			    "%s=%s\n", tag, value);
2812917c27c8Sdougm 			string[MAX_STRING_LENGTH - 1] = '\0';
2813917c27c8Sdougm 			defs->line = strdup(string);
2814917c27c8Sdougm 			update = B_TRUE;
2815917c27c8Sdougm 			break;
2816917c27c8Sdougm 		}
2817917c27c8Sdougm 		defs = defs->next;
2818917c27c8Sdougm 	}
2819917c27c8Sdougm 	if (!update) {
2820917c27c8Sdougm 		defs = root;
2821917c27c8Sdougm 		/* didn't find, so see if it is a comment */
2822917c27c8Sdougm 		(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
2823917c27c8Sdougm 		len = strlen(string);
28246185db85Sdougm 		while (defs != NULL) {
2825917c27c8Sdougm 			if (strncasecmp(defs->line, string, len) == 0) {
2826a3351425Sdougm 				/* replace with the new value */
2827a3351425Sdougm 				free(defs->line);
2828a3351425Sdougm 				fixcaseupper(tag);
2829a3351425Sdougm 				(void) snprintf(string, sizeof (string),
28306185db85Sdougm 				    "%s=%s\n", tag, value);
2831a3351425Sdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
2832a3351425Sdougm 				defs->line = strdup(string);
2833917c27c8Sdougm 				update = B_TRUE;
2834a3351425Sdougm 				break;
2835a3351425Sdougm 			}
2836a3351425Sdougm 			defs = defs->next;
2837a3351425Sdougm 		}
2838917c27c8Sdougm 	}
2839917c27c8Sdougm 	if (!update) {
2840917c27c8Sdougm 		fixcaseupper(tag);
2841917c27c8Sdougm 		(void) snprintf(string, sizeof (string), "%s=%s\n",
2842917c27c8Sdougm 		    tag, value);
2843917c27c8Sdougm 		prev = root;
2844917c27c8Sdougm 		while (prev->next != NULL)
2845917c27c8Sdougm 			prev = prev->next;
2846917c27c8Sdougm 		defs = malloc(sizeof (struct deffile));
2847917c27c8Sdougm 		prev->next = defs;
2848917c27c8Sdougm 		if (defs != NULL) {
2849917c27c8Sdougm 			defs->next = NULL;
2850917c27c8Sdougm 			defs->line = strdup(string);
2851917c27c8Sdougm 			update = B_TRUE;
2852a3351425Sdougm 		}
28536185db85Sdougm 	}
2854917c27c8Sdougm 	if (update) {
2855917c27c8Sdougm 		ret = write_default_file(NFSADMIN, root);
2856917c27c8Sdougm 	}
2857917c27c8Sdougm 	free_default_file(root);
28586185db85Sdougm 	return (ret);
28596185db85Sdougm }
28606185db85Sdougm 
28613472f5dcSdougm /*
28623472f5dcSdougm  * service_in_state(service, chkstate)
28633472f5dcSdougm  *
28643472f5dcSdougm  * Want to know if the specified service is in the desired state
28653472f5dcSdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
28663472f5dcSdougm  * isn't.
28673472f5dcSdougm  */
28683472f5dcSdougm static int
28693472f5dcSdougm service_in_state(char *service, const char *chkstate)
28703472f5dcSdougm {
28713472f5dcSdougm 	char *state;
28723472f5dcSdougm 	int ret = B_FALSE;
28733472f5dcSdougm 
28743472f5dcSdougm 	state = smf_get_state(service);
28753472f5dcSdougm 	if (state != NULL) {
2876a3351425Sdougm 		/* got the state so get the equality for the return value */
2877a3351425Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2878a3351425Sdougm 		free(state);
28793472f5dcSdougm 	}
28803472f5dcSdougm 	return (ret);
28813472f5dcSdougm }
28823472f5dcSdougm 
28836185db85Sdougm /*
28846185db85Sdougm  * restart_service(svcs)
28856185db85Sdougm  *
28866185db85Sdougm  * Walk through the bit mask of services that need to be restarted in
28876185db85Sdougm  * order to use the new property values. Some properties affect
28883472f5dcSdougm  * multiple daemons. Should only restart a service if it is currently
28893472f5dcSdougm  * enabled (online).
28906185db85Sdougm  */
28916185db85Sdougm 
28926185db85Sdougm static void
28936185db85Sdougm restart_service(uint32_t svcs)
28946185db85Sdougm {
28956185db85Sdougm 	uint32_t mask;
28963472f5dcSdougm 	int ret;
28973472f5dcSdougm 	char *service;
28983472f5dcSdougm 
28996185db85Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
2900a3351425Sdougm 		switch (svcs & mask) {
2901a3351425Sdougm 		case SVC_LOCKD:
2902a3351425Sdougm 			service = LOCKD;
2903a3351425Sdougm 			break;
2904a3351425Sdougm 		case SVC_STATD:
2905a3351425Sdougm 			service = STATD;
2906a3351425Sdougm 			break;
2907a3351425Sdougm 		case SVC_NFSD:
2908a3351425Sdougm 			service = NFSD;
2909a3351425Sdougm 			break;
2910a3351425Sdougm 		case SVC_MOUNTD:
2911a3351425Sdougm 			service = MOUNTD;
2912a3351425Sdougm 			break;
2913a3351425Sdougm 		case SVC_NFS4CBD:
2914a3351425Sdougm 			service = NFS4CBD;
2915a3351425Sdougm 			break;
2916a3351425Sdougm 		case SVC_NFSMAPID:
2917a3351425Sdougm 			service = NFSMAPID;
2918a3351425Sdougm 			break;
2919a3351425Sdougm 		case SVC_RQUOTAD:
2920a3351425Sdougm 			service = RQUOTAD;
2921a3351425Sdougm 			break;
2922a3351425Sdougm 		case SVC_NFSLOGD:
2923a3351425Sdougm 			service = NFSLOGD;
2924a3351425Sdougm 			break;
2925a3351425Sdougm 		default:
2926a3351425Sdougm 			continue;
2927a3351425Sdougm 		}
29283472f5dcSdougm 
29293472f5dcSdougm 		/*
29303472f5dcSdougm 		 * Only attempt to restart the service if it is
29313472f5dcSdougm 		 * currently running. In the future, it may be
29323472f5dcSdougm 		 * desirable to use smf_refresh_instance if the NFS
29333472f5dcSdougm 		 * services ever implement the refresh method.
29343472f5dcSdougm 		 */
2935a3351425Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2936a3351425Sdougm 			ret = smf_restart_instance(service);
29373472f5dcSdougm 			/*
2938a3351425Sdougm 			 * There are only a few SMF errors at this point, but
2939a3351425Sdougm 			 * it is also possible that a bad value may have put
2940a3351425Sdougm 			 * the service into maintenance if there wasn't an
2941a3351425Sdougm 			 * SMF level error.
29423472f5dcSdougm 			 */
2943a3351425Sdougm 			if (ret != 0) {
2944a3351425Sdougm 				(void) fprintf(stderr,
2945a3351425Sdougm 				    dgettext(TEXT_DOMAIN,
2946a3351425Sdougm 				    "%s failed to restart: %s\n"),
2947a3351425Sdougm 				    scf_strerror(scf_error()));
2948a3351425Sdougm 			} else {
2949a3351425Sdougm 				/*
2950a3351425Sdougm 				 * Check whether it has gone to "maintenance"
2951a3351425Sdougm 				 * mode or not. Maintenance implies something
2952a3351425Sdougm 				 * went wrong.
2953a3351425Sdougm 				 */
2954a3351425Sdougm 				if (service_in_state(service,
2955a3351425Sdougm 				    SCF_STATE_STRING_MAINT)) {
2956a3351425Sdougm 					(void) fprintf(stderr,
2957a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
2958a3351425Sdougm 					    "%s failed to restart\n"),
2959a3351425Sdougm 					    service);
2960a3351425Sdougm 				}
2961a3351425Sdougm 			}
29623472f5dcSdougm 		}
2963a3351425Sdougm 		svcs &= ~mask;
29646185db85Sdougm 	}
29656185db85Sdougm }
29666185db85Sdougm 
29673472f5dcSdougm /*
29683472f5dcSdougm  * nfs_minmax_check(name, value)
29693472f5dcSdougm  *
29703472f5dcSdougm  * Verify that the value for the property specified by index is valid
29713472f5dcSdougm  * relative to the opposite value in the case of a min/max variable.
29723472f5dcSdougm  * Currently, server_minvers/server_maxvers and
29733472f5dcSdougm  * client_minvers/client_maxvers are the only ones to check.
29743472f5dcSdougm  */
29753472f5dcSdougm 
29763472f5dcSdougm static int
29773472f5dcSdougm nfs_minmax_check(int index, int value)
29783472f5dcSdougm {
29793472f5dcSdougm 	int val;
29803472f5dcSdougm 	char *pval;
29813472f5dcSdougm 	sa_property_t prop;
29823472f5dcSdougm 	sa_optionset_t opts;
29833472f5dcSdougm 	int ret = B_TRUE;
29843472f5dcSdougm 
29853472f5dcSdougm 	if (proto_options[index].other != NULL) {
2986a3351425Sdougm 		/* have a property to compare against */
2987a3351425Sdougm 		opts = nfs_get_proto_set();
2988a3351425Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
29893472f5dcSdougm 		/*
29903472f5dcSdougm 		 * If we don't find the property, assume default
29913472f5dcSdougm 		 * values which will work since the max will be at the
29923472f5dcSdougm 		 * max and the min at the min.
29933472f5dcSdougm 		 */
2994a3351425Sdougm 		if (prop != NULL) {
2995a3351425Sdougm 			pval = sa_get_property_attr(prop, "value");
2996a3351425Sdougm 			if (pval != NULL) {
2997a3351425Sdougm 				val = strtoul(pval, NULL, 0);
2998a3351425Sdougm 				if (proto_options[index].compare ==
2999a3351425Sdougm 				    OPT_CMP_LE) {
3000a3351425Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
3001a3351425Sdougm 				} else if (proto_options[index].compare ==
3002a3351425Sdougm 				    OPT_CMP_GE) {
3003a3351425Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
3004a3351425Sdougm 				}
3005a3351425Sdougm 			}
30063472f5dcSdougm 		}
30073472f5dcSdougm 	}
30083472f5dcSdougm 	return (ret);
30093472f5dcSdougm }
30103472f5dcSdougm 
30116185db85Sdougm /*
30126185db85Sdougm  * nfs_validate_proto_prop(index, name, value)
30136185db85Sdougm  *
3014da6c28aaSamw  * Verify that the property specified by name can take the new
30156185db85Sdougm  * value. This is a sanity check to prevent bad values getting into
30163472f5dcSdougm  * the default files. All values need to be checked against what is
30173472f5dcSdougm  * allowed by their defined type. If a type isn't explicitly defined
30183472f5dcSdougm  * here, it is treated as a string.
30193472f5dcSdougm  *
30203472f5dcSdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
30213472f5dcSdougm  * within the range specified and potentially against another property
30223472f5dcSdougm  * value as well as specified in the proto_options members other and
30233472f5dcSdougm  * compare.
30246185db85Sdougm  */
30256185db85Sdougm 
30266185db85Sdougm static int
30276185db85Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
30286185db85Sdougm {
30296185db85Sdougm 	int ret = SA_OK;
30306185db85Sdougm 	char *cp;
30316185db85Sdougm #ifdef lint
30326185db85Sdougm 	name = name;
30336185db85Sdougm #endif
30346185db85Sdougm 
30356185db85Sdougm 	switch (proto_options[index].type) {
30366185db85Sdougm 	case OPT_TYPE_NUMBER:
3037a3351425Sdougm 		if (!is_a_number(value))
3038a3351425Sdougm 			ret = SA_BAD_VALUE;
3039a3351425Sdougm 		else {
3040a3351425Sdougm 			int val;
3041a3351425Sdougm 			val = strtoul(value, NULL, 0);
3042a3351425Sdougm 			if (val < proto_options[index].minval ||
3043a3351425Sdougm 			    val > proto_options[index].maxval)
3044a3351425Sdougm 				ret = SA_BAD_VALUE;
3045a3351425Sdougm 			/*
3046a3351425Sdougm 			 * For server_versmin/server_versmax and
3047a3351425Sdougm 			 * client_versmin/client_versmax, the value of the
3048a3351425Sdougm 			 * min(max) should be checked to be correct relative
3049a3351425Sdougm 			 * to the current max(min).
3050a3351425Sdougm 			 */
3051a3351425Sdougm 			if (!nfs_minmax_check(index, val)) {
3052a3351425Sdougm 				ret = SA_BAD_VALUE;
3053a3351425Sdougm 			}
30543472f5dcSdougm 		}
3055a3351425Sdougm 		break;
30563472f5dcSdougm 
30576185db85Sdougm 	case OPT_TYPE_DOMAIN:
30586185db85Sdougm 		/*
30593472f5dcSdougm 		 * needs to be a qualified domain so will have at
30603472f5dcSdougm 		 * least one period and other characters on either
30613472f5dcSdougm 		 * side of it.  A zero length string is also allowed
30623472f5dcSdougm 		 * and is the way to turn off the override.
30636185db85Sdougm 		 */
3064a3351425Sdougm 		if (strlen(value) == 0)
3065a3351425Sdougm 			break;
3066a3351425Sdougm 		cp = strchr(value, '.');
3067a3351425Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
3068a3351425Sdougm 			ret = SA_BAD_VALUE;
30693472f5dcSdougm 		break;
30703472f5dcSdougm 
30716185db85Sdougm 	case OPT_TYPE_BOOLEAN:
3072a3351425Sdougm 		if (strlen(value) == 0 ||
3073a3351425Sdougm 		    strcasecmp(value, "true") == 0 ||
3074a3351425Sdougm 		    strcmp(value, "1") == 0 ||
3075a3351425Sdougm 		    strcasecmp(value, "false") == 0 ||
3076a3351425Sdougm 		    strcmp(value, "0") == 0) {
3077a3351425Sdougm 			ret = SA_OK;
3078a3351425Sdougm 		} else {
3079a3351425Sdougm 			ret = SA_BAD_VALUE;
3080a3351425Sdougm 		}
3081a3351425Sdougm 		break;
30823472f5dcSdougm 
30836185db85Sdougm 	case OPT_TYPE_ONOFF:
3084a3351425Sdougm 		if (strcasecmp(value, "on") != 0 &&
3085a3351425Sdougm 		    strcasecmp(value, "off") != 0) {
3086a3351425Sdougm 			ret = SA_BAD_VALUE;
3087a3351425Sdougm 		}
3088a3351425Sdougm 		break;
30893472f5dcSdougm 
30906185db85Sdougm 	case OPT_TYPE_PROTOCOL:
3091917c27c8Sdougm 		if (strlen(value) != 0 &&
3092917c27c8Sdougm 		    strcasecmp(value, "all") != 0 &&
3093a3351425Sdougm 		    strcasecmp(value, "tcp") != 0 &&
3094a3351425Sdougm 		    strcasecmp(value, "udp") != 0)
3095a3351425Sdougm 			ret = SA_BAD_VALUE;
3096a3351425Sdougm 		break;
30973472f5dcSdougm 
30983472f5dcSdougm 	default:
3099a3351425Sdougm 		/* treat as a string */
3100a3351425Sdougm 		break;
31016185db85Sdougm 	}
31026185db85Sdougm 	return (ret);
31036185db85Sdougm }
31046185db85Sdougm 
31056185db85Sdougm /*
31066185db85Sdougm  * nfs_set_proto_prop(prop)
31076185db85Sdougm  *
31086185db85Sdougm  * check that prop is valid.
31096185db85Sdougm  */
31106185db85Sdougm 
31116185db85Sdougm static int
31126185db85Sdougm nfs_set_proto_prop(sa_property_t prop)
31136185db85Sdougm {
31146185db85Sdougm 	int ret = SA_OK;
31156185db85Sdougm 	char *name;
31166185db85Sdougm 	char *value;
31176185db85Sdougm 
31186185db85Sdougm 	name = sa_get_property_attr(prop, "type");
31196185db85Sdougm 	value = sa_get_property_attr(prop, "value");
31206185db85Sdougm 	if (name != NULL && value != NULL) {
3121a3351425Sdougm 		int index = findprotoopt(name, 1);
3122a3351425Sdougm 		if (index >= 0) {
3123a3351425Sdougm 			/* should test for valid value */
3124a3351425Sdougm 			ret = nfs_validate_proto_prop(index, name, value);
3125a3351425Sdougm 			if (ret == SA_OK)
3126a3351425Sdougm 				ret = set_default_file_value(
3127a3351425Sdougm 				    proto_options[index].tag, value);
3128a3351425Sdougm 			if (ret == SA_OK)
3129a3351425Sdougm 				restart_service(proto_options[index].svcs);
3130a3351425Sdougm 		}
31316185db85Sdougm 	}
31326185db85Sdougm 	if (name != NULL)
3133a3351425Sdougm 		sa_free_attr_string(name);
31346185db85Sdougm 	if (value != NULL)
3135a3351425Sdougm 		sa_free_attr_string(value);
31366185db85Sdougm 	return (ret);
31376185db85Sdougm }
31386185db85Sdougm 
31396185db85Sdougm /*
31406185db85Sdougm  * nfs_get_status()
31416185db85Sdougm  *
31426185db85Sdougm  * What is the current status of the nfsd? We use the SMF state here.
31436185db85Sdougm  * Caller must free the returned value.
31446185db85Sdougm  */
31456185db85Sdougm 
31466185db85Sdougm static char *
31476185db85Sdougm nfs_get_status()
31486185db85Sdougm {
31496185db85Sdougm 	char *state;
31506185db85Sdougm 	state = smf_get_state(NFSD);
31516185db85Sdougm 	return (state != NULL ? state : strdup("-"));
31526185db85Sdougm }
31536185db85Sdougm 
31546185db85Sdougm /*
31556185db85Sdougm  * nfs_space_alias(alias)
31566185db85Sdougm  *
31576185db85Sdougm  * Lookup the space (security) name. If it is default, convert to the
31586185db85Sdougm  * real name.
31596185db85Sdougm  */
31606185db85Sdougm 
31616185db85Sdougm static char *
31626185db85Sdougm nfs_space_alias(char *space)
31636185db85Sdougm {
31646185db85Sdougm 	char *name = space;
31656185db85Sdougm 	seconfig_t secconf;
3166a99982a7Sdougm 
3167a99982a7Sdougm 	/*
3168a99982a7Sdougm 	 * Only the space named "default" is special. If it is used,
3169a99982a7Sdougm 	 * the default needs to be looked up and the real name used.
3170a99982a7Sdougm 	 * This is normally "sys" but could be changed.  We always
3171a99982a7Sdougm 	 * change defautl to the real name.
3172a99982a7Sdougm 	 */
3173a99982a7Sdougm 	if (strcmp(space, "default") == 0 &&
3174a99982a7Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
3175a3351425Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
3176a3351425Sdougm 			name = secconf.sc_name;
31776185db85Sdougm 	}
31786185db85Sdougm 	return (strdup(name));
31796185db85Sdougm }
3180da6c28aaSamw 
3181da6c28aaSamw /*
3182da6c28aaSamw  * nfs_features()
3183da6c28aaSamw  *
3184da6c28aaSamw  * Return a mask of the features required.
3185da6c28aaSamw  */
3186da6c28aaSamw 
3187da6c28aaSamw static uint64_t
3188da6c28aaSamw nfs_features()
3189da6c28aaSamw {
31909e5da854Sdougm 	return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);
3191da6c28aaSamw }
3192