xref: /titanic_51/usr/src/lib/libshare/nfs/libshare_nfs.c (revision 07b64d17ebeb1cc3f2777494d108511275630d14)
1549ec3ffSdougm /*
2549ec3ffSdougm  * CDDL HEADER START
3549ec3ffSdougm  *
4549ec3ffSdougm  * The contents of this file are subject to the terms of the
5549ec3ffSdougm  * Common Development and Distribution License (the "License").
6549ec3ffSdougm  * You may not use this file except in compliance with the License.
7549ec3ffSdougm  *
8549ec3ffSdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9549ec3ffSdougm  * or http://www.opensolaris.org/os/licensing.
10549ec3ffSdougm  * See the License for the specific language governing permissions
11549ec3ffSdougm  * and limitations under the License.
12549ec3ffSdougm  *
13549ec3ffSdougm  * When distributing Covered Code, include this CDDL HEADER in each
14549ec3ffSdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15549ec3ffSdougm  * If applicable, add the following below this CDDL HEADER, with the
16549ec3ffSdougm  * fields enclosed by brackets "[]" replaced with your own identifying
17549ec3ffSdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
18549ec3ffSdougm  *
19549ec3ffSdougm  * CDDL HEADER END
20549ec3ffSdougm  */
21549ec3ffSdougm 
22549ec3ffSdougm /*
234b92079bSAlexander Eremin  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24822e6387SMarcel Telka  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2587da98afSVitaliy Gusev  */
26549ec3ffSdougm 
27549ec3ffSdougm /*
28549ec3ffSdougm  * NFS specific functions
29549ec3ffSdougm  */
30549ec3ffSdougm #include <stdio.h>
31549ec3ffSdougm #include <string.h>
32549ec3ffSdougm #include <ctype.h>
33549ec3ffSdougm #include <stdlib.h>
34549ec3ffSdougm #include <unistd.h>
35549ec3ffSdougm #include <zone.h>
36549ec3ffSdougm #include <errno.h>
37549ec3ffSdougm #include <locale.h>
38549ec3ffSdougm #include <signal.h>
39dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include <strings.h>
40549ec3ffSdougm #include "libshare.h"
41549ec3ffSdougm #include "libshare_impl.h"
42549ec3ffSdougm #include <nfs/export.h>
43549ec3ffSdougm #include <pwd.h>
445cb0d679SMarcel Telka #include <grp.h>
45549ec3ffSdougm #include <limits.h>
46549ec3ffSdougm #include <libscf.h>
47dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include <syslog.h>
48dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include <rpcsvc/daemon_utils.h>
49549ec3ffSdougm #include "nfslog_config.h"
50549ec3ffSdougm #include "nfslogtab.h"
51549ec3ffSdougm #include "libshare_nfs.h"
52549ec3ffSdougm #include <nfs/nfs.h>
53ecd6cf80Smarks #include <nfs/nfssys.h>
54dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include "smfcfg.h"
55549ec3ffSdougm 
56549ec3ffSdougm /* should really be in some global place */
57549ec3ffSdougm #define	DEF_WIN	30000
58549ec3ffSdougm #define	OPT_CHUNK	1024
59549ec3ffSdougm 
60549ec3ffSdougm int debug = 0;
61549ec3ffSdougm 
62ecd6cf80Smarks #define	NFS_SERVER_SVC	"svc:/network/nfs/server:default"
63dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #define	NFS_CLIENT_SVC	(char *)"svc:/network/nfs/client:default"
64549ec3ffSdougm 
65549ec3ffSdougm /* internal functions */
66549ec3ffSdougm static int nfs_init();
67549ec3ffSdougm static void nfs_fini();
68549ec3ffSdougm static int nfs_enable_share(sa_share_t);
69ecd6cf80Smarks static int nfs_disable_share(sa_share_t, char *);
70687915e9Sdougm static int nfs_validate_property(sa_handle_t, sa_property_t, sa_optionset_t);
71549ec3ffSdougm static int nfs_validate_security_mode(char *);
72549ec3ffSdougm static int nfs_is_security_opt(char *);
73549ec3ffSdougm static int nfs_parse_legacy_options(sa_group_t, char *);
74549ec3ffSdougm static char *nfs_format_options(sa_group_t, int);
75549ec3ffSdougm static int nfs_set_proto_prop(sa_property_t);
76549ec3ffSdougm static sa_protocol_properties_t nfs_get_proto_set();
77549ec3ffSdougm static char *nfs_get_status();
78549ec3ffSdougm static char *nfs_space_alias(char *);
79da6c28aaSamw static uint64_t nfs_features();
80549ec3ffSdougm 
81549ec3ffSdougm /*
82549ec3ffSdougm  * ops vector that provides the protocol specific info and operations
83549ec3ffSdougm  * for share management.
84549ec3ffSdougm  */
85549ec3ffSdougm 
86549ec3ffSdougm struct sa_plugin_ops sa_plugin_ops = {
87549ec3ffSdougm 	SA_PLUGIN_VERSION,
88549ec3ffSdougm 	"nfs",
89549ec3ffSdougm 	nfs_init,
90549ec3ffSdougm 	nfs_fini,
91549ec3ffSdougm 	nfs_enable_share,
92549ec3ffSdougm 	nfs_disable_share,
93549ec3ffSdougm 	nfs_validate_property,
94549ec3ffSdougm 	nfs_validate_security_mode,
95549ec3ffSdougm 	nfs_is_security_opt,
96549ec3ffSdougm 	nfs_parse_legacy_options,
97549ec3ffSdougm 	nfs_format_options,
98549ec3ffSdougm 	nfs_set_proto_prop,
99549ec3ffSdougm 	nfs_get_proto_set,
100549ec3ffSdougm 	nfs_get_status,
101549ec3ffSdougm 	nfs_space_alias,
102da6c28aaSamw 	NULL,	/* update_legacy */
103da6c28aaSamw 	NULL,	/* delete_legacy */
104da6c28aaSamw 	NULL,	/* change_notify */
105da6c28aaSamw 	NULL,	/* enable_resource */
106da6c28aaSamw 	NULL,	/* disable_resource */
107da6c28aaSamw 	nfs_features,
108da6c28aaSamw 	NULL,	/* transient shares */
109da6c28aaSamw 	NULL,	/* notify resource */
1104bff34e3Sthurlow 	NULL,	/* rename_resource */
1114bff34e3Sthurlow 	NULL,	/* run_command */
1124bff34e3Sthurlow 	NULL,	/* command_help */
1134bff34e3Sthurlow 	NULL	/* delete_proto_section */
114549ec3ffSdougm };
115549ec3ffSdougm 
116549ec3ffSdougm /*
117549ec3ffSdougm  * list of support services needed
118549ec3ffSdougm  * defines should come from head/rpcsvc/daemon_utils.h
119549ec3ffSdougm  */
120549ec3ffSdougm 
121549ec3ffSdougm static char *service_list_default[] =
1222f172c55SRobert Thurlow 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, REPARSED, NULL };
123549ec3ffSdougm static char *service_list_logging[] =
1242f172c55SRobert Thurlow 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, REPARSED,
1252f172c55SRobert Thurlow 	    NULL };
126549ec3ffSdougm 
127549ec3ffSdougm /*
128549ec3ffSdougm  * option definitions.  Make sure to keep the #define for the option
129549ec3ffSdougm  * index just before the entry it is the index for. Changing the order
130549ec3ffSdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
131549ec3ffSdougm  * line that includes the SHOPT_RW and OPT_RW entries.
132549ec3ffSdougm  */
133549ec3ffSdougm 
134549ec3ffSdougm struct option_defs optdefs[] = {
135549ec3ffSdougm #define	OPT_RO		0
136549ec3ffSdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
137549ec3ffSdougm #define	OPT_RW		1
138549ec3ffSdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
139549ec3ffSdougm #define	OPT_ROOT	2
140549ec3ffSdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
141549ec3ffSdougm #define	OPT_SECURE	3
142549ec3ffSdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
143549ec3ffSdougm #define	OPT_ANON	4
144549ec3ffSdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
145549ec3ffSdougm #define	OPT_WINDOW	5
146549ec3ffSdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
147549ec3ffSdougm #define	OPT_NOSUID	6
148549ec3ffSdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
149549ec3ffSdougm #define	OPT_ACLOK	7
150549ec3ffSdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
151549ec3ffSdougm #define	OPT_NOSUB	8
152549ec3ffSdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
153549ec3ffSdougm #define	OPT_SEC		9
154549ec3ffSdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
155549ec3ffSdougm #define	OPT_PUBLIC	10
156549ec3ffSdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
157549ec3ffSdougm #define	OPT_INDEX	11
158549ec3ffSdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
159549ec3ffSdougm #define	OPT_LOG		12
160549ec3ffSdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
161549ec3ffSdougm #define	OPT_CKSUM	13
162549ec3ffSdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
163b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_NONE	14
164b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{SHOPT_NONE, OPT_NONE, OPT_TYPE_ACCLIST},
165b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_ROOT_MAPPING	15
166b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{SHOPT_ROOT_MAPPING, OPT_ROOT_MAPPING, OPT_TYPE_USER},
167b89a8333Snatalie li - Sun Microsystems - Irvine United States #define	OPT_CHARSET_MAP	16
168b89a8333Snatalie li - Sun Microsystems - Irvine United States 	{"", OPT_CHARSET_MAP, OPT_TYPE_ACCLIST},
1696764e83bSVallish Vaidyeshwara #define	OPT_NOACLFAB	17
1706764e83bSVallish Vaidyeshwara 	{SHOPT_NOACLFAB, OPT_NOACLFAB, OPT_TYPE_BOOLEAN},
1715cb0d679SMarcel Telka #define	OPT_UIDMAP	18
1725cb0d679SMarcel Telka 	{SHOPT_UIDMAP, OPT_UIDMAP, OPT_TYPE_MAPPING},
1735cb0d679SMarcel Telka #define	OPT_GIDMAP	19
1745cb0d679SMarcel Telka 	{SHOPT_GIDMAP, OPT_GIDMAP, OPT_TYPE_MAPPING},
175549ec3ffSdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
1765cb0d679SMarcel Telka #define	OPT_VOLFH	20
177549ec3ffSdougm 	{SHOPT_VOLFH, OPT_VOLFH},
178549ec3ffSdougm #endif /* VOLATILE_FH_TEST */
179549ec3ffSdougm 	NULL
180549ec3ffSdougm };
181549ec3ffSdougm 
182549ec3ffSdougm /*
183b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Codesets that may need to be converted to UTF-8 for file paths.
184b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Add new names here to add new property support. If we ever get a
185b89a8333Snatalie li - Sun Microsystems - Irvine United States  * way to query the kernel for character sets, this should become
186b89a8333Snatalie li - Sun Microsystems - Irvine United States  * dynamically loaded. Make sure changes here are reflected in
187b89a8333Snatalie li - Sun Microsystems - Irvine United States  * cmd/fs.d/nfs/mountd/nfscmd.c
188b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
189b89a8333Snatalie li - Sun Microsystems - Irvine United States 
190b89a8333Snatalie li - Sun Microsystems - Irvine United States static char *legal_conv[] = {
191b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-cn",
192b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-jp",
193b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-jpms",
194b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-kr",
195b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"euc-tw",
196b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-1",
197b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-2",
198b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-5",
199b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-6",
200b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-7",
201b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-8",
202b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-9",
203b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-13",
204b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"iso8859-15",
205b89a8333Snatalie li - Sun Microsystems - Irvine United States 	"koi8-r",
206b89a8333Snatalie li - Sun Microsystems - Irvine United States 	NULL
207b89a8333Snatalie li - Sun Microsystems - Irvine United States };
208b89a8333Snatalie li - Sun Microsystems - Irvine United States 
209b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
210549ec3ffSdougm  * list of properties that are related to security flavors.
211549ec3ffSdougm  */
212549ec3ffSdougm static char *seclist[] = {
213549ec3ffSdougm 	SHOPT_RO,
214549ec3ffSdougm 	SHOPT_RW,
215549ec3ffSdougm 	SHOPT_ROOT,
216549ec3ffSdougm 	SHOPT_WINDOW,
217b89a8333Snatalie li - Sun Microsystems - Irvine United States 	SHOPT_NONE,
218b89a8333Snatalie li - Sun Microsystems - Irvine United States 	SHOPT_ROOT_MAPPING,
2195cb0d679SMarcel Telka 	SHOPT_UIDMAP,
2205cb0d679SMarcel Telka 	SHOPT_GIDMAP,
221549ec3ffSdougm 	NULL
222549ec3ffSdougm };
223549ec3ffSdougm 
224549ec3ffSdougm /* structure for list of securities */
225549ec3ffSdougm struct securities {
226549ec3ffSdougm 	sa_security_t security;
227549ec3ffSdougm 	struct securities *next;
228549ec3ffSdougm };
229549ec3ffSdougm 
230549ec3ffSdougm /*
231b89a8333Snatalie li - Sun Microsystems - Irvine United States  * findcharset(charset)
232b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
233b89a8333Snatalie li - Sun Microsystems - Irvine United States  * Returns B_TRUE if the charset is a legal conversion otherwise
234b89a8333Snatalie li - Sun Microsystems - Irvine United States  * B_FALSE. This will need to be rewritten to be more efficient when
235b89a8333Snatalie li - Sun Microsystems - Irvine United States  * we have a dynamic list of legal conversions.
236b89a8333Snatalie li - Sun Microsystems - Irvine United States  */
237b89a8333Snatalie li - Sun Microsystems - Irvine United States 
238b89a8333Snatalie li - Sun Microsystems - Irvine United States static boolean_t
239b89a8333Snatalie li - Sun Microsystems - Irvine United States findcharset(char *charset)
240b89a8333Snatalie li - Sun Microsystems - Irvine United States {
241b89a8333Snatalie li - Sun Microsystems - Irvine United States 	int i;
242b89a8333Snatalie li - Sun Microsystems - Irvine United States 
243b89a8333Snatalie li - Sun Microsystems - Irvine United States 	for (i = 0; legal_conv[i] != NULL; i++)
244b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (strcmp(charset, legal_conv[i]) == 0)
245b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (B_TRUE);
246b89a8333Snatalie li - Sun Microsystems - Irvine United States 	return (B_FALSE);
247b89a8333Snatalie li - Sun Microsystems - Irvine United States }
248b89a8333Snatalie li - Sun Microsystems - Irvine United States 
249b89a8333Snatalie li - Sun Microsystems - Irvine United States /*
250549ec3ffSdougm  * findopt(name)
251549ec3ffSdougm  *
252549ec3ffSdougm  * Lookup option "name" in the option table and return the table
253549ec3ffSdougm  * index.
254549ec3ffSdougm  */
255549ec3ffSdougm 
256549ec3ffSdougm static int
257549ec3ffSdougm findopt(char *name)
258549ec3ffSdougm {
259549ec3ffSdougm 	int i;
260549ec3ffSdougm 	if (name != NULL) {
261549ec3ffSdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
262549ec3ffSdougm 			if (strcmp(optdefs[i].tag, name) == 0)
26387da98afSVitaliy Gusev 				return (optdefs[i].index);
264549ec3ffSdougm 		}
265b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (findcharset(name))
266b89a8333Snatalie li - Sun Microsystems - Irvine United States 			return (OPT_CHARSET_MAP);
267549ec3ffSdougm 	}
268549ec3ffSdougm 	return (-1);
269549ec3ffSdougm }
270549ec3ffSdougm 
271549ec3ffSdougm /*
272549ec3ffSdougm  * gettype(name)
273549ec3ffSdougm  *
274549ec3ffSdougm  * Return the type of option "name".
275549ec3ffSdougm  */
276549ec3ffSdougm 
277549ec3ffSdougm static int
278549ec3ffSdougm gettype(char *name)
279549ec3ffSdougm {
280549ec3ffSdougm 	int optdef;
281549ec3ffSdougm 
282549ec3ffSdougm 	optdef = findopt(name);
283549ec3ffSdougm 	if (optdef != -1)
284549ec3ffSdougm 		return (optdefs[optdef].type);
285549ec3ffSdougm 	return (OPT_TYPE_ANY);
286549ec3ffSdougm }
287549ec3ffSdougm 
288549ec3ffSdougm /*
289549ec3ffSdougm  * nfs_validate_security_mode(mode)
290549ec3ffSdougm  *
291549ec3ffSdougm  * is the specified mode string a valid one for use with NFS?
292549ec3ffSdougm  */
293549ec3ffSdougm 
294549ec3ffSdougm static int
295549ec3ffSdougm nfs_validate_security_mode(char *mode)
296549ec3ffSdougm {
297549ec3ffSdougm 	seconfig_t secinfo;
298549ec3ffSdougm 	int err;
299549ec3ffSdougm 
300549ec3ffSdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
301549ec3ffSdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
302549ec3ffSdougm 	if (err == SC_NOERROR)
303549ec3ffSdougm 		return (1);
304549ec3ffSdougm 	return (0);
305549ec3ffSdougm }
306549ec3ffSdougm 
307549ec3ffSdougm /*
308549ec3ffSdougm  * nfs_is_security_opt(tok)
309549ec3ffSdougm  *
310549ec3ffSdougm  * check to see if tok represents an option that is only valid in some
311549ec3ffSdougm  * security flavor.
312549ec3ffSdougm  */
313549ec3ffSdougm 
314549ec3ffSdougm static int
315549ec3ffSdougm nfs_is_security_opt(char *tok)
316549ec3ffSdougm {
317549ec3ffSdougm 	int i;
318549ec3ffSdougm 
319549ec3ffSdougm 	for (i = 0; seclist[i] != NULL; i++) {
320549ec3ffSdougm 		if (strcmp(tok, seclist[i]) == 0)
321549ec3ffSdougm 			return (1);
322549ec3ffSdougm 	}
323549ec3ffSdougm 	return (0);
324549ec3ffSdougm }
325549ec3ffSdougm 
326549ec3ffSdougm /*
327549ec3ffSdougm  * find_security(seclist, sec)
328549ec3ffSdougm  *
329549ec3ffSdougm  * Walk the current list of security flavors and return true if it is
330549ec3ffSdougm  * present, else return false.
331549ec3ffSdougm  */
332549ec3ffSdougm 
333549ec3ffSdougm static int
334549ec3ffSdougm find_security(struct securities *seclist, sa_security_t sec)
335549ec3ffSdougm {
336549ec3ffSdougm 	while (seclist != NULL) {
337549ec3ffSdougm 		if (seclist->security == sec)
338549ec3ffSdougm 			return (1);
339549ec3ffSdougm 		seclist = seclist->next;
340549ec3ffSdougm 	}
341549ec3ffSdougm 	return (0);
342549ec3ffSdougm }
343549ec3ffSdougm 
344549ec3ffSdougm /*
345549ec3ffSdougm  * make_security_list(group, securitymodes, proto)
346549ec3ffSdougm  *	go through the list of securitymodes and add them to the
347549ec3ffSdougm  *	group's list of security optionsets. We also keep a list of
348549ec3ffSdougm  *	those optionsets so we don't have to find them later. All of
349549ec3ffSdougm  *	these will get copies of the same properties.
350549ec3ffSdougm  */
351549ec3ffSdougm 
352549ec3ffSdougm static struct securities *
353549ec3ffSdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
354549ec3ffSdougm {
355549ec3ffSdougm 	char *tok, *next = NULL;
356549ec3ffSdougm 	struct securities *curp, *headp = NULL, *prev;
357549ec3ffSdougm 	sa_security_t check;
358549ec3ffSdougm 	int freetok = 0;
359549ec3ffSdougm 
360549ec3ffSdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
361549ec3ffSdougm 		next = strchr(tok, ':');
362549ec3ffSdougm 		if (next != NULL)
363549ec3ffSdougm 			*next++ = '\0';
364549ec3ffSdougm 		if (strcmp(tok, "default") == 0) {
365549ec3ffSdougm 			/* resolve default into the real type */
366549ec3ffSdougm 			tok = nfs_space_alias(tok);
367549ec3ffSdougm 			freetok = 1;
368549ec3ffSdougm 		}
369549ec3ffSdougm 		check = sa_get_security(group, tok, proto);
370549ec3ffSdougm 
371549ec3ffSdougm 		/* add to the security list if it isn't there already */
372549ec3ffSdougm 		if (check == NULL || !find_security(headp, check)) {
373549ec3ffSdougm 			curp = (struct securities *)calloc(1,
374549ec3ffSdougm 			    sizeof (struct securities));
375549ec3ffSdougm 			if (curp != NULL) {
376549ec3ffSdougm 				if (check == NULL) {
377a3351425Sdougm 					curp->security = sa_create_security(
378a3351425Sdougm 					    group, tok, proto);
379549ec3ffSdougm 				} else {
380549ec3ffSdougm 					curp->security = check;
381549ec3ffSdougm 				}
382549ec3ffSdougm 				/*
383549ec3ffSdougm 				 * note that the first time through the loop,
384549ec3ffSdougm 				 * headp will be NULL and prev will be
385549ec3ffSdougm 				 * undefined.  Since headp is NULL, we set
386549ec3ffSdougm 				 * both it and prev to the curp (first
387549ec3ffSdougm 				 * structure to be allocated).
388549ec3ffSdougm 				 *
389549ec3ffSdougm 				 * later passes through the loop will have
390549ec3ffSdougm 				 * headp not being NULL and prev will be used
391549ec3ffSdougm 				 * to allocate at the end of the list.
392549ec3ffSdougm 				 */
393549ec3ffSdougm 				if (headp == NULL) {
394549ec3ffSdougm 					headp = curp;
395549ec3ffSdougm 					prev = curp;
396549ec3ffSdougm 				} else {
397549ec3ffSdougm 					prev->next = curp;
398549ec3ffSdougm 					prev = curp;
399549ec3ffSdougm 				}
400549ec3ffSdougm 			}
401549ec3ffSdougm 		}
402549ec3ffSdougm 
403549ec3ffSdougm 		if (freetok) {
404549ec3ffSdougm 			freetok = 0;
405549ec3ffSdougm 			sa_free_attr_string(tok);
406549ec3ffSdougm 		}
407549ec3ffSdougm 	}
408549ec3ffSdougm 	return (headp);
409549ec3ffSdougm }
410549ec3ffSdougm 
411549ec3ffSdougm static void
412549ec3ffSdougm free_security_list(struct securities *sec)
413549ec3ffSdougm {
414549ec3ffSdougm 	struct securities *next;
415549ec3ffSdougm 	if (sec != NULL) {
416549ec3ffSdougm 		for (next = sec->next; sec != NULL; sec = next) {
417549ec3ffSdougm 			next = sec->next;
418549ec3ffSdougm 			free(sec);
419549ec3ffSdougm 		}
420549ec3ffSdougm 	}
421549ec3ffSdougm }
422549ec3ffSdougm 
423549ec3ffSdougm /*
424549ec3ffSdougm  * nfs_alistcat(str1, str2, sep)
425549ec3ffSdougm  *
426549ec3ffSdougm  * concatenate str1 and str2 into a new string using sep as a separate
427549ec3ffSdougm  * character. If memory allocation fails, return NULL;
428549ec3ffSdougm  */
429549ec3ffSdougm 
430549ec3ffSdougm static char *
431549ec3ffSdougm nfs_alistcat(char *str1, char *str2, char sep)
432549ec3ffSdougm {
433549ec3ffSdougm 	char *newstr;
434549ec3ffSdougm 	size_t len;
435549ec3ffSdougm 
436549ec3ffSdougm 	len = strlen(str1) + strlen(str2) + 2;
437549ec3ffSdougm 	newstr = (char *)malloc(len);
438549ec3ffSdougm 	if (newstr != NULL)
439549ec3ffSdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
440549ec3ffSdougm 	return (newstr);
441549ec3ffSdougm }
442549ec3ffSdougm 
443549ec3ffSdougm /*
4445cb0d679SMarcel Telka  * add_security_prop(sec, name, value, persist, iszfs)
445549ec3ffSdougm  *
446549ec3ffSdougm  * Add the property to the securities structure. This accumulates
447549ec3ffSdougm  * properties for as part of parsing legacy options.
448549ec3ffSdougm  */
449549ec3ffSdougm 
450549ec3ffSdougm static int
451549ec3ffSdougm add_security_prop(struct securities *sec, char *name, char *value,
452549ec3ffSdougm 			int persist, int iszfs)
453549ec3ffSdougm {
454549ec3ffSdougm 	sa_property_t prop;
455549ec3ffSdougm 	int ret = SA_OK;
456549ec3ffSdougm 
457549ec3ffSdougm 	for (; sec != NULL; sec = sec->next) {
458549ec3ffSdougm 		if (value == NULL) {
459a3351425Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
460a3351425Sdougm 			    strcmp(name, SHOPT_RO) == 0)
461549ec3ffSdougm 				value = "*";
462549ec3ffSdougm 			else
463549ec3ffSdougm 				value = "true";
464549ec3ffSdougm 		}
465549ec3ffSdougm 
466549ec3ffSdougm 		/*
467549ec3ffSdougm 		 * Get the existing property, if it exists, so we can
468549ec3ffSdougm 		 * determine what to do with it. The ro/rw/root
469549ec3ffSdougm 		 * properties can be merged if multiple instances of
470549ec3ffSdougm 		 * these properies are given. For example, if "rw"
471549ec3ffSdougm 		 * exists with a value "host1" and a later token of
472549ec3ffSdougm 		 * rw="host2" is seen, the values are merged into a
473549ec3ffSdougm 		 * single rw="host1:host2".
474549ec3ffSdougm 		 */
475549ec3ffSdougm 		prop = sa_get_property(sec->security, name);
476549ec3ffSdougm 
477549ec3ffSdougm 		if (prop != NULL) {
478549ec3ffSdougm 			char *oldvalue;
479549ec3ffSdougm 			char *newvalue;
480549ec3ffSdougm 
481549ec3ffSdougm 			/*
4825cb0d679SMarcel Telka 			 * The security options of ro/rw/root/uidmap/gidmap
4835cb0d679SMarcel Telka 			 * might appear multiple times.  If they do, the values
4845cb0d679SMarcel Telka 			 * need to be merged.  If it was previously empty, the
4855cb0d679SMarcel Telka 			 * new value alone is added.
486549ec3ffSdougm 			 */
487549ec3ffSdougm 			oldvalue = sa_get_property_attr(prop, "value");
488549ec3ffSdougm 			if (oldvalue != NULL) {
4895cb0d679SMarcel Telka 				char sep = ':';
4905cb0d679SMarcel Telka 
4915cb0d679SMarcel Telka 				if (strcmp(name, SHOPT_UIDMAP) == 0 ||
4925cb0d679SMarcel Telka 				    strcmp(name, SHOPT_GIDMAP) == 0)
4935cb0d679SMarcel Telka 					sep = '~';
4945cb0d679SMarcel Telka 
495549ec3ffSdougm 				/*
496549ec3ffSdougm 				 * The general case is to concatenate the new
497549ec3ffSdougm 				 * value onto the old value for multiple
4985cb0d679SMarcel Telka 				 * rw(ro/root/uidmap/gidmap) properties.  For
4995cb0d679SMarcel Telka 				 * rw/ro/root a special case exists when either
5005cb0d679SMarcel Telka 				 * the old or new is the "all" case.  In the
5015cb0d679SMarcel Telka 				 * special case, if both are "all", then it is
5025cb0d679SMarcel Telka 				 * "all", else if one is an access-list, that
5035cb0d679SMarcel Telka 				 * replaces the "all".
504549ec3ffSdougm 				 */
505549ec3ffSdougm 				if (strcmp(oldvalue, "*") == 0) {
506549ec3ffSdougm 					/* Replace old value with new value. */
507549ec3ffSdougm 					newvalue = strdup(value);
50897df5ac9Sdougm 				} else if (strcmp(value, "*") == 0 ||
50997df5ac9Sdougm 				    strcmp(oldvalue, value) == 0) {
510a3351425Sdougm 					/*
511a3351425Sdougm 					 * Keep old value and ignore
512a3351425Sdougm 					 * the new value.
513a3351425Sdougm 					 */
514549ec3ffSdougm 					newvalue = NULL;
515549ec3ffSdougm 				} else {
516a3351425Sdougm 					/*
517a3351425Sdougm 					 * Make a new list of old plus new
518a3351425Sdougm 					 * access-list.
519a3351425Sdougm 					 */
520a3351425Sdougm 					newvalue = nfs_alistcat(oldvalue,
5215cb0d679SMarcel Telka 					    value, sep);
522549ec3ffSdougm 				}
523549ec3ffSdougm 
524549ec3ffSdougm 				if (newvalue != NULL) {
525549ec3ffSdougm 					(void) sa_remove_property(prop);
526a3351425Sdougm 					prop = sa_create_property(name,
527a3351425Sdougm 					    newvalue);
528a3351425Sdougm 					ret = sa_add_property(sec->security,
529a3351425Sdougm 					    prop);
530549ec3ffSdougm 					free(newvalue);
531549ec3ffSdougm 				}
5325cb0d679SMarcel Telka 
533549ec3ffSdougm 				sa_free_attr_string(oldvalue);
534549ec3ffSdougm 			}
535549ec3ffSdougm 		} else {
536549ec3ffSdougm 			prop = sa_create_property(name, value);
537549ec3ffSdougm 			ret = sa_add_property(sec->security, prop);
538549ec3ffSdougm 		}
539549ec3ffSdougm 		if (ret == SA_OK && !iszfs) {
540549ec3ffSdougm 			ret = sa_commit_properties(sec->security, !persist);
541549ec3ffSdougm 		}
542549ec3ffSdougm 	}
543549ec3ffSdougm 	return (ret);
544549ec3ffSdougm }
545549ec3ffSdougm 
546549ec3ffSdougm /*
547549ec3ffSdougm  * check to see if group/share is persistent.
548549ec3ffSdougm  */
549549ec3ffSdougm static int
550549ec3ffSdougm is_persistent(sa_group_t group)
551549ec3ffSdougm {
552549ec3ffSdougm 	char *type;
553549ec3ffSdougm 	int persist = 1;
554549ec3ffSdougm 
555549ec3ffSdougm 	type = sa_get_group_attr(group, "type");
556549ec3ffSdougm 	if (type != NULL && strcmp(type, "persist") != 0)
557549ec3ffSdougm 		persist = 0;
558549ec3ffSdougm 	if (type != NULL)
559549ec3ffSdougm 		sa_free_attr_string(type);
560549ec3ffSdougm 	return (persist);
561549ec3ffSdougm }
562549ec3ffSdougm 
563549ec3ffSdougm /*
564549ec3ffSdougm  * invalid_security(options)
565549ec3ffSdougm  *
566549ec3ffSdougm  * search option string for any invalid sec= type.
567549ec3ffSdougm  * return true (1) if any are not valid else false (0)
568549ec3ffSdougm  */
569549ec3ffSdougm static int
570549ec3ffSdougm invalid_security(char *options)
571549ec3ffSdougm {
572549ec3ffSdougm 	char *copy, *base, *token, *value;
573549ec3ffSdougm 	int ret = 0;
574549ec3ffSdougm 
575549ec3ffSdougm 	copy = strdup(options);
576549ec3ffSdougm 	token = base = copy;
577549ec3ffSdougm 	while (token != NULL && ret == 0) {
578549ec3ffSdougm 		token = strtok(base, ",");
579549ec3ffSdougm 		base = NULL;
580549ec3ffSdougm 		if (token != NULL) {
581549ec3ffSdougm 			value = strchr(token, '=');
582549ec3ffSdougm 			if (value != NULL)
583549ec3ffSdougm 				*value++ = '\0';
5845cb0d679SMarcel Telka 			if (strcmp(token, SHOPT_SEC) == 0) {
585a3351425Sdougm 				/* HAVE security flavors so check them */
586549ec3ffSdougm 				char *tok, *next;
587a3351425Sdougm 				for (next = NULL, tok = value; tok != NULL;
588a3351425Sdougm 				    tok = next) {
589549ec3ffSdougm 					next = strchr(tok, ':');
590549ec3ffSdougm 					if (next != NULL)
591549ec3ffSdougm 						*next++ = '\0';
592549ec3ffSdougm 					ret = !nfs_validate_security_mode(tok);
593549ec3ffSdougm 					if (ret)
594549ec3ffSdougm 						break;
595549ec3ffSdougm 				}
596549ec3ffSdougm 			}
597549ec3ffSdougm 		}
598549ec3ffSdougm 	}
599549ec3ffSdougm 	if (copy != NULL)
600549ec3ffSdougm 		free(copy);
601549ec3ffSdougm 	return (ret);
602549ec3ffSdougm }
603549ec3ffSdougm 
604549ec3ffSdougm /*
605549ec3ffSdougm  * nfs_parse_legacy_options(group, options)
606549ec3ffSdougm  *
607549ec3ffSdougm  * Parse the old style options into internal format and store on the
608549ec3ffSdougm  * specified group.  Group could be a share for full legacy support.
609549ec3ffSdougm  */
610549ec3ffSdougm 
611549ec3ffSdougm static int
612549ec3ffSdougm nfs_parse_legacy_options(sa_group_t group, char *options)
613549ec3ffSdougm {
614aba7a40bSdougm 	char *dup;
615549ec3ffSdougm 	char *base;
616549ec3ffSdougm 	char *token;
617549ec3ffSdougm 	sa_optionset_t optionset;
618549ec3ffSdougm 	struct securities *security_list = NULL;
619549ec3ffSdougm 	sa_property_t prop;
620549ec3ffSdougm 	int ret = SA_OK;
621549ec3ffSdougm 	int iszfs = 0;
622549ec3ffSdougm 	sa_group_t parent;
623549ec3ffSdougm 	int persist = 0;
624549ec3ffSdougm 	char *lasts;
625549ec3ffSdougm 
626549ec3ffSdougm 	/* do we have an existing optionset? */
627549ec3ffSdougm 	optionset = sa_get_optionset(group, "nfs");
628549ec3ffSdougm 	if (optionset == NULL) {
629549ec3ffSdougm 		/* didn't find existing optionset so create one */
630549ec3ffSdougm 		optionset = sa_create_optionset(group, "nfs");
631549ec3ffSdougm 	} else {
632549ec3ffSdougm 		/*
633da6c28aaSamw 		 * Have an existing optionset . Ideally, we would need
634da6c28aaSamw 		 * to compare options in order to detect errors. For
635da6c28aaSamw 		 * now, we assume that the first optionset is the
636da6c28aaSamw 		 * correct one and the others will be the same. An
637da6c28aaSamw 		 * empty optionset is the same as no optionset so we
638da6c28aaSamw 		 * don't want to exit in that case. Getting an empty
639da6c28aaSamw 		 * optionset can occur with ZFS property checking.
640549ec3ffSdougm 		 */
641da6c28aaSamw 		if (sa_get_property(optionset, NULL) != NULL)
642549ec3ffSdougm 			return (ret);
643549ec3ffSdougm 	}
644549ec3ffSdougm 
645549ec3ffSdougm 	if (strcmp(options, SHOPT_RW) == 0) {
646549ec3ffSdougm 		/*
647549ec3ffSdougm 		 * there is a special case of only the option "rw"
648549ec3ffSdougm 		 * being the default option. We don't have to do
649549ec3ffSdougm 		 * anything.
650549ec3ffSdougm 		 */
651549ec3ffSdougm 		return (ret);
652549ec3ffSdougm 	}
653549ec3ffSdougm 
654549ec3ffSdougm 	/*
655549ec3ffSdougm 	 * check if security types are present and validate them. If
656549ec3ffSdougm 	 * any are not legal, fail.
657549ec3ffSdougm 	 */
658549ec3ffSdougm 
659549ec3ffSdougm 	if (invalid_security(options)) {
660549ec3ffSdougm 		return (SA_INVALID_SECURITY);
661549ec3ffSdougm 	}
662549ec3ffSdougm 
663549ec3ffSdougm 	/*
664549ec3ffSdougm 	 * in order to not attempt to change ZFS properties unless
665549ec3ffSdougm 	 * absolutely necessary, we never do it in the legacy parsing.
666549ec3ffSdougm 	 */
667549ec3ffSdougm 	if (sa_is_share(group)) {
668549ec3ffSdougm 		char *zfs;
669549ec3ffSdougm 		parent = sa_get_parent_group(group);
670549ec3ffSdougm 		if (parent != NULL) {
671549ec3ffSdougm 			zfs = sa_get_group_attr(parent, "zfs");
672549ec3ffSdougm 			if (zfs != NULL) {
673549ec3ffSdougm 				sa_free_attr_string(zfs);
674549ec3ffSdougm 				iszfs++;
675549ec3ffSdougm 			}
676549ec3ffSdougm 		}
677549ec3ffSdougm 	} else {
678549ec3ffSdougm 		iszfs = sa_group_is_zfs(group);
679549ec3ffSdougm 	}
680549ec3ffSdougm 
681aba7a40bSdougm 	/* We need a copy of options for the next part. */
682aba7a40bSdougm 	dup = strdup(options);
683aba7a40bSdougm 	if (dup == NULL)
684aba7a40bSdougm 		return (SA_NO_MEMORY);
685aba7a40bSdougm 
686549ec3ffSdougm 	/*
687549ec3ffSdougm 	 * we need to step through each option in the string and then
688549ec3ffSdougm 	 * add either the option or the security option as needed. If
689549ec3ffSdougm 	 * this is not a persistent share, don't commit to the
690549ec3ffSdougm 	 * repository. If there is an error, we also want to abort the
691549ec3ffSdougm 	 * processing and report it.
692549ec3ffSdougm 	 */
693549ec3ffSdougm 	persist = is_persistent(group);
694549ec3ffSdougm 	base = dup;
695549ec3ffSdougm 	token = dup;
696549ec3ffSdougm 	lasts = NULL;
697549ec3ffSdougm 	while (token != NULL && ret == SA_OK) {
698549ec3ffSdougm 		token = strtok_r(base, ",", &lasts);
699549ec3ffSdougm 		base = NULL;
700549ec3ffSdougm 		if (token != NULL) {
701549ec3ffSdougm 			char *value;
702549ec3ffSdougm 			/*
703549ec3ffSdougm 			 * if the option has a value, it will have an '=' to
704549ec3ffSdougm 			 * separate the name from the value. The following
705549ec3ffSdougm 			 * code will result in value != NULL and token
706549ec3ffSdougm 			 * pointing to just the name if there is a value.
707549ec3ffSdougm 			 */
708549ec3ffSdougm 			value = strchr(token, '=');
709549ec3ffSdougm 			if (value != NULL) {
710549ec3ffSdougm 				*value++ = '\0';
711549ec3ffSdougm 			}
7125cb0d679SMarcel Telka 			if (strcmp(token, SHOPT_SEC) == 0 ||
7135cb0d679SMarcel Telka 			    strcmp(token, SHOPT_SECURE) == 0) {
714549ec3ffSdougm 				/*
715549ec3ffSdougm 				 * Once in security parsing, we only
716549ec3ffSdougm 				 * do security. We do need to move
717549ec3ffSdougm 				 * between the security node and the
718549ec3ffSdougm 				 * toplevel. The security tag goes on
719549ec3ffSdougm 				 * the root while the following ones
720549ec3ffSdougm 				 * go on the security.
721549ec3ffSdougm 				 */
722549ec3ffSdougm 				if (security_list != NULL) {
723a3351425Sdougm 					/*
724a3351425Sdougm 					 * have an old list so close it and
725a3351425Sdougm 					 * start the new
726a3351425Sdougm 					 */
727549ec3ffSdougm 					free_security_list(security_list);
728549ec3ffSdougm 				}
7295cb0d679SMarcel Telka 				if (strcmp(token, SHOPT_SECURE) == 0) {
730549ec3ffSdougm 					value = "dh";
731549ec3ffSdougm 				} else {
732549ec3ffSdougm 					if (value == NULL) {
733549ec3ffSdougm 						ret = SA_SYNTAX_ERR;
734549ec3ffSdougm 						break;
735549ec3ffSdougm 					}
736549ec3ffSdougm 				}
737a3351425Sdougm 				security_list = make_security_list(group,
738a3351425Sdougm 				    value, "nfs");
739549ec3ffSdougm 			} else {
740549ec3ffSdougm 				/*
741549ec3ffSdougm 				 * Note that the "old" syntax allowed a
7425cb0d679SMarcel Telka 				 * default security model.  This must be
743549ec3ffSdougm 				 * accounted for and internally converted to
744549ec3ffSdougm 				 * "standard" security structure.
745549ec3ffSdougm 				 */
746549ec3ffSdougm 				if (nfs_is_security_opt(token)) {
747549ec3ffSdougm 					if (security_list == NULL) {
748549ec3ffSdougm 						/*
749a3351425Sdougm 						 * need to have a
750a3351425Sdougm 						 * security
751a3351425Sdougm 						 * option. This will
752a3351425Sdougm 						 * be "closed" when a
753a3351425Sdougm 						 * defined "sec="
754a3351425Sdougm 						 * option is
755a3351425Sdougm 						 * seen. This is
756a3351425Sdougm 						 * technically an
757a3351425Sdougm 						 * error but will be
758a3351425Sdougm 						 * allowed with
759a3351425Sdougm 						 * warning.
760549ec3ffSdougm 						 */
761a3351425Sdougm 						security_list =
762a3351425Sdougm 						    make_security_list(group,
763549ec3ffSdougm 						    "default",
764549ec3ffSdougm 						    "nfs");
765549ec3ffSdougm 					}
766549ec3ffSdougm 					if (security_list != NULL) {
767a3351425Sdougm 						ret = add_security_prop(
768a3351425Sdougm 						    security_list, token,
769a3351425Sdougm 						    value, persist, iszfs);
770549ec3ffSdougm 					} else {
771549ec3ffSdougm 						ret = SA_NO_MEMORY;
772549ec3ffSdougm 					}
773549ec3ffSdougm 				} else {
774549ec3ffSdougm 					/* regular options */
775549ec3ffSdougm 					if (value == NULL) {
776a3351425Sdougm 						if (strcmp(token, SHOPT_RW) ==
777a3351425Sdougm 						    0 || strcmp(token,
778a3351425Sdougm 						    SHOPT_RO) == 0) {
779549ec3ffSdougm 							value = "*";
780a3351425Sdougm 						} else {
781549ec3ffSdougm 							value = "global";
782a3351425Sdougm 							if (strcmp(token,
783a3351425Sdougm 							    SHOPT_LOG) != 0) {
784549ec3ffSdougm 								value = "true";
785549ec3ffSdougm 							}
786a3351425Sdougm 						}
787d6405362Sdougm 					}
788d6405362Sdougm 					/*
789d6405362Sdougm 					 * In all cases, create the
790d6405362Sdougm 					 * property specified. If the
791d6405362Sdougm 					 * value was NULL, the default
792d6405362Sdougm 					 * value will have been
793d6405362Sdougm 					 * substituted.
794d6405362Sdougm 					 */
795d6405362Sdougm 					prop = sa_create_property(token, value);
796d6405362Sdougm 					ret =  sa_add_property(optionset, prop);
797a3351425Sdougm 					if (ret != SA_OK)
798549ec3ffSdougm 						break;
799d6405362Sdougm 
800549ec3ffSdougm 					if (!iszfs) {
801a3351425Sdougm 						ret = sa_commit_properties(
802a3351425Sdougm 						    optionset, !persist);
803549ec3ffSdougm 					}
804549ec3ffSdougm 				}
805549ec3ffSdougm 			}
806549ec3ffSdougm 		}
807549ec3ffSdougm 	}
808549ec3ffSdougm 	if (security_list != NULL)
809549ec3ffSdougm 		free_security_list(security_list);
810aba7a40bSdougm 
811549ec3ffSdougm 	free(dup);
812549ec3ffSdougm 	return (ret);
813549ec3ffSdougm }
814549ec3ffSdougm 
815549ec3ffSdougm /*
816549ec3ffSdougm  * is_a_number(number)
817549ec3ffSdougm  *
818549ec3ffSdougm  * is the string a number in one of the forms we want to use?
819549ec3ffSdougm  */
820549ec3ffSdougm 
821549ec3ffSdougm static int
822549ec3ffSdougm is_a_number(char *number)
823549ec3ffSdougm {
824549ec3ffSdougm 	int ret = 1;
825549ec3ffSdougm 	int hex = 0;
826549ec3ffSdougm 
827549ec3ffSdougm 	if (strncmp(number, "0x", 2) == 0) {
828549ec3ffSdougm 		number += 2;
829549ec3ffSdougm 		hex = 1;
830a3351425Sdougm 	} else if (*number == '-') {
831549ec3ffSdougm 		number++; /* skip the minus */
832a3351425Sdougm 	}
833549ec3ffSdougm 	while (ret == 1 && *number != '\0') {
834549ec3ffSdougm 		if (hex) {
835549ec3ffSdougm 			ret = isxdigit(*number++);
836549ec3ffSdougm 		} else {
837549ec3ffSdougm 			ret = isdigit(*number++);
838549ec3ffSdougm 		}
839549ec3ffSdougm 	}
840549ec3ffSdougm 	return (ret);
841549ec3ffSdougm }
842549ec3ffSdougm 
843549ec3ffSdougm /*
844549ec3ffSdougm  * Look for the specified tag in the configuration file. If it is found,
845549ec3ffSdougm  * enable logging and set the logging configuration information for exp.
846549ec3ffSdougm  */
847549ec3ffSdougm static void
848549ec3ffSdougm configlog(struct exportdata *exp, char *tag)
849549ec3ffSdougm {
850549ec3ffSdougm 	nfsl_config_t *configlist = NULL, *configp;
851549ec3ffSdougm 	int error = 0;
852549ec3ffSdougm 	char globaltag[] = DEFAULTTAG;
853549ec3ffSdougm 
854549ec3ffSdougm 	/*
855549ec3ffSdougm 	 * Sends config errors to stderr
856549ec3ffSdougm 	 */
857549ec3ffSdougm 	nfsl_errs_to_syslog = B_FALSE;
858549ec3ffSdougm 
859549ec3ffSdougm 	/*
860549ec3ffSdougm 	 * get the list of configuration settings
861549ec3ffSdougm 	 */
862549ec3ffSdougm 	error = nfsl_getconfig_list(&configlist);
863549ec3ffSdougm 	if (error) {
864549ec3ffSdougm 		(void) fprintf(stderr,
865a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
866549ec3ffSdougm 		    strerror(error));
867549ec3ffSdougm 	}
868549ec3ffSdougm 
869549ec3ffSdougm 	if (tag == NULL)
870549ec3ffSdougm 		tag = globaltag;
871549ec3ffSdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
872549ec3ffSdougm 		nfsl_freeconfig_list(&configlist);
873549ec3ffSdougm 		(void) fprintf(stderr,
874a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
875549ec3ffSdougm 		/* bad configuration */
876549ec3ffSdougm 		error = ENOENT;
877549ec3ffSdougm 		goto err;
878549ec3ffSdougm 	}
879549ec3ffSdougm 
880549ec3ffSdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
881549ec3ffSdougm 		error = ENOMEM;
882549ec3ffSdougm 		goto out;
883549ec3ffSdougm 	}
884549ec3ffSdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
885549ec3ffSdougm 		error = ENOMEM;
886549ec3ffSdougm 		goto out;
887549ec3ffSdougm 	}
888549ec3ffSdougm 	exp->ex_flags |= EX_LOG;
889549ec3ffSdougm 	if (configp->nc_rpclogpath != NULL)
890549ec3ffSdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
891549ec3ffSdougm out:
892549ec3ffSdougm 	if (configlist != NULL)
893549ec3ffSdougm 		nfsl_freeconfig_list(&configlist);
894549ec3ffSdougm 
895549ec3ffSdougm err:
896549ec3ffSdougm 	if (error != 0) {
897549ec3ffSdougm 		free(exp->ex_tag);
898549ec3ffSdougm 		free(exp->ex_log_buffer);
899549ec3ffSdougm 		(void) fprintf(stderr,
900a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
901549ec3ffSdougm 		    strerror(error));
902549ec3ffSdougm 	}
903549ec3ffSdougm }
904549ec3ffSdougm 
905549ec3ffSdougm /*
906549ec3ffSdougm  * fill_export_from_optionset(export, optionset)
907549ec3ffSdougm  *
908549ec3ffSdougm  * In order to share, we need to set all the possible general options
909549ec3ffSdougm  * into the export structure. Share info will be filled in by the
910549ec3ffSdougm  * caller. Various property values get turned into structure specific
911549ec3ffSdougm  * values.
912549ec3ffSdougm  */
913549ec3ffSdougm 
914549ec3ffSdougm static int
915549ec3ffSdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
916549ec3ffSdougm {
917549ec3ffSdougm 	sa_property_t option;
918549ec3ffSdougm 	int ret = SA_OK;
919549ec3ffSdougm 
920549ec3ffSdougm 	for (option = sa_get_property(optionset, NULL);
921549ec3ffSdougm 	    option != NULL; option = sa_get_next_property(option)) {
922549ec3ffSdougm 		char *name;
923549ec3ffSdougm 		char *value;
924549ec3ffSdougm 		uint32_t val;
925549ec3ffSdougm 
926549ec3ffSdougm 		/*
927549ec3ffSdougm 		 * since options may be set/reset multiple times, always do an
928549ec3ffSdougm 		 * explicit set or clear of the option. This allows defaults
929da6c28aaSamw 		 * to be set and then the protocol specific to override.
930549ec3ffSdougm 		 */
931549ec3ffSdougm 
932549ec3ffSdougm 		name = sa_get_property_attr(option, "type");
933549ec3ffSdougm 		value = sa_get_property_attr(option, "value");
934549ec3ffSdougm 		switch (findopt(name)) {
935549ec3ffSdougm 		case OPT_ANON:
936549ec3ffSdougm 			if (value != NULL && is_a_number(value)) {
937549ec3ffSdougm 				val = strtoul(value, NULL, 0);
938549ec3ffSdougm 			} else {
939549ec3ffSdougm 				struct passwd *pw;
940549ec3ffSdougm 				pw = getpwnam(value != NULL ? value : "nobody");
941549ec3ffSdougm 				if (pw != NULL) {
942549ec3ffSdougm 					val = pw->pw_uid;
943549ec3ffSdougm 				} else {
944549ec3ffSdougm 					val = UID_NOBODY;
945549ec3ffSdougm 				}
946549ec3ffSdougm 				endpwent();
947549ec3ffSdougm 			}
948549ec3ffSdougm 			export->ex_anon = val;
949549ec3ffSdougm 			break;
950549ec3ffSdougm 		case OPT_NOSUID:
951a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
952a3351425Sdougm 			    strcmp(value, "1") == 0))
953549ec3ffSdougm 				export->ex_flags |= EX_NOSUID;
954549ec3ffSdougm 			else
955549ec3ffSdougm 				export->ex_flags &= ~EX_NOSUID;
956549ec3ffSdougm 			break;
957549ec3ffSdougm 		case OPT_ACLOK:
958a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
959549ec3ffSdougm 			    strcmp(value, "1") == 0))
960549ec3ffSdougm 				export->ex_flags |= EX_ACLOK;
961549ec3ffSdougm 			else
962549ec3ffSdougm 				export->ex_flags &= ~EX_ACLOK;
963549ec3ffSdougm 			break;
964549ec3ffSdougm 		case OPT_NOSUB:
965a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
966a3351425Sdougm 			    strcmp(value, "1") == 0))
967549ec3ffSdougm 				export->ex_flags |= EX_NOSUB;
968549ec3ffSdougm 			else
969549ec3ffSdougm 				export->ex_flags &= ~EX_NOSUB;
970549ec3ffSdougm 			break;
971549ec3ffSdougm 		case OPT_PUBLIC:
972a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
973a3351425Sdougm 			    strcmp(value, "1") == 0))
974549ec3ffSdougm 				export->ex_flags |= EX_PUBLIC;
975549ec3ffSdougm 			else
976549ec3ffSdougm 				export->ex_flags &= ~EX_PUBLIC;
977549ec3ffSdougm 			break;
978549ec3ffSdougm 		case OPT_INDEX:
979a3351425Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
980a3351425Sdougm 			    strchr(value, '/') != NULL)) {
981549ec3ffSdougm 				/* this is an error */
982549ec3ffSdougm 				(void) printf(dgettext(TEXT_DOMAIN,
983549ec3ffSdougm 				    "NFS: index=\"%s\" not valid;"
984549ec3ffSdougm 				    "must be a filename.\n"),
985549ec3ffSdougm 				    value);
986549ec3ffSdougm 				break;
987549ec3ffSdougm 			}
988549ec3ffSdougm 			if (value != NULL && *value != '\0' &&
989549ec3ffSdougm 			    strcmp(value, ".") != 0) {
990549ec3ffSdougm 				/* valid index file string */
991549ec3ffSdougm 				if (export->ex_index != NULL) {
992549ec3ffSdougm 					/* left over from "default" */
993549ec3ffSdougm 					free(export->ex_index);
994549ec3ffSdougm 				}
995a3351425Sdougm 				/* remember to free */
996a3351425Sdougm 				export->ex_index = strdup(value);
997549ec3ffSdougm 				if (export->ex_index == NULL) {
998549ec3ffSdougm 					(void) printf(dgettext(TEXT_DOMAIN,
999549ec3ffSdougm 					    "NFS: out of memory setting "
1000549ec3ffSdougm 					    "index property\n"));
1001549ec3ffSdougm 					break;
1002549ec3ffSdougm 				}
1003549ec3ffSdougm 				export->ex_flags |= EX_INDEX;
1004549ec3ffSdougm 			}
1005549ec3ffSdougm 			break;
1006549ec3ffSdougm 		case OPT_LOG:
1007549ec3ffSdougm 			if (value == NULL)
1008549ec3ffSdougm 				value = strdup("global");
1009549ec3ffSdougm 			if (value != NULL)
1010a3351425Sdougm 				configlog(export,
1011a3351425Sdougm 				    strlen(value) ? value : "global");
1012549ec3ffSdougm 			break;
1013b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_CHARSET_MAP:
1014b89a8333Snatalie li - Sun Microsystems - Irvine United States 			/*
1015b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * Set EX_CHARMAP when there is at least one
1016b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * charmap conversion property. This will get
1017b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 * checked by the nfs server when it needs to.
1018b89a8333Snatalie li - Sun Microsystems - Irvine United States 			 */
1019b89a8333Snatalie li - Sun Microsystems - Irvine United States 			export->ex_flags |= EX_CHARMAP;
1020b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
10216764e83bSVallish Vaidyeshwara 		case OPT_NOACLFAB:
10226764e83bSVallish Vaidyeshwara 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
10236764e83bSVallish Vaidyeshwara 			    strcmp(value, "1") == 0))
10246764e83bSVallish Vaidyeshwara 				export->ex_flags |= EX_NOACLFAB;
10256764e83bSVallish Vaidyeshwara 			else
10266764e83bSVallish Vaidyeshwara 				export->ex_flags &= ~EX_NOACLFAB;
10276764e83bSVallish Vaidyeshwara 			break;
1028549ec3ffSdougm 		default:
1029549ec3ffSdougm 			/* have a syntactic error */
1030549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1031549ec3ffSdougm 			    "NFS: unrecognized option %s=%s\n"),
1032fe1c642dSBill Krier 			    name != NULL ? name : "",
1033fe1c642dSBill Krier 			    value != NULL ? value : "");
1034549ec3ffSdougm 			break;
1035549ec3ffSdougm 		}
1036549ec3ffSdougm 		if (name != NULL)
1037549ec3ffSdougm 			sa_free_attr_string(name);
1038549ec3ffSdougm 		if (value != NULL)
1039549ec3ffSdougm 			sa_free_attr_string(value);
1040549ec3ffSdougm 	}
1041549ec3ffSdougm 	return (ret);
1042549ec3ffSdougm }
1043549ec3ffSdougm 
1044549ec3ffSdougm /*
1045549ec3ffSdougm  * cleanup_export(export)
1046549ec3ffSdougm  *
1047549ec3ffSdougm  * Cleanup the allocated areas so we don't leak memory
1048549ec3ffSdougm  */
1049549ec3ffSdougm 
1050549ec3ffSdougm static void
1051549ec3ffSdougm cleanup_export(struct exportdata *export)
1052549ec3ffSdougm {
1053549ec3ffSdougm 	int i;
1054549ec3ffSdougm 
1055549ec3ffSdougm 	free(export->ex_index);
10563253d86fSMarcel Telka 
10573253d86fSMarcel Telka 	for (i = 0; i < export->ex_seccnt; i++) {
10583253d86fSMarcel Telka 		struct secinfo *s = &export->ex_secinfo[i];
10593253d86fSMarcel Telka 
10603253d86fSMarcel Telka 		while (s->s_rootcnt > 0)
10613253d86fSMarcel Telka 			free(s->s_rootnames[--s->s_rootcnt]);
10623253d86fSMarcel Telka 
10633253d86fSMarcel Telka 		free(s->s_rootnames);
1064549ec3ffSdougm 	}
10653253d86fSMarcel Telka 	free(export->ex_secinfo);
1066549ec3ffSdougm }
1067549ec3ffSdougm 
1068549ec3ffSdougm /*
1069549ec3ffSdougm  * Given a seconfig entry and a colon-separated
1070549ec3ffSdougm  * list of names, allocate an array big enough
1071549ec3ffSdougm  * to hold the root list, then convert each name to
1072549ec3ffSdougm  * a principal name according to the security
1073549ec3ffSdougm  * info and assign it to an array element.
1074549ec3ffSdougm  * Return the array and its size.
1075549ec3ffSdougm  */
1076549ec3ffSdougm static caddr_t *
1077549ec3ffSdougm get_rootnames(seconfig_t *sec, char *list, int *count)
1078549ec3ffSdougm {
1079549ec3ffSdougm 	caddr_t *a;
1080549ec3ffSdougm 	int c, i;
1081549ec3ffSdougm 	char *host, *p;
1082549ec3ffSdougm 
1083549ec3ffSdougm 	/*
1084549ec3ffSdougm 	 * Count the number of strings in the list.
1085549ec3ffSdougm 	 * This is the number of colon separators + 1.
1086549ec3ffSdougm 	 */
1087549ec3ffSdougm 	c = 1;
1088549ec3ffSdougm 	for (p = list; *p; p++)
1089549ec3ffSdougm 		if (*p == ':')
1090549ec3ffSdougm 			c++;
1091549ec3ffSdougm 	*count = c;
1092549ec3ffSdougm 
1093549ec3ffSdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
1094549ec3ffSdougm 	if (a == NULL) {
1095549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1096549ec3ffSdougm 		    "get_rootnames: no memory\n"));
1097*07b64d17SMarcel Telka 		*count = 0;
1098549ec3ffSdougm 	} else {
1099549ec3ffSdougm 		for (i = 0; i < c; i++) {
1100549ec3ffSdougm 			host = strtok(list, ":");
1101549ec3ffSdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
11023253d86fSMarcel Telka 				while (i > 0)
11033253d86fSMarcel Telka 					free(a[--i]);
1104549ec3ffSdougm 				free(a);
1105549ec3ffSdougm 				a = NULL;
1106*07b64d17SMarcel Telka 				*count = 0;
1107549ec3ffSdougm 				break;
1108549ec3ffSdougm 			}
1109549ec3ffSdougm 			list = NULL;
1110549ec3ffSdougm 		}
1111549ec3ffSdougm 	}
1112549ec3ffSdougm 
1113549ec3ffSdougm 	return (a);
1114549ec3ffSdougm }
1115549ec3ffSdougm 
1116549ec3ffSdougm /*
1117549ec3ffSdougm  * fill_security_from_secopts(sp, secopts)
1118549ec3ffSdougm  *
1119549ec3ffSdougm  * Fill the secinfo structure from the secopts optionset.
1120549ec3ffSdougm  */
1121549ec3ffSdougm 
1122549ec3ffSdougm static int
1123549ec3ffSdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
1124549ec3ffSdougm {
1125549ec3ffSdougm 	sa_property_t prop;
1126549ec3ffSdougm 	char *type;
1127549ec3ffSdougm 	int longform;
1128549ec3ffSdougm 	int err = SC_NOERROR;
1129b89a8333Snatalie li - Sun Microsystems - Irvine United States 	uint32_t val;
1130549ec3ffSdougm 
1131549ec3ffSdougm 	type = sa_get_security_attr(secopts, "sectype");
1132549ec3ffSdougm 	if (type != NULL) {
1133549ec3ffSdougm 		/* named security type needs secinfo to be filled in */
1134549ec3ffSdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
1135549ec3ffSdougm 		sa_free_attr_string(type);
1136549ec3ffSdougm 		if (err != SC_NOERROR)
1137549ec3ffSdougm 			return (err);
1138549ec3ffSdougm 	} else {
1139549ec3ffSdougm 		/* default case */
1140549ec3ffSdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
1141549ec3ffSdougm 		if (err != SC_NOERROR)
1142549ec3ffSdougm 			return (err);
1143549ec3ffSdougm 	}
1144549ec3ffSdougm 
1145549ec3ffSdougm 	err = SA_OK;
1146549ec3ffSdougm 	for (prop = sa_get_property(secopts, NULL);
1147549ec3ffSdougm 	    prop != NULL && err == SA_OK;
1148549ec3ffSdougm 	    prop = sa_get_next_property(prop)) {
1149549ec3ffSdougm 		char *name;
1150549ec3ffSdougm 		char *value;
1151549ec3ffSdougm 
1152549ec3ffSdougm 		name = sa_get_property_attr(prop, "type");
1153549ec3ffSdougm 		value = sa_get_property_attr(prop, "value");
1154549ec3ffSdougm 
1155549ec3ffSdougm 		longform = value != NULL && strcmp(value, "*") != 0;
1156549ec3ffSdougm 
1157549ec3ffSdougm 		switch (findopt(name)) {
1158549ec3ffSdougm 		case OPT_RO:
1159549ec3ffSdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
1160549ec3ffSdougm 			break;
1161549ec3ffSdougm 		case OPT_RW:
1162549ec3ffSdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
1163549ec3ffSdougm 			break;
1164549ec3ffSdougm 		case OPT_ROOT:
1165549ec3ffSdougm 			sp->s_flags |= M_ROOT;
1166549ec3ffSdougm 			/*
1167549ec3ffSdougm 			 * if we are using AUTH_UNIX, handle like other things
1168549ec3ffSdougm 			 * such as RO/RW
1169549ec3ffSdougm 			 */
1170549ec3ffSdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
11716020a1b7SMarcel Telka 				break;
1172549ec3ffSdougm 			/* not AUTH_UNIX */
1173549ec3ffSdougm 			if (value != NULL) {
1174a3351425Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
1175a3351425Sdougm 				    value, &sp->s_rootcnt);
1176549ec3ffSdougm 				if (sp->s_rootnames == NULL) {
1177549ec3ffSdougm 					err = SA_BAD_VALUE;
1178a3351425Sdougm 					(void) fprintf(stderr,
1179a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
1180549ec3ffSdougm 					    "Bad root list\n"));
1181549ec3ffSdougm 				}
1182549ec3ffSdougm 			}
1183549ec3ffSdougm 			break;
1184b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_NONE:
1185b89a8333Snatalie li - Sun Microsystems - Irvine United States 			sp->s_flags |= M_NONE;
1186b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
1187549ec3ffSdougm 		case OPT_WINDOW:
1188549ec3ffSdougm 			if (value != NULL) {
1189549ec3ffSdougm 				sp->s_window = atoi(value);
1190a3351425Sdougm 				/* just in case */
1191549ec3ffSdougm 				if (sp->s_window < 0)
1192a3351425Sdougm 					sp->s_window = DEF_WIN;
1193549ec3ffSdougm 			}
1194549ec3ffSdougm 			break;
1195b89a8333Snatalie li - Sun Microsystems - Irvine United States 		case OPT_ROOT_MAPPING:
1196b89a8333Snatalie li - Sun Microsystems - Irvine United States 			if (value != NULL && is_a_number(value)) {
1197b89a8333Snatalie li - Sun Microsystems - Irvine United States 				val = strtoul(value, NULL, 0);
1198b89a8333Snatalie li - Sun Microsystems - Irvine United States 			} else {
1199b89a8333Snatalie li - Sun Microsystems - Irvine United States 				struct passwd *pw;
1200b89a8333Snatalie li - Sun Microsystems - Irvine United States 				pw = getpwnam(value != NULL ? value : "nobody");
1201b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (pw != NULL) {
1202b89a8333Snatalie li - Sun Microsystems - Irvine United States 					val = pw->pw_uid;
1203b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else {
1204b89a8333Snatalie li - Sun Microsystems - Irvine United States 					val = UID_NOBODY;
1205b89a8333Snatalie li - Sun Microsystems - Irvine United States 				}
1206b89a8333Snatalie li - Sun Microsystems - Irvine United States 				endpwent();
1207b89a8333Snatalie li - Sun Microsystems - Irvine United States 			}
1208b89a8333Snatalie li - Sun Microsystems - Irvine United States 			sp->s_rootid = val;
1209b89a8333Snatalie li - Sun Microsystems - Irvine United States 			break;
12105cb0d679SMarcel Telka 		case OPT_UIDMAP:
12115cb0d679SMarcel Telka 		case OPT_GIDMAP:
12125cb0d679SMarcel Telka 			sp->s_flags |= M_MAP;
12135cb0d679SMarcel Telka 			break;
1214549ec3ffSdougm 		default:
1215549ec3ffSdougm 			break;
1216549ec3ffSdougm 		}
1217549ec3ffSdougm 		if (name != NULL)
1218549ec3ffSdougm 			sa_free_attr_string(name);
1219549ec3ffSdougm 		if (value != NULL)
1220549ec3ffSdougm 			sa_free_attr_string(value);
1221549ec3ffSdougm 	}
1222549ec3ffSdougm 	/* if rw/ro options not set, use default of RW */
1223549ec3ffSdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
1224549ec3ffSdougm 		sp->s_flags |= M_RW;
1225549ec3ffSdougm 	return (err);
1226549ec3ffSdougm }
1227549ec3ffSdougm 
1228549ec3ffSdougm /*
1229549ec3ffSdougm  * This is for testing only
1230549ec3ffSdougm  * It displays the export structure that
1231549ec3ffSdougm  * goes into the kernel.
1232549ec3ffSdougm  */
1233549ec3ffSdougm static void
1234549ec3ffSdougm printarg(char *path, struct exportdata *ep)
1235549ec3ffSdougm {
1236549ec3ffSdougm 	int i, j;
1237549ec3ffSdougm 	struct secinfo *sp;
1238549ec3ffSdougm 
1239549ec3ffSdougm 	if (debug == 0)
1240549ec3ffSdougm 		return;
1241549ec3ffSdougm 
1242549ec3ffSdougm 	(void) printf("%s:\n", path);
1243549ec3ffSdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
1244549ec3ffSdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
1245549ec3ffSdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
1246549ec3ffSdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
1247549ec3ffSdougm 	if (ep->ex_flags & EX_NOSUID)
1248549ec3ffSdougm 		(void) printf("NOSUID ");
1249549ec3ffSdougm 	if (ep->ex_flags & EX_ACLOK)
1250549ec3ffSdougm 		(void) printf("ACLOK ");
1251549ec3ffSdougm 	if (ep->ex_flags & EX_PUBLIC)
1252549ec3ffSdougm 		(void) printf("PUBLIC ");
1253549ec3ffSdougm 	if (ep->ex_flags & EX_NOSUB)
1254549ec3ffSdougm 		(void) printf("NOSUB ");
1255549ec3ffSdougm 	if (ep->ex_flags & EX_LOG)
1256549ec3ffSdougm 		(void) printf("LOG ");
1257b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (ep->ex_flags & EX_CHARMAP)
1258b89a8333Snatalie li - Sun Microsystems - Irvine United States 		(void) printf("CHARMAP ");
1259549ec3ffSdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
1260549ec3ffSdougm 		(void) printf("LOG_ALLOPS ");
1261549ec3ffSdougm 	if (ep->ex_flags == 0)
1262549ec3ffSdougm 		(void) printf("(none)");
1263549ec3ffSdougm 	(void) 	printf("\n");
1264549ec3ffSdougm 	if (ep->ex_flags & EX_LOG) {
1265549ec3ffSdougm 		(void) printf("\tex_log_buffer = %s\n",
1266549ec3ffSdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
1267549ec3ffSdougm 		(void) printf("\tex_tag = %s\n",
1268549ec3ffSdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
1269549ec3ffSdougm 	}
1270549ec3ffSdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
1271549ec3ffSdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
1272549ec3ffSdougm 	(void) printf("\n");
1273549ec3ffSdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
1274549ec3ffSdougm 		sp = &ep->ex_secinfo[i];
1275549ec3ffSdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
1276549ec3ffSdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
1277549ec3ffSdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
1278549ec3ffSdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
1279549ec3ffSdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
1280549ec3ffSdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
1281549ec3ffSdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
1282b89a8333Snatalie li - Sun Microsystems - Irvine United States 		if (sp->s_flags & M_NONE) (void) printf("M_NONE ");
12835cb0d679SMarcel Telka 		if (sp->s_flags & M_MAP) (void) printf("M_MAP ");
1284549ec3ffSdougm 		if (sp->s_flags == 0) (void) printf("(none)");
1285549ec3ffSdougm 		(void) printf("\n");
1286549ec3ffSdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
1287b89a8333Snatalie li - Sun Microsystems - Irvine United States 		(void) printf("\t\ts_rootid = %d\n", sp->s_rootid);
1288549ec3ffSdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
1289549ec3ffSdougm 		(void) fflush(stdout);
1290549ec3ffSdougm 		for (j = 0; j < sp->s_rootcnt; j++)
1291549ec3ffSdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
1292549ec3ffSdougm 			    sp->s_rootnames[j] : "<null>");
1293549ec3ffSdougm 		(void) printf("\n\n");
1294549ec3ffSdougm 	}
1295549ec3ffSdougm }
1296549ec3ffSdougm 
1297549ec3ffSdougm /*
1298549ec3ffSdougm  * count_security(opts)
1299549ec3ffSdougm  *
1300549ec3ffSdougm  * Count the number of security types (flavors). The optionset has
1301549ec3ffSdougm  * been populated with the security flavors as a holding mechanism.
1302549ec3ffSdougm  * We later use this number to allocate data structures.
1303549ec3ffSdougm  */
1304549ec3ffSdougm 
1305549ec3ffSdougm static int
1306549ec3ffSdougm count_security(sa_optionset_t opts)
1307549ec3ffSdougm {
1308549ec3ffSdougm 	int count = 0;
1309549ec3ffSdougm 	sa_property_t prop;
1310549ec3ffSdougm 	if (opts != NULL) {
1311549ec3ffSdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
1312549ec3ffSdougm 		    prop = sa_get_next_property(prop)) {
1313549ec3ffSdougm 			count++;
1314549ec3ffSdougm 		}
1315549ec3ffSdougm 	}
1316549ec3ffSdougm 	return (count);
1317549ec3ffSdougm }
1318549ec3ffSdougm 
1319549ec3ffSdougm /*
1320549ec3ffSdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1321549ec3ffSdougm  *
1322549ec3ffSdougm  * provides a mechanism to format NFS properties into legacy output
1323549ec3ffSdougm  * format. If the buffer would overflow, it is reallocated and grown
1324549ec3ffSdougm  * as appropriate. Special cases of converting internal form of values
1325549ec3ffSdougm  * to those used by "share" are done. this function does one property
1326549ec3ffSdougm  * at a time.
1327549ec3ffSdougm  */
1328549ec3ffSdougm 
1329aed5d200Sdougm static int
1330549ec3ffSdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1331549ec3ffSdougm 			sa_property_t prop, int sep)
1332549ec3ffSdougm {
1333549ec3ffSdougm 	char *name;
1334549ec3ffSdougm 	char *value;
1335549ec3ffSdougm 	int curlen;
1336549ec3ffSdougm 	char *buff = *rbuff;
1337549ec3ffSdougm 	size_t buffsize = *rbuffsize;
1338aed5d200Sdougm 	int printed = B_FALSE;
1339549ec3ffSdougm 
1340549ec3ffSdougm 	name = sa_get_property_attr(prop, "type");
1341549ec3ffSdougm 	value = sa_get_property_attr(prop, "value");
1342549ec3ffSdougm 	if (buff != NULL)
1343549ec3ffSdougm 		curlen = strlen(buff);
1344549ec3ffSdougm 	else
1345549ec3ffSdougm 		curlen = 0;
1346549ec3ffSdougm 	if (name != NULL) {
1347549ec3ffSdougm 		int len;
1348549ec3ffSdougm 		len = strlen(name) + sep;
1349549ec3ffSdougm 
1350549ec3ffSdougm 		/*
1351549ec3ffSdougm 		 * A future RFE would be to replace this with more
1352549ec3ffSdougm 		 * generic code and to possibly handle more types.
1353549ec3ffSdougm 		 */
1354549ec3ffSdougm 		switch (gettype(name)) {
1355549ec3ffSdougm 		case OPT_TYPE_BOOLEAN:
1356aed5d200Sdougm 			/*
1357aed5d200Sdougm 			 * For NFS, boolean value of FALSE means it
1358aed5d200Sdougm 			 * doesn't show up in the option list at all.
1359aed5d200Sdougm 			 */
1360a3351425Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
1361aed5d200Sdougm 				goto skip;
1362aed5d200Sdougm 			if (value != NULL) {
1363549ec3ffSdougm 				sa_free_attr_string(value);
1364549ec3ffSdougm 				value = NULL;
1365aed5d200Sdougm 			}
1366549ec3ffSdougm 			break;
1367549ec3ffSdougm 		case OPT_TYPE_ACCLIST:
1368549ec3ffSdougm 			if (value != NULL && strcmp(value, "*") == 0) {
1369549ec3ffSdougm 				sa_free_attr_string(value);
1370549ec3ffSdougm 				value = NULL;
1371549ec3ffSdougm 			} else {
1372549ec3ffSdougm 				if (value != NULL)
1373549ec3ffSdougm 					len += 1 + strlen(value);
1374549ec3ffSdougm 			}
1375549ec3ffSdougm 			break;
1376549ec3ffSdougm 		case OPT_TYPE_LOGTAG:
1377549ec3ffSdougm 			if (value != NULL && strlen(value) == 0) {
1378549ec3ffSdougm 				sa_free_attr_string(value);
1379549ec3ffSdougm 				value = NULL;
1380549ec3ffSdougm 			} else {
1381549ec3ffSdougm 				if (value != NULL)
1382549ec3ffSdougm 					len += 1 + strlen(value);
1383549ec3ffSdougm 			}
1384549ec3ffSdougm 			break;
1385549ec3ffSdougm 		default:
1386549ec3ffSdougm 			if (value != NULL)
1387549ec3ffSdougm 				len += 1 + strlen(value);
1388549ec3ffSdougm 			break;
1389549ec3ffSdougm 		}
1390549ec3ffSdougm 		while (buffsize <= (curlen + len)) {
1391549ec3ffSdougm 			/* need more room */
1392549ec3ffSdougm 			buffsize += incr;
1393549ec3ffSdougm 			buff = realloc(buff, buffsize);
1394549ec3ffSdougm 			if (buff == NULL) {
1395549ec3ffSdougm 				/* realloc failed so free everything */
1396549ec3ffSdougm 				if (*rbuff != NULL)
1397549ec3ffSdougm 					free(*rbuff);
1398549ec3ffSdougm 			}
1399549ec3ffSdougm 			*rbuff = buff;
1400549ec3ffSdougm 			*rbuffsize = buffsize;
1401549ec3ffSdougm 			if (buff == NULL)
1402aed5d200Sdougm 				goto skip;
1403aed5d200Sdougm 
1404aed5d200Sdougm 		}
1405aed5d200Sdougm 
1406aed5d200Sdougm 		if (buff == NULL)
1407aed5d200Sdougm 			goto skip;
1408aed5d200Sdougm 
1409a3351425Sdougm 		if (value == NULL) {
1410549ec3ffSdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
14115cb0d679SMarcel Telka 			    "%s%s", sep ? "," : "", name);
1412a3351425Sdougm 		} else {
1413549ec3ffSdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1414549ec3ffSdougm 			    "%s%s=%s", sep ? "," : "",
1415549ec3ffSdougm 			    name, value != NULL ? value : "");
1416549ec3ffSdougm 		}
1417aed5d200Sdougm 		printed = B_TRUE;
1418a3351425Sdougm 	}
1419aed5d200Sdougm skip:
1420549ec3ffSdougm 	if (name != NULL)
1421549ec3ffSdougm 		sa_free_attr_string(name);
1422549ec3ffSdougm 	if (value != NULL)
1423549ec3ffSdougm 		sa_free_attr_string(value);
1424aed5d200Sdougm 	return (printed);
1425549ec3ffSdougm }
1426549ec3ffSdougm 
1427549ec3ffSdougm /*
1428549ec3ffSdougm  * nfs_format_options(group, hier)
1429549ec3ffSdougm  *
1430549ec3ffSdougm  * format all the options on the group into an old-style option
1431549ec3ffSdougm  * string. If hier is non-zero, walk up the tree to get inherited
1432549ec3ffSdougm  * options.
1433549ec3ffSdougm  */
1434549ec3ffSdougm 
1435549ec3ffSdougm static char *
1436549ec3ffSdougm nfs_format_options(sa_group_t group, int hier)
1437549ec3ffSdougm {
1438549ec3ffSdougm 	sa_optionset_t options = NULL;
1439a3351425Sdougm 	sa_optionset_t secoptions = NULL;
1440549ec3ffSdougm 	sa_property_t prop, secprop;
1441a3351425Sdougm 	sa_security_t security = NULL;
1442549ec3ffSdougm 	char *buff;
1443549ec3ffSdougm 	size_t buffsize;
1444a3351425Sdougm 	char *sectype = NULL;
1445a3351425Sdougm 	int sep = 0;
1446a3351425Sdougm 
1447a3351425Sdougm 
1448a3351425Sdougm 	buff = malloc(OPT_CHUNK);
1449a3351425Sdougm 	if (buff == NULL) {
1450a3351425Sdougm 		return (NULL);
1451a3351425Sdougm 	}
1452a3351425Sdougm 
1453a3351425Sdougm 	buff[0] = '\0';
1454a3351425Sdougm 	buffsize = OPT_CHUNK;
1455a3351425Sdougm 
1456a3351425Sdougm 	/*
1457a3351425Sdougm 	 * We may have a an optionset relative to this item. format
1458a3351425Sdougm 	 * these if we find them and then add any security definitions.
1459a3351425Sdougm 	 */
1460549ec3ffSdougm 
1461549ec3ffSdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
1462549ec3ffSdougm 
1463549ec3ffSdougm 	/*
1464549ec3ffSdougm 	 * do the default set first but skip any option that is also
1465549ec3ffSdougm 	 * in the protocol specific optionset.
1466549ec3ffSdougm 	 */
1467549ec3ffSdougm 	if (options != NULL) {
1468a3351425Sdougm 		for (prop = sa_get_property(options, NULL);
1469a3351425Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
1470549ec3ffSdougm 			/*
1471a3351425Sdougm 			 * use this one since we skipped any
1472a3351425Sdougm 			 * of these that were also in
1473a3351425Sdougm 			 * optdefault
1474549ec3ffSdougm 			 */
1475aed5d200Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1476aed5d200Sdougm 			    prop, sep))
1477aed5d200Sdougm 				sep = 1;
1478549ec3ffSdougm 			if (buff == NULL) {
1479549ec3ffSdougm 				/*
1480a3351425Sdougm 				 * buff could become NULL if there
1481a3351425Sdougm 				 * isn't enough memory for
1482a3351425Sdougm 				 * nfs_sprint_option to realloc()
1483a3351425Sdougm 				 * as necessary. We can't really
1484a3351425Sdougm 				 * do anything about it at this
1485a3351425Sdougm 				 * point so we return NULL.  The
1486a3351425Sdougm 				 * caller should handle the
1487a3351425Sdougm 				 * failure.
1488549ec3ffSdougm 				 */
1489a3351425Sdougm 				if (options != NULL)
1490a3351425Sdougm 					sa_free_derived_optionset(
1491a3351425Sdougm 					    options);
1492549ec3ffSdougm 				return (buff);
1493549ec3ffSdougm 			}
1494549ec3ffSdougm 		}
1495549ec3ffSdougm 	}
1496549ec3ffSdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1497549ec3ffSdougm 	    "nfs", hier);
1498549ec3ffSdougm 	if (secoptions != NULL) {
1499549ec3ffSdougm 		for (secprop = sa_get_property(secoptions, NULL);
1500a3351425Sdougm 		    secprop != NULL;
1501a3351425Sdougm 		    secprop = sa_get_next_property(secprop)) {
1502549ec3ffSdougm 			sectype = sa_get_property_attr(secprop, "type");
1503a3351425Sdougm 			security =
1504a3351425Sdougm 			    (sa_security_t)sa_get_derived_security(
1505a3351425Sdougm 			    group, sectype, "nfs", hier);
1506549ec3ffSdougm 			if (security != NULL) {
1507549ec3ffSdougm 				if (sectype != NULL) {
1508a3351425Sdougm 					prop = sa_create_property(
1509a3351425Sdougm 					    "sec", sectype);
1510aed5d200Sdougm 					if (prop == NULL)
1511aed5d200Sdougm 						goto err;
1512aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1513aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1514549ec3ffSdougm 						sep = 1;
1515aed5d200Sdougm 					(void) sa_remove_property(prop);
1516aed5d200Sdougm 					if (buff == NULL)
1517aed5d200Sdougm 						goto err;
1518549ec3ffSdougm 				}
1519a3351425Sdougm 				for (prop = sa_get_property(security,
1520a3351425Sdougm 				    NULL); prop != NULL;
1521549ec3ffSdougm 				    prop = sa_get_next_property(prop)) {
1522aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1523aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1524aed5d200Sdougm 						sep = 1;
1525a3351425Sdougm 					if (buff == NULL)
1526a3351425Sdougm 						goto err;
1527a3351425Sdougm 				}
1528a3351425Sdougm 				sa_free_derived_optionset(security);
1529a3351425Sdougm 			}
1530a3351425Sdougm 			if (sectype != NULL)
1531a3351425Sdougm 				sa_free_attr_string(sectype);
1532a3351425Sdougm 		}
1533a3351425Sdougm 		sa_free_derived_optionset(secoptions);
1534a3351425Sdougm 	}
1535549ec3ffSdougm 
1536a3351425Sdougm 	if (options != NULL)
1537a3351425Sdougm 		sa_free_derived_optionset(options);
1538a3351425Sdougm 	return (buff);
1539a3351425Sdougm 
1540a3351425Sdougm err:
1541a3351425Sdougm 	/*
1542a3351425Sdougm 	 * If we couldn't allocate memory for option printing, we need
1543a3351425Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
1544a3351425Sdougm 	 */
1545a3351425Sdougm 	if (secoptions != NULL)
1546549ec3ffSdougm 		sa_free_derived_optionset(secoptions);
1547549ec3ffSdougm 	if (security != NULL)
1548549ec3ffSdougm 		sa_free_derived_optionset(security);
1549549ec3ffSdougm 	if (sectype != NULL)
1550549ec3ffSdougm 		sa_free_attr_string(sectype);
1551549ec3ffSdougm 	if (options != NULL)
1552549ec3ffSdougm 		sa_free_derived_optionset(options);
1553dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	return (buff);
1554549ec3ffSdougm }
1555a3351425Sdougm 
1556549ec3ffSdougm /*
1557549ec3ffSdougm  * Append an entry to the nfslogtab file
1558549ec3ffSdougm  */
1559549ec3ffSdougm static int
1560549ec3ffSdougm nfslogtab_add(dir, buffer, tag)
1561549ec3ffSdougm 	char *dir, *buffer, *tag;
1562549ec3ffSdougm {
1563549ec3ffSdougm 	FILE *f;
1564549ec3ffSdougm 	struct logtab_ent lep;
1565549ec3ffSdougm 	int error = 0;
1566549ec3ffSdougm 
1567549ec3ffSdougm 	/*
1568549ec3ffSdougm 	 * Open the file for update and create it if necessary.
1569549ec3ffSdougm 	 * This may leave the I/O offset at the end of the file,
1570549ec3ffSdougm 	 * so rewind back to the beginning of the file.
1571549ec3ffSdougm 	 */
1572549ec3ffSdougm 	f = fopen(NFSLOGTAB, "a+");
1573549ec3ffSdougm 	if (f == NULL) {
1574549ec3ffSdougm 		error = errno;
1575549ec3ffSdougm 		goto out;
1576549ec3ffSdougm 	}
1577549ec3ffSdougm 	rewind(f);
1578549ec3ffSdougm 
1579549ec3ffSdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1580549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1581549ec3ffSdougm 		    "share complete, however failed to lock %s "
1582549ec3ffSdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
1583549ec3ffSdougm 		error = -1;
1584549ec3ffSdougm 		goto out;
1585549ec3ffSdougm 	}
1586549ec3ffSdougm 
1587549ec3ffSdougm 	if (logtab_deactivate_after_boot(f) == -1) {
1588549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1589549ec3ffSdougm 		    "share complete, however could not deactivate "
1590549ec3ffSdougm 		    "entries in %s\n"), NFSLOGTAB);
1591549ec3ffSdougm 		error = -1;
1592549ec3ffSdougm 		goto out;
1593549ec3ffSdougm 	}
1594549ec3ffSdougm 
1595549ec3ffSdougm 	/*
1596549ec3ffSdougm 	 * Remove entries matching buffer and sharepoint since we're
1597549ec3ffSdougm 	 * going to replace it with perhaps an entry with a new tag.
1598549ec3ffSdougm 	 */
1599549ec3ffSdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
1600549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1601549ec3ffSdougm 		    "share complete, however could not remove matching "
1602549ec3ffSdougm 		    "entries in %s\n"), NFSLOGTAB);
1603549ec3ffSdougm 		error = -1;
1604549ec3ffSdougm 		goto out;
1605549ec3ffSdougm 	}
1606549ec3ffSdougm 
1607549ec3ffSdougm 	/*
1608549ec3ffSdougm 	 * Deactivate all active entries matching this sharepoint
1609549ec3ffSdougm 	 */
1610549ec3ffSdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
1611549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1612549ec3ffSdougm 		    "share complete, however could not deactivate matching "
1613549ec3ffSdougm 		    "entries in %s\n"), NFSLOGTAB);
1614549ec3ffSdougm 		error = -1;
1615549ec3ffSdougm 		goto out;
1616549ec3ffSdougm 	}
1617549ec3ffSdougm 
1618549ec3ffSdougm 	lep.le_buffer = buffer;
1619549ec3ffSdougm 	lep.le_path = dir;
1620549ec3ffSdougm 	lep.le_tag = tag;
1621549ec3ffSdougm 	lep.le_state = LES_ACTIVE;
1622549ec3ffSdougm 
1623549ec3ffSdougm 	/*
1624549ec3ffSdougm 	 * Add new sharepoint / buffer location to nfslogtab
1625549ec3ffSdougm 	 */
1626549ec3ffSdougm 	if (logtab_putent(f, &lep) < 0) {
1627549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1628549ec3ffSdougm 		    "share complete, however could not add %s to %s\n"),
1629549ec3ffSdougm 		    dir, NFSLOGTAB);
1630549ec3ffSdougm 		error = -1;
1631549ec3ffSdougm 	}
1632549ec3ffSdougm 
1633549ec3ffSdougm out:
1634549ec3ffSdougm 	if (f != NULL)
1635549ec3ffSdougm 		(void) fclose(f);
1636549ec3ffSdougm 	return (error);
1637549ec3ffSdougm }
1638549ec3ffSdougm 
1639549ec3ffSdougm /*
1640549ec3ffSdougm  * Deactivate an entry from the nfslogtab file
1641549ec3ffSdougm  */
1642549ec3ffSdougm static int
1643549ec3ffSdougm nfslogtab_deactivate(path)
1644549ec3ffSdougm 	char *path;
1645549ec3ffSdougm {
1646549ec3ffSdougm 	FILE *f;
1647549ec3ffSdougm 	int error = 0;
1648549ec3ffSdougm 
1649549ec3ffSdougm 	f = fopen(NFSLOGTAB, "r+");
1650549ec3ffSdougm 	if (f == NULL) {
1651549ec3ffSdougm 		error = errno;
1652549ec3ffSdougm 		goto out;
1653549ec3ffSdougm 	}
1654549ec3ffSdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1655549ec3ffSdougm 		error = errno;
1656549ec3ffSdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
1657549ec3ffSdougm 		    "share complete, however could not lock %s for "
1658549ec3ffSdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
1659549ec3ffSdougm 		goto out;
1660549ec3ffSdougm 	}
1661549ec3ffSdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
1662549ec3ffSdougm 		error = -1;
1663549ec3ffSdougm 		(void) fprintf(stderr,
1664549ec3ffSdougm 		    dgettext(TEXT_DOMAIN,
1665549ec3ffSdougm 		    "share complete, however could not "
1666549ec3ffSdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
1667549ec3ffSdougm 		goto out;
1668549ec3ffSdougm 	}
1669549ec3ffSdougm 
1670549ec3ffSdougm out:	if (f != NULL)
1671549ec3ffSdougm 		(void) fclose(f);
1672549ec3ffSdougm 
1673549ec3ffSdougm 	return (error);
1674549ec3ffSdougm }
1675549ec3ffSdougm 
1676549ec3ffSdougm /*
1677546405c3Sdougm  * check_public(group, skipshare)
1678546405c3Sdougm  *
1679546405c3Sdougm  * Check the group for any shares that have the public property
1680546405c3Sdougm  * enabled. We skip "skipshare" since that is the one we are
1681546405c3Sdougm  * working with. This is a separate function to make handling
1682546405c3Sdougm  * subgroups simpler. Returns true if there is a share with public.
1683546405c3Sdougm  */
1684546405c3Sdougm static int
1685546405c3Sdougm check_public(sa_group_t group, sa_share_t skipshare)
1686546405c3Sdougm {
1687546405c3Sdougm 	int exists = B_FALSE;
1688546405c3Sdougm 	sa_share_t share;
1689546405c3Sdougm 	sa_optionset_t opt;
1690546405c3Sdougm 	sa_property_t prop;
1691546405c3Sdougm 	char *shared;
1692546405c3Sdougm 
1693546405c3Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
1694546405c3Sdougm 	    share = sa_get_next_share(share)) {
1695546405c3Sdougm 		if (share == skipshare)
1696546405c3Sdougm 			continue;
1697546405c3Sdougm 
1698546405c3Sdougm 		opt = sa_get_optionset(share, "nfs");
1699546405c3Sdougm 		if (opt == NULL)
1700546405c3Sdougm 			continue;
1701546405c3Sdougm 		prop = sa_get_property(opt, "public");
1702546405c3Sdougm 		if (prop == NULL)
1703546405c3Sdougm 			continue;
1704546405c3Sdougm 		shared = sa_get_share_attr(share, "shared");
1705546405c3Sdougm 		if (shared != NULL) {
1706546405c3Sdougm 			exists = strcmp(shared, "true") == 0;
1707546405c3Sdougm 			sa_free_attr_string(shared);
1708546405c3Sdougm 			if (exists == B_TRUE)
1709546405c3Sdougm 				break;
1710546405c3Sdougm 		}
1711546405c3Sdougm 	}
1712546405c3Sdougm 
1713546405c3Sdougm 	return (exists);
1714546405c3Sdougm }
1715546405c3Sdougm 
1716546405c3Sdougm /*
17175cb0d679SMarcel Telka  * public_exists(handle, skipshare)
1718549ec3ffSdougm  *
1719549ec3ffSdougm  * check to see if public option is set on any other share than the
1720546405c3Sdougm  * one specified. Need to check zfs sub-groups as well as the top
1721546405c3Sdougm  * level groups.
1722549ec3ffSdougm  */
1723549ec3ffSdougm static int
1724687915e9Sdougm public_exists(sa_handle_t handle, sa_share_t skipshare)
1725549ec3ffSdougm {
1726687915e9Sdougm 	sa_group_t group = NULL;
1727549ec3ffSdougm 
17283b61b335Sdougm 	/*
17293b61b335Sdougm 	 * If we don't have a handle, we can only do syntax check. We
17303b61b335Sdougm 	 * can't check against other shares so we assume OK and will
17313b61b335Sdougm 	 * catch the problem only when we actually try to apply it.
17323b61b335Sdougm 	 */
1733687915e9Sdougm 	if (handle == NULL)
17343b61b335Sdougm 		return (SA_OK);
1735687915e9Sdougm 
1736687915e9Sdougm 	if (skipshare != NULL) {
1737549ec3ffSdougm 		group = sa_get_parent_group(skipshare);
1738549ec3ffSdougm 		if (group == NULL)
1739549ec3ffSdougm 			return (SA_NO_SUCH_GROUP);
1740687915e9Sdougm 	}
1741549ec3ffSdougm 
1742549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
1743549ec3ffSdougm 	    group = sa_get_next_group(group)) {
1744546405c3Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
1745546405c3Sdougm 		if (sa_group_is_zfs(group)) {
1746546405c3Sdougm 			sa_group_t subgroup;
1747546405c3Sdougm 			for (subgroup = sa_get_sub_group(group);
1748546405c3Sdougm 			    subgroup != NULL;
1749546405c3Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
1750546405c3Sdougm 				if (check_public(subgroup, skipshare))
1751546405c3Sdougm 					return (B_TRUE);
1752546405c3Sdougm 			}
1753546405c3Sdougm 		} else {
1754546405c3Sdougm 			if (check_public(group, skipshare))
1755546405c3Sdougm 				return (B_TRUE);
1756549ec3ffSdougm 		}
1757549ec3ffSdougm 	}
1758546405c3Sdougm 	return (B_FALSE);
1759549ec3ffSdougm }
1760549ec3ffSdougm 
1761549ec3ffSdougm /*
1762549ec3ffSdougm  * sa_enable_share at the protocol level, enable_share must tell the
1763549ec3ffSdougm  * implementation that it is to enable the share. This entails
1764549ec3ffSdougm  * converting the path and options into the appropriate ioctl
1765549ec3ffSdougm  * calls. It is assumed that all error checking of paths, etc. were
1766549ec3ffSdougm  * done earlier.
1767549ec3ffSdougm  */
1768549ec3ffSdougm static int
1769549ec3ffSdougm nfs_enable_share(sa_share_t share)
1770549ec3ffSdougm {
1771549ec3ffSdougm 	struct exportdata export;
1772549ec3ffSdougm 	sa_optionset_t secoptlist;
1773549ec3ffSdougm 	struct secinfo *sp;
1774549ec3ffSdougm 	int num_secinfo;
1775549ec3ffSdougm 	sa_optionset_t opt;
1776549ec3ffSdougm 	sa_security_t sec;
1777549ec3ffSdougm 	sa_property_t prop;
1778549ec3ffSdougm 	char *path;
1779549ec3ffSdougm 	int err = SA_OK;
1780546405c3Sdougm 	int i;
1781ecd6cf80Smarks 	int iszfs;
1782687915e9Sdougm 	sa_handle_t handle;
1783549ec3ffSdougm 
1784549ec3ffSdougm 	/* Don't drop core if the NFS module isn't loaded. */
1785549ec3ffSdougm 	(void) signal(SIGSYS, SIG_IGN);
1786549ec3ffSdougm 
1787549ec3ffSdougm 	/* get the path since it is important in several places */
1788549ec3ffSdougm 	path = sa_get_share_attr(share, "path");
1789549ec3ffSdougm 	if (path == NULL)
1790549ec3ffSdougm 		return (SA_NO_SUCH_PATH);
1791549ec3ffSdougm 
1792ecd6cf80Smarks 	iszfs = sa_path_is_zfs(path);
1793549ec3ffSdougm 	/*
1794549ec3ffSdougm 	 * find the optionsets and security sets.  There may not be
1795549ec3ffSdougm 	 * any or there could be one or two for each of optionset and
1796549ec3ffSdougm 	 * security may have multiple, one per security type per
1797549ec3ffSdougm 	 * protocol type.
1798549ec3ffSdougm 	 */
1799549ec3ffSdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
1800549ec3ffSdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
1801549ec3ffSdougm 	if (secoptlist != NULL)
1802549ec3ffSdougm 		num_secinfo = MAX(1, count_security(secoptlist));
1803549ec3ffSdougm 	else
1804549ec3ffSdougm 		num_secinfo = 1;
1805549ec3ffSdougm 
1806549ec3ffSdougm 	/*
1807549ec3ffSdougm 	 * walk through the options and fill in the structure
1808549ec3ffSdougm 	 * appropriately.
1809549ec3ffSdougm 	 */
1810549ec3ffSdougm 
1811549ec3ffSdougm 	(void) memset(&export, '\0', sizeof (export));
1812549ec3ffSdougm 
1813549ec3ffSdougm 	/*
1814549ec3ffSdougm 	 * do non-security options first since there is only one after
1815549ec3ffSdougm 	 * the derived group is constructed.
1816549ec3ffSdougm 	 */
1817549ec3ffSdougm 	export.ex_version = EX_CURRENT_VERSION;
1818549ec3ffSdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
1819549ec3ffSdougm 	export.ex_index = NULL;
1820549ec3ffSdougm 	export.ex_path = path;
1821549ec3ffSdougm 	export.ex_pathlen = strlen(path) + 1;
1822549ec3ffSdougm 
1823549ec3ffSdougm 	if (opt != NULL)
1824549ec3ffSdougm 		err = fill_export_from_optionset(&export, opt);
1825549ec3ffSdougm 
1826549ec3ffSdougm 	/*
1827549ec3ffSdougm 	 * check to see if "public" is set. If it is, then make sure
1828549ec3ffSdougm 	 * no other share has it set. If it is already used, fail.
1829549ec3ffSdougm 	 */
1830549ec3ffSdougm 
1831687915e9Sdougm 	handle = sa_find_group_handle((sa_group_t)share);
1832687915e9Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
1833549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1834549ec3ffSdougm 		    "NFS: Cannot share more than one file "
1835549ec3ffSdougm 		    "system with 'public' property\n"));
1836549ec3ffSdougm 		err = SA_NOT_ALLOWED;
1837549ec3ffSdougm 		goto out;
1838549ec3ffSdougm 	}
1839549ec3ffSdougm 
1840546405c3Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
1841549ec3ffSdougm 	if (sp == NULL) {
1842549ec3ffSdougm 		err = SA_NO_MEMORY;
1843546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1844546405c3Sdougm 		    "NFS: NFS: no memory for security\n"));
1845546405c3Sdougm 		goto out;
1846546405c3Sdougm 	}
1847549ec3ffSdougm 	export.ex_secinfo = sp;
1848549ec3ffSdougm 	/* get default secinfo */
1849549ec3ffSdougm 	export.ex_seccnt = num_secinfo;
1850549ec3ffSdougm 	/*
1851549ec3ffSdougm 	 * since we must have one security option defined, we
1852549ec3ffSdougm 	 * init to the default and then override as we find
1853549ec3ffSdougm 	 * defined security options. This handles the case
1854549ec3ffSdougm 	 * where we have no defined options but we need to set
1855549ec3ffSdougm 	 * up one.
1856549ec3ffSdougm 	 */
1857549ec3ffSdougm 	sp[0].s_window = DEF_WIN;
1858549ec3ffSdougm 	sp[0].s_rootnames = NULL;
1859549ec3ffSdougm 	/* setup a default in case no properties defined */
1860549ec3ffSdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1861549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1862549ec3ffSdougm 		    "NFS: nfs_getseconfig_default: failed to "
1863549ec3ffSdougm 		    "get default security mode\n"));
1864549ec3ffSdougm 		err = SA_CONFIG_ERR;
1865549ec3ffSdougm 	}
1866549ec3ffSdougm 	if (secoptlist != NULL) {
1867549ec3ffSdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
1868549ec3ffSdougm 		    prop != NULL && i < num_secinfo;
1869549ec3ffSdougm 		    prop = sa_get_next_property(prop), i++) {
1870549ec3ffSdougm 			char *sectype;
1871549ec3ffSdougm 			sectype = sa_get_property_attr(prop, "type");
1872a3351425Sdougm 			/*
1873a3351425Sdougm 			 * if sectype is NULL, we probably
1874a3351425Sdougm 			 * have a memory problem and can't get
1875a3351425Sdougm 			 * the correct values. Rather than
1876a3351425Sdougm 			 * exporting with incorrect security,
1877a3351425Sdougm 			 * don't share it.
1878a3351425Sdougm 			 */
1879a3351425Sdougm 			if (sectype == NULL) {
1880a3351425Sdougm 				err = SA_NO_MEMORY;
1881a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1882a3351425Sdougm 				    "NFS: Cannot share %s: "
1883a3351425Sdougm 				    "no memory\n"), path);
1884a3351425Sdougm 				goto out;
1885a3351425Sdougm 			}
1886a3351425Sdougm 			sec = (sa_security_t)sa_get_derived_security(
1887a3351425Sdougm 			    share, sectype, "nfs", 1);
1888549ec3ffSdougm 			sp[i].s_window = DEF_WIN;
1889549ec3ffSdougm 			sp[i].s_rootcnt = 0;
1890549ec3ffSdougm 			sp[i].s_rootnames = NULL;
1891549ec3ffSdougm 			(void) fill_security_from_secopts(&sp[i], sec);
1892549ec3ffSdougm 			if (sec != NULL)
1893549ec3ffSdougm 				sa_free_derived_security(sec);
1894549ec3ffSdougm 			if (sectype != NULL)
1895549ec3ffSdougm 				sa_free_attr_string(sectype);
1896549ec3ffSdougm 		}
1897549ec3ffSdougm 	}
1898549ec3ffSdougm 	/*
1899549ec3ffSdougm 	 * when we get here, we can do the exportfs system call and
19004b92079bSAlexander Eremin 	 * initiate things. We probably want to enable the
19014b92079bSAlexander Eremin 	 * svc:/network/nfs/server service first if it isn't running.
1902549ec3ffSdougm 	 */
19034b92079bSAlexander Eremin 	/* check svc:/network/nfs/server status and start if needed */
1904549ec3ffSdougm 	/* now add the share to the internal tables */
1905549ec3ffSdougm 	printarg(path, &export);
1906549ec3ffSdougm 	/*
1907549ec3ffSdougm 	 * call the exportfs system call which is implemented
1908549ec3ffSdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
1909549ec3ffSdougm 	 */
1910ecd6cf80Smarks 	if (iszfs) {
1911ecd6cf80Smarks 		struct exportfs_args ea;
1912ecd6cf80Smarks 		share_t sh;
1913ecd6cf80Smarks 		char *str;
1914ecd6cf80Smarks 		priv_set_t *priv_effective;
1915ecd6cf80Smarks 		int privileged;
1916ecd6cf80Smarks 
1917ecd6cf80Smarks 		/*
1918ecd6cf80Smarks 		 * If we aren't a privileged user
1919ecd6cf80Smarks 		 * and NFS server service isn't running
1920ecd6cf80Smarks 		 * then print out an error message
1921ecd6cf80Smarks 		 * and return EPERM
1922ecd6cf80Smarks 		 */
1923ecd6cf80Smarks 
1924ecd6cf80Smarks 		priv_effective = priv_allocset();
1925ecd6cf80Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
1926ecd6cf80Smarks 
1927ecd6cf80Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
1928ecd6cf80Smarks 		priv_freeset(priv_effective);
1929ecd6cf80Smarks 
1930ecd6cf80Smarks 		if (!privileged &&
1931ecd6cf80Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
1932ecd6cf80Smarks 			err = 0;
1933ecd6cf80Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
1934ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1935ecd6cf80Smarks 				    "NFS: Cannot share remote "
1936ecd6cf80Smarks 				    "filesystem: %s\n"), path);
1937ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1938ecd6cf80Smarks 				    "NFS: Service needs to be enabled "
1939ecd6cf80Smarks 				    "by a privileged user\n"));
1940ecd6cf80Smarks 				err = SA_SYSTEM_ERR;
1941ecd6cf80Smarks 				errno = EPERM;
1942ecd6cf80Smarks 			}
1943ecd6cf80Smarks 			free(str);
1944ecd6cf80Smarks 		}
1945ecd6cf80Smarks 
1946ecd6cf80Smarks 		if (err == 0) {
1947ecd6cf80Smarks 			ea.dname = path;
1948ecd6cf80Smarks 			ea.uex = &export;
1949ecd6cf80Smarks 
19505ad42b1bSSurya Prakki 			(void) sa_sharetab_fill_zfs(share, &sh, "nfs");
1951743a77edSAlan Wright 			err = sa_share_zfs(share, NULL, path, &sh,
1952da6c28aaSamw 			    &ea, ZFS_SHARE_NFS);
1953a63214d6SBill Krier 			if (err != SA_OK) {
1954a63214d6SBill Krier 				errno = err;
1955a63214d6SBill Krier 				err = -1;
1956a63214d6SBill Krier 			}
1957ecd6cf80Smarks 			sa_emptyshare(&sh);
1958ecd6cf80Smarks 		}
1959ecd6cf80Smarks 	} else {
1960ecd6cf80Smarks 		err = exportfs(path, &export);
1961ecd6cf80Smarks 	}
1962ecd6cf80Smarks 
1963ecd6cf80Smarks 	if (err < 0) {
1964549ec3ffSdougm 		err = SA_SYSTEM_ERR;
1965549ec3ffSdougm 		switch (errno) {
1966549ec3ffSdougm 		case EREMOTE:
1967549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1968ecd6cf80Smarks 			    "NFS: Cannot share filesystems "
1969ecd6cf80Smarks 			    "in non-global zones: %s\n"), path);
1970ecd6cf80Smarks 			err = SA_NOT_SUPPORTED;
1971549ec3ffSdougm 			break;
1972549ec3ffSdougm 		case EPERM:
1973549ec3ffSdougm 			if (getzoneid() != GLOBAL_ZONEID) {
1974549ec3ffSdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1975549ec3ffSdougm 				    "NFS: Cannot share file systems "
1976549ec3ffSdougm 				    "in non-global zones: %s\n"), path);
1977549ec3ffSdougm 				err = SA_NOT_SUPPORTED;
1978549ec3ffSdougm 				break;
1979549ec3ffSdougm 			}
1980549ec3ffSdougm 			err = SA_NO_PERMISSION;
19810616fd7fSPavel Filipensky 			break;
19820616fd7fSPavel Filipensky 		case EEXIST:
19830616fd7fSPavel Filipensky 			err = SA_SHARE_EXISTS;
19840616fd7fSPavel Filipensky 			break;
1985549ec3ffSdougm 		default:
1986549ec3ffSdougm 			break;
1987549ec3ffSdougm 		}
1988549ec3ffSdougm 	} else {
1989549ec3ffSdougm 		/* update sharetab with an add/modify */
1990ecd6cf80Smarks 		if (!iszfs) {
1991549ec3ffSdougm 			(void) sa_update_sharetab(share, "nfs");
1992549ec3ffSdougm 		}
1993ecd6cf80Smarks 	}
1994549ec3ffSdougm 
1995549ec3ffSdougm 	if (err == SA_OK) {
1996549ec3ffSdougm 		/*
1997549ec3ffSdougm 		 * enable services as needed. This should probably be
1998549ec3ffSdougm 		 * done elsewhere in order to minimize the calls to
1999549ec3ffSdougm 		 * check services.
2000549ec3ffSdougm 		 */
2001549ec3ffSdougm 		/*
2002549ec3ffSdougm 		 * check to see if logging and other services need to
2003549ec3ffSdougm 		 * be triggered, but only if there wasn't an
2004549ec3ffSdougm 		 * error. This is probably where sharetab should be
2005549ec3ffSdougm 		 * updated with the NFS specific entry.
2006549ec3ffSdougm 		 */
2007549ec3ffSdougm 		if (export.ex_flags & EX_LOG) {
2008549ec3ffSdougm 			/* enable logging */
2009549ec3ffSdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
2010549ec3ffSdougm 			    export.ex_tag) != 0) {
2011a3351425Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2012549ec3ffSdougm 				    "Could not enable logging for %s\n"),
2013549ec3ffSdougm 				    path);
2014549ec3ffSdougm 			}
2015549ec3ffSdougm 			_check_services(service_list_logging);
2016549ec3ffSdougm 		} else {
2017549ec3ffSdougm 			/*
2018549ec3ffSdougm 			 * don't have logging so remove it from file. It might
2019549ec3ffSdougm 			 * not be thre, but that doesn't matter.
2020549ec3ffSdougm 			 */
2021549ec3ffSdougm 			(void) nfslogtab_deactivate(path);
2022549ec3ffSdougm 			_check_services(service_list_default);
2023549ec3ffSdougm 		}
2024549ec3ffSdougm 	}
2025549ec3ffSdougm 
2026549ec3ffSdougm out:
2027549ec3ffSdougm 	if (path != NULL)
2028549ec3ffSdougm 		free(path);
2029549ec3ffSdougm 
2030549ec3ffSdougm 	cleanup_export(&export);
2031549ec3ffSdougm 	if (opt != NULL)
2032549ec3ffSdougm 		sa_free_derived_optionset(opt);
2033549ec3ffSdougm 	if (secoptlist != NULL)
2034549ec3ffSdougm 		(void) sa_destroy_optionset(secoptlist);
2035549ec3ffSdougm 	return (err);
2036549ec3ffSdougm }
2037549ec3ffSdougm 
2038549ec3ffSdougm /*
20398e314a44Sdougm  * nfs_disable_share(share, path)
2040549ec3ffSdougm  *
20418e314a44Sdougm  * Unshare the specified share. Note that "path" is the same path as
20428e314a44Sdougm  * what is in the "share" object. It is passed in to avoid an
20438e314a44Sdougm  * additional lookup. A missing "path" value makes this a no-op
20448e314a44Sdougm  * function.
2045549ec3ffSdougm  */
2046549ec3ffSdougm static int
2047ecd6cf80Smarks nfs_disable_share(sa_share_t share, char *path)
2048549ec3ffSdougm {
2049549ec3ffSdougm 	int err;
2050549ec3ffSdougm 	int ret = SA_OK;
2051ecd6cf80Smarks 	int iszfs;
20528e314a44Sdougm 	sa_group_t parent;
20535b6e0c46Sdougm 	sa_handle_t handle;
2054549ec3ffSdougm 
20558e314a44Sdougm 	if (path == NULL)
20568e314a44Sdougm 		return (ret);
2057ecd6cf80Smarks 
20588e314a44Sdougm 	/*
20598e314a44Sdougm 	 * If the share is in a ZFS group we need to handle it
20608e314a44Sdougm 	 * differently.  Just being on a ZFS file system isn't
20618e314a44Sdougm 	 * enough since we may be in a legacy share case.
20628e314a44Sdougm 	 */
20638e314a44Sdougm 	parent = sa_get_parent_group(share);
20648e314a44Sdougm 	iszfs = sa_group_is_zfs(parent);
2065ecd6cf80Smarks 	if (iszfs) {
2066ecd6cf80Smarks 		struct exportfs_args ea;
2067ecd6cf80Smarks 		share_t sh = { 0 };
2068ecd6cf80Smarks 		ea.dname = path;
2069ecd6cf80Smarks 		ea.uex = NULL;
2070ecd6cf80Smarks 		sh.sh_path = path;
2071ecd6cf80Smarks 		sh.sh_fstype = "nfs";
2072ecd6cf80Smarks 
2073743a77edSAlan Wright 		err = sa_share_zfs(share, NULL, path, &sh,
2074da6c28aaSamw 		    &ea, ZFS_UNSHARE_NFS);
2075a63214d6SBill Krier 		if (err != SA_OK) {
2076a63214d6SBill Krier 			errno = err;
2077a63214d6SBill Krier 			err = -1;
2078a63214d6SBill Krier 		}
20798e314a44Sdougm 	} else {
2080ecd6cf80Smarks 		err = exportfs(path, NULL);
20818e314a44Sdougm 	}
2082549ec3ffSdougm 	if (err < 0) {
2083a3351425Sdougm 		/*
2084ecd6cf80Smarks 		 * TBD: only an error in some
2085ecd6cf80Smarks 		 * cases - need better analysis
2086a3351425Sdougm 		 */
2087549ec3ffSdougm 		switch (errno) {
2088549ec3ffSdougm 		case EPERM:
2089549ec3ffSdougm 		case EACCES:
2090549ec3ffSdougm 			ret = SA_NO_PERMISSION;
2091ecd6cf80Smarks 			if (getzoneid() != GLOBAL_ZONEID) {
2092549ec3ffSdougm 				ret = SA_NOT_SUPPORTED;
2093ecd6cf80Smarks 			}
2094549ec3ffSdougm 			break;
2095549ec3ffSdougm 		case EINVAL:
2096549ec3ffSdougm 		case ENOENT:
2097549ec3ffSdougm 			ret = SA_NO_SUCH_PATH;
2098549ec3ffSdougm 			break;
2099549ec3ffSdougm 		default:
2100549ec3ffSdougm 			ret = SA_SYSTEM_ERR;
2101549ec3ffSdougm 			break;
2102549ec3ffSdougm 		}
2103549ec3ffSdougm 	}
2104549ec3ffSdougm 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
21055b6e0c46Sdougm 		handle = sa_find_group_handle((sa_group_t)share);
2106ecd6cf80Smarks 		if (!iszfs)
21075b6e0c46Sdougm 			(void) sa_delete_sharetab(handle, path, "nfs");
2108549ec3ffSdougm 		/* just in case it was logged */
2109ecd6cf80Smarks 		(void) nfslogtab_deactivate(path);
2110549ec3ffSdougm 	}
2111549ec3ffSdougm 	return (ret);
2112549ec3ffSdougm }
2113549ec3ffSdougm 
21145cb0d679SMarcel Telka static int
21155cb0d679SMarcel Telka check_user(char *value)
21165cb0d679SMarcel Telka {
21175cb0d679SMarcel Telka 	int ret = SA_OK;
21185cb0d679SMarcel Telka 
21195cb0d679SMarcel Telka 	if (!is_a_number(value)) {
21205cb0d679SMarcel Telka 		struct passwd *pw;
21215cb0d679SMarcel Telka 		/*
21225cb0d679SMarcel Telka 		 * in this case it would have to be a
21235cb0d679SMarcel Telka 		 * user name
21245cb0d679SMarcel Telka 		 */
21255cb0d679SMarcel Telka 		pw = getpwnam(value);
21265cb0d679SMarcel Telka 		if (pw == NULL)
21275cb0d679SMarcel Telka 			ret = SA_BAD_VALUE;
21285cb0d679SMarcel Telka 		endpwent();
21295cb0d679SMarcel Telka 	} else {
21305cb0d679SMarcel Telka 		uint64_t intval;
21315cb0d679SMarcel Telka 		intval = strtoull(value, NULL, 0);
21325cb0d679SMarcel Telka 		if (intval > UID_MAX && intval != -1)
21335cb0d679SMarcel Telka 			ret = SA_BAD_VALUE;
21345cb0d679SMarcel Telka 	}
21355cb0d679SMarcel Telka 
21365cb0d679SMarcel Telka 	return (ret);
21375cb0d679SMarcel Telka }
21385cb0d679SMarcel Telka 
21395cb0d679SMarcel Telka static int
21405cb0d679SMarcel Telka check_group(char *value)
21415cb0d679SMarcel Telka {
21425cb0d679SMarcel Telka 	int ret = SA_OK;
21435cb0d679SMarcel Telka 
21445cb0d679SMarcel Telka 	if (!is_a_number(value)) {
21455cb0d679SMarcel Telka 		struct group *gr;
21465cb0d679SMarcel Telka 		/*
21475cb0d679SMarcel Telka 		 * in this case it would have to be a
21485cb0d679SMarcel Telka 		 * group name
21495cb0d679SMarcel Telka 		 */
21505cb0d679SMarcel Telka 		gr = getgrnam(value);
21515cb0d679SMarcel Telka 		if (gr == NULL)
21525cb0d679SMarcel Telka 			ret = SA_BAD_VALUE;
21535cb0d679SMarcel Telka 		endgrent();
21545cb0d679SMarcel Telka 	} else {
21555cb0d679SMarcel Telka 		uint64_t intval;
21565cb0d679SMarcel Telka 		intval = strtoull(value, NULL, 0);
21575cb0d679SMarcel Telka 		if (intval > UID_MAX && intval != -1)
21585cb0d679SMarcel Telka 			ret = SA_BAD_VALUE;
21595cb0d679SMarcel Telka 	}
21605cb0d679SMarcel Telka 
21615cb0d679SMarcel Telka 	return (ret);
21625cb0d679SMarcel Telka }
21635cb0d679SMarcel Telka 
2164549ec3ffSdougm /*
2165b89a8333Snatalie li - Sun Microsystems - Irvine United States  * check_rorwnone(v1, v2, v3)
2166b89a8333Snatalie li - Sun Microsystems - Irvine United States  *
2167b89a8333Snatalie li - Sun Microsystems - Irvine United States  * check ro vs rw vs none values.  Over time this may get beefed up.
2168b89a8333Snatalie li - Sun Microsystems - Irvine United States  * for now it just does simple checks. v1 is never NULL but v2 or v3
2169b89a8333Snatalie li - Sun Microsystems - Irvine United States  * could be.
2170549ec3ffSdougm  */
2171549ec3ffSdougm 
2172549ec3ffSdougm static int
2173b89a8333Snatalie li - Sun Microsystems - Irvine United States check_rorwnone(char *v1, char *v2, char *v3)
2174549ec3ffSdougm {
2175549ec3ffSdougm 	int ret = SA_OK;
2176b89a8333Snatalie li - Sun Microsystems - Irvine United States 	if (v2 != NULL && strcmp(v1, v2) == 0)
2177549ec3ffSdougm 		ret = SA_VALUE_CONFLICT;
2178b89a8333Snatalie li - Sun Microsystems - Irvine United States 	else if (v3 != NULL && strcmp(v1, v3) == 0)
2179b89a8333Snatalie li - Sun Microsystems - Irvine United States 		ret = SA_VALUE_CONFLICT;
2180b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2181549ec3ffSdougm 	return (ret);
2182549ec3ffSdougm }
2183549ec3ffSdougm 
2184549ec3ffSdougm /*
2185687915e9Sdougm  * nfs_validate_property(handle, property, parent)
2186549ec3ffSdougm  *
2187549ec3ffSdougm  * Check that the property has a legitimate value for its type.
2188549ec3ffSdougm  */
2189549ec3ffSdougm 
2190549ec3ffSdougm static int
2191687915e9Sdougm nfs_validate_property(sa_handle_t handle, sa_property_t property,
2192687915e9Sdougm     sa_optionset_t parent)
2193549ec3ffSdougm {
2194549ec3ffSdougm 	int ret = SA_OK;
2195549ec3ffSdougm 	char *propname;
2196b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *other1;
2197b89a8333Snatalie li - Sun Microsystems - Irvine United States 	char *other2;
2198549ec3ffSdougm 	int optindex;
2199549ec3ffSdougm 	nfsl_config_t *configlist;
2200549ec3ffSdougm 	sa_group_t parent_group;
2201549ec3ffSdougm 	char *value;
2202549ec3ffSdougm 
2203549ec3ffSdougm 	propname = sa_get_property_attr(property, "type");
2204549ec3ffSdougm 
2205549ec3ffSdougm 	if ((optindex = findopt(propname)) < 0)
2206549ec3ffSdougm 		ret = SA_NO_SUCH_PROP;
2207549ec3ffSdougm 
2208549ec3ffSdougm 	/* need to validate value range here as well */
2209549ec3ffSdougm 
2210549ec3ffSdougm 	if (ret == SA_OK) {
2211549ec3ffSdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
2212687915e9Sdougm 		if (optdefs[optindex].share && parent_group != NULL &&
2213687915e9Sdougm 		    !sa_is_share(parent_group))
2214549ec3ffSdougm 			ret = SA_PROP_SHARE_ONLY;
2215549ec3ffSdougm 	}
2216549ec3ffSdougm 	if (ret == SA_OK) {
2217687915e9Sdougm 		if (optdefs[optindex].index == OPT_PUBLIC) {
2218687915e9Sdougm 			/*
2219687915e9Sdougm 			 * Public is special in that only one instance can
2220687915e9Sdougm 			 * be in the repository at the same time.
2221687915e9Sdougm 			 */
2222687915e9Sdougm 			if (public_exists(handle, parent_group)) {
2223687915e9Sdougm 				sa_free_attr_string(propname);
2224687915e9Sdougm 				return (SA_VALUE_CONFLICT);
2225687915e9Sdougm 			}
2226687915e9Sdougm 		}
2227549ec3ffSdougm 		value = sa_get_property_attr(property, "value");
2228549ec3ffSdougm 		if (value != NULL) {
2229549ec3ffSdougm 			/* first basic type checking */
2230549ec3ffSdougm 			switch (optdefs[optindex].type) {
22315cb0d679SMarcel Telka 
2232549ec3ffSdougm 			case OPT_TYPE_NUMBER:
2233549ec3ffSdougm 				/* check that the value is all digits */
2234549ec3ffSdougm 				if (!is_a_number(value))
2235549ec3ffSdougm 					ret = SA_BAD_VALUE;
2236549ec3ffSdougm 				break;
22375cb0d679SMarcel Telka 
2238549ec3ffSdougm 			case OPT_TYPE_BOOLEAN:
2239549ec3ffSdougm 				if (strlen(value) == 0 ||
2240549ec3ffSdougm 				    strcasecmp(value, "true") == 0 ||
2241549ec3ffSdougm 				    strcmp(value, "1") == 0 ||
2242549ec3ffSdougm 				    strcasecmp(value, "false") == 0 ||
2243549ec3ffSdougm 				    strcmp(value, "0") == 0) {
2244549ec3ffSdougm 					ret = SA_OK;
2245549ec3ffSdougm 				} else {
2246549ec3ffSdougm 					ret = SA_BAD_VALUE;
2247549ec3ffSdougm 				}
2248549ec3ffSdougm 				break;
22495cb0d679SMarcel Telka 
2250549ec3ffSdougm 			case OPT_TYPE_USER:
22515cb0d679SMarcel Telka 				ret = check_user(value);
2252549ec3ffSdougm 				break;
22535cb0d679SMarcel Telka 
2254549ec3ffSdougm 			case OPT_TYPE_FILE:
2255549ec3ffSdougm 				if (strcmp(value, "..") == 0 ||
2256549ec3ffSdougm 				    strchr(value, '/') != NULL) {
2257549ec3ffSdougm 					ret = SA_BAD_VALUE;
2258549ec3ffSdougm 				}
2259549ec3ffSdougm 				break;
22605cb0d679SMarcel Telka 
2261b89a8333Snatalie li - Sun Microsystems - Irvine United States 			case OPT_TYPE_ACCLIST: {
2262b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_property_t oprop1;
2263b89a8333Snatalie li - Sun Microsystems - Irvine United States 				sa_property_t oprop2;
2264b89a8333Snatalie li - Sun Microsystems - Irvine United States 				char *ovalue1 = NULL;
2265b89a8333Snatalie li - Sun Microsystems - Irvine United States 				char *ovalue2 = NULL;
2266b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2267b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (parent == NULL)
2268b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2269549ec3ffSdougm 				/*
2270549ec3ffSdougm 				 * access list handling. Should eventually
2271549ec3ffSdougm 				 * validate that all the values make sense.
2272549ec3ffSdougm 				 * Also, ro and rw may have cross value
2273549ec3ffSdougm 				 * conflicts.
2274549ec3ffSdougm 				 */
2275b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (strcmp(propname, SHOPT_RO) == 0) {
2276b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RW;
2277b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_NONE;
2278b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else if (strcmp(propname, SHOPT_RW) == 0) {
2279b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RO;
2280b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_NONE;
2281b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else if (strcmp(propname, SHOPT_NONE) == 0) {
2282b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = SHOPT_RO;
2283b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = SHOPT_RW;
2284b89a8333Snatalie li - Sun Microsystems - Irvine United States 				} else {
2285b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other1 = NULL;
2286b89a8333Snatalie li - Sun Microsystems - Irvine United States 					other2 = NULL;
2287549ec3ffSdougm 				}
2288b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (other1 == NULL && other2 == NULL)
2289549ec3ffSdougm 					break;
2290b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2291b89a8333Snatalie li - Sun Microsystems - Irvine United States 				/* compare rw(ro) with ro(rw) */
2292b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2293b89a8333Snatalie li - Sun Microsystems - Irvine United States 				oprop1 = sa_get_property(parent, other1);
2294b89a8333Snatalie li - Sun Microsystems - Irvine United States 				oprop2 = sa_get_property(parent, other2);
2295b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (oprop1 == NULL && oprop2 == NULL)
2296b89a8333Snatalie li - Sun Microsystems - Irvine United States 					break;
2297b89a8333Snatalie li - Sun Microsystems - Irvine United States 				/*
2298b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * Only potential confusion if other1
2299b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * or other2 exists. Check the values
2300b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * and run the check if there is a
2301b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * value other than the one we are
2302b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 * explicitly looking at.
2303b89a8333Snatalie li - Sun Microsystems - Irvine United States 				 */
2304b89a8333Snatalie li - Sun Microsystems - Irvine United States 				ovalue1 = sa_get_property_attr(oprop1, "value");
2305b89a8333Snatalie li - Sun Microsystems - Irvine United States 				ovalue2 = sa_get_property_attr(oprop2, "value");
2306b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue1 != NULL || ovalue2 != NULL)
2307b89a8333Snatalie li - Sun Microsystems - Irvine United States 					ret = check_rorwnone(value, ovalue1,
2308b89a8333Snatalie li - Sun Microsystems - Irvine United States 					    ovalue2);
2309b89a8333Snatalie li - Sun Microsystems - Irvine United States 
2310b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue1 != NULL)
2311b89a8333Snatalie li - Sun Microsystems - Irvine United States 					sa_free_attr_string(ovalue1);
2312b89a8333Snatalie li - Sun Microsystems - Irvine United States 				if (ovalue2 != NULL)
2313b89a8333Snatalie li - Sun Microsystems - Irvine United States 					sa_free_attr_string(ovalue2);
2314b89a8333Snatalie li - Sun Microsystems - Irvine United States 				break;
2315b89a8333Snatalie li - Sun Microsystems - Irvine United States 			}
23165cb0d679SMarcel Telka 
2317549ec3ffSdougm 			case OPT_TYPE_LOGTAG:
2318549ec3ffSdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
2319549ec3ffSdougm 					int error;
2320a3351425Sdougm 					if (value == NULL ||
2321a3351425Sdougm 					    strlen(value) == 0) {
2322549ec3ffSdougm 						if (value != NULL)
2323a3351425Sdougm 							sa_free_attr_string(
2324a3351425Sdougm 							    value);
2325549ec3ffSdougm 						value = strdup("global");
2326549ec3ffSdougm 					}
2327a3351425Sdougm 					if (value != NULL &&
2328a3351425Sdougm 					    nfsl_findconfig(configlist, value,
2329a3351425Sdougm 					    &error) == NULL) {
2330549ec3ffSdougm 						ret = SA_BAD_VALUE;
2331a3351425Sdougm 					}
2332aed5d200Sdougm 					/* Must always free when done */
2333aed5d200Sdougm 					nfsl_freeconfig_list(&configlist);
2334549ec3ffSdougm 				} else {
2335549ec3ffSdougm 					ret = SA_CONFIG_ERR;
2336549ec3ffSdougm 				}
2337549ec3ffSdougm 				break;
23385cb0d679SMarcel Telka 
2339549ec3ffSdougm 			case OPT_TYPE_STRING:
2340549ec3ffSdougm 				/* whatever is here should be ok */
2341549ec3ffSdougm 				break;
23425cb0d679SMarcel Telka 
2343549ec3ffSdougm 			case OPT_TYPE_SECURITY:
2344549ec3ffSdougm 				/*
2345549ec3ffSdougm 				 * The "sec" property isn't used in the
2346549ec3ffSdougm 				 * non-legacy parts of sharemgr. We need to
2347549ec3ffSdougm 				 * reject it here. For legacy, it is pulled
2348549ec3ffSdougm 				 * out well before we get here.
2349549ec3ffSdougm 				 */
2350549ec3ffSdougm 				ret = SA_NO_SUCH_PROP;
2351549ec3ffSdougm 				break;
23525cb0d679SMarcel Telka 
23535cb0d679SMarcel Telka 			case OPT_TYPE_MAPPING: {
23545cb0d679SMarcel Telka 				char *p;
23555cb0d679SMarcel Telka 				char *n;
23565cb0d679SMarcel Telka 				char *c;
23575cb0d679SMarcel Telka 				int (*f)(char *);
23585cb0d679SMarcel Telka 
23595cb0d679SMarcel Telka 				sa_security_t security;
23605cb0d679SMarcel Telka 
23615cb0d679SMarcel Telka 				/*
23625cb0d679SMarcel Telka 				 * mapping is only supported for sec=sys
23635cb0d679SMarcel Telka 				 */
23645cb0d679SMarcel Telka 				ret = SA_CONFIG_ERR;
23655cb0d679SMarcel Telka 				if (parent_group == NULL)
23665cb0d679SMarcel Telka 					break;
23675cb0d679SMarcel Telka 
23685cb0d679SMarcel Telka 				for (security = sa_get_security(parent_group,
23695cb0d679SMarcel Telka 				    NULL, NULL); security != NULL;
23705cb0d679SMarcel Telka 				    security = sa_get_next_security(security)) {
23715cb0d679SMarcel Telka 					char *type;
23725cb0d679SMarcel Telka 					char *sectype;
23735cb0d679SMarcel Telka 
23745cb0d679SMarcel Telka 					type = sa_get_security_attr(security,
23755cb0d679SMarcel Telka 					    "type");
23765cb0d679SMarcel Telka 					if (type == NULL)
23775cb0d679SMarcel Telka 						continue;
23785cb0d679SMarcel Telka 
23795cb0d679SMarcel Telka 					if (strcmp(type, "nfs") != 0) {
23805cb0d679SMarcel Telka 						sa_free_attr_string(type);
23815cb0d679SMarcel Telka 						continue;
23825cb0d679SMarcel Telka 					}
23835cb0d679SMarcel Telka 					sa_free_attr_string(type);
23845cb0d679SMarcel Telka 
23855cb0d679SMarcel Telka 					sectype = sa_get_security_attr(security,
23865cb0d679SMarcel Telka 					    "sectype");
23875cb0d679SMarcel Telka 					if (sectype == NULL)
23885cb0d679SMarcel Telka 						continue;
23895cb0d679SMarcel Telka 
23905cb0d679SMarcel Telka 					if (strcmp(sectype, "sys") != 0) {
23915cb0d679SMarcel Telka 						sa_free_attr_string(sectype);
23925cb0d679SMarcel Telka 						ret = SA_CONFIG_ERR;
23935cb0d679SMarcel Telka 						break;
23945cb0d679SMarcel Telka 					}
23955cb0d679SMarcel Telka 					sa_free_attr_string(sectype);
23965cb0d679SMarcel Telka 					ret = SA_OK;
23975cb0d679SMarcel Telka 				}
23985cb0d679SMarcel Telka 
23995cb0d679SMarcel Telka 				if (ret != SA_OK)
24005cb0d679SMarcel Telka 					break;
24015cb0d679SMarcel Telka 
24025cb0d679SMarcel Telka 				assert(optindex == OPT_UIDMAP ||
24035cb0d679SMarcel Telka 				    optindex == OPT_GIDMAP);
24045cb0d679SMarcel Telka 				f = optindex == OPT_UIDMAP ? check_user :
24055cb0d679SMarcel Telka 				    check_group;
24065cb0d679SMarcel Telka 
24075cb0d679SMarcel Telka 
24085cb0d679SMarcel Telka 				p = strdup(value);
24095cb0d679SMarcel Telka 				if (p == NULL)
24105cb0d679SMarcel Telka 					ret = SA_BAD_VALUE;
24115cb0d679SMarcel Telka 
24125cb0d679SMarcel Telka 				for (c = p; ret == SA_OK && c != NULL; c = n) {
24135cb0d679SMarcel Telka 					char *s;
24145cb0d679SMarcel Telka 					char *t;
24155cb0d679SMarcel Telka 
24165cb0d679SMarcel Telka 					n = strchr(c, '~');
24175cb0d679SMarcel Telka 					if (n != NULL)
24185cb0d679SMarcel Telka 						*n++ = '\0';
24195cb0d679SMarcel Telka 
24205cb0d679SMarcel Telka 					s = strchr(c, ':');
24215cb0d679SMarcel Telka 					if (s != NULL) {
24225cb0d679SMarcel Telka 						*s++ = '\0';
24235cb0d679SMarcel Telka 						t = strchr(s, ':');
24245cb0d679SMarcel Telka 						if (t != NULL)
24255cb0d679SMarcel Telka 							*t = '\0';
24265cb0d679SMarcel Telka 					}
24275cb0d679SMarcel Telka 
24285cb0d679SMarcel Telka 					if (s == NULL || t == NULL)
24295cb0d679SMarcel Telka 						ret = SA_BAD_VALUE;
24305cb0d679SMarcel Telka 
24315cb0d679SMarcel Telka 					if (ret == SA_OK && *c != '\0' &&
24325cb0d679SMarcel Telka 					    strcmp(c, "*") != 0)
24335cb0d679SMarcel Telka 						ret = f(c);
24345cb0d679SMarcel Telka 
24355cb0d679SMarcel Telka 					if (ret == SA_OK && *s != '\0' &&
24365cb0d679SMarcel Telka 					    strcmp(s, "-1") != 0)
24375cb0d679SMarcel Telka 						ret = f(s);
24385cb0d679SMarcel Telka 				}
24395cb0d679SMarcel Telka 
24405cb0d679SMarcel Telka 				free(p);
24415cb0d679SMarcel Telka 
24425cb0d679SMarcel Telka 				break;
24435cb0d679SMarcel Telka 			}
24445cb0d679SMarcel Telka 
2445549ec3ffSdougm 			default:
2446549ec3ffSdougm 				break;
2447549ec3ffSdougm 			}
2448aed5d200Sdougm 
2449aed5d200Sdougm 			if (value != NULL)
2450549ec3ffSdougm 				sa_free_attr_string(value);
2451aed5d200Sdougm 
2452549ec3ffSdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
2453549ec3ffSdougm 				/* do the property specific check */
2454687915e9Sdougm 				ret = optdefs[optindex].check(handle, property);
2455549ec3ffSdougm 			}
2456549ec3ffSdougm 		}
2457549ec3ffSdougm 	}
2458549ec3ffSdougm 
2459549ec3ffSdougm 	if (propname != NULL)
2460549ec3ffSdougm 		sa_free_attr_string(propname);
2461549ec3ffSdougm 	return (ret);
2462549ec3ffSdougm }
2463549ec3ffSdougm 
2464549ec3ffSdougm /*
2465549ec3ffSdougm  * Protocol management functions
2466549ec3ffSdougm  *
2467549ec3ffSdougm  * Properties defined in the default files are defined in
2468549ec3ffSdougm  * proto_option_defs for parsing and validation. If "other" and
2469549ec3ffSdougm  * "compare" are set, then the value for this property should be
2470549ec3ffSdougm  * compared against the property specified in "other" using the
2471549ec3ffSdougm  * "compare" check (either <= or >=) in order to ensure that the
2472549ec3ffSdougm  * values are in the correct range.  E.g. setting server_versmin
2473549ec3ffSdougm  * higher than server_versmax should not be allowed.
2474549ec3ffSdougm  */
2475549ec3ffSdougm 
2476549ec3ffSdougm struct proto_option_defs {
2477549ec3ffSdougm 	char *tag;
2478549ec3ffSdougm 	char *name;	/* display name -- remove protocol identifier */
2479549ec3ffSdougm 	int index;
2480549ec3ffSdougm 	int type;
2481549ec3ffSdougm 	union {
2482549ec3ffSdougm 	    int intval;
2483549ec3ffSdougm 	    char *string;
2484549ec3ffSdougm 	} defvalue;
2485549ec3ffSdougm 	uint32_t svcs;
2486549ec3ffSdougm 	int32_t minval;
2487549ec3ffSdougm 	int32_t maxval;
2488549ec3ffSdougm 	char *other;
2489549ec3ffSdougm 	int compare;
2490549ec3ffSdougm #define	OPT_CMP_GE	0
2491549ec3ffSdougm #define	OPT_CMP_LE	1
2492549ec3ffSdougm 	int (*check)(char *);
2493549ec3ffSdougm } proto_options[] = {
2494549ec3ffSdougm #define	PROTO_OPT_NFSD_SERVERS			0
2495549ec3ffSdougm 	{"nfsd_servers",
2496549ec3ffSdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
2497dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    1, INT32_MAX},
2498549ec3ffSdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
2499549ec3ffSdougm 	{"lockd_listen_backlog",
2500549ec3ffSdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
2501dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX},
2502549ec3ffSdougm #define	PROTO_OPT_LOCKD_SERVERS			2
2503549ec3ffSdougm 	{"lockd_servers",
2504549ec3ffSdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
2505dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SVC_LOCKD, 1, INT32_MAX},
2506549ec3ffSdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
2507549ec3ffSdougm 	{"lockd_retransmit_timeout",
2508549ec3ffSdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
2509dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX},
2510549ec3ffSdougm #define	PROTO_OPT_GRACE_PERIOD			4
2511549ec3ffSdougm 	{"grace_period",
2512549ec3ffSdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
2513dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SVC_LOCKD, 0, INT32_MAX},
2514549ec3ffSdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
2515549ec3ffSdougm 	{"nfs_server_versmin",
2516549ec3ffSdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
2517549ec3ffSdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2518dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    NFS_VERSMAX, "server_versmax", OPT_CMP_LE},
2519549ec3ffSdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
2520549ec3ffSdougm 	{"nfs_server_versmax",
2521549ec3ffSdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
2522549ec3ffSdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2523dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    NFS_VERSMAX, "server_versmin", OPT_CMP_GE},
2524549ec3ffSdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
2525549ec3ffSdougm 	{"nfs_client_versmin",
2526549ec3ffSdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
2527dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    (int)NFS_VERSMIN_DEFAULT, SVC_CLIENT, NFS_VERSMIN, NFS_VERSMAX,
2528dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    "client_versmax", OPT_CMP_LE},
2529549ec3ffSdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
2530549ec3ffSdougm 	{"nfs_client_versmax",
2531549ec3ffSdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
2532dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    (int)NFS_VERSMAX_DEFAULT, SVC_CLIENT, NFS_VERSMIN, NFS_VERSMAX,
2533dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    "client_versmin", OPT_CMP_GE},
2534549ec3ffSdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
2535549ec3ffSdougm 	{"nfs_server_delegation",
2536549ec3ffSdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
2537dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0},
2538549ec3ffSdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
2539549ec3ffSdougm 	{"nfsmapid_domain",
2540549ec3ffSdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
2541dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    NULL, SVC_NFSMAPID, 0, 0},
2542549ec3ffSdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
2543549ec3ffSdougm 	{"nfsd_max_connections",
2544549ec3ffSdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
2545dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX},
2546549ec3ffSdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
2547549ec3ffSdougm 	{"nfsd_protocol",
2548549ec3ffSdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
2549dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SVC_NFSD, 0, 0},
2550549ec3ffSdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
2551549ec3ffSdougm 	{"nfsd_listen_backlog",
2552549ec3ffSdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
2553dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_NUMBER, 0, SVC_NFSD, 0, INT32_MAX},
2554dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #define	PROTO_OPT_NFSD_DEVICE			14
2555dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	{"nfsd_device",
2556dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    "device", PROTO_OPT_NFSD_DEVICE,
2557dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    OPT_TYPE_STRING, NULL, SVC_NFSD, 0, 0},
2558361f55a5SMarcel Telka #define	PROTO_OPT_MOUNTD_LISTEN_BACKLOG		15
2559361f55a5SMarcel Telka 	{"mountd_listen_backlog",
2560361f55a5SMarcel Telka 	    "mountd_listen_backlog", PROTO_OPT_MOUNTD_LISTEN_BACKLOG,
2561361f55a5SMarcel Telka 	    OPT_TYPE_NUMBER, 64, SVC_NFSD|SVC_MOUNTD, 1, INT32_MAX},
2562361f55a5SMarcel Telka #define	PROTO_OPT_MOUNTD_MAX_THREADS		16
2563361f55a5SMarcel Telka 	{"mountd_max_threads",
2564361f55a5SMarcel Telka 	    "mountd_max_threads", PROTO_OPT_MOUNTD_MAX_THREADS,
2565361f55a5SMarcel Telka 	    OPT_TYPE_NUMBER, 16, SVC_NFSD|SVC_MOUNTD, 1, INT32_MAX},
2566549ec3ffSdougm 	{NULL}
2567549ec3ffSdougm };
2568549ec3ffSdougm 
2569549ec3ffSdougm /*
2570549ec3ffSdougm  * the protoset holds the defined options so we don't have to read
2571549ec3ffSdougm  * them multiple times
2572549ec3ffSdougm  */
2573aed5d200Sdougm static sa_protocol_properties_t protoset;
2574549ec3ffSdougm 
2575549ec3ffSdougm static int
2576549ec3ffSdougm findprotoopt(char *name, int whichname)
2577549ec3ffSdougm {
2578549ec3ffSdougm 	int i;
2579549ec3ffSdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2580549ec3ffSdougm 		if (whichname == 1) {
2581549ec3ffSdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
2582549ec3ffSdougm 			return (i);
2583549ec3ffSdougm 		} else {
2584549ec3ffSdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
2585549ec3ffSdougm 				return (i);
2586549ec3ffSdougm 		}
2587549ec3ffSdougm 	}
2588549ec3ffSdougm 	return (-1);
2589549ec3ffSdougm }
2590549ec3ffSdougm 
2591549ec3ffSdougm /*
2592549ec3ffSdougm  * fixcaselower(str)
2593549ec3ffSdougm  *
2594549ec3ffSdougm  * convert a string to lower case (inplace).
2595549ec3ffSdougm  */
2596549ec3ffSdougm 
2597549ec3ffSdougm static void
2598549ec3ffSdougm fixcaselower(char *str)
2599549ec3ffSdougm {
2600549ec3ffSdougm 	while (*str) {
2601549ec3ffSdougm 		*str = tolower(*str);
2602549ec3ffSdougm 		str++;
2603549ec3ffSdougm 	}
2604549ec3ffSdougm }
2605549ec3ffSdougm 
2606549ec3ffSdougm /*
2607330ef417Sdougm  * skipwhitespace(str)
2608330ef417Sdougm  *
2609330ef417Sdougm  * Skip leading white space. It is assumed that it is called with a
2610330ef417Sdougm  * valid pointer.
2611330ef417Sdougm  */
2612330ef417Sdougm 
2613330ef417Sdougm static char *
2614330ef417Sdougm skipwhitespace(char *str)
2615330ef417Sdougm {
2616330ef417Sdougm 	while (*str && isspace(*str))
2617330ef417Sdougm 		str++;
2618330ef417Sdougm 
2619330ef417Sdougm 	return (str);
2620330ef417Sdougm }
2621330ef417Sdougm 
2622330ef417Sdougm /*
2623a3351425Sdougm  * extractprop()
2624a3351425Sdougm  *
2625a3351425Sdougm  * Extract the property and value out of the line and create the
2626a3351425Sdougm  * property in the optionset.
2627a3351425Sdougm  */
2628e7bab347Sdougm static int
2629a3351425Sdougm extractprop(char *name, char *value)
2630a3351425Sdougm {
2631a3351425Sdougm 	sa_property_t prop;
2632a3351425Sdougm 	int index;
2633e7bab347Sdougm 	int ret = SA_OK;
2634a3351425Sdougm 	/*
2635a3351425Sdougm 	 * Remove any leading
2636a3351425Sdougm 	 * white space.
2637a3351425Sdougm 	 */
2638a3351425Sdougm 	name = skipwhitespace(name);
2639a3351425Sdougm 
2640dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	index = findprotoopt(name, 1);
2641a3351425Sdougm 	if (index >= 0) {
2642a3351425Sdougm 		fixcaselower(name);
2643a3351425Sdougm 		prop = sa_create_property(proto_options[index].name, value);
2644a3351425Sdougm 		if (prop != NULL)
2645e7bab347Sdougm 			ret = sa_add_protocol_property(protoset, prop);
2646e7bab347Sdougm 		else
2647e7bab347Sdougm 			ret = SA_NO_MEMORY;
2648a3351425Sdougm 	}
2649e7bab347Sdougm 	return (ret);
2650a3351425Sdougm }
2651a3351425Sdougm 
2652dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States scf_type_t
2653dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States getscftype(int type)
2654549ec3ffSdougm {
2655dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	scf_type_t ret;
2656dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
2657dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	switch (type) {
2658dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	case OPT_TYPE_NUMBER:
2659dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		ret = SCF_TYPE_INTEGER;
2660dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	break;
2661dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	case OPT_TYPE_BOOLEAN:
2662dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		ret = SCF_TYPE_BOOLEAN;
2663dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	break;
2664dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	default:
2665dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		ret = SCF_TYPE_ASTRING;
2666dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	}
2667dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	return (ret);
2668dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States }
2669dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
2670dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States char *
2671dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States getsvcname(uint32_t svcs)
2672dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States {
2673dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	char *service;
2674dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	switch (svcs) {
2675dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_LOCKD:
2676dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = LOCKD;
2677dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2678dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_STATD:
2679dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = STATD;
2680dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2681dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_NFSD:
2682dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFSD;
2683dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2684dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_CLIENT:
2685dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFS_CLIENT_SVC;
2686dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2687dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_NFS4CBD:
2688dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFS4CBD;
2689dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2690dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_NFSMAPID:
2691dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFSMAPID;
2692dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2693dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_RQUOTAD:
2694dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = RQUOTAD;
2695dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2696dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_NFSLOGD:
2697dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFSLOGD;
2698dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2699dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_REPARSED:
2700dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = REPARSED;
2701dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2702dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		default:
2703dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFSD;
2704dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	}
2705dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	return (service);
2706dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States }
2707dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
2708dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States /*
2709dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * initprotofromsmf()
2710dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  *
2711dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * Read NFS SMF properties and add the defined values to the
2712dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * protoset.  Note that default values are known from the built in
2713dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * table in case SMF doesn't have a definition. Not having
2714dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * SMF properties is OK since we have builtin default
2715dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * values.
2716dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  */
2717dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States static int
2718dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States initprotofromsmf()
2719dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States {
2720dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	char name[PATH_MAX];
2721dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	char value[PATH_MAX];
2722dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	int ret = SA_OK, bufsz = 0, i;
2723549ec3ffSdougm 
2724549ec3ffSdougm 	protoset = sa_create_protocol_properties("nfs");
2725549ec3ffSdougm 	if (protoset != NULL) {
2726dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		for (i = 0; proto_options[i].tag != NULL; i++) {
2727dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			scf_type_t ptype;
2728dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			char *svc_name;
2729dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
2730dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			bzero(value, PATH_MAX);
2731dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			(void) strncpy(name, proto_options[i].name, PATH_MAX);
2732dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			/* Replace NULL with the correct instance */
2733dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			ptype = getscftype(proto_options[i].type);
2734dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			svc_name = getsvcname(proto_options[i].svcs);
2735dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			bufsz = PATH_MAX;
2736dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			ret = nfs_smf_get_prop(name, value,
2737dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			    (char *)DEFAULT_INSTANCE, ptype,
2738dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			    svc_name, &bufsz);
2739dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			if (ret == SA_OK) {
2740e7bab347Sdougm 				ret = extractprop(name, value);
2741549ec3ffSdougm 			}
2742549ec3ffSdougm 		}
2743e7bab347Sdougm 	} else {
2744e7bab347Sdougm 		ret = SA_NO_MEMORY;
2745549ec3ffSdougm 	}
2746dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
2747e7bab347Sdougm 	return (ret);
2748549ec3ffSdougm }
2749549ec3ffSdougm 
2750549ec3ffSdougm /*
2751a3351425Sdougm  * add_defaults()
2752549ec3ffSdougm  *
2753dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * Add the default values for any property not defined
2754dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * in NFS SMF repository.
2755dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * Values are set according to their defined types.
2756549ec3ffSdougm  */
2757549ec3ffSdougm 
2758549ec3ffSdougm static void
2759549ec3ffSdougm add_defaults()
2760549ec3ffSdougm {
2761549ec3ffSdougm 	int i;
2762549ec3ffSdougm 	char number[MAXDIGITS];
2763549ec3ffSdougm 
2764549ec3ffSdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2765549ec3ffSdougm 		sa_property_t prop;
2766a3351425Sdougm 		prop = sa_get_protocol_property(protoset,
2767a3351425Sdougm 		    proto_options[i].name);
2768549ec3ffSdougm 		if (prop == NULL) {
2769549ec3ffSdougm 			/* add the default value */
2770549ec3ffSdougm 			switch (proto_options[i].type) {
2771549ec3ffSdougm 			case OPT_TYPE_NUMBER:
2772549ec3ffSdougm 				(void) snprintf(number, sizeof (number), "%d",
2773549ec3ffSdougm 				    proto_options[i].defvalue.intval);
2774a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2775a3351425Sdougm 				    number);
2776549ec3ffSdougm 				break;
2777549ec3ffSdougm 
2778549ec3ffSdougm 			case OPT_TYPE_BOOLEAN:
2779549ec3ffSdougm 				prop = sa_create_property(proto_options[i].name,
2780549ec3ffSdougm 				    proto_options[i].defvalue.intval ?
2781549ec3ffSdougm 				    "true" : "false");
2782549ec3ffSdougm 				break;
2783549ec3ffSdougm 
2784549ec3ffSdougm 			case OPT_TYPE_ONOFF:
2785549ec3ffSdougm 				prop = sa_create_property(proto_options[i].name,
2786549ec3ffSdougm 				    proto_options[i].defvalue.intval ?
2787549ec3ffSdougm 				    "on" : "off");
2788549ec3ffSdougm 				break;
2789549ec3ffSdougm 
2790549ec3ffSdougm 			default:
2791549ec3ffSdougm 				/* treat as strings of zero length */
2792a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2793a3351425Sdougm 				    "");
2794549ec3ffSdougm 				break;
2795549ec3ffSdougm 			}
2796549ec3ffSdougm 			if (prop != NULL)
2797549ec3ffSdougm 				(void) sa_add_protocol_property(protoset, prop);
2798549ec3ffSdougm 		}
2799549ec3ffSdougm 	}
2800549ec3ffSdougm }
2801549ec3ffSdougm 
2802549ec3ffSdougm static void
2803549ec3ffSdougm free_protoprops()
2804549ec3ffSdougm {
2805aed5d200Sdougm 	if (protoset != NULL) {
2806549ec3ffSdougm 		xmlFreeNode(protoset);
2807aed5d200Sdougm 		protoset = NULL;
2808aed5d200Sdougm 	}
2809549ec3ffSdougm }
2810549ec3ffSdougm 
2811549ec3ffSdougm /*
2812549ec3ffSdougm  * nfs_init()
2813549ec3ffSdougm  *
2814549ec3ffSdougm  * Initialize the NFS plugin.
2815549ec3ffSdougm  */
2816549ec3ffSdougm 
2817549ec3ffSdougm static int
2818549ec3ffSdougm nfs_init()
2819549ec3ffSdougm {
2820549ec3ffSdougm 	int ret = SA_OK;
2821549ec3ffSdougm 
2822917c27c8Sdougm 	if (sa_plugin_ops.sa_init != nfs_init) {
2823549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2824549ec3ffSdougm 		    "NFS plugin not properly initialized\n"));
2825917c27c8Sdougm 		return (SA_CONFIG_ERR);
2826917c27c8Sdougm 	}
2827549ec3ffSdougm 
2828dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	ret = initprotofromsmf();
2829917c27c8Sdougm 	if (ret != SA_OK) {
2830917c27c8Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2831dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		    "NFS plugin problem with SMF repository: %s\n"),
2832917c27c8Sdougm 		    sa_errorstr(ret));
2833917c27c8Sdougm 		ret = SA_OK;
2834917c27c8Sdougm 	}
2835549ec3ffSdougm 	add_defaults();
2836549ec3ffSdougm 
2837549ec3ffSdougm 	return (ret);
2838549ec3ffSdougm }
2839549ec3ffSdougm 
2840549ec3ffSdougm /*
2841549ec3ffSdougm  * nfs_fini()
2842549ec3ffSdougm  *
2843549ec3ffSdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
2844549ec3ffSdougm  */
2845549ec3ffSdougm 
2846549ec3ffSdougm static void
2847549ec3ffSdougm nfs_fini()
2848549ec3ffSdougm {
2849549ec3ffSdougm 	free_protoprops();
2850549ec3ffSdougm }
2851549ec3ffSdougm 
2852549ec3ffSdougm /*
2853549ec3ffSdougm  * nfs_get_proto_set()
2854549ec3ffSdougm  *
2855549ec3ffSdougm  * Return an optionset with all the protocol specific properties in
2856549ec3ffSdougm  * it.
2857549ec3ffSdougm  */
2858549ec3ffSdougm 
2859549ec3ffSdougm static sa_protocol_properties_t
2860549ec3ffSdougm nfs_get_proto_set()
2861549ec3ffSdougm {
2862549ec3ffSdougm 	return (protoset);
2863549ec3ffSdougm }
2864549ec3ffSdougm 
2865549ec3ffSdougm /*
2866549ec3ffSdougm  * service_in_state(service, chkstate)
2867549ec3ffSdougm  *
2868549ec3ffSdougm  * Want to know if the specified service is in the desired state
2869549ec3ffSdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
2870549ec3ffSdougm  * isn't.
2871549ec3ffSdougm  */
2872549ec3ffSdougm static int
2873549ec3ffSdougm service_in_state(char *service, const char *chkstate)
2874549ec3ffSdougm {
2875549ec3ffSdougm 	char *state;
2876549ec3ffSdougm 	int ret = B_FALSE;
2877549ec3ffSdougm 
2878549ec3ffSdougm 	state = smf_get_state(service);
2879549ec3ffSdougm 	if (state != NULL) {
2880549ec3ffSdougm 		/* got the state so get the equality for the return value */
2881549ec3ffSdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2882549ec3ffSdougm 		free(state);
2883549ec3ffSdougm 	}
2884549ec3ffSdougm 	return (ret);
2885549ec3ffSdougm }
2886549ec3ffSdougm 
2887549ec3ffSdougm /*
2888549ec3ffSdougm  * restart_service(svcs)
2889549ec3ffSdougm  *
2890549ec3ffSdougm  * Walk through the bit mask of services that need to be restarted in
2891549ec3ffSdougm  * order to use the new property values. Some properties affect
2892549ec3ffSdougm  * multiple daemons. Should only restart a service if it is currently
2893549ec3ffSdougm  * enabled (online).
2894549ec3ffSdougm  */
2895549ec3ffSdougm 
2896549ec3ffSdougm static void
2897549ec3ffSdougm restart_service(uint32_t svcs)
2898549ec3ffSdougm {
2899549ec3ffSdougm 	uint32_t mask;
2900549ec3ffSdougm 	int ret;
2901549ec3ffSdougm 	char *service;
2902549ec3ffSdougm 
2903549ec3ffSdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
2904549ec3ffSdougm 		switch (svcs & mask) {
2905549ec3ffSdougm 		case SVC_LOCKD:
2906549ec3ffSdougm 			service = LOCKD;
2907549ec3ffSdougm 			break;
2908549ec3ffSdougm 		case SVC_STATD:
2909549ec3ffSdougm 			service = STATD;
2910549ec3ffSdougm 			break;
2911549ec3ffSdougm 		case SVC_NFSD:
2912549ec3ffSdougm 			service = NFSD;
2913549ec3ffSdougm 			break;
2914549ec3ffSdougm 		case SVC_MOUNTD:
2915549ec3ffSdougm 			service = MOUNTD;
2916549ec3ffSdougm 			break;
2917549ec3ffSdougm 		case SVC_NFS4CBD:
2918549ec3ffSdougm 			service = NFS4CBD;
2919549ec3ffSdougm 			break;
2920549ec3ffSdougm 		case SVC_NFSMAPID:
2921549ec3ffSdougm 			service = NFSMAPID;
2922549ec3ffSdougm 			break;
2923549ec3ffSdougm 		case SVC_RQUOTAD:
2924549ec3ffSdougm 			service = RQUOTAD;
2925549ec3ffSdougm 			break;
2926549ec3ffSdougm 		case SVC_NFSLOGD:
2927549ec3ffSdougm 			service = NFSLOGD;
2928549ec3ffSdougm 			break;
29292f172c55SRobert Thurlow 		case SVC_REPARSED:
29302f172c55SRobert Thurlow 			service = REPARSED;
29312f172c55SRobert Thurlow 			break;
2932dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		case SVC_CLIENT:
2933dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			service = NFS_CLIENT_SVC;
2934dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			break;
2935549ec3ffSdougm 		default:
2936549ec3ffSdougm 			continue;
2937549ec3ffSdougm 		}
2938549ec3ffSdougm 
2939549ec3ffSdougm 		/*
2940549ec3ffSdougm 		 * Only attempt to restart the service if it is
2941549ec3ffSdougm 		 * currently running. In the future, it may be
2942549ec3ffSdougm 		 * desirable to use smf_refresh_instance if the NFS
2943549ec3ffSdougm 		 * services ever implement the refresh method.
2944549ec3ffSdougm 		 */
2945549ec3ffSdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2946549ec3ffSdougm 			ret = smf_restart_instance(service);
2947549ec3ffSdougm 			/*
2948549ec3ffSdougm 			 * There are only a few SMF errors at this point, but
2949549ec3ffSdougm 			 * it is also possible that a bad value may have put
2950549ec3ffSdougm 			 * the service into maintenance if there wasn't an
2951549ec3ffSdougm 			 * SMF level error.
2952549ec3ffSdougm 			 */
2953549ec3ffSdougm 			if (ret != 0) {
2954549ec3ffSdougm 				(void) fprintf(stderr,
2955549ec3ffSdougm 				    dgettext(TEXT_DOMAIN,
2956549ec3ffSdougm 				    "%s failed to restart: %s\n"),
295703205638SMarcel Telka 				    service, scf_strerror(scf_error()));
2958549ec3ffSdougm 			} else {
2959549ec3ffSdougm 				/*
2960549ec3ffSdougm 				 * Check whether it has gone to "maintenance"
2961549ec3ffSdougm 				 * mode or not. Maintenance implies something
2962549ec3ffSdougm 				 * went wrong.
2963549ec3ffSdougm 				 */
2964a3351425Sdougm 				if (service_in_state(service,
2965a3351425Sdougm 				    SCF_STATE_STRING_MAINT)) {
2966549ec3ffSdougm 					(void) fprintf(stderr,
2967549ec3ffSdougm 					    dgettext(TEXT_DOMAIN,
2968549ec3ffSdougm 					    "%s failed to restart\n"),
2969549ec3ffSdougm 					    service);
2970549ec3ffSdougm 				}
2971549ec3ffSdougm 			}
2972549ec3ffSdougm 		}
2973549ec3ffSdougm 		svcs &= ~mask;
2974549ec3ffSdougm 	}
2975549ec3ffSdougm }
2976549ec3ffSdougm 
2977549ec3ffSdougm /*
2978549ec3ffSdougm  * nfs_minmax_check(name, value)
2979549ec3ffSdougm  *
2980549ec3ffSdougm  * Verify that the value for the property specified by index is valid
2981549ec3ffSdougm  * relative to the opposite value in the case of a min/max variable.
2982549ec3ffSdougm  * Currently, server_minvers/server_maxvers and
2983549ec3ffSdougm  * client_minvers/client_maxvers are the only ones to check.
2984549ec3ffSdougm  */
2985549ec3ffSdougm 
2986549ec3ffSdougm static int
2987549ec3ffSdougm nfs_minmax_check(int index, int value)
2988549ec3ffSdougm {
2989549ec3ffSdougm 	int val;
2990549ec3ffSdougm 	char *pval;
2991549ec3ffSdougm 	sa_property_t prop;
2992549ec3ffSdougm 	sa_optionset_t opts;
2993549ec3ffSdougm 	int ret = B_TRUE;
2994549ec3ffSdougm 
2995549ec3ffSdougm 	if (proto_options[index].other != NULL) {
2996549ec3ffSdougm 		/* have a property to compare against */
2997549ec3ffSdougm 		opts = nfs_get_proto_set();
2998549ec3ffSdougm 		prop = sa_get_property(opts, proto_options[index].other);
2999549ec3ffSdougm 		/*
3000549ec3ffSdougm 		 * If we don't find the property, assume default
3001549ec3ffSdougm 		 * values which will work since the max will be at the
3002549ec3ffSdougm 		 * max and the min at the min.
3003549ec3ffSdougm 		 */
3004549ec3ffSdougm 		if (prop != NULL) {
3005549ec3ffSdougm 			pval = sa_get_property_attr(prop, "value");
3006549ec3ffSdougm 			if (pval != NULL) {
3007549ec3ffSdougm 				val = strtoul(pval, NULL, 0);
3008a3351425Sdougm 				if (proto_options[index].compare ==
3009a3351425Sdougm 				    OPT_CMP_LE) {
3010549ec3ffSdougm 					ret = value <= val ? B_TRUE : B_FALSE;
3011a3351425Sdougm 				} else if (proto_options[index].compare ==
3012a3351425Sdougm 				    OPT_CMP_GE) {
3013549ec3ffSdougm 					ret = value >= val ? B_TRUE : B_FALSE;
3014549ec3ffSdougm 				}
3015fe1c642dSBill Krier 				sa_free_attr_string(pval);
3016549ec3ffSdougm 			}
3017549ec3ffSdougm 		}
3018549ec3ffSdougm 	}
3019549ec3ffSdougm 	return (ret);
3020549ec3ffSdougm }
3021549ec3ffSdougm 
3022549ec3ffSdougm /*
3023549ec3ffSdougm  * nfs_validate_proto_prop(index, name, value)
3024549ec3ffSdougm  *
3025da6c28aaSamw  * Verify that the property specified by name can take the new
3026549ec3ffSdougm  * value. This is a sanity check to prevent bad values getting into
3027549ec3ffSdougm  * the default files. All values need to be checked against what is
3028549ec3ffSdougm  * allowed by their defined type. If a type isn't explicitly defined
3029549ec3ffSdougm  * here, it is treated as a string.
3030549ec3ffSdougm  *
3031549ec3ffSdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
3032549ec3ffSdougm  * within the range specified and potentially against another property
3033549ec3ffSdougm  * value as well as specified in the proto_options members other and
3034549ec3ffSdougm  * compare.
3035549ec3ffSdougm  */
3036549ec3ffSdougm 
3037549ec3ffSdougm static int
3038549ec3ffSdougm nfs_validate_proto_prop(int index, char *name, char *value)
3039549ec3ffSdougm {
3040549ec3ffSdougm 	int ret = SA_OK;
3041549ec3ffSdougm 	char *cp;
3042549ec3ffSdougm #ifdef lint
3043549ec3ffSdougm 	name = name;
3044549ec3ffSdougm #endif
3045549ec3ffSdougm 	switch (proto_options[index].type) {
3046549ec3ffSdougm 	case OPT_TYPE_NUMBER:
3047549ec3ffSdougm 		if (!is_a_number(value))
3048549ec3ffSdougm 			ret = SA_BAD_VALUE;
3049549ec3ffSdougm 		else {
3050549ec3ffSdougm 			int val;
3051549ec3ffSdougm 			val = strtoul(value, NULL, 0);
3052549ec3ffSdougm 			if (val < proto_options[index].minval ||
3053549ec3ffSdougm 			    val > proto_options[index].maxval)
3054549ec3ffSdougm 				ret = SA_BAD_VALUE;
3055549ec3ffSdougm 			/*
3056549ec3ffSdougm 			 * For server_versmin/server_versmax and
3057549ec3ffSdougm 			 * client_versmin/client_versmax, the value of the
3058549ec3ffSdougm 			 * min(max) should be checked to be correct relative
3059549ec3ffSdougm 			 * to the current max(min).
3060549ec3ffSdougm 			 */
3061549ec3ffSdougm 			if (!nfs_minmax_check(index, val)) {
3062549ec3ffSdougm 				ret = SA_BAD_VALUE;
3063549ec3ffSdougm 			}
3064549ec3ffSdougm 		}
3065549ec3ffSdougm 		break;
3066549ec3ffSdougm 
3067549ec3ffSdougm 	case OPT_TYPE_DOMAIN:
3068549ec3ffSdougm 		/*
3069549ec3ffSdougm 		 * needs to be a qualified domain so will have at
3070549ec3ffSdougm 		 * least one period and other characters on either
3071549ec3ffSdougm 		 * side of it.  A zero length string is also allowed
3072549ec3ffSdougm 		 * and is the way to turn off the override.
3073549ec3ffSdougm 		 */
3074549ec3ffSdougm 		if (strlen(value) == 0)
3075549ec3ffSdougm 			break;
3076549ec3ffSdougm 		cp = strchr(value, '.');
3077549ec3ffSdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
3078549ec3ffSdougm 			ret = SA_BAD_VALUE;
3079549ec3ffSdougm 		break;
3080549ec3ffSdougm 
3081549ec3ffSdougm 	case OPT_TYPE_BOOLEAN:
3082549ec3ffSdougm 		if (strlen(value) == 0 ||
3083549ec3ffSdougm 		    strcasecmp(value, "true") == 0 ||
3084549ec3ffSdougm 		    strcmp(value, "1") == 0 ||
3085549ec3ffSdougm 		    strcasecmp(value, "false") == 0 ||
3086549ec3ffSdougm 		    strcmp(value, "0") == 0) {
3087549ec3ffSdougm 			ret = SA_OK;
3088549ec3ffSdougm 		} else {
3089549ec3ffSdougm 			ret = SA_BAD_VALUE;
3090549ec3ffSdougm 		}
3091549ec3ffSdougm 		break;
3092549ec3ffSdougm 
3093549ec3ffSdougm 	case OPT_TYPE_ONOFF:
3094549ec3ffSdougm 		if (strcasecmp(value, "on") != 0 &&
3095549ec3ffSdougm 		    strcasecmp(value, "off") != 0) {
3096549ec3ffSdougm 			ret = SA_BAD_VALUE;
3097549ec3ffSdougm 		}
3098549ec3ffSdougm 		break;
3099549ec3ffSdougm 
3100549ec3ffSdougm 	case OPT_TYPE_PROTOCOL:
3101917c27c8Sdougm 		if (strlen(value) != 0 &&
3102917c27c8Sdougm 		    strcasecmp(value, "all") != 0 &&
3103549ec3ffSdougm 		    strcasecmp(value, "tcp") != 0 &&
3104549ec3ffSdougm 		    strcasecmp(value, "udp") != 0)
3105549ec3ffSdougm 			ret = SA_BAD_VALUE;
3106549ec3ffSdougm 		break;
3107549ec3ffSdougm 
3108549ec3ffSdougm 	default:
3109549ec3ffSdougm 		/* treat as a string */
3110549ec3ffSdougm 		break;
3111549ec3ffSdougm 	}
3112549ec3ffSdougm 	return (ret);
3113549ec3ffSdougm }
3114549ec3ffSdougm 
3115549ec3ffSdougm /*
3116549ec3ffSdougm  * nfs_set_proto_prop(prop)
3117549ec3ffSdougm  *
3118549ec3ffSdougm  * check that prop is valid.
3119549ec3ffSdougm  */
3120549ec3ffSdougm 
3121549ec3ffSdougm static int
3122549ec3ffSdougm nfs_set_proto_prop(sa_property_t prop)
3123549ec3ffSdougm {
3124549ec3ffSdougm 	int ret = SA_OK;
3125549ec3ffSdougm 	char *name;
3126549ec3ffSdougm 	char *value;
3127549ec3ffSdougm 
3128549ec3ffSdougm 	name = sa_get_property_attr(prop, "type");
3129549ec3ffSdougm 	value = sa_get_property_attr(prop, "value");
3130549ec3ffSdougm 	if (name != NULL && value != NULL) {
3131dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		scf_type_t sctype;
3132dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		char *svc_name;
3133dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		char *instance = NULL;
3134549ec3ffSdougm 		int index = findprotoopt(name, 1);
3135dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
3136549ec3ffSdougm 		ret = nfs_validate_proto_prop(index, name, value);
3137dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		if (ret == SA_OK) {
3138dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			sctype = getscftype(proto_options[index].type);
3139dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			svc_name = getsvcname(proto_options[index].svcs);
3140dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			if (sctype == SCF_TYPE_BOOLEAN) {
3141dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				if (value != NULL)
3142dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 					sa_free_attr_string(value);
3143dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				if (string_to_boolean(value) == 0)
3144dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 					value = strdup("0");
3145dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				else
3146dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 					value = strdup("1");
3147dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			}
3148dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			ret = nfs_smf_set_prop(name, value, instance, sctype,
3149dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			    svc_name);
3150dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			if (ret == SA_OK) {
3151549ec3ffSdougm 				restart_service(proto_options[index].svcs);
3152dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			} else {
3153dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				(void) printf(dgettext(TEXT_DOMAIN,
3154dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				    "Cannot restart NFS services : %s\n"),
3155dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 				    sa_errorstr(ret));
3156dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			}
3157549ec3ffSdougm 		}
3158549ec3ffSdougm 	}
3159549ec3ffSdougm 	if (name != NULL)
3160549ec3ffSdougm 		sa_free_attr_string(name);
3161549ec3ffSdougm 	if (value != NULL)
3162549ec3ffSdougm 		sa_free_attr_string(value);
3163549ec3ffSdougm 	return (ret);
3164549ec3ffSdougm }
3165549ec3ffSdougm 
3166549ec3ffSdougm /*
3167549ec3ffSdougm  * nfs_get_status()
3168549ec3ffSdougm  *
3169549ec3ffSdougm  * What is the current status of the nfsd? We use the SMF state here.
3170549ec3ffSdougm  * Caller must free the returned value.
3171549ec3ffSdougm  */
3172549ec3ffSdougm 
3173549ec3ffSdougm static char *
3174549ec3ffSdougm nfs_get_status()
3175549ec3ffSdougm {
3176652601fbSRobert Mustacchi 	return (smf_get_state(NFSD));
3177549ec3ffSdougm }
3178549ec3ffSdougm 
3179549ec3ffSdougm /*
3180549ec3ffSdougm  * nfs_space_alias(alias)
3181549ec3ffSdougm  *
3182549ec3ffSdougm  * Lookup the space (security) name. If it is default, convert to the
3183549ec3ffSdougm  * real name.
3184549ec3ffSdougm  */
3185549ec3ffSdougm 
3186549ec3ffSdougm static char *
3187549ec3ffSdougm nfs_space_alias(char *space)
3188549ec3ffSdougm {
3189549ec3ffSdougm 	char *name = space;
3190549ec3ffSdougm 	seconfig_t secconf;
3191549ec3ffSdougm 
3192549ec3ffSdougm 	/*
3193549ec3ffSdougm 	 * Only the space named "default" is special. If it is used,
3194549ec3ffSdougm 	 * the default needs to be looked up and the real name used.
3195549ec3ffSdougm 	 * This is normally "sys" but could be changed.  We always
31965cb0d679SMarcel Telka 	 * change default to the real name.
3197549ec3ffSdougm 	 */
3198549ec3ffSdougm 	if (strcmp(space, "default") == 0 &&
3199549ec3ffSdougm 	    nfs_getseconfig_default(&secconf) == 0) {
3200549ec3ffSdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
3201549ec3ffSdougm 			name = secconf.sc_name;
3202549ec3ffSdougm 	}
3203549ec3ffSdougm 	return (strdup(name));
3204549ec3ffSdougm }
3205da6c28aaSamw 
3206da6c28aaSamw /*
3207da6c28aaSamw  * nfs_features()
3208da6c28aaSamw  *
3209da6c28aaSamw  * Return a mask of the features required.
3210da6c28aaSamw  */
3211da6c28aaSamw 
3212da6c28aaSamw static uint64_t
3213da6c28aaSamw nfs_features()
3214da6c28aaSamw {
32159e5da854Sdougm 	return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);
3216da6c28aaSamw }
3217