xref: /illumos-gate/usr/src/lib/libshare/nfs/libshare_nfs.c (revision aed5d200ef9c2e844519c27576f834981d696a46)
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 /*
23549ec3ffSdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24549ec3ffSdougm  * Use is subject to license terms.
25549ec3ffSdougm  */
26549ec3ffSdougm 
27549ec3ffSdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
28549ec3ffSdougm 
29549ec3ffSdougm /*
30549ec3ffSdougm  * NFS specific functions
31549ec3ffSdougm  */
32549ec3ffSdougm #include <stdio.h>
33549ec3ffSdougm #include <string.h>
34549ec3ffSdougm #include <ctype.h>
35549ec3ffSdougm #include <stdlib.h>
36549ec3ffSdougm #include <unistd.h>
37549ec3ffSdougm #include <zone.h>
38549ec3ffSdougm #include <errno.h>
39549ec3ffSdougm #include <locale.h>
40549ec3ffSdougm #include <signal.h>
41549ec3ffSdougm #include "libshare.h"
42549ec3ffSdougm #include "libshare_impl.h"
43549ec3ffSdougm #include <nfs/export.h>
44549ec3ffSdougm #include <pwd.h>
45549ec3ffSdougm #include <limits.h>
46549ec3ffSdougm #include <libscf.h>
47549ec3ffSdougm #include "nfslog_config.h"
48549ec3ffSdougm #include "nfslogtab.h"
49549ec3ffSdougm #include "libshare_nfs.h"
50549ec3ffSdougm #include <rpcsvc/daemon_utils.h>
51549ec3ffSdougm #include <nfs/nfs.h>
52ecd6cf80Smarks #include <nfs/nfssys.h>
53549ec3ffSdougm 
54549ec3ffSdougm /* should really be in some global place */
55549ec3ffSdougm #define	DEF_WIN	30000
56549ec3ffSdougm #define	OPT_CHUNK	1024
57549ec3ffSdougm 
58549ec3ffSdougm int debug = 0;
59549ec3ffSdougm 
60ecd6cf80Smarks #define	NFS_SERVER_SVC	"svc:/network/nfs/server:default"
61549ec3ffSdougm 
62549ec3ffSdougm /* internal functions */
63549ec3ffSdougm static int nfs_init();
64549ec3ffSdougm static void nfs_fini();
65549ec3ffSdougm static int nfs_enable_share(sa_share_t);
66ecd6cf80Smarks static int nfs_disable_share(sa_share_t, char *);
67549ec3ffSdougm static int nfs_validate_property(sa_property_t, sa_optionset_t);
68549ec3ffSdougm static int nfs_validate_security_mode(char *);
69549ec3ffSdougm static int nfs_is_security_opt(char *);
70549ec3ffSdougm static int nfs_parse_legacy_options(sa_group_t, char *);
71549ec3ffSdougm static char *nfs_format_options(sa_group_t, int);
72549ec3ffSdougm static int nfs_set_proto_prop(sa_property_t);
73549ec3ffSdougm static sa_protocol_properties_t nfs_get_proto_set();
74549ec3ffSdougm static char *nfs_get_status();
75549ec3ffSdougm static char *nfs_space_alias(char *);
76549ec3ffSdougm 
77549ec3ffSdougm /*
78549ec3ffSdougm  * ops vector that provides the protocol specific info and operations
79549ec3ffSdougm  * for share management.
80549ec3ffSdougm  */
81549ec3ffSdougm 
82549ec3ffSdougm struct sa_plugin_ops sa_plugin_ops = {
83549ec3ffSdougm 	SA_PLUGIN_VERSION,
84549ec3ffSdougm 	"nfs",
85549ec3ffSdougm 	nfs_init,
86549ec3ffSdougm 	nfs_fini,
87549ec3ffSdougm 	nfs_enable_share,
88549ec3ffSdougm 	nfs_disable_share,
89549ec3ffSdougm 	nfs_validate_property,
90549ec3ffSdougm 	nfs_validate_security_mode,
91549ec3ffSdougm 	nfs_is_security_opt,
92549ec3ffSdougm 	nfs_parse_legacy_options,
93549ec3ffSdougm 	nfs_format_options,
94549ec3ffSdougm 	nfs_set_proto_prop,
95549ec3ffSdougm 	nfs_get_proto_set,
96549ec3ffSdougm 	nfs_get_status,
97549ec3ffSdougm 	nfs_space_alias,
98549ec3ffSdougm 	NULL,
99549ec3ffSdougm 	NULL
100549ec3ffSdougm };
101549ec3ffSdougm 
102549ec3ffSdougm /*
103549ec3ffSdougm  * list of support services needed
104549ec3ffSdougm  * defines should come from head/rpcsvc/daemon_utils.h
105549ec3ffSdougm  */
106549ec3ffSdougm 
107549ec3ffSdougm static char *service_list_default[] =
108549ec3ffSdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL };
109549ec3ffSdougm static char *service_list_logging[] =
110549ec3ffSdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL };
111549ec3ffSdougm 
112549ec3ffSdougm /*
113549ec3ffSdougm  * option definitions.  Make sure to keep the #define for the option
114549ec3ffSdougm  * index just before the entry it is the index for. Changing the order
115549ec3ffSdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
116549ec3ffSdougm  * line that includes the SHOPT_RW and OPT_RW entries.
117549ec3ffSdougm  */
118549ec3ffSdougm 
119549ec3ffSdougm struct option_defs optdefs[] = {
120549ec3ffSdougm #define	OPT_RO		0
121549ec3ffSdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
122549ec3ffSdougm #define	OPT_RW		1
123549ec3ffSdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
124549ec3ffSdougm #define	OPT_ROOT	2
125549ec3ffSdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
126549ec3ffSdougm #define	OPT_SECURE	3
127549ec3ffSdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
128549ec3ffSdougm #define	OPT_ANON	4
129549ec3ffSdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
130549ec3ffSdougm #define	OPT_WINDOW	5
131549ec3ffSdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
132549ec3ffSdougm #define	OPT_NOSUID	6
133549ec3ffSdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
134549ec3ffSdougm #define	OPT_ACLOK	7
135549ec3ffSdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
136549ec3ffSdougm #define	OPT_NOSUB	8
137549ec3ffSdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
138549ec3ffSdougm #define	OPT_SEC		9
139549ec3ffSdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
140549ec3ffSdougm #define	OPT_PUBLIC	10
141549ec3ffSdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
142549ec3ffSdougm #define	OPT_INDEX	11
143549ec3ffSdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
144549ec3ffSdougm #define	OPT_LOG		12
145549ec3ffSdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
146549ec3ffSdougm #define	OPT_CKSUM	13
147549ec3ffSdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
148549ec3ffSdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
149549ec3ffSdougm #define	OPT_VOLFH	14
150549ec3ffSdougm 	{SHOPT_VOLFH, OPT_VOLFH},
151549ec3ffSdougm #endif /* VOLATILE_FH_TEST */
152549ec3ffSdougm 	NULL
153549ec3ffSdougm };
154549ec3ffSdougm 
155549ec3ffSdougm /*
156549ec3ffSdougm  * list of properties that are related to security flavors.
157549ec3ffSdougm  */
158549ec3ffSdougm static char *seclist[] = {
159549ec3ffSdougm 	SHOPT_RO,
160549ec3ffSdougm 	SHOPT_RW,
161549ec3ffSdougm 	SHOPT_ROOT,
162549ec3ffSdougm 	SHOPT_WINDOW,
163549ec3ffSdougm 	NULL
164549ec3ffSdougm };
165549ec3ffSdougm 
166549ec3ffSdougm /* structure for list of securities */
167549ec3ffSdougm struct securities {
168549ec3ffSdougm 	sa_security_t security;
169549ec3ffSdougm 	struct securities *next;
170549ec3ffSdougm };
171549ec3ffSdougm 
172549ec3ffSdougm /*
173549ec3ffSdougm  * findopt(name)
174549ec3ffSdougm  *
175549ec3ffSdougm  * Lookup option "name" in the option table and return the table
176549ec3ffSdougm  * index.
177549ec3ffSdougm  */
178549ec3ffSdougm 
179549ec3ffSdougm static int
180549ec3ffSdougm findopt(char *name)
181549ec3ffSdougm {
182549ec3ffSdougm 	int i;
183549ec3ffSdougm 	if (name != NULL) {
184549ec3ffSdougm 		for (i = 0; optdefs[i].tag != NULL; i++) {
185549ec3ffSdougm 			if (strcmp(optdefs[i].tag, name) == 0)
186549ec3ffSdougm 				return (i);
187549ec3ffSdougm 		}
188549ec3ffSdougm 	}
189549ec3ffSdougm 	return (-1);
190549ec3ffSdougm }
191549ec3ffSdougm 
192549ec3ffSdougm /*
193549ec3ffSdougm  * gettype(name)
194549ec3ffSdougm  *
195549ec3ffSdougm  * Return the type of option "name".
196549ec3ffSdougm  */
197549ec3ffSdougm 
198549ec3ffSdougm static int
199549ec3ffSdougm gettype(char *name)
200549ec3ffSdougm {
201549ec3ffSdougm 	int optdef;
202549ec3ffSdougm 
203549ec3ffSdougm 	optdef = findopt(name);
204549ec3ffSdougm 	if (optdef != -1)
205549ec3ffSdougm 		return (optdefs[optdef].type);
206549ec3ffSdougm 	return (OPT_TYPE_ANY);
207549ec3ffSdougm }
208549ec3ffSdougm 
209549ec3ffSdougm /*
210549ec3ffSdougm  * nfs_validate_security_mode(mode)
211549ec3ffSdougm  *
212549ec3ffSdougm  * is the specified mode string a valid one for use with NFS?
213549ec3ffSdougm  */
214549ec3ffSdougm 
215549ec3ffSdougm static int
216549ec3ffSdougm nfs_validate_security_mode(char *mode)
217549ec3ffSdougm {
218549ec3ffSdougm 	seconfig_t secinfo;
219549ec3ffSdougm 	int err;
220549ec3ffSdougm 
221549ec3ffSdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
222549ec3ffSdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
223549ec3ffSdougm 	if (err == SC_NOERROR)
224549ec3ffSdougm 		return (1);
225549ec3ffSdougm 	return (0);
226549ec3ffSdougm }
227549ec3ffSdougm 
228549ec3ffSdougm /*
229549ec3ffSdougm  * nfs_is_security_opt(tok)
230549ec3ffSdougm  *
231549ec3ffSdougm  * check to see if tok represents an option that is only valid in some
232549ec3ffSdougm  * security flavor.
233549ec3ffSdougm  */
234549ec3ffSdougm 
235549ec3ffSdougm static int
236549ec3ffSdougm nfs_is_security_opt(char *tok)
237549ec3ffSdougm {
238549ec3ffSdougm 	int i;
239549ec3ffSdougm 
240549ec3ffSdougm 	for (i = 0; seclist[i] != NULL; i++) {
241549ec3ffSdougm 		if (strcmp(tok, seclist[i]) == 0)
242549ec3ffSdougm 			return (1);
243549ec3ffSdougm 	}
244549ec3ffSdougm 	return (0);
245549ec3ffSdougm }
246549ec3ffSdougm 
247549ec3ffSdougm /*
248549ec3ffSdougm  * find_security(seclist, sec)
249549ec3ffSdougm  *
250549ec3ffSdougm  * Walk the current list of security flavors and return true if it is
251549ec3ffSdougm  * present, else return false.
252549ec3ffSdougm  */
253549ec3ffSdougm 
254549ec3ffSdougm static int
255549ec3ffSdougm find_security(struct securities *seclist, sa_security_t sec)
256549ec3ffSdougm {
257549ec3ffSdougm 	while (seclist != NULL) {
258549ec3ffSdougm 		if (seclist->security == sec)
259549ec3ffSdougm 			return (1);
260549ec3ffSdougm 		seclist = seclist->next;
261549ec3ffSdougm 	}
262549ec3ffSdougm 	return (0);
263549ec3ffSdougm }
264549ec3ffSdougm 
265549ec3ffSdougm /*
266549ec3ffSdougm  * make_security_list(group, securitymodes, proto)
267549ec3ffSdougm  *	go through the list of securitymodes and add them to the
268549ec3ffSdougm  *	group's list of security optionsets. We also keep a list of
269549ec3ffSdougm  *	those optionsets so we don't have to find them later. All of
270549ec3ffSdougm  *	these will get copies of the same properties.
271549ec3ffSdougm  */
272549ec3ffSdougm 
273549ec3ffSdougm static struct securities *
274549ec3ffSdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
275549ec3ffSdougm {
276549ec3ffSdougm 	char *tok, *next = NULL;
277549ec3ffSdougm 	struct securities *curp, *headp = NULL, *prev;
278549ec3ffSdougm 	sa_security_t check;
279549ec3ffSdougm 	int freetok = 0;
280549ec3ffSdougm 
281549ec3ffSdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
282549ec3ffSdougm 		next = strchr(tok, ':');
283549ec3ffSdougm 		if (next != NULL)
284549ec3ffSdougm 			*next++ = '\0';
285549ec3ffSdougm 		if (strcmp(tok, "default") == 0) {
286549ec3ffSdougm 			/* resolve default into the real type */
287549ec3ffSdougm 			tok = nfs_space_alias(tok);
288549ec3ffSdougm 			freetok = 1;
289549ec3ffSdougm 		}
290549ec3ffSdougm 		check = sa_get_security(group, tok, proto);
291549ec3ffSdougm 
292549ec3ffSdougm 		/* add to the security list if it isn't there already */
293549ec3ffSdougm 		if (check == NULL || !find_security(headp, check)) {
294549ec3ffSdougm 			curp = (struct securities *)calloc(1,
295549ec3ffSdougm 			    sizeof (struct securities));
296549ec3ffSdougm 			if (curp != NULL) {
297549ec3ffSdougm 				if (check == NULL) {
298a3351425Sdougm 					curp->security = sa_create_security(
299a3351425Sdougm 					    group, tok, proto);
300549ec3ffSdougm 				} else {
301549ec3ffSdougm 					curp->security = check;
302549ec3ffSdougm 				}
303549ec3ffSdougm 				/*
304549ec3ffSdougm 				 * note that the first time through the loop,
305549ec3ffSdougm 				 * headp will be NULL and prev will be
306549ec3ffSdougm 				 * undefined.  Since headp is NULL, we set
307549ec3ffSdougm 				 * both it and prev to the curp (first
308549ec3ffSdougm 				 * structure to be allocated).
309549ec3ffSdougm 				 *
310549ec3ffSdougm 				 * later passes through the loop will have
311549ec3ffSdougm 				 * headp not being NULL and prev will be used
312549ec3ffSdougm 				 * to allocate at the end of the list.
313549ec3ffSdougm 				 */
314549ec3ffSdougm 				if (headp == NULL) {
315549ec3ffSdougm 					headp = curp;
316549ec3ffSdougm 					prev = curp;
317549ec3ffSdougm 				} else {
318549ec3ffSdougm 					prev->next = curp;
319549ec3ffSdougm 					prev = curp;
320549ec3ffSdougm 				}
321549ec3ffSdougm 			}
322549ec3ffSdougm 		}
323549ec3ffSdougm 
324549ec3ffSdougm 		if (freetok) {
325549ec3ffSdougm 			freetok = 0;
326549ec3ffSdougm 			sa_free_attr_string(tok);
327549ec3ffSdougm 		}
328549ec3ffSdougm 	}
329549ec3ffSdougm 	return (headp);
330549ec3ffSdougm }
331549ec3ffSdougm 
332549ec3ffSdougm static void
333549ec3ffSdougm free_security_list(struct securities *sec)
334549ec3ffSdougm {
335549ec3ffSdougm 	struct securities *next;
336549ec3ffSdougm 	if (sec != NULL) {
337549ec3ffSdougm 		for (next = sec->next; sec != NULL; sec = next) {
338549ec3ffSdougm 			next = sec->next;
339549ec3ffSdougm 			free(sec);
340549ec3ffSdougm 		}
341549ec3ffSdougm 	}
342549ec3ffSdougm }
343549ec3ffSdougm 
344549ec3ffSdougm /*
345549ec3ffSdougm  * nfs_alistcat(str1, str2, sep)
346549ec3ffSdougm  *
347549ec3ffSdougm  * concatenate str1 and str2 into a new string using sep as a separate
348549ec3ffSdougm  * character. If memory allocation fails, return NULL;
349549ec3ffSdougm  */
350549ec3ffSdougm 
351549ec3ffSdougm static char *
352549ec3ffSdougm nfs_alistcat(char *str1, char *str2, char sep)
353549ec3ffSdougm {
354549ec3ffSdougm 	char *newstr;
355549ec3ffSdougm 	size_t len;
356549ec3ffSdougm 
357549ec3ffSdougm 	len = strlen(str1) + strlen(str2) + 2;
358549ec3ffSdougm 	newstr = (char *)malloc(len);
359549ec3ffSdougm 	if (newstr != NULL)
360549ec3ffSdougm 		(void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
361549ec3ffSdougm 	return (newstr);
362549ec3ffSdougm }
363549ec3ffSdougm 
364549ec3ffSdougm /*
365549ec3ffSdougm  * add_security_prop(sec, name, value, persist)
366549ec3ffSdougm  *
367549ec3ffSdougm  * Add the property to the securities structure. This accumulates
368549ec3ffSdougm  * properties for as part of parsing legacy options.
369549ec3ffSdougm  */
370549ec3ffSdougm 
371549ec3ffSdougm static int
372549ec3ffSdougm add_security_prop(struct securities *sec, char *name, char *value,
373549ec3ffSdougm 			int persist, int iszfs)
374549ec3ffSdougm {
375549ec3ffSdougm 	sa_property_t prop;
376549ec3ffSdougm 	int ret = SA_OK;
377549ec3ffSdougm 
378549ec3ffSdougm 	for (; sec != NULL; sec = sec->next) {
379549ec3ffSdougm 		if (value == NULL) {
380a3351425Sdougm 			if (strcmp(name, SHOPT_RW) == 0 ||
381a3351425Sdougm 			    strcmp(name, SHOPT_RO) == 0)
382549ec3ffSdougm 				value = "*";
383549ec3ffSdougm 			else
384549ec3ffSdougm 				value = "true";
385549ec3ffSdougm 		}
386549ec3ffSdougm 
387549ec3ffSdougm 		/*
388549ec3ffSdougm 		 * Get the existing property, if it exists, so we can
389549ec3ffSdougm 		 * determine what to do with it. The ro/rw/root
390549ec3ffSdougm 		 * properties can be merged if multiple instances of
391549ec3ffSdougm 		 * these properies are given. For example, if "rw"
392549ec3ffSdougm 		 * exists with a value "host1" and a later token of
393549ec3ffSdougm 		 * rw="host2" is seen, the values are merged into a
394549ec3ffSdougm 		 * single rw="host1:host2".
395549ec3ffSdougm 		 */
396549ec3ffSdougm 		prop = sa_get_property(sec->security, name);
397549ec3ffSdougm 
398549ec3ffSdougm 		if (prop != NULL) {
399549ec3ffSdougm 			char *oldvalue;
400549ec3ffSdougm 			char *newvalue;
401549ec3ffSdougm 
402549ec3ffSdougm 			/*
403549ec3ffSdougm 			 * The security options of ro/rw/root might appear
404549ec3ffSdougm 			 * multiple times. If they do, the values need to be
405549ec3ffSdougm 			 * merged into an access list. If it was previously
406549ec3ffSdougm 			 * empty, the new value alone is added.
407549ec3ffSdougm 			 */
408549ec3ffSdougm 			oldvalue = sa_get_property_attr(prop, "value");
409549ec3ffSdougm 			if (oldvalue != NULL) {
410549ec3ffSdougm 				/*
411549ec3ffSdougm 				 * The general case is to concatenate the new
412549ec3ffSdougm 				 * value onto the old value for multiple
413549ec3ffSdougm 				 * rw(ro/root) properties. A special case
414549ec3ffSdougm 				 * exists when either the old or new is the
415549ec3ffSdougm 				 * "all" case. In the special case, if both
416549ec3ffSdougm 				 * are "all", then it is "all", else if one is
417549ec3ffSdougm 				 * an access-list, that replaces the "all".
418549ec3ffSdougm 				 */
419549ec3ffSdougm 				if (strcmp(oldvalue, "*") == 0) {
420549ec3ffSdougm 					/* Replace old value with new value. */
421549ec3ffSdougm 					newvalue = strdup(value);
422549ec3ffSdougm 				} else if (strcmp(value, "*") == 0) {
423a3351425Sdougm 					/*
424a3351425Sdougm 					 * Keep old value and ignore
425a3351425Sdougm 					 * the new value.
426a3351425Sdougm 					 */
427549ec3ffSdougm 					newvalue = NULL;
428549ec3ffSdougm 				} else {
429a3351425Sdougm 					/*
430a3351425Sdougm 					 * Make a new list of old plus new
431a3351425Sdougm 					 * access-list.
432a3351425Sdougm 					 */
433a3351425Sdougm 					newvalue = nfs_alistcat(oldvalue,
434a3351425Sdougm 					    value, ':');
435549ec3ffSdougm 				}
436549ec3ffSdougm 
437549ec3ffSdougm 				if (newvalue != NULL) {
438549ec3ffSdougm 					(void) sa_remove_property(prop);
439a3351425Sdougm 					prop = sa_create_property(name,
440a3351425Sdougm 					    newvalue);
441a3351425Sdougm 					ret = sa_add_property(sec->security,
442a3351425Sdougm 					    prop);
443549ec3ffSdougm 					free(newvalue);
444549ec3ffSdougm 				}
445549ec3ffSdougm 				if (oldvalue != NULL)
446549ec3ffSdougm 					sa_free_attr_string(oldvalue);
447549ec3ffSdougm 			}
448549ec3ffSdougm 		} else {
449549ec3ffSdougm 			prop = sa_create_property(name, value);
450549ec3ffSdougm 			ret = sa_add_property(sec->security, prop);
451549ec3ffSdougm 		}
452549ec3ffSdougm 		if (ret == SA_OK && !iszfs) {
453549ec3ffSdougm 			ret = sa_commit_properties(sec->security, !persist);
454549ec3ffSdougm 		}
455549ec3ffSdougm 	}
456549ec3ffSdougm 	return (ret);
457549ec3ffSdougm }
458549ec3ffSdougm 
459549ec3ffSdougm /*
460549ec3ffSdougm  * check to see if group/share is persistent.
461549ec3ffSdougm  */
462549ec3ffSdougm static int
463549ec3ffSdougm is_persistent(sa_group_t group)
464549ec3ffSdougm {
465549ec3ffSdougm 	char *type;
466549ec3ffSdougm 	int persist = 1;
467549ec3ffSdougm 
468549ec3ffSdougm 	type = sa_get_group_attr(group, "type");
469549ec3ffSdougm 	if (type != NULL && strcmp(type, "persist") != 0)
470549ec3ffSdougm 		persist = 0;
471549ec3ffSdougm 	if (type != NULL)
472549ec3ffSdougm 		sa_free_attr_string(type);
473549ec3ffSdougm 	return (persist);
474549ec3ffSdougm }
475549ec3ffSdougm 
476549ec3ffSdougm /*
477549ec3ffSdougm  * invalid_security(options)
478549ec3ffSdougm  *
479549ec3ffSdougm  * search option string for any invalid sec= type.
480549ec3ffSdougm  * return true (1) if any are not valid else false (0)
481549ec3ffSdougm  */
482549ec3ffSdougm static int
483549ec3ffSdougm invalid_security(char *options)
484549ec3ffSdougm {
485549ec3ffSdougm 	char *copy, *base, *token, *value;
486549ec3ffSdougm 	int ret = 0;
487549ec3ffSdougm 
488549ec3ffSdougm 	copy = strdup(options);
489549ec3ffSdougm 	token = base = copy;
490549ec3ffSdougm 	while (token != NULL && ret == 0) {
491549ec3ffSdougm 		token = strtok(base, ",");
492549ec3ffSdougm 		base = NULL;
493549ec3ffSdougm 		if (token != NULL) {
494549ec3ffSdougm 			value = strchr(token, '=');
495549ec3ffSdougm 			if (value != NULL)
496549ec3ffSdougm 				*value++ = '\0';
497549ec3ffSdougm 			if (strcmp(token, "sec") == 0) {
498a3351425Sdougm 				/* HAVE security flavors so check them */
499549ec3ffSdougm 				char *tok, *next;
500a3351425Sdougm 				for (next = NULL, tok = value; tok != NULL;
501a3351425Sdougm 				    tok = next) {
502549ec3ffSdougm 					next = strchr(tok, ':');
503549ec3ffSdougm 					if (next != NULL)
504549ec3ffSdougm 						*next++ = '\0';
505549ec3ffSdougm 					ret = !nfs_validate_security_mode(tok);
506549ec3ffSdougm 					if (ret)
507549ec3ffSdougm 						break;
508549ec3ffSdougm 				}
509549ec3ffSdougm 			}
510549ec3ffSdougm 		}
511549ec3ffSdougm 	}
512549ec3ffSdougm 	if (copy != NULL)
513549ec3ffSdougm 		free(copy);
514549ec3ffSdougm 	return (ret);
515549ec3ffSdougm }
516549ec3ffSdougm 
517549ec3ffSdougm /*
518549ec3ffSdougm  * nfs_parse_legacy_options(group, options)
519549ec3ffSdougm  *
520549ec3ffSdougm  * Parse the old style options into internal format and store on the
521549ec3ffSdougm  * specified group.  Group could be a share for full legacy support.
522549ec3ffSdougm  */
523549ec3ffSdougm 
524549ec3ffSdougm static int
525549ec3ffSdougm nfs_parse_legacy_options(sa_group_t group, char *options)
526549ec3ffSdougm {
527aba7a40bSdougm 	char *dup;
528549ec3ffSdougm 	char *base;
529549ec3ffSdougm 	char *token;
530549ec3ffSdougm 	sa_optionset_t optionset;
531549ec3ffSdougm 	struct securities *security_list = NULL;
532549ec3ffSdougm 	sa_property_t prop;
533549ec3ffSdougm 	int ret = SA_OK;
534549ec3ffSdougm 	int iszfs = 0;
535549ec3ffSdougm 	sa_group_t parent;
536549ec3ffSdougm 	int persist = 0;
537549ec3ffSdougm 	char *lasts;
538549ec3ffSdougm 
539549ec3ffSdougm 	/* do we have an existing optionset? */
540549ec3ffSdougm 	optionset = sa_get_optionset(group, "nfs");
541549ec3ffSdougm 	if (optionset == NULL) {
542549ec3ffSdougm 		/* didn't find existing optionset so create one */
543549ec3ffSdougm 		optionset = sa_create_optionset(group, "nfs");
544549ec3ffSdougm 	} else {
545549ec3ffSdougm 		/*
546549ec3ffSdougm 		 * have an existing optionset so we need to compare
547549ec3ffSdougm 		 * options in order to detect errors. For now, we
548549ec3ffSdougm 		 * assume that the first optionset is the correct one
549549ec3ffSdougm 		 * and the others will be the same. This needs to be
550549ec3ffSdougm 		 * fixed before the final code is ready.
551549ec3ffSdougm 		 */
552549ec3ffSdougm 		return (ret);
553549ec3ffSdougm 	}
554549ec3ffSdougm 
555549ec3ffSdougm 	if (strcmp(options, SHOPT_RW) == 0) {
556549ec3ffSdougm 		/*
557549ec3ffSdougm 		 * there is a special case of only the option "rw"
558549ec3ffSdougm 		 * being the default option. We don't have to do
559549ec3ffSdougm 		 * anything.
560549ec3ffSdougm 		 */
561549ec3ffSdougm 		return (ret);
562549ec3ffSdougm 	}
563549ec3ffSdougm 
564549ec3ffSdougm 	/*
565549ec3ffSdougm 	 * check if security types are present and validate them. If
566549ec3ffSdougm 	 * any are not legal, fail.
567549ec3ffSdougm 	 */
568549ec3ffSdougm 
569549ec3ffSdougm 	if (invalid_security(options)) {
570549ec3ffSdougm 		return (SA_INVALID_SECURITY);
571549ec3ffSdougm 	}
572549ec3ffSdougm 
573549ec3ffSdougm 	/*
574549ec3ffSdougm 	 * in order to not attempt to change ZFS properties unless
575549ec3ffSdougm 	 * absolutely necessary, we never do it in the legacy parsing.
576549ec3ffSdougm 	 */
577549ec3ffSdougm 	if (sa_is_share(group)) {
578549ec3ffSdougm 		char *zfs;
579549ec3ffSdougm 		parent = sa_get_parent_group(group);
580549ec3ffSdougm 		if (parent != NULL) {
581549ec3ffSdougm 			zfs = sa_get_group_attr(parent, "zfs");
582549ec3ffSdougm 			if (zfs != NULL) {
583549ec3ffSdougm 				sa_free_attr_string(zfs);
584549ec3ffSdougm 				iszfs++;
585549ec3ffSdougm 			}
586549ec3ffSdougm 		}
587549ec3ffSdougm 	} else {
588549ec3ffSdougm 		iszfs = sa_group_is_zfs(group);
589549ec3ffSdougm 	}
590549ec3ffSdougm 
591aba7a40bSdougm 	/* We need a copy of options for the next part. */
592aba7a40bSdougm 	dup = strdup(options);
593aba7a40bSdougm 	if (dup == NULL)
594aba7a40bSdougm 		return (SA_NO_MEMORY);
595aba7a40bSdougm 
596549ec3ffSdougm 	/*
597549ec3ffSdougm 	 * we need to step through each option in the string and then
598549ec3ffSdougm 	 * add either the option or the security option as needed. If
599549ec3ffSdougm 	 * this is not a persistent share, don't commit to the
600549ec3ffSdougm 	 * repository. If there is an error, we also want to abort the
601549ec3ffSdougm 	 * processing and report it.
602549ec3ffSdougm 	 */
603549ec3ffSdougm 	persist = is_persistent(group);
604549ec3ffSdougm 	base = dup;
605549ec3ffSdougm 	token = dup;
606549ec3ffSdougm 	lasts = NULL;
607549ec3ffSdougm 	while (token != NULL && ret == SA_OK) {
608549ec3ffSdougm 		ret = SA_OK;
609549ec3ffSdougm 		token = strtok_r(base, ",", &lasts);
610549ec3ffSdougm 		base = NULL;
611549ec3ffSdougm 		if (token != NULL) {
612549ec3ffSdougm 			char *value;
613549ec3ffSdougm 			/*
614549ec3ffSdougm 			 * if the option has a value, it will have an '=' to
615549ec3ffSdougm 			 * separate the name from the value. The following
616549ec3ffSdougm 			 * code will result in value != NULL and token
617549ec3ffSdougm 			 * pointing to just the name if there is a value.
618549ec3ffSdougm 			 */
619549ec3ffSdougm 			value = strchr(token, '=');
620549ec3ffSdougm 			if (value != NULL) {
621549ec3ffSdougm 				*value++ = '\0';
622549ec3ffSdougm 			}
623a3351425Sdougm 			if (strcmp(token, "sec") == 0 ||
624a3351425Sdougm 			    strcmp(token, "secure") == 0) {
625549ec3ffSdougm 				/*
626549ec3ffSdougm 				 * Once in security parsing, we only
627549ec3ffSdougm 				 * do security. We do need to move
628549ec3ffSdougm 				 * between the security node and the
629549ec3ffSdougm 				 * toplevel. The security tag goes on
630549ec3ffSdougm 				 * the root while the following ones
631549ec3ffSdougm 				 * go on the security.
632549ec3ffSdougm 				 */
633549ec3ffSdougm 				if (security_list != NULL) {
634a3351425Sdougm 					/*
635a3351425Sdougm 					 * have an old list so close it and
636a3351425Sdougm 					 * start the new
637a3351425Sdougm 					 */
638549ec3ffSdougm 					free_security_list(security_list);
639549ec3ffSdougm 				}
640549ec3ffSdougm 				if (strcmp(token, "secure") == 0) {
641549ec3ffSdougm 					value = "dh";
642549ec3ffSdougm 				} else {
643549ec3ffSdougm 					if (value == NULL) {
644549ec3ffSdougm 						ret = SA_SYNTAX_ERR;
645549ec3ffSdougm 						break;
646549ec3ffSdougm 					}
647549ec3ffSdougm 				}
648a3351425Sdougm 				security_list = make_security_list(group,
649a3351425Sdougm 				    value, "nfs");
650549ec3ffSdougm 			} else {
651549ec3ffSdougm 				/*
652549ec3ffSdougm 				 * Note that the "old" syntax allowed a
653549ec3ffSdougm 				 * default security model This must be
654549ec3ffSdougm 				 * accounted for and internally converted to
655549ec3ffSdougm 				 * "standard" security structure.
656549ec3ffSdougm 				 */
657549ec3ffSdougm 				if (nfs_is_security_opt(token)) {
658549ec3ffSdougm 					if (security_list == NULL) {
659549ec3ffSdougm 						/*
660a3351425Sdougm 						 * need to have a
661a3351425Sdougm 						 * security
662a3351425Sdougm 						 * option. This will
663a3351425Sdougm 						 * be "closed" when a
664a3351425Sdougm 						 * defined "sec="
665a3351425Sdougm 						 * option is
666a3351425Sdougm 						 * seen. This is
667a3351425Sdougm 						 * technically an
668a3351425Sdougm 						 * error but will be
669a3351425Sdougm 						 * allowed with
670a3351425Sdougm 						 * warning.
671549ec3ffSdougm 						 */
672a3351425Sdougm 						security_list =
673a3351425Sdougm 						    make_security_list(group,
674549ec3ffSdougm 						    "default",
675549ec3ffSdougm 						    "nfs");
676549ec3ffSdougm 					}
677549ec3ffSdougm 					if (security_list != NULL) {
678a3351425Sdougm 						ret = add_security_prop(
679a3351425Sdougm 						    security_list, token,
680a3351425Sdougm 						    value, persist, iszfs);
681549ec3ffSdougm 					} else {
682549ec3ffSdougm 						ret = SA_NO_MEMORY;
683549ec3ffSdougm 					}
684549ec3ffSdougm 				} else {
685549ec3ffSdougm 					/* regular options */
686549ec3ffSdougm 					if (value == NULL) {
687a3351425Sdougm 						if (strcmp(token, SHOPT_RW) ==
688a3351425Sdougm 						    0 || strcmp(token,
689a3351425Sdougm 						    SHOPT_RO) == 0) {
690549ec3ffSdougm 							value = "*";
691a3351425Sdougm 						} else {
692549ec3ffSdougm 							value = "global";
693a3351425Sdougm 							if (strcmp(token,
694a3351425Sdougm 							    SHOPT_LOG) != 0) {
695549ec3ffSdougm 								value = "true";
696549ec3ffSdougm 							}
697a3351425Sdougm 						}
698d6405362Sdougm 					}
699d6405362Sdougm 					/*
700d6405362Sdougm 					 * In all cases, create the
701d6405362Sdougm 					 * property specified. If the
702d6405362Sdougm 					 * value was NULL, the default
703d6405362Sdougm 					 * value will have been
704d6405362Sdougm 					 * substituted.
705d6405362Sdougm 					 */
706d6405362Sdougm 					prop = sa_create_property(token, value);
707d6405362Sdougm 					ret =  sa_add_property(optionset, prop);
708a3351425Sdougm 					if (ret != SA_OK)
709549ec3ffSdougm 						break;
710d6405362Sdougm 
711549ec3ffSdougm 					if (!iszfs) {
712a3351425Sdougm 						ret = sa_commit_properties(
713a3351425Sdougm 						    optionset, !persist);
714549ec3ffSdougm 					}
715549ec3ffSdougm 				}
716549ec3ffSdougm 			}
717549ec3ffSdougm 		}
718549ec3ffSdougm 	}
719549ec3ffSdougm 	if (security_list != NULL)
720549ec3ffSdougm 		free_security_list(security_list);
721aba7a40bSdougm 
722549ec3ffSdougm 	free(dup);
723549ec3ffSdougm 	return (ret);
724549ec3ffSdougm }
725549ec3ffSdougm 
726549ec3ffSdougm /*
727549ec3ffSdougm  * is_a_number(number)
728549ec3ffSdougm  *
729549ec3ffSdougm  * is the string a number in one of the forms we want to use?
730549ec3ffSdougm  */
731549ec3ffSdougm 
732549ec3ffSdougm static int
733549ec3ffSdougm is_a_number(char *number)
734549ec3ffSdougm {
735549ec3ffSdougm 	int ret = 1;
736549ec3ffSdougm 	int hex = 0;
737549ec3ffSdougm 
738549ec3ffSdougm 	if (strncmp(number, "0x", 2) == 0) {
739549ec3ffSdougm 		number += 2;
740549ec3ffSdougm 		hex = 1;
741a3351425Sdougm 	} else if (*number == '-') {
742549ec3ffSdougm 		number++; /* skip the minus */
743a3351425Sdougm 	}
744549ec3ffSdougm 	while (ret == 1 && *number != '\0') {
745549ec3ffSdougm 		if (hex) {
746549ec3ffSdougm 			ret = isxdigit(*number++);
747549ec3ffSdougm 		} else {
748549ec3ffSdougm 			ret = isdigit(*number++);
749549ec3ffSdougm 		}
750549ec3ffSdougm 	}
751549ec3ffSdougm 	return (ret);
752549ec3ffSdougm }
753549ec3ffSdougm 
754549ec3ffSdougm /*
755549ec3ffSdougm  * Look for the specified tag in the configuration file. If it is found,
756549ec3ffSdougm  * enable logging and set the logging configuration information for exp.
757549ec3ffSdougm  */
758549ec3ffSdougm static void
759549ec3ffSdougm configlog(struct exportdata *exp, char *tag)
760549ec3ffSdougm {
761549ec3ffSdougm 	nfsl_config_t *configlist = NULL, *configp;
762549ec3ffSdougm 	int error = 0;
763549ec3ffSdougm 	char globaltag[] = DEFAULTTAG;
764549ec3ffSdougm 
765549ec3ffSdougm 	/*
766549ec3ffSdougm 	 * Sends config errors to stderr
767549ec3ffSdougm 	 */
768549ec3ffSdougm 	nfsl_errs_to_syslog = B_FALSE;
769549ec3ffSdougm 
770549ec3ffSdougm 	/*
771549ec3ffSdougm 	 * get the list of configuration settings
772549ec3ffSdougm 	 */
773549ec3ffSdougm 	error = nfsl_getconfig_list(&configlist);
774549ec3ffSdougm 	if (error) {
775549ec3ffSdougm 		(void) fprintf(stderr,
776a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"),
777549ec3ffSdougm 		    strerror(error));
778549ec3ffSdougm 	}
779549ec3ffSdougm 
780549ec3ffSdougm 	if (tag == NULL)
781549ec3ffSdougm 		tag = globaltag;
782549ec3ffSdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
783549ec3ffSdougm 		nfsl_freeconfig_list(&configlist);
784549ec3ffSdougm 		(void) fprintf(stderr,
785a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag);
786549ec3ffSdougm 		/* bad configuration */
787549ec3ffSdougm 		error = ENOENT;
788549ec3ffSdougm 		goto err;
789549ec3ffSdougm 	}
790549ec3ffSdougm 
791549ec3ffSdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
792549ec3ffSdougm 		error = ENOMEM;
793549ec3ffSdougm 		goto out;
794549ec3ffSdougm 	}
795549ec3ffSdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
796549ec3ffSdougm 		error = ENOMEM;
797549ec3ffSdougm 		goto out;
798549ec3ffSdougm 	}
799549ec3ffSdougm 	exp->ex_flags |= EX_LOG;
800549ec3ffSdougm 	if (configp->nc_rpclogpath != NULL)
801549ec3ffSdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
802549ec3ffSdougm out:
803549ec3ffSdougm 	if (configlist != NULL)
804549ec3ffSdougm 		nfsl_freeconfig_list(&configlist);
805549ec3ffSdougm 
806549ec3ffSdougm err:
807549ec3ffSdougm 	if (error != 0) {
808549ec3ffSdougm 		if (exp->ex_flags != NULL)
809549ec3ffSdougm 			free(exp->ex_tag);
810549ec3ffSdougm 		if (exp->ex_log_buffer != NULL)
811549ec3ffSdougm 			free(exp->ex_log_buffer);
812549ec3ffSdougm 		(void) fprintf(stderr,
813a3351425Sdougm 		    dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"),
814549ec3ffSdougm 		    strerror(error));
815549ec3ffSdougm 	}
816549ec3ffSdougm }
817549ec3ffSdougm 
818549ec3ffSdougm /*
819549ec3ffSdougm  * fill_export_from_optionset(export, optionset)
820549ec3ffSdougm  *
821549ec3ffSdougm  * In order to share, we need to set all the possible general options
822549ec3ffSdougm  * into the export structure. Share info will be filled in by the
823549ec3ffSdougm  * caller. Various property values get turned into structure specific
824549ec3ffSdougm  * values.
825549ec3ffSdougm  */
826549ec3ffSdougm 
827549ec3ffSdougm static int
828549ec3ffSdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
829549ec3ffSdougm {
830549ec3ffSdougm 	sa_property_t option;
831549ec3ffSdougm 	int ret = SA_OK;
832549ec3ffSdougm 
833549ec3ffSdougm 	for (option = sa_get_property(optionset, NULL);
834549ec3ffSdougm 	    option != NULL; option = sa_get_next_property(option)) {
835549ec3ffSdougm 		char *name;
836549ec3ffSdougm 		char *value;
837549ec3ffSdougm 		uint32_t val;
838549ec3ffSdougm 
839549ec3ffSdougm 		/*
840549ec3ffSdougm 		 * since options may be set/reset multiple times, always do an
841549ec3ffSdougm 		 * explicit set or clear of the option. This allows defaults
842549ec3ffSdougm 		 * to be set and then the protocol specifici to override.
843549ec3ffSdougm 		 */
844549ec3ffSdougm 
845549ec3ffSdougm 		name = sa_get_property_attr(option, "type");
846549ec3ffSdougm 		value = sa_get_property_attr(option, "value");
847549ec3ffSdougm 		switch (findopt(name)) {
848549ec3ffSdougm 		case OPT_ANON:
849549ec3ffSdougm 			if (value != NULL && is_a_number(value)) {
850549ec3ffSdougm 				val = strtoul(value, NULL, 0);
851549ec3ffSdougm 			} else {
852549ec3ffSdougm 				struct passwd *pw;
853549ec3ffSdougm 				pw = getpwnam(value != NULL ? value : "nobody");
854549ec3ffSdougm 				if (pw != NULL) {
855549ec3ffSdougm 					val = pw->pw_uid;
856549ec3ffSdougm 				} else {
857549ec3ffSdougm 					val = UID_NOBODY;
858549ec3ffSdougm 				}
859549ec3ffSdougm 				endpwent();
860549ec3ffSdougm 			}
861549ec3ffSdougm 			export->ex_anon = val;
862549ec3ffSdougm 			break;
863549ec3ffSdougm 		case OPT_NOSUID:
864a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
865a3351425Sdougm 			    strcmp(value, "1") == 0))
866549ec3ffSdougm 				export->ex_flags |= EX_NOSUID;
867549ec3ffSdougm 			else
868549ec3ffSdougm 				export->ex_flags &= ~EX_NOSUID;
869549ec3ffSdougm 			break;
870549ec3ffSdougm 		case OPT_ACLOK:
871a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
872549ec3ffSdougm 			    strcmp(value, "1") == 0))
873549ec3ffSdougm 				export->ex_flags |= EX_ACLOK;
874549ec3ffSdougm 			else
875549ec3ffSdougm 				export->ex_flags &= ~EX_ACLOK;
876549ec3ffSdougm 			break;
877549ec3ffSdougm 		case OPT_NOSUB:
878a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
879a3351425Sdougm 			    strcmp(value, "1") == 0))
880549ec3ffSdougm 				export->ex_flags |= EX_NOSUB;
881549ec3ffSdougm 			else
882549ec3ffSdougm 				export->ex_flags &= ~EX_NOSUB;
883549ec3ffSdougm 			break;
884549ec3ffSdougm 		case OPT_PUBLIC:
885a3351425Sdougm 			if (value != NULL && (strcasecmp(value, "true") == 0 ||
886a3351425Sdougm 			    strcmp(value, "1") == 0))
887549ec3ffSdougm 				export->ex_flags |= EX_PUBLIC;
888549ec3ffSdougm 			else
889549ec3ffSdougm 				export->ex_flags &= ~EX_PUBLIC;
890549ec3ffSdougm 			break;
891549ec3ffSdougm 		case OPT_INDEX:
892a3351425Sdougm 			if (value != NULL && (strcmp(value, "..") == 0 ||
893a3351425Sdougm 			    strchr(value, '/') != NULL)) {
894549ec3ffSdougm 				/* this is an error */
895549ec3ffSdougm 				(void) printf(dgettext(TEXT_DOMAIN,
896549ec3ffSdougm 				    "NFS: index=\"%s\" not valid;"
897549ec3ffSdougm 				    "must be a filename.\n"),
898549ec3ffSdougm 				    value);
899549ec3ffSdougm 				break;
900549ec3ffSdougm 			}
901549ec3ffSdougm 			if (value != NULL && *value != '\0' &&
902549ec3ffSdougm 			    strcmp(value, ".") != 0) {
903549ec3ffSdougm 				/* valid index file string */
904549ec3ffSdougm 				if (export->ex_index != NULL) {
905549ec3ffSdougm 					/* left over from "default" */
906549ec3ffSdougm 					free(export->ex_index);
907549ec3ffSdougm 				}
908a3351425Sdougm 				/* remember to free */
909a3351425Sdougm 				export->ex_index = strdup(value);
910549ec3ffSdougm 				if (export->ex_index == NULL) {
911549ec3ffSdougm 					(void) printf(dgettext(TEXT_DOMAIN,
912549ec3ffSdougm 					    "NFS: out of memory setting "
913549ec3ffSdougm 					    "index property\n"));
914549ec3ffSdougm 					break;
915549ec3ffSdougm 				}
916549ec3ffSdougm 				export->ex_flags |= EX_INDEX;
917549ec3ffSdougm 			}
918549ec3ffSdougm 			break;
919549ec3ffSdougm 		case OPT_LOG:
920549ec3ffSdougm 			if (value == NULL)
921549ec3ffSdougm 				value = strdup("global");
922549ec3ffSdougm 			if (value != NULL)
923a3351425Sdougm 				configlog(export,
924a3351425Sdougm 				    strlen(value) ? value : "global");
925549ec3ffSdougm 			break;
926549ec3ffSdougm 		default:
927549ec3ffSdougm 			/* have a syntactic error */
928549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
929549ec3ffSdougm 			    "NFS: unrecognized option %s=%s\n"),
930549ec3ffSdougm 			    name, value != NULL ? value : "");
931549ec3ffSdougm 			break;
932549ec3ffSdougm 		}
933549ec3ffSdougm 		if (name != NULL)
934549ec3ffSdougm 			sa_free_attr_string(name);
935549ec3ffSdougm 		if (value != NULL)
936549ec3ffSdougm 			sa_free_attr_string(value);
937549ec3ffSdougm 	}
938549ec3ffSdougm 	return (ret);
939549ec3ffSdougm }
940549ec3ffSdougm 
941549ec3ffSdougm /*
942549ec3ffSdougm  * cleanup_export(export)
943549ec3ffSdougm  *
944549ec3ffSdougm  * Cleanup the allocated areas so we don't leak memory
945549ec3ffSdougm  */
946549ec3ffSdougm 
947549ec3ffSdougm static void
948549ec3ffSdougm cleanup_export(struct exportdata *export)
949549ec3ffSdougm {
950549ec3ffSdougm 	int i;
951549ec3ffSdougm 
952549ec3ffSdougm 	if (export->ex_index != NULL)
953549ec3ffSdougm 		free(export->ex_index);
954549ec3ffSdougm 	if (export->ex_secinfo != NULL) {
955549ec3ffSdougm 		for (i = 0; i < export->ex_seccnt; i++)
956a3351425Sdougm 			if (export->ex_secinfo[i].s_rootnames != NULL)
957549ec3ffSdougm 				free(export->ex_secinfo[i].s_rootnames);
958549ec3ffSdougm 		free(export->ex_secinfo);
959549ec3ffSdougm 	}
960549ec3ffSdougm }
961549ec3ffSdougm 
962549ec3ffSdougm /*
963549ec3ffSdougm  * Given a seconfig entry and a colon-separated
964549ec3ffSdougm  * list of names, allocate an array big enough
965549ec3ffSdougm  * to hold the root list, then convert each name to
966549ec3ffSdougm  * a principal name according to the security
967549ec3ffSdougm  * info and assign it to an array element.
968549ec3ffSdougm  * Return the array and its size.
969549ec3ffSdougm  */
970549ec3ffSdougm static caddr_t *
971549ec3ffSdougm get_rootnames(seconfig_t *sec, char *list, int *count)
972549ec3ffSdougm {
973549ec3ffSdougm 	caddr_t *a;
974549ec3ffSdougm 	int c, i;
975549ec3ffSdougm 	char *host, *p;
976549ec3ffSdougm 
977549ec3ffSdougm 	/*
978549ec3ffSdougm 	 * Count the number of strings in the list.
979549ec3ffSdougm 	 * This is the number of colon separators + 1.
980549ec3ffSdougm 	 */
981549ec3ffSdougm 	c = 1;
982549ec3ffSdougm 	for (p = list; *p; p++)
983549ec3ffSdougm 		if (*p == ':')
984549ec3ffSdougm 			c++;
985549ec3ffSdougm 	*count = c;
986549ec3ffSdougm 
987549ec3ffSdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
988549ec3ffSdougm 	if (a == NULL) {
989549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
990549ec3ffSdougm 		    "get_rootnames: no memory\n"));
991549ec3ffSdougm 	} else {
992549ec3ffSdougm 		for (i = 0; i < c; i++) {
993549ec3ffSdougm 			host = strtok(list, ":");
994549ec3ffSdougm 			if (!nfs_get_root_principal(sec, host, &a[i])) {
995549ec3ffSdougm 				free(a);
996549ec3ffSdougm 				a = NULL;
997549ec3ffSdougm 				break;
998549ec3ffSdougm 			}
999549ec3ffSdougm 			list = NULL;
1000549ec3ffSdougm 		}
1001549ec3ffSdougm 	}
1002549ec3ffSdougm 
1003549ec3ffSdougm 	return (a);
1004549ec3ffSdougm }
1005549ec3ffSdougm 
1006549ec3ffSdougm /*
1007549ec3ffSdougm  * fill_security_from_secopts(sp, secopts)
1008549ec3ffSdougm  *
1009549ec3ffSdougm  * Fill the secinfo structure from the secopts optionset.
1010549ec3ffSdougm  */
1011549ec3ffSdougm 
1012549ec3ffSdougm static int
1013549ec3ffSdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
1014549ec3ffSdougm {
1015549ec3ffSdougm 	sa_property_t prop;
1016549ec3ffSdougm 	char *type;
1017549ec3ffSdougm 	int longform;
1018549ec3ffSdougm 	int err = SC_NOERROR;
1019549ec3ffSdougm 
1020549ec3ffSdougm 	type = sa_get_security_attr(secopts, "sectype");
1021549ec3ffSdougm 	if (type != NULL) {
1022549ec3ffSdougm 		/* named security type needs secinfo to be filled in */
1023549ec3ffSdougm 		err = nfs_getseconfig_byname(type, &sp->s_secinfo);
1024549ec3ffSdougm 		sa_free_attr_string(type);
1025549ec3ffSdougm 		if (err != SC_NOERROR)
1026549ec3ffSdougm 			return (err);
1027549ec3ffSdougm 	} else {
1028549ec3ffSdougm 		/* default case */
1029549ec3ffSdougm 		err = nfs_getseconfig_default(&sp->s_secinfo);
1030549ec3ffSdougm 		if (err != SC_NOERROR)
1031549ec3ffSdougm 			return (err);
1032549ec3ffSdougm 	}
1033549ec3ffSdougm 
1034549ec3ffSdougm 	err = SA_OK;
1035549ec3ffSdougm 	for (prop = sa_get_property(secopts, NULL);
1036549ec3ffSdougm 	    prop != NULL && err == SA_OK;
1037549ec3ffSdougm 	    prop = sa_get_next_property(prop)) {
1038549ec3ffSdougm 		char *name;
1039549ec3ffSdougm 		char *value;
1040549ec3ffSdougm 
1041549ec3ffSdougm 		name = sa_get_property_attr(prop, "type");
1042549ec3ffSdougm 		value = sa_get_property_attr(prop, "value");
1043549ec3ffSdougm 
1044549ec3ffSdougm 		longform = value != NULL && strcmp(value, "*") != 0;
1045549ec3ffSdougm 
1046549ec3ffSdougm 		switch (findopt(name)) {
1047549ec3ffSdougm 		case OPT_RO:
1048549ec3ffSdougm 			sp->s_flags |= longform ? M_ROL : M_RO;
1049549ec3ffSdougm 			break;
1050549ec3ffSdougm 		case OPT_RW:
1051549ec3ffSdougm 			sp->s_flags |= longform ? M_RWL : M_RW;
1052549ec3ffSdougm 			break;
1053549ec3ffSdougm 		case OPT_ROOT:
1054549ec3ffSdougm 			sp->s_flags |= M_ROOT;
1055549ec3ffSdougm 			/*
1056549ec3ffSdougm 			 * if we are using AUTH_UNIX, handle like other things
1057549ec3ffSdougm 			 * such as RO/RW
1058549ec3ffSdougm 			 */
1059549ec3ffSdougm 			if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
1060549ec3ffSdougm 				continue;
1061549ec3ffSdougm 			/* not AUTH_UNIX */
1062549ec3ffSdougm 			if (value != NULL) {
1063a3351425Sdougm 				sp->s_rootnames = get_rootnames(&sp->s_secinfo,
1064a3351425Sdougm 				    value, &sp->s_rootcnt);
1065549ec3ffSdougm 				if (sp->s_rootnames == NULL) {
1066549ec3ffSdougm 					err = SA_BAD_VALUE;
1067a3351425Sdougm 					(void) fprintf(stderr,
1068a3351425Sdougm 					    dgettext(TEXT_DOMAIN,
1069549ec3ffSdougm 					    "Bad root list\n"));
1070549ec3ffSdougm 				}
1071549ec3ffSdougm 			}
1072549ec3ffSdougm 			break;
1073549ec3ffSdougm 		case OPT_WINDOW:
1074549ec3ffSdougm 			if (value != NULL) {
1075549ec3ffSdougm 				sp->s_window = atoi(value);
1076a3351425Sdougm 				/* just in case */
1077549ec3ffSdougm 				if (sp->s_window < 0)
1078a3351425Sdougm 					sp->s_window = DEF_WIN;
1079549ec3ffSdougm 			}
1080549ec3ffSdougm 			break;
1081549ec3ffSdougm 		default:
1082549ec3ffSdougm 			break;
1083549ec3ffSdougm 		}
1084549ec3ffSdougm 		if (name != NULL)
1085549ec3ffSdougm 			sa_free_attr_string(name);
1086549ec3ffSdougm 		if (value != NULL)
1087549ec3ffSdougm 			sa_free_attr_string(value);
1088549ec3ffSdougm 	}
1089549ec3ffSdougm 	/* if rw/ro options not set, use default of RW */
1090549ec3ffSdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
1091549ec3ffSdougm 		sp->s_flags |= M_RW;
1092549ec3ffSdougm 	return (err);
1093549ec3ffSdougm }
1094549ec3ffSdougm 
1095549ec3ffSdougm /*
1096549ec3ffSdougm  * This is for testing only
1097549ec3ffSdougm  * It displays the export structure that
1098549ec3ffSdougm  * goes into the kernel.
1099549ec3ffSdougm  */
1100549ec3ffSdougm static void
1101549ec3ffSdougm printarg(char *path, struct exportdata *ep)
1102549ec3ffSdougm {
1103549ec3ffSdougm 	int i, j;
1104549ec3ffSdougm 	struct secinfo *sp;
1105549ec3ffSdougm 
1106549ec3ffSdougm 	if (debug == 0)
1107549ec3ffSdougm 		return;
1108549ec3ffSdougm 
1109549ec3ffSdougm 	(void) printf("%s:\n", path);
1110549ec3ffSdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
1111549ec3ffSdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
1112549ec3ffSdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
1113549ec3ffSdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
1114549ec3ffSdougm 	if (ep->ex_flags & EX_NOSUID)
1115549ec3ffSdougm 		(void) printf("NOSUID ");
1116549ec3ffSdougm 	if (ep->ex_flags & EX_ACLOK)
1117549ec3ffSdougm 		(void) printf("ACLOK ");
1118549ec3ffSdougm 	if (ep->ex_flags & EX_PUBLIC)
1119549ec3ffSdougm 		(void) printf("PUBLIC ");
1120549ec3ffSdougm 	if (ep->ex_flags & EX_NOSUB)
1121549ec3ffSdougm 		(void) printf("NOSUB ");
1122549ec3ffSdougm 	if (ep->ex_flags & EX_LOG)
1123549ec3ffSdougm 		(void) printf("LOG ");
1124549ec3ffSdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
1125549ec3ffSdougm 		(void) printf("LOG_ALLOPS ");
1126549ec3ffSdougm 	if (ep->ex_flags == 0)
1127549ec3ffSdougm 		(void) printf("(none)");
1128549ec3ffSdougm 	(void) 	printf("\n");
1129549ec3ffSdougm 	if (ep->ex_flags & EX_LOG) {
1130549ec3ffSdougm 		(void) printf("\tex_log_buffer = %s\n",
1131549ec3ffSdougm 		    (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
1132549ec3ffSdougm 		(void) printf("\tex_tag = %s\n",
1133549ec3ffSdougm 		    (ep->ex_tag ? ep->ex_tag : "(NULL)"));
1134549ec3ffSdougm 	}
1135549ec3ffSdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
1136549ec3ffSdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
1137549ec3ffSdougm 	(void) printf("\n");
1138549ec3ffSdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
1139549ec3ffSdougm 		sp = &ep->ex_secinfo[i];
1140549ec3ffSdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
1141549ec3ffSdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
1142549ec3ffSdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
1143549ec3ffSdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
1144549ec3ffSdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
1145549ec3ffSdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
1146549ec3ffSdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
1147549ec3ffSdougm 		if (sp->s_flags == 0) (void) printf("(none)");
1148549ec3ffSdougm 		(void) printf("\n");
1149549ec3ffSdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
1150549ec3ffSdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
1151549ec3ffSdougm 		(void) fflush(stdout);
1152549ec3ffSdougm 		for (j = 0; j < sp->s_rootcnt; j++)
1153549ec3ffSdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
1154549ec3ffSdougm 			    sp->s_rootnames[j] : "<null>");
1155549ec3ffSdougm 		(void) printf("\n\n");
1156549ec3ffSdougm 	}
1157549ec3ffSdougm }
1158549ec3ffSdougm 
1159549ec3ffSdougm /*
1160549ec3ffSdougm  * count_security(opts)
1161549ec3ffSdougm  *
1162549ec3ffSdougm  * Count the number of security types (flavors). The optionset has
1163549ec3ffSdougm  * been populated with the security flavors as a holding mechanism.
1164549ec3ffSdougm  * We later use this number to allocate data structures.
1165549ec3ffSdougm  */
1166549ec3ffSdougm 
1167549ec3ffSdougm static int
1168549ec3ffSdougm count_security(sa_optionset_t opts)
1169549ec3ffSdougm {
1170549ec3ffSdougm 	int count = 0;
1171549ec3ffSdougm 	sa_property_t prop;
1172549ec3ffSdougm 	if (opts != NULL) {
1173549ec3ffSdougm 		for (prop = sa_get_property(opts, NULL); prop != NULL;
1174549ec3ffSdougm 		    prop = sa_get_next_property(prop)) {
1175549ec3ffSdougm 			count++;
1176549ec3ffSdougm 		}
1177549ec3ffSdougm 	}
1178549ec3ffSdougm 	return (count);
1179549ec3ffSdougm }
1180549ec3ffSdougm 
1181549ec3ffSdougm /*
1182549ec3ffSdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1183549ec3ffSdougm  *
1184549ec3ffSdougm  * provides a mechanism to format NFS properties into legacy output
1185549ec3ffSdougm  * format. If the buffer would overflow, it is reallocated and grown
1186549ec3ffSdougm  * as appropriate. Special cases of converting internal form of values
1187549ec3ffSdougm  * to those used by "share" are done. this function does one property
1188549ec3ffSdougm  * at a time.
1189549ec3ffSdougm  */
1190549ec3ffSdougm 
1191*aed5d200Sdougm static int
1192549ec3ffSdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1193549ec3ffSdougm 			sa_property_t prop, int sep)
1194549ec3ffSdougm {
1195549ec3ffSdougm 	char *name;
1196549ec3ffSdougm 	char *value;
1197549ec3ffSdougm 	int curlen;
1198549ec3ffSdougm 	char *buff = *rbuff;
1199549ec3ffSdougm 	size_t buffsize = *rbuffsize;
1200*aed5d200Sdougm 	int printed = B_FALSE;
1201549ec3ffSdougm 
1202549ec3ffSdougm 	name = sa_get_property_attr(prop, "type");
1203549ec3ffSdougm 	value = sa_get_property_attr(prop, "value");
1204549ec3ffSdougm 	if (buff != NULL)
1205549ec3ffSdougm 		curlen = strlen(buff);
1206549ec3ffSdougm 	else
1207549ec3ffSdougm 		curlen = 0;
1208549ec3ffSdougm 	if (name != NULL) {
1209549ec3ffSdougm 		int len;
1210549ec3ffSdougm 		len = strlen(name) + sep;
1211549ec3ffSdougm 
1212549ec3ffSdougm 		/*
1213549ec3ffSdougm 		 * A future RFE would be to replace this with more
1214549ec3ffSdougm 		 * generic code and to possibly handle more types.
1215549ec3ffSdougm 		 */
1216549ec3ffSdougm 		switch (gettype(name)) {
1217549ec3ffSdougm 		case OPT_TYPE_BOOLEAN:
1218*aed5d200Sdougm 			/*
1219*aed5d200Sdougm 			 * For NFS, boolean value of FALSE means it
1220*aed5d200Sdougm 			 * doesn't show up in the option list at all.
1221*aed5d200Sdougm 			 */
1222a3351425Sdougm 			if (value != NULL && strcasecmp(value, "false") == 0)
1223*aed5d200Sdougm 				goto skip;
1224*aed5d200Sdougm 			if (value != NULL) {
1225549ec3ffSdougm 				sa_free_attr_string(value);
1226549ec3ffSdougm 				value = NULL;
1227*aed5d200Sdougm 			}
1228549ec3ffSdougm 			break;
1229549ec3ffSdougm 		case OPT_TYPE_ACCLIST:
1230549ec3ffSdougm 			if (value != NULL && strcmp(value, "*") == 0) {
1231549ec3ffSdougm 				sa_free_attr_string(value);
1232549ec3ffSdougm 				value = NULL;
1233549ec3ffSdougm 			} else {
1234549ec3ffSdougm 				if (value != NULL)
1235549ec3ffSdougm 					len += 1 + strlen(value);
1236549ec3ffSdougm 			}
1237549ec3ffSdougm 			break;
1238549ec3ffSdougm 		case OPT_TYPE_LOGTAG:
1239549ec3ffSdougm 			if (value != NULL && strlen(value) == 0) {
1240549ec3ffSdougm 				sa_free_attr_string(value);
1241549ec3ffSdougm 				value = NULL;
1242549ec3ffSdougm 			} else {
1243549ec3ffSdougm 				if (value != NULL)
1244549ec3ffSdougm 					len += 1 + strlen(value);
1245549ec3ffSdougm 			}
1246549ec3ffSdougm 			break;
1247549ec3ffSdougm 		default:
1248549ec3ffSdougm 			if (value != NULL)
1249549ec3ffSdougm 				len += 1 + strlen(value);
1250549ec3ffSdougm 			break;
1251549ec3ffSdougm 		}
1252549ec3ffSdougm 		while (buffsize <= (curlen + len)) {
1253549ec3ffSdougm 			/* need more room */
1254549ec3ffSdougm 			buffsize += incr;
1255549ec3ffSdougm 			buff = realloc(buff, buffsize);
1256549ec3ffSdougm 			if (buff == NULL) {
1257549ec3ffSdougm 				/* realloc failed so free everything */
1258549ec3ffSdougm 				if (*rbuff != NULL)
1259549ec3ffSdougm 					free(*rbuff);
1260549ec3ffSdougm 			}
1261549ec3ffSdougm 			*rbuff = buff;
1262549ec3ffSdougm 			*rbuffsize = buffsize;
1263549ec3ffSdougm 			if (buff == NULL)
1264*aed5d200Sdougm 				goto skip;
1265*aed5d200Sdougm 
1266*aed5d200Sdougm 		}
1267*aed5d200Sdougm 
1268*aed5d200Sdougm 		if (buff == NULL)
1269*aed5d200Sdougm 			goto skip;
1270*aed5d200Sdougm 
1271a3351425Sdougm 		if (value == NULL) {
1272549ec3ffSdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1273549ec3ffSdougm 			    "%s%s", sep ? "," : "",
1274549ec3ffSdougm 			    name, value != NULL ? value : "");
1275a3351425Sdougm 		} else {
1276549ec3ffSdougm 			(void) snprintf(buff + curlen, buffsize - curlen,
1277549ec3ffSdougm 			    "%s%s=%s", sep ? "," : "",
1278549ec3ffSdougm 			    name, value != NULL ? value : "");
1279549ec3ffSdougm 		}
1280*aed5d200Sdougm 		printed = B_TRUE;
1281a3351425Sdougm 	}
1282*aed5d200Sdougm skip:
1283549ec3ffSdougm 	if (name != NULL)
1284549ec3ffSdougm 		sa_free_attr_string(name);
1285549ec3ffSdougm 	if (value != NULL)
1286549ec3ffSdougm 		sa_free_attr_string(value);
1287*aed5d200Sdougm 	return (printed);
1288549ec3ffSdougm }
1289549ec3ffSdougm 
1290549ec3ffSdougm /*
1291549ec3ffSdougm  * nfs_format_options(group, hier)
1292549ec3ffSdougm  *
1293549ec3ffSdougm  * format all the options on the group into an old-style option
1294549ec3ffSdougm  * string. If hier is non-zero, walk up the tree to get inherited
1295549ec3ffSdougm  * options.
1296549ec3ffSdougm  */
1297549ec3ffSdougm 
1298549ec3ffSdougm static char *
1299549ec3ffSdougm nfs_format_options(sa_group_t group, int hier)
1300549ec3ffSdougm {
1301549ec3ffSdougm 	sa_optionset_t options = NULL;
1302a3351425Sdougm 	sa_optionset_t secoptions = NULL;
1303549ec3ffSdougm 	sa_property_t prop, secprop;
1304a3351425Sdougm 	sa_security_t security = NULL;
1305549ec3ffSdougm 	char *buff;
1306549ec3ffSdougm 	size_t buffsize;
1307a3351425Sdougm 	char *sectype = NULL;
1308a3351425Sdougm 	int sep = 0;
1309a3351425Sdougm 
1310a3351425Sdougm 
1311a3351425Sdougm 	buff = malloc(OPT_CHUNK);
1312a3351425Sdougm 	if (buff == NULL) {
1313a3351425Sdougm 		return (NULL);
1314a3351425Sdougm 	}
1315a3351425Sdougm 
1316a3351425Sdougm 	buff[0] = '\0';
1317a3351425Sdougm 	buffsize = OPT_CHUNK;
1318a3351425Sdougm 
1319a3351425Sdougm 	/*
1320a3351425Sdougm 	 * We may have a an optionset relative to this item. format
1321a3351425Sdougm 	 * these if we find them and then add any security definitions.
1322a3351425Sdougm 	 */
1323549ec3ffSdougm 
1324549ec3ffSdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
1325549ec3ffSdougm 
1326549ec3ffSdougm 	/*
1327549ec3ffSdougm 	 * do the default set first but skip any option that is also
1328549ec3ffSdougm 	 * in the protocol specific optionset.
1329549ec3ffSdougm 	 */
1330549ec3ffSdougm 	if (options != NULL) {
1331a3351425Sdougm 		for (prop = sa_get_property(options, NULL);
1332a3351425Sdougm 		    prop != NULL; prop = sa_get_next_property(prop)) {
1333549ec3ffSdougm 			/*
1334a3351425Sdougm 			 * use this one since we skipped any
1335a3351425Sdougm 			 * of these that were also in
1336a3351425Sdougm 			 * optdefault
1337549ec3ffSdougm 			 */
1338*aed5d200Sdougm 			if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1339*aed5d200Sdougm 			    prop, sep))
1340*aed5d200Sdougm 				sep = 1;
1341549ec3ffSdougm 			if (buff == NULL) {
1342549ec3ffSdougm 				/*
1343a3351425Sdougm 				 * buff could become NULL if there
1344a3351425Sdougm 				 * isn't enough memory for
1345a3351425Sdougm 				 * nfs_sprint_option to realloc()
1346a3351425Sdougm 				 * as necessary. We can't really
1347a3351425Sdougm 				 * do anything about it at this
1348a3351425Sdougm 				 * point so we return NULL.  The
1349a3351425Sdougm 				 * caller should handle the
1350a3351425Sdougm 				 * failure.
1351549ec3ffSdougm 				 */
1352a3351425Sdougm 				if (options != NULL)
1353a3351425Sdougm 					sa_free_derived_optionset(
1354a3351425Sdougm 					    options);
1355549ec3ffSdougm 				return (buff);
1356549ec3ffSdougm 			}
1357549ec3ffSdougm 		}
1358549ec3ffSdougm 	}
1359549ec3ffSdougm 	secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1360549ec3ffSdougm 	    "nfs", hier);
1361549ec3ffSdougm 	if (secoptions != NULL) {
1362549ec3ffSdougm 		for (secprop = sa_get_property(secoptions, NULL);
1363a3351425Sdougm 		    secprop != NULL;
1364a3351425Sdougm 		    secprop = sa_get_next_property(secprop)) {
1365549ec3ffSdougm 			sectype = sa_get_property_attr(secprop, "type");
1366a3351425Sdougm 			security =
1367a3351425Sdougm 			    (sa_security_t)sa_get_derived_security(
1368a3351425Sdougm 			    group, sectype, "nfs", hier);
1369549ec3ffSdougm 			if (security != NULL) {
1370549ec3ffSdougm 				if (sectype != NULL) {
1371a3351425Sdougm 					prop = sa_create_property(
1372a3351425Sdougm 					    "sec", sectype);
1373*aed5d200Sdougm 					if (prop == NULL)
1374*aed5d200Sdougm 						goto err;
1375*aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1376*aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1377549ec3ffSdougm 						sep = 1;
1378*aed5d200Sdougm 					(void) sa_remove_property(prop);
1379*aed5d200Sdougm 					if (buff == NULL)
1380*aed5d200Sdougm 						goto err;
1381549ec3ffSdougm 				}
1382a3351425Sdougm 				for (prop = sa_get_property(security,
1383a3351425Sdougm 				    NULL); prop != NULL;
1384549ec3ffSdougm 				    prop = sa_get_next_property(prop)) {
1385*aed5d200Sdougm 					if (nfs_sprint_option(&buff,
1386*aed5d200Sdougm 					    &buffsize, OPT_CHUNK, prop, sep))
1387*aed5d200Sdougm 						sep = 1;
1388a3351425Sdougm 					if (buff == NULL)
1389a3351425Sdougm 						goto err;
1390a3351425Sdougm 				}
1391a3351425Sdougm 				sa_free_derived_optionset(security);
1392a3351425Sdougm 			}
1393a3351425Sdougm 			if (sectype != NULL)
1394a3351425Sdougm 				sa_free_attr_string(sectype);
1395a3351425Sdougm 		}
1396a3351425Sdougm 		sa_free_derived_optionset(secoptions);
1397a3351425Sdougm 	}
1398549ec3ffSdougm 
1399a3351425Sdougm 	if (options != NULL)
1400a3351425Sdougm 		sa_free_derived_optionset(options);
1401a3351425Sdougm 	return (buff);
1402a3351425Sdougm 
1403a3351425Sdougm err:
1404a3351425Sdougm 	/*
1405a3351425Sdougm 	 * If we couldn't allocate memory for option printing, we need
1406a3351425Sdougm 	 * to break out of the nested loops, cleanup and return NULL.
1407a3351425Sdougm 	 */
1408a3351425Sdougm 	if (secoptions != NULL)
1409549ec3ffSdougm 		sa_free_derived_optionset(secoptions);
1410549ec3ffSdougm 	if (security != NULL)
1411549ec3ffSdougm 		sa_free_derived_optionset(security);
1412549ec3ffSdougm 	if (sectype != NULL)
1413549ec3ffSdougm 		sa_free_attr_string(sectype);
1414549ec3ffSdougm 	if (options != NULL)
1415549ec3ffSdougm 		sa_free_derived_optionset(options);
1416549ec3ffSdougm 	return (buff);
1417549ec3ffSdougm }
1418a3351425Sdougm 
1419549ec3ffSdougm /*
1420549ec3ffSdougm  * Append an entry to the nfslogtab file
1421549ec3ffSdougm  */
1422549ec3ffSdougm static int
1423549ec3ffSdougm nfslogtab_add(dir, buffer, tag)
1424549ec3ffSdougm 	char *dir, *buffer, *tag;
1425549ec3ffSdougm {
1426549ec3ffSdougm 	FILE *f;
1427549ec3ffSdougm 	struct logtab_ent lep;
1428549ec3ffSdougm 	int error = 0;
1429549ec3ffSdougm 
1430549ec3ffSdougm 	/*
1431549ec3ffSdougm 	 * Open the file for update and create it if necessary.
1432549ec3ffSdougm 	 * This may leave the I/O offset at the end of the file,
1433549ec3ffSdougm 	 * so rewind back to the beginning of the file.
1434549ec3ffSdougm 	 */
1435549ec3ffSdougm 	f = fopen(NFSLOGTAB, "a+");
1436549ec3ffSdougm 	if (f == NULL) {
1437549ec3ffSdougm 		error = errno;
1438549ec3ffSdougm 		goto out;
1439549ec3ffSdougm 	}
1440549ec3ffSdougm 	rewind(f);
1441549ec3ffSdougm 
1442549ec3ffSdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1443549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1444549ec3ffSdougm 		    "share complete, however failed to lock %s "
1445549ec3ffSdougm 		    "for update: %s\n"), NFSLOGTAB, strerror(errno));
1446549ec3ffSdougm 		error = -1;
1447549ec3ffSdougm 		goto out;
1448549ec3ffSdougm 	}
1449549ec3ffSdougm 
1450549ec3ffSdougm 	if (logtab_deactivate_after_boot(f) == -1) {
1451549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1452549ec3ffSdougm 		    "share complete, however could not deactivate "
1453549ec3ffSdougm 		    "entries in %s\n"), NFSLOGTAB);
1454549ec3ffSdougm 		error = -1;
1455549ec3ffSdougm 		goto out;
1456549ec3ffSdougm 	}
1457549ec3ffSdougm 
1458549ec3ffSdougm 	/*
1459549ec3ffSdougm 	 * Remove entries matching buffer and sharepoint since we're
1460549ec3ffSdougm 	 * going to replace it with perhaps an entry with a new tag.
1461549ec3ffSdougm 	 */
1462549ec3ffSdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
1463549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1464549ec3ffSdougm 		    "share complete, however could not remove matching "
1465549ec3ffSdougm 		    "entries in %s\n"), NFSLOGTAB);
1466549ec3ffSdougm 		error = -1;
1467549ec3ffSdougm 		goto out;
1468549ec3ffSdougm 	}
1469549ec3ffSdougm 
1470549ec3ffSdougm 	/*
1471549ec3ffSdougm 	 * Deactivate all active entries matching this sharepoint
1472549ec3ffSdougm 	 */
1473549ec3ffSdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
1474549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1475549ec3ffSdougm 		    "share complete, however could not deactivate matching "
1476549ec3ffSdougm 		    "entries in %s\n"), NFSLOGTAB);
1477549ec3ffSdougm 		error = -1;
1478549ec3ffSdougm 		goto out;
1479549ec3ffSdougm 	}
1480549ec3ffSdougm 
1481549ec3ffSdougm 	lep.le_buffer = buffer;
1482549ec3ffSdougm 	lep.le_path = dir;
1483549ec3ffSdougm 	lep.le_tag = tag;
1484549ec3ffSdougm 	lep.le_state = LES_ACTIVE;
1485549ec3ffSdougm 
1486549ec3ffSdougm 	/*
1487549ec3ffSdougm 	 * Add new sharepoint / buffer location to nfslogtab
1488549ec3ffSdougm 	 */
1489549ec3ffSdougm 	if (logtab_putent(f, &lep) < 0) {
1490549ec3ffSdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1491549ec3ffSdougm 		    "share complete, however could not add %s to %s\n"),
1492549ec3ffSdougm 		    dir, NFSLOGTAB);
1493549ec3ffSdougm 		error = -1;
1494549ec3ffSdougm 	}
1495549ec3ffSdougm 
1496549ec3ffSdougm out:
1497549ec3ffSdougm 	if (f != NULL)
1498549ec3ffSdougm 		(void) fclose(f);
1499549ec3ffSdougm 	return (error);
1500549ec3ffSdougm }
1501549ec3ffSdougm 
1502549ec3ffSdougm /*
1503549ec3ffSdougm  * Deactivate an entry from the nfslogtab file
1504549ec3ffSdougm  */
1505549ec3ffSdougm static int
1506549ec3ffSdougm nfslogtab_deactivate(path)
1507549ec3ffSdougm 	char *path;
1508549ec3ffSdougm {
1509549ec3ffSdougm 	FILE *f;
1510549ec3ffSdougm 	int error = 0;
1511549ec3ffSdougm 
1512549ec3ffSdougm 	f = fopen(NFSLOGTAB, "r+");
1513549ec3ffSdougm 	if (f == NULL) {
1514549ec3ffSdougm 		error = errno;
1515549ec3ffSdougm 		goto out;
1516549ec3ffSdougm 	}
1517549ec3ffSdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1518549ec3ffSdougm 		error = errno;
1519549ec3ffSdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
1520549ec3ffSdougm 		    "share complete, however could not lock %s for "
1521549ec3ffSdougm 		    "update: %s\n"), NFSLOGTAB, strerror(error));
1522549ec3ffSdougm 		goto out;
1523549ec3ffSdougm 	}
1524549ec3ffSdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
1525549ec3ffSdougm 		error = -1;
1526549ec3ffSdougm 		(void) fprintf(stderr,
1527549ec3ffSdougm 		    dgettext(TEXT_DOMAIN,
1528549ec3ffSdougm 		    "share complete, however could not "
1529549ec3ffSdougm 		    "deactivate %s in %s\n"), path, NFSLOGTAB);
1530549ec3ffSdougm 		goto out;
1531549ec3ffSdougm 	}
1532549ec3ffSdougm 
1533549ec3ffSdougm out:	if (f != NULL)
1534549ec3ffSdougm 		(void) fclose(f);
1535549ec3ffSdougm 
1536549ec3ffSdougm 	return (error);
1537549ec3ffSdougm }
1538549ec3ffSdougm 
1539549ec3ffSdougm /*
1540546405c3Sdougm  * check_public(group, skipshare)
1541546405c3Sdougm  *
1542546405c3Sdougm  * Check the group for any shares that have the public property
1543546405c3Sdougm  * enabled. We skip "skipshare" since that is the one we are
1544546405c3Sdougm  * working with. This is a separate function to make handling
1545546405c3Sdougm  * subgroups simpler. Returns true if there is a share with public.
1546546405c3Sdougm  */
1547546405c3Sdougm static int
1548546405c3Sdougm check_public(sa_group_t group, sa_share_t skipshare)
1549546405c3Sdougm {
1550546405c3Sdougm 	int exists = B_FALSE;
1551546405c3Sdougm 	sa_share_t share;
1552546405c3Sdougm 	sa_optionset_t opt;
1553546405c3Sdougm 	sa_property_t prop;
1554546405c3Sdougm 	char *shared;
1555546405c3Sdougm 
1556546405c3Sdougm 	for (share = sa_get_share(group, NULL); share != NULL;
1557546405c3Sdougm 	    share = sa_get_next_share(share)) {
1558546405c3Sdougm 		if (share == skipshare)
1559546405c3Sdougm 			continue;
1560546405c3Sdougm 
1561546405c3Sdougm 		opt = sa_get_optionset(share, "nfs");
1562546405c3Sdougm 		if (opt == NULL)
1563546405c3Sdougm 			continue;
1564546405c3Sdougm 		prop = sa_get_property(opt, "public");
1565546405c3Sdougm 		if (prop == NULL)
1566546405c3Sdougm 			continue;
1567546405c3Sdougm 		shared = sa_get_share_attr(share, "shared");
1568546405c3Sdougm 		if (shared != NULL) {
1569546405c3Sdougm 			exists = strcmp(shared, "true") == 0;
1570546405c3Sdougm 			sa_free_attr_string(shared);
1571546405c3Sdougm 			if (exists == B_TRUE)
1572546405c3Sdougm 				break;
1573546405c3Sdougm 		}
1574546405c3Sdougm 	}
1575546405c3Sdougm 
1576546405c3Sdougm 	return (exists);
1577546405c3Sdougm }
1578546405c3Sdougm 
1579546405c3Sdougm /*
1580549ec3ffSdougm  * public_exists(share)
1581549ec3ffSdougm  *
1582549ec3ffSdougm  * check to see if public option is set on any other share than the
1583546405c3Sdougm  * one specified. Need to check zfs sub-groups as well as the top
1584546405c3Sdougm  * level groups.
1585549ec3ffSdougm  */
1586549ec3ffSdougm static int
1587549ec3ffSdougm public_exists(sa_share_t skipshare)
1588549ec3ffSdougm {
1589549ec3ffSdougm 	sa_group_t group;
1590549ec3ffSdougm 	sa_handle_t handle;
1591549ec3ffSdougm 
1592549ec3ffSdougm 	group = sa_get_parent_group(skipshare);
1593549ec3ffSdougm 	if (group == NULL)
1594549ec3ffSdougm 		return (SA_NO_SUCH_GROUP);
1595549ec3ffSdougm 
1596549ec3ffSdougm 	handle = sa_find_group_handle(group);
1597549ec3ffSdougm 	if (handle == NULL)
1598549ec3ffSdougm 		return (SA_SYSTEM_ERR);
1599549ec3ffSdougm 
1600549ec3ffSdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
1601549ec3ffSdougm 	    group = sa_get_next_group(group)) {
1602546405c3Sdougm 		/* Walk any ZFS subgroups as well as all standard groups */
1603546405c3Sdougm 		if (sa_group_is_zfs(group)) {
1604546405c3Sdougm 			sa_group_t subgroup;
1605546405c3Sdougm 			for (subgroup = sa_get_sub_group(group);
1606546405c3Sdougm 			    subgroup != NULL;
1607546405c3Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
1608546405c3Sdougm 				if (check_public(subgroup, skipshare))
1609546405c3Sdougm 					return (B_TRUE);
1610546405c3Sdougm 			}
1611546405c3Sdougm 		} else {
1612546405c3Sdougm 			if (check_public(group, skipshare))
1613546405c3Sdougm 				return (B_TRUE);
1614549ec3ffSdougm 		}
1615549ec3ffSdougm 	}
1616546405c3Sdougm 	return (B_FALSE);
1617549ec3ffSdougm }
1618549ec3ffSdougm 
1619549ec3ffSdougm /*
1620549ec3ffSdougm  * sa_enable_share at the protocol level, enable_share must tell the
1621549ec3ffSdougm  * implementation that it is to enable the share. This entails
1622549ec3ffSdougm  * converting the path and options into the appropriate ioctl
1623549ec3ffSdougm  * calls. It is assumed that all error checking of paths, etc. were
1624549ec3ffSdougm  * done earlier.
1625549ec3ffSdougm  */
1626549ec3ffSdougm static int
1627549ec3ffSdougm nfs_enable_share(sa_share_t share)
1628549ec3ffSdougm {
1629549ec3ffSdougm 	struct exportdata export;
1630549ec3ffSdougm 	sa_optionset_t secoptlist;
1631549ec3ffSdougm 	struct secinfo *sp;
1632549ec3ffSdougm 	int num_secinfo;
1633549ec3ffSdougm 	sa_optionset_t opt;
1634549ec3ffSdougm 	sa_security_t sec;
1635549ec3ffSdougm 	sa_property_t prop;
1636549ec3ffSdougm 	char *path;
1637549ec3ffSdougm 	int err = SA_OK;
1638546405c3Sdougm 	int i;
1639ecd6cf80Smarks 	int iszfs;
1640549ec3ffSdougm 
1641549ec3ffSdougm 	/* Don't drop core if the NFS module isn't loaded. */
1642549ec3ffSdougm 	(void) signal(SIGSYS, SIG_IGN);
1643549ec3ffSdougm 
1644549ec3ffSdougm 	/* get the path since it is important in several places */
1645549ec3ffSdougm 	path = sa_get_share_attr(share, "path");
1646549ec3ffSdougm 	if (path == NULL)
1647549ec3ffSdougm 		return (SA_NO_SUCH_PATH);
1648549ec3ffSdougm 
1649ecd6cf80Smarks 	iszfs = sa_path_is_zfs(path);
1650549ec3ffSdougm 	/*
1651549ec3ffSdougm 	 * find the optionsets and security sets.  There may not be
1652549ec3ffSdougm 	 * any or there could be one or two for each of optionset and
1653549ec3ffSdougm 	 * security may have multiple, one per security type per
1654549ec3ffSdougm 	 * protocol type.
1655549ec3ffSdougm 	 */
1656549ec3ffSdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
1657549ec3ffSdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
1658549ec3ffSdougm 	if (secoptlist != NULL)
1659549ec3ffSdougm 		num_secinfo = MAX(1, count_security(secoptlist));
1660549ec3ffSdougm 	else
1661549ec3ffSdougm 		num_secinfo = 1;
1662549ec3ffSdougm 
1663549ec3ffSdougm 	/*
1664549ec3ffSdougm 	 * walk through the options and fill in the structure
1665549ec3ffSdougm 	 * appropriately.
1666549ec3ffSdougm 	 */
1667549ec3ffSdougm 
1668549ec3ffSdougm 	(void) memset(&export, '\0', sizeof (export));
1669549ec3ffSdougm 
1670549ec3ffSdougm 	/*
1671549ec3ffSdougm 	 * do non-security options first since there is only one after
1672549ec3ffSdougm 	 * the derived group is constructed.
1673549ec3ffSdougm 	 */
1674549ec3ffSdougm 	export.ex_version = EX_CURRENT_VERSION;
1675549ec3ffSdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
1676549ec3ffSdougm 	export.ex_index = NULL;
1677549ec3ffSdougm 	export.ex_path = path;
1678549ec3ffSdougm 	export.ex_pathlen = strlen(path) + 1;
1679549ec3ffSdougm 
1680549ec3ffSdougm 	if (opt != NULL)
1681549ec3ffSdougm 		err = fill_export_from_optionset(&export, opt);
1682549ec3ffSdougm 
1683549ec3ffSdougm 	/*
1684549ec3ffSdougm 	 * check to see if "public" is set. If it is, then make sure
1685549ec3ffSdougm 	 * no other share has it set. If it is already used, fail.
1686549ec3ffSdougm 	 */
1687549ec3ffSdougm 
1688549ec3ffSdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(share)) {
1689549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1690549ec3ffSdougm 		    "NFS: Cannot share more than one file "
1691549ec3ffSdougm 		    "system with 'public' property\n"));
1692549ec3ffSdougm 		err = SA_NOT_ALLOWED;
1693549ec3ffSdougm 		goto out;
1694549ec3ffSdougm 	}
1695549ec3ffSdougm 
1696546405c3Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
1697549ec3ffSdougm 	if (sp == NULL) {
1698549ec3ffSdougm 		err = SA_NO_MEMORY;
1699546405c3Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1700546405c3Sdougm 		    "NFS: NFS: no memory for security\n"));
1701546405c3Sdougm 		goto out;
1702546405c3Sdougm 	}
1703549ec3ffSdougm 	export.ex_secinfo = sp;
1704549ec3ffSdougm 	/* get default secinfo */
1705549ec3ffSdougm 	export.ex_seccnt = num_secinfo;
1706549ec3ffSdougm 	/*
1707549ec3ffSdougm 	 * since we must have one security option defined, we
1708549ec3ffSdougm 	 * init to the default and then override as we find
1709549ec3ffSdougm 	 * defined security options. This handles the case
1710549ec3ffSdougm 	 * where we have no defined options but we need to set
1711549ec3ffSdougm 	 * up one.
1712549ec3ffSdougm 	 */
1713549ec3ffSdougm 	sp[0].s_window = DEF_WIN;
1714549ec3ffSdougm 	sp[0].s_rootnames = NULL;
1715549ec3ffSdougm 	/* setup a default in case no properties defined */
1716549ec3ffSdougm 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1717549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1718549ec3ffSdougm 		    "NFS: nfs_getseconfig_default: failed to "
1719549ec3ffSdougm 		    "get default security mode\n"));
1720549ec3ffSdougm 		err = SA_CONFIG_ERR;
1721549ec3ffSdougm 	}
1722549ec3ffSdougm 	if (secoptlist != NULL) {
1723549ec3ffSdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
1724549ec3ffSdougm 		    prop != NULL && i < num_secinfo;
1725549ec3ffSdougm 		    prop = sa_get_next_property(prop), i++) {
1726549ec3ffSdougm 			char *sectype;
1727549ec3ffSdougm 				sectype = sa_get_property_attr(prop, "type");
1728a3351425Sdougm 			/*
1729a3351425Sdougm 			 * if sectype is NULL, we probably
1730a3351425Sdougm 			 * have a memory problem and can't get
1731a3351425Sdougm 			 * the correct values. Rather than
1732a3351425Sdougm 			 * exporting with incorrect security,
1733a3351425Sdougm 			 * don't share it.
1734a3351425Sdougm 			 */
1735a3351425Sdougm 			if (sectype == NULL) {
1736a3351425Sdougm 				err = SA_NO_MEMORY;
1737a3351425Sdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1738a3351425Sdougm 				    "NFS: Cannot share %s: "
1739a3351425Sdougm 				    "no memory\n"), path);
1740a3351425Sdougm 				goto out;
1741a3351425Sdougm 			}
1742a3351425Sdougm 			sec = (sa_security_t)sa_get_derived_security(
1743a3351425Sdougm 			    share, sectype, "nfs", 1);
1744549ec3ffSdougm 			sp[i].s_window = DEF_WIN;
1745549ec3ffSdougm 			sp[i].s_rootcnt = 0;
1746549ec3ffSdougm 			sp[i].s_rootnames = NULL;
1747549ec3ffSdougm 				(void) fill_security_from_secopts(&sp[i], sec);
1748549ec3ffSdougm 			if (sec != NULL)
1749549ec3ffSdougm 				sa_free_derived_security(sec);
1750549ec3ffSdougm 			if (sectype != NULL)
1751549ec3ffSdougm 				sa_free_attr_string(sectype);
1752549ec3ffSdougm 		}
1753549ec3ffSdougm 	}
1754549ec3ffSdougm 	/*
1755549ec3ffSdougm 	 * when we get here, we can do the exportfs system call and
1756549ec3ffSdougm 	 * initiate thinsg. We probably want to enable the nfs.server
1757549ec3ffSdougm 	 * service first if it isn't running within SMF.
1758549ec3ffSdougm 	 */
1759549ec3ffSdougm 	/* check nfs.server status and start if needed */
1760549ec3ffSdougm 	/* now add the share to the internal tables */
1761549ec3ffSdougm 	printarg(path, &export);
1762549ec3ffSdougm 	/*
1763549ec3ffSdougm 	 * call the exportfs system call which is implemented
1764549ec3ffSdougm 	 * via the nfssys() call as the EXPORTFS subfunction.
1765549ec3ffSdougm 	 */
1766ecd6cf80Smarks 	if (iszfs) {
1767ecd6cf80Smarks 		struct exportfs_args ea;
1768ecd6cf80Smarks 		share_t sh;
1769ecd6cf80Smarks 		char *str;
1770ecd6cf80Smarks 		priv_set_t *priv_effective;
1771ecd6cf80Smarks 		int privileged;
1772ecd6cf80Smarks 
1773ecd6cf80Smarks 		/*
1774ecd6cf80Smarks 		 * If we aren't a privileged user
1775ecd6cf80Smarks 		 * and NFS server service isn't running
1776ecd6cf80Smarks 		 * then print out an error message
1777ecd6cf80Smarks 		 * and return EPERM
1778ecd6cf80Smarks 		 */
1779ecd6cf80Smarks 
1780ecd6cf80Smarks 		priv_effective = priv_allocset();
1781ecd6cf80Smarks 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
1782ecd6cf80Smarks 
1783ecd6cf80Smarks 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
1784ecd6cf80Smarks 		priv_freeset(priv_effective);
1785ecd6cf80Smarks 
1786ecd6cf80Smarks 		if (!privileged &&
1787ecd6cf80Smarks 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
1788ecd6cf80Smarks 			err = 0;
1789ecd6cf80Smarks 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
1790ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1791ecd6cf80Smarks 				    "NFS: Cannot share remote "
1792ecd6cf80Smarks 				    "filesystem: %s\n"), path);
1793ecd6cf80Smarks 				(void) printf(dgettext(TEXT_DOMAIN,
1794ecd6cf80Smarks 				    "NFS: Service needs to be enabled "
1795ecd6cf80Smarks 				    "by a privileged user\n"));
1796ecd6cf80Smarks 				err = SA_SYSTEM_ERR;
1797ecd6cf80Smarks 				errno = EPERM;
1798ecd6cf80Smarks 			}
1799ecd6cf80Smarks 			free(str);
1800ecd6cf80Smarks 		}
1801ecd6cf80Smarks 
1802ecd6cf80Smarks 		if (err == 0) {
1803ecd6cf80Smarks 			ea.dname = path;
1804ecd6cf80Smarks 			ea.uex = &export;
1805ecd6cf80Smarks 
1806ecd6cf80Smarks 			sa_sharetab_fill_zfs(share, &sh, "nfs");
1807ecd6cf80Smarks 			err = sa_share_zfs(share, path, &sh, &ea, B_TRUE);
1808ecd6cf80Smarks 			sa_emptyshare(&sh);
1809ecd6cf80Smarks 		}
1810ecd6cf80Smarks 	} else {
1811ecd6cf80Smarks 		err = exportfs(path, &export);
1812ecd6cf80Smarks 	}
1813ecd6cf80Smarks 
1814ecd6cf80Smarks 	if (err < 0) {
1815549ec3ffSdougm 		err = SA_SYSTEM_ERR;
1816549ec3ffSdougm 		switch (errno) {
1817549ec3ffSdougm 		case EREMOTE:
1818549ec3ffSdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1819ecd6cf80Smarks 			    "NFS: Cannot share filesystems "
1820ecd6cf80Smarks 			    "in non-global zones: %s\n"), path);
1821ecd6cf80Smarks 			err = SA_NOT_SUPPORTED;
1822549ec3ffSdougm 			break;
1823549ec3ffSdougm 		case EPERM:
1824549ec3ffSdougm 			if (getzoneid() != GLOBAL_ZONEID) {
1825549ec3ffSdougm 				(void) printf(dgettext(TEXT_DOMAIN,
1826549ec3ffSdougm 				    "NFS: Cannot share file systems "
1827549ec3ffSdougm 				    "in non-global zones: %s\n"), path);
1828549ec3ffSdougm 				err = SA_NOT_SUPPORTED;
1829549ec3ffSdougm 				break;
1830549ec3ffSdougm 			}
1831549ec3ffSdougm 			err = SA_NO_PERMISSION;
1832549ec3ffSdougm 			/* FALLTHROUGH */
1833549ec3ffSdougm 		default:
1834549ec3ffSdougm 			break;
1835549ec3ffSdougm 		}
1836549ec3ffSdougm 	} else {
1837549ec3ffSdougm 		/* update sharetab with an add/modify */
1838ecd6cf80Smarks 		if (!iszfs) {
1839549ec3ffSdougm 			(void) sa_update_sharetab(share, "nfs");
1840549ec3ffSdougm 		}
1841ecd6cf80Smarks 	}
1842549ec3ffSdougm 
1843549ec3ffSdougm 	if (err == SA_OK) {
1844549ec3ffSdougm 		/*
1845549ec3ffSdougm 		 * enable services as needed. This should probably be
1846549ec3ffSdougm 		 * done elsewhere in order to minimize the calls to
1847549ec3ffSdougm 		 * check services.
1848549ec3ffSdougm 		 */
1849549ec3ffSdougm 		/*
1850549ec3ffSdougm 		 * check to see if logging and other services need to
1851549ec3ffSdougm 		 * be triggered, but only if there wasn't an
1852549ec3ffSdougm 		 * error. This is probably where sharetab should be
1853549ec3ffSdougm 		 * updated with the NFS specific entry.
1854549ec3ffSdougm 		 */
1855549ec3ffSdougm 		if (export.ex_flags & EX_LOG) {
1856549ec3ffSdougm 			/* enable logging */
1857549ec3ffSdougm 			if (nfslogtab_add(path, export.ex_log_buffer,
1858549ec3ffSdougm 			    export.ex_tag) != 0) {
1859a3351425Sdougm 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1860549ec3ffSdougm 				    "Could not enable logging for %s\n"),
1861549ec3ffSdougm 				    path);
1862549ec3ffSdougm 			}
1863549ec3ffSdougm 			_check_services(service_list_logging);
1864549ec3ffSdougm 		} else {
1865549ec3ffSdougm 			/*
1866549ec3ffSdougm 			 * don't have logging so remove it from file. It might
1867549ec3ffSdougm 			 * not be thre, but that doesn't matter.
1868549ec3ffSdougm 			 */
1869549ec3ffSdougm 			(void) nfslogtab_deactivate(path);
1870549ec3ffSdougm 			_check_services(service_list_default);
1871549ec3ffSdougm 		}
1872549ec3ffSdougm 	}
1873549ec3ffSdougm 
1874549ec3ffSdougm out:
1875549ec3ffSdougm 	if (path != NULL)
1876549ec3ffSdougm 		free(path);
1877549ec3ffSdougm 
1878549ec3ffSdougm 	cleanup_export(&export);
1879549ec3ffSdougm 	if (opt != NULL)
1880549ec3ffSdougm 		sa_free_derived_optionset(opt);
1881549ec3ffSdougm 	if (secoptlist != NULL)
1882549ec3ffSdougm 		(void) sa_destroy_optionset(secoptlist);
1883549ec3ffSdougm 	return (err);
1884549ec3ffSdougm }
1885549ec3ffSdougm 
1886549ec3ffSdougm /*
1887549ec3ffSdougm  * nfs_disable_share(share)
1888549ec3ffSdougm  *
1889549ec3ffSdougm  * Unshare the specified share.  How much error checking should be
1890549ec3ffSdougm  * done? We only do basic errors for now.
1891549ec3ffSdougm  */
1892549ec3ffSdougm static int
1893ecd6cf80Smarks nfs_disable_share(sa_share_t share, char *path)
1894549ec3ffSdougm {
1895549ec3ffSdougm 	int err;
1896549ec3ffSdougm 	int ret = SA_OK;
1897ecd6cf80Smarks 	int iszfs;
1898549ec3ffSdougm 
1899ecd6cf80Smarks 
1900ecd6cf80Smarks 	if (path != NULL) {
1901ecd6cf80Smarks 		iszfs = sa_path_is_zfs(path);
1902ecd6cf80Smarks 
1903ecd6cf80Smarks 		if (iszfs) {
1904ecd6cf80Smarks 			struct exportfs_args ea;
1905ecd6cf80Smarks 			share_t sh = { 0 };
1906ecd6cf80Smarks 
1907ecd6cf80Smarks 			ea.dname = path;
1908ecd6cf80Smarks 			ea.uex = NULL;
1909ecd6cf80Smarks 			sh.sh_path = path;
1910ecd6cf80Smarks 			sh.sh_fstype = "nfs";
1911ecd6cf80Smarks 
1912ecd6cf80Smarks 			err = sa_share_zfs(share, path, &sh, &ea, B_FALSE);
1913ecd6cf80Smarks 		} else
1914ecd6cf80Smarks 			err = exportfs(path, NULL);
1915549ec3ffSdougm 		if (err < 0) {
1916a3351425Sdougm 			/*
1917ecd6cf80Smarks 			 * TBD: only an error in some
1918ecd6cf80Smarks 			 * cases - need better analysis
1919a3351425Sdougm 			 */
1920ecd6cf80Smarks 
1921549ec3ffSdougm 			switch (errno) {
1922549ec3ffSdougm 			case EPERM:
1923549ec3ffSdougm 			case EACCES:
1924549ec3ffSdougm 				ret = SA_NO_PERMISSION;
1925ecd6cf80Smarks 				if (getzoneid() != GLOBAL_ZONEID) {
1926549ec3ffSdougm 					ret = SA_NOT_SUPPORTED;
1927ecd6cf80Smarks 				}
1928549ec3ffSdougm 				break;
1929549ec3ffSdougm 			case EINVAL:
1930549ec3ffSdougm 			case ENOENT:
1931549ec3ffSdougm 				ret = SA_NO_SUCH_PATH;
1932549ec3ffSdougm 			break;
1933549ec3ffSdougm 			default:
1934549ec3ffSdougm 				ret = SA_SYSTEM_ERR;
1935549ec3ffSdougm 			break;
1936549ec3ffSdougm 			}
1937549ec3ffSdougm 		}
1938549ec3ffSdougm 		if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
1939ecd6cf80Smarks 			if (!iszfs)
1940ecd6cf80Smarks 				(void) sa_delete_sharetab(path, "nfs");
1941549ec3ffSdougm 			/* just in case it was logged */
1942ecd6cf80Smarks 			(void) nfslogtab_deactivate(path);
1943549ec3ffSdougm 		}
1944549ec3ffSdougm 	}
1945549ec3ffSdougm 	return (ret);
1946549ec3ffSdougm }
1947549ec3ffSdougm 
1948549ec3ffSdougm /*
1949549ec3ffSdougm  * check ro vs rw values.  Over time this may get beefed up.
1950549ec3ffSdougm  * for now it just does simple checks.
1951549ec3ffSdougm  */
1952549ec3ffSdougm 
1953549ec3ffSdougm static int
1954549ec3ffSdougm check_rorw(char *v1, char *v2)
1955549ec3ffSdougm {
1956549ec3ffSdougm 	int ret = SA_OK;
1957549ec3ffSdougm 	if (strcmp(v1, v2) == 0)
1958549ec3ffSdougm 		ret = SA_VALUE_CONFLICT;
1959549ec3ffSdougm 	return (ret);
1960549ec3ffSdougm }
1961549ec3ffSdougm 
1962549ec3ffSdougm /*
1963549ec3ffSdougm  * nfs_validate_property(property, parent)
1964549ec3ffSdougm  *
1965549ec3ffSdougm  * Check that the property has a legitimate value for its type.
1966549ec3ffSdougm  */
1967549ec3ffSdougm 
1968549ec3ffSdougm static int
1969549ec3ffSdougm nfs_validate_property(sa_property_t property, sa_optionset_t parent)
1970549ec3ffSdougm {
1971549ec3ffSdougm 	int ret = SA_OK;
1972549ec3ffSdougm 	char *propname;
1973549ec3ffSdougm 	char *other;
1974549ec3ffSdougm 	int optindex;
1975549ec3ffSdougm 	nfsl_config_t *configlist;
1976549ec3ffSdougm 	sa_group_t parent_group;
1977549ec3ffSdougm 	char *value;
1978549ec3ffSdougm 
1979549ec3ffSdougm 	propname = sa_get_property_attr(property, "type");
1980549ec3ffSdougm 
1981549ec3ffSdougm 	if ((optindex = findopt(propname)) < 0)
1982549ec3ffSdougm 		ret = SA_NO_SUCH_PROP;
1983549ec3ffSdougm 
1984549ec3ffSdougm 	/* need to validate value range here as well */
1985549ec3ffSdougm 
1986549ec3ffSdougm 	if (ret == SA_OK) {
1987549ec3ffSdougm 		parent_group = sa_get_parent_group((sa_share_t)parent);
1988a3351425Sdougm 		if (optdefs[optindex].share && !sa_is_share(parent_group))
1989549ec3ffSdougm 			ret = SA_PROP_SHARE_ONLY;
1990549ec3ffSdougm 	}
1991549ec3ffSdougm 	if (ret == SA_OK) {
1992549ec3ffSdougm 		value = sa_get_property_attr(property, "value");
1993549ec3ffSdougm 		if (value != NULL) {
1994549ec3ffSdougm 			/* first basic type checking */
1995549ec3ffSdougm 			switch (optdefs[optindex].type) {
1996549ec3ffSdougm 			case OPT_TYPE_NUMBER:
1997549ec3ffSdougm 				/* check that the value is all digits */
1998549ec3ffSdougm 				if (!is_a_number(value))
1999549ec3ffSdougm 					ret = SA_BAD_VALUE;
2000549ec3ffSdougm 				break;
2001549ec3ffSdougm 			case OPT_TYPE_BOOLEAN:
2002549ec3ffSdougm 				if (strlen(value) == 0 ||
2003549ec3ffSdougm 				    strcasecmp(value, "true") == 0 ||
2004549ec3ffSdougm 				    strcmp(value, "1") == 0 ||
2005549ec3ffSdougm 				    strcasecmp(value, "false") == 0 ||
2006549ec3ffSdougm 				    strcmp(value, "0") == 0) {
2007549ec3ffSdougm 					ret = SA_OK;
2008549ec3ffSdougm 				} else {
2009549ec3ffSdougm 					ret = SA_BAD_VALUE;
2010549ec3ffSdougm 				}
2011549ec3ffSdougm 				break;
2012549ec3ffSdougm 			case OPT_TYPE_USER:
2013549ec3ffSdougm 				if (!is_a_number(value)) {
2014549ec3ffSdougm 					struct passwd *pw;
2015a3351425Sdougm 					/*
2016a3351425Sdougm 					 * in this case it would have to be a
2017a3351425Sdougm 					 * user name
2018a3351425Sdougm 					 */
2019549ec3ffSdougm 					pw = getpwnam(value);
2020549ec3ffSdougm 					if (pw == NULL)
2021549ec3ffSdougm 						ret = SA_BAD_VALUE;
2022549ec3ffSdougm 					endpwent();
2023549ec3ffSdougm 				} else {
2024549ec3ffSdougm 					uint64_t intval;
2025549ec3ffSdougm 					intval = strtoull(value, NULL, 0);
2026549ec3ffSdougm 					if (intval > UID_MAX && intval != ~0)
2027549ec3ffSdougm 						ret = SA_BAD_VALUE;
2028549ec3ffSdougm 				}
2029549ec3ffSdougm 				break;
2030549ec3ffSdougm 			case OPT_TYPE_FILE:
2031549ec3ffSdougm 				if (strcmp(value, "..") == 0 ||
2032549ec3ffSdougm 				    strchr(value, '/') != NULL) {
2033549ec3ffSdougm 					ret = SA_BAD_VALUE;
2034549ec3ffSdougm 				}
2035549ec3ffSdougm 				break;
2036549ec3ffSdougm 			case OPT_TYPE_ACCLIST:
2037549ec3ffSdougm 				/*
2038549ec3ffSdougm 				 * access list handling. Should eventually
2039549ec3ffSdougm 				 * validate that all the values make sense.
2040549ec3ffSdougm 				 * Also, ro and rw may have cross value
2041549ec3ffSdougm 				 * conflicts.
2042549ec3ffSdougm 				 */
2043549ec3ffSdougm 				if (strcmp(propname, SHOPT_RO) == 0)
2044549ec3ffSdougm 					other = SHOPT_RW;
2045549ec3ffSdougm 				else if (strcmp(propname, SHOPT_RW) == 0)
2046549ec3ffSdougm 					other = SHOPT_RO;
2047549ec3ffSdougm 				else
2048549ec3ffSdougm 					other = NULL;
2049a3351425Sdougm 
2050549ec3ffSdougm 				if (other != NULL && parent != NULL) {
2051549ec3ffSdougm 					/* compare rw(ro) with ro(rw) */
2052549ec3ffSdougm 					sa_property_t oprop;
2053549ec3ffSdougm 					oprop = sa_get_property(parent, other);
2054549ec3ffSdougm 					if (oprop != NULL) {
2055a3351425Sdougm 						/*
2056a3351425Sdougm 						 * only potential
2057a3351425Sdougm 						 * confusion if other
2058a3351425Sdougm 						 * exists
2059a3351425Sdougm 						 */
2060549ec3ffSdougm 						char *ovalue;
2061a3351425Sdougm 						ovalue = sa_get_property_attr(
2062a3351425Sdougm 						    oprop, "value");
2063549ec3ffSdougm 						if (ovalue != NULL) {
2064a3351425Sdougm 							ret = check_rorw(value,
2065a3351425Sdougm 							    ovalue);
2066a3351425Sdougm 							sa_free_attr_string(
2067a3351425Sdougm 							    ovalue);
2068549ec3ffSdougm 						}
2069549ec3ffSdougm 					}
2070549ec3ffSdougm 				}
2071549ec3ffSdougm 				break;
2072549ec3ffSdougm 			case OPT_TYPE_LOGTAG:
2073549ec3ffSdougm 				if (nfsl_getconfig_list(&configlist) == 0) {
2074549ec3ffSdougm 					int error;
2075a3351425Sdougm 					if (value == NULL ||
2076a3351425Sdougm 					    strlen(value) == 0) {
2077549ec3ffSdougm 						if (value != NULL)
2078a3351425Sdougm 							sa_free_attr_string(
2079a3351425Sdougm 							    value);
2080549ec3ffSdougm 						value = strdup("global");
2081549ec3ffSdougm 					}
2082a3351425Sdougm 					if (value != NULL &&
2083a3351425Sdougm 					    nfsl_findconfig(configlist, value,
2084a3351425Sdougm 					    &error) == NULL) {
2085549ec3ffSdougm 						ret = SA_BAD_VALUE;
2086a3351425Sdougm 					}
2087*aed5d200Sdougm 					/* Must always free when done */
2088*aed5d200Sdougm 					nfsl_freeconfig_list(&configlist);
2089549ec3ffSdougm 				} else {
2090549ec3ffSdougm 					ret = SA_CONFIG_ERR;
2091549ec3ffSdougm 				}
2092549ec3ffSdougm 				break;
2093549ec3ffSdougm 			case OPT_TYPE_STRING:
2094549ec3ffSdougm 				/* whatever is here should be ok */
2095549ec3ffSdougm 				break;
2096549ec3ffSdougm 			case OPT_TYPE_SECURITY:
2097549ec3ffSdougm 				/*
2098549ec3ffSdougm 				 * The "sec" property isn't used in the
2099549ec3ffSdougm 				 * non-legacy parts of sharemgr. We need to
2100549ec3ffSdougm 				 * reject it here. For legacy, it is pulled
2101549ec3ffSdougm 				 * out well before we get here.
2102549ec3ffSdougm 				 */
2103549ec3ffSdougm 				ret = SA_NO_SUCH_PROP;
2104549ec3ffSdougm 				break;
2105549ec3ffSdougm 			default:
2106549ec3ffSdougm 				break;
2107549ec3ffSdougm 			}
2108*aed5d200Sdougm 
2109*aed5d200Sdougm 			if (value != NULL)
2110549ec3ffSdougm 				sa_free_attr_string(value);
2111*aed5d200Sdougm 
2112549ec3ffSdougm 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
2113549ec3ffSdougm 				/* do the property specific check */
2114549ec3ffSdougm 				ret = optdefs[optindex].check(property);
2115549ec3ffSdougm 			}
2116549ec3ffSdougm 		}
2117549ec3ffSdougm 	}
2118549ec3ffSdougm 
2119549ec3ffSdougm 	if (propname != NULL)
2120549ec3ffSdougm 		sa_free_attr_string(propname);
2121549ec3ffSdougm 	return (ret);
2122549ec3ffSdougm }
2123549ec3ffSdougm 
2124549ec3ffSdougm /*
2125549ec3ffSdougm  * Protocol management functions
2126549ec3ffSdougm  *
2127549ec3ffSdougm  * Properties defined in the default files are defined in
2128549ec3ffSdougm  * proto_option_defs for parsing and validation. If "other" and
2129549ec3ffSdougm  * "compare" are set, then the value for this property should be
2130549ec3ffSdougm  * compared against the property specified in "other" using the
2131549ec3ffSdougm  * "compare" check (either <= or >=) in order to ensure that the
2132549ec3ffSdougm  * values are in the correct range.  E.g. setting server_versmin
2133549ec3ffSdougm  * higher than server_versmax should not be allowed.
2134549ec3ffSdougm  */
2135549ec3ffSdougm 
2136549ec3ffSdougm struct proto_option_defs {
2137549ec3ffSdougm 	char *tag;
2138549ec3ffSdougm 	char *name;	/* display name -- remove protocol identifier */
2139549ec3ffSdougm 	int index;
2140549ec3ffSdougm 	int type;
2141549ec3ffSdougm 	union {
2142549ec3ffSdougm 	    int intval;
2143549ec3ffSdougm 	    char *string;
2144549ec3ffSdougm 	} defvalue;
2145549ec3ffSdougm 	uint32_t svcs;
2146549ec3ffSdougm 	int32_t minval;
2147549ec3ffSdougm 	int32_t maxval;
2148549ec3ffSdougm 	char *file;
2149549ec3ffSdougm 	char *other;
2150549ec3ffSdougm 	int compare;
2151549ec3ffSdougm #define	OPT_CMP_GE	0
2152549ec3ffSdougm #define	OPT_CMP_LE	1
2153549ec3ffSdougm 	int (*check)(char *);
2154549ec3ffSdougm } proto_options[] = {
2155549ec3ffSdougm #define	PROTO_OPT_NFSD_SERVERS			0
2156549ec3ffSdougm 	{"nfsd_servers",
2157549ec3ffSdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
2158549ec3ffSdougm 	    1, INT32_MAX, NFSADMIN},
2159549ec3ffSdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
2160549ec3ffSdougm 	{"lockd_listen_backlog",
2161549ec3ffSdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
2162549ec3ffSdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
2163549ec3ffSdougm #define	PROTO_OPT_LOCKD_SERVERS			2
2164549ec3ffSdougm 	{"lockd_servers",
2165549ec3ffSdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
2166549ec3ffSdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
2167549ec3ffSdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
2168549ec3ffSdougm 	{"lockd_retransmit_timeout",
2169549ec3ffSdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
2170549ec3ffSdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
2171549ec3ffSdougm #define	PROTO_OPT_GRACE_PERIOD			4
2172549ec3ffSdougm 	{"grace_period",
2173549ec3ffSdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
2174549ec3ffSdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
2175549ec3ffSdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
2176549ec3ffSdougm 	{"nfs_server_versmin",
2177549ec3ffSdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
2178549ec3ffSdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2179549ec3ffSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
2180549ec3ffSdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
2181549ec3ffSdougm 	{"nfs_server_versmax",
2182549ec3ffSdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
2183549ec3ffSdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2184549ec3ffSdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
2185549ec3ffSdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
2186549ec3ffSdougm 	{"nfs_client_versmin",
2187549ec3ffSdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
2188549ec3ffSdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
2189549ec3ffSdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
2190549ec3ffSdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
2191549ec3ffSdougm 	{"nfs_client_versmax",
2192549ec3ffSdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
2193549ec3ffSdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
2194549ec3ffSdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
2195549ec3ffSdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
2196549ec3ffSdougm 	{"nfs_server_delegation",
2197549ec3ffSdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
2198549ec3ffSdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
2199549ec3ffSdougm 	    NFSADMIN},
2200549ec3ffSdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
2201549ec3ffSdougm 	{"nfsmapid_domain",
2202549ec3ffSdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
2203549ec3ffSdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
2204549ec3ffSdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
2205549ec3ffSdougm 	{"nfsd_max_connections",
2206549ec3ffSdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
2207549ec3ffSdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
2208549ec3ffSdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
2209549ec3ffSdougm 	{"nfsd_protocol",
2210549ec3ffSdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
2211549ec3ffSdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
2212549ec3ffSdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
2213549ec3ffSdougm 	{"nfsd_listen_backlog",
2214549ec3ffSdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
2215549ec3ffSdougm 	    OPT_TYPE_NUMBER, 0,
2216549ec3ffSdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
2217549ec3ffSdougm 	{NULL}
2218549ec3ffSdougm };
2219549ec3ffSdougm 
2220549ec3ffSdougm /*
2221549ec3ffSdougm  * the protoset holds the defined options so we don't have to read
2222549ec3ffSdougm  * them multiple times
2223549ec3ffSdougm  */
2224*aed5d200Sdougm static sa_protocol_properties_t protoset;
2225549ec3ffSdougm 
2226549ec3ffSdougm static int
2227549ec3ffSdougm findprotoopt(char *name, int whichname)
2228549ec3ffSdougm {
2229549ec3ffSdougm 	int i;
2230549ec3ffSdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2231549ec3ffSdougm 		if (whichname == 1) {
2232549ec3ffSdougm 			if (strcasecmp(proto_options[i].name, name) == 0)
2233549ec3ffSdougm 			return (i);
2234549ec3ffSdougm 		} else {
2235549ec3ffSdougm 			if (strcasecmp(proto_options[i].tag, name) == 0)
2236549ec3ffSdougm 				return (i);
2237549ec3ffSdougm 		}
2238549ec3ffSdougm 	}
2239549ec3ffSdougm 	return (-1);
2240549ec3ffSdougm }
2241549ec3ffSdougm 
2242549ec3ffSdougm /*
2243549ec3ffSdougm  * fixcaselower(str)
2244549ec3ffSdougm  *
2245549ec3ffSdougm  * convert a string to lower case (inplace).
2246549ec3ffSdougm  */
2247549ec3ffSdougm 
2248549ec3ffSdougm static void
2249549ec3ffSdougm fixcaselower(char *str)
2250549ec3ffSdougm {
2251549ec3ffSdougm 	while (*str) {
2252549ec3ffSdougm 		*str = tolower(*str);
2253549ec3ffSdougm 		str++;
2254549ec3ffSdougm 	}
2255549ec3ffSdougm }
2256549ec3ffSdougm 
2257549ec3ffSdougm /*
2258549ec3ffSdougm  * fixcaseupper(str)
2259549ec3ffSdougm  *
2260549ec3ffSdougm  * convert a string to upper case (inplace).
2261549ec3ffSdougm  */
2262549ec3ffSdougm 
2263549ec3ffSdougm static void
2264549ec3ffSdougm fixcaseupper(char *str)
2265549ec3ffSdougm {
2266549ec3ffSdougm 	while (*str) {
2267549ec3ffSdougm 		*str = toupper(*str);
2268549ec3ffSdougm 		str++;
2269549ec3ffSdougm 	}
2270549ec3ffSdougm }
2271549ec3ffSdougm 
2272549ec3ffSdougm /*
2273330ef417Sdougm  * skipwhitespace(str)
2274330ef417Sdougm  *
2275330ef417Sdougm  * Skip leading white space. It is assumed that it is called with a
2276330ef417Sdougm  * valid pointer.
2277330ef417Sdougm  */
2278330ef417Sdougm 
2279330ef417Sdougm static char *
2280330ef417Sdougm skipwhitespace(char *str)
2281330ef417Sdougm {
2282330ef417Sdougm 	while (*str && isspace(*str))
2283330ef417Sdougm 		str++;
2284330ef417Sdougm 
2285330ef417Sdougm 	return (str);
2286330ef417Sdougm }
2287330ef417Sdougm 
2288330ef417Sdougm /*
2289a3351425Sdougm  * extractprop()
2290a3351425Sdougm  *
2291a3351425Sdougm  * Extract the property and value out of the line and create the
2292a3351425Sdougm  * property in the optionset.
2293a3351425Sdougm  */
2294a3351425Sdougm static void
2295a3351425Sdougm extractprop(char *name, char *value)
2296a3351425Sdougm {
2297a3351425Sdougm 	sa_property_t prop;
2298a3351425Sdougm 	int index;
2299a3351425Sdougm 	/*
2300a3351425Sdougm 	 * Remove any leading
2301a3351425Sdougm 	 * white space.
2302a3351425Sdougm 	 */
2303a3351425Sdougm 	name = skipwhitespace(name);
2304a3351425Sdougm 
2305a3351425Sdougm 	index = findprotoopt(name, 0);
2306a3351425Sdougm 	if (index >= 0) {
2307a3351425Sdougm 		fixcaselower(name);
2308a3351425Sdougm 		prop = sa_create_property(proto_options[index].name, value);
2309a3351425Sdougm 		if (prop != NULL)
2310a3351425Sdougm 			(void) sa_add_protocol_property(protoset, prop);
2311a3351425Sdougm 	}
2312a3351425Sdougm }
2313a3351425Sdougm 
2314a3351425Sdougm /*
2315549ec3ffSdougm  * initprotofromdefault()
2316549ec3ffSdougm  *
2317549ec3ffSdougm  * read the default file(s) and add the defined values to the
2318549ec3ffSdougm  * protoset.  Note that default values are known from the built in
2319549ec3ffSdougm  * table in case the file doesn't have a definition.
2320549ec3ffSdougm  */
2321549ec3ffSdougm 
2322549ec3ffSdougm static int
2323549ec3ffSdougm initprotofromdefault()
2324549ec3ffSdougm {
2325549ec3ffSdougm 	FILE *nfs;
2326549ec3ffSdougm 	char buff[BUFSIZ];
2327549ec3ffSdougm 	char *name;
2328549ec3ffSdougm 	char *value;
2329549ec3ffSdougm 
2330549ec3ffSdougm 	protoset = sa_create_protocol_properties("nfs");
2331549ec3ffSdougm 
2332549ec3ffSdougm 	if (protoset != NULL) {
2333549ec3ffSdougm 		nfs = fopen(NFSADMIN, "r");
2334549ec3ffSdougm 		if (nfs != NULL) {
2335549ec3ffSdougm 			while (fgets(buff, sizeof (buff), nfs) != NULL) {
2336549ec3ffSdougm 				switch (buff[0]) {
2337549ec3ffSdougm 				case '\n':
2338549ec3ffSdougm 				case '#':
2339549ec3ffSdougm 					/* skip */
2340549ec3ffSdougm 					break;
2341549ec3ffSdougm 				default:
2342549ec3ffSdougm 					name = buff;
2343549ec3ffSdougm 					buff[strlen(buff) - 1] = '\0';
2344549ec3ffSdougm 					value = strchr(name, '=');
2345549ec3ffSdougm 					if (value != NULL) {
2346549ec3ffSdougm 						*value++ = '\0';
2347a3351425Sdougm 						extractprop(name, value);
2348549ec3ffSdougm 					}
2349549ec3ffSdougm 				}
2350549ec3ffSdougm 			}
2351549ec3ffSdougm 			if (nfs != NULL)
2352549ec3ffSdougm 				(void) fclose(nfs);
2353549ec3ffSdougm 		}
2354549ec3ffSdougm 	}
2355549ec3ffSdougm 	if (protoset == NULL)
2356549ec3ffSdougm 		return (SA_NO_MEMORY);
2357549ec3ffSdougm 	return (SA_OK);
2358549ec3ffSdougm }
2359549ec3ffSdougm 
2360549ec3ffSdougm /*
2361a3351425Sdougm  * add_defaults()
2362549ec3ffSdougm  *
2363549ec3ffSdougm  * Add the default values for any property not defined in the parsing
2364549ec3ffSdougm  * of the default files. Values are set according to their defined
2365549ec3ffSdougm  * types.
2366549ec3ffSdougm  */
2367549ec3ffSdougm 
2368549ec3ffSdougm static void
2369549ec3ffSdougm add_defaults()
2370549ec3ffSdougm {
2371549ec3ffSdougm 	int i;
2372549ec3ffSdougm 	char number[MAXDIGITS];
2373549ec3ffSdougm 
2374549ec3ffSdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2375549ec3ffSdougm 		sa_property_t prop;
2376a3351425Sdougm 		prop = sa_get_protocol_property(protoset,
2377a3351425Sdougm 		    proto_options[i].name);
2378549ec3ffSdougm 		if (prop == NULL) {
2379549ec3ffSdougm 			/* add the default value */
2380549ec3ffSdougm 			switch (proto_options[i].type) {
2381549ec3ffSdougm 			case OPT_TYPE_NUMBER:
2382549ec3ffSdougm 				(void) snprintf(number, sizeof (number), "%d",
2383549ec3ffSdougm 				    proto_options[i].defvalue.intval);
2384a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2385a3351425Sdougm 				    number);
2386549ec3ffSdougm 				break;
2387549ec3ffSdougm 
2388549ec3ffSdougm 			case OPT_TYPE_BOOLEAN:
2389549ec3ffSdougm 				prop = sa_create_property(proto_options[i].name,
2390549ec3ffSdougm 				    proto_options[i].defvalue.intval ?
2391549ec3ffSdougm 				    "true" : "false");
2392549ec3ffSdougm 				break;
2393549ec3ffSdougm 
2394549ec3ffSdougm 			case OPT_TYPE_ONOFF:
2395549ec3ffSdougm 				prop = sa_create_property(proto_options[i].name,
2396549ec3ffSdougm 				    proto_options[i].defvalue.intval ?
2397549ec3ffSdougm 				    "on" : "off");
2398549ec3ffSdougm 				break;
2399549ec3ffSdougm 
2400549ec3ffSdougm 			default:
2401549ec3ffSdougm 				/* treat as strings of zero length */
2402a3351425Sdougm 				prop = sa_create_property(proto_options[i].name,
2403a3351425Sdougm 				    "");
2404549ec3ffSdougm 				break;
2405549ec3ffSdougm 			}
2406549ec3ffSdougm 			if (prop != NULL)
2407549ec3ffSdougm 				(void) sa_add_protocol_property(protoset, prop);
2408549ec3ffSdougm 		}
2409549ec3ffSdougm 	}
2410549ec3ffSdougm }
2411549ec3ffSdougm 
2412549ec3ffSdougm static void
2413549ec3ffSdougm free_protoprops()
2414549ec3ffSdougm {
2415*aed5d200Sdougm 	if (protoset != NULL) {
2416549ec3ffSdougm 		xmlFreeNode(protoset);
2417*aed5d200Sdougm 		protoset = NULL;
2418*aed5d200Sdougm 	}
2419549ec3ffSdougm }
2420549ec3ffSdougm 
2421549ec3ffSdougm /*
2422549ec3ffSdougm  * nfs_init()
2423549ec3ffSdougm  *
2424549ec3ffSdougm  * Initialize the NFS plugin.
2425549ec3ffSdougm  */
2426549ec3ffSdougm 
2427549ec3ffSdougm static int
2428549ec3ffSdougm nfs_init()
2429549ec3ffSdougm {
2430549ec3ffSdougm 	int ret = SA_OK;
2431549ec3ffSdougm 
2432549ec3ffSdougm 	if (sa_plugin_ops.sa_init != nfs_init)
2433549ec3ffSdougm 		(void) printf(dgettext(TEXT_DOMAIN,
2434549ec3ffSdougm 		    "NFS plugin not properly initialized\n"));
2435549ec3ffSdougm 
2436549ec3ffSdougm 	ret = initprotofromdefault();
2437a3351425Sdougm 	if (ret == SA_OK)
2438549ec3ffSdougm 		add_defaults();
2439549ec3ffSdougm 
2440549ec3ffSdougm 	return (ret);
2441549ec3ffSdougm }
2442549ec3ffSdougm 
2443549ec3ffSdougm /*
2444549ec3ffSdougm  * nfs_fini()
2445549ec3ffSdougm  *
2446549ec3ffSdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
2447549ec3ffSdougm  */
2448549ec3ffSdougm 
2449549ec3ffSdougm static void
2450549ec3ffSdougm nfs_fini()
2451549ec3ffSdougm {
2452549ec3ffSdougm 	free_protoprops();
2453549ec3ffSdougm }
2454549ec3ffSdougm 
2455549ec3ffSdougm /*
2456549ec3ffSdougm  * nfs_get_proto_set()
2457549ec3ffSdougm  *
2458549ec3ffSdougm  * Return an optionset with all the protocol specific properties in
2459549ec3ffSdougm  * it.
2460549ec3ffSdougm  */
2461549ec3ffSdougm 
2462549ec3ffSdougm static sa_protocol_properties_t
2463549ec3ffSdougm nfs_get_proto_set()
2464549ec3ffSdougm {
2465549ec3ffSdougm 	return (protoset);
2466549ec3ffSdougm }
2467549ec3ffSdougm 
2468549ec3ffSdougm struct deffile {
2469549ec3ffSdougm 	struct deffile *next;
2470549ec3ffSdougm 	char *line;
2471549ec3ffSdougm };
2472549ec3ffSdougm 
2473549ec3ffSdougm /*
2474549ec3ffSdougm  * read_default_file(fname)
2475549ec3ffSdougm  *
2476549ec3ffSdougm  * Read the specified default file. We return a list of entries. This
2477549ec3ffSdougm  * get used for adding or removing values.
2478549ec3ffSdougm  */
2479549ec3ffSdougm 
2480549ec3ffSdougm static struct deffile *
2481549ec3ffSdougm read_default_file(char *fname)
2482549ec3ffSdougm {
2483549ec3ffSdougm 	FILE *file;
2484549ec3ffSdougm 	struct deffile *defs = NULL;
2485549ec3ffSdougm 	struct deffile *newdef;
2486549ec3ffSdougm 	struct deffile *prevdef = NULL;
2487549ec3ffSdougm 	char buff[BUFSIZ * 2];
2488549ec3ffSdougm 
2489549ec3ffSdougm 	file = fopen(fname, "r");
2490549ec3ffSdougm 	if (file != NULL) {
2491549ec3ffSdougm 		while (fgets(buff, sizeof (buff), file) != NULL) {
2492a3351425Sdougm 			newdef = (struct deffile *)calloc(1,
2493a3351425Sdougm 			    sizeof (struct deffile));
2494549ec3ffSdougm 			if (newdef != NULL) {
2495330ef417Sdougm 				/* Make sure we skip any leading whitespace. */
2496330ef417Sdougm 				newdef->line = strdup(skipwhitespace(buff));
2497549ec3ffSdougm 				if (defs == NULL) {
2498549ec3ffSdougm 					prevdef = defs = newdef;
2499549ec3ffSdougm 				} else {
2500549ec3ffSdougm 					prevdef->next = newdef;
2501549ec3ffSdougm 					prevdef = newdef;
2502549ec3ffSdougm 				}
2503549ec3ffSdougm 			}
2504549ec3ffSdougm 		}
2505549ec3ffSdougm 	}
2506549ec3ffSdougm 	(void) fclose(file);
2507549ec3ffSdougm 	return (defs);
2508549ec3ffSdougm }
2509549ec3ffSdougm 
2510549ec3ffSdougm static void
2511549ec3ffSdougm free_default_file(struct deffile *defs)
2512549ec3ffSdougm {
2513549ec3ffSdougm 	struct deffile *curdefs = NULL;
2514549ec3ffSdougm 
2515549ec3ffSdougm 	while (defs != NULL) {
2516549ec3ffSdougm 		curdefs = defs;
2517549ec3ffSdougm 		defs = defs->next;
2518549ec3ffSdougm 		if (curdefs->line != NULL)
2519549ec3ffSdougm 			free(curdefs->line);
2520549ec3ffSdougm 		free(curdefs);
2521549ec3ffSdougm 	}
2522549ec3ffSdougm }
2523549ec3ffSdougm 
2524549ec3ffSdougm /*
2525549ec3ffSdougm  * write_default_file(fname, defs)
2526549ec3ffSdougm  *
2527549ec3ffSdougm  * Write the default file back.
2528549ec3ffSdougm  */
2529549ec3ffSdougm 
2530549ec3ffSdougm static int
2531549ec3ffSdougm write_default_file(char *fname, struct deffile *defs)
2532549ec3ffSdougm {
2533549ec3ffSdougm 	FILE *file;
2534549ec3ffSdougm 	int ret = SA_OK;
2535549ec3ffSdougm 	sigset_t old, new;
2536549ec3ffSdougm 
2537549ec3ffSdougm 	file = fopen(fname, "w+");
2538549ec3ffSdougm 	if (file != NULL) {
2539549ec3ffSdougm 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
2540549ec3ffSdougm 		(void) sigaddset(&new, SIGHUP);
2541549ec3ffSdougm 		(void) sigaddset(&new, SIGINT);
2542549ec3ffSdougm 		(void) sigaddset(&new, SIGQUIT);
2543549ec3ffSdougm 		(void) sigaddset(&new, SIGTSTP);
2544549ec3ffSdougm 		(void) sigprocmask(SIG_SETMASK, &new, &old);
2545549ec3ffSdougm 		while (defs != NULL) {
2546549ec3ffSdougm 			(void) fputs(defs->line, file);
2547549ec3ffSdougm 			defs = defs->next;
2548549ec3ffSdougm 		}
2549549ec3ffSdougm 		(void) fsync(fileno(file));
2550549ec3ffSdougm 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
2551549ec3ffSdougm 		(void) fclose(file);
2552549ec3ffSdougm 	} else {
2553549ec3ffSdougm 		switch (errno) {
2554549ec3ffSdougm 		case EPERM:
2555549ec3ffSdougm 		case EACCES:
2556549ec3ffSdougm 			ret = SA_NO_PERMISSION;
2557549ec3ffSdougm 			break;
2558549ec3ffSdougm 		default:
2559549ec3ffSdougm 			ret = SA_SYSTEM_ERR;
2560549ec3ffSdougm 		}
2561549ec3ffSdougm 	}
2562549ec3ffSdougm 	return (ret);
2563549ec3ffSdougm }
2564549ec3ffSdougm 
2565549ec3ffSdougm 
2566549ec3ffSdougm /*
2567549ec3ffSdougm  * set_default_file_value(tag, value)
2568549ec3ffSdougm  *
2569549ec3ffSdougm  * Set the default file value for tag to value. Then rewrite the file.
2570549ec3ffSdougm  * tag and value are always set.  The caller must ensure this.
2571549ec3ffSdougm  */
2572549ec3ffSdougm 
2573549ec3ffSdougm #define	MAX_STRING_LENGTH	256
2574549ec3ffSdougm static int
2575549ec3ffSdougm set_default_file_value(char *tag, char *value)
2576549ec3ffSdougm {
2577549ec3ffSdougm 	int ret = SA_OK;
2578549ec3ffSdougm 	struct deffile *root;
2579549ec3ffSdougm 	struct deffile *defs;
2580549ec3ffSdougm 	struct deffile *prev;
2581549ec3ffSdougm 	char string[MAX_STRING_LENGTH];
2582549ec3ffSdougm 	int len;
2583549ec3ffSdougm 	int update = 0;
2584549ec3ffSdougm 
2585549ec3ffSdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
2586549ec3ffSdougm 	len = strlen(string);
2587549ec3ffSdougm 
2588549ec3ffSdougm 	root = defs = read_default_file(NFSADMIN);
2589549ec3ffSdougm 	if (root == NULL) {
2590549ec3ffSdougm 		if (errno == EPERM || errno == EACCES)
2591549ec3ffSdougm 			ret = SA_NO_PERMISSION;
2592549ec3ffSdougm 		else
2593549ec3ffSdougm 			ret = SA_SYSTEM_ERR;
2594549ec3ffSdougm 	} else {
2595549ec3ffSdougm 		while (defs != NULL) {
2596549ec3ffSdougm 			if (defs->line != NULL &&
2597549ec3ffSdougm 			    strncasecmp(defs->line, string, len) == 0) {
2598549ec3ffSdougm 				/* replace with the new value */
2599549ec3ffSdougm 				free(defs->line);
2600549ec3ffSdougm 				fixcaseupper(tag);
2601a3351425Sdougm 				(void) snprintf(string, sizeof (string),
2602a3351425Sdougm 				    "%s=%s\n", tag, value);
2603549ec3ffSdougm 				string[MAX_STRING_LENGTH - 1] = '\0';
2604549ec3ffSdougm 				defs->line = strdup(string);
2605549ec3ffSdougm 				update = 1;
2606549ec3ffSdougm 				break;
2607549ec3ffSdougm 			}
2608549ec3ffSdougm 			defs = defs->next;
2609549ec3ffSdougm 		}
2610549ec3ffSdougm 		if (!update) {
2611549ec3ffSdougm 			defs = root;
2612549ec3ffSdougm 			/* didn't find, so see if it is a comment */
2613549ec3ffSdougm 			(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
2614549ec3ffSdougm 			len = strlen(string);
2615549ec3ffSdougm 			while (defs != NULL) {
2616549ec3ffSdougm 				if (strncasecmp(defs->line, string, len) == 0) {
2617549ec3ffSdougm 					/* replace with the new value */
2618549ec3ffSdougm 					free(defs->line);
2619549ec3ffSdougm 					fixcaseupper(tag);
2620549ec3ffSdougm 					(void) snprintf(string, sizeof (string),
2621549ec3ffSdougm 					    "%s=%s\n", tag, value);
2622549ec3ffSdougm 					string[MAX_STRING_LENGTH - 1] = '\0';
2623549ec3ffSdougm 					defs->line = strdup(string);
2624549ec3ffSdougm 					update = 1;
2625549ec3ffSdougm 					break;
2626549ec3ffSdougm 				}
2627549ec3ffSdougm 				defs = defs->next;
2628549ec3ffSdougm 			}
2629549ec3ffSdougm 		}
2630549ec3ffSdougm 		if (!update) {
2631549ec3ffSdougm 			fixcaseupper(tag);
2632549ec3ffSdougm 			(void) snprintf(string, sizeof (string), "%s=%s\n",
2633549ec3ffSdougm 			    tag, value);
2634549ec3ffSdougm 			prev = root;
2635549ec3ffSdougm 			while (prev->next != NULL)
2636549ec3ffSdougm 				prev = prev->next;
2637549ec3ffSdougm 			defs = malloc(sizeof (struct deffile));
2638549ec3ffSdougm 			prev->next = defs;
2639549ec3ffSdougm 			if (defs != NULL) {
2640549ec3ffSdougm 				defs->next = NULL;
2641549ec3ffSdougm 				defs->line = strdup(string);
2642549ec3ffSdougm 			}
2643549ec3ffSdougm 		}
2644549ec3ffSdougm 		if (update) {
2645549ec3ffSdougm 			ret = write_default_file(NFSADMIN, root);
2646549ec3ffSdougm 		}
2647549ec3ffSdougm 		free_default_file(root);
2648549ec3ffSdougm 	}
2649549ec3ffSdougm 	return (ret);
2650549ec3ffSdougm }
2651549ec3ffSdougm 
2652549ec3ffSdougm /*
2653549ec3ffSdougm  * service_in_state(service, chkstate)
2654549ec3ffSdougm  *
2655549ec3ffSdougm  * Want to know if the specified service is in the desired state
2656549ec3ffSdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
2657549ec3ffSdougm  * isn't.
2658549ec3ffSdougm  */
2659549ec3ffSdougm static int
2660549ec3ffSdougm service_in_state(char *service, const char *chkstate)
2661549ec3ffSdougm {
2662549ec3ffSdougm 	char *state;
2663549ec3ffSdougm 	int ret = B_FALSE;
2664549ec3ffSdougm 
2665549ec3ffSdougm 	state = smf_get_state(service);
2666549ec3ffSdougm 	if (state != NULL) {
2667549ec3ffSdougm 		/* got the state so get the equality for the return value */
2668549ec3ffSdougm 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2669549ec3ffSdougm 		free(state);
2670549ec3ffSdougm 	}
2671549ec3ffSdougm 	return (ret);
2672549ec3ffSdougm }
2673549ec3ffSdougm 
2674549ec3ffSdougm /*
2675549ec3ffSdougm  * restart_service(svcs)
2676549ec3ffSdougm  *
2677549ec3ffSdougm  * Walk through the bit mask of services that need to be restarted in
2678549ec3ffSdougm  * order to use the new property values. Some properties affect
2679549ec3ffSdougm  * multiple daemons. Should only restart a service if it is currently
2680549ec3ffSdougm  * enabled (online).
2681549ec3ffSdougm  */
2682549ec3ffSdougm 
2683549ec3ffSdougm static void
2684549ec3ffSdougm restart_service(uint32_t svcs)
2685549ec3ffSdougm {
2686549ec3ffSdougm 	uint32_t mask;
2687549ec3ffSdougm 	int ret;
2688549ec3ffSdougm 	char *service;
2689549ec3ffSdougm 
2690549ec3ffSdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
2691549ec3ffSdougm 		switch (svcs & mask) {
2692549ec3ffSdougm 		case SVC_LOCKD:
2693549ec3ffSdougm 			service = LOCKD;
2694549ec3ffSdougm 			break;
2695549ec3ffSdougm 		case SVC_STATD:
2696549ec3ffSdougm 			service = STATD;
2697549ec3ffSdougm 			break;
2698549ec3ffSdougm 		case SVC_NFSD:
2699549ec3ffSdougm 			service = NFSD;
2700549ec3ffSdougm 			break;
2701549ec3ffSdougm 		case SVC_MOUNTD:
2702549ec3ffSdougm 			service = MOUNTD;
2703549ec3ffSdougm 			break;
2704549ec3ffSdougm 		case SVC_NFS4CBD:
2705549ec3ffSdougm 			service = NFS4CBD;
2706549ec3ffSdougm 			break;
2707549ec3ffSdougm 		case SVC_NFSMAPID:
2708549ec3ffSdougm 			service = NFSMAPID;
2709549ec3ffSdougm 			break;
2710549ec3ffSdougm 		case SVC_RQUOTAD:
2711549ec3ffSdougm 			service = RQUOTAD;
2712549ec3ffSdougm 			break;
2713549ec3ffSdougm 		case SVC_NFSLOGD:
2714549ec3ffSdougm 			service = NFSLOGD;
2715549ec3ffSdougm 			break;
2716549ec3ffSdougm 		default:
2717549ec3ffSdougm 			continue;
2718549ec3ffSdougm 		}
2719549ec3ffSdougm 
2720549ec3ffSdougm 		/*
2721549ec3ffSdougm 		 * Only attempt to restart the service if it is
2722549ec3ffSdougm 		 * currently running. In the future, it may be
2723549ec3ffSdougm 		 * desirable to use smf_refresh_instance if the NFS
2724549ec3ffSdougm 		 * services ever implement the refresh method.
2725549ec3ffSdougm 		 */
2726549ec3ffSdougm 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2727549ec3ffSdougm 			ret = smf_restart_instance(service);
2728549ec3ffSdougm 			/*
2729549ec3ffSdougm 			 * There are only a few SMF errors at this point, but
2730549ec3ffSdougm 			 * it is also possible that a bad value may have put
2731549ec3ffSdougm 			 * the service into maintenance if there wasn't an
2732549ec3ffSdougm 			 * SMF level error.
2733549ec3ffSdougm 			 */
2734549ec3ffSdougm 			if (ret != 0) {
2735549ec3ffSdougm 				(void) fprintf(stderr,
2736549ec3ffSdougm 				    dgettext(TEXT_DOMAIN,
2737549ec3ffSdougm 				    "%s failed to restart: %s\n"),
2738549ec3ffSdougm 				    scf_strerror(scf_error()));
2739549ec3ffSdougm 			} else {
2740549ec3ffSdougm 				/*
2741549ec3ffSdougm 				 * Check whether it has gone to "maintenance"
2742549ec3ffSdougm 				 * mode or not. Maintenance implies something
2743549ec3ffSdougm 				 * went wrong.
2744549ec3ffSdougm 				 */
2745a3351425Sdougm 				if (service_in_state(service,
2746a3351425Sdougm 				    SCF_STATE_STRING_MAINT)) {
2747549ec3ffSdougm 					(void) fprintf(stderr,
2748549ec3ffSdougm 					    dgettext(TEXT_DOMAIN,
2749549ec3ffSdougm 					    "%s failed to restart\n"),
2750549ec3ffSdougm 					    service);
2751549ec3ffSdougm 				}
2752549ec3ffSdougm 			}
2753549ec3ffSdougm 		}
2754549ec3ffSdougm 		svcs &= ~mask;
2755549ec3ffSdougm 	}
2756549ec3ffSdougm }
2757549ec3ffSdougm 
2758549ec3ffSdougm /*
2759549ec3ffSdougm  * nfs_minmax_check(name, value)
2760549ec3ffSdougm  *
2761549ec3ffSdougm  * Verify that the value for the property specified by index is valid
2762549ec3ffSdougm  * relative to the opposite value in the case of a min/max variable.
2763549ec3ffSdougm  * Currently, server_minvers/server_maxvers and
2764549ec3ffSdougm  * client_minvers/client_maxvers are the only ones to check.
2765549ec3ffSdougm  */
2766549ec3ffSdougm 
2767549ec3ffSdougm static int
2768549ec3ffSdougm nfs_minmax_check(int index, int value)
2769549ec3ffSdougm {
2770549ec3ffSdougm 	int val;
2771549ec3ffSdougm 	char *pval;
2772549ec3ffSdougm 	sa_property_t prop;
2773549ec3ffSdougm 	sa_optionset_t opts;
2774549ec3ffSdougm 	int ret = B_TRUE;
2775549ec3ffSdougm 
2776549ec3ffSdougm 	if (proto_options[index].other != NULL) {
2777549ec3ffSdougm 		/* have a property to compare against */
2778549ec3ffSdougm 		opts = nfs_get_proto_set();
2779549ec3ffSdougm 		prop = sa_get_property(opts, proto_options[index].other);
2780549ec3ffSdougm 		/*
2781549ec3ffSdougm 		 * If we don't find the property, assume default
2782549ec3ffSdougm 		 * values which will work since the max will be at the
2783549ec3ffSdougm 		 * max and the min at the min.
2784549ec3ffSdougm 		 */
2785549ec3ffSdougm 		if (prop != NULL) {
2786549ec3ffSdougm 			pval = sa_get_property_attr(prop, "value");
2787549ec3ffSdougm 			if (pval != NULL) {
2788549ec3ffSdougm 				val = strtoul(pval, NULL, 0);
2789a3351425Sdougm 				if (proto_options[index].compare ==
2790a3351425Sdougm 				    OPT_CMP_LE) {
2791549ec3ffSdougm 					ret = value <= val ? B_TRUE : B_FALSE;
2792a3351425Sdougm 				} else if (proto_options[index].compare ==
2793a3351425Sdougm 				    OPT_CMP_GE) {
2794549ec3ffSdougm 					ret = value >= val ? B_TRUE : B_FALSE;
2795549ec3ffSdougm 				}
2796549ec3ffSdougm 			}
2797549ec3ffSdougm 		}
2798549ec3ffSdougm 	}
2799549ec3ffSdougm 	return (ret);
2800549ec3ffSdougm }
2801549ec3ffSdougm 
2802549ec3ffSdougm /*
2803549ec3ffSdougm  * nfs_validate_proto_prop(index, name, value)
2804549ec3ffSdougm  *
2805549ec3ffSdougm  * Verify that the property specifed by name can take the new
2806549ec3ffSdougm  * value. This is a sanity check to prevent bad values getting into
2807549ec3ffSdougm  * the default files. All values need to be checked against what is
2808549ec3ffSdougm  * allowed by their defined type. If a type isn't explicitly defined
2809549ec3ffSdougm  * here, it is treated as a string.
2810549ec3ffSdougm  *
2811549ec3ffSdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
2812549ec3ffSdougm  * within the range specified and potentially against another property
2813549ec3ffSdougm  * value as well as specified in the proto_options members other and
2814549ec3ffSdougm  * compare.
2815549ec3ffSdougm  */
2816549ec3ffSdougm 
2817549ec3ffSdougm static int
2818549ec3ffSdougm nfs_validate_proto_prop(int index, char *name, char *value)
2819549ec3ffSdougm {
2820549ec3ffSdougm 	int ret = SA_OK;
2821549ec3ffSdougm 	char *cp;
2822549ec3ffSdougm #ifdef lint
2823549ec3ffSdougm 	name = name;
2824549ec3ffSdougm #endif
2825549ec3ffSdougm 
2826549ec3ffSdougm 	switch (proto_options[index].type) {
2827549ec3ffSdougm 	case OPT_TYPE_NUMBER:
2828549ec3ffSdougm 		if (!is_a_number(value))
2829549ec3ffSdougm 			ret = SA_BAD_VALUE;
2830549ec3ffSdougm 		else {
2831549ec3ffSdougm 			int val;
2832549ec3ffSdougm 			val = strtoul(value, NULL, 0);
2833549ec3ffSdougm 			if (val < proto_options[index].minval ||
2834549ec3ffSdougm 			    val > proto_options[index].maxval)
2835549ec3ffSdougm 				ret = SA_BAD_VALUE;
2836549ec3ffSdougm 			/*
2837549ec3ffSdougm 			 * For server_versmin/server_versmax and
2838549ec3ffSdougm 			 * client_versmin/client_versmax, the value of the
2839549ec3ffSdougm 			 * min(max) should be checked to be correct relative
2840549ec3ffSdougm 			 * to the current max(min).
2841549ec3ffSdougm 			 */
2842549ec3ffSdougm 			if (!nfs_minmax_check(index, val)) {
2843549ec3ffSdougm 				ret = SA_BAD_VALUE;
2844549ec3ffSdougm 			}
2845549ec3ffSdougm 		}
2846549ec3ffSdougm 		break;
2847549ec3ffSdougm 
2848549ec3ffSdougm 	case OPT_TYPE_DOMAIN:
2849549ec3ffSdougm 		/*
2850549ec3ffSdougm 		 * needs to be a qualified domain so will have at
2851549ec3ffSdougm 		 * least one period and other characters on either
2852549ec3ffSdougm 		 * side of it.  A zero length string is also allowed
2853549ec3ffSdougm 		 * and is the way to turn off the override.
2854549ec3ffSdougm 		 */
2855549ec3ffSdougm 		if (strlen(value) == 0)
2856549ec3ffSdougm 			break;
2857549ec3ffSdougm 		cp = strchr(value, '.');
2858549ec3ffSdougm 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
2859549ec3ffSdougm 			ret = SA_BAD_VALUE;
2860549ec3ffSdougm 		break;
2861549ec3ffSdougm 
2862549ec3ffSdougm 	case OPT_TYPE_BOOLEAN:
2863549ec3ffSdougm 		if (strlen(value) == 0 ||
2864549ec3ffSdougm 		    strcasecmp(value, "true") == 0 ||
2865549ec3ffSdougm 		    strcmp(value, "1") == 0 ||
2866549ec3ffSdougm 		    strcasecmp(value, "false") == 0 ||
2867549ec3ffSdougm 		    strcmp(value, "0") == 0) {
2868549ec3ffSdougm 			ret = SA_OK;
2869549ec3ffSdougm 		} else {
2870549ec3ffSdougm 			ret = SA_BAD_VALUE;
2871549ec3ffSdougm 		}
2872549ec3ffSdougm 		break;
2873549ec3ffSdougm 
2874549ec3ffSdougm 	case OPT_TYPE_ONOFF:
2875549ec3ffSdougm 		if (strcasecmp(value, "on") != 0 &&
2876549ec3ffSdougm 		    strcasecmp(value, "off") != 0) {
2877549ec3ffSdougm 			ret = SA_BAD_VALUE;
2878549ec3ffSdougm 		}
2879549ec3ffSdougm 		break;
2880549ec3ffSdougm 
2881549ec3ffSdougm 	case OPT_TYPE_PROTOCOL:
2882549ec3ffSdougm 		if (strcasecmp(value, "all") != 0 &&
2883549ec3ffSdougm 		    strcasecmp(value, "tcp") != 0 &&
2884549ec3ffSdougm 		    strcasecmp(value, "udp") != 0)
2885549ec3ffSdougm 			ret = SA_BAD_VALUE;
2886549ec3ffSdougm 		break;
2887549ec3ffSdougm 
2888549ec3ffSdougm 	default:
2889549ec3ffSdougm 		/* treat as a string */
2890549ec3ffSdougm 		break;
2891549ec3ffSdougm 	}
2892549ec3ffSdougm 	return (ret);
2893549ec3ffSdougm }
2894549ec3ffSdougm 
2895549ec3ffSdougm /*
2896549ec3ffSdougm  * nfs_set_proto_prop(prop)
2897549ec3ffSdougm  *
2898549ec3ffSdougm  * check that prop is valid.
2899549ec3ffSdougm  */
2900549ec3ffSdougm 
2901549ec3ffSdougm static int
2902549ec3ffSdougm nfs_set_proto_prop(sa_property_t prop)
2903549ec3ffSdougm {
2904549ec3ffSdougm 	int ret = SA_OK;
2905549ec3ffSdougm 	char *name;
2906549ec3ffSdougm 	char *value;
2907549ec3ffSdougm 
2908549ec3ffSdougm 	name = sa_get_property_attr(prop, "type");
2909549ec3ffSdougm 	value = sa_get_property_attr(prop, "value");
2910549ec3ffSdougm 	if (name != NULL && value != NULL) {
2911549ec3ffSdougm 		int index = findprotoopt(name, 1);
2912549ec3ffSdougm 		if (index >= 0) {
2913549ec3ffSdougm 			/* should test for valid value */
2914549ec3ffSdougm 			ret = nfs_validate_proto_prop(index, name, value);
2915549ec3ffSdougm 			if (ret == SA_OK)
2916a3351425Sdougm 				ret = set_default_file_value(
2917a3351425Sdougm 				    proto_options[index].tag, value);
2918549ec3ffSdougm 			if (ret == SA_OK)
2919549ec3ffSdougm 				restart_service(proto_options[index].svcs);
2920549ec3ffSdougm 		}
2921549ec3ffSdougm 	}
2922549ec3ffSdougm 	if (name != NULL)
2923549ec3ffSdougm 		sa_free_attr_string(name);
2924549ec3ffSdougm 	if (value != NULL)
2925549ec3ffSdougm 		sa_free_attr_string(value);
2926549ec3ffSdougm 	return (ret);
2927549ec3ffSdougm }
2928549ec3ffSdougm 
2929549ec3ffSdougm /*
2930549ec3ffSdougm  * nfs_get_status()
2931549ec3ffSdougm  *
2932549ec3ffSdougm  * What is the current status of the nfsd? We use the SMF state here.
2933549ec3ffSdougm  * Caller must free the returned value.
2934549ec3ffSdougm  */
2935549ec3ffSdougm 
2936549ec3ffSdougm static char *
2937549ec3ffSdougm nfs_get_status()
2938549ec3ffSdougm {
2939549ec3ffSdougm 	char *state;
2940549ec3ffSdougm 	state = smf_get_state(NFSD);
2941549ec3ffSdougm 	return (state != NULL ? state : strdup("-"));
2942549ec3ffSdougm }
2943549ec3ffSdougm 
2944549ec3ffSdougm /*
2945549ec3ffSdougm  * nfs_space_alias(alias)
2946549ec3ffSdougm  *
2947549ec3ffSdougm  * Lookup the space (security) name. If it is default, convert to the
2948549ec3ffSdougm  * real name.
2949549ec3ffSdougm  */
2950549ec3ffSdougm 
2951549ec3ffSdougm static char *
2952549ec3ffSdougm nfs_space_alias(char *space)
2953549ec3ffSdougm {
2954549ec3ffSdougm 	char *name = space;
2955549ec3ffSdougm 	seconfig_t secconf;
2956549ec3ffSdougm 
2957549ec3ffSdougm 	/*
2958549ec3ffSdougm 	 * Only the space named "default" is special. If it is used,
2959549ec3ffSdougm 	 * the default needs to be looked up and the real name used.
2960549ec3ffSdougm 	 * This is normally "sys" but could be changed.  We always
2961549ec3ffSdougm 	 * change defautl to the real name.
2962549ec3ffSdougm 	 */
2963549ec3ffSdougm 	if (strcmp(space, "default") == 0 &&
2964549ec3ffSdougm 	    nfs_getseconfig_default(&secconf) == 0) {
2965549ec3ffSdougm 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
2966549ec3ffSdougm 			name = secconf.sc_name;
2967549ec3ffSdougm 	}
2968549ec3ffSdougm 	return (strdup(name));
2969549ec3ffSdougm }
2970