13034Sdougm /*
23034Sdougm  * CDDL HEADER START
33034Sdougm  *
43034Sdougm  * The contents of this file are subject to the terms of the
53034Sdougm  * Common Development and Distribution License (the "License").
63034Sdougm  * You may not use this file except in compliance with the License.
73034Sdougm  *
83034Sdougm  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93034Sdougm  * or http://www.opensolaris.org/os/licensing.
103034Sdougm  * See the License for the specific language governing permissions
113034Sdougm  * and limitations under the License.
123034Sdougm  *
133034Sdougm  * When distributing Covered Code, include this CDDL HEADER in each
143034Sdougm  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153034Sdougm  * If applicable, add the following below this CDDL HEADER, with the
163034Sdougm  * fields enclosed by brackets "[]" replaced with your own identifying
173034Sdougm  * information: Portions Copyright [yyyy] [name of copyright owner]
183034Sdougm  *
193034Sdougm  * CDDL HEADER END
203034Sdougm  */
213034Sdougm 
223034Sdougm /*
235772Sas200622  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
243034Sdougm  * Use is subject to license terms.
253034Sdougm  */
263034Sdougm 
273034Sdougm #pragma ident	"%Z%%M%	%I%	%E% SMI"
283034Sdougm 
293034Sdougm #include <sys/types.h>
303034Sdougm #include <sys/stat.h>
313034Sdougm #include <fcntl.h>
323034Sdougm #include <stdlib.h>
333034Sdougm #include <stdio.h>
343034Sdougm #include <string.h>
353034Sdougm #include <ctype.h>
363034Sdougm #include <unistd.h>
373034Sdougm #include <getopt.h>
383034Sdougm #include <utmpx.h>
393034Sdougm #include <pwd.h>
403034Sdougm #include <auth_attr.h>
413034Sdougm #include <secdb.h>
423034Sdougm #include <sys/param.h>
433034Sdougm #include <sys/stat.h>
443034Sdougm #include <errno.h>
453034Sdougm 
463034Sdougm #include <libshare.h>
473034Sdougm #include "sharemgr.h"
483034Sdougm #include <libscf.h>
493034Sdougm #include <libxml/tree.h>
503034Sdougm #include <libintl.h>
515331Samw #include <assert.h>
525331Samw #include <iconv.h>
535331Samw #include <langinfo.h>
545331Samw #include <dirent.h>
553034Sdougm 
563034Sdougm static char *sa_get_usage(sa_usage_t);
573034Sdougm 
583034Sdougm /*
593034Sdougm  * Implementation of the common sub-commands supported by sharemgr.
603034Sdougm  * A number of helper functions are also included.
613034Sdougm  */
623034Sdougm 
633034Sdougm /*
643034Sdougm  * has_protocol(group, proto)
653034Sdougm  *	If the group has an optionset with the specified protocol,
663034Sdougm  *	return true (1) otherwise false (0).
673034Sdougm  */
683034Sdougm static int
693034Sdougm has_protocol(sa_group_t group, char *protocol)
703034Sdougm {
713034Sdougm 	sa_optionset_t optionset;
723034Sdougm 	int result = 0;
733034Sdougm 
743034Sdougm 	optionset = sa_get_optionset(group, protocol);
753034Sdougm 	if (optionset != NULL) {
764653Sdougm 		result++;
773034Sdougm 	}
783034Sdougm 	return (result);
793034Sdougm }
803034Sdougm 
813034Sdougm /*
825331Samw  * validresource(name)
835331Samw  *
845331Samw  * Check that name only has valid characters in it. The current valid
855331Samw  * set are the printable characters but not including:
865331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
875331Samw  * Note that space is included and there is a maximum length.
885331Samw  */
895331Samw static int
905331Samw validresource(const char *name)
915331Samw {
925331Samw 	const char *cp;
935331Samw 	size_t len;
945331Samw 
955331Samw 	if (name == NULL)
965331Samw 		return (B_FALSE);
975331Samw 
985331Samw 	len = strlen(name);
995331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
1005331Samw 		return (B_FALSE);
1015331Samw 
1025331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
1035331Samw 		return (B_FALSE);
1045331Samw 	}
1055331Samw 
1065331Samw 	for (cp = name; *cp != '\0'; cp++)
1075331Samw 		if (iscntrl(*cp))
1085331Samw 			return (B_FALSE);
1095331Samw 
1105331Samw 	return (B_TRUE);
1115331Samw }
1125331Samw 
1135331Samw /*
1145331Samw  * conv_to_utf8(input)
1155331Samw  *
1165331Samw  * Convert the input string to utf8 from the current locale.  If the
1175331Samw  * conversion fails, use the current locale, it is likely close
1185331Samw  * enough. For example, the "C" locale is a subset of utf-8. The
1195331Samw  * return value may be a new string or the original input string.
1205331Samw  */
1215331Samw 
1225331Samw static char *
1235331Samw conv_to_utf8(char *input)
1245331Samw {
1255331Samw 	iconv_t cd;
1265521Sas200622 	char *inval = input;
1275331Samw 	char *output = input;
1285331Samw 	char *outleft;
1295331Samw 	char *curlocale;
1305331Samw 	size_t bytesleft;
1315331Samw 	size_t size;
1325331Samw 	size_t osize;
1335331Samw 	static int warned = 0;
1345331Samw 
1355331Samw 	curlocale = nl_langinfo(CODESET);
1365331Samw 	if (curlocale == NULL)
1375331Samw 		curlocale = "C";
1385331Samw 	cd = iconv_open("UTF-8", curlocale);
1395331Samw 	if (cd != NULL && cd != (iconv_t)-1) {
1405331Samw 		size = strlen(input);
1415331Samw 		/* Assume worst case of characters expanding to 4 bytes. */
1425331Samw 		bytesleft = size * 4;
1435331Samw 		output = calloc(bytesleft, 1);
1445331Samw 		if (output != NULL) {
1455331Samw 			outleft = output;
1465521Sas200622 			/* inval can be modified on return */
1475521Sas200622 			osize = iconv(cd, (const char **)&inval, &size,
1485331Samw 			    &outleft, &bytesleft);
1495331Samw 			if (osize == (size_t)-1 || size != 0) {
1505331Samw 				free(output);
1515331Samw 				output = input;
1525331Samw 			}
1535521Sas200622 		} else {
1545521Sas200622 			/* Need to return something. */
1555521Sas200622 			output = input;
1565331Samw 		}
1575331Samw 		(void) iconv_close(cd);
1585331Samw 	} else {
1595331Samw 		if (!warned)
1605331Samw 			(void) fprintf(stderr,
1615331Samw 			    gettext("Cannot convert to UTF-8 from %s\n"),
1625331Samw 			    curlocale ? curlocale : gettext("unknown"));
1635331Samw 		warned = 1;
1645331Samw 	}
1655331Samw 	return (output);
1665331Samw }
1675331Samw 
1685331Samw /*
1695331Samw  * conv_from(input)
1705331Samw  *
1715331Samw  * Convert the input string from utf8 to current locale.  If the
1725331Samw  * conversion isn't supported, just use as is. The return value may be
1735331Samw  * a new string or the original input string.
1745331Samw  */
1755331Samw 
1765331Samw static char *
1775331Samw conv_from_utf8(char *input)
1785331Samw {
1795331Samw 	iconv_t cd;
1805331Samw 	char *output = input;
1815521Sas200622 	char *inval = input;
1825331Samw 	char *outleft;
1835331Samw 	char *curlocale;
1845331Samw 	size_t bytesleft;
1855331Samw 	size_t size;
1865331Samw 	size_t osize;
1875331Samw 	static int warned = 0;
1885331Samw 
1895331Samw 	curlocale = nl_langinfo(CODESET);
1905331Samw 	if (curlocale == NULL)
1915331Samw 		curlocale = "C";
1925331Samw 	cd = iconv_open(curlocale, "UTF-8");
1935331Samw 	if (cd != NULL && cd != (iconv_t)-1) {
1945331Samw 		size = strlen(input);
1955331Samw 		/* Assume worst case of characters expanding to 4 bytes. */
1965331Samw 		bytesleft = size * 4;
1975331Samw 		output = calloc(bytesleft, 1);
1985331Samw 		if (output != NULL) {
1995331Samw 			outleft = output;
2005521Sas200622 			osize = iconv(cd, (const char **)&inval, &size,
2015331Samw 			    &outleft, &bytesleft);
2025521Sas200622 			if (osize == (size_t)-1 || size != 0)
2035331Samw 				output = input;
2045521Sas200622 		} else {
2055521Sas200622 			/* Need to return something. */
2065521Sas200622 			output = input;
2075331Samw 		}
2085331Samw 		(void) iconv_close(cd);
2095331Samw 	} else {
2105331Samw 		if (!warned)
2115331Samw 			(void) fprintf(stderr,
2125331Samw 			    gettext("Cannot convert to %s from UTF-8\n"),
2135331Samw 			    curlocale ? curlocale : gettext("unknown"));
2145331Samw 		warned = 1;
2155331Samw 	}
2165331Samw 	return (output);
2175331Samw }
2185331Samw 
2195885Sdougm /*
2205885Sdougm  * print_rsrc_desc(resource, sharedesc)
2215885Sdougm  *
2225885Sdougm  * Print the resource description string after converting from UTF8 to
2235885Sdougm  * the current locale. If sharedesc is not NULL and there is no
2245885Sdougm  * description on the resource, use sharedesc. sharedesc will already
2255885Sdougm  * be converted to UTF8.
2265885Sdougm  */
2275885Sdougm 
2285331Samw static void
2295885Sdougm print_rsrc_desc(sa_resource_t resource, char *sharedesc)
2305331Samw {
2315331Samw 	char *description;
2325331Samw 	char *desc;
2335331Samw 
2345885Sdougm 	if (resource == NULL)
2355885Sdougm 		return;
2365885Sdougm 
2375331Samw 	description = sa_get_resource_description(resource);
2385331Samw 	if (description != NULL) {
2395331Samw 		desc = conv_from_utf8(description);
2405331Samw 		if (desc != description) {
2415331Samw 			sa_free_share_description(description);
2425331Samw 			description = desc;
2435331Samw 		}
2445885Sdougm 	} else if (sharedesc != NULL) {
2455885Sdougm 		description = strdup(sharedesc);
2465885Sdougm 	}
2475885Sdougm 	if (description != NULL) {
2485331Samw 		(void) printf("\t\"%s\"", description);
2495331Samw 		sa_free_share_description(description);
2505331Samw 	}
2515331Samw }
2525331Samw 
2535885Sdougm /*
2545885Sdougm  * set_resource_desc(share, description)
2555885Sdougm  *
2565885Sdougm  * Set the share description value after converting the description
2575885Sdougm  * string to UTF8 from the current locale.
2585885Sdougm  */
2595885Sdougm 
2605885Sdougm static int
2615885Sdougm set_resource_desc(sa_share_t share, char *description)
2625885Sdougm {
2635885Sdougm 	char *desc;
2645885Sdougm 	int ret;
2655885Sdougm 
2665885Sdougm 	desc = conv_to_utf8(description);
2675885Sdougm 	ret = sa_set_resource_description(share, desc);
2685885Sdougm 	if (description != desc)
2695885Sdougm 		sa_free_share_description(desc);
2705885Sdougm 	return (ret);
2715885Sdougm }
2725885Sdougm 
2735885Sdougm /*
2745885Sdougm  * set_share_desc(share, description)
2755885Sdougm  *
2765885Sdougm  * Set the resource description value after converting the description
2775885Sdougm  * string to UTF8 from the current locale.
2785885Sdougm  */
2795885Sdougm 
2805331Samw static int
2815331Samw set_share_desc(sa_share_t share, char *description)
2825331Samw {
2835331Samw 	char *desc;
2845331Samw 	int ret;
2855331Samw 
2865331Samw 	desc = conv_to_utf8(description);
2875331Samw 	ret = sa_set_share_description(share, desc);
2885331Samw 	if (description != desc)
2895331Samw 		sa_free_share_description(desc);
2905331Samw 	return (ret);
2915331Samw }
2925331Samw 
2935331Samw /*
2945331Samw  * add_list(list, item, data, proto)
2955331Samw  *	Adds a new list member that points holds item in the list.
2963034Sdougm  *	If list is NULL, it starts a new list.  The function returns
2973034Sdougm  *	the first member of the list.
2983034Sdougm  */
2993034Sdougm struct list *
3005331Samw add_list(struct list *listp, void *item, void *data, char *proto)
3013034Sdougm {
3023034Sdougm 	struct list *new, *tmp;
3033034Sdougm 
3043034Sdougm 	new = malloc(sizeof (struct list));
3053034Sdougm 	if (new != NULL) {
3064653Sdougm 		new->next = NULL;
3074653Sdougm 		new->item = item;
3084653Sdougm 		new->itemdata = data;
3095331Samw 		new->proto = proto;
3103034Sdougm 	} else {
3114653Sdougm 		return (listp);
3123034Sdougm 	}
3133034Sdougm 
3143034Sdougm 	if (listp == NULL)
3154653Sdougm 		return (new);
3163034Sdougm 
3173034Sdougm 	for (tmp = listp; tmp->next != NULL; tmp = tmp->next) {
3183034Sdougm 		/* get to end of list */
3193034Sdougm 	}
3203034Sdougm 	tmp->next = new;
3213034Sdougm 	return (listp);
3223034Sdougm }
3233034Sdougm 
3243034Sdougm /*
3253034Sdougm  * free_list(list)
3263034Sdougm  *	Given a list, free all the members of the list;
3273034Sdougm  */
3283034Sdougm static void
3293034Sdougm free_list(struct list *listp)
3303034Sdougm {
3313034Sdougm 	struct list *tmp;
3323034Sdougm 	while (listp != NULL) {
3334653Sdougm 		tmp = listp;
3344653Sdougm 		listp = listp->next;
3354653Sdougm 		free(tmp);
3363034Sdougm 	}
3373034Sdougm }
3383034Sdougm 
3393034Sdougm /*
3403034Sdougm  * check_authorization(instname, which)
3413034Sdougm  *
3423034Sdougm  * Checks to see if the specific type of authorization in which is
3433034Sdougm  * enabled for the user in this SMF service instance.
3443034Sdougm  */
3453034Sdougm 
3463034Sdougm static int
3473034Sdougm check_authorization(char *instname, int which)
3483034Sdougm {
3493034Sdougm 	scf_handle_t *handle = NULL;
3503034Sdougm 	scf_simple_prop_t *prop = NULL;
3513034Sdougm 	char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
3523034Sdougm 	char *authstr = NULL;
3533034Sdougm 	ssize_t numauths;
3544653Sdougm 	int ret = B_TRUE;
3553034Sdougm 	uid_t uid;
3563034Sdougm 	struct passwd *pw = NULL;
3573034Sdougm 
3583034Sdougm 	uid = getuid();
3593034Sdougm 	pw = getpwuid(uid);
3604653Sdougm 	if (pw == NULL) {
3614653Sdougm 		ret = B_FALSE;
3624653Sdougm 	} else {
3634653Sdougm 		/*
3644653Sdougm 		 * Since names are restricted to SA_MAX_NAME_LEN won't
3654653Sdougm 		 * overflow.
3664653Sdougm 		 */
3674653Sdougm 		(void) snprintf(svcstring, sizeof (svcstring), "%s:%s",
3684653Sdougm 		    SA_SVC_FMRI_BASE, instname);
3694653Sdougm 		handle = scf_handle_create(SCF_VERSION);
3704653Sdougm 		if (handle != NULL) {
3714653Sdougm 			if (scf_handle_bind(handle) == 0) {
3724653Sdougm 				switch (which) {
3734653Sdougm 				case SVC_SET:
3744653Sdougm 					prop = scf_simple_prop_get(handle,
3754653Sdougm 					    svcstring, "general",
3764653Sdougm 					    SVC_AUTH_VALUE);
3774653Sdougm 					break;
3784653Sdougm 				case SVC_ACTION:
3794653Sdougm 					prop = scf_simple_prop_get(handle,
3804653Sdougm 					    svcstring, "general",
3814653Sdougm 					    SVC_AUTH_ACTION);
3824653Sdougm 					break;
3834653Sdougm 				}
3844653Sdougm 			}
3853034Sdougm 		}
3863034Sdougm 	}
3873034Sdougm 	/* make sure we have an authorization string property */
3883034Sdougm 	if (prop != NULL) {
3894653Sdougm 		int i;
3904653Sdougm 		numauths = scf_simple_prop_numvalues(prop);
3914653Sdougm 		for (ret = 0, i = 0; i < numauths; i++) {
3924653Sdougm 			authstr = scf_simple_prop_next_astring(prop);
3934653Sdougm 			if (authstr != NULL) {
3944653Sdougm 				/* check if this user has one of the strings */
3954653Sdougm 				if (chkauthattr(authstr, pw->pw_name)) {
3964653Sdougm 					ret = 1;
3974653Sdougm 					break;
3984653Sdougm 				}
3994653Sdougm 			}
4003034Sdougm 		}
4014653Sdougm 		endauthattr();
4024653Sdougm 		scf_simple_prop_free(prop);
4033034Sdougm 	} else {
4044653Sdougm 		/* no authorization string defined */
4054653Sdougm 		ret = 0;
4063034Sdougm 	}
4073034Sdougm 	if (handle != NULL)
4084653Sdougm 		scf_handle_destroy(handle);
4093034Sdougm 	return (ret);
4103034Sdougm }
4113034Sdougm 
4123034Sdougm /*
4133034Sdougm  * check_authorizations(instname, flags)
4143034Sdougm  *
4153034Sdougm  * check all the needed authorizations for the user in this service
4163034Sdougm  * instance. Return value of 1(true) or 0(false) indicates whether
4173034Sdougm  * there are authorizations for the user or not.
4183034Sdougm  */
4193034Sdougm 
4203034Sdougm static int
4213034Sdougm check_authorizations(char *instname, int flags)
4223034Sdougm {
4233034Sdougm 	int ret1 = 0;
4243034Sdougm 	int ret2 = 0;
4253034Sdougm 	int ret;
4263034Sdougm 
4273034Sdougm 	if (flags & SVC_SET)
4284653Sdougm 		ret1 = check_authorization(instname, SVC_SET);
4293034Sdougm 	if (flags & SVC_ACTION)
4304653Sdougm 		ret2 = check_authorization(instname, SVC_ACTION);
4313034Sdougm 	switch (flags) {
4323034Sdougm 	case SVC_ACTION:
4334653Sdougm 		ret = ret2;
4344653Sdougm 		break;
4353034Sdougm 	case SVC_SET:
4364653Sdougm 		ret = ret1;
4374653Sdougm 		break;
4383034Sdougm 	case SVC_ACTION|SVC_SET:
4394653Sdougm 		ret = ret1 & ret2;
4404653Sdougm 		break;
4413034Sdougm 	default:
4424653Sdougm 		/* if not flags set, we assume we don't need authorizations */
4434653Sdougm 		ret = 1;
4443034Sdougm 	}
4453034Sdougm 	return (ret);
4463034Sdougm }
4473034Sdougm 
4483034Sdougm /*
4495331Samw  * notify_or_enable_share(share, protocol)
4505331Samw  *
4515331Samw  * Since some protocols don't want an "enable" when properties change,
4525331Samw  * this function will use the protocol specific notify function
4535331Samw  * first. If that fails, it will then attempt to use the
4545331Samw  * sa_enable_share().  "protocol" is the protocol that was specified
4555331Samw  * on the command line.
4565331Samw  */
4575331Samw static void
4585331Samw notify_or_enable_share(sa_share_t share, char *protocol)
4595331Samw {
4605331Samw 	sa_group_t group;
4615331Samw 	sa_optionset_t opt;
4625331Samw 	int ret = SA_OK;
4635331Samw 	char *path;
4645331Samw 	char *groupproto;
4655331Samw 	sa_share_t parent = share;
4665331Samw 
4675331Samw 	/* If really a resource, get parent share */
4685331Samw 	if (!sa_is_share(share)) {
4695331Samw 		parent = sa_get_resource_parent((sa_resource_t)share);
4705331Samw 	}
4715331Samw 
4725331Samw 	/*
4735331Samw 	 * Now that we've got a share in "parent", make sure it has a path.
4745331Samw 	 */
4755331Samw 	path = sa_get_share_attr(parent, "path");
4765331Samw 	if (path == NULL)
4775331Samw 		return;
4785331Samw 
4795331Samw 	group = sa_get_parent_group(parent);
4805331Samw 
4815331Samw 	if (group == NULL) {
4825331Samw 		sa_free_attr_string(path);
4835331Samw 		return;
4845331Samw 	}
4855331Samw 	for (opt = sa_get_optionset(group, NULL);
4865331Samw 	    opt != NULL;
4875331Samw 	    opt = sa_get_next_optionset(opt)) {
4885331Samw 		groupproto = sa_get_optionset_attr(opt, "type");
4895331Samw 		if (groupproto == NULL ||
4905331Samw 		    (protocol != NULL && strcmp(groupproto, protocol) != 0)) {
4915331Samw 			sa_free_attr_string(groupproto);
4925331Samw 			continue;
4935331Samw 		}
4945331Samw 		if (sa_is_share(share)) {
4955331Samw 			if ((ret = sa_proto_change_notify(share,
4965331Samw 			    groupproto)) != SA_OK) {
4975331Samw 				ret = sa_enable_share(share, groupproto);
4985331Samw 				if (ret != SA_OK) {
4995331Samw 					(void) printf(
5005331Samw 					    gettext("Could not reenable"
5015331Samw 					    " share %s: %s\n"),
5025331Samw 					    path, sa_errorstr(ret));
5035331Samw 				}
5045331Samw 			}
5055331Samw 		} else {
5065331Samw 			/* Must be a resource */
5075331Samw 			if ((ret = sa_proto_notify_resource(share,
5085331Samw 			    groupproto)) != SA_OK) {
5095331Samw 				ret = sa_enable_resource(share, groupproto);
5105331Samw 				if (ret != SA_OK) {
5115331Samw 					(void) printf(
5125331Samw 					    gettext("Could not "
5135331Samw 					    "reenable resource %s: "
5145331Samw 					    "%s\n"), path,
5155331Samw 					    sa_errorstr(ret));
5165331Samw 				}
5175331Samw 			}
5185331Samw 		}
5195331Samw 		sa_free_attr_string(groupproto);
5205331Samw 	}
5215331Samw 	sa_free_attr_string(path);
5225331Samw }
5235331Samw 
5245331Samw /*
5255331Samw  * enable_group(group, updateproto, notify, proto)
5263082Sdougm  *
5273082Sdougm  * enable all the shares in the specified group. This is a helper for
5283082Sdougm  * enable_all_groups in order to simplify regular and subgroup (zfs)
5295331Samw  * enabling. Group has already been checked for non-NULL. If notify
5305331Samw  * is non-zero, attempt to use the notify interface rather than
5315331Samw  * enable.
5323082Sdougm  */
5333082Sdougm static void
5345331Samw enable_group(sa_group_t group, char *updateproto, int notify, char *proto)
5353082Sdougm {
5363082Sdougm 	sa_share_t share;
5373082Sdougm 
5383082Sdougm 	for (share = sa_get_share(group, NULL);
5393082Sdougm 	    share != NULL;
5403082Sdougm 	    share = sa_get_next_share(share)) {
5414653Sdougm 		if (updateproto != NULL)
5424653Sdougm 			(void) sa_update_legacy(share, updateproto);
5435331Samw 		if (notify)
5445331Samw 			notify_or_enable_share(share, proto);
5455331Samw 		else
5465331Samw 			(void) sa_enable_share(share, proto);
5473082Sdougm 	}
5483082Sdougm }
5493082Sdougm 
5503082Sdougm /*
5514241Sdougm  * isenabled(group)
5524241Sdougm  *
5534241Sdougm  * Returns B_TRUE if the group is enabled or B_FALSE if it isn't.
5544241Sdougm  * Moved to separate function to reduce clutter in the code.
5554241Sdougm  */
5564241Sdougm 
5574241Sdougm static int
5584241Sdougm isenabled(sa_group_t group)
5594241Sdougm {
5604241Sdougm 	char *state;
5614241Sdougm 	int ret = B_FALSE;
5624241Sdougm 
5634241Sdougm 	if (group != NULL) {
5644653Sdougm 		state = sa_get_group_attr(group, "state");
5654653Sdougm 		if (state != NULL) {
5665331Samw 
5674653Sdougm 			if (strcmp(state, "enabled") == 0)
5684653Sdougm 				ret = B_TRUE;
5694653Sdougm 			sa_free_attr_string(state);
5704653Sdougm 		}
5714241Sdougm 	}
5724241Sdougm 	return (ret);
5734241Sdougm }
5744241Sdougm 
5754241Sdougm /*
5763082Sdougm  * enable_all_groups(list, setstate, online, updateproto)
5775331Samw  *
5785331Samw  * Given a list of groups, enable each one found.  If updateproto is
5795331Samw  * not NULL, then update all the shares for the protocol that was
5805331Samw  * passed in. If enable is non-zero, tell enable_group to try the
5815331Samw  * notify interface since this is a property change.
5823034Sdougm  */
5833034Sdougm static int
5843910Sdougm enable_all_groups(sa_handle_t handle, struct list *work, int setstate,
5855331Samw     int online, char *updateproto, int enable)
5863034Sdougm {
5874241Sdougm 	int ret;
5883034Sdougm 	char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
5893034Sdougm 	char *state;
5903034Sdougm 	char *name;
5913034Sdougm 	char *zfs = NULL;
5923034Sdougm 	sa_group_t group;
5933082Sdougm 	sa_group_t subgroup;
5943034Sdougm 
5954241Sdougm 	for (ret = SA_OK; work != NULL;	work = work->next) {
5964653Sdougm 		group = (sa_group_t)work->item;
5974241Sdougm 
5984241Sdougm 		/*
5994241Sdougm 		 * If setstate == TRUE, then make sure to set
6004241Sdougm 		 * enabled. This needs to be done here in order for
6014241Sdougm 		 * the isenabled check to succeed on a newly enabled
6024241Sdougm 		 * group.
6034241Sdougm 		 */
6044653Sdougm 		if (setstate == B_TRUE) {
6054653Sdougm 			ret = sa_set_group_attr(group, "state",	"enabled");
6064653Sdougm 			if (ret != SA_OK)
6074653Sdougm 				break;
6084653Sdougm 		}
6094241Sdougm 
6104241Sdougm 		/*
6114241Sdougm 		 * Check to see if group is enabled. If it isn't, skip
6124241Sdougm 		 * the rest.  We don't want shares starting if the
6134241Sdougm 		 * group is disabled. The properties may have been
6144241Sdougm 		 * updated, but there won't be a change until the
6154241Sdougm 		 * group is enabled.
6164241Sdougm 		 */
6174653Sdougm 		if (!isenabled(group))
6184653Sdougm 			continue;
6194653Sdougm 
6204653Sdougm 		/* if itemdata != NULL then a single share */
6214653Sdougm 		if (work->itemdata != NULL) {
6225331Samw 			if (enable) {
6235331Samw 				if (work->itemdata != NULL)
6245331Samw 					notify_or_enable_share(work->itemdata,
6255331Samw 					    updateproto);
6265331Samw 				else
6275331Samw 					ret = SA_CONFIG_ERR;
6285331Samw 			} else {
6295331Samw 				if (sa_is_share(work->itemdata)) {
6305331Samw 					ret = sa_enable_share(
6315331Samw 					    (sa_share_t)work->itemdata,
6325331Samw 					    updateproto);
6335331Samw 				} else {
6345331Samw 					ret = sa_enable_resource(
6355331Samw 					    (sa_resource_t)work->itemdata,
6365331Samw 					    updateproto);
6375331Samw 				}
6385331Samw 			}
6393034Sdougm 		}
6404653Sdougm 		if (ret != SA_OK)
6414653Sdougm 			break;
6424653Sdougm 
6434653Sdougm 		/* if itemdata == NULL then the whole group */
6444653Sdougm 		if (work->itemdata == NULL) {
6454653Sdougm 			zfs = sa_get_group_attr(group, "zfs");
6464653Sdougm 			/*
6475331Samw 			 * If the share is managed by ZFS, don't
6484653Sdougm 			 * update any of the protocols since ZFS is
6495331Samw 			 * handling this.  Updateproto will contain
6504653Sdougm 			 * the name of the protocol that we want to
6514653Sdougm 			 * update legacy files for.
6524653Sdougm 			 */
6535331Samw 			enable_group(group, zfs == NULL ? updateproto : NULL,
6545331Samw 			    enable, work->proto);
6554653Sdougm 			for (subgroup = sa_get_sub_group(group);
6564653Sdougm 			    subgroup != NULL;
6574653Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
6584653Sdougm 				/* never update legacy for ZFS subgroups */
6595331Samw 				enable_group(subgroup, NULL, enable,
6605331Samw 				    work->proto);
6613034Sdougm 			}
6623034Sdougm 		}
6634653Sdougm 		if (online) {
6644653Sdougm 			zfs = sa_get_group_attr(group, "zfs");
6654653Sdougm 			name = sa_get_group_attr(group, "name");
6664653Sdougm 			if (name != NULL) {
6674653Sdougm 				if (zfs == NULL) {
6684653Sdougm 					(void) snprintf(instance,
6694653Sdougm 					    sizeof (instance), "%s:%s",
6704653Sdougm 					    SA_SVC_FMRI_BASE, name);
6714653Sdougm 					state = smf_get_state(instance);
6724653Sdougm 					if (state == NULL ||
6734653Sdougm 					    strcmp(state, "online") != 0) {
6744653Sdougm 						(void) smf_enable_instance(
6754653Sdougm 						    instance, 0);
6764653Sdougm 						free(state);
6774653Sdougm 					}
6784653Sdougm 				} else {
6794653Sdougm 					sa_free_attr_string(zfs);
6804653Sdougm 					zfs = NULL;
6814653Sdougm 				}
6824653Sdougm 				if (name != NULL)
6834653Sdougm 					sa_free_attr_string(name);
6844653Sdougm 			}
6854653Sdougm 		}
6863034Sdougm 	}
6873034Sdougm 	if (ret == SA_OK) {
6884653Sdougm 		ret = sa_update_config(handle);
6893034Sdougm 	}
6903034Sdougm 	return (ret);
6913034Sdougm }
6923034Sdougm 
6933034Sdougm /*
6943034Sdougm  * chk_opt(optlistp, security, proto)
6953034Sdougm  *
6963034Sdougm  * Do a sanity check on the optlist provided for the protocol.  This
6973034Sdougm  * is a syntax check and verification that the property is either a
6983034Sdougm  * general or specific to a names optionset.
6993034Sdougm  */
7003034Sdougm 
7013034Sdougm static int
7023034Sdougm chk_opt(struct options *optlistp, int security, char *proto)
7033034Sdougm {
7043034Sdougm 	struct options *optlist;
7053034Sdougm 	char *sep = "";
7063034Sdougm 	int notfirst = 0;
7073034Sdougm 	int ret;
7083034Sdougm 
7093034Sdougm 	for (optlist = optlistp; optlist != NULL; optlist = optlist->next) {
7104653Sdougm 		char *optname;
7114653Sdougm 
7124653Sdougm 		optname = optlist->optname;
7134653Sdougm 		ret = OPT_ADD_OK;
7144653Sdougm 		/* extract property/value pair */
7154653Sdougm 		if (sa_is_security(optname, proto)) {
7164653Sdougm 			if (!security)
7174653Sdougm 				ret = OPT_ADD_SECURITY;
7184653Sdougm 		} else {
7194653Sdougm 			if (security)
7204653Sdougm 				ret = OPT_ADD_PROPERTY;
7214653Sdougm 		}
7224653Sdougm 		if (ret != OPT_ADD_OK) {
7234653Sdougm 			if (notfirst == 0)
7244653Sdougm 				(void) printf(
7254653Sdougm 				    gettext("Property syntax error: "));
7264653Sdougm 			switch (ret) {
7274653Sdougm 			case OPT_ADD_SYNTAX:
7284653Sdougm 				(void) printf(gettext("%ssyntax error: %s"),
7293034Sdougm 				    sep, optname);
7304653Sdougm 				sep = ", ";
7314653Sdougm 				break;
7324653Sdougm 			case OPT_ADD_SECURITY:
7334653Sdougm 				(void) printf(gettext("%s%s requires -S"),
7343034Sdougm 				    optname, sep);
7354653Sdougm 				sep = ", ";
7364653Sdougm 				break;
7374653Sdougm 			case OPT_ADD_PROPERTY:
7384653Sdougm 				(void) printf(
7394653Sdougm 				    gettext("%s%s not supported with -S"),
7403034Sdougm 				    optname, sep);
7414653Sdougm 				sep = ", ";
7424653Sdougm 				break;
7434653Sdougm 			}
7444653Sdougm 			notfirst++;
7453034Sdougm 		}
7463034Sdougm 	}
7473034Sdougm 	if (notfirst) {
7484653Sdougm 		(void) printf("\n");
7494653Sdougm 		ret = SA_SYNTAX_ERR;
7503034Sdougm 	}
7513034Sdougm 	return (ret);
7523034Sdougm }
7533034Sdougm 
7543034Sdougm /*
7553034Sdougm  * free_opt(optlist)
7563034Sdougm  *	Free the specified option list.
7573034Sdougm  */
7583034Sdougm static void
7593034Sdougm free_opt(struct options *optlist)
7603034Sdougm {
7613034Sdougm 	struct options *nextopt;
7623034Sdougm 	while (optlist != NULL) {
7633034Sdougm 		nextopt = optlist->next;
7643034Sdougm 		free(optlist);
7653034Sdougm 		optlist = nextopt;
7663034Sdougm 	}
7673034Sdougm }
7683034Sdougm 
7693034Sdougm /*
7703034Sdougm  * check property list for valid properties
7713034Sdougm  * A null value is a remove which is always valid.
7723034Sdougm  */
7733034Sdougm static int
774*6214Sdougm valid_options(sa_handle_t handle, struct options *optlist, char *proto,
775*6214Sdougm     void *object, char *sec)
7763034Sdougm {
7773034Sdougm 	int ret = SA_OK;
7783034Sdougm 	struct options *cur;
7793034Sdougm 	sa_property_t prop;
7803034Sdougm 	sa_optionset_t parent = NULL;
7813034Sdougm 
7823034Sdougm 	if (object != NULL) {
7834653Sdougm 		if (sec == NULL)
7844653Sdougm 			parent = sa_get_optionset(object, proto);
7854653Sdougm 		else
7864653Sdougm 			parent = sa_get_security(object, sec, proto);
7873034Sdougm 	}
7883034Sdougm 
7893034Sdougm 	for (cur = optlist; cur != NULL; cur = cur->next) {
7904653Sdougm 		if (cur->optvalue == NULL)
7914653Sdougm 			continue;
7923034Sdougm 		prop = sa_create_property(cur->optname, cur->optvalue);
7933034Sdougm 		if (prop == NULL)
7944653Sdougm 			ret = SA_NO_MEMORY;
7953034Sdougm 		if (ret != SA_OK ||
796*6214Sdougm 		    (ret = sa_valid_property(handle, parent, proto, prop)) !=
797*6214Sdougm 		    SA_OK) {
7984653Sdougm 			(void) printf(
7994653Sdougm 			    gettext("Could not add property %s: %s\n"),
8004653Sdougm 			    cur->optname, sa_errorstr(ret));
8013034Sdougm 		}
8023034Sdougm 		(void) sa_remove_property(prop);
8033034Sdougm 	}
8043034Sdougm 	return (ret);
8053034Sdougm }
8063034Sdougm 
8073034Sdougm /*
8083034Sdougm  * add_optionset(group, optlist, protocol, *err)
8093034Sdougm  *	Add the options in optlist to an optionset and then add the optionset
8103034Sdougm  *	to the group.
8113034Sdougm  *
8123034Sdougm  *	The return value indicates if there was a "change" while errors are
8133034Sdougm  *	returned via the *err parameters.
8143034Sdougm  */
8153034Sdougm static int
8163034Sdougm add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err)
8173034Sdougm {
8183034Sdougm 	sa_optionset_t optionset;
8193034Sdougm 	int ret = SA_OK;
8205331Samw 	int result = B_FALSE;
821*6214Sdougm 	sa_handle_t handle;
8223034Sdougm 
8233034Sdougm 	optionset = sa_get_optionset(group, proto);
8243034Sdougm 	if (optionset == NULL) {
8254653Sdougm 		optionset = sa_create_optionset(group, proto);
8265331Samw 		if (optionset == NULL)
8275331Samw 			ret = SA_NO_MEMORY;
8285331Samw 		result = B_TRUE; /* adding a protocol is a change */
8293034Sdougm 	}
8304653Sdougm 	if (optionset == NULL) {
8314653Sdougm 		ret = SA_NO_MEMORY;
8324653Sdougm 		goto out;
8334653Sdougm 	}
834*6214Sdougm 	handle = sa_find_group_handle(group);
835*6214Sdougm 	if (handle == NULL) {
836*6214Sdougm 		ret = SA_CONFIG_ERR;
837*6214Sdougm 		goto out;
838*6214Sdougm 	}
8394653Sdougm 	while (optlist != NULL) {
8403034Sdougm 		sa_property_t prop;
8413034Sdougm 		prop = sa_get_property(optionset, optlist->optname);
8423034Sdougm 		if (prop == NULL) {
8433034Sdougm 			/*
8443034Sdougm 			 * add the property, but only if it is
8453034Sdougm 			 * a non-NULL or non-zero length value
8463034Sdougm 			 */
8474653Sdougm 			if (optlist->optvalue != NULL) {
8484653Sdougm 				prop = sa_create_property(optlist->optname,
8494653Sdougm 				    optlist->optvalue);
8504653Sdougm 				if (prop != NULL) {
851*6214Sdougm 					ret = sa_valid_property(handle,
852*6214Sdougm 					    optionset, proto, prop);
8534653Sdougm 					if (ret != SA_OK) {
8544653Sdougm 						(void) sa_remove_property(prop);
8554653Sdougm 						(void) printf(gettext("Could "
8564653Sdougm 						    "not add property "
8574653Sdougm 						    "%s: %s\n"),
8584653Sdougm 						    optlist->optname,
8594653Sdougm 						    sa_errorstr(ret));
8604653Sdougm 					}
8614653Sdougm 				}
8624653Sdougm 				if (ret == SA_OK) {
8634653Sdougm 					ret = sa_add_property(optionset, prop);
8644653Sdougm 					if (ret != SA_OK) {
8654653Sdougm 						(void) printf(gettext(
8664653Sdougm 						    "Could not add property "
8674653Sdougm 						    "%s: %s\n"),
8684653Sdougm 						    optlist->optname,
8694653Sdougm 						    sa_errorstr(ret));
8704653Sdougm 					} else {
8714653Sdougm 						/* there was a change */
8725331Samw 						result = B_TRUE;
8734653Sdougm 					}
8744653Sdougm 				}
8753034Sdougm 			}
8764653Sdougm 		} else {
8774653Sdougm 			ret = sa_update_property(prop, optlist->optvalue);
8784653Sdougm 			/* should check to see if value changed */
8794653Sdougm 			if (ret != SA_OK) {
8804653Sdougm 				(void) printf(gettext("Could not update "
8814653Sdougm 				    "property %s: %s\n"), optlist->optname,
8824653Sdougm 				    sa_errorstr(ret));
8834653Sdougm 			} else {
8845331Samw 				result = B_TRUE;
8853034Sdougm 			}
8863034Sdougm 		}
8873034Sdougm 		optlist = optlist->next;
8883034Sdougm 	}
8894653Sdougm 	ret = sa_commit_properties(optionset, 0);
8904653Sdougm 
8914653Sdougm out:
8923034Sdougm 	if (err != NULL)
8934653Sdougm 		*err = ret;
8943034Sdougm 	return (result);
8953034Sdougm }
8963034Sdougm 
8973034Sdougm /*
8985331Samw  * resource_compliant(group)
8995331Samw  *
9005331Samw  * Go through all the shares in the group. Assume compliant, but if
9015331Samw  * any share doesn't have at least one resource name, it isn't
9025331Samw  * compliant.
9035331Samw  */
9045331Samw static int
9055331Samw resource_compliant(sa_group_t group)
9065331Samw {
9075331Samw 	sa_share_t share;
9085331Samw 
9095331Samw 	for (share = sa_get_share(group, NULL); share != NULL;
9105331Samw 	    share = sa_get_next_share(share)) {
9115331Samw 		if (sa_get_share_resource(share, NULL) == NULL) {
9125331Samw 			return (B_FALSE);
9135331Samw 		}
9145331Samw 	}
9155331Samw 	return (B_TRUE);
9165331Samw }
9175331Samw 
9185331Samw /*
9195331Samw  * fix_path(path)
9205331Samw  *
9215331Samw  * change all illegal characters to something else.  For now, all get
9225331Samw  * converted to '_' and the leading '/' is stripped off. This is used
9235331Samw  * to construct an resource name (SMB share name) that is valid.
9245331Samw  * Caller must pass a valid path.
9255331Samw  */
9265331Samw static void
9275331Samw fix_path(char *path)
9285331Samw {
9295331Samw 	char *cp;
9305331Samw 	size_t len;
9315331Samw 
9325331Samw 	assert(path != NULL);
9335331Samw 
9345331Samw 	/* make sure we are appropriate length */
9355331Samw 	cp = path + 1; /* skip leading slash */
9365331Samw 	while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) {
9375331Samw 		cp = strchr(cp, '/');
9385331Samw 		if (cp != NULL)
9395331Samw 			cp++;
9405331Samw 	}
9415331Samw 	/* two cases - cp == NULL and cp is substring of path */
9425331Samw 	if (cp == NULL) {
9435331Samw 		/* just take last SA_MAX_RESOURCE_NAME chars */
9445331Samw 		len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME;
9455331Samw 		(void) memmove(path, path + len, SA_MAX_RESOURCE_NAME);
9465331Samw 		path[SA_MAX_RESOURCE_NAME] = '\0';
9475331Samw 	} else {
9485331Samw 		len = strlen(cp) + 1;
9495331Samw 		(void) memmove(path, cp, len);
9505331Samw 	}
9515331Samw 
9525331Samw 	/*
9535331Samw 	 * Don't want any of the characters that are not allowed
9545331Samw 	 * in and SMB share name. Replace them with '_'.
9555331Samw 	 */
9565331Samw 	while (*path) {
9575331Samw 		switch (*path) {
9585331Samw 		case '/':
9595331Samw 		case '"':
9605331Samw 		case '\\':
9615331Samw 		case '[':
9625331Samw 		case ']':
9635331Samw 		case ':':
9645331Samw 		case '|':
9655331Samw 		case '<':
9665331Samw 		case '>':
9675331Samw 		case '+':
9685331Samw 		case ';':
9695331Samw 		case ',':
9705331Samw 		case '?':
9715331Samw 		case '*':
9725331Samw 		case '=':
9735331Samw 		case '\t':
9745331Samw 			*path = '_';
9755331Samw 			break;
9765331Samw 		}
9775331Samw 		path++;
9785331Samw 	}
9795331Samw }
9805331Samw 
9815331Samw /*
9825331Samw  * name_adjust(path, count)
9835331Samw  *
9845331Samw  * Add a ~<count> in place of last few characters. The total number of
9855331Samw  * characters is dependent on count.
9865331Samw  */
9875331Samw #define	MAX_MANGLE_NUMBER	10000
9885331Samw 
9895331Samw static int
9905331Samw name_adjust(char *path, int count)
9915331Samw {
9925331Samw 	size_t len;
9935331Samw 
9945331Samw 	len = strlen(path) - 2;
9955331Samw 	if (count > 10)
9965331Samw 		len--;
9975331Samw 	if (count > 100)
9985331Samw 		len--;
9995331Samw 	if (count > 1000)
10005331Samw 		len--;
10015331Samw 	if (len > 0)
10025331Samw 		(void) sprintf(path + len, "~%d", count);
10035331Samw 	else
10045331Samw 		return (SA_BAD_VALUE);
10055331Samw 
10065331Samw 	return (SA_OK);
10075331Samw }
10085331Samw 
10095331Samw /*
10105331Samw  * make_resources(group)
10115331Samw  *
10125331Samw  * Go through all the shares in the group and make them have resource
10135331Samw  * names.
10145331Samw  */
10155331Samw static void
10165331Samw make_resources(sa_group_t group)
10175331Samw {
10185331Samw 	sa_share_t share;
10195331Samw 	int count;
10205331Samw 	int err = SA_OK;
10215331Samw 
10225331Samw 	for (share = sa_get_share(group, NULL); share != NULL;
10235331Samw 	    share = sa_get_next_share(share)) {
10245331Samw 		/* Skip those with resources */
10255331Samw 		if (sa_get_share_resource(share, NULL) == NULL) {
10265331Samw 			char *path;
10275331Samw 			path = sa_get_share_attr(share, "path");
10285331Samw 			if (path == NULL)
10295331Samw 				continue;
10305331Samw 			fix_path(path);
10315331Samw 			count = 0;	/* reset for next resource */
10325331Samw 			while (sa_add_resource(share, path,
10335331Samw 			    SA_SHARE_PERMANENT, &err) == NULL &&
10345331Samw 			    err == SA_DUPLICATE_NAME) {
10355331Samw 				int ret;
10365331Samw 				ret = name_adjust(path, count);
10375331Samw 				count++;
10385331Samw 				if (ret != SA_OK ||
10395331Samw 				    count >= MAX_MANGLE_NUMBER) {
10405331Samw 					(void) printf(gettext(
10415331Samw 					    "Cannot create resource name for"
10425331Samw 					    " path: %s\n"), path);
10435331Samw 					break;
10445331Samw 				}
10455331Samw 			}
10465331Samw 			sa_free_attr_string(path);
10475331Samw 		}
10485331Samw 	}
10495331Samw }
10505331Samw 
10515331Samw /*
10526088Sdougm  * check_valid_group(group, protocol)
10536088Sdougm  *
10546088Sdougm  * Check to see that the group should have the protocol added (if
10556088Sdougm  * there is one specified).
10566088Sdougm  */
10576088Sdougm 
10586088Sdougm static int
10596088Sdougm check_valid_group(sa_group_t group, char *groupname, char *protocol)
10606088Sdougm {
10616088Sdougm 
10626088Sdougm 	if (protocol != NULL) {
10636088Sdougm 		if (has_protocol(group, protocol)) {
10646088Sdougm 			(void) printf(gettext(
10656088Sdougm 			    "Group \"%s\" already exists"
10666088Sdougm 			    " with protocol %s\n"), groupname,
10676088Sdougm 			    protocol);
10686088Sdougm 			return (SA_DUPLICATE_NAME);
10696088Sdougm 		} else if (strcmp(groupname, "default") == 0 &&
10706088Sdougm 		    strcmp(protocol, "nfs") != 0) {
10716088Sdougm 			(void) printf(gettext(
10726088Sdougm 			    "Group \"%s\" only allows protocol "
10736088Sdougm 			    "\"%s\"\n"), groupname, "nfs");
10746088Sdougm 			return (SA_INVALID_PROTOCOL);
10756088Sdougm 		}
10766088Sdougm 	} else {
10776088Sdougm 		/* must add new protocol */
10786088Sdougm 		(void) printf(gettext(
10796088Sdougm 		    "Group already exists and no protocol "
10806088Sdougm 		    "specified.\n"));
10816088Sdougm 		return (SA_DUPLICATE_NAME);
10826088Sdougm 	}
10836088Sdougm 	return (SA_OK);
10846088Sdougm }
10856088Sdougm 
10866088Sdougm /*
10876088Sdougm  * enforce_featureset(group, protocol, dryrun, force)
10886088Sdougm  *
10896088Sdougm  * Check the protocol featureset against the group and enforce any
10906088Sdougm  * rules that might be imposed.
10916088Sdougm  */
10926088Sdougm 
10936088Sdougm static int
10946088Sdougm enforce_featureset(sa_group_t group, char *protocol, boolean_t dryrun,
10956088Sdougm     boolean_t force)
10966088Sdougm {
10976088Sdougm 	uint64_t features;
10986088Sdougm 
10996088Sdougm 	if (protocol == NULL)
11006088Sdougm 		return (SA_OK);
11016088Sdougm 
11026088Sdougm 	/*
11036088Sdougm 	 * First check to see if specified protocol is one we want to
11046088Sdougm 	 * allow on a group. Only server protocols are allowed here.
11056088Sdougm 	 */
11066088Sdougm 	features = sa_proto_get_featureset(protocol);
11076088Sdougm 	if (!(features & SA_FEATURE_SERVER)) {
11086088Sdougm 		(void) printf(
11096088Sdougm 		    gettext("Protocol \"%s\" not supported.\n"), protocol);
11106088Sdougm 		return (SA_INVALID_PROTOCOL);
11116088Sdougm 	}
11126088Sdougm 
11136088Sdougm 	/*
11146088Sdougm 	 * Check to see if the new protocol is one that requires
11156088Sdougm 	 * resource names and make sure we are compliant before
11166088Sdougm 	 * proceeding.
11176088Sdougm 	 */
11186088Sdougm 	if ((features & SA_FEATURE_RESOURCE) &&
11196088Sdougm 	    !resource_compliant(group)) {
11206088Sdougm 		if (force && !dryrun) {
11216088Sdougm 			make_resources(group);
11226088Sdougm 		} else {
11236088Sdougm 			(void) printf(
11246088Sdougm 			    gettext("Protocol requires resource names to be "
11256088Sdougm 			    "set: %s\n"), protocol);
11266088Sdougm 			return (SA_RESOURCE_REQUIRED);
11276088Sdougm 		}
11286088Sdougm 	}
11296088Sdougm 	return (SA_OK);
11306088Sdougm }
11316088Sdougm 
11326088Sdougm /*
11336088Sdougm  * set_all_protocols(group)
11346088Sdougm  *
11356088Sdougm  * Get the list of all protocols and add all server protocols to the
11366088Sdougm  * group.
11376088Sdougm  */
11386088Sdougm 
11396088Sdougm static int
11406088Sdougm set_all_protocols(sa_group_t group)
11416088Sdougm {
11426088Sdougm 	char **protolist;
11436088Sdougm 	int numprotos, i;
11446088Sdougm 	uint64_t features;
11456088Sdougm 	sa_optionset_t optionset;
11466088Sdougm 	int ret = SA_OK;
11476088Sdougm 
11486088Sdougm 	/*
11496088Sdougm 	 * Now make sure we really want to put this protocol on a
11506088Sdougm 	 * group. Only server protocols can go here.
11516088Sdougm 	 */
11526088Sdougm 	numprotos = sa_get_protocols(&protolist);
11536088Sdougm 	for (i = 0; i < numprotos; i++) {
11546088Sdougm 		features = sa_proto_get_featureset(protolist[i]);
11556088Sdougm 		if (features & SA_FEATURE_SERVER) {
11566088Sdougm 			optionset = sa_create_optionset(group, protolist[i]);
11576088Sdougm 			if (optionset == NULL) {
11586088Sdougm 				ret = SA_NO_MEMORY;
11596088Sdougm 				break;
11606088Sdougm 			}
11616088Sdougm 		}
11626088Sdougm 	}
11636088Sdougm 
11646088Sdougm 	if (protolist != NULL)
11656088Sdougm 		free(protolist);
11666088Sdougm 
11676088Sdougm 	return (ret);
11686088Sdougm }
11696088Sdougm 
11706088Sdougm /*
11713034Sdougm  * sa_create(flags, argc, argv)
11723034Sdougm  *	create a new group
11733034Sdougm  *	this may or may not have a protocol associated with it.
11743034Sdougm  *	No protocol means "all" protocols in this case.
11753034Sdougm  */
11763034Sdougm static int
11773910Sdougm sa_create(sa_handle_t handle, int flags, int argc, char *argv[])
11783034Sdougm {
11793034Sdougm 	char *groupname;
11803034Sdougm 
11813034Sdougm 	sa_group_t group;
11826088Sdougm 	boolean_t force = B_FALSE;
11836088Sdougm 	boolean_t verbose = B_FALSE;
11846088Sdougm 	boolean_t dryrun = B_FALSE;
11853034Sdougm 	int c;
11863034Sdougm 	char *protocol = NULL;
11873034Sdougm 	int ret = SA_OK;
11883034Sdougm 	struct options *optlist = NULL;
11896019Sdougm 	int err = SA_OK;
11903034Sdougm 	int auth;
11916088Sdougm 	boolean_t created = B_FALSE;
11923034Sdougm 
11935331Samw 	while ((c = getopt(argc, argv, "?fhvnP:p:")) != EOF) {
11944653Sdougm 		switch (c) {
11955331Samw 		case 'f':
11966088Sdougm 			force = B_TRUE;
11975331Samw 			break;
11984653Sdougm 		case 'v':
11996088Sdougm 			verbose = B_TRUE;
12004653Sdougm 			break;
12014653Sdougm 		case 'n':
12026088Sdougm 			dryrun = B_TRUE;
12034653Sdougm 			break;
12044653Sdougm 		case 'P':
12055331Samw 			if (protocol != NULL) {
12065331Samw 				(void) printf(gettext("Specifying "
12075331Samw 				    "multiple protocols "
12085331Samw 				    "not supported: %s\n"), protocol);
12095331Samw 				return (SA_SYNTAX_ERR);
12105331Samw 			}
12114653Sdougm 			protocol = optarg;
12124653Sdougm 			if (sa_valid_protocol(protocol))
12134653Sdougm 				break;
12144653Sdougm 			(void) printf(gettext(
12154653Sdougm 			    "Invalid protocol specified: %s\n"), protocol);
12164653Sdougm 			return (SA_INVALID_PROTOCOL);
12174653Sdougm 			break;
12184653Sdougm 		case 'p':
12194653Sdougm 			ret = add_opt(&optlist, optarg, 0);
12204653Sdougm 			switch (ret) {
12214653Sdougm 			case OPT_ADD_SYNTAX:
12224653Sdougm 				(void) printf(gettext(
12234653Sdougm 				    "Property syntax error for property: %s\n"),
12244653Sdougm 				    optarg);
12254653Sdougm 				return (SA_SYNTAX_ERR);
12264653Sdougm 			case OPT_ADD_SECURITY:
12274653Sdougm 				(void) printf(gettext(
12284653Sdougm 				    "Security properties need "
12294653Sdougm 				    "to be set with set-security: %s\n"),
12304653Sdougm 				    optarg);
12314653Sdougm 				return (SA_SYNTAX_ERR);
12324653Sdougm 			default:
12334653Sdougm 				break;
12344653Sdougm 			}
12354653Sdougm 			break;
12366019Sdougm 		case 'h':
12376019Sdougm 			/* optopt on valid arg isn't defined */
12386019Sdougm 			optopt = c;
12396019Sdougm 			/*FALLTHROUGH*/
12406019Sdougm 		case '?':
12414653Sdougm 		default:
12426019Sdougm 			/*
12436019Sdougm 			 * Since a bad option gets to here, sort it
12446019Sdougm 			 * out and return a syntax error return value
12456019Sdougm 			 * if necessary.
12466019Sdougm 			 */
12476019Sdougm 			switch (optopt) {
12486019Sdougm 			default:
12496019Sdougm 				err = SA_SYNTAX_ERR;
12506019Sdougm 				break;
12516019Sdougm 			case 'h':
12526019Sdougm 			case '?':
12536019Sdougm 				break;
12546019Sdougm 			}
12554653Sdougm 			(void) printf(gettext("usage: %s\n"),
12564653Sdougm 			    sa_get_usage(USAGE_CREATE));
12576019Sdougm 			return (err);
12583034Sdougm 		}
12593034Sdougm 	}
12603034Sdougm 
12613034Sdougm 	if (optind >= argc) {
12624653Sdougm 		(void) printf(gettext("usage: %s\n"),
12634653Sdougm 		    sa_get_usage(USAGE_CREATE));
12644653Sdougm 		(void) printf(gettext("\tgroup must be specified.\n"));
12654653Sdougm 		return (SA_BAD_PATH);
12663034Sdougm 	}
12673034Sdougm 
12683034Sdougm 	if ((optind + 1) < argc) {
12694653Sdougm 		(void) printf(gettext("usage: %s\n"),
12704653Sdougm 		    sa_get_usage(USAGE_CREATE));
12714653Sdougm 		(void) printf(gettext("\textraneous group(s) at end\n"));
12724653Sdougm 		return (SA_SYNTAX_ERR);
12733034Sdougm 	}
12743034Sdougm 
12753034Sdougm 	if (protocol == NULL && optlist != NULL) {
12764653Sdougm 		/* lookup default protocol */
12774653Sdougm 		(void) printf(gettext("usage: %s\n"),
12784653Sdougm 		    sa_get_usage(USAGE_CREATE));
12794653Sdougm 		(void) printf(gettext("\tprotocol must be specified "
12804653Sdougm 		    "with properties\n"));
12814653Sdougm 		return (SA_INVALID_PROTOCOL);
12823034Sdougm 	}
12833034Sdougm 
12843034Sdougm 	if (optlist != NULL)
12854653Sdougm 		ret = chk_opt(optlist, 0, protocol);
12863034Sdougm 	if (ret == OPT_ADD_SECURITY) {
12874653Sdougm 		(void) printf(gettext("Security properties not "
12884653Sdougm 		    "supported with create\n"));
12894653Sdougm 		return (SA_SYNTAX_ERR);
12903034Sdougm 	}
12913034Sdougm 
12923034Sdougm 	/*
12934653Sdougm 	 * If a group already exists, we can only add a new protocol
12943034Sdougm 	 * to it and not create a new one or add the same protocol
12953034Sdougm 	 * again.
12963034Sdougm 	 */
12973034Sdougm 
12983034Sdougm 	groupname = argv[optind];
12993034Sdougm 
13003034Sdougm 	auth = check_authorizations(groupname, flags);
13013034Sdougm 
13023910Sdougm 	group = sa_get_group(handle, groupname);
13033034Sdougm 	if (group != NULL) {
13044653Sdougm 		/* group exists so must be a protocol add */
13056088Sdougm 		ret = check_valid_group(group, groupname, protocol);
13063034Sdougm 	} else {
13073034Sdougm 		/*
13083034Sdougm 		 * is it a valid name? Must comply with SMF instance
13093034Sdougm 		 * name restrictions.
13103034Sdougm 		 */
13114653Sdougm 		if (!sa_valid_group_name(groupname)) {
13124653Sdougm 			ret = SA_INVALID_NAME;
13134653Sdougm 			(void) printf(gettext("Invalid group name: %s\n"),
13144653Sdougm 			    groupname);
13154653Sdougm 		}
13163034Sdougm 	}
13173034Sdougm 	if (ret == SA_OK) {
13184653Sdougm 		/* check protocol vs optlist */
13194653Sdougm 		if (optlist != NULL) {
13204653Sdougm 			/* check options, if any, for validity */
1321*6214Sdougm 			ret = valid_options(handle, optlist, protocol,
1322*6214Sdougm 			    group, NULL);
13234653Sdougm 		}
13243034Sdougm 	}
13253034Sdougm 	if (ret == SA_OK && !dryrun) {
13264653Sdougm 		if (group == NULL) {
13274653Sdougm 			group = sa_create_group(handle, (char *)groupname,
13284653Sdougm 			    &err);
13296088Sdougm 			created = B_TRUE;
13303034Sdougm 		}
13314653Sdougm 		if (group != NULL) {
13324653Sdougm 			sa_optionset_t optionset;
13336088Sdougm 
13345331Samw 			/*
13356088Sdougm 			 * Check group and protocol against featureset
13366088Sdougm 			 * requirements.
13375331Samw 			 */
13386088Sdougm 			ret = enforce_featureset(group, protocol,
13396088Sdougm 			    dryrun, force);
13406088Sdougm 			if (ret != SA_OK)
13416088Sdougm 				goto err;
13426088Sdougm 
13436088Sdougm 			/*
13446088Sdougm 			 * So far so good. Now add the required
13456088Sdougm 			 * optionset(s) to the group.
13466088Sdougm 			 */
13474653Sdougm 			if (optlist != NULL) {
13484653Sdougm 				(void) add_optionset(group, optlist, protocol,
13494653Sdougm 				    &ret);
13504653Sdougm 			} else if (protocol != NULL) {
13514653Sdougm 				optionset = sa_create_optionset(group,
13524653Sdougm 				    protocol);
13534653Sdougm 				if (optionset == NULL)
13544653Sdougm 					ret = SA_NO_MEMORY;
13554653Sdougm 			} else if (protocol == NULL) {
13566088Sdougm 				/* default group create so add all protocols */
13576088Sdougm 				ret = set_all_protocols(group);
13584653Sdougm 			}
13593034Sdougm 			/*
13604653Sdougm 			 * We have a group and legal additions
13613034Sdougm 			 */
13624653Sdougm 			if (ret == SA_OK) {
13634653Sdougm 				/*
13644653Sdougm 				 * Commit to configuration for protocols that
13654653Sdougm 				 * need to do block updates. For NFS, this
13664653Sdougm 				 * doesn't do anything but it will be run for
13674653Sdougm 				 * all protocols that implement the
13684653Sdougm 				 * appropriate plugin.
13694653Sdougm 				 */
13704653Sdougm 				ret = sa_update_config(handle);
13714653Sdougm 			} else {
13724653Sdougm 				if (group != NULL)
13734653Sdougm 					(void) sa_remove_group(group);
13744653Sdougm 			}
13753034Sdougm 		} else {
13764653Sdougm 			ret = err;
13774653Sdougm 			(void) printf(gettext("Could not create group: %s\n"),
13784653Sdougm 			    sa_errorstr(ret));
13793034Sdougm 		}
13803034Sdougm 	}
13813034Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose) {
13824653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
13834653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
13844653Sdougm 		ret = SA_NO_PERMISSION;
13853034Sdougm 	}
13865331Samw err:
13876088Sdougm 	if (ret != SA_OK && created)
13886088Sdougm 		ret = sa_remove_group(group);
13896088Sdougm 
13903034Sdougm 	free_opt(optlist);
13913034Sdougm 	return (ret);
13923034Sdougm }
13933034Sdougm 
13943034Sdougm /*
13953034Sdougm  * group_status(group)
13963034Sdougm  *
13973034Sdougm  * return the current status (enabled/disabled) of the group.
13983034Sdougm  */
13993034Sdougm 
14003034Sdougm static char *
14013034Sdougm group_status(sa_group_t group)
14023034Sdougm {
14033034Sdougm 	char *state;
14043034Sdougm 	int enabled = 0;
14053034Sdougm 
14063034Sdougm 	state = sa_get_group_attr(group, "state");
14073034Sdougm 	if (state != NULL) {
14084653Sdougm 		if (strcmp(state, "enabled") == 0) {
14094653Sdougm 			enabled = 1;
14104653Sdougm 		}
14114653Sdougm 		sa_free_attr_string(state);
14123034Sdougm 	}
14134255Sdougm 	return (enabled ? "enabled" : "disabled");
14143034Sdougm }
14153034Sdougm 
14163034Sdougm /*
14173034Sdougm  * sa_delete(flags, argc, argv)
14183034Sdougm  *
14193034Sdougm  *	Delete a group.
14203034Sdougm  */
14213034Sdougm 
14223034Sdougm static int
14233910Sdougm sa_delete(sa_handle_t handle, int flags, int argc, char *argv[])
14243034Sdougm {
14253034Sdougm 	char *groupname;
14263034Sdougm 	sa_group_t group;
14273034Sdougm 	sa_share_t share;
14283034Sdougm 	int verbose = 0;
14293034Sdougm 	int dryrun = 0;
14303034Sdougm 	int force = 0;
14313034Sdougm 	int c;
14323034Sdougm 	char *protocol = NULL;
14333034Sdougm 	char *sectype = NULL;
14343034Sdougm 	int ret = SA_OK;
14353034Sdougm 	int auth;
14363034Sdougm 
14373034Sdougm 	while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) {
14384653Sdougm 		switch (c) {
14394653Sdougm 		case 'v':
14404653Sdougm 			verbose++;
14414653Sdougm 			break;
14424653Sdougm 		case 'n':
14434653Sdougm 			dryrun++;
14444653Sdougm 			break;
14454653Sdougm 		case 'P':
14465331Samw 			if (protocol != NULL) {
14475331Samw 				(void) printf(gettext("Specifying "
14485331Samw 				    "multiple protocols "
14495331Samw 				    "not supported: %s\n"), protocol);
14505331Samw 				return (SA_SYNTAX_ERR);
14515331Samw 			}
14524653Sdougm 			protocol = optarg;
14534653Sdougm 			if (!sa_valid_protocol(protocol)) {
14544653Sdougm 				(void) printf(gettext("Invalid protocol "
14555331Samw 				    "specified: %s\n"), protocol);
14564653Sdougm 				return (SA_INVALID_PROTOCOL);
14574653Sdougm 			}
14584653Sdougm 			break;
14594653Sdougm 		case 'S':
14605331Samw 			if (sectype != NULL) {
14615331Samw 				(void) printf(gettext("Specifying "
14625331Samw 				    "multiple property "
14635331Samw 				    "spaces not supported: %s\n"), sectype);
14645331Samw 				return (SA_SYNTAX_ERR);
14655331Samw 			}
14664653Sdougm 			sectype = optarg;
14674653Sdougm 			break;
14684653Sdougm 		case 'f':
14694653Sdougm 			force++;
14704653Sdougm 			break;
14716019Sdougm 		case 'h':
14726019Sdougm 			/* optopt on valid arg isn't defined */
14736019Sdougm 			optopt = c;
14746019Sdougm 			/*FALLTHROUGH*/
14756019Sdougm 		case '?':
14764653Sdougm 		default:
14776019Sdougm 			/*
14786019Sdougm 			 * Since a bad option gets to here, sort it
14796019Sdougm 			 * out and return a syntax error return value
14806019Sdougm 			 * if necessary.
14816019Sdougm 			 */
14826019Sdougm 			switch (optopt) {
14836019Sdougm 			default:
14846019Sdougm 				ret = SA_SYNTAX_ERR;
14856019Sdougm 				break;
14866019Sdougm 			case 'h':
14876019Sdougm 			case '?':
14886019Sdougm 				break;
14896019Sdougm 			}
14904653Sdougm 			(void) printf(gettext("usage: %s\n"),
14914653Sdougm 			    sa_get_usage(USAGE_DELETE));
14926019Sdougm 			return (ret);
14933034Sdougm 		}
14943034Sdougm 	}
14953034Sdougm 
14963034Sdougm 	if (optind >= argc) {
14974653Sdougm 		(void) printf(gettext("usage: %s\n"),
14984653Sdougm 		    sa_get_usage(USAGE_DELETE));
14994653Sdougm 		(void) printf(gettext("\tgroup must be specified.\n"));
15004653Sdougm 		return (SA_SYNTAX_ERR);
15013034Sdougm 	}
15023034Sdougm 
15033034Sdougm 	if ((optind + 1) < argc) {
15044653Sdougm 		(void) printf(gettext("usage: %s\n"),
15054653Sdougm 		    sa_get_usage(USAGE_DELETE));
15064653Sdougm 		(void) printf(gettext("\textraneous group(s) at end\n"));
15074653Sdougm 		return (SA_SYNTAX_ERR);
15083034Sdougm 	}
15093034Sdougm 
15103034Sdougm 	if (sectype != NULL && protocol == NULL) {
15114653Sdougm 		(void) printf(gettext("usage: %s\n"),
15124653Sdougm 		    sa_get_usage(USAGE_DELETE));
15134653Sdougm 		(void) printf(gettext("\tsecurity requires protocol to be "
15144653Sdougm 		    "specified.\n"));
15154653Sdougm 		return (SA_SYNTAX_ERR);
15163034Sdougm 	}
15173034Sdougm 
15183034Sdougm 	/*
15193034Sdougm 	 * Determine if the group already exists since it must in
15203034Sdougm 	 * order to be removed.
15213034Sdougm 	 *
15223034Sdougm 	 * We can delete when:
15233034Sdougm 	 *
15243034Sdougm 	 *	- group is empty
15253034Sdougm 	 *	- force flag is set
15263034Sdougm 	 *	- if protocol specified, only delete the protocol
15273034Sdougm 	 */
15283034Sdougm 
15293034Sdougm 	groupname = argv[optind];
15303910Sdougm 	group = sa_get_group(handle, groupname);
15313034Sdougm 	if (group == NULL) {
15323034Sdougm 		ret = SA_NO_SUCH_GROUP;
15334653Sdougm 		goto done;
15344653Sdougm 	}
15354653Sdougm 	auth = check_authorizations(groupname, flags);
15364653Sdougm 	if (protocol == NULL) {
15373034Sdougm 		share = sa_get_share(group, NULL);
15383034Sdougm 		if (share != NULL)
15394653Sdougm 			ret = SA_BUSY;
15403034Sdougm 		if (share == NULL || (share != NULL && force == 1)) {
15414653Sdougm 			ret = SA_OK;
15424653Sdougm 			if (!dryrun) {
15434653Sdougm 				while (share != NULL) {
15444653Sdougm 					sa_share_t next_share;
15454653Sdougm 					next_share = sa_get_next_share(share);
15464653Sdougm 					/*
15474653Sdougm 					 * need to do the disable of
15484653Sdougm 					 * each share, but don't
15494653Sdougm 					 * actually do anything on a
15504653Sdougm 					 * dryrun.
15514653Sdougm 					 */
15524653Sdougm 					ret = sa_disable_share(share, NULL);
15534653Sdougm 					ret = sa_remove_share(share);
15544653Sdougm 					share = next_share;
15554653Sdougm 				}
15564653Sdougm 				ret = sa_remove_group(group);
15573034Sdougm 			}
15583034Sdougm 		}
15594653Sdougm 		/* Commit to configuration if not a dryrun */
15603034Sdougm 		if (!dryrun && ret == SA_OK) {
15614653Sdougm 			ret = sa_update_config(handle);
15623034Sdougm 		}
15634653Sdougm 	} else {
15643034Sdougm 		/* a protocol delete */
15653034Sdougm 		sa_optionset_t optionset;
15663034Sdougm 		sa_security_t security;
15675331Samw 		if (sectype != NULL) {
15684653Sdougm 			/* only delete specified security */
15694653Sdougm 			security = sa_get_security(group, sectype, protocol);
15704653Sdougm 			if (security != NULL && !dryrun)
15714653Sdougm 				ret = sa_destroy_security(security);
15724653Sdougm 			else
15734653Sdougm 				ret = SA_INVALID_PROTOCOL;
15743034Sdougm 		} else {
15754653Sdougm 			optionset = sa_get_optionset(group, protocol);
15764653Sdougm 			if (optionset != NULL && !dryrun) {
15774653Sdougm 				/*
15784653Sdougm 				 * have an optionset with
15794653Sdougm 				 * protocol to delete
15804653Sdougm 				 */
15814653Sdougm 				ret = sa_destroy_optionset(optionset);
15824653Sdougm 				/*
15834653Sdougm 				 * Now find all security sets
15844653Sdougm 				 * for the protocol and remove
15854653Sdougm 				 * them. Don't remove other
15864653Sdougm 				 * protocols.
15874653Sdougm 				 */
15884653Sdougm 				for (security =
15894653Sdougm 				    sa_get_security(group, NULL, NULL);
15904653Sdougm 				    ret == SA_OK && security != NULL;
15914653Sdougm 				    security = sa_get_next_security(security)) {
15924653Sdougm 					char *secprot;
15934653Sdougm 					secprot = sa_get_security_attr(security,
15944653Sdougm 					    "type");
15954653Sdougm 					if (secprot != NULL &&
15964653Sdougm 					    strcmp(secprot, protocol) == 0)
15974653Sdougm 						ret = sa_destroy_security(
15984653Sdougm 						    security);
15994653Sdougm 					if (secprot != NULL)
16004653Sdougm 						sa_free_attr_string(secprot);
16014653Sdougm 				}
16024653Sdougm 			} else {
16034653Sdougm 				if (!dryrun)
16044653Sdougm 					ret = SA_INVALID_PROTOCOL;
16053034Sdougm 			}
16063034Sdougm 		}
16075331Samw 		/*
16085331Samw 		 * With the protocol items removed, make sure that all
16095331Samw 		 * the shares are updated in the legacy files, if
16105331Samw 		 * necessary.
16115331Samw 		 */
16125331Samw 		for (share = sa_get_share(group, NULL);
16135331Samw 		    share != NULL;
16145331Samw 		    share = sa_get_next_share(share)) {
16155331Samw 			(void) sa_delete_legacy(share, protocol);
16165331Samw 		}
16173034Sdougm 	}
16184653Sdougm 
16194653Sdougm done:
16203034Sdougm 	if (ret != SA_OK) {
16214653Sdougm 		(void) printf(gettext("Could not delete group: %s\n"),
16224653Sdougm 		    sa_errorstr(ret));
16233034Sdougm 	} else if (dryrun && !auth && verbose) {
16244653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
16254653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
16263034Sdougm 	}
16273034Sdougm 	return (ret);
16283034Sdougm }
16293034Sdougm 
16303034Sdougm /*
16313034Sdougm  * strndupr(*buff, str, buffsize)
16323034Sdougm  *
16333034Sdougm  * used with small strings to duplicate and possibly increase the
16343034Sdougm  * buffer size of a string.
16353034Sdougm  */
16363034Sdougm static char *
16373034Sdougm strndupr(char *buff, char *str, int *buffsize)
16383034Sdougm {
16393034Sdougm 	int limit;
16403034Sdougm 	char *orig_buff = buff;
16413034Sdougm 
16423034Sdougm 	if (buff == NULL) {
16434653Sdougm 		buff = (char *)malloc(64);
16444653Sdougm 		if (buff == NULL)
16454653Sdougm 			return (NULL);
16464653Sdougm 		*buffsize = 64;
16474653Sdougm 		buff[0] = '\0';
16483034Sdougm 	}
16493034Sdougm 	limit = strlen(buff) + strlen(str) + 1;
16503034Sdougm 	if (limit > *buffsize) {
16514653Sdougm 		limit = *buffsize = *buffsize + ((limit / 64) + 64);
16524653Sdougm 		buff = realloc(buff, limit);
16533034Sdougm 	}
16543034Sdougm 	if (buff != NULL) {
16554653Sdougm 		(void) strcat(buff, str);
16563034Sdougm 	} else {
16574653Sdougm 		/* if it fails, fail it hard */
16584653Sdougm 		if (orig_buff != NULL)
16594653Sdougm 			free(orig_buff);
16603034Sdougm 	}
16613034Sdougm 	return (buff);
16623034Sdougm }
16633034Sdougm 
16643034Sdougm /*
16653034Sdougm  * group_proto(group)
16663034Sdougm  *
16673034Sdougm  * return a string of all the protocols (space separated) associated
16683034Sdougm  * with this group.
16693034Sdougm  */
16703034Sdougm 
16713034Sdougm static char *
16723034Sdougm group_proto(sa_group_t group)
16733034Sdougm {
16743034Sdougm 	sa_optionset_t optionset;
16753034Sdougm 	char *proto;
16763034Sdougm 	char *buff = NULL;
16773034Sdougm 	int buffsize = 0;
16783034Sdougm 	int addspace = 0;
16793034Sdougm 	/*
16803034Sdougm 	 * get the protocol list by finding the optionsets on this
16813034Sdougm 	 * group and extracting the type value. The initial call to
16823034Sdougm 	 * strndupr() initailizes buff.
16833034Sdougm 	 */
16843034Sdougm 	buff = strndupr(buff, "", &buffsize);
16853034Sdougm 	if (buff != NULL) {
16864653Sdougm 		for (optionset = sa_get_optionset(group, NULL);
16874653Sdougm 		    optionset != NULL && buff != NULL;
16884653Sdougm 		    optionset = sa_get_next_optionset(optionset)) {
16894653Sdougm 			/*
16904653Sdougm 			 * extract out the protocol type from this optionset
16914653Sdougm 			 * and append it to the buffer "buff". strndupr() will
16924653Sdougm 			 * reallocate space as necessay.
16934653Sdougm 			 */
16944653Sdougm 			proto = sa_get_optionset_attr(optionset, "type");
16954653Sdougm 			if (proto != NULL) {
16964653Sdougm 				if (addspace++)
16974653Sdougm 					buff = strndupr(buff, " ", &buffsize);
16984653Sdougm 				buff = strndupr(buff, proto, &buffsize);
16994653Sdougm 				sa_free_attr_string(proto);
17004653Sdougm 			}
17013034Sdougm 		}
17023034Sdougm 	}
17033034Sdougm 	return (buff);
17043034Sdougm }
17053034Sdougm 
17063034Sdougm /*
17073034Sdougm  * sa_list(flags, argc, argv)
17083034Sdougm  *
17093034Sdougm  * implements the "list" subcommand to list groups and optionally
17103034Sdougm  * their state and protocols.
17113034Sdougm  */
17123034Sdougm 
17133034Sdougm static int
17143910Sdougm sa_list(sa_handle_t handle, int flags, int argc, char *argv[])
17153034Sdougm {
17163034Sdougm 	sa_group_t group;
17173034Sdougm 	int verbose = 0;
17183034Sdougm 	int c;
17193034Sdougm 	char *protocol = NULL;
17206019Sdougm 	int ret = SA_OK;
17215331Samw #ifdef lint
17225331Samw 	flags = flags;
17235331Samw #endif
17243034Sdougm 
17253034Sdougm 	while ((c = getopt(argc, argv, "?hvP:")) != EOF) {
17264653Sdougm 		switch (c) {
17274653Sdougm 		case 'v':
17284653Sdougm 			verbose++;
17294653Sdougm 			break;
17304653Sdougm 		case 'P':
17315331Samw 			if (protocol != NULL) {
17325331Samw 				(void) printf(gettext(
17335331Samw 				    "Specifying multiple protocols "
17345331Samw 				    "not supported: %s\n"),
17355331Samw 				    protocol);
17365331Samw 				return (SA_SYNTAX_ERR);
17375331Samw 			}
17384653Sdougm 			protocol = optarg;
17394653Sdougm 			if (!sa_valid_protocol(protocol)) {
17404653Sdougm 				(void) printf(gettext(
17414653Sdougm 				    "Invalid protocol specified: %s\n"),
17424653Sdougm 				    protocol);
17434653Sdougm 				return (SA_INVALID_PROTOCOL);
17444653Sdougm 			}
17454653Sdougm 			break;
17466019Sdougm 		case 'h':
17476019Sdougm 			/* optopt on valid arg isn't defined */
17486019Sdougm 			optopt = c;
17496019Sdougm 			/*FALLTHROUGH*/
17506019Sdougm 		case '?':
17514653Sdougm 		default:
17526019Sdougm 			/*
17536019Sdougm 			 * Since a bad option gets to here, sort it
17546019Sdougm 			 * out and return a syntax error return value
17556019Sdougm 			 * if necessary.
17566019Sdougm 			 */
17576019Sdougm 			switch (optopt) {
17586019Sdougm 			default:
17596019Sdougm 				ret = SA_SYNTAX_ERR;
17606019Sdougm 				break;
17616019Sdougm 			case 'h':
17626019Sdougm 			case '?':
17636019Sdougm 				break;
17646019Sdougm 			}
17654653Sdougm 			(void) printf(gettext("usage: %s\n"),
17664653Sdougm 			    sa_get_usage(USAGE_LIST));
17676019Sdougm 				return (ret);
17683034Sdougm 		}
17693034Sdougm 	}
17703034Sdougm 
17715885Sdougm 	if (optind != argc) {
17725885Sdougm 		(void) printf(gettext("usage: %s\n"),
17735885Sdougm 		    sa_get_usage(USAGE_LIST));
17745885Sdougm 		return (SA_SYNTAX_ERR);
17755885Sdougm 	}
17765885Sdougm 
17774653Sdougm 	for (group = sa_get_group(handle, NULL);
17784653Sdougm 	    group != NULL;
17793034Sdougm 	    group = sa_get_next_group(group)) {
17804653Sdougm 		char *name;
17814653Sdougm 		char *proto;
17824653Sdougm 		if (protocol == NULL || has_protocol(group, protocol)) {
17834653Sdougm 			name = sa_get_group_attr(group, "name");
17844653Sdougm 			if (name != NULL && (verbose > 1 || name[0] != '#')) {
17854653Sdougm 				(void) printf("%s", (char *)name);
17864653Sdougm 				if (verbose) {
17874653Sdougm 					/*
17884653Sdougm 					 * Need the list of protocols
17894653Sdougm 					 * and current status once
17904653Sdougm 					 * available. We do want to
17914653Sdougm 					 * translate the
17924653Sdougm 					 * enabled/disabled text here.
17934653Sdougm 					 */
17944653Sdougm 					(void) printf("\t%s", isenabled(group) ?
17954653Sdougm 					    gettext("enabled") :
17964653Sdougm 					    gettext("disabled"));
17974653Sdougm 					proto = group_proto(group);
17984653Sdougm 					if (proto != NULL) {
17994653Sdougm 						(void) printf("\t%s",
18004653Sdougm 						    (char *)proto);
18014653Sdougm 						free(proto);
18024653Sdougm 					}
18034653Sdougm 				}
18044653Sdougm 				(void) printf("\n");
18053034Sdougm 			}
18064653Sdougm 			if (name != NULL)
18074653Sdougm 				sa_free_attr_string(name);
18083034Sdougm 		}
18093034Sdougm 	}
18103034Sdougm 	return (0);
18113034Sdougm }
18123034Sdougm 
18133034Sdougm /*
18143034Sdougm  * out_properties(optionset, proto, sec)
18153034Sdougm  *
18163034Sdougm  * Format the properties and encode the protocol and optional named
18173034Sdougm  * optionset into the string.
18183034Sdougm  *
18193034Sdougm  * format is protocol[:name]=(property-list)
18203034Sdougm  */
18213034Sdougm 
18223034Sdougm static void
18233034Sdougm out_properties(sa_optionset_t optionset, char *proto, char *sec)
18243034Sdougm {
18253034Sdougm 	char *type;
18263034Sdougm 	char *value;
18273034Sdougm 	int spacer;
18283034Sdougm 	sa_property_t prop;
18293034Sdougm 
18304653Sdougm 	if (sec == NULL)
18314653Sdougm 		(void) printf(" %s=(", proto ? proto : gettext("all"));
18324653Sdougm 	else
18334653Sdougm 		(void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec);
18343034Sdougm 
18353034Sdougm 	for (spacer = 0, prop = sa_get_property(optionset, NULL);
18364653Sdougm 	    prop != NULL;
18374653Sdougm 	    prop = sa_get_next_property(prop)) {
18383034Sdougm 
18393034Sdougm 		/*
18403034Sdougm 		 * extract the property name/value and output with
18413034Sdougm 		 * appropriate spacing. I.e. no prefixed space the
18423034Sdougm 		 * first time through but a space on subsequent
18433034Sdougm 		 * properties.
18443034Sdougm 		 */
18454653Sdougm 		type = sa_get_property_attr(prop, "type");
18464653Sdougm 		value = sa_get_property_attr(prop, "value");
18474653Sdougm 		if (type != NULL) {
18484653Sdougm 			(void) printf("%s%s=", spacer ? " " : "",	type);
18494653Sdougm 			spacer = 1;
18504653Sdougm 			if (value != NULL)
18514653Sdougm 				(void) printf("\"%s\"", value);
18524653Sdougm 			else
18534653Sdougm 				(void) printf("\"\"");
18544653Sdougm 		}
18554653Sdougm 		if (type != NULL)
18564653Sdougm 			sa_free_attr_string(type);
18573034Sdougm 		if (value != NULL)
18584653Sdougm 			sa_free_attr_string(value);
18593034Sdougm 	}
18603034Sdougm 	(void) printf(")");
18613034Sdougm }
18623034Sdougm 
18633034Sdougm /*
18643034Sdougm  * show_properties(group, protocol, prefix)
18653034Sdougm  *
18663034Sdougm  * print the properties for a group. If protocol is NULL, do all
18673034Sdougm  * protocols otherwise only the specified protocol. All security
18683034Sdougm  * (named groups specific to the protocol) are included.
18693034Sdougm  *
18703034Sdougm  * The "prefix" is always applied. The caller knows whether it wants
18713034Sdougm  * some type of prefix string (white space) or not.  Once the prefix
18723034Sdougm  * has been output, it is reduced to the zero length string for the
18733034Sdougm  * remainder of the property output.
18743034Sdougm  */
18753034Sdougm 
18763034Sdougm static void
18773034Sdougm show_properties(sa_group_t group, char *protocol, char *prefix)
18783034Sdougm {
18793034Sdougm 	sa_optionset_t optionset;
18803034Sdougm 	sa_security_t security;
18813034Sdougm 	char *value;
18823034Sdougm 	char *secvalue;
18833034Sdougm 
18843034Sdougm 	if (protocol != NULL) {
18854653Sdougm 		optionset = sa_get_optionset(group, protocol);
18864653Sdougm 		if (optionset != NULL) {
18874653Sdougm 			(void) printf("%s", prefix);
18884653Sdougm 			prefix = "";
18894653Sdougm 			out_properties(optionset, protocol, NULL);
18904653Sdougm 		}
18914653Sdougm 		security = sa_get_security(group, protocol, NULL);
18924653Sdougm 		if (security != NULL) {
18934653Sdougm 			(void) printf("%s", prefix);
18944653Sdougm 			prefix = "";
18954653Sdougm 			out_properties(security, protocol, NULL);
18964653Sdougm 		}
18973034Sdougm 	} else {
18984653Sdougm 		for (optionset = sa_get_optionset(group, protocol);
18994653Sdougm 		    optionset != NULL;
19004653Sdougm 		    optionset = sa_get_next_optionset(optionset)) {
19014653Sdougm 
19024653Sdougm 			value = sa_get_optionset_attr(optionset, "type");
19034653Sdougm 			(void) printf("%s", prefix);
19044653Sdougm 			prefix = "";
19054653Sdougm 			out_properties(optionset, value, 0);
19064653Sdougm 			if (value != NULL)
19074653Sdougm 				sa_free_attr_string(value);
19084653Sdougm 		}
19094653Sdougm 		for (security = sa_get_security(group, NULL, protocol);
19104653Sdougm 		    security != NULL;
19114653Sdougm 		    security = sa_get_next_security(security)) {
19124653Sdougm 
19134653Sdougm 			value = sa_get_security_attr(security, "type");
19144653Sdougm 			secvalue = sa_get_security_attr(security, "sectype");
19154653Sdougm 			(void) printf("%s", prefix);
19164653Sdougm 			prefix = "";
19174653Sdougm 			out_properties(security, value, secvalue);
19184653Sdougm 			if (value != NULL)
19194653Sdougm 				sa_free_attr_string(value);
19204653Sdougm 			if (secvalue != NULL)
19214653Sdougm 				sa_free_attr_string(secvalue);
19224653Sdougm 		}
19233034Sdougm 	}
19243034Sdougm }
19253034Sdougm 
19263034Sdougm /*
19275331Samw  * get_resource(share)
19285331Samw  *
19295331Samw  * Get the first resource name, if any, and fix string to be in
19305331Samw  * current locale and have quotes if it has embedded spaces.  Return
19315331Samw  * an attr string that must be freed.
19325331Samw  */
19335331Samw 
19345331Samw static char *
19355331Samw get_resource(sa_share_t share)
19365331Samw {
19375331Samw 	sa_resource_t resource;
19385331Samw 	char *resstring = NULL;
19395331Samw 	char *retstring;
19405331Samw 
19415331Samw 	if ((resource = sa_get_share_resource(share, NULL)) != NULL) {
19425331Samw 		resstring = sa_get_resource_attr(resource, "name");
19435331Samw 		if (resstring != NULL) {
19445331Samw 			char *cp;
19455331Samw 			int len;
19465331Samw 
19475331Samw 			retstring = conv_from_utf8(resstring);
19485331Samw 			if (retstring != resstring) {
19495331Samw 				sa_free_attr_string(resstring);
19505331Samw 				resstring = retstring;
19515331Samw 			}
19525331Samw 			if (strpbrk(resstring, " ") != NULL) {
19535331Samw 				/* account for quotes */
19545331Samw 				len = strlen(resstring) + 3;
19555331Samw 				cp = calloc(len, sizeof (char));
19565331Samw 				if (cp != NULL) {
19575331Samw 					(void) snprintf(cp, len,
19585331Samw 					    "\"%s\"", resstring);
19595331Samw 					sa_free_attr_string(resstring);
19605331Samw 					resstring = cp;
19615331Samw 				} else {
19625331Samw 					sa_free_attr_string(resstring);
19635331Samw 					resstring = NULL;
19645331Samw 				}
19655331Samw 			}
19665331Samw 		}
19675331Samw 	}
19685331Samw 	return (resstring);
19695331Samw }
19705331Samw 
19715331Samw /*
19725331Samw  * has_resource_with_opt(share)
19735331Samw  *
19745331Samw  * Check to see if the share has any resource names with optionsets
19755331Samw  * set. Also indicate if multiple resource names since the syntax
19765331Samw  * would be about the same.
19775331Samw  */
19785331Samw static int
19795331Samw has_resource_with_opt(sa_share_t share)
19805331Samw {
19815331Samw 	sa_resource_t resource;
19825331Samw 	int ret = B_FALSE;
19835331Samw 
19845331Samw 	for (resource = sa_get_share_resource(share, NULL);
19855331Samw 	    resource != NULL;
19865331Samw 	    resource = sa_get_next_resource(resource)) {
19875331Samw 
19885331Samw 		if (sa_get_optionset(resource, NULL) != NULL) {
19895331Samw 			ret = B_TRUE;
19905331Samw 			break;
19915331Samw 		}
19925331Samw 	}
19935331Samw 	return (ret);
19945331Samw }
19955331Samw 
19965331Samw /*
19975331Samw  * has_multiple_resource(share)
19985331Samw  *
19995885Sdougm  * Check to see if the share has multiple resource names since
20005885Sdougm  * the syntax would be about the same.
20015331Samw  */
20025885Sdougm static boolean_t
20035331Samw has_multiple_resource(sa_share_t share)
20045331Samw {
20055331Samw 	sa_resource_t resource;
20065331Samw 	int num;
20075331Samw 
20085331Samw 	for (num = 0, resource = sa_get_share_resource(share, NULL);
20095331Samw 	    resource != NULL;
20105331Samw 	    resource = sa_get_next_resource(resource)) {
20115331Samw 		num++;
20125331Samw 		if (num > 1)
20135331Samw 			return (B_TRUE);
20145331Samw 	}
20155331Samw 	return (B_FALSE);
20165331Samw }
20175331Samw 
20185331Samw /*
20195331Samw  * show_share(share, verbose, properties, proto, iszfs, sharepath)
20205331Samw  *
20215331Samw  * print out the share information. With the addition of resource as a
20225331Samw  * full object that can have multiple instances below the share, we
20235331Samw  * need to display that as well.
20245331Samw  */
20255331Samw 
20265331Samw static void
20275331Samw show_share(sa_share_t share, int verbose, int properties, char *proto,
20285331Samw     int iszfs, char *sharepath)
20295331Samw {
20305331Samw 	char *drive;
20315331Samw 	char *exclude;
20325331Samw 	sa_resource_t resource = NULL;
20335331Samw 	char *description;
20345331Samw 	char *rsrcname;
20355331Samw 	int rsrcwithopt;
20365885Sdougm 	boolean_t multiple;
20375331Samw 	char *type;
20385331Samw 
20395331Samw 	rsrcwithopt = has_resource_with_opt(share);
20405331Samw 
20415331Samw 	if (verbose || (properties && rsrcwithopt)) {
20425331Samw 		/* First, indicate if transient */
20435331Samw 		type = sa_get_share_attr(share, "type");
20445331Samw 		if (type != NULL && !iszfs && verbose &&
20455331Samw 		    strcmp(type, "transient") == 0)
20465331Samw 			(void) printf("\t* ");
20475331Samw 		else
20485331Samw 			(void) printf("\t  ");
20495331Samw 
20505331Samw 		if (type != NULL)
20515331Samw 			sa_free_attr_string(type);
20525331Samw 
20535331Samw 		/*
20545331Samw 		 * If we came in with verbose, we want to handle the case of
20555331Samw 		 * multiple resources as though they had properties set.
20565331Samw 		 */
20575331Samw 		multiple = has_multiple_resource(share);
20585331Samw 
20595885Sdougm 		/*
20605885Sdougm 		 * if there is a description on the share and there
20615885Sdougm 		 * are resources, treat as multiple resources in order
20625885Sdougm 		 * to get all descriptions displayed.
20635885Sdougm 		 */
20645885Sdougm 		description = sa_get_share_description(share);
20655885Sdougm 		resource = sa_get_share_resource(share, NULL);
20665885Sdougm 
20675885Sdougm 		if (description != NULL && resource != NULL)
20685885Sdougm 			multiple = B_TRUE;
20695885Sdougm 
20705331Samw 		/* Next, if not multiple follow old model */
20715331Samw 		if (!multiple && !rsrcwithopt) {
20725331Samw 			rsrcname = get_resource(share);
20735331Samw 			if (rsrcname != NULL && strlen(rsrcname) > 0) {
20745331Samw 				(void) printf("%s=%s", rsrcname, sharepath);
20755331Samw 			} else {
20765331Samw 				(void) printf("%s", sharepath);
20775331Samw 			}
20785331Samw 			if (rsrcname != NULL)
20795331Samw 				sa_free_attr_string(rsrcname);
20805885Sdougm 			/* Print the description string if there is one. */
20815885Sdougm 			print_rsrc_desc(resource, description);
20825331Samw 		} else {
20835331Samw 			/* Treat as simple and then resources come later */
20845331Samw 			(void) printf("%s", sharepath);
20855331Samw 		}
20865331Samw 		drive = sa_get_share_attr(share, "drive-letter");
20875331Samw 		if (drive != NULL) {
20885331Samw 			if (strlen(drive) > 0)
20895331Samw 				(void) printf(gettext("\tdrive-letter=\"%s:\""),
20905331Samw 				    drive);
20915331Samw 			sa_free_attr_string(drive);
20925331Samw 		}
20935331Samw 		if (properties)
20945331Samw 			show_properties(share, proto, "\t");
20955331Samw 		exclude = sa_get_share_attr(share, "exclude");
20965331Samw 		if (exclude != NULL) {
20975331Samw 			(void) printf(gettext("\tnot-shared-with=[%s]"),
20985331Samw 			    exclude);
20995331Samw 			sa_free_attr_string(exclude);
21005331Samw 		}
21015885Sdougm 
21025331Samw 		if (description != NULL) {
21035885Sdougm 			print_rsrc_desc((sa_resource_t)share, description);
21045331Samw 		}
21055331Samw 		/*
21065331Samw 		 * If there are resource names with options, show them
21075331Samw 		 * here, with one line per resource. Resource specific
21085331Samw 		 * options are at the end of the line followed by
21095331Samw 		 * description, if any.
21105331Samw 		 */
21115331Samw 		if (rsrcwithopt || multiple) {
21125331Samw 			for (resource = sa_get_share_resource(share, NULL);
21135331Samw 			    resource != NULL;
21145331Samw 			    resource = sa_get_next_resource(resource)) {
21155331Samw 				int has_space;
21165331Samw 				char *rsrc;
21175331Samw 
21185331Samw 				(void) printf("\n\t\t  ");
21195331Samw 				rsrcname = sa_get_resource_attr(resource,
21205331Samw 				    "name");
21215331Samw 				if (rsrcname == NULL)
21225331Samw 					continue;
21235331Samw 
21245331Samw 				rsrc = conv_from_utf8(rsrcname);
21255331Samw 				has_space = strpbrk(rsrc, " ") != NULL;
21265331Samw 
21275331Samw 				if (has_space)
21285331Samw 					(void) printf("\"%s\"=%s", rsrc,
21295331Samw 					    sharepath);
21305331Samw 				else
21315331Samw 					(void) printf("%s=%s", rsrc,
21325331Samw 					    sharepath);
21335331Samw 				if (rsrc != rsrcname)
21345331Samw 					sa_free_attr_string(rsrc);
21355331Samw 				sa_free_attr_string(rsrcname);
21365331Samw 				if (properties || rsrcwithopt)
21375331Samw 					show_properties(resource, proto, "\t");
21385331Samw 
21395331Samw 				/* Get description string if any */
21405885Sdougm 				print_rsrc_desc(resource, description);
21415331Samw 			}
21425331Samw 		}
21435885Sdougm 		if (description != NULL)
21445885Sdougm 			sa_free_share_description(description);
21455331Samw 	} else {
21465331Samw 		(void) printf("\t  %s", sharepath);
21475331Samw 		if (properties)
21485331Samw 			show_properties(share, proto, "\t");
21495331Samw 	}
21505331Samw 	(void) printf("\n");
21515331Samw }
21525331Samw 
21535331Samw /*
21543034Sdougm  * show_group(group, verbose, properties, proto, subgroup)
21553034Sdougm  *
21563034Sdougm  * helper function to show the contents of a group.
21573034Sdougm  */
21583034Sdougm 
21593034Sdougm static void
21603034Sdougm show_group(sa_group_t group, int verbose, int properties, char *proto,
21615331Samw     char *subgroup)
21623034Sdougm {
21633034Sdougm 	sa_share_t share;
21643034Sdougm 	char *groupname;
21653034Sdougm 	char *zfs = NULL;
21663034Sdougm 	int iszfs = 0;
21675331Samw 	char *sharepath;
21683034Sdougm 
21693034Sdougm 	groupname = sa_get_group_attr(group, "name");
21703034Sdougm 	if (groupname != NULL) {
21714653Sdougm 		if (proto != NULL && !has_protocol(group, proto)) {
21724653Sdougm 			sa_free_attr_string(groupname);
21734653Sdougm 			return;
21744653Sdougm 		}
21753034Sdougm 		/*
21763034Sdougm 		 * check to see if the group is managed by ZFS. If
21773034Sdougm 		 * there is an attribute, then it is. A non-NULL zfs
21783034Sdougm 		 * variable will trigger the different way to display
21793034Sdougm 		 * and will remove the transient property indicator
21803034Sdougm 		 * from the output.
21813034Sdougm 		 */
21824653Sdougm 		zfs = sa_get_group_attr(group, "zfs");
21834653Sdougm 		if (zfs != NULL) {
21844653Sdougm 			iszfs = 1;
21854653Sdougm 			sa_free_attr_string(zfs);
21863034Sdougm 		}
21874653Sdougm 		share = sa_get_share(group, NULL);
21884653Sdougm 		if (subgroup == NULL)
21894653Sdougm 			(void) printf("%s", groupname);
21904653Sdougm 		else
21914653Sdougm 			(void) printf("    %s/%s", subgroup, groupname);
21924653Sdougm 		if (properties)
21934653Sdougm 			show_properties(group, proto, "");
21944653Sdougm 		(void) printf("\n");
21954653Sdougm 		if (strcmp(groupname, "zfs") == 0) {
21964653Sdougm 			sa_group_t zgroup;
21974653Sdougm 
21984653Sdougm 			for (zgroup = sa_get_sub_group(group);
21994653Sdougm 			    zgroup != NULL;
22004653Sdougm 			    zgroup = sa_get_next_group(zgroup)) {
22014653Sdougm 				show_group(zgroup, verbose, properties, proto,
22024653Sdougm 				    "zfs");
22034653Sdougm 			}
22044653Sdougm 			sa_free_attr_string(groupname);
22054653Sdougm 			return;
22064653Sdougm 		}
22073034Sdougm 		/*
22084653Sdougm 		 * Have a group, so list the contents. Resource and
22093034Sdougm 		 * description are only listed if verbose is set.
22103034Sdougm 		 */
22114653Sdougm 		for (share = sa_get_share(group, NULL);
22124653Sdougm 		    share != NULL;
22134653Sdougm 		    share = sa_get_next_share(share)) {
22144653Sdougm 			sharepath = sa_get_share_attr(share, "path");
22154653Sdougm 			if (sharepath != NULL) {
22165331Samw 				show_share(share, verbose, properties, proto,
22175331Samw 				    iszfs, sharepath);
22184653Sdougm 				sa_free_attr_string(sharepath);
22193034Sdougm 			}
22203034Sdougm 		}
22213034Sdougm 	}
22223034Sdougm 	if (groupname != NULL) {
22233034Sdougm 		sa_free_attr_string(groupname);
22243034Sdougm 	}
22253034Sdougm }
22263034Sdougm 
22273034Sdougm /*
22283034Sdougm  * show_group_xml_init()
22293034Sdougm  *
22303034Sdougm  * Create an XML document that will be used to display config info via
22313034Sdougm  * XML format.
22323034Sdougm  */
22333034Sdougm 
22343034Sdougm xmlDocPtr
22353034Sdougm show_group_xml_init()
22363034Sdougm {
22373034Sdougm 	xmlDocPtr doc;
22383034Sdougm 	xmlNodePtr root;
22393034Sdougm 
22403034Sdougm 	doc = xmlNewDoc((xmlChar *)"1.0");
22413034Sdougm 	if (doc != NULL) {
22424653Sdougm 		root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
22434653Sdougm 		if (root != NULL)
22444653Sdougm 			xmlDocSetRootElement(doc, root);
22453034Sdougm 	}
22463034Sdougm 	return (doc);
22473034Sdougm }
22483034Sdougm 
22493034Sdougm /*
22503034Sdougm  * show_group_xml(doc, group)
22513034Sdougm  *
22523034Sdougm  * Copy the group info into the XML doc.
22533034Sdougm  */
22543034Sdougm 
22553034Sdougm static void
22563034Sdougm show_group_xml(xmlDocPtr doc, sa_group_t group)
22573034Sdougm {
22583034Sdougm 	xmlNodePtr node;
22593034Sdougm 	xmlNodePtr root;
22603034Sdougm 
22613034Sdougm 	root = xmlDocGetRootElement(doc);
22623034Sdougm 	node = xmlCopyNode((xmlNodePtr)group, 1);
22633034Sdougm 	if (node != NULL && root != NULL) {
22644653Sdougm 		xmlAddChild(root, node);
22653034Sdougm 		/*
22663034Sdougm 		 * In the future, we may have interally used tags that
22673034Sdougm 		 * should not appear in the XML output. Remove
22683034Sdougm 		 * anything we don't want to show here.
22693034Sdougm 		 */
22703034Sdougm 	}
22713034Sdougm }
22723034Sdougm 
22733034Sdougm /*
22743034Sdougm  * sa_show(flags, argc, argv)
22753034Sdougm  *
22763034Sdougm  * Implements the show subcommand.
22773034Sdougm  */
22783034Sdougm 
22793034Sdougm int
22803910Sdougm sa_show(sa_handle_t handle, int flags, int argc, char *argv[])
22813034Sdougm {
22823034Sdougm 	sa_group_t group;
22833034Sdougm 	int verbose = 0;
22843034Sdougm 	int properties = 0;
22853034Sdougm 	int c;
22863034Sdougm 	int ret = SA_OK;
22873034Sdougm 	char *protocol = NULL;
22883034Sdougm 	int xml = 0;
22893034Sdougm 	xmlDocPtr doc;
22905331Samw #ifdef lint
22915331Samw 	flags = flags;
22925331Samw #endif
22933034Sdougm 
22943034Sdougm 	while ((c = getopt(argc, argv, "?hvP:px")) !=	EOF) {
22954653Sdougm 		switch (c) {
22964653Sdougm 		case 'v':
22974653Sdougm 			verbose++;
22984653Sdougm 			break;
22994653Sdougm 		case 'p':
23004653Sdougm 			properties++;
23014653Sdougm 			break;
23024653Sdougm 		case 'P':
23035331Samw 			if (protocol != NULL) {
23045331Samw 				(void) printf(gettext(
23055331Samw 				    "Specifying multiple protocols "
23065331Samw 				    "not supported: %s\n"),
23075331Samw 				    protocol);
23085331Samw 				return (SA_SYNTAX_ERR);
23095331Samw 			}
23104653Sdougm 			protocol = optarg;
23114653Sdougm 			if (!sa_valid_protocol(protocol)) {
23124653Sdougm 				(void) printf(gettext(
23134653Sdougm 				    "Invalid protocol specified: %s\n"),
23144653Sdougm 				    protocol);
23154653Sdougm 				return (SA_INVALID_PROTOCOL);
23164653Sdougm 			}
23174653Sdougm 			break;
23184653Sdougm 		case 'x':
23194653Sdougm 			xml++;
23204653Sdougm 			break;
23216019Sdougm 		case 'h':
23226019Sdougm 			/* optopt on valid arg isn't defined */
23236019Sdougm 			optopt = c;
23246019Sdougm 			/*FALLTHROUGH*/
23256019Sdougm 		case '?':
23264653Sdougm 		default:
23276019Sdougm 			/*
23286019Sdougm 			 * Since a bad option gets to here, sort it
23296019Sdougm 			 * out and return a syntax error return value
23306019Sdougm 			 * if necessary.
23316019Sdougm 			 */
23326019Sdougm 			switch (optopt) {
23336019Sdougm 			default:
23346019Sdougm 				ret = SA_SYNTAX_ERR;
23356019Sdougm 				break;
23366019Sdougm 			case 'h':
23376019Sdougm 			case '?':
23386019Sdougm 				break;
23396019Sdougm 			}
23404653Sdougm 			(void) printf(gettext("usage: %s\n"),
23414653Sdougm 			    sa_get_usage(USAGE_SHOW));
23426019Sdougm 			return (ret);
23433034Sdougm 		}
23443034Sdougm 	}
23453034Sdougm 
23463034Sdougm 	if (xml) {
23474653Sdougm 		doc = show_group_xml_init();
23484653Sdougm 		if (doc == NULL)
23494653Sdougm 			ret = SA_NO_MEMORY;
23503034Sdougm 	}
23513034Sdougm 
23523034Sdougm 	if (optind == argc) {
23534653Sdougm 		/* No group specified so go through them all */
23544653Sdougm 		for (group = sa_get_group(handle, NULL);
23554653Sdougm 		    group != NULL;
23564653Sdougm 		    group = sa_get_next_group(group)) {
23574653Sdougm 			/*
23584653Sdougm 			 * Have a group so check if one we want and then list
23594653Sdougm 			 * contents with appropriate options.
23604653Sdougm 			 */
23614653Sdougm 			if (xml)
23624653Sdougm 				show_group_xml(doc, group);
23634653Sdougm 			else
23644653Sdougm 				show_group(group, verbose, properties, protocol,
23654653Sdougm 				    NULL);
23664653Sdougm 		}
23673034Sdougm 	} else {
23684653Sdougm 		/* Have a specified list of groups */
23694653Sdougm 		for (; optind < argc; optind++) {
23704653Sdougm 			group = sa_get_group(handle, argv[optind]);
23714653Sdougm 			if (group != NULL) {
23724653Sdougm 				if (xml)
23734653Sdougm 					show_group_xml(doc, group);
23744653Sdougm 				else
23754653Sdougm 					show_group(group, verbose, properties,
23764653Sdougm 					    protocol, NULL);
23774653Sdougm 			} else {
23784653Sdougm 				(void) printf(gettext("%s: not found\n"),
23794653Sdougm 				    argv[optind]);
23804653Sdougm 				ret = SA_NO_SUCH_GROUP;
23814653Sdougm 			}
23823034Sdougm 		}
23833034Sdougm 	}
23843034Sdougm 	if (xml && ret == SA_OK) {
23854653Sdougm 		xmlDocFormatDump(stdout, doc, 1);
23864653Sdougm 		xmlFreeDoc(doc);
23873034Sdougm 	}
23883034Sdougm 	return (ret);
23893034Sdougm 
23903034Sdougm }
23913034Sdougm 
23923034Sdougm /*
23933034Sdougm  * enable_share(group, share, update_legacy)
23943034Sdougm  *
23953034Sdougm  * helper function to enable a share if the group is enabled.
23963034Sdougm  */
23973034Sdougm 
23983034Sdougm static int
23993910Sdougm enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share,
24005331Samw     int update_legacy)
24013034Sdougm {
24023034Sdougm 	char *value;
24033034Sdougm 	int enabled;
24043034Sdougm 	sa_optionset_t optionset;
24055331Samw 	int err;
24063034Sdougm 	int ret = SA_OK;
24073034Sdougm 	char *zfs = NULL;
24083034Sdougm 	int iszfs = 0;
24095331Samw 	int isshare;
24103034Sdougm 
24113034Sdougm 	/*
24123034Sdougm 	 * need to enable this share if the group is enabled but not
24133034Sdougm 	 * otherwise. The enable is also done on each protocol
24143034Sdougm 	 * represented in the group.
24153034Sdougm 	 */
24163034Sdougm 	value = sa_get_group_attr(group, "state");
24173034Sdougm 	enabled = value != NULL && strcmp(value, "enabled") == 0;
24183034Sdougm 	if (value != NULL)
24194653Sdougm 		sa_free_attr_string(value);
24203034Sdougm 	/* remove legacy config if necessary */
24213034Sdougm 	if (update_legacy)
24225331Samw 		ret = sa_delete_legacy(share, NULL);
24233034Sdougm 	zfs = sa_get_group_attr(group, "zfs");
24243034Sdougm 	if (zfs != NULL) {
24254653Sdougm 		iszfs++;
24264653Sdougm 		sa_free_attr_string(zfs);
24273034Sdougm 	}
24283034Sdougm 
24293034Sdougm 	/*
24303034Sdougm 	 * Step through each optionset at the group level and
24313034Sdougm 	 * enable the share based on the protocol type. This
24323034Sdougm 	 * works because protocols must be set on the group
24333034Sdougm 	 * for the protocol to be enabled.
24343034Sdougm 	 */
24355331Samw 	isshare = sa_is_share(share);
24363034Sdougm 	for (optionset = sa_get_optionset(group, NULL);
24373034Sdougm 	    optionset != NULL && ret == SA_OK;
24383034Sdougm 	    optionset = sa_get_next_optionset(optionset)) {
24394653Sdougm 		value = sa_get_optionset_attr(optionset, "type");
24404653Sdougm 		if (value != NULL) {
24415331Samw 			if (enabled) {
24425331Samw 				if (isshare) {
24435331Samw 					err = sa_enable_share(share, value);
24445331Samw 				} else {
24455331Samw 					err = sa_enable_resource(share, value);
24465331Samw 					if (err == SA_NOT_SUPPORTED) {
24475331Samw 						sa_share_t parent;
24485331Samw 						parent = sa_get_resource_parent(
24495331Samw 						    share);
24505331Samw 						if (parent != NULL)
24515331Samw 							err = sa_enable_share(
24525331Samw 							    parent, value);
24535331Samw 					}
24545331Samw 				}
24555331Samw 				if (err != SA_OK) {
24565331Samw 					ret = err;
24575331Samw 					(void) printf(gettext(
24585331Samw 					    "Failed to enable share for "
24595331Samw 					    "\"%s\": %s\n"),
24605331Samw 					    value, sa_errorstr(ret));
24615331Samw 				}
24625331Samw 			}
24635331Samw 			/*
24645331Samw 			 * If we want to update the legacy, use a copy of
24655331Samw 			 * share so we can avoid breaking the loop we are in
24665331Samw 			 * since we might also need to go up the tree to the
24675331Samw 			 * parent.
24685331Samw 			 */
24695331Samw 			if (update_legacy && !iszfs) {
24705331Samw 				sa_share_t update = share;
24715331Samw 				if (!sa_is_share(share)) {
24725331Samw 					update = sa_get_resource_parent(share);
24735331Samw 				}
24745331Samw 				(void) sa_update_legacy(update, value);
24755331Samw 			}
24764653Sdougm 			sa_free_attr_string(value);
24774653Sdougm 		}
24783034Sdougm 	}
24793034Sdougm 	if (ret == SA_OK)
24804653Sdougm 		(void) sa_update_config(handle);
24813034Sdougm 	return (ret);
24823034Sdougm }
24833034Sdougm 
24843034Sdougm /*
24855331Samw  * sa_require_resource(group)
24865331Samw  *
24875331Samw  * if any of the defined protocols on the group require resource
24885331Samw  * names, then all shares must have them.
24895331Samw  */
24905331Samw 
24915331Samw static int
24925331Samw sa_require_resource(sa_group_t group)
24935331Samw {
24945331Samw 	sa_optionset_t optionset;
24955331Samw 
24965331Samw 	for (optionset = sa_get_optionset(group, NULL);
24975331Samw 	    optionset != NULL;
24985331Samw 	    optionset = sa_get_next_optionset(optionset)) {
24995331Samw 		char *proto;
25005331Samw 
25015331Samw 		proto = sa_get_optionset_attr(optionset, "type");
25025331Samw 		if (proto != NULL) {
25035331Samw 			uint64_t features;
25045331Samw 
25055331Samw 			features = sa_proto_get_featureset(proto);
25065331Samw 			if (features & SA_FEATURE_RESOURCE) {
25075331Samw 				sa_free_attr_string(proto);
25085331Samw 				return (B_TRUE);
25095331Samw 			}
25105331Samw 			sa_free_attr_string(proto);
25115331Samw 		}
25125331Samw 	}
25135331Samw 	return (B_FALSE);
25145331Samw }
25155331Samw 
25165331Samw /*
25173034Sdougm  * sa_addshare(flags, argc, argv)
25183034Sdougm  *
25193034Sdougm  * implements add-share subcommand.
25203034Sdougm  */
25213034Sdougm 
25225331Samw static int
25233910Sdougm sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[])
25243034Sdougm {
25253034Sdougm 	int verbose = 0;
25263034Sdougm 	int dryrun = 0;
25273034Sdougm 	int c;
25283034Sdougm 	int ret = SA_OK;
25293034Sdougm 	sa_group_t group;
25303034Sdougm 	sa_share_t share;
25315331Samw 	sa_resource_t resource = NULL;
25323034Sdougm 	char *sharepath = NULL;
25333034Sdougm 	char *description = NULL;
25345331Samw 	char *rsrcname = NULL;
25355331Samw 	char *rsrc = NULL;
25363034Sdougm 	int persist = SA_SHARE_PERMANENT; /* default to persist */
25373034Sdougm 	int auth;
25383034Sdougm 	char dir[MAXPATHLEN];
25393034Sdougm 
25403034Sdougm 	while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) {
25414653Sdougm 		switch (c) {
25424653Sdougm 		case 'n':
25434653Sdougm 			dryrun++;
25444653Sdougm 			break;
25454653Sdougm 		case 'v':
25464653Sdougm 			verbose++;
25474653Sdougm 			break;
25484653Sdougm 		case 'd':
25494653Sdougm 			description = optarg;
25504653Sdougm 			break;
25514653Sdougm 		case 'r':
25525331Samw 			if (rsrcname != NULL) {
25535331Samw 				(void) printf(gettext("Adding multiple "
25545331Samw 				    "resource names not"
25555331Samw 				    " supported\n"));
25565331Samw 				return (SA_SYNTAX_ERR);
25575331Samw 			}
25585331Samw 			rsrcname = optarg;
25594653Sdougm 			break;
25604653Sdougm 		case 's':
25614653Sdougm 			/*
25624653Sdougm 			 * Save share path into group. Currently limit
25634653Sdougm 			 * to one share per command.
25644653Sdougm 			 */
25654653Sdougm 			if (sharepath != NULL) {
25664653Sdougm 				(void) printf(gettext(
25674653Sdougm 				    "Adding multiple shares not supported\n"));
25685331Samw 				return (SA_SYNTAX_ERR);
25694653Sdougm 			}
25704653Sdougm 			sharepath = optarg;
25714653Sdougm 			break;
25724653Sdougm 		case 't':
25734653Sdougm 			persist = SA_SHARE_TRANSIENT;
25744653Sdougm 			break;
25756019Sdougm 		case 'h':
25766019Sdougm 			/* optopt on valid arg isn't defined */
25776019Sdougm 			optopt = c;
25786019Sdougm 			/*FALLTHROUGH*/
25796019Sdougm 		case '?':
25804653Sdougm 		default:
25816019Sdougm 			/*
25826019Sdougm 			 * Since a bad option gets to here, sort it
25836019Sdougm 			 * out and return a syntax error return value
25846019Sdougm 			 * if necessary.
25856019Sdougm 			 */
25866019Sdougm 			switch (optopt) {
25876019Sdougm 			default:
25886019Sdougm 				ret = SA_SYNTAX_ERR;
25896019Sdougm 				break;
25906019Sdougm 			case 'h':
25916019Sdougm 			case '?':
25926019Sdougm 				break;
25936019Sdougm 			}
25944653Sdougm 			(void) printf(gettext("usage: %s\n"),
25954653Sdougm 			    sa_get_usage(USAGE_ADD_SHARE));
25966019Sdougm 			return (ret);
25973034Sdougm 		}
25983034Sdougm 	}
25993034Sdougm 
26003034Sdougm 	if (optind >= argc) {
26014653Sdougm 		(void) printf(gettext("usage: %s\n"),
26024653Sdougm 		    sa_get_usage(USAGE_ADD_SHARE));
26034653Sdougm 		if (dryrun || sharepath != NULL || description != NULL ||
26045331Samw 		    rsrcname != NULL || verbose || persist) {
26054653Sdougm 			(void) printf(gettext("\tgroup must be specified\n"));
26064653Sdougm 			ret = SA_NO_SUCH_GROUP;
26074653Sdougm 		} else {
26084653Sdougm 			ret = SA_OK;
26094653Sdougm 		}
26103034Sdougm 	} else {
26114653Sdougm 		if (sharepath == NULL) {
26124653Sdougm 			(void) printf(gettext("usage: %s\n"),
26134653Sdougm 			    sa_get_usage(USAGE_ADD_SHARE));
26144653Sdougm 			(void) printf(gettext(
26154653Sdougm 			    "\t-s sharepath must be specified\n"));
26165331Samw 			ret = SA_BAD_PATH;
26174653Sdougm 		}
26185331Samw 		if (ret == SA_OK) {
26195331Samw 			if (realpath(sharepath, dir) == NULL) {
26205331Samw 				ret = SA_BAD_PATH;
26215331Samw 				(void) printf(gettext("Path "
26225331Samw 				    "is not valid: %s\n"),
26235331Samw 				    sharepath);
26245331Samw 			} else {
26255331Samw 				sharepath = dir;
26265331Samw 			}
26273034Sdougm 		}
26285331Samw 		if (ret == SA_OK && rsrcname != NULL) {
26295331Samw 			/* check for valid syntax */
26305331Samw 			if (validresource(rsrcname)) {
26315331Samw 				rsrc = conv_to_utf8(rsrcname);
26325331Samw 				resource = sa_find_resource(handle, rsrc);
26335331Samw 				if (resource != NULL) {
26345331Samw 					/*
26355331Samw 					 * Resource names must be
26365331Samw 					 * unique in the system
26375331Samw 					 */
26385331Samw 					ret = SA_DUPLICATE_NAME;
26395331Samw 					(void) printf(gettext("usage: %s\n"),
26405331Samw 					    sa_get_usage(USAGE_ADD_SHARE));
26415331Samw 					(void) printf(gettext(
26425331Samw 					    "\tresource names must be unique "
26435331Samw 					    "in the system\n"));
26445331Samw 				}
26455331Samw 			} else {
26465331Samw 				(void) printf(gettext("usage: %s\n"),
26475331Samw 				    sa_get_usage(USAGE_ADD_SHARE));
26485331Samw 				(void) printf(gettext(
26495331Samw 				    "\tresource names use restricted "
26505331Samw 				    "character set\n"));
26515331Samw 				ret = SA_INVALID_NAME;
26525331Samw 			}
26533034Sdougm 		}
26545331Samw 
26555331Samw 		if (ret != SA_OK) {
26565331Samw 			if (rsrc != NULL && rsrcname != rsrc)
26575331Samw 				sa_free_attr_string(rsrc);
26585331Samw 			return (ret);
26594653Sdougm 		}
26605331Samw 
26614653Sdougm 		share = sa_find_share(handle, sharepath);
26624653Sdougm 		if (share != NULL) {
26635331Samw 			if (rsrcname == NULL) {
26645331Samw 				/*
26655331Samw 				 * Can only have a duplicate share if a new
26665331Samw 				 * resource name is being added.
26675331Samw 				 */
26685331Samw 				ret = SA_DUPLICATE_NAME;
26695331Samw 				(void) printf(gettext("Share path already "
26705331Samw 				    "shared: %s\n"), sharepath);
26715331Samw 			}
26725331Samw 		}
26735331Samw 		if (ret != SA_OK)
26745331Samw 			return (ret);
26755331Samw 
26765331Samw 		group = sa_get_group(handle, argv[optind]);
26775331Samw 		if (group != NULL) {
26785331Samw 			if (sa_require_resource(group) == B_TRUE &&
26795331Samw 			    rsrcname == NULL) {
26805331Samw 				(void) printf(gettext(
26815331Samw 				    "Resource name is required "
26825331Samw 				    "by at least one enabled protocol "
26835331Samw 				    "in group\n"));
26845331Samw 				return (SA_RESOURCE_REQUIRED);
26855331Samw 			}
26865331Samw 			if (share == NULL && ret == SA_OK) {
26875331Samw 				if (dryrun)
26885331Samw 					ret = sa_check_path(group, sharepath,
26895331Samw 					    SA_CHECK_NORMAL);
26905331Samw 				else
26915331Samw 					share = sa_add_share(group, sharepath,
26925331Samw 					    persist, &ret);
26935331Samw 			}
26945331Samw 			/*
26955331Samw 			 * Make sure this isn't an attempt to put a resourced
26965331Samw 			 * share into a different group than it already is in.
26975331Samw 			 */
26985331Samw 			if (share != NULL) {
26995331Samw 				sa_group_t parent;
27005331Samw 				parent = sa_get_parent_group(share);
27015331Samw 				if (parent != group) {
27025331Samw 					ret = SA_DUPLICATE_NAME;
27034653Sdougm 					(void) printf(gettext(
27044653Sdougm 					    "Share path already "
27055331Samw 					    "shared: %s\n"), sharepath);
27064653Sdougm 				}
27073034Sdougm 			}
27083034Sdougm 			if (!dryrun && share == NULL) {
27094653Sdougm 				(void) printf(gettext(
27104653Sdougm 				    "Could not add share: %s\n"),
27114653Sdougm 				    sa_errorstr(ret));
27123034Sdougm 			} else {
27135331Samw 				auth = check_authorizations(argv[optind],
27145331Samw 				    flags);
27154653Sdougm 				if (!dryrun && ret == SA_OK) {
27165331Samw 					if (rsrcname != NULL) {
27175331Samw 						resource = sa_add_resource(
27185331Samw 						    share,
27195331Samw 						    rsrc,
27205331Samw 						    SA_SHARE_PERMANENT,
27215331Samw 						    &ret);
27224653Sdougm 					}
27234653Sdougm 					if (ret == SA_OK &&
27244653Sdougm 					    description != NULL) {
27255885Sdougm 						if (resource != NULL)
27265885Sdougm 							ret =
27275885Sdougm 							    set_resource_desc(
27285885Sdougm 							    resource,
27295885Sdougm 							    description);
27305885Sdougm 						else
27315331Samw 							ret =
27325331Samw 							    set_share_desc(
27335331Samw 							    share,
27345331Samw 							    description);
27354653Sdougm 					}
27364653Sdougm 					if (ret == SA_OK) {
27375331Samw 						/* now enable the share(s) */
27385331Samw 						if (resource != NULL) {
27395331Samw 							ret = enable_share(
27405331Samw 							    handle,
27415331Samw 							    group,
27425331Samw 							    resource,
27435331Samw 							    1);
27445331Samw 						} else {
27455331Samw 							ret = enable_share(
27465331Samw 							    handle,
27475331Samw 							    group,
27485331Samw 							    share,
27495331Samw 							    1);
27505331Samw 						}
27514653Sdougm 						ret = sa_update_config(handle);
27524653Sdougm 					}
27534653Sdougm 					switch (ret) {
27544653Sdougm 					case SA_DUPLICATE_NAME:
27554653Sdougm 						(void) printf(gettext(
27564653Sdougm 						    "Resource name in"
27575331Samw 						    "use: %s\n"),
27585331Samw 						    rsrcname);
27594653Sdougm 						break;
27604653Sdougm 					default:
27615331Samw 						(void) printf(gettext(
27625331Samw 						    "Could not set "
27634653Sdougm 						    "attribute: %s\n"),
27644653Sdougm 						    sa_errorstr(ret));
27654653Sdougm 						break;
27664653Sdougm 					case SA_OK:
27674653Sdougm 						break;
27684653Sdougm 					}
27695331Samw 				} else if (dryrun && ret == SA_OK &&
27705331Samw 				    !auth && verbose) {
27714653Sdougm 					(void) printf(gettext(
27724653Sdougm 					    "Command would fail: %s\n"),
27734653Sdougm 					    sa_errorstr(SA_NO_PERMISSION));
27744653Sdougm 					ret = SA_NO_PERMISSION;
27753034Sdougm 				}
27763034Sdougm 			}
27775331Samw 		} else {
27785331Samw 			switch (ret) {
27795331Samw 			default:
27805331Samw 				(void) printf(gettext(
27815331Samw 				    "Group \"%s\" not found\n"), argv[optind]);
27825331Samw 				ret = SA_NO_SUCH_GROUP;
27835331Samw 				break;
27845331Samw 			case SA_BAD_PATH:
27855331Samw 			case SA_DUPLICATE_NAME:
27865331Samw 				break;
27875331Samw 			}
27883034Sdougm 		}
27893034Sdougm 	}
27903034Sdougm 	return (ret);
27913034Sdougm }
27923034Sdougm 
27933034Sdougm /*
27943034Sdougm  * sa_moveshare(flags, argc, argv)
27953034Sdougm  *
27963034Sdougm  * implements move-share subcommand.
27973034Sdougm  */
27983034Sdougm 
27993034Sdougm int
28003910Sdougm sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[])
28013034Sdougm {
28023034Sdougm 	int verbose = 0;
28033034Sdougm 	int dryrun = 0;
28043034Sdougm 	int c;
28053034Sdougm 	int ret = SA_OK;
28063034Sdougm 	sa_group_t group;
28073034Sdougm 	sa_share_t share;
28085331Samw 	char *rsrcname = NULL;
28093034Sdougm 	char *sharepath = NULL;
28103034Sdougm 	int authsrc = 0, authdst = 0;
28115885Sdougm 	char dir[MAXPATHLEN];
28123034Sdougm 
28135331Samw 	while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) {
28144653Sdougm 		switch (c) {
28154653Sdougm 		case 'n':
28164653Sdougm 			dryrun++;
28174653Sdougm 			break;
28184653Sdougm 		case 'v':
28194653Sdougm 			verbose++;
28204653Sdougm 			break;
28215331Samw 		case 'r':
28225331Samw 			if (rsrcname != NULL) {
28235331Samw 				(void) printf(gettext(
28245331Samw 				    "Moving multiple resource names not"
28255331Samw 				    " supported\n"));
28265331Samw 				return (SA_SYNTAX_ERR);
28275331Samw 			}
28285331Samw 			rsrcname = optarg;
28295331Samw 			break;
28304653Sdougm 		case 's':
28314653Sdougm 			/*
28324653Sdougm 			 * Remove share path from group. Currently limit
28334653Sdougm 			 * to one share per command.
28344653Sdougm 			 */
28354653Sdougm 			if (sharepath != NULL) {
28364653Sdougm 				(void) printf(gettext("Moving multiple shares"
28375331Samw 				    " not supported\n"));
28385331Samw 				return (SA_SYNTAX_ERR);
28394653Sdougm 			}
28404653Sdougm 			sharepath = optarg;
28414653Sdougm 			break;
28426019Sdougm 		case 'h':
28436019Sdougm 			/* optopt on valid arg isn't defined */
28446019Sdougm 			optopt = c;
28456019Sdougm 			/*FALLTHROUGH*/
28466019Sdougm 		case '?':
28474653Sdougm 		default:
28486019Sdougm 			/*
28496019Sdougm 			 * Since a bad option gets to here, sort it
28506019Sdougm 			 * out and return a syntax error return value
28516019Sdougm 			 * if necessary.
28526019Sdougm 			 */
28536019Sdougm 			switch (optopt) {
28546019Sdougm 			default:
28556019Sdougm 				ret = SA_SYNTAX_ERR;
28566019Sdougm 				break;
28576019Sdougm 			case 'h':
28586019Sdougm 			case '?':
28596019Sdougm 				break;
28606019Sdougm 			}
28614653Sdougm 			(void) printf(gettext("usage: %s\n"),
28624653Sdougm 			    sa_get_usage(USAGE_MOVE_SHARE));
28636019Sdougm 			return (ret);
28643034Sdougm 		}
28653034Sdougm 	}
28663034Sdougm 
28673034Sdougm 	if (optind >= argc || sharepath == NULL) {
28685331Samw 		(void) printf(gettext("usage: %s\n"),
28695331Samw 		    sa_get_usage(USAGE_MOVE_SHARE));
28705331Samw 		if (dryrun || verbose || sharepath != NULL) {
28715331Samw 			(void) printf(gettext("\tgroup must be specified\n"));
28725331Samw 			ret = SA_NO_SUCH_GROUP;
28735331Samw 		} else {
28745331Samw 			if (sharepath == NULL) {
28755331Samw 				ret = SA_SYNTAX_ERR;
28764653Sdougm 				(void) printf(gettext(
28775331Samw 				    "\tsharepath must be specified\n"));
28784653Sdougm 			} else {
28795331Samw 				ret = SA_OK;
28804653Sdougm 			}
28815331Samw 		}
28824653Sdougm 	} else {
28834653Sdougm 		sa_group_t parent;
28844653Sdougm 		char *zfsold;
28854653Sdougm 		char *zfsnew;
28864653Sdougm 
28873034Sdougm 		if (sharepath == NULL) {
28884653Sdougm 			(void) printf(gettext(
28894653Sdougm 			    "sharepath must be specified with the -s "
28904653Sdougm 			    "option\n"));
28914653Sdougm 			return (SA_BAD_PATH);
28924653Sdougm 		}
28933910Sdougm 		group = sa_get_group(handle, argv[optind]);
28944653Sdougm 		if (group == NULL) {
28954653Sdougm 			(void) printf(gettext("Group \"%s\" not found\n"),
28964653Sdougm 			    argv[optind]);
28974653Sdougm 			return (SA_NO_SUCH_GROUP);
28984653Sdougm 		}
28994653Sdougm 		share = sa_find_share(handle, sharepath);
29005885Sdougm 		/*
29015885Sdougm 		 * If a share wasn't found, it may have been a symlink
29025885Sdougm 		 * or has a trailing '/'. Try again after resolving
29035885Sdougm 		 * with realpath().
29045885Sdougm 		 */
29055885Sdougm 		if (share == NULL) {
29065885Sdougm 			if (realpath(sharepath, dir) == NULL) {
29075885Sdougm 				(void) printf(gettext("Path "
29085885Sdougm 				    "is not valid: %s\n"),
29095885Sdougm 				    sharepath);
29105885Sdougm 				return (SA_BAD_PATH);
29115885Sdougm 			}
29125885Sdougm 			sharepath = dir;
29135885Sdougm 			share = sa_find_share(handle, sharepath);
29145885Sdougm 		}
29154653Sdougm 		if (share == NULL) {
29163034Sdougm 			(void) printf(gettext("Share not found: %s\n"),
29174653Sdougm 			    sharepath);
29184653Sdougm 			return (SA_NO_SUCH_PATH);
29194653Sdougm 		}
29205885Sdougm 		authdst = check_authorizations(argv[optind], flags);
29214653Sdougm 
29224653Sdougm 		parent = sa_get_parent_group(share);
29234653Sdougm 		if (parent != NULL) {
29244653Sdougm 			char *pname;
29254653Sdougm 			pname = sa_get_group_attr(parent, "name");
29264653Sdougm 			if (pname != NULL) {
29273034Sdougm 				authsrc = check_authorizations(pname, flags);
29283034Sdougm 				sa_free_attr_string(pname);
29294653Sdougm 			}
29304653Sdougm 			zfsold = sa_get_group_attr(parent, "zfs");
29314653Sdougm 			zfsnew = sa_get_group_attr(group, "zfs");
29324653Sdougm 			if ((zfsold != NULL && zfsnew == NULL) ||
29334653Sdougm 			    (zfsold == NULL && zfsnew != NULL)) {
29343034Sdougm 				ret = SA_NOT_ALLOWED;
29353034Sdougm 			}
29364653Sdougm 			if (zfsold != NULL)
29374653Sdougm 				sa_free_attr_string(zfsold);
29384653Sdougm 			if (zfsnew != NULL)
29394653Sdougm 				sa_free_attr_string(zfsnew);
29404653Sdougm 		}
29414653Sdougm 
29424653Sdougm 		if (ret == SA_OK && parent != group && !dryrun) {
29434653Sdougm 			char *oldstate;
29444653Sdougm 			/*
29454653Sdougm 			 * Note that the share may need to be
29465331Samw 			 * "unshared" if the new group is disabled and
29475331Samw 			 * the old was enabled or it may need to be
29485331Samw 			 * share to update if the new group is
29495331Samw 			 * enabled. We disable before the move and
29505331Samw 			 * will have to enable after the move in order
29515331Samw 			 * to cleanup entries for protocols that
29525331Samw 			 * aren't in the new group.
29534653Sdougm 			 */
29544653Sdougm 			oldstate = sa_get_group_attr(parent, "state");
29554653Sdougm 
29564653Sdougm 			/* enable_share determines what to do */
29575331Samw 			if (strcmp(oldstate, "enabled") == 0)
29583034Sdougm 				(void) sa_disable_share(share, NULL);
29595331Samw 
29604653Sdougm 			if (oldstate != NULL)
29613034Sdougm 				sa_free_attr_string(oldstate);
29623034Sdougm 		}
29634653Sdougm 
29645331Samw 		if (!dryrun && ret == SA_OK)
29655331Samw 			ret = sa_move_share(group, share);
29665331Samw 
29675331Samw 		/*
29685331Samw 		 * Reenable and update any config information.
29695331Samw 		 */
29705331Samw 		if (ret == SA_OK && parent != group && !dryrun) {
29715331Samw 			ret = sa_update_config(handle);
29725331Samw 
29735331Samw 			(void) enable_share(handle, group, share, 1);
29745331Samw 		}
29755331Samw 
29764653Sdougm 		if (ret != SA_OK)
29774653Sdougm 			(void) printf(gettext("Could not move share: %s\n"),
29784653Sdougm 			    sa_errorstr(ret));
29794653Sdougm 
29804653Sdougm 		if (dryrun && ret == SA_OK && !(authsrc & authdst) &&
29814653Sdougm 		    verbose) {
29824653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
29834653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
29844653Sdougm 		}
29853034Sdougm 	}
29863034Sdougm 	return (ret);
29873034Sdougm }
29883034Sdougm 
29893034Sdougm /*
29903034Sdougm  * sa_removeshare(flags, argc, argv)
29913034Sdougm  *
29923034Sdougm  * implements remove-share subcommand.
29933034Sdougm  */
29943034Sdougm 
29953034Sdougm int
29963910Sdougm sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[])
29973034Sdougm {
29983034Sdougm 	int verbose = 0;
29993034Sdougm 	int dryrun = 0;
30003034Sdougm 	int force = 0;
30013034Sdougm 	int c;
30023034Sdougm 	int ret = SA_OK;
30033034Sdougm 	sa_group_t group;
30045331Samw 	sa_resource_t resource = NULL;
30055331Samw 	sa_share_t share = NULL;
30065331Samw 	char *rsrcname = NULL;
30073034Sdougm 	char *sharepath = NULL;
30083034Sdougm 	char dir[MAXPATHLEN];
30093034Sdougm 	int auth;
30103034Sdougm 
30115331Samw 	while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) {
30124653Sdougm 		switch (c) {
30134653Sdougm 		case 'n':
30144653Sdougm 			dryrun++;
30154653Sdougm 			break;
30164653Sdougm 		case 'v':
30174653Sdougm 			verbose++;
30184653Sdougm 			break;
30194653Sdougm 		case 'f':
30204653Sdougm 			force++;
30214653Sdougm 			break;
30224653Sdougm 		case 's':
30234653Sdougm 			/*
30244653Sdougm 			 * Remove share path from group. Currently limit
30254653Sdougm 			 * to one share per command.
30264653Sdougm 			 */
30274653Sdougm 			if (sharepath != NULL) {
30284653Sdougm 				(void) printf(gettext(
30294653Sdougm 				    "Removing multiple shares not "
30303034Sdougm 				    "supported\n"));
30314653Sdougm 				return (SA_SYNTAX_ERR);
30324653Sdougm 			}
30334653Sdougm 			sharepath = optarg;
30344653Sdougm 			break;
30355331Samw 		case 'r':
30365331Samw 			/*
30375331Samw 			 * Remove share from group if last resource or remove
30385331Samw 			 * resource from share if multiple resources.
30395331Samw 			 */
30405331Samw 			if (rsrcname != NULL) {
30415331Samw 				(void) printf(gettext(
30425331Samw 				    "Removing multiple resource names not "
30435331Samw 				    "supported\n"));
30445331Samw 				return (SA_SYNTAX_ERR);
30455331Samw 			}
30465331Samw 			rsrcname = optarg;
30475331Samw 			break;
30486019Sdougm 		case 'h':
30496019Sdougm 			/* optopt on valid arg isn't defined */
30506019Sdougm 			optopt = c;
30516019Sdougm 			/*FALLTHROUGH*/
30526019Sdougm 		case '?':
30534653Sdougm 		default:
30546019Sdougm 			/*
30556019Sdougm 			 * Since a bad option gets to here, sort it
30566019Sdougm 			 * out and return a syntax error return value
30576019Sdougm 			 * if necessary.
30586019Sdougm 			 */
30596019Sdougm 			switch (optopt) {
30606019Sdougm 			default:
30616019Sdougm 				ret = SA_SYNTAX_ERR;
30626019Sdougm 				break;
30636019Sdougm 			case 'h':
30646019Sdougm 			case '?':
30656019Sdougm 				break;
30666019Sdougm 			}
30674653Sdougm 			(void) printf(gettext("usage: %s\n"),
30684653Sdougm 			    sa_get_usage(USAGE_REMOVE_SHARE));
30696019Sdougm 			return (ret);
30703034Sdougm 		}
30713034Sdougm 	}
30723034Sdougm 
30735331Samw 	if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) {
30745331Samw 		if (sharepath == NULL && rsrcname == NULL) {
30753034Sdougm 			(void) printf(gettext("usage: %s\n"),
30764653Sdougm 			    sa_get_usage(USAGE_REMOVE_SHARE));
30775331Samw 			(void) printf(gettext("\t-s sharepath or -r resource"
30785331Samw 			    " must be specified\n"));
30794653Sdougm 			ret = SA_BAD_PATH;
30804653Sdougm 		} else {
30814653Sdougm 			ret = SA_OK;
30824653Sdougm 		}
30833034Sdougm 	}
30844653Sdougm 	if (ret != SA_OK) {
30854653Sdougm 		return (ret);
30864653Sdougm 	}
30874653Sdougm 
30884653Sdougm 	if (optind < argc) {
30893034Sdougm 		if ((optind + 1) < argc) {
30904653Sdougm 			(void) printf(gettext("Extraneous group(s) at end of "
30914653Sdougm 			    "command\n"));
30924653Sdougm 			ret = SA_SYNTAX_ERR;
30933034Sdougm 		} else {
30944653Sdougm 			group = sa_get_group(handle, argv[optind]);
30954653Sdougm 			if (group == NULL) {
30964653Sdougm 				(void) printf(gettext(
30974653Sdougm 				    "Group \"%s\" not found\n"), argv[optind]);
30984653Sdougm 				ret = SA_NO_SUCH_GROUP;
30994653Sdougm 			}
31003034Sdougm 		}
31014653Sdougm 	} else {
31023034Sdougm 		group = NULL;
31034653Sdougm 	}
31044653Sdougm 
31055331Samw 	if (rsrcname != NULL) {
31065331Samw 		resource = sa_find_resource(handle, rsrcname);
31075331Samw 		if (resource == NULL) {
31085331Samw 			ret = SA_NO_SUCH_RESOURCE;
31095331Samw 			(void) printf(gettext(
31105331Samw 			    "Resource name not found for share: %s\n"),
31115331Samw 			    rsrcname);
31125331Samw 		}
31135331Samw 	}
31145331Samw 
31154653Sdougm 	/*
31164653Sdougm 	 * Lookup the path in the internal configuration. Care
31174653Sdougm 	 * must be taken to handle the case where the
31184653Sdougm 	 * underlying path has been removed since we need to
31194653Sdougm 	 * be able to deal with that as well.
31204653Sdougm 	 */
31214653Sdougm 	if (ret == SA_OK) {
31225331Samw 		if (sharepath != NULL) {
31235331Samw 			if (group != NULL)
31245331Samw 				share = sa_get_share(group, sharepath);
31255331Samw 			else
31265331Samw 				share = sa_find_share(handle, sharepath);
31275331Samw 		}
31285331Samw 
31295331Samw 		if (resource != NULL) {
31305331Samw 			sa_share_t rsrcshare;
31315331Samw 			rsrcshare = sa_get_resource_parent(resource);
31325331Samw 			if (share == NULL)
31335331Samw 				share = rsrcshare;
31345331Samw 			else if (share != rsrcshare) {
31355331Samw 				ret = SA_NO_SUCH_RESOURCE;
31365331Samw 				(void) printf(gettext(
31375331Samw 				    "Bad resource name for share: %s\n"),
31385331Samw 				    rsrcname);
31395331Samw 				share = NULL;
31405331Samw 			}
31415331Samw 		}
31425331Samw 
31433663Sdougm 		/*
31443663Sdougm 		 * If we didn't find the share with the provided path,
31453663Sdougm 		 * it may be a symlink so attempt to resolve it using
31463663Sdougm 		 * realpath and try again. Realpath will resolve any
31473663Sdougm 		 * symlinks and place them in "dir". Note that
31483663Sdougm 		 * sharepath is only used for the lookup the first
31493663Sdougm 		 * time and later for error messages. dir will be used
31503663Sdougm 		 * on the second attempt. Once a share is found, all
31513663Sdougm 		 * operations are based off of the share variable.
31523663Sdougm 		 */
31533663Sdougm 		if (share == NULL) {
31544653Sdougm 			if (realpath(sharepath, dir) == NULL) {
31554653Sdougm 				ret = SA_BAD_PATH;
31564653Sdougm 				(void) printf(gettext(
31574653Sdougm 				    "Path is not valid: %s\n"), sharepath);
31584653Sdougm 			} else {
31594653Sdougm 				if (group != NULL)
31604653Sdougm 					share = sa_get_share(group, dir);
31614653Sdougm 				else
31624653Sdougm 					share = sa_find_share(handle, dir);
31634653Sdougm 			}
31643663Sdougm 		}
31654653Sdougm 	}
31664653Sdougm 
31674653Sdougm 	/*
31684653Sdougm 	 * If there hasn't been an error, there was likely a
31694653Sdougm 	 * path found. If not, give the appropriate error
31704653Sdougm 	 * message and set the return error. If it was found,
31714653Sdougm 	 * then disable the share and then remove it from the
31724653Sdougm 	 * configuration.
31734653Sdougm 	 */
31744653Sdougm 	if (ret != SA_OK) {
31754653Sdougm 		return (ret);
31764653Sdougm 	}
31774653Sdougm 	if (share == NULL) {
31784653Sdougm 		if (group != NULL)
31793034Sdougm 			(void) printf(gettext("Share not found in group %s:"
31804653Sdougm 			    " %s\n"), argv[optind], sharepath);
31814653Sdougm 		else
31823034Sdougm 			(void) printf(gettext("Share not found: %s\n"),
31834653Sdougm 			    sharepath);
31845331Samw 		ret = SA_NO_SUCH_PATH;
31854653Sdougm 	} else {
31864653Sdougm 		if (group == NULL)
31873034Sdougm 			group = sa_get_parent_group(share);
31884653Sdougm 		if (!dryrun) {
31893034Sdougm 			if (ret == SA_OK) {
31905331Samw 				if (resource != NULL)
31915331Samw 					ret = sa_disable_resource(resource,
31925331Samw 					    NULL);
31935331Samw 				else
31945331Samw 					ret = sa_disable_share(share, NULL);
31953034Sdougm 				/*
31964653Sdougm 				 * We don't care if it fails since it
31973663Sdougm 				 * could be disabled already. Some
31983663Sdougm 				 * unexpected errors could occur that
31993663Sdougm 				 * prevent removal, so also check for
32003663Sdougm 				 * force being set.
32013034Sdougm 				 */
32025331Samw 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
32035331Samw 				    ret == SA_NOT_SUPPORTED ||
32045331Samw 				    ret == SA_SYSTEM_ERR || force) &&
32055331Samw 				    resource == NULL)
32065331Samw 					ret = sa_remove_share(share);
32075331Samw 
32085331Samw 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
32094653Sdougm 				    ret == SA_NOT_SUPPORTED ||
32105331Samw 				    ret == SA_SYSTEM_ERR || force) &&
32115331Samw 				    resource != NULL) {
32125331Samw 					ret = sa_remove_resource(resource);
32135331Samw 					if (ret == SA_OK) {
32145331Samw 						/*
32155331Samw 						 * If this was the
32165331Samw 						 * last one, remove
32175331Samw 						 * the share as well.
32185331Samw 						 */
32195331Samw 						resource =
32205331Samw 						    sa_get_share_resource(
32215331Samw 						    share, NULL);
32225331Samw 						if (resource == NULL)
32235331Samw 							ret = sa_remove_share(
32245331Samw 							    share);
32255331Samw 					}
32264653Sdougm 				}
32274653Sdougm 				if (ret == SA_OK)
32284653Sdougm 					ret = sa_update_config(handle);
32293034Sdougm 			}
32304653Sdougm 			if (ret != SA_OK)
32315331Samw 				(void) printf(gettext("Could not remove share:"
32325331Samw 				    " %s\n"), sa_errorstr(ret));
32334653Sdougm 		} else if (ret == SA_OK) {
32343034Sdougm 			char *pname;
32353034Sdougm 			pname = sa_get_group_attr(group, "name");
32363034Sdougm 			if (pname != NULL) {
32374653Sdougm 				auth = check_authorizations(pname, flags);
32384653Sdougm 				sa_free_attr_string(pname);
32393034Sdougm 			}
32403034Sdougm 			if (!auth && verbose) {
32414653Sdougm 				(void) printf(gettext(
32424653Sdougm 				    "Command would fail: %s\n"),
32434653Sdougm 				    sa_errorstr(SA_NO_PERMISSION));
32443034Sdougm 			}
32453034Sdougm 		}
32463034Sdougm 	}
32473034Sdougm 	return (ret);
32483034Sdougm }
32493034Sdougm 
32503034Sdougm /*
32513034Sdougm  * sa_set_share(flags, argc, argv)
32523034Sdougm  *
32533034Sdougm  * implements set-share subcommand.
32543034Sdougm  */
32553034Sdougm 
32563034Sdougm int
32573910Sdougm sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[])
32583034Sdougm {
32593034Sdougm 	int dryrun = 0;
32603034Sdougm 	int c;
32613034Sdougm 	int ret = SA_OK;
32623034Sdougm 	sa_group_t group, sharegroup;
32635772Sas200622 	sa_share_t share = NULL;
32645331Samw 	sa_resource_t resource = NULL;
32653034Sdougm 	char *sharepath = NULL;
32663034Sdougm 	char *description = NULL;
32675331Samw 	char *rsrcname = NULL;
32685331Samw 	char *rsrc = NULL;
32695331Samw 	char *newname = NULL;
32705331Samw 	char *newrsrc;
32715331Samw 	char *groupname = NULL;
32723034Sdougm 	int auth;
32733034Sdougm 	int verbose = 0;
32743034Sdougm 
32753034Sdougm 	while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) {
32764653Sdougm 		switch (c) {
32774653Sdougm 		case 'n':
32784653Sdougm 			dryrun++;
32794653Sdougm 			break;
32804653Sdougm 		case 'd':
32814653Sdougm 			description = optarg;
32824653Sdougm 			break;
32834653Sdougm 		case 'v':
32844653Sdougm 			verbose++;
32854653Sdougm 			break;
32865331Samw 		case 'r':
32875331Samw 			/*
32885331Samw 			 * Update share by resource name
32895331Samw 			 */
32905331Samw 			if (rsrcname != NULL) {
32915331Samw 				(void) printf(gettext(
32925331Samw 				    "Updating multiple resource names not "
32935331Samw 				    "supported\n"));
32945331Samw 				return (SA_SYNTAX_ERR);
32955331Samw 			}
32965331Samw 			rsrcname = optarg;
32975331Samw 			break;
32984653Sdougm 		case 's':
32994653Sdougm 			/*
33004653Sdougm 			 * Save share path into group. Currently limit
33014653Sdougm 			 * to one share per command.
33024653Sdougm 			 */
33034653Sdougm 			if (sharepath != NULL) {
33044653Sdougm 				(void) printf(gettext(
33054653Sdougm 				    "Updating multiple shares not "
33063034Sdougm 				    "supported\n"));
33075331Samw 				return (SA_SYNTAX_ERR);
33084653Sdougm 			}
33094653Sdougm 			sharepath = optarg;
33104653Sdougm 			break;
33116019Sdougm 		case 'h':
33126019Sdougm 			/* optopt on valid arg isn't defined */
33136019Sdougm 			optopt = c;
33146019Sdougm 			/*FALLTHROUGH*/
33156019Sdougm 		case '?':
33164653Sdougm 		default:
33176019Sdougm 			/*
33186019Sdougm 			 * Since a bad option gets to here, sort it
33196019Sdougm 			 * out and return a syntax error return value
33206019Sdougm 			 * if necessary.
33216019Sdougm 			 */
33226019Sdougm 			switch (optopt) {
33236019Sdougm 			default:
33246019Sdougm 				ret = SA_SYNTAX_ERR;
33256019Sdougm 				break;
33266019Sdougm 			case 'h':
33276019Sdougm 			case '?':
33286019Sdougm 				break;
33296019Sdougm 			}
33304653Sdougm 			(void) printf(gettext("usage: %s\n"),
33314653Sdougm 			    sa_get_usage(USAGE_SET_SHARE));
33326019Sdougm 			return (ret);
33333034Sdougm 		}
33343034Sdougm 	}
33354653Sdougm 
33365331Samw 	if (optind >= argc && sharepath == NULL && rsrcname == NULL) {
33374653Sdougm 		if (sharepath == NULL) {
33384653Sdougm 			(void) printf(gettext("usage: %s\n"),
33394653Sdougm 			    sa_get_usage(USAGE_SET_SHARE));
33404653Sdougm 			(void) printf(gettext("\tgroup must be specified\n"));
33414653Sdougm 			ret = SA_BAD_PATH;
33424653Sdougm 		} else {
33434653Sdougm 			ret = SA_OK;
33444653Sdougm 		}
33453034Sdougm 	}
33463034Sdougm 	if ((optind + 1) < argc) {
33474653Sdougm 		(void) printf(gettext("usage: %s\n"),
33484653Sdougm 		    sa_get_usage(USAGE_SET_SHARE));
33494653Sdougm 		(void) printf(gettext("\tExtraneous group(s) at end\n"));
33504653Sdougm 		ret = SA_SYNTAX_ERR;
33513034Sdougm 	}
33524653Sdougm 
33535331Samw 	/*
33545331Samw 	 * Must have at least one of sharepath and rsrcrname.
33555331Samw 	 * It is a syntax error to be missing both.
33565331Samw 	 */
33575331Samw 	if (sharepath == NULL && rsrcname == NULL) {
33585331Samw 		(void) printf(gettext("usage: %s\n"),
33595331Samw 		    sa_get_usage(USAGE_SET_SHARE));
33605331Samw 		ret = SA_SYNTAX_ERR;
33615331Samw 	}
33625331Samw 
33634653Sdougm 	if (ret != SA_OK)
33644653Sdougm 		return (ret);
33654653Sdougm 
33664653Sdougm 	if (optind < argc) {
33673034Sdougm 		groupname = argv[optind];
33683910Sdougm 		group = sa_get_group(handle, groupname);
33694653Sdougm 	} else {
33703034Sdougm 		group = NULL;
33713034Sdougm 		groupname = NULL;
33724653Sdougm 	}
33735331Samw 	if (rsrcname != NULL) {
33745331Samw 		/*
33755331Samw 		 * If rsrcname exists, split rename syntax and then
33765331Samw 		 * convert to utf 8 if no errors.
33775331Samw 		 */
33785331Samw 		newname = strchr(rsrcname, '=');
33795331Samw 		if (newname != NULL) {
33805331Samw 			*newname++ = '\0';
33815331Samw 		}
33825331Samw 		if (!validresource(rsrcname)) {
33835331Samw 			ret = SA_INVALID_NAME;
33845331Samw 			(void) printf(gettext("Invalid resource name: "
33855331Samw 			    "\"%s\"\n"), rsrcname);
33865331Samw 		} else {
33875331Samw 			rsrc = conv_to_utf8(rsrcname);
33885331Samw 		}
33895331Samw 		if (newname != NULL) {
33905331Samw 			if (!validresource(newname)) {
33915331Samw 				ret = SA_INVALID_NAME;
33925331Samw 				(void) printf(gettext("Invalid resource name: "
33935331Samw 				    "%s\n"), newname);
33945331Samw 			} else {
33955331Samw 				newrsrc = conv_to_utf8(newname);
33965331Samw 			}
33975331Samw 		}
33985331Samw 	}
33995331Samw 
34005331Samw 	if (ret != SA_OK) {
34015331Samw 		if (rsrcname != NULL && rsrcname != rsrc)
34025331Samw 			sa_free_attr_string(rsrc);
34035331Samw 		if (newname != NULL && newname != newrsrc)
34045331Samw 			sa_free_attr_string(newrsrc);
34055331Samw 		return (ret);
34065331Samw 	}
34075331Samw 
34085331Samw 	if (sharepath != NULL) {
34095331Samw 		share = sa_find_share(handle, sharepath);
34105331Samw 	} else if (rsrcname != NULL) {
34115331Samw 		resource = sa_find_resource(handle, rsrc);
34125772Sas200622 		if (resource != NULL)
34135331Samw 			share = sa_get_resource_parent(resource);
34145772Sas200622 		else
34155772Sas200622 			ret = SA_NO_SUCH_RESOURCE;
34165331Samw 	}
34175331Samw 	if (share != NULL) {
34185331Samw 		sharegroup = sa_get_parent_group(share);
34195331Samw 		if (group != NULL && group != sharegroup) {
34205331Samw 			(void) printf(gettext("Group \"%s\" does not contain "
34215331Samw 			    "share %s\n"),
34225331Samw 			    argv[optind], sharepath);
34235331Samw 			ret = SA_BAD_PATH;
34245331Samw 		} else {
34255331Samw 			int delgroupname = 0;
34265331Samw 			if (groupname == NULL) {
34275331Samw 				groupname = sa_get_group_attr(sharegroup,
34285331Samw 				    "name");
34295331Samw 				delgroupname = 1;
34305331Samw 			}
34315331Samw 			if (groupname != NULL) {
34325331Samw 				auth = check_authorizations(groupname, flags);
34335331Samw 				if (delgroupname) {
34345331Samw 					sa_free_attr_string(groupname);
34355331Samw 					groupname = NULL;
34365331Samw 				}
34375331Samw 			} else {
34385331Samw 				ret = SA_NO_MEMORY;
34395331Samw 			}
34405331Samw 			if (rsrcname != NULL) {
34415331Samw 				resource = sa_find_resource(handle, rsrc);
34425331Samw 				if (!dryrun) {
34435331Samw 					if (newname != NULL &&
34445331Samw 					    resource != NULL)
34455331Samw 						ret = sa_rename_resource(
34465331Samw 						    resource, newrsrc);
34475331Samw 					else if (newname != NULL)
34485331Samw 						ret = SA_NO_SUCH_RESOURCE;
34495331Samw 					if (newname != NULL &&
34505331Samw 					    newname != newrsrc)
34515331Samw 						sa_free_attr_string(newrsrc);
34525331Samw 				}
34535331Samw 				if (rsrc != rsrcname)
34545331Samw 					sa_free_attr_string(rsrc);
34555331Samw 			}
34565331Samw 
34575331Samw 			/*
34585331Samw 			 * If the user has set a description, it will be
34595331Samw 			 * on the resource if -r was used otherwise it
34605331Samw 			 * must be on the share.
34615331Samw 			 */
34625967Scp160787 			if (!dryrun && ret == SA_OK && description != NULL) {
34635967Scp160787 				char *desc;
34645967Scp160787 				desc = conv_to_utf8(description);
34655331Samw 				if (resource != NULL)
34665967Scp160787 					ret = sa_set_resource_description(
34675967Scp160787 					    resource, desc);
34685331Samw 				else
34695967Scp160787 					ret = sa_set_share_description(share,
34705967Scp160787 					    desc);
34715967Scp160787 				if (desc != description)
34725967Scp160787 					sa_free_share_description(desc);
34735331Samw 			}
34745331Samw 		}
34755331Samw 		if (!dryrun && ret == SA_OK) {
34765331Samw 			if (resource != NULL)
34775331Samw 				(void) sa_enable_resource(resource, NULL);
34785331Samw 			ret = sa_update_config(handle);
34795331Samw 		}
34805331Samw 		switch (ret) {
34815331Samw 		case SA_DUPLICATE_NAME:
34825331Samw 			(void) printf(gettext("Resource name in use: %s\n"),
34835331Samw 			    rsrcname);
34845331Samw 			break;
34855331Samw 		default:
34865331Samw 			(void) printf(gettext("Could not set: %s\n"),
34875331Samw 			    sa_errorstr(ret));
34885331Samw 			break;
34895331Samw 		case SA_OK:
34905331Samw 			if (dryrun && !auth && verbose) {
34915331Samw 				(void) printf(gettext(
34925331Samw 				    "Command would fail: %s\n"),
34935331Samw 				    sa_errorstr(SA_NO_PERMISSION));
34945331Samw 			}
34955331Samw 			break;
34965331Samw 		}
34975331Samw 	} else {
34985772Sas200622 		switch (ret) {
34995772Sas200622 		case SA_NO_SUCH_RESOURCE:
35005772Sas200622 			(void) printf(gettext("Resource \"%s\" not found\n"),
35015772Sas200622 			    rsrcname);
35025772Sas200622 			break;
35035772Sas200622 		default:
35045772Sas200622 			if (sharepath != NULL) {
35055772Sas200622 				(void) printf(
35065772Sas200622 				    gettext("Share path \"%s\" not found\n"),
35075772Sas200622 				    sharepath);
35085772Sas200622 				ret = SA_NO_SUCH_PATH;
35095772Sas200622 			} else {
35105772Sas200622 				(void) printf(gettext("Set failed: %s\n"),
35115772Sas200622 				    sa_errorstr(ret));
35125772Sas200622 			}
35135772Sas200622 		}
35143034Sdougm 	}
35154653Sdougm 
35163034Sdougm 	return (ret);
35173034Sdougm }
35183034Sdougm 
35193034Sdougm /*
35203034Sdougm  * add_security(group, sectype, optlist, proto, *err)
35213034Sdougm  *
35223034Sdougm  * Helper function to add a security option (named optionset) to the
35233034Sdougm  * group.
35243034Sdougm  */
35253034Sdougm 
35263034Sdougm static int
35273034Sdougm add_security(sa_group_t group, char *sectype,
35285331Samw     struct options *optlist, char *proto, int *err)
35293034Sdougm {
35303034Sdougm 	sa_security_t security;
35313034Sdougm 	int ret = SA_OK;
35323034Sdougm 	int result = 0;
3533*6214Sdougm 	sa_handle_t handle;
35343034Sdougm 
35353034Sdougm 	sectype = sa_proto_space_alias(proto, sectype);
35363034Sdougm 	security = sa_get_security(group, sectype, proto);
35374653Sdougm 	if (security == NULL)
35384653Sdougm 		security = sa_create_security(group, sectype, proto);
35394653Sdougm 
35403034Sdougm 	if (sectype != NULL)
35414653Sdougm 		sa_free_attr_string(sectype);
35424653Sdougm 
35434653Sdougm 	if (security == NULL)
3544*6214Sdougm 		goto done;
3545*6214Sdougm 
3546*6214Sdougm 	handle = sa_find_group_handle(group);
3547*6214Sdougm 	if (handle == NULL) {
3548*6214Sdougm 		ret = SA_CONFIG_ERR;
3549*6214Sdougm 		goto done;
3550*6214Sdougm 	}
35514653Sdougm 	while (optlist != NULL) {
35523034Sdougm 		sa_property_t prop;
35533034Sdougm 		prop = sa_get_property(security, optlist->optname);
35543034Sdougm 		if (prop == NULL) {
35553034Sdougm 			/*
35564653Sdougm 			 * Add the property, but only if it is
35573034Sdougm 			 * a non-NULL or non-zero length value
35583034Sdougm 			 */
35594653Sdougm 			if (optlist->optvalue != NULL) {
35604653Sdougm 				prop = sa_create_property(optlist->optname,
35614653Sdougm 				    optlist->optvalue);
35624653Sdougm 				if (prop != NULL) {
3563*6214Sdougm 					ret = sa_valid_property(handle,
3564*6214Sdougm 					    security, proto, prop);
35654653Sdougm 					if (ret != SA_OK) {
35664653Sdougm 						(void) sa_remove_property(prop);
35674653Sdougm 						(void) printf(gettext(
35684653Sdougm 						    "Could not add "
35694653Sdougm 						    "property %s: %s\n"),
35704653Sdougm 						    optlist->optname,
35714653Sdougm 						    sa_errorstr(ret));
35724653Sdougm 					}
35734653Sdougm 					if (ret == SA_OK) {
35744653Sdougm 						ret = sa_add_property(security,
35754653Sdougm 						    prop);
35764653Sdougm 						if (ret != SA_OK) {
35774653Sdougm 							(void) printf(gettext(
35784653Sdougm 							    "Could not add "
35795331Samw 							    "property (%s=%s):"
35805331Samw 							    " %s\n"),
35814653Sdougm 							    optlist->optname,
35824653Sdougm 							    optlist->optvalue,
35834653Sdougm 							    sa_errorstr(ret));
35844653Sdougm 						} else {
35854653Sdougm 							result = 1;
35864653Sdougm 						}
35874653Sdougm 					}
35883034Sdougm 				}
35893034Sdougm 			}
35903034Sdougm 		} else {
35914653Sdougm 			ret = sa_update_property(prop, optlist->optvalue);
35924653Sdougm 			result = 1; /* should check if really changed */
35933034Sdougm 		}
35943034Sdougm 		optlist = optlist->next;
35954653Sdougm 	}
35964653Sdougm 	/*
35974653Sdougm 	 * When done, properties may have all been removed but
35984653Sdougm 	 * we need to keep the security type itself until
35994653Sdougm 	 * explicitly removed.
36004653Sdougm 	 */
36014653Sdougm 	if (result)
36023034Sdougm 		ret = sa_commit_properties(security, 0);
3603*6214Sdougm done:
36043034Sdougm 	*err = ret;
36053034Sdougm 	return (result);
36063034Sdougm }
36073034Sdougm 
36083034Sdougm /*
36095089Sdougm  * zfscheck(group, share)
36105089Sdougm  *
36115089Sdougm  * For the special case where a share was provided, make sure it is a
36125089Sdougm  * compatible path for a ZFS property change.  The only path
36135089Sdougm  * acceptable is the path that defines the zfs sub-group (dataset with
36145089Sdougm  * the sharenfs property set) and not one of the paths that inherited
36155089Sdougm  * the NFS properties. Returns SA_OK if it is usable and
36165089Sdougm  * SA_NOT_ALLOWED if it isn't.
36175089Sdougm  *
36185089Sdougm  * If group is not a ZFS group/subgroup, we assume OK since the check
36195089Sdougm  * on return will catch errors for those cases.  What we are looking
36205089Sdougm  * for here is that the group is ZFS and the share is not the defining
36215089Sdougm  * share.  All else is SA_OK.
36225089Sdougm  */
36235089Sdougm 
36245089Sdougm static int
36255089Sdougm zfscheck(sa_group_t group, sa_share_t share)
36265089Sdougm {
36275089Sdougm 	int ret = SA_OK;
36285089Sdougm 	char *attr;
36295089Sdougm 
36305089Sdougm 	if (sa_group_is_zfs(group)) {
36315089Sdougm 		/*
36325089Sdougm 		 * The group is a ZFS group.  Does the share represent
36335089Sdougm 		 * the dataset that defined the group? It is only OK
36345089Sdougm 		 * if the attribute "subgroup" exists on the share and
36355089Sdougm 		 * has a value of "true".
36365089Sdougm 		 */
36375089Sdougm 
36385089Sdougm 		ret = SA_NOT_ALLOWED;
36395089Sdougm 		attr = sa_get_share_attr(share, "subgroup");
36405089Sdougm 		if (attr != NULL) {
36415089Sdougm 			if (strcmp(attr, "true") == 0)
36425089Sdougm 				ret = SA_OK;
36435089Sdougm 			sa_free_attr_string(attr);
36445089Sdougm 		}
36455089Sdougm 	}
36465089Sdougm 	return (ret);
36475089Sdougm }
36485089Sdougm 
36495089Sdougm /*
36505331Samw  * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
36513034Sdougm  *
36523034Sdougm  * This function implements "set" when a name space (-S) is not
36533034Sdougm  * specified. It is a basic set. Options and other CLI parsing has
36543034Sdougm  * already been done.
36555331Samw  *
36565331Samw  * "rsrcname" is a "resource name". If it is non-NULL, it must match
36575331Samw  * the sharepath if present or group if present, otherwise it is used
36585331Samw  * to set options.
36595331Samw  *
36605331Samw  * Resource names may take options if the protocol supports it. If the
36615331Samw  * protocol doesn't support resource level options, rsrcname is just
36625331Samw  * an alias for the share.
36633034Sdougm  */
36643034Sdougm 
36653034Sdougm static int
36663910Sdougm basic_set(sa_handle_t handle, char *groupname, struct options *optlist,
36675331Samw     char *protocol, char *sharepath, char *rsrcname, int dryrun)
36683034Sdougm {
36693034Sdougm 	sa_group_t group;
36703034Sdougm 	int ret = SA_OK;
36713034Sdougm 	int change = 0;
36723034Sdougm 	struct list *worklist = NULL;
36733034Sdougm 
36743910Sdougm 	group = sa_get_group(handle, groupname);
36753034Sdougm 	if (group != NULL) {
36764653Sdougm 		sa_share_t share = NULL;
36775331Samw 		sa_resource_t resource = NULL;
36785331Samw 
36795331Samw 		/*
36805331Samw 		 * If there is a sharepath, make sure it belongs to
36815331Samw 		 * the group.
36825331Samw 		 */
36834653Sdougm 		if (sharepath != NULL) {
36844653Sdougm 			share = sa_get_share(group, sharepath);
36854653Sdougm 			if (share == NULL) {
36864653Sdougm 				(void) printf(gettext(
36874653Sdougm 				    "Share does not exist in group %s\n"),
36884653Sdougm 				    groupname, sharepath);
36894653Sdougm 				ret = SA_NO_SUCH_PATH;
36905089Sdougm 			} else {
36915089Sdougm 				/* if ZFS and OK, then only group */
36925089Sdougm 				ret = zfscheck(group, share);
36935089Sdougm 				if (ret == SA_OK &&
36945089Sdougm 				    sa_group_is_zfs(group))
36955089Sdougm 					share = NULL;
36965089Sdougm 				if (ret == SA_NOT_ALLOWED)
36975089Sdougm 					(void) printf(gettext(
36985089Sdougm 					    "Properties on ZFS group shares "
36995089Sdougm 					    "not supported: %s\n"), sharepath);
37004653Sdougm 			}
37013034Sdougm 		}
37025331Samw 
37035331Samw 		/*
37045331Samw 		 * If a resource name exists, make sure it belongs to
37055331Samw 		 * the share if present else it belongs to the
37065331Samw 		 * group. Also check the protocol to see if it
37075331Samw 		 * supports resource level properties or not. If not,
37085331Samw 		 * use share only.
37095331Samw 		 */
37105331Samw 		if (rsrcname != NULL) {
37115331Samw 			if (share != NULL) {
37125331Samw 				resource = sa_get_share_resource(share,
37135331Samw 				    rsrcname);
37145331Samw 				if (resource == NULL)
37155331Samw 					ret = SA_NO_SUCH_RESOURCE;
37165331Samw 			} else {
37175331Samw 				resource = sa_get_resource(group, rsrcname);
37185331Samw 				if (resource != NULL)
37195331Samw 					share = sa_get_resource_parent(
37205331Samw 					    resource);
37215331Samw 				else
37225331Samw 					ret = SA_NO_SUCH_RESOURCE;
37235331Samw 			}
37245331Samw 			if (ret == SA_OK && resource != NULL) {
37255331Samw 				uint64_t features;
37265331Samw 				/*
37275331Samw 				 * Check to see if the resource can take
37285331Samw 				 * properties. If so, stick the resource into
37295331Samw 				 * "share" so it will all just work.
37305331Samw 				 */
37315331Samw 				features = sa_proto_get_featureset(protocol);
37325331Samw 				if (features & SA_FEATURE_RESOURCE)
37335331Samw 					share = (sa_share_t)resource;
37345331Samw 			}
37355331Samw 		}
37365331Samw 
37374653Sdougm 		if (ret == SA_OK) {
37384653Sdougm 			/* group must exist */
3739*6214Sdougm 			ret = valid_options(handle, optlist, protocol,
37404653Sdougm 			    share == NULL ? group : share, NULL);
37414653Sdougm 			if (ret == SA_OK && !dryrun) {
37424653Sdougm 				if (share != NULL)
37434653Sdougm 					change |= add_optionset(share, optlist,
37444653Sdougm 					    protocol, &ret);
37454653Sdougm 				else
37464653Sdougm 					change |= add_optionset(group, optlist,
37474653Sdougm 					    protocol, &ret);
37484653Sdougm 				if (ret == SA_OK && change)
37494653Sdougm 					worklist = add_list(worklist, group,
37505331Samw 					    share, protocol);
37514653Sdougm 			}
37523034Sdougm 		}
37534653Sdougm 		free_opt(optlist);
37543034Sdougm 	} else {
37553034Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
37563034Sdougm 		ret = SA_NO_SUCH_GROUP;
37573034Sdougm 	}
37583034Sdougm 	/*
37593034Sdougm 	 * we have a group and potentially legal additions
37603034Sdougm 	 */
37613034Sdougm 
37624653Sdougm 	/*
37634653Sdougm 	 * Commit to configuration if not a dryrunp and properties
37644653Sdougm 	 * have changed.
37654653Sdougm 	 */
37664653Sdougm 	if (!dryrun && ret == SA_OK && change && worklist != NULL)
37673034Sdougm 		/* properties changed, so update all shares */
37685331Samw 		(void) enable_all_groups(handle, worklist, 0, 0, protocol,
37695331Samw 		    B_TRUE);
37704653Sdougm 
37713034Sdougm 	if (worklist != NULL)
37724653Sdougm 		free_list(worklist);
37733034Sdougm 	return (ret);
37743034Sdougm }
37753034Sdougm 
37763034Sdougm /*
37773034Sdougm  * space_set(groupname, optlist, protocol, sharepath, dryrun)
37783034Sdougm  *
37793034Sdougm  * This function implements "set" when a name space (-S) is
37803034Sdougm  * specified. It is a namespace set. Options and other CLI parsing has
37813034Sdougm  * already been done.
37823034Sdougm  */
37833034Sdougm 
37843034Sdougm static int
37853910Sdougm space_set(sa_handle_t handle, char *groupname, struct options *optlist,
37865331Samw     char *protocol, char *sharepath, int dryrun, char *sectype)
37873034Sdougm {
37883034Sdougm 	sa_group_t group;
37893034Sdougm 	int ret = SA_OK;
37903034Sdougm 	int change = 0;
37913034Sdougm 	struct list *worklist = NULL;
37923034Sdougm 
37933034Sdougm 	/*
37943034Sdougm 	 * make sure protcol and sectype are valid
37953034Sdougm 	 */
37963034Sdougm 
37973034Sdougm 	if (sa_proto_valid_space(protocol, sectype) == 0) {
37984653Sdougm 		(void) printf(gettext("Option space \"%s\" not valid "
37994653Sdougm 		    "for protocol.\n"), sectype);
38004653Sdougm 		return (SA_INVALID_SECURITY);
38013034Sdougm 	}
38023034Sdougm 
38033910Sdougm 	group = sa_get_group(handle, groupname);
38043034Sdougm 	if (group != NULL) {
38054653Sdougm 		sa_share_t share = NULL;
38064653Sdougm 		if (sharepath != NULL) {
38074653Sdougm 			share = sa_get_share(group, sharepath);
38084653Sdougm 			if (share == NULL) {
38094653Sdougm 				(void) printf(gettext(
38104653Sdougm 				    "Share does not exist in group %s\n"),
38114653Sdougm 				    groupname, sharepath);
38124653Sdougm 				ret = SA_NO_SUCH_PATH;
38135089Sdougm 			} else {
38145089Sdougm 				/* if ZFS and OK, then only group */
38155089Sdougm 				ret = zfscheck(group, share);
38165089Sdougm 				if (ret == SA_OK &&
38175089Sdougm 				    sa_group_is_zfs(group))
38185089Sdougm 					share = NULL;
38195089Sdougm 				if (ret == SA_NOT_ALLOWED)
38205089Sdougm 					(void) printf(gettext(
38215089Sdougm 					    "Properties on ZFS group shares "
38225089Sdougm 					    "not supported: %s\n"), sharepath);
38234653Sdougm 			}
38243034Sdougm 		}
38254653Sdougm 		if (ret == SA_OK) {
38264653Sdougm 			/* group must exist */
3827*6214Sdougm 			ret = valid_options(handle, optlist, protocol,
38284653Sdougm 			    share == NULL ? group : share, sectype);
38294653Sdougm 			if (ret == SA_OK && !dryrun) {
38304653Sdougm 				if (share != NULL)
38314653Sdougm 					change = add_security(share, sectype,
38324653Sdougm 					    optlist, protocol, &ret);
38334653Sdougm 				else
38344653Sdougm 					change = add_security(group, sectype,
38354653Sdougm 					    optlist, protocol, &ret);
38364653Sdougm 				if (ret != SA_OK)
38374653Sdougm 					(void) printf(gettext(
38384653Sdougm 					    "Could not set property: %s\n"),
38394653Sdougm 					    sa_errorstr(ret));
38404653Sdougm 			}
38414653Sdougm 			if (ret == SA_OK && change)
38425331Samw 				worklist = add_list(worklist, group, share,
38435331Samw 				    protocol);
38443034Sdougm 		}
38454653Sdougm 		free_opt(optlist);
38463034Sdougm 	} else {
38473034Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
38483034Sdougm 		ret = SA_NO_SUCH_GROUP;
38493034Sdougm 	}
38505331Samw 
38513034Sdougm 	/*
38525331Samw 	 * We have a group and potentially legal additions.
38533034Sdougm 	 */
38543034Sdougm 
38554653Sdougm 	/* Commit to configuration if not a dryrun */
38563034Sdougm 	if (!dryrun && ret == 0) {
38574653Sdougm 		if (change && worklist != NULL) {
38584653Sdougm 			/* properties changed, so update all shares */
38594653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
38605331Samw 			    protocol, B_TRUE);
38614653Sdougm 		}
38624653Sdougm 		ret = sa_update_config(handle);
38633034Sdougm 	}
38643034Sdougm 	if (worklist != NULL)
38654653Sdougm 		free_list(worklist);
38663034Sdougm 	return (ret);
38673034Sdougm }
38683034Sdougm 
38693034Sdougm /*
38703034Sdougm  * sa_set(flags, argc, argv)
38713034Sdougm  *
38723034Sdougm  * Implements the set subcommand. It keys off of -S to determine which
38733034Sdougm  * set of operations to actually do.
38743034Sdougm  */
38753034Sdougm 
38763034Sdougm int
38773910Sdougm sa_set(sa_handle_t handle, int flags, int argc, char *argv[])
38783034Sdougm {
38793034Sdougm 	char *groupname;
38803034Sdougm 	int verbose = 0;
38813034Sdougm 	int dryrun = 0;
38823034Sdougm 	int c;
38833034Sdougm 	char *protocol = NULL;
38843034Sdougm 	int ret = SA_OK;
38853034Sdougm 	struct options *optlist = NULL;
38865331Samw 	char *rsrcname = NULL;
38873034Sdougm 	char *sharepath = NULL;
38883034Sdougm 	char *optset = NULL;
38893034Sdougm 	int auth;
38903034Sdougm 
38915331Samw 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
38924653Sdougm 		switch (c) {
38934653Sdougm 		case 'v':
38944653Sdougm 			verbose++;
38954653Sdougm 			break;
38964653Sdougm 		case 'n':
38974653Sdougm 			dryrun++;
38984653Sdougm 			break;
38994653Sdougm 		case 'P':
39005331Samw 			if (protocol != NULL) {
39015331Samw 				(void) printf(gettext(
39025331Samw 				    "Specifying multiple protocols "
39035331Samw 				    "not supported: %s\n"), protocol);
39045331Samw 				return (SA_SYNTAX_ERR);
39055331Samw 			}
39064653Sdougm 			protocol = optarg;
39074653Sdougm 			if (!sa_valid_protocol(protocol)) {
39084653Sdougm 				(void) printf(gettext(
39094653Sdougm 				    "Invalid protocol specified: %s\n"),
39104653Sdougm 				    protocol);
39114653Sdougm 				return (SA_INVALID_PROTOCOL);
39124653Sdougm 			}
39134653Sdougm 			break;
39144653Sdougm 		case 'p':
39154653Sdougm 			ret = add_opt(&optlist, optarg, 0);
39164653Sdougm 			switch (ret) {
39174653Sdougm 			case OPT_ADD_SYNTAX:
39184653Sdougm 				(void) printf(gettext("Property syntax error:"
39194653Sdougm 				    " %s\n"), optarg);
39204653Sdougm 				return (SA_SYNTAX_ERR);
39214653Sdougm 			case OPT_ADD_MEMORY:
39224653Sdougm 				(void) printf(gettext("No memory to set "
39234653Sdougm 				    "property: %s\n"), optarg);
39244653Sdougm 				return (SA_NO_MEMORY);
39254653Sdougm 			default:
39264653Sdougm 				break;
39274653Sdougm 			}
39284653Sdougm 			break;
39295331Samw 		case 'r':
39305331Samw 			if (rsrcname != NULL) {
39315331Samw 				(void) printf(gettext(
39325331Samw 				    "Setting multiple resource names not"
39335331Samw 				    " supported\n"));
39345331Samw 				return (SA_SYNTAX_ERR);
39355331Samw 			}
39365331Samw 			rsrcname = optarg;
39375331Samw 			break;
39384653Sdougm 		case 's':
39395331Samw 			if (sharepath != NULL) {
39405331Samw 				(void) printf(gettext(
39415331Samw 				    "Setting multiple shares not supported\n"));
39425331Samw 				return (SA_SYNTAX_ERR);
39435331Samw 			}
39444653Sdougm 			sharepath = optarg;
39454653Sdougm 			break;
39464653Sdougm 		case 'S':
39475331Samw 			if (optset != NULL) {
39485331Samw 				(void) printf(gettext(
39495331Samw 				    "Specifying multiple property "
39505331Samw 				    "spaces not supported: %s\n"), optset);
39515331Samw 				return (SA_SYNTAX_ERR);
39525331Samw 			}
39534653Sdougm 			optset = optarg;
39544653Sdougm 			break;
39556019Sdougm 		case 'h':
39566019Sdougm 			/* optopt on valid arg isn't defined */
39576019Sdougm 			optopt = c;
39586019Sdougm 			/*FALLTHROUGH*/
39596019Sdougm 		case '?':
39604653Sdougm 		default:
39616019Sdougm 			/*
39626019Sdougm 			 * Since a bad option gets to here, sort it
39636019Sdougm 			 * out and return a syntax error return value
39646019Sdougm 			 * if necessary.
39656019Sdougm 			 */
39666019Sdougm 			switch (optopt) {
39676019Sdougm 			default:
39686019Sdougm 				ret = SA_SYNTAX_ERR;
39696019Sdougm 				break;
39706019Sdougm 			case 'h':
39716019Sdougm 			case '?':
39726019Sdougm 				break;
39736019Sdougm 			}
39744653Sdougm 			(void) printf(gettext("usage: %s\n"),
39754653Sdougm 			    sa_get_usage(USAGE_SET));
39766019Sdougm 			return (ret);
39773034Sdougm 		}
39783034Sdougm 	}
39793034Sdougm 
39803034Sdougm 	if (optlist != NULL)
39814653Sdougm 		ret = chk_opt(optlist, optset != NULL, protocol);
39823034Sdougm 
39833034Sdougm 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
39844653Sdougm 	    protocol == NULL || ret != OPT_ADD_OK) {
39854653Sdougm 		char *sep = "\t";
39864653Sdougm 
39874653Sdougm 		(void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET));
39884653Sdougm 		if (optind >= argc) {
39894653Sdougm 			(void) printf(gettext("%sgroup must be specified"),
39904653Sdougm 			    sep);
39914653Sdougm 			sep = ", ";
39924653Sdougm 		}
39934653Sdougm 		if (optlist == NULL) {
39944653Sdougm 			(void) printf(gettext("%sat least one property must be"
39954653Sdougm 			    " specified"), sep);
39964653Sdougm 			sep = ", ";
39974653Sdougm 		}
39984653Sdougm 		if (protocol == NULL) {
39994653Sdougm 			(void) printf(gettext("%sprotocol must be specified"),
40004653Sdougm 			    sep);
40014653Sdougm 			sep = ", ";
40024653Sdougm 		}
40034653Sdougm 		(void) printf("\n");
40044653Sdougm 		ret = SA_SYNTAX_ERR;
40053034Sdougm 	} else {
40063034Sdougm 		/*
40075089Sdougm 		 * Group already exists so we can proceed after a few
40085089Sdougm 		 * additional checks related to ZFS handling.
40093034Sdougm 		 */
40103034Sdougm 
40114653Sdougm 		groupname = argv[optind];
40125089Sdougm 		if (strcmp(groupname, "zfs") == 0) {
40135089Sdougm 			(void) printf(gettext("Changing properties for group "
40145089Sdougm 			    "\"zfs\" not allowed\n"));
40155089Sdougm 			return (SA_NOT_ALLOWED);
40165089Sdougm 		}
40175089Sdougm 
40184653Sdougm 		auth = check_authorizations(groupname, flags);
40194653Sdougm 		if (optset == NULL)
40204653Sdougm 			ret = basic_set(handle, groupname, optlist, protocol,
40215331Samw 			    sharepath, rsrcname, dryrun);
40224653Sdougm 		else
40234653Sdougm 			ret = space_set(handle, groupname, optlist, protocol,
40244653Sdougm 			    sharepath, dryrun, optset);
40254653Sdougm 		if (dryrun && ret == SA_OK && !auth && verbose) {
40264653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
40274653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
40284653Sdougm 		}
40293034Sdougm 	}
40303034Sdougm 	return (ret);
40313034Sdougm }
40323034Sdougm 
40333034Sdougm /*
40343034Sdougm  * remove_options(group, optlist, proto, *err)
40353034Sdougm  *
40364653Sdougm  * Helper function to actually remove options from a group after all
40373034Sdougm  * preprocessing is done.
40383034Sdougm  */
40393034Sdougm 
40403034Sdougm static int
40413034Sdougm remove_options(sa_group_t group, struct options *optlist,
40425331Samw     char *proto, int *err)
40433034Sdougm {
40443034Sdougm 	struct options *cur;
40453034Sdougm 	sa_optionset_t optionset;
40463034Sdougm 	sa_property_t prop;
40473034Sdougm 	int change = 0;
40483034Sdougm 	int ret = SA_OK;
40493034Sdougm 
40503034Sdougm 	optionset = sa_get_optionset(group, proto);
40513034Sdougm 	if (optionset != NULL) {
40524653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
40534653Sdougm 			prop = sa_get_property(optionset, cur->optname);
40544653Sdougm 			if (prop != NULL) {
40554653Sdougm 				ret = sa_remove_property(prop);
40564653Sdougm 				if (ret != SA_OK)
40574653Sdougm 					break;
40584653Sdougm 				change = 1;
40594653Sdougm 			}
40603034Sdougm 		}
40613034Sdougm 	}
40623034Sdougm 	if (ret == SA_OK && change)
40634653Sdougm 		ret = sa_commit_properties(optionset, 0);
40643034Sdougm 
40653034Sdougm 	if (err != NULL)
40664653Sdougm 		*err = ret;
40673034Sdougm 	return (change);
40683034Sdougm }
40693034Sdougm 
40703034Sdougm /*
40713034Sdougm  * valid_unset(group, optlist, proto)
40723034Sdougm  *
40733034Sdougm  * Sanity check the optlist to make sure they can be removed. Issue an
40743034Sdougm  * error if a property doesn't exist.
40753034Sdougm  */
40763034Sdougm 
40773034Sdougm static int
40783034Sdougm valid_unset(sa_group_t group, struct options *optlist, char *proto)
40793034Sdougm {
40803034Sdougm 	struct options *cur;
40813034Sdougm 	sa_optionset_t optionset;
40823034Sdougm 	sa_property_t prop;
40833034Sdougm 	int ret = SA_OK;
40843034Sdougm 
40853034Sdougm 	optionset = sa_get_optionset(group, proto);
40863034Sdougm 	if (optionset != NULL) {
40874653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
40884653Sdougm 			prop = sa_get_property(optionset, cur->optname);
40894653Sdougm 			if (prop == NULL) {
40904653Sdougm 				(void) printf(gettext(
40914653Sdougm 				    "Could not unset property %s: not set\n"),
40924653Sdougm 				    cur->optname);
40934653Sdougm 				ret = SA_NO_SUCH_PROP;
40944653Sdougm 			}
40953034Sdougm 		}
40963034Sdougm 	}
40973034Sdougm 	return (ret);
40983034Sdougm }
40993034Sdougm 
41003034Sdougm /*
41013034Sdougm  * valid_unset_security(group, optlist, proto)
41023034Sdougm  *
41033034Sdougm  * Sanity check the optlist to make sure they can be removed. Issue an
41043034Sdougm  * error if a property doesn't exist.
41053034Sdougm  */
41063034Sdougm 
41073034Sdougm static int
41083034Sdougm valid_unset_security(sa_group_t group, struct options *optlist, char *proto,
41095331Samw     char *sectype)
41103034Sdougm {
41113034Sdougm 	struct options *cur;
41123034Sdougm 	sa_security_t security;
41133034Sdougm 	sa_property_t prop;
41143034Sdougm 	int ret = SA_OK;
41153034Sdougm 	char *sec;
41163034Sdougm 
41173034Sdougm 	sec = sa_proto_space_alias(proto, sectype);
41183034Sdougm 	security = sa_get_security(group, sec, proto);
41193034Sdougm 	if (security != NULL) {
41204653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
41214653Sdougm 			prop = sa_get_property(security, cur->optname);
41224653Sdougm 			if (prop == NULL) {
41234653Sdougm 				(void) printf(gettext(
41244653Sdougm 				    "Could not unset property %s: not set\n"),
41254653Sdougm 				    cur->optname);
41264653Sdougm 				ret = SA_NO_SUCH_PROP;
41274653Sdougm 			}
41283034Sdougm 		}
41293034Sdougm 	} else {
41304653Sdougm 		(void) printf(gettext(
41314653Sdougm 		    "Could not unset %s: space not defined\n"), sectype);
41324653Sdougm 		ret = SA_NO_SUCH_SECURITY;
41333034Sdougm 	}
41343034Sdougm 	if (sec != NULL)
41354653Sdougm 		sa_free_attr_string(sec);
41363034Sdougm 	return (ret);
41373034Sdougm }
41383034Sdougm 
41393034Sdougm /*
41403034Sdougm  * remove_security(group, optlist, proto)
41413034Sdougm  *
41423034Sdougm  * Remove the properties since they were checked as valid.
41433034Sdougm  */
41443034Sdougm 
41453034Sdougm static int
41463034Sdougm remove_security(sa_group_t group, char *sectype,
41475331Samw     struct options *optlist, char *proto, int *err)
41483034Sdougm {
41493034Sdougm 	sa_security_t security;
41503034Sdougm 	int ret = SA_OK;
41513034Sdougm 	int change = 0;
41523034Sdougm 
41533034Sdougm 	sectype = sa_proto_space_alias(proto, sectype);
41543034Sdougm 	security = sa_get_security(group, sectype, proto);
41553034Sdougm 	if (sectype != NULL)
41564653Sdougm 		sa_free_attr_string(sectype);
41573034Sdougm 
41583034Sdougm 	if (security != NULL) {
41594653Sdougm 		while (optlist != NULL) {
41604653Sdougm 			sa_property_t prop;
41614653Sdougm 			prop = sa_get_property(security, optlist->optname);
41624653Sdougm 			if (prop != NULL) {
41634653Sdougm 				ret = sa_remove_property(prop);
41644653Sdougm 				if (ret != SA_OK)
41654653Sdougm 					break;
41664653Sdougm 				change = 1;
41674653Sdougm 			}
41684653Sdougm 			optlist = optlist->next;
41693034Sdougm 		}
41703034Sdougm 		/*
41713034Sdougm 		 * when done, properties may have all been removed but
41723034Sdougm 		 * we need to keep the security type itself until
41733034Sdougm 		 * explicitly removed.
41743034Sdougm 		 */
41754653Sdougm 		if (ret == SA_OK && change)
41764653Sdougm 			ret = sa_commit_properties(security, 0);
41773034Sdougm 	} else {
41784653Sdougm 		ret = SA_NO_SUCH_PROP;
41793034Sdougm 	}
41803034Sdougm 	if (err != NULL)
41814653Sdougm 		*err = ret;
41823034Sdougm 	return (change);
41833034Sdougm }
41843034Sdougm 
41853034Sdougm /*
41865331Samw  * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
41873034Sdougm  *
41884653Sdougm  * Unset non-named optionset properties.
41893034Sdougm  */
41903034Sdougm 
41913034Sdougm static int
41923910Sdougm basic_unset(sa_handle_t handle, char *groupname, struct options *optlist,
41935331Samw     char *protocol, char *sharepath, char *rsrcname, int dryrun)
41943034Sdougm {
41953034Sdougm 	sa_group_t group;
41963034Sdougm 	int ret = SA_OK;
41973034Sdougm 	int change = 0;
41983034Sdougm 	struct list *worklist = NULL;
41994653Sdougm 	sa_share_t share = NULL;
42005331Samw 	sa_resource_t resource = NULL;
42013034Sdougm 
42023910Sdougm 	group = sa_get_group(handle, groupname);
42034653Sdougm 	if (group == NULL)
42044653Sdougm 		return (ret);
42054653Sdougm 
42065331Samw 	/*
42075331Samw 	 * If there is a sharepath, make sure it belongs to
42085331Samw 	 * the group.
42095331Samw 	 */
42104653Sdougm 	if (sharepath != NULL) {
42113034Sdougm 		share = sa_get_share(group, sharepath);
42123034Sdougm 		if (share == NULL) {
42134653Sdougm 			(void) printf(gettext(
42144653Sdougm 			    "Share does not exist in group %s\n"),
42154653Sdougm 			    groupname, sharepath);
42164653Sdougm 			ret = SA_NO_SUCH_PATH;
42173034Sdougm 		}
42184653Sdougm 	}
42195331Samw 	/*
42205331Samw 	 * If a resource name exists, make sure it belongs to
42215331Samw 	 * the share if present else it belongs to the
42225331Samw 	 * group. Also check the protocol to see if it
42235331Samw 	 * supports resource level properties or not. If not,
42245331Samw 	 * use share only.
42255331Samw 	 */
42265331Samw 	if (rsrcname != NULL) {
42275331Samw 		if (share != NULL) {
42285331Samw 			resource = sa_get_share_resource(share, rsrcname);
42295331Samw 			if (resource == NULL)
42305331Samw 				ret = SA_NO_SUCH_RESOURCE;
42315331Samw 		} else {
42325331Samw 			resource = sa_get_resource(group, rsrcname);
42335331Samw 			if (resource != NULL) {
42345331Samw 				share = sa_get_resource_parent(resource);
42355331Samw 			} else {
42365331Samw 				ret = SA_NO_SUCH_RESOURCE;
42375331Samw 			}
42385331Samw 		}
42395331Samw 		if (ret == SA_OK && resource != NULL) {
42405331Samw 			uint64_t features;
42415331Samw 			/*
42425331Samw 			 * Check to see if the resource can take
42435331Samw 			 * properties. If so, stick the resource into
42445331Samw 			 * "share" so it will all just work.
42455331Samw 			 */
42465331Samw 			features = sa_proto_get_featureset(protocol);
42475331Samw 			if (features & SA_FEATURE_RESOURCE)
42485331Samw 				share = (sa_share_t)resource;
42495331Samw 		}
42505331Samw 	}
42515331Samw 
42524653Sdougm 	if (ret == SA_OK) {
42533034Sdougm 		/* group must exist */
42543034Sdougm 		ret = valid_unset(share != NULL ? share : group,
42554653Sdougm 		    optlist, protocol);
42563034Sdougm 		if (ret == SA_OK && !dryrun) {
42574653Sdougm 			if (share != NULL) {
42584653Sdougm 				sa_optionset_t optionset;
42594653Sdougm 				sa_property_t prop;
42604653Sdougm 				change |= remove_options(share, optlist,
42614653Sdougm 				    protocol, &ret);
42624653Sdougm 				/*
42634653Sdougm 				 * If a share optionset is
42644653Sdougm 				 * empty, remove it.
42654653Sdougm 				 */
42664653Sdougm 				optionset = sa_get_optionset((sa_share_t)share,
42674653Sdougm 				    protocol);
42684653Sdougm 				if (optionset != NULL) {
42694653Sdougm 					prop = sa_get_property(optionset, NULL);
42704653Sdougm 					if (prop == NULL)
42714653Sdougm 						(void) sa_destroy_optionset(
42724653Sdougm 						    optionset);
42734653Sdougm 				}
42744653Sdougm 			} else {
42754653Sdougm 				change |= remove_options(group,
42764653Sdougm 				    optlist, protocol, &ret);
42773034Sdougm 			}
42784653Sdougm 			if (ret == SA_OK && change)
42795331Samw 				worklist = add_list(worklist, group, share,
42805331Samw 				    protocol);
42814653Sdougm 			if (ret != SA_OK)
42824653Sdougm 				(void) printf(gettext(
42834653Sdougm 				    "Could not remove properties: "
42844653Sdougm 				    "%s\n"), sa_errorstr(ret));
42853034Sdougm 		}
42864653Sdougm 	} else {
42875331Samw 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
42883034Sdougm 		ret = SA_NO_SUCH_GROUP;
42893034Sdougm 	}
42904653Sdougm 	free_opt(optlist);
42913034Sdougm 
42923034Sdougm 	/*
42934653Sdougm 	 * We have a group and potentially legal additions
42944653Sdougm 	 *
42954653Sdougm 	 * Commit to configuration if not a dryrun
42963034Sdougm 	 */
42973034Sdougm 	if (!dryrun && ret == SA_OK) {
42984653Sdougm 		if (change && worklist != NULL) {
42994653Sdougm 			/* properties changed, so update all shares */
43004653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
43015331Samw 			    protocol, B_TRUE);
43024653Sdougm 		}
43033034Sdougm 	}
43043034Sdougm 	if (worklist != NULL)
43054653Sdougm 		free_list(worklist);
43063034Sdougm 	return (ret);
43073034Sdougm }
43083034Sdougm 
43093034Sdougm /*
43103034Sdougm  * space_unset(groupname, optlist, protocol, sharepath, dryrun)
43113034Sdougm  *
43124653Sdougm  * Unset named optionset properties.
43133034Sdougm  */
43143034Sdougm static int
43153910Sdougm space_unset(sa_handle_t handle, char *groupname, struct options *optlist,
43165331Samw     char *protocol, char *sharepath, int dryrun, char *sectype)
43173034Sdougm {
43183034Sdougm 	sa_group_t group;
43193034Sdougm 	int ret = SA_OK;
43203034Sdougm 	int change = 0;
43213034Sdougm 	struct list *worklist = NULL;
43224653Sdougm 	sa_share_t share = NULL;
43233034Sdougm 
43243910Sdougm 	group = sa_get_group(handle, groupname);
43254653Sdougm 	if (group == NULL) {
43264653Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
43274653Sdougm 		return (SA_NO_SUCH_GROUP);
43284653Sdougm 	}
43294653Sdougm 	if (sharepath != NULL) {
43303034Sdougm 		share = sa_get_share(group, sharepath);
43313034Sdougm 		if (share == NULL) {
43324653Sdougm 			(void) printf(gettext(
43334653Sdougm 			    "Share does not exist in group %s\n"),
43344653Sdougm 			    groupname, sharepath);
43354653Sdougm 			return (SA_NO_SUCH_PATH);
43363034Sdougm 		}
43374653Sdougm 	}
43385331Samw 	ret = valid_unset_security(share != NULL ? share : group,
43395331Samw 	    optlist, protocol, sectype);
43404653Sdougm 
43414653Sdougm 	if (ret == SA_OK && !dryrun) {
43424653Sdougm 		if (optlist != NULL) {
43433034Sdougm 			if (share != NULL) {
43444653Sdougm 				sa_security_t optionset;
43454653Sdougm 				sa_property_t prop;
43464653Sdougm 				change = remove_security(share,
43474653Sdougm 				    sectype, optlist, protocol, &ret);
43484653Sdougm 
43494653Sdougm 				/* If a share security is empty, remove it */
43504653Sdougm 				optionset = sa_get_security((sa_group_t)share,
43514653Sdougm 				    sectype, protocol);
43524653Sdougm 				if (optionset != NULL) {
43534653Sdougm 					prop = sa_get_property(optionset,
43544653Sdougm 					    NULL);
43554653Sdougm 					if (prop == NULL)
43564653Sdougm 						ret = sa_destroy_security(
43574653Sdougm 						    optionset);
43584653Sdougm 				}
43593034Sdougm 			} else {
43604653Sdougm 				change = remove_security(group, sectype,
43614653Sdougm 				    optlist, protocol, &ret);
43623034Sdougm 			}
43634653Sdougm 		} else {
43643034Sdougm 			sa_security_t security;
43653034Sdougm 			char *sec;
43663034Sdougm 			sec = sa_proto_space_alias(protocol, sectype);
43673034Sdougm 			security = sa_get_security(group, sec, protocol);
43683034Sdougm 			if (sec != NULL)
43694653Sdougm 				sa_free_attr_string(sec);
43703034Sdougm 			if (security != NULL) {
43714653Sdougm 				ret = sa_destroy_security(security);
43724653Sdougm 				if (ret == SA_OK)
43734653Sdougm 					change = 1;
43743034Sdougm 			} else {
43754653Sdougm 				ret = SA_NO_SUCH_PROP;
43763034Sdougm 			}
43774653Sdougm 		}
43784653Sdougm 		if (ret != SA_OK)
43793034Sdougm 			(void) printf(gettext("Could not unset property: %s\n"),
43804653Sdougm 			    sa_errorstr(ret));
43813034Sdougm 	}
43824653Sdougm 
43834653Sdougm 	if (ret == SA_OK && change)
43845331Samw 		worklist = add_list(worklist, group, 0, protocol);
43854653Sdougm 
43863034Sdougm 	free_opt(optlist);
43873034Sdougm 	/*
43884653Sdougm 	 * We have a group and potentially legal additions
43893034Sdougm 	 */
43903034Sdougm 
43914653Sdougm 	/* Commit to configuration if not a dryrun */
43923034Sdougm 	if (!dryrun && ret == 0) {
43933034Sdougm 		/* properties changed, so update all shares */
43944653Sdougm 		if (change && worklist != NULL)
43954653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
43965331Samw 			    protocol, B_TRUE);
43974653Sdougm 		ret = sa_update_config(handle);
43983034Sdougm 	}
43993034Sdougm 	if (worklist != NULL)
44004653Sdougm 		free_list(worklist);
44013034Sdougm 	return (ret);
44023034Sdougm }
44033034Sdougm 
44043034Sdougm /*
44053034Sdougm  * sa_unset(flags, argc, argv)
44063034Sdougm  *
44074653Sdougm  * Implements the unset subcommand. Parsing done here and then basic
44083034Sdougm  * or space versions of the real code are called.
44093034Sdougm  */
44103034Sdougm 
44113034Sdougm int
44123910Sdougm sa_unset(sa_handle_t handle, int flags, int argc, char *argv[])
44133034Sdougm {
44143034Sdougm 	char *groupname;
44153034Sdougm 	int verbose = 0;
44163034Sdougm 	int dryrun = 0;
44173034Sdougm 	int c;
44183034Sdougm 	char *protocol = NULL;
44193034Sdougm 	int ret = SA_OK;
44203034Sdougm 	struct options *optlist = NULL;
44215331Samw 	char *rsrcname = NULL;
44223034Sdougm 	char *sharepath = NULL;
44233034Sdougm 	char *optset = NULL;
44243034Sdougm 	int auth;
44253034Sdougm 
44265331Samw 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
44274653Sdougm 		switch (c) {
44284653Sdougm 		case 'v':
44294653Sdougm 			verbose++;
44304653Sdougm 			break;
44314653Sdougm 		case 'n':
44324653Sdougm 			dryrun++;
44334653Sdougm 			break;
44344653Sdougm 		case 'P':
44355331Samw 			if (protocol != NULL) {
44365331Samw 				(void) printf(gettext(
44375331Samw 				    "Specifying multiple protocols "
44385331Samw 				    "not supported: %s\n"), protocol);
44395331Samw 				return (SA_SYNTAX_ERR);
44405331Samw 			}
44414653Sdougm 			protocol = optarg;
44424653Sdougm 			if (!sa_valid_protocol(protocol)) {
44434653Sdougm 				(void) printf(gettext(
44444653Sdougm 				    "Invalid protocol specified: %s\n"),
44454653Sdougm 				    protocol);
44464653Sdougm 				return (SA_INVALID_PROTOCOL);
44474653Sdougm 			}
44484653Sdougm 			break;
44494653Sdougm 		case 'p':
44504653Sdougm 			ret = add_opt(&optlist, optarg, 1);
44514653Sdougm 			switch (ret) {
44524653Sdougm 			case OPT_ADD_SYNTAX:
44534653Sdougm 				(void) printf(gettext("Property syntax error "
44544653Sdougm 				    "for property %s\n"), optarg);
44554653Sdougm 				return (SA_SYNTAX_ERR);
44564653Sdougm 
44574653Sdougm 			case OPT_ADD_PROPERTY:
44584653Sdougm 				(void) printf(gettext("Properties need to be "
44594653Sdougm 				    "set with set command: %s\n"), optarg);
44604653Sdougm 				return (SA_SYNTAX_ERR);
44614653Sdougm 
44624653Sdougm 			default:
44634653Sdougm 				break;
44644653Sdougm 			}
44654653Sdougm 			break;
44665331Samw 		case 'r':
44675331Samw 			/*
44685331Samw 			 * Unset properties on resource if applicable or on
44695331Samw 			 * share if resource for this protocol doesn't use
44705331Samw 			 * resources.
44715331Samw 			 */
44725331Samw 			if (rsrcname != NULL) {
44735331Samw 				(void) printf(gettext(
44745331Samw 				    "Unsetting multiple resource "
44755331Samw 				    "names not supported\n"));
44765331Samw 				return (SA_SYNTAX_ERR);
44775331Samw 			}
44785331Samw 			rsrcname = optarg;
44795331Samw 			break;
44804653Sdougm 		case 's':
44815331Samw 			if (sharepath != NULL) {
44825331Samw 				(void) printf(gettext(
44835331Samw 				    "Adding multiple shares not supported\n"));
44845331Samw 				return (SA_SYNTAX_ERR);
44855331Samw 			}
44864653Sdougm 			sharepath = optarg;
44874653Sdougm 			break;
44884653Sdougm 		case 'S':
44895331Samw 			if (optset != NULL) {
44905331Samw 				(void) printf(gettext(
44915331Samw 				    "Specifying multiple property "
44925331Samw 				    "spaces not supported: %s\n"), optset);
44935331Samw 				return (SA_SYNTAX_ERR);
44945331Samw 			}
44954653Sdougm 			optset = optarg;
44964653Sdougm 			break;
44976019Sdougm 		case 'h':
44986019Sdougm 			/* optopt on valid arg isn't defined */
44996019Sdougm 			optopt = c;
45006019Sdougm 			/*FALLTHROUGH*/
45016019Sdougm 		case '?':
45024653Sdougm 		default:
45036019Sdougm 			/*
45046019Sdougm 			 * Since a bad option gets to here, sort it
45056019Sdougm 			 * out and return a syntax error return value
45066019Sdougm 			 * if necessary.
45076019Sdougm 			 */
45086019Sdougm 			switch (optopt) {
45096019Sdougm 			default:
45106019Sdougm 				ret = SA_SYNTAX_ERR;
45116019Sdougm 				break;
45126019Sdougm 			case 'h':
45136019Sdougm 			case '?':
45146019Sdougm 				break;
45156019Sdougm 			}
45164653Sdougm 			(void) printf(gettext("usage: %s\n"),
45174653Sdougm 			    sa_get_usage(USAGE_UNSET));
45186019Sdougm 			return (ret);
45193034Sdougm 		}
45203034Sdougm 	}
45213034Sdougm 
45223034Sdougm 	if (optlist != NULL)
45234653Sdougm 		ret = chk_opt(optlist, optset != NULL, protocol);
45243034Sdougm 
45253034Sdougm 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
45263034Sdougm 	    protocol == NULL) {
45274653Sdougm 		char *sep = "\t";
45284653Sdougm 		(void) printf(gettext("usage: %s\n"),
45294653Sdougm 		    sa_get_usage(USAGE_UNSET));
45304653Sdougm 		if (optind >= argc) {
45314653Sdougm 			(void) printf(gettext("%sgroup must be specified"),
45324653Sdougm 			    sep);
45334653Sdougm 			sep = ", ";
45344653Sdougm 		}
45354653Sdougm 		if (optlist == NULL) {
45364653Sdougm 			(void) printf(gettext("%sat least one property must "
45374653Sdougm 			    "be specified"), sep);
45384653Sdougm 			sep = ", ";
45394653Sdougm 		}
45404653Sdougm 		if (protocol == NULL) {
45414653Sdougm 			(void) printf(gettext("%sprotocol must be specified"),
45424653Sdougm 			    sep);
45434653Sdougm 			sep = ", ";
45444653Sdougm 		}
45454653Sdougm 		(void) printf("\n");
45464653Sdougm 		ret = SA_SYNTAX_ERR;
45473034Sdougm 	} else {
45483034Sdougm 
45493034Sdougm 		/*
45504653Sdougm 		 * If a group already exists, we can only add a new
45513034Sdougm 		 * protocol to it and not create a new one or add the
45523034Sdougm 		 * same protocol again.
45533034Sdougm 		 */
45543034Sdougm 
45554653Sdougm 		groupname = argv[optind];
45564653Sdougm 		auth = check_authorizations(groupname, flags);
45574653Sdougm 		if (optset == NULL)
45584653Sdougm 			ret = basic_unset(handle, groupname, optlist, protocol,
45595331Samw 			    sharepath, rsrcname, dryrun);
45604653Sdougm 		else
45614653Sdougm 			ret = space_unset(handle, groupname, optlist, protocol,
45624653Sdougm 			    sharepath, dryrun, optset);
45634653Sdougm 
45644653Sdougm 		if (dryrun && ret == SA_OK && !auth && verbose)
45654653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
45664653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
45673034Sdougm 	}
45683034Sdougm 	return (ret);
45693034Sdougm }
45703034Sdougm 
45713034Sdougm /*
45723034Sdougm  * sa_enable_group(flags, argc, argv)
45733034Sdougm  *
45743034Sdougm  * Implements the enable subcommand
45753034Sdougm  */
45763034Sdougm 
45773034Sdougm int
45783910Sdougm sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[])
45793034Sdougm {
45803034Sdougm 	int verbose = 0;
45813034Sdougm 	int dryrun = 0;
45823034Sdougm 	int all = 0;
45833034Sdougm 	int c;
45843034Sdougm 	int ret = SA_OK;
45853034Sdougm 	char *protocol = NULL;
45863034Sdougm 	char *state;
45873034Sdougm 	struct list *worklist = NULL;
45883034Sdougm 	int auth = 1;
45894653Sdougm 	sa_group_t group;
45903034Sdougm 
45913034Sdougm 	while ((c = getopt(argc, argv, "?havnP:")) != EOF) {
45924653Sdougm 		switch (c) {
45934653Sdougm 		case 'a':
45944653Sdougm 			all = 1;
45954653Sdougm 			break;
45964653Sdougm 		case 'n':
45974653Sdougm 			dryrun++;
45984653Sdougm 			break;
45994653Sdougm 		case 'P':
46005331Samw 			if (protocol != NULL) {
46015331Samw 				(void) printf(gettext(
46025331Samw 				    "Specifying multiple protocols "
46035331Samw 				    "not supported: %s\n"), protocol);
46045331Samw 				return (SA_SYNTAX_ERR);
46055331Samw 			}
46064653Sdougm 			protocol = optarg;
46074653Sdougm 			if (!sa_valid_protocol(protocol)) {
46084653Sdougm 				(void) printf(gettext(
46094653Sdougm 				    "Invalid protocol specified: %s\n"),
46103034Sdougm 				    protocol);
46114653Sdougm 				return (SA_INVALID_PROTOCOL);
46124653Sdougm 			}
46134653Sdougm 			break;
46144653Sdougm 		case 'v':
46154653Sdougm 			verbose++;
46164653Sdougm 			break;
46174653Sdougm 		case 'h':
46186019Sdougm 			/* optopt on valid arg isn't defined */
46196019Sdougm 			optopt = c;
46206019Sdougm 			/*FALLTHROUGH*/
46214653Sdougm 		case '?':
46226019Sdougm 		default:
46236019Sdougm 			/*
46246019Sdougm 			 * Since a bad option gets to here, sort it
46256019Sdougm 			 * out and return a syntax error return value
46266019Sdougm 			 * if necessary.
46276019Sdougm 			 */
46286019Sdougm 			switch (optopt) {
46296019Sdougm 			default:
46306019Sdougm 				ret = SA_SYNTAX_ERR;
46316019Sdougm 				break;
46326019Sdougm 			case 'h':
46336019Sdougm 			case '?':
46346019Sdougm 				(void) printf(gettext("usage: %s\n"),
46356019Sdougm 				    sa_get_usage(USAGE_ENABLE));
46366019Sdougm 				return (ret);
46376019Sdougm 			}
46383034Sdougm 		}
46393034Sdougm 	}
46403034Sdougm 
46413034Sdougm 	if (optind == argc && !all) {
46424653Sdougm 		(void) printf(gettext("usage: %s\n"),
46434653Sdougm 		    sa_get_usage(USAGE_ENABLE));
46444653Sdougm 		(void) printf(gettext("\tmust specify group\n"));
46454653Sdougm 		return (SA_NO_SUCH_PATH);
46464653Sdougm 	}
46474653Sdougm 	if (!all) {
46483034Sdougm 		while (optind < argc) {
46494653Sdougm 			group = sa_get_group(handle, argv[optind]);
46504653Sdougm 			if (group != NULL) {
46514653Sdougm 				auth &= check_authorizations(argv[optind],
46524653Sdougm 				    flags);
46534653Sdougm 				state = sa_get_group_attr(group, "state");
46544653Sdougm 				if (state != NULL &&
46554653Sdougm 				    strcmp(state, "enabled") == 0) {
46564653Sdougm 					/* already enabled */
46574653Sdougm 					if (verbose)
46584653Sdougm 						(void) printf(gettext(
46594653Sdougm 						    "Group \"%s\" is already "
46604653Sdougm 						    "enabled\n"),
46614653Sdougm 						    argv[optind]);
46624653Sdougm 					ret = SA_BUSY; /* already enabled */
46634653Sdougm 				} else {
46644653Sdougm 					worklist = add_list(worklist, group,
46655331Samw 					    0, protocol);
46664653Sdougm 					if (verbose)
46674653Sdougm 						(void) printf(gettext(
46684653Sdougm 						    "Enabling group \"%s\"\n"),
46694653Sdougm 						    argv[optind]);
46704653Sdougm 				}
46714653Sdougm 				if (state != NULL)
46724653Sdougm 					sa_free_attr_string(state);
46733034Sdougm 			} else {
46744653Sdougm 				ret = SA_NO_SUCH_GROUP;
46753034Sdougm 			}
46764653Sdougm 			optind++;
46773034Sdougm 		}
46784653Sdougm 	} else {
46794653Sdougm 		for (group = sa_get_group(handle, NULL);
46804653Sdougm 		    group != NULL;
46813034Sdougm 		    group = sa_get_next_group(group)) {
46825331Samw 			worklist = add_list(worklist, group, 0, protocol);
46833034Sdougm 		}
46844653Sdougm 	}
46854653Sdougm 	if (!dryrun && ret == SA_OK)
46865331Samw 		ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE);
46874653Sdougm 
46884653Sdougm 	if (ret != SA_OK && ret != SA_BUSY)
46893034Sdougm 		(void) printf(gettext("Could not enable group: %s\n"),
46904653Sdougm 		    sa_errorstr(ret));
46914653Sdougm 	if (ret == SA_BUSY)
46923034Sdougm 		ret = SA_OK;
46934653Sdougm 
46943034Sdougm 	if (worklist != NULL)
46954653Sdougm 		free_list(worklist);
46963034Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose) {
46974653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
46984653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
46993034Sdougm 	}
47003034Sdougm 	return (ret);
47013034Sdougm }
47023034Sdougm 
47033034Sdougm /*
47045331Samw  * disable_group(group, proto)
47053034Sdougm  *
47065331Samw  * Disable all the shares in the specified group.. This is a helper
47075331Samw  * for disable_all_groups in order to simplify regular and subgroup
47085331Samw  * (zfs) disabling. Group has already been checked for non-NULL.
47093034Sdougm  */
47103034Sdougm 
47113034Sdougm static int
47125331Samw disable_group(sa_group_t group, char *proto)
47133034Sdougm {
47143034Sdougm 	sa_share_t share;
47153034Sdougm 	int ret = SA_OK;
47163034Sdougm 
47175331Samw 	/*
47185331Samw 	 * If the protocol isn't enabled, skip it and treat as
47195331Samw 	 * successful.
47205331Samw 	 */
47215331Samw 	if (!has_protocol(group, proto))
47225331Samw 		return (ret);
47235331Samw 
47243034Sdougm 	for (share = sa_get_share(group, NULL);
47253034Sdougm 	    share != NULL && ret == SA_OK;
47263034Sdougm 	    share = sa_get_next_share(share)) {
47275331Samw 		ret = sa_disable_share(share, proto);
47284653Sdougm 		if (ret == SA_NO_SUCH_PATH) {
47294653Sdougm 			/*
47304653Sdougm 			 * this is OK since the path is gone. we can't
47314653Sdougm 			 * re-share it anyway so no error.
47324653Sdougm 			 */
47334653Sdougm 			ret = SA_OK;
47344653Sdougm 		}
47353034Sdougm 	}
47363034Sdougm 	return (ret);
47373034Sdougm }
47383034Sdougm 
47393034Sdougm /*
47403034Sdougm  * disable_all_groups(work, setstate)
47413034Sdougm  *
47423034Sdougm  * helper function that disables the shares in the list of groups
47433034Sdougm  * provided. It optionally marks the group as disabled. Used by both
47443034Sdougm  * enable and start subcommands.
47453034Sdougm  */
47463034Sdougm 
47473034Sdougm static int
47483910Sdougm disable_all_groups(sa_handle_t handle, struct list *work, int setstate)
47493034Sdougm {
47503034Sdougm 	int ret = SA_OK;
47513034Sdougm 	sa_group_t subgroup, group;
47523034Sdougm 
47533034Sdougm 	while (work != NULL && ret == SA_OK) {
47544653Sdougm 		group = (sa_group_t)work->item;
47554653Sdougm 		if (setstate)
47564653Sdougm 			ret = sa_set_group_attr(group, "state", "disabled");
47574653Sdougm 		if (ret == SA_OK) {
47584653Sdougm 			char *name;
47594653Sdougm 			name = sa_get_group_attr(group, "name");
47604653Sdougm 			if (name != NULL && strcmp(name, "zfs") == 0) {
47614653Sdougm 				/* need to get the sub-groups for stopping */
47624653Sdougm 				for (subgroup = sa_get_sub_group(group);
47634653Sdougm 				    subgroup != NULL;
47644653Sdougm 				    subgroup = sa_get_next_group(subgroup)) {
47655331Samw 					ret = disable_group(subgroup,
47665331Samw 					    work->proto);
47674653Sdougm 				}
47684653Sdougm 			} else {
47695331Samw 				ret = disable_group(group, work->proto);
47704653Sdougm 			}
47714653Sdougm 			/*
47724653Sdougm 			 * We don't want to "disable" since it won't come
47734653Sdougm 			 * up after a reboot.  The SMF framework should do
47744653Sdougm 			 * the right thing. On enable we do want to do
47754653Sdougm 			 * something.
47764653Sdougm 			 */
47773034Sdougm 		}
47784653Sdougm 		work = work->next;
47793034Sdougm 	}
47803034Sdougm 	if (ret == SA_OK)
47814653Sdougm 		ret = sa_update_config(handle);
47823034Sdougm 	return (ret);
47833034Sdougm }
47843034Sdougm 
47853034Sdougm /*
47863034Sdougm  * sa_disable_group(flags, argc, argv)
47873034Sdougm  *
47883034Sdougm  * Implements the disable subcommand
47893034Sdougm  */
47903034Sdougm 
47913034Sdougm int
47923910Sdougm sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[])
47933034Sdougm {
47943034Sdougm 	int verbose = 0;
47953034Sdougm 	int dryrun = 0;
47963034Sdougm 	int all = 0;
47973034Sdougm 	int c;
47983034Sdougm 	int ret = SA_OK;
47995331Samw 	char *protocol = NULL;
48003034Sdougm 	char *state;
48013034Sdougm 	struct list *worklist = NULL;
48024653Sdougm 	sa_group_t group;
48033034Sdougm 	int auth = 1;
48043034Sdougm 
48053034Sdougm 	while ((c = getopt(argc, argv, "?havn")) != EOF) {
48064653Sdougm 		switch (c) {
48074653Sdougm 		case 'a':
48084653Sdougm 			all = 1;
48094653Sdougm 			break;
48104653Sdougm 		case 'n':
48114653Sdougm 			dryrun++;
48124653Sdougm 			break;
48134653Sdougm 		case 'P':
48145331Samw 			if (protocol != NULL) {
48155331Samw 				(void) printf(gettext(
48165331Samw 				    "Specifying multiple protocols "
48175331Samw 				    "not supported: %s\n"), protocol);
48185331Samw 				return (SA_SYNTAX_ERR);
48195331Samw 			}
48204653Sdougm 			protocol = optarg;
48214653Sdougm 			if (!sa_valid_protocol(protocol)) {
48224653Sdougm 				(void) printf(gettext(
48234653Sdougm 				    "Invalid protocol specified: %s\n"),
48244653Sdougm 				    protocol);
48254653Sdougm 				return (SA_INVALID_PROTOCOL);
48264653Sdougm 			}
48274653Sdougm 			break;
48284653Sdougm 		case 'v':
48294653Sdougm 			verbose++;
48304653Sdougm 			break;
48316019Sdougm 		case 'h':
48326019Sdougm 			/* optopt on valid arg isn't defined */
48336019Sdougm 			optopt = c;
48346019Sdougm 			/*FALLTHROUGH*/
48356019Sdougm 		case '?':
48364653Sdougm 		default:
48376019Sdougm 			/*
48386019Sdougm 			 * Since a bad option gets to here, sort it
48396019Sdougm 			 * out and return a syntax error return value
48406019Sdougm 			 * if necessary.
48416019Sdougm 			 */
48426019Sdougm 			switch (optopt) {
48436019Sdougm 			default:
48446019Sdougm 				ret = SA_SYNTAX_ERR;
48456019Sdougm 				break;
48466019Sdougm 			case 'h':
48476019Sdougm 			case '?':
48486019Sdougm 				break;
48496019Sdougm 			}
48504653Sdougm 			(void) printf(gettext("usage: %s\n"),
48514653Sdougm 			    sa_get_usage(USAGE_DISABLE));
48526019Sdougm 			return (ret);
48533034Sdougm 		}
48543034Sdougm 	}
48553034Sdougm 
48563034Sdougm 	if (optind == argc && !all) {
48573034Sdougm 		(void) printf(gettext("usage: %s\n"),
48584653Sdougm 		    sa_get_usage(USAGE_DISABLE));
48593034Sdougm 		(void) printf(gettext("\tmust specify group\n"));
48604653Sdougm 		return (SA_NO_SUCH_PATH);
48614653Sdougm 	}
48624653Sdougm 	if (!all) {
48634653Sdougm 		while (optind < argc) {
48643910Sdougm 			group = sa_get_group(handle, argv[optind]);
48653034Sdougm 			if (group != NULL) {
48664653Sdougm 				auth &= check_authorizations(argv[optind],
48674653Sdougm 				    flags);
48684653Sdougm 				state = sa_get_group_attr(group, "state");
48694653Sdougm 				if (state == NULL ||
48704653Sdougm 				    strcmp(state, "disabled") == 0) {
48714653Sdougm 					/* already disabled */
48724653Sdougm 					if (verbose)
48734653Sdougm 						(void) printf(gettext(
48744653Sdougm 						    "Group \"%s\" is "
48754653Sdougm 						    "already disabled\n"),
48764653Sdougm 						    argv[optind]);
48775331Samw 					ret = SA_BUSY; /* already disabled */
48784653Sdougm 				} else {
48795331Samw 					worklist = add_list(worklist, group, 0,
48805331Samw 					    protocol);
48814653Sdougm 					if (verbose)
48824653Sdougm 						(void) printf(gettext(
48834653Sdougm 						    "Disabling group "
48844653Sdougm 						    "\"%s\"\n"), argv[optind]);
48854653Sdougm 				}
48864653Sdougm 				if (state != NULL)
48874653Sdougm 					sa_free_attr_string(state);
48883034Sdougm 			} else {
48894653Sdougm 				ret = SA_NO_SUCH_GROUP;
48903034Sdougm 			}
48913034Sdougm 			optind++;
48924653Sdougm 		}
48934653Sdougm 	} else {
48944653Sdougm 		for (group = sa_get_group(handle, NULL);
48954653Sdougm 		    group != NULL;
48964653Sdougm 		    group = sa_get_next_group(group))
48975331Samw 			worklist = add_list(worklist, group, 0, protocol);
48983034Sdougm 	}
48994653Sdougm 
49004653Sdougm 	if (ret == SA_OK && !dryrun)
49014653Sdougm 		ret = disable_all_groups(handle, worklist, 1);
49024653Sdougm 	if (ret != SA_OK && ret != SA_BUSY)
49034653Sdougm 		(void) printf(gettext("Could not disable group: %s\n"),
49044653Sdougm 		    sa_errorstr(ret));
49054653Sdougm 	if (ret == SA_BUSY)
49064653Sdougm 		ret = SA_OK;
49073034Sdougm 	if (worklist != NULL)
49084653Sdougm 		free_list(worklist);
49094653Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose)
49104653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
49114653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
49123034Sdougm 	return (ret);
49133034Sdougm }
49143034Sdougm 
49153034Sdougm /*
49163034Sdougm  * sa_start_group(flags, argc, argv)
49173034Sdougm  *
49183034Sdougm  * Implements the start command.
49193034Sdougm  * This is similar to enable except it doesn't change the state
49203034Sdougm  * of the group(s) and only enables shares if the group is already
49213034Sdougm  * enabled.
49223034Sdougm  */
49235331Samw 
49243034Sdougm int
49253910Sdougm sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[])
49263034Sdougm {
49273034Sdougm 	int verbose = 0;
49283034Sdougm 	int all = 0;
49293034Sdougm 	int c;
49303034Sdougm 	int ret = SMF_EXIT_OK;
49313034Sdougm 	char *protocol = NULL;
49323034Sdougm 	char *state;
49333034Sdougm 	struct list *worklist = NULL;
49344653Sdougm 	sa_group_t group;
49355331Samw #ifdef lint
49365331Samw 	flags = flags;
49375331Samw #endif
49383034Sdougm 
49393034Sdougm 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
49404653Sdougm 		switch (c) {
49414653Sdougm 		case 'a':
49424653Sdougm 			all = 1;
49434653Sdougm 			break;
49444653Sdougm 		case 'P':
49455331Samw 			if (protocol != NULL) {
49465331Samw 				(void) printf(gettext(
49475331Samw 				    "Specifying multiple protocols "
49485331Samw 				    "not supported: %s\n"), protocol);
49495331Samw 				return (SA_SYNTAX_ERR);
49505331Samw 			}
49514653Sdougm 			protocol = optarg;
49524653Sdougm 			if (!sa_valid_protocol(protocol)) {
49534653Sdougm 				(void) printf(gettext(
49544653Sdougm 				    "Invalid protocol specified: %s\n"),
49553034Sdougm 				    protocol);
49564653Sdougm 				return (SA_INVALID_PROTOCOL);
49574653Sdougm 			}
49584653Sdougm 			break;
49594653Sdougm 		case 'v':
49604653Sdougm 			verbose++;
49614653Sdougm 			break;
49626019Sdougm 		case 'h':
49636019Sdougm 			/* optopt on valid arg isn't defined */
49646019Sdougm 			optopt = c;
49656019Sdougm 			/*FALLTHROUGH*/
49666019Sdougm 		case '?':
49674653Sdougm 		default:
49686019Sdougm 			/*
49696019Sdougm 			 * Since a bad option gets to here, sort it
49706019Sdougm 			 * out and return a syntax error return value
49716019Sdougm 			 * if necessary.
49726019Sdougm 			 */
49736019Sdougm 			ret = SA_OK;
49746019Sdougm 			switch (optopt) {
49756019Sdougm 			default:
49766019Sdougm 				ret = SA_SYNTAX_ERR;
49776019Sdougm 				break;
49786019Sdougm 			case 'h':
49796019Sdougm 			case '?':
49806019Sdougm 				break;
49816019Sdougm 			}
49824653Sdougm 			(void) printf(gettext("usage: %s\n"),
49834653Sdougm 			    sa_get_usage(USAGE_START));
49846019Sdougm 			return (ret);
49853034Sdougm 		}
49863034Sdougm 	}
49873034Sdougm 
49883034Sdougm 	if (optind == argc && !all) {
49893034Sdougm 		(void) printf(gettext("usage: %s\n"),
49904653Sdougm 		    sa_get_usage(USAGE_START));
49914653Sdougm 		return (SMF_EXIT_ERR_FATAL);
49924653Sdougm 	}
49934653Sdougm 
49944653Sdougm 	if (!all) {
49954653Sdougm 		while (optind < argc) {
49963910Sdougm 			group = sa_get_group(handle, argv[optind]);
49973034Sdougm 			if (group != NULL) {
49984653Sdougm 				state = sa_get_group_attr(group, "state");
49994653Sdougm 				if (state == NULL ||
50004653Sdougm 				    strcmp(state, "enabled") == 0) {
50015331Samw 					worklist = add_list(worklist, group, 0,
50025331Samw 					    protocol);
50034653Sdougm 					if (verbose)
50044653Sdougm 						(void) printf(gettext(
50054653Sdougm 						    "Starting group \"%s\"\n"),
50064653Sdougm 						    argv[optind]);
50074653Sdougm 				} else {
50084653Sdougm 					/*
50094653Sdougm 					 * Determine if there are any
50105331Samw 					 * protocols.  If there aren't any,
50114653Sdougm 					 * then there isn't anything to do in
50124653Sdougm 					 * any case so no error.
50134653Sdougm 					 */
50144653Sdougm 					if (sa_get_optionset(group,
50154653Sdougm 					    protocol) != NULL) {
50164653Sdougm 						ret = SMF_EXIT_OK;
50174653Sdougm 					}
50183034Sdougm 				}
50194653Sdougm 				if (state != NULL)
50204653Sdougm 					sa_free_attr_string(state);
50213034Sdougm 			}
50223034Sdougm 			optind++;
50234653Sdougm 		}
50244653Sdougm 	} else {
50255331Samw 		for (group = sa_get_group(handle, NULL);
50265331Samw 		    group != NULL;
50274653Sdougm 		    group = sa_get_next_group(group)) {
50283034Sdougm 			state = sa_get_group_attr(group, "state");
50293034Sdougm 			if (state == NULL || strcmp(state, "enabled") == 0)
50305331Samw 				worklist = add_list(worklist, group, 0,
50315331Samw 				    protocol);
50323034Sdougm 			if (state != NULL)
50334653Sdougm 				sa_free_attr_string(state);
50343034Sdougm 		}
50353034Sdougm 	}
50364653Sdougm 
50375331Samw 	(void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE);
50384653Sdougm 
50393034Sdougm 	if (worklist != NULL)
50404653Sdougm 		free_list(worklist);
50413034Sdougm 	return (ret);
50423034Sdougm }
50433034Sdougm 
50443034Sdougm /*
50453034Sdougm  * sa_stop_group(flags, argc, argv)
50463034Sdougm  *
50473034Sdougm  * Implements the stop command.
50483034Sdougm  * This is similar to disable except it doesn't change the state
50493034Sdougm  * of the group(s) and only disables shares if the group is already
50503034Sdougm  * enabled.
50513034Sdougm  */
50523034Sdougm int
50533910Sdougm sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[])
50543034Sdougm {
50553034Sdougm 	int verbose = 0;
50563034Sdougm 	int all = 0;
50573034Sdougm 	int c;
50583034Sdougm 	int ret = SMF_EXIT_OK;
50593034Sdougm 	char *protocol = NULL;
50603034Sdougm 	char *state;
50613034Sdougm 	struct list *worklist = NULL;
50624653Sdougm 	sa_group_t group;
50635331Samw #ifdef lint
50645331Samw 	flags = flags;
50655331Samw #endif
50663034Sdougm 
50673034Sdougm 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
50684653Sdougm 		switch (c) {
50694653Sdougm 		case 'a':
50704653Sdougm 			all = 1;
50714653Sdougm 			break;
50724653Sdougm 		case 'P':
50735331Samw 			if (protocol != NULL) {
50745331Samw 				(void) printf(gettext(
50755331Samw 				    "Specifying multiple protocols "
50765331Samw 				    "not supported: %s\n"), protocol);
50775331Samw 				return (SA_SYNTAX_ERR);
50785331Samw 			}
50794653Sdougm 			protocol = optarg;
50804653Sdougm 			if (!sa_valid_protocol(protocol)) {
50814653Sdougm 				(void) printf(gettext(
50824653Sdougm 				    "Invalid protocol specified: %s\n"),
50834653Sdougm 				    protocol);
50844653Sdougm 				return (SA_INVALID_PROTOCOL);
50854653Sdougm 			}
50864653Sdougm 			break;
50874653Sdougm 		case 'v':
50884653Sdougm 			verbose++;
50894653Sdougm 			break;
50906019Sdougm 		case 'h':
50916019Sdougm 			/* optopt on valid arg isn't defined */
50926019Sdougm 			optopt = c;
50936019Sdougm 			/*FALLTHROUGH*/
50946019Sdougm 		case '?':
50954653Sdougm 		default:
50966019Sdougm 			/*
50976019Sdougm 			 * Since a bad option gets to here, sort it
50986019Sdougm 			 * out and return a syntax error return value
50996019Sdougm 			 * if necessary.
51006019Sdougm 			 */
51016019Sdougm 			ret = SA_OK;
51026019Sdougm 			switch (optopt) {
51036019Sdougm 			default:
51046019Sdougm 				ret = SA_SYNTAX_ERR;
51056019Sdougm 				break;
51066019Sdougm 			case 'h':
51076019Sdougm 			case '?':
51086019Sdougm 				break;
51096019Sdougm 			}
51104653Sdougm 			(void) printf(gettext("usage: %s\n"),
51114653Sdougm 			    sa_get_usage(USAGE_STOP));
51126019Sdougm 			return (ret);
51133034Sdougm 		}
51143034Sdougm 	}
51153034Sdougm 
51163034Sdougm 	if (optind == argc && !all) {
51174653Sdougm 		(void) printf(gettext("usage: %s\n"),
51184653Sdougm 		    sa_get_usage(USAGE_STOP));
51194653Sdougm 		return (SMF_EXIT_ERR_FATAL);
51204653Sdougm 	} else if (!all) {
51214653Sdougm 		while (optind < argc) {
51223910Sdougm 			group = sa_get_group(handle, argv[optind]);
51233034Sdougm 			if (group != NULL) {
51244653Sdougm 				state = sa_get_group_attr(group, "state");
51254653Sdougm 				if (state == NULL ||
51264653Sdougm 				    strcmp(state, "enabled") == 0) {
51275331Samw 					worklist = add_list(worklist, group, 0,
51285331Samw 					    protocol);
51294653Sdougm 					if (verbose)
51304653Sdougm 						(void) printf(gettext(
51314653Sdougm 						    "Stopping group \"%s\"\n"),
51324653Sdougm 						    argv[optind]);
51334653Sdougm 				} else {
51344653Sdougm 					ret = SMF_EXIT_OK;
51354653Sdougm 				}
51364653Sdougm 				if (state != NULL)
51374653Sdougm 					sa_free_attr_string(state);
51383034Sdougm 			}
51393034Sdougm 			optind++;
51404653Sdougm 		}
51414653Sdougm 	} else {
51425331Samw 		for (group = sa_get_group(handle, NULL);
51435331Samw 		    group != NULL;
51444653Sdougm 		    group = sa_get_next_group(group)) {
51453034Sdougm 			state = sa_get_group_attr(group, "state");
51463034Sdougm 			if (state == NULL || strcmp(state, "enabled") == 0)
51475331Samw 				worklist = add_list(worklist, group, 0,
51485331Samw 				    protocol);
51493034Sdougm 			if (state != NULL)
51504653Sdougm 				sa_free_attr_string(state);
51513034Sdougm 		}
51523034Sdougm 	}
51534653Sdougm 	(void) disable_all_groups(handle, worklist, 0);
51544653Sdougm 	ret = sa_update_config(handle);
51554653Sdougm 
51563034Sdougm 	if (worklist != NULL)
51574653Sdougm 		free_list(worklist);
51583034Sdougm 	return (ret);
51593034Sdougm }
51603034Sdougm 
51613034Sdougm /*
51623034Sdougm  * remove_all_options(share, proto)
51633034Sdougm  *
51643034Sdougm  * Removes all options on a share.
51653034Sdougm  */
51663034Sdougm 
51673034Sdougm static void
51683034Sdougm remove_all_options(sa_share_t share, char *proto)
51693034Sdougm {
51703034Sdougm 	sa_optionset_t optionset;
51713034Sdougm 	sa_security_t security;
51723034Sdougm 	sa_security_t prevsec = NULL;
51733034Sdougm 
51743034Sdougm 	optionset = sa_get_optionset(share, proto);
51753034Sdougm 	if (optionset != NULL)
51764653Sdougm 		(void) sa_destroy_optionset(optionset);
51773034Sdougm 	for (security = sa_get_security(share, NULL, NULL);
51783034Sdougm 	    security != NULL;
51793034Sdougm 	    security = sa_get_next_security(security)) {
51804653Sdougm 		char *type;
51813034Sdougm 		/*
51824653Sdougm 		 * We walk through the list.  prevsec keeps the
51833034Sdougm 		 * previous security so we can delete it without
51843034Sdougm 		 * destroying the list.
51853034Sdougm 		 */
51864653Sdougm 		if (prevsec != NULL) {
51874653Sdougm 			/* remove the previously seen security */
51884653Sdougm 			(void) sa_destroy_security(prevsec);
51894653Sdougm 			/* set to NULL so we don't try multiple times */
51904653Sdougm 			prevsec = NULL;
51914653Sdougm 		}
51924653Sdougm 		type = sa_get_security_attr(security, "type");
51934653Sdougm 		if (type != NULL) {
51944653Sdougm 			/*
51954653Sdougm 			 * if the security matches the specified protocol, we
51964653Sdougm 			 * want to remove it. prevsec holds it until either
51974653Sdougm 			 * the next pass or we fall out of the loop.
51984653Sdougm 			 */
51994653Sdougm 			if (strcmp(type, proto) == 0)
52004653Sdougm 				prevsec = security;
52014653Sdougm 			sa_free_attr_string(type);
52024653Sdougm 		}
52033034Sdougm 	}
52043034Sdougm 	/* in case there is one left */
52053034Sdougm 	if (prevsec != NULL)
52064653Sdougm 		(void) sa_destroy_security(prevsec);
52073034Sdougm }
52083034Sdougm 
52093034Sdougm 
52103034Sdougm /*
52113034Sdougm  * for legacy support, we need to handle the old syntax. This is what
52123034Sdougm  * we get if sharemgr is called with the name "share" rather than
52133034Sdougm  * sharemgr.
52143034Sdougm  */
52153034Sdougm 
52163034Sdougm static int
52173034Sdougm format_legacy_path(char *buff, int buffsize, char *proto, char *cmd)
52183034Sdougm {
52193034Sdougm 	int err;
52203034Sdougm 
52213034Sdougm 	err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd);
52223034Sdougm 	if (err > buffsize)
52234653Sdougm 		return (-1);
52243034Sdougm 	return (0);
52253034Sdougm }
52263034Sdougm 
52273034Sdougm 
52283034Sdougm /*
52293034Sdougm  * check_legacy_cmd(proto, cmd)
52303034Sdougm  *
52313034Sdougm  * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is
52323034Sdougm  * executable.
52333034Sdougm  */
52343034Sdougm 
52353034Sdougm static int
52363034Sdougm check_legacy_cmd(char *path)
52373034Sdougm {
52383034Sdougm 	struct stat st;
52393034Sdougm 	int ret = 0;
52403034Sdougm 
52413034Sdougm 	if (stat(path, &st) == 0) {
52424653Sdougm 		if (S_ISREG(st.st_mode) &&
52434653Sdougm 		    st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
52444653Sdougm 			ret = 1;
52453034Sdougm 	}
52463034Sdougm 	return (ret);
52473034Sdougm }
52483034Sdougm 
52493034Sdougm /*
52503034Sdougm  * run_legacy_command(proto, cmd, argv)
52513034Sdougm  *
52524653Sdougm  * We know the command exists, so attempt to execute it with all the
52533034Sdougm  * arguments. This implements full legacy share support for those
52543034Sdougm  * protocols that don't have plugin providers.
52553034Sdougm  */
52563034Sdougm 
52573034Sdougm static int
52583034Sdougm run_legacy_command(char *path, char *argv[])
52593034Sdougm {
52603034Sdougm 	int ret;
52613034Sdougm 
52623034Sdougm 	ret = execv(path, argv);
52633034Sdougm 	if (ret < 0) {
52644653Sdougm 		switch (errno) {
52654653Sdougm 		case EACCES:
52664653Sdougm 			ret = SA_NO_PERMISSION;
52674653Sdougm 			break;
52684653Sdougm 		default:
52694653Sdougm 			ret = SA_SYSTEM_ERR;
52704653Sdougm 			break;
52714653Sdougm 		}
52723034Sdougm 	}
52733034Sdougm 	return (ret);
52743034Sdougm }
52753034Sdougm 
52763034Sdougm /*
52773348Sdougm  * out_share(out, group, proto)
52783034Sdougm  *
52793034Sdougm  * Display the share information in the format that the "share"
52803034Sdougm  * command has traditionally used.
52813034Sdougm  */
52823034Sdougm 
52833034Sdougm static void
52843348Sdougm out_share(FILE *out, sa_group_t group, char *proto)
52853034Sdougm {
52863034Sdougm 	sa_share_t share;
52873034Sdougm 	char resfmt[128];
52885331Samw 	char *defprop;
52895331Samw 
52905331Samw 	/*
52915331Samw 	 * The original share command defaulted to displaying NFS
52925331Samw 	 * shares or allowed a protocol to be specified. We want to
52935331Samw 	 * skip those shares that are not the specified protocol.
52945331Samw 	 */
52955331Samw 	if (proto != NULL && sa_get_optionset(group, proto) == NULL)
52965331Samw 		return;
52975331Samw 
52985331Samw 	if (proto == NULL)
52995331Samw 		proto = "nfs";
53005331Samw 
53015331Samw 	/*
53025331Samw 	 * get the default property string.  NFS uses "rw" but
53035331Samw 	 * everything else will use "".
53045331Samw 	 */
53055331Samw 	if (proto != NULL && strcmp(proto, "nfs") != 0)
53065331Samw 		defprop = "\"\"";
53075331Samw 	else
53085331Samw 		defprop = "rw";
53093034Sdougm 
53104653Sdougm 	for (share = sa_get_share(group, NULL);
53114653Sdougm 	    share != NULL;
53124653Sdougm 	    share = sa_get_next_share(share)) {
53134653Sdougm 		char *path;
53144653Sdougm 		char *type;
53154653Sdougm 		char *resource;
53164653Sdougm 		char *description;
53174653Sdougm 		char *groupname;
53184653Sdougm 		char *sharedstate;
53194653Sdougm 		int shared = 1;
53204653Sdougm 		char *soptions;
53215331Samw 		char shareopts[MAXNAMLEN];
53224653Sdougm 
53234653Sdougm 		sharedstate = sa_get_share_attr(share, "shared");
53244653Sdougm 		path = sa_get_share_attr(share, "path");
53254653Sdougm 		type = sa_get_share_attr(share, "type");
53265331Samw 		resource = get_resource(share);
53274653Sdougm 		groupname = sa_get_group_attr(group, "name");
53284653Sdougm 
53294653Sdougm 		if (groupname != NULL && strcmp(groupname, "default") == 0) {
53304653Sdougm 			sa_free_attr_string(groupname);
53314653Sdougm 			groupname = NULL;
53324653Sdougm 		}
53334653Sdougm 		description = sa_get_share_description(share);
53344653Sdougm 
53355331Samw 		/*
53365331Samw 		 * Want the sharetab version if it exists, defaulting
53375331Samw 		 * to NFS if no protocol specified.
53385331Samw 		 */
53395331Samw 		(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto);
53405331Samw 		soptions = sa_get_share_attr(share, shareopts);
53414653Sdougm 
53424653Sdougm 		if (sharedstate == NULL)
53434653Sdougm 			shared = 0;
53444653Sdougm 
53454653Sdougm 		if (soptions == NULL)
53464653Sdougm 			soptions = sa_proto_legacy_format(proto, share, 1);
53474653Sdougm 
53484653Sdougm 		if (shared) {
53494653Sdougm 			/* only active shares go here */
53504653Sdougm 			(void) snprintf(resfmt, sizeof (resfmt), "%s%s%s",
53514653Sdougm 			    resource != NULL ? resource : "-",
53524653Sdougm 			    groupname != NULL ? "@" : "",
53534653Sdougm 			    groupname != NULL ? groupname : "");
53544653Sdougm 			(void) fprintf(out, "%-14.14s  %s   %s   \"%s\"  \n",
53554653Sdougm 			    resfmt, path,
53564653Sdougm 			    (soptions != NULL && strlen(soptions) > 0) ?
53575331Samw 			    soptions : defprop,
53584653Sdougm 			    (description != NULL) ? description : "");
53594653Sdougm 		}
53604653Sdougm 
53614653Sdougm 		if (path != NULL)
53624653Sdougm 			sa_free_attr_string(path);
53634653Sdougm 		if (type != NULL)
53644653Sdougm 			sa_free_attr_string(type);
53654653Sdougm 		if (resource != NULL)
53664653Sdougm 			sa_free_attr_string(resource);
53674653Sdougm 		if (groupname != NULL)
53684653Sdougm 			sa_free_attr_string(groupname);
53694653Sdougm 		if (description != NULL)
53704653Sdougm 			sa_free_share_description(description);
53714653Sdougm 		if (sharedstate != NULL)
53724653Sdougm 			sa_free_attr_string(sharedstate);
53734653Sdougm 		if (soptions != NULL)
53744653Sdougm 			sa_format_free(soptions);
53753034Sdougm 	}
53763034Sdougm }
53773034Sdougm 
53783034Sdougm /*
53793034Sdougm  * output_legacy_file(out, proto)
53803034Sdougm  *
53813034Sdougm  * Walk all of the groups for the specified protocol and call
53823034Sdougm  * out_share() to format and write in the format displayed by the
53833034Sdougm  * "share" command with no arguments.
53843034Sdougm  */
53853034Sdougm 
53863034Sdougm static void
53873910Sdougm output_legacy_file(FILE *out, char *proto, sa_handle_t handle)
53883034Sdougm {
53893034Sdougm 	sa_group_t group;
53903034Sdougm 
53915331Samw 	for (group = sa_get_group(handle, NULL);
53925331Samw 	    group != NULL;
53934653Sdougm 	    group = sa_get_next_group(group)) {
53944653Sdougm 		char *zfs;
53953034Sdougm 
53963034Sdougm 		/*
53975331Samw 		 * Go through all the groups and ZFS
53985331Samw 		 * sub-groups. out_share() will format the shares in
53995331Samw 		 * the group appropriately.
54003034Sdougm 		 */
54013034Sdougm 
54024653Sdougm 		zfs = sa_get_group_attr(group, "zfs");
54034653Sdougm 		if (zfs != NULL) {
54044653Sdougm 			sa_group_t zgroup;
54054653Sdougm 			sa_free_attr_string(zfs);
54064653Sdougm 			for (zgroup = sa_get_sub_group(group);
54074653Sdougm 			    zgroup != NULL;
54084653Sdougm 			    zgroup = sa_get_next_group(zgroup)) {
54094653Sdougm 
54104653Sdougm 				/* got a group, so display it */
54114653Sdougm 				out_share(out, zgroup, proto);
54124653Sdougm 			}
54134653Sdougm 		} else {
54144653Sdougm 			out_share(out, group, proto);
54153034Sdougm 		}
54163034Sdougm 	}
54173034Sdougm }
54183034Sdougm 
54193034Sdougm int
54203910Sdougm sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[])
54213034Sdougm {
54223034Sdougm 	char *protocol = "nfs";
54233034Sdougm 	char *options = NULL;
54243034Sdougm 	char *description = NULL;
54253034Sdougm 	char *groupname = NULL;
54263034Sdougm 	char *sharepath = NULL;
54273034Sdougm 	char *resource = NULL;
54283034Sdougm 	char *groupstatus = NULL;
54293034Sdougm 	int persist = SA_SHARE_TRANSIENT;
54303034Sdougm 	int argsused = 0;
54313034Sdougm 	int c;
54323034Sdougm 	int ret = SA_OK;
54333034Sdougm 	int zfs = 0;
54343034Sdougm 	int true_legacy = 0;
54353034Sdougm 	int curtype = SA_SHARE_TRANSIENT;
54363034Sdougm 	char cmd[MAXPATHLEN];
54374653Sdougm 	sa_group_t group = NULL;
54385331Samw 	sa_resource_t rsrc = NULL;
54394653Sdougm 	sa_share_t share;
54404653Sdougm 	char dir[MAXPATHLEN];
54415331Samw 	uint64_t features;
54425331Samw #ifdef lint
54435331Samw 	flags = flags;
54445331Samw #endif
54453034Sdougm 
54463034Sdougm 	while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) {
54474653Sdougm 		switch (c) {
54484653Sdougm 		case 'd':
54494653Sdougm 			description = optarg;
54504653Sdougm 			argsused++;
54514653Sdougm 			break;
54524653Sdougm 		case 'F':
54534653Sdougm 			protocol = optarg;
54544653Sdougm 			if (!sa_valid_protocol(protocol)) {
54554653Sdougm 				if (format_legacy_path(cmd, MAXPATHLEN,
54564653Sdougm 				    protocol, "share") == 0 &&
54574653Sdougm 				    check_legacy_cmd(cmd)) {
54584653Sdougm 					true_legacy++;
54594653Sdougm 				} else {
54604653Sdougm 					(void) fprintf(stderr, gettext(
54614653Sdougm 					    "Invalid protocol specified: "
54624653Sdougm 					    "%s\n"), protocol);
54634653Sdougm 					return (SA_INVALID_PROTOCOL);
54644653Sdougm 				}
54654653Sdougm 			}
54664653Sdougm 			break;
54674653Sdougm 		case 'o':
54684653Sdougm 			options = optarg;
54694653Sdougm 			argsused++;
54704653Sdougm 			break;
54714653Sdougm 		case 'p':
54724653Sdougm 			persist = SA_SHARE_PERMANENT;
54734653Sdougm 			argsused++;
54744653Sdougm 			break;
54754653Sdougm 		case 'h':
54766019Sdougm 			/* optopt on valid arg isn't defined */
54776019Sdougm 			optopt = c;
54786019Sdougm 			/*FALLTHROUGH*/
54794653Sdougm 		case '?':
54804653Sdougm 		default:
54816019Sdougm 			/*
54826019Sdougm 			 * Since a bad option gets to here, sort it
54836019Sdougm 			 * out and return a syntax error return value
54846019Sdougm 			 * if necessary.
54856019Sdougm 			 */
54866019Sdougm 			switch (optopt) {
54876019Sdougm 			default:
54886019Sdougm 				ret = SA_LEGACY_ERR;
54896019Sdougm 				break;
54906019Sdougm 			case 'h':
54916019Sdougm 			case '?':
54926019Sdougm 				break;
54936019Sdougm 			}
54944653Sdougm 			(void) fprintf(stderr, gettext("usage: %s\n"),
54954653Sdougm 			    sa_get_usage(USAGE_SHARE));
54966019Sdougm 			return (ret);
54973034Sdougm 		}
54984653Sdougm 	}
54994653Sdougm 
55004653Sdougm 	/* Have the info so construct what is needed */
55014653Sdougm 	if (!argsused && optind == argc) {
55024653Sdougm 		/* display current info in share format */
55035331Samw 		(void) output_legacy_file(stdout, protocol, handle);
55044653Sdougm 		return (ret);
55053034Sdougm 	}
55063034Sdougm 
55074653Sdougm 	/* We are modifying the configuration */
55084653Sdougm 	if (optind == argc) {
55093034Sdougm 		(void) fprintf(stderr, gettext("usage: %s\n"),
55104653Sdougm 		    sa_get_usage(USAGE_SHARE));
55113034Sdougm 		return (SA_LEGACY_ERR);
55124653Sdougm 	}
55134653Sdougm 	if (true_legacy) {
55144653Sdougm 		/* If still using legacy share/unshare, exec it */
55153034Sdougm 		ret = run_legacy_command(cmd, argv);
55163034Sdougm 		return (ret);
55174653Sdougm 	}
55184653Sdougm 
55194653Sdougm 	sharepath = argv[optind++];
55204653Sdougm 	if (optind < argc) {
55213034Sdougm 		resource = argv[optind];
55223034Sdougm 		groupname = strchr(resource, '@');
55233034Sdougm 		if (groupname != NULL)
55244653Sdougm 			*groupname++ = '\0';
55254653Sdougm 	}
55264653Sdougm 	if (realpath(sharepath, dir) == NULL)
55273034Sdougm 		ret = SA_BAD_PATH;
55284653Sdougm 	else
55293034Sdougm 		sharepath = dir;
55304653Sdougm 	if (ret == SA_OK)
55313910Sdougm 		share = sa_find_share(handle, sharepath);
55324653Sdougm 	else
55333034Sdougm 		share = NULL;
55344653Sdougm 
55355331Samw 	features = sa_proto_get_featureset(protocol);
55365331Samw 
55374653Sdougm 	if (groupname != NULL) {
55384653Sdougm 		ret = SA_NOT_ALLOWED;
55394653Sdougm 	} else if (ret == SA_OK) {
55405331Samw 		char *legacygroup;
55413034Sdougm 		/*
55424653Sdougm 		 * The legacy group is always present and zfs groups
55433034Sdougm 		 * come and go.  zfs shares may be in sub-groups and
55443034Sdougm 		 * the zfs share will already be in that group so it
55455331Samw 		 * isn't an error. If the protocol is "smb", the group
55465331Samw 		 * "smb" is used when "default" would otherwise be
55475331Samw 		 * used.  "default" is NFS only and "smb" is SMB only.
55483034Sdougm 		 */
55495331Samw 		if (strcmp(protocol, "smb") == 0)
55505331Samw 			legacygroup = "smb";
55515331Samw 		else
55525331Samw 			legacygroup = "default";
55535331Samw 
55543034Sdougm 		/*
55554653Sdougm 		 * If the share exists (not NULL), then make sure it
55564653Sdougm 		 * is one we want to handle by getting the parent
55574653Sdougm 		 * group.
55583034Sdougm 		 */
55595331Samw 		if (share != NULL) {
55604653Sdougm 			group = sa_get_parent_group(share);
55615331Samw 		} else {
55624653Sdougm 			group = sa_get_group(handle, legacygroup);
55635331Samw 			if (group == NULL && strcmp(legacygroup, "smb") == 0) {
55645331Samw 				/*
55655331Samw 				 * This group may not exist, so create
55665331Samw 				 * as necessary. It only contains the
55675331Samw 				 * "smb" protocol.
55685331Samw 				 */
55695331Samw 				group = sa_create_group(handle, legacygroup,
55705331Samw 				    &ret);
55715331Samw 				if (group != NULL)
55725331Samw 					(void) sa_create_optionset(group,
55735331Samw 					    protocol);
55745331Samw 			}
55755331Samw 		}
55765331Samw 
55775331Samw 		if (group == NULL) {
55785331Samw 			ret = SA_SYSTEM_ERR;
55795331Samw 			goto err;
55805331Samw 		}
55815331Samw 
55825331Samw 		groupstatus = group_status(group);
55835331Samw 		if (share == NULL) {
55845331Samw 			share = sa_add_share(group, sharepath,
55855331Samw 			    persist, &ret);
55865331Samw 			if (share == NULL &&
55875331Samw 			    ret == SA_DUPLICATE_NAME) {
55885331Samw 				/*
55895331Samw 				 * Could be a ZFS path being started
55905331Samw 				 */
55915331Samw 				if (sa_zfs_is_shared(handle,
55925331Samw 				    sharepath)) {
55935331Samw 					ret = SA_OK;
55945331Samw 					group = sa_get_group(handle,
55955331Samw 					    "zfs");
55965331Samw 					if (group == NULL) {
55975331Samw 						/*
55985331Samw 						 * This shouldn't
55995331Samw 						 * happen.
56005331Samw 						 */
56015331Samw 						ret = SA_CONFIG_ERR;
56025331Samw 					} else {
56035331Samw 						share = sa_add_share(
56045331Samw 						    group, sharepath,
56055331Samw 						    persist, &ret);
56064653Sdougm 					}
56073034Sdougm 				}
56085331Samw 			}
56095331Samw 		} else {
56105331Samw 			char *type;
56115331Samw 			/*
56125331Samw 			 * May want to change persist state, but the
56135331Samw 			 * important thing is to change options. We
56145331Samw 			 * need to change them regardless of the
56155331Samw 			 * source.
56165331Samw 			 */
56175331Samw 
56185331Samw 			if (sa_zfs_is_shared(handle, sharepath)) {
56195331Samw 				zfs = 1;
56205331Samw 			}
56215331Samw 			remove_all_options(share, protocol);
56225331Samw 			type = sa_get_share_attr(share, "type");
56235331Samw 			if (type != NULL &&
56245331Samw 			    strcmp(type, "transient") != 0) {
56255331Samw 				curtype = SA_SHARE_PERMANENT;
56265331Samw 			}
56275331Samw 			if (type != NULL)
56285331Samw 				sa_free_attr_string(type);
56295331Samw 			if (curtype != persist) {
56305331Samw 				(void) sa_set_share_attr(share, "type",
56315331Samw 				    persist == SA_SHARE_PERMANENT ?
56325331Samw 				    "persist" : "transient");
56335331Samw 			}
56345331Samw 		}
56355331Samw 
56365331Samw 		/*
56375331Samw 		 * If there is a resource name, we may
56385331Samw 		 * actually care about it if this is share for
56395331Samw 		 * a protocol that uses resource level sharing
56405331Samw 		 * (SMB). We need to find the resource and, if
56415331Samw 		 * it exists, make sure it belongs to the
56425331Samw 		 * current share. If it doesn't exist, attempt
56435331Samw 		 * to create it.
56445331Samw 		 */
56455331Samw 
56465331Samw 		if (ret == SA_OK && resource != NULL) {
56475331Samw 			rsrc = sa_find_resource(handle, resource);
56485331Samw 			if (rsrc != NULL) {
56495331Samw 				if (share != sa_get_resource_parent(rsrc))
56505331Samw 					ret = SA_DUPLICATE_NAME;
56515331Samw 				} else {
56525331Samw 					rsrc = sa_add_resource(share, resource,
56535331Samw 					    persist, &ret);
56543034Sdougm 				}
56555331Samw 				if (features & SA_FEATURE_RESOURCE)
56565331Samw 					share = rsrc;
56573108Sdougm 			}
56585331Samw 
56594653Sdougm 			/* Have a group to hold this share path */
56604653Sdougm 			if (ret == SA_OK && options != NULL &&
56614653Sdougm 			    strlen(options) > 0) {
56624653Sdougm 				ret = sa_parse_legacy_options(share,
56634653Sdougm 				    options,
56644653Sdougm 				    protocol);
56653034Sdougm 			}
56664653Sdougm 			if (!zfs) {
56674653Sdougm 				/*
56685331Samw 				 * ZFS shares never have a description
56695331Samw 				 * and we can't store the values so
56705331Samw 				 * don't try.
56714653Sdougm 				 */
56724653Sdougm 				if (ret == SA_OK && description != NULL)
56734653Sdougm 					ret = sa_set_share_description(share,
56744653Sdougm 					    description);
56753034Sdougm 			}
56765331Samw 			if (ret == SA_OK &&
56775331Samw 			    strcmp(groupstatus, "enabled") == 0) {
56785331Samw 				if (rsrc != share)
56794653Sdougm 					ret = sa_enable_share(share, protocol);
56805331Samw 				else
56815331Samw 					ret = sa_enable_resource(rsrc,
56825331Samw 					    protocol);
56834653Sdougm 				if (ret == SA_OK &&
56844653Sdougm 				    persist == SA_SHARE_PERMANENT) {
56854653Sdougm 					(void) sa_update_legacy(share,
56864653Sdougm 					    protocol);
56874653Sdougm 				}
56884653Sdougm 				if (ret == SA_OK)
56894653Sdougm 					ret = sa_update_config(handle);
56904653Sdougm 			}
56913034Sdougm 	}
56925331Samw err:
56933034Sdougm 	if (ret != SA_OK) {
56944653Sdougm 		(void) fprintf(stderr, gettext("Could not share: %s: %s\n"),
56954653Sdougm 		    sharepath, sa_errorstr(ret));
56964653Sdougm 		ret = SA_LEGACY_ERR;
56973034Sdougm 	}
56983034Sdougm 	return (ret);
56993034Sdougm }
57003034Sdougm 
57013034Sdougm /*
57023034Sdougm  * sa_legacy_unshare(flags, argc, argv)
57033034Sdougm  *
57043034Sdougm  * Implements the original unshare command.
57053034Sdougm  */
57063034Sdougm int
57073910Sdougm sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[])
57083034Sdougm {
57093034Sdougm 	char *protocol = "nfs"; /* for now */
57103034Sdougm 	char *options = NULL;
57113034Sdougm 	char *sharepath = NULL;
57123034Sdougm 	int persist = SA_SHARE_TRANSIENT;
57133034Sdougm 	int argsused = 0;
57143034Sdougm 	int c;
57153034Sdougm 	int ret = SA_OK;
57163034Sdougm 	int true_legacy = 0;
57175331Samw 	uint64_t features = 0;
57185331Samw 	sa_resource_t resource = NULL;
57193034Sdougm 	char cmd[MAXPATHLEN];
57205331Samw #ifdef lint
57215331Samw 	flags = flags;
57225331Samw 	options = options;
57235331Samw #endif
57243034Sdougm 
57253034Sdougm 	while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) {
57264653Sdougm 		switch (c) {
57274653Sdougm 		case 'F':
57284653Sdougm 			protocol = optarg;
57294653Sdougm 			if (!sa_valid_protocol(protocol)) {
57304653Sdougm 				if (format_legacy_path(cmd, MAXPATHLEN,
57314653Sdougm 				    protocol, "unshare") == 0 &&
57324653Sdougm 				    check_legacy_cmd(cmd)) {
57334653Sdougm 					true_legacy++;
57344653Sdougm 				} else {
57354653Sdougm 					(void) printf(gettext(
57364653Sdougm 					    "Invalid file system name\n"));
57374653Sdougm 					return (SA_INVALID_PROTOCOL);
57384653Sdougm 				}
57394653Sdougm 			}
57404653Sdougm 			break;
57414653Sdougm 		case 'o':
57424653Sdougm 			options = optarg;
57434653Sdougm 			argsused++;
57444653Sdougm 			break;
57454653Sdougm 		case 'p':
57464653Sdougm 			persist = SA_SHARE_PERMANENT;
57474653Sdougm 			argsused++;
57484653Sdougm 			break;
57496019Sdougm 		case 'h':
57506019Sdougm 			/* optopt on valid arg isn't defined */
57516019Sdougm 			optopt = c;
57526019Sdougm 			/*FALLTHROUGH*/
57536019Sdougm 		case '?':
57544653Sdougm 		default:
57556019Sdougm 			/*
57566019Sdougm 			 * Since a bad option gets to here, sort it
57576019Sdougm 			 * out and return a syntax error return value
57586019Sdougm 			 * if necessary.
57596019Sdougm 			 */
57606019Sdougm 			switch (optopt) {
57616019Sdougm 			default:
57626019Sdougm 				ret = SA_LEGACY_ERR;
57636019Sdougm 				break;
57646019Sdougm 			case 'h':
57656019Sdougm 			case '?':
57666019Sdougm 				break;
57676019Sdougm 			}
57684653Sdougm 			(void) printf(gettext("usage: %s\n"),
57694653Sdougm 			    sa_get_usage(USAGE_UNSHARE));
57706019Sdougm 			return (ret);
57713034Sdougm 		}
57723034Sdougm 	}
57733034Sdougm 
57744653Sdougm 	/* Have the info so construct what is needed */
57754653Sdougm 	if (optind == argc || (optind + 1) < argc || options != NULL) {
57764653Sdougm 		ret = SA_SYNTAX_ERR;
57773034Sdougm 	} else {
57784653Sdougm 		sa_share_t share;
57794653Sdougm 		char dir[MAXPATHLEN];
57804653Sdougm 		if (true_legacy) {
57814653Sdougm 			/* if still using legacy share/unshare, exec it */
57824653Sdougm 			ret = run_legacy_command(cmd, argv);
57834653Sdougm 			return (ret);
57844653Sdougm 		}
57853663Sdougm 		/*
57863663Sdougm 		 * Find the path in the internal configuration. If it
57873663Sdougm 		 * isn't found, attempt to resolve the path via
57883663Sdougm 		 * realpath() and try again.
57893663Sdougm 		 */
57904653Sdougm 		sharepath = argv[optind++];
57914653Sdougm 		share = sa_find_share(handle, sharepath);
57924653Sdougm 		if (share == NULL) {
57934653Sdougm 			if (realpath(sharepath, dir) == NULL) {
57944653Sdougm 				ret = SA_NO_SUCH_PATH;
57954653Sdougm 			} else {
57964653Sdougm 				share = sa_find_share(handle, dir);
57974653Sdougm 			}
57983663Sdougm 		}
57995331Samw 		if (share == NULL) {
58005331Samw 			/* Could be a resource name so check that next */
58015331Samw 			features = sa_proto_get_featureset(protocol);
58025331Samw 			resource = sa_find_resource(handle, sharepath);
58035331Samw 			if (resource != NULL) {
58045331Samw 				share = sa_get_resource_parent(resource);
58055331Samw 				if (features & SA_FEATURE_RESOURCE)
58065331Samw 					(void) sa_disable_resource(resource,
58075331Samw 					    protocol);
58085331Samw 				if (persist == SA_SHARE_PERMANENT) {
58095331Samw 					ret = sa_remove_resource(resource);
58105331Samw 					if (ret == SA_OK)
58115331Samw 						ret = sa_update_config(handle);
58125331Samw 				}
58135331Samw 				/*
58145331Samw 				 * If we still have a resource on the
58155331Samw 				 * share, we don't disable the share
58165331Samw 				 * itself. IF there aren't anymore, we
58175331Samw 				 * need to remove the share. The
58185331Samw 				 * removal will be done in the next
58195331Samw 				 * section if appropriate.
58205331Samw 				 */
58215331Samw 				resource = sa_get_share_resource(share, NULL);
58225331Samw 				if (resource != NULL)
58235331Samw 					share = NULL;
58245331Samw 			} else if (ret == SA_OK) {
58255331Samw 				/* Didn't find path and no  resource */
58265331Samw 				ret = SA_BAD_PATH;
58275331Samw 			}
58285331Samw 		}
58295331Samw 		if (share != NULL && resource == NULL) {
58304653Sdougm 			ret = sa_disable_share(share, protocol);
58314653Sdougm 			/*
58324653Sdougm 			 * Errors are ok and removal should still occur. The
58334653Sdougm 			 * legacy unshare is more forgiving of errors than the
58344653Sdougm 			 * remove-share subcommand which may need the force
58354653Sdougm 			 * flag set for some error conditions. That is, the
58364653Sdougm 			 * "unshare" command will always unshare if it can
58374653Sdougm 			 * while "remove-share" might require the force option.
58384653Sdougm 			 */
58394653Sdougm 			if (persist == SA_SHARE_PERMANENT) {
58404653Sdougm 				ret = sa_remove_share(share);
58414653Sdougm 				if (ret == SA_OK)
58424653Sdougm 					ret = sa_update_config(handle);
58434653Sdougm 			}
58445331Samw 		} else if (ret == SA_OK && share == NULL && resource == NULL) {
58455331Samw 			/*
58465331Samw 			 * If both share and resource are NULL, then
58475331Samw 			 * share not found. If one or the other was
58485331Samw 			 * found or there was an earlier error, we
58495331Samw 			 * assume it was handled earlier.
58505331Samw 			 */
58514653Sdougm 			ret = SA_NOT_SHARED;
58523663Sdougm 		}
58533034Sdougm 	}
58543034Sdougm 	switch (ret) {
58553034Sdougm 	default:
58564653Sdougm 		(void) printf("%s: %s\n", sharepath, sa_errorstr(ret));
58574653Sdougm 		ret = SA_LEGACY_ERR;
58584653Sdougm 		break;
58593034Sdougm 	case SA_SYNTAX_ERR:
58604653Sdougm 		(void) printf(gettext("usage: %s\n"),
58614653Sdougm 		    sa_get_usage(USAGE_UNSHARE));
58624653Sdougm 		break;
58633034Sdougm 	case SA_OK:
58644653Sdougm 		break;
58653034Sdougm 	}
58663034Sdougm 	return (ret);
58673034Sdougm }
58683034Sdougm 
58693034Sdougm /*
58704653Sdougm  * Common commands that implement the sub-commands used by all
58715331Samw  * protocols. The entries are found via the lookup command
58723034Sdougm  */
58733034Sdougm 
58743034Sdougm static sa_command_t commands[] = {
58753034Sdougm 	{"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET},
58763034Sdougm 	{"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION},
58773034Sdougm 	{"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION},
58783034Sdougm 	{"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION},
58793034Sdougm 	{"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION},
58803034Sdougm 	{"list", 0, sa_list, USAGE_LIST},
58813034Sdougm 	{"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET},
58823034Sdougm 	{"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET},
58833034Sdougm 	{"set", 0, sa_set, USAGE_SET, SVC_SET},
58843034Sdougm 	{"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET},
58853034Sdougm 	{"show", 0, sa_show, USAGE_SHOW},
58863034Sdougm 	{"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION},
58873034Sdougm 	{"start", CMD_NODISPLAY, sa_start_group, USAGE_START,
58885331Samw 	    SVC_SET|SVC_ACTION},
58893034Sdougm 	{"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION},
58903034Sdougm 	{"unset", 0, sa_unset, USAGE_UNSET, SVC_SET},
58913034Sdougm 	{"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION},
58923034Sdougm 	{NULL, 0, NULL, NULL}
58933034Sdougm };
58943034Sdougm 
58953034Sdougm static char *
58963034Sdougm sa_get_usage(sa_usage_t index)
58973034Sdougm {
58983034Sdougm 	char *ret = NULL;
58993034Sdougm 	switch (index) {
59003034Sdougm 	case USAGE_ADD_SHARE:
59014653Sdougm 		ret = gettext("add-share [-nth] [-r resource-name] "
59024653Sdougm 		    "[-d \"description text\"] -s sharepath group");
59034653Sdougm 		break;
59043034Sdougm 	case USAGE_CREATE:
59054653Sdougm 		ret = gettext(
59064653Sdougm 		    "create [-nvh] [-P proto [-p property=value]] group");
59074653Sdougm 		break;
59083034Sdougm 	case USAGE_DELETE:
59094653Sdougm 		ret = gettext("delete [-nvh] [-P proto] [-f] group");
59104653Sdougm 		break;
59113034Sdougm 	case USAGE_DISABLE:
59124653Sdougm 		ret = gettext("disable [-nvh] {-a | group ...}");
59134653Sdougm 		break;
59143034Sdougm 	case USAGE_ENABLE:
59154653Sdougm 		ret = gettext("enable [-nvh] {-a | group ...}");
59164653Sdougm 		break;
59173034Sdougm 	case USAGE_LIST:
59184653Sdougm 		ret = gettext("list [-vh] [-P proto]");
59194653Sdougm 		break;
59203034Sdougm 	case USAGE_MOVE_SHARE:
59214653Sdougm 		ret = gettext(
59224653Sdougm 		    "move-share [-nvh] -s sharepath destination-group");
59234653Sdougm 		break;
59243034Sdougm 	case USAGE_REMOVE_SHARE:
59255331Samw 		ret = gettext(
59265331Samw 		    "remove-share [-fnvh] {-s sharepath | -r resource} "
59275331Samw 		    "group");
59284653Sdougm 		break;
59293034Sdougm 	case USAGE_SET:
59304653Sdougm 		ret = gettext("set [-nvh] -P proto [-S optspace] "
59315331Samw 		    "[-p property=value]* [-s sharepath] [-r resource]] "
59325331Samw 		    "group");
59334653Sdougm 		break;
59343034Sdougm 	case USAGE_SET_SECURITY:
59354653Sdougm 		ret = gettext("set-security [-nvh] -P proto -S security-type "
59364653Sdougm 		    "[-p property=value]* group");
59374653Sdougm 		break;
59383034Sdougm 	case USAGE_SET_SHARE:
59394653Sdougm 		ret = gettext("set-share [-nh] [-r resource] "
59404653Sdougm 		    "[-d \"description text\"] -s sharepath group");
59414653Sdougm 		break;
59423034Sdougm 	case USAGE_SHOW:
59434653Sdougm 		ret = gettext("show [-pvxh] [-P proto] [group ...]");
59444653Sdougm 		break;
59453034Sdougm 	case USAGE_SHARE:
59464653Sdougm 		ret = gettext("share [-F fstype] [-p] [-o optionlist]"
59474653Sdougm 		    "[-d description] [pathname [resourcename]]");
59484653Sdougm 		break;
59493034Sdougm 	case USAGE_START:
59504653Sdougm 		ret = gettext("start [-vh] [-P proto] {-a | group ...}");
59514653Sdougm 		break;
59523034Sdougm 	case USAGE_STOP:
59534653Sdougm 		ret = gettext("stop [-vh] [-P proto] {-a | group ...}");
59544653Sdougm 		break;
59553034Sdougm 	case USAGE_UNSET:
59564653Sdougm 		ret = gettext("unset [-nvh] -P proto [-S optspace] "
59574653Sdougm 		    "[-p property]* group");
59584653Sdougm 		break;
59593034Sdougm 	case USAGE_UNSET_SECURITY:
59605331Samw 		ret = gettext("unset-security [-nvh] -P proto "
59615331Samw 		    "-S security-type [-p property]* group");
59624653Sdougm 		break;
59633034Sdougm 	case USAGE_UNSHARE:
59644653Sdougm 		ret = gettext(
59655331Samw 		    "unshare [-F fstype] [-p] [-o optionlist] sharepath");
59664653Sdougm 		break;
59673034Sdougm 	}
59683034Sdougm 	return (ret);
59693034Sdougm }
59703034Sdougm 
59713034Sdougm /*
59723034Sdougm  * sa_lookup(cmd, proto)
59733034Sdougm  *
59743034Sdougm  * Lookup the sub-command. proto isn't currently used, but it may
59753034Sdougm  * eventually provide a way to provide protocol specific sub-commands.
59763034Sdougm  */
59773034Sdougm sa_command_t *
59783034Sdougm sa_lookup(char *cmd, char *proto)
59793034Sdougm {
59803034Sdougm 	int i;
59813034Sdougm 	size_t len;
59825331Samw #ifdef lint
59835331Samw 	proto = proto;
59845331Samw #endif
59853034Sdougm 
59863034Sdougm 	len = strlen(cmd);
59873034Sdougm 	for (i = 0; commands[i].cmdname != NULL; i++) {
59884653Sdougm 		if (strncmp(cmd, commands[i].cmdname, len) == 0)
59894653Sdougm 			return (&commands[i]);
59903034Sdougm 	}
59913034Sdougm 	return (NULL);
59923034Sdougm }
59933034Sdougm 
59943034Sdougm void
59953034Sdougm sub_command_help(char *proto)
59963034Sdougm {
59973034Sdougm 	int i;
59985331Samw #ifdef lint
59995331Samw 	proto = proto;
60005331Samw #endif
60013034Sdougm 
60023034Sdougm 	(void) printf(gettext("\tsub-commands:\n"));
60033034Sdougm 	for (i = 0; commands[i].cmdname != NULL; i++) {
60044653Sdougm 		if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY)))
60054653Sdougm 			(void) printf("\t%s\n",
60064653Sdougm 			    sa_get_usage((sa_usage_t)commands[i].cmdidx));
60073034Sdougm 	}
60083034Sdougm }
6009