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