xref: /titanic_41/usr/src/lib/libshare/nfs/libshare_nfs.c (revision cc1a9a89a73172cc2db053635fab3b1b91691657)
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_handle_t, 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(handle, 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_handle_t handle, sa_share_t skipshare)
1603 {
1604 	sa_group_t group = NULL;
1605 
1606 	/*
1607 	 * If we don't have a handle, we can only do syntax check. We
1608 	 * can't check against other shares so we assume OK and will
1609 	 * catch the problem only when we actually try to apply it.
1610 	 */
1611 	if (handle == NULL)
1612 		return (SA_OK);
1613 
1614 	if (skipshare != NULL) {
1615 		group = sa_get_parent_group(skipshare);
1616 		if (group == NULL)
1617 			return (SA_NO_SUCH_GROUP);
1618 	}
1619 
1620 	for (group = sa_get_group(handle, NULL); group != NULL;
1621 	    group = sa_get_next_group(group)) {
1622 		/* Walk any ZFS subgroups as well as all standard groups */
1623 		if (sa_group_is_zfs(group)) {
1624 			sa_group_t subgroup;
1625 			for (subgroup = sa_get_sub_group(group);
1626 			    subgroup != NULL;
1627 			    subgroup = sa_get_next_group(subgroup)) {
1628 				if (check_public(subgroup, skipshare))
1629 					return (B_TRUE);
1630 			}
1631 		} else {
1632 			if (check_public(group, skipshare))
1633 				return (B_TRUE);
1634 		}
1635 	}
1636 	return (B_FALSE);
1637 }
1638 
1639 /*
1640  * sa_enable_share at the protocol level, enable_share must tell the
1641  * implementation that it is to enable the share. This entails
1642  * converting the path and options into the appropriate ioctl
1643  * calls. It is assumed that all error checking of paths, etc. were
1644  * done earlier.
1645  */
1646 static int
1647 nfs_enable_share(sa_share_t share)
1648 {
1649 	struct exportdata export;
1650 	sa_optionset_t secoptlist;
1651 	struct secinfo *sp;
1652 	int num_secinfo;
1653 	sa_optionset_t opt;
1654 	sa_security_t sec;
1655 	sa_property_t prop;
1656 	char *path;
1657 	int err = SA_OK;
1658 	int i;
1659 	int iszfs;
1660 	sa_handle_t handle;
1661 
1662 	/* Don't drop core if the NFS module isn't loaded. */
1663 	(void) signal(SIGSYS, SIG_IGN);
1664 
1665 	/* get the path since it is important in several places */
1666 	path = sa_get_share_attr(share, "path");
1667 	if (path == NULL)
1668 		return (SA_NO_SUCH_PATH);
1669 
1670 	iszfs = sa_path_is_zfs(path);
1671 	/*
1672 	 * find the optionsets and security sets.  There may not be
1673 	 * any or there could be one or two for each of optionset and
1674 	 * security may have multiple, one per security type per
1675 	 * protocol type.
1676 	 */
1677 	opt = sa_get_derived_optionset(share, "nfs", 1);
1678 	secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1);
1679 	if (secoptlist != NULL)
1680 		num_secinfo = MAX(1, count_security(secoptlist));
1681 	else
1682 		num_secinfo = 1;
1683 
1684 	/*
1685 	 * walk through the options and fill in the structure
1686 	 * appropriately.
1687 	 */
1688 
1689 	(void) memset(&export, '\0', sizeof (export));
1690 
1691 	/*
1692 	 * do non-security options first since there is only one after
1693 	 * the derived group is constructed.
1694 	 */
1695 	export.ex_version = EX_CURRENT_VERSION;
1696 	export.ex_anon = UID_NOBODY; /* this is our default value */
1697 	export.ex_index = NULL;
1698 	export.ex_path = path;
1699 	export.ex_pathlen = strlen(path) + 1;
1700 
1701 	if (opt != NULL)
1702 		err = fill_export_from_optionset(&export, opt);
1703 
1704 	/*
1705 	 * check to see if "public" is set. If it is, then make sure
1706 	 * no other share has it set. If it is already used, fail.
1707 	 */
1708 
1709 	handle = sa_find_group_handle((sa_group_t)share);
1710 	if (export.ex_flags & EX_PUBLIC && public_exists(handle, share)) {
1711 		(void) printf(dgettext(TEXT_DOMAIN,
1712 		    "NFS: Cannot share more than one file "
1713 		    "system with 'public' property\n"));
1714 		err = SA_NOT_ALLOWED;
1715 		goto out;
1716 	}
1717 
1718 	sp = calloc(num_secinfo, sizeof (struct secinfo));
1719 	if (sp == NULL) {
1720 		err = SA_NO_MEMORY;
1721 		(void) printf(dgettext(TEXT_DOMAIN,
1722 		    "NFS: NFS: no memory for security\n"));
1723 		goto out;
1724 	}
1725 	export.ex_secinfo = sp;
1726 	/* get default secinfo */
1727 	export.ex_seccnt = num_secinfo;
1728 	/*
1729 	 * since we must have one security option defined, we
1730 	 * init to the default and then override as we find
1731 	 * defined security options. This handles the case
1732 	 * where we have no defined options but we need to set
1733 	 * up one.
1734 	 */
1735 	sp[0].s_window = DEF_WIN;
1736 	sp[0].s_rootnames = NULL;
1737 	/* setup a default in case no properties defined */
1738 	if (nfs_getseconfig_default(&sp[0].s_secinfo)) {
1739 		(void) printf(dgettext(TEXT_DOMAIN,
1740 		    "NFS: nfs_getseconfig_default: failed to "
1741 		    "get default security mode\n"));
1742 		err = SA_CONFIG_ERR;
1743 	}
1744 	if (secoptlist != NULL) {
1745 		for (i = 0, prop = sa_get_property(secoptlist, NULL);
1746 		    prop != NULL && i < num_secinfo;
1747 		    prop = sa_get_next_property(prop), i++) {
1748 			char *sectype;
1749 				sectype = sa_get_property_attr(prop, "type");
1750 			/*
1751 			 * if sectype is NULL, we probably
1752 			 * have a memory problem and can't get
1753 			 * the correct values. Rather than
1754 			 * exporting with incorrect security,
1755 			 * don't share it.
1756 			 */
1757 			if (sectype == NULL) {
1758 				err = SA_NO_MEMORY;
1759 				(void) printf(dgettext(TEXT_DOMAIN,
1760 				    "NFS: Cannot share %s: "
1761 				    "no memory\n"), path);
1762 				goto out;
1763 			}
1764 			sec = (sa_security_t)sa_get_derived_security(
1765 			    share, sectype, "nfs", 1);
1766 			sp[i].s_window = DEF_WIN;
1767 			sp[i].s_rootcnt = 0;
1768 			sp[i].s_rootnames = NULL;
1769 				(void) fill_security_from_secopts(&sp[i], sec);
1770 			if (sec != NULL)
1771 				sa_free_derived_security(sec);
1772 			if (sectype != NULL)
1773 				sa_free_attr_string(sectype);
1774 		}
1775 	}
1776 	/*
1777 	 * when we get here, we can do the exportfs system call and
1778 	 * initiate thinsg. We probably want to enable the nfs.server
1779 	 * service first if it isn't running within SMF.
1780 	 */
1781 	/* check nfs.server status and start if needed */
1782 	/* now add the share to the internal tables */
1783 	printarg(path, &export);
1784 	/*
1785 	 * call the exportfs system call which is implemented
1786 	 * via the nfssys() call as the EXPORTFS subfunction.
1787 	 */
1788 	if (iszfs) {
1789 		struct exportfs_args ea;
1790 		share_t sh;
1791 		char *str;
1792 		priv_set_t *priv_effective;
1793 		int privileged;
1794 
1795 		/*
1796 		 * If we aren't a privileged user
1797 		 * and NFS server service isn't running
1798 		 * then print out an error message
1799 		 * and return EPERM
1800 		 */
1801 
1802 		priv_effective = priv_allocset();
1803 		(void) getppriv(PRIV_EFFECTIVE, priv_effective);
1804 
1805 		privileged = (priv_isfullset(priv_effective) == B_TRUE);
1806 		priv_freeset(priv_effective);
1807 
1808 		if (!privileged &&
1809 		    (str = smf_get_state(NFS_SERVER_SVC)) != NULL) {
1810 			err = 0;
1811 			if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) {
1812 				(void) printf(dgettext(TEXT_DOMAIN,
1813 				    "NFS: Cannot share remote "
1814 				    "filesystem: %s\n"), path);
1815 				(void) printf(dgettext(TEXT_DOMAIN,
1816 				    "NFS: Service needs to be enabled "
1817 				    "by a privileged user\n"));
1818 				err = SA_SYSTEM_ERR;
1819 				errno = EPERM;
1820 			}
1821 			free(str);
1822 		}
1823 
1824 		if (err == 0) {
1825 			ea.dname = path;
1826 			ea.uex = &export;
1827 
1828 			sa_sharetab_fill_zfs(share, &sh, "nfs");
1829 			err = sa_share_zfs(share, path, &sh,
1830 			    &ea, ZFS_SHARE_NFS);
1831 			sa_emptyshare(&sh);
1832 		}
1833 	} else {
1834 		err = exportfs(path, &export);
1835 	}
1836 
1837 	if (err < 0) {
1838 		err = SA_SYSTEM_ERR;
1839 		switch (errno) {
1840 		case EREMOTE:
1841 			(void) printf(dgettext(TEXT_DOMAIN,
1842 			    "NFS: Cannot share filesystems "
1843 			    "in non-global zones: %s\n"), path);
1844 			err = SA_NOT_SUPPORTED;
1845 			break;
1846 		case EPERM:
1847 			if (getzoneid() != GLOBAL_ZONEID) {
1848 				(void) printf(dgettext(TEXT_DOMAIN,
1849 				    "NFS: Cannot share file systems "
1850 				    "in non-global zones: %s\n"), path);
1851 				err = SA_NOT_SUPPORTED;
1852 				break;
1853 			}
1854 			err = SA_NO_PERMISSION;
1855 			/* FALLTHROUGH */
1856 		default:
1857 			break;
1858 		}
1859 	} else {
1860 		/* update sharetab with an add/modify */
1861 		if (!iszfs) {
1862 			(void) sa_update_sharetab(share, "nfs");
1863 		}
1864 	}
1865 
1866 	if (err == SA_OK) {
1867 		/*
1868 		 * enable services as needed. This should probably be
1869 		 * done elsewhere in order to minimize the calls to
1870 		 * check services.
1871 		 */
1872 		/*
1873 		 * check to see if logging and other services need to
1874 		 * be triggered, but only if there wasn't an
1875 		 * error. This is probably where sharetab should be
1876 		 * updated with the NFS specific entry.
1877 		 */
1878 		if (export.ex_flags & EX_LOG) {
1879 			/* enable logging */
1880 			if (nfslogtab_add(path, export.ex_log_buffer,
1881 			    export.ex_tag) != 0) {
1882 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1883 				    "Could not enable logging for %s\n"),
1884 				    path);
1885 			}
1886 			_check_services(service_list_logging);
1887 		} else {
1888 			/*
1889 			 * don't have logging so remove it from file. It might
1890 			 * not be thre, but that doesn't matter.
1891 			 */
1892 			(void) nfslogtab_deactivate(path);
1893 			_check_services(service_list_default);
1894 		}
1895 	}
1896 
1897 out:
1898 	if (path != NULL)
1899 		free(path);
1900 
1901 	cleanup_export(&export);
1902 	if (opt != NULL)
1903 		sa_free_derived_optionset(opt);
1904 	if (secoptlist != NULL)
1905 		(void) sa_destroy_optionset(secoptlist);
1906 	return (err);
1907 }
1908 
1909 /*
1910  * nfs_disable_share(share, path)
1911  *
1912  * Unshare the specified share. Note that "path" is the same path as
1913  * what is in the "share" object. It is passed in to avoid an
1914  * additional lookup. A missing "path" value makes this a no-op
1915  * function.
1916  */
1917 static int
1918 nfs_disable_share(sa_share_t share, char *path)
1919 {
1920 	int err;
1921 	int ret = SA_OK;
1922 	int iszfs;
1923 	sa_group_t parent;
1924 	sa_handle_t handle;
1925 
1926 	if (path == NULL)
1927 		return (ret);
1928 
1929 	/*
1930 	 * If the share is in a ZFS group we need to handle it
1931 	 * differently.  Just being on a ZFS file system isn't
1932 	 * enough since we may be in a legacy share case.
1933 	 */
1934 	parent = sa_get_parent_group(share);
1935 	iszfs = sa_group_is_zfs(parent);
1936 	if (iszfs) {
1937 		struct exportfs_args ea;
1938 		share_t sh = { 0 };
1939 		ea.dname = path;
1940 		ea.uex = NULL;
1941 		sh.sh_path = path;
1942 		sh.sh_fstype = "nfs";
1943 
1944 		err = sa_share_zfs(share, path, &sh,
1945 		    &ea, ZFS_UNSHARE_NFS);
1946 	} else {
1947 		err = exportfs(path, NULL);
1948 	}
1949 	if (err < 0) {
1950 		/*
1951 		 * TBD: only an error in some
1952 		 * cases - need better analysis
1953 		 */
1954 		switch (errno) {
1955 		case EPERM:
1956 		case EACCES:
1957 			ret = SA_NO_PERMISSION;
1958 			if (getzoneid() != GLOBAL_ZONEID) {
1959 				ret = SA_NOT_SUPPORTED;
1960 			}
1961 			break;
1962 		case EINVAL:
1963 		case ENOENT:
1964 			ret = SA_NO_SUCH_PATH;
1965 			break;
1966 			default:
1967 				ret = SA_SYSTEM_ERR;
1968 			break;
1969 		}
1970 	}
1971 	if (ret == SA_OK || ret == SA_NO_SUCH_PATH) {
1972 		handle = sa_find_group_handle((sa_group_t)share);
1973 		if (!iszfs)
1974 			(void) sa_delete_sharetab(handle, path, "nfs");
1975 		/* just in case it was logged */
1976 		(void) nfslogtab_deactivate(path);
1977 	}
1978 	return (ret);
1979 }
1980 
1981 /*
1982  * check ro vs rw values.  Over time this may get beefed up.
1983  * for now it just does simple checks.
1984  */
1985 
1986 static int
1987 check_rorw(char *v1, char *v2)
1988 {
1989 	int ret = SA_OK;
1990 	if (strcmp(v1, v2) == 0)
1991 		ret = SA_VALUE_CONFLICT;
1992 	return (ret);
1993 }
1994 
1995 /*
1996  * nfs_validate_property(handle, property, parent)
1997  *
1998  * Check that the property has a legitimate value for its type.
1999  */
2000 
2001 static int
2002 nfs_validate_property(sa_handle_t handle, sa_property_t property,
2003     sa_optionset_t parent)
2004 {
2005 	int ret = SA_OK;
2006 	char *propname;
2007 	char *other;
2008 	int optindex;
2009 	nfsl_config_t *configlist;
2010 	sa_group_t parent_group;
2011 	char *value;
2012 
2013 	propname = sa_get_property_attr(property, "type");
2014 
2015 	if ((optindex = findopt(propname)) < 0)
2016 		ret = SA_NO_SUCH_PROP;
2017 
2018 	/* need to validate value range here as well */
2019 
2020 	if (ret == SA_OK) {
2021 		parent_group = sa_get_parent_group((sa_share_t)parent);
2022 		if (optdefs[optindex].share && parent_group != NULL &&
2023 		    !sa_is_share(parent_group))
2024 			ret = SA_PROP_SHARE_ONLY;
2025 	}
2026 	if (ret == SA_OK) {
2027 		if (optdefs[optindex].index == OPT_PUBLIC) {
2028 			/*
2029 			 * Public is special in that only one instance can
2030 			 * be in the repository at the same time.
2031 			 */
2032 			if (public_exists(handle, parent_group)) {
2033 				if (propname != NULL)
2034 					sa_free_attr_string(propname);
2035 				return (SA_VALUE_CONFLICT);
2036 			}
2037 		}
2038 		value = sa_get_property_attr(property, "value");
2039 		if (value != NULL) {
2040 			/* first basic type checking */
2041 			switch (optdefs[optindex].type) {
2042 			case OPT_TYPE_NUMBER:
2043 				/* check that the value is all digits */
2044 				if (!is_a_number(value))
2045 					ret = SA_BAD_VALUE;
2046 				break;
2047 			case OPT_TYPE_BOOLEAN:
2048 				if (strlen(value) == 0 ||
2049 				    strcasecmp(value, "true") == 0 ||
2050 				    strcmp(value, "1") == 0 ||
2051 				    strcasecmp(value, "false") == 0 ||
2052 				    strcmp(value, "0") == 0) {
2053 					ret = SA_OK;
2054 				} else {
2055 					ret = SA_BAD_VALUE;
2056 				}
2057 				break;
2058 			case OPT_TYPE_USER:
2059 				if (!is_a_number(value)) {
2060 					struct passwd *pw;
2061 					/*
2062 					 * in this case it would have to be a
2063 					 * user name
2064 					 */
2065 					pw = getpwnam(value);
2066 					if (pw == NULL)
2067 						ret = SA_BAD_VALUE;
2068 					endpwent();
2069 				} else {
2070 					uint64_t intval;
2071 					intval = strtoull(value, NULL, 0);
2072 					if (intval > UID_MAX && intval != ~0)
2073 						ret = SA_BAD_VALUE;
2074 				}
2075 				break;
2076 			case OPT_TYPE_FILE:
2077 				if (strcmp(value, "..") == 0 ||
2078 				    strchr(value, '/') != NULL) {
2079 					ret = SA_BAD_VALUE;
2080 				}
2081 				break;
2082 			case OPT_TYPE_ACCLIST:
2083 				/*
2084 				 * access list handling. Should eventually
2085 				 * validate that all the values make sense.
2086 				 * Also, ro and rw may have cross value
2087 				 * conflicts.
2088 				 */
2089 				if (strcmp(propname, SHOPT_RO) == 0)
2090 					other = SHOPT_RW;
2091 				else if (strcmp(propname, SHOPT_RW) == 0)
2092 					other = SHOPT_RO;
2093 				else
2094 					other = NULL;
2095 
2096 				if (other != NULL && parent != NULL) {
2097 					/* compare rw(ro) with ro(rw) */
2098 					sa_property_t oprop;
2099 					oprop = sa_get_property(parent, other);
2100 					if (oprop != NULL) {
2101 						/*
2102 						 * only potential
2103 						 * confusion if other
2104 						 * exists
2105 						 */
2106 						char *ovalue;
2107 						ovalue = sa_get_property_attr(
2108 						    oprop, "value");
2109 						if (ovalue != NULL) {
2110 							ret = check_rorw(value,
2111 							    ovalue);
2112 							sa_free_attr_string(
2113 							    ovalue);
2114 						}
2115 					}
2116 				}
2117 				break;
2118 			case OPT_TYPE_LOGTAG:
2119 				if (nfsl_getconfig_list(&configlist) == 0) {
2120 					int error;
2121 					if (value == NULL ||
2122 					    strlen(value) == 0) {
2123 						if (value != NULL)
2124 							sa_free_attr_string(
2125 							    value);
2126 						value = strdup("global");
2127 					}
2128 					if (value != NULL &&
2129 					    nfsl_findconfig(configlist, value,
2130 					    &error) == NULL) {
2131 						ret = SA_BAD_VALUE;
2132 					}
2133 					/* Must always free when done */
2134 					nfsl_freeconfig_list(&configlist);
2135 				} else {
2136 					ret = SA_CONFIG_ERR;
2137 				}
2138 				break;
2139 			case OPT_TYPE_STRING:
2140 				/* whatever is here should be ok */
2141 				break;
2142 			case OPT_TYPE_SECURITY:
2143 				/*
2144 				 * The "sec" property isn't used in the
2145 				 * non-legacy parts of sharemgr. We need to
2146 				 * reject it here. For legacy, it is pulled
2147 				 * out well before we get here.
2148 				 */
2149 				ret = SA_NO_SUCH_PROP;
2150 				break;
2151 			default:
2152 				break;
2153 			}
2154 
2155 			if (value != NULL)
2156 				sa_free_attr_string(value);
2157 
2158 			if (ret == SA_OK && optdefs[optindex].check != NULL) {
2159 				/* do the property specific check */
2160 				ret = optdefs[optindex].check(handle, property);
2161 			}
2162 		}
2163 	}
2164 
2165 	if (propname != NULL)
2166 		sa_free_attr_string(propname);
2167 	return (ret);
2168 }
2169 
2170 /*
2171  * Protocol management functions
2172  *
2173  * Properties defined in the default files are defined in
2174  * proto_option_defs for parsing and validation. If "other" and
2175  * "compare" are set, then the value for this property should be
2176  * compared against the property specified in "other" using the
2177  * "compare" check (either <= or >=) in order to ensure that the
2178  * values are in the correct range.  E.g. setting server_versmin
2179  * higher than server_versmax should not be allowed.
2180  */
2181 
2182 struct proto_option_defs {
2183 	char *tag;
2184 	char *name;	/* display name -- remove protocol identifier */
2185 	int index;
2186 	int type;
2187 	union {
2188 	    int intval;
2189 	    char *string;
2190 	} defvalue;
2191 	uint32_t svcs;
2192 	int32_t minval;
2193 	int32_t maxval;
2194 	char *file;
2195 	char *other;
2196 	int compare;
2197 #define	OPT_CMP_GE	0
2198 #define	OPT_CMP_LE	1
2199 	int (*check)(char *);
2200 } proto_options[] = {
2201 #define	PROTO_OPT_NFSD_SERVERS			0
2202 	{"nfsd_servers",
2203 	    "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD,
2204 	    1, INT32_MAX, NFSADMIN},
2205 #define	PROTO_OPT_LOCKD_LISTEN_BACKLOG		1
2206 	{"lockd_listen_backlog",
2207 	    "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG,
2208 	    OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN},
2209 #define	PROTO_OPT_LOCKD_SERVERS			2
2210 	{"lockd_servers",
2211 	    "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20,
2212 	    SVC_LOCKD, 1, INT32_MAX, NFSADMIN},
2213 #define	PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT	3
2214 	{"lockd_retransmit_timeout",
2215 	    "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT,
2216 	    OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
2217 #define	PROTO_OPT_GRACE_PERIOD			4
2218 	{"grace_period",
2219 	    "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90,
2220 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
2221 #define	PROTO_OPT_NFS_SERVER_VERSMIN		5
2222 	{"nfs_server_versmin",
2223 	    "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER,
2224 	    (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2225 	    NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE},
2226 #define	PROTO_OPT_NFS_SERVER_VERSMAX		6
2227 	{"nfs_server_versmax",
2228 	    "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER,
2229 	    (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN,
2230 	    NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE},
2231 #define	PROTO_OPT_NFS_CLIENT_VERSMIN		7
2232 	{"nfs_client_versmin",
2233 	    "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER,
2234 	    (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
2235 	    NFSADMIN, "client_versmax", OPT_CMP_LE},
2236 #define	PROTO_OPT_NFS_CLIENT_VERSMAX		8
2237 	{"nfs_client_versmax",
2238 	    "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER,
2239 	    (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX,
2240 	    NFSADMIN, "client_versmin", OPT_CMP_GE},
2241 #define	PROTO_OPT_NFS_SERVER_DELEGATION		9
2242 	{"nfs_server_delegation",
2243 	    "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION,
2244 	    OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0,
2245 	    NFSADMIN},
2246 #define	PROTO_OPT_NFSMAPID_DOMAIN		10
2247 	{"nfsmapid_domain",
2248 	    "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN,
2249 	    NULL, SVC_NFSMAPID, 0, 0, NFSADMIN},
2250 #define	PROTO_OPT_NFSD_MAX_CONNECTIONS		11
2251 	{"nfsd_max_connections",
2252 	    "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS,
2253 	    OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN},
2254 #define	PROTO_OPT_NFSD_PROTOCOL			12
2255 	{"nfsd_protocol",
2256 	    "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0,
2257 	    SVC_NFSD, 0, 0, NFSADMIN},
2258 #define	PROTO_OPT_NFSD_LISTEN_BACKLOG		13
2259 	{"nfsd_listen_backlog",
2260 	    "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG,
2261 	    OPT_TYPE_NUMBER, 0,
2262 	    SVC_LOCKD, 0, INT32_MAX, NFSADMIN},
2263 	{NULL}
2264 };
2265 
2266 /*
2267  * the protoset holds the defined options so we don't have to read
2268  * them multiple times
2269  */
2270 static sa_protocol_properties_t protoset;
2271 
2272 static int
2273 findprotoopt(char *name, int whichname)
2274 {
2275 	int i;
2276 	for (i = 0; proto_options[i].tag != NULL; i++) {
2277 		if (whichname == 1) {
2278 			if (strcasecmp(proto_options[i].name, name) == 0)
2279 			return (i);
2280 		} else {
2281 			if (strcasecmp(proto_options[i].tag, name) == 0)
2282 				return (i);
2283 		}
2284 	}
2285 	return (-1);
2286 }
2287 
2288 /*
2289  * fixcaselower(str)
2290  *
2291  * convert a string to lower case (inplace).
2292  */
2293 
2294 static void
2295 fixcaselower(char *str)
2296 {
2297 	while (*str) {
2298 		*str = tolower(*str);
2299 		str++;
2300 	}
2301 }
2302 
2303 /*
2304  * fixcaseupper(str)
2305  *
2306  * convert a string to upper case (inplace).
2307  */
2308 
2309 static void
2310 fixcaseupper(char *str)
2311 {
2312 	while (*str) {
2313 		*str = toupper(*str);
2314 		str++;
2315 	}
2316 }
2317 
2318 /*
2319  * skipwhitespace(str)
2320  *
2321  * Skip leading white space. It is assumed that it is called with a
2322  * valid pointer.
2323  */
2324 
2325 static char *
2326 skipwhitespace(char *str)
2327 {
2328 	while (*str && isspace(*str))
2329 		str++;
2330 
2331 	return (str);
2332 }
2333 
2334 /*
2335  * extractprop()
2336  *
2337  * Extract the property and value out of the line and create the
2338  * property in the optionset.
2339  */
2340 static int
2341 extractprop(char *name, char *value)
2342 {
2343 	sa_property_t prop;
2344 	int index;
2345 	int ret = SA_OK;
2346 	/*
2347 	 * Remove any leading
2348 	 * white space.
2349 	 */
2350 	name = skipwhitespace(name);
2351 
2352 	index = findprotoopt(name, 0);
2353 	if (index >= 0) {
2354 		fixcaselower(name);
2355 		prop = sa_create_property(proto_options[index].name, value);
2356 		if (prop != NULL)
2357 			ret = sa_add_protocol_property(protoset, prop);
2358 		else
2359 			ret = SA_NO_MEMORY;
2360 	}
2361 	return (ret);
2362 }
2363 
2364 /*
2365  * initprotofromdefault()
2366  *
2367  * Read the default file(s) and add the defined values to the
2368  * protoset.  Note that default values are known from the built in
2369  * table in case the file doesn't have a definition. Not having the
2370  * /etc/default/nfs file is OK since we have builtin default
2371  * values. The default file will get constructed as needed if values
2372  * are changed from the defaults.
2373  */
2374 
2375 static int
2376 initprotofromdefault()
2377 {
2378 	FILE *nfs;
2379 	char buff[BUFSIZ];
2380 	char *name;
2381 	char *value;
2382 	int ret = SA_OK;
2383 
2384 	protoset = sa_create_protocol_properties("nfs");
2385 
2386 	if (protoset != NULL) {
2387 		nfs = fopen(NFSADMIN, "r");
2388 		if (nfs != NULL) {
2389 			while (ret == SA_OK &&
2390 			    fgets(buff, sizeof (buff), nfs) != NULL) {
2391 				switch (buff[0]) {
2392 				case '\n':
2393 				case '#':
2394 					/* skip */
2395 					break;
2396 				default:
2397 					name = buff;
2398 					buff[strlen(buff) - 1] = '\0';
2399 					value = strchr(name, '=');
2400 					if (value != NULL) {
2401 						*value++ = '\0';
2402 						ret = extractprop(name, value);
2403 					}
2404 				}
2405 			}
2406 			(void) fclose(nfs);
2407 		} else {
2408 			switch (errno) {
2409 			case EPERM:
2410 			case EACCES:
2411 				ret = SA_NO_PERMISSION;
2412 				break;
2413 			case ENOENT:
2414 				break;
2415 			default:
2416 				ret = SA_SYSTEM_ERR;
2417 				break;
2418 			}
2419 		}
2420 	} else {
2421 		ret = SA_NO_MEMORY;
2422 	}
2423 	return (ret);
2424 }
2425 
2426 /*
2427  * add_defaults()
2428  *
2429  * Add the default values for any property not defined in the parsing
2430  * of the default files. Values are set according to their defined
2431  * types.
2432  */
2433 
2434 static void
2435 add_defaults()
2436 {
2437 	int i;
2438 	char number[MAXDIGITS];
2439 
2440 	for (i = 0; proto_options[i].tag != NULL; i++) {
2441 		sa_property_t prop;
2442 		prop = sa_get_protocol_property(protoset,
2443 		    proto_options[i].name);
2444 		if (prop == NULL) {
2445 			/* add the default value */
2446 			switch (proto_options[i].type) {
2447 			case OPT_TYPE_NUMBER:
2448 				(void) snprintf(number, sizeof (number), "%d",
2449 				    proto_options[i].defvalue.intval);
2450 				prop = sa_create_property(proto_options[i].name,
2451 				    number);
2452 				break;
2453 
2454 			case OPT_TYPE_BOOLEAN:
2455 				prop = sa_create_property(proto_options[i].name,
2456 				    proto_options[i].defvalue.intval ?
2457 				    "true" : "false");
2458 				break;
2459 
2460 			case OPT_TYPE_ONOFF:
2461 				prop = sa_create_property(proto_options[i].name,
2462 				    proto_options[i].defvalue.intval ?
2463 				    "on" : "off");
2464 				break;
2465 
2466 			default:
2467 				/* treat as strings of zero length */
2468 				prop = sa_create_property(proto_options[i].name,
2469 				    "");
2470 				break;
2471 			}
2472 			if (prop != NULL)
2473 				(void) sa_add_protocol_property(protoset, prop);
2474 		}
2475 	}
2476 }
2477 
2478 static void
2479 free_protoprops()
2480 {
2481 	if (protoset != NULL) {
2482 		xmlFreeNode(protoset);
2483 		protoset = NULL;
2484 	}
2485 }
2486 
2487 /*
2488  * nfs_init()
2489  *
2490  * Initialize the NFS plugin.
2491  */
2492 
2493 static int
2494 nfs_init()
2495 {
2496 	int ret = SA_OK;
2497 
2498 	if (sa_plugin_ops.sa_init != nfs_init) {
2499 		(void) printf(dgettext(TEXT_DOMAIN,
2500 		    "NFS plugin not properly initialized\n"));
2501 		return (SA_CONFIG_ERR);
2502 	}
2503 
2504 	ret = initprotofromdefault();
2505 	if (ret != SA_OK) {
2506 		(void) printf(dgettext(TEXT_DOMAIN,
2507 		    "NFS plugin problem with default file: %s\n"),
2508 		    sa_errorstr(ret));
2509 		ret = SA_OK;
2510 	}
2511 	add_defaults();
2512 
2513 	return (ret);
2514 }
2515 
2516 /*
2517  * nfs_fini()
2518  *
2519  * uninitialize the NFS plugin. Want to avoid memory leaks.
2520  */
2521 
2522 static void
2523 nfs_fini()
2524 {
2525 	free_protoprops();
2526 }
2527 
2528 /*
2529  * nfs_get_proto_set()
2530  *
2531  * Return an optionset with all the protocol specific properties in
2532  * it.
2533  */
2534 
2535 static sa_protocol_properties_t
2536 nfs_get_proto_set()
2537 {
2538 	return (protoset);
2539 }
2540 
2541 struct deffile {
2542 	struct deffile *next;
2543 	char *line;
2544 };
2545 
2546 /*
2547  * read_default_file(fname)
2548  *
2549  * Read the specified default file. We return a list of entries. This
2550  * get used for adding or removing values.
2551  */
2552 
2553 static struct deffile *
2554 read_default_file(char *fname)
2555 {
2556 	FILE *file;
2557 	struct deffile *defs = NULL;
2558 	struct deffile *newdef;
2559 	struct deffile *prevdef = NULL;
2560 	char buff[BUFSIZ * 2];
2561 
2562 	file = fopen(fname, "r");
2563 	if (file != NULL) {
2564 		while (fgets(buff, sizeof (buff), file) != NULL) {
2565 			newdef = (struct deffile *)calloc(1,
2566 			    sizeof (struct deffile));
2567 			if (newdef != NULL) {
2568 				/* Make sure we skip any leading whitespace. */
2569 				newdef->line = strdup(skipwhitespace(buff));
2570 				if (defs == NULL) {
2571 					prevdef = defs = newdef;
2572 				} else {
2573 					prevdef->next = newdef;
2574 					prevdef = newdef;
2575 				}
2576 			}
2577 		}
2578 		(void) fclose(file);
2579 	} else {
2580 		int ret = SA_OK;
2581 		switch (errno) {
2582 		case EPERM:
2583 		case EACCES:
2584 			ret = SA_NO_PERMISSION;
2585 			break;
2586 		case ENOENT:
2587 			break;
2588 		default:
2589 			ret = SA_SYSTEM_ERR;
2590 			break;
2591 		}
2592 		if (ret == SA_OK) {
2593 			/* Want at least one comment line */
2594 			defs = (struct deffile *)
2595 			    calloc(1, sizeof (struct deffile));
2596 			defs->line = strdup("# NFS default file\n");
2597 		}
2598 	}
2599 	return (defs);
2600 }
2601 
2602 static void
2603 free_default_file(struct deffile *defs)
2604 {
2605 	struct deffile *curdefs = NULL;
2606 
2607 	while (defs != NULL) {
2608 		curdefs = defs;
2609 		defs = defs->next;
2610 		if (curdefs->line != NULL)
2611 			free(curdefs->line);
2612 		free(curdefs);
2613 	}
2614 }
2615 
2616 /*
2617  * write_default_file(fname, defs)
2618  *
2619  * Write the default file back.
2620  */
2621 
2622 static int
2623 write_default_file(char *fname, struct deffile *defs)
2624 {
2625 	FILE *file;
2626 	int ret = SA_OK;
2627 	sigset_t old, new;
2628 
2629 	file = fopen(fname, "w+");
2630 	if (file != NULL) {
2631 		(void) sigprocmask(SIG_BLOCK, NULL, &new);
2632 		(void) sigaddset(&new, SIGHUP);
2633 		(void) sigaddset(&new, SIGINT);
2634 		(void) sigaddset(&new, SIGQUIT);
2635 		(void) sigaddset(&new, SIGTSTP);
2636 		(void) sigprocmask(SIG_SETMASK, &new, &old);
2637 		while (defs != NULL) {
2638 			(void) fputs(defs->line, file);
2639 			defs = defs->next;
2640 		}
2641 		(void) fsync(fileno(file));
2642 		(void) sigprocmask(SIG_SETMASK, &old, NULL);
2643 		(void) fclose(file);
2644 	} else {
2645 		switch (errno) {
2646 		case EPERM:
2647 		case EACCES:
2648 			ret = SA_NO_PERMISSION;
2649 			break;
2650 		default:
2651 			ret = SA_SYSTEM_ERR;
2652 		}
2653 	}
2654 	return (ret);
2655 }
2656 
2657 
2658 /*
2659  * set_default_file_value(tag, value)
2660  *
2661  * Set the default file value for tag to value. Then rewrite the file.
2662  * tag and value are always set.  The caller must ensure this.
2663  */
2664 
2665 #define	MAX_STRING_LENGTH	256
2666 static int
2667 set_default_file_value(char *tag, char *value)
2668 {
2669 	int ret = SA_OK;
2670 	struct deffile *root;
2671 	struct deffile *defs;
2672 	struct deffile *prev;
2673 	char string[MAX_STRING_LENGTH];
2674 	int len;
2675 	boolean_t update = B_FALSE;
2676 
2677 	(void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag);
2678 	len = strlen(string);
2679 
2680 	root = defs = read_default_file(NFSADMIN);
2681 	if (root == NULL) {
2682 		switch (errno) {
2683 		case EPERM:
2684 		case EACCES:
2685 			ret = SA_NO_PERMISSION;
2686 			break;
2687 		default:
2688 			ret = SA_NO_MEMORY;
2689 			break;
2690 		}
2691 		return (ret);
2692 	}
2693 
2694 	while (defs != NULL) {
2695 		if (defs->line != NULL &&
2696 		    strncasecmp(defs->line, string, len) == 0) {
2697 			/* replace with the new value */
2698 			free(defs->line);
2699 			fixcaseupper(tag);
2700 			(void) snprintf(string, sizeof (string),
2701 			    "%s=%s\n", tag, value);
2702 			string[MAX_STRING_LENGTH - 1] = '\0';
2703 			defs->line = strdup(string);
2704 			update = B_TRUE;
2705 			break;
2706 		}
2707 		defs = defs->next;
2708 	}
2709 	if (!update) {
2710 		defs = root;
2711 		/* didn't find, so see if it is a comment */
2712 		(void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag);
2713 		len = strlen(string);
2714 		while (defs != NULL) {
2715 			if (strncasecmp(defs->line, string, len) == 0) {
2716 				/* replace with the new value */
2717 				free(defs->line);
2718 				fixcaseupper(tag);
2719 				(void) snprintf(string, sizeof (string),
2720 				    "%s=%s\n", tag, value);
2721 				string[MAX_STRING_LENGTH - 1] = '\0';
2722 				defs->line = strdup(string);
2723 				update = B_TRUE;
2724 				break;
2725 			}
2726 			defs = defs->next;
2727 		}
2728 	}
2729 	if (!update) {
2730 		fixcaseupper(tag);
2731 		(void) snprintf(string, sizeof (string), "%s=%s\n",
2732 		    tag, value);
2733 		prev = root;
2734 		while (prev->next != NULL)
2735 			prev = prev->next;
2736 		defs = malloc(sizeof (struct deffile));
2737 		prev->next = defs;
2738 		if (defs != NULL) {
2739 			defs->next = NULL;
2740 			defs->line = strdup(string);
2741 			update = B_TRUE;
2742 		}
2743 	}
2744 	if (update) {
2745 		ret = write_default_file(NFSADMIN, root);
2746 	}
2747 	free_default_file(root);
2748 	return (ret);
2749 }
2750 
2751 /*
2752  * service_in_state(service, chkstate)
2753  *
2754  * Want to know if the specified service is in the desired state
2755  * (chkstate) or not. Return true (1) if it is and false (0) if it
2756  * isn't.
2757  */
2758 static int
2759 service_in_state(char *service, const char *chkstate)
2760 {
2761 	char *state;
2762 	int ret = B_FALSE;
2763 
2764 	state = smf_get_state(service);
2765 	if (state != NULL) {
2766 		/* got the state so get the equality for the return value */
2767 		ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE;
2768 		free(state);
2769 	}
2770 	return (ret);
2771 }
2772 
2773 /*
2774  * restart_service(svcs)
2775  *
2776  * Walk through the bit mask of services that need to be restarted in
2777  * order to use the new property values. Some properties affect
2778  * multiple daemons. Should only restart a service if it is currently
2779  * enabled (online).
2780  */
2781 
2782 static void
2783 restart_service(uint32_t svcs)
2784 {
2785 	uint32_t mask;
2786 	int ret;
2787 	char *service;
2788 
2789 	for (mask = 1; svcs != 0; mask <<= 1) {
2790 		switch (svcs & mask) {
2791 		case SVC_LOCKD:
2792 			service = LOCKD;
2793 			break;
2794 		case SVC_STATD:
2795 			service = STATD;
2796 			break;
2797 		case SVC_NFSD:
2798 			service = NFSD;
2799 			break;
2800 		case SVC_MOUNTD:
2801 			service = MOUNTD;
2802 			break;
2803 		case SVC_NFS4CBD:
2804 			service = NFS4CBD;
2805 			break;
2806 		case SVC_NFSMAPID:
2807 			service = NFSMAPID;
2808 			break;
2809 		case SVC_RQUOTAD:
2810 			service = RQUOTAD;
2811 			break;
2812 		case SVC_NFSLOGD:
2813 			service = NFSLOGD;
2814 			break;
2815 		default:
2816 			continue;
2817 		}
2818 
2819 		/*
2820 		 * Only attempt to restart the service if it is
2821 		 * currently running. In the future, it may be
2822 		 * desirable to use smf_refresh_instance if the NFS
2823 		 * services ever implement the refresh method.
2824 		 */
2825 		if (service_in_state(service, SCF_STATE_STRING_ONLINE)) {
2826 			ret = smf_restart_instance(service);
2827 			/*
2828 			 * There are only a few SMF errors at this point, but
2829 			 * it is also possible that a bad value may have put
2830 			 * the service into maintenance if there wasn't an
2831 			 * SMF level error.
2832 			 */
2833 			if (ret != 0) {
2834 				(void) fprintf(stderr,
2835 				    dgettext(TEXT_DOMAIN,
2836 				    "%s failed to restart: %s\n"),
2837 				    scf_strerror(scf_error()));
2838 			} else {
2839 				/*
2840 				 * Check whether it has gone to "maintenance"
2841 				 * mode or not. Maintenance implies something
2842 				 * went wrong.
2843 				 */
2844 				if (service_in_state(service,
2845 				    SCF_STATE_STRING_MAINT)) {
2846 					(void) fprintf(stderr,
2847 					    dgettext(TEXT_DOMAIN,
2848 					    "%s failed to restart\n"),
2849 					    service);
2850 				}
2851 			}
2852 		}
2853 		svcs &= ~mask;
2854 	}
2855 }
2856 
2857 /*
2858  * nfs_minmax_check(name, value)
2859  *
2860  * Verify that the value for the property specified by index is valid
2861  * relative to the opposite value in the case of a min/max variable.
2862  * Currently, server_minvers/server_maxvers and
2863  * client_minvers/client_maxvers are the only ones to check.
2864  */
2865 
2866 static int
2867 nfs_minmax_check(int index, int value)
2868 {
2869 	int val;
2870 	char *pval;
2871 	sa_property_t prop;
2872 	sa_optionset_t opts;
2873 	int ret = B_TRUE;
2874 
2875 	if (proto_options[index].other != NULL) {
2876 		/* have a property to compare against */
2877 		opts = nfs_get_proto_set();
2878 		prop = sa_get_property(opts, proto_options[index].other);
2879 		/*
2880 		 * If we don't find the property, assume default
2881 		 * values which will work since the max will be at the
2882 		 * max and the min at the min.
2883 		 */
2884 		if (prop != NULL) {
2885 			pval = sa_get_property_attr(prop, "value");
2886 			if (pval != NULL) {
2887 				val = strtoul(pval, NULL, 0);
2888 				if (proto_options[index].compare ==
2889 				    OPT_CMP_LE) {
2890 					ret = value <= val ? B_TRUE : B_FALSE;
2891 				} else if (proto_options[index].compare ==
2892 				    OPT_CMP_GE) {
2893 					ret = value >= val ? B_TRUE : B_FALSE;
2894 				}
2895 			}
2896 		}
2897 	}
2898 	return (ret);
2899 }
2900 
2901 /*
2902  * nfs_validate_proto_prop(index, name, value)
2903  *
2904  * Verify that the property specified by name can take the new
2905  * value. This is a sanity check to prevent bad values getting into
2906  * the default files. All values need to be checked against what is
2907  * allowed by their defined type. If a type isn't explicitly defined
2908  * here, it is treated as a string.
2909  *
2910  * Note that OPT_TYPE_NUMBER will additionally check that the value is
2911  * within the range specified and potentially against another property
2912  * value as well as specified in the proto_options members other and
2913  * compare.
2914  */
2915 
2916 static int
2917 nfs_validate_proto_prop(int index, char *name, char *value)
2918 {
2919 	int ret = SA_OK;
2920 	char *cp;
2921 #ifdef lint
2922 	name = name;
2923 #endif
2924 
2925 	switch (proto_options[index].type) {
2926 	case OPT_TYPE_NUMBER:
2927 		if (!is_a_number(value))
2928 			ret = SA_BAD_VALUE;
2929 		else {
2930 			int val;
2931 			val = strtoul(value, NULL, 0);
2932 			if (val < proto_options[index].minval ||
2933 			    val > proto_options[index].maxval)
2934 				ret = SA_BAD_VALUE;
2935 			/*
2936 			 * For server_versmin/server_versmax and
2937 			 * client_versmin/client_versmax, the value of the
2938 			 * min(max) should be checked to be correct relative
2939 			 * to the current max(min).
2940 			 */
2941 			if (!nfs_minmax_check(index, val)) {
2942 				ret = SA_BAD_VALUE;
2943 			}
2944 		}
2945 		break;
2946 
2947 	case OPT_TYPE_DOMAIN:
2948 		/*
2949 		 * needs to be a qualified domain so will have at
2950 		 * least one period and other characters on either
2951 		 * side of it.  A zero length string is also allowed
2952 		 * and is the way to turn off the override.
2953 		 */
2954 		if (strlen(value) == 0)
2955 			break;
2956 		cp = strchr(value, '.');
2957 		if (cp == NULL || cp == value || strchr(value, '@') != NULL)
2958 			ret = SA_BAD_VALUE;
2959 		break;
2960 
2961 	case OPT_TYPE_BOOLEAN:
2962 		if (strlen(value) == 0 ||
2963 		    strcasecmp(value, "true") == 0 ||
2964 		    strcmp(value, "1") == 0 ||
2965 		    strcasecmp(value, "false") == 0 ||
2966 		    strcmp(value, "0") == 0) {
2967 			ret = SA_OK;
2968 		} else {
2969 			ret = SA_BAD_VALUE;
2970 		}
2971 		break;
2972 
2973 	case OPT_TYPE_ONOFF:
2974 		if (strcasecmp(value, "on") != 0 &&
2975 		    strcasecmp(value, "off") != 0) {
2976 			ret = SA_BAD_VALUE;
2977 		}
2978 		break;
2979 
2980 	case OPT_TYPE_PROTOCOL:
2981 		if (strlen(value) != 0 &&
2982 		    strcasecmp(value, "all") != 0 &&
2983 		    strcasecmp(value, "tcp") != 0 &&
2984 		    strcasecmp(value, "udp") != 0)
2985 			ret = SA_BAD_VALUE;
2986 		break;
2987 
2988 	default:
2989 		/* treat as a string */
2990 		break;
2991 	}
2992 	return (ret);
2993 }
2994 
2995 /*
2996  * nfs_set_proto_prop(prop)
2997  *
2998  * check that prop is valid.
2999  */
3000 
3001 static int
3002 nfs_set_proto_prop(sa_property_t prop)
3003 {
3004 	int ret = SA_OK;
3005 	char *name;
3006 	char *value;
3007 
3008 	name = sa_get_property_attr(prop, "type");
3009 	value = sa_get_property_attr(prop, "value");
3010 	if (name != NULL && value != NULL) {
3011 		int index = findprotoopt(name, 1);
3012 		if (index >= 0) {
3013 			/* should test for valid value */
3014 			ret = nfs_validate_proto_prop(index, name, value);
3015 			if (ret == SA_OK)
3016 				ret = set_default_file_value(
3017 				    proto_options[index].tag, value);
3018 			if (ret == SA_OK)
3019 				restart_service(proto_options[index].svcs);
3020 		}
3021 	}
3022 	if (name != NULL)
3023 		sa_free_attr_string(name);
3024 	if (value != NULL)
3025 		sa_free_attr_string(value);
3026 	return (ret);
3027 }
3028 
3029 /*
3030  * nfs_get_status()
3031  *
3032  * What is the current status of the nfsd? We use the SMF state here.
3033  * Caller must free the returned value.
3034  */
3035 
3036 static char *
3037 nfs_get_status()
3038 {
3039 	char *state;
3040 	state = smf_get_state(NFSD);
3041 	return (state != NULL ? state : strdup("-"));
3042 }
3043 
3044 /*
3045  * nfs_space_alias(alias)
3046  *
3047  * Lookup the space (security) name. If it is default, convert to the
3048  * real name.
3049  */
3050 
3051 static char *
3052 nfs_space_alias(char *space)
3053 {
3054 	char *name = space;
3055 	seconfig_t secconf;
3056 
3057 	/*
3058 	 * Only the space named "default" is special. If it is used,
3059 	 * the default needs to be looked up and the real name used.
3060 	 * This is normally "sys" but could be changed.  We always
3061 	 * change defautl to the real name.
3062 	 */
3063 	if (strcmp(space, "default") == 0 &&
3064 	    nfs_getseconfig_default(&secconf) == 0) {
3065 		if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0)
3066 			name = secconf.sc_name;
3067 	}
3068 	return (strdup(name));
3069 }
3070 
3071 /*
3072  * nfs_features()
3073  *
3074  * Return a mask of the features required.
3075  */
3076 
3077 static uint64_t
3078 nfs_features()
3079 {
3080 	return ((uint64_t)SA_FEATURE_DFSTAB | SA_FEATURE_SERVER);
3081 }
3082