xref: /titanic_50/usr/src/cmd/dfs.cmds/sharemgr/commands.c (revision d62bc4badc1c1f1549c961cfb8b420e650e1272b)
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 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <unistd.h>
37 #include <getopt.h>
38 #include <utmpx.h>
39 #include <pwd.h>
40 #include <auth_attr.h>
41 #include <secdb.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <errno.h>
45 
46 #include <libshare.h>
47 #include "sharemgr.h"
48 #include <libscf.h>
49 #include <libxml/tree.h>
50 #include <libintl.h>
51 #include <assert.h>
52 #include <iconv.h>
53 #include <langinfo.h>
54 #include <dirent.h>
55 
56 static char *sa_get_usage(sa_usage_t);
57 
58 /*
59  * Implementation of the common sub-commands supported by sharemgr.
60  * A number of helper functions are also included.
61  */
62 
63 /*
64  * has_protocol(group, proto)
65  *	If the group has an optionset with the specified protocol,
66  *	return true (1) otherwise false (0).
67  */
68 static int
69 has_protocol(sa_group_t group, char *protocol)
70 {
71 	sa_optionset_t optionset;
72 	int result = 0;
73 
74 	optionset = sa_get_optionset(group, protocol);
75 	if (optionset != NULL) {
76 		result++;
77 	}
78 	return (result);
79 }
80 
81 /*
82  * validresource(name)
83  *
84  * Check that name only has valid characters in it. The current valid
85  * set are the printable characters but not including:
86  *	" / \ [ ] : | < > + ; , ? * = \t
87  * Note that space is included and there is a maximum length.
88  */
89 static int
90 validresource(const char *name)
91 {
92 	const char *cp;
93 	size_t len;
94 
95 	if (name == NULL)
96 		return (B_FALSE);
97 
98 	len = strlen(name);
99 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
100 		return (B_FALSE);
101 
102 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
103 		return (B_FALSE);
104 	}
105 
106 	for (cp = name; *cp != '\0'; cp++)
107 		if (iscntrl(*cp))
108 			return (B_FALSE);
109 
110 	return (B_TRUE);
111 }
112 
113 /*
114  * conv_to_utf8(input)
115  *
116  * Convert the input string to utf8 from the current locale.  If the
117  * conversion fails, use the current locale, it is likely close
118  * enough. For example, the "C" locale is a subset of utf-8. The
119  * return value may be a new string or the original input string.
120  */
121 
122 static char *
123 conv_to_utf8(char *input)
124 {
125 	iconv_t cd;
126 	char *inval = input;
127 	char *output = input;
128 	char *outleft;
129 	char *curlocale;
130 	size_t bytesleft;
131 	size_t size;
132 	size_t osize;
133 	static int warned = 0;
134 
135 	curlocale = nl_langinfo(CODESET);
136 	if (curlocale == NULL)
137 		curlocale = "C";
138 	cd = iconv_open("UTF-8", curlocale);
139 	if (cd != NULL && cd != (iconv_t)-1) {
140 		size = strlen(input);
141 		/* Assume worst case of characters expanding to 4 bytes. */
142 		bytesleft = size * 4;
143 		output = calloc(bytesleft, 1);
144 		if (output != NULL) {
145 			outleft = output;
146 			/* inval can be modified on return */
147 			osize = iconv(cd, (const char **)&inval, &size,
148 			    &outleft, &bytesleft);
149 			if (osize == (size_t)-1 || size != 0) {
150 				free(output);
151 				output = input;
152 			}
153 		} else {
154 			/* Need to return something. */
155 			output = input;
156 		}
157 		(void) iconv_close(cd);
158 	} else {
159 		if (!warned)
160 			(void) fprintf(stderr,
161 			    gettext("Cannot convert to UTF-8 from %s\n"),
162 			    curlocale ? curlocale : gettext("unknown"));
163 		warned = 1;
164 	}
165 	return (output);
166 }
167 
168 /*
169  * conv_from(input)
170  *
171  * Convert the input string from utf8 to current locale.  If the
172  * conversion isn't supported, just use as is. The return value may be
173  * a new string or the original input string.
174  */
175 
176 static char *
177 conv_from_utf8(char *input)
178 {
179 	iconv_t cd;
180 	char *output = input;
181 	char *inval = input;
182 	char *outleft;
183 	char *curlocale;
184 	size_t bytesleft;
185 	size_t size;
186 	size_t osize;
187 	static int warned = 0;
188 
189 	curlocale = nl_langinfo(CODESET);
190 	if (curlocale == NULL)
191 		curlocale = "C";
192 	cd = iconv_open(curlocale, "UTF-8");
193 	if (cd != NULL && cd != (iconv_t)-1) {
194 		size = strlen(input);
195 		/* Assume worst case of characters expanding to 4 bytes. */
196 		bytesleft = size * 4;
197 		output = calloc(bytesleft, 1);
198 		if (output != NULL) {
199 			outleft = output;
200 			osize = iconv(cd, (const char **)&inval, &size,
201 			    &outleft, &bytesleft);
202 			if (osize == (size_t)-1 || size != 0)
203 				output = input;
204 		} else {
205 			/* Need to return something. */
206 			output = input;
207 		}
208 		(void) iconv_close(cd);
209 	} else {
210 		if (!warned)
211 			(void) fprintf(stderr,
212 			    gettext("Cannot convert to %s from UTF-8\n"),
213 			    curlocale ? curlocale : gettext("unknown"));
214 		warned = 1;
215 	}
216 	return (output);
217 }
218 
219 /*
220  * print_rsrc_desc(resource, sharedesc)
221  *
222  * Print the resource description string after converting from UTF8 to
223  * the current locale. If sharedesc is not NULL and there is no
224  * description on the resource, use sharedesc. sharedesc will already
225  * be converted to UTF8.
226  */
227 
228 static void
229 print_rsrc_desc(sa_resource_t resource, char *sharedesc)
230 {
231 	char *description;
232 	char *desc;
233 
234 	if (resource == NULL)
235 		return;
236 
237 	description = sa_get_resource_description(resource);
238 	if (description != NULL) {
239 		desc = conv_from_utf8(description);
240 		if (desc != description) {
241 			sa_free_share_description(description);
242 			description = desc;
243 		}
244 	} else if (sharedesc != NULL) {
245 		description = strdup(sharedesc);
246 	}
247 	if (description != NULL) {
248 		(void) printf("\t\"%s\"", description);
249 		sa_free_share_description(description);
250 	}
251 }
252 
253 /*
254  * set_resource_desc(share, description)
255  *
256  * Set the share description value after converting the description
257  * string to UTF8 from the current locale.
258  */
259 
260 static int
261 set_resource_desc(sa_share_t share, char *description)
262 {
263 	char *desc;
264 	int ret;
265 
266 	desc = conv_to_utf8(description);
267 	ret = sa_set_resource_description(share, desc);
268 	if (description != desc)
269 		sa_free_share_description(desc);
270 	return (ret);
271 }
272 
273 /*
274  * set_share_desc(share, description)
275  *
276  * Set the resource description value after converting the description
277  * string to UTF8 from the current locale.
278  */
279 
280 static int
281 set_share_desc(sa_share_t share, char *description)
282 {
283 	char *desc;
284 	int ret;
285 
286 	desc = conv_to_utf8(description);
287 	ret = sa_set_share_description(share, desc);
288 	if (description != desc)
289 		sa_free_share_description(desc);
290 	return (ret);
291 }
292 
293 /*
294  * add_list(list, item, data, proto)
295  *	Adds a new list member that points holds item in the list.
296  *	If list is NULL, it starts a new list.  The function returns
297  *	the first member of the list.
298  */
299 struct list *
300 add_list(struct list *listp, void *item, void *data, char *proto)
301 {
302 	struct list *new, *tmp;
303 
304 	new = malloc(sizeof (struct list));
305 	if (new != NULL) {
306 		new->next = NULL;
307 		new->item = item;
308 		new->itemdata = data;
309 		new->proto = proto;
310 	} else {
311 		return (listp);
312 	}
313 
314 	if (listp == NULL)
315 		return (new);
316 
317 	for (tmp = listp; tmp->next != NULL; tmp = tmp->next) {
318 		/* get to end of list */
319 	}
320 	tmp->next = new;
321 	return (listp);
322 }
323 
324 /*
325  * free_list(list)
326  *	Given a list, free all the members of the list;
327  */
328 static void
329 free_list(struct list *listp)
330 {
331 	struct list *tmp;
332 	while (listp != NULL) {
333 		tmp = listp;
334 		listp = listp->next;
335 		free(tmp);
336 	}
337 }
338 
339 /*
340  * check_authorization(instname, which)
341  *
342  * Checks to see if the specific type of authorization in which is
343  * enabled for the user in this SMF service instance.
344  */
345 
346 static int
347 check_authorization(char *instname, int which)
348 {
349 	scf_handle_t *handle = NULL;
350 	scf_simple_prop_t *prop = NULL;
351 	char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
352 	char *authstr = NULL;
353 	ssize_t numauths;
354 	int ret = B_TRUE;
355 	uid_t uid;
356 	struct passwd *pw = NULL;
357 
358 	uid = getuid();
359 	pw = getpwuid(uid);
360 	if (pw == NULL) {
361 		ret = B_FALSE;
362 	} else {
363 		/*
364 		 * Since names are restricted to SA_MAX_NAME_LEN won't
365 		 * overflow.
366 		 */
367 		(void) snprintf(svcstring, sizeof (svcstring), "%s:%s",
368 		    SA_SVC_FMRI_BASE, instname);
369 		handle = scf_handle_create(SCF_VERSION);
370 		if (handle != NULL) {
371 			if (scf_handle_bind(handle) == 0) {
372 				switch (which) {
373 				case SVC_SET:
374 					prop = scf_simple_prop_get(handle,
375 					    svcstring, "general",
376 					    SVC_AUTH_VALUE);
377 					break;
378 				case SVC_ACTION:
379 					prop = scf_simple_prop_get(handle,
380 					    svcstring, "general",
381 					    SVC_AUTH_ACTION);
382 					break;
383 				}
384 			}
385 		}
386 	}
387 	/* make sure we have an authorization string property */
388 	if (prop != NULL) {
389 		int i;
390 		numauths = scf_simple_prop_numvalues(prop);
391 		for (ret = 0, i = 0; i < numauths; i++) {
392 			authstr = scf_simple_prop_next_astring(prop);
393 			if (authstr != NULL) {
394 				/* check if this user has one of the strings */
395 				if (chkauthattr(authstr, pw->pw_name)) {
396 					ret = 1;
397 					break;
398 				}
399 			}
400 		}
401 		endauthattr();
402 		scf_simple_prop_free(prop);
403 	} else {
404 		/* no authorization string defined */
405 		ret = 0;
406 	}
407 	if (handle != NULL)
408 		scf_handle_destroy(handle);
409 	return (ret);
410 }
411 
412 /*
413  * check_authorizations(instname, flags)
414  *
415  * check all the needed authorizations for the user in this service
416  * instance. Return value of 1(true) or 0(false) indicates whether
417  * there are authorizations for the user or not.
418  */
419 
420 static int
421 check_authorizations(char *instname, int flags)
422 {
423 	int ret1 = 0;
424 	int ret2 = 0;
425 	int ret;
426 
427 	if (flags & SVC_SET)
428 		ret1 = check_authorization(instname, SVC_SET);
429 	if (flags & SVC_ACTION)
430 		ret2 = check_authorization(instname, SVC_ACTION);
431 	switch (flags) {
432 	case SVC_ACTION:
433 		ret = ret2;
434 		break;
435 	case SVC_SET:
436 		ret = ret1;
437 		break;
438 	case SVC_ACTION|SVC_SET:
439 		ret = ret1 & ret2;
440 		break;
441 	default:
442 		/* if not flags set, we assume we don't need authorizations */
443 		ret = 1;
444 	}
445 	return (ret);
446 }
447 
448 /*
449  * notify_or_enable_share(share, protocol)
450  *
451  * Since some protocols don't want an "enable" when properties change,
452  * this function will use the protocol specific notify function
453  * first. If that fails, it will then attempt to use the
454  * sa_enable_share().  "protocol" is the protocol that was specified
455  * on the command line.
456  */
457 static void
458 notify_or_enable_share(sa_share_t share, char *protocol)
459 {
460 	sa_group_t group;
461 	sa_optionset_t opt;
462 	int ret = SA_OK;
463 	char *path;
464 	char *groupproto;
465 	sa_share_t parent = share;
466 
467 	/* If really a resource, get parent share */
468 	if (!sa_is_share(share)) {
469 		parent = sa_get_resource_parent((sa_resource_t)share);
470 	}
471 
472 	/*
473 	 * Now that we've got a share in "parent", make sure it has a path.
474 	 */
475 	path = sa_get_share_attr(parent, "path");
476 	if (path == NULL)
477 		return;
478 
479 	group = sa_get_parent_group(parent);
480 
481 	if (group == NULL) {
482 		sa_free_attr_string(path);
483 		return;
484 	}
485 	for (opt = sa_get_optionset(group, NULL);
486 	    opt != NULL;
487 	    opt = sa_get_next_optionset(opt)) {
488 		groupproto = sa_get_optionset_attr(opt, "type");
489 		if (groupproto == NULL ||
490 		    (protocol != NULL && strcmp(groupproto, protocol) != 0)) {
491 			sa_free_attr_string(groupproto);
492 			continue;
493 		}
494 		if (sa_is_share(share)) {
495 			if ((ret = sa_proto_change_notify(share,
496 			    groupproto)) != SA_OK) {
497 				ret = sa_enable_share(share, groupproto);
498 				if (ret != SA_OK) {
499 					(void) printf(
500 					    gettext("Could not reenable"
501 					    " share %s: %s\n"),
502 					    path, sa_errorstr(ret));
503 				}
504 			}
505 		} else {
506 			/* Must be a resource */
507 			if ((ret = sa_proto_notify_resource(share,
508 			    groupproto)) != SA_OK) {
509 				ret = sa_enable_resource(share, groupproto);
510 				if (ret != SA_OK) {
511 					(void) printf(
512 					    gettext("Could not "
513 					    "reenable resource %s: "
514 					    "%s\n"), path,
515 					    sa_errorstr(ret));
516 				}
517 			}
518 		}
519 		sa_free_attr_string(groupproto);
520 	}
521 	sa_free_attr_string(path);
522 }
523 
524 /*
525  * enable_group(group, updateproto, notify, proto)
526  *
527  * enable all the shares in the specified group. This is a helper for
528  * enable_all_groups in order to simplify regular and subgroup (zfs)
529  * enabling. Group has already been checked for non-NULL. If notify
530  * is non-zero, attempt to use the notify interface rather than
531  * enable.
532  */
533 static void
534 enable_group(sa_group_t group, char *updateproto, int notify, char *proto)
535 {
536 	sa_share_t share;
537 
538 	for (share = sa_get_share(group, NULL);
539 	    share != NULL;
540 	    share = sa_get_next_share(share)) {
541 		if (updateproto != NULL)
542 			(void) sa_update_legacy(share, updateproto);
543 		if (notify)
544 			notify_or_enable_share(share, proto);
545 		else
546 			(void) sa_enable_share(share, proto);
547 	}
548 }
549 
550 /*
551  * isenabled(group)
552  *
553  * Returns B_TRUE if the group is enabled or B_FALSE if it isn't.
554  * Moved to separate function to reduce clutter in the code.
555  */
556 
557 static int
558 isenabled(sa_group_t group)
559 {
560 	char *state;
561 	int ret = B_FALSE;
562 
563 	if (group != NULL) {
564 		state = sa_get_group_attr(group, "state");
565 		if (state != NULL) {
566 
567 			if (strcmp(state, "enabled") == 0)
568 				ret = B_TRUE;
569 			sa_free_attr_string(state);
570 		}
571 	}
572 	return (ret);
573 }
574 
575 /*
576  * enable_all_groups(list, setstate, online, updateproto)
577  *
578  * Given a list of groups, enable each one found.  If updateproto is
579  * not NULL, then update all the shares for the protocol that was
580  * passed in. If enable is non-zero, tell enable_group to try the
581  * notify interface since this is a property change.
582  */
583 static int
584 enable_all_groups(sa_handle_t handle, struct list *work, int setstate,
585     int online, char *updateproto, int enable)
586 {
587 	int ret;
588 	char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
589 	char *state;
590 	char *name;
591 	char *zfs = NULL;
592 	sa_group_t group;
593 	sa_group_t subgroup;
594 
595 	for (ret = SA_OK; work != NULL;	work = work->next) {
596 		group = (sa_group_t)work->item;
597 
598 		/*
599 		 * If setstate == TRUE, then make sure to set
600 		 * enabled. This needs to be done here in order for
601 		 * the isenabled check to succeed on a newly enabled
602 		 * group.
603 		 */
604 		if (setstate == B_TRUE) {
605 			ret = sa_set_group_attr(group, "state",	"enabled");
606 			if (ret != SA_OK)
607 				break;
608 		}
609 
610 		/*
611 		 * Check to see if group is enabled. If it isn't, skip
612 		 * the rest.  We don't want shares starting if the
613 		 * group is disabled. The properties may have been
614 		 * updated, but there won't be a change until the
615 		 * group is enabled.
616 		 */
617 		if (!isenabled(group))
618 			continue;
619 
620 		/* if itemdata != NULL then a single share */
621 		if (work->itemdata != NULL) {
622 			if (enable) {
623 				if (work->itemdata != NULL)
624 					notify_or_enable_share(work->itemdata,
625 					    updateproto);
626 				else
627 					ret = SA_CONFIG_ERR;
628 			} else {
629 				if (sa_is_share(work->itemdata)) {
630 					ret = sa_enable_share(
631 					    (sa_share_t)work->itemdata,
632 					    updateproto);
633 				} else {
634 					ret = sa_enable_resource(
635 					    (sa_resource_t)work->itemdata,
636 					    updateproto);
637 				}
638 			}
639 		}
640 		if (ret != SA_OK)
641 			break;
642 
643 		/* if itemdata == NULL then the whole group */
644 		if (work->itemdata == NULL) {
645 			zfs = sa_get_group_attr(group, "zfs");
646 			/*
647 			 * If the share is managed by ZFS, don't
648 			 * update any of the protocols since ZFS is
649 			 * handling this.  Updateproto will contain
650 			 * the name of the protocol that we want to
651 			 * update legacy files for.
652 			 */
653 			enable_group(group, zfs == NULL ? updateproto : NULL,
654 			    enable, work->proto);
655 			for (subgroup = sa_get_sub_group(group);
656 			    subgroup != NULL;
657 			    subgroup = sa_get_next_group(subgroup)) {
658 				/* never update legacy for ZFS subgroups */
659 				enable_group(subgroup, NULL, enable,
660 				    work->proto);
661 			}
662 		}
663 		if (online) {
664 			zfs = sa_get_group_attr(group, "zfs");
665 			name = sa_get_group_attr(group, "name");
666 			if (name != NULL) {
667 				if (zfs == NULL) {
668 					(void) snprintf(instance,
669 					    sizeof (instance), "%s:%s",
670 					    SA_SVC_FMRI_BASE, name);
671 					state = smf_get_state(instance);
672 					if (state == NULL ||
673 					    strcmp(state, "online") != 0) {
674 						(void) smf_enable_instance(
675 						    instance, 0);
676 						free(state);
677 					}
678 				} else {
679 					sa_free_attr_string(zfs);
680 					zfs = NULL;
681 				}
682 				if (name != NULL)
683 					sa_free_attr_string(name);
684 			}
685 		}
686 	}
687 	if (ret == SA_OK) {
688 		ret = sa_update_config(handle);
689 	}
690 	return (ret);
691 }
692 
693 /*
694  * chk_opt(optlistp, security, proto)
695  *
696  * Do a sanity check on the optlist provided for the protocol.  This
697  * is a syntax check and verification that the property is either a
698  * general or specific to a names optionset.
699  */
700 
701 static int
702 chk_opt(struct options *optlistp, int security, char *proto)
703 {
704 	struct options *optlist;
705 	char *sep = "";
706 	int notfirst = 0;
707 	int ret;
708 
709 	for (optlist = optlistp; optlist != NULL; optlist = optlist->next) {
710 		char *optname;
711 
712 		optname = optlist->optname;
713 		ret = OPT_ADD_OK;
714 		/* extract property/value pair */
715 		if (sa_is_security(optname, proto)) {
716 			if (!security)
717 				ret = OPT_ADD_SECURITY;
718 		} else {
719 			if (security)
720 				ret = OPT_ADD_PROPERTY;
721 		}
722 		if (ret != OPT_ADD_OK) {
723 			if (notfirst == 0)
724 				(void) printf(
725 				    gettext("Property syntax error: "));
726 			switch (ret) {
727 			case OPT_ADD_SYNTAX:
728 				(void) printf(gettext("%ssyntax error: %s"),
729 				    sep, optname);
730 				sep = ", ";
731 				break;
732 			case OPT_ADD_SECURITY:
733 				(void) printf(gettext("%s%s requires -S"),
734 				    optname, sep);
735 				sep = ", ";
736 				break;
737 			case OPT_ADD_PROPERTY:
738 				(void) printf(
739 				    gettext("%s%s not supported with -S"),
740 				    optname, sep);
741 				sep = ", ";
742 				break;
743 			}
744 			notfirst++;
745 		}
746 	}
747 	if (notfirst) {
748 		(void) printf("\n");
749 		ret = SA_SYNTAX_ERR;
750 	}
751 	return (ret);
752 }
753 
754 /*
755  * free_opt(optlist)
756  *	Free the specified option list.
757  */
758 static void
759 free_opt(struct options *optlist)
760 {
761 	struct options *nextopt;
762 	while (optlist != NULL) {
763 		nextopt = optlist->next;
764 		free(optlist);
765 		optlist = nextopt;
766 	}
767 }
768 
769 /*
770  * check property list for valid properties
771  * A null value is a remove which is always valid.
772  */
773 static int
774 valid_options(struct options *optlist, char *proto, void *object, char *sec)
775 {
776 	int ret = SA_OK;
777 	struct options *cur;
778 	sa_property_t prop;
779 	sa_optionset_t parent = NULL;
780 
781 	if (object != NULL) {
782 		if (sec == NULL)
783 			parent = sa_get_optionset(object, proto);
784 		else
785 			parent = sa_get_security(object, sec, proto);
786 	}
787 
788 	for (cur = optlist; cur != NULL; cur = cur->next) {
789 		if (cur->optvalue == NULL)
790 			continue;
791 		prop = sa_create_property(cur->optname, cur->optvalue);
792 		if (prop == NULL)
793 			ret = SA_NO_MEMORY;
794 		if (ret != SA_OK ||
795 		    (ret = sa_valid_property(parent, proto, prop)) != SA_OK) {
796 			(void) printf(
797 			    gettext("Could not add property %s: %s\n"),
798 			    cur->optname, sa_errorstr(ret));
799 		}
800 		(void) sa_remove_property(prop);
801 	}
802 	return (ret);
803 }
804 
805 /*
806  * add_optionset(group, optlist, protocol, *err)
807  *	Add the options in optlist to an optionset and then add the optionset
808  *	to the group.
809  *
810  *	The return value indicates if there was a "change" while errors are
811  *	returned via the *err parameters.
812  */
813 static int
814 add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err)
815 {
816 	sa_optionset_t optionset;
817 	int ret = SA_OK;
818 	int result = B_FALSE;
819 
820 	optionset = sa_get_optionset(group, proto);
821 	if (optionset == NULL) {
822 		optionset = sa_create_optionset(group, proto);
823 		if (optionset == NULL)
824 			ret = SA_NO_MEMORY;
825 		result = B_TRUE; /* adding a protocol is a change */
826 	}
827 	if (optionset == NULL) {
828 		ret = SA_NO_MEMORY;
829 		goto out;
830 	}
831 	while (optlist != NULL) {
832 		sa_property_t prop;
833 		prop = sa_get_property(optionset, optlist->optname);
834 		if (prop == NULL) {
835 			/*
836 			 * add the property, but only if it is
837 			 * a non-NULL or non-zero length value
838 			 */
839 			if (optlist->optvalue != NULL) {
840 				prop = sa_create_property(optlist->optname,
841 				    optlist->optvalue);
842 				if (prop != NULL) {
843 					ret = sa_valid_property(optionset,
844 					    proto, prop);
845 					if (ret != SA_OK) {
846 						(void) sa_remove_property(prop);
847 						(void) printf(gettext("Could "
848 						    "not add property "
849 						    "%s: %s\n"),
850 						    optlist->optname,
851 						    sa_errorstr(ret));
852 					}
853 				}
854 				if (ret == SA_OK) {
855 					ret = sa_add_property(optionset, prop);
856 					if (ret != SA_OK) {
857 						(void) printf(gettext(
858 						    "Could not add property "
859 						    "%s: %s\n"),
860 						    optlist->optname,
861 						    sa_errorstr(ret));
862 					} else {
863 						/* there was a change */
864 						result = B_TRUE;
865 					}
866 				}
867 			}
868 		} else {
869 			ret = sa_update_property(prop, optlist->optvalue);
870 			/* should check to see if value changed */
871 			if (ret != SA_OK) {
872 				(void) printf(gettext("Could not update "
873 				    "property %s: %s\n"), optlist->optname,
874 				    sa_errorstr(ret));
875 			} else {
876 				result = B_TRUE;
877 			}
878 		}
879 		optlist = optlist->next;
880 	}
881 	ret = sa_commit_properties(optionset, 0);
882 
883 out:
884 	if (err != NULL)
885 		*err = ret;
886 	return (result);
887 }
888 
889 /*
890  * resource_compliant(group)
891  *
892  * Go through all the shares in the group. Assume compliant, but if
893  * any share doesn't have at least one resource name, it isn't
894  * compliant.
895  */
896 static int
897 resource_compliant(sa_group_t group)
898 {
899 	sa_share_t share;
900 
901 	for (share = sa_get_share(group, NULL); share != NULL;
902 	    share = sa_get_next_share(share)) {
903 		if (sa_get_share_resource(share, NULL) == NULL) {
904 			return (B_FALSE);
905 		}
906 	}
907 	return (B_TRUE);
908 }
909 
910 /*
911  * fix_path(path)
912  *
913  * change all illegal characters to something else.  For now, all get
914  * converted to '_' and the leading '/' is stripped off. This is used
915  * to construct an resource name (SMB share name) that is valid.
916  * Caller must pass a valid path.
917  */
918 static void
919 fix_path(char *path)
920 {
921 	char *cp;
922 	size_t len;
923 
924 	assert(path != NULL);
925 
926 	/* make sure we are appropriate length */
927 	cp = path + 1; /* skip leading slash */
928 	while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) {
929 		cp = strchr(cp, '/');
930 		if (cp != NULL)
931 			cp++;
932 	}
933 	/* two cases - cp == NULL and cp is substring of path */
934 	if (cp == NULL) {
935 		/* just take last SA_MAX_RESOURCE_NAME chars */
936 		len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME;
937 		(void) memmove(path, path + len, SA_MAX_RESOURCE_NAME);
938 		path[SA_MAX_RESOURCE_NAME] = '\0';
939 	} else {
940 		len = strlen(cp) + 1;
941 		(void) memmove(path, cp, len);
942 	}
943 
944 	/*
945 	 * Don't want any of the characters that are not allowed
946 	 * in and SMB share name. Replace them with '_'.
947 	 */
948 	while (*path) {
949 		switch (*path) {
950 		case '/':
951 		case '"':
952 		case '\\':
953 		case '[':
954 		case ']':
955 		case ':':
956 		case '|':
957 		case '<':
958 		case '>':
959 		case '+':
960 		case ';':
961 		case ',':
962 		case '?':
963 		case '*':
964 		case '=':
965 		case '\t':
966 			*path = '_';
967 			break;
968 		}
969 		path++;
970 	}
971 }
972 
973 /*
974  * name_adjust(path, count)
975  *
976  * Add a ~<count> in place of last few characters. The total number of
977  * characters is dependent on count.
978  */
979 #define	MAX_MANGLE_NUMBER	10000
980 
981 static int
982 name_adjust(char *path, int count)
983 {
984 	size_t len;
985 
986 	len = strlen(path) - 2;
987 	if (count > 10)
988 		len--;
989 	if (count > 100)
990 		len--;
991 	if (count > 1000)
992 		len--;
993 	if (len > 0)
994 		(void) sprintf(path + len, "~%d", count);
995 	else
996 		return (SA_BAD_VALUE);
997 
998 	return (SA_OK);
999 }
1000 
1001 /*
1002  * make_resources(group)
1003  *
1004  * Go through all the shares in the group and make them have resource
1005  * names.
1006  */
1007 static void
1008 make_resources(sa_group_t group)
1009 {
1010 	sa_share_t share;
1011 	int count;
1012 	int err = SA_OK;
1013 
1014 	for (share = sa_get_share(group, NULL); share != NULL;
1015 	    share = sa_get_next_share(share)) {
1016 		/* Skip those with resources */
1017 		if (sa_get_share_resource(share, NULL) == NULL) {
1018 			char *path;
1019 			path = sa_get_share_attr(share, "path");
1020 			if (path == NULL)
1021 				continue;
1022 			fix_path(path);
1023 			count = 0;	/* reset for next resource */
1024 			while (sa_add_resource(share, path,
1025 			    SA_SHARE_PERMANENT, &err) == NULL &&
1026 			    err == SA_DUPLICATE_NAME) {
1027 				int ret;
1028 				ret = name_adjust(path, count);
1029 				count++;
1030 				if (ret != SA_OK ||
1031 				    count >= MAX_MANGLE_NUMBER) {
1032 					(void) printf(gettext(
1033 					    "Cannot create resource name for"
1034 					    " path: %s\n"), path);
1035 					break;
1036 				}
1037 			}
1038 			sa_free_attr_string(path);
1039 		}
1040 	}
1041 }
1042 
1043 /*
1044  * sa_create(flags, argc, argv)
1045  *	create a new group
1046  *	this may or may not have a protocol associated with it.
1047  *	No protocol means "all" protocols in this case.
1048  */
1049 static int
1050 sa_create(sa_handle_t handle, int flags, int argc, char *argv[])
1051 {
1052 	char *groupname;
1053 
1054 	sa_group_t group;
1055 	int force = 0;
1056 	int verbose = 0;
1057 	int dryrun = 0;
1058 	int c;
1059 	char *protocol = NULL;
1060 	int ret = SA_OK;
1061 	struct options *optlist = NULL;
1062 	int err = 0;
1063 	int auth;
1064 
1065 	while ((c = getopt(argc, argv, "?fhvnP:p:")) != EOF) {
1066 		switch (c) {
1067 		case 'f':
1068 			force++;
1069 			break;
1070 		case 'v':
1071 			verbose++;
1072 			break;
1073 		case 'n':
1074 			dryrun++;
1075 			break;
1076 		case 'P':
1077 			if (protocol != NULL) {
1078 				(void) printf(gettext("Specifying "
1079 				    "multiple protocols "
1080 				    "not supported: %s\n"), protocol);
1081 				return (SA_SYNTAX_ERR);
1082 			}
1083 			protocol = optarg;
1084 			if (sa_valid_protocol(protocol))
1085 				break;
1086 			(void) printf(gettext(
1087 			    "Invalid protocol specified: %s\n"), protocol);
1088 			return (SA_INVALID_PROTOCOL);
1089 			break;
1090 		case 'p':
1091 			ret = add_opt(&optlist, optarg, 0);
1092 			switch (ret) {
1093 			case OPT_ADD_SYNTAX:
1094 				(void) printf(gettext(
1095 				    "Property syntax error for property: %s\n"),
1096 				    optarg);
1097 				return (SA_SYNTAX_ERR);
1098 			case OPT_ADD_SECURITY:
1099 				(void) printf(gettext(
1100 				    "Security properties need "
1101 				    "to be set with set-security: %s\n"),
1102 				    optarg);
1103 				return (SA_SYNTAX_ERR);
1104 			default:
1105 				break;
1106 			}
1107 			break;
1108 		default:
1109 		case 'h':
1110 		case '?':
1111 			(void) printf(gettext("usage: %s\n"),
1112 			    sa_get_usage(USAGE_CREATE));
1113 			return (0);
1114 		}
1115 	}
1116 
1117 	if (optind >= argc) {
1118 		(void) printf(gettext("usage: %s\n"),
1119 		    sa_get_usage(USAGE_CREATE));
1120 		(void) printf(gettext("\tgroup must be specified.\n"));
1121 		return (SA_BAD_PATH);
1122 	}
1123 
1124 	if ((optind + 1) < argc) {
1125 		(void) printf(gettext("usage: %s\n"),
1126 		    sa_get_usage(USAGE_CREATE));
1127 		(void) printf(gettext("\textraneous group(s) at end\n"));
1128 		return (SA_SYNTAX_ERR);
1129 	}
1130 
1131 	if (protocol == NULL && optlist != NULL) {
1132 		/* lookup default protocol */
1133 		(void) printf(gettext("usage: %s\n"),
1134 		    sa_get_usage(USAGE_CREATE));
1135 		(void) printf(gettext("\tprotocol must be specified "
1136 		    "with properties\n"));
1137 		return (SA_INVALID_PROTOCOL);
1138 	}
1139 
1140 	if (optlist != NULL)
1141 		ret = chk_opt(optlist, 0, protocol);
1142 	if (ret == OPT_ADD_SECURITY) {
1143 		(void) printf(gettext("Security properties not "
1144 		    "supported with create\n"));
1145 		return (SA_SYNTAX_ERR);
1146 	}
1147 
1148 	/*
1149 	 * If a group already exists, we can only add a new protocol
1150 	 * to it and not create a new one or add the same protocol
1151 	 * again.
1152 	 */
1153 
1154 	groupname = argv[optind];
1155 
1156 	auth = check_authorizations(groupname, flags);
1157 
1158 	group = sa_get_group(handle, groupname);
1159 	if (group != NULL) {
1160 		/* group exists so must be a protocol add */
1161 		if (protocol != NULL) {
1162 			if (has_protocol(group, protocol)) {
1163 				(void) printf(gettext(
1164 				    "Group \"%s\" already exists"
1165 				    " with protocol %s\n"), groupname,
1166 				    protocol);
1167 				ret = SA_DUPLICATE_NAME;
1168 			}
1169 		} else {
1170 			/* must add new protocol */
1171 			(void) printf(gettext(
1172 			    "Group already exists and no protocol "
1173 			    "specified.\n"));
1174 			ret = SA_DUPLICATE_NAME;
1175 		}
1176 	} else {
1177 		/*
1178 		 * is it a valid name? Must comply with SMF instance
1179 		 * name restrictions.
1180 		 */
1181 		if (!sa_valid_group_name(groupname)) {
1182 			ret = SA_INVALID_NAME;
1183 			(void) printf(gettext("Invalid group name: %s\n"),
1184 			    groupname);
1185 		}
1186 	}
1187 	if (ret == SA_OK) {
1188 		/* check protocol vs optlist */
1189 		if (optlist != NULL) {
1190 			/* check options, if any, for validity */
1191 			ret = valid_options(optlist, protocol, group, NULL);
1192 		}
1193 	}
1194 	if (ret == SA_OK && !dryrun) {
1195 		if (group == NULL) {
1196 			group = sa_create_group(handle, (char *)groupname,
1197 			    &err);
1198 		}
1199 		if (group != NULL) {
1200 			sa_optionset_t optionset;
1201 			/*
1202 			 * First check to see if the new protocol is one that
1203 			 * requires resource names and make sure we are
1204 			 * compliant before proceeding.
1205 			 */
1206 			if (protocol != NULL) {
1207 				uint64_t features;
1208 
1209 				features = sa_proto_get_featureset(protocol);
1210 				if ((features & SA_FEATURE_RESOURCE) &&
1211 				    !resource_compliant(group)) {
1212 					if (force) {
1213 						make_resources(group);
1214 					} else {
1215 						ret = SA_RESOURCE_REQUIRED;
1216 						(void) printf(
1217 						    gettext("Protocol "
1218 						    "requires resource "
1219 						    "names to be "
1220 						    "set: %s\n"),
1221 						    protocol);
1222 						goto err;
1223 					}
1224 				}
1225 			}
1226 			if (optlist != NULL) {
1227 				(void) add_optionset(group, optlist, protocol,
1228 				    &ret);
1229 			} else if (protocol != NULL) {
1230 				optionset = sa_create_optionset(group,
1231 				    protocol);
1232 				if (optionset == NULL)
1233 					ret = SA_NO_MEMORY;
1234 			} else if (protocol == NULL) {
1235 				char **protolist;
1236 				int numprotos, i;
1237 				numprotos = sa_get_protocols(&protolist);
1238 				for (i = 0; i < numprotos; i++) {
1239 					optionset = sa_create_optionset(group,
1240 					    protolist[i]);
1241 				}
1242 				if (protolist != NULL)
1243 					free(protolist);
1244 			}
1245 			/*
1246 			 * We have a group and legal additions
1247 			 */
1248 			if (ret == SA_OK) {
1249 				/*
1250 				 * Commit to configuration for protocols that
1251 				 * need to do block updates. For NFS, this
1252 				 * doesn't do anything but it will be run for
1253 				 * all protocols that implement the
1254 				 * appropriate plugin.
1255 				 */
1256 				ret = sa_update_config(handle);
1257 			} else {
1258 				if (group != NULL)
1259 					(void) sa_remove_group(group);
1260 			}
1261 		} else {
1262 			ret = err;
1263 			(void) printf(gettext("Could not create group: %s\n"),
1264 			    sa_errorstr(ret));
1265 		}
1266 	}
1267 	if (dryrun && ret == SA_OK && !auth && verbose) {
1268 		(void) printf(gettext("Command would fail: %s\n"),
1269 		    sa_errorstr(SA_NO_PERMISSION));
1270 		ret = SA_NO_PERMISSION;
1271 	}
1272 err:
1273 	free_opt(optlist);
1274 	return (ret);
1275 }
1276 
1277 /*
1278  * group_status(group)
1279  *
1280  * return the current status (enabled/disabled) of the group.
1281  */
1282 
1283 static char *
1284 group_status(sa_group_t group)
1285 {
1286 	char *state;
1287 	int enabled = 0;
1288 
1289 	state = sa_get_group_attr(group, "state");
1290 	if (state != NULL) {
1291 		if (strcmp(state, "enabled") == 0) {
1292 			enabled = 1;
1293 		}
1294 		sa_free_attr_string(state);
1295 	}
1296 	return (enabled ? "enabled" : "disabled");
1297 }
1298 
1299 /*
1300  * sa_delete(flags, argc, argv)
1301  *
1302  *	Delete a group.
1303  */
1304 
1305 static int
1306 sa_delete(sa_handle_t handle, int flags, int argc, char *argv[])
1307 {
1308 	char *groupname;
1309 	sa_group_t group;
1310 	sa_share_t share;
1311 	int verbose = 0;
1312 	int dryrun = 0;
1313 	int force = 0;
1314 	int c;
1315 	char *protocol = NULL;
1316 	char *sectype = NULL;
1317 	int ret = SA_OK;
1318 	int auth;
1319 
1320 	while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) {
1321 		switch (c) {
1322 		case 'v':
1323 			verbose++;
1324 			break;
1325 		case 'n':
1326 			dryrun++;
1327 			break;
1328 		case 'P':
1329 			if (protocol != NULL) {
1330 				(void) printf(gettext("Specifying "
1331 				    "multiple protocols "
1332 				    "not supported: %s\n"), protocol);
1333 				return (SA_SYNTAX_ERR);
1334 			}
1335 			protocol = optarg;
1336 			if (!sa_valid_protocol(protocol)) {
1337 				(void) printf(gettext("Invalid protocol "
1338 				    "specified: %s\n"), protocol);
1339 				return (SA_INVALID_PROTOCOL);
1340 			}
1341 			break;
1342 		case 'S':
1343 			if (sectype != NULL) {
1344 				(void) printf(gettext("Specifying "
1345 				    "multiple property "
1346 				    "spaces not supported: %s\n"), sectype);
1347 				return (SA_SYNTAX_ERR);
1348 			}
1349 			sectype = optarg;
1350 			break;
1351 		case 'f':
1352 			force++;
1353 			break;
1354 		default:
1355 		case 'h':
1356 		case '?':
1357 			(void) printf(gettext("usage: %s\n"),
1358 			    sa_get_usage(USAGE_DELETE));
1359 			return (0);
1360 		}
1361 	}
1362 
1363 	if (optind >= argc) {
1364 		(void) printf(gettext("usage: %s\n"),
1365 		    sa_get_usage(USAGE_DELETE));
1366 		(void) printf(gettext("\tgroup must be specified.\n"));
1367 		return (SA_SYNTAX_ERR);
1368 	}
1369 
1370 	if ((optind + 1) < argc) {
1371 		(void) printf(gettext("usage: %s\n"),
1372 		    sa_get_usage(USAGE_DELETE));
1373 		(void) printf(gettext("\textraneous group(s) at end\n"));
1374 		return (SA_SYNTAX_ERR);
1375 	}
1376 
1377 	if (sectype != NULL && protocol == NULL) {
1378 		(void) printf(gettext("usage: %s\n"),
1379 		    sa_get_usage(USAGE_DELETE));
1380 		(void) printf(gettext("\tsecurity requires protocol to be "
1381 		    "specified.\n"));
1382 		return (SA_SYNTAX_ERR);
1383 	}
1384 
1385 	/*
1386 	 * Determine if the group already exists since it must in
1387 	 * order to be removed.
1388 	 *
1389 	 * We can delete when:
1390 	 *
1391 	 *	- group is empty
1392 	 *	- force flag is set
1393 	 *	- if protocol specified, only delete the protocol
1394 	 */
1395 
1396 	groupname = argv[optind];
1397 	group = sa_get_group(handle, groupname);
1398 	if (group == NULL) {
1399 		ret = SA_NO_SUCH_GROUP;
1400 		goto done;
1401 	}
1402 	auth = check_authorizations(groupname, flags);
1403 	if (protocol == NULL) {
1404 		share = sa_get_share(group, NULL);
1405 		if (share != NULL)
1406 			ret = SA_BUSY;
1407 		if (share == NULL || (share != NULL && force == 1)) {
1408 			ret = SA_OK;
1409 			if (!dryrun) {
1410 				while (share != NULL) {
1411 					sa_share_t next_share;
1412 					next_share = sa_get_next_share(share);
1413 					/*
1414 					 * need to do the disable of
1415 					 * each share, but don't
1416 					 * actually do anything on a
1417 					 * dryrun.
1418 					 */
1419 					ret = sa_disable_share(share, NULL);
1420 					ret = sa_remove_share(share);
1421 					share = next_share;
1422 				}
1423 				ret = sa_remove_group(group);
1424 			}
1425 		}
1426 		/* Commit to configuration if not a dryrun */
1427 		if (!dryrun && ret == SA_OK) {
1428 			ret = sa_update_config(handle);
1429 		}
1430 	} else {
1431 		/* a protocol delete */
1432 		sa_optionset_t optionset;
1433 		sa_security_t security;
1434 		if (sectype != NULL) {
1435 			/* only delete specified security */
1436 			security = sa_get_security(group, sectype, protocol);
1437 			if (security != NULL && !dryrun)
1438 				ret = sa_destroy_security(security);
1439 			else
1440 				ret = SA_INVALID_PROTOCOL;
1441 		} else {
1442 			optionset = sa_get_optionset(group, protocol);
1443 			if (optionset != NULL && !dryrun) {
1444 				/*
1445 				 * have an optionset with
1446 				 * protocol to delete
1447 				 */
1448 				ret = sa_destroy_optionset(optionset);
1449 				/*
1450 				 * Now find all security sets
1451 				 * for the protocol and remove
1452 				 * them. Don't remove other
1453 				 * protocols.
1454 				 */
1455 				for (security =
1456 				    sa_get_security(group, NULL, NULL);
1457 				    ret == SA_OK && security != NULL;
1458 				    security = sa_get_next_security(security)) {
1459 					char *secprot;
1460 					secprot = sa_get_security_attr(security,
1461 					    "type");
1462 					if (secprot != NULL &&
1463 					    strcmp(secprot, protocol) == 0)
1464 						ret = sa_destroy_security(
1465 						    security);
1466 					if (secprot != NULL)
1467 						sa_free_attr_string(secprot);
1468 				}
1469 			} else {
1470 				if (!dryrun)
1471 					ret = SA_INVALID_PROTOCOL;
1472 			}
1473 		}
1474 		/*
1475 		 * With the protocol items removed, make sure that all
1476 		 * the shares are updated in the legacy files, if
1477 		 * necessary.
1478 		 */
1479 		for (share = sa_get_share(group, NULL);
1480 		    share != NULL;
1481 		    share = sa_get_next_share(share)) {
1482 			(void) sa_delete_legacy(share, protocol);
1483 		}
1484 	}
1485 
1486 done:
1487 	if (ret != SA_OK) {
1488 		(void) printf(gettext("Could not delete group: %s\n"),
1489 		    sa_errorstr(ret));
1490 	} else if (dryrun && !auth && verbose) {
1491 		(void) printf(gettext("Command would fail: %s\n"),
1492 		    sa_errorstr(SA_NO_PERMISSION));
1493 	}
1494 	return (ret);
1495 }
1496 
1497 /*
1498  * strndupr(*buff, str, buffsize)
1499  *
1500  * used with small strings to duplicate and possibly increase the
1501  * buffer size of a string.
1502  */
1503 static char *
1504 strndupr(char *buff, char *str, int *buffsize)
1505 {
1506 	int limit;
1507 	char *orig_buff = buff;
1508 
1509 	if (buff == NULL) {
1510 		buff = (char *)malloc(64);
1511 		if (buff == NULL)
1512 			return (NULL);
1513 		*buffsize = 64;
1514 		buff[0] = '\0';
1515 	}
1516 	limit = strlen(buff) + strlen(str) + 1;
1517 	if (limit > *buffsize) {
1518 		limit = *buffsize = *buffsize + ((limit / 64) + 64);
1519 		buff = realloc(buff, limit);
1520 	}
1521 	if (buff != NULL) {
1522 		(void) strcat(buff, str);
1523 	} else {
1524 		/* if it fails, fail it hard */
1525 		if (orig_buff != NULL)
1526 			free(orig_buff);
1527 	}
1528 	return (buff);
1529 }
1530 
1531 /*
1532  * group_proto(group)
1533  *
1534  * return a string of all the protocols (space separated) associated
1535  * with this group.
1536  */
1537 
1538 static char *
1539 group_proto(sa_group_t group)
1540 {
1541 	sa_optionset_t optionset;
1542 	char *proto;
1543 	char *buff = NULL;
1544 	int buffsize = 0;
1545 	int addspace = 0;
1546 	/*
1547 	 * get the protocol list by finding the optionsets on this
1548 	 * group and extracting the type value. The initial call to
1549 	 * strndupr() initailizes buff.
1550 	 */
1551 	buff = strndupr(buff, "", &buffsize);
1552 	if (buff != NULL) {
1553 		for (optionset = sa_get_optionset(group, NULL);
1554 		    optionset != NULL && buff != NULL;
1555 		    optionset = sa_get_next_optionset(optionset)) {
1556 			/*
1557 			 * extract out the protocol type from this optionset
1558 			 * and append it to the buffer "buff". strndupr() will
1559 			 * reallocate space as necessay.
1560 			 */
1561 			proto = sa_get_optionset_attr(optionset, "type");
1562 			if (proto != NULL) {
1563 				if (addspace++)
1564 					buff = strndupr(buff, " ", &buffsize);
1565 				buff = strndupr(buff, proto, &buffsize);
1566 				sa_free_attr_string(proto);
1567 			}
1568 		}
1569 	}
1570 	return (buff);
1571 }
1572 
1573 /*
1574  * sa_list(flags, argc, argv)
1575  *
1576  * implements the "list" subcommand to list groups and optionally
1577  * their state and protocols.
1578  */
1579 
1580 static int
1581 sa_list(sa_handle_t handle, int flags, int argc, char *argv[])
1582 {
1583 	sa_group_t group;
1584 	int verbose = 0;
1585 	int c;
1586 	char *protocol = NULL;
1587 #ifdef lint
1588 	flags = flags;
1589 #endif
1590 
1591 	while ((c = getopt(argc, argv, "?hvP:")) != EOF) {
1592 		switch (c) {
1593 		case 'v':
1594 			verbose++;
1595 			break;
1596 		case 'P':
1597 			if (protocol != NULL) {
1598 				(void) printf(gettext(
1599 				    "Specifying multiple protocols "
1600 				    "not supported: %s\n"),
1601 				    protocol);
1602 				return (SA_SYNTAX_ERR);
1603 			}
1604 			protocol = optarg;
1605 			if (!sa_valid_protocol(protocol)) {
1606 				(void) printf(gettext(
1607 				    "Invalid protocol specified: %s\n"),
1608 				    protocol);
1609 				return (SA_INVALID_PROTOCOL);
1610 			}
1611 			break;
1612 		default:
1613 		case 'h':
1614 		case '?':
1615 			(void) printf(gettext("usage: %s\n"),
1616 			    sa_get_usage(USAGE_LIST));
1617 			return (0);
1618 		}
1619 	}
1620 
1621 	if (optind != argc) {
1622 		(void) printf(gettext("usage: %s\n"),
1623 		    sa_get_usage(USAGE_LIST));
1624 		return (SA_SYNTAX_ERR);
1625 	}
1626 
1627 	for (group = sa_get_group(handle, NULL);
1628 	    group != NULL;
1629 	    group = sa_get_next_group(group)) {
1630 		char *name;
1631 		char *proto;
1632 		if (protocol == NULL || has_protocol(group, protocol)) {
1633 			name = sa_get_group_attr(group, "name");
1634 			if (name != NULL && (verbose > 1 || name[0] != '#')) {
1635 				(void) printf("%s", (char *)name);
1636 				if (verbose) {
1637 					/*
1638 					 * Need the list of protocols
1639 					 * and current status once
1640 					 * available. We do want to
1641 					 * translate the
1642 					 * enabled/disabled text here.
1643 					 */
1644 					(void) printf("\t%s", isenabled(group) ?
1645 					    gettext("enabled") :
1646 					    gettext("disabled"));
1647 					proto = group_proto(group);
1648 					if (proto != NULL) {
1649 						(void) printf("\t%s",
1650 						    (char *)proto);
1651 						free(proto);
1652 					}
1653 				}
1654 				(void) printf("\n");
1655 			}
1656 			if (name != NULL)
1657 				sa_free_attr_string(name);
1658 		}
1659 	}
1660 	return (0);
1661 }
1662 
1663 /*
1664  * out_properties(optionset, proto, sec)
1665  *
1666  * Format the properties and encode the protocol and optional named
1667  * optionset into the string.
1668  *
1669  * format is protocol[:name]=(property-list)
1670  */
1671 
1672 static void
1673 out_properties(sa_optionset_t optionset, char *proto, char *sec)
1674 {
1675 	char *type;
1676 	char *value;
1677 	int spacer;
1678 	sa_property_t prop;
1679 
1680 	if (sec == NULL)
1681 		(void) printf(" %s=(", proto ? proto : gettext("all"));
1682 	else
1683 		(void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec);
1684 
1685 	for (spacer = 0, prop = sa_get_property(optionset, NULL);
1686 	    prop != NULL;
1687 	    prop = sa_get_next_property(prop)) {
1688 
1689 		/*
1690 		 * extract the property name/value and output with
1691 		 * appropriate spacing. I.e. no prefixed space the
1692 		 * first time through but a space on subsequent
1693 		 * properties.
1694 		 */
1695 		type = sa_get_property_attr(prop, "type");
1696 		value = sa_get_property_attr(prop, "value");
1697 		if (type != NULL) {
1698 			(void) printf("%s%s=", spacer ? " " : "",	type);
1699 			spacer = 1;
1700 			if (value != NULL)
1701 				(void) printf("\"%s\"", value);
1702 			else
1703 				(void) printf("\"\"");
1704 		}
1705 		if (type != NULL)
1706 			sa_free_attr_string(type);
1707 		if (value != NULL)
1708 			sa_free_attr_string(value);
1709 	}
1710 	(void) printf(")");
1711 }
1712 
1713 /*
1714  * show_properties(group, protocol, prefix)
1715  *
1716  * print the properties for a group. If protocol is NULL, do all
1717  * protocols otherwise only the specified protocol. All security
1718  * (named groups specific to the protocol) are included.
1719  *
1720  * The "prefix" is always applied. The caller knows whether it wants
1721  * some type of prefix string (white space) or not.  Once the prefix
1722  * has been output, it is reduced to the zero length string for the
1723  * remainder of the property output.
1724  */
1725 
1726 static void
1727 show_properties(sa_group_t group, char *protocol, char *prefix)
1728 {
1729 	sa_optionset_t optionset;
1730 	sa_security_t security;
1731 	char *value;
1732 	char *secvalue;
1733 
1734 	if (protocol != NULL) {
1735 		optionset = sa_get_optionset(group, protocol);
1736 		if (optionset != NULL) {
1737 			(void) printf("%s", prefix);
1738 			prefix = "";
1739 			out_properties(optionset, protocol, NULL);
1740 		}
1741 		security = sa_get_security(group, protocol, NULL);
1742 		if (security != NULL) {
1743 			(void) printf("%s", prefix);
1744 			prefix = "";
1745 			out_properties(security, protocol, NULL);
1746 		}
1747 	} else {
1748 		for (optionset = sa_get_optionset(group, protocol);
1749 		    optionset != NULL;
1750 		    optionset = sa_get_next_optionset(optionset)) {
1751 
1752 			value = sa_get_optionset_attr(optionset, "type");
1753 			(void) printf("%s", prefix);
1754 			prefix = "";
1755 			out_properties(optionset, value, 0);
1756 			if (value != NULL)
1757 				sa_free_attr_string(value);
1758 		}
1759 		for (security = sa_get_security(group, NULL, protocol);
1760 		    security != NULL;
1761 		    security = sa_get_next_security(security)) {
1762 
1763 			value = sa_get_security_attr(security, "type");
1764 			secvalue = sa_get_security_attr(security, "sectype");
1765 			(void) printf("%s", prefix);
1766 			prefix = "";
1767 			out_properties(security, value, secvalue);
1768 			if (value != NULL)
1769 				sa_free_attr_string(value);
1770 			if (secvalue != NULL)
1771 				sa_free_attr_string(secvalue);
1772 		}
1773 	}
1774 }
1775 
1776 /*
1777  * get_resource(share)
1778  *
1779  * Get the first resource name, if any, and fix string to be in
1780  * current locale and have quotes if it has embedded spaces.  Return
1781  * an attr string that must be freed.
1782  */
1783 
1784 static char *
1785 get_resource(sa_share_t share)
1786 {
1787 	sa_resource_t resource;
1788 	char *resstring = NULL;
1789 	char *retstring;
1790 
1791 	if ((resource = sa_get_share_resource(share, NULL)) != NULL) {
1792 		resstring = sa_get_resource_attr(resource, "name");
1793 		if (resstring != NULL) {
1794 			char *cp;
1795 			int len;
1796 
1797 			retstring = conv_from_utf8(resstring);
1798 			if (retstring != resstring) {
1799 				sa_free_attr_string(resstring);
1800 				resstring = retstring;
1801 			}
1802 			if (strpbrk(resstring, " ") != NULL) {
1803 				/* account for quotes */
1804 				len = strlen(resstring) + 3;
1805 				cp = calloc(len, sizeof (char));
1806 				if (cp != NULL) {
1807 					(void) snprintf(cp, len,
1808 					    "\"%s\"", resstring);
1809 					sa_free_attr_string(resstring);
1810 					resstring = cp;
1811 				} else {
1812 					sa_free_attr_string(resstring);
1813 					resstring = NULL;
1814 				}
1815 			}
1816 		}
1817 	}
1818 	return (resstring);
1819 }
1820 
1821 /*
1822  * has_resource_with_opt(share)
1823  *
1824  * Check to see if the share has any resource names with optionsets
1825  * set. Also indicate if multiple resource names since the syntax
1826  * would be about the same.
1827  */
1828 static int
1829 has_resource_with_opt(sa_share_t share)
1830 {
1831 	sa_resource_t resource;
1832 	int ret = B_FALSE;
1833 
1834 	for (resource = sa_get_share_resource(share, NULL);
1835 	    resource != NULL;
1836 	    resource = sa_get_next_resource(resource)) {
1837 
1838 		if (sa_get_optionset(resource, NULL) != NULL) {
1839 			ret = B_TRUE;
1840 			break;
1841 		}
1842 	}
1843 	return (ret);
1844 }
1845 
1846 /*
1847  * has_multiple_resource(share)
1848  *
1849  * Check to see if the share has multiple resource names since
1850  * the syntax would be about the same.
1851  */
1852 static boolean_t
1853 has_multiple_resource(sa_share_t share)
1854 {
1855 	sa_resource_t resource;
1856 	int num;
1857 
1858 	for (num = 0, resource = sa_get_share_resource(share, NULL);
1859 	    resource != NULL;
1860 	    resource = sa_get_next_resource(resource)) {
1861 		num++;
1862 		if (num > 1)
1863 			return (B_TRUE);
1864 	}
1865 	return (B_FALSE);
1866 }
1867 
1868 /*
1869  * show_share(share, verbose, properties, proto, iszfs, sharepath)
1870  *
1871  * print out the share information. With the addition of resource as a
1872  * full object that can have multiple instances below the share, we
1873  * need to display that as well.
1874  */
1875 
1876 static void
1877 show_share(sa_share_t share, int verbose, int properties, char *proto,
1878     int iszfs, char *sharepath)
1879 {
1880 	char *drive;
1881 	char *exclude;
1882 	sa_resource_t resource = NULL;
1883 	char *description;
1884 	char *rsrcname;
1885 	int rsrcwithopt;
1886 	boolean_t multiple;
1887 	char *type;
1888 
1889 	rsrcwithopt = has_resource_with_opt(share);
1890 
1891 	if (verbose || (properties && rsrcwithopt)) {
1892 		/* First, indicate if transient */
1893 		type = sa_get_share_attr(share, "type");
1894 		if (type != NULL && !iszfs && verbose &&
1895 		    strcmp(type, "transient") == 0)
1896 			(void) printf("\t* ");
1897 		else
1898 			(void) printf("\t  ");
1899 
1900 		if (type != NULL)
1901 			sa_free_attr_string(type);
1902 
1903 		/*
1904 		 * If we came in with verbose, we want to handle the case of
1905 		 * multiple resources as though they had properties set.
1906 		 */
1907 		multiple = has_multiple_resource(share);
1908 
1909 		/*
1910 		 * if there is a description on the share and there
1911 		 * are resources, treat as multiple resources in order
1912 		 * to get all descriptions displayed.
1913 		 */
1914 		description = sa_get_share_description(share);
1915 		resource = sa_get_share_resource(share, NULL);
1916 
1917 		if (description != NULL && resource != NULL)
1918 			multiple = B_TRUE;
1919 
1920 		/* Next, if not multiple follow old model */
1921 		if (!multiple && !rsrcwithopt) {
1922 			rsrcname = get_resource(share);
1923 			if (rsrcname != NULL && strlen(rsrcname) > 0) {
1924 				(void) printf("%s=%s", rsrcname, sharepath);
1925 			} else {
1926 				(void) printf("%s", sharepath);
1927 			}
1928 			if (rsrcname != NULL)
1929 				sa_free_attr_string(rsrcname);
1930 			/* Print the description string if there is one. */
1931 			print_rsrc_desc(resource, description);
1932 		} else {
1933 			/* Treat as simple and then resources come later */
1934 			(void) printf("%s", sharepath);
1935 		}
1936 		drive = sa_get_share_attr(share, "drive-letter");
1937 		if (drive != NULL) {
1938 			if (strlen(drive) > 0)
1939 				(void) printf(gettext("\tdrive-letter=\"%s:\""),
1940 				    drive);
1941 			sa_free_attr_string(drive);
1942 		}
1943 		if (properties)
1944 			show_properties(share, proto, "\t");
1945 		exclude = sa_get_share_attr(share, "exclude");
1946 		if (exclude != NULL) {
1947 			(void) printf(gettext("\tnot-shared-with=[%s]"),
1948 			    exclude);
1949 			sa_free_attr_string(exclude);
1950 		}
1951 
1952 		if (description != NULL) {
1953 			print_rsrc_desc((sa_resource_t)share, description);
1954 		}
1955 		/*
1956 		 * If there are resource names with options, show them
1957 		 * here, with one line per resource. Resource specific
1958 		 * options are at the end of the line followed by
1959 		 * description, if any.
1960 		 */
1961 		if (rsrcwithopt || multiple) {
1962 			for (resource = sa_get_share_resource(share, NULL);
1963 			    resource != NULL;
1964 			    resource = sa_get_next_resource(resource)) {
1965 				int has_space;
1966 				char *rsrc;
1967 
1968 				(void) printf("\n\t\t  ");
1969 				rsrcname = sa_get_resource_attr(resource,
1970 				    "name");
1971 				if (rsrcname == NULL)
1972 					continue;
1973 
1974 				rsrc = conv_from_utf8(rsrcname);
1975 				has_space = strpbrk(rsrc, " ") != NULL;
1976 
1977 				if (has_space)
1978 					(void) printf("\"%s\"=%s", rsrc,
1979 					    sharepath);
1980 				else
1981 					(void) printf("%s=%s", rsrc,
1982 					    sharepath);
1983 				if (rsrc != rsrcname)
1984 					sa_free_attr_string(rsrc);
1985 				sa_free_attr_string(rsrcname);
1986 				if (properties || rsrcwithopt)
1987 					show_properties(resource, proto, "\t");
1988 
1989 				/* Get description string if any */
1990 				print_rsrc_desc(resource, description);
1991 			}
1992 		}
1993 		if (description != NULL)
1994 			sa_free_share_description(description);
1995 	} else {
1996 		(void) printf("\t  %s", sharepath);
1997 		if (properties)
1998 			show_properties(share, proto, "\t");
1999 	}
2000 	(void) printf("\n");
2001 }
2002 
2003 /*
2004  * show_group(group, verbose, properties, proto, subgroup)
2005  *
2006  * helper function to show the contents of a group.
2007  */
2008 
2009 static void
2010 show_group(sa_group_t group, int verbose, int properties, char *proto,
2011     char *subgroup)
2012 {
2013 	sa_share_t share;
2014 	char *groupname;
2015 	char *zfs = NULL;
2016 	int iszfs = 0;
2017 	char *sharepath;
2018 
2019 	groupname = sa_get_group_attr(group, "name");
2020 	if (groupname != NULL) {
2021 		if (proto != NULL && !has_protocol(group, proto)) {
2022 			sa_free_attr_string(groupname);
2023 			return;
2024 		}
2025 		/*
2026 		 * check to see if the group is managed by ZFS. If
2027 		 * there is an attribute, then it is. A non-NULL zfs
2028 		 * variable will trigger the different way to display
2029 		 * and will remove the transient property indicator
2030 		 * from the output.
2031 		 */
2032 		zfs = sa_get_group_attr(group, "zfs");
2033 		if (zfs != NULL) {
2034 			iszfs = 1;
2035 			sa_free_attr_string(zfs);
2036 		}
2037 		share = sa_get_share(group, NULL);
2038 		if (subgroup == NULL)
2039 			(void) printf("%s", groupname);
2040 		else
2041 			(void) printf("    %s/%s", subgroup, groupname);
2042 		if (properties)
2043 			show_properties(group, proto, "");
2044 		(void) printf("\n");
2045 		if (strcmp(groupname, "zfs") == 0) {
2046 			sa_group_t zgroup;
2047 
2048 			for (zgroup = sa_get_sub_group(group);
2049 			    zgroup != NULL;
2050 			    zgroup = sa_get_next_group(zgroup)) {
2051 				show_group(zgroup, verbose, properties, proto,
2052 				    "zfs");
2053 			}
2054 			sa_free_attr_string(groupname);
2055 			return;
2056 		}
2057 		/*
2058 		 * Have a group, so list the contents. Resource and
2059 		 * description are only listed if verbose is set.
2060 		 */
2061 		for (share = sa_get_share(group, NULL);
2062 		    share != NULL;
2063 		    share = sa_get_next_share(share)) {
2064 			sharepath = sa_get_share_attr(share, "path");
2065 			if (sharepath != NULL) {
2066 				show_share(share, verbose, properties, proto,
2067 				    iszfs, sharepath);
2068 				sa_free_attr_string(sharepath);
2069 			}
2070 		}
2071 	}
2072 	if (groupname != NULL) {
2073 		sa_free_attr_string(groupname);
2074 	}
2075 }
2076 
2077 /*
2078  * show_group_xml_init()
2079  *
2080  * Create an XML document that will be used to display config info via
2081  * XML format.
2082  */
2083 
2084 xmlDocPtr
2085 show_group_xml_init()
2086 {
2087 	xmlDocPtr doc;
2088 	xmlNodePtr root;
2089 
2090 	doc = xmlNewDoc((xmlChar *)"1.0");
2091 	if (doc != NULL) {
2092 		root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
2093 		if (root != NULL)
2094 			xmlDocSetRootElement(doc, root);
2095 	}
2096 	return (doc);
2097 }
2098 
2099 /*
2100  * show_group_xml(doc, group)
2101  *
2102  * Copy the group info into the XML doc.
2103  */
2104 
2105 static void
2106 show_group_xml(xmlDocPtr doc, sa_group_t group)
2107 {
2108 	xmlNodePtr node;
2109 	xmlNodePtr root;
2110 
2111 	root = xmlDocGetRootElement(doc);
2112 	node = xmlCopyNode((xmlNodePtr)group, 1);
2113 	if (node != NULL && root != NULL) {
2114 		xmlAddChild(root, node);
2115 		/*
2116 		 * In the future, we may have interally used tags that
2117 		 * should not appear in the XML output. Remove
2118 		 * anything we don't want to show here.
2119 		 */
2120 	}
2121 }
2122 
2123 /*
2124  * sa_show(flags, argc, argv)
2125  *
2126  * Implements the show subcommand.
2127  */
2128 
2129 int
2130 sa_show(sa_handle_t handle, int flags, int argc, char *argv[])
2131 {
2132 	sa_group_t group;
2133 	int verbose = 0;
2134 	int properties = 0;
2135 	int c;
2136 	int ret = SA_OK;
2137 	char *protocol = NULL;
2138 	int xml = 0;
2139 	xmlDocPtr doc;
2140 #ifdef lint
2141 	flags = flags;
2142 #endif
2143 
2144 	while ((c = getopt(argc, argv, "?hvP:px")) !=	EOF) {
2145 		switch (c) {
2146 		case 'v':
2147 			verbose++;
2148 			break;
2149 		case 'p':
2150 			properties++;
2151 			break;
2152 		case 'P':
2153 			if (protocol != NULL) {
2154 				(void) printf(gettext(
2155 				    "Specifying multiple protocols "
2156 				    "not supported: %s\n"),
2157 				    protocol);
2158 				return (SA_SYNTAX_ERR);
2159 			}
2160 			protocol = optarg;
2161 			if (!sa_valid_protocol(protocol)) {
2162 				(void) printf(gettext(
2163 				    "Invalid protocol specified: %s\n"),
2164 				    protocol);
2165 				return (SA_INVALID_PROTOCOL);
2166 			}
2167 			break;
2168 		case 'x':
2169 			xml++;
2170 			break;
2171 		default:
2172 		case 'h':
2173 		case '?':
2174 			(void) printf(gettext("usage: %s\n"),
2175 			    sa_get_usage(USAGE_SHOW));
2176 			return (0);
2177 		}
2178 	}
2179 
2180 	if (xml) {
2181 		doc = show_group_xml_init();
2182 		if (doc == NULL)
2183 			ret = SA_NO_MEMORY;
2184 	}
2185 
2186 	if (optind == argc) {
2187 		/* No group specified so go through them all */
2188 		for (group = sa_get_group(handle, NULL);
2189 		    group != NULL;
2190 		    group = sa_get_next_group(group)) {
2191 			/*
2192 			 * Have a group so check if one we want and then list
2193 			 * contents with appropriate options.
2194 			 */
2195 			if (xml)
2196 				show_group_xml(doc, group);
2197 			else
2198 				show_group(group, verbose, properties, protocol,
2199 				    NULL);
2200 		}
2201 	} else {
2202 		/* Have a specified list of groups */
2203 		for (; optind < argc; optind++) {
2204 			group = sa_get_group(handle, argv[optind]);
2205 			if (group != NULL) {
2206 				if (xml)
2207 					show_group_xml(doc, group);
2208 				else
2209 					show_group(group, verbose, properties,
2210 					    protocol, NULL);
2211 			} else {
2212 				(void) printf(gettext("%s: not found\n"),
2213 				    argv[optind]);
2214 				ret = SA_NO_SUCH_GROUP;
2215 			}
2216 		}
2217 	}
2218 	if (xml && ret == SA_OK) {
2219 		xmlDocFormatDump(stdout, doc, 1);
2220 		xmlFreeDoc(doc);
2221 	}
2222 	return (ret);
2223 
2224 }
2225 
2226 /*
2227  * enable_share(group, share, update_legacy)
2228  *
2229  * helper function to enable a share if the group is enabled.
2230  */
2231 
2232 static int
2233 enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share,
2234     int update_legacy)
2235 {
2236 	char *value;
2237 	int enabled;
2238 	sa_optionset_t optionset;
2239 	int err;
2240 	int ret = SA_OK;
2241 	char *zfs = NULL;
2242 	int iszfs = 0;
2243 	int isshare;
2244 
2245 	/*
2246 	 * need to enable this share if the group is enabled but not
2247 	 * otherwise. The enable is also done on each protocol
2248 	 * represented in the group.
2249 	 */
2250 	value = sa_get_group_attr(group, "state");
2251 	enabled = value != NULL && strcmp(value, "enabled") == 0;
2252 	if (value != NULL)
2253 		sa_free_attr_string(value);
2254 	/* remove legacy config if necessary */
2255 	if (update_legacy)
2256 		ret = sa_delete_legacy(share, NULL);
2257 	zfs = sa_get_group_attr(group, "zfs");
2258 	if (zfs != NULL) {
2259 		iszfs++;
2260 		sa_free_attr_string(zfs);
2261 	}
2262 
2263 	/*
2264 	 * Step through each optionset at the group level and
2265 	 * enable the share based on the protocol type. This
2266 	 * works because protocols must be set on the group
2267 	 * for the protocol to be enabled.
2268 	 */
2269 	isshare = sa_is_share(share);
2270 	for (optionset = sa_get_optionset(group, NULL);
2271 	    optionset != NULL && ret == SA_OK;
2272 	    optionset = sa_get_next_optionset(optionset)) {
2273 		value = sa_get_optionset_attr(optionset, "type");
2274 		if (value != NULL) {
2275 			if (enabled) {
2276 				if (isshare) {
2277 					err = sa_enable_share(share, value);
2278 				} else {
2279 					err = sa_enable_resource(share, value);
2280 					if (err == SA_NOT_SUPPORTED) {
2281 						sa_share_t parent;
2282 						parent = sa_get_resource_parent(
2283 						    share);
2284 						if (parent != NULL)
2285 							err = sa_enable_share(
2286 							    parent, value);
2287 					}
2288 				}
2289 				if (err != SA_OK) {
2290 					ret = err;
2291 					(void) printf(gettext(
2292 					    "Failed to enable share for "
2293 					    "\"%s\": %s\n"),
2294 					    value, sa_errorstr(ret));
2295 				}
2296 			}
2297 			/*
2298 			 * If we want to update the legacy, use a copy of
2299 			 * share so we can avoid breaking the loop we are in
2300 			 * since we might also need to go up the tree to the
2301 			 * parent.
2302 			 */
2303 			if (update_legacy && !iszfs) {
2304 				sa_share_t update = share;
2305 				if (!sa_is_share(share)) {
2306 					update = sa_get_resource_parent(share);
2307 				}
2308 				(void) sa_update_legacy(update, value);
2309 			}
2310 			sa_free_attr_string(value);
2311 		}
2312 	}
2313 	if (ret == SA_OK)
2314 		(void) sa_update_config(handle);
2315 	return (ret);
2316 }
2317 
2318 /*
2319  * sa_require_resource(group)
2320  *
2321  * if any of the defined protocols on the group require resource
2322  * names, then all shares must have them.
2323  */
2324 
2325 static int
2326 sa_require_resource(sa_group_t group)
2327 {
2328 	sa_optionset_t optionset;
2329 
2330 	for (optionset = sa_get_optionset(group, NULL);
2331 	    optionset != NULL;
2332 	    optionset = sa_get_next_optionset(optionset)) {
2333 		char *proto;
2334 
2335 		proto = sa_get_optionset_attr(optionset, "type");
2336 		if (proto != NULL) {
2337 			uint64_t features;
2338 
2339 			features = sa_proto_get_featureset(proto);
2340 			if (features & SA_FEATURE_RESOURCE) {
2341 				sa_free_attr_string(proto);
2342 				return (B_TRUE);
2343 			}
2344 			sa_free_attr_string(proto);
2345 		}
2346 	}
2347 	return (B_FALSE);
2348 }
2349 
2350 /*
2351  * sa_addshare(flags, argc, argv)
2352  *
2353  * implements add-share subcommand.
2354  */
2355 
2356 static int
2357 sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[])
2358 {
2359 	int verbose = 0;
2360 	int dryrun = 0;
2361 	int c;
2362 	int ret = SA_OK;
2363 	sa_group_t group;
2364 	sa_share_t share;
2365 	sa_resource_t resource = NULL;
2366 	char *sharepath = NULL;
2367 	char *description = NULL;
2368 	char *rsrcname = NULL;
2369 	char *rsrc = NULL;
2370 	int persist = SA_SHARE_PERMANENT; /* default to persist */
2371 	int auth;
2372 	char dir[MAXPATHLEN];
2373 
2374 	while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) {
2375 		switch (c) {
2376 		case 'n':
2377 			dryrun++;
2378 			break;
2379 		case 'v':
2380 			verbose++;
2381 			break;
2382 		case 'd':
2383 			description = optarg;
2384 			break;
2385 		case 'r':
2386 			if (rsrcname != NULL) {
2387 				(void) printf(gettext("Adding multiple "
2388 				    "resource names not"
2389 				    " supported\n"));
2390 				return (SA_SYNTAX_ERR);
2391 			}
2392 			rsrcname = optarg;
2393 			break;
2394 		case 's':
2395 			/*
2396 			 * Save share path into group. Currently limit
2397 			 * to one share per command.
2398 			 */
2399 			if (sharepath != NULL) {
2400 				(void) printf(gettext(
2401 				    "Adding multiple shares not supported\n"));
2402 				return (SA_SYNTAX_ERR);
2403 			}
2404 			sharepath = optarg;
2405 			break;
2406 		case 't':
2407 			persist = SA_SHARE_TRANSIENT;
2408 			break;
2409 		default:
2410 		case 'h':
2411 		case '?':
2412 			(void) printf(gettext("usage: %s\n"),
2413 			    sa_get_usage(USAGE_ADD_SHARE));
2414 			return (0);
2415 		}
2416 	}
2417 
2418 	if (optind >= argc) {
2419 		(void) printf(gettext("usage: %s\n"),
2420 		    sa_get_usage(USAGE_ADD_SHARE));
2421 		if (dryrun || sharepath != NULL || description != NULL ||
2422 		    rsrcname != NULL || verbose || persist) {
2423 			(void) printf(gettext("\tgroup must be specified\n"));
2424 			ret = SA_NO_SUCH_GROUP;
2425 		} else {
2426 			ret = SA_OK;
2427 		}
2428 	} else {
2429 		if (sharepath == NULL) {
2430 			(void) printf(gettext("usage: %s\n"),
2431 			    sa_get_usage(USAGE_ADD_SHARE));
2432 			(void) printf(gettext(
2433 			    "\t-s sharepath must be specified\n"));
2434 			ret = SA_BAD_PATH;
2435 		}
2436 		if (ret == SA_OK) {
2437 			if (realpath(sharepath, dir) == NULL) {
2438 				ret = SA_BAD_PATH;
2439 				(void) printf(gettext("Path "
2440 				    "is not valid: %s\n"),
2441 				    sharepath);
2442 			} else {
2443 				sharepath = dir;
2444 			}
2445 		}
2446 		if (ret == SA_OK && rsrcname != NULL) {
2447 			/* check for valid syntax */
2448 			if (validresource(rsrcname)) {
2449 				rsrc = conv_to_utf8(rsrcname);
2450 				resource = sa_find_resource(handle, rsrc);
2451 				if (resource != NULL) {
2452 					/*
2453 					 * Resource names must be
2454 					 * unique in the system
2455 					 */
2456 					ret = SA_DUPLICATE_NAME;
2457 					(void) printf(gettext("usage: %s\n"),
2458 					    sa_get_usage(USAGE_ADD_SHARE));
2459 					(void) printf(gettext(
2460 					    "\tresource names must be unique "
2461 					    "in the system\n"));
2462 				}
2463 			} else {
2464 				(void) printf(gettext("usage: %s\n"),
2465 				    sa_get_usage(USAGE_ADD_SHARE));
2466 				(void) printf(gettext(
2467 				    "\tresource names use restricted "
2468 				    "character set\n"));
2469 				ret = SA_INVALID_NAME;
2470 			}
2471 		}
2472 
2473 		if (ret != SA_OK) {
2474 			if (rsrc != NULL && rsrcname != rsrc)
2475 				sa_free_attr_string(rsrc);
2476 			return (ret);
2477 		}
2478 
2479 		share = sa_find_share(handle, sharepath);
2480 		if (share != NULL) {
2481 			if (rsrcname == NULL) {
2482 				/*
2483 				 * Can only have a duplicate share if a new
2484 				 * resource name is being added.
2485 				 */
2486 				ret = SA_DUPLICATE_NAME;
2487 				(void) printf(gettext("Share path already "
2488 				    "shared: %s\n"), sharepath);
2489 			}
2490 		}
2491 		if (ret != SA_OK)
2492 			return (ret);
2493 
2494 		group = sa_get_group(handle, argv[optind]);
2495 		if (group != NULL) {
2496 			if (sa_require_resource(group) == B_TRUE &&
2497 			    rsrcname == NULL) {
2498 				(void) printf(gettext(
2499 				    "Resource name is required "
2500 				    "by at least one enabled protocol "
2501 				    "in group\n"));
2502 				return (SA_RESOURCE_REQUIRED);
2503 			}
2504 			if (share == NULL && ret == SA_OK) {
2505 				if (dryrun)
2506 					ret = sa_check_path(group, sharepath,
2507 					    SA_CHECK_NORMAL);
2508 				else
2509 					share = sa_add_share(group, sharepath,
2510 					    persist, &ret);
2511 			}
2512 			/*
2513 			 * Make sure this isn't an attempt to put a resourced
2514 			 * share into a different group than it already is in.
2515 			 */
2516 			if (share != NULL) {
2517 				sa_group_t parent;
2518 				parent = sa_get_parent_group(share);
2519 				if (parent != group) {
2520 					ret = SA_DUPLICATE_NAME;
2521 					(void) printf(gettext(
2522 					    "Share path already "
2523 					    "shared: %s\n"), sharepath);
2524 				}
2525 			}
2526 			if (!dryrun && share == NULL) {
2527 				(void) printf(gettext(
2528 				    "Could not add share: %s\n"),
2529 				    sa_errorstr(ret));
2530 			} else {
2531 				auth = check_authorizations(argv[optind],
2532 				    flags);
2533 				if (!dryrun && ret == SA_OK) {
2534 					if (rsrcname != NULL) {
2535 						resource = sa_add_resource(
2536 						    share,
2537 						    rsrc,
2538 						    SA_SHARE_PERMANENT,
2539 						    &ret);
2540 					}
2541 					if (ret == SA_OK &&
2542 					    description != NULL) {
2543 						if (resource != NULL)
2544 							ret =
2545 							    set_resource_desc(
2546 							    resource,
2547 							    description);
2548 						else
2549 							ret =
2550 							    set_share_desc(
2551 							    share,
2552 							    description);
2553 					}
2554 					if (ret == SA_OK) {
2555 						/* now enable the share(s) */
2556 						if (resource != NULL) {
2557 							ret = enable_share(
2558 							    handle,
2559 							    group,
2560 							    resource,
2561 							    1);
2562 						} else {
2563 							ret = enable_share(
2564 							    handle,
2565 							    group,
2566 							    share,
2567 							    1);
2568 						}
2569 						ret = sa_update_config(handle);
2570 					}
2571 					switch (ret) {
2572 					case SA_DUPLICATE_NAME:
2573 						(void) printf(gettext(
2574 						    "Resource name in"
2575 						    "use: %s\n"),
2576 						    rsrcname);
2577 						break;
2578 					default:
2579 						(void) printf(gettext(
2580 						    "Could not set "
2581 						    "attribute: %s\n"),
2582 						    sa_errorstr(ret));
2583 						break;
2584 					case SA_OK:
2585 						break;
2586 					}
2587 				} else if (dryrun && ret == SA_OK &&
2588 				    !auth && verbose) {
2589 					(void) printf(gettext(
2590 					    "Command would fail: %s\n"),
2591 					    sa_errorstr(SA_NO_PERMISSION));
2592 					ret = SA_NO_PERMISSION;
2593 				}
2594 			}
2595 		} else {
2596 			switch (ret) {
2597 			default:
2598 				(void) printf(gettext(
2599 				    "Group \"%s\" not found\n"), argv[optind]);
2600 				ret = SA_NO_SUCH_GROUP;
2601 				break;
2602 			case SA_BAD_PATH:
2603 			case SA_DUPLICATE_NAME:
2604 				break;
2605 			}
2606 		}
2607 	}
2608 	return (ret);
2609 }
2610 
2611 /*
2612  * sa_moveshare(flags, argc, argv)
2613  *
2614  * implements move-share subcommand.
2615  */
2616 
2617 int
2618 sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[])
2619 {
2620 	int verbose = 0;
2621 	int dryrun = 0;
2622 	int c;
2623 	int ret = SA_OK;
2624 	sa_group_t group;
2625 	sa_share_t share;
2626 	char *rsrcname = NULL;
2627 	char *sharepath = NULL;
2628 	int authsrc = 0, authdst = 0;
2629 	char dir[MAXPATHLEN];
2630 
2631 	while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) {
2632 		switch (c) {
2633 		case 'n':
2634 			dryrun++;
2635 			break;
2636 		case 'v':
2637 			verbose++;
2638 			break;
2639 		case 'r':
2640 			if (rsrcname != NULL) {
2641 				(void) printf(gettext(
2642 				    "Moving multiple resource names not"
2643 				    " supported\n"));
2644 				return (SA_SYNTAX_ERR);
2645 			}
2646 			rsrcname = optarg;
2647 			break;
2648 		case 's':
2649 			/*
2650 			 * Remove share path from group. Currently limit
2651 			 * to one share per command.
2652 			 */
2653 			if (sharepath != NULL) {
2654 				(void) printf(gettext("Moving multiple shares"
2655 				    " not supported\n"));
2656 				return (SA_SYNTAX_ERR);
2657 			}
2658 			sharepath = optarg;
2659 			break;
2660 		default:
2661 		case 'h':
2662 		case '?':
2663 			(void) printf(gettext("usage: %s\n"),
2664 			    sa_get_usage(USAGE_MOVE_SHARE));
2665 			return (0);
2666 		}
2667 	}
2668 
2669 	if (optind >= argc || sharepath == NULL) {
2670 		(void) printf(gettext("usage: %s\n"),
2671 		    sa_get_usage(USAGE_MOVE_SHARE));
2672 		if (dryrun || verbose || sharepath != NULL) {
2673 			(void) printf(gettext("\tgroup must be specified\n"));
2674 			ret = SA_NO_SUCH_GROUP;
2675 		} else {
2676 			if (sharepath == NULL) {
2677 				ret = SA_SYNTAX_ERR;
2678 				(void) printf(gettext(
2679 				    "\tsharepath must be specified\n"));
2680 			} else {
2681 				ret = SA_OK;
2682 			}
2683 		}
2684 	} else {
2685 		sa_group_t parent;
2686 		char *zfsold;
2687 		char *zfsnew;
2688 
2689 		if (sharepath == NULL) {
2690 			(void) printf(gettext(
2691 			    "sharepath must be specified with the -s "
2692 			    "option\n"));
2693 			return (SA_BAD_PATH);
2694 		}
2695 		group = sa_get_group(handle, argv[optind]);
2696 		if (group == NULL) {
2697 			(void) printf(gettext("Group \"%s\" not found\n"),
2698 			    argv[optind]);
2699 			return (SA_NO_SUCH_GROUP);
2700 		}
2701 		share = sa_find_share(handle, sharepath);
2702 		/*
2703 		 * If a share wasn't found, it may have been a symlink
2704 		 * or has a trailing '/'. Try again after resolving
2705 		 * with realpath().
2706 		 */
2707 		if (share == NULL) {
2708 			if (realpath(sharepath, dir) == NULL) {
2709 				(void) printf(gettext("Path "
2710 				    "is not valid: %s\n"),
2711 				    sharepath);
2712 				return (SA_BAD_PATH);
2713 			}
2714 			sharepath = dir;
2715 			share = sa_find_share(handle, sharepath);
2716 		}
2717 		if (share == NULL) {
2718 			(void) printf(gettext("Share not found: %s\n"),
2719 			    sharepath);
2720 			return (SA_NO_SUCH_PATH);
2721 		}
2722 		authdst = check_authorizations(argv[optind], flags);
2723 
2724 		parent = sa_get_parent_group(share);
2725 		if (parent != NULL) {
2726 			char *pname;
2727 			pname = sa_get_group_attr(parent, "name");
2728 			if (pname != NULL) {
2729 				authsrc = check_authorizations(pname, flags);
2730 				sa_free_attr_string(pname);
2731 			}
2732 			zfsold = sa_get_group_attr(parent, "zfs");
2733 			zfsnew = sa_get_group_attr(group, "zfs");
2734 			if ((zfsold != NULL && zfsnew == NULL) ||
2735 			    (zfsold == NULL && zfsnew != NULL)) {
2736 				ret = SA_NOT_ALLOWED;
2737 			}
2738 			if (zfsold != NULL)
2739 				sa_free_attr_string(zfsold);
2740 			if (zfsnew != NULL)
2741 				sa_free_attr_string(zfsnew);
2742 		}
2743 
2744 		if (ret == SA_OK && parent != group && !dryrun) {
2745 			char *oldstate;
2746 			/*
2747 			 * Note that the share may need to be
2748 			 * "unshared" if the new group is disabled and
2749 			 * the old was enabled or it may need to be
2750 			 * share to update if the new group is
2751 			 * enabled. We disable before the move and
2752 			 * will have to enable after the move in order
2753 			 * to cleanup entries for protocols that
2754 			 * aren't in the new group.
2755 			 */
2756 			oldstate = sa_get_group_attr(parent, "state");
2757 
2758 			/* enable_share determines what to do */
2759 			if (strcmp(oldstate, "enabled") == 0)
2760 				(void) sa_disable_share(share, NULL);
2761 
2762 			if (oldstate != NULL)
2763 				sa_free_attr_string(oldstate);
2764 		}
2765 
2766 		if (!dryrun && ret == SA_OK)
2767 			ret = sa_move_share(group, share);
2768 
2769 		/*
2770 		 * Reenable and update any config information.
2771 		 */
2772 		if (ret == SA_OK && parent != group && !dryrun) {
2773 			ret = sa_update_config(handle);
2774 
2775 			(void) enable_share(handle, group, share, 1);
2776 		}
2777 
2778 		if (ret != SA_OK)
2779 			(void) printf(gettext("Could not move share: %s\n"),
2780 			    sa_errorstr(ret));
2781 
2782 		if (dryrun && ret == SA_OK && !(authsrc & authdst) &&
2783 		    verbose) {
2784 			(void) printf(gettext("Command would fail: %s\n"),
2785 			    sa_errorstr(SA_NO_PERMISSION));
2786 		}
2787 	}
2788 	return (ret);
2789 }
2790 
2791 /*
2792  * sa_removeshare(flags, argc, argv)
2793  *
2794  * implements remove-share subcommand.
2795  */
2796 
2797 int
2798 sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[])
2799 {
2800 	int verbose = 0;
2801 	int dryrun = 0;
2802 	int force = 0;
2803 	int c;
2804 	int ret = SA_OK;
2805 	sa_group_t group;
2806 	sa_resource_t resource = NULL;
2807 	sa_share_t share = NULL;
2808 	char *rsrcname = NULL;
2809 	char *sharepath = NULL;
2810 	char dir[MAXPATHLEN];
2811 	int auth;
2812 
2813 	while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) {
2814 		switch (c) {
2815 		case 'n':
2816 			dryrun++;
2817 			break;
2818 		case 'v':
2819 			verbose++;
2820 			break;
2821 		case 'f':
2822 			force++;
2823 			break;
2824 		case 's':
2825 			/*
2826 			 * Remove share path from group. Currently limit
2827 			 * to one share per command.
2828 			 */
2829 			if (sharepath != NULL) {
2830 				(void) printf(gettext(
2831 				    "Removing multiple shares not "
2832 				    "supported\n"));
2833 				return (SA_SYNTAX_ERR);
2834 			}
2835 			sharepath = optarg;
2836 			break;
2837 		case 'r':
2838 			/*
2839 			 * Remove share from group if last resource or remove
2840 			 * resource from share if multiple resources.
2841 			 */
2842 			if (rsrcname != NULL) {
2843 				(void) printf(gettext(
2844 				    "Removing multiple resource names not "
2845 				    "supported\n"));
2846 				return (SA_SYNTAX_ERR);
2847 			}
2848 			rsrcname = optarg;
2849 			break;
2850 		default:
2851 		case 'h':
2852 		case '?':
2853 			(void) printf(gettext("usage: %s\n"),
2854 			    sa_get_usage(USAGE_REMOVE_SHARE));
2855 			return (0);
2856 		}
2857 	}
2858 
2859 	if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) {
2860 		if (sharepath == NULL && rsrcname == NULL) {
2861 			(void) printf(gettext("usage: %s\n"),
2862 			    sa_get_usage(USAGE_REMOVE_SHARE));
2863 			(void) printf(gettext("\t-s sharepath or -r resource"
2864 			    " must be specified\n"));
2865 			ret = SA_BAD_PATH;
2866 		} else {
2867 			ret = SA_OK;
2868 		}
2869 	}
2870 	if (ret != SA_OK) {
2871 		return (ret);
2872 	}
2873 
2874 	if (optind < argc) {
2875 		if ((optind + 1) < argc) {
2876 			(void) printf(gettext("Extraneous group(s) at end of "
2877 			    "command\n"));
2878 			ret = SA_SYNTAX_ERR;
2879 		} else {
2880 			group = sa_get_group(handle, argv[optind]);
2881 			if (group == NULL) {
2882 				(void) printf(gettext(
2883 				    "Group \"%s\" not found\n"), argv[optind]);
2884 				ret = SA_NO_SUCH_GROUP;
2885 			}
2886 		}
2887 	} else {
2888 		group = NULL;
2889 	}
2890 
2891 	if (rsrcname != NULL) {
2892 		resource = sa_find_resource(handle, rsrcname);
2893 		if (resource == NULL) {
2894 			ret = SA_NO_SUCH_RESOURCE;
2895 			(void) printf(gettext(
2896 			    "Resource name not found for share: %s\n"),
2897 			    rsrcname);
2898 		}
2899 	}
2900 
2901 	/*
2902 	 * Lookup the path in the internal configuration. Care
2903 	 * must be taken to handle the case where the
2904 	 * underlying path has been removed since we need to
2905 	 * be able to deal with that as well.
2906 	 */
2907 	if (ret == SA_OK) {
2908 		if (sharepath != NULL) {
2909 			if (group != NULL)
2910 				share = sa_get_share(group, sharepath);
2911 			else
2912 				share = sa_find_share(handle, sharepath);
2913 		}
2914 
2915 		if (resource != NULL) {
2916 			sa_share_t rsrcshare;
2917 			rsrcshare = sa_get_resource_parent(resource);
2918 			if (share == NULL)
2919 				share = rsrcshare;
2920 			else if (share != rsrcshare) {
2921 				ret = SA_NO_SUCH_RESOURCE;
2922 				(void) printf(gettext(
2923 				    "Bad resource name for share: %s\n"),
2924 				    rsrcname);
2925 				share = NULL;
2926 			}
2927 		}
2928 
2929 		/*
2930 		 * If we didn't find the share with the provided path,
2931 		 * it may be a symlink so attempt to resolve it using
2932 		 * realpath and try again. Realpath will resolve any
2933 		 * symlinks and place them in "dir". Note that
2934 		 * sharepath is only used for the lookup the first
2935 		 * time and later for error messages. dir will be used
2936 		 * on the second attempt. Once a share is found, all
2937 		 * operations are based off of the share variable.
2938 		 */
2939 		if (share == NULL) {
2940 			if (realpath(sharepath, dir) == NULL) {
2941 				ret = SA_BAD_PATH;
2942 				(void) printf(gettext(
2943 				    "Path is not valid: %s\n"), sharepath);
2944 			} else {
2945 				if (group != NULL)
2946 					share = sa_get_share(group, dir);
2947 				else
2948 					share = sa_find_share(handle, dir);
2949 			}
2950 		}
2951 	}
2952 
2953 	/*
2954 	 * If there hasn't been an error, there was likely a
2955 	 * path found. If not, give the appropriate error
2956 	 * message and set the return error. If it was found,
2957 	 * then disable the share and then remove it from the
2958 	 * configuration.
2959 	 */
2960 	if (ret != SA_OK) {
2961 		return (ret);
2962 	}
2963 	if (share == NULL) {
2964 		if (group != NULL)
2965 			(void) printf(gettext("Share not found in group %s:"
2966 			    " %s\n"), argv[optind], sharepath);
2967 		else
2968 			(void) printf(gettext("Share not found: %s\n"),
2969 			    sharepath);
2970 		ret = SA_NO_SUCH_PATH;
2971 	} else {
2972 		if (group == NULL)
2973 			group = sa_get_parent_group(share);
2974 		if (!dryrun) {
2975 			if (ret == SA_OK) {
2976 				if (resource != NULL)
2977 					ret = sa_disable_resource(resource,
2978 					    NULL);
2979 				else
2980 					ret = sa_disable_share(share, NULL);
2981 				/*
2982 				 * We don't care if it fails since it
2983 				 * could be disabled already. Some
2984 				 * unexpected errors could occur that
2985 				 * prevent removal, so also check for
2986 				 * force being set.
2987 				 */
2988 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
2989 				    ret == SA_NOT_SUPPORTED ||
2990 				    ret == SA_SYSTEM_ERR || force) &&
2991 				    resource == NULL)
2992 					ret = sa_remove_share(share);
2993 
2994 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
2995 				    ret == SA_NOT_SUPPORTED ||
2996 				    ret == SA_SYSTEM_ERR || force) &&
2997 				    resource != NULL) {
2998 					ret = sa_remove_resource(resource);
2999 					if (ret == SA_OK) {
3000 						/*
3001 						 * If this was the
3002 						 * last one, remove
3003 						 * the share as well.
3004 						 */
3005 						resource =
3006 						    sa_get_share_resource(
3007 						    share, NULL);
3008 						if (resource == NULL)
3009 							ret = sa_remove_share(
3010 							    share);
3011 					}
3012 				}
3013 				if (ret == SA_OK)
3014 					ret = sa_update_config(handle);
3015 			}
3016 			if (ret != SA_OK)
3017 				(void) printf(gettext("Could not remove share:"
3018 				    " %s\n"), sa_errorstr(ret));
3019 		} else if (ret == SA_OK) {
3020 			char *pname;
3021 			pname = sa_get_group_attr(group, "name");
3022 			if (pname != NULL) {
3023 				auth = check_authorizations(pname, flags);
3024 				sa_free_attr_string(pname);
3025 			}
3026 			if (!auth && verbose) {
3027 				(void) printf(gettext(
3028 				    "Command would fail: %s\n"),
3029 				    sa_errorstr(SA_NO_PERMISSION));
3030 			}
3031 		}
3032 	}
3033 	return (ret);
3034 }
3035 
3036 /*
3037  * sa_set_share(flags, argc, argv)
3038  *
3039  * implements set-share subcommand.
3040  */
3041 
3042 int
3043 sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[])
3044 {
3045 	int dryrun = 0;
3046 	int c;
3047 	int ret = SA_OK;
3048 	sa_group_t group, sharegroup;
3049 	sa_share_t share = NULL;
3050 	sa_resource_t resource = NULL;
3051 	char *sharepath = NULL;
3052 	char *description = NULL;
3053 	char *rsrcname = NULL;
3054 	char *rsrc = NULL;
3055 	char *newname = NULL;
3056 	char *newrsrc;
3057 	char *groupname = NULL;
3058 	int auth;
3059 	int verbose = 0;
3060 
3061 	while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) {
3062 		switch (c) {
3063 		case 'n':
3064 			dryrun++;
3065 			break;
3066 		case 'd':
3067 			description = optarg;
3068 			break;
3069 		case 'v':
3070 			verbose++;
3071 			break;
3072 		case 'r':
3073 			/*
3074 			 * Update share by resource name
3075 			 */
3076 			if (rsrcname != NULL) {
3077 				(void) printf(gettext(
3078 				    "Updating multiple resource names not "
3079 				    "supported\n"));
3080 				return (SA_SYNTAX_ERR);
3081 			}
3082 			rsrcname = optarg;
3083 			break;
3084 		case 's':
3085 			/*
3086 			 * Save share path into group. Currently limit
3087 			 * to one share per command.
3088 			 */
3089 			if (sharepath != NULL) {
3090 				(void) printf(gettext(
3091 				    "Updating multiple shares not "
3092 				    "supported\n"));
3093 				return (SA_SYNTAX_ERR);
3094 			}
3095 			sharepath = optarg;
3096 			break;
3097 		default:
3098 		case 'h':
3099 		case '?':
3100 			(void) printf(gettext("usage: %s\n"),
3101 			    sa_get_usage(USAGE_SET_SHARE));
3102 			return (SA_OK);
3103 		}
3104 	}
3105 
3106 	if (optind >= argc && sharepath == NULL && rsrcname == NULL) {
3107 		if (sharepath == NULL) {
3108 			(void) printf(gettext("usage: %s\n"),
3109 			    sa_get_usage(USAGE_SET_SHARE));
3110 			(void) printf(gettext("\tgroup must be specified\n"));
3111 			ret = SA_BAD_PATH;
3112 		} else {
3113 			ret = SA_OK;
3114 		}
3115 	}
3116 	if ((optind + 1) < argc) {
3117 		(void) printf(gettext("usage: %s\n"),
3118 		    sa_get_usage(USAGE_SET_SHARE));
3119 		(void) printf(gettext("\tExtraneous group(s) at end\n"));
3120 		ret = SA_SYNTAX_ERR;
3121 	}
3122 
3123 	/*
3124 	 * Must have at least one of sharepath and rsrcrname.
3125 	 * It is a syntax error to be missing both.
3126 	 */
3127 	if (sharepath == NULL && rsrcname == NULL) {
3128 		(void) printf(gettext("usage: %s\n"),
3129 		    sa_get_usage(USAGE_SET_SHARE));
3130 		ret = SA_SYNTAX_ERR;
3131 	}
3132 
3133 	if (ret != SA_OK)
3134 		return (ret);
3135 
3136 	if (optind < argc) {
3137 		groupname = argv[optind];
3138 		group = sa_get_group(handle, groupname);
3139 	} else {
3140 		group = NULL;
3141 		groupname = NULL;
3142 	}
3143 	if (rsrcname != NULL) {
3144 		/*
3145 		 * If rsrcname exists, split rename syntax and then
3146 		 * convert to utf 8 if no errors.
3147 		 */
3148 		newname = strchr(rsrcname, '=');
3149 		if (newname != NULL) {
3150 			*newname++ = '\0';
3151 		}
3152 		if (!validresource(rsrcname)) {
3153 			ret = SA_INVALID_NAME;
3154 			(void) printf(gettext("Invalid resource name: "
3155 			    "\"%s\"\n"), rsrcname);
3156 		} else {
3157 			rsrc = conv_to_utf8(rsrcname);
3158 		}
3159 		if (newname != NULL) {
3160 			if (!validresource(newname)) {
3161 				ret = SA_INVALID_NAME;
3162 				(void) printf(gettext("Invalid resource name: "
3163 				    "%s\n"), newname);
3164 			} else {
3165 				newrsrc = conv_to_utf8(newname);
3166 			}
3167 		}
3168 	}
3169 
3170 	if (ret != SA_OK) {
3171 		if (rsrcname != NULL && rsrcname != rsrc)
3172 			sa_free_attr_string(rsrc);
3173 		if (newname != NULL && newname != newrsrc)
3174 			sa_free_attr_string(newrsrc);
3175 		return (ret);
3176 	}
3177 
3178 	if (sharepath != NULL) {
3179 		share = sa_find_share(handle, sharepath);
3180 	} else if (rsrcname != NULL) {
3181 		resource = sa_find_resource(handle, rsrc);
3182 		if (resource != NULL)
3183 			share = sa_get_resource_parent(resource);
3184 		else
3185 			ret = SA_NO_SUCH_RESOURCE;
3186 	}
3187 	if (share != NULL) {
3188 		sharegroup = sa_get_parent_group(share);
3189 		if (group != NULL && group != sharegroup) {
3190 			(void) printf(gettext("Group \"%s\" does not contain "
3191 			    "share %s\n"),
3192 			    argv[optind], sharepath);
3193 			ret = SA_BAD_PATH;
3194 		} else {
3195 			int delgroupname = 0;
3196 			if (groupname == NULL) {
3197 				groupname = sa_get_group_attr(sharegroup,
3198 				    "name");
3199 				delgroupname = 1;
3200 			}
3201 			if (groupname != NULL) {
3202 				auth = check_authorizations(groupname, flags);
3203 				if (delgroupname) {
3204 					sa_free_attr_string(groupname);
3205 					groupname = NULL;
3206 				}
3207 			} else {
3208 				ret = SA_NO_MEMORY;
3209 			}
3210 			if (rsrcname != NULL) {
3211 				resource = sa_find_resource(handle, rsrc);
3212 				if (!dryrun) {
3213 					if (newname != NULL &&
3214 					    resource != NULL)
3215 						ret = sa_rename_resource(
3216 						    resource, newrsrc);
3217 					else if (newname != NULL)
3218 						ret = SA_NO_SUCH_RESOURCE;
3219 					if (newname != NULL &&
3220 					    newname != newrsrc)
3221 						sa_free_attr_string(newrsrc);
3222 				}
3223 				if (rsrc != rsrcname)
3224 					sa_free_attr_string(rsrc);
3225 			}
3226 
3227 			/*
3228 			 * If the user has set a description, it will be
3229 			 * on the resource if -r was used otherwise it
3230 			 * must be on the share.
3231 			 */
3232 			if (ret == SA_OK && description != NULL) {
3233 				if (resource != NULL)
3234 					ret = set_resource_desc(resource,
3235 					    description);
3236 				else
3237 					ret = set_share_desc(share,
3238 					    description);
3239 			}
3240 		}
3241 		if (!dryrun && ret == SA_OK) {
3242 			if (resource != NULL)
3243 				(void) sa_enable_resource(resource, NULL);
3244 			ret = sa_update_config(handle);
3245 		}
3246 		switch (ret) {
3247 		case SA_DUPLICATE_NAME:
3248 			(void) printf(gettext("Resource name in use: %s\n"),
3249 			    rsrcname);
3250 			break;
3251 		default:
3252 			(void) printf(gettext("Could not set: %s\n"),
3253 			    sa_errorstr(ret));
3254 			break;
3255 		case SA_OK:
3256 			if (dryrun && !auth && verbose) {
3257 				(void) printf(gettext(
3258 				    "Command would fail: %s\n"),
3259 				    sa_errorstr(SA_NO_PERMISSION));
3260 			}
3261 			break;
3262 		}
3263 	} else {
3264 		switch (ret) {
3265 		case SA_NO_SUCH_RESOURCE:
3266 			(void) printf(gettext("Resource \"%s\" not found\n"),
3267 			    rsrcname);
3268 			break;
3269 		default:
3270 			if (sharepath != NULL) {
3271 				(void) printf(
3272 				    gettext("Share path \"%s\" not found\n"),
3273 				    sharepath);
3274 				ret = SA_NO_SUCH_PATH;
3275 			} else {
3276 				(void) printf(gettext("Set failed: %s\n"),
3277 				    sa_errorstr(ret));
3278 			}
3279 		}
3280 	}
3281 
3282 	return (ret);
3283 }
3284 
3285 /*
3286  * add_security(group, sectype, optlist, proto, *err)
3287  *
3288  * Helper function to add a security option (named optionset) to the
3289  * group.
3290  */
3291 
3292 static int
3293 add_security(sa_group_t group, char *sectype,
3294     struct options *optlist, char *proto, int *err)
3295 {
3296 	sa_security_t security;
3297 	int ret = SA_OK;
3298 	int result = 0;
3299 
3300 	sectype = sa_proto_space_alias(proto, sectype);
3301 	security = sa_get_security(group, sectype, proto);
3302 	if (security == NULL)
3303 		security = sa_create_security(group, sectype, proto);
3304 
3305 	if (sectype != NULL)
3306 		sa_free_attr_string(sectype);
3307 
3308 	if (security == NULL)
3309 		return (ret);
3310 
3311 	while (optlist != NULL) {
3312 		sa_property_t prop;
3313 		prop = sa_get_property(security, optlist->optname);
3314 		if (prop == NULL) {
3315 			/*
3316 			 * Add the property, but only if it is
3317 			 * a non-NULL or non-zero length value
3318 			 */
3319 			if (optlist->optvalue != NULL) {
3320 				prop = sa_create_property(optlist->optname,
3321 				    optlist->optvalue);
3322 				if (prop != NULL) {
3323 					ret = sa_valid_property(security,
3324 					    proto, prop);
3325 					if (ret != SA_OK) {
3326 						(void) sa_remove_property(prop);
3327 						(void) printf(gettext(
3328 						    "Could not add "
3329 						    "property %s: %s\n"),
3330 						    optlist->optname,
3331 						    sa_errorstr(ret));
3332 					}
3333 					if (ret == SA_OK) {
3334 						ret = sa_add_property(security,
3335 						    prop);
3336 						if (ret != SA_OK) {
3337 							(void) printf(gettext(
3338 							    "Could not add "
3339 							    "property (%s=%s):"
3340 							    " %s\n"),
3341 							    optlist->optname,
3342 							    optlist->optvalue,
3343 							    sa_errorstr(ret));
3344 						} else {
3345 							result = 1;
3346 						}
3347 					}
3348 				}
3349 			}
3350 		} else {
3351 			ret = sa_update_property(prop, optlist->optvalue);
3352 			result = 1; /* should check if really changed */
3353 		}
3354 		optlist = optlist->next;
3355 	}
3356 	/*
3357 	 * When done, properties may have all been removed but
3358 	 * we need to keep the security type itself until
3359 	 * explicitly removed.
3360 	 */
3361 	if (result)
3362 		ret = sa_commit_properties(security, 0);
3363 	*err = ret;
3364 	return (result);
3365 }
3366 
3367 /*
3368  * zfscheck(group, share)
3369  *
3370  * For the special case where a share was provided, make sure it is a
3371  * compatible path for a ZFS property change.  The only path
3372  * acceptable is the path that defines the zfs sub-group (dataset with
3373  * the sharenfs property set) and not one of the paths that inherited
3374  * the NFS properties. Returns SA_OK if it is usable and
3375  * SA_NOT_ALLOWED if it isn't.
3376  *
3377  * If group is not a ZFS group/subgroup, we assume OK since the check
3378  * on return will catch errors for those cases.  What we are looking
3379  * for here is that the group is ZFS and the share is not the defining
3380  * share.  All else is SA_OK.
3381  */
3382 
3383 static int
3384 zfscheck(sa_group_t group, sa_share_t share)
3385 {
3386 	int ret = SA_OK;
3387 	char *attr;
3388 
3389 	if (sa_group_is_zfs(group)) {
3390 		/*
3391 		 * The group is a ZFS group.  Does the share represent
3392 		 * the dataset that defined the group? It is only OK
3393 		 * if the attribute "subgroup" exists on the share and
3394 		 * has a value of "true".
3395 		 */
3396 
3397 		ret = SA_NOT_ALLOWED;
3398 		attr = sa_get_share_attr(share, "subgroup");
3399 		if (attr != NULL) {
3400 			if (strcmp(attr, "true") == 0)
3401 				ret = SA_OK;
3402 			sa_free_attr_string(attr);
3403 		}
3404 	}
3405 	return (ret);
3406 }
3407 
3408 /*
3409  * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
3410  *
3411  * This function implements "set" when a name space (-S) is not
3412  * specified. It is a basic set. Options and other CLI parsing has
3413  * already been done.
3414  *
3415  * "rsrcname" is a "resource name". If it is non-NULL, it must match
3416  * the sharepath if present or group if present, otherwise it is used
3417  * to set options.
3418  *
3419  * Resource names may take options if the protocol supports it. If the
3420  * protocol doesn't support resource level options, rsrcname is just
3421  * an alias for the share.
3422  */
3423 
3424 static int
3425 basic_set(sa_handle_t handle, char *groupname, struct options *optlist,
3426     char *protocol, char *sharepath, char *rsrcname, int dryrun)
3427 {
3428 	sa_group_t group;
3429 	int ret = SA_OK;
3430 	int change = 0;
3431 	struct list *worklist = NULL;
3432 
3433 	group = sa_get_group(handle, groupname);
3434 	if (group != NULL) {
3435 		sa_share_t share = NULL;
3436 		sa_resource_t resource = NULL;
3437 
3438 		/*
3439 		 * If there is a sharepath, make sure it belongs to
3440 		 * the group.
3441 		 */
3442 		if (sharepath != NULL) {
3443 			share = sa_get_share(group, sharepath);
3444 			if (share == NULL) {
3445 				(void) printf(gettext(
3446 				    "Share does not exist in group %s\n"),
3447 				    groupname, sharepath);
3448 				ret = SA_NO_SUCH_PATH;
3449 			} else {
3450 				/* if ZFS and OK, then only group */
3451 				ret = zfscheck(group, share);
3452 				if (ret == SA_OK &&
3453 				    sa_group_is_zfs(group))
3454 					share = NULL;
3455 				if (ret == SA_NOT_ALLOWED)
3456 					(void) printf(gettext(
3457 					    "Properties on ZFS group shares "
3458 					    "not supported: %s\n"), sharepath);
3459 			}
3460 		}
3461 
3462 		/*
3463 		 * If a resource name exists, make sure it belongs to
3464 		 * the share if present else it belongs to the
3465 		 * group. Also check the protocol to see if it
3466 		 * supports resource level properties or not. If not,
3467 		 * use share only.
3468 		 */
3469 		if (rsrcname != NULL) {
3470 			if (share != NULL) {
3471 				resource = sa_get_share_resource(share,
3472 				    rsrcname);
3473 				if (resource == NULL)
3474 					ret = SA_NO_SUCH_RESOURCE;
3475 			} else {
3476 				resource = sa_get_resource(group, rsrcname);
3477 				if (resource != NULL)
3478 					share = sa_get_resource_parent(
3479 					    resource);
3480 				else
3481 					ret = SA_NO_SUCH_RESOURCE;
3482 			}
3483 			if (ret == SA_OK && resource != NULL) {
3484 				uint64_t features;
3485 				/*
3486 				 * Check to see if the resource can take
3487 				 * properties. If so, stick the resource into
3488 				 * "share" so it will all just work.
3489 				 */
3490 				features = sa_proto_get_featureset(protocol);
3491 				if (features & SA_FEATURE_RESOURCE)
3492 					share = (sa_share_t)resource;
3493 			}
3494 		}
3495 
3496 		if (ret == SA_OK) {
3497 			/* group must exist */
3498 			ret = valid_options(optlist, protocol,
3499 			    share == NULL ? group : share, NULL);
3500 			if (ret == SA_OK && !dryrun) {
3501 				if (share != NULL)
3502 					change |= add_optionset(share, optlist,
3503 					    protocol, &ret);
3504 				else
3505 					change |= add_optionset(group, optlist,
3506 					    protocol, &ret);
3507 				if (ret == SA_OK && change)
3508 					worklist = add_list(worklist, group,
3509 					    share, protocol);
3510 			}
3511 		}
3512 		free_opt(optlist);
3513 	} else {
3514 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
3515 		ret = SA_NO_SUCH_GROUP;
3516 	}
3517 	/*
3518 	 * we have a group and potentially legal additions
3519 	 */
3520 
3521 	/*
3522 	 * Commit to configuration if not a dryrunp and properties
3523 	 * have changed.
3524 	 */
3525 	if (!dryrun && ret == SA_OK && change && worklist != NULL)
3526 		/* properties changed, so update all shares */
3527 		(void) enable_all_groups(handle, worklist, 0, 0, protocol,
3528 		    B_TRUE);
3529 
3530 	if (worklist != NULL)
3531 		free_list(worklist);
3532 	return (ret);
3533 }
3534 
3535 /*
3536  * space_set(groupname, optlist, protocol, sharepath, dryrun)
3537  *
3538  * This function implements "set" when a name space (-S) is
3539  * specified. It is a namespace set. Options and other CLI parsing has
3540  * already been done.
3541  */
3542 
3543 static int
3544 space_set(sa_handle_t handle, char *groupname, struct options *optlist,
3545     char *protocol, char *sharepath, int dryrun, char *sectype)
3546 {
3547 	sa_group_t group;
3548 	int ret = SA_OK;
3549 	int change = 0;
3550 	struct list *worklist = NULL;
3551 
3552 	/*
3553 	 * make sure protcol and sectype are valid
3554 	 */
3555 
3556 	if (sa_proto_valid_space(protocol, sectype) == 0) {
3557 		(void) printf(gettext("Option space \"%s\" not valid "
3558 		    "for protocol.\n"), sectype);
3559 		return (SA_INVALID_SECURITY);
3560 	}
3561 
3562 	group = sa_get_group(handle, groupname);
3563 	if (group != NULL) {
3564 		sa_share_t share = NULL;
3565 		if (sharepath != NULL) {
3566 			share = sa_get_share(group, sharepath);
3567 			if (share == NULL) {
3568 				(void) printf(gettext(
3569 				    "Share does not exist in group %s\n"),
3570 				    groupname, sharepath);
3571 				ret = SA_NO_SUCH_PATH;
3572 			} else {
3573 				/* if ZFS and OK, then only group */
3574 				ret = zfscheck(group, share);
3575 				if (ret == SA_OK &&
3576 				    sa_group_is_zfs(group))
3577 					share = NULL;
3578 				if (ret == SA_NOT_ALLOWED)
3579 					(void) printf(gettext(
3580 					    "Properties on ZFS group shares "
3581 					    "not supported: %s\n"), sharepath);
3582 			}
3583 		}
3584 		if (ret == SA_OK) {
3585 			/* group must exist */
3586 			ret = valid_options(optlist, protocol,
3587 			    share == NULL ? group : share, sectype);
3588 			if (ret == SA_OK && !dryrun) {
3589 				if (share != NULL)
3590 					change = add_security(share, sectype,
3591 					    optlist, protocol, &ret);
3592 				else
3593 					change = add_security(group, sectype,
3594 					    optlist, protocol, &ret);
3595 				if (ret != SA_OK)
3596 					(void) printf(gettext(
3597 					    "Could not set property: %s\n"),
3598 					    sa_errorstr(ret));
3599 			}
3600 			if (ret == SA_OK && change)
3601 				worklist = add_list(worklist, group, share,
3602 				    protocol);
3603 		}
3604 		free_opt(optlist);
3605 	} else {
3606 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
3607 		ret = SA_NO_SUCH_GROUP;
3608 	}
3609 
3610 	/*
3611 	 * We have a group and potentially legal additions.
3612 	 */
3613 
3614 	/* Commit to configuration if not a dryrun */
3615 	if (!dryrun && ret == 0) {
3616 		if (change && worklist != NULL) {
3617 			/* properties changed, so update all shares */
3618 			(void) enable_all_groups(handle, worklist, 0, 0,
3619 			    protocol, B_TRUE);
3620 		}
3621 		ret = sa_update_config(handle);
3622 	}
3623 	if (worklist != NULL)
3624 		free_list(worklist);
3625 	return (ret);
3626 }
3627 
3628 /*
3629  * sa_set(flags, argc, argv)
3630  *
3631  * Implements the set subcommand. It keys off of -S to determine which
3632  * set of operations to actually do.
3633  */
3634 
3635 int
3636 sa_set(sa_handle_t handle, int flags, int argc, char *argv[])
3637 {
3638 	char *groupname;
3639 	int verbose = 0;
3640 	int dryrun = 0;
3641 	int c;
3642 	char *protocol = NULL;
3643 	int ret = SA_OK;
3644 	struct options *optlist = NULL;
3645 	char *rsrcname = NULL;
3646 	char *sharepath = NULL;
3647 	char *optset = NULL;
3648 	int auth;
3649 
3650 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
3651 		switch (c) {
3652 		case 'v':
3653 			verbose++;
3654 			break;
3655 		case 'n':
3656 			dryrun++;
3657 			break;
3658 		case 'P':
3659 			if (protocol != NULL) {
3660 				(void) printf(gettext(
3661 				    "Specifying multiple protocols "
3662 				    "not supported: %s\n"), protocol);
3663 				return (SA_SYNTAX_ERR);
3664 			}
3665 			protocol = optarg;
3666 			if (!sa_valid_protocol(protocol)) {
3667 				(void) printf(gettext(
3668 				    "Invalid protocol specified: %s\n"),
3669 				    protocol);
3670 				return (SA_INVALID_PROTOCOL);
3671 			}
3672 			break;
3673 		case 'p':
3674 			ret = add_opt(&optlist, optarg, 0);
3675 			switch (ret) {
3676 			case OPT_ADD_SYNTAX:
3677 				(void) printf(gettext("Property syntax error:"
3678 				    " %s\n"), optarg);
3679 				return (SA_SYNTAX_ERR);
3680 			case OPT_ADD_MEMORY:
3681 				(void) printf(gettext("No memory to set "
3682 				    "property: %s\n"), optarg);
3683 				return (SA_NO_MEMORY);
3684 			default:
3685 				break;
3686 			}
3687 			break;
3688 		case 'r':
3689 			if (rsrcname != NULL) {
3690 				(void) printf(gettext(
3691 				    "Setting multiple resource names not"
3692 				    " supported\n"));
3693 				return (SA_SYNTAX_ERR);
3694 			}
3695 			rsrcname = optarg;
3696 			break;
3697 		case 's':
3698 			if (sharepath != NULL) {
3699 				(void) printf(gettext(
3700 				    "Setting multiple shares not supported\n"));
3701 				return (SA_SYNTAX_ERR);
3702 			}
3703 			sharepath = optarg;
3704 			break;
3705 		case 'S':
3706 			if (optset != NULL) {
3707 				(void) printf(gettext(
3708 				    "Specifying multiple property "
3709 				    "spaces not supported: %s\n"), optset);
3710 				return (SA_SYNTAX_ERR);
3711 			}
3712 			optset = optarg;
3713 			break;
3714 		default:
3715 		case 'h':
3716 		case '?':
3717 			(void) printf(gettext("usage: %s\n"),
3718 			    sa_get_usage(USAGE_SET));
3719 			return (SA_OK);
3720 		}
3721 	}
3722 
3723 	if (optlist != NULL)
3724 		ret = chk_opt(optlist, optset != NULL, protocol);
3725 
3726 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
3727 	    protocol == NULL || ret != OPT_ADD_OK) {
3728 		char *sep = "\t";
3729 
3730 		(void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET));
3731 		if (optind >= argc) {
3732 			(void) printf(gettext("%sgroup must be specified"),
3733 			    sep);
3734 			sep = ", ";
3735 		}
3736 		if (optlist == NULL) {
3737 			(void) printf(gettext("%sat least one property must be"
3738 			    " specified"), sep);
3739 			sep = ", ";
3740 		}
3741 		if (protocol == NULL) {
3742 			(void) printf(gettext("%sprotocol must be specified"),
3743 			    sep);
3744 			sep = ", ";
3745 		}
3746 		(void) printf("\n");
3747 		ret = SA_SYNTAX_ERR;
3748 	} else {
3749 		/*
3750 		 * Group already exists so we can proceed after a few
3751 		 * additional checks related to ZFS handling.
3752 		 */
3753 
3754 		groupname = argv[optind];
3755 		if (strcmp(groupname, "zfs") == 0) {
3756 			(void) printf(gettext("Changing properties for group "
3757 			    "\"zfs\" not allowed\n"));
3758 			return (SA_NOT_ALLOWED);
3759 		}
3760 
3761 		auth = check_authorizations(groupname, flags);
3762 		if (optset == NULL)
3763 			ret = basic_set(handle, groupname, optlist, protocol,
3764 			    sharepath, rsrcname, dryrun);
3765 		else
3766 			ret = space_set(handle, groupname, optlist, protocol,
3767 			    sharepath, dryrun, optset);
3768 		if (dryrun && ret == SA_OK && !auth && verbose) {
3769 			(void) printf(gettext("Command would fail: %s\n"),
3770 			    sa_errorstr(SA_NO_PERMISSION));
3771 		}
3772 	}
3773 	return (ret);
3774 }
3775 
3776 /*
3777  * remove_options(group, optlist, proto, *err)
3778  *
3779  * Helper function to actually remove options from a group after all
3780  * preprocessing is done.
3781  */
3782 
3783 static int
3784 remove_options(sa_group_t group, struct options *optlist,
3785     char *proto, int *err)
3786 {
3787 	struct options *cur;
3788 	sa_optionset_t optionset;
3789 	sa_property_t prop;
3790 	int change = 0;
3791 	int ret = SA_OK;
3792 
3793 	optionset = sa_get_optionset(group, proto);
3794 	if (optionset != NULL) {
3795 		for (cur = optlist; cur != NULL; cur = cur->next) {
3796 			prop = sa_get_property(optionset, cur->optname);
3797 			if (prop != NULL) {
3798 				ret = sa_remove_property(prop);
3799 				if (ret != SA_OK)
3800 					break;
3801 				change = 1;
3802 			}
3803 		}
3804 	}
3805 	if (ret == SA_OK && change)
3806 		ret = sa_commit_properties(optionset, 0);
3807 
3808 	if (err != NULL)
3809 		*err = ret;
3810 	return (change);
3811 }
3812 
3813 /*
3814  * valid_unset(group, optlist, proto)
3815  *
3816  * Sanity check the optlist to make sure they can be removed. Issue an
3817  * error if a property doesn't exist.
3818  */
3819 
3820 static int
3821 valid_unset(sa_group_t group, struct options *optlist, char *proto)
3822 {
3823 	struct options *cur;
3824 	sa_optionset_t optionset;
3825 	sa_property_t prop;
3826 	int ret = SA_OK;
3827 
3828 	optionset = sa_get_optionset(group, proto);
3829 	if (optionset != NULL) {
3830 		for (cur = optlist; cur != NULL; cur = cur->next) {
3831 			prop = sa_get_property(optionset, cur->optname);
3832 			if (prop == NULL) {
3833 				(void) printf(gettext(
3834 				    "Could not unset property %s: not set\n"),
3835 				    cur->optname);
3836 				ret = SA_NO_SUCH_PROP;
3837 			}
3838 		}
3839 	}
3840 	return (ret);
3841 }
3842 
3843 /*
3844  * valid_unset_security(group, optlist, proto)
3845  *
3846  * Sanity check the optlist to make sure they can be removed. Issue an
3847  * error if a property doesn't exist.
3848  */
3849 
3850 static int
3851 valid_unset_security(sa_group_t group, struct options *optlist, char *proto,
3852     char *sectype)
3853 {
3854 	struct options *cur;
3855 	sa_security_t security;
3856 	sa_property_t prop;
3857 	int ret = SA_OK;
3858 	char *sec;
3859 
3860 	sec = sa_proto_space_alias(proto, sectype);
3861 	security = sa_get_security(group, sec, proto);
3862 	if (security != NULL) {
3863 		for (cur = optlist; cur != NULL; cur = cur->next) {
3864 			prop = sa_get_property(security, cur->optname);
3865 			if (prop == NULL) {
3866 				(void) printf(gettext(
3867 				    "Could not unset property %s: not set\n"),
3868 				    cur->optname);
3869 				ret = SA_NO_SUCH_PROP;
3870 			}
3871 		}
3872 	} else {
3873 		(void) printf(gettext(
3874 		    "Could not unset %s: space not defined\n"), sectype);
3875 		ret = SA_NO_SUCH_SECURITY;
3876 	}
3877 	if (sec != NULL)
3878 		sa_free_attr_string(sec);
3879 	return (ret);
3880 }
3881 
3882 /*
3883  * remove_security(group, optlist, proto)
3884  *
3885  * Remove the properties since they were checked as valid.
3886  */
3887 
3888 static int
3889 remove_security(sa_group_t group, char *sectype,
3890     struct options *optlist, char *proto, int *err)
3891 {
3892 	sa_security_t security;
3893 	int ret = SA_OK;
3894 	int change = 0;
3895 
3896 	sectype = sa_proto_space_alias(proto, sectype);
3897 	security = sa_get_security(group, sectype, proto);
3898 	if (sectype != NULL)
3899 		sa_free_attr_string(sectype);
3900 
3901 	if (security != NULL) {
3902 		while (optlist != NULL) {
3903 			sa_property_t prop;
3904 			prop = sa_get_property(security, optlist->optname);
3905 			if (prop != NULL) {
3906 				ret = sa_remove_property(prop);
3907 				if (ret != SA_OK)
3908 					break;
3909 				change = 1;
3910 			}
3911 			optlist = optlist->next;
3912 		}
3913 		/*
3914 		 * when done, properties may have all been removed but
3915 		 * we need to keep the security type itself until
3916 		 * explicitly removed.
3917 		 */
3918 		if (ret == SA_OK && change)
3919 			ret = sa_commit_properties(security, 0);
3920 	} else {
3921 		ret = SA_NO_SUCH_PROP;
3922 	}
3923 	if (err != NULL)
3924 		*err = ret;
3925 	return (change);
3926 }
3927 
3928 /*
3929  * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
3930  *
3931  * Unset non-named optionset properties.
3932  */
3933 
3934 static int
3935 basic_unset(sa_handle_t handle, char *groupname, struct options *optlist,
3936     char *protocol, char *sharepath, char *rsrcname, int dryrun)
3937 {
3938 	sa_group_t group;
3939 	int ret = SA_OK;
3940 	int change = 0;
3941 	struct list *worklist = NULL;
3942 	sa_share_t share = NULL;
3943 	sa_resource_t resource = NULL;
3944 
3945 	group = sa_get_group(handle, groupname);
3946 	if (group == NULL)
3947 		return (ret);
3948 
3949 	/*
3950 	 * If there is a sharepath, make sure it belongs to
3951 	 * the group.
3952 	 */
3953 	if (sharepath != NULL) {
3954 		share = sa_get_share(group, sharepath);
3955 		if (share == NULL) {
3956 			(void) printf(gettext(
3957 			    "Share does not exist in group %s\n"),
3958 			    groupname, sharepath);
3959 			ret = SA_NO_SUCH_PATH;
3960 		}
3961 	}
3962 	/*
3963 	 * If a resource name exists, make sure it belongs to
3964 	 * the share if present else it belongs to the
3965 	 * group. Also check the protocol to see if it
3966 	 * supports resource level properties or not. If not,
3967 	 * use share only.
3968 	 */
3969 	if (rsrcname != NULL) {
3970 		if (share != NULL) {
3971 			resource = sa_get_share_resource(share, rsrcname);
3972 			if (resource == NULL)
3973 				ret = SA_NO_SUCH_RESOURCE;
3974 		} else {
3975 			resource = sa_get_resource(group, rsrcname);
3976 			if (resource != NULL) {
3977 				share = sa_get_resource_parent(resource);
3978 			} else {
3979 				ret = SA_NO_SUCH_RESOURCE;
3980 			}
3981 		}
3982 		if (ret == SA_OK && resource != NULL) {
3983 			uint64_t features;
3984 			/*
3985 			 * Check to see if the resource can take
3986 			 * properties. If so, stick the resource into
3987 			 * "share" so it will all just work.
3988 			 */
3989 			features = sa_proto_get_featureset(protocol);
3990 			if (features & SA_FEATURE_RESOURCE)
3991 				share = (sa_share_t)resource;
3992 		}
3993 	}
3994 
3995 	if (ret == SA_OK) {
3996 		/* group must exist */
3997 		ret = valid_unset(share != NULL ? share : group,
3998 		    optlist, protocol);
3999 		if (ret == SA_OK && !dryrun) {
4000 			if (share != NULL) {
4001 				sa_optionset_t optionset;
4002 				sa_property_t prop;
4003 				change |= remove_options(share, optlist,
4004 				    protocol, &ret);
4005 				/*
4006 				 * If a share optionset is
4007 				 * empty, remove it.
4008 				 */
4009 				optionset = sa_get_optionset((sa_share_t)share,
4010 				    protocol);
4011 				if (optionset != NULL) {
4012 					prop = sa_get_property(optionset, NULL);
4013 					if (prop == NULL)
4014 						(void) sa_destroy_optionset(
4015 						    optionset);
4016 				}
4017 			} else {
4018 				change |= remove_options(group,
4019 				    optlist, protocol, &ret);
4020 			}
4021 			if (ret == SA_OK && change)
4022 				worklist = add_list(worklist, group, share,
4023 				    protocol);
4024 			if (ret != SA_OK)
4025 				(void) printf(gettext(
4026 				    "Could not remove properties: "
4027 				    "%s\n"), sa_errorstr(ret));
4028 		}
4029 	} else {
4030 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
4031 		ret = SA_NO_SUCH_GROUP;
4032 	}
4033 	free_opt(optlist);
4034 
4035 	/*
4036 	 * We have a group and potentially legal additions
4037 	 *
4038 	 * Commit to configuration if not a dryrun
4039 	 */
4040 	if (!dryrun && ret == SA_OK) {
4041 		if (change && worklist != NULL) {
4042 			/* properties changed, so update all shares */
4043 			(void) enable_all_groups(handle, worklist, 0, 0,
4044 			    protocol, B_TRUE);
4045 		}
4046 	}
4047 	if (worklist != NULL)
4048 		free_list(worklist);
4049 	return (ret);
4050 }
4051 
4052 /*
4053  * space_unset(groupname, optlist, protocol, sharepath, dryrun)
4054  *
4055  * Unset named optionset properties.
4056  */
4057 static int
4058 space_unset(sa_handle_t handle, char *groupname, struct options *optlist,
4059     char *protocol, char *sharepath, int dryrun, char *sectype)
4060 {
4061 	sa_group_t group;
4062 	int ret = SA_OK;
4063 	int change = 0;
4064 	struct list *worklist = NULL;
4065 	sa_share_t share = NULL;
4066 
4067 	group = sa_get_group(handle, groupname);
4068 	if (group == NULL) {
4069 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
4070 		return (SA_NO_SUCH_GROUP);
4071 	}
4072 	if (sharepath != NULL) {
4073 		share = sa_get_share(group, sharepath);
4074 		if (share == NULL) {
4075 			(void) printf(gettext(
4076 			    "Share does not exist in group %s\n"),
4077 			    groupname, sharepath);
4078 			return (SA_NO_SUCH_PATH);
4079 		}
4080 	}
4081 	ret = valid_unset_security(share != NULL ? share : group,
4082 	    optlist, protocol, sectype);
4083 
4084 	if (ret == SA_OK && !dryrun) {
4085 		if (optlist != NULL) {
4086 			if (share != NULL) {
4087 				sa_security_t optionset;
4088 				sa_property_t prop;
4089 				change = remove_security(share,
4090 				    sectype, optlist, protocol, &ret);
4091 
4092 				/* If a share security is empty, remove it */
4093 				optionset = sa_get_security((sa_group_t)share,
4094 				    sectype, protocol);
4095 				if (optionset != NULL) {
4096 					prop = sa_get_property(optionset,
4097 					    NULL);
4098 					if (prop == NULL)
4099 						ret = sa_destroy_security(
4100 						    optionset);
4101 				}
4102 			} else {
4103 				change = remove_security(group, sectype,
4104 				    optlist, protocol, &ret);
4105 			}
4106 		} else {
4107 			sa_security_t security;
4108 			char *sec;
4109 			sec = sa_proto_space_alias(protocol, sectype);
4110 			security = sa_get_security(group, sec, protocol);
4111 			if (sec != NULL)
4112 				sa_free_attr_string(sec);
4113 			if (security != NULL) {
4114 				ret = sa_destroy_security(security);
4115 				if (ret == SA_OK)
4116 					change = 1;
4117 			} else {
4118 				ret = SA_NO_SUCH_PROP;
4119 			}
4120 		}
4121 		if (ret != SA_OK)
4122 			(void) printf(gettext("Could not unset property: %s\n"),
4123 			    sa_errorstr(ret));
4124 	}
4125 
4126 	if (ret == SA_OK && change)
4127 		worklist = add_list(worklist, group, 0, protocol);
4128 
4129 	free_opt(optlist);
4130 	/*
4131 	 * We have a group and potentially legal additions
4132 	 */
4133 
4134 	/* Commit to configuration if not a dryrun */
4135 	if (!dryrun && ret == 0) {
4136 		/* properties changed, so update all shares */
4137 		if (change && worklist != NULL)
4138 			(void) enable_all_groups(handle, worklist, 0, 0,
4139 			    protocol, B_TRUE);
4140 		ret = sa_update_config(handle);
4141 	}
4142 	if (worklist != NULL)
4143 		free_list(worklist);
4144 	return (ret);
4145 }
4146 
4147 /*
4148  * sa_unset(flags, argc, argv)
4149  *
4150  * Implements the unset subcommand. Parsing done here and then basic
4151  * or space versions of the real code are called.
4152  */
4153 
4154 int
4155 sa_unset(sa_handle_t handle, int flags, int argc, char *argv[])
4156 {
4157 	char *groupname;
4158 	int verbose = 0;
4159 	int dryrun = 0;
4160 	int c;
4161 	char *protocol = NULL;
4162 	int ret = SA_OK;
4163 	struct options *optlist = NULL;
4164 	char *rsrcname = NULL;
4165 	char *sharepath = NULL;
4166 	char *optset = NULL;
4167 	int auth;
4168 
4169 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
4170 		switch (c) {
4171 		case 'v':
4172 			verbose++;
4173 			break;
4174 		case 'n':
4175 			dryrun++;
4176 			break;
4177 		case 'P':
4178 			if (protocol != NULL) {
4179 				(void) printf(gettext(
4180 				    "Specifying multiple protocols "
4181 				    "not supported: %s\n"), protocol);
4182 				return (SA_SYNTAX_ERR);
4183 			}
4184 			protocol = optarg;
4185 			if (!sa_valid_protocol(protocol)) {
4186 				(void) printf(gettext(
4187 				    "Invalid protocol specified: %s\n"),
4188 				    protocol);
4189 				return (SA_INVALID_PROTOCOL);
4190 			}
4191 			break;
4192 		case 'p':
4193 			ret = add_opt(&optlist, optarg, 1);
4194 			switch (ret) {
4195 			case OPT_ADD_SYNTAX:
4196 				(void) printf(gettext("Property syntax error "
4197 				    "for property %s\n"), optarg);
4198 				return (SA_SYNTAX_ERR);
4199 
4200 			case OPT_ADD_PROPERTY:
4201 				(void) printf(gettext("Properties need to be "
4202 				    "set with set command: %s\n"), optarg);
4203 				return (SA_SYNTAX_ERR);
4204 
4205 			default:
4206 				break;
4207 			}
4208 			break;
4209 		case 'r':
4210 			/*
4211 			 * Unset properties on resource if applicable or on
4212 			 * share if resource for this protocol doesn't use
4213 			 * resources.
4214 			 */
4215 			if (rsrcname != NULL) {
4216 				(void) printf(gettext(
4217 				    "Unsetting multiple resource "
4218 				    "names not supported\n"));
4219 				return (SA_SYNTAX_ERR);
4220 			}
4221 			rsrcname = optarg;
4222 			break;
4223 		case 's':
4224 			if (sharepath != NULL) {
4225 				(void) printf(gettext(
4226 				    "Adding multiple shares not supported\n"));
4227 				return (SA_SYNTAX_ERR);
4228 			}
4229 			sharepath = optarg;
4230 			break;
4231 		case 'S':
4232 			if (optset != NULL) {
4233 				(void) printf(gettext(
4234 				    "Specifying multiple property "
4235 				    "spaces not supported: %s\n"), optset);
4236 				return (SA_SYNTAX_ERR);
4237 			}
4238 			optset = optarg;
4239 			break;
4240 		default:
4241 		case 'h':
4242 		case '?':
4243 			(void) printf(gettext("usage: %s\n"),
4244 			    sa_get_usage(USAGE_UNSET));
4245 			return (SA_OK);
4246 		}
4247 	}
4248 
4249 	if (optlist != NULL)
4250 		ret = chk_opt(optlist, optset != NULL, protocol);
4251 
4252 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
4253 	    protocol == NULL) {
4254 		char *sep = "\t";
4255 		(void) printf(gettext("usage: %s\n"),
4256 		    sa_get_usage(USAGE_UNSET));
4257 		if (optind >= argc) {
4258 			(void) printf(gettext("%sgroup must be specified"),
4259 			    sep);
4260 			sep = ", ";
4261 		}
4262 		if (optlist == NULL) {
4263 			(void) printf(gettext("%sat least one property must "
4264 			    "be specified"), sep);
4265 			sep = ", ";
4266 		}
4267 		if (protocol == NULL) {
4268 			(void) printf(gettext("%sprotocol must be specified"),
4269 			    sep);
4270 			sep = ", ";
4271 		}
4272 		(void) printf("\n");
4273 		ret = SA_SYNTAX_ERR;
4274 	} else {
4275 
4276 		/*
4277 		 * If a group already exists, we can only add a new
4278 		 * protocol to it and not create a new one or add the
4279 		 * same protocol again.
4280 		 */
4281 
4282 		groupname = argv[optind];
4283 		auth = check_authorizations(groupname, flags);
4284 		if (optset == NULL)
4285 			ret = basic_unset(handle, groupname, optlist, protocol,
4286 			    sharepath, rsrcname, dryrun);
4287 		else
4288 			ret = space_unset(handle, groupname, optlist, protocol,
4289 			    sharepath, dryrun, optset);
4290 
4291 		if (dryrun && ret == SA_OK && !auth && verbose)
4292 			(void) printf(gettext("Command would fail: %s\n"),
4293 			    sa_errorstr(SA_NO_PERMISSION));
4294 	}
4295 	return (ret);
4296 }
4297 
4298 /*
4299  * sa_enable_group(flags, argc, argv)
4300  *
4301  * Implements the enable subcommand
4302  */
4303 
4304 int
4305 sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[])
4306 {
4307 	int verbose = 0;
4308 	int dryrun = 0;
4309 	int all = 0;
4310 	int c;
4311 	int ret = SA_OK;
4312 	char *protocol = NULL;
4313 	char *state;
4314 	struct list *worklist = NULL;
4315 	int auth = 1;
4316 	sa_group_t group;
4317 
4318 	while ((c = getopt(argc, argv, "?havnP:")) != EOF) {
4319 		switch (c) {
4320 		case 'a':
4321 			all = 1;
4322 			break;
4323 		case 'n':
4324 			dryrun++;
4325 			break;
4326 		case 'P':
4327 			if (protocol != NULL) {
4328 				(void) printf(gettext(
4329 				    "Specifying multiple protocols "
4330 				    "not supported: %s\n"), protocol);
4331 				return (SA_SYNTAX_ERR);
4332 			}
4333 			protocol = optarg;
4334 			if (!sa_valid_protocol(protocol)) {
4335 				(void) printf(gettext(
4336 				    "Invalid protocol specified: %s\n"),
4337 				    protocol);
4338 				return (SA_INVALID_PROTOCOL);
4339 			}
4340 			break;
4341 		case 'v':
4342 			verbose++;
4343 			break;
4344 		default:
4345 		case 'h':
4346 		case '?':
4347 			(void) printf(gettext("usage: %s\n"),
4348 			    sa_get_usage(USAGE_ENABLE));
4349 			return (0);
4350 		}
4351 	}
4352 
4353 	if (optind == argc && !all) {
4354 		(void) printf(gettext("usage: %s\n"),
4355 		    sa_get_usage(USAGE_ENABLE));
4356 		(void) printf(gettext("\tmust specify group\n"));
4357 		return (SA_NO_SUCH_PATH);
4358 	}
4359 	if (!all) {
4360 		while (optind < argc) {
4361 			group = sa_get_group(handle, argv[optind]);
4362 			if (group != NULL) {
4363 				auth &= check_authorizations(argv[optind],
4364 				    flags);
4365 				state = sa_get_group_attr(group, "state");
4366 				if (state != NULL &&
4367 				    strcmp(state, "enabled") == 0) {
4368 					/* already enabled */
4369 					if (verbose)
4370 						(void) printf(gettext(
4371 						    "Group \"%s\" is already "
4372 						    "enabled\n"),
4373 						    argv[optind]);
4374 					ret = SA_BUSY; /* already enabled */
4375 				} else {
4376 					worklist = add_list(worklist, group,
4377 					    0, protocol);
4378 					if (verbose)
4379 						(void) printf(gettext(
4380 						    "Enabling group \"%s\"\n"),
4381 						    argv[optind]);
4382 				}
4383 				if (state != NULL)
4384 					sa_free_attr_string(state);
4385 			} else {
4386 				ret = SA_NO_SUCH_GROUP;
4387 			}
4388 			optind++;
4389 		}
4390 	} else {
4391 		for (group = sa_get_group(handle, NULL);
4392 		    group != NULL;
4393 		    group = sa_get_next_group(group)) {
4394 			worklist = add_list(worklist, group, 0, protocol);
4395 		}
4396 	}
4397 	if (!dryrun && ret == SA_OK)
4398 		ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE);
4399 
4400 	if (ret != SA_OK && ret != SA_BUSY)
4401 		(void) printf(gettext("Could not enable group: %s\n"),
4402 		    sa_errorstr(ret));
4403 	if (ret == SA_BUSY)
4404 		ret = SA_OK;
4405 
4406 	if (worklist != NULL)
4407 		free_list(worklist);
4408 	if (dryrun && ret == SA_OK && !auth && verbose) {
4409 		(void) printf(gettext("Command would fail: %s\n"),
4410 		    sa_errorstr(SA_NO_PERMISSION));
4411 	}
4412 	return (ret);
4413 }
4414 
4415 /*
4416  * disable_group(group, proto)
4417  *
4418  * Disable all the shares in the specified group.. This is a helper
4419  * for disable_all_groups in order to simplify regular and subgroup
4420  * (zfs) disabling. Group has already been checked for non-NULL.
4421  */
4422 
4423 static int
4424 disable_group(sa_group_t group, char *proto)
4425 {
4426 	sa_share_t share;
4427 	int ret = SA_OK;
4428 
4429 	/*
4430 	 * If the protocol isn't enabled, skip it and treat as
4431 	 * successful.
4432 	 */
4433 	if (!has_protocol(group, proto))
4434 		return (ret);
4435 
4436 	for (share = sa_get_share(group, NULL);
4437 	    share != NULL && ret == SA_OK;
4438 	    share = sa_get_next_share(share)) {
4439 		ret = sa_disable_share(share, proto);
4440 		if (ret == SA_NO_SUCH_PATH) {
4441 			/*
4442 			 * this is OK since the path is gone. we can't
4443 			 * re-share it anyway so no error.
4444 			 */
4445 			ret = SA_OK;
4446 		}
4447 	}
4448 	return (ret);
4449 }
4450 
4451 /*
4452  * disable_all_groups(work, setstate)
4453  *
4454  * helper function that disables the shares in the list of groups
4455  * provided. It optionally marks the group as disabled. Used by both
4456  * enable and start subcommands.
4457  */
4458 
4459 static int
4460 disable_all_groups(sa_handle_t handle, struct list *work, int setstate)
4461 {
4462 	int ret = SA_OK;
4463 	sa_group_t subgroup, group;
4464 
4465 	while (work != NULL && ret == SA_OK) {
4466 		group = (sa_group_t)work->item;
4467 		if (setstate)
4468 			ret = sa_set_group_attr(group, "state", "disabled");
4469 		if (ret == SA_OK) {
4470 			char *name;
4471 			name = sa_get_group_attr(group, "name");
4472 			if (name != NULL && strcmp(name, "zfs") == 0) {
4473 				/* need to get the sub-groups for stopping */
4474 				for (subgroup = sa_get_sub_group(group);
4475 				    subgroup != NULL;
4476 				    subgroup = sa_get_next_group(subgroup)) {
4477 					ret = disable_group(subgroup,
4478 					    work->proto);
4479 				}
4480 			} else {
4481 				ret = disable_group(group, work->proto);
4482 			}
4483 			/*
4484 			 * We don't want to "disable" since it won't come
4485 			 * up after a reboot.  The SMF framework should do
4486 			 * the right thing. On enable we do want to do
4487 			 * something.
4488 			 */
4489 		}
4490 		work = work->next;
4491 	}
4492 	if (ret == SA_OK)
4493 		ret = sa_update_config(handle);
4494 	return (ret);
4495 }
4496 
4497 /*
4498  * sa_disable_group(flags, argc, argv)
4499  *
4500  * Implements the disable subcommand
4501  */
4502 
4503 int
4504 sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[])
4505 {
4506 	int verbose = 0;
4507 	int dryrun = 0;
4508 	int all = 0;
4509 	int c;
4510 	int ret = SA_OK;
4511 	char *protocol = NULL;
4512 	char *state;
4513 	struct list *worklist = NULL;
4514 	sa_group_t group;
4515 	int auth = 1;
4516 
4517 	while ((c = getopt(argc, argv, "?havn")) != EOF) {
4518 		switch (c) {
4519 		case 'a':
4520 			all = 1;
4521 			break;
4522 		case 'n':
4523 			dryrun++;
4524 			break;
4525 		case 'P':
4526 			if (protocol != NULL) {
4527 				(void) printf(gettext(
4528 				    "Specifying multiple protocols "
4529 				    "not supported: %s\n"), protocol);
4530 				return (SA_SYNTAX_ERR);
4531 			}
4532 			protocol = optarg;
4533 			if (!sa_valid_protocol(protocol)) {
4534 				(void) printf(gettext(
4535 				    "Invalid protocol specified: %s\n"),
4536 				    protocol);
4537 				return (SA_INVALID_PROTOCOL);
4538 			}
4539 			break;
4540 		case 'v':
4541 			verbose++;
4542 			break;
4543 		default:
4544 		case 'h':
4545 		case '?':
4546 			(void) printf(gettext("usage: %s\n"),
4547 			    sa_get_usage(USAGE_DISABLE));
4548 			return (0);
4549 		}
4550 	}
4551 
4552 	if (optind == argc && !all) {
4553 		(void) printf(gettext("usage: %s\n"),
4554 		    sa_get_usage(USAGE_DISABLE));
4555 		(void) printf(gettext("\tmust specify group\n"));
4556 		return (SA_NO_SUCH_PATH);
4557 	}
4558 	if (!all) {
4559 		while (optind < argc) {
4560 			group = sa_get_group(handle, argv[optind]);
4561 			if (group != NULL) {
4562 				auth &= check_authorizations(argv[optind],
4563 				    flags);
4564 				state = sa_get_group_attr(group, "state");
4565 				if (state == NULL ||
4566 				    strcmp(state, "disabled") == 0) {
4567 					/* already disabled */
4568 					if (verbose)
4569 						(void) printf(gettext(
4570 						    "Group \"%s\" is "
4571 						    "already disabled\n"),
4572 						    argv[optind]);
4573 					ret = SA_BUSY; /* already disabled */
4574 				} else {
4575 					worklist = add_list(worklist, group, 0,
4576 					    protocol);
4577 					if (verbose)
4578 						(void) printf(gettext(
4579 						    "Disabling group "
4580 						    "\"%s\"\n"), argv[optind]);
4581 				}
4582 				if (state != NULL)
4583 					sa_free_attr_string(state);
4584 			} else {
4585 				ret = SA_NO_SUCH_GROUP;
4586 			}
4587 			optind++;
4588 		}
4589 	} else {
4590 		for (group = sa_get_group(handle, NULL);
4591 		    group != NULL;
4592 		    group = sa_get_next_group(group))
4593 			worklist = add_list(worklist, group, 0, protocol);
4594 	}
4595 
4596 	if (ret == SA_OK && !dryrun)
4597 		ret = disable_all_groups(handle, worklist, 1);
4598 	if (ret != SA_OK && ret != SA_BUSY)
4599 		(void) printf(gettext("Could not disable group: %s\n"),
4600 		    sa_errorstr(ret));
4601 	if (ret == SA_BUSY)
4602 		ret = SA_OK;
4603 	if (worklist != NULL)
4604 		free_list(worklist);
4605 	if (dryrun && ret == SA_OK && !auth && verbose)
4606 		(void) printf(gettext("Command would fail: %s\n"),
4607 		    sa_errorstr(SA_NO_PERMISSION));
4608 	return (ret);
4609 }
4610 
4611 /*
4612  * sa_start_group(flags, argc, argv)
4613  *
4614  * Implements the start command.
4615  * This is similar to enable except it doesn't change the state
4616  * of the group(s) and only enables shares if the group is already
4617  * enabled.
4618  */
4619 
4620 int
4621 sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[])
4622 {
4623 	int verbose = 0;
4624 	int all = 0;
4625 	int c;
4626 	int ret = SMF_EXIT_OK;
4627 	char *protocol = NULL;
4628 	char *state;
4629 	struct list *worklist = NULL;
4630 	sa_group_t group;
4631 #ifdef lint
4632 	flags = flags;
4633 #endif
4634 
4635 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
4636 		switch (c) {
4637 		case 'a':
4638 			all = 1;
4639 			break;
4640 		case 'P':
4641 			if (protocol != NULL) {
4642 				(void) printf(gettext(
4643 				    "Specifying multiple protocols "
4644 				    "not supported: %s\n"), protocol);
4645 				return (SA_SYNTAX_ERR);
4646 			}
4647 			protocol = optarg;
4648 			if (!sa_valid_protocol(protocol)) {
4649 				(void) printf(gettext(
4650 				    "Invalid protocol specified: %s\n"),
4651 				    protocol);
4652 				return (SA_INVALID_PROTOCOL);
4653 			}
4654 			break;
4655 		case 'v':
4656 			verbose++;
4657 			break;
4658 		default:
4659 		case 'h':
4660 		case '?':
4661 			(void) printf(gettext("usage: %s\n"),
4662 			    sa_get_usage(USAGE_START));
4663 			return (SA_OK);
4664 		}
4665 	}
4666 
4667 	if (optind == argc && !all) {
4668 		(void) printf(gettext("usage: %s\n"),
4669 		    sa_get_usage(USAGE_START));
4670 		return (SMF_EXIT_ERR_FATAL);
4671 	}
4672 
4673 	if (!all) {
4674 		while (optind < argc) {
4675 			group = sa_get_group(handle, argv[optind]);
4676 			if (group != NULL) {
4677 				state = sa_get_group_attr(group, "state");
4678 				if (state == NULL ||
4679 				    strcmp(state, "enabled") == 0) {
4680 					worklist = add_list(worklist, group, 0,
4681 					    protocol);
4682 					if (verbose)
4683 						(void) printf(gettext(
4684 						    "Starting group \"%s\"\n"),
4685 						    argv[optind]);
4686 				} else {
4687 					/*
4688 					 * Determine if there are any
4689 					 * protocols.  If there aren't any,
4690 					 * then there isn't anything to do in
4691 					 * any case so no error.
4692 					 */
4693 					if (sa_get_optionset(group,
4694 					    protocol) != NULL) {
4695 						ret = SMF_EXIT_OK;
4696 					}
4697 				}
4698 				if (state != NULL)
4699 					sa_free_attr_string(state);
4700 			}
4701 			optind++;
4702 		}
4703 	} else {
4704 		for (group = sa_get_group(handle, NULL);
4705 		    group != NULL;
4706 		    group = sa_get_next_group(group)) {
4707 			state = sa_get_group_attr(group, "state");
4708 			if (state == NULL || strcmp(state, "enabled") == 0)
4709 				worklist = add_list(worklist, group, 0,
4710 				    protocol);
4711 			if (state != NULL)
4712 				sa_free_attr_string(state);
4713 		}
4714 	}
4715 
4716 	(void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE);
4717 
4718 	if (worklist != NULL)
4719 		free_list(worklist);
4720 	return (ret);
4721 }
4722 
4723 /*
4724  * sa_stop_group(flags, argc, argv)
4725  *
4726  * Implements the stop command.
4727  * This is similar to disable except it doesn't change the state
4728  * of the group(s) and only disables shares if the group is already
4729  * enabled.
4730  */
4731 int
4732 sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[])
4733 {
4734 	int verbose = 0;
4735 	int all = 0;
4736 	int c;
4737 	int ret = SMF_EXIT_OK;
4738 	char *protocol = NULL;
4739 	char *state;
4740 	struct list *worklist = NULL;
4741 	sa_group_t group;
4742 #ifdef lint
4743 	flags = flags;
4744 #endif
4745 
4746 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
4747 		switch (c) {
4748 		case 'a':
4749 			all = 1;
4750 			break;
4751 		case 'P':
4752 			if (protocol != NULL) {
4753 				(void) printf(gettext(
4754 				    "Specifying multiple protocols "
4755 				    "not supported: %s\n"), protocol);
4756 				return (SA_SYNTAX_ERR);
4757 			}
4758 			protocol = optarg;
4759 			if (!sa_valid_protocol(protocol)) {
4760 				(void) printf(gettext(
4761 				    "Invalid protocol specified: %s\n"),
4762 				    protocol);
4763 				return (SA_INVALID_PROTOCOL);
4764 			}
4765 			break;
4766 		case 'v':
4767 			verbose++;
4768 			break;
4769 		default:
4770 		case 'h':
4771 		case '?':
4772 			(void) printf(gettext("usage: %s\n"),
4773 			    sa_get_usage(USAGE_STOP));
4774 			return (0);
4775 		}
4776 	}
4777 
4778 	if (optind == argc && !all) {
4779 		(void) printf(gettext("usage: %s\n"),
4780 		    sa_get_usage(USAGE_STOP));
4781 		return (SMF_EXIT_ERR_FATAL);
4782 	} else if (!all) {
4783 		while (optind < argc) {
4784 			group = sa_get_group(handle, argv[optind]);
4785 			if (group != NULL) {
4786 				state = sa_get_group_attr(group, "state");
4787 				if (state == NULL ||
4788 				    strcmp(state, "enabled") == 0) {
4789 					worklist = add_list(worklist, group, 0,
4790 					    protocol);
4791 					if (verbose)
4792 						(void) printf(gettext(
4793 						    "Stopping group \"%s\"\n"),
4794 						    argv[optind]);
4795 				} else {
4796 					ret = SMF_EXIT_OK;
4797 				}
4798 				if (state != NULL)
4799 					sa_free_attr_string(state);
4800 			}
4801 			optind++;
4802 		}
4803 	} else {
4804 		for (group = sa_get_group(handle, NULL);
4805 		    group != NULL;
4806 		    group = sa_get_next_group(group)) {
4807 			state = sa_get_group_attr(group, "state");
4808 			if (state == NULL || strcmp(state, "enabled") == 0)
4809 				worklist = add_list(worklist, group, 0,
4810 				    protocol);
4811 			if (state != NULL)
4812 				sa_free_attr_string(state);
4813 		}
4814 	}
4815 	(void) disable_all_groups(handle, worklist, 0);
4816 	ret = sa_update_config(handle);
4817 
4818 	if (worklist != NULL)
4819 		free_list(worklist);
4820 	return (ret);
4821 }
4822 
4823 /*
4824  * remove_all_options(share, proto)
4825  *
4826  * Removes all options on a share.
4827  */
4828 
4829 static void
4830 remove_all_options(sa_share_t share, char *proto)
4831 {
4832 	sa_optionset_t optionset;
4833 	sa_security_t security;
4834 	sa_security_t prevsec = NULL;
4835 
4836 	optionset = sa_get_optionset(share, proto);
4837 	if (optionset != NULL)
4838 		(void) sa_destroy_optionset(optionset);
4839 	for (security = sa_get_security(share, NULL, NULL);
4840 	    security != NULL;
4841 	    security = sa_get_next_security(security)) {
4842 		char *type;
4843 		/*
4844 		 * We walk through the list.  prevsec keeps the
4845 		 * previous security so we can delete it without
4846 		 * destroying the list.
4847 		 */
4848 		if (prevsec != NULL) {
4849 			/* remove the previously seen security */
4850 			(void) sa_destroy_security(prevsec);
4851 			/* set to NULL so we don't try multiple times */
4852 			prevsec = NULL;
4853 		}
4854 		type = sa_get_security_attr(security, "type");
4855 		if (type != NULL) {
4856 			/*
4857 			 * if the security matches the specified protocol, we
4858 			 * want to remove it. prevsec holds it until either
4859 			 * the next pass or we fall out of the loop.
4860 			 */
4861 			if (strcmp(type, proto) == 0)
4862 				prevsec = security;
4863 			sa_free_attr_string(type);
4864 		}
4865 	}
4866 	/* in case there is one left */
4867 	if (prevsec != NULL)
4868 		(void) sa_destroy_security(prevsec);
4869 }
4870 
4871 
4872 /*
4873  * for legacy support, we need to handle the old syntax. This is what
4874  * we get if sharemgr is called with the name "share" rather than
4875  * sharemgr.
4876  */
4877 
4878 static int
4879 format_legacy_path(char *buff, int buffsize, char *proto, char *cmd)
4880 {
4881 	int err;
4882 
4883 	err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd);
4884 	if (err > buffsize)
4885 		return (-1);
4886 	return (0);
4887 }
4888 
4889 
4890 /*
4891  * check_legacy_cmd(proto, cmd)
4892  *
4893  * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is
4894  * executable.
4895  */
4896 
4897 static int
4898 check_legacy_cmd(char *path)
4899 {
4900 	struct stat st;
4901 	int ret = 0;
4902 
4903 	if (stat(path, &st) == 0) {
4904 		if (S_ISREG(st.st_mode) &&
4905 		    st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
4906 			ret = 1;
4907 	}
4908 	return (ret);
4909 }
4910 
4911 /*
4912  * run_legacy_command(proto, cmd, argv)
4913  *
4914  * We know the command exists, so attempt to execute it with all the
4915  * arguments. This implements full legacy share support for those
4916  * protocols that don't have plugin providers.
4917  */
4918 
4919 static int
4920 run_legacy_command(char *path, char *argv[])
4921 {
4922 	int ret;
4923 
4924 	ret = execv(path, argv);
4925 	if (ret < 0) {
4926 		switch (errno) {
4927 		case EACCES:
4928 			ret = SA_NO_PERMISSION;
4929 			break;
4930 		default:
4931 			ret = SA_SYSTEM_ERR;
4932 			break;
4933 		}
4934 	}
4935 	return (ret);
4936 }
4937 
4938 /*
4939  * out_share(out, group, proto)
4940  *
4941  * Display the share information in the format that the "share"
4942  * command has traditionally used.
4943  */
4944 
4945 static void
4946 out_share(FILE *out, sa_group_t group, char *proto)
4947 {
4948 	sa_share_t share;
4949 	char resfmt[128];
4950 	char *defprop;
4951 
4952 	/*
4953 	 * The original share command defaulted to displaying NFS
4954 	 * shares or allowed a protocol to be specified. We want to
4955 	 * skip those shares that are not the specified protocol.
4956 	 */
4957 	if (proto != NULL && sa_get_optionset(group, proto) == NULL)
4958 		return;
4959 
4960 	if (proto == NULL)
4961 		proto = "nfs";
4962 
4963 	/*
4964 	 * get the default property string.  NFS uses "rw" but
4965 	 * everything else will use "".
4966 	 */
4967 	if (proto != NULL && strcmp(proto, "nfs") != 0)
4968 		defprop = "\"\"";
4969 	else
4970 		defprop = "rw";
4971 
4972 	for (share = sa_get_share(group, NULL);
4973 	    share != NULL;
4974 	    share = sa_get_next_share(share)) {
4975 		char *path;
4976 		char *type;
4977 		char *resource;
4978 		char *description;
4979 		char *groupname;
4980 		char *sharedstate;
4981 		int shared = 1;
4982 		char *soptions;
4983 		char shareopts[MAXNAMLEN];
4984 
4985 		sharedstate = sa_get_share_attr(share, "shared");
4986 		path = sa_get_share_attr(share, "path");
4987 		type = sa_get_share_attr(share, "type");
4988 		resource = get_resource(share);
4989 		groupname = sa_get_group_attr(group, "name");
4990 
4991 		if (groupname != NULL && strcmp(groupname, "default") == 0) {
4992 			sa_free_attr_string(groupname);
4993 			groupname = NULL;
4994 		}
4995 		description = sa_get_share_description(share);
4996 
4997 		/*
4998 		 * Want the sharetab version if it exists, defaulting
4999 		 * to NFS if no protocol specified.
5000 		 */
5001 		(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto);
5002 		soptions = sa_get_share_attr(share, shareopts);
5003 
5004 		if (sharedstate == NULL)
5005 			shared = 0;
5006 
5007 		if (soptions == NULL)
5008 			soptions = sa_proto_legacy_format(proto, share, 1);
5009 
5010 		if (shared) {
5011 			/* only active shares go here */
5012 			(void) snprintf(resfmt, sizeof (resfmt), "%s%s%s",
5013 			    resource != NULL ? resource : "-",
5014 			    groupname != NULL ? "@" : "",
5015 			    groupname != NULL ? groupname : "");
5016 			(void) fprintf(out, "%-14.14s  %s   %s   \"%s\"  \n",
5017 			    resfmt, path,
5018 			    (soptions != NULL && strlen(soptions) > 0) ?
5019 			    soptions : defprop,
5020 			    (description != NULL) ? description : "");
5021 		}
5022 
5023 		if (path != NULL)
5024 			sa_free_attr_string(path);
5025 		if (type != NULL)
5026 			sa_free_attr_string(type);
5027 		if (resource != NULL)
5028 			sa_free_attr_string(resource);
5029 		if (groupname != NULL)
5030 			sa_free_attr_string(groupname);
5031 		if (description != NULL)
5032 			sa_free_share_description(description);
5033 		if (sharedstate != NULL)
5034 			sa_free_attr_string(sharedstate);
5035 		if (soptions != NULL)
5036 			sa_format_free(soptions);
5037 	}
5038 }
5039 
5040 /*
5041  * output_legacy_file(out, proto)
5042  *
5043  * Walk all of the groups for the specified protocol and call
5044  * out_share() to format and write in the format displayed by the
5045  * "share" command with no arguments.
5046  */
5047 
5048 static void
5049 output_legacy_file(FILE *out, char *proto, sa_handle_t handle)
5050 {
5051 	sa_group_t group;
5052 
5053 	for (group = sa_get_group(handle, NULL);
5054 	    group != NULL;
5055 	    group = sa_get_next_group(group)) {
5056 		char *zfs;
5057 
5058 		/*
5059 		 * Go through all the groups and ZFS
5060 		 * sub-groups. out_share() will format the shares in
5061 		 * the group appropriately.
5062 		 */
5063 
5064 		zfs = sa_get_group_attr(group, "zfs");
5065 		if (zfs != NULL) {
5066 			sa_group_t zgroup;
5067 			sa_free_attr_string(zfs);
5068 			for (zgroup = sa_get_sub_group(group);
5069 			    zgroup != NULL;
5070 			    zgroup = sa_get_next_group(zgroup)) {
5071 
5072 				/* got a group, so display it */
5073 				out_share(out, zgroup, proto);
5074 			}
5075 		} else {
5076 			out_share(out, group, proto);
5077 		}
5078 	}
5079 }
5080 
5081 int
5082 sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[])
5083 {
5084 	char *protocol = "nfs";
5085 	char *options = NULL;
5086 	char *description = NULL;
5087 	char *groupname = NULL;
5088 	char *sharepath = NULL;
5089 	char *resource = NULL;
5090 	char *groupstatus = NULL;
5091 	int persist = SA_SHARE_TRANSIENT;
5092 	int argsused = 0;
5093 	int c;
5094 	int ret = SA_OK;
5095 	int zfs = 0;
5096 	int true_legacy = 0;
5097 	int curtype = SA_SHARE_TRANSIENT;
5098 	char cmd[MAXPATHLEN];
5099 	sa_group_t group = NULL;
5100 	sa_resource_t rsrc = NULL;
5101 	sa_share_t share;
5102 	char dir[MAXPATHLEN];
5103 	uint64_t features;
5104 #ifdef lint
5105 	flags = flags;
5106 #endif
5107 
5108 	while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) {
5109 		switch (c) {
5110 		case 'd':
5111 			description = optarg;
5112 			argsused++;
5113 			break;
5114 		case 'F':
5115 			protocol = optarg;
5116 			if (!sa_valid_protocol(protocol)) {
5117 				if (format_legacy_path(cmd, MAXPATHLEN,
5118 				    protocol, "share") == 0 &&
5119 				    check_legacy_cmd(cmd)) {
5120 					true_legacy++;
5121 				} else {
5122 					(void) fprintf(stderr, gettext(
5123 					    "Invalid protocol specified: "
5124 					    "%s\n"), protocol);
5125 					return (SA_INVALID_PROTOCOL);
5126 				}
5127 			}
5128 			break;
5129 		case 'o':
5130 			options = optarg;
5131 			argsused++;
5132 			break;
5133 		case 'p':
5134 			persist = SA_SHARE_PERMANENT;
5135 			argsused++;
5136 			break;
5137 		case 'h':
5138 		case '?':
5139 		default:
5140 			(void) fprintf(stderr, gettext("usage: %s\n"),
5141 			    sa_get_usage(USAGE_SHARE));
5142 			return (SA_OK);
5143 		}
5144 	}
5145 
5146 	/* Have the info so construct what is needed */
5147 	if (!argsused && optind == argc) {
5148 		/* display current info in share format */
5149 		(void) output_legacy_file(stdout, protocol, handle);
5150 		return (ret);
5151 	}
5152 
5153 	/* We are modifying the configuration */
5154 	if (optind == argc) {
5155 		(void) fprintf(stderr, gettext("usage: %s\n"),
5156 		    sa_get_usage(USAGE_SHARE));
5157 		return (SA_LEGACY_ERR);
5158 	}
5159 	if (true_legacy) {
5160 		/* If still using legacy share/unshare, exec it */
5161 		ret = run_legacy_command(cmd, argv);
5162 		return (ret);
5163 	}
5164 
5165 	sharepath = argv[optind++];
5166 	if (optind < argc) {
5167 		resource = argv[optind];
5168 		groupname = strchr(resource, '@');
5169 		if (groupname != NULL)
5170 			*groupname++ = '\0';
5171 	}
5172 	if (realpath(sharepath, dir) == NULL)
5173 		ret = SA_BAD_PATH;
5174 	else
5175 		sharepath = dir;
5176 	if (ret == SA_OK)
5177 		share = sa_find_share(handle, sharepath);
5178 	else
5179 		share = NULL;
5180 
5181 	features = sa_proto_get_featureset(protocol);
5182 
5183 	if (groupname != NULL) {
5184 		ret = SA_NOT_ALLOWED;
5185 	} else if (ret == SA_OK) {
5186 		char *legacygroup;
5187 		/*
5188 		 * The legacy group is always present and zfs groups
5189 		 * come and go.  zfs shares may be in sub-groups and
5190 		 * the zfs share will already be in that group so it
5191 		 * isn't an error. If the protocol is "smb", the group
5192 		 * "smb" is used when "default" would otherwise be
5193 		 * used.  "default" is NFS only and "smb" is SMB only.
5194 		 */
5195 		if (strcmp(protocol, "smb") == 0)
5196 			legacygroup = "smb";
5197 		else
5198 			legacygroup = "default";
5199 
5200 		/*
5201 		 * If the share exists (not NULL), then make sure it
5202 		 * is one we want to handle by getting the parent
5203 		 * group.
5204 		 */
5205 		if (share != NULL) {
5206 			group = sa_get_parent_group(share);
5207 		} else {
5208 			group = sa_get_group(handle, legacygroup);
5209 			if (group == NULL && strcmp(legacygroup, "smb") == 0) {
5210 				/*
5211 				 * This group may not exist, so create
5212 				 * as necessary. It only contains the
5213 				 * "smb" protocol.
5214 				 */
5215 				group = sa_create_group(handle, legacygroup,
5216 				    &ret);
5217 				if (group != NULL)
5218 					(void) sa_create_optionset(group,
5219 					    protocol);
5220 			}
5221 		}
5222 
5223 		if (group == NULL) {
5224 			ret = SA_SYSTEM_ERR;
5225 			goto err;
5226 		}
5227 
5228 		groupstatus = group_status(group);
5229 		if (share == NULL) {
5230 			share = sa_add_share(group, sharepath,
5231 			    persist, &ret);
5232 			if (share == NULL &&
5233 			    ret == SA_DUPLICATE_NAME) {
5234 				/*
5235 				 * Could be a ZFS path being started
5236 				 */
5237 				if (sa_zfs_is_shared(handle,
5238 				    sharepath)) {
5239 					ret = SA_OK;
5240 					group = sa_get_group(handle,
5241 					    "zfs");
5242 					if (group == NULL) {
5243 						/*
5244 						 * This shouldn't
5245 						 * happen.
5246 						 */
5247 						ret = SA_CONFIG_ERR;
5248 					} else {
5249 						share = sa_add_share(
5250 						    group, sharepath,
5251 						    persist, &ret);
5252 					}
5253 				}
5254 			}
5255 		} else {
5256 			char *type;
5257 			/*
5258 			 * May want to change persist state, but the
5259 			 * important thing is to change options. We
5260 			 * need to change them regardless of the
5261 			 * source.
5262 			 */
5263 
5264 			if (sa_zfs_is_shared(handle, sharepath)) {
5265 				zfs = 1;
5266 			}
5267 			remove_all_options(share, protocol);
5268 			type = sa_get_share_attr(share, "type");
5269 			if (type != NULL &&
5270 			    strcmp(type, "transient") != 0) {
5271 				curtype = SA_SHARE_PERMANENT;
5272 			}
5273 			if (type != NULL)
5274 				sa_free_attr_string(type);
5275 			if (curtype != persist) {
5276 				(void) sa_set_share_attr(share, "type",
5277 				    persist == SA_SHARE_PERMANENT ?
5278 				    "persist" : "transient");
5279 			}
5280 		}
5281 
5282 		/*
5283 		 * If there is a resource name, we may
5284 		 * actually care about it if this is share for
5285 		 * a protocol that uses resource level sharing
5286 		 * (SMB). We need to find the resource and, if
5287 		 * it exists, make sure it belongs to the
5288 		 * current share. If it doesn't exist, attempt
5289 		 * to create it.
5290 		 */
5291 
5292 		if (ret == SA_OK && resource != NULL) {
5293 			rsrc = sa_find_resource(handle, resource);
5294 			if (rsrc != NULL) {
5295 				if (share != sa_get_resource_parent(rsrc))
5296 					ret = SA_DUPLICATE_NAME;
5297 				} else {
5298 					rsrc = sa_add_resource(share, resource,
5299 					    persist, &ret);
5300 				}
5301 				if (features & SA_FEATURE_RESOURCE)
5302 					share = rsrc;
5303 			}
5304 
5305 			/* Have a group to hold this share path */
5306 			if (ret == SA_OK && options != NULL &&
5307 			    strlen(options) > 0) {
5308 				ret = sa_parse_legacy_options(share,
5309 				    options,
5310 				    protocol);
5311 			}
5312 			if (!zfs) {
5313 				/*
5314 				 * ZFS shares never have a description
5315 				 * and we can't store the values so
5316 				 * don't try.
5317 				 */
5318 				if (ret == SA_OK && description != NULL)
5319 					ret = sa_set_share_description(share,
5320 					    description);
5321 			}
5322 			if (ret == SA_OK &&
5323 			    strcmp(groupstatus, "enabled") == 0) {
5324 				if (rsrc != share)
5325 					ret = sa_enable_share(share, protocol);
5326 				else
5327 					ret = sa_enable_resource(rsrc,
5328 					    protocol);
5329 				if (ret == SA_OK &&
5330 				    persist == SA_SHARE_PERMANENT) {
5331 					(void) sa_update_legacy(share,
5332 					    protocol);
5333 				}
5334 				if (ret == SA_OK)
5335 					ret = sa_update_config(handle);
5336 			}
5337 	}
5338 err:
5339 	if (ret != SA_OK) {
5340 		(void) fprintf(stderr, gettext("Could not share: %s: %s\n"),
5341 		    sharepath, sa_errorstr(ret));
5342 		ret = SA_LEGACY_ERR;
5343 	}
5344 	return (ret);
5345 }
5346 
5347 /*
5348  * sa_legacy_unshare(flags, argc, argv)
5349  *
5350  * Implements the original unshare command.
5351  */
5352 int
5353 sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[])
5354 {
5355 	char *protocol = "nfs"; /* for now */
5356 	char *options = NULL;
5357 	char *sharepath = NULL;
5358 	int persist = SA_SHARE_TRANSIENT;
5359 	int argsused = 0;
5360 	int c;
5361 	int ret = SA_OK;
5362 	int true_legacy = 0;
5363 	uint64_t features = 0;
5364 	sa_resource_t resource = NULL;
5365 	char cmd[MAXPATHLEN];
5366 #ifdef lint
5367 	flags = flags;
5368 	options = options;
5369 #endif
5370 
5371 	while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) {
5372 		switch (c) {
5373 		case 'h':
5374 		case '?':
5375 			break;
5376 		case 'F':
5377 			protocol = optarg;
5378 			if (!sa_valid_protocol(protocol)) {
5379 				if (format_legacy_path(cmd, MAXPATHLEN,
5380 				    protocol, "unshare") == 0 &&
5381 				    check_legacy_cmd(cmd)) {
5382 					true_legacy++;
5383 				} else {
5384 					(void) printf(gettext(
5385 					    "Invalid file system name\n"));
5386 					return (SA_INVALID_PROTOCOL);
5387 				}
5388 			}
5389 			break;
5390 		case 'o':
5391 			options = optarg;
5392 			argsused++;
5393 			break;
5394 		case 'p':
5395 			persist = SA_SHARE_PERMANENT;
5396 			argsused++;
5397 			break;
5398 		default:
5399 			(void) printf(gettext("usage: %s\n"),
5400 			    sa_get_usage(USAGE_UNSHARE));
5401 			return (SA_OK);
5402 		}
5403 	}
5404 
5405 	/* Have the info so construct what is needed */
5406 	if (optind == argc || (optind + 1) < argc || options != NULL) {
5407 		ret = SA_SYNTAX_ERR;
5408 	} else {
5409 		sa_share_t share;
5410 		char dir[MAXPATHLEN];
5411 		if (true_legacy) {
5412 			/* if still using legacy share/unshare, exec it */
5413 			ret = run_legacy_command(cmd, argv);
5414 			return (ret);
5415 		}
5416 		/*
5417 		 * Find the path in the internal configuration. If it
5418 		 * isn't found, attempt to resolve the path via
5419 		 * realpath() and try again.
5420 		 */
5421 		sharepath = argv[optind++];
5422 		share = sa_find_share(handle, sharepath);
5423 		if (share == NULL) {
5424 			if (realpath(sharepath, dir) == NULL) {
5425 				ret = SA_NO_SUCH_PATH;
5426 			} else {
5427 				share = sa_find_share(handle, dir);
5428 			}
5429 		}
5430 		if (share == NULL) {
5431 			/* Could be a resource name so check that next */
5432 			features = sa_proto_get_featureset(protocol);
5433 			resource = sa_find_resource(handle, sharepath);
5434 			if (resource != NULL) {
5435 				share = sa_get_resource_parent(resource);
5436 				if (features & SA_FEATURE_RESOURCE)
5437 					(void) sa_disable_resource(resource,
5438 					    protocol);
5439 				if (persist == SA_SHARE_PERMANENT) {
5440 					ret = sa_remove_resource(resource);
5441 					if (ret == SA_OK)
5442 						ret = sa_update_config(handle);
5443 				}
5444 				/*
5445 				 * If we still have a resource on the
5446 				 * share, we don't disable the share
5447 				 * itself. IF there aren't anymore, we
5448 				 * need to remove the share. The
5449 				 * removal will be done in the next
5450 				 * section if appropriate.
5451 				 */
5452 				resource = sa_get_share_resource(share, NULL);
5453 				if (resource != NULL)
5454 					share = NULL;
5455 			} else if (ret == SA_OK) {
5456 				/* Didn't find path and no  resource */
5457 				ret = SA_BAD_PATH;
5458 			}
5459 		}
5460 		if (share != NULL && resource == NULL) {
5461 			ret = sa_disable_share(share, protocol);
5462 			/*
5463 			 * Errors are ok and removal should still occur. The
5464 			 * legacy unshare is more forgiving of errors than the
5465 			 * remove-share subcommand which may need the force
5466 			 * flag set for some error conditions. That is, the
5467 			 * "unshare" command will always unshare if it can
5468 			 * while "remove-share" might require the force option.
5469 			 */
5470 			if (persist == SA_SHARE_PERMANENT) {
5471 				ret = sa_remove_share(share);
5472 				if (ret == SA_OK)
5473 					ret = sa_update_config(handle);
5474 			}
5475 		} else if (ret == SA_OK && share == NULL && resource == NULL) {
5476 			/*
5477 			 * If both share and resource are NULL, then
5478 			 * share not found. If one or the other was
5479 			 * found or there was an earlier error, we
5480 			 * assume it was handled earlier.
5481 			 */
5482 			ret = SA_NOT_SHARED;
5483 		}
5484 	}
5485 	switch (ret) {
5486 	default:
5487 		(void) printf("%s: %s\n", sharepath, sa_errorstr(ret));
5488 		ret = SA_LEGACY_ERR;
5489 		break;
5490 	case SA_SYNTAX_ERR:
5491 		(void) printf(gettext("usage: %s\n"),
5492 		    sa_get_usage(USAGE_UNSHARE));
5493 		break;
5494 	case SA_OK:
5495 		break;
5496 	}
5497 	return (ret);
5498 }
5499 
5500 /*
5501  * Common commands that implement the sub-commands used by all
5502  * protocols. The entries are found via the lookup command
5503  */
5504 
5505 static sa_command_t commands[] = {
5506 	{"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET},
5507 	{"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION},
5508 	{"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION},
5509 	{"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION},
5510 	{"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION},
5511 	{"list", 0, sa_list, USAGE_LIST},
5512 	{"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET},
5513 	{"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET},
5514 	{"set", 0, sa_set, USAGE_SET, SVC_SET},
5515 	{"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET},
5516 	{"show", 0, sa_show, USAGE_SHOW},
5517 	{"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION},
5518 	{"start", CMD_NODISPLAY, sa_start_group, USAGE_START,
5519 	    SVC_SET|SVC_ACTION},
5520 	{"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION},
5521 	{"unset", 0, sa_unset, USAGE_UNSET, SVC_SET},
5522 	{"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION},
5523 	{NULL, 0, NULL, NULL}
5524 };
5525 
5526 static char *
5527 sa_get_usage(sa_usage_t index)
5528 {
5529 	char *ret = NULL;
5530 	switch (index) {
5531 	case USAGE_ADD_SHARE:
5532 		ret = gettext("add-share [-nth] [-r resource-name] "
5533 		    "[-d \"description text\"] -s sharepath group");
5534 		break;
5535 	case USAGE_CREATE:
5536 		ret = gettext(
5537 		    "create [-nvh] [-P proto [-p property=value]] group");
5538 		break;
5539 	case USAGE_DELETE:
5540 		ret = gettext("delete [-nvh] [-P proto] [-f] group");
5541 		break;
5542 	case USAGE_DISABLE:
5543 		ret = gettext("disable [-nvh] {-a | group ...}");
5544 		break;
5545 	case USAGE_ENABLE:
5546 		ret = gettext("enable [-nvh] {-a | group ...}");
5547 		break;
5548 	case USAGE_LIST:
5549 		ret = gettext("list [-vh] [-P proto]");
5550 		break;
5551 	case USAGE_MOVE_SHARE:
5552 		ret = gettext(
5553 		    "move-share [-nvh] -s sharepath destination-group");
5554 		break;
5555 	case USAGE_REMOVE_SHARE:
5556 		ret = gettext(
5557 		    "remove-share [-fnvh] {-s sharepath | -r resource} "
5558 		    "group");
5559 		break;
5560 	case USAGE_SET:
5561 		ret = gettext("set [-nvh] -P proto [-S optspace] "
5562 		    "[-p property=value]* [-s sharepath] [-r resource]] "
5563 		    "group");
5564 		break;
5565 	case USAGE_SET_SECURITY:
5566 		ret = gettext("set-security [-nvh] -P proto -S security-type "
5567 		    "[-p property=value]* group");
5568 		break;
5569 	case USAGE_SET_SHARE:
5570 		ret = gettext("set-share [-nh] [-r resource] "
5571 		    "[-d \"description text\"] -s sharepath group");
5572 		break;
5573 	case USAGE_SHOW:
5574 		ret = gettext("show [-pvxh] [-P proto] [group ...]");
5575 		break;
5576 	case USAGE_SHARE:
5577 		ret = gettext("share [-F fstype] [-p] [-o optionlist]"
5578 		    "[-d description] [pathname [resourcename]]");
5579 		break;
5580 	case USAGE_START:
5581 		ret = gettext("start [-vh] [-P proto] {-a | group ...}");
5582 		break;
5583 	case USAGE_STOP:
5584 		ret = gettext("stop [-vh] [-P proto] {-a | group ...}");
5585 		break;
5586 	case USAGE_UNSET:
5587 		ret = gettext("unset [-nvh] -P proto [-S optspace] "
5588 		    "[-p property]* group");
5589 		break;
5590 	case USAGE_UNSET_SECURITY:
5591 		ret = gettext("unset-security [-nvh] -P proto "
5592 		    "-S security-type [-p property]* group");
5593 		break;
5594 	case USAGE_UNSHARE:
5595 		ret = gettext(
5596 		    "unshare [-F fstype] [-p] [-o optionlist] sharepath");
5597 		break;
5598 	}
5599 	return (ret);
5600 }
5601 
5602 /*
5603  * sa_lookup(cmd, proto)
5604  *
5605  * Lookup the sub-command. proto isn't currently used, but it may
5606  * eventually provide a way to provide protocol specific sub-commands.
5607  */
5608 sa_command_t *
5609 sa_lookup(char *cmd, char *proto)
5610 {
5611 	int i;
5612 	size_t len;
5613 #ifdef lint
5614 	proto = proto;
5615 #endif
5616 
5617 	len = strlen(cmd);
5618 	for (i = 0; commands[i].cmdname != NULL; i++) {
5619 		if (strncmp(cmd, commands[i].cmdname, len) == 0)
5620 			return (&commands[i]);
5621 	}
5622 	return (NULL);
5623 }
5624 
5625 void
5626 sub_command_help(char *proto)
5627 {
5628 	int i;
5629 #ifdef lint
5630 	proto = proto;
5631 #endif
5632 
5633 	(void) printf(gettext("\tsub-commands:\n"));
5634 	for (i = 0; commands[i].cmdname != NULL; i++) {
5635 		if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY)))
5636 			(void) printf("\t%s\n",
5637 			    sa_get_usage((sa_usage_t)commands[i].cmdidx));
5638 	}
5639 }
5640