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