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