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