1*3910Sdougm /*
2*3910Sdougm  * CDDL HEADER START
3*3910Sdougm  *
4*3910Sdougm  * The contents of this file are subject to the terms of the
5*3910Sdougm  * Common Development and Distribution License (the "License").
6*3910Sdougm  * You may not use this file except in compliance with the License.
7*3910Sdougm  *
8*3910Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3910Sdougm  * or http://www.opensolaris.org/os/licensing.
10*3910Sdougm  * See the License for the specific language governing permissions
11*3910Sdougm  * and limitations under the License.
12*3910Sdougm  *
13*3910Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
14*3910Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3910Sdougm  * If applicable, add the following below this CDDL HEADER, with the
16*3910Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
17*3910Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
18*3910Sdougm  *
19*3910Sdougm  * CDDL HEADER END
20*3910Sdougm  */
21*3910Sdougm 
22*3910Sdougm /*
23*3910Sdougm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*3910Sdougm  * Use is subject to license terms.
25*3910Sdougm  */
26*3910Sdougm 
27*3910Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*3910Sdougm 
29*3910Sdougm /*
30*3910Sdougm  * NFS specific functions
31*3910Sdougm  */
32*3910Sdougm #include <stdio.h>
33*3910Sdougm #include <string.h>
34*3910Sdougm #include <ctype.h>
35*3910Sdougm #include <stdlib.h>
36*3910Sdougm #include <unistd.h>
37*3910Sdougm #include <zone.h>
38*3910Sdougm #include <errno.h>
39*3910Sdougm #include <locale.h>
40*3910Sdougm #include <signal.h>
41*3910Sdougm #include "libshare.h"
42*3910Sdougm #include "libshare_impl.h"
43*3910Sdougm #include <nfs/export.h>
44*3910Sdougm #include <pwd.h>
45*3910Sdougm #include <limits.h>
46*3910Sdougm #include <libscf.h>
47*3910Sdougm #include "nfslog_config.h"
48*3910Sdougm #include "nfslogtab.h"
49*3910Sdougm #include "libshare_nfs.h"
50*3910Sdougm #include <rpcsvc/daemon_utils.h>
51*3910Sdougm #include <nfs/nfs.h>
52*3910Sdougm 
53*3910Sdougm /* should really be in some global place */
54*3910Sdougm #define	DEF_WIN	30000
55*3910Sdougm #define	OPT_CHUNK	1024
56*3910Sdougm 
57*3910Sdougm int debug = 0;
58*3910Sdougm 
59*3910Sdougm 
60*3910Sdougm /* internal functions */
61*3910Sdougm static int nfs_init();
62*3910Sdougm static void nfs_fini();
63*3910Sdougm static int nfs_enable_share(sa_share_t);
64*3910Sdougm static int nfs_disable_share(char *);
65*3910Sdougm static int nfs_validate_property(sa_property_t, sa_optionset_t);
66*3910Sdougm static int nfs_validate_security_mode(char *);
67*3910Sdougm static int nfs_is_security_opt(char *);
68*3910Sdougm static int nfs_parse_legacy_options(sa_group_t, char *);
69*3910Sdougm static char *nfs_format_options(sa_group_t, int);
70*3910Sdougm static int nfs_set_proto_prop(sa_property_t);
71*3910Sdougm static sa_protocol_properties_t nfs_get_proto_set();
72*3910Sdougm static char *nfs_get_status();
73*3910Sdougm static char *nfs_space_alias(char *);
74*3910Sdougm 
75*3910Sdougm /*
76*3910Sdougm  * ops vector that provides the protocol specific info and operations
77*3910Sdougm  * for share management.
78*3910Sdougm  */
79*3910Sdougm 
80*3910Sdougm struct sa_plugin_ops sa_plugin_ops = {
81*3910Sdougm 	SA_PLUGIN_VERSION,
82*3910Sdougm 	"nfs",
83*3910Sdougm 	nfs_init,
84*3910Sdougm 	nfs_fini,
85*3910Sdougm 	nfs_enable_share,
86*3910Sdougm 	nfs_disable_share,
87*3910Sdougm 	nfs_validate_property,
88*3910Sdougm 	nfs_validate_security_mode,
89*3910Sdougm 	nfs_is_security_opt,
90*3910Sdougm 	nfs_parse_legacy_options,
91*3910Sdougm 	nfs_format_options,
92*3910Sdougm 	nfs_set_proto_prop,
93*3910Sdougm 	nfs_get_proto_set,
94*3910Sdougm 	nfs_get_status,
95*3910Sdougm 	nfs_space_alias,
96*3910Sdougm 	NULL,
97*3910Sdougm 	NULL
98*3910Sdougm };
99*3910Sdougm 
100*3910Sdougm /*
101*3910Sdougm  * list of support services needed
102*3910Sdougm  * defines should come from head/rpcsvc/daemon_utils.h
103*3910Sdougm  */
104*3910Sdougm 
105*3910Sdougm static char *service_list_default[] =
106*3910Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL };
107*3910Sdougm static char *service_list_logging[] =
108*3910Sdougm 	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL };
109*3910Sdougm 
110*3910Sdougm /*
111*3910Sdougm  * option definitions.  Make sure to keep the #define for the option
112*3910Sdougm  * index just before the entry it is the index for. Changing the order
113*3910Sdougm  * can cause breakage.  E.g OPT_RW is index 1 and must precede the
114*3910Sdougm  * line that includes the SHOPT_RW and OPT_RW entries.
115*3910Sdougm  */
116*3910Sdougm 
117*3910Sdougm struct option_defs optdefs[] = {
118*3910Sdougm #define	OPT_RO		0
119*3910Sdougm 	{SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST},
120*3910Sdougm #define	OPT_RW		1
121*3910Sdougm 	{SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST},
122*3910Sdougm #define	OPT_ROOT	2
123*3910Sdougm 	{SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST},
124*3910Sdougm #define	OPT_SECURE	3
125*3910Sdougm 	{SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED},
126*3910Sdougm #define	OPT_ANON	4
127*3910Sdougm 	{SHOPT_ANON, OPT_ANON, OPT_TYPE_USER},
128*3910Sdougm #define	OPT_WINDOW	5
129*3910Sdougm 	{SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER},
130*3910Sdougm #define	OPT_NOSUID	6
131*3910Sdougm 	{SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN},
132*3910Sdougm #define	OPT_ACLOK	7
133*3910Sdougm 	{SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN},
134*3910Sdougm #define	OPT_NOSUB	8
135*3910Sdougm 	{SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN},
136*3910Sdougm #define	OPT_SEC		9
137*3910Sdougm 	{SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY},
138*3910Sdougm #define	OPT_PUBLIC	10
139*3910Sdougm 	{SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY},
140*3910Sdougm #define	OPT_INDEX	11
141*3910Sdougm 	{SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE},
142*3910Sdougm #define	OPT_LOG		12
143*3910Sdougm 	{SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG},
144*3910Sdougm #define	OPT_CKSUM	13
145*3910Sdougm 	{SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET},
146*3910Sdougm #ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
147*3910Sdougm #define	OPT_VOLFH	14
148*3910Sdougm 	{SHOPT_VOLFH, OPT_VOLFH},
149*3910Sdougm #endif /* VOLATILE_FH_TEST */
150*3910Sdougm 	NULL
151*3910Sdougm };
152*3910Sdougm 
153*3910Sdougm /*
154*3910Sdougm  * list of properties that are related to security flavors.
155*3910Sdougm  */
156*3910Sdougm static char *seclist[] = {
157*3910Sdougm 	SHOPT_RO,
158*3910Sdougm 	SHOPT_RW,
159*3910Sdougm 	SHOPT_ROOT,
160*3910Sdougm 	SHOPT_WINDOW,
161*3910Sdougm 	NULL
162*3910Sdougm };
163*3910Sdougm 
164*3910Sdougm /* structure for list of securities */
165*3910Sdougm struct securities {
166*3910Sdougm 	sa_security_t security;
167*3910Sdougm 	struct securities *next;
168*3910Sdougm };
169*3910Sdougm 
170*3910Sdougm /*
171*3910Sdougm  * findopt(name)
172*3910Sdougm  *
173*3910Sdougm  * Lookup option "name" in the option table and return the table
174*3910Sdougm  * index.
175*3910Sdougm  */
176*3910Sdougm 
177*3910Sdougm static int
178*3910Sdougm findopt(char *name)
179*3910Sdougm {
180*3910Sdougm 	int i;
181*3910Sdougm 	if (name != NULL) {
182*3910Sdougm 	    for (i = 0; optdefs[i].tag != NULL; i++) {
183*3910Sdougm 		if (strcmp(optdefs[i].tag, name) == 0)
184*3910Sdougm 		    return (i);
185*3910Sdougm 	    }
186*3910Sdougm 	}
187*3910Sdougm 	return (-1);
188*3910Sdougm }
189*3910Sdougm 
190*3910Sdougm /*
191*3910Sdougm  * gettype(name)
192*3910Sdougm  *
193*3910Sdougm  * Return the type of option "name".
194*3910Sdougm  */
195*3910Sdougm 
196*3910Sdougm static int
197*3910Sdougm gettype(char *name)
198*3910Sdougm {
199*3910Sdougm 	int optdef;
200*3910Sdougm 
201*3910Sdougm 	optdef = findopt(name);
202*3910Sdougm 	if (optdef != -1)
203*3910Sdougm 	    return (optdefs[optdef].type);
204*3910Sdougm 	return (OPT_TYPE_ANY);
205*3910Sdougm }
206*3910Sdougm 
207*3910Sdougm /*
208*3910Sdougm  * nfs_validate_security_mode(mode)
209*3910Sdougm  *
210*3910Sdougm  * is the specified mode string a valid one for use with NFS?
211*3910Sdougm  */
212*3910Sdougm 
213*3910Sdougm static int
214*3910Sdougm nfs_validate_security_mode(char *mode)
215*3910Sdougm {
216*3910Sdougm 	seconfig_t secinfo;
217*3910Sdougm 	int err;
218*3910Sdougm 
219*3910Sdougm 	(void) memset(&secinfo, '\0', sizeof (secinfo));
220*3910Sdougm 	err = nfs_getseconfig_byname(mode, &secinfo);
221*3910Sdougm 	if (err == SC_NOERROR)
222*3910Sdougm 	    return (1);
223*3910Sdougm 	return (0);
224*3910Sdougm }
225*3910Sdougm 
226*3910Sdougm /*
227*3910Sdougm  * nfs_is_security_opt(tok)
228*3910Sdougm  *
229*3910Sdougm  * check to see if tok represents an option that is only valid in some
230*3910Sdougm  * security flavor.
231*3910Sdougm  */
232*3910Sdougm 
233*3910Sdougm static int
234*3910Sdougm nfs_is_security_opt(char *tok)
235*3910Sdougm {
236*3910Sdougm 	int i;
237*3910Sdougm 
238*3910Sdougm 	for (i = 0; seclist[i] != NULL; i++) {
239*3910Sdougm 	    if (strcmp(tok, seclist[i]) == 0)
240*3910Sdougm 		return (1);
241*3910Sdougm 	}
242*3910Sdougm 	return (0);
243*3910Sdougm }
244*3910Sdougm 
245*3910Sdougm /*
246*3910Sdougm  * find_security(seclist, sec)
247*3910Sdougm  *
248*3910Sdougm  * Walk the current list of security flavors and return true if it is
249*3910Sdougm  * present, else return false.
250*3910Sdougm  */
251*3910Sdougm 
252*3910Sdougm static int
253*3910Sdougm find_security(struct securities *seclist, sa_security_t sec)
254*3910Sdougm {
255*3910Sdougm 	while (seclist != NULL) {
256*3910Sdougm 	    if (seclist->security == sec)
257*3910Sdougm 		return (1);
258*3910Sdougm 	    seclist = seclist->next;
259*3910Sdougm 	}
260*3910Sdougm 	return (0);
261*3910Sdougm }
262*3910Sdougm 
263*3910Sdougm /*
264*3910Sdougm  * make_security_list(group, securitymodes, proto)
265*3910Sdougm  *	go through the list of securitymodes and add them to the
266*3910Sdougm  *	group's list of security optionsets. We also keep a list of
267*3910Sdougm  *	those optionsets so we don't have to find them later. All of
268*3910Sdougm  *	these will get copies of the same properties.
269*3910Sdougm  */
270*3910Sdougm 
271*3910Sdougm static struct securities *
272*3910Sdougm make_security_list(sa_group_t group, char *securitymodes, char *proto)
273*3910Sdougm {
274*3910Sdougm 	char *tok, *next = NULL;
275*3910Sdougm 	struct securities *curp, *headp = NULL, *prev;
276*3910Sdougm 	sa_security_t check;
277*3910Sdougm 	int freetok = 0;
278*3910Sdougm 
279*3910Sdougm 	for (tok = securitymodes; tok != NULL; tok = next) {
280*3910Sdougm 	    next = strchr(tok, ':');
281*3910Sdougm 	    if (next != NULL)
282*3910Sdougm 		*next++ = '\0';
283*3910Sdougm 	    if (strcmp(tok, "default") == 0) {
284*3910Sdougm 		/* resolve default into the real type */
285*3910Sdougm 		tok = nfs_space_alias(tok);
286*3910Sdougm 		freetok = 1;
287*3910Sdougm 	    }
288*3910Sdougm 	    check = sa_get_security(group, tok, proto);
289*3910Sdougm 
290*3910Sdougm 	    /* add to the security list if it isn't there already */
291*3910Sdougm 	    if (check == NULL || !find_security(headp, check)) {
292*3910Sdougm 		curp = (struct securities *)calloc(1,
293*3910Sdougm 						sizeof (struct securities));
294*3910Sdougm 		if (curp != NULL) {
295*3910Sdougm 		    if (check == NULL) {
296*3910Sdougm 			curp->security = sa_create_security(group, tok,
297*3910Sdougm 								proto);
298*3910Sdougm 		    } else {
299*3910Sdougm 			curp->security = check;
300*3910Sdougm 		    }
301*3910Sdougm 			/*
302*3910Sdougm 			 * note that the first time through the loop,
303*3910Sdougm 			 * headp will be NULL and prev will be
304*3910Sdougm 			 * undefined.  Since headp is NULL, we set
305*3910Sdougm 			 * both it and prev to the curp (first
306*3910Sdougm 			 * structure to be allocated).
307*3910Sdougm 			 *
308*3910Sdougm 			 * later passes through the loop will have
309*3910Sdougm 			 * headp not being NULL and prev will be used
310*3910Sdougm 			 * to allocate at the end of the list.
311*3910Sdougm 			 */
312*3910Sdougm 		    if (headp == NULL) {
313*3910Sdougm 			headp = curp;
314*3910Sdougm 			prev = curp;
315*3910Sdougm 		    } else {
316*3910Sdougm 			prev->next = curp;
317*3910Sdougm 			prev = curp;
318*3910Sdougm 		    }
319*3910Sdougm 		}
320*3910Sdougm 	    }
321*3910Sdougm 
322*3910Sdougm 	    if (freetok) {
323*3910Sdougm 		freetok = 0;
324*3910Sdougm 		sa_free_attr_string(tok);
325*3910Sdougm 	    }
326*3910Sdougm 	}
327*3910Sdougm 	return (headp);
328*3910Sdougm }
329*3910Sdougm 
330*3910Sdougm static void
331*3910Sdougm free_security_list(struct securities *sec)
332*3910Sdougm {
333*3910Sdougm 	struct securities *next;
334*3910Sdougm 	if (sec != NULL) {
335*3910Sdougm 	    for (next = sec->next; sec != NULL; sec = next) {
336*3910Sdougm 		next = sec->next;
337*3910Sdougm 		free(sec);
338*3910Sdougm 	    }
339*3910Sdougm 	}
340*3910Sdougm }
341*3910Sdougm 
342*3910Sdougm /*
343*3910Sdougm  * nfs_alistcat(str1, str2, sep)
344*3910Sdougm  *
345*3910Sdougm  * concatenate str1 and str2 into a new string using sep as a separate
346*3910Sdougm  * character. If memory allocation fails, return NULL;
347*3910Sdougm  */
348*3910Sdougm 
349*3910Sdougm static char *
350*3910Sdougm nfs_alistcat(char *str1, char *str2, char sep)
351*3910Sdougm {
352*3910Sdougm 	char *newstr;
353*3910Sdougm 	size_t len;
354*3910Sdougm 
355*3910Sdougm 	len = strlen(str1) + strlen(str2) + 2;
356*3910Sdougm 	newstr = (char *)malloc(len);
357*3910Sdougm 	if (newstr != NULL)
358*3910Sdougm 	    (void) snprintf(newstr, len, "%s%c%s", str1, sep, str2);
359*3910Sdougm 	return (newstr);
360*3910Sdougm }
361*3910Sdougm 
362*3910Sdougm /*
363*3910Sdougm  * add_security_prop(sec, name, value, persist)
364*3910Sdougm  *
365*3910Sdougm  * Add the property to the securities structure. This accumulates
366*3910Sdougm  * properties for as part of parsing legacy options.
367*3910Sdougm  */
368*3910Sdougm 
369*3910Sdougm static int
370*3910Sdougm add_security_prop(struct securities *sec, char *name, char *value,
371*3910Sdougm 			int persist, int iszfs)
372*3910Sdougm {
373*3910Sdougm 	sa_property_t prop;
374*3910Sdougm 	int ret = SA_OK;
375*3910Sdougm 
376*3910Sdougm 	for (; sec != NULL; sec = sec->next) {
377*3910Sdougm 	    if (value == NULL) {
378*3910Sdougm 		if (strcmp(name, SHOPT_RW) == 0 || strcmp(name, SHOPT_RO) == 0)
379*3910Sdougm 		    value = "*";
380*3910Sdougm 		else
381*3910Sdougm 		    value = "true";
382*3910Sdougm 	    }
383*3910Sdougm 
384*3910Sdougm 		/*
385*3910Sdougm 		 * Get the existing property, if it exists, so we can
386*3910Sdougm 		 * determine what to do with it. The ro/rw/root
387*3910Sdougm 		 * properties can be merged if multiple instances of
388*3910Sdougm 		 * these properies are given. For example, if "rw"
389*3910Sdougm 		 * exists with a value "host1" and a later token of
390*3910Sdougm 		 * rw="host2" is seen, the values are merged into a
391*3910Sdougm 		 * single rw="host1:host2".
392*3910Sdougm 		 */
393*3910Sdougm 	    prop = sa_get_property(sec->security, name);
394*3910Sdougm 
395*3910Sdougm 	    if (prop != NULL) {
396*3910Sdougm 		char *oldvalue;
397*3910Sdougm 		char *newvalue;
398*3910Sdougm 
399*3910Sdougm 		/*
400*3910Sdougm 		 * The security options of ro/rw/root might appear
401*3910Sdougm 		 * multiple times. If they do, the values need to be
402*3910Sdougm 		 * merged into an access list. If it was previously
403*3910Sdougm 		 * empty, the new value alone is added.
404*3910Sdougm 		 */
405*3910Sdougm 		oldvalue = sa_get_property_attr(prop, "value");
406*3910Sdougm 		if (oldvalue != NULL) {
407*3910Sdougm 			/*
408*3910Sdougm 			 * The general case is to concatenate the new
409*3910Sdougm 			 * value onto the old value for multiple
410*3910Sdougm 			 * rw(ro/root) properties. A special case
411*3910Sdougm 			 * exists when either the old or new is the
412*3910Sdougm 			 * "all" case. In the special case, if both
413*3910Sdougm 			 * are "all", then it is "all", else if one is
414*3910Sdougm 			 * an access-list, that replaces the "all".
415*3910Sdougm 			 */
416*3910Sdougm 		    if (strcmp(oldvalue, "*") == 0) {
417*3910Sdougm 			/* Replace old value with new value. */
418*3910Sdougm 			newvalue = strdup(value);
419*3910Sdougm 		    } else if (strcmp(value, "*") == 0) {
420*3910Sdougm 			/* Keep old value and ignore the new value. */
421*3910Sdougm 			newvalue = NULL;
422*3910Sdougm 		    } else {
423*3910Sdougm 			/* Make a new list of old plus new access-list. */
424*3910Sdougm 			newvalue = nfs_alistcat(oldvalue, value, ':');
425*3910Sdougm 		    }
426*3910Sdougm 
427*3910Sdougm 		    if (newvalue != NULL) {
428*3910Sdougm 			(void) sa_remove_property(prop);
429*3910Sdougm 			prop = sa_create_property(name, newvalue);
430*3910Sdougm 			ret = sa_add_property(sec->security, prop);
431*3910Sdougm 			free(newvalue);
432*3910Sdougm 		    }
433*3910Sdougm 		    if (oldvalue != NULL)
434*3910Sdougm 			sa_free_attr_string(oldvalue);
435*3910Sdougm 		}
436*3910Sdougm 	    } else {
437*3910Sdougm 		prop = sa_create_property(name, value);
438*3910Sdougm 		ret = sa_add_property(sec->security, prop);
439*3910Sdougm 	    }
440*3910Sdougm 	    if (ret == SA_OK && !iszfs) {
441*3910Sdougm 		ret = sa_commit_properties(sec->security, !persist);
442*3910Sdougm 	    }
443*3910Sdougm 	}
444*3910Sdougm 	return (ret);
445*3910Sdougm }
446*3910Sdougm 
447*3910Sdougm /*
448*3910Sdougm  * check to see if group/share is persistent.
449*3910Sdougm  */
450*3910Sdougm static int
451*3910Sdougm is_persistent(sa_group_t group)
452*3910Sdougm {
453*3910Sdougm 	char *type;
454*3910Sdougm 	int persist = 1;
455*3910Sdougm 
456*3910Sdougm 	type = sa_get_group_attr(group, "type");
457*3910Sdougm 	if (type != NULL && strcmp(type, "persist") != 0)
458*3910Sdougm 	    persist = 0;
459*3910Sdougm 	if (type != NULL)
460*3910Sdougm 	    sa_free_attr_string(type);
461*3910Sdougm 	return (persist);
462*3910Sdougm }
463*3910Sdougm 
464*3910Sdougm /*
465*3910Sdougm  * invalid_security(options)
466*3910Sdougm  *
467*3910Sdougm  * search option string for any invalid sec= type.
468*3910Sdougm  * return true (1) if any are not valid else false (0)
469*3910Sdougm  */
470*3910Sdougm static int
471*3910Sdougm invalid_security(char *options)
472*3910Sdougm {
473*3910Sdougm 	char *copy, *base, *token, *value;
474*3910Sdougm 	int ret = 0;
475*3910Sdougm 
476*3910Sdougm 	copy = strdup(options);
477*3910Sdougm 	token = base = copy;
478*3910Sdougm 	while (token != NULL && ret == 0) {
479*3910Sdougm 	    token = strtok(base, ",");
480*3910Sdougm 	    base = NULL;
481*3910Sdougm 	    if (token != NULL) {
482*3910Sdougm 		value = strchr(token, '=');
483*3910Sdougm 		if (value != NULL)
484*3910Sdougm 		    *value++ = '\0';
485*3910Sdougm 		if (strcmp(token, "sec") == 0) {
486*3910Sdougm 		    /* have security flavors so check them */
487*3910Sdougm 		    char *tok, *next;
488*3910Sdougm 		    for (next = NULL, tok = value; tok != NULL; tok = next) {
489*3910Sdougm 			next = strchr(tok, ':');
490*3910Sdougm 			if (next != NULL)
491*3910Sdougm 			    *next++ = '\0';
492*3910Sdougm 			ret = !nfs_validate_security_mode(tok);
493*3910Sdougm 			if (ret)
494*3910Sdougm 			    break;
495*3910Sdougm 		    }
496*3910Sdougm 		}
497*3910Sdougm 	    }
498*3910Sdougm 	}
499*3910Sdougm 	if (copy != NULL)
500*3910Sdougm 	    free(copy);
501*3910Sdougm 	return (ret);
502*3910Sdougm }
503*3910Sdougm 
504*3910Sdougm /*
505*3910Sdougm  * nfs_parse_legacy_options(group, options)
506*3910Sdougm  *
507*3910Sdougm  * Parse the old style options into internal format and store on the
508*3910Sdougm  * specified group.  Group could be a share for full legacy support.
509*3910Sdougm  */
510*3910Sdougm 
511*3910Sdougm static int
512*3910Sdougm nfs_parse_legacy_options(sa_group_t group, char *options)
513*3910Sdougm {
514*3910Sdougm 	char *dup = strdup(options);
515*3910Sdougm 	char *base;
516*3910Sdougm 	char *token;
517*3910Sdougm 	sa_optionset_t optionset;
518*3910Sdougm 	struct securities *security_list = NULL;
519*3910Sdougm 	sa_property_t prop;
520*3910Sdougm 	int ret = SA_OK;
521*3910Sdougm 	int iszfs = 0;
522*3910Sdougm 	sa_group_t parent;
523*3910Sdougm 	int persist = 0;
524*3910Sdougm 	char *lasts;
525*3910Sdougm 
526*3910Sdougm 	/* do we have an existing optionset? */
527*3910Sdougm 	optionset = sa_get_optionset(group, "nfs");
528*3910Sdougm 	if (optionset == NULL) {
529*3910Sdougm 	    /* didn't find existing optionset so create one */
530*3910Sdougm 	    optionset = sa_create_optionset(group, "nfs");
531*3910Sdougm 	} else {
532*3910Sdougm 		/*
533*3910Sdougm 		 * have an existing optionset so we need to compare
534*3910Sdougm 		 * options in order to detect errors. For now, we
535*3910Sdougm 		 * assume that the first optionset is the correct one
536*3910Sdougm 		 * and the others will be the same. This needs to be
537*3910Sdougm 		 * fixed before the final code is ready.
538*3910Sdougm 		 */
539*3910Sdougm 	    return (ret);
540*3910Sdougm 	}
541*3910Sdougm 
542*3910Sdougm 	if (strcmp(options, SHOPT_RW) == 0) {
543*3910Sdougm 		/*
544*3910Sdougm 		 * there is a special case of only the option "rw"
545*3910Sdougm 		 * being the default option. We don't have to do
546*3910Sdougm 		 * anything.
547*3910Sdougm 		 */
548*3910Sdougm 	    return (ret);
549*3910Sdougm 	}
550*3910Sdougm 
551*3910Sdougm 	/*
552*3910Sdougm 	 * check if security types are present and validate them. If
553*3910Sdougm 	 * any are not legal, fail.
554*3910Sdougm 	 */
555*3910Sdougm 
556*3910Sdougm 	if (invalid_security(options)) {
557*3910Sdougm 	    return (SA_INVALID_SECURITY);
558*3910Sdougm 	}
559*3910Sdougm 
560*3910Sdougm 	/*
561*3910Sdougm 	 * in order to not attempt to change ZFS properties unless
562*3910Sdougm 	 * absolutely necessary, we never do it in the legacy parsing.
563*3910Sdougm 	 */
564*3910Sdougm 	if (sa_is_share(group)) {
565*3910Sdougm 	    char *zfs;
566*3910Sdougm 	    parent = sa_get_parent_group(group);
567*3910Sdougm 	    if (parent != NULL) {
568*3910Sdougm 		zfs = sa_get_group_attr(parent, "zfs");
569*3910Sdougm 		if (zfs != NULL) {
570*3910Sdougm 		    sa_free_attr_string(zfs);
571*3910Sdougm 		    iszfs++;
572*3910Sdougm 		}
573*3910Sdougm 	    }
574*3910Sdougm 	} else {
575*3910Sdougm 	    iszfs = sa_group_is_zfs(group);
576*3910Sdougm 	}
577*3910Sdougm 
578*3910Sdougm 	/*
579*3910Sdougm 	 * we need to step through each option in the string and then
580*3910Sdougm 	 * add either the option or the security option as needed. If
581*3910Sdougm 	 * this is not a persistent share, don't commit to the
582*3910Sdougm 	 * repository. If there is an error, we also want to abort the
583*3910Sdougm 	 * processing and report it.
584*3910Sdougm 	 */
585*3910Sdougm 	persist = is_persistent(group);
586*3910Sdougm 	base = dup;
587*3910Sdougm 	token = dup;
588*3910Sdougm 	lasts = NULL;
589*3910Sdougm 	while (token != NULL && ret == SA_OK) {
590*3910Sdougm 	    ret = SA_OK;
591*3910Sdougm 	    token = strtok_r(base, ",", &lasts);
592*3910Sdougm 	    base = NULL;
593*3910Sdougm 	    if (token != NULL) {
594*3910Sdougm 		char *value;
595*3910Sdougm 		/*
596*3910Sdougm 		 * if the option has a value, it will have an '=' to
597*3910Sdougm 		 * separate the name from the value. The following
598*3910Sdougm 		 * code will result in value != NULL and token
599*3910Sdougm 		 * pointing to just the name if there is a value.
600*3910Sdougm 		 */
601*3910Sdougm 		value = strchr(token, '=');
602*3910Sdougm 		if (value != NULL) {
603*3910Sdougm 		    *value++ = '\0';
604*3910Sdougm 		}
605*3910Sdougm 		if (strcmp(token, "sec") == 0 || strcmp(token, "secure") == 0) {
606*3910Sdougm 			/*
607*3910Sdougm 			 * Once in security parsing, we only
608*3910Sdougm 			 * do security. We do need to move
609*3910Sdougm 			 * between the security node and the
610*3910Sdougm 			 * toplevel. The security tag goes on
611*3910Sdougm 			 * the root while the following ones
612*3910Sdougm 			 * go on the security.
613*3910Sdougm 			 */
614*3910Sdougm 		    if (security_list != NULL) {
615*3910Sdougm 			/* have an old list so close it and start the new */
616*3910Sdougm 			free_security_list(security_list);
617*3910Sdougm 		    }
618*3910Sdougm 		    if (strcmp(token, "secure") == 0) {
619*3910Sdougm 			value = "dh";
620*3910Sdougm 		    } else {
621*3910Sdougm 			if (value == NULL) {
622*3910Sdougm 			    ret = SA_SYNTAX_ERR;
623*3910Sdougm 			    break;
624*3910Sdougm 			}
625*3910Sdougm 		    }
626*3910Sdougm 		    security_list = make_security_list(group, value, "nfs");
627*3910Sdougm 		} else {
628*3910Sdougm 			/*
629*3910Sdougm 			 * Note that the "old" syntax allowed a
630*3910Sdougm 			 * default security model This must be
631*3910Sdougm 			 * accounted for and internally converted to
632*3910Sdougm 			 * "standard" security structure.
633*3910Sdougm 			 */
634*3910Sdougm 		    if (nfs_is_security_opt(token)) {
635*3910Sdougm 			if (security_list == NULL) {
636*3910Sdougm 				/*
637*3910Sdougm 				 * need to have a security option. This
638*3910Sdougm 				 * will be "closed" when a defined "sec="
639*3910Sdougm 				 * option is seen. This is technically an
640*3910Sdougm 				 * error but will be allowed with warning.
641*3910Sdougm 				 */
642*3910Sdougm 			    security_list = make_security_list(group,
643*3910Sdougm 								"default",
644*3910Sdougm 								"nfs");
645*3910Sdougm 			}
646*3910Sdougm 			if (security_list != NULL) {
647*3910Sdougm 			    ret = add_security_prop(security_list, token,
648*3910Sdougm 							value, persist,
649*3910Sdougm 							iszfs);
650*3910Sdougm 			} else {
651*3910Sdougm 			    ret = SA_NO_MEMORY;
652*3910Sdougm 			}
653*3910Sdougm 		    } else {
654*3910Sdougm 			/* regular options */
655*3910Sdougm 			if (value == NULL) {
656*3910Sdougm 			    if (strcmp(token, SHOPT_RW) == 0 ||
657*3910Sdougm 				strcmp(token, SHOPT_RO) == 0)
658*3910Sdougm 				value = "*";
659*3910Sdougm 			    else if (strcmp(token, SHOPT_LOG) == 0)
660*3910Sdougm 				value = "global";
661*3910Sdougm 			    else
662*3910Sdougm 				value = "true";
663*3910Sdougm 			}
664*3910Sdougm 			prop = sa_create_property(token, value);
665*3910Sdougm 			ret = sa_add_property(optionset, prop);
666*3910Sdougm 			if (ret != SA_OK) {
667*3910Sdougm 			    break;
668*3910Sdougm 			}
669*3910Sdougm 			if (!iszfs) {
670*3910Sdougm 			    ret = sa_commit_properties(optionset, !persist);
671*3910Sdougm 			}
672*3910Sdougm 		    }
673*3910Sdougm 		}
674*3910Sdougm 	    }
675*3910Sdougm 	}
676*3910Sdougm 	if (security_list != NULL)
677*3910Sdougm 	    free_security_list(security_list);
678*3910Sdougm 	if (dup != NULL)
679*3910Sdougm 	    free(dup);
680*3910Sdougm 	return (ret);
681*3910Sdougm }
682*3910Sdougm 
683*3910Sdougm /*
684*3910Sdougm  * is_a_number(number)
685*3910Sdougm  *
686*3910Sdougm  * is the string a number in one of the forms we want to use?
687*3910Sdougm  */
688*3910Sdougm 
689*3910Sdougm static int
690*3910Sdougm is_a_number(char *number)
691*3910Sdougm {
692*3910Sdougm 	int ret = 1;
693*3910Sdougm 	int hex = 0;
694*3910Sdougm 
695*3910Sdougm 	if (strncmp(number, "0x", 2) == 0) {
696*3910Sdougm 	    number += 2;
697*3910Sdougm 	    hex = 1;
698*3910Sdougm 	} else if (*number == '-')
699*3910Sdougm 	    number++; /* skip the minus */
700*3910Sdougm 
701*3910Sdougm 	while (ret == 1 && *number != '\0') {
702*3910Sdougm 	    if (hex) {
703*3910Sdougm 		ret = isxdigit(*number++);
704*3910Sdougm 	    } else {
705*3910Sdougm 		ret = isdigit(*number++);
706*3910Sdougm 	    }
707*3910Sdougm 	}
708*3910Sdougm 	return (ret);
709*3910Sdougm }
710*3910Sdougm 
711*3910Sdougm /*
712*3910Sdougm  * Look for the specified tag in the configuration file. If it is found,
713*3910Sdougm  * enable logging and set the logging configuration information for exp.
714*3910Sdougm  */
715*3910Sdougm static void
716*3910Sdougm configlog(struct exportdata *exp, char *tag)
717*3910Sdougm {
718*3910Sdougm 	nfsl_config_t *configlist = NULL, *configp;
719*3910Sdougm 	int error = 0;
720*3910Sdougm 	char globaltag[] = DEFAULTTAG;
721*3910Sdougm 
722*3910Sdougm 	/*
723*3910Sdougm 	 * Sends config errors to stderr
724*3910Sdougm 	 */
725*3910Sdougm 	nfsl_errs_to_syslog = B_FALSE;
726*3910Sdougm 
727*3910Sdougm 	/*
728*3910Sdougm 	 * get the list of configuration settings
729*3910Sdougm 	 */
730*3910Sdougm 	error = nfsl_getconfig_list(&configlist);
731*3910Sdougm 	if (error) {
732*3910Sdougm 		(void) fprintf(stderr,
733*3910Sdougm 				dgettext(TEXT_DOMAIN,
734*3910Sdougm 					"Cannot get log configuration: %s\n"),
735*3910Sdougm 				strerror(error));
736*3910Sdougm 	}
737*3910Sdougm 
738*3910Sdougm 	if (tag == NULL)
739*3910Sdougm 		tag = globaltag;
740*3910Sdougm 	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
741*3910Sdougm 		nfsl_freeconfig_list(&configlist);
742*3910Sdougm 		(void) fprintf(stderr,
743*3910Sdougm 				dgettext(TEXT_DOMAIN,
744*3910Sdougm 					"No tags matching \"%s\"\n"), tag);
745*3910Sdougm 		/* bad configuration */
746*3910Sdougm 		error = ENOENT;
747*3910Sdougm 		goto err;
748*3910Sdougm 	}
749*3910Sdougm 
750*3910Sdougm 	if ((exp->ex_tag = strdup(tag)) == NULL) {
751*3910Sdougm 		error = ENOMEM;
752*3910Sdougm 		goto out;
753*3910Sdougm 	}
754*3910Sdougm 	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
755*3910Sdougm 		error = ENOMEM;
756*3910Sdougm 		goto out;
757*3910Sdougm 	}
758*3910Sdougm 	exp->ex_flags |= EX_LOG;
759*3910Sdougm 	if (configp->nc_rpclogpath != NULL)
760*3910Sdougm 		exp->ex_flags |= EX_LOG_ALLOPS;
761*3910Sdougm out:
762*3910Sdougm 	if (configlist != NULL)
763*3910Sdougm 	    nfsl_freeconfig_list(&configlist);
764*3910Sdougm 
765*3910Sdougm err:
766*3910Sdougm 	if (error != 0) {
767*3910Sdougm 		if (exp->ex_flags != NULL)
768*3910Sdougm 			free(exp->ex_tag);
769*3910Sdougm 		if (exp->ex_log_buffer != NULL)
770*3910Sdougm 			free(exp->ex_log_buffer);
771*3910Sdougm 		(void) fprintf(stderr,
772*3910Sdougm 				dgettext(TEXT_DOMAIN,
773*3910Sdougm 					"Cannot set log configuration: %s\n"),
774*3910Sdougm 				strerror(error));
775*3910Sdougm 	}
776*3910Sdougm }
777*3910Sdougm 
778*3910Sdougm /*
779*3910Sdougm  * fill_export_from_optionset(export, optionset)
780*3910Sdougm  *
781*3910Sdougm  * In order to share, we need to set all the possible general options
782*3910Sdougm  * into the export structure. Share info will be filled in by the
783*3910Sdougm  * caller. Various property values get turned into structure specific
784*3910Sdougm  * values.
785*3910Sdougm  */
786*3910Sdougm 
787*3910Sdougm static int
788*3910Sdougm fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset)
789*3910Sdougm {
790*3910Sdougm 	sa_property_t option;
791*3910Sdougm 	int ret = SA_OK;
792*3910Sdougm 
793*3910Sdougm 	for (option = sa_get_property(optionset, NULL);
794*3910Sdougm 		option != NULL; option = sa_get_next_property(option)) {
795*3910Sdougm 	    char *name;
796*3910Sdougm 	    char *value;
797*3910Sdougm 	    uint32_t val;
798*3910Sdougm 
799*3910Sdougm 	/*
800*3910Sdougm 	 * since options may be set/reset multiple times, always do an
801*3910Sdougm 	 * explicit set or clear of the option. This allows defaults
802*3910Sdougm 	 * to be set and then the protocol specifici to override.
803*3910Sdougm 	 */
804*3910Sdougm 
805*3910Sdougm 	    name = sa_get_property_attr(option, "type");
806*3910Sdougm 	    value = sa_get_property_attr(option, "value");
807*3910Sdougm 	    switch (findopt(name)) {
808*3910Sdougm 	    case OPT_ANON:
809*3910Sdougm 		if (value != NULL && is_a_number(value)) {
810*3910Sdougm 		    val = strtoul(value, NULL, 0);
811*3910Sdougm 		} else {
812*3910Sdougm 		    struct passwd *pw;
813*3910Sdougm 		    pw = getpwnam(value != NULL ? value : "nobody");
814*3910Sdougm 		    if (pw != NULL) {
815*3910Sdougm 			val = pw->pw_uid;
816*3910Sdougm 		    } else {
817*3910Sdougm 			val = UID_NOBODY;
818*3910Sdougm 		    }
819*3910Sdougm 		    endpwent();
820*3910Sdougm 		}
821*3910Sdougm 		export->ex_anon = val;
822*3910Sdougm 		break;
823*3910Sdougm 	    case OPT_NOSUID:
824*3910Sdougm 		if (value != NULL &&
825*3910Sdougm 		    (strcasecmp(value, "true") == 0 || strcmp(value, "1") == 0))
826*3910Sdougm 		    export->ex_flags |= EX_NOSUID;
827*3910Sdougm 		else
828*3910Sdougm 		    export->ex_flags &= ~EX_NOSUID;
829*3910Sdougm 		break;
830*3910Sdougm 	    case OPT_ACLOK:
831*3910Sdougm 		if (value != NULL &&
832*3910Sdougm 		    (strcasecmp(value, "true") == 0 ||
833*3910Sdougm 			strcmp(value, "1") == 0))
834*3910Sdougm 		    export->ex_flags |= EX_ACLOK;
835*3910Sdougm 		else
836*3910Sdougm 		    export->ex_flags &= ~EX_ACLOK;
837*3910Sdougm 		break;
838*3910Sdougm 	    case OPT_NOSUB:
839*3910Sdougm 		if (value != NULL &&
840*3910Sdougm 		    (strcasecmp(value, "true") == 0 || strcmp(value, "1") == 0))
841*3910Sdougm 		    export->ex_flags |= EX_NOSUB;
842*3910Sdougm 		else
843*3910Sdougm 		    export->ex_flags &= ~EX_NOSUB;
844*3910Sdougm 		break;
845*3910Sdougm 	    case OPT_PUBLIC:
846*3910Sdougm 		if (value != NULL &&
847*3910Sdougm 		    (strcasecmp(value, "true") == 0 || strcmp(value, "1") == 0))
848*3910Sdougm 		    export->ex_flags |= EX_PUBLIC;
849*3910Sdougm 		else
850*3910Sdougm 		    export->ex_flags &= ~EX_PUBLIC;
851*3910Sdougm 		break;
852*3910Sdougm 	    case OPT_INDEX:
853*3910Sdougm 		if (value != NULL &&
854*3910Sdougm 		    (strcmp(value, "..") == 0 || strchr(value, '/') != NULL)) {
855*3910Sdougm 		    /* this is an error */
856*3910Sdougm 		    (void) printf(dgettext(TEXT_DOMAIN,
857*3910Sdougm 					    "NFS: index=\"%s\" not valid;"
858*3910Sdougm 					    "must be a filename.\n"),
859*3910Sdougm 			    value);
860*3910Sdougm 		    break;
861*3910Sdougm 		}
862*3910Sdougm 		if (value != NULL && *value != '\0' &&
863*3910Sdougm 			strcmp(value, ".") != 0) {
864*3910Sdougm 		    /* valid index file string */
865*3910Sdougm 		    if (export->ex_index != NULL) {
866*3910Sdougm 			/* left over from "default" */
867*3910Sdougm 			free(export->ex_index);
868*3910Sdougm 		    }
869*3910Sdougm 		    export->ex_index = strdup(value); /* remember to free */
870*3910Sdougm 		    if (export->ex_index == NULL) {
871*3910Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
872*3910Sdougm 						"NFS: out of memory setting "
873*3910Sdougm 						"index property\n"));
874*3910Sdougm 			break;
875*3910Sdougm 		    }
876*3910Sdougm 		    export->ex_flags |= EX_INDEX;
877*3910Sdougm 		}
878*3910Sdougm 		break;
879*3910Sdougm 	    case OPT_LOG:
880*3910Sdougm 		if (value == NULL)
881*3910Sdougm 		    value = strdup("global");
882*3910Sdougm 		if (value != NULL)
883*3910Sdougm 		    configlog(export, strlen(value) ? value : "global");
884*3910Sdougm 		break;
885*3910Sdougm 	    case OPT_CKSUM:
886*3910Sdougm 		/* TBD: not ready yet */
887*3910Sdougm 		break;
888*3910Sdougm 	    default:
889*3910Sdougm 		/* have a syntactic error */
890*3910Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
891*3910Sdougm 					"NFS: unrecognized option %s=%s\n"),
892*3910Sdougm 			name, value != NULL ? value : "");
893*3910Sdougm 		break;
894*3910Sdougm 	    }
895*3910Sdougm 	    if (name != NULL)
896*3910Sdougm 		sa_free_attr_string(name);
897*3910Sdougm 	    if (value != NULL)
898*3910Sdougm 		sa_free_attr_string(value);
899*3910Sdougm 	}
900*3910Sdougm 	return (ret);
901*3910Sdougm }
902*3910Sdougm 
903*3910Sdougm /*
904*3910Sdougm  * cleanup_export(export)
905*3910Sdougm  *
906*3910Sdougm  * Cleanup the allocated areas so we don't leak memory
907*3910Sdougm  */
908*3910Sdougm 
909*3910Sdougm static void
910*3910Sdougm cleanup_export(struct exportdata *export)
911*3910Sdougm {
912*3910Sdougm 	int i;
913*3910Sdougm 
914*3910Sdougm 	if (export->ex_index != NULL)
915*3910Sdougm 	    free(export->ex_index);
916*3910Sdougm 	if (export->ex_secinfo != NULL) {
917*3910Sdougm 	    for (i = 0; i < export->ex_seccnt; i++)
918*3910Sdougm 		if (export->ex_secinfo[i].s_rootnames != NULL) {
919*3910Sdougm 		    free(export->ex_secinfo[i].s_rootnames);
920*3910Sdougm 		}
921*3910Sdougm 	    free(export->ex_secinfo);
922*3910Sdougm 	}
923*3910Sdougm }
924*3910Sdougm 
925*3910Sdougm /*
926*3910Sdougm  * Given a seconfig entry and a colon-separated
927*3910Sdougm  * list of names, allocate an array big enough
928*3910Sdougm  * to hold the root list, then convert each name to
929*3910Sdougm  * a principal name according to the security
930*3910Sdougm  * info and assign it to an array element.
931*3910Sdougm  * Return the array and its size.
932*3910Sdougm  */
933*3910Sdougm static caddr_t *
934*3910Sdougm get_rootnames(seconfig_t *sec, char *list, int *count)
935*3910Sdougm {
936*3910Sdougm 	caddr_t *a;
937*3910Sdougm 	int c, i;
938*3910Sdougm 	char *host, *p;
939*3910Sdougm 
940*3910Sdougm 	/*
941*3910Sdougm 	 * Count the number of strings in the list.
942*3910Sdougm 	 * This is the number of colon separators + 1.
943*3910Sdougm 	 */
944*3910Sdougm 	c = 1;
945*3910Sdougm 	for (p = list; *p; p++)
946*3910Sdougm 		if (*p == ':')
947*3910Sdougm 			c++;
948*3910Sdougm 	*count = c;
949*3910Sdougm 
950*3910Sdougm 	a = (caddr_t *)malloc(c * sizeof (char *));
951*3910Sdougm 	if (a == NULL) {
952*3910Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
953*3910Sdougm 					"get_rootnames: no memory\n"));
954*3910Sdougm 	} else {
955*3910Sdougm 	    for (i = 0; i < c; i++) {
956*3910Sdougm 		host = strtok(list, ":");
957*3910Sdougm 		if (!nfs_get_root_principal(sec, host, &a[i])) {
958*3910Sdougm 		    free(a);
959*3910Sdougm 		    a = NULL;
960*3910Sdougm 		    break;
961*3910Sdougm 		}
962*3910Sdougm 		list = NULL;
963*3910Sdougm 	    }
964*3910Sdougm 	}
965*3910Sdougm 
966*3910Sdougm 	return (a);
967*3910Sdougm }
968*3910Sdougm 
969*3910Sdougm /*
970*3910Sdougm  * fill_security_from_secopts(sp, secopts)
971*3910Sdougm  *
972*3910Sdougm  * Fill the secinfo structure from the secopts optionset.
973*3910Sdougm  */
974*3910Sdougm 
975*3910Sdougm static int
976*3910Sdougm fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts)
977*3910Sdougm {
978*3910Sdougm 	sa_property_t prop;
979*3910Sdougm 	char *type;
980*3910Sdougm 	int longform;
981*3910Sdougm 	int err = SC_NOERROR;
982*3910Sdougm 
983*3910Sdougm 	type = sa_get_security_attr(secopts, "sectype");
984*3910Sdougm 	if (type != NULL) {
985*3910Sdougm 	    /* named security type needs secinfo to be filled in */
986*3910Sdougm 	    err = nfs_getseconfig_byname(type, &sp->s_secinfo);
987*3910Sdougm 	    sa_free_attr_string(type);
988*3910Sdougm 	    if (err != SC_NOERROR)
989*3910Sdougm 		return (err);
990*3910Sdougm 	} else {
991*3910Sdougm 	    /* default case */
992*3910Sdougm 	    err = nfs_getseconfig_default(&sp->s_secinfo);
993*3910Sdougm 	    if (err != SC_NOERROR)
994*3910Sdougm 		return (err);
995*3910Sdougm 	}
996*3910Sdougm 
997*3910Sdougm 	err = SA_OK;
998*3910Sdougm 	for (prop = sa_get_property(secopts, NULL);
999*3910Sdougm 		prop != NULL && err == SA_OK;
1000*3910Sdougm 		prop = sa_get_next_property(prop)) {
1001*3910Sdougm 	    char *name;
1002*3910Sdougm 	    char *value;
1003*3910Sdougm 
1004*3910Sdougm 	    name = sa_get_property_attr(prop, "type");
1005*3910Sdougm 	    value = sa_get_property_attr(prop, "value");
1006*3910Sdougm 
1007*3910Sdougm 	    longform = value != NULL && strcmp(value, "*") != 0;
1008*3910Sdougm 
1009*3910Sdougm 	    switch (findopt(name)) {
1010*3910Sdougm 	    case OPT_RO:
1011*3910Sdougm 		sp->s_flags |= longform ? M_ROL : M_RO;
1012*3910Sdougm 		break;
1013*3910Sdougm 	    case OPT_RW:
1014*3910Sdougm 		sp->s_flags |= longform ? M_RWL : M_RW;
1015*3910Sdougm 		break;
1016*3910Sdougm 	    case OPT_ROOT:
1017*3910Sdougm 		sp->s_flags |= M_ROOT;
1018*3910Sdougm 		/*
1019*3910Sdougm 		 * if we are using AUTH_UNIX, handle like other things
1020*3910Sdougm 		 * such as RO/RW
1021*3910Sdougm 		 */
1022*3910Sdougm 		if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX)
1023*3910Sdougm 		    continue;
1024*3910Sdougm 		/* not AUTH_UNIX */
1025*3910Sdougm 		if (value != NULL) {
1026*3910Sdougm 		    sp->s_rootnames = get_rootnames(&sp->s_secinfo, value,
1027*3910Sdougm 							&sp->s_rootcnt);
1028*3910Sdougm 		    if (sp->s_rootnames == NULL) {
1029*3910Sdougm 			err = SA_BAD_VALUE;
1030*3910Sdougm 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1031*3910Sdougm 							"Bad root list\n"));
1032*3910Sdougm 		    }
1033*3910Sdougm 		}
1034*3910Sdougm 		break;
1035*3910Sdougm 	    case OPT_WINDOW:
1036*3910Sdougm 		if (value != NULL) {
1037*3910Sdougm 		    sp->s_window = atoi(value);
1038*3910Sdougm 		    if (sp->s_window < 0)
1039*3910Sdougm 			sp->s_window = DEF_WIN; /* just in case */
1040*3910Sdougm 		}
1041*3910Sdougm 		break;
1042*3910Sdougm 	    default:
1043*3910Sdougm 		break;
1044*3910Sdougm 	    }
1045*3910Sdougm 	    if (name != NULL)
1046*3910Sdougm 		sa_free_attr_string(name);
1047*3910Sdougm 	    if (value != NULL)
1048*3910Sdougm 		sa_free_attr_string(value);
1049*3910Sdougm 	}
1050*3910Sdougm 	/* if rw/ro options not set, use default of RW */
1051*3910Sdougm 	if ((sp->s_flags & NFS_RWMODES) == 0)
1052*3910Sdougm 	    sp->s_flags |= M_RW;
1053*3910Sdougm 	return (err);
1054*3910Sdougm }
1055*3910Sdougm 
1056*3910Sdougm /*
1057*3910Sdougm  * This is for testing only
1058*3910Sdougm  * It displays the export structure that
1059*3910Sdougm  * goes into the kernel.
1060*3910Sdougm  */
1061*3910Sdougm static void
1062*3910Sdougm printarg(char *path, struct exportdata *ep)
1063*3910Sdougm {
1064*3910Sdougm 	int i, j;
1065*3910Sdougm 	struct secinfo *sp;
1066*3910Sdougm 
1067*3910Sdougm 	if (debug == 0)
1068*3910Sdougm 	    return;
1069*3910Sdougm 
1070*3910Sdougm 	(void) printf("%s:\n", path);
1071*3910Sdougm 	(void) printf("\tex_version = %d\n", ep->ex_version);
1072*3910Sdougm 	(void) printf("\tex_path = %s\n", ep->ex_path);
1073*3910Sdougm 	(void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen);
1074*3910Sdougm 	(void) printf("\tex_flags: (0x%02x) ", ep->ex_flags);
1075*3910Sdougm 	if (ep->ex_flags & EX_NOSUID)
1076*3910Sdougm 		(void) printf("NOSUID ");
1077*3910Sdougm 	if (ep->ex_flags & EX_ACLOK)
1078*3910Sdougm 		(void) printf("ACLOK ");
1079*3910Sdougm 	if (ep->ex_flags & EX_PUBLIC)
1080*3910Sdougm 		(void) printf("PUBLIC ");
1081*3910Sdougm 	if (ep->ex_flags & EX_NOSUB)
1082*3910Sdougm 		(void) printf("NOSUB ");
1083*3910Sdougm 	if (ep->ex_flags & EX_LOG)
1084*3910Sdougm 		(void) printf("LOG ");
1085*3910Sdougm 	if (ep->ex_flags & EX_LOG_ALLOPS)
1086*3910Sdougm 		(void) printf("LOG_ALLOPS ");
1087*3910Sdougm 	if (ep->ex_flags == 0)
1088*3910Sdougm 		(void) printf("(none)");
1089*3910Sdougm 	(void) 	printf("\n");
1090*3910Sdougm 	if (ep->ex_flags & EX_LOG) {
1091*3910Sdougm 		(void) printf("\tex_log_buffer = %s\n",
1092*3910Sdougm 			(ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
1093*3910Sdougm 		(void) printf("\tex_tag = %s\n",
1094*3910Sdougm 				(ep->ex_tag ? ep->ex_tag : "(NULL)"));
1095*3910Sdougm 	}
1096*3910Sdougm 	(void) printf("\tex_anon = %d\n", ep->ex_anon);
1097*3910Sdougm 	(void) printf("\tex_seccnt = %d\n", ep->ex_seccnt);
1098*3910Sdougm 	(void) printf("\n");
1099*3910Sdougm 	for (i = 0; i < ep->ex_seccnt; i++) {
1100*3910Sdougm 		sp = &ep->ex_secinfo[i];
1101*3910Sdougm 		(void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
1102*3910Sdougm 		(void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
1103*3910Sdougm 		if (sp->s_flags & M_ROOT) (void) printf("M_ROOT ");
1104*3910Sdougm 		if (sp->s_flags & M_RO) (void) printf("M_RO ");
1105*3910Sdougm 		if (sp->s_flags & M_ROL) (void) printf("M_ROL ");
1106*3910Sdougm 		if (sp->s_flags & M_RW) (void) printf("M_RW ");
1107*3910Sdougm 		if (sp->s_flags & M_RWL) (void) printf("M_RWL ");
1108*3910Sdougm 		if (sp->s_flags == 0) (void) printf("(none)");
1109*3910Sdougm 		(void) printf("\n");
1110*3910Sdougm 		(void) printf("\t\ts_window = %d\n", sp->s_window);
1111*3910Sdougm 		(void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
1112*3910Sdougm 		(void) fflush(stdout);
1113*3910Sdougm 		for (j = 0; j < sp->s_rootcnt; j++)
1114*3910Sdougm 			(void) printf("%s ", sp->s_rootnames[j] ?
1115*3910Sdougm 					sp->s_rootnames[j] : "<null>");
1116*3910Sdougm 		(void) printf("\n\n");
1117*3910Sdougm 	}
1118*3910Sdougm }
1119*3910Sdougm 
1120*3910Sdougm /*
1121*3910Sdougm  * count_security(opts)
1122*3910Sdougm  *
1123*3910Sdougm  * Count the number of security types (flavors). The optionset has
1124*3910Sdougm  * been populated with the security flavors as a holding mechanism.
1125*3910Sdougm  * We later use this number to allocate data structures.
1126*3910Sdougm  */
1127*3910Sdougm 
1128*3910Sdougm static int
1129*3910Sdougm count_security(sa_optionset_t opts)
1130*3910Sdougm {
1131*3910Sdougm 	int count = 0;
1132*3910Sdougm 	sa_property_t prop;
1133*3910Sdougm 	if (opts != NULL) {
1134*3910Sdougm 	    for (prop = sa_get_property(opts, NULL); prop != NULL;
1135*3910Sdougm 		prop = sa_get_next_property(prop)) {
1136*3910Sdougm 		count++;
1137*3910Sdougm 	    }
1138*3910Sdougm 	}
1139*3910Sdougm 	return (count);
1140*3910Sdougm }
1141*3910Sdougm 
1142*3910Sdougm /*
1143*3910Sdougm  * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep)
1144*3910Sdougm  *
1145*3910Sdougm  * provides a mechanism to format NFS properties into legacy output
1146*3910Sdougm  * format. If the buffer would overflow, it is reallocated and grown
1147*3910Sdougm  * as appropriate. Special cases of converting internal form of values
1148*3910Sdougm  * to those used by "share" are done. this function does one property
1149*3910Sdougm  * at a time.
1150*3910Sdougm  */
1151*3910Sdougm 
1152*3910Sdougm static void
1153*3910Sdougm nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr,
1154*3910Sdougm 			sa_property_t prop, int sep)
1155*3910Sdougm {
1156*3910Sdougm 	char *name;
1157*3910Sdougm 	char *value;
1158*3910Sdougm 	int curlen;
1159*3910Sdougm 	char *buff = *rbuff;
1160*3910Sdougm 	size_t buffsize = *rbuffsize;
1161*3910Sdougm 
1162*3910Sdougm 	name = sa_get_property_attr(prop, "type");
1163*3910Sdougm 	value = sa_get_property_attr(prop, "value");
1164*3910Sdougm 	if (buff != NULL)
1165*3910Sdougm 	    curlen = strlen(buff);
1166*3910Sdougm 	else
1167*3910Sdougm 	    curlen = 0;
1168*3910Sdougm 	if (name != NULL) {
1169*3910Sdougm 	    int len;
1170*3910Sdougm 	    len = strlen(name) + sep;
1171*3910Sdougm 
1172*3910Sdougm 		/*
1173*3910Sdougm 		 * A future RFE would be to replace this with more
1174*3910Sdougm 		 * generic code and to possibly handle more types.
1175*3910Sdougm 		 */
1176*3910Sdougm 	    switch (gettype(name)) {
1177*3910Sdougm 	    case OPT_TYPE_BOOLEAN:
1178*3910Sdougm 		if (value != NULL && strcasecmp(value, "false") == 0) {
1179*3910Sdougm 		    *name = '\0';
1180*3910Sdougm 		}
1181*3910Sdougm 		if (value != NULL)
1182*3910Sdougm 		    sa_free_attr_string(value);
1183*3910Sdougm 		value = NULL;
1184*3910Sdougm 		break;
1185*3910Sdougm 	    case OPT_TYPE_ACCLIST:
1186*3910Sdougm 		if (value != NULL && strcmp(value, "*") == 0) {
1187*3910Sdougm 		    sa_free_attr_string(value);
1188*3910Sdougm 		    value = NULL;
1189*3910Sdougm 		} else {
1190*3910Sdougm 		    if (value != NULL)
1191*3910Sdougm 			len += 1 + strlen(value);
1192*3910Sdougm 		}
1193*3910Sdougm 		break;
1194*3910Sdougm 	    case OPT_TYPE_LOGTAG:
1195*3910Sdougm 		if (value != NULL && strlen(value) == 0) {
1196*3910Sdougm 		    sa_free_attr_string(value);
1197*3910Sdougm 		    value = NULL;
1198*3910Sdougm 		} else {
1199*3910Sdougm 		    if (value != NULL)
1200*3910Sdougm 			len += 1 + strlen(value);
1201*3910Sdougm 		}
1202*3910Sdougm 		break;
1203*3910Sdougm 	    default:
1204*3910Sdougm 		if (value != NULL)
1205*3910Sdougm 		    len += 1 + strlen(value);
1206*3910Sdougm 		break;
1207*3910Sdougm 	    }
1208*3910Sdougm 	    while (buffsize <= (curlen + len)) {
1209*3910Sdougm 		/* need more room */
1210*3910Sdougm 		buffsize += incr;
1211*3910Sdougm 		buff = realloc(buff, buffsize);
1212*3910Sdougm 		if (buff == NULL) {
1213*3910Sdougm 		    /* realloc failed so free everything */
1214*3910Sdougm 		    if (*rbuff != NULL)
1215*3910Sdougm 			free(*rbuff);
1216*3910Sdougm 		}
1217*3910Sdougm 		*rbuff = buff;
1218*3910Sdougm 		*rbuffsize = buffsize;
1219*3910Sdougm 		if (buff == NULL) {
1220*3910Sdougm 		    return;
1221*3910Sdougm 		}
1222*3910Sdougm 	    }
1223*3910Sdougm 	    if (buff == NULL)
1224*3910Sdougm 		return;
1225*3910Sdougm 	    if (value == NULL)
1226*3910Sdougm 		(void) snprintf(buff + curlen, buffsize - curlen,
1227*3910Sdougm 				"%s%s", sep ? "," : "",
1228*3910Sdougm 				name, value != NULL ? value : "");
1229*3910Sdougm 	    else
1230*3910Sdougm 		(void) snprintf(buff + curlen, buffsize - curlen,
1231*3910Sdougm 				"%s%s=%s", sep ? "," : "",
1232*3910Sdougm 				name, value != NULL ? value : "");
1233*3910Sdougm 	}
1234*3910Sdougm 	if (name != NULL)
1235*3910Sdougm 	    sa_free_attr_string(name);
1236*3910Sdougm 	if (value != NULL)
1237*3910Sdougm 	    sa_free_attr_string(value);
1238*3910Sdougm }
1239*3910Sdougm 
1240*3910Sdougm /*
1241*3910Sdougm  * nfs_format_options(group, hier)
1242*3910Sdougm  *
1243*3910Sdougm  * format all the options on the group into an old-style option
1244*3910Sdougm  * string. If hier is non-zero, walk up the tree to get inherited
1245*3910Sdougm  * options.
1246*3910Sdougm  */
1247*3910Sdougm 
1248*3910Sdougm static char *
1249*3910Sdougm nfs_format_options(sa_group_t group, int hier)
1250*3910Sdougm {
1251*3910Sdougm 	sa_optionset_t options = NULL;
1252*3910Sdougm 	sa_optionset_t secoptions;
1253*3910Sdougm 	sa_property_t prop, secprop;
1254*3910Sdougm 	sa_security_t security;
1255*3910Sdougm 	char *buff;
1256*3910Sdougm 	size_t buffsize;
1257*3910Sdougm 
1258*3910Sdougm 	options = sa_get_derived_optionset(group, "nfs", hier);
1259*3910Sdougm 
1260*3910Sdougm 	/*
1261*3910Sdougm 	 * have a an optionset relative to this item, if any. format
1262*3910Sdougm 	 * these then add any security definitions.
1263*3910Sdougm 	 */
1264*3910Sdougm 	buff = malloc(OPT_CHUNK);
1265*3910Sdougm 	if (buff != NULL) {
1266*3910Sdougm 	    int sep = 0;
1267*3910Sdougm 	    buff[0] = '\0';
1268*3910Sdougm 	    buffsize = OPT_CHUNK;
1269*3910Sdougm 		/*
1270*3910Sdougm 		 * do the default set first but skip any option that is also
1271*3910Sdougm 		 * in the protocol specific optionset.
1272*3910Sdougm 		 */
1273*3910Sdougm 	    if (options != NULL) {
1274*3910Sdougm 		for (prop = sa_get_property(options, NULL); prop != NULL;
1275*3910Sdougm 			prop = sa_get_next_property(prop)) {
1276*3910Sdougm 			/*
1277*3910Sdougm 			 * use this one since we skipped any of these that
1278*3910Sdougm 			 * were also in optdefault
1279*3910Sdougm 			 */
1280*3910Sdougm 		    nfs_sprint_option(&buff, &buffsize, OPT_CHUNK, prop, sep);
1281*3910Sdougm 		    if (buff == NULL) {
1282*3910Sdougm 			/*
1283*3910Sdougm 			 * buff could become NULL if there isn't
1284*3910Sdougm 			 * enough memory for nfs_sprint_option to
1285*3910Sdougm 			 * realloc() as necessary. We can't really do
1286*3910Sdougm 			 * anything about it at this point so we
1287*3910Sdougm 			 * return NULL.  The caller should handle the
1288*3910Sdougm 			 * failure. Note that this
1289*3910Sdougm 			 */
1290*3910Sdougm 			return (buff);
1291*3910Sdougm 		    }
1292*3910Sdougm 		    sep = 1;
1293*3910Sdougm 		}
1294*3910Sdougm 	    }
1295*3910Sdougm 	    secoptions = (sa_optionset_t)sa_get_all_security_types(group,
1296*3910Sdougm 								"nfs", hier);
1297*3910Sdougm 	    if (secoptions != NULL) {
1298*3910Sdougm 		for (secprop = sa_get_property(secoptions, NULL);
1299*3910Sdougm 		    secprop != NULL; secprop = sa_get_next_property(secprop)) {
1300*3910Sdougm 		    char *sectype;
1301*3910Sdougm 
1302*3910Sdougm 		    sectype = sa_get_property_attr(secprop, "type");
1303*3910Sdougm 		    security = (sa_security_t)sa_get_derived_security(group,
1304*3910Sdougm 								sectype,
1305*3910Sdougm 								"nfs", hier);
1306*3910Sdougm 		    if (security != NULL) {
1307*3910Sdougm 			if (sectype != NULL) {
1308*3910Sdougm 			    prop = sa_create_property("sec", sectype);
1309*3910Sdougm 			    nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1310*3910Sdougm 						prop, sep);
1311*3910Sdougm 			    (void) sa_remove_property(prop);
1312*3910Sdougm 			sep = 1;
1313*3910Sdougm 			}
1314*3910Sdougm 			for (prop = sa_get_property(security, NULL);
1315*3910Sdougm 			    prop != NULL;
1316*3910Sdougm 			    prop = sa_get_next_property(prop)) {
1317*3910Sdougm 
1318*3910Sdougm 			    nfs_sprint_option(&buff, &buffsize, OPT_CHUNK,
1319*3910Sdougm 						prop, sep);
1320*3910Sdougm 			    if (buff == NULL) {
1321*3910Sdougm 				/* catastrophic memory failure */
1322*3910Sdougm 				sa_free_derived_optionset(secoptions);
1323*3910Sdougm 				if (security != NULL)
1324*3910Sdougm 				    sa_free_derived_optionset(security);
1325*3910Sdougm 				if (sectype != NULL)
1326*3910Sdougm 				    sa_free_attr_string(sectype);
1327*3910Sdougm 				if (options != NULL)
1328*3910Sdougm 				    sa_free_derived_optionset(options);
1329*3910Sdougm 				return (buff);
1330*3910Sdougm 			    }
1331*3910Sdougm 			    sep = 1;
1332*3910Sdougm 			}
1333*3910Sdougm 			sa_free_derived_optionset(security);
1334*3910Sdougm 		    }
1335*3910Sdougm 		    if (sectype != NULL)
1336*3910Sdougm 			sa_free_attr_string(sectype);
1337*3910Sdougm 		}
1338*3910Sdougm 		sa_free_derived_optionset(secoptions);
1339*3910Sdougm 	    }
1340*3910Sdougm 	}
1341*3910Sdougm 	if (options != NULL)
1342*3910Sdougm 	    sa_free_derived_optionset(options);
1343*3910Sdougm 	return (buff);
1344*3910Sdougm }
1345*3910Sdougm /*
1346*3910Sdougm  * Append an entry to the nfslogtab file
1347*3910Sdougm  */
1348*3910Sdougm static int
1349*3910Sdougm nfslogtab_add(dir, buffer, tag)
1350*3910Sdougm 	char *dir, *buffer, *tag;
1351*3910Sdougm {
1352*3910Sdougm 	FILE *f;
1353*3910Sdougm 	struct logtab_ent lep;
1354*3910Sdougm 	int error = 0;
1355*3910Sdougm 
1356*3910Sdougm 	/*
1357*3910Sdougm 	 * Open the file for update and create it if necessary.
1358*3910Sdougm 	 * This may leave the I/O offset at the end of the file,
1359*3910Sdougm 	 * so rewind back to the beginning of the file.
1360*3910Sdougm 	 */
1361*3910Sdougm 	f = fopen(NFSLOGTAB, "a+");
1362*3910Sdougm 	if (f == NULL) {
1363*3910Sdougm 		error = errno;
1364*3910Sdougm 		goto out;
1365*3910Sdougm 	}
1366*3910Sdougm 	rewind(f);
1367*3910Sdougm 
1368*3910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1369*3910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1370*3910Sdougm 			"share complete, however failed to lock %s "
1371*3910Sdougm 			"for update: %s\n"), NFSLOGTAB, strerror(errno));
1372*3910Sdougm 		error = -1;
1373*3910Sdougm 		goto out;
1374*3910Sdougm 	}
1375*3910Sdougm 
1376*3910Sdougm 	if (logtab_deactivate_after_boot(f) == -1) {
1377*3910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1378*3910Sdougm 			"share complete, however could not deactivate "
1379*3910Sdougm 			"entries in %s\n"), NFSLOGTAB);
1380*3910Sdougm 		error = -1;
1381*3910Sdougm 		goto out;
1382*3910Sdougm 	}
1383*3910Sdougm 
1384*3910Sdougm 	/*
1385*3910Sdougm 	 * Remove entries matching buffer and sharepoint since we're
1386*3910Sdougm 	 * going to replace it with perhaps an entry with a new tag.
1387*3910Sdougm 	 */
1388*3910Sdougm 	if (logtab_rement(f, buffer, dir, NULL, -1)) {
1389*3910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1390*3910Sdougm 			"share complete, however could not remove matching "
1391*3910Sdougm 			"entries in %s\n"), NFSLOGTAB);
1392*3910Sdougm 		error = -1;
1393*3910Sdougm 		goto out;
1394*3910Sdougm 	}
1395*3910Sdougm 
1396*3910Sdougm 	/*
1397*3910Sdougm 	 * Deactivate all active entries matching this sharepoint
1398*3910Sdougm 	 */
1399*3910Sdougm 	if (logtab_deactivate(f, NULL, dir, NULL)) {
1400*3910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1401*3910Sdougm 			"share complete, however could not deactivate matching "
1402*3910Sdougm 			"entries in %s\n"), NFSLOGTAB);
1403*3910Sdougm 		error = -1;
1404*3910Sdougm 		goto out;
1405*3910Sdougm 	}
1406*3910Sdougm 
1407*3910Sdougm 	lep.le_buffer = buffer;
1408*3910Sdougm 	lep.le_path = dir;
1409*3910Sdougm 	lep.le_tag = tag;
1410*3910Sdougm 	lep.le_state = LES_ACTIVE;
1411*3910Sdougm 
1412*3910Sdougm 	/*
1413*3910Sdougm 	 * Add new sharepoint / buffer location to nfslogtab
1414*3910Sdougm 	 */
1415*3910Sdougm 	if (logtab_putent(f, &lep) < 0) {
1416*3910Sdougm 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1417*3910Sdougm 			"share complete, however could not add %s to %s\n"),
1418*3910Sdougm 			dir, NFSLOGTAB);
1419*3910Sdougm 		error = -1;
1420*3910Sdougm 	}
1421*3910Sdougm 
1422*3910Sdougm out:
1423*3910Sdougm 	if (f != NULL)
1424*3910Sdougm 		(void) fclose(f);
1425*3910Sdougm 	return (error);
1426*3910Sdougm }
1427*3910Sdougm 
1428*3910Sdougm /*
1429*3910Sdougm  * Deactivate an entry from the nfslogtab file
1430*3910Sdougm  */
1431*3910Sdougm static int
1432*3910Sdougm nfslogtab_deactivate(path)
1433*3910Sdougm 	char *path;
1434*3910Sdougm {
1435*3910Sdougm 	FILE *f;
1436*3910Sdougm 	int error = 0;
1437*3910Sdougm 
1438*3910Sdougm 	f = fopen(NFSLOGTAB, "r+");
1439*3910Sdougm 	if (f == NULL) {
1440*3910Sdougm 		error = errno;
1441*3910Sdougm 		goto out;
1442*3910Sdougm 	}
1443*3910Sdougm 	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
1444*3910Sdougm 		error = errno;
1445*3910Sdougm 		(void)  fprintf(stderr, dgettext(TEXT_DOMAIN,
1446*3910Sdougm 			"share complete, however could not lock %s for "
1447*3910Sdougm 			"update: %s\n"), NFSLOGTAB, strerror(error));
1448*3910Sdougm 		goto out;
1449*3910Sdougm 	}
1450*3910Sdougm 	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
1451*3910Sdougm 		error = -1;
1452*3910Sdougm 		(void) fprintf(stderr,
1453*3910Sdougm 			dgettext(TEXT_DOMAIN,
1454*3910Sdougm 				"share complete, however could not "
1455*3910Sdougm 				"deactivate %s in %s\n"), path, NFSLOGTAB);
1456*3910Sdougm 		goto out;
1457*3910Sdougm 	}
1458*3910Sdougm 
1459*3910Sdougm out:	if (f != NULL)
1460*3910Sdougm 		(void) fclose(f);
1461*3910Sdougm 
1462*3910Sdougm 	return (error);
1463*3910Sdougm }
1464*3910Sdougm 
1465*3910Sdougm /*
1466*3910Sdougm  * public_exists(share)
1467*3910Sdougm  *
1468*3910Sdougm  * check to see if public option is set on any other share than the
1469*3910Sdougm  * one specified.
1470*3910Sdougm  */
1471*3910Sdougm static int
1472*3910Sdougm public_exists(sa_share_t skipshare)
1473*3910Sdougm {
1474*3910Sdougm 	sa_share_t share;
1475*3910Sdougm 	sa_group_t group;
1476*3910Sdougm 	sa_optionset_t opt;
1477*3910Sdougm 	sa_property_t prop;
1478*3910Sdougm 	int exists = 0;
1479*3910Sdougm 	sa_handle_t handle;
1480*3910Sdougm 
1481*3910Sdougm 	group = sa_get_parent_group(skipshare);
1482*3910Sdougm 	if (group == NULL)
1483*3910Sdougm 	    return (SA_NO_SUCH_GROUP);
1484*3910Sdougm 
1485*3910Sdougm 	handle = sa_find_group_handle(group);
1486*3910Sdougm 	if (handle == NULL)
1487*3910Sdougm 	    return (SA_SYSTEM_ERR);
1488*3910Sdougm 
1489*3910Sdougm 	for (group = sa_get_group(handle, NULL); group != NULL;
1490*3910Sdougm 	    group = sa_get_next_group(group)) {
1491*3910Sdougm 	    for (share = sa_get_share(group, NULL); share != NULL;
1492*3910Sdougm 		share = sa_get_next_share(share)) {
1493*3910Sdougm 		if (share == skipshare)
1494*3910Sdougm 		    continue;
1495*3910Sdougm 		opt = sa_get_optionset(share, "nfs");
1496*3910Sdougm 		if (opt != NULL) {
1497*3910Sdougm 		    prop = sa_get_property(opt, "public");
1498*3910Sdougm 		    if (prop != NULL) {
1499*3910Sdougm 			char *shared;
1500*3910Sdougm 			shared = sa_get_share_attr(share, "shared");
1501*3910Sdougm 			if (shared != NULL) {
1502*3910Sdougm 			    exists = strcmp(shared, "true") == 0;
1503*3910Sdougm 			    sa_free_attr_string(shared);
1504*3910Sdougm 			    goto out;
1505*3910Sdougm 			}
1506*3910Sdougm 		    }
1507*3910Sdougm 		}
1508*3910Sdougm 	    }
1509*3910Sdougm 	}
1510*3910Sdougm out:
1511*3910Sdougm 	return (exists);
1512*3910Sdougm }
1513*3910Sdougm 
1514*3910Sdougm /*
1515*3910Sdougm  * sa_enable_share at the protocol level, enable_share must tell the
1516*3910Sdougm  * implementation that it is to enable the share. This entails
1517*3910Sdougm  * converting the path and options into the appropriate ioctl
1518*3910Sdougm  * calls. It is assumed that all error checking of paths, etc. were
1519*3910Sdougm  * done earlier.
1520*3910Sdougm  */
1521*3910Sdougm static int
1522*3910Sdougm nfs_enable_share(sa_share_t share)
1523*3910Sdougm {
1524*3910Sdougm 	struct exportdata export;
1525*3910Sdougm 	sa_optionset_t secoptlist;
1526*3910Sdougm 	struct secinfo *sp;
1527*3910Sdougm 	int num_secinfo;
1528*3910Sdougm 	sa_optionset_t opt;
1529*3910Sdougm 	sa_security_t sec;
1530*3910Sdougm 	sa_property_t prop;
1531*3910Sdougm 	char *path;
1532*3910Sdougm 	int err = SA_OK;
1533*3910Sdougm 
1534*3910Sdougm 	/* Don't drop core if the NFS module isn't loaded. */
1535*3910Sdougm 	(void) signal(SIGSYS, SIG_IGN);
1536*3910Sdougm 
1537*3910Sdougm 	/* get the path since it is important in several places */
1538*3910Sdougm 	path = sa_get_share_attr(share, "path");
1539*3910Sdougm 	if (path == NULL)
1540*3910Sdougm 	    return (SA_NO_SUCH_PATH);
1541*3910Sdougm 
1542*3910Sdougm 	/*
1543*3910Sdougm 	 * find the optionsets and security sets.  There may not be
1544*3910Sdougm 	 * any or there could be one or two for each of optionset and
1545*3910Sdougm 	 * security may have multiple, one per security type per
1546*3910Sdougm 	 * protocol type.
1547*3910Sdougm 	 */
1548*3910Sdougm 	opt = sa_get_derived_optionset(share, "nfs", 1);
1549*3910Sdougm 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
1550*3910Sdougm 	if (secoptlist != NULL)
1551*3910Sdougm 	    num_secinfo = MAX(1, count_security(secoptlist));
1552*3910Sdougm 	else
1553*3910Sdougm 	    num_secinfo = 1;
1554*3910Sdougm 
1555*3910Sdougm 	/*
1556*3910Sdougm 	 * walk through the options and fill in the structure
1557*3910Sdougm 	 * appropriately.
1558*3910Sdougm 	 */
1559*3910Sdougm 
1560*3910Sdougm 	(void) memset(&export, '\0', sizeof (export));
1561*3910Sdougm 
1562*3910Sdougm 	/*
1563*3910Sdougm 	 * do non-security options first since there is only one after
1564*3910Sdougm 	 * the derived group is constructed.
1565*3910Sdougm 	 */
1566*3910Sdougm 	export.ex_version = EX_CURRENT_VERSION;
1567*3910Sdougm 	export.ex_anon = UID_NOBODY; /* this is our default value */
1568*3910Sdougm 	export.ex_index = NULL;
1569*3910Sdougm 	export.ex_path = path;
1570*3910Sdougm 	export.ex_pathlen = strlen(path) + 1;
1571*3910Sdougm 
1572*3910Sdougm 	sp = calloc(num_secinfo, sizeof (struct secinfo));
1573*3910Sdougm 
1574*3910Sdougm 	if (opt != NULL)
1575*3910Sdougm 	    err = fill_export_from_optionset(&export, opt);
1576*3910Sdougm 
1577*3910Sdougm 	/*
1578*3910Sdougm 	 * check to see if "public" is set. If it is, then make sure
1579*3910Sdougm 	 * no other share has it set. If it is already used, fail.
1580*3910Sdougm 	 */
1581*3910Sdougm 
1582*3910Sdougm 	if (export.ex_flags & EX_PUBLIC && public_exists(share)) {
1583*3910Sdougm 	    (void) printf(dgettext(TEXT_DOMAIN,
1584*3910Sdougm 					"NFS: Cannot share more than one file "
1585*3910Sdougm 			    "system with 'public' property\n"));
1586*3910Sdougm 	    err = SA_NOT_ALLOWED;
1587*3910Sdougm 	    goto out;
1588*3910Sdougm 	}
1589*3910Sdougm 
1590*3910Sdougm 	if (sp == NULL) {
1591*3910Sdougm 		/* failed to alloc memory */
1592*3910Sdougm 	    (void) printf("NFS: no memory for security\n");
1593*3910Sdougm 	    err = SA_NO_MEMORY;
1594*3910Sdougm 	} else {
1595*3910Sdougm 	    int i;
1596*3910Sdougm 	    export.ex_secinfo = sp;
1597*3910Sdougm 	    /* get default secinfo */
1598*3910Sdougm 	    export.ex_seccnt = num_secinfo;
1599*3910Sdougm 		/*
1600*3910Sdougm 		 * since we must have one security option defined, we
1601*3910Sdougm 		 * init to the default and then override as we find
1602*3910Sdougm 		 * defined security options. This handles the case
1603*3910Sdougm 		 * where we have no defined options but we need to set
1604*3910Sdougm 		 * up one.
1605*3910Sdougm 		 */
1606*3910Sdougm 	    sp[0].s_window = DEF_WIN;
1607*3910Sdougm 	    sp[0].s_rootnames = NULL;
1608*3910Sdougm 	    /* setup a default in case no properties defined */
1609*3910Sdougm 	    if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1610*3910Sdougm 		(void) printf(dgettext(TEXT_DOMAIN,
1611*3910Sdougm 				    "NFS: nfs_getseconfig_default: failed to "
1612*3910Sdougm 				    "get default security mode\n"));
1613*3910Sdougm 		err = SA_CONFIG_ERR;
1614*3910Sdougm 	    }
1615*3910Sdougm 	    if (secoptlist != NULL) {
1616*3910Sdougm 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
1617*3910Sdougm 		    prop != NULL && i < num_secinfo;
1618*3910Sdougm 		    prop = sa_get_next_property(prop), i++) {
1619*3910Sdougm 		    char *sectype;
1620*3910Sdougm 
1621*3910Sdougm 		    sectype = sa_get_property_attr(prop, "type");
1622*3910Sdougm 		    /* if sectype is NULL, we can't do anything so skip */
1623*3910Sdougm 		    if (sectype == NULL)
1624*3910Sdougm 			continue;
1625*3910Sdougm 		    sec = (sa_security_t)sa_get_derived_security(share,
1626*3910Sdougm 								sectype,
1627*3910Sdougm 								"nfs", 1);
1628*3910Sdougm 		    sp[i].s_window = DEF_WIN;
1629*3910Sdougm 		    sp[i].s_rootcnt = 0;
1630*3910Sdougm 		    sp[i].s_rootnames = NULL;
1631*3910Sdougm 
1632*3910Sdougm 		    (void) fill_security_from_secopts(&sp[i], sec);
1633*3910Sdougm 		    if (sec != NULL)
1634*3910Sdougm 			sa_free_derived_security(sec);
1635*3910Sdougm 		    if (sectype != NULL)
1636*3910Sdougm 			sa_free_attr_string(sectype);
1637*3910Sdougm 		}
1638*3910Sdougm 	    }
1639*3910Sdougm 		/*
1640*3910Sdougm 		 * when we get here, we can do the exportfs system call and
1641*3910Sdougm 		 * initiate thinsg. We probably want to enable the nfs.server
1642*3910Sdougm 		 * service first if it isn't running within SMF.
1643*3910Sdougm 		 */
1644*3910Sdougm 		/* check nfs.server status and start if needed */
1645*3910Sdougm 
1646*3910Sdougm 		/* now add the share to the internal tables */
1647*3910Sdougm 	    printarg(path, &export);
1648*3910Sdougm 		/*
1649*3910Sdougm 		 * call the exportfs system call which is implemented
1650*3910Sdougm 		 * via the nfssys() call as the EXPORTFS subfunction.
1651*3910Sdougm 		 */
1652*3910Sdougm 	    if ((err = exportfs(path, &export)) < 0) {
1653*3910Sdougm 		err = SA_SYSTEM_ERR;
1654*3910Sdougm 		switch (errno) {
1655*3910Sdougm 		case EREMOTE:
1656*3910Sdougm 		    (void) printf(dgettext(TEXT_DOMAIN,
1657*3910Sdougm 						"NFS: Cannot share remote file"
1658*3910Sdougm 						"system: %s\n"),
1659*3910Sdougm 					path);
1660*3910Sdougm 		    break;
1661*3910Sdougm 		case EPERM:
1662*3910Sdougm 		    if (getzoneid() != GLOBAL_ZONEID) {
1663*3910Sdougm 			(void) printf(dgettext(TEXT_DOMAIN,
1664*3910Sdougm 					"NFS: Cannot share file systems "
1665*3910Sdougm 					"in non-global zones: %s\n"), path);
1666*3910Sdougm 			err = SA_NOT_SUPPORTED;
1667*3910Sdougm 			break;
1668*3910Sdougm 		    }
1669*3910Sdougm 		    err = SA_NO_PERMISSION;
1670*3910Sdougm 		    /* FALLTHROUGH */
1671*3910Sdougm 		default:
1672*3910Sdougm 		    break;
1673*3910Sdougm 		}
1674*3910Sdougm 	    } else {
1675*3910Sdougm 		/* update sharetab with an add/modify */
1676*3910Sdougm 		(void) sa_update_sharetab(share, "nfs");
1677*3910Sdougm 	    }
1678*3910Sdougm 	}
1679*3910Sdougm 
1680*3910Sdougm 	if (err == SA_OK) {
1681*3910Sdougm 		/*
1682*3910Sdougm 		 * enable services as needed. This should probably be
1683*3910Sdougm 		 * done elsewhere in order to minimize the calls to
1684*3910Sdougm 		 * check services.
1685*3910Sdougm 		 */
1686*3910Sdougm 		/*
1687*3910Sdougm 		 * check to see if logging and other services need to
1688*3910Sdougm 		 * be triggered, but only if there wasn't an
1689*3910Sdougm 		 * error. This is probably where sharetab should be
1690*3910Sdougm 		 * updated with the NFS specific entry.
1691*3910Sdougm 		 */
1692*3910Sdougm 	    if (export.ex_flags & EX_LOG) {
1693*3910Sdougm 		/* enable logging */
1694*3910Sdougm 		if (nfslogtab_add(path, export.ex_log_buffer,
1695*3910Sdougm 		    export.ex_tag) != 0) {
1696*3910Sdougm 		    (void) fprintf(stderr,
1697*3910Sdougm 				dgettext(TEXT_DOMAIN,
1698*3910Sdougm 					"Could not enable logging for %s\n"),
1699*3910Sdougm 				path);
1700*3910Sdougm 		}
1701*3910Sdougm 		_check_services(service_list_logging);
1702*3910Sdougm 	    } else {
1703*3910Sdougm 		/*
1704*3910Sdougm 		 * don't have logging so remove it from file. It might
1705*3910Sdougm 		 * not be thre, but that doesn't matter.
1706*3910Sdougm 		 */
1707*3910Sdougm 		(void) nfslogtab_deactivate(path);
1708*3910Sdougm 		_check_services(service_list_default);
1709*3910Sdougm 	    }
1710*3910Sdougm 	}
1711*3910Sdougm 
1712*3910Sdougm out:
1713*3910Sdougm 	if (path != NULL)
1714*3910Sdougm 	    free(path);
1715*3910Sdougm 
1716*3910Sdougm 	cleanup_export(&export);
1717*3910Sdougm 	if (opt != NULL)
1718*3910Sdougm 	    sa_free_derived_optionset(opt);
1719*3910Sdougm 	if (secoptlist != NULL)
1720*3910Sdougm 	    (void) sa_destroy_optionset(secoptlist);
1721*3910Sdougm 	return (err);
1722*3910Sdougm }
1723*3910Sdougm 
1724*3910Sdougm /*
1725*3910Sdougm  * nfs_disable_share(share)
1726*3910Sdougm  *
1727*3910Sdougm  * Unshare the specified share.  How much error checking should be
1728*3910Sdougm  * done? We only do basic errors for now.
1729*3910Sdougm  */
1730*3910Sdougm static int
1731*3910Sdougm nfs_disable_share(char *share)
1732*3910Sdougm {
1733*3910Sdougm 	int err;
1734*3910Sdougm 	int ret = SA_OK;
1735*3910Sdougm 
1736*3910Sdougm 	if (share != NULL) {
1737*3910Sdougm 	    err = exportfs(share, NULL);
1738*3910Sdougm 	    if (err < 0) {
1739*3910Sdougm 		/* TBD: only an error in some cases - need better analysis */
1740*3910Sdougm 		switch (errno) {
1741*3910Sdougm 		case EPERM:
1742*3910Sdougm 		case EACCES:
1743*3910Sdougm 		    ret = SA_NO_PERMISSION;
1744*3910Sdougm 		    if (getzoneid() != GLOBAL_ZONEID) {
1745*3910Sdougm 			ret = SA_NOT_SUPPORTED;
1746*3910Sdougm 		    }
1747*3910Sdougm 		    break;
1748*3910Sdougm 		case EINVAL:
1749*3910Sdougm 		case ENOENT:
1750*3910Sdougm 		    ret = SA_NO_SUCH_PATH;
1751*3910Sdougm 		    break;
1752*3910Sdougm 		default:
1753*3910Sdougm 		    ret = SA_SYSTEM_ERR;
1754*3910Sdougm 		    break;
1755*3910Sdougm 		}
1756*3910Sdougm 	    }
1757*3910Sdougm 	    if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
1758*3910Sdougm 		(void) sa_delete_sharetab(share, "nfs");
1759*3910Sdougm 		/* just in case it was logged */
1760*3910Sdougm 		(void) nfslogtab_deactivate(share);
1761*3910Sdougm 	    }
1762*3910Sdougm 	}
1763*3910Sdougm 	return (ret);
1764*3910Sdougm }
1765*3910Sdougm 
1766*3910Sdougm /*
1767*3910Sdougm  * check ro vs rw values.  Over time this may get beefed up.
1768*3910Sdougm  * for now it just does simple checks.
1769*3910Sdougm  */
1770*3910Sdougm 
1771*3910Sdougm static int
1772*3910Sdougm check_rorw(char *v1, char *v2)
1773*3910Sdougm {
1774*3910Sdougm 	int ret = SA_OK;
1775*3910Sdougm 	if (strcmp(v1, v2) == 0)
1776*3910Sdougm 	    ret = SA_VALUE_CONFLICT;
1777*3910Sdougm 	return (ret);
1778*3910Sdougm }
1779*3910Sdougm 
1780*3910Sdougm /*
1781*3910Sdougm  * nfs_validate_property(property, parent)
1782*3910Sdougm  *
1783*3910Sdougm  * Check that the property has a legitimate value for its type.
1784*3910Sdougm  */
1785*3910Sdougm 
1786*3910Sdougm static int
1787*3910Sdougm nfs_validate_property(sa_property_t property, sa_optionset_t parent)
1788*3910Sdougm {
1789*3910Sdougm 	int ret = SA_OK;
1790*3910Sdougm 	char *propname;
1791*3910Sdougm 	char *other;
1792*3910Sdougm 	int optindex;
1793*3910Sdougm 	nfsl_config_t *configlist;
1794*3910Sdougm 	sa_group_t parent_group;
1795*3910Sdougm 	char *value;
1796*3910Sdougm 
1797*3910Sdougm 	propname = sa_get_property_attr(property, "type");
1798*3910Sdougm 
1799*3910Sdougm 	if ((optindex = findopt(propname)) < 0)
1800*3910Sdougm 	    ret = SA_NO_SUCH_PROP;
1801*3910Sdougm 
1802*3910Sdougm 	/* need to validate value range here as well */
1803*3910Sdougm 
1804*3910Sdougm 	if (ret == SA_OK) {
1805*3910Sdougm 	    parent_group = sa_get_parent_group((sa_share_t)parent);
1806*3910Sdougm 	    if (optdefs[optindex].share && !sa_is_share(parent_group)) {
1807*3910Sdougm 		ret = SA_PROP_SHARE_ONLY;
1808*3910Sdougm 	    }
1809*3910Sdougm 	}
1810*3910Sdougm 	if (ret == SA_OK) {
1811*3910Sdougm 	    value = sa_get_property_attr(property, "value");
1812*3910Sdougm 	    if (value != NULL) {
1813*3910Sdougm 		/* first basic type checking */
1814*3910Sdougm 		switch (optdefs[optindex].type) {
1815*3910Sdougm 		case OPT_TYPE_NUMBER:
1816*3910Sdougm 		    /* check that the value is all digits */
1817*3910Sdougm 		    if (!is_a_number(value))
1818*3910Sdougm 			ret = SA_BAD_VALUE;
1819*3910Sdougm 		    break;
1820*3910Sdougm 		case OPT_TYPE_BOOLEAN:
1821*3910Sdougm 		    if (strlen(value) == 0 ||
1822*3910Sdougm 			strcasecmp(value, "true") == 0 ||
1823*3910Sdougm 			strcmp(value, "1") == 0 ||
1824*3910Sdougm 			strcasecmp(value, "false") == 0 ||
1825*3910Sdougm 			strcmp(value, "0") == 0) {
1826*3910Sdougm 			ret = SA_OK;
1827*3910Sdougm 		    } else {
1828*3910Sdougm 			ret = SA_BAD_VALUE;
1829*3910Sdougm 		    }
1830*3910Sdougm 		    break;
1831*3910Sdougm 		case OPT_TYPE_USER:
1832*3910Sdougm 		    if (!is_a_number(value)) {
1833*3910Sdougm 			struct passwd *pw;
1834*3910Sdougm 			/* in this case it would have to be a user name */
1835*3910Sdougm 			pw = getpwnam(value);
1836*3910Sdougm 			if (pw == NULL)
1837*3910Sdougm 			    ret = SA_BAD_VALUE;
1838*3910Sdougm 			endpwent();
1839*3910Sdougm 		    } else {
1840*3910Sdougm 			uint64_t intval;
1841*3910Sdougm 			intval = strtoull(value, NULL, 0);
1842*3910Sdougm 			if (intval > UID_MAX && intval != ~0)
1843*3910Sdougm 			    ret = SA_BAD_VALUE;
1844*3910Sdougm 		    }
1845*3910Sdougm 		    break;
1846*3910Sdougm 		case OPT_TYPE_FILE:
1847*3910Sdougm 		    if (strcmp(value, "..") == 0 ||
1848*3910Sdougm 			strchr(value, '/') != NULL) {
1849*3910Sdougm 			ret = SA_BAD_VALUE;
1850*3910Sdougm 		    }
1851*3910Sdougm 		    break;
1852*3910Sdougm 		case OPT_TYPE_ACCLIST:
1853*3910Sdougm 			/*
1854*3910Sdougm 			 * access list handling. Should eventually
1855*3910Sdougm 			 * validate that all the values make sense.
1856*3910Sdougm 			 * Also, ro and rw may have cross value
1857*3910Sdougm 			 * conflicts.
1858*3910Sdougm 			 */
1859*3910Sdougm 		    if (strcmp(propname, SHOPT_RO) == 0)
1860*3910Sdougm 			other = SHOPT_RW;
1861*3910Sdougm 		    else if (strcmp(propname, SHOPT_RW) == 0)
1862*3910Sdougm 			other = SHOPT_RO;
1863*3910Sdougm 		    else
1864*3910Sdougm 			other = NULL;
1865*3910Sdougm 		    if (other != NULL && parent != NULL) {
1866*3910Sdougm 			/* compare rw(ro) with ro(rw) */
1867*3910Sdougm 			sa_property_t oprop;
1868*3910Sdougm 			oprop = sa_get_property(parent, other);
1869*3910Sdougm 			if (oprop != NULL) {
1870*3910Sdougm 			    /* only potential confusion if other exists */
1871*3910Sdougm 			    char *ovalue;
1872*3910Sdougm 			    ovalue = sa_get_property_attr(oprop, "value");
1873*3910Sdougm 			    if (ovalue != NULL) {
1874*3910Sdougm 				ret = check_rorw(value, ovalue);
1875*3910Sdougm 				sa_free_attr_string(ovalue);
1876*3910Sdougm 			    }
1877*3910Sdougm 			}
1878*3910Sdougm 		    }
1879*3910Sdougm 		    break;
1880*3910Sdougm 		case OPT_TYPE_LOGTAG:
1881*3910Sdougm 		    if (nfsl_getconfig_list(&configlist) == 0) {
1882*3910Sdougm 			int error;
1883*3910Sdougm 			if (value == NULL || strlen(value) == 0) {
1884*3910Sdougm 			    if (value != NULL)
1885*3910Sdougm 				sa_free_attr_string(value);
1886*3910Sdougm 			    value = strdup("global");
1887*3910Sdougm 			}
1888*3910Sdougm 			if (nfsl_findconfig(configlist, value, &error) == NULL)
1889*3910Sdougm 			    ret = SA_BAD_VALUE;
1890*3910Sdougm 			nfsl_freeconfig_list(&configlist);
1891*3910Sdougm 		    } else {
1892*3910Sdougm 			ret = SA_CONFIG_ERR;
1893*3910Sdougm 		    }
1894*3910Sdougm 		    break;
1895*3910Sdougm 		case OPT_TYPE_STRING:
1896*3910Sdougm 		    /* whatever is here should be ok */
1897*3910Sdougm 		    break;
1898*3910Sdougm 		case OPT_TYPE_SECURITY:
1899*3910Sdougm 			/*
1900*3910Sdougm 			 * The "sec" property isn't used in the
1901*3910Sdougm 			 * non-legacy parts of sharemgr. We need to
1902*3910Sdougm 			 * reject it here. For legacy, it is pulled
1903*3910Sdougm 			 * out well before we get here.
1904*3910Sdougm 			 */
1905*3910Sdougm 		    ret = SA_NO_SUCH_PROP;
1906*3910Sdougm 		    break;
1907*3910Sdougm 		default:
1908*3910Sdougm 		    break;
1909*3910Sdougm 		}
1910*3910Sdougm 		sa_free_attr_string(value);
1911*3910Sdougm 		if (ret == SA_OK && optdefs[optindex].check != NULL) {
1912*3910Sdougm 		    /* do the property specific check */
1913*3910Sdougm 		    ret = optdefs[optindex].check(property);
1914*3910Sdougm 		}
1915*3910Sdougm 	    }
1916*3910Sdougm 	}
1917*3910Sdougm 
1918*3910Sdougm 	if (propname != NULL)
1919*3910Sdougm 	    sa_free_attr_string(propname);
1920*3910Sdougm 	return (ret);
1921*3910Sdougm }
1922*3910Sdougm 
1923*3910Sdougm /*
1924*3910Sdougm  * Protocol management functions
1925*3910Sdougm  *
1926*3910Sdougm  * Properties defined in the default files are defined in
1927*3910Sdougm  * proto_option_defs for parsing and validation. If "other" and
1928*3910Sdougm  * "compare" are set, then the value for this property should be
1929*3910Sdougm  * compared against the property specified in "other" using the
1930*3910Sdougm  * "compare" check (either <= or >=) in order to ensure that the
1931*3910Sdougm  * values are in the correct range.  E.g. setting server_versmin
1932*3910Sdougm  * higher than server_versmax should not be allowed.
1933*3910Sdougm  */
1934*3910Sdougm 
1935*3910Sdougm struct proto_option_defs {
1936*3910Sdougm 	char *tag;
1937*3910Sdougm 	char *name;	/* display name -- remove protocol identifier */
1938*3910Sdougm 	int index;
1939*3910Sdougm 	int type;
1940*3910Sdougm 	union {
1941*3910Sdougm 	    int intval;
1942*3910Sdougm 	    char *string;
1943*3910Sdougm 	} defvalue;
1944*3910Sdougm 	uint32_t svcs;
1945*3910Sdougm 	int32_t minval;
1946*3910Sdougm 	int32_t maxval;
1947*3910Sdougm 	char *file;
1948*3910Sdougm 	char *other;
1949*3910Sdougm 	int compare;
1950*3910Sdougm #define	OPT_CMP_GE	0
1951*3910Sdougm #define	OPT_CMP_LE	1
1952*3910Sdougm 	int (*check)(char *);
1953*3910Sdougm } proto_options[] = {
1954*3910Sdougm #define	PROTO_OPT_NFSD_SERVERS			0
1955*3910Sdougm 	{"nfsd_servers",
1956*3910Sdougm 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
1957*3910Sdougm 	    1, INT32_MAX, NFSADMIN},
1958*3910Sdougm #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
1959*3910Sdougm 	{"lockd_listen_backlog",
1960*3910Sdougm 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
1961*3910Sdougm 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
1962*3910Sdougm #define	PROTO_OPT_LOCKD_SERVERS			2
1963*3910Sdougm 	{"lockd_servers",
1964*3910Sdougm 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
1965*3910Sdougm 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
1966*3910Sdougm #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
1967*3910Sdougm 	{"lockd_retransmit_timeout",
1968*3910Sdougm 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
1969*3910Sdougm 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
1970*3910Sdougm #define	PROTO_OPT_GRACE_PERIOD			4
1971*3910Sdougm 	{"grace_period",
1972*3910Sdougm 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
1973*3910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
1974*3910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
1975*3910Sdougm 	{"nfs_server_versmin",
1976*3910Sdougm 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
1977*3910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
1978*3910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
1979*3910Sdougm #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
1980*3910Sdougm 	{"nfs_server_versmax",
1981*3910Sdougm 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
1982*3910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
1983*3910Sdougm 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
1984*3910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
1985*3910Sdougm 	{"nfs_client_versmin",
1986*3910Sdougm 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
1987*3910Sdougm 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
1988*3910Sdougm 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
1989*3910Sdougm #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
1990*3910Sdougm 	{"nfs_client_versmax",
1991*3910Sdougm 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
1992*3910Sdougm 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
1993*3910Sdougm 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
1994*3910Sdougm #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
1995*3910Sdougm 	{"nfs_server_delegation",
1996*3910Sdougm 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
1997*3910Sdougm 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
1998*3910Sdougm 	    NFSADMIN},
1999*3910Sdougm #define	PROTO_OPT_NFSMAPID_DOMAIN		10
2000*3910Sdougm 	{"nfsmapid_domain",
2001*3910Sdougm 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
2002*3910Sdougm 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
2003*3910Sdougm #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
2004*3910Sdougm 	{"nfsd_max_connections",
2005*3910Sdougm 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
2006*3910Sdougm 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
2007*3910Sdougm #define	PROTO_OPT_NFSD_PROTOCOL			12
2008*3910Sdougm 	{"nfsd_protocol",
2009*3910Sdougm 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
2010*3910Sdougm 	    SVC_NFSD, 0, 0, NFSADMIN},
2011*3910Sdougm #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
2012*3910Sdougm 	{"nfsd_listen_backlog",
2013*3910Sdougm 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
2014*3910Sdougm 	    OPT_TYPE_NUMBER, 0,
2015*3910Sdougm 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
2016*3910Sdougm 	{NULL}
2017*3910Sdougm };
2018*3910Sdougm 
2019*3910Sdougm /*
2020*3910Sdougm  * the protoset holds the defined options so we don't have to read
2021*3910Sdougm  * them multiple times
2022*3910Sdougm  */
2023*3910Sdougm sa_protocol_properties_t protoset;
2024*3910Sdougm 
2025*3910Sdougm static int
2026*3910Sdougm findprotoopt(char *name, int whichname)
2027*3910Sdougm {
2028*3910Sdougm 	int i;
2029*3910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2030*3910Sdougm 	    if (whichname == 1) {
2031*3910Sdougm 		if (strcasecmp(proto_options[i].name, name) == 0)
2032*3910Sdougm 			return (i);
2033*3910Sdougm 	    } else {
2034*3910Sdougm 		if (strcasecmp(proto_options[i].tag, name) == 0)
2035*3910Sdougm 			return (i);
2036*3910Sdougm 	    }
2037*3910Sdougm 	}
2038*3910Sdougm 	return (-1);
2039*3910Sdougm }
2040*3910Sdougm 
2041*3910Sdougm /*
2042*3910Sdougm  * fixcaselower(str)
2043*3910Sdougm  *
2044*3910Sdougm  * convert a string to lower case (inplace).
2045*3910Sdougm  */
2046*3910Sdougm 
2047*3910Sdougm static void
2048*3910Sdougm fixcaselower(char *str)
2049*3910Sdougm {
2050*3910Sdougm 	while (*str) {
2051*3910Sdougm 	    *str = tolower(*str);
2052*3910Sdougm 	    str++;
2053*3910Sdougm 	}
2054*3910Sdougm }
2055*3910Sdougm 
2056*3910Sdougm /*
2057*3910Sdougm  * fixcaseupper(str)
2058*3910Sdougm  *
2059*3910Sdougm  * convert a string to upper case (inplace).
2060*3910Sdougm  */
2061*3910Sdougm 
2062*3910Sdougm static void
2063*3910Sdougm fixcaseupper(char *str)
2064*3910Sdougm {
2065*3910Sdougm 	while (*str) {
2066*3910Sdougm 	    *str = toupper(*str);
2067*3910Sdougm 	    str++;
2068*3910Sdougm 	}
2069*3910Sdougm }
2070*3910Sdougm 
2071*3910Sdougm /*
2072*3910Sdougm  * initprotofromdefault()
2073*3910Sdougm  *
2074*3910Sdougm  * read the default file(s) and add the defined values to the
2075*3910Sdougm  * protoset.  Note that default values are known from the built in
2076*3910Sdougm  * table in case the file doesn't have a definition.
2077*3910Sdougm  */
2078*3910Sdougm 
2079*3910Sdougm static int
2080*3910Sdougm initprotofromdefault()
2081*3910Sdougm {
2082*3910Sdougm 	FILE *nfs;
2083*3910Sdougm 	char buff[BUFSIZ];
2084*3910Sdougm 	char *name;
2085*3910Sdougm 	char *value;
2086*3910Sdougm 	sa_property_t prop;
2087*3910Sdougm 	int index;
2088*3910Sdougm 
2089*3910Sdougm 	protoset = sa_create_protocol_properties("nfs");
2090*3910Sdougm 
2091*3910Sdougm 	if (protoset != NULL) {
2092*3910Sdougm 	    nfs = fopen(NFSADMIN, "r");
2093*3910Sdougm 	    if (nfs != NULL) {
2094*3910Sdougm 		while (fgets(buff, sizeof (buff), nfs) != NULL) {
2095*3910Sdougm 		    switch (buff[0]) {
2096*3910Sdougm 		    case '\n':
2097*3910Sdougm 		    case '#':
2098*3910Sdougm 			/* skip */
2099*3910Sdougm 			break;
2100*3910Sdougm 		    default:
2101*3910Sdougm 			name = buff;
2102*3910Sdougm 			buff[strlen(buff) - 1] = '\0';
2103*3910Sdougm 			value = strchr(name, '=');
2104*3910Sdougm 			if (value != NULL) {
2105*3910Sdougm 			    *value++ = '\0';
2106*3910Sdougm 			    if ((index = findprotoopt(name, 0)) >= 0) {
2107*3910Sdougm 				fixcaselower(name);
2108*3910Sdougm 				prop = sa_create_property(
2109*3910Sdougm 					proto_options[index].name,
2110*3910Sdougm 					value);
2111*3910Sdougm 				(void) sa_add_protocol_property(protoset, prop);
2112*3910Sdougm 			    }
2113*3910Sdougm 			}
2114*3910Sdougm 		    }
2115*3910Sdougm 		}
2116*3910Sdougm 		if (nfs != NULL)
2117*3910Sdougm 		    (void) fclose(nfs);
2118*3910Sdougm 	    }
2119*3910Sdougm 	}
2120*3910Sdougm 	if (protoset == NULL)
2121*3910Sdougm 	    return (SA_NO_MEMORY);
2122*3910Sdougm 	return (SA_OK);
2123*3910Sdougm }
2124*3910Sdougm 
2125*3910Sdougm /*
2126*3910Sdougm  * add_default()
2127*3910Sdougm  *
2128*3910Sdougm  * Add the default values for any property not defined in the parsing
2129*3910Sdougm  * of the default files. Values are set according to their defined
2130*3910Sdougm  * types.
2131*3910Sdougm  */
2132*3910Sdougm 
2133*3910Sdougm static void
2134*3910Sdougm add_defaults()
2135*3910Sdougm {
2136*3910Sdougm 	int i;
2137*3910Sdougm 	char number[MAXDIGITS];
2138*3910Sdougm 
2139*3910Sdougm 	for (i = 0; proto_options[i].tag != NULL; i++) {
2140*3910Sdougm 	    sa_property_t prop;
2141*3910Sdougm 	    prop = sa_get_protocol_property(protoset, proto_options[i].name);
2142*3910Sdougm 	    if (prop == NULL) {
2143*3910Sdougm 		/* add the default value */
2144*3910Sdougm 		switch (proto_options[i].type) {
2145*3910Sdougm 		case OPT_TYPE_NUMBER:
2146*3910Sdougm 		    (void) snprintf(number, sizeof (number), "%d",
2147*3910Sdougm 				proto_options[i].defvalue.intval);
2148*3910Sdougm 		    prop = sa_create_property(proto_options[i].name, number);
2149*3910Sdougm 		    break;
2150*3910Sdougm 
2151*3910Sdougm 		case OPT_TYPE_BOOLEAN:
2152*3910Sdougm 		    prop = sa_create_property(proto_options[i].name,
2153*3910Sdougm 					proto_options[i].defvalue.intval ?
2154*3910Sdougm 					"true" : "false");
2155*3910Sdougm 		    break;
2156*3910Sdougm 
2157*3910Sdougm 		case OPT_TYPE_ONOFF:
2158*3910Sdougm 		    prop = sa_create_property(proto_options[i].name,
2159*3910Sdougm 					proto_options[i].defvalue.intval ?
2160*3910Sdougm 					"on" : "off");
2161*3910Sdougm 		    break;
2162*3910Sdougm 
2163*3910Sdougm 		default:
2164*3910Sdougm 		    /* treat as strings of zero length */
2165*3910Sdougm 		    prop = sa_create_property(proto_options[i].name, "");
2166*3910Sdougm 		    break;
2167*3910Sdougm 		}
2168*3910Sdougm 		if (prop != NULL)
2169*3910Sdougm 		    (void) sa_add_protocol_property(protoset, prop);
2170*3910Sdougm 	    }
2171*3910Sdougm 	}
2172*3910Sdougm }
2173*3910Sdougm 
2174*3910Sdougm static void
2175*3910Sdougm free_protoprops()
2176*3910Sdougm {
2177*3910Sdougm 	xmlFreeNode(protoset);
2178*3910Sdougm }
2179*3910Sdougm 
2180*3910Sdougm /*
2181*3910Sdougm  * nfs_init()
2182*3910Sdougm  *
2183*3910Sdougm  * Initialize the NFS plugin.
2184*3910Sdougm  */
2185*3910Sdougm 
2186*3910Sdougm static int
2187*3910Sdougm nfs_init()
2188*3910Sdougm {
2189*3910Sdougm 	int ret = SA_OK;
2190*3910Sdougm 
2191*3910Sdougm 	if (sa_plugin_ops.sa_init != nfs_init)
2192*3910Sdougm 	    (void) printf(dgettext(TEXT_DOMAIN,
2193*3910Sdougm 				    "NFS plugin not properly initialized\n"));
2194*3910Sdougm 
2195*3910Sdougm 	ret = initprotofromdefault();
2196*3910Sdougm 	add_defaults();
2197*3910Sdougm 
2198*3910Sdougm 	return (ret);
2199*3910Sdougm }
2200*3910Sdougm 
2201*3910Sdougm /*
2202*3910Sdougm  * nfs_fini()
2203*3910Sdougm  *
2204*3910Sdougm  * uninitialize the NFS plugin. Want to avoid memory leaks.
2205*3910Sdougm  */
2206*3910Sdougm 
2207*3910Sdougm static void
2208*3910Sdougm nfs_fini()
2209*3910Sdougm {
2210*3910Sdougm 	free_protoprops();
2211*3910Sdougm }
2212*3910Sdougm 
2213*3910Sdougm /*
2214*3910Sdougm  * nfs_get_proto_set()
2215*3910Sdougm  *
2216*3910Sdougm  * Return an optionset with all the protocol specific properties in
2217*3910Sdougm  * it.
2218*3910Sdougm  */
2219*3910Sdougm 
2220*3910Sdougm static sa_protocol_properties_t
2221*3910Sdougm nfs_get_proto_set()
2222*3910Sdougm {
2223*3910Sdougm 	return (protoset);
2224*3910Sdougm }
2225*3910Sdougm 
2226*3910Sdougm struct deffile {
2227*3910Sdougm 	struct deffile *next;
2228*3910Sdougm 	char *line;
2229*3910Sdougm };
2230*3910Sdougm 
2231*3910Sdougm /*
2232*3910Sdougm  * read_default_file(fname)
2233*3910Sdougm  *
2234*3910Sdougm  * Read the specified default file. We return a list of entries. This
2235*3910Sdougm  * get used for adding or removing values.
2236*3910Sdougm  */
2237*3910Sdougm 
2238*3910Sdougm static struct deffile *
2239*3910Sdougm read_default_file(char *fname)
2240*3910Sdougm {
2241*3910Sdougm 	FILE *file;
2242*3910Sdougm 	struct deffile *defs = NULL;
2243*3910Sdougm 	struct deffile *newdef;
2244*3910Sdougm 	struct deffile *prevdef = NULL;
2245*3910Sdougm 	char buff[BUFSIZ * 2];
2246*3910Sdougm 
2247*3910Sdougm 	file = fopen(fname, "r");
2248*3910Sdougm 	if (file != NULL) {
2249*3910Sdougm 	    while (fgets(buff, sizeof (buff), file) != NULL) {
2250*3910Sdougm 		newdef = (struct deffile *)calloc(1, sizeof (struct deffile));
2251*3910Sdougm 		if (newdef != NULL) {
2252*3910Sdougm 		    newdef->line = strdup(buff);
2253*3910Sdougm 		    if (defs == NULL) {
2254*3910Sdougm 			prevdef = defs = newdef;
2255*3910Sdougm 		    } else {
2256*3910Sdougm 			prevdef->next = newdef;
2257*3910Sdougm 			prevdef = newdef;
2258*3910Sdougm 		    }
2259*3910Sdougm 		}
2260*3910Sdougm 	    }
2261*3910Sdougm 	}
2262*3910Sdougm 	(void) fclose(file);
2263*3910Sdougm 	return (defs);
2264*3910Sdougm }
2265*3910Sdougm 
2266*3910Sdougm static void
2267*3910Sdougm free_default_file(struct deffile *defs)
2268*3910Sdougm {
2269*3910Sdougm 	struct deffile *curdefs = NULL;
2270*3910Sdougm 
2271*3910Sdougm 	while (defs != NULL) {
2272*3910Sdougm 	    curdefs = defs;
2273*3910Sdougm 	    defs = defs->next;
2274*3910Sdougm 	    if (curdefs->line != NULL)
2275*3910Sdougm 		free(curdefs->line);
2276*3910Sdougm 	    free(curdefs);
2277*3910Sdougm 	}
2278*3910Sdougm }
2279*3910Sdougm 
2280*3910Sdougm /*
2281*3910Sdougm  * write_default_file(fname, defs)
2282*3910Sdougm  *
2283*3910Sdougm  * Write the default file back.
2284*3910Sdougm  */
2285*3910Sdougm 
2286*3910Sdougm static int
2287*3910Sdougm write_default_file(char *fname, struct deffile *defs)
2288*3910Sdougm {
2289*3910Sdougm 	FILE *file;
2290*3910Sdougm 	int ret = SA_OK;
2291*3910Sdougm 	sigset_t old, new;
2292*3910Sdougm 
2293*3910Sdougm 	file = fopen(fname, "w+");
2294*3910Sdougm 	if (file != NULL) {
2295*3910Sdougm 	    (void) sigprocmask(SIG_BLOCK, NULL, &new);
2296*3910Sdougm 	    (void) sigaddset(&new, SIGHUP);
2297*3910Sdougm 	    (void) sigaddset(&new, SIGINT);
2298*3910Sdougm 	    (void) sigaddset(&new, SIGQUIT);
2299*3910Sdougm 	    (void) sigaddset(&new, SIGTSTP);
2300*3910Sdougm 	    (void) sigprocmask(SIG_SETMASK, &new, &old);
2301*3910Sdougm 	    while (defs != NULL) {
2302*3910Sdougm 		(void) fputs(defs->line, file);
2303*3910Sdougm 		defs = defs->next;
2304*3910Sdougm 	    }
2305*3910Sdougm 	    (void) fsync(fileno(file));
2306*3910Sdougm 	    (void) sigprocmask(SIG_SETMASK, &old, NULL);
2307*3910Sdougm 	    (void) fclose(file);
2308*3910Sdougm 	} else {
2309*3910Sdougm 	    switch (errno) {
2310*3910Sdougm 	    case EPERM:
2311*3910Sdougm 	    case EACCES:
2312*3910Sdougm 		ret = SA_NO_PERMISSION;
2313*3910Sdougm 		break;
2314*3910Sdougm 	    default:
2315*3910Sdougm 		ret = SA_SYSTEM_ERR;
2316*3910Sdougm 	    }
2317*3910Sdougm 	}
2318*3910Sdougm 	return (ret);
2319*3910Sdougm }
2320*3910Sdougm 
2321*3910Sdougm 
2322*3910Sdougm /*
2323*3910Sdougm  * set_default_file_value(tag, value)
2324*3910Sdougm  *
2325*3910Sdougm  * Set the default file value for tag to value. Then rewrite the file.
2326*3910Sdougm  * tag and value are always set.  The caller must ensure this.
2327*3910Sdougm  */
2328*3910Sdougm 
2329*3910Sdougm #define	MAX_STRING_LENGTH	256
2330*3910Sdougm static int
2331*3910Sdougm set_default_file_value(char *tag, char *value)
2332*3910Sdougm {
2333*3910Sdougm 	int ret = SA_OK;
2334*3910Sdougm 	struct deffile *root;
2335*3910Sdougm 	struct deffile *defs;
2336*3910Sdougm 	struct deffile *prev;
2337*3910Sdougm 	char string[MAX_STRING_LENGTH];
2338*3910Sdougm 	int len;
2339*3910Sdougm 	int update = 0;
2340*3910Sdougm 
2341*3910Sdougm 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
2342*3910Sdougm 	len = strlen(string);
2343*3910Sdougm 
2344*3910Sdougm 	root = defs = read_default_file(NFSADMIN);
2345*3910Sdougm 	if (root == NULL) {
2346*3910Sdougm 	    if (errno == EPERM || errno == EACCES)
2347*3910Sdougm 		ret = SA_NO_PERMISSION;
2348*3910Sdougm 	    else
2349*3910Sdougm 		ret = SA_SYSTEM_ERR;
2350*3910Sdougm 	} else {
2351*3910Sdougm 	    while (defs != NULL) {
2352*3910Sdougm 		if (defs->line != NULL &&
2353*3910Sdougm 		    strncasecmp(defs->line, string, len) == 0) {
2354*3910Sdougm 		    /* replace with the new value */
2355*3910Sdougm 		    free(defs->line);
2356*3910Sdougm 		    fixcaseupper(tag);
2357*3910Sdougm 		    (void) snprintf(string, sizeof (string), "%s=%s\n",
2358*3910Sdougm 					tag, value);
2359*3910Sdougm 		    string[MAX_STRING_LENGTH - 1] = '\0';
2360*3910Sdougm 		    defs->line = strdup(string);
2361*3910Sdougm 		    update = 1;
2362*3910Sdougm 		    break;
2363*3910Sdougm 		}
2364*3910Sdougm 		defs = defs->next;
2365*3910Sdougm 	    }
2366*3910Sdougm 	    if (!update) {
2367*3910Sdougm 		defs = root;
2368*3910Sdougm 		/* didn't find, so see if it is a comment */
2369*3910Sdougm 		(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
2370*3910Sdougm 		len = strlen(string);
2371*3910Sdougm 		while (defs != NULL) {
2372*3910Sdougm 		    if (strncasecmp(defs->line, string, len) == 0) {
2373*3910Sdougm 			/* replace with the new value */
2374*3910Sdougm 			free(defs->line);
2375*3910Sdougm 			fixcaseupper(tag);
2376*3910Sdougm 			(void) snprintf(string, sizeof (string),
2377*3910Sdougm 				    "%s=%s\n", tag, value);
2378*3910Sdougm 			string[MAX_STRING_LENGTH - 1] = '\0';
2379*3910Sdougm 			defs->line = strdup(string);
2380*3910Sdougm 			update = 1;
2381*3910Sdougm 			break;
2382*3910Sdougm 		    }
2383*3910Sdougm 		    defs = defs->next;
2384*3910Sdougm 		}
2385*3910Sdougm 	    }
2386*3910Sdougm 	    if (!update) {
2387*3910Sdougm 		fixcaseupper(tag);
2388*3910Sdougm 		(void) snprintf(string, sizeof (string), "%s=%s\n",
2389*3910Sdougm 					tag, value);
2390*3910Sdougm 		prev = root;
2391*3910Sdougm 		while (prev->next != NULL)
2392*3910Sdougm 		    prev = prev->next;
2393*3910Sdougm 		defs = malloc(sizeof (struct deffile));
2394*3910Sdougm 		prev->next = defs;
2395*3910Sdougm 		if (defs != NULL) {
2396*3910Sdougm 		    defs->next = NULL;
2397*3910Sdougm 		    defs->line = strdup(string);
2398*3910Sdougm 		}
2399*3910Sdougm 	    }
2400*3910Sdougm 	    if (update) {
2401*3910Sdougm 		ret = write_default_file(NFSADMIN, root);
2402*3910Sdougm 	    }
2403*3910Sdougm 	    free_default_file(root);
2404*3910Sdougm 	}
2405*3910Sdougm 	return (ret);
2406*3910Sdougm }
2407*3910Sdougm 
2408*3910Sdougm /*
2409*3910Sdougm  * service_in_state(service, chkstate)
2410*3910Sdougm  *
2411*3910Sdougm  * Want to know if the specified service is in the desired state
2412*3910Sdougm  * (chkstate) or not. Return true (1) if it is and false (0) if it
2413*3910Sdougm  * isn't.
2414*3910Sdougm  */
2415*3910Sdougm static int
2416*3910Sdougm service_in_state(char *service, const char *chkstate)
2417*3910Sdougm {
2418*3910Sdougm 	char *state;
2419*3910Sdougm 	int ret = B_FALSE;
2420*3910Sdougm 
2421*3910Sdougm 	state = smf_get_state(service);
2422*3910Sdougm 	if (state != NULL) {
2423*3910Sdougm 	    /* got the state so get the equality for the return value */
2424*3910Sdougm 	    ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2425*3910Sdougm 	    free(state);
2426*3910Sdougm 	}
2427*3910Sdougm 	return (ret);
2428*3910Sdougm }
2429*3910Sdougm 
2430*3910Sdougm /*
2431*3910Sdougm  * restart_service(svcs)
2432*3910Sdougm  *
2433*3910Sdougm  * Walk through the bit mask of services that need to be restarted in
2434*3910Sdougm  * order to use the new property values. Some properties affect
2435*3910Sdougm  * multiple daemons. Should only restart a service if it is currently
2436*3910Sdougm  * enabled (online).
2437*3910Sdougm  */
2438*3910Sdougm 
2439*3910Sdougm static void
2440*3910Sdougm restart_service(uint32_t svcs)
2441*3910Sdougm {
2442*3910Sdougm 	uint32_t mask;
2443*3910Sdougm 	int ret;
2444*3910Sdougm 	char *service;
2445*3910Sdougm 
2446*3910Sdougm 	for (mask = 1; svcs != 0; mask <<= 1) {
2447*3910Sdougm 	    switch (svcs & mask) {
2448*3910Sdougm 	    case SVC_LOCKD:
2449*3910Sdougm 		service = LOCKD;
2450*3910Sdougm 		break;
2451*3910Sdougm 	    case SVC_STATD:
2452*3910Sdougm 		service = STATD;
2453*3910Sdougm 		break;
2454*3910Sdougm 	    case SVC_NFSD:
2455*3910Sdougm 		service = NFSD;
2456*3910Sdougm 		break;
2457*3910Sdougm 	    case SVC_MOUNTD:
2458*3910Sdougm 		service = MOUNTD;
2459*3910Sdougm 		break;
2460*3910Sdougm 	    case SVC_NFS4CBD:
2461*3910Sdougm 		service = NFS4CBD;
2462*3910Sdougm 		break;
2463*3910Sdougm 	    case SVC_NFSMAPID:
2464*3910Sdougm 		service = NFSMAPID;
2465*3910Sdougm 		break;
2466*3910Sdougm 	    case SVC_RQUOTAD:
2467*3910Sdougm 		service = RQUOTAD;
2468*3910Sdougm 		break;
2469*3910Sdougm 	    case SVC_NFSLOGD:
2470*3910Sdougm 		service = NFSLOGD;
2471*3910Sdougm 		break;
2472*3910Sdougm 	    default:
2473*3910Sdougm 		continue;
2474*3910Sdougm 	    }
2475*3910Sdougm 
2476*3910Sdougm 		/*
2477*3910Sdougm 		 * Only attempt to restart the service if it is
2478*3910Sdougm 		 * currently running. In the future, it may be
2479*3910Sdougm 		 * desirable to use smf_refresh_instance if the NFS
2480*3910Sdougm 		 * services ever implement the refresh method.
2481*3910Sdougm 		 */
2482*3910Sdougm 	    if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2483*3910Sdougm 		ret = smf_restart_instance(service);
2484*3910Sdougm 		/*
2485*3910Sdougm 		 * There are only a few SMF errors at this point, but
2486*3910Sdougm 		 * it is also possible that a bad value may have put
2487*3910Sdougm 		 * the service into maintenance if there wasn't an
2488*3910Sdougm 		 * SMF level error.
2489*3910Sdougm 		 */
2490*3910Sdougm 		if (ret != 0) {
2491*3910Sdougm 		    (void) fprintf(stderr,
2492*3910Sdougm 				dgettext(TEXT_DOMAIN,
2493*3910Sdougm 					"%s failed to restart: %s\n"),
2494*3910Sdougm 				scf_strerror(scf_error()));
2495*3910Sdougm 		} else {
2496*3910Sdougm 			/*
2497*3910Sdougm 			 * Check whether it has gone to "maintenance"
2498*3910Sdougm 			 * mode or not. Maintenance implies something
2499*3910Sdougm 			 * went wrong.
2500*3910Sdougm 			 */
2501*3910Sdougm 		    if (service_in_state(service, SCF_STATE_STRING_MAINT)) {
2502*3910Sdougm 			(void) fprintf(stderr,
2503*3910Sdougm 					dgettext(TEXT_DOMAIN,
2504*3910Sdougm 						"%s failed to restart\n"),
2505*3910Sdougm 					service);
2506*3910Sdougm 		    }
2507*3910Sdougm 		}
2508*3910Sdougm 	    }
2509*3910Sdougm 	    svcs &= ~mask;
2510*3910Sdougm 	}
2511*3910Sdougm }
2512*3910Sdougm 
2513*3910Sdougm /*
2514*3910Sdougm  * nfs_minmax_check(name, value)
2515*3910Sdougm  *
2516*3910Sdougm  * Verify that the value for the property specified by index is valid
2517*3910Sdougm  * relative to the opposite value in the case of a min/max variable.
2518*3910Sdougm  * Currently, server_minvers/server_maxvers and
2519*3910Sdougm  * client_minvers/client_maxvers are the only ones to check.
2520*3910Sdougm  */
2521*3910Sdougm 
2522*3910Sdougm static int
2523*3910Sdougm nfs_minmax_check(int index, int value)
2524*3910Sdougm {
2525*3910Sdougm 	int val;
2526*3910Sdougm 	char *pval;
2527*3910Sdougm 	sa_property_t prop;
2528*3910Sdougm 	sa_optionset_t opts;
2529*3910Sdougm 	int ret = B_TRUE;
2530*3910Sdougm 
2531*3910Sdougm 	if (proto_options[index].other != NULL) {
2532*3910Sdougm 	    /* have a property to compare against */
2533*3910Sdougm 	    opts = nfs_get_proto_set();
2534*3910Sdougm 	    prop = sa_get_property(opts, proto_options[index].other);
2535*3910Sdougm 		/*
2536*3910Sdougm 		 * If we don't find the property, assume default
2537*3910Sdougm 		 * values which will work since the max will be at the
2538*3910Sdougm 		 * max and the min at the min.
2539*3910Sdougm 		 */
2540*3910Sdougm 	    if (prop != NULL) {
2541*3910Sdougm 		pval = sa_get_property_attr(prop, "value");
2542*3910Sdougm 		if (pval != NULL) {
2543*3910Sdougm 		    val = strtoul(pval, NULL, 0);
2544*3910Sdougm 		    if (proto_options[index].compare == OPT_CMP_LE) {
2545*3910Sdougm 			ret = value <= val ? B_TRUE : B_FALSE;
2546*3910Sdougm 		    } else if (proto_options[index].compare == OPT_CMP_GE) {
2547*3910Sdougm 			ret = value >= val ? B_TRUE : B_FALSE;
2548*3910Sdougm 		    }
2549*3910Sdougm 		}
2550*3910Sdougm 	    }
2551*3910Sdougm 	}
2552*3910Sdougm 	return (ret);
2553*3910Sdougm }
2554*3910Sdougm 
2555*3910Sdougm /*
2556*3910Sdougm  * nfs_validate_proto_prop(index, name, value)
2557*3910Sdougm  *
2558*3910Sdougm  * Verify that the property specifed by name can take the new
2559*3910Sdougm  * value. This is a sanity check to prevent bad values getting into
2560*3910Sdougm  * the default files. All values need to be checked against what is
2561*3910Sdougm  * allowed by their defined type. If a type isn't explicitly defined
2562*3910Sdougm  * here, it is treated as a string.
2563*3910Sdougm  *
2564*3910Sdougm  * Note that OPT_TYPE_NUMBER will additionally check that the value is
2565*3910Sdougm  * within the range specified and potentially against another property
2566*3910Sdougm  * value as well as specified in the proto_options members other and
2567*3910Sdougm  * compare.
2568*3910Sdougm  */
2569*3910Sdougm 
2570*3910Sdougm static int
2571*3910Sdougm nfs_validate_proto_prop(int index, char *name, char *value)
2572*3910Sdougm {
2573*3910Sdougm 	int ret = SA_OK;
2574*3910Sdougm 	char *cp;
2575*3910Sdougm #ifdef lint
2576*3910Sdougm 	name = name;
2577*3910Sdougm #endif
2578*3910Sdougm 
2579*3910Sdougm 	switch (proto_options[index].type) {
2580*3910Sdougm 	case OPT_TYPE_NUMBER:
2581*3910Sdougm 	    if (!is_a_number(value))
2582*3910Sdougm 		ret = SA_BAD_VALUE;
2583*3910Sdougm 	    else {
2584*3910Sdougm 		int val;
2585*3910Sdougm 		val = strtoul(value, NULL, 0);
2586*3910Sdougm 		if (val < proto_options[index].minval ||
2587*3910Sdougm 		    val > proto_options[index].maxval)
2588*3910Sdougm 		    ret = SA_BAD_VALUE;
2589*3910Sdougm 		/*
2590*3910Sdougm 		 * For server_versmin/server_versmax and
2591*3910Sdougm 		 * client_versmin/client_versmax, the value of the
2592*3910Sdougm 		 * min(max) should be checked to be correct relative
2593*3910Sdougm 		 * to the current max(min).
2594*3910Sdougm 		 */
2595*3910Sdougm 		if (!nfs_minmax_check(index, val)) {
2596*3910Sdougm 		    ret = SA_BAD_VALUE;
2597*3910Sdougm 		}
2598*3910Sdougm 	    }
2599*3910Sdougm 	    break;
2600*3910Sdougm 
2601*3910Sdougm 	case OPT_TYPE_DOMAIN:
2602*3910Sdougm 		/*
2603*3910Sdougm 		 * needs to be a qualified domain so will have at
2604*3910Sdougm 		 * least one period and other characters on either
2605*3910Sdougm 		 * side of it.  A zero length string is also allowed
2606*3910Sdougm 		 * and is the way to turn off the override.
2607*3910Sdougm 		 */
2608*3910Sdougm 	    if (strlen(value) == 0)
2609*3910Sdougm 		break;
2610*3910Sdougm 	    cp = strchr(value, '.');
2611*3910Sdougm 	    if (cp == NULL || cp == value || strchr(value, '@') != NULL)
2612*3910Sdougm 		ret = SA_BAD_VALUE;
2613*3910Sdougm 	    break;
2614*3910Sdougm 
2615*3910Sdougm 	case OPT_TYPE_BOOLEAN:
2616*3910Sdougm 	    if (strlen(value) == 0 ||
2617*3910Sdougm 		strcasecmp(value, "true") == 0 ||
2618*3910Sdougm 		strcmp(value, "1") == 0 ||
2619*3910Sdougm 		strcasecmp(value, "false") == 0 ||
2620*3910Sdougm 		strcmp(value, "0") == 0) {
2621*3910Sdougm 		ret = SA_OK;
2622*3910Sdougm 	    } else {
2623*3910Sdougm 		ret = SA_BAD_VALUE;
2624*3910Sdougm 	    }
2625*3910Sdougm 	    break;
2626*3910Sdougm 
2627*3910Sdougm 	case OPT_TYPE_ONOFF:
2628*3910Sdougm 	    if (strcasecmp(value, "on") != 0 &&
2629*3910Sdougm 		strcasecmp(value, "off") != 0) {
2630*3910Sdougm 		ret = SA_BAD_VALUE;
2631*3910Sdougm 	    }
2632*3910Sdougm 	    break;
2633*3910Sdougm 
2634*3910Sdougm 	case OPT_TYPE_PROTOCOL:
2635*3910Sdougm 	    if (strcasecmp(value, "all") != 0 &&
2636*3910Sdougm 		strcasecmp(value, "tcp") != 0 &&
2637*3910Sdougm 		strcasecmp(value, "udp") != 0)
2638*3910Sdougm 		ret = SA_BAD_VALUE;
2639*3910Sdougm 	    break;
2640*3910Sdougm 
2641*3910Sdougm 	default:
2642*3910Sdougm 	    /* treat as a string */
2643*3910Sdougm 	    break;
2644*3910Sdougm 	}
2645*3910Sdougm 	return (ret);
2646*3910Sdougm }
2647*3910Sdougm 
2648*3910Sdougm /*
2649*3910Sdougm  * nfs_set_proto_prop(prop)
2650*3910Sdougm  *
2651*3910Sdougm  * check that prop is valid.
2652*3910Sdougm  */
2653*3910Sdougm 
2654*3910Sdougm static int
2655*3910Sdougm nfs_set_proto_prop(sa_property_t prop)
2656*3910Sdougm {
2657*3910Sdougm 	int ret = SA_OK;
2658*3910Sdougm 	char *name;
2659*3910Sdougm 	char *value;
2660*3910Sdougm 
2661*3910Sdougm 	name = sa_get_property_attr(prop, "type");
2662*3910Sdougm 	value = sa_get_property_attr(prop, "value");
2663*3910Sdougm 	if (name != NULL && value != NULL) {
2664*3910Sdougm 	    int index = findprotoopt(name, 1);
2665*3910Sdougm 	    if (index >= 0) {
2666*3910Sdougm 		/* should test for valid value */
2667*3910Sdougm 		ret = nfs_validate_proto_prop(index, name, value);
2668*3910Sdougm 		if (ret == SA_OK)
2669*3910Sdougm 		    ret = set_default_file_value(proto_options[index].tag,
2670*3910Sdougm 							value);
2671*3910Sdougm 		if (ret == SA_OK)
2672*3910Sdougm 		    restart_service(proto_options[index].svcs);
2673*3910Sdougm 	    }
2674*3910Sdougm 	}
2675*3910Sdougm 	if (name != NULL)
2676*3910Sdougm 	    sa_free_attr_string(name);
2677*3910Sdougm 	if (value != NULL)
2678*3910Sdougm 	    sa_free_attr_string(value);
2679*3910Sdougm 	return (ret);
2680*3910Sdougm }
2681*3910Sdougm 
2682*3910Sdougm /*
2683*3910Sdougm  * nfs_get_status()
2684*3910Sdougm  *
2685*3910Sdougm  * What is the current status of the nfsd? We use the SMF state here.
2686*3910Sdougm  * Caller must free the returned value.
2687*3910Sdougm  */
2688*3910Sdougm 
2689*3910Sdougm static char *
2690*3910Sdougm nfs_get_status()
2691*3910Sdougm {
2692*3910Sdougm 	char *state;
2693*3910Sdougm 	state = smf_get_state(NFSD);
2694*3910Sdougm 	return (state != NULL ? state : strdup("-"));
2695*3910Sdougm }
2696*3910Sdougm 
2697*3910Sdougm /*
2698*3910Sdougm  * nfs_space_alias(alias)
2699*3910Sdougm  *
2700*3910Sdougm  * Lookup the space (security) name. If it is default, convert to the
2701*3910Sdougm  * real name.
2702*3910Sdougm  */
2703*3910Sdougm 
2704*3910Sdougm static char *
2705*3910Sdougm nfs_space_alias(char *space)
2706*3910Sdougm {
2707*3910Sdougm 	char *name = space;
2708*3910Sdougm 	seconfig_t secconf;
2709*3910Sdougm 
2710*3910Sdougm 	/*
2711*3910Sdougm 	 * Only the space named "default" is special. If it is used,
2712*3910Sdougm 	 * the default needs to be looked up and the real name used.
2713*3910Sdougm 	 * This is normally "sys" but could be changed.  We always
2714*3910Sdougm 	 * change defautl to the real name.
2715*3910Sdougm 	 */
2716*3910Sdougm 	if (strcmp(space, "default") == 0 &&
2717*3910Sdougm 	    nfs_getseconfig_default(&secconf) == 0) {
2718*3910Sdougm 	    if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
2719*3910Sdougm 		name = secconf.sc_name;
2720*3910Sdougm 	}
2721*3910Sdougm 	return (strdup(name));
2722*3910Sdougm }
2723