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