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