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