xref: /onnv-gate/usr/src/lib/libshare/nfs/libshare_nfs.c (revision 13080:fcc1e406c13f)
13910Sdougm /*
23910Sdougm  * CDDL HEADER START
33910Sdougm  *
43910Sdougm  * The contents of this file are subject to the terms of the
53910Sdougm  * Common Development and Distribution License (the "License").
63910Sdougm  * You may not use this file except in compliance with the License.
73910Sdougm  *
83910Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93910Sdougm  * or http://www.opensolaris.org/os/licensing.
103910Sdougm  * See the License for the specific language governing permissions
113910Sdougm  * and limitations under the License.
123910Sdougm  *
133910Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
143910Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153910Sdougm  * If applicable, add the following below this CDDL HEADER, with the
163910Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
173910Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
183910Sdougm  *
193910Sdougm  * CDDL HEADER END
203910Sdougm  */
213910Sdougm 
223910Sdougm /*
2312679SPavel.Filipensky@Sun.COM  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
243910Sdougm  */
253910Sdougm 
263910Sdougm /*
273910Sdougm  * NFS specific functions
283910Sdougm  */
293910Sdougm #include <stdio.h>
303910Sdougm #include <string.h>
313910Sdougm #include <ctype.h>
323910Sdougm #include <stdlib.h>
333910Sdougm #include <unistd.h>
343910Sdougm #include <zone.h>
353910Sdougm #include <errno.h>
363910Sdougm #include <locale.h>
373910Sdougm #include <signal.h>
38*13080SPavan.Mettu@Oracle.COM #include <strings.h>
393910Sdougm #include "libshare.h"
403910Sdougm #include "libshare_impl.h"
413910Sdougm #include <nfs/export.h>
423910Sdougm #include <pwd.h>
433910Sdougm #include <limits.h>
443910Sdougm #include <libscf.h>
45*13080SPavan.Mettu@Oracle.COM #include <syslog.h>
46*13080SPavan.Mettu@Oracle.COM #include <rpcsvc/daemon_utils.h>
473910Sdougm #include "nfslog_config.h"
483910Sdougm #include "nfslogtab.h"
493910Sdougm #include "libshare_nfs.h"
503910Sdougm #include <nfs/nfs.h>
514543Smarks #include <nfs/nfssys.h>
52*13080SPavan.Mettu@Oracle.COM #include "smfcfg.h"
533910Sdougm 
543910Sdougm /* should really be in some global place */
553910Sdougm #define	DEF_WIN	30000
563910Sdougm #define	OPT_CHUNK	1024
573910Sdougm 
583910Sdougm int debug = 0;
593910Sdougm 
604543Smarks #define	NFS_SERVER_SVC	"svc:/network/nfs/server:default"
61*13080SPavan.Mettu@Oracle.COM #define	NFS_CLIENT_SVC	(char *)"svc:/network/nfs/client:default"
623910Sdougm 
633910Sdougm /* internal functions */
643910Sdougm static int nfs_init();
653910Sdougm static void nfs_fini();
663910Sdougm static int nfs_enable_share(sa_share_t);
674543Smarks static int nfs_disable_share(sa_share_t, char *);
686214Sdougm static int nfs_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
693910Sdougm static int nfs_validate_security_mode(char *);
703910Sdougm static int nfs_is_security_opt(char *);
713910Sdougm static int nfs_parse_legacy_options(sa_group_t, char *);
723910Sdougm static char *nfs_format_options(sa_group_t, int);
733910Sdougm static int nfs_set_proto_prop(sa_property_t);
743910Sdougm static sa_protocol_properties_t nfs_get_proto_set();
753910Sdougm static char *nfs_get_status();
763910Sdougm static char *nfs_space_alias(char *);
775331Samw static uint64_t nfs_features();
783910Sdougm 
793910Sdougm /*
803910Sdougm  * ops vector that provides the protocol specific info and operations
813910Sdougm  * for share management.
823910Sdougm  */
833910Sdougm 
843910Sdougm struct sa_plugin_ops sa_plugin_ops = {
853910Sdougm 	SA_PLUGIN_VERSION,
863910Sdougm 	"nfs",
873910Sdougm 	nfs_init,
883910Sdougm 	nfs_fini,
893910Sdougm 	nfs_enable_share,
903910Sdougm 	nfs_disable_share,
913910Sdougm 	nfs_validate_property,
923910Sdougm 	nfs_validate_security_mode,
933910Sdougm 	nfs_is_security_opt,
943910Sdougm 	nfs_parse_legacy_options,
953910Sdougm 	nfs_format_options,
963910Sdougm 	nfs_set_proto_prop,
973910Sdougm 	nfs_get_proto_set,
983910Sdougm 	nfs_get_status,
993910Sdougm 	nfs_space_alias,
1005331Samw 	NULL,	/* update_legacy */
1015331Samw 	NULL,	/* delete_legacy */
1025331Samw 	NULL,	/* change_notify */
1035331Samw 	NULL,	/* enable_resource */
1045331Samw 	NULL,	/* disable_resource */
1055331Samw 	nfs_features,
1065331Samw 	NULL,	/* transient shares */
1075331Samw 	NULL,	/* notify resource */
1086007Sthurlow 	NULL,	/* rename_resource */
1096007Sthurlow 	NULL,	/* run_command */
1106007Sthurlow 	NULL,	/* command_help */
1116007Sthurlow 	NULL	/* delete_proto_section */
1123910Sdougm };
1133910Sdougm 
1143910Sdougm /*
1153910Sdougm  * list of support services needed
1163910Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
1173910Sdougm  */
1183910Sdougm 
1193910Sdougm static char *service_list_default[] =
12011291SRobert.Thurlow@Sun.COM 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, REPARSED, NULL };
1213910Sdougm static char *service_list_logging[] =
12211291SRobert.Thurlow@Sun.COM 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, REPARSED,
12311291SRobert.Thurlow@Sun.COM 	    NULL };
1243910Sdougm 
1253910Sdougm /*
1263910Sdougm  * option definitions.  Make sure to keep the #define for the option
1273910Sdougm  * index just before the entry it is the index for. Changing the order
1283910Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
1293910Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
1303910Sdougm  */
1313910Sdougm 
1323910Sdougm struct option_defs optdefs[] = {
1333910Sdougm #define	OPT_RO		0
1343910Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
1353910Sdougm #define	OPT_RW		1
1363910Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
1373910Sdougm #define	OPT_ROOT	2
1383910Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
1393910Sdougm #define	OPT_SECURE	3
1403910Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
1413910Sdougm #define	OPT_ANON	4
1423910Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
1433910Sdougm #define	OPT_WINDOW	5
1443910Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
1453910Sdougm #define	OPT_NOSUID	6
1463910Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
1473910Sdougm #define	OPT_ACLOK	7
1483910Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
1493910Sdougm #define	OPT_NOSUB	8
1503910Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
1513910Sdougm #define	OPT_SEC		9
1523910Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
1533910Sdougm #define	OPT_PUBLIC	10
1543910Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
1553910Sdougm #define	OPT_INDEX	11
1563910Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
1573910Sdougm #define	OPT_LOG		12
1583910Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
1593910Sdougm #define	OPT_CKSUM	13
1603910Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
1617961SNatalie.Li@Sun.COM #define	OPT_NONE	14
1627961SNatalie.Li@Sun.COM 	{SHOPT_NONE, OPT_NONE, OPT_TYPE_ACCLIST},
1637961SNatalie.Li@Sun.COM #define	OPT_ROOT_MAPPING	15
1647961SNatalie.Li@Sun.COM 	{SHOPT_ROOT_MAPPING, OPT_ROOT_MAPPING, OPT_TYPE_USER},
1657961SNatalie.Li@Sun.COM #define	OPT_CHARSET_MAP	16
1667961SNatalie.Li@Sun.COM 	{"", OPT_CHARSET_MAP, OPT_TYPE_ACCLIST},
16711323SVallish.Vaidyeshwara@Sun.COM #define	OPT_NOACLFAB	17
16811323SVallish.Vaidyeshwara@Sun.COM 	{SHOPT_NOACLFAB, OPT_NOACLFAB, OPT_TYPE_BOOLEAN},
1693910Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
17011323SVallish.Vaidyeshwara@Sun.COM #define	OPT_VOLFH	18
1713910Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
1723910Sdougm #endif /* VOLATILE_FH_TEST */
1733910Sdougm 	NULL
1743910Sdougm };
1753910Sdougm 
1763910Sdougm /*
1777961SNatalie.Li@Sun.COM  * Codesets that may need to be converted to UTF-8 for file paths.
1787961SNatalie.Li@Sun.COM  * Add new names here to add new property support. If we ever get a
1797961SNatalie.Li@Sun.COM  * way to query the kernel for character sets, this should become
1807961SNatalie.Li@Sun.COM  * dynamically loaded. Make sure changes here are reflected in
1817961SNatalie.Li@Sun.COM  * cmd/fs.d/nfs/mountd/nfscmd.c
1827961SNatalie.Li@Sun.COM  */
1837961SNatalie.Li@Sun.COM 
1847961SNatalie.Li@Sun.COM static char *legal_conv[] = {
1857961SNatalie.Li@Sun.COM 	"euc-cn",
1867961SNatalie.Li@Sun.COM 	"euc-jp",
1877961SNatalie.Li@Sun.COM 	"euc-jpms",
1887961SNatalie.Li@Sun.COM 	"euc-kr",
1897961SNatalie.Li@Sun.COM 	"euc-tw",
1907961SNatalie.Li@Sun.COM 	"iso8859-1",
1917961SNatalie.Li@Sun.COM 	"iso8859-2",
1927961SNatalie.Li@Sun.COM 	"iso8859-5",
1937961SNatalie.Li@Sun.COM 	"iso8859-6",
1947961SNatalie.Li@Sun.COM 	"iso8859-7",
1957961SNatalie.Li@Sun.COM 	"iso8859-8",
1967961SNatalie.Li@Sun.COM 	"iso8859-9",
1977961SNatalie.Li@Sun.COM 	"iso8859-13",
1987961SNatalie.Li@Sun.COM 	"iso8859-15",
1997961SNatalie.Li@Sun.COM 	"koi8-r",
2007961SNatalie.Li@Sun.COM 	NULL
2017961SNatalie.Li@Sun.COM };
2027961SNatalie.Li@Sun.COM 
2037961SNatalie.Li@Sun.COM /*
2043910Sdougm  * list of properties that are related to security flavors.
2053910Sdougm  */
2063910Sdougm static char *seclist[] = {
2073910Sdougm 	SHOPT_RO,
2083910Sdougm 	SHOPT_RW,
2093910Sdougm 	SHOPT_ROOT,
2103910Sdougm 	SHOPT_WINDOW,
2117961SNatalie.Li@Sun.COM 	SHOPT_NONE,
2127961SNatalie.Li@Sun.COM 	SHOPT_ROOT_MAPPING,
2133910Sdougm 	NULL
2143910Sdougm };
2153910Sdougm 
2163910Sdougm /* structure for list of securities */
2173910Sdougm struct securities {
2183910Sdougm 	sa_security_t security;
2193910Sdougm 	struct securities *next;
2203910Sdougm };
2213910Sdougm 
2223910Sdougm /*
2237961SNatalie.Li@Sun.COM  * findcharset(charset)
2247961SNatalie.Li@Sun.COM  *
2257961SNatalie.Li@Sun.COM  * Returns B_TRUE if the charset is a legal conversion otherwise
2267961SNatalie.Li@Sun.COM  * B_FALSE. This will need to be rewritten to be more efficient when
2277961SNatalie.Li@Sun.COM  * we have a dynamic list of legal conversions.
2287961SNatalie.Li@Sun.COM  */
2297961SNatalie.Li@Sun.COM 
2307961SNatalie.Li@Sun.COM static boolean_t
findcharset(char * charset)2317961SNatalie.Li@Sun.COM findcharset(char *charset)
2327961SNatalie.Li@Sun.COM {
2337961SNatalie.Li@Sun.COM 	int i;
2347961SNatalie.Li@Sun.COM 
2357961SNatalie.Li@Sun.COM 	for (i = 0; legal_conv[i] != NULL; i++)
2367961SNatalie.Li@Sun.COM 		if (strcmp(charset, legal_conv[i]) == 0)
2377961SNatalie.Li@Sun.COM 			return (B_TRUE);
2387961SNatalie.Li@Sun.COM 	return (B_FALSE);
2397961SNatalie.Li@Sun.COM }
2407961SNatalie.Li@Sun.COM 
2417961SNatalie.Li@Sun.COM /*
2423910Sdougm  * findopt(name)
2433910Sdougm  *
2443910Sdougm  * Lookup option "name" in the option table and return the table
2453910Sdougm  * index.
2463910Sdougm  */
2473910Sdougm 
2483910Sdougm static int
findopt(char * name)2493910Sdougm findopt(char *name)
2503910Sdougm {
2513910Sdougm 	int i;
2523910Sdougm 	if (name != NULL) {
2534345Sdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
2544345Sdougm 			if (strcmp(optdefs[i].tag, name) == 0)
2554345Sdougm 				return (i);
2564345Sdougm 		}
2577961SNatalie.Li@Sun.COM 		if (findcharset(name))
2587961SNatalie.Li@Sun.COM 			return (OPT_CHARSET_MAP);
2593910Sdougm 	}
2603910Sdougm 	return (-1);
2613910Sdougm }
2623910Sdougm 
2633910Sdougm /*
2643910Sdougm  * gettype(name)
2653910Sdougm  *
2663910Sdougm  * Return the type of option "name".
2673910Sdougm  */
2683910Sdougm 
2693910Sdougm static int
gettype(char * name)2703910Sdougm gettype(char *name)
2713910Sdougm {
2723910Sdougm 	int optdef;
2733910Sdougm 
2743910Sdougm 	optdef = findopt(name);
2753910Sdougm 	if (optdef != -1)
2764345Sdougm 		return (optdefs[optdef].type);
2773910Sdougm 	return (OPT_TYPE_ANY);
2783910Sdougm }
2793910Sdougm 
2803910Sdougm /*
2813910Sdougm  * nfs_validate_security_mode(mode)
2823910Sdougm  *
2833910Sdougm  * is the specified mode string a valid one for use with NFS?
2843910Sdougm  */
2853910Sdougm 
2863910Sdougm static int
nfs_validate_security_mode(char * mode)2873910Sdougm nfs_validate_security_mode(char *mode)
2883910Sdougm {
2893910Sdougm 	seconfig_t secinfo;
2903910Sdougm 	int err;
2913910Sdougm 
2923910Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
2933910Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
2943910Sdougm 	if (err == SC_NOERROR)
2954345Sdougm 		return (1);
2963910Sdougm 	return (0);
2973910Sdougm }
2983910Sdougm 
2993910Sdougm /*
3003910Sdougm  * nfs_is_security_opt(tok)
3013910Sdougm  *
3023910Sdougm  * check to see if tok represents an option that is only valid in some
3033910Sdougm  * security flavor.
3043910Sdougm  */
3053910Sdougm 
3063910Sdougm static int
nfs_is_security_opt(char * tok)3073910Sdougm nfs_is_security_opt(char *tok)
3083910Sdougm {
3093910Sdougm 	int i;
3103910Sdougm 
3113910Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
3124345Sdougm 		if (strcmp(tok, seclist[i]) == 0)
3134345Sdougm 			return (1);
3143910Sdougm 	}
3153910Sdougm 	return (0);
3163910Sdougm }
3173910Sdougm 
3183910Sdougm /*
3193910Sdougm  * find_security(seclist, sec)
3203910Sdougm  *
3213910Sdougm  * Walk the current list of security flavors and return true if it is
3223910Sdougm  * present, else return false.
3233910Sdougm  */
3243910Sdougm 
3253910Sdougm static int
find_security(struct securities * seclist,sa_security_t sec)3263910Sdougm find_security(struct securities *seclist, sa_security_t sec)
3273910Sdougm {
3283910Sdougm 	while (seclist != NULL) {
3294345Sdougm 		if (seclist->security == sec)
3304345Sdougm 			return (1);
3314345Sdougm 		seclist = seclist->next;
3323910Sdougm 	}
3333910Sdougm 	return (0);
3343910Sdougm }
3353910Sdougm 
3363910Sdougm /*
3373910Sdougm  * make_security_list(group, securitymodes, proto)
3383910Sdougm  *	go through the list of securitymodes and add them to the
3393910Sdougm  *	group's list of security optionsets. We also keep a list of
3403910Sdougm  *	those optionsets so we don't have to find them later. All of
3413910Sdougm  *	these will get copies of the same properties.
3423910Sdougm  */
3433910Sdougm 
3443910Sdougm static struct securities *
make_security_list(sa_group_t group,char * securitymodes,char * proto)3453910Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
3463910Sdougm {
3473910Sdougm 	char *tok, *next = NULL;
3483910Sdougm 	struct securities *curp, *headp = NULL, *prev;
3493910Sdougm 	sa_security_t check;
3503910Sdougm 	int freetok = 0;
3513910Sdougm 
3523910Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
3534345Sdougm 		next = strchr(tok, ':');
3544345Sdougm 		if (next != NULL)
3554345Sdougm 			*next++ = '\0';
3564345Sdougm 		if (strcmp(tok, "default") == 0) {
3574345Sdougm 			/* resolve default into the real type */
3584345Sdougm 			tok = nfs_space_alias(tok);
3594345Sdougm 			freetok = 1;
3604345Sdougm 		}
3614345Sdougm 		check = sa_get_security(group, tok, proto);
3623910Sdougm 
3634345Sdougm 		/* add to the security list if it isn't there already */
3644345Sdougm 		if (check == NULL || !find_security(headp, check)) {
3654345Sdougm 			curp = (struct securities *)calloc(1,
3664345Sdougm 			    sizeof (struct securities));
3674345Sdougm 			if (curp != NULL) {
3684345Sdougm 				if (check == NULL) {
3694345Sdougm 					curp->security = sa_create_security(
3704345Sdougm 					    group, tok, proto);
3714345Sdougm 				} else {
3724345Sdougm 					curp->security = check;
3734345Sdougm 				}
3744345Sdougm 				/*
3754345Sdougm 				 * note that the first time through the loop,
3764345Sdougm 				 * headp will be NULL and prev will be
3774345Sdougm 				 * undefined.  Since headp is NULL, we set
3784345Sdougm 				 * both it and prev to the curp (first
3794345Sdougm 				 * structure to be allocated).
3804345Sdougm 				 *
3814345Sdougm 				 * later passes through the loop will have
3824345Sdougm 				 * headp not being NULL and prev will be used
3834345Sdougm 				 * to allocate at the end of the list.
3844345Sdougm 				 */
3854345Sdougm 				if (headp == NULL) {
3864345Sdougm 					headp = curp;
3874345Sdougm 					prev = curp;
3884345Sdougm 				} else {
3894345Sdougm 					prev->next = curp;
3904345Sdougm 					prev = curp;
3914345Sdougm 				}
3924345Sdougm 			}
3933910Sdougm 		}
3943910Sdougm 
3954345Sdougm 		if (freetok) {
3964345Sdougm 			freetok = 0;
3974345Sdougm 			sa_free_attr_string(tok);
3984345Sdougm 		}
3993910Sdougm 	}
4003910Sdougm 	return (headp);
4013910Sdougm }
4023910Sdougm 
4033910Sdougm static void
free_security_list(struct securities * sec)4043910Sdougm free_security_list(struct securities *sec)
4053910Sdougm {
4063910Sdougm 	struct securities *next;
4073910Sdougm 	if (sec != NULL) {
4084345Sdougm 		for (next = sec->next; sec != NULL; sec = next) {
4094345Sdougm 			next = sec->next;
4104345Sdougm 			free(sec);
4114345Sdougm 		}
4123910Sdougm 	}
4133910Sdougm }
4143910Sdougm 
4153910Sdougm /*
4163910Sdougm  * nfs_alistcat(str1, str2, sep)
4173910Sdougm  *
4183910Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
4193910Sdougm  * character. If memory allocation fails, return NULL;
4203910Sdougm  */
4213910Sdougm 
4223910Sdougm static char *
nfs_alistcat(char * str1,char * str2,char sep)4233910Sdougm nfs_alistcat(char *str1, char *str2, char sep)
4243910Sdougm {
4253910Sdougm 	char *newstr;
4263910Sdougm 	size_t len;
4273910Sdougm 
4283910Sdougm 	len = strlen(str1) + strlen(str2) + 2;
4293910Sdougm 	newstr = (char *)malloc(len);
4303910Sdougm 	if (newstr != NULL)
4314345Sdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
4323910Sdougm 	return (newstr);
4333910Sdougm }
4343910Sdougm 
4353910Sdougm /*
4363910Sdougm  * add_security_prop(sec, name, value, persist)
4373910Sdougm  *
4383910Sdougm  * Add the property to the securities structure. This accumulates
4393910Sdougm  * properties for as part of parsing legacy options.
4403910Sdougm  */
4413910Sdougm 
4423910Sdougm static int
add_security_prop(struct securities * sec,char * name,char * value,int persist,int iszfs)4433910Sdougm add_security_prop(struct securities *sec, char *name, char *value,
4443910Sdougm 			int persist, int iszfs)
4453910Sdougm {
4463910Sdougm 	sa_property_t prop;
4473910Sdougm 	int ret = SA_OK;
4483910Sdougm 
4493910Sdougm 	for (; sec != NULL; sec = sec->next) {
4504345Sdougm 		if (value == NULL) {
4514345Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
4524345Sdougm 			    strcmp(name, SHOPT_RO) == 0)
4534345Sdougm 				value = "*";
4544345Sdougm 			else
4554345Sdougm 				value = "true";
4564345Sdougm 		}
4573910Sdougm 
4583910Sdougm 		/*
4593910Sdougm 		 * Get the existing property, if it exists, so we can
4603910Sdougm 		 * determine what to do with it. The ro/rw/root
4613910Sdougm 		 * properties can be merged if multiple instances of
4623910Sdougm 		 * these properies are given. For example, if "rw"
4633910Sdougm 		 * exists with a value "host1" and a later token of
4643910Sdougm 		 * rw="host2" is seen, the values are merged into a
4653910Sdougm 		 * single rw="host1:host2".
4663910Sdougm 		 */
4674345Sdougm 		prop = sa_get_property(sec->security, name);
4683910Sdougm 
4694345Sdougm 		if (prop != NULL) {
4704345Sdougm 			char *oldvalue;
4714345Sdougm 			char *newvalue;
4723910Sdougm 
4733910Sdougm 			/*
4744345Sdougm 			 * The security options of ro/rw/root might appear
4754345Sdougm 			 * multiple times. If they do, the values need to be
4764345Sdougm 			 * merged into an access list. If it was previously
4774345Sdougm 			 * empty, the new value alone is added.
4783910Sdougm 			 */
4794345Sdougm 			oldvalue = sa_get_property_attr(prop, "value");
4804345Sdougm 			if (oldvalue != NULL) {
4814345Sdougm 				/*
4824345Sdougm 				 * The general case is to concatenate the new
4834345Sdougm 				 * value onto the old value for multiple
4844345Sdougm 				 * rw(ro/root) properties. A special case
4854345Sdougm 				 * exists when either the old or new is the
4864345Sdougm 				 * "all" case. In the special case, if both
4874345Sdougm 				 * are "all", then it is "all", else if one is
4884345Sdougm 				 * an access-list, that replaces the "all".
4894345Sdougm 				 */
4904345Sdougm 				if (strcmp(oldvalue, "*") == 0) {
4914345Sdougm 					/* Replace old value with new value. */
4924345Sdougm 					newvalue = strdup(value);
4935454Sdougm 				} else if (strcmp(value, "*") == 0 ||
4945454Sdougm 				    strcmp(oldvalue, value) == 0) {
4954345Sdougm 					/*
4964345Sdougm 					 * Keep old value and ignore
4974345Sdougm 					 * the new value.
4984345Sdougm 					 */
4994345Sdougm 					newvalue = NULL;
5004345Sdougm 				} else {
5014345Sdougm 					/*
5024345Sdougm 					 * Make a new list of old plus new
5034345Sdougm 					 * access-list.
5044345Sdougm 					 */
5054345Sdougm 					newvalue = nfs_alistcat(oldvalue,
5064345Sdougm 					    value, ':');
5074345Sdougm 				}
5083910Sdougm 
5094345Sdougm 				if (newvalue != NULL) {
5104345Sdougm 					(void) sa_remove_property(prop);
5114345Sdougm 					prop = sa_create_property(name,
5124345Sdougm 					    newvalue);
5134345Sdougm 					ret = sa_add_property(sec->security,
5144345Sdougm 					    prop);
5154345Sdougm 					free(newvalue);
5164345Sdougm 				}
5174345Sdougm 				if (oldvalue != NULL)
5184345Sdougm 					sa_free_attr_string(oldvalue);
5194345Sdougm 			}
5204345Sdougm 		} else {
5214345Sdougm 			prop = sa_create_property(name, value);
5223910Sdougm 			ret = sa_add_property(sec->security, prop);
5233910Sdougm 		}
5244345Sdougm 		if (ret == SA_OK && !iszfs) {
5254345Sdougm 			ret = sa_commit_properties(sec->security, !persist);
5264345Sdougm 		}
5273910Sdougm 	}
5283910Sdougm 	return (ret);
5293910Sdougm }
5303910Sdougm 
5313910Sdougm /*
5323910Sdougm  * check to see if group/share is persistent.
5333910Sdougm  */
5343910Sdougm static int
is_persistent(sa_group_t group)5353910Sdougm is_persistent(sa_group_t group)
5363910Sdougm {
5373910Sdougm 	char *type;
5383910Sdougm 	int persist = 1;
5393910Sdougm 
5403910Sdougm 	type = sa_get_group_attr(group, "type");
5413910Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
5424345Sdougm 		persist = 0;
5433910Sdougm 	if (type != NULL)
5444345Sdougm 		sa_free_attr_string(type);
5453910Sdougm 	return (persist);
5463910Sdougm }
5473910Sdougm 
5483910Sdougm /*
5493910Sdougm  * invalid_security(options)
5503910Sdougm  *
5513910Sdougm  * search option string for any invalid sec= type.
5523910Sdougm  * return true (1) if any are not valid else false (0)
5533910Sdougm  */
5543910Sdougm static int
invalid_security(char * options)5553910Sdougm invalid_security(char *options)
5563910Sdougm {
5573910Sdougm 	char *copy, *base, *token, *value;
5583910Sdougm 	int ret = 0;
5593910Sdougm 
5603910Sdougm 	copy = strdup(options);
5613910Sdougm 	token = base = copy;
5623910Sdougm 	while (token != NULL && ret == 0) {
5634345Sdougm 		token = strtok(base, ",");
5644345Sdougm 		base = NULL;
5654345Sdougm 		if (token != NULL) {
5664345Sdougm 			value = strchr(token, '=');
5674345Sdougm 			if (value != NULL)
5684345Sdougm 				*value++ = '\0';
5694345Sdougm 			if (strcmp(token, "sec") == 0) {
5704345Sdougm 				/* HAVE security flavors so check them */
5714345Sdougm 				char *tok, *next;
5724345Sdougm 				for (next = NULL, tok = value; tok != NULL;
5734345Sdougm 				    tok = next) {
5744345Sdougm 					next = strchr(tok, ':');
5754345Sdougm 					if (next != NULL)
5764345Sdougm 						*next++ = '\0';
5774345Sdougm 					ret = !nfs_validate_security_mode(tok);
5784345Sdougm 					if (ret)
5794345Sdougm 						break;
5804345Sdougm 				}
5814345Sdougm 			}
5823910Sdougm 		}
5833910Sdougm 	}
5843910Sdougm 	if (copy != NULL)
5854345Sdougm 		free(copy);
5863910Sdougm 	return (ret);
5873910Sdougm }
5883910Sdougm 
5893910Sdougm /*
5903910Sdougm  * nfs_parse_legacy_options(group, options)
5913910Sdougm  *
5923910Sdougm  * Parse the old style options into internal format and store on the
5933910Sdougm  * specified group.  Group could be a share for full legacy support.
5943910Sdougm  */
5953910Sdougm 
5963910Sdougm static int
nfs_parse_legacy_options(sa_group_t group,char * options)5973910Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
5983910Sdougm {
5994704Sdougm 	char *dup;
6003910Sdougm 	char *base;
6013910Sdougm 	char *token;
6023910Sdougm 	sa_optionset_t optionset;
6033910Sdougm 	struct securities *security_list = NULL;
6043910Sdougm 	sa_property_t prop;
6053910Sdougm 	int ret = SA_OK;
6063910Sdougm 	int iszfs = 0;
6073910Sdougm 	sa_group_t parent;
6083910Sdougm 	int persist = 0;
6093910Sdougm 	char *lasts;
6103910Sdougm 
6113910Sdougm 	/* do we have an existing optionset? */
6123910Sdougm 	optionset = sa_get_optionset(group, "nfs");
6133910Sdougm 	if (optionset == NULL) {
6144345Sdougm 		/* didn't find existing optionset so create one */
6154345Sdougm 		optionset = sa_create_optionset(group, "nfs");
6163910Sdougm 	} else {
6173910Sdougm 		/*
6185331Samw 		 * Have an existing optionset . Ideally, we would need
6195331Samw 		 * to compare options in order to detect errors. For
6205331Samw 		 * now, we assume that the first optionset is the
6215331Samw 		 * correct one and the others will be the same. An
6225331Samw 		 * empty optionset is the same as no optionset so we
6235331Samw 		 * don't want to exit in that case. Getting an empty
6245331Samw 		 * optionset can occur with ZFS property checking.
6253910Sdougm 		 */
6265331Samw 		if (sa_get_property(optionset, NULL) != NULL)
6275331Samw 			return (ret);
6283910Sdougm 	}
6293910Sdougm 
6303910Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
6313910Sdougm 		/*
6323910Sdougm 		 * there is a special case of only the option "rw"
6333910Sdougm 		 * being the default option. We don't have to do
6343910Sdougm 		 * anything.
6353910Sdougm 		 */
6364345Sdougm 		return (ret);
6373910Sdougm 	}
6383910Sdougm 
6393910Sdougm 	/*
6403910Sdougm 	 * check if security types are present and validate them. If
6413910Sdougm 	 * any are not legal, fail.
6423910Sdougm 	 */
6433910Sdougm 
6443910Sdougm 	if (invalid_security(options)) {
6454345Sdougm 		return (SA_INVALID_SECURITY);
6463910Sdougm 	}
6473910Sdougm 
6483910Sdougm 	/*
6493910Sdougm 	 * in order to not attempt to change ZFS properties unless
6503910Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
6513910Sdougm 	 */
6523910Sdougm 	if (sa_is_share(group)) {
6534345Sdougm 		char *zfs;
6544345Sdougm 		parent = sa_get_parent_group(group);
6554345Sdougm 		if (parent != NULL) {
6564345Sdougm 			zfs = sa_get_group_attr(parent, "zfs");
6574345Sdougm 			if (zfs != NULL) {
6584345Sdougm 				sa_free_attr_string(zfs);
6594345Sdougm 				iszfs++;
6604345Sdougm 			}
6613910Sdougm 		}
6623910Sdougm 	} else {
6634345Sdougm 		iszfs = sa_group_is_zfs(group);
6643910Sdougm 	}
6653910Sdougm 
6664704Sdougm 	/* We need a copy of options for the next part. */
6674704Sdougm 	dup = strdup(options);
6684704Sdougm 	if (dup == NULL)
6694704Sdougm 		return (SA_NO_MEMORY);
6704704Sdougm 
6713910Sdougm 	/*
6723910Sdougm 	 * we need to step through each option in the string and then
6733910Sdougm 	 * add either the option or the security option as needed. If
6743910Sdougm 	 * this is not a persistent share, don't commit to the
6753910Sdougm 	 * repository. If there is an error, we also want to abort the
6763910Sdougm 	 * processing and report it.
6773910Sdougm 	 */
6783910Sdougm 	persist = is_persistent(group);
6793910Sdougm 	base = dup;
6803910Sdougm 	token = dup;
6813910Sdougm 	lasts = NULL;
6823910Sdougm 	while (token != NULL && ret == SA_OK) {
6834345Sdougm 		ret = SA_OK;
6844345Sdougm 		token = strtok_r(base, ",", &lasts);
6854345Sdougm 		base = NULL;
6864345Sdougm 		if (token != NULL) {
6874345Sdougm 			char *value;
6883910Sdougm 			/*
6894345Sdougm 			 * if the option has a value, it will have an '=' to
6904345Sdougm 			 * separate the name from the value. The following
6914345Sdougm 			 * code will result in value != NULL and token
6924345Sdougm 			 * pointing to just the name if there is a value.
6933910Sdougm 			 */
6944345Sdougm 			value = strchr(token, '=');
6954345Sdougm 			if (value != NULL) {
6964345Sdougm 				*value++ = '\0';
6974345Sdougm 			}
6984345Sdougm 			if (strcmp(token, "sec") == 0 ||
6994345Sdougm 			    strcmp(token, "secure") == 0) {
7003910Sdougm 				/*
7014345Sdougm 				 * Once in security parsing, we only
7024345Sdougm 				 * do security. We do need to move
7034345Sdougm 				 * between the security node and the
7044345Sdougm 				 * toplevel. The security tag goes on
7054345Sdougm 				 * the root while the following ones
7064345Sdougm 				 * go on the security.
7073910Sdougm 				 */
7084345Sdougm 				if (security_list != NULL) {
7094345Sdougm 					/*
7104345Sdougm 					 * have an old list so close it and
7114345Sdougm 					 * start the new
7124345Sdougm 					 */
7134345Sdougm 					free_security_list(security_list);
7144345Sdougm 				}
7154345Sdougm 				if (strcmp(token, "secure") == 0) {
7164345Sdougm 					value = "dh";
7174345Sdougm 				} else {
7184345Sdougm 					if (value == NULL) {
7194345Sdougm 						ret = SA_SYNTAX_ERR;
7204345Sdougm 						break;
7214345Sdougm 					}
7224345Sdougm 				}
7234345Sdougm 				security_list = make_security_list(group,
7244345Sdougm 				    value, "nfs");
7253910Sdougm 			} else {
7264345Sdougm 				/*
7274345Sdougm 				 * Note that the "old" syntax allowed a
7284345Sdougm 				 * default security model This must be
7294345Sdougm 				 * accounted for and internally converted to
7304345Sdougm 				 * "standard" security structure.
7314345Sdougm 				 */
7324345Sdougm 				if (nfs_is_security_opt(token)) {
7334345Sdougm 					if (security_list == NULL) {
7344345Sdougm 						/*
7354345Sdougm 						 * need to have a
7364345Sdougm 						 * security
7374345Sdougm 						 * option. This will
7384345Sdougm 						 * be "closed" when a
7394345Sdougm 						 * defined "sec="
7404345Sdougm 						 * option is
7414345Sdougm 						 * seen. This is
7424345Sdougm 						 * technically an
7434345Sdougm 						 * error but will be
7444345Sdougm 						 * allowed with
7454345Sdougm 						 * warning.
7464345Sdougm 						 */
7474345Sdougm 						security_list =
7484345Sdougm 						    make_security_list(group,
7494345Sdougm 						    "default",
7504345Sdougm 						    "nfs");
7514345Sdougm 					}
7524345Sdougm 					if (security_list != NULL) {
7534345Sdougm 						ret = add_security_prop(
7544345Sdougm 						    security_list, token,
7554345Sdougm 						    value, persist, iszfs);
7564345Sdougm 					} else {
7574345Sdougm 						ret = SA_NO_MEMORY;
7584345Sdougm 					}
7594345Sdougm 				} else {
7604345Sdougm 					/* regular options */
7614345Sdougm 					if (value == NULL) {
7624345Sdougm 						if (strcmp(token, SHOPT_RW) ==
7634345Sdougm 						    0 || strcmp(token,
7644345Sdougm 						    SHOPT_RO) == 0) {
7654345Sdougm 							value = "*";
7664345Sdougm 						} else {
7674345Sdougm 							value = "global";
7684345Sdougm 							if (strcmp(token,
7694345Sdougm 							    SHOPT_LOG) != 0) {
7704345Sdougm 								value = "true";
7714345Sdougm 							}
7724345Sdougm 						}
7734345Sdougm 					}
7744372Sdougm 					/*
7754372Sdougm 					 * In all cases, create the
7764372Sdougm 					 * property specified. If the
7774372Sdougm 					 * value was NULL, the default
7784372Sdougm 					 * value will have been
7794372Sdougm 					 * substituted.
7804372Sdougm 					 */
7814372Sdougm 					prop = sa_create_property(token, value);
7824372Sdougm 					ret =  sa_add_property(optionset, prop);
7834372Sdougm 					if (ret != SA_OK)
7844372Sdougm 						break;
7854372Sdougm 
7864345Sdougm 					if (!iszfs) {
7874345Sdougm 						ret = sa_commit_properties(
7884345Sdougm 						    optionset, !persist);
7894345Sdougm 					}
7904345Sdougm 				}
7913910Sdougm 			}
7923910Sdougm 		}
7933910Sdougm 	}
7943910Sdougm 	if (security_list != NULL)
7954345Sdougm 		free_security_list(security_list);
7964704Sdougm 
7974704Sdougm 	free(dup);
7983910Sdougm 	return (ret);
7993910Sdougm }
8003910Sdougm 
8013910Sdougm /*
8023910Sdougm  * is_a_number(number)
8033910Sdougm  *
8043910Sdougm  * is the string a number in one of the forms we want to use?
8053910Sdougm  */
8063910Sdougm 
8073910Sdougm static int
is_a_number(char * number)8083910Sdougm is_a_number(char *number)
8093910Sdougm {
8103910Sdougm 	int ret = 1;
8113910Sdougm 	int hex = 0;
8123910Sdougm 
8133910Sdougm 	if (strncmp(number, "0x", 2) == 0) {
8144345Sdougm 		number += 2;
8154345Sdougm 		hex = 1;
8164345Sdougm 	} else if (*number == '-') {
8174345Sdougm 		number++; /* skip the minus */
8184345Sdougm 	}
8193910Sdougm 	while (ret == 1 && *number != '\0') {
8204345Sdougm 		if (hex) {
8214345Sdougm 			ret = isxdigit(*number++);
8224345Sdougm 		} else {
8234345Sdougm 			ret = isdigit(*number++);
8244345Sdougm 		}
8253910Sdougm 	}
8263910Sdougm 	return (ret);
8273910Sdougm }
8283910Sdougm 
8293910Sdougm /*
8303910Sdougm  * Look for the specified tag in the configuration file. If it is found,
8313910Sdougm  * enable logging and set the logging configuration information for exp.
8323910Sdougm  */
8333910Sdougm static void
configlog(struct exportdata * exp,char * tag)8343910Sdougm configlog(struct exportdata *exp, char *tag)
8353910Sdougm {
8363910Sdougm 	nfsl_config_t *configlist = NULL, *configp;
8373910Sdougm 	int error = 0;
8383910Sdougm 	char globaltag[] = DEFAULTTAG;
8393910Sdougm 
8403910Sdougm 	/*
8413910Sdougm 	 * Sends config errors to stderr
8423910Sdougm 	 */
8433910Sdougm 	nfsl_errs_to_syslog = B_FALSE;
8443910Sdougm 
8453910Sdougm 	/*
8463910Sdougm 	 * get the list of configuration settings
8473910Sdougm 	 */
8483910Sdougm 	error = nfsl_getconfig_list(&configlist);
8493910Sdougm 	if (error) {
8503910Sdougm 		(void) fprintf(stderr,
8514345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
8524345Sdougm 		    strerror(error));
8533910Sdougm 	}
8543910Sdougm 
8553910Sdougm 	if (tag == NULL)
8563910Sdougm 		tag = globaltag;
8573910Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
8583910Sdougm 		nfsl_freeconfig_list(&configlist);
8593910Sdougm 		(void) fprintf(stderr,
8604345Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
8613910Sdougm 		/* bad configuration */
8623910Sdougm 		error = ENOENT;
8633910Sdougm 		goto err;
8643910Sdougm 	}
8653910Sdougm 
8663910Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
8673910Sdougm 		error = ENOMEM;
8683910Sdougm 		goto out;
8693910Sdougm 	}
8703910Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
8713910Sdougm 		error = ENOMEM;
8723910Sdougm 		goto out;
8733910Sdougm 	}
8743910Sdougm 	exp->ex_flags |= EX_LOG;
8753910Sdougm 	if (configp->nc_rpclogpath != NULL)
8763910Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
8773910Sdougm out:
8783910Sdougm 	if (configlist != NULL)
8794345Sdougm 		nfsl_freeconfig_list(&configlist);
8803910Sdougm 
8813910Sdougm err:
8823910Sdougm 	if (error != 0) {
8833910Sdougm 		if (exp->ex_flags != NULL)
8843910Sdougm 			free(exp->ex_tag);
8853910Sdougm 		if (exp->ex_log_buffer != NULL)
8863910Sdougm 			free(exp->ex_log_buffer);
8873910Sdougm 		(void) fprintf(stderr,
8884345Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
8894345Sdougm 		    strerror(error));
8903910Sdougm 	}
8913910Sdougm }
8923910Sdougm 
8933910Sdougm /*
8943910Sdougm  * fill_export_from_optionset(export, optionset)
8953910Sdougm  *
8963910Sdougm  * In order to share, we need to set all the possible general options
8973910Sdougm  * into the export structure. Share info will be filled in by the
8983910Sdougm  * caller. Various property values get turned into structure specific
8993910Sdougm  * values.
9003910Sdougm  */
9013910Sdougm 
9023910Sdougm static int
fill_export_from_optionset(struct exportdata * export,sa_optionset_t optionset)9033910Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
9043910Sdougm {
9053910Sdougm 	sa_property_t option;
9063910Sdougm 	int ret = SA_OK;
9073910Sdougm 
9083910Sdougm 	for (option = sa_get_property(optionset, NULL);
9094345Sdougm 	    option != NULL; option = sa_get_next_property(option)) {
9104345Sdougm 		char *name;
9114345Sdougm 		char *value;
9124345Sdougm 		uint32_t val;
9133910Sdougm 
9144345Sdougm 		/*
9154345Sdougm 		 * since options may be set/reset multiple times, always do an
9164345Sdougm 		 * explicit set or clear of the option. This allows defaults
9175331Samw 		 * to be set and then the protocol specific to override.
9184345Sdougm 		 */
9193910Sdougm 
9204345Sdougm 		name = sa_get_property_attr(option, "type");
9214345Sdougm 		value = sa_get_property_attr(option, "value");
9224345Sdougm 		switch (findopt(name)) {
9234345Sdougm 		case OPT_ANON:
9244345Sdougm 			if (value != NULL && is_a_number(value)) {
9254345Sdougm 				val = strtoul(value, NULL, 0);
9264345Sdougm 			} else {
9274345Sdougm 				struct passwd *pw;
9284345Sdougm 				pw = getpwnam(value != NULL ? value : "nobody");
9294345Sdougm 				if (pw != NULL) {
9304345Sdougm 					val = pw->pw_uid;
9314345Sdougm 				} else {
9324345Sdougm 					val = UID_NOBODY;
9334345Sdougm 				}
9344345Sdougm 				endpwent();
9354345Sdougm 			}
9364345Sdougm 			export->ex_anon = val;
9374345Sdougm 			break;
9384345Sdougm 		case OPT_NOSUID:
9394345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9404345Sdougm 			    strcmp(value, "1") == 0))
9414345Sdougm 				export->ex_flags |= EX_NOSUID;
9424345Sdougm 			else
9434345Sdougm 				export->ex_flags &= ~EX_NOSUID;
9444345Sdougm 			break;
9454345Sdougm 		case OPT_ACLOK:
9464345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9474345Sdougm 			    strcmp(value, "1") == 0))
9484345Sdougm 				export->ex_flags |= EX_ACLOK;
9494345Sdougm 			else
9504345Sdougm 				export->ex_flags &= ~EX_ACLOK;
9514345Sdougm 			break;
9524345Sdougm 		case OPT_NOSUB:
9534345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9544345Sdougm 			    strcmp(value, "1") == 0))
9554345Sdougm 				export->ex_flags |= EX_NOSUB;
9564345Sdougm 			else
9574345Sdougm 				export->ex_flags &= ~EX_NOSUB;
9584345Sdougm 			break;
9594345Sdougm 		case OPT_PUBLIC:
9604345Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
9614345Sdougm 			    strcmp(value, "1") == 0))
9624345Sdougm 				export->ex_flags |= EX_PUBLIC;
9634345Sdougm 			else
9644345Sdougm 				export->ex_flags &= ~EX_PUBLIC;
9654345Sdougm 			break;
9664345Sdougm 		case OPT_INDEX:
9674345Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
9684345Sdougm 			    strchr(value, '/') != NULL)) {
9694345Sdougm 				/* this is an error */
9704345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
9714345Sdougm 				    "NFS: index=\"%s\" not valid;"
9724345Sdougm 				    "must be a filename.\n"),
9734345Sdougm 				    value);
9744345Sdougm 				break;
9754345Sdougm 			}
9764345Sdougm 			if (value != NULL && *value != '\0' &&
9774345Sdougm 			    strcmp(value, ".") != 0) {
9784345Sdougm 				/* valid index file string */
9794345Sdougm 				if (export->ex_index != NULL) {
9804345Sdougm 					/* left over from "default" */
9814345Sdougm 					free(export->ex_index);
9824345Sdougm 				}
9834345Sdougm 				/* remember to free */
9844345Sdougm 				export->ex_index = strdup(value);
9854345Sdougm 				if (export->ex_index == NULL) {
9864345Sdougm 					(void) printf(dgettext(TEXT_DOMAIN,
9874345Sdougm 					    "NFS: out of memory setting "
9884345Sdougm 					    "index property\n"));
9894345Sdougm 					break;
9904345Sdougm 				}
9914345Sdougm 				export->ex_flags |= EX_INDEX;
9924345Sdougm 			}
9934345Sdougm 			break;
9944345Sdougm 		case OPT_LOG:
9954345Sdougm 			if (value == NULL)
9964345Sdougm 				value = strdup("global");
9974345Sdougm 			if (value != NULL)
9984345Sdougm 				configlog(export,
9994345Sdougm 				    strlen(value) ? value : "global");
10004345Sdougm 			break;
10017961SNatalie.Li@Sun.COM 		case OPT_CHARSET_MAP:
10027961SNatalie.Li@Sun.COM 			/*
10037961SNatalie.Li@Sun.COM 			 * Set EX_CHARMAP when there is at least one
10047961SNatalie.Li@Sun.COM 			 * charmap conversion property. This will get
10057961SNatalie.Li@Sun.COM 			 * checked by the nfs server when it needs to.
10067961SNatalie.Li@Sun.COM 			 */
10077961SNatalie.Li@Sun.COM 			export->ex_flags |= EX_CHARMAP;
10087961SNatalie.Li@Sun.COM 			break;
100911323SVallish.Vaidyeshwara@Sun.COM 		case OPT_NOACLFAB:
101011323SVallish.Vaidyeshwara@Sun.COM 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
101111323SVallish.Vaidyeshwara@Sun.COM 			    strcmp(value, "1") == 0))
101211323SVallish.Vaidyeshwara@Sun.COM 				export->ex_flags |= EX_NOACLFAB;
101311323SVallish.Vaidyeshwara@Sun.COM 			else
101411323SVallish.Vaidyeshwara@Sun.COM 				export->ex_flags &= ~EX_NOACLFAB;
101511323SVallish.Vaidyeshwara@Sun.COM 			break;
10164345Sdougm 		default:
10174345Sdougm 			/* have a syntactic error */
10184345Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
10194345Sdougm 			    "NFS: unrecognized option %s=%s\n"),
102011337SWilliam.Krier@Sun.COM 			    name != NULL ? name : "",
102111337SWilliam.Krier@Sun.COM 			    value != NULL ? value : "");
10224345Sdougm 			break;
10233910Sdougm 		}
10244345Sdougm 		if (name != NULL)
10254345Sdougm 			sa_free_attr_string(name);
10263910Sdougm 		if (value != NULL)
10274345Sdougm 			sa_free_attr_string(value);
10283910Sdougm 	}
10293910Sdougm 	return (ret);
10303910Sdougm }
10313910Sdougm 
10323910Sdougm /*
10333910Sdougm  * cleanup_export(export)
10343910Sdougm  *
10353910Sdougm  * Cleanup the allocated areas so we don't leak memory
10363910Sdougm  */
10373910Sdougm 
10383910Sdougm static void
cleanup_export(struct exportdata * export)10393910Sdougm cleanup_export(struct exportdata *export)
10403910Sdougm {
10413910Sdougm 	int i;
10423910Sdougm 
10433910Sdougm 	if (export->ex_index != NULL)
10444345Sdougm 		free(export->ex_index);
10453910Sdougm 	if (export->ex_secinfo != NULL) {
10464345Sdougm 		for (i = 0; i < export->ex_seccnt; i++)
10474345Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
10484345Sdougm 				free(export->ex_secinfo[i].s_rootnames);
10494345Sdougm 		free(export->ex_secinfo);
10503910Sdougm 	}
10513910Sdougm }
10523910Sdougm 
10533910Sdougm /*
10543910Sdougm  * Given a seconfig entry and a colon-separated
10553910Sdougm  * list of names, allocate an array big enough
10563910Sdougm  * to hold the root list, then convert each name to
10573910Sdougm  * a principal name according to the security
10583910Sdougm  * info and assign it to an array element.
10593910Sdougm  * Return the array and its size.
10603910Sdougm  */
10613910Sdougm static caddr_t *
get_rootnames(seconfig_t * sec,char * list,int * count)10623910Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
10633910Sdougm {
10643910Sdougm 	caddr_t *a;
10653910Sdougm 	int c, i;
10663910Sdougm 	char *host, *p;
10673910Sdougm 
10683910Sdougm 	/*
10693910Sdougm 	 * Count the number of strings in the list.
10703910Sdougm 	 * This is the number of colon separators + 1.
10713910Sdougm 	 */
10723910Sdougm 	c = 1;
10733910Sdougm 	for (p = list; *p; p++)
10743910Sdougm 		if (*p == ':')
10753910Sdougm 			c++;
10763910Sdougm 	*count = c;
10773910Sdougm 
10783910Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
10793910Sdougm 	if (a == NULL) {
10803910Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
10814345Sdougm 		    "get_rootnames: no memory\n"));
10823910Sdougm 	} else {
10834345Sdougm 		for (i = 0; i < c; i++) {
10844345Sdougm 			host = strtok(list, ":");
10854345Sdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
10864345Sdougm 				free(a);
10874345Sdougm 				a = NULL;
10884345Sdougm 				break;
10894345Sdougm 			}
10904345Sdougm 			list = NULL;
10913910Sdougm 		}
10923910Sdougm 	}
10933910Sdougm 
10943910Sdougm 	return (a);
10953910Sdougm }
10963910Sdougm 
10973910Sdougm /*
10983910Sdougm  * fill_security_from_secopts(sp, secopts)
10993910Sdougm  *
11003910Sdougm  * Fill the secinfo structure from the secopts optionset.
11013910Sdougm  */
11023910Sdougm 
11033910Sdougm static int
fill_security_from_secopts(struct secinfo * sp,sa_security_t secopts)11043910Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
11053910Sdougm {
11063910Sdougm 	sa_property_t prop;
11073910Sdougm 	char *type;
11083910Sdougm 	int longform;
11093910Sdougm 	int err = SC_NOERROR;
11107961SNatalie.Li@Sun.COM 	uint32_t val;
11113910Sdougm 
11123910Sdougm 	type = sa_get_security_attr(secopts, "sectype");
11133910Sdougm 	if (type != NULL) {
11144345Sdougm 		/* named security type needs secinfo to be filled in */
11154345Sdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
11164345Sdougm 		sa_free_attr_string(type);
11174345Sdougm 		if (err != SC_NOERROR)
11184345Sdougm 			return (err);
11193910Sdougm 	} else {
11204345Sdougm 		/* default case */
11214345Sdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
11224345Sdougm 		if (err != SC_NOERROR)
11234345Sdougm 			return (err);
11243910Sdougm 	}
11253910Sdougm 
11263910Sdougm 	err = SA_OK;
11273910Sdougm 	for (prop = sa_get_property(secopts, NULL);
11284345Sdougm 	    prop != NULL && err == SA_OK;
11294345Sdougm 	    prop = sa_get_next_property(prop)) {
11304345Sdougm 		char *name;
11314345Sdougm 		char *value;
11323910Sdougm 
11334345Sdougm 		name = sa_get_property_attr(prop, "type");
11344345Sdougm 		value = sa_get_property_attr(prop, "value");
11353910Sdougm 
11364345Sdougm 		longform = value != NULL && strcmp(value, "*") != 0;
11373910Sdougm 
11384345Sdougm 		switch (findopt(name)) {
11394345Sdougm 		case OPT_RO:
11404345Sdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
11414345Sdougm 			break;
11424345Sdougm 		case OPT_RW:
11434345Sdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
11444345Sdougm 			break;
11454345Sdougm 		case OPT_ROOT:
11464345Sdougm 			sp->s_flags |= M_ROOT;
11474345Sdougm 			/*
11484345Sdougm 			 * if we are using AUTH_UNIX, handle like other things
11494345Sdougm 			 * such as RO/RW
11504345Sdougm 			 */
11514345Sdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
11524345Sdougm 				continue;
11534345Sdougm 			/* not AUTH_UNIX */
11544345Sdougm 			if (value != NULL) {
11554345Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
11564345Sdougm 				    value, &sp->s_rootcnt);
11574345Sdougm 				if (sp->s_rootnames == NULL) {
11584345Sdougm 					err = SA_BAD_VALUE;
11594345Sdougm 					(void) fprintf(stderr,
11604345Sdougm 					    dgettext(TEXT_DOMAIN,
11614345Sdougm 					    "Bad root list\n"));
11624345Sdougm 				}
11634345Sdougm 			}
11644345Sdougm 			break;
11657961SNatalie.Li@Sun.COM 		case OPT_NONE:
11667961SNatalie.Li@Sun.COM 			sp->s_flags |= M_NONE;
11677961SNatalie.Li@Sun.COM 			break;
11684345Sdougm 		case OPT_WINDOW:
11694345Sdougm 			if (value != NULL) {
11704345Sdougm 				sp->s_window = atoi(value);
11714345Sdougm 				/* just in case */
11724345Sdougm 				if (sp->s_window < 0)
11734345Sdougm 					sp->s_window = DEF_WIN;
11744345Sdougm 			}
11754345Sdougm 			break;
11767961SNatalie.Li@Sun.COM 		case OPT_ROOT_MAPPING:
11777961SNatalie.Li@Sun.COM 			if (value != NULL && is_a_number(value)) {
11787961SNatalie.Li@Sun.COM 				val = strtoul(value, NULL, 0);
11797961SNatalie.Li@Sun.COM 			} else {
11807961SNatalie.Li@Sun.COM 				struct passwd *pw;
11817961SNatalie.Li@Sun.COM 				pw = getpwnam(value != NULL ? value : "nobody");
11827961SNatalie.Li@Sun.COM 				if (pw != NULL) {
11837961SNatalie.Li@Sun.COM 					val = pw->pw_uid;
11847961SNatalie.Li@Sun.COM 				} else {
11857961SNatalie.Li@Sun.COM 					val = UID_NOBODY;
11867961SNatalie.Li@Sun.COM 				}
11877961SNatalie.Li@Sun.COM 				endpwent();
11887961SNatalie.Li@Sun.COM 			}
11897961SNatalie.Li@Sun.COM 			sp->s_rootid = val;
11907961SNatalie.Li@Sun.COM 			break;
11914345Sdougm 		default:
11924345Sdougm 			break;
11933910Sdougm 		}
11944345Sdougm 		if (name != NULL)
11954345Sdougm 			sa_free_attr_string(name);
11964345Sdougm 		if (value != NULL)
11974345Sdougm 			sa_free_attr_string(value);
11983910Sdougm 	}
11993910Sdougm 	/* if rw/ro options not set, use default of RW */
12003910Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
12014345Sdougm 		sp->s_flags |= M_RW;
12023910Sdougm 	return (err);
12033910Sdougm }
12043910Sdougm 
12053910Sdougm /*
12063910Sdougm  * This is for testing only
12073910Sdougm  * It displays the export structure that
12083910Sdougm  * goes into the kernel.
12093910Sdougm  */
12103910Sdougm static void
printarg(char * path,struct exportdata * ep)12113910Sdougm printarg(char *path, struct exportdata *ep)
12123910Sdougm {
12133910Sdougm 	int i, j;
12143910Sdougm 	struct secinfo *sp;
12153910Sdougm 
12163910Sdougm 	if (debug == 0)
12174345Sdougm 		return;
12183910Sdougm 
12193910Sdougm 	(void) printf("%s:\n", path);
12203910Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
12213910Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
12223910Sdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
12233910Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
12243910Sdougm 	if (ep->ex_flags & EX_NOSUID)
12253910Sdougm 		(void) printf("NOSUID ");
12263910Sdougm 	if (ep->ex_flags & EX_ACLOK)
12273910Sdougm 		(void) printf("ACLOK ");
12283910Sdougm 	if (ep->ex_flags & EX_PUBLIC)
12293910Sdougm 		(void) printf("PUBLIC ");
12303910Sdougm 	if (ep->ex_flags & EX_NOSUB)
12313910Sdougm 		(void) printf("NOSUB ");
12323910Sdougm 	if (ep->ex_flags & EX_LOG)
12333910Sdougm 		(void) printf("LOG ");
12347961SNatalie.Li@Sun.COM 	if (ep->ex_flags & EX_CHARMAP)
12357961SNatalie.Li@Sun.COM 		(void) printf("CHARMAP ");
12363910Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
12373910Sdougm 		(void) printf("LOG_ALLOPS ");
12383910Sdougm 	if (ep->ex_flags == 0)
12393910Sdougm 		(void) printf("(none)");
12403910Sdougm 	(void) 	printf("\n");
12413910Sdougm 	if (ep->ex_flags & EX_LOG) {
12423910Sdougm 		(void) printf("\tex_log_buffer = %s\n",
12434345Sdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
12443910Sdougm 		(void) printf("\tex_tag = %s\n",
12454345Sdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
12463910Sdougm 	}
12473910Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
12483910Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
12493910Sdougm 	(void) printf("\n");
12503910Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
12513910Sdougm 		sp = &ep->ex_secinfo[i];
12523910Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
12533910Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
12543910Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
12553910Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
12563910Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
12573910Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
12583910Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
12597961SNatalie.Li@Sun.COM 		if (sp->s_flags & M_NONE) (void) printf("M_NONE ");
12603910Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
12613910Sdougm 		(void) printf("\n");
12623910Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
12637961SNatalie.Li@Sun.COM 		(void) printf("\t\ts_rootid = %d\n", sp->s_rootid);
12643910Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
12653910Sdougm 		(void) fflush(stdout);
12663910Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
12673910Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
12684345Sdougm 			    sp->s_rootnames[j] : "<null>");
12693910Sdougm 		(void) printf("\n\n");
12703910Sdougm 	}
12713910Sdougm }
12723910Sdougm 
12733910Sdougm /*
12743910Sdougm  * count_security(opts)
12753910Sdougm  *
12763910Sdougm  * Count the number of security types (flavors). The optionset has
12773910Sdougm  * been populated with the security flavors as a holding mechanism.
12783910Sdougm  * We later use this number to allocate data structures.
12793910Sdougm  */
12803910Sdougm 
12813910Sdougm static int
count_security(sa_optionset_t opts)12823910Sdougm count_security(sa_optionset_t opts)
12833910Sdougm {
12843910Sdougm 	int count = 0;
12853910Sdougm 	sa_property_t prop;
12863910Sdougm 	if (opts != NULL) {
12874345Sdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
12884345Sdougm 		    prop = sa_get_next_property(prop)) {
12894345Sdougm 			count++;
12904345Sdougm 		}
12913910Sdougm 	}
12923910Sdougm 	return (count);
12933910Sdougm }
12943910Sdougm 
12953910Sdougm /*
12963910Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
12973910Sdougm  *
12983910Sdougm  * provides a mechanism to format NFS properties into legacy output
12993910Sdougm  * format. If the buffer would overflow, it is reallocated and grown
13003910Sdougm  * as appropriate. Special cases of converting internal form of values
13013910Sdougm  * to those used by "share" are done. this function does one property
13023910Sdougm  * at a time.
13033910Sdougm  */
13043910Sdougm 
13055179Sdougm static int
nfs_sprint_option(char ** rbuff,size_t * rbuffsize,size_t incr,sa_property_t prop,int sep)13063910Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
13073910Sdougm 			sa_property_t prop, int sep)
13083910Sdougm {
13093910Sdougm 	char *name;
13103910Sdougm 	char *value;
13113910Sdougm 	int curlen;
13123910Sdougm 	char *buff = *rbuff;
13133910Sdougm 	size_t buffsize = *rbuffsize;
13145179Sdougm 	int printed = B_FALSE;
13153910Sdougm 
13163910Sdougm 	name = sa_get_property_attr(prop, "type");
13173910Sdougm 	value = sa_get_property_attr(prop, "value");
13183910Sdougm 	if (buff != NULL)
13194345Sdougm 		curlen = strlen(buff);
13203910Sdougm 	else
13214345Sdougm 		curlen = 0;
13223910Sdougm 	if (name != NULL) {
13234345Sdougm 		int len;
13244345Sdougm 		len = strlen(name) + sep;
13253910Sdougm 
13263910Sdougm 		/*
13273910Sdougm 		 * A future RFE would be to replace this with more
13283910Sdougm 		 * generic code and to possibly handle more types.
13293910Sdougm 		 */
13304345Sdougm 		switch (gettype(name)) {
13314345Sdougm 		case OPT_TYPE_BOOLEAN:
13325179Sdougm 			/*
13335179Sdougm 			 * For NFS, boolean value of FALSE means it
13345179Sdougm 			 * doesn't show up in the option list at all.
13355179Sdougm 			 */
13364345Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
13375179Sdougm 				goto skip;
13385179Sdougm 			if (value != NULL) {
13394345Sdougm 				sa_free_attr_string(value);
13405179Sdougm 				value = NULL;
13415179Sdougm 			}
13424345Sdougm 			break;
13434345Sdougm 		case OPT_TYPE_ACCLIST:
13444345Sdougm 			if (value != NULL && strcmp(value, "*") == 0) {
13454345Sdougm 				sa_free_attr_string(value);
13464345Sdougm 				value = NULL;
13474345Sdougm 			} else {
13484345Sdougm 				if (value != NULL)
13494345Sdougm 					len += 1 + strlen(value);
13504345Sdougm 			}
13514345Sdougm 			break;
13524345Sdougm 		case OPT_TYPE_LOGTAG:
13534345Sdougm 			if (value != NULL && strlen(value) == 0) {
13544345Sdougm 				sa_free_attr_string(value);
13554345Sdougm 				value = NULL;
13564345Sdougm 			} else {
13574345Sdougm 				if (value != NULL)
13584345Sdougm 					len += 1 + strlen(value);
13594345Sdougm 			}
13604345Sdougm 			break;
13614345Sdougm 		default:
13624345Sdougm 			if (value != NULL)
13634345Sdougm 				len += 1 + strlen(value);
13644345Sdougm 			break;
13653910Sdougm 		}
13664345Sdougm 		while (buffsize <= (curlen + len)) {
13674345Sdougm 			/* need more room */
13684345Sdougm 			buffsize += incr;
13694345Sdougm 			buff = realloc(buff, buffsize);
13704345Sdougm 			if (buff == NULL) {
13714345Sdougm 				/* realloc failed so free everything */
13724345Sdougm 				if (*rbuff != NULL)
13734345Sdougm 					free(*rbuff);
13744345Sdougm 			}
13754345Sdougm 			*rbuff = buff;
13764345Sdougm 			*rbuffsize = buffsize;
13775179Sdougm 			if (buff == NULL)
13785179Sdougm 				goto skip;
13795179Sdougm 
13803910Sdougm 		}
13815179Sdougm 
13824345Sdougm 		if (buff == NULL)
13835179Sdougm 			goto skip;
13845179Sdougm 
13854345Sdougm 		if (value == NULL) {
13864345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
13874345Sdougm 			    "%s%s", sep ? "," : "",
13884345Sdougm 			    name, value != NULL ? value : "");
13894345Sdougm 		} else {
13904345Sdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
13914345Sdougm 			    "%s%s=%s", sep ? "," : "",
13924345Sdougm 			    name, value != NULL ? value : "");
13933910Sdougm 		}
13945179Sdougm 		printed = B_TRUE;
13953910Sdougm 	}
13965179Sdougm skip:
13973910Sdougm 	if (name != NULL)
13984345Sdougm 		sa_free_attr_string(name);
13993910Sdougm 	if (value != NULL)
14004345Sdougm 		sa_free_attr_string(value);
14015179Sdougm 	return (printed);
14023910Sdougm }
14033910Sdougm 
14043910Sdougm /*
14053910Sdougm  * nfs_format_options(group, hier)
14063910Sdougm  *
14073910Sdougm  * format all the options on the group into an old-style option
14083910Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
14093910Sdougm  * options.
14103910Sdougm  */
14113910Sdougm 
14123910Sdougm static char *
nfs_format_options(sa_group_t group,int hier)14133910Sdougm nfs_format_options(sa_group_t group, int hier)
14143910Sdougm {
14153910Sdougm 	sa_optionset_t options = NULL;
14164345Sdougm 	sa_optionset_t secoptions = NULL;
14173910Sdougm 	sa_property_t prop, secprop;
14184345Sdougm 	sa_security_t security = NULL;
14193910Sdougm 	char *buff;
14203910Sdougm 	size_t buffsize;
14214345Sdougm 	char *sectype = NULL;
14224345Sdougm 	int sep = 0;
14234345Sdougm 
14244345Sdougm 
14254345Sdougm 	buff = malloc(OPT_CHUNK);
14264345Sdougm 	if (buff == NULL) {
14274345Sdougm 		return (NULL);
14284345Sdougm 	}
14294345Sdougm 
14304345Sdougm 	buff[0] = '\0';
14314345Sdougm 	buffsize = OPT_CHUNK;
14324345Sdougm 
14334345Sdougm 	/*
14344345Sdougm 	 * We may have a an optionset relative to this item. format
14354345Sdougm 	 * these if we find them and then add any security definitions.
14364345Sdougm 	 */
14373910Sdougm 
14383910Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
14393910Sdougm 
14403910Sdougm 	/*
14414345Sdougm 	 * do the default set first but skip any option that is also
14424345Sdougm 	 * in the protocol specific optionset.
14433910Sdougm 	 */
14444345Sdougm 	if (options != NULL) {
14454345Sdougm 		for (prop = sa_get_property(options, NULL);
14464345Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
14473910Sdougm 			/*
14484345Sdougm 			 * use this one since we skipped any
14494345Sdougm 			 * of these that were also in
14504345Sdougm 			 * optdefault
14513910Sdougm 			 */
14525179Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
14535179Sdougm 			    prop, sep))
14545179Sdougm 				sep = 1;
14554345Sdougm 			if (buff == NULL) {
14564345Sdougm 				/*
14574345Sdougm 				 * buff could become NULL if there
14584345Sdougm 				 * isn't enough memory for
14594345Sdougm 				 * nfs_sprint_option to realloc()
14604345Sdougm 				 * as necessary. We can't really
14614345Sdougm 				 * do anything about it at this
14624345Sdougm 				 * point so we return NULL.  The
14634345Sdougm 				 * caller should handle the
14644345Sdougm 				 * failure.
14654345Sdougm 				 */
14664345Sdougm 				if (options != NULL)
14674345Sdougm 					sa_free_derived_optionset(
14684345Sdougm 					    options);
14694345Sdougm 				return (buff);
14704345Sdougm 			}
14713910Sdougm 		}
14724345Sdougm 	}
14734345Sdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
14744345Sdougm 	    "nfs", hier);
14754345Sdougm 	if (secoptions != NULL) {
14763910Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
14774345Sdougm 		    secprop != NULL;
14784345Sdougm 		    secprop = sa_get_next_property(secprop)) {
14794345Sdougm 			sectype = sa_get_property_attr(secprop, "type");
14804345Sdougm 			security =
14814345Sdougm 			    (sa_security_t)sa_get_derived_security(
14824345Sdougm 			    group, sectype, "nfs", hier);
14834345Sdougm 			if (security != NULL) {
14844345Sdougm 				if (sectype != NULL) {
14854345Sdougm 					prop = sa_create_property(
14864345Sdougm 					    "sec", sectype);
14875179Sdougm 					if (prop == NULL)
14885179Sdougm 						goto err;
14895179Sdougm 					if (nfs_sprint_option(&buff,
14905179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
14915179Sdougm 						sep = 1;
14924345Sdougm 					(void) sa_remove_property(prop);
14935179Sdougm 					if (buff == NULL)
14945179Sdougm 						goto err;
14954345Sdougm 				}
14964345Sdougm 				for (prop = sa_get_property(security,
14974345Sdougm 				    NULL); prop != NULL;
14984345Sdougm 				    prop = sa_get_next_property(prop)) {
14995179Sdougm 					if (nfs_sprint_option(&buff,
15005179Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
15015179Sdougm 						sep = 1;
15024345Sdougm 					if (buff == NULL)
15034345Sdougm 						goto err;
15044345Sdougm 				}
15054345Sdougm 				sa_free_derived_optionset(security);
15063910Sdougm 			}
15074345Sdougm 			if (sectype != NULL)
15084345Sdougm 				sa_free_attr_string(sectype);
15093910Sdougm 		}
15103910Sdougm 		sa_free_derived_optionset(secoptions);
15113910Sdougm 	}
15124345Sdougm 
15133910Sdougm 	if (options != NULL)
15144345Sdougm 		sa_free_derived_optionset(options);
15154345Sdougm 	return (buff);
15164345Sdougm 
15174345Sdougm err:
15184345Sdougm 	/*
15194345Sdougm 	 * If we couldn't allocate memory for option printing, we need
15204345Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
15214345Sdougm 	 */
15224345Sdougm 	if (secoptions != NULL)
15234345Sdougm 		sa_free_derived_optionset(secoptions);
15244345Sdougm 	if (security != NULL)
15254345Sdougm 		sa_free_derived_optionset(security);
15264345Sdougm 	if (sectype != NULL)
15274345Sdougm 		sa_free_attr_string(sectype);
15284345Sdougm 	if (options != NULL)
15294345Sdougm 		sa_free_derived_optionset(options);
1530*13080SPavan.Mettu@Oracle.COM 	return (buff);
15313910Sdougm }
15324345Sdougm 
15333910Sdougm /*
15343910Sdougm  * Append an entry to the nfslogtab file
15353910Sdougm  */
15363910Sdougm static int
nfslogtab_add(dir,buffer,tag)15373910Sdougm nfslogtab_add(dir, buffer, tag)
15383910Sdougm 	char *dir, *buffer, *tag;
15393910Sdougm {
15403910Sdougm 	FILE *f;
15413910Sdougm 	struct logtab_ent lep;
15423910Sdougm 	int error = 0;
15433910Sdougm 
15443910Sdougm 	/*
15453910Sdougm 	 * Open the file for update and create it if necessary.
15463910Sdougm 	 * This may leave the I/O offset at the end of the file,
15473910Sdougm 	 * so rewind back to the beginning of the file.
15483910Sdougm 	 */
15493910Sdougm 	f = fopen(NFSLOGTAB, "a+");
15503910Sdougm 	if (f == NULL) {
15513910Sdougm 		error = errno;
15523910Sdougm 		goto out;
15533910Sdougm 	}
15543910Sdougm 	rewind(f);
15553910Sdougm 
15563910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
15573910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15584345Sdougm 		    "share complete, however failed to lock %s "
15594345Sdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
15603910Sdougm 		error = -1;
15613910Sdougm 		goto out;
15623910Sdougm 	}
15633910Sdougm 
15643910Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
15653910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15664345Sdougm 		    "share complete, however could not deactivate "
15674345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15683910Sdougm 		error = -1;
15693910Sdougm 		goto out;
15703910Sdougm 	}
15713910Sdougm 
15723910Sdougm 	/*
15733910Sdougm 	 * Remove entries matching buffer and sharepoint since we're
15743910Sdougm 	 * going to replace it with perhaps an entry with a new tag.
15753910Sdougm 	 */
15763910Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
15773910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15784345Sdougm 		    "share complete, however could not remove matching "
15794345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15803910Sdougm 		error = -1;
15813910Sdougm 		goto out;
15823910Sdougm 	}
15833910Sdougm 
15843910Sdougm 	/*
15853910Sdougm 	 * Deactivate all active entries matching this sharepoint
15863910Sdougm 	 */
15873910Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
15883910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
15894345Sdougm 		    "share complete, however could not deactivate matching "
15904345Sdougm 		    "entries in %s\n"), NFSLOGTAB);
15913910Sdougm 		error = -1;
15923910Sdougm 		goto out;
15933910Sdougm 	}
15943910Sdougm 
15953910Sdougm 	lep.le_buffer = buffer;
15963910Sdougm 	lep.le_path = dir;
15973910Sdougm 	lep.le_tag = tag;
15983910Sdougm 	lep.le_state = LES_ACTIVE;
15993910Sdougm 
16003910Sdougm 	/*
16013910Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
16023910Sdougm 	 */
16033910Sdougm 	if (logtab_putent(f, &lep) < 0) {
16043910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
16054345Sdougm 		    "share complete, however could not add %s to %s\n"),
16064345Sdougm 		    dir, NFSLOGTAB);
16073910Sdougm 		error = -1;
16083910Sdougm 	}
16093910Sdougm 
16103910Sdougm out:
16113910Sdougm 	if (f != NULL)
16123910Sdougm 		(void) fclose(f);
16133910Sdougm 	return (error);
16143910Sdougm }
16153910Sdougm 
16163910Sdougm /*
16173910Sdougm  * Deactivate an entry from the nfslogtab file
16183910Sdougm  */
16193910Sdougm static int
nfslogtab_deactivate(path)16203910Sdougm nfslogtab_deactivate(path)
16213910Sdougm 	char *path;
16223910Sdougm {
16233910Sdougm 	FILE *f;
16243910Sdougm 	int error = 0;
16253910Sdougm 
16263910Sdougm 	f = fopen(NFSLOGTAB, "r+");
16273910Sdougm 	if (f == NULL) {
16283910Sdougm 		error = errno;
16293910Sdougm 		goto out;
16303910Sdougm 	}
16313910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
16323910Sdougm 		error = errno;
16333910Sdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
16344345Sdougm 		    "share complete, however could not lock %s for "
16354345Sdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
16363910Sdougm 		goto out;
16373910Sdougm 	}
16383910Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
16393910Sdougm 		error = -1;
16403910Sdougm 		(void) fprintf(stderr,
16414345Sdougm 		    dgettext(TEXT_DOMAIN,
16424345Sdougm 		    "share complete, however could not "
16434345Sdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
16443910Sdougm 		goto out;
16453910Sdougm 	}
16463910Sdougm 
16473910Sdougm out:	if (f != NULL)
16483910Sdougm 		(void) fclose(f);
16493910Sdougm 
16503910Sdougm 	return (error);
16513910Sdougm }
16523910Sdougm 
16533910Sdougm /*
16544524Sdougm  * check_public(group, skipshare)
16554524Sdougm  *
16564524Sdougm  * Check the group for any shares that have the public property
16574524Sdougm  * enabled. We skip "skipshare" since that is the one we are
16584524Sdougm  * working with. This is a separate function to make handling
16594524Sdougm  * subgroups simpler. Returns true if there is a share with public.
16604524Sdougm  */
16614524Sdougm static int
check_public(sa_group_t group,sa_share_t skipshare)16624524Sdougm check_public(sa_group_t group, sa_share_t skipshare)
16634524Sdougm {
16644524Sdougm 	int exists = B_FALSE;
16654524Sdougm 	sa_share_t share;
16664524Sdougm 	sa_optionset_t opt;
16674524Sdougm 	sa_property_t prop;
16684524Sdougm 	char *shared;
16694524Sdougm 
16704524Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
16714524Sdougm 	    share = sa_get_next_share(share)) {
16724524Sdougm 		if (share == skipshare)
16734524Sdougm 			continue;
16744524Sdougm 
16754524Sdougm 		opt = sa_get_optionset(share, "nfs");
16764524Sdougm 		if (opt == NULL)
16774524Sdougm 			continue;
16784524Sdougm 		prop = sa_get_property(opt, "public");
16794524Sdougm 		if (prop == NULL)
16804524Sdougm 			continue;
16814524Sdougm 		shared = sa_get_share_attr(share, "shared");
16824524Sdougm 		if (shared != NULL) {
16834524Sdougm 			exists = strcmp(shared, "true") == 0;
16844524Sdougm 			sa_free_attr_string(shared);
16854524Sdougm 			if (exists == B_TRUE)
16864524Sdougm 				break;
16874524Sdougm 		}
16884524Sdougm 	}
16894524Sdougm 
16904524Sdougm 	return (exists);
16914524Sdougm }
16924524Sdougm 
16934524Sdougm /*
16946214Sdougm  * public_exists(handle, share)
16953910Sdougm  *
16963910Sdougm  * check to see if public option is set on any other share than the
16974524Sdougm  * one specified. Need to check zfs sub-groups as well as the top
16984524Sdougm  * level groups.
16993910Sdougm  */
17003910Sdougm static int
public_exists(sa_handle_t handle,sa_share_t skipshare)17016214Sdougm public_exists(sa_handle_t handle, sa_share_t skipshare)
17023910Sdougm {
17036214Sdougm 	sa_group_t group = NULL;
17043910Sdougm 
17056271Sdougm 	/*
17066271Sdougm 	 * If we don't have a handle, we can only do syntax check. We
17076271Sdougm 	 * can't check against other shares so we assume OK and will
17086271Sdougm 	 * catch the problem only when we actually try to apply it.
17096271Sdougm 	 */
17103910Sdougm 	if (handle == NULL)
17116271Sdougm 		return (SA_OK);
17123910Sdougm 
17136214Sdougm 	if (skipshare != NULL) {
17146214Sdougm 		group = sa_get_parent_group(skipshare);
17156214Sdougm 		if (group == NULL)
17166214Sdougm 			return (SA_NO_SUCH_GROUP);
17176214Sdougm 	}
17186214Sdougm 
17193910Sdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
17203910Sdougm 	    group = sa_get_next_group(group)) {
17214524Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
17224524Sdougm 		if (sa_group_is_zfs(group)) {
17234524Sdougm 			sa_group_t subgroup;
17244524Sdougm 			for (subgroup = sa_get_sub_group(group);
17254524Sdougm 			    subgroup != NULL;
17264524Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
17274524Sdougm 				if (check_public(subgroup, skipshare))
17284524Sdougm 					return (B_TRUE);
17293910Sdougm 			}
17304524Sdougm 		} else {
17314524Sdougm 			if (check_public(group, skipshare))
17324524Sdougm 				return (B_TRUE);
17333910Sdougm 		}
17343910Sdougm 	}
17354524Sdougm 	return (B_FALSE);
17363910Sdougm }
17373910Sdougm 
17383910Sdougm /*
17393910Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
17403910Sdougm  * implementation that it is to enable the share. This entails
17413910Sdougm  * converting the path and options into the appropriate ioctl
17423910Sdougm  * calls. It is assumed that all error checking of paths, etc. were
17433910Sdougm  * done earlier.
17443910Sdougm  */
17453910Sdougm static int
nfs_enable_share(sa_share_t share)17463910Sdougm nfs_enable_share(sa_share_t share)
17473910Sdougm {
17483910Sdougm 	struct exportdata export;
17493910Sdougm 	sa_optionset_t secoptlist;
17503910Sdougm 	struct secinfo *sp;
17513910Sdougm 	int num_secinfo;
17523910Sdougm 	sa_optionset_t opt;
17533910Sdougm 	sa_security_t sec;
17543910Sdougm 	sa_property_t prop;
17553910Sdougm 	char *path;
17563910Sdougm 	int err = SA_OK;
17574524Sdougm 	int i;
17584543Smarks 	int iszfs;
17596214Sdougm 	sa_handle_t handle;
17603910Sdougm 
17613910Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
17623910Sdougm 	(void) signal(SIGSYS, SIG_IGN);
17633910Sdougm 
17643910Sdougm 	/* get the path since it is important in several places */
17653910Sdougm 	path = sa_get_share_attr(share, "path");
17663910Sdougm 	if (path == NULL)
17674345Sdougm 		return (SA_NO_SUCH_PATH);
17683910Sdougm 
17694543Smarks 	iszfs = sa_path_is_zfs(path);
17703910Sdougm 	/*
17713910Sdougm 	 * find the optionsets and security sets.  There may not be
17723910Sdougm 	 * any or there could be one or two for each of optionset and
17733910Sdougm 	 * security may have multiple, one per security type per
17743910Sdougm 	 * protocol type.
17753910Sdougm 	 */
17763910Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
17773910Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
17783910Sdougm 	if (secoptlist != NULL)
17794345Sdougm 		num_secinfo = MAX(1, count_security(secoptlist));
17803910Sdougm 	else
17814345Sdougm 		num_secinfo = 1;
17823910Sdougm 
17833910Sdougm 	/*
17843910Sdougm 	 * walk through the options and fill in the structure
17853910Sdougm 	 * appropriately.
17863910Sdougm 	 */
17873910Sdougm 
17883910Sdougm 	(void) memset(&export, '\0', sizeof (export));
17893910Sdougm 
17903910Sdougm 	/*
17913910Sdougm 	 * do non-security options first since there is only one after
17923910Sdougm 	 * the derived group is constructed.
17933910Sdougm 	 */
17943910Sdougm 	export.ex_version = EX_CURRENT_VERSION;
17953910Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
17963910Sdougm 	export.ex_index = NULL;
17973910Sdougm 	export.ex_path = path;
17983910Sdougm 	export.ex_pathlen = strlen(path) + 1;
17993910Sdougm 
18003910Sdougm 	if (opt != NULL)
18014345Sdougm 		err = fill_export_from_optionset(&export, opt);
18023910Sdougm 
18033910Sdougm 	/*
18043910Sdougm 	 * check to see if "public" is set. If it is, then make sure
18053910Sdougm 	 * no other share has it set. If it is already used, fail.
18063910Sdougm 	 */
18073910Sdougm 
18086214Sdougm 	handle = sa_find_group_handle((sa_group_t)share);
18096214Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
18104345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18114345Sdougm 		    "NFS: Cannot share more than one file "
18124345Sdougm 		    "system with 'public' property\n"));
18134345Sdougm 		err = SA_NOT_ALLOWED;
18144345Sdougm 		goto out;
18153910Sdougm 	}
18163910Sdougm 
18174524Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
18183910Sdougm 	if (sp == NULL) {
18194345Sdougm 		err = SA_NO_MEMORY;
18204524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18214524Sdougm 		    "NFS: NFS: no memory for security\n"));
18224524Sdougm 		goto out;
18234524Sdougm 	}
18244524Sdougm 	export.ex_secinfo = sp;
18254524Sdougm 	/* get default secinfo */
18264524Sdougm 	export.ex_seccnt = num_secinfo;
18274524Sdougm 	/*
18284524Sdougm 	 * since we must have one security option defined, we
18294524Sdougm 	 * init to the default and then override as we find
18304524Sdougm 	 * defined security options. This handles the case
18314524Sdougm 	 * where we have no defined options but we need to set
18324524Sdougm 	 * up one.
18334524Sdougm 	 */
18344524Sdougm 	sp[0].s_window = DEF_WIN;
18354524Sdougm 	sp[0].s_rootnames = NULL;
18364524Sdougm 	/* setup a default in case no properties defined */
18374524Sdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
18384524Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
18394524Sdougm 		    "NFS: nfs_getseconfig_default: failed to "
18404524Sdougm 		    "get default security mode\n"));
18414524Sdougm 		err = SA_CONFIG_ERR;
18424524Sdougm 	}
18434524Sdougm 	if (secoptlist != NULL) {
18444524Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
18454524Sdougm 		    prop != NULL && i < num_secinfo;
18464524Sdougm 		    prop = sa_get_next_property(prop), i++) {
18474524Sdougm 			char *sectype;
184811337SWilliam.Krier@Sun.COM 			sectype = sa_get_property_attr(prop, "type");
18494524Sdougm 			/*
18504524Sdougm 			 * if sectype is NULL, we probably
18514524Sdougm 			 * have a memory problem and can't get
18524524Sdougm 			 * the correct values. Rather than
18534524Sdougm 			 * exporting with incorrect security,
18544524Sdougm 			 * don't share it.
18554524Sdougm 			 */
18564524Sdougm 			if (sectype == NULL) {
18574524Sdougm 				err = SA_NO_MEMORY;
18584524Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
18594524Sdougm 				    "NFS: Cannot share %s: "
18604524Sdougm 				    "no memory\n"), path);
18614524Sdougm 				goto out;
18624524Sdougm 			}
18634524Sdougm 			sec = (sa_security_t)sa_get_derived_security(
18644524Sdougm 			    share, sectype, "nfs", 1);
18654524Sdougm 			sp[i].s_window = DEF_WIN;
18664524Sdougm 			sp[i].s_rootcnt = 0;
18674524Sdougm 			sp[i].s_rootnames = NULL;
18684345Sdougm 				(void) fill_security_from_secopts(&sp[i], sec);
18694524Sdougm 			if (sec != NULL)
18704524Sdougm 				sa_free_derived_security(sec);
18714524Sdougm 			if (sectype != NULL)
18724524Sdougm 				sa_free_attr_string(sectype);
18733910Sdougm 		}
18744524Sdougm 	}
18754524Sdougm 	/*
18764524Sdougm 	 * when we get here, we can do the exportfs system call and
18774524Sdougm 	 * initiate thinsg. We probably want to enable the nfs.server
18784524Sdougm 	 * service first if it isn't running within SMF.
18794524Sdougm 	 */
18804524Sdougm 	/* check nfs.server status and start if needed */
18814524Sdougm 	/* now add the share to the internal tables */
18824524Sdougm 	printarg(path, &export);
18834524Sdougm 	/*
18844524Sdougm 	 * call the exportfs system call which is implemented
18854524Sdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
18864524Sdougm 	 */
18874543Smarks 	if (iszfs) {
18884543Smarks 		struct exportfs_args ea;
18894543Smarks 		share_t sh;
18904543Smarks 		char *str;
18914543Smarks 		priv_set_t *priv_effective;
18924543Smarks 		int privileged;
18934543Smarks 
18944543Smarks 		/*
18954543Smarks 		 * If we aren't a privileged user
18964543Smarks 		 * and NFS server service isn't running
18974543Smarks 		 * then print out an error message
18984543Smarks 		 * and return EPERM
18994543Smarks 		 */
19004543Smarks 
19014543Smarks 		priv_effective = priv_allocset();
19024543Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
19034543Smarks 
19044543Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
19054543Smarks 		priv_freeset(priv_effective);
19064543Smarks 
19074543Smarks 		if (!privileged &&
19084543Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
19094543Smarks 			err = 0;
19104543Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
19114543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
19124543Smarks 				    "NFS: Cannot share remote "
19134543Smarks 				    "filesystem: %s\n"), path);
19144543Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
19154543Smarks 				    "NFS: Service needs to be enabled "
19164543Smarks 				    "by a privileged user\n"));
19174543Smarks 				err = SA_SYSTEM_ERR;
19184543Smarks 				errno = EPERM;
19194543Smarks 			}
19204543Smarks 			free(str);
19214543Smarks 		}
19224543Smarks 
19234543Smarks 		if (err == 0) {
19244543Smarks 			ea.dname = path;
19254543Smarks 			ea.uex = &export;
19264543Smarks 
192711411SSurya.Prakki@Sun.COM 			(void) sa_sharetab_fill_zfs(share, &sh, "nfs");
19288845Samw@Sun.COM 			err = sa_share_zfs(share, NULL, path, &sh,
19295331Samw 			    &ea, ZFS_SHARE_NFS);
193010761SWilliam.Krier@Sun.COM 			if (err != SA_OK) {
193110761SWilliam.Krier@Sun.COM 				errno = err;
193210761SWilliam.Krier@Sun.COM 				err = -1;
193310761SWilliam.Krier@Sun.COM 			}
19344543Smarks 			sa_emptyshare(&sh);
19354543Smarks 		}
19364543Smarks 	} else {
19374543Smarks 		err = exportfs(path, &export);
19384543Smarks 	}
19394543Smarks 
19404543Smarks 	if (err < 0) {
19414524Sdougm 		err = SA_SYSTEM_ERR;
19424524Sdougm 		switch (errno) {
19434524Sdougm 		case EREMOTE:
19444524Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
19454543Smarks 			    "NFS: Cannot share filesystems "
19464543Smarks 			    "in non-global zones: %s\n"), path);
19474543Smarks 			err = SA_NOT_SUPPORTED;
19484524Sdougm 			break;
19494524Sdougm 		case EPERM:
19504524Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
19514345Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
19524543Smarks 				    "NFS: Cannot share file systems "
19534524Sdougm 				    "in non-global zones: %s\n"), path);
19544524Sdougm 				err = SA_NOT_SUPPORTED;
19554345Sdougm 				break;
19564345Sdougm 			}
19574524Sdougm 			err = SA_NO_PERMISSION;
195812679SPavel.Filipensky@Sun.COM 			break;
195912679SPavel.Filipensky@Sun.COM 		case EEXIST:
196012679SPavel.Filipensky@Sun.COM 			err = SA_SHARE_EXISTS;
196112679SPavel.Filipensky@Sun.COM 			break;
19624524Sdougm 		default:
19634524Sdougm 			break;
19643910Sdougm 		}
19654524Sdougm 	} else {
19664524Sdougm 		/* update sharetab with an add/modify */
19674543Smarks 		if (!iszfs) {
19684543Smarks 			(void) sa_update_sharetab(share, "nfs");
19694543Smarks 		}
19703910Sdougm 	}
19713910Sdougm 
19723910Sdougm 	if (err == SA_OK) {
19733910Sdougm 		/*
19743910Sdougm 		 * enable services as needed. This should probably be
19753910Sdougm 		 * done elsewhere in order to minimize the calls to
19763910Sdougm 		 * check services.
19773910Sdougm 		 */
19783910Sdougm 		/*
19793910Sdougm 		 * check to see if logging and other services need to
19803910Sdougm 		 * be triggered, but only if there wasn't an
19813910Sdougm 		 * error. This is probably where sharetab should be
19823910Sdougm 		 * updated with the NFS specific entry.
19833910Sdougm 		 */
19844345Sdougm 		if (export.ex_flags & EX_LOG) {
19854345Sdougm 			/* enable logging */
19864345Sdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
19874345Sdougm 			    export.ex_tag) != 0) {
19884345Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
19894345Sdougm 				    "Could not enable logging for %s\n"),
19904345Sdougm 				    path);
19914345Sdougm 			}
19924345Sdougm 			_check_services(service_list_logging);
19934345Sdougm 		} else {
19944345Sdougm 			/*
19954345Sdougm 			 * don't have logging so remove it from file. It might
19964345Sdougm 			 * not be thre, but that doesn't matter.
19974345Sdougm 			 */
19984345Sdougm 			(void) nfslogtab_deactivate(path);
19994345Sdougm 			_check_services(service_list_default);
20003910Sdougm 		}
20013910Sdougm 	}
20023910Sdougm 
20033910Sdougm out:
20043910Sdougm 	if (path != NULL)
20054345Sdougm 		free(path);
20063910Sdougm 
20073910Sdougm 	cleanup_export(&export);
20083910Sdougm 	if (opt != NULL)
20094345Sdougm 		sa_free_derived_optionset(opt);
20103910Sdougm 	if (secoptlist != NULL)
20114345Sdougm 		(void) sa_destroy_optionset(secoptlist);
20123910Sdougm 	return (err);
20133910Sdougm }
20143910Sdougm 
20153910Sdougm /*
20165800Sdougm  * nfs_disable_share(share, path)
20173910Sdougm  *
20185800Sdougm  * Unshare the specified share. Note that "path" is the same path as
20195800Sdougm  * what is in the "share" object. It is passed in to avoid an
20205800Sdougm  * additional lookup. A missing "path" value makes this a no-op
20215800Sdougm  * function.
20223910Sdougm  */
20233910Sdougm static int
nfs_disable_share(sa_share_t share,char * path)20244543Smarks nfs_disable_share(sa_share_t share, char *path)
20253910Sdougm {
20263910Sdougm 	int err;
20273910Sdougm 	int ret = SA_OK;
20284543Smarks 	int iszfs;
20295800Sdougm 	sa_group_t parent;
20305951Sdougm 	sa_handle_t handle;
20314543Smarks 
20325800Sdougm 	if (path == NULL)
20335800Sdougm 		return (ret);
20344543Smarks 
20355800Sdougm 	/*
20365800Sdougm 	 * If the share is in a ZFS group we need to handle it
20375800Sdougm 	 * differently.  Just being on a ZFS file system isn't
20385800Sdougm 	 * enough since we may be in a legacy share case.
20395800Sdougm 	 */
20405800Sdougm 	parent = sa_get_parent_group(share);
20415800Sdougm 	iszfs = sa_group_is_zfs(parent);
20425800Sdougm 	if (iszfs) {
20435800Sdougm 		struct exportfs_args ea;
20445800Sdougm 		share_t sh = { 0 };
20455800Sdougm 		ea.dname = path;
20465800Sdougm 		ea.uex = NULL;
20475800Sdougm 		sh.sh_path = path;
20485800Sdougm 		sh.sh_fstype = "nfs";
20494543Smarks 
20508845Samw@Sun.COM 		err = sa_share_zfs(share, NULL, path, &sh,
20515800Sdougm 		    &ea, ZFS_UNSHARE_NFS);
205210761SWilliam.Krier@Sun.COM 		if (err != SA_OK) {
205310761SWilliam.Krier@Sun.COM 			errno = err;
205410761SWilliam.Krier@Sun.COM 			err = -1;
205510761SWilliam.Krier@Sun.COM 		}
20565800Sdougm 	} else {
20575800Sdougm 		err = exportfs(path, NULL);
20585800Sdougm 	}
20595800Sdougm 	if (err < 0) {
20605800Sdougm 		/*
20615800Sdougm 		 * TBD: only an error in some
20625800Sdougm 		 * cases - need better analysis
20635800Sdougm 		 */
20645800Sdougm 		switch (errno) {
20655800Sdougm 		case EPERM:
20665800Sdougm 		case EACCES:
20675800Sdougm 			ret = SA_NO_PERMISSION;
20685800Sdougm 			if (getzoneid() != GLOBAL_ZONEID) {
20695800Sdougm 				ret = SA_NOT_SUPPORTED;
20705800Sdougm 			}
20715800Sdougm 			break;
20725800Sdougm 		case EINVAL:
20735800Sdougm 		case ENOENT:
20745800Sdougm 			ret = SA_NO_SUCH_PATH;
20754543Smarks 			break;
207610761SWilliam.Krier@Sun.COM 		default:
207710761SWilliam.Krier@Sun.COM 			ret = SA_SYSTEM_ERR;
20784543Smarks 			break;
20793910Sdougm 		}
20805800Sdougm 	}
20815800Sdougm 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
20825951Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
20835800Sdougm 		if (!iszfs)
20845951Sdougm 			(void) sa_delete_sharetab(handle, path, "nfs");
20855800Sdougm 		/* just in case it was logged */
20865800Sdougm 		(void) nfslogtab_deactivate(path);
20873910Sdougm 	}
20883910Sdougm 	return (ret);
20893910Sdougm }
20903910Sdougm 
20913910Sdougm /*
20927961SNatalie.Li@Sun.COM  * check_rorwnone(v1, v2, v3)
20937961SNatalie.Li@Sun.COM  *
20947961SNatalie.Li@Sun.COM  * check ro vs rw vs none values.  Over time this may get beefed up.
20957961SNatalie.Li@Sun.COM  * for now it just does simple checks. v1 is never NULL but v2 or v3
20967961SNatalie.Li@Sun.COM  * could be.
20973910Sdougm  */
20983910Sdougm 
20993910Sdougm static int
check_rorwnone(char * v1,char * v2,char * v3)21007961SNatalie.Li@Sun.COM check_rorwnone(char *v1, char *v2, char *v3)
21013910Sdougm {
21023910Sdougm 	int ret = SA_OK;
21037961SNatalie.Li@Sun.COM 	if (v2 != NULL && strcmp(v1, v2) == 0)
21044345Sdougm 		ret = SA_VALUE_CONFLICT;
21057961SNatalie.Li@Sun.COM 	else if (v3 != NULL && strcmp(v1, v3) == 0)
21067961SNatalie.Li@Sun.COM 		ret = SA_VALUE_CONFLICT;
21077961SNatalie.Li@Sun.COM 
21083910Sdougm 	return (ret);
21093910Sdougm }
21103910Sdougm 
21113910Sdougm /*
21126214Sdougm  * nfs_validate_property(handle, property, parent)
21133910Sdougm  *
21143910Sdougm  * Check that the property has a legitimate value for its type.
21153910Sdougm  */
21163910Sdougm 
21173910Sdougm static int
nfs_validate_property(sa_handle_t handle,sa_property_t property,sa_optionset_t parent)21186214Sdougm nfs_validate_property(sa_handle_t handle, sa_property_t property,
21196214Sdougm     sa_optionset_t parent)
21203910Sdougm {
21213910Sdougm 	int ret = SA_OK;
21223910Sdougm 	char *propname;
21237961SNatalie.Li@Sun.COM 	char *other1;
21247961SNatalie.Li@Sun.COM 	char *other2;
21253910Sdougm 	int optindex;
21263910Sdougm 	nfsl_config_t *configlist;
21273910Sdougm 	sa_group_t parent_group;
21283910Sdougm 	char *value;
21293910Sdougm 
21303910Sdougm 	propname = sa_get_property_attr(property, "type");
21313910Sdougm 
21323910Sdougm 	if ((optindex = findopt(propname)) < 0)
21334345Sdougm 		ret = SA_NO_SUCH_PROP;
21343910Sdougm 
21353910Sdougm 	/* need to validate value range here as well */
21363910Sdougm 
21373910Sdougm 	if (ret == SA_OK) {
21384345Sdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
21396214Sdougm 		if (optdefs[optindex].share && parent_group != NULL &&
21406214Sdougm 		    !sa_is_share(parent_group))
21414345Sdougm 			ret = SA_PROP_SHARE_ONLY;
21423910Sdougm 	}
21433910Sdougm 	if (ret == SA_OK) {
21446214Sdougm 		if (optdefs[optindex].index == OPT_PUBLIC) {
21456214Sdougm 			/*
21466214Sdougm 			 * Public is special in that only one instance can
21476214Sdougm 			 * be in the repository at the same time.
21486214Sdougm 			 */
21496214Sdougm 			if (public_exists(handle, parent_group)) {
21507961SNatalie.Li@Sun.COM 				sa_free_attr_string(propname);
21516214Sdougm 				return (SA_VALUE_CONFLICT);
21526214Sdougm 			}
21536214Sdougm 		}
21544345Sdougm 		value = sa_get_property_attr(property, "value");
21554345Sdougm 		if (value != NULL) {
21564345Sdougm 			/* first basic type checking */
21574345Sdougm 			switch (optdefs[optindex].type) {
21584345Sdougm 			case OPT_TYPE_NUMBER:
21594345Sdougm 				/* check that the value is all digits */
21604345Sdougm 				if (!is_a_number(value))
21614345Sdougm 					ret = SA_BAD_VALUE;
21624345Sdougm 				break;
21634345Sdougm 			case OPT_TYPE_BOOLEAN:
21644345Sdougm 				if (strlen(value) == 0 ||
21654345Sdougm 				    strcasecmp(value, "true") == 0 ||
21664345Sdougm 				    strcmp(value, "1") == 0 ||
21674345Sdougm 				    strcasecmp(value, "false") == 0 ||
21684345Sdougm 				    strcmp(value, "0") == 0) {
21694345Sdougm 					ret = SA_OK;
21704345Sdougm 				} else {
21714345Sdougm 					ret = SA_BAD_VALUE;
21724345Sdougm 				}
21734345Sdougm 				break;
21744345Sdougm 			case OPT_TYPE_USER:
21754345Sdougm 				if (!is_a_number(value)) {
21764345Sdougm 					struct passwd *pw;
21774345Sdougm 					/*
21784345Sdougm 					 * in this case it would have to be a
21794345Sdougm 					 * user name
21804345Sdougm 					 */
21814345Sdougm 					pw = getpwnam(value);
21824345Sdougm 					if (pw == NULL)
21834345Sdougm 						ret = SA_BAD_VALUE;
21844345Sdougm 					endpwent();
21854345Sdougm 				} else {
21864345Sdougm 					uint64_t intval;
21874345Sdougm 					intval = strtoull(value, NULL, 0);
21884345Sdougm 					if (intval > UID_MAX && intval != ~0)
21894345Sdougm 						ret = SA_BAD_VALUE;
21904345Sdougm 				}
21914345Sdougm 				break;
21924345Sdougm 			case OPT_TYPE_FILE:
21934345Sdougm 				if (strcmp(value, "..") == 0 ||
21944345Sdougm 				    strchr(value, '/') != NULL) {
21954345Sdougm 					ret = SA_BAD_VALUE;
21964345Sdougm 				}
21974345Sdougm 				break;
21987961SNatalie.Li@Sun.COM 			case OPT_TYPE_ACCLIST: {
21997961SNatalie.Li@Sun.COM 				sa_property_t oprop1;
22007961SNatalie.Li@Sun.COM 				sa_property_t oprop2;
22017961SNatalie.Li@Sun.COM 				char *ovalue1 = NULL;
22027961SNatalie.Li@Sun.COM 				char *ovalue2 = NULL;
22037961SNatalie.Li@Sun.COM 
22047961SNatalie.Li@Sun.COM 				if (parent == NULL)
22057961SNatalie.Li@Sun.COM 					break;
22064345Sdougm 				/*
22074345Sdougm 				 * access list handling. Should eventually
22084345Sdougm 				 * validate that all the values make sense.
22094345Sdougm 				 * Also, ro and rw may have cross value
22104345Sdougm 				 * conflicts.
22114345Sdougm 				 */
22127961SNatalie.Li@Sun.COM 				if (strcmp(propname, SHOPT_RO) == 0) {
22137961SNatalie.Li@Sun.COM 					other1 = SHOPT_RW;
22147961SNatalie.Li@Sun.COM 					other2 = SHOPT_NONE;
22157961SNatalie.Li@Sun.COM 				} else if (strcmp(propname, SHOPT_RW) == 0) {
22167961SNatalie.Li@Sun.COM 					other1 = SHOPT_RO;
22177961SNatalie.Li@Sun.COM 					other2 = SHOPT_NONE;
22187961SNatalie.Li@Sun.COM 				} else if (strcmp(propname, SHOPT_NONE) == 0) {
22197961SNatalie.Li@Sun.COM 					other1 = SHOPT_RO;
22207961SNatalie.Li@Sun.COM 					other2 = SHOPT_RW;
22217961SNatalie.Li@Sun.COM 				} else {
22227961SNatalie.Li@Sun.COM 					other1 = NULL;
22237961SNatalie.Li@Sun.COM 					other2 = NULL;
22247961SNatalie.Li@Sun.COM 				}
22257961SNatalie.Li@Sun.COM 				if (other1 == NULL && other2 == NULL)
22267961SNatalie.Li@Sun.COM 					break;
22277961SNatalie.Li@Sun.COM 
22287961SNatalie.Li@Sun.COM 				/* compare rw(ro) with ro(rw) */
22294345Sdougm 
22307961SNatalie.Li@Sun.COM 				oprop1 = sa_get_property(parent, other1);
22317961SNatalie.Li@Sun.COM 				oprop2 = sa_get_property(parent, other2);
22327961SNatalie.Li@Sun.COM 				if (oprop1 == NULL && oprop2 == NULL)
22337961SNatalie.Li@Sun.COM 					break;
22347961SNatalie.Li@Sun.COM 				/*
22357961SNatalie.Li@Sun.COM 				 * Only potential confusion if other1
22367961SNatalie.Li@Sun.COM 				 * or other2 exists. Check the values
22377961SNatalie.Li@Sun.COM 				 * and run the check if there is a
22387961SNatalie.Li@Sun.COM 				 * value other than the one we are
22397961SNatalie.Li@Sun.COM 				 * explicitly looking at.
22407961SNatalie.Li@Sun.COM 				 */
22417961SNatalie.Li@Sun.COM 				ovalue1 = sa_get_property_attr(oprop1, "value");
22427961SNatalie.Li@Sun.COM 				ovalue2 = sa_get_property_attr(oprop2, "value");
22437961SNatalie.Li@Sun.COM 				if (ovalue1 != NULL || ovalue2 != NULL)
22447961SNatalie.Li@Sun.COM 					ret = check_rorwnone(value, ovalue1,
22457961SNatalie.Li@Sun.COM 					    ovalue2);
22467961SNatalie.Li@Sun.COM 
22477961SNatalie.Li@Sun.COM 				if (ovalue1 != NULL)
22487961SNatalie.Li@Sun.COM 					sa_free_attr_string(ovalue1);
22497961SNatalie.Li@Sun.COM 				if (ovalue2 != NULL)
22507961SNatalie.Li@Sun.COM 					sa_free_attr_string(ovalue2);
22514345Sdougm 				break;
22527961SNatalie.Li@Sun.COM 			}
22534345Sdougm 			case OPT_TYPE_LOGTAG:
22544345Sdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
22554345Sdougm 					int error;
22564345Sdougm 					if (value == NULL ||
22574345Sdougm 					    strlen(value) == 0) {
22584345Sdougm 						if (value != NULL)
22594345Sdougm 							sa_free_attr_string(
22604345Sdougm 							    value);
22614345Sdougm 						value = strdup("global");
22624345Sdougm 					}
22634345Sdougm 					if (value != NULL &&
22644345Sdougm 					    nfsl_findconfig(configlist, value,
22654345Sdougm 					    &error) == NULL) {
22664345Sdougm 						ret = SA_BAD_VALUE;
22674345Sdougm 					}
22685179Sdougm 					/* Must always free when done */
22695179Sdougm 					nfsl_freeconfig_list(&configlist);
22704345Sdougm 				} else {
22714345Sdougm 					ret = SA_CONFIG_ERR;
22724345Sdougm 				}
22734345Sdougm 				break;
22744345Sdougm 			case OPT_TYPE_STRING:
22754345Sdougm 				/* whatever is here should be ok */
22764345Sdougm 				break;
22774345Sdougm 			case OPT_TYPE_SECURITY:
22784345Sdougm 				/*
22794345Sdougm 				 * The "sec" property isn't used in the
22804345Sdougm 				 * non-legacy parts of sharemgr. We need to
22814345Sdougm 				 * reject it here. For legacy, it is pulled
22824345Sdougm 				 * out well before we get here.
22834345Sdougm 				 */
22844345Sdougm 				ret = SA_NO_SUCH_PROP;
22854345Sdougm 				break;
22864345Sdougm 			default:
22874345Sdougm 				break;
22883910Sdougm 			}
22895179Sdougm 
22905179Sdougm 			if (value != NULL)
22915179Sdougm 				sa_free_attr_string(value);
22925179Sdougm 
22934345Sdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
22944345Sdougm 				/* do the property specific check */
22956214Sdougm 				ret = optdefs[optindex].check(handle, property);
22963910Sdougm 			}
22973910Sdougm 		}
22983910Sdougm 	}
22993910Sdougm 
23003910Sdougm 	if (propname != NULL)
23014345Sdougm 		sa_free_attr_string(propname);
23023910Sdougm 	return (ret);
23033910Sdougm }
23043910Sdougm 
23053910Sdougm /*
23063910Sdougm  * Protocol management functions
23073910Sdougm  *
23083910Sdougm  * Properties defined in the default files are defined in
23093910Sdougm  * proto_option_defs for parsing and validation. If "other" and
23103910Sdougm  * "compare" are set, then the value for this property should be
23113910Sdougm  * compared against the property specified in "other" using the
23123910Sdougm  * "compare" check (either <= or >=) in order to ensure that the
23133910Sdougm  * values are in the correct range.  E.g. setting server_versmin
23143910Sdougm  * higher than server_versmax should not be allowed.
23153910Sdougm  */
23163910Sdougm 
23173910Sdougm struct proto_option_defs {
23183910Sdougm 	char *tag;
23193910Sdougm 	char *name;	/* display name -- remove protocol identifier */
23203910Sdougm 	int index;
23213910Sdougm 	int type;
23223910Sdougm 	union {
23233910Sdougm 	    int intval;
23243910Sdougm 	    char *string;
23253910Sdougm 	} defvalue;
23263910Sdougm 	uint32_t svcs;
23273910Sdougm 	int32_t minval;
23283910Sdougm 	int32_t maxval;
23293910Sdougm 	char *other;
23303910Sdougm 	int compare;
23313910Sdougm #define	OPT_CMP_GE	0
23323910Sdougm #define	OPT_CMP_LE	1
23333910Sdougm 	int (*check)(char *);
23343910Sdougm } proto_options[] = {
23353910Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
23363910Sdougm 	{"nfsd_servers",
23373910Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
2338*13080SPavan.Mettu@Oracle.COM 	    1, INT32_MAX},
23393910Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
23403910Sdougm 	{"lockd_listen_backlog",
23413910Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
2342*13080SPavan.Mettu@Oracle.COM 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX},
23433910Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
23443910Sdougm 	{"lockd_servers",
23453910Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
2346*13080SPavan.Mettu@Oracle.COM 	    SVC_LOCKD, 1, INT32_MAX},
23473910Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
23483910Sdougm 	{"lockd_retransmit_timeout",
23493910Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
2350*13080SPavan.Mettu@Oracle.COM 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX},
23513910Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
23523910Sdougm 	{"grace_period",
23533910Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
2354*13080SPavan.Mettu@Oracle.COM 	    SVC_LOCKD, 0, INT32_MAX},
23553910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
23563910Sdougm 	{"nfs_server_versmin",
23573910Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
23583910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2359*13080SPavan.Mettu@Oracle.COM 	    NFS_VERSMAX, "server_versmax", OPT_CMP_LE},
23603910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
23613910Sdougm 	{"nfs_server_versmax",
23623910Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
23633910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2364*13080SPavan.Mettu@Oracle.COM 	    NFS_VERSMAX, "server_versmin", OPT_CMP_GE},
23653910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
23663910Sdougm 	{"nfs_client_versmin",
23673910Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
2368*13080SPavan.Mettu@Oracle.COM 	    (int)NFS_VERSMIN_DEFAULT, SVC_CLIENT, NFS_VERSMIN, NFS_VERSMAX,
2369*13080SPavan.Mettu@Oracle.COM 	    "client_versmax", OPT_CMP_LE},
23703910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
23713910Sdougm 	{"nfs_client_versmax",
23723910Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
2373*13080SPavan.Mettu@Oracle.COM 	    (int)NFS_VERSMAX_DEFAULT, SVC_CLIENT, NFS_VERSMIN, NFS_VERSMAX,
2374*13080SPavan.Mettu@Oracle.COM 	    "client_versmin", OPT_CMP_GE},
23753910Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
23763910Sdougm 	{"nfs_server_delegation",
23773910Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
2378*13080SPavan.Mettu@Oracle.COM 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0},
23793910Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
23803910Sdougm 	{"nfsmapid_domain",
23813910Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
2382*13080SPavan.Mettu@Oracle.COM 	    NULL, SVC_NFSMAPID, 0, 0},
23833910Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
23843910Sdougm 	{"nfsd_max_connections",
23853910Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
2386*13080SPavan.Mettu@Oracle.COM 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX},
23873910Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
23883910Sdougm 	{"nfsd_protocol",
23893910Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
2390*13080SPavan.Mettu@Oracle.COM 	    SVC_NFSD, 0, 0},
23913910Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
23923910Sdougm 	{"nfsd_listen_backlog",
23933910Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
2394*13080SPavan.Mettu@Oracle.COM 	    OPT_TYPE_NUMBER, 0, SVC_NFSD, 0, INT32_MAX},
2395*13080SPavan.Mettu@Oracle.COM #define	PROTO_OPT_NFSD_DEVICE			14
2396*13080SPavan.Mettu@Oracle.COM 	{"nfsd_device",
2397*13080SPavan.Mettu@Oracle.COM 	    "device", PROTO_OPT_NFSD_DEVICE,
2398*13080SPavan.Mettu@Oracle.COM 	    OPT_TYPE_STRING, NULL, SVC_NFSD, 0, 0},
23993910Sdougm 	{NULL}
24003910Sdougm };
24013910Sdougm 
24023910Sdougm /*
24033910Sdougm  * the protoset holds the defined options so we don't have to read
24043910Sdougm  * them multiple times
24053910Sdougm  */
24065179Sdougm static sa_protocol_properties_t protoset;
24073910Sdougm 
24083910Sdougm static int
findprotoopt(char * name,int whichname)24093910Sdougm findprotoopt(char *name, int whichname)
24103910Sdougm {
24113910Sdougm 	int i;
24123910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
24134345Sdougm 		if (whichname == 1) {
24144345Sdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
24153910Sdougm 			return (i);
24164345Sdougm 		} else {
24174345Sdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
24184345Sdougm 				return (i);
24194345Sdougm 		}
24203910Sdougm 	}
24213910Sdougm 	return (-1);
24223910Sdougm }
24233910Sdougm 
24243910Sdougm /*
24253910Sdougm  * fixcaselower(str)
24263910Sdougm  *
24273910Sdougm  * convert a string to lower case (inplace).
24283910Sdougm  */
24293910Sdougm 
24303910Sdougm static void
fixcaselower(char * str)24313910Sdougm fixcaselower(char *str)
24323910Sdougm {
24333910Sdougm 	while (*str) {
24344345Sdougm 		*str = tolower(*str);
24354345Sdougm 		str++;
24363910Sdougm 	}
24373910Sdougm }
24383910Sdougm 
24393910Sdougm /*
24404241Sdougm  * skipwhitespace(str)
24414241Sdougm  *
24424241Sdougm  * Skip leading white space. It is assumed that it is called with a
24434241Sdougm  * valid pointer.
24444241Sdougm  */
24454241Sdougm 
24464241Sdougm static char *
skipwhitespace(char * str)24474241Sdougm skipwhitespace(char *str)
24484241Sdougm {
24494241Sdougm 	while (*str && isspace(*str))
24504241Sdougm 		str++;
24514241Sdougm 
24524241Sdougm 	return (str);
24534241Sdougm }
24544241Sdougm 
24554241Sdougm /*
24564345Sdougm  * extractprop()
24574345Sdougm  *
24584345Sdougm  * Extract the property and value out of the line and create the
24594345Sdougm  * property in the optionset.
24604345Sdougm  */
24616019Sdougm static int
extractprop(char * name,char * value)24624345Sdougm extractprop(char *name, char *value)
24634345Sdougm {
24644345Sdougm 	sa_property_t prop;
24654345Sdougm 	int index;
24666019Sdougm 	int ret = SA_OK;
24674345Sdougm 	/*
24684345Sdougm 	 * Remove any leading
24694345Sdougm 	 * white space.
24704345Sdougm 	 */
24714345Sdougm 	name = skipwhitespace(name);
24724345Sdougm 
2473*13080SPavan.Mettu@Oracle.COM 	index = findprotoopt(name, 1);
24744345Sdougm 	if (index >= 0) {
24754345Sdougm 		fixcaselower(name);
24764345Sdougm 		prop = sa_create_property(proto_options[index].name, value);
24774345Sdougm 		if (prop != NULL)
24786019Sdougm 			ret = sa_add_protocol_property(protoset, prop);
24796019Sdougm 		else
24806019Sdougm 			ret = SA_NO_MEMORY;
24814345Sdougm 	}
24826019Sdougm 	return (ret);
24834345Sdougm }
24844345Sdougm 
2485*13080SPavan.Mettu@Oracle.COM scf_type_t
getscftype(int type)2486*13080SPavan.Mettu@Oracle.COM getscftype(int type)
2487*13080SPavan.Mettu@Oracle.COM {
2488*13080SPavan.Mettu@Oracle.COM 	scf_type_t ret;
2489*13080SPavan.Mettu@Oracle.COM 
2490*13080SPavan.Mettu@Oracle.COM 	switch (type) {
2491*13080SPavan.Mettu@Oracle.COM 	case OPT_TYPE_NUMBER:
2492*13080SPavan.Mettu@Oracle.COM 		ret = SCF_TYPE_INTEGER;
2493*13080SPavan.Mettu@Oracle.COM 	break;
2494*13080SPavan.Mettu@Oracle.COM 	case OPT_TYPE_BOOLEAN:
2495*13080SPavan.Mettu@Oracle.COM 		ret = SCF_TYPE_BOOLEAN;
2496*13080SPavan.Mettu@Oracle.COM 	break;
2497*13080SPavan.Mettu@Oracle.COM 	default:
2498*13080SPavan.Mettu@Oracle.COM 		ret = SCF_TYPE_ASTRING;
2499*13080SPavan.Mettu@Oracle.COM 	}
2500*13080SPavan.Mettu@Oracle.COM 	return (ret);
2501*13080SPavan.Mettu@Oracle.COM }
2502*13080SPavan.Mettu@Oracle.COM 
2503*13080SPavan.Mettu@Oracle.COM char *
getsvcname(uint32_t svcs)2504*13080SPavan.Mettu@Oracle.COM getsvcname(uint32_t svcs)
2505*13080SPavan.Mettu@Oracle.COM {
2506*13080SPavan.Mettu@Oracle.COM 	char *service;
2507*13080SPavan.Mettu@Oracle.COM 	switch (svcs) {
2508*13080SPavan.Mettu@Oracle.COM 		case SVC_LOCKD:
2509*13080SPavan.Mettu@Oracle.COM 			service = LOCKD;
2510*13080SPavan.Mettu@Oracle.COM 			break;
2511*13080SPavan.Mettu@Oracle.COM 		case SVC_STATD:
2512*13080SPavan.Mettu@Oracle.COM 			service = STATD;
2513*13080SPavan.Mettu@Oracle.COM 			break;
2514*13080SPavan.Mettu@Oracle.COM 		case SVC_NFSD:
2515*13080SPavan.Mettu@Oracle.COM 			service = NFSD;
2516*13080SPavan.Mettu@Oracle.COM 			break;
2517*13080SPavan.Mettu@Oracle.COM 		case SVC_CLIENT:
2518*13080SPavan.Mettu@Oracle.COM 			service = NFS_CLIENT_SVC;
2519*13080SPavan.Mettu@Oracle.COM 			break;
2520*13080SPavan.Mettu@Oracle.COM 		case SVC_NFS4CBD:
2521*13080SPavan.Mettu@Oracle.COM 			service = NFS4CBD;
2522*13080SPavan.Mettu@Oracle.COM 			break;
2523*13080SPavan.Mettu@Oracle.COM 		case SVC_NFSMAPID:
2524*13080SPavan.Mettu@Oracle.COM 			service = NFSMAPID;
2525*13080SPavan.Mettu@Oracle.COM 			break;
2526*13080SPavan.Mettu@Oracle.COM 		case SVC_RQUOTAD:
2527*13080SPavan.Mettu@Oracle.COM 			service = RQUOTAD;
2528*13080SPavan.Mettu@Oracle.COM 			break;
2529*13080SPavan.Mettu@Oracle.COM 		case SVC_NFSLOGD:
2530*13080SPavan.Mettu@Oracle.COM 			service = NFSLOGD;
2531*13080SPavan.Mettu@Oracle.COM 			break;
2532*13080SPavan.Mettu@Oracle.COM 		case SVC_REPARSED:
2533*13080SPavan.Mettu@Oracle.COM 			service = REPARSED;
2534*13080SPavan.Mettu@Oracle.COM 			break;
2535*13080SPavan.Mettu@Oracle.COM 		default:
2536*13080SPavan.Mettu@Oracle.COM 			service = NFSD;
2537*13080SPavan.Mettu@Oracle.COM 	}
2538*13080SPavan.Mettu@Oracle.COM 	return (service);
2539*13080SPavan.Mettu@Oracle.COM }
2540*13080SPavan.Mettu@Oracle.COM 
25414345Sdougm /*
2542*13080SPavan.Mettu@Oracle.COM  * initprotofromsmf()
25433910Sdougm  *
2544*13080SPavan.Mettu@Oracle.COM  * Read NFS SMF properties and add the defined values to the
25453910Sdougm  * protoset.  Note that default values are known from the built in
2546*13080SPavan.Mettu@Oracle.COM  * table in case SMF doesn't have a definition. Not having
2547*13080SPavan.Mettu@Oracle.COM  * SMF properties is OK since we have builtin default
2548*13080SPavan.Mettu@Oracle.COM  * values.
25493910Sdougm  */
25503910Sdougm static int
initprotofromsmf()2551*13080SPavan.Mettu@Oracle.COM initprotofromsmf()
25523910Sdougm {
2553*13080SPavan.Mettu@Oracle.COM 	char name[PATH_MAX];
2554*13080SPavan.Mettu@Oracle.COM 	char value[PATH_MAX];
2555*13080SPavan.Mettu@Oracle.COM 	int ret = SA_OK, bufsz = 0, i;
25563910Sdougm 
25573910Sdougm 	protoset = sa_create_protocol_properties("nfs");
2558*13080SPavan.Mettu@Oracle.COM 	if (protoset != NULL) {
2559*13080SPavan.Mettu@Oracle.COM 		for (i = 0; proto_options[i].tag != NULL; i++) {
2560*13080SPavan.Mettu@Oracle.COM 			scf_type_t ptype;
2561*13080SPavan.Mettu@Oracle.COM 			char *svc_name;
25623910Sdougm 
2563*13080SPavan.Mettu@Oracle.COM 			bzero(value, PATH_MAX);
2564*13080SPavan.Mettu@Oracle.COM 			(void) strncpy(name, proto_options[i].name, PATH_MAX);
2565*13080SPavan.Mettu@Oracle.COM 			/* Replace NULL with the correct instance */
2566*13080SPavan.Mettu@Oracle.COM 			ptype = getscftype(proto_options[i].type);
2567*13080SPavan.Mettu@Oracle.COM 			svc_name = getsvcname(proto_options[i].svcs);
2568*13080SPavan.Mettu@Oracle.COM 			bufsz = PATH_MAX;
2569*13080SPavan.Mettu@Oracle.COM 			ret = nfs_smf_get_prop(name, value,
2570*13080SPavan.Mettu@Oracle.COM 			    (char *)DEFAULT_INSTANCE, ptype,
2571*13080SPavan.Mettu@Oracle.COM 			    svc_name, &bufsz);
2572*13080SPavan.Mettu@Oracle.COM 			if (ret == SA_OK) {
2573*13080SPavan.Mettu@Oracle.COM 				ret = extractprop(name, value);
25746162Sdougm 			}
25753910Sdougm 		}
25766019Sdougm 	} else {
25776019Sdougm 		ret = SA_NO_MEMORY;
25783910Sdougm 	}
2579*13080SPavan.Mettu@Oracle.COM 
25806019Sdougm 	return (ret);
25813910Sdougm }
25823910Sdougm 
25833910Sdougm /*
25844345Sdougm  * add_defaults()
25853910Sdougm  *
2586*13080SPavan.Mettu@Oracle.COM  * Add the default values for any property not defined
2587*13080SPavan.Mettu@Oracle.COM  * in NFS SMF repository.
2588*13080SPavan.Mettu@Oracle.COM  * Values are set according to their defined types.
25893910Sdougm  */
25903910Sdougm 
25913910Sdougm static void
add_defaults()25923910Sdougm add_defaults()
25933910Sdougm {
25943910Sdougm 	int i;
25953910Sdougm 	char number[MAXDIGITS];
25963910Sdougm 
25973910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
25984345Sdougm 		sa_property_t prop;
25994345Sdougm 		prop = sa_get_protocol_property(protoset,
26004345Sdougm 		    proto_options[i].name);
26014345Sdougm 		if (prop == NULL) {
26024345Sdougm 			/* add the default value */
26034345Sdougm 			switch (proto_options[i].type) {
26044345Sdougm 			case OPT_TYPE_NUMBER:
26054345Sdougm 				(void) snprintf(number, sizeof (number), "%d",
26064345Sdougm 				    proto_options[i].defvalue.intval);
26074345Sdougm 				prop = sa_create_property(proto_options[i].name,
26084345Sdougm 				    number);
26094345Sdougm 				break;
26103910Sdougm 
26114345Sdougm 			case OPT_TYPE_BOOLEAN:
26124345Sdougm 				prop = sa_create_property(proto_options[i].name,
26134345Sdougm 				    proto_options[i].defvalue.intval ?
26144345Sdougm 				    "true" : "false");
26154345Sdougm 				break;
26163910Sdougm 
26174345Sdougm 			case OPT_TYPE_ONOFF:
26184345Sdougm 				prop = sa_create_property(proto_options[i].name,
26194345Sdougm 				    proto_options[i].defvalue.intval ?
26204345Sdougm 				    "on" : "off");
26214345Sdougm 				break;
26223910Sdougm 
26234345Sdougm 			default:
26244345Sdougm 				/* treat as strings of zero length */
26254345Sdougm 				prop = sa_create_property(proto_options[i].name,
26264345Sdougm 				    "");
26274345Sdougm 				break;
26284345Sdougm 			}
26294345Sdougm 			if (prop != NULL)
26304345Sdougm 				(void) sa_add_protocol_property(protoset, prop);
26313910Sdougm 		}
26323910Sdougm 	}
26333910Sdougm }
26343910Sdougm 
26353910Sdougm static void
free_protoprops()26363910Sdougm free_protoprops()
26373910Sdougm {
26385179Sdougm 	if (protoset != NULL) {
26395179Sdougm 		xmlFreeNode(protoset);
26405179Sdougm 		protoset = NULL;
26415179Sdougm 	}
26423910Sdougm }
26433910Sdougm 
26443910Sdougm /*
26453910Sdougm  * nfs_init()
26463910Sdougm  *
26473910Sdougm  * Initialize the NFS plugin.
26483910Sdougm  */
26493910Sdougm 
26503910Sdougm static int
nfs_init()26513910Sdougm nfs_init()
26523910Sdougm {
26533910Sdougm 	int ret = SA_OK;
26543910Sdougm 
26556162Sdougm 	if (sa_plugin_ops.sa_init != nfs_init) {
26564345Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
26574345Sdougm 		    "NFS plugin not properly initialized\n"));
26586162Sdougm 		return (SA_CONFIG_ERR);
26596162Sdougm 	}
26603910Sdougm 
2661*13080SPavan.Mettu@Oracle.COM 	ret = initprotofromsmf();
26626162Sdougm 	if (ret != SA_OK) {
26636162Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2664*13080SPavan.Mettu@Oracle.COM 		    "NFS plugin problem with SMF repository: %s\n"),
26656162Sdougm 		    sa_errorstr(ret));
26666162Sdougm 		ret = SA_OK;
26676162Sdougm 	}
26686162Sdougm 	add_defaults();
26693910Sdougm 
26703910Sdougm 	return (ret);
26713910Sdougm }
26723910Sdougm 
26733910Sdougm /*
26743910Sdougm  * nfs_fini()
26753910Sdougm  *
26763910Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
26773910Sdougm  */
26783910Sdougm 
26793910Sdougm static void
nfs_fini()26803910Sdougm nfs_fini()
26813910Sdougm {
26823910Sdougm 	free_protoprops();
26833910Sdougm }
26843910Sdougm 
26853910Sdougm /*
26863910Sdougm  * nfs_get_proto_set()
26873910Sdougm  *
26883910Sdougm  * Return an optionset with all the protocol specific properties in
26893910Sdougm  * it.
26903910Sdougm  */
26913910Sdougm 
26923910Sdougm static sa_protocol_properties_t
nfs_get_proto_set()26933910Sdougm nfs_get_proto_set()
26943910Sdougm {
26953910Sdougm 	return (protoset);
26963910Sdougm }
26973910Sdougm 
26983910Sdougm /*
26993910Sdougm  * service_in_state(service, chkstate)
27003910Sdougm  *
27013910Sdougm  * Want to know if the specified service is in the desired state
27023910Sdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
27033910Sdougm  * isn't.
27043910Sdougm  */
27053910Sdougm static int
service_in_state(char * service,const char * chkstate)27063910Sdougm service_in_state(char *service, const char *chkstate)
27073910Sdougm {
27083910Sdougm 	char *state;
27093910Sdougm 	int ret = B_FALSE;
27103910Sdougm 
27113910Sdougm 	state = smf_get_state(service);
27123910Sdougm 	if (state != NULL) {
27134345Sdougm 		/* got the state so get the equality for the return value */
27144345Sdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
27154345Sdougm 		free(state);
27163910Sdougm 	}
27173910Sdougm 	return (ret);
27183910Sdougm }
27193910Sdougm 
27203910Sdougm /*
27213910Sdougm  * restart_service(svcs)
27223910Sdougm  *
27233910Sdougm  * Walk through the bit mask of services that need to be restarted in
27243910Sdougm  * order to use the new property values. Some properties affect
27253910Sdougm  * multiple daemons. Should only restart a service if it is currently
27263910Sdougm  * enabled (online).
27273910Sdougm  */
27283910Sdougm 
27293910Sdougm static void
restart_service(uint32_t svcs)27303910Sdougm restart_service(uint32_t svcs)
27313910Sdougm {
27323910Sdougm 	uint32_t mask;
27333910Sdougm 	int ret;
27343910Sdougm 	char *service;
27353910Sdougm 
27363910Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
27374345Sdougm 		switch (svcs & mask) {
27384345Sdougm 		case SVC_LOCKD:
27394345Sdougm 			service = LOCKD;
27404345Sdougm 			break;
27414345Sdougm 		case SVC_STATD:
27424345Sdougm 			service = STATD;
27434345Sdougm 			break;
27444345Sdougm 		case SVC_NFSD:
27454345Sdougm 			service = NFSD;
27464345Sdougm 			break;
27474345Sdougm 		case SVC_MOUNTD:
27484345Sdougm 			service = MOUNTD;
27494345Sdougm 			break;
27504345Sdougm 		case SVC_NFS4CBD:
27514345Sdougm 			service = NFS4CBD;
27524345Sdougm 			break;
27534345Sdougm 		case SVC_NFSMAPID:
27544345Sdougm 			service = NFSMAPID;
27554345Sdougm 			break;
27564345Sdougm 		case SVC_RQUOTAD:
27574345Sdougm 			service = RQUOTAD;
27584345Sdougm 			break;
27594345Sdougm 		case SVC_NFSLOGD:
27604345Sdougm 			service = NFSLOGD;
27614345Sdougm 			break;
276211291SRobert.Thurlow@Sun.COM 		case SVC_REPARSED:
276311291SRobert.Thurlow@Sun.COM 			service = REPARSED;
276411291SRobert.Thurlow@Sun.COM 			break;
2765*13080SPavan.Mettu@Oracle.COM 		case SVC_CLIENT:
2766*13080SPavan.Mettu@Oracle.COM 			service = NFS_CLIENT_SVC;
2767*13080SPavan.Mettu@Oracle.COM 			break;
27684345Sdougm 		default:
27694345Sdougm 			continue;
27704345Sdougm 		}
27713910Sdougm 
27723910Sdougm 		/*
27733910Sdougm 		 * Only attempt to restart the service if it is
27743910Sdougm 		 * currently running. In the future, it may be
27753910Sdougm 		 * desirable to use smf_refresh_instance if the NFS
27763910Sdougm 		 * services ever implement the refresh method.
27773910Sdougm 		 */
27784345Sdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
27794345Sdougm 			ret = smf_restart_instance(service);
27803910Sdougm 			/*
27814345Sdougm 			 * There are only a few SMF errors at this point, but
27824345Sdougm 			 * it is also possible that a bad value may have put
27834345Sdougm 			 * the service into maintenance if there wasn't an
27844345Sdougm 			 * SMF level error.
27853910Sdougm 			 */
27864345Sdougm 			if (ret != 0) {
27874345Sdougm 				(void) fprintf(stderr,
27884345Sdougm 				    dgettext(TEXT_DOMAIN,
27894345Sdougm 				    "%s failed to restart: %s\n"),
27904345Sdougm 				    scf_strerror(scf_error()));
27914345Sdougm 			} else {
27924345Sdougm 				/*
27934345Sdougm 				 * Check whether it has gone to "maintenance"
27944345Sdougm 				 * mode or not. Maintenance implies something
27954345Sdougm 				 * went wrong.
27964345Sdougm 				 */
27974345Sdougm 				if (service_in_state(service,
27984345Sdougm 				    SCF_STATE_STRING_MAINT)) {
27994345Sdougm 					(void) fprintf(stderr,
28004345Sdougm 					    dgettext(TEXT_DOMAIN,
28014345Sdougm 					    "%s failed to restart\n"),
28024345Sdougm 					    service);
28034345Sdougm 				}
28044345Sdougm 			}
28053910Sdougm 		}
28064345Sdougm 		svcs &= ~mask;
28073910Sdougm 	}
28083910Sdougm }
28093910Sdougm 
28103910Sdougm /*
28113910Sdougm  * nfs_minmax_check(name, value)
28123910Sdougm  *
28133910Sdougm  * Verify that the value for the property specified by index is valid
28143910Sdougm  * relative to the opposite value in the case of a min/max variable.
28153910Sdougm  * Currently, server_minvers/server_maxvers and
28163910Sdougm  * client_minvers/client_maxvers are the only ones to check.
28173910Sdougm  */
28183910Sdougm 
28193910Sdougm static int
nfs_minmax_check(int index,int value)28203910Sdougm nfs_minmax_check(int index, int value)
28213910Sdougm {
28223910Sdougm 	int val;
28233910Sdougm 	char *pval;
28243910Sdougm 	sa_property_t prop;
28253910Sdougm 	sa_optionset_t opts;
28263910Sdougm 	int ret = B_TRUE;
28273910Sdougm 
28283910Sdougm 	if (proto_options[index].other != NULL) {
28294345Sdougm 		/* have a property to compare against */
28304345Sdougm 		opts = nfs_get_proto_set();
28314345Sdougm 		prop = sa_get_property(opts, proto_options[index].other);
28323910Sdougm 		/*
28333910Sdougm 		 * If we don't find the property, assume default
28343910Sdougm 		 * values which will work since the max will be at the
28353910Sdougm 		 * max and the min at the min.
28363910Sdougm 		 */
28374345Sdougm 		if (prop != NULL) {
28384345Sdougm 			pval = sa_get_property_attr(prop, "value");
28394345Sdougm 			if (pval != NULL) {
28404345Sdougm 				val = strtoul(pval, NULL, 0);
28414345Sdougm 				if (proto_options[index].compare ==
28424345Sdougm 				    OPT_CMP_LE) {
28434345Sdougm 					ret = value <= val ? B_TRUE : B_FALSE;
28444345Sdougm 				} else if (proto_options[index].compare ==
28454345Sdougm 				    OPT_CMP_GE) {
28464345Sdougm 					ret = value >= val ? B_TRUE : B_FALSE;
28474345Sdougm 				}
284811337SWilliam.Krier@Sun.COM 				sa_free_attr_string(pval);
28494345Sdougm 			}
28503910Sdougm 		}
28513910Sdougm 	}
28523910Sdougm 	return (ret);
28533910Sdougm }
28543910Sdougm 
28553910Sdougm /*
28563910Sdougm  * nfs_validate_proto_prop(index, name, value)
28573910Sdougm  *
28585331Samw  * Verify that the property specified by name can take the new
28593910Sdougm  * value. This is a sanity check to prevent bad values getting into
28603910Sdougm  * the default files. All values need to be checked against what is
28613910Sdougm  * allowed by their defined type. If a type isn't explicitly defined
28623910Sdougm  * here, it is treated as a string.
28633910Sdougm  *
28643910Sdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
28653910Sdougm  * within the range specified and potentially against another property
28663910Sdougm  * value as well as specified in the proto_options members other and
28673910Sdougm  * compare.
28683910Sdougm  */
28693910Sdougm 
28703910Sdougm static int
nfs_validate_proto_prop(int index,char * name,char * value)28713910Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
28723910Sdougm {
28733910Sdougm 	int ret = SA_OK;
28743910Sdougm 	char *cp;
28753910Sdougm #ifdef lint
28763910Sdougm 	name = name;
28773910Sdougm #endif
28783910Sdougm 	switch (proto_options[index].type) {
28793910Sdougm 	case OPT_TYPE_NUMBER:
28804345Sdougm 		if (!is_a_number(value))
28814345Sdougm 			ret = SA_BAD_VALUE;
28824345Sdougm 		else {
28834345Sdougm 			int val;
28844345Sdougm 			val = strtoul(value, NULL, 0);
28854345Sdougm 			if (val < proto_options[index].minval ||
28864345Sdougm 			    val > proto_options[index].maxval)
28874345Sdougm 				ret = SA_BAD_VALUE;
28884345Sdougm 			/*
28894345Sdougm 			 * For server_versmin/server_versmax and
28904345Sdougm 			 * client_versmin/client_versmax, the value of the
28914345Sdougm 			 * min(max) should be checked to be correct relative
28924345Sdougm 			 * to the current max(min).
28934345Sdougm 			 */
28944345Sdougm 			if (!nfs_minmax_check(index, val)) {
28954345Sdougm 				ret = SA_BAD_VALUE;
28964345Sdougm 			}
28973910Sdougm 		}
28984345Sdougm 		break;
28993910Sdougm 
29003910Sdougm 	case OPT_TYPE_DOMAIN:
29013910Sdougm 		/*
29023910Sdougm 		 * needs to be a qualified domain so will have at
29033910Sdougm 		 * least one period and other characters on either
29043910Sdougm 		 * side of it.  A zero length string is also allowed
29053910Sdougm 		 * and is the way to turn off the override.
29063910Sdougm 		 */
29074345Sdougm 		if (strlen(value) == 0)
29084345Sdougm 			break;
29094345Sdougm 		cp = strchr(value, '.');
29104345Sdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
29114345Sdougm 			ret = SA_BAD_VALUE;
29123910Sdougm 		break;
29133910Sdougm 
29143910Sdougm 	case OPT_TYPE_BOOLEAN:
29154345Sdougm 		if (strlen(value) == 0 ||
29164345Sdougm 		    strcasecmp(value, "true") == 0 ||
29174345Sdougm 		    strcmp(value, "1") == 0 ||
29184345Sdougm 		    strcasecmp(value, "false") == 0 ||
29194345Sdougm 		    strcmp(value, "0") == 0) {
29204345Sdougm 			ret = SA_OK;
29214345Sdougm 		} else {
29224345Sdougm 			ret = SA_BAD_VALUE;
29234345Sdougm 		}
29244345Sdougm 		break;
29253910Sdougm 
29263910Sdougm 	case OPT_TYPE_ONOFF:
29274345Sdougm 		if (strcasecmp(value, "on") != 0 &&
29284345Sdougm 		    strcasecmp(value, "off") != 0) {
29294345Sdougm 			ret = SA_BAD_VALUE;
29304345Sdougm 		}
29314345Sdougm 		break;
29323910Sdougm 
29333910Sdougm 	case OPT_TYPE_PROTOCOL:
29346162Sdougm 		if (strlen(value) != 0 &&
29356162Sdougm 		    strcasecmp(value, "all") != 0 &&
29364345Sdougm 		    strcasecmp(value, "tcp") != 0 &&
29374345Sdougm 		    strcasecmp(value, "udp") != 0)
29384345Sdougm 			ret = SA_BAD_VALUE;
29394345Sdougm 		break;
29403910Sdougm 
29413910Sdougm 	default:
29424345Sdougm 		/* treat as a string */
29434345Sdougm 		break;
29443910Sdougm 	}
29453910Sdougm 	return (ret);
29463910Sdougm }
29473910Sdougm 
29483910Sdougm /*
29493910Sdougm  * nfs_set_proto_prop(prop)
29503910Sdougm  *
29513910Sdougm  * check that prop is valid.
29523910Sdougm  */
29533910Sdougm 
29543910Sdougm static int
nfs_set_proto_prop(sa_property_t prop)29553910Sdougm nfs_set_proto_prop(sa_property_t prop)
29563910Sdougm {
29573910Sdougm 	int ret = SA_OK;
29583910Sdougm 	char *name;
29593910Sdougm 	char *value;
29603910Sdougm 
29613910Sdougm 	name = sa_get_property_attr(prop, "type");
29623910Sdougm 	value = sa_get_property_attr(prop, "value");
29633910Sdougm 	if (name != NULL && value != NULL) {
2964*13080SPavan.Mettu@Oracle.COM 		scf_type_t sctype;
2965*13080SPavan.Mettu@Oracle.COM 		char *svc_name;
2966*13080SPavan.Mettu@Oracle.COM 		char *instance = NULL;
29674345Sdougm 		int index = findprotoopt(name, 1);
2968*13080SPavan.Mettu@Oracle.COM 
2969*13080SPavan.Mettu@Oracle.COM 		ret = nfs_validate_proto_prop(index, name, value);
2970*13080SPavan.Mettu@Oracle.COM 		if (ret == SA_OK) {
2971*13080SPavan.Mettu@Oracle.COM 			sctype = getscftype(proto_options[index].type);
2972*13080SPavan.Mettu@Oracle.COM 			svc_name = getsvcname(proto_options[index].svcs);
2973*13080SPavan.Mettu@Oracle.COM 			if (sctype == SCF_TYPE_BOOLEAN) {
2974*13080SPavan.Mettu@Oracle.COM 				if (value != NULL)
2975*13080SPavan.Mettu@Oracle.COM 					sa_free_attr_string(value);
2976*13080SPavan.Mettu@Oracle.COM 				if (string_to_boolean(value) == 0)
2977*13080SPavan.Mettu@Oracle.COM 					value = strdup("0");
2978*13080SPavan.Mettu@Oracle.COM 				else
2979*13080SPavan.Mettu@Oracle.COM 					value = strdup("1");
2980*13080SPavan.Mettu@Oracle.COM 			}
2981*13080SPavan.Mettu@Oracle.COM 			ret = nfs_smf_set_prop(name, value, instance, sctype,
2982*13080SPavan.Mettu@Oracle.COM 			    svc_name);
2983*13080SPavan.Mettu@Oracle.COM 			if (ret == SA_OK) {
29844345Sdougm 				restart_service(proto_options[index].svcs);
2985*13080SPavan.Mettu@Oracle.COM 			} else {
2986*13080SPavan.Mettu@Oracle.COM 				(void) printf(dgettext(TEXT_DOMAIN,
2987*13080SPavan.Mettu@Oracle.COM 				    "Cannot restart NFS services : %s\n"),
2988*13080SPavan.Mettu@Oracle.COM 				    sa_errorstr(ret));
2989*13080SPavan.Mettu@Oracle.COM 			}
29904345Sdougm 		}
29913910Sdougm 	}
29923910Sdougm 	if (name != NULL)
29934345Sdougm 		sa_free_attr_string(name);
29943910Sdougm 	if (value != NULL)
29954345Sdougm 		sa_free_attr_string(value);
29963910Sdougm 	return (ret);
29973910Sdougm }
29983910Sdougm 
29993910Sdougm /*
30003910Sdougm  * nfs_get_status()
30013910Sdougm  *
30023910Sdougm  * What is the current status of the nfsd? We use the SMF state here.
30033910Sdougm  * Caller must free the returned value.
30043910Sdougm  */
30053910Sdougm 
30063910Sdougm static char *
nfs_get_status()30073910Sdougm nfs_get_status()
30083910Sdougm {
30093910Sdougm 	char *state;
30103910Sdougm 	state = smf_get_state(NFSD);
30113910Sdougm 	return (state != NULL ? state : strdup("-"));
30123910Sdougm }
30133910Sdougm 
30143910Sdougm /*
30153910Sdougm  * nfs_space_alias(alias)
30163910Sdougm  *
30173910Sdougm  * Lookup the space (security) name. If it is default, convert to the
30183910Sdougm  * real name.
30193910Sdougm  */
30203910Sdougm 
30213910Sdougm static char *
nfs_space_alias(char * space)30223910Sdougm nfs_space_alias(char *space)
30233910Sdougm {
30243910Sdougm 	char *name = space;
30253910Sdougm 	seconfig_t secconf;
30263910Sdougm 
30273910Sdougm 	/*
30283910Sdougm 	 * Only the space named "default" is special. If it is used,
30293910Sdougm 	 * the default needs to be looked up and the real name used.
30303910Sdougm 	 * This is normally "sys" but could be changed.  We always
30313910Sdougm 	 * change defautl to the real name.
30323910Sdougm 	 */
30333910Sdougm 	if (strcmp(space, "default") == 0 &&
30343910Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
30354345Sdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
30364345Sdougm 			name = secconf.sc_name;
30373910Sdougm 	}
30383910Sdougm 	return (strdup(name));
30393910Sdougm }
30405331Samw 
30415331Samw /*
30425331Samw  * nfs_features()
30435331Samw  *
30445331Samw  * Return a mask of the features required.
30455331Samw  */
30465331Samw 
30475331Samw static uint64_t
nfs_features()30485331Samw nfs_features()
30495331Samw {
30506088Sdougm 	return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);
30515331Samw }
3052