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 #include <sys/types.h>
283034Sdougm #include <sys/stat.h>
293034Sdougm #include <fcntl.h>
303034Sdougm #include <stdlib.h>
313034Sdougm #include <stdio.h>
323034Sdougm #include <string.h>
333034Sdougm #include <ctype.h>
343034Sdougm #include <unistd.h>
353034Sdougm #include <getopt.h>
363034Sdougm #include <utmpx.h>
373034Sdougm #include <pwd.h>
383034Sdougm #include <auth_attr.h>
393034Sdougm #include <secdb.h>
403034Sdougm #include <sys/param.h>
413034Sdougm #include <sys/stat.h>
423034Sdougm #include <errno.h>
433034Sdougm 
443034Sdougm #include <libshare.h>
453034Sdougm #include "sharemgr.h"
463034Sdougm #include <libscf.h>
473034Sdougm #include <libxml/tree.h>
483034Sdougm #include <libintl.h>
495331Samw #include <assert.h>
505331Samw #include <iconv.h>
515331Samw #include <langinfo.h>
525331Samw #include <dirent.h>
533034Sdougm 
543034Sdougm static char *sa_get_usage(sa_usage_t);
553034Sdougm 
563034Sdougm /*
573034Sdougm  * Implementation of the common sub-commands supported by sharemgr.
583034Sdougm  * A number of helper functions are also included.
593034Sdougm  */
603034Sdougm 
613034Sdougm /*
623034Sdougm  * has_protocol(group, proto)
633034Sdougm  *	If the group has an optionset with the specified protocol,
643034Sdougm  *	return true (1) otherwise false (0).
653034Sdougm  */
663034Sdougm static int
673034Sdougm has_protocol(sa_group_t group, char *protocol)
683034Sdougm {
693034Sdougm 	sa_optionset_t optionset;
703034Sdougm 	int result = 0;
713034Sdougm 
723034Sdougm 	optionset = sa_get_optionset(group, protocol);
733034Sdougm 	if (optionset != NULL) {
744653Sdougm 		result++;
753034Sdougm 	}
763034Sdougm 	return (result);
773034Sdougm }
783034Sdougm 
793034Sdougm /*
805331Samw  * validresource(name)
815331Samw  *
825331Samw  * Check that name only has valid characters in it. The current valid
835331Samw  * set are the printable characters but not including:
845331Samw  *	" / \ [ ] : | < > + ; , ? * = \t
855331Samw  * Note that space is included and there is a maximum length.
865331Samw  */
875331Samw static int
885331Samw validresource(const char *name)
895331Samw {
905331Samw 	const char *cp;
915331Samw 	size_t len;
925331Samw 
935331Samw 	if (name == NULL)
945331Samw 		return (B_FALSE);
955331Samw 
965331Samw 	len = strlen(name);
975331Samw 	if (len == 0 || len > SA_MAX_RESOURCE_NAME)
985331Samw 		return (B_FALSE);
995331Samw 
1005331Samw 	if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) {
1015331Samw 		return (B_FALSE);
1025331Samw 	}
1035331Samw 
1045331Samw 	for (cp = name; *cp != '\0'; cp++)
1055331Samw 		if (iscntrl(*cp))
1065331Samw 			return (B_FALSE);
1075331Samw 
1085331Samw 	return (B_TRUE);
1095331Samw }
1105331Samw 
1115331Samw /*
1125331Samw  * conv_to_utf8(input)
1135331Samw  *
1145331Samw  * Convert the input string to utf8 from the current locale.  If the
1155331Samw  * conversion fails, use the current locale, it is likely close
1165331Samw  * enough. For example, the "C" locale is a subset of utf-8. The
1175331Samw  * return value may be a new string or the original input string.
1185331Samw  */
1195331Samw 
1205331Samw static char *
1215331Samw conv_to_utf8(char *input)
1225331Samw {
1235331Samw 	iconv_t cd;
1245521Sas200622 	char *inval = input;
1255331Samw 	char *output = input;
1265331Samw 	char *outleft;
1275331Samw 	char *curlocale;
1285331Samw 	size_t bytesleft;
1295331Samw 	size_t size;
1305331Samw 	size_t osize;
1315331Samw 	static int warned = 0;
1325331Samw 
1335331Samw 	curlocale = nl_langinfo(CODESET);
1345331Samw 	if (curlocale == NULL)
1355331Samw 		curlocale = "C";
1365331Samw 	cd = iconv_open("UTF-8", curlocale);
1375331Samw 	if (cd != NULL && cd != (iconv_t)-1) {
1385331Samw 		size = strlen(input);
1395331Samw 		/* Assume worst case of characters expanding to 4 bytes. */
1405331Samw 		bytesleft = size * 4;
1415331Samw 		output = calloc(bytesleft, 1);
1425331Samw 		if (output != NULL) {
1435331Samw 			outleft = output;
1445521Sas200622 			/* inval can be modified on return */
1455521Sas200622 			osize = iconv(cd, (const char **)&inval, &size,
1465331Samw 			    &outleft, &bytesleft);
1475331Samw 			if (osize == (size_t)-1 || size != 0) {
1485331Samw 				free(output);
1495331Samw 				output = input;
1505331Samw 			}
1515521Sas200622 		} else {
1525521Sas200622 			/* Need to return something. */
1535521Sas200622 			output = input;
1545331Samw 		}
1555331Samw 		(void) iconv_close(cd);
1565331Samw 	} else {
1575331Samw 		if (!warned)
1585331Samw 			(void) fprintf(stderr,
1595331Samw 			    gettext("Cannot convert to UTF-8 from %s\n"),
1605331Samw 			    curlocale ? curlocale : gettext("unknown"));
1615331Samw 		warned = 1;
1625331Samw 	}
1635331Samw 	return (output);
1645331Samw }
1655331Samw 
1665331Samw /*
1675331Samw  * conv_from(input)
1685331Samw  *
1695331Samw  * Convert the input string from utf8 to current locale.  If the
1705331Samw  * conversion isn't supported, just use as is. The return value may be
1715331Samw  * a new string or the original input string.
1725331Samw  */
1735331Samw 
1745331Samw static char *
1755331Samw conv_from_utf8(char *input)
1765331Samw {
1775331Samw 	iconv_t cd;
1785331Samw 	char *output = input;
1795521Sas200622 	char *inval = input;
1805331Samw 	char *outleft;
1815331Samw 	char *curlocale;
1825331Samw 	size_t bytesleft;
1835331Samw 	size_t size;
1845331Samw 	size_t osize;
1855331Samw 	static int warned = 0;
1865331Samw 
1875331Samw 	curlocale = nl_langinfo(CODESET);
1885331Samw 	if (curlocale == NULL)
1895331Samw 		curlocale = "C";
1905331Samw 	cd = iconv_open(curlocale, "UTF-8");
1915331Samw 	if (cd != NULL && cd != (iconv_t)-1) {
1925331Samw 		size = strlen(input);
1935331Samw 		/* Assume worst case of characters expanding to 4 bytes. */
1945331Samw 		bytesleft = size * 4;
1955331Samw 		output = calloc(bytesleft, 1);
1965331Samw 		if (output != NULL) {
1975331Samw 			outleft = output;
1985521Sas200622 			osize = iconv(cd, (const char **)&inval, &size,
1995331Samw 			    &outleft, &bytesleft);
2005521Sas200622 			if (osize == (size_t)-1 || size != 0)
2015331Samw 				output = input;
2025521Sas200622 		} else {
2035521Sas200622 			/* Need to return something. */
2045521Sas200622 			output = input;
2055331Samw 		}
2065331Samw 		(void) iconv_close(cd);
2075331Samw 	} else {
2085331Samw 		if (!warned)
2095331Samw 			(void) fprintf(stderr,
2105331Samw 			    gettext("Cannot convert to %s from UTF-8\n"),
2115331Samw 			    curlocale ? curlocale : gettext("unknown"));
2125331Samw 		warned = 1;
2135331Samw 	}
2145331Samw 	return (output);
2155331Samw }
2165331Samw 
2175885Sdougm /*
2185885Sdougm  * print_rsrc_desc(resource, sharedesc)
2195885Sdougm  *
2205885Sdougm  * Print the resource description string after converting from UTF8 to
2215885Sdougm  * the current locale. If sharedesc is not NULL and there is no
2225885Sdougm  * description on the resource, use sharedesc. sharedesc will already
2235885Sdougm  * be converted to UTF8.
2245885Sdougm  */
2255885Sdougm 
2265331Samw static void
2275885Sdougm print_rsrc_desc(sa_resource_t resource, char *sharedesc)
2285331Samw {
2295331Samw 	char *description;
2305331Samw 	char *desc;
2315331Samw 
2325885Sdougm 	if (resource == NULL)
2335885Sdougm 		return;
2345885Sdougm 
2355331Samw 	description = sa_get_resource_description(resource);
2365331Samw 	if (description != NULL) {
2375331Samw 		desc = conv_from_utf8(description);
2385331Samw 		if (desc != description) {
2395331Samw 			sa_free_share_description(description);
2405331Samw 			description = desc;
2415331Samw 		}
2425885Sdougm 	} else if (sharedesc != NULL) {
2435885Sdougm 		description = strdup(sharedesc);
2445885Sdougm 	}
2455885Sdougm 	if (description != NULL) {
2465331Samw 		(void) printf("\t\"%s\"", description);
2475331Samw 		sa_free_share_description(description);
2485331Samw 	}
2495331Samw }
2505331Samw 
2515885Sdougm /*
2525885Sdougm  * set_resource_desc(share, description)
2535885Sdougm  *
2545885Sdougm  * Set the share description value after converting the description
2555885Sdougm  * string to UTF8 from the current locale.
2565885Sdougm  */
2575885Sdougm 
2585885Sdougm static int
2595885Sdougm set_resource_desc(sa_share_t share, char *description)
2605885Sdougm {
2615885Sdougm 	char *desc;
2625885Sdougm 	int ret;
2635885Sdougm 
2645885Sdougm 	desc = conv_to_utf8(description);
2655885Sdougm 	ret = sa_set_resource_description(share, desc);
2665885Sdougm 	if (description != desc)
2675885Sdougm 		sa_free_share_description(desc);
2685885Sdougm 	return (ret);
2695885Sdougm }
2705885Sdougm 
2715885Sdougm /*
2725885Sdougm  * set_share_desc(share, description)
2735885Sdougm  *
2745885Sdougm  * Set the resource description value after converting the description
2755885Sdougm  * string to UTF8 from the current locale.
2765885Sdougm  */
2775885Sdougm 
2785331Samw static int
2795331Samw set_share_desc(sa_share_t share, char *description)
2805331Samw {
2815331Samw 	char *desc;
2825331Samw 	int ret;
2835331Samw 
2845331Samw 	desc = conv_to_utf8(description);
2855331Samw 	ret = sa_set_share_description(share, desc);
2865331Samw 	if (description != desc)
2875331Samw 		sa_free_share_description(desc);
2885331Samw 	return (ret);
2895331Samw }
2905331Samw 
2915331Samw /*
2925331Samw  * add_list(list, item, data, proto)
2935331Samw  *	Adds a new list member that points holds item in the list.
2943034Sdougm  *	If list is NULL, it starts a new list.  The function returns
2953034Sdougm  *	the first member of the list.
2963034Sdougm  */
2973034Sdougm struct list *
2985331Samw add_list(struct list *listp, void *item, void *data, char *proto)
2993034Sdougm {
3003034Sdougm 	struct list *new, *tmp;
3013034Sdougm 
3023034Sdougm 	new = malloc(sizeof (struct list));
3033034Sdougm 	if (new != NULL) {
3044653Sdougm 		new->next = NULL;
3054653Sdougm 		new->item = item;
3064653Sdougm 		new->itemdata = data;
3075331Samw 		new->proto = proto;
3083034Sdougm 	} else {
3094653Sdougm 		return (listp);
3103034Sdougm 	}
3113034Sdougm 
3123034Sdougm 	if (listp == NULL)
3134653Sdougm 		return (new);
3143034Sdougm 
3153034Sdougm 	for (tmp = listp; tmp->next != NULL; tmp = tmp->next) {
3163034Sdougm 		/* get to end of list */
3173034Sdougm 	}
3183034Sdougm 	tmp->next = new;
3193034Sdougm 	return (listp);
3203034Sdougm }
3213034Sdougm 
3223034Sdougm /*
3233034Sdougm  * free_list(list)
3243034Sdougm  *	Given a list, free all the members of the list;
3253034Sdougm  */
3263034Sdougm static void
3273034Sdougm free_list(struct list *listp)
3283034Sdougm {
3293034Sdougm 	struct list *tmp;
3303034Sdougm 	while (listp != NULL) {
3314653Sdougm 		tmp = listp;
3324653Sdougm 		listp = listp->next;
3334653Sdougm 		free(tmp);
3343034Sdougm 	}
3353034Sdougm }
3363034Sdougm 
3373034Sdougm /*
3383034Sdougm  * check_authorization(instname, which)
3393034Sdougm  *
3403034Sdougm  * Checks to see if the specific type of authorization in which is
3413034Sdougm  * enabled for the user in this SMF service instance.
3423034Sdougm  */
3433034Sdougm 
3443034Sdougm static int
3453034Sdougm check_authorization(char *instname, int which)
3463034Sdougm {
3473034Sdougm 	scf_handle_t *handle = NULL;
3483034Sdougm 	scf_simple_prop_t *prop = NULL;
3493034Sdougm 	char svcstring[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
3503034Sdougm 	char *authstr = NULL;
3513034Sdougm 	ssize_t numauths;
3524653Sdougm 	int ret = B_TRUE;
3533034Sdougm 	uid_t uid;
3543034Sdougm 	struct passwd *pw = NULL;
3553034Sdougm 
3563034Sdougm 	uid = getuid();
3573034Sdougm 	pw = getpwuid(uid);
3584653Sdougm 	if (pw == NULL) {
3594653Sdougm 		ret = B_FALSE;
3604653Sdougm 	} else {
3614653Sdougm 		/*
3624653Sdougm 		 * Since names are restricted to SA_MAX_NAME_LEN won't
3634653Sdougm 		 * overflow.
3644653Sdougm 		 */
3654653Sdougm 		(void) snprintf(svcstring, sizeof (svcstring), "%s:%s",
3664653Sdougm 		    SA_SVC_FMRI_BASE, instname);
3674653Sdougm 		handle = scf_handle_create(SCF_VERSION);
3684653Sdougm 		if (handle != NULL) {
3694653Sdougm 			if (scf_handle_bind(handle) == 0) {
3704653Sdougm 				switch (which) {
3714653Sdougm 				case SVC_SET:
3724653Sdougm 					prop = scf_simple_prop_get(handle,
3734653Sdougm 					    svcstring, "general",
3744653Sdougm 					    SVC_AUTH_VALUE);
3754653Sdougm 					break;
3764653Sdougm 				case SVC_ACTION:
3774653Sdougm 					prop = scf_simple_prop_get(handle,
3784653Sdougm 					    svcstring, "general",
3794653Sdougm 					    SVC_AUTH_ACTION);
3804653Sdougm 					break;
3814653Sdougm 				}
3824653Sdougm 			}
3833034Sdougm 		}
3843034Sdougm 	}
3853034Sdougm 	/* make sure we have an authorization string property */
3863034Sdougm 	if (prop != NULL) {
3874653Sdougm 		int i;
3884653Sdougm 		numauths = scf_simple_prop_numvalues(prop);
3894653Sdougm 		for (ret = 0, i = 0; i < numauths; i++) {
3904653Sdougm 			authstr = scf_simple_prop_next_astring(prop);
3914653Sdougm 			if (authstr != NULL) {
3924653Sdougm 				/* check if this user has one of the strings */
3934653Sdougm 				if (chkauthattr(authstr, pw->pw_name)) {
3944653Sdougm 					ret = 1;
3954653Sdougm 					break;
3964653Sdougm 				}
3974653Sdougm 			}
3983034Sdougm 		}
3994653Sdougm 		endauthattr();
4004653Sdougm 		scf_simple_prop_free(prop);
4013034Sdougm 	} else {
4024653Sdougm 		/* no authorization string defined */
4034653Sdougm 		ret = 0;
4043034Sdougm 	}
4053034Sdougm 	if (handle != NULL)
4064653Sdougm 		scf_handle_destroy(handle);
4073034Sdougm 	return (ret);
4083034Sdougm }
4093034Sdougm 
4103034Sdougm /*
4113034Sdougm  * check_authorizations(instname, flags)
4123034Sdougm  *
4133034Sdougm  * check all the needed authorizations for the user in this service
4143034Sdougm  * instance. Return value of 1(true) or 0(false) indicates whether
4153034Sdougm  * there are authorizations for the user or not.
4163034Sdougm  */
4173034Sdougm 
4183034Sdougm static int
4193034Sdougm check_authorizations(char *instname, int flags)
4203034Sdougm {
4213034Sdougm 	int ret1 = 0;
4223034Sdougm 	int ret2 = 0;
4233034Sdougm 	int ret;
4243034Sdougm 
4253034Sdougm 	if (flags & SVC_SET)
4264653Sdougm 		ret1 = check_authorization(instname, SVC_SET);
4273034Sdougm 	if (flags & SVC_ACTION)
4284653Sdougm 		ret2 = check_authorization(instname, SVC_ACTION);
4293034Sdougm 	switch (flags) {
4303034Sdougm 	case SVC_ACTION:
4314653Sdougm 		ret = ret2;
4324653Sdougm 		break;
4333034Sdougm 	case SVC_SET:
4344653Sdougm 		ret = ret1;
4354653Sdougm 		break;
4363034Sdougm 	case SVC_ACTION|SVC_SET:
4374653Sdougm 		ret = ret1 & ret2;
4384653Sdougm 		break;
4393034Sdougm 	default:
4404653Sdougm 		/* if not flags set, we assume we don't need authorizations */
4414653Sdougm 		ret = 1;
4423034Sdougm 	}
4433034Sdougm 	return (ret);
4443034Sdougm }
4453034Sdougm 
4463034Sdougm /*
4475331Samw  * notify_or_enable_share(share, protocol)
4485331Samw  *
4495331Samw  * Since some protocols don't want an "enable" when properties change,
4505331Samw  * this function will use the protocol specific notify function
4515331Samw  * first. If that fails, it will then attempt to use the
4525331Samw  * sa_enable_share().  "protocol" is the protocol that was specified
4535331Samw  * on the command line.
4545331Samw  */
4555331Samw static void
4565331Samw notify_or_enable_share(sa_share_t share, char *protocol)
4575331Samw {
4585331Samw 	sa_group_t group;
4595331Samw 	sa_optionset_t opt;
4605331Samw 	int ret = SA_OK;
4615331Samw 	char *path;
4625331Samw 	char *groupproto;
4635331Samw 	sa_share_t parent = share;
4645331Samw 
4655331Samw 	/* If really a resource, get parent share */
4665331Samw 	if (!sa_is_share(share)) {
4675331Samw 		parent = sa_get_resource_parent((sa_resource_t)share);
4685331Samw 	}
4695331Samw 
4705331Samw 	/*
4715331Samw 	 * Now that we've got a share in "parent", make sure it has a path.
4725331Samw 	 */
4735331Samw 	path = sa_get_share_attr(parent, "path");
4745331Samw 	if (path == NULL)
4755331Samw 		return;
4765331Samw 
4775331Samw 	group = sa_get_parent_group(parent);
4785331Samw 
4795331Samw 	if (group == NULL) {
4805331Samw 		sa_free_attr_string(path);
4815331Samw 		return;
4825331Samw 	}
4835331Samw 	for (opt = sa_get_optionset(group, NULL);
4845331Samw 	    opt != NULL;
4855331Samw 	    opt = sa_get_next_optionset(opt)) {
4865331Samw 		groupproto = sa_get_optionset_attr(opt, "type");
4875331Samw 		if (groupproto == NULL ||
4885331Samw 		    (protocol != NULL && strcmp(groupproto, protocol) != 0)) {
4895331Samw 			sa_free_attr_string(groupproto);
4905331Samw 			continue;
4915331Samw 		}
4925331Samw 		if (sa_is_share(share)) {
4935331Samw 			if ((ret = sa_proto_change_notify(share,
4945331Samw 			    groupproto)) != SA_OK) {
4955331Samw 				ret = sa_enable_share(share, groupproto);
4965331Samw 				if (ret != SA_OK) {
4975331Samw 					(void) printf(
4985331Samw 					    gettext("Could not reenable"
4995331Samw 					    " share %s: %s\n"),
5005331Samw 					    path, sa_errorstr(ret));
5015331Samw 				}
5025331Samw 			}
5035331Samw 		} else {
5045331Samw 			/* Must be a resource */
5055331Samw 			if ((ret = sa_proto_notify_resource(share,
5065331Samw 			    groupproto)) != SA_OK) {
5075331Samw 				ret = sa_enable_resource(share, groupproto);
5085331Samw 				if (ret != SA_OK) {
5095331Samw 					(void) printf(
5105331Samw 					    gettext("Could not "
5115331Samw 					    "reenable resource %s: "
5125331Samw 					    "%s\n"), path,
5135331Samw 					    sa_errorstr(ret));
5145331Samw 				}
5155331Samw 			}
5165331Samw 		}
5175331Samw 		sa_free_attr_string(groupproto);
5185331Samw 	}
5195331Samw 	sa_free_attr_string(path);
5205331Samw }
5215331Samw 
5225331Samw /*
5235331Samw  * enable_group(group, updateproto, notify, proto)
5243082Sdougm  *
5253082Sdougm  * enable all the shares in the specified group. This is a helper for
5263082Sdougm  * enable_all_groups in order to simplify regular and subgroup (zfs)
5275331Samw  * enabling. Group has already been checked for non-NULL. If notify
5285331Samw  * is non-zero, attempt to use the notify interface rather than
5295331Samw  * enable.
5303082Sdougm  */
5313082Sdougm static void
5325331Samw enable_group(sa_group_t group, char *updateproto, int notify, char *proto)
5333082Sdougm {
5343082Sdougm 	sa_share_t share;
5353082Sdougm 
5363082Sdougm 	for (share = sa_get_share(group, NULL);
5373082Sdougm 	    share != NULL;
5383082Sdougm 	    share = sa_get_next_share(share)) {
5394653Sdougm 		if (updateproto != NULL)
5404653Sdougm 			(void) sa_update_legacy(share, updateproto);
5415331Samw 		if (notify)
5425331Samw 			notify_or_enable_share(share, proto);
5435331Samw 		else
5445331Samw 			(void) sa_enable_share(share, proto);
5453082Sdougm 	}
5463082Sdougm }
5473082Sdougm 
5483082Sdougm /*
5494241Sdougm  * isenabled(group)
5504241Sdougm  *
5514241Sdougm  * Returns B_TRUE if the group is enabled or B_FALSE if it isn't.
5524241Sdougm  * Moved to separate function to reduce clutter in the code.
5534241Sdougm  */
5544241Sdougm 
5554241Sdougm static int
5564241Sdougm isenabled(sa_group_t group)
5574241Sdougm {
5584241Sdougm 	char *state;
5594241Sdougm 	int ret = B_FALSE;
5604241Sdougm 
5614241Sdougm 	if (group != NULL) {
5624653Sdougm 		state = sa_get_group_attr(group, "state");
5634653Sdougm 		if (state != NULL) {
5645331Samw 
5654653Sdougm 			if (strcmp(state, "enabled") == 0)
5664653Sdougm 				ret = B_TRUE;
5674653Sdougm 			sa_free_attr_string(state);
5684653Sdougm 		}
5694241Sdougm 	}
5704241Sdougm 	return (ret);
5714241Sdougm }
5724241Sdougm 
5734241Sdougm /*
5743082Sdougm  * enable_all_groups(list, setstate, online, updateproto)
5755331Samw  *
5765331Samw  * Given a list of groups, enable each one found.  If updateproto is
5775331Samw  * not NULL, then update all the shares for the protocol that was
5785331Samw  * passed in. If enable is non-zero, tell enable_group to try the
5795331Samw  * notify interface since this is a property change.
5803034Sdougm  */
5813034Sdougm static int
5823910Sdougm enable_all_groups(sa_handle_t handle, struct list *work, int setstate,
5835331Samw     int online, char *updateproto, int enable)
5843034Sdougm {
5854241Sdougm 	int ret;
5863034Sdougm 	char instance[SA_MAX_NAME_LEN + sizeof (SA_SVC_FMRI_BASE) + 1];
5873034Sdougm 	char *state;
5883034Sdougm 	char *name;
5893034Sdougm 	char *zfs = NULL;
5903034Sdougm 	sa_group_t group;
5913082Sdougm 	sa_group_t subgroup;
5923034Sdougm 
5934241Sdougm 	for (ret = SA_OK; work != NULL;	work = work->next) {
5944653Sdougm 		group = (sa_group_t)work->item;
5954241Sdougm 
5964241Sdougm 		/*
5974241Sdougm 		 * If setstate == TRUE, then make sure to set
5984241Sdougm 		 * enabled. This needs to be done here in order for
5994241Sdougm 		 * the isenabled check to succeed on a newly enabled
6004241Sdougm 		 * group.
6014241Sdougm 		 */
6024653Sdougm 		if (setstate == B_TRUE) {
6034653Sdougm 			ret = sa_set_group_attr(group, "state",	"enabled");
6044653Sdougm 			if (ret != SA_OK)
6054653Sdougm 				break;
6064653Sdougm 		}
6074241Sdougm 
6084241Sdougm 		/*
6094241Sdougm 		 * Check to see if group is enabled. If it isn't, skip
6104241Sdougm 		 * the rest.  We don't want shares starting if the
6114241Sdougm 		 * group is disabled. The properties may have been
6124241Sdougm 		 * updated, but there won't be a change until the
6134241Sdougm 		 * group is enabled.
6144241Sdougm 		 */
6154653Sdougm 		if (!isenabled(group))
6164653Sdougm 			continue;
6174653Sdougm 
6184653Sdougm 		/* if itemdata != NULL then a single share */
6194653Sdougm 		if (work->itemdata != NULL) {
6205331Samw 			if (enable) {
6215331Samw 				if (work->itemdata != NULL)
6225331Samw 					notify_or_enable_share(work->itemdata,
6235331Samw 					    updateproto);
6245331Samw 				else
6255331Samw 					ret = SA_CONFIG_ERR;
6265331Samw 			} else {
6275331Samw 				if (sa_is_share(work->itemdata)) {
6285331Samw 					ret = sa_enable_share(
6295331Samw 					    (sa_share_t)work->itemdata,
6305331Samw 					    updateproto);
6315331Samw 				} else {
6325331Samw 					ret = sa_enable_resource(
6335331Samw 					    (sa_resource_t)work->itemdata,
6345331Samw 					    updateproto);
6355331Samw 				}
6365331Samw 			}
6373034Sdougm 		}
6384653Sdougm 		if (ret != SA_OK)
6394653Sdougm 			break;
6404653Sdougm 
6414653Sdougm 		/* if itemdata == NULL then the whole group */
6424653Sdougm 		if (work->itemdata == NULL) {
6434653Sdougm 			zfs = sa_get_group_attr(group, "zfs");
6444653Sdougm 			/*
6455331Samw 			 * If the share is managed by ZFS, don't
6464653Sdougm 			 * update any of the protocols since ZFS is
6475331Samw 			 * handling this.  Updateproto will contain
6484653Sdougm 			 * the name of the protocol that we want to
6494653Sdougm 			 * update legacy files for.
6504653Sdougm 			 */
6515331Samw 			enable_group(group, zfs == NULL ? updateproto : NULL,
6525331Samw 			    enable, work->proto);
6534653Sdougm 			for (subgroup = sa_get_sub_group(group);
6544653Sdougm 			    subgroup != NULL;
6554653Sdougm 			    subgroup = sa_get_next_group(subgroup)) {
6564653Sdougm 				/* never update legacy for ZFS subgroups */
6575331Samw 				enable_group(subgroup, NULL, enable,
6585331Samw 				    work->proto);
6593034Sdougm 			}
6603034Sdougm 		}
6614653Sdougm 		if (online) {
6624653Sdougm 			zfs = sa_get_group_attr(group, "zfs");
6634653Sdougm 			name = sa_get_group_attr(group, "name");
6644653Sdougm 			if (name != NULL) {
6654653Sdougm 				if (zfs == NULL) {
6664653Sdougm 					(void) snprintf(instance,
6674653Sdougm 					    sizeof (instance), "%s:%s",
6684653Sdougm 					    SA_SVC_FMRI_BASE, name);
6694653Sdougm 					state = smf_get_state(instance);
6704653Sdougm 					if (state == NULL ||
6714653Sdougm 					    strcmp(state, "online") != 0) {
6724653Sdougm 						(void) smf_enable_instance(
6734653Sdougm 						    instance, 0);
6744653Sdougm 						free(state);
6754653Sdougm 					}
6764653Sdougm 				} else {
6774653Sdougm 					sa_free_attr_string(zfs);
6784653Sdougm 					zfs = NULL;
6794653Sdougm 				}
6804653Sdougm 				if (name != NULL)
6814653Sdougm 					sa_free_attr_string(name);
6824653Sdougm 			}
6834653Sdougm 		}
6843034Sdougm 	}
6853034Sdougm 	if (ret == SA_OK) {
6864653Sdougm 		ret = sa_update_config(handle);
6873034Sdougm 	}
6883034Sdougm 	return (ret);
6893034Sdougm }
6903034Sdougm 
6913034Sdougm /*
6923034Sdougm  * chk_opt(optlistp, security, proto)
6933034Sdougm  *
6943034Sdougm  * Do a sanity check on the optlist provided for the protocol.  This
6953034Sdougm  * is a syntax check and verification that the property is either a
6963034Sdougm  * general or specific to a names optionset.
6973034Sdougm  */
6983034Sdougm 
6993034Sdougm static int
7003034Sdougm chk_opt(struct options *optlistp, int security, char *proto)
7013034Sdougm {
7023034Sdougm 	struct options *optlist;
7033034Sdougm 	char *sep = "";
7043034Sdougm 	int notfirst = 0;
7053034Sdougm 	int ret;
7063034Sdougm 
7073034Sdougm 	for (optlist = optlistp; optlist != NULL; optlist = optlist->next) {
7084653Sdougm 		char *optname;
7094653Sdougm 
7104653Sdougm 		optname = optlist->optname;
7114653Sdougm 		ret = OPT_ADD_OK;
7124653Sdougm 		/* extract property/value pair */
7134653Sdougm 		if (sa_is_security(optname, proto)) {
7144653Sdougm 			if (!security)
7154653Sdougm 				ret = OPT_ADD_SECURITY;
7164653Sdougm 		} else {
7174653Sdougm 			if (security)
7184653Sdougm 				ret = OPT_ADD_PROPERTY;
7194653Sdougm 		}
7204653Sdougm 		if (ret != OPT_ADD_OK) {
7214653Sdougm 			if (notfirst == 0)
7224653Sdougm 				(void) printf(
7234653Sdougm 				    gettext("Property syntax error: "));
7244653Sdougm 			switch (ret) {
7254653Sdougm 			case OPT_ADD_SYNTAX:
7264653Sdougm 				(void) printf(gettext("%ssyntax error: %s"),
7273034Sdougm 				    sep, optname);
7284653Sdougm 				sep = ", ";
7294653Sdougm 				break;
7304653Sdougm 			case OPT_ADD_SECURITY:
7314653Sdougm 				(void) printf(gettext("%s%s requires -S"),
7323034Sdougm 				    optname, sep);
7334653Sdougm 				sep = ", ";
7344653Sdougm 				break;
7354653Sdougm 			case OPT_ADD_PROPERTY:
7364653Sdougm 				(void) printf(
7374653Sdougm 				    gettext("%s%s not supported with -S"),
7383034Sdougm 				    optname, sep);
7394653Sdougm 				sep = ", ";
7404653Sdougm 				break;
7414653Sdougm 			}
7424653Sdougm 			notfirst++;
7433034Sdougm 		}
7443034Sdougm 	}
7453034Sdougm 	if (notfirst) {
7464653Sdougm 		(void) printf("\n");
7474653Sdougm 		ret = SA_SYNTAX_ERR;
7483034Sdougm 	}
7493034Sdougm 	return (ret);
7503034Sdougm }
7513034Sdougm 
7523034Sdougm /*
7533034Sdougm  * free_opt(optlist)
7543034Sdougm  *	Free the specified option list.
7553034Sdougm  */
7563034Sdougm static void
7573034Sdougm free_opt(struct options *optlist)
7583034Sdougm {
7593034Sdougm 	struct options *nextopt;
7603034Sdougm 	while (optlist != NULL) {
7613034Sdougm 		nextopt = optlist->next;
7623034Sdougm 		free(optlist);
7633034Sdougm 		optlist = nextopt;
7643034Sdougm 	}
7653034Sdougm }
7663034Sdougm 
7673034Sdougm /*
7683034Sdougm  * check property list for valid properties
7693034Sdougm  * A null value is a remove which is always valid.
7703034Sdougm  */
7713034Sdougm static int
7726214Sdougm valid_options(sa_handle_t handle, struct options *optlist, char *proto,
7736214Sdougm     void *object, char *sec)
7743034Sdougm {
7753034Sdougm 	int ret = SA_OK;
7763034Sdougm 	struct options *cur;
7773034Sdougm 	sa_property_t prop;
7783034Sdougm 	sa_optionset_t parent = NULL;
7793034Sdougm 
7803034Sdougm 	if (object != NULL) {
7814653Sdougm 		if (sec == NULL)
7824653Sdougm 			parent = sa_get_optionset(object, proto);
7834653Sdougm 		else
7844653Sdougm 			parent = sa_get_security(object, sec, proto);
7853034Sdougm 	}
7863034Sdougm 
7873034Sdougm 	for (cur = optlist; cur != NULL; cur = cur->next) {
7884653Sdougm 		if (cur->optvalue == NULL)
7894653Sdougm 			continue;
7903034Sdougm 		prop = sa_create_property(cur->optname, cur->optvalue);
7913034Sdougm 		if (prop == NULL)
7924653Sdougm 			ret = SA_NO_MEMORY;
7933034Sdougm 		if (ret != SA_OK ||
7946214Sdougm 		    (ret = sa_valid_property(handle, parent, proto, prop)) !=
7956214Sdougm 		    SA_OK) {
7964653Sdougm 			(void) printf(
7974653Sdougm 			    gettext("Could not add property %s: %s\n"),
7984653Sdougm 			    cur->optname, sa_errorstr(ret));
7993034Sdougm 		}
8003034Sdougm 		(void) sa_remove_property(prop);
8013034Sdougm 	}
8023034Sdougm 	return (ret);
8033034Sdougm }
8043034Sdougm 
8053034Sdougm /*
8063034Sdougm  * add_optionset(group, optlist, protocol, *err)
8073034Sdougm  *	Add the options in optlist to an optionset and then add the optionset
8083034Sdougm  *	to the group.
8093034Sdougm  *
8103034Sdougm  *	The return value indicates if there was a "change" while errors are
8113034Sdougm  *	returned via the *err parameters.
8123034Sdougm  */
8133034Sdougm static int
8143034Sdougm add_optionset(sa_group_t group, struct options *optlist, char *proto, int *err)
8153034Sdougm {
8163034Sdougm 	sa_optionset_t optionset;
8173034Sdougm 	int ret = SA_OK;
8185331Samw 	int result = B_FALSE;
8196214Sdougm 	sa_handle_t handle;
8203034Sdougm 
8213034Sdougm 	optionset = sa_get_optionset(group, proto);
8223034Sdougm 	if (optionset == NULL) {
8234653Sdougm 		optionset = sa_create_optionset(group, proto);
8245331Samw 		if (optionset == NULL)
8255331Samw 			ret = SA_NO_MEMORY;
8265331Samw 		result = B_TRUE; /* adding a protocol is a change */
8273034Sdougm 	}
8284653Sdougm 	if (optionset == NULL) {
8294653Sdougm 		ret = SA_NO_MEMORY;
8304653Sdougm 		goto out;
8314653Sdougm 	}
8326214Sdougm 	handle = sa_find_group_handle(group);
8336214Sdougm 	if (handle == NULL) {
8346214Sdougm 		ret = SA_CONFIG_ERR;
8356214Sdougm 		goto out;
8366214Sdougm 	}
8374653Sdougm 	while (optlist != NULL) {
8383034Sdougm 		sa_property_t prop;
8393034Sdougm 		prop = sa_get_property(optionset, optlist->optname);
8403034Sdougm 		if (prop == NULL) {
8413034Sdougm 			/*
8423034Sdougm 			 * add the property, but only if it is
8433034Sdougm 			 * a non-NULL or non-zero length value
8443034Sdougm 			 */
8454653Sdougm 			if (optlist->optvalue != NULL) {
8464653Sdougm 				prop = sa_create_property(optlist->optname,
8474653Sdougm 				    optlist->optvalue);
8484653Sdougm 				if (prop != NULL) {
8496214Sdougm 					ret = sa_valid_property(handle,
8506214Sdougm 					    optionset, proto, prop);
8514653Sdougm 					if (ret != SA_OK) {
8524653Sdougm 						(void) sa_remove_property(prop);
8534653Sdougm 						(void) printf(gettext("Could "
8544653Sdougm 						    "not add property "
8554653Sdougm 						    "%s: %s\n"),
8564653Sdougm 						    optlist->optname,
8574653Sdougm 						    sa_errorstr(ret));
8584653Sdougm 					}
8594653Sdougm 				}
8604653Sdougm 				if (ret == SA_OK) {
8614653Sdougm 					ret = sa_add_property(optionset, prop);
8624653Sdougm 					if (ret != SA_OK) {
8634653Sdougm 						(void) printf(gettext(
8644653Sdougm 						    "Could not add property "
8654653Sdougm 						    "%s: %s\n"),
8664653Sdougm 						    optlist->optname,
8674653Sdougm 						    sa_errorstr(ret));
8684653Sdougm 					} else {
8694653Sdougm 						/* there was a change */
8705331Samw 						result = B_TRUE;
8714653Sdougm 					}
8724653Sdougm 				}
8733034Sdougm 			}
8744653Sdougm 		} else {
8754653Sdougm 			ret = sa_update_property(prop, optlist->optvalue);
8764653Sdougm 			/* should check to see if value changed */
8774653Sdougm 			if (ret != SA_OK) {
8784653Sdougm 				(void) printf(gettext("Could not update "
8794653Sdougm 				    "property %s: %s\n"), optlist->optname,
8804653Sdougm 				    sa_errorstr(ret));
8814653Sdougm 			} else {
8825331Samw 				result = B_TRUE;
8833034Sdougm 			}
8843034Sdougm 		}
8853034Sdougm 		optlist = optlist->next;
8863034Sdougm 	}
8874653Sdougm 	ret = sa_commit_properties(optionset, 0);
8884653Sdougm 
8894653Sdougm out:
8903034Sdougm 	if (err != NULL)
8914653Sdougm 		*err = ret;
8923034Sdougm 	return (result);
8933034Sdougm }
8943034Sdougm 
8953034Sdougm /*
8965331Samw  * resource_compliant(group)
8975331Samw  *
8985331Samw  * Go through all the shares in the group. Assume compliant, but if
8995331Samw  * any share doesn't have at least one resource name, it isn't
9005331Samw  * compliant.
9015331Samw  */
9025331Samw static int
9035331Samw resource_compliant(sa_group_t group)
9045331Samw {
9055331Samw 	sa_share_t share;
9065331Samw 
9075331Samw 	for (share = sa_get_share(group, NULL); share != NULL;
9085331Samw 	    share = sa_get_next_share(share)) {
9095331Samw 		if (sa_get_share_resource(share, NULL) == NULL) {
9105331Samw 			return (B_FALSE);
9115331Samw 		}
9125331Samw 	}
9135331Samw 	return (B_TRUE);
9145331Samw }
9155331Samw 
9165331Samw /*
9175331Samw  * fix_path(path)
9185331Samw  *
9195331Samw  * change all illegal characters to something else.  For now, all get
9205331Samw  * converted to '_' and the leading '/' is stripped off. This is used
9215331Samw  * to construct an resource name (SMB share name) that is valid.
9225331Samw  * Caller must pass a valid path.
9235331Samw  */
9245331Samw static void
9255331Samw fix_path(char *path)
9265331Samw {
9275331Samw 	char *cp;
9285331Samw 	size_t len;
9295331Samw 
9305331Samw 	assert(path != NULL);
9315331Samw 
9325331Samw 	/* make sure we are appropriate length */
9335331Samw 	cp = path + 1; /* skip leading slash */
9345331Samw 	while (cp != NULL && strlen(cp) > SA_MAX_RESOURCE_NAME) {
9355331Samw 		cp = strchr(cp, '/');
9365331Samw 		if (cp != NULL)
9375331Samw 			cp++;
9385331Samw 	}
9395331Samw 	/* two cases - cp == NULL and cp is substring of path */
9405331Samw 	if (cp == NULL) {
9415331Samw 		/* just take last SA_MAX_RESOURCE_NAME chars */
9425331Samw 		len = 1 + strlen(path) - SA_MAX_RESOURCE_NAME;
9435331Samw 		(void) memmove(path, path + len, SA_MAX_RESOURCE_NAME);
9445331Samw 		path[SA_MAX_RESOURCE_NAME] = '\0';
9455331Samw 	} else {
9465331Samw 		len = strlen(cp) + 1;
9475331Samw 		(void) memmove(path, cp, len);
9485331Samw 	}
9495331Samw 
9505331Samw 	/*
9515331Samw 	 * Don't want any of the characters that are not allowed
9525331Samw 	 * in and SMB share name. Replace them with '_'.
9535331Samw 	 */
9545331Samw 	while (*path) {
9555331Samw 		switch (*path) {
9565331Samw 		case '/':
9575331Samw 		case '"':
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 '\t':
9725331Samw 			*path = '_';
9735331Samw 			break;
9745331Samw 		}
9755331Samw 		path++;
9765331Samw 	}
9775331Samw }
9785331Samw 
9795331Samw /*
9805331Samw  * name_adjust(path, count)
9815331Samw  *
9825331Samw  * Add a ~<count> in place of last few characters. The total number of
9835331Samw  * characters is dependent on count.
9845331Samw  */
9855331Samw #define	MAX_MANGLE_NUMBER	10000
9865331Samw 
9875331Samw static int
9885331Samw name_adjust(char *path, int count)
9895331Samw {
9905331Samw 	size_t len;
9915331Samw 
9925331Samw 	len = strlen(path) - 2;
9935331Samw 	if (count > 10)
9945331Samw 		len--;
9955331Samw 	if (count > 100)
9965331Samw 		len--;
9975331Samw 	if (count > 1000)
9985331Samw 		len--;
9995331Samw 	if (len > 0)
10005331Samw 		(void) sprintf(path + len, "~%d", count);
10015331Samw 	else
10025331Samw 		return (SA_BAD_VALUE);
10035331Samw 
10045331Samw 	return (SA_OK);
10055331Samw }
10065331Samw 
10075331Samw /*
10085331Samw  * make_resources(group)
10095331Samw  *
10105331Samw  * Go through all the shares in the group and make them have resource
10115331Samw  * names.
10125331Samw  */
10135331Samw static void
10145331Samw make_resources(sa_group_t group)
10155331Samw {
10165331Samw 	sa_share_t share;
10175331Samw 	int count;
10185331Samw 	int err = SA_OK;
10195331Samw 
10205331Samw 	for (share = sa_get_share(group, NULL); share != NULL;
10215331Samw 	    share = sa_get_next_share(share)) {
10225331Samw 		/* Skip those with resources */
10235331Samw 		if (sa_get_share_resource(share, NULL) == NULL) {
10245331Samw 			char *path;
10255331Samw 			path = sa_get_share_attr(share, "path");
10265331Samw 			if (path == NULL)
10275331Samw 				continue;
10285331Samw 			fix_path(path);
10295331Samw 			count = 0;	/* reset for next resource */
10305331Samw 			while (sa_add_resource(share, path,
10315331Samw 			    SA_SHARE_PERMANENT, &err) == NULL &&
10325331Samw 			    err == SA_DUPLICATE_NAME) {
10335331Samw 				int ret;
10345331Samw 				ret = name_adjust(path, count);
10355331Samw 				count++;
10365331Samw 				if (ret != SA_OK ||
10375331Samw 				    count >= MAX_MANGLE_NUMBER) {
10385331Samw 					(void) printf(gettext(
10395331Samw 					    "Cannot create resource name for"
10405331Samw 					    " path: %s\n"), path);
10415331Samw 					break;
10425331Samw 				}
10435331Samw 			}
10445331Samw 			sa_free_attr_string(path);
10455331Samw 		}
10465331Samw 	}
10475331Samw }
10485331Samw 
10495331Samw /*
10506088Sdougm  * check_valid_group(group, protocol)
10516088Sdougm  *
10526088Sdougm  * Check to see that the group should have the protocol added (if
10536088Sdougm  * there is one specified).
10546088Sdougm  */
10556088Sdougm 
10566088Sdougm static int
10576088Sdougm check_valid_group(sa_group_t group, char *groupname, char *protocol)
10586088Sdougm {
10596088Sdougm 
10606088Sdougm 	if (protocol != NULL) {
10616088Sdougm 		if (has_protocol(group, protocol)) {
10626088Sdougm 			(void) printf(gettext(
10636088Sdougm 			    "Group \"%s\" already exists"
10646088Sdougm 			    " with protocol %s\n"), groupname,
10656088Sdougm 			    protocol);
10666088Sdougm 			return (SA_DUPLICATE_NAME);
10676088Sdougm 		} else if (strcmp(groupname, "default") == 0 &&
10686088Sdougm 		    strcmp(protocol, "nfs") != 0) {
10696088Sdougm 			(void) printf(gettext(
10706088Sdougm 			    "Group \"%s\" only allows protocol "
10716088Sdougm 			    "\"%s\"\n"), groupname, "nfs");
10726088Sdougm 			return (SA_INVALID_PROTOCOL);
10736088Sdougm 		}
10746088Sdougm 	} else {
10756088Sdougm 		/* must add new protocol */
10766088Sdougm 		(void) printf(gettext(
10776088Sdougm 		    "Group already exists and no protocol "
10786088Sdougm 		    "specified.\n"));
10796088Sdougm 		return (SA_DUPLICATE_NAME);
10806088Sdougm 	}
10816088Sdougm 	return (SA_OK);
10826088Sdougm }
10836088Sdougm 
10846088Sdougm /*
10856088Sdougm  * enforce_featureset(group, protocol, dryrun, force)
10866088Sdougm  *
10876088Sdougm  * Check the protocol featureset against the group and enforce any
10886088Sdougm  * rules that might be imposed.
10896088Sdougm  */
10906088Sdougm 
10916088Sdougm static int
10926088Sdougm enforce_featureset(sa_group_t group, char *protocol, boolean_t dryrun,
10936088Sdougm     boolean_t force)
10946088Sdougm {
10956088Sdougm 	uint64_t features;
10966088Sdougm 
10976088Sdougm 	if (protocol == NULL)
10986088Sdougm 		return (SA_OK);
10996088Sdougm 
11006088Sdougm 	/*
11016088Sdougm 	 * First check to see if specified protocol is one we want to
11026088Sdougm 	 * allow on a group. Only server protocols are allowed here.
11036088Sdougm 	 */
11046088Sdougm 	features = sa_proto_get_featureset(protocol);
11056088Sdougm 	if (!(features & SA_FEATURE_SERVER)) {
11066088Sdougm 		(void) printf(
11076088Sdougm 		    gettext("Protocol \"%s\" not supported.\n"), protocol);
11086088Sdougm 		return (SA_INVALID_PROTOCOL);
11096088Sdougm 	}
11106088Sdougm 
11116088Sdougm 	/*
11126088Sdougm 	 * Check to see if the new protocol is one that requires
11136088Sdougm 	 * resource names and make sure we are compliant before
11146088Sdougm 	 * proceeding.
11156088Sdougm 	 */
11166088Sdougm 	if ((features & SA_FEATURE_RESOURCE) &&
11176088Sdougm 	    !resource_compliant(group)) {
11186088Sdougm 		if (force && !dryrun) {
11196088Sdougm 			make_resources(group);
11206088Sdougm 		} else {
11216088Sdougm 			(void) printf(
11226088Sdougm 			    gettext("Protocol requires resource names to be "
11236088Sdougm 			    "set: %s\n"), protocol);
11246088Sdougm 			return (SA_RESOURCE_REQUIRED);
11256088Sdougm 		}
11266088Sdougm 	}
11276088Sdougm 	return (SA_OK);
11286088Sdougm }
11296088Sdougm 
11306088Sdougm /*
11316088Sdougm  * set_all_protocols(group)
11326088Sdougm  *
11336088Sdougm  * Get the list of all protocols and add all server protocols to the
11346088Sdougm  * group.
11356088Sdougm  */
11366088Sdougm 
11376088Sdougm static int
11386088Sdougm set_all_protocols(sa_group_t group)
11396088Sdougm {
11406088Sdougm 	char **protolist;
11416088Sdougm 	int numprotos, i;
11426088Sdougm 	uint64_t features;
11436088Sdougm 	sa_optionset_t optionset;
11446088Sdougm 	int ret = SA_OK;
11456088Sdougm 
11466088Sdougm 	/*
11476088Sdougm 	 * Now make sure we really want to put this protocol on a
11486088Sdougm 	 * group. Only server protocols can go here.
11496088Sdougm 	 */
11506088Sdougm 	numprotos = sa_get_protocols(&protolist);
11516088Sdougm 	for (i = 0; i < numprotos; i++) {
11526088Sdougm 		features = sa_proto_get_featureset(protolist[i]);
11536088Sdougm 		if (features & SA_FEATURE_SERVER) {
11546088Sdougm 			optionset = sa_create_optionset(group, protolist[i]);
11556088Sdougm 			if (optionset == NULL) {
11566088Sdougm 				ret = SA_NO_MEMORY;
11576088Sdougm 				break;
11586088Sdougm 			}
11596088Sdougm 		}
11606088Sdougm 	}
11616088Sdougm 
11626088Sdougm 	if (protolist != NULL)
11636088Sdougm 		free(protolist);
11646088Sdougm 
11656088Sdougm 	return (ret);
11666088Sdougm }
11676088Sdougm 
11686088Sdougm /*
11693034Sdougm  * sa_create(flags, argc, argv)
11703034Sdougm  *	create a new group
11713034Sdougm  *	this may or may not have a protocol associated with it.
11723034Sdougm  *	No protocol means "all" protocols in this case.
11733034Sdougm  */
11743034Sdougm static int
11753910Sdougm sa_create(sa_handle_t handle, int flags, int argc, char *argv[])
11763034Sdougm {
11773034Sdougm 	char *groupname;
11783034Sdougm 
11793034Sdougm 	sa_group_t group;
11806088Sdougm 	boolean_t force = B_FALSE;
11816088Sdougm 	boolean_t verbose = B_FALSE;
11826088Sdougm 	boolean_t dryrun = B_FALSE;
11833034Sdougm 	int c;
11843034Sdougm 	char *protocol = NULL;
11853034Sdougm 	int ret = SA_OK;
11863034Sdougm 	struct options *optlist = NULL;
11876019Sdougm 	int err = SA_OK;
11883034Sdougm 	int auth;
11896088Sdougm 	boolean_t created = B_FALSE;
11903034Sdougm 
11915331Samw 	while ((c = getopt(argc, argv, "?fhvnP:p:")) != EOF) {
11924653Sdougm 		switch (c) {
11935331Samw 		case 'f':
11946088Sdougm 			force = B_TRUE;
11955331Samw 			break;
11964653Sdougm 		case 'v':
11976088Sdougm 			verbose = B_TRUE;
11984653Sdougm 			break;
11994653Sdougm 		case 'n':
12006088Sdougm 			dryrun = B_TRUE;
12014653Sdougm 			break;
12024653Sdougm 		case 'P':
12035331Samw 			if (protocol != NULL) {
12045331Samw 				(void) printf(gettext("Specifying "
12055331Samw 				    "multiple protocols "
12065331Samw 				    "not supported: %s\n"), protocol);
12075331Samw 				return (SA_SYNTAX_ERR);
12085331Samw 			}
12094653Sdougm 			protocol = optarg;
12104653Sdougm 			if (sa_valid_protocol(protocol))
12114653Sdougm 				break;
12124653Sdougm 			(void) printf(gettext(
12134653Sdougm 			    "Invalid protocol specified: %s\n"), protocol);
12144653Sdougm 			return (SA_INVALID_PROTOCOL);
12154653Sdougm 			break;
12164653Sdougm 		case 'p':
12174653Sdougm 			ret = add_opt(&optlist, optarg, 0);
12184653Sdougm 			switch (ret) {
12194653Sdougm 			case OPT_ADD_SYNTAX:
12204653Sdougm 				(void) printf(gettext(
12214653Sdougm 				    "Property syntax error for property: %s\n"),
12224653Sdougm 				    optarg);
12234653Sdougm 				return (SA_SYNTAX_ERR);
12244653Sdougm 			case OPT_ADD_SECURITY:
12254653Sdougm 				(void) printf(gettext(
12264653Sdougm 				    "Security properties need "
12274653Sdougm 				    "to be set with set-security: %s\n"),
12284653Sdougm 				    optarg);
12294653Sdougm 				return (SA_SYNTAX_ERR);
12304653Sdougm 			default:
12314653Sdougm 				break;
12324653Sdougm 			}
12334653Sdougm 			break;
12346019Sdougm 		case 'h':
12356019Sdougm 			/* optopt on valid arg isn't defined */
12366019Sdougm 			optopt = c;
12376019Sdougm 			/*FALLTHROUGH*/
12386019Sdougm 		case '?':
12394653Sdougm 		default:
12406019Sdougm 			/*
12416019Sdougm 			 * Since a bad option gets to here, sort it
12426019Sdougm 			 * out and return a syntax error return value
12436019Sdougm 			 * if necessary.
12446019Sdougm 			 */
12456019Sdougm 			switch (optopt) {
12466019Sdougm 			default:
12476019Sdougm 				err = SA_SYNTAX_ERR;
12486019Sdougm 				break;
12496019Sdougm 			case 'h':
12506019Sdougm 			case '?':
12516019Sdougm 				break;
12526019Sdougm 			}
12534653Sdougm 			(void) printf(gettext("usage: %s\n"),
12544653Sdougm 			    sa_get_usage(USAGE_CREATE));
12556019Sdougm 			return (err);
12563034Sdougm 		}
12573034Sdougm 	}
12583034Sdougm 
12593034Sdougm 	if (optind >= argc) {
12604653Sdougm 		(void) printf(gettext("usage: %s\n"),
12614653Sdougm 		    sa_get_usage(USAGE_CREATE));
12624653Sdougm 		(void) printf(gettext("\tgroup must be specified.\n"));
12634653Sdougm 		return (SA_BAD_PATH);
12643034Sdougm 	}
12653034Sdougm 
12663034Sdougm 	if ((optind + 1) < argc) {
12674653Sdougm 		(void) printf(gettext("usage: %s\n"),
12684653Sdougm 		    sa_get_usage(USAGE_CREATE));
12694653Sdougm 		(void) printf(gettext("\textraneous group(s) at end\n"));
12704653Sdougm 		return (SA_SYNTAX_ERR);
12713034Sdougm 	}
12723034Sdougm 
12733034Sdougm 	if (protocol == NULL && optlist != NULL) {
12744653Sdougm 		/* lookup default protocol */
12754653Sdougm 		(void) printf(gettext("usage: %s\n"),
12764653Sdougm 		    sa_get_usage(USAGE_CREATE));
12774653Sdougm 		(void) printf(gettext("\tprotocol must be specified "
12784653Sdougm 		    "with properties\n"));
12794653Sdougm 		return (SA_INVALID_PROTOCOL);
12803034Sdougm 	}
12813034Sdougm 
12823034Sdougm 	if (optlist != NULL)
12834653Sdougm 		ret = chk_opt(optlist, 0, protocol);
12843034Sdougm 	if (ret == OPT_ADD_SECURITY) {
12854653Sdougm 		(void) printf(gettext("Security properties not "
12864653Sdougm 		    "supported with create\n"));
12874653Sdougm 		return (SA_SYNTAX_ERR);
12883034Sdougm 	}
12893034Sdougm 
12903034Sdougm 	/*
12914653Sdougm 	 * If a group already exists, we can only add a new protocol
12923034Sdougm 	 * to it and not create a new one or add the same protocol
12933034Sdougm 	 * again.
12943034Sdougm 	 */
12953034Sdougm 
12963034Sdougm 	groupname = argv[optind];
12973034Sdougm 
12983034Sdougm 	auth = check_authorizations(groupname, flags);
12993034Sdougm 
13003910Sdougm 	group = sa_get_group(handle, groupname);
13013034Sdougm 	if (group != NULL) {
13024653Sdougm 		/* group exists so must be a protocol add */
13036088Sdougm 		ret = check_valid_group(group, groupname, protocol);
13043034Sdougm 	} else {
13053034Sdougm 		/*
13063034Sdougm 		 * is it a valid name? Must comply with SMF instance
13073034Sdougm 		 * name restrictions.
13083034Sdougm 		 */
13094653Sdougm 		if (!sa_valid_group_name(groupname)) {
13104653Sdougm 			ret = SA_INVALID_NAME;
13114653Sdougm 			(void) printf(gettext("Invalid group name: %s\n"),
13124653Sdougm 			    groupname);
13134653Sdougm 		}
13143034Sdougm 	}
13153034Sdougm 	if (ret == SA_OK) {
13164653Sdougm 		/* check protocol vs optlist */
13174653Sdougm 		if (optlist != NULL) {
13184653Sdougm 			/* check options, if any, for validity */
13196214Sdougm 			ret = valid_options(handle, optlist, protocol,
13206214Sdougm 			    group, NULL);
13214653Sdougm 		}
13223034Sdougm 	}
13233034Sdougm 	if (ret == SA_OK && !dryrun) {
13244653Sdougm 		if (group == NULL) {
13254653Sdougm 			group = sa_create_group(handle, (char *)groupname,
13264653Sdougm 			    &err);
13276088Sdougm 			created = B_TRUE;
13283034Sdougm 		}
13294653Sdougm 		if (group != NULL) {
13304653Sdougm 			sa_optionset_t optionset;
13316088Sdougm 
13325331Samw 			/*
13336088Sdougm 			 * Check group and protocol against featureset
13346088Sdougm 			 * requirements.
13355331Samw 			 */
13366088Sdougm 			ret = enforce_featureset(group, protocol,
13376088Sdougm 			    dryrun, force);
13386088Sdougm 			if (ret != SA_OK)
13396088Sdougm 				goto err;
13406088Sdougm 
13416088Sdougm 			/*
13426088Sdougm 			 * So far so good. Now add the required
13436088Sdougm 			 * optionset(s) to the group.
13446088Sdougm 			 */
13454653Sdougm 			if (optlist != NULL) {
13464653Sdougm 				(void) add_optionset(group, optlist, protocol,
13474653Sdougm 				    &ret);
13484653Sdougm 			} else if (protocol != NULL) {
13494653Sdougm 				optionset = sa_create_optionset(group,
13504653Sdougm 				    protocol);
13514653Sdougm 				if (optionset == NULL)
13524653Sdougm 					ret = SA_NO_MEMORY;
13534653Sdougm 			} else if (protocol == NULL) {
13546088Sdougm 				/* default group create so add all protocols */
13556088Sdougm 				ret = set_all_protocols(group);
13564653Sdougm 			}
13573034Sdougm 			/*
13584653Sdougm 			 * We have a group and legal additions
13593034Sdougm 			 */
13604653Sdougm 			if (ret == SA_OK) {
13614653Sdougm 				/*
13624653Sdougm 				 * Commit to configuration for protocols that
13634653Sdougm 				 * need to do block updates. For NFS, this
13644653Sdougm 				 * doesn't do anything but it will be run for
13654653Sdougm 				 * all protocols that implement the
13664653Sdougm 				 * appropriate plugin.
13674653Sdougm 				 */
13684653Sdougm 				ret = sa_update_config(handle);
13694653Sdougm 			} else {
13704653Sdougm 				if (group != NULL)
13714653Sdougm 					(void) sa_remove_group(group);
13724653Sdougm 			}
13733034Sdougm 		} else {
13744653Sdougm 			ret = err;
13754653Sdougm 			(void) printf(gettext("Could not create group: %s\n"),
13764653Sdougm 			    sa_errorstr(ret));
13773034Sdougm 		}
13783034Sdougm 	}
13793034Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose) {
13804653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
13814653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
13824653Sdougm 		ret = SA_NO_PERMISSION;
13833034Sdougm 	}
13845331Samw err:
13856088Sdougm 	if (ret != SA_OK && created)
13866088Sdougm 		ret = sa_remove_group(group);
13876088Sdougm 
13883034Sdougm 	free_opt(optlist);
13893034Sdougm 	return (ret);
13903034Sdougm }
13913034Sdougm 
13923034Sdougm /*
13933034Sdougm  * group_status(group)
13943034Sdougm  *
13953034Sdougm  * return the current status (enabled/disabled) of the group.
13963034Sdougm  */
13973034Sdougm 
13983034Sdougm static char *
13993034Sdougm group_status(sa_group_t group)
14003034Sdougm {
14013034Sdougm 	char *state;
14023034Sdougm 	int enabled = 0;
14033034Sdougm 
14043034Sdougm 	state = sa_get_group_attr(group, "state");
14053034Sdougm 	if (state != NULL) {
14064653Sdougm 		if (strcmp(state, "enabled") == 0) {
14074653Sdougm 			enabled = 1;
14084653Sdougm 		}
14094653Sdougm 		sa_free_attr_string(state);
14103034Sdougm 	}
14114255Sdougm 	return (enabled ? "enabled" : "disabled");
14123034Sdougm }
14133034Sdougm 
14143034Sdougm /*
14153034Sdougm  * sa_delete(flags, argc, argv)
14163034Sdougm  *
14173034Sdougm  *	Delete a group.
14183034Sdougm  */
14193034Sdougm 
14203034Sdougm static int
14213910Sdougm sa_delete(sa_handle_t handle, int flags, int argc, char *argv[])
14223034Sdougm {
14233034Sdougm 	char *groupname;
14243034Sdougm 	sa_group_t group;
14253034Sdougm 	sa_share_t share;
14263034Sdougm 	int verbose = 0;
14273034Sdougm 	int dryrun = 0;
14283034Sdougm 	int force = 0;
14293034Sdougm 	int c;
14303034Sdougm 	char *protocol = NULL;
14313034Sdougm 	char *sectype = NULL;
14323034Sdougm 	int ret = SA_OK;
14333034Sdougm 	int auth;
14343034Sdougm 
14353034Sdougm 	while ((c = getopt(argc, argv, "?hvnP:fS:")) != EOF) {
14364653Sdougm 		switch (c) {
14374653Sdougm 		case 'v':
14384653Sdougm 			verbose++;
14394653Sdougm 			break;
14404653Sdougm 		case 'n':
14414653Sdougm 			dryrun++;
14424653Sdougm 			break;
14434653Sdougm 		case 'P':
14445331Samw 			if (protocol != NULL) {
14455331Samw 				(void) printf(gettext("Specifying "
14465331Samw 				    "multiple protocols "
14475331Samw 				    "not supported: %s\n"), protocol);
14485331Samw 				return (SA_SYNTAX_ERR);
14495331Samw 			}
14504653Sdougm 			protocol = optarg;
14514653Sdougm 			if (!sa_valid_protocol(protocol)) {
14524653Sdougm 				(void) printf(gettext("Invalid protocol "
14535331Samw 				    "specified: %s\n"), protocol);
14544653Sdougm 				return (SA_INVALID_PROTOCOL);
14554653Sdougm 			}
14564653Sdougm 			break;
14574653Sdougm 		case 'S':
14585331Samw 			if (sectype != NULL) {
14595331Samw 				(void) printf(gettext("Specifying "
14605331Samw 				    "multiple property "
14615331Samw 				    "spaces not supported: %s\n"), sectype);
14625331Samw 				return (SA_SYNTAX_ERR);
14635331Samw 			}
14644653Sdougm 			sectype = optarg;
14654653Sdougm 			break;
14664653Sdougm 		case 'f':
14674653Sdougm 			force++;
14684653Sdougm 			break;
14696019Sdougm 		case 'h':
14706019Sdougm 			/* optopt on valid arg isn't defined */
14716019Sdougm 			optopt = c;
14726019Sdougm 			/*FALLTHROUGH*/
14736019Sdougm 		case '?':
14744653Sdougm 		default:
14756019Sdougm 			/*
14766019Sdougm 			 * Since a bad option gets to here, sort it
14776019Sdougm 			 * out and return a syntax error return value
14786019Sdougm 			 * if necessary.
14796019Sdougm 			 */
14806019Sdougm 			switch (optopt) {
14816019Sdougm 			default:
14826019Sdougm 				ret = SA_SYNTAX_ERR;
14836019Sdougm 				break;
14846019Sdougm 			case 'h':
14856019Sdougm 			case '?':
14866019Sdougm 				break;
14876019Sdougm 			}
14884653Sdougm 			(void) printf(gettext("usage: %s\n"),
14894653Sdougm 			    sa_get_usage(USAGE_DELETE));
14906019Sdougm 			return (ret);
14913034Sdougm 		}
14923034Sdougm 	}
14933034Sdougm 
14943034Sdougm 	if (optind >= argc) {
14954653Sdougm 		(void) printf(gettext("usage: %s\n"),
14964653Sdougm 		    sa_get_usage(USAGE_DELETE));
14974653Sdougm 		(void) printf(gettext("\tgroup must be specified.\n"));
14984653Sdougm 		return (SA_SYNTAX_ERR);
14993034Sdougm 	}
15003034Sdougm 
15013034Sdougm 	if ((optind + 1) < argc) {
15024653Sdougm 		(void) printf(gettext("usage: %s\n"),
15034653Sdougm 		    sa_get_usage(USAGE_DELETE));
15044653Sdougm 		(void) printf(gettext("\textraneous group(s) at end\n"));
15054653Sdougm 		return (SA_SYNTAX_ERR);
15063034Sdougm 	}
15073034Sdougm 
15083034Sdougm 	if (sectype != NULL && protocol == NULL) {
15094653Sdougm 		(void) printf(gettext("usage: %s\n"),
15104653Sdougm 		    sa_get_usage(USAGE_DELETE));
15114653Sdougm 		(void) printf(gettext("\tsecurity requires protocol to be "
15124653Sdougm 		    "specified.\n"));
15134653Sdougm 		return (SA_SYNTAX_ERR);
15143034Sdougm 	}
15153034Sdougm 
15163034Sdougm 	/*
15173034Sdougm 	 * Determine if the group already exists since it must in
15183034Sdougm 	 * order to be removed.
15193034Sdougm 	 *
15203034Sdougm 	 * We can delete when:
15213034Sdougm 	 *
15223034Sdougm 	 *	- group is empty
15233034Sdougm 	 *	- force flag is set
15243034Sdougm 	 *	- if protocol specified, only delete the protocol
15253034Sdougm 	 */
15263034Sdougm 
15273034Sdougm 	groupname = argv[optind];
15283910Sdougm 	group = sa_get_group(handle, groupname);
15293034Sdougm 	if (group == NULL) {
15303034Sdougm 		ret = SA_NO_SUCH_GROUP;
15314653Sdougm 		goto done;
15324653Sdougm 	}
15334653Sdougm 	auth = check_authorizations(groupname, flags);
15344653Sdougm 	if (protocol == NULL) {
15353034Sdougm 		share = sa_get_share(group, NULL);
15363034Sdougm 		if (share != NULL)
15374653Sdougm 			ret = SA_BUSY;
15383034Sdougm 		if (share == NULL || (share != NULL && force == 1)) {
15394653Sdougm 			ret = SA_OK;
15404653Sdougm 			if (!dryrun) {
15414653Sdougm 				while (share != NULL) {
15424653Sdougm 					sa_share_t next_share;
15434653Sdougm 					next_share = sa_get_next_share(share);
15444653Sdougm 					/*
15454653Sdougm 					 * need to do the disable of
15464653Sdougm 					 * each share, but don't
15474653Sdougm 					 * actually do anything on a
15484653Sdougm 					 * dryrun.
15494653Sdougm 					 */
15504653Sdougm 					ret = sa_disable_share(share, NULL);
15514653Sdougm 					ret = sa_remove_share(share);
15524653Sdougm 					share = next_share;
15534653Sdougm 				}
15544653Sdougm 				ret = sa_remove_group(group);
15553034Sdougm 			}
15563034Sdougm 		}
15574653Sdougm 		/* Commit to configuration if not a dryrun */
15583034Sdougm 		if (!dryrun && ret == SA_OK) {
15594653Sdougm 			ret = sa_update_config(handle);
15603034Sdougm 		}
15614653Sdougm 	} else {
15623034Sdougm 		/* a protocol delete */
15633034Sdougm 		sa_optionset_t optionset;
15643034Sdougm 		sa_security_t security;
15655331Samw 		if (sectype != NULL) {
15664653Sdougm 			/* only delete specified security */
15674653Sdougm 			security = sa_get_security(group, sectype, protocol);
15684653Sdougm 			if (security != NULL && !dryrun)
15694653Sdougm 				ret = sa_destroy_security(security);
15704653Sdougm 			else
15714653Sdougm 				ret = SA_INVALID_PROTOCOL;
15723034Sdougm 		} else {
15734653Sdougm 			optionset = sa_get_optionset(group, protocol);
15744653Sdougm 			if (optionset != NULL && !dryrun) {
15754653Sdougm 				/*
15764653Sdougm 				 * have an optionset with
15774653Sdougm 				 * protocol to delete
15784653Sdougm 				 */
15794653Sdougm 				ret = sa_destroy_optionset(optionset);
15804653Sdougm 				/*
15814653Sdougm 				 * Now find all security sets
15824653Sdougm 				 * for the protocol and remove
15834653Sdougm 				 * them. Don't remove other
15844653Sdougm 				 * protocols.
15854653Sdougm 				 */
15864653Sdougm 				for (security =
15874653Sdougm 				    sa_get_security(group, NULL, NULL);
15884653Sdougm 				    ret == SA_OK && security != NULL;
15894653Sdougm 				    security = sa_get_next_security(security)) {
15904653Sdougm 					char *secprot;
15914653Sdougm 					secprot = sa_get_security_attr(security,
15924653Sdougm 					    "type");
15934653Sdougm 					if (secprot != NULL &&
15944653Sdougm 					    strcmp(secprot, protocol) == 0)
15954653Sdougm 						ret = sa_destroy_security(
15964653Sdougm 						    security);
15974653Sdougm 					if (secprot != NULL)
15984653Sdougm 						sa_free_attr_string(secprot);
15994653Sdougm 				}
16004653Sdougm 			} else {
16014653Sdougm 				if (!dryrun)
16024653Sdougm 					ret = SA_INVALID_PROTOCOL;
16033034Sdougm 			}
16043034Sdougm 		}
16055331Samw 		/*
16065331Samw 		 * With the protocol items removed, make sure that all
16075331Samw 		 * the shares are updated in the legacy files, if
16085331Samw 		 * necessary.
16095331Samw 		 */
16105331Samw 		for (share = sa_get_share(group, NULL);
16115331Samw 		    share != NULL;
16125331Samw 		    share = sa_get_next_share(share)) {
16135331Samw 			(void) sa_delete_legacy(share, protocol);
16145331Samw 		}
16153034Sdougm 	}
16164653Sdougm 
16174653Sdougm done:
16183034Sdougm 	if (ret != SA_OK) {
16194653Sdougm 		(void) printf(gettext("Could not delete group: %s\n"),
16204653Sdougm 		    sa_errorstr(ret));
16213034Sdougm 	} else if (dryrun && !auth && verbose) {
16224653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
16234653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
16243034Sdougm 	}
16253034Sdougm 	return (ret);
16263034Sdougm }
16273034Sdougm 
16283034Sdougm /*
16293034Sdougm  * strndupr(*buff, str, buffsize)
16303034Sdougm  *
16313034Sdougm  * used with small strings to duplicate and possibly increase the
16323034Sdougm  * buffer size of a string.
16333034Sdougm  */
16343034Sdougm static char *
16353034Sdougm strndupr(char *buff, char *str, int *buffsize)
16363034Sdougm {
16373034Sdougm 	int limit;
16383034Sdougm 	char *orig_buff = buff;
16393034Sdougm 
16403034Sdougm 	if (buff == NULL) {
16414653Sdougm 		buff = (char *)malloc(64);
16424653Sdougm 		if (buff == NULL)
16434653Sdougm 			return (NULL);
16444653Sdougm 		*buffsize = 64;
16454653Sdougm 		buff[0] = '\0';
16463034Sdougm 	}
16473034Sdougm 	limit = strlen(buff) + strlen(str) + 1;
16483034Sdougm 	if (limit > *buffsize) {
16494653Sdougm 		limit = *buffsize = *buffsize + ((limit / 64) + 64);
16504653Sdougm 		buff = realloc(buff, limit);
16513034Sdougm 	}
16523034Sdougm 	if (buff != NULL) {
16534653Sdougm 		(void) strcat(buff, str);
16543034Sdougm 	} else {
16554653Sdougm 		/* if it fails, fail it hard */
16564653Sdougm 		if (orig_buff != NULL)
16574653Sdougm 			free(orig_buff);
16583034Sdougm 	}
16593034Sdougm 	return (buff);
16603034Sdougm }
16613034Sdougm 
16623034Sdougm /*
16633034Sdougm  * group_proto(group)
16643034Sdougm  *
16653034Sdougm  * return a string of all the protocols (space separated) associated
16663034Sdougm  * with this group.
16673034Sdougm  */
16683034Sdougm 
16693034Sdougm static char *
16703034Sdougm group_proto(sa_group_t group)
16713034Sdougm {
16723034Sdougm 	sa_optionset_t optionset;
16733034Sdougm 	char *proto;
16743034Sdougm 	char *buff = NULL;
16753034Sdougm 	int buffsize = 0;
16763034Sdougm 	int addspace = 0;
16773034Sdougm 	/*
16783034Sdougm 	 * get the protocol list by finding the optionsets on this
16793034Sdougm 	 * group and extracting the type value. The initial call to
16803034Sdougm 	 * strndupr() initailizes buff.
16813034Sdougm 	 */
16823034Sdougm 	buff = strndupr(buff, "", &buffsize);
16833034Sdougm 	if (buff != NULL) {
16844653Sdougm 		for (optionset = sa_get_optionset(group, NULL);
16854653Sdougm 		    optionset != NULL && buff != NULL;
16864653Sdougm 		    optionset = sa_get_next_optionset(optionset)) {
16874653Sdougm 			/*
16884653Sdougm 			 * extract out the protocol type from this optionset
16894653Sdougm 			 * and append it to the buffer "buff". strndupr() will
16904653Sdougm 			 * reallocate space as necessay.
16914653Sdougm 			 */
16924653Sdougm 			proto = sa_get_optionset_attr(optionset, "type");
16934653Sdougm 			if (proto != NULL) {
16944653Sdougm 				if (addspace++)
16954653Sdougm 					buff = strndupr(buff, " ", &buffsize);
16964653Sdougm 				buff = strndupr(buff, proto, &buffsize);
16974653Sdougm 				sa_free_attr_string(proto);
16984653Sdougm 			}
16993034Sdougm 		}
17003034Sdougm 	}
17013034Sdougm 	return (buff);
17023034Sdougm }
17033034Sdougm 
17043034Sdougm /*
17053034Sdougm  * sa_list(flags, argc, argv)
17063034Sdougm  *
17073034Sdougm  * implements the "list" subcommand to list groups and optionally
17083034Sdougm  * their state and protocols.
17093034Sdougm  */
17103034Sdougm 
17113034Sdougm static int
17123910Sdougm sa_list(sa_handle_t handle, int flags, int argc, char *argv[])
17133034Sdougm {
17143034Sdougm 	sa_group_t group;
17153034Sdougm 	int verbose = 0;
17163034Sdougm 	int c;
17173034Sdougm 	char *protocol = NULL;
17186019Sdougm 	int ret = SA_OK;
17195331Samw #ifdef lint
17205331Samw 	flags = flags;
17215331Samw #endif
17223034Sdougm 
17233034Sdougm 	while ((c = getopt(argc, argv, "?hvP:")) != EOF) {
17244653Sdougm 		switch (c) {
17254653Sdougm 		case 'v':
17264653Sdougm 			verbose++;
17274653Sdougm 			break;
17284653Sdougm 		case 'P':
17295331Samw 			if (protocol != NULL) {
17305331Samw 				(void) printf(gettext(
17315331Samw 				    "Specifying multiple protocols "
17325331Samw 				    "not supported: %s\n"),
17335331Samw 				    protocol);
17345331Samw 				return (SA_SYNTAX_ERR);
17355331Samw 			}
17364653Sdougm 			protocol = optarg;
17374653Sdougm 			if (!sa_valid_protocol(protocol)) {
17384653Sdougm 				(void) printf(gettext(
17394653Sdougm 				    "Invalid protocol specified: %s\n"),
17404653Sdougm 				    protocol);
17414653Sdougm 				return (SA_INVALID_PROTOCOL);
17424653Sdougm 			}
17434653Sdougm 			break;
17446019Sdougm 		case 'h':
17456019Sdougm 			/* optopt on valid arg isn't defined */
17466019Sdougm 			optopt = c;
17476019Sdougm 			/*FALLTHROUGH*/
17486019Sdougm 		case '?':
17494653Sdougm 		default:
17506019Sdougm 			/*
17516019Sdougm 			 * Since a bad option gets to here, sort it
17526019Sdougm 			 * out and return a syntax error return value
17536019Sdougm 			 * if necessary.
17546019Sdougm 			 */
17556019Sdougm 			switch (optopt) {
17566019Sdougm 			default:
17576019Sdougm 				ret = SA_SYNTAX_ERR;
17586019Sdougm 				break;
17596019Sdougm 			case 'h':
17606019Sdougm 			case '?':
17616019Sdougm 				break;
17626019Sdougm 			}
17634653Sdougm 			(void) printf(gettext("usage: %s\n"),
17644653Sdougm 			    sa_get_usage(USAGE_LIST));
17656019Sdougm 				return (ret);
17663034Sdougm 		}
17673034Sdougm 	}
17683034Sdougm 
17695885Sdougm 	if (optind != argc) {
17705885Sdougm 		(void) printf(gettext("usage: %s\n"),
17715885Sdougm 		    sa_get_usage(USAGE_LIST));
17725885Sdougm 		return (SA_SYNTAX_ERR);
17735885Sdougm 	}
17745885Sdougm 
17754653Sdougm 	for (group = sa_get_group(handle, NULL);
17764653Sdougm 	    group != NULL;
17773034Sdougm 	    group = sa_get_next_group(group)) {
17784653Sdougm 		char *name;
17794653Sdougm 		char *proto;
17804653Sdougm 		if (protocol == NULL || has_protocol(group, protocol)) {
17814653Sdougm 			name = sa_get_group_attr(group, "name");
17824653Sdougm 			if (name != NULL && (verbose > 1 || name[0] != '#')) {
17834653Sdougm 				(void) printf("%s", (char *)name);
17844653Sdougm 				if (verbose) {
17854653Sdougm 					/*
17864653Sdougm 					 * Need the list of protocols
17874653Sdougm 					 * and current status once
17884653Sdougm 					 * available. We do want to
17894653Sdougm 					 * translate the
17904653Sdougm 					 * enabled/disabled text here.
17914653Sdougm 					 */
17924653Sdougm 					(void) printf("\t%s", isenabled(group) ?
17934653Sdougm 					    gettext("enabled") :
17944653Sdougm 					    gettext("disabled"));
17954653Sdougm 					proto = group_proto(group);
17964653Sdougm 					if (proto != NULL) {
17974653Sdougm 						(void) printf("\t%s",
17984653Sdougm 						    (char *)proto);
17994653Sdougm 						free(proto);
18004653Sdougm 					}
18014653Sdougm 				}
18024653Sdougm 				(void) printf("\n");
18033034Sdougm 			}
18044653Sdougm 			if (name != NULL)
18054653Sdougm 				sa_free_attr_string(name);
18063034Sdougm 		}
18073034Sdougm 	}
18083034Sdougm 	return (0);
18093034Sdougm }
18103034Sdougm 
18113034Sdougm /*
18123034Sdougm  * out_properties(optionset, proto, sec)
18133034Sdougm  *
18143034Sdougm  * Format the properties and encode the protocol and optional named
18153034Sdougm  * optionset into the string.
18163034Sdougm  *
18173034Sdougm  * format is protocol[:name]=(property-list)
18183034Sdougm  */
18193034Sdougm 
18203034Sdougm static void
18213034Sdougm out_properties(sa_optionset_t optionset, char *proto, char *sec)
18223034Sdougm {
18233034Sdougm 	char *type;
18243034Sdougm 	char *value;
18253034Sdougm 	int spacer;
18263034Sdougm 	sa_property_t prop;
18273034Sdougm 
18284653Sdougm 	if (sec == NULL)
18294653Sdougm 		(void) printf(" %s=(", proto ? proto : gettext("all"));
18304653Sdougm 	else
18314653Sdougm 		(void) printf(" %s:%s=(", proto ? proto : gettext("all"), sec);
18323034Sdougm 
18333034Sdougm 	for (spacer = 0, prop = sa_get_property(optionset, NULL);
18344653Sdougm 	    prop != NULL;
18354653Sdougm 	    prop = sa_get_next_property(prop)) {
18363034Sdougm 
18373034Sdougm 		/*
18383034Sdougm 		 * extract the property name/value and output with
18393034Sdougm 		 * appropriate spacing. I.e. no prefixed space the
18403034Sdougm 		 * first time through but a space on subsequent
18413034Sdougm 		 * properties.
18423034Sdougm 		 */
18434653Sdougm 		type = sa_get_property_attr(prop, "type");
18444653Sdougm 		value = sa_get_property_attr(prop, "value");
18454653Sdougm 		if (type != NULL) {
18464653Sdougm 			(void) printf("%s%s=", spacer ? " " : "",	type);
18474653Sdougm 			spacer = 1;
18484653Sdougm 			if (value != NULL)
18494653Sdougm 				(void) printf("\"%s\"", value);
18504653Sdougm 			else
18514653Sdougm 				(void) printf("\"\"");
18524653Sdougm 		}
18534653Sdougm 		if (type != NULL)
18544653Sdougm 			sa_free_attr_string(type);
18553034Sdougm 		if (value != NULL)
18564653Sdougm 			sa_free_attr_string(value);
18573034Sdougm 	}
18583034Sdougm 	(void) printf(")");
18593034Sdougm }
18603034Sdougm 
18613034Sdougm /*
18623034Sdougm  * show_properties(group, protocol, prefix)
18633034Sdougm  *
18643034Sdougm  * print the properties for a group. If protocol is NULL, do all
18653034Sdougm  * protocols otherwise only the specified protocol. All security
18663034Sdougm  * (named groups specific to the protocol) are included.
18673034Sdougm  *
18683034Sdougm  * The "prefix" is always applied. The caller knows whether it wants
18693034Sdougm  * some type of prefix string (white space) or not.  Once the prefix
18703034Sdougm  * has been output, it is reduced to the zero length string for the
18713034Sdougm  * remainder of the property output.
18723034Sdougm  */
18733034Sdougm 
18743034Sdougm static void
18753034Sdougm show_properties(sa_group_t group, char *protocol, char *prefix)
18763034Sdougm {
18773034Sdougm 	sa_optionset_t optionset;
18783034Sdougm 	sa_security_t security;
18793034Sdougm 	char *value;
18803034Sdougm 	char *secvalue;
18813034Sdougm 
18823034Sdougm 	if (protocol != NULL) {
18834653Sdougm 		optionset = sa_get_optionset(group, protocol);
18844653Sdougm 		if (optionset != NULL) {
18854653Sdougm 			(void) printf("%s", prefix);
18864653Sdougm 			prefix = "";
18874653Sdougm 			out_properties(optionset, protocol, NULL);
18884653Sdougm 		}
18894653Sdougm 		security = sa_get_security(group, protocol, NULL);
18904653Sdougm 		if (security != NULL) {
18914653Sdougm 			(void) printf("%s", prefix);
18924653Sdougm 			prefix = "";
18934653Sdougm 			out_properties(security, protocol, NULL);
18944653Sdougm 		}
18953034Sdougm 	} else {
18964653Sdougm 		for (optionset = sa_get_optionset(group, protocol);
18974653Sdougm 		    optionset != NULL;
18984653Sdougm 		    optionset = sa_get_next_optionset(optionset)) {
18994653Sdougm 
19004653Sdougm 			value = sa_get_optionset_attr(optionset, "type");
19014653Sdougm 			(void) printf("%s", prefix);
19024653Sdougm 			prefix = "";
19034653Sdougm 			out_properties(optionset, value, 0);
19044653Sdougm 			if (value != NULL)
19054653Sdougm 				sa_free_attr_string(value);
19064653Sdougm 		}
19074653Sdougm 		for (security = sa_get_security(group, NULL, protocol);
19084653Sdougm 		    security != NULL;
19094653Sdougm 		    security = sa_get_next_security(security)) {
19104653Sdougm 
19114653Sdougm 			value = sa_get_security_attr(security, "type");
19124653Sdougm 			secvalue = sa_get_security_attr(security, "sectype");
19134653Sdougm 			(void) printf("%s", prefix);
19144653Sdougm 			prefix = "";
19154653Sdougm 			out_properties(security, value, secvalue);
19164653Sdougm 			if (value != NULL)
19174653Sdougm 				sa_free_attr_string(value);
19184653Sdougm 			if (secvalue != NULL)
19194653Sdougm 				sa_free_attr_string(secvalue);
19204653Sdougm 		}
19213034Sdougm 	}
19223034Sdougm }
19233034Sdougm 
19243034Sdougm /*
19255331Samw  * get_resource(share)
19265331Samw  *
19275331Samw  * Get the first resource name, if any, and fix string to be in
19285331Samw  * current locale and have quotes if it has embedded spaces.  Return
19295331Samw  * an attr string that must be freed.
19305331Samw  */
19315331Samw 
19325331Samw static char *
19335331Samw get_resource(sa_share_t share)
19345331Samw {
19355331Samw 	sa_resource_t resource;
19365331Samw 	char *resstring = NULL;
19375331Samw 	char *retstring;
19385331Samw 
19395331Samw 	if ((resource = sa_get_share_resource(share, NULL)) != NULL) {
19405331Samw 		resstring = sa_get_resource_attr(resource, "name");
19415331Samw 		if (resstring != NULL) {
19425331Samw 			char *cp;
19435331Samw 			int len;
19445331Samw 
19455331Samw 			retstring = conv_from_utf8(resstring);
19465331Samw 			if (retstring != resstring) {
19475331Samw 				sa_free_attr_string(resstring);
19485331Samw 				resstring = retstring;
19495331Samw 			}
19505331Samw 			if (strpbrk(resstring, " ") != NULL) {
19515331Samw 				/* account for quotes */
19525331Samw 				len = strlen(resstring) + 3;
19535331Samw 				cp = calloc(len, sizeof (char));
19545331Samw 				if (cp != NULL) {
19555331Samw 					(void) snprintf(cp, len,
19565331Samw 					    "\"%s\"", resstring);
19575331Samw 					sa_free_attr_string(resstring);
19585331Samw 					resstring = cp;
19595331Samw 				} else {
19605331Samw 					sa_free_attr_string(resstring);
19615331Samw 					resstring = NULL;
19625331Samw 				}
19635331Samw 			}
19645331Samw 		}
19655331Samw 	}
19665331Samw 	return (resstring);
19675331Samw }
19685331Samw 
19695331Samw /*
19705331Samw  * has_resource_with_opt(share)
19715331Samw  *
19725331Samw  * Check to see if the share has any resource names with optionsets
19735331Samw  * set. Also indicate if multiple resource names since the syntax
19745331Samw  * would be about the same.
19755331Samw  */
19765331Samw static int
19775331Samw has_resource_with_opt(sa_share_t share)
19785331Samw {
19795331Samw 	sa_resource_t resource;
19805331Samw 	int ret = B_FALSE;
19815331Samw 
19825331Samw 	for (resource = sa_get_share_resource(share, NULL);
19835331Samw 	    resource != NULL;
19845331Samw 	    resource = sa_get_next_resource(resource)) {
19855331Samw 
19865331Samw 		if (sa_get_optionset(resource, NULL) != NULL) {
19875331Samw 			ret = B_TRUE;
19885331Samw 			break;
19895331Samw 		}
19905331Samw 	}
19915331Samw 	return (ret);
19925331Samw }
19935331Samw 
19945331Samw /*
19955331Samw  * has_multiple_resource(share)
19965331Samw  *
19975885Sdougm  * Check to see if the share has multiple resource names since
19985885Sdougm  * the syntax would be about the same.
19995331Samw  */
20005885Sdougm static boolean_t
20015331Samw has_multiple_resource(sa_share_t share)
20025331Samw {
20035331Samw 	sa_resource_t resource;
20045331Samw 	int num;
20055331Samw 
20065331Samw 	for (num = 0, resource = sa_get_share_resource(share, NULL);
20075331Samw 	    resource != NULL;
20085331Samw 	    resource = sa_get_next_resource(resource)) {
20095331Samw 		num++;
20105331Samw 		if (num > 1)
20115331Samw 			return (B_TRUE);
20125331Samw 	}
20135331Samw 	return (B_FALSE);
20145331Samw }
20155331Samw 
20165331Samw /*
20175331Samw  * show_share(share, verbose, properties, proto, iszfs, sharepath)
20185331Samw  *
20195331Samw  * print out the share information. With the addition of resource as a
20205331Samw  * full object that can have multiple instances below the share, we
20215331Samw  * need to display that as well.
20225331Samw  */
20235331Samw 
20245331Samw static void
20255331Samw show_share(sa_share_t share, int verbose, int properties, char *proto,
20265331Samw     int iszfs, char *sharepath)
20275331Samw {
20285331Samw 	char *drive;
20295331Samw 	char *exclude;
20305331Samw 	sa_resource_t resource = NULL;
20315331Samw 	char *description;
20325331Samw 	char *rsrcname;
20335331Samw 	int rsrcwithopt;
20345885Sdougm 	boolean_t multiple;
20355331Samw 	char *type;
20365331Samw 
20375331Samw 	rsrcwithopt = has_resource_with_opt(share);
20385331Samw 
20395331Samw 	if (verbose || (properties && rsrcwithopt)) {
20405331Samw 		/* First, indicate if transient */
20415331Samw 		type = sa_get_share_attr(share, "type");
20425331Samw 		if (type != NULL && !iszfs && verbose &&
20435331Samw 		    strcmp(type, "transient") == 0)
20445331Samw 			(void) printf("\t* ");
20455331Samw 		else
20465331Samw 			(void) printf("\t  ");
20475331Samw 
20485331Samw 		if (type != NULL)
20495331Samw 			sa_free_attr_string(type);
20505331Samw 
20515331Samw 		/*
20525331Samw 		 * If we came in with verbose, we want to handle the case of
20535331Samw 		 * multiple resources as though they had properties set.
20545331Samw 		 */
20555331Samw 		multiple = has_multiple_resource(share);
20565331Samw 
20575885Sdougm 		/*
20585885Sdougm 		 * if there is a description on the share and there
20595885Sdougm 		 * are resources, treat as multiple resources in order
20605885Sdougm 		 * to get all descriptions displayed.
20615885Sdougm 		 */
20625885Sdougm 		description = sa_get_share_description(share);
20635885Sdougm 		resource = sa_get_share_resource(share, NULL);
20645885Sdougm 
20655885Sdougm 		if (description != NULL && resource != NULL)
20665885Sdougm 			multiple = B_TRUE;
20675885Sdougm 
20685331Samw 		/* Next, if not multiple follow old model */
20695331Samw 		if (!multiple && !rsrcwithopt) {
20705331Samw 			rsrcname = get_resource(share);
20715331Samw 			if (rsrcname != NULL && strlen(rsrcname) > 0) {
20725331Samw 				(void) printf("%s=%s", rsrcname, sharepath);
20735331Samw 			} else {
20745331Samw 				(void) printf("%s", sharepath);
20755331Samw 			}
20765331Samw 			if (rsrcname != NULL)
20775331Samw 				sa_free_attr_string(rsrcname);
20785885Sdougm 			/* Print the description string if there is one. */
20795885Sdougm 			print_rsrc_desc(resource, description);
20805331Samw 		} else {
20815331Samw 			/* Treat as simple and then resources come later */
20825331Samw 			(void) printf("%s", sharepath);
20835331Samw 		}
20845331Samw 		drive = sa_get_share_attr(share, "drive-letter");
20855331Samw 		if (drive != NULL) {
20865331Samw 			if (strlen(drive) > 0)
20875331Samw 				(void) printf(gettext("\tdrive-letter=\"%s:\""),
20885331Samw 				    drive);
20895331Samw 			sa_free_attr_string(drive);
20905331Samw 		}
20915331Samw 		if (properties)
20925331Samw 			show_properties(share, proto, "\t");
20935331Samw 		exclude = sa_get_share_attr(share, "exclude");
20945331Samw 		if (exclude != NULL) {
20955331Samw 			(void) printf(gettext("\tnot-shared-with=[%s]"),
20965331Samw 			    exclude);
20975331Samw 			sa_free_attr_string(exclude);
20985331Samw 		}
20995885Sdougm 
21005331Samw 		if (description != NULL) {
21015885Sdougm 			print_rsrc_desc((sa_resource_t)share, description);
21025331Samw 		}
21035331Samw 		/*
21045331Samw 		 * If there are resource names with options, show them
21055331Samw 		 * here, with one line per resource. Resource specific
21065331Samw 		 * options are at the end of the line followed by
21075331Samw 		 * description, if any.
21085331Samw 		 */
21095331Samw 		if (rsrcwithopt || multiple) {
21105331Samw 			for (resource = sa_get_share_resource(share, NULL);
21115331Samw 			    resource != NULL;
21125331Samw 			    resource = sa_get_next_resource(resource)) {
21135331Samw 				int has_space;
21145331Samw 				char *rsrc;
21155331Samw 
21165331Samw 				(void) printf("\n\t\t  ");
21175331Samw 				rsrcname = sa_get_resource_attr(resource,
21185331Samw 				    "name");
21195331Samw 				if (rsrcname == NULL)
21205331Samw 					continue;
21215331Samw 
21225331Samw 				rsrc = conv_from_utf8(rsrcname);
21235331Samw 				has_space = strpbrk(rsrc, " ") != NULL;
21245331Samw 
21255331Samw 				if (has_space)
21265331Samw 					(void) printf("\"%s\"=%s", rsrc,
21275331Samw 					    sharepath);
21285331Samw 				else
21295331Samw 					(void) printf("%s=%s", rsrc,
21305331Samw 					    sharepath);
21315331Samw 				if (rsrc != rsrcname)
21325331Samw 					sa_free_attr_string(rsrc);
21335331Samw 				sa_free_attr_string(rsrcname);
21345331Samw 				if (properties || rsrcwithopt)
21355331Samw 					show_properties(resource, proto, "\t");
21365331Samw 
21375331Samw 				/* Get description string if any */
21385885Sdougm 				print_rsrc_desc(resource, description);
21395331Samw 			}
21405331Samw 		}
21415885Sdougm 		if (description != NULL)
21425885Sdougm 			sa_free_share_description(description);
21435331Samw 	} else {
21445331Samw 		(void) printf("\t  %s", sharepath);
21455331Samw 		if (properties)
21465331Samw 			show_properties(share, proto, "\t");
21475331Samw 	}
21485331Samw 	(void) printf("\n");
21495331Samw }
21505331Samw 
21515331Samw /*
21523034Sdougm  * show_group(group, verbose, properties, proto, subgroup)
21533034Sdougm  *
21543034Sdougm  * helper function to show the contents of a group.
21553034Sdougm  */
21563034Sdougm 
21573034Sdougm static void
21583034Sdougm show_group(sa_group_t group, int verbose, int properties, char *proto,
21595331Samw     char *subgroup)
21603034Sdougm {
21613034Sdougm 	sa_share_t share;
21623034Sdougm 	char *groupname;
21633034Sdougm 	char *zfs = NULL;
21643034Sdougm 	int iszfs = 0;
21655331Samw 	char *sharepath;
21663034Sdougm 
21673034Sdougm 	groupname = sa_get_group_attr(group, "name");
21683034Sdougm 	if (groupname != NULL) {
21694653Sdougm 		if (proto != NULL && !has_protocol(group, proto)) {
21704653Sdougm 			sa_free_attr_string(groupname);
21714653Sdougm 			return;
21724653Sdougm 		}
21733034Sdougm 		/*
21743034Sdougm 		 * check to see if the group is managed by ZFS. If
21753034Sdougm 		 * there is an attribute, then it is. A non-NULL zfs
21763034Sdougm 		 * variable will trigger the different way to display
21773034Sdougm 		 * and will remove the transient property indicator
21783034Sdougm 		 * from the output.
21793034Sdougm 		 */
21804653Sdougm 		zfs = sa_get_group_attr(group, "zfs");
21814653Sdougm 		if (zfs != NULL) {
21824653Sdougm 			iszfs = 1;
21834653Sdougm 			sa_free_attr_string(zfs);
21843034Sdougm 		}
21854653Sdougm 		share = sa_get_share(group, NULL);
21864653Sdougm 		if (subgroup == NULL)
21874653Sdougm 			(void) printf("%s", groupname);
21884653Sdougm 		else
21894653Sdougm 			(void) printf("    %s/%s", subgroup, groupname);
21904653Sdougm 		if (properties)
21914653Sdougm 			show_properties(group, proto, "");
21924653Sdougm 		(void) printf("\n");
21934653Sdougm 		if (strcmp(groupname, "zfs") == 0) {
21944653Sdougm 			sa_group_t zgroup;
21954653Sdougm 
21964653Sdougm 			for (zgroup = sa_get_sub_group(group);
21974653Sdougm 			    zgroup != NULL;
21984653Sdougm 			    zgroup = sa_get_next_group(zgroup)) {
21994653Sdougm 				show_group(zgroup, verbose, properties, proto,
22004653Sdougm 				    "zfs");
22014653Sdougm 			}
22024653Sdougm 			sa_free_attr_string(groupname);
22034653Sdougm 			return;
22044653Sdougm 		}
22053034Sdougm 		/*
22064653Sdougm 		 * Have a group, so list the contents. Resource and
22073034Sdougm 		 * description are only listed if verbose is set.
22083034Sdougm 		 */
22094653Sdougm 		for (share = sa_get_share(group, NULL);
22104653Sdougm 		    share != NULL;
22114653Sdougm 		    share = sa_get_next_share(share)) {
22124653Sdougm 			sharepath = sa_get_share_attr(share, "path");
22134653Sdougm 			if (sharepath != NULL) {
22145331Samw 				show_share(share, verbose, properties, proto,
22155331Samw 				    iszfs, sharepath);
22164653Sdougm 				sa_free_attr_string(sharepath);
22173034Sdougm 			}
22183034Sdougm 		}
22193034Sdougm 	}
22203034Sdougm 	if (groupname != NULL) {
22213034Sdougm 		sa_free_attr_string(groupname);
22223034Sdougm 	}
22233034Sdougm }
22243034Sdougm 
22253034Sdougm /*
22263034Sdougm  * show_group_xml_init()
22273034Sdougm  *
22283034Sdougm  * Create an XML document that will be used to display config info via
22293034Sdougm  * XML format.
22303034Sdougm  */
22313034Sdougm 
22323034Sdougm xmlDocPtr
22333034Sdougm show_group_xml_init()
22343034Sdougm {
22353034Sdougm 	xmlDocPtr doc;
22363034Sdougm 	xmlNodePtr root;
22373034Sdougm 
22383034Sdougm 	doc = xmlNewDoc((xmlChar *)"1.0");
22393034Sdougm 	if (doc != NULL) {
22404653Sdougm 		root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
22414653Sdougm 		if (root != NULL)
22424653Sdougm 			xmlDocSetRootElement(doc, root);
22433034Sdougm 	}
22443034Sdougm 	return (doc);
22453034Sdougm }
22463034Sdougm 
22473034Sdougm /*
22483034Sdougm  * show_group_xml(doc, group)
22493034Sdougm  *
22503034Sdougm  * Copy the group info into the XML doc.
22513034Sdougm  */
22523034Sdougm 
22533034Sdougm static void
22543034Sdougm show_group_xml(xmlDocPtr doc, sa_group_t group)
22553034Sdougm {
22563034Sdougm 	xmlNodePtr node;
22573034Sdougm 	xmlNodePtr root;
22583034Sdougm 
22593034Sdougm 	root = xmlDocGetRootElement(doc);
22603034Sdougm 	node = xmlCopyNode((xmlNodePtr)group, 1);
22613034Sdougm 	if (node != NULL && root != NULL) {
22624653Sdougm 		xmlAddChild(root, node);
22633034Sdougm 		/*
22643034Sdougm 		 * In the future, we may have interally used tags that
22653034Sdougm 		 * should not appear in the XML output. Remove
22663034Sdougm 		 * anything we don't want to show here.
22673034Sdougm 		 */
22683034Sdougm 	}
22693034Sdougm }
22703034Sdougm 
22713034Sdougm /*
22723034Sdougm  * sa_show(flags, argc, argv)
22733034Sdougm  *
22743034Sdougm  * Implements the show subcommand.
22753034Sdougm  */
22763034Sdougm 
22773034Sdougm int
22783910Sdougm sa_show(sa_handle_t handle, int flags, int argc, char *argv[])
22793034Sdougm {
22803034Sdougm 	sa_group_t group;
22813034Sdougm 	int verbose = 0;
22823034Sdougm 	int properties = 0;
22833034Sdougm 	int c;
22843034Sdougm 	int ret = SA_OK;
22853034Sdougm 	char *protocol = NULL;
22863034Sdougm 	int xml = 0;
22873034Sdougm 	xmlDocPtr doc;
22885331Samw #ifdef lint
22895331Samw 	flags = flags;
22905331Samw #endif
22913034Sdougm 
22923034Sdougm 	while ((c = getopt(argc, argv, "?hvP:px")) !=	EOF) {
22934653Sdougm 		switch (c) {
22944653Sdougm 		case 'v':
22954653Sdougm 			verbose++;
22964653Sdougm 			break;
22974653Sdougm 		case 'p':
22984653Sdougm 			properties++;
22994653Sdougm 			break;
23004653Sdougm 		case 'P':
23015331Samw 			if (protocol != NULL) {
23025331Samw 				(void) printf(gettext(
23035331Samw 				    "Specifying multiple protocols "
23045331Samw 				    "not supported: %s\n"),
23055331Samw 				    protocol);
23065331Samw 				return (SA_SYNTAX_ERR);
23075331Samw 			}
23084653Sdougm 			protocol = optarg;
23094653Sdougm 			if (!sa_valid_protocol(protocol)) {
23104653Sdougm 				(void) printf(gettext(
23114653Sdougm 				    "Invalid protocol specified: %s\n"),
23124653Sdougm 				    protocol);
23134653Sdougm 				return (SA_INVALID_PROTOCOL);
23144653Sdougm 			}
23154653Sdougm 			break;
23164653Sdougm 		case 'x':
23174653Sdougm 			xml++;
23184653Sdougm 			break;
23196019Sdougm 		case 'h':
23206019Sdougm 			/* optopt on valid arg isn't defined */
23216019Sdougm 			optopt = c;
23226019Sdougm 			/*FALLTHROUGH*/
23236019Sdougm 		case '?':
23244653Sdougm 		default:
23256019Sdougm 			/*
23266019Sdougm 			 * Since a bad option gets to here, sort it
23276019Sdougm 			 * out and return a syntax error return value
23286019Sdougm 			 * if necessary.
23296019Sdougm 			 */
23306019Sdougm 			switch (optopt) {
23316019Sdougm 			default:
23326019Sdougm 				ret = SA_SYNTAX_ERR;
23336019Sdougm 				break;
23346019Sdougm 			case 'h':
23356019Sdougm 			case '?':
23366019Sdougm 				break;
23376019Sdougm 			}
23384653Sdougm 			(void) printf(gettext("usage: %s\n"),
23394653Sdougm 			    sa_get_usage(USAGE_SHOW));
23406019Sdougm 			return (ret);
23413034Sdougm 		}
23423034Sdougm 	}
23433034Sdougm 
23443034Sdougm 	if (xml) {
23454653Sdougm 		doc = show_group_xml_init();
23464653Sdougm 		if (doc == NULL)
23474653Sdougm 			ret = SA_NO_MEMORY;
23483034Sdougm 	}
23493034Sdougm 
23503034Sdougm 	if (optind == argc) {
23514653Sdougm 		/* No group specified so go through them all */
23524653Sdougm 		for (group = sa_get_group(handle, NULL);
23534653Sdougm 		    group != NULL;
23544653Sdougm 		    group = sa_get_next_group(group)) {
23554653Sdougm 			/*
23564653Sdougm 			 * Have a group so check if one we want and then list
23574653Sdougm 			 * contents with appropriate options.
23584653Sdougm 			 */
23594653Sdougm 			if (xml)
23604653Sdougm 				show_group_xml(doc, group);
23614653Sdougm 			else
23624653Sdougm 				show_group(group, verbose, properties, protocol,
23634653Sdougm 				    NULL);
23644653Sdougm 		}
23653034Sdougm 	} else {
23664653Sdougm 		/* Have a specified list of groups */
23674653Sdougm 		for (; optind < argc; optind++) {
23684653Sdougm 			group = sa_get_group(handle, argv[optind]);
23694653Sdougm 			if (group != NULL) {
23704653Sdougm 				if (xml)
23714653Sdougm 					show_group_xml(doc, group);
23724653Sdougm 				else
23734653Sdougm 					show_group(group, verbose, properties,
23744653Sdougm 					    protocol, NULL);
23754653Sdougm 			} else {
23764653Sdougm 				(void) printf(gettext("%s: not found\n"),
23774653Sdougm 				    argv[optind]);
23784653Sdougm 				ret = SA_NO_SUCH_GROUP;
23794653Sdougm 			}
23803034Sdougm 		}
23813034Sdougm 	}
23823034Sdougm 	if (xml && ret == SA_OK) {
23834653Sdougm 		xmlDocFormatDump(stdout, doc, 1);
23844653Sdougm 		xmlFreeDoc(doc);
23853034Sdougm 	}
23863034Sdougm 	return (ret);
23873034Sdougm 
23883034Sdougm }
23893034Sdougm 
23903034Sdougm /*
23913034Sdougm  * enable_share(group, share, update_legacy)
23923034Sdougm  *
23933034Sdougm  * helper function to enable a share if the group is enabled.
23943034Sdougm  */
23953034Sdougm 
23963034Sdougm static int
23973910Sdougm enable_share(sa_handle_t handle, sa_group_t group, sa_share_t share,
23985331Samw     int update_legacy)
23993034Sdougm {
24003034Sdougm 	char *value;
24013034Sdougm 	int enabled;
24023034Sdougm 	sa_optionset_t optionset;
24035331Samw 	int err;
24043034Sdougm 	int ret = SA_OK;
24053034Sdougm 	char *zfs = NULL;
24063034Sdougm 	int iszfs = 0;
24075331Samw 	int isshare;
24083034Sdougm 
24093034Sdougm 	/*
24103034Sdougm 	 * need to enable this share if the group is enabled but not
24113034Sdougm 	 * otherwise. The enable is also done on each protocol
24123034Sdougm 	 * represented in the group.
24133034Sdougm 	 */
24143034Sdougm 	value = sa_get_group_attr(group, "state");
24153034Sdougm 	enabled = value != NULL && strcmp(value, "enabled") == 0;
24163034Sdougm 	if (value != NULL)
24174653Sdougm 		sa_free_attr_string(value);
24183034Sdougm 	/* remove legacy config if necessary */
24193034Sdougm 	if (update_legacy)
24205331Samw 		ret = sa_delete_legacy(share, NULL);
24213034Sdougm 	zfs = sa_get_group_attr(group, "zfs");
24223034Sdougm 	if (zfs != NULL) {
24234653Sdougm 		iszfs++;
24244653Sdougm 		sa_free_attr_string(zfs);
24253034Sdougm 	}
24263034Sdougm 
24273034Sdougm 	/*
24283034Sdougm 	 * Step through each optionset at the group level and
24293034Sdougm 	 * enable the share based on the protocol type. This
24303034Sdougm 	 * works because protocols must be set on the group
24313034Sdougm 	 * for the protocol to be enabled.
24323034Sdougm 	 */
24335331Samw 	isshare = sa_is_share(share);
24343034Sdougm 	for (optionset = sa_get_optionset(group, NULL);
24353034Sdougm 	    optionset != NULL && ret == SA_OK;
24363034Sdougm 	    optionset = sa_get_next_optionset(optionset)) {
24374653Sdougm 		value = sa_get_optionset_attr(optionset, "type");
24384653Sdougm 		if (value != NULL) {
24395331Samw 			if (enabled) {
24405331Samw 				if (isshare) {
24415331Samw 					err = sa_enable_share(share, value);
24425331Samw 				} else {
24435331Samw 					err = sa_enable_resource(share, value);
24445331Samw 					if (err == SA_NOT_SUPPORTED) {
24455331Samw 						sa_share_t parent;
24465331Samw 						parent = sa_get_resource_parent(
24475331Samw 						    share);
24485331Samw 						if (parent != NULL)
24495331Samw 							err = sa_enable_share(
24505331Samw 							    parent, value);
24515331Samw 					}
24525331Samw 				}
24535331Samw 				if (err != SA_OK) {
24545331Samw 					ret = err;
24555331Samw 					(void) printf(gettext(
24565331Samw 					    "Failed to enable share for "
24575331Samw 					    "\"%s\": %s\n"),
24585331Samw 					    value, sa_errorstr(ret));
24595331Samw 				}
24605331Samw 			}
24615331Samw 			/*
24625331Samw 			 * If we want to update the legacy, use a copy of
24635331Samw 			 * share so we can avoid breaking the loop we are in
24645331Samw 			 * since we might also need to go up the tree to the
24655331Samw 			 * parent.
24665331Samw 			 */
24675331Samw 			if (update_legacy && !iszfs) {
24685331Samw 				sa_share_t update = share;
24695331Samw 				if (!sa_is_share(share)) {
24705331Samw 					update = sa_get_resource_parent(share);
24715331Samw 				}
24725331Samw 				(void) sa_update_legacy(update, value);
24735331Samw 			}
24744653Sdougm 			sa_free_attr_string(value);
24754653Sdougm 		}
24763034Sdougm 	}
24773034Sdougm 	if (ret == SA_OK)
24784653Sdougm 		(void) sa_update_config(handle);
24793034Sdougm 	return (ret);
24803034Sdougm }
24813034Sdougm 
24823034Sdougm /*
24835331Samw  * sa_require_resource(group)
24845331Samw  *
24855331Samw  * if any of the defined protocols on the group require resource
24865331Samw  * names, then all shares must have them.
24875331Samw  */
24885331Samw 
24895331Samw static int
24905331Samw sa_require_resource(sa_group_t group)
24915331Samw {
24925331Samw 	sa_optionset_t optionset;
24935331Samw 
24945331Samw 	for (optionset = sa_get_optionset(group, NULL);
24955331Samw 	    optionset != NULL;
24965331Samw 	    optionset = sa_get_next_optionset(optionset)) {
24975331Samw 		char *proto;
24985331Samw 
24995331Samw 		proto = sa_get_optionset_attr(optionset, "type");
25005331Samw 		if (proto != NULL) {
25015331Samw 			uint64_t features;
25025331Samw 
25035331Samw 			features = sa_proto_get_featureset(proto);
25045331Samw 			if (features & SA_FEATURE_RESOURCE) {
25055331Samw 				sa_free_attr_string(proto);
25065331Samw 				return (B_TRUE);
25075331Samw 			}
25085331Samw 			sa_free_attr_string(proto);
25095331Samw 		}
25105331Samw 	}
25115331Samw 	return (B_FALSE);
25125331Samw }
25135331Samw 
25145331Samw /*
25153034Sdougm  * sa_addshare(flags, argc, argv)
25163034Sdougm  *
25173034Sdougm  * implements add-share subcommand.
25183034Sdougm  */
25193034Sdougm 
25205331Samw static int
25213910Sdougm sa_addshare(sa_handle_t handle, int flags, int argc, char *argv[])
25223034Sdougm {
25233034Sdougm 	int verbose = 0;
25243034Sdougm 	int dryrun = 0;
25253034Sdougm 	int c;
25263034Sdougm 	int ret = SA_OK;
25273034Sdougm 	sa_group_t group;
25283034Sdougm 	sa_share_t share;
25295331Samw 	sa_resource_t resource = NULL;
25303034Sdougm 	char *sharepath = NULL;
25313034Sdougm 	char *description = NULL;
25325331Samw 	char *rsrcname = NULL;
25335331Samw 	char *rsrc = NULL;
25343034Sdougm 	int persist = SA_SHARE_PERMANENT; /* default to persist */
25353034Sdougm 	int auth;
25363034Sdougm 	char dir[MAXPATHLEN];
25373034Sdougm 
25383034Sdougm 	while ((c = getopt(argc, argv, "?hvns:d:r:t")) != EOF) {
25394653Sdougm 		switch (c) {
25404653Sdougm 		case 'n':
25414653Sdougm 			dryrun++;
25424653Sdougm 			break;
25434653Sdougm 		case 'v':
25444653Sdougm 			verbose++;
25454653Sdougm 			break;
25464653Sdougm 		case 'd':
25474653Sdougm 			description = optarg;
25484653Sdougm 			break;
25494653Sdougm 		case 'r':
25505331Samw 			if (rsrcname != NULL) {
25515331Samw 				(void) printf(gettext("Adding multiple "
25525331Samw 				    "resource names not"
25535331Samw 				    " supported\n"));
25545331Samw 				return (SA_SYNTAX_ERR);
25555331Samw 			}
25565331Samw 			rsrcname = optarg;
25574653Sdougm 			break;
25584653Sdougm 		case 's':
25594653Sdougm 			/*
25604653Sdougm 			 * Save share path into group. Currently limit
25614653Sdougm 			 * to one share per command.
25624653Sdougm 			 */
25634653Sdougm 			if (sharepath != NULL) {
25644653Sdougm 				(void) printf(gettext(
25654653Sdougm 				    "Adding multiple shares not supported\n"));
25665331Samw 				return (SA_SYNTAX_ERR);
25674653Sdougm 			}
25684653Sdougm 			sharepath = optarg;
25694653Sdougm 			break;
25704653Sdougm 		case 't':
25714653Sdougm 			persist = SA_SHARE_TRANSIENT;
25724653Sdougm 			break;
25736019Sdougm 		case 'h':
25746019Sdougm 			/* optopt on valid arg isn't defined */
25756019Sdougm 			optopt = c;
25766019Sdougm 			/*FALLTHROUGH*/
25776019Sdougm 		case '?':
25784653Sdougm 		default:
25796019Sdougm 			/*
25806019Sdougm 			 * Since a bad option gets to here, sort it
25816019Sdougm 			 * out and return a syntax error return value
25826019Sdougm 			 * if necessary.
25836019Sdougm 			 */
25846019Sdougm 			switch (optopt) {
25856019Sdougm 			default:
25866019Sdougm 				ret = SA_SYNTAX_ERR;
25876019Sdougm 				break;
25886019Sdougm 			case 'h':
25896019Sdougm 			case '?':
25906019Sdougm 				break;
25916019Sdougm 			}
25924653Sdougm 			(void) printf(gettext("usage: %s\n"),
25934653Sdougm 			    sa_get_usage(USAGE_ADD_SHARE));
25946019Sdougm 			return (ret);
25953034Sdougm 		}
25963034Sdougm 	}
25973034Sdougm 
25983034Sdougm 	if (optind >= argc) {
25994653Sdougm 		(void) printf(gettext("usage: %s\n"),
26004653Sdougm 		    sa_get_usage(USAGE_ADD_SHARE));
26014653Sdougm 		if (dryrun || sharepath != NULL || description != NULL ||
26025331Samw 		    rsrcname != NULL || verbose || persist) {
26034653Sdougm 			(void) printf(gettext("\tgroup must be specified\n"));
26044653Sdougm 			ret = SA_NO_SUCH_GROUP;
26054653Sdougm 		} else {
26064653Sdougm 			ret = SA_OK;
26074653Sdougm 		}
26083034Sdougm 	} else {
26094653Sdougm 		if (sharepath == NULL) {
26104653Sdougm 			(void) printf(gettext("usage: %s\n"),
26114653Sdougm 			    sa_get_usage(USAGE_ADD_SHARE));
26124653Sdougm 			(void) printf(gettext(
26134653Sdougm 			    "\t-s sharepath must be specified\n"));
26145331Samw 			ret = SA_BAD_PATH;
26154653Sdougm 		}
26165331Samw 		if (ret == SA_OK) {
26175331Samw 			if (realpath(sharepath, dir) == NULL) {
26185331Samw 				ret = SA_BAD_PATH;
26195331Samw 				(void) printf(gettext("Path "
26205331Samw 				    "is not valid: %s\n"),
26215331Samw 				    sharepath);
26225331Samw 			} else {
26235331Samw 				sharepath = dir;
26245331Samw 			}
26253034Sdougm 		}
26265331Samw 		if (ret == SA_OK && rsrcname != NULL) {
26275331Samw 			/* check for valid syntax */
26285331Samw 			if (validresource(rsrcname)) {
26295331Samw 				rsrc = conv_to_utf8(rsrcname);
26305331Samw 				resource = sa_find_resource(handle, rsrc);
26315331Samw 				if (resource != NULL) {
26325331Samw 					/*
26335331Samw 					 * Resource names must be
26345331Samw 					 * unique in the system
26355331Samw 					 */
26365331Samw 					ret = SA_DUPLICATE_NAME;
26375331Samw 					(void) printf(gettext("usage: %s\n"),
26385331Samw 					    sa_get_usage(USAGE_ADD_SHARE));
26395331Samw 					(void) printf(gettext(
26405331Samw 					    "\tresource names must be unique "
26415331Samw 					    "in the system\n"));
26425331Samw 				}
26435331Samw 			} else {
26445331Samw 				(void) printf(gettext("usage: %s\n"),
26455331Samw 				    sa_get_usage(USAGE_ADD_SHARE));
26465331Samw 				(void) printf(gettext(
26475331Samw 				    "\tresource names use restricted "
26485331Samw 				    "character set\n"));
26495331Samw 				ret = SA_INVALID_NAME;
26505331Samw 			}
26513034Sdougm 		}
26525331Samw 
26535331Samw 		if (ret != SA_OK) {
26545331Samw 			if (rsrc != NULL && rsrcname != rsrc)
26555331Samw 				sa_free_attr_string(rsrc);
26565331Samw 			return (ret);
26574653Sdougm 		}
26585331Samw 
26594653Sdougm 		share = sa_find_share(handle, sharepath);
26604653Sdougm 		if (share != NULL) {
26615331Samw 			if (rsrcname == NULL) {
26625331Samw 				/*
26635331Samw 				 * Can only have a duplicate share if a new
26645331Samw 				 * resource name is being added.
26655331Samw 				 */
26665331Samw 				ret = SA_DUPLICATE_NAME;
26675331Samw 				(void) printf(gettext("Share path already "
26685331Samw 				    "shared: %s\n"), sharepath);
26695331Samw 			}
26705331Samw 		}
26715331Samw 		if (ret != SA_OK)
26725331Samw 			return (ret);
26735331Samw 
26745331Samw 		group = sa_get_group(handle, argv[optind]);
26755331Samw 		if (group != NULL) {
26765331Samw 			if (sa_require_resource(group) == B_TRUE &&
26775331Samw 			    rsrcname == NULL) {
26785331Samw 				(void) printf(gettext(
26795331Samw 				    "Resource name is required "
26805331Samw 				    "by at least one enabled protocol "
26815331Samw 				    "in group\n"));
26825331Samw 				return (SA_RESOURCE_REQUIRED);
26835331Samw 			}
26845331Samw 			if (share == NULL && ret == SA_OK) {
26855331Samw 				if (dryrun)
26865331Samw 					ret = sa_check_path(group, sharepath,
26875331Samw 					    SA_CHECK_NORMAL);
26885331Samw 				else
26895331Samw 					share = sa_add_share(group, sharepath,
26905331Samw 					    persist, &ret);
26915331Samw 			}
26925331Samw 			/*
26935331Samw 			 * Make sure this isn't an attempt to put a resourced
26945331Samw 			 * share into a different group than it already is in.
26955331Samw 			 */
26965331Samw 			if (share != NULL) {
26975331Samw 				sa_group_t parent;
26985331Samw 				parent = sa_get_parent_group(share);
26995331Samw 				if (parent != group) {
27005331Samw 					ret = SA_DUPLICATE_NAME;
27014653Sdougm 					(void) printf(gettext(
27024653Sdougm 					    "Share path already "
27035331Samw 					    "shared: %s\n"), sharepath);
27044653Sdougm 				}
27053034Sdougm 			}
27063034Sdougm 			if (!dryrun && share == NULL) {
27074653Sdougm 				(void) printf(gettext(
27084653Sdougm 				    "Could not add share: %s\n"),
27094653Sdougm 				    sa_errorstr(ret));
27103034Sdougm 			} else {
27115331Samw 				auth = check_authorizations(argv[optind],
27125331Samw 				    flags);
27134653Sdougm 				if (!dryrun && ret == SA_OK) {
27145331Samw 					if (rsrcname != NULL) {
27155331Samw 						resource = sa_add_resource(
27165331Samw 						    share,
27175331Samw 						    rsrc,
27185331Samw 						    SA_SHARE_PERMANENT,
27195331Samw 						    &ret);
27204653Sdougm 					}
27214653Sdougm 					if (ret == SA_OK &&
27224653Sdougm 					    description != NULL) {
27235885Sdougm 						if (resource != NULL)
27245885Sdougm 							ret =
27255885Sdougm 							    set_resource_desc(
27265885Sdougm 							    resource,
27275885Sdougm 							    description);
27285885Sdougm 						else
27295331Samw 							ret =
27305331Samw 							    set_share_desc(
27315331Samw 							    share,
27325331Samw 							    description);
27334653Sdougm 					}
27344653Sdougm 					if (ret == SA_OK) {
27355331Samw 						/* now enable the share(s) */
27365331Samw 						if (resource != NULL) {
27375331Samw 							ret = enable_share(
27385331Samw 							    handle,
27395331Samw 							    group,
27405331Samw 							    resource,
27415331Samw 							    1);
27425331Samw 						} else {
27435331Samw 							ret = enable_share(
27445331Samw 							    handle,
27455331Samw 							    group,
27465331Samw 							    share,
27475331Samw 							    1);
27485331Samw 						}
27494653Sdougm 						ret = sa_update_config(handle);
27504653Sdougm 					}
27514653Sdougm 					switch (ret) {
27524653Sdougm 					case SA_DUPLICATE_NAME:
27534653Sdougm 						(void) printf(gettext(
27544653Sdougm 						    "Resource name in"
27555331Samw 						    "use: %s\n"),
27565331Samw 						    rsrcname);
27574653Sdougm 						break;
27584653Sdougm 					default:
27595331Samw 						(void) printf(gettext(
27605331Samw 						    "Could not set "
27614653Sdougm 						    "attribute: %s\n"),
27624653Sdougm 						    sa_errorstr(ret));
27634653Sdougm 						break;
27644653Sdougm 					case SA_OK:
27654653Sdougm 						break;
27664653Sdougm 					}
27675331Samw 				} else if (dryrun && ret == SA_OK &&
27685331Samw 				    !auth && verbose) {
27694653Sdougm 					(void) printf(gettext(
27704653Sdougm 					    "Command would fail: %s\n"),
27714653Sdougm 					    sa_errorstr(SA_NO_PERMISSION));
27724653Sdougm 					ret = SA_NO_PERMISSION;
27733034Sdougm 				}
27743034Sdougm 			}
27755331Samw 		} else {
27765331Samw 			switch (ret) {
27775331Samw 			default:
27785331Samw 				(void) printf(gettext(
27795331Samw 				    "Group \"%s\" not found\n"), argv[optind]);
27805331Samw 				ret = SA_NO_SUCH_GROUP;
27815331Samw 				break;
27825331Samw 			case SA_BAD_PATH:
27835331Samw 			case SA_DUPLICATE_NAME:
27845331Samw 				break;
27855331Samw 			}
27863034Sdougm 		}
27873034Sdougm 	}
27883034Sdougm 	return (ret);
27893034Sdougm }
27903034Sdougm 
27913034Sdougm /*
27923034Sdougm  * sa_moveshare(flags, argc, argv)
27933034Sdougm  *
27943034Sdougm  * implements move-share subcommand.
27953034Sdougm  */
27963034Sdougm 
27973034Sdougm int
27983910Sdougm sa_moveshare(sa_handle_t handle, int flags, int argc, char *argv[])
27993034Sdougm {
28003034Sdougm 	int verbose = 0;
28013034Sdougm 	int dryrun = 0;
28023034Sdougm 	int c;
28033034Sdougm 	int ret = SA_OK;
28043034Sdougm 	sa_group_t group;
28053034Sdougm 	sa_share_t share;
28065331Samw 	char *rsrcname = NULL;
28073034Sdougm 	char *sharepath = NULL;
28083034Sdougm 	int authsrc = 0, authdst = 0;
28095885Sdougm 	char dir[MAXPATHLEN];
28103034Sdougm 
28115331Samw 	while ((c = getopt(argc, argv, "?hvnr:s:")) != EOF) {
28124653Sdougm 		switch (c) {
28134653Sdougm 		case 'n':
28144653Sdougm 			dryrun++;
28154653Sdougm 			break;
28164653Sdougm 		case 'v':
28174653Sdougm 			verbose++;
28184653Sdougm 			break;
28195331Samw 		case 'r':
28205331Samw 			if (rsrcname != NULL) {
28215331Samw 				(void) printf(gettext(
28225331Samw 				    "Moving multiple resource names not"
28235331Samw 				    " supported\n"));
28245331Samw 				return (SA_SYNTAX_ERR);
28255331Samw 			}
28265331Samw 			rsrcname = optarg;
28275331Samw 			break;
28284653Sdougm 		case 's':
28294653Sdougm 			/*
28304653Sdougm 			 * Remove share path from group. Currently limit
28314653Sdougm 			 * to one share per command.
28324653Sdougm 			 */
28334653Sdougm 			if (sharepath != NULL) {
28344653Sdougm 				(void) printf(gettext("Moving multiple shares"
28355331Samw 				    " not supported\n"));
28365331Samw 				return (SA_SYNTAX_ERR);
28374653Sdougm 			}
28384653Sdougm 			sharepath = optarg;
28394653Sdougm 			break;
28406019Sdougm 		case 'h':
28416019Sdougm 			/* optopt on valid arg isn't defined */
28426019Sdougm 			optopt = c;
28436019Sdougm 			/*FALLTHROUGH*/
28446019Sdougm 		case '?':
28454653Sdougm 		default:
28466019Sdougm 			/*
28476019Sdougm 			 * Since a bad option gets to here, sort it
28486019Sdougm 			 * out and return a syntax error return value
28496019Sdougm 			 * if necessary.
28506019Sdougm 			 */
28516019Sdougm 			switch (optopt) {
28526019Sdougm 			default:
28536019Sdougm 				ret = SA_SYNTAX_ERR;
28546019Sdougm 				break;
28556019Sdougm 			case 'h':
28566019Sdougm 			case '?':
28576019Sdougm 				break;
28586019Sdougm 			}
28594653Sdougm 			(void) printf(gettext("usage: %s\n"),
28604653Sdougm 			    sa_get_usage(USAGE_MOVE_SHARE));
28616019Sdougm 			return (ret);
28623034Sdougm 		}
28633034Sdougm 	}
28643034Sdougm 
28653034Sdougm 	if (optind >= argc || sharepath == NULL) {
28665331Samw 		(void) printf(gettext("usage: %s\n"),
28675331Samw 		    sa_get_usage(USAGE_MOVE_SHARE));
28685331Samw 		if (dryrun || verbose || sharepath != NULL) {
28695331Samw 			(void) printf(gettext("\tgroup must be specified\n"));
28705331Samw 			ret = SA_NO_SUCH_GROUP;
28715331Samw 		} else {
28725331Samw 			if (sharepath == NULL) {
28735331Samw 				ret = SA_SYNTAX_ERR;
28744653Sdougm 				(void) printf(gettext(
28755331Samw 				    "\tsharepath must be specified\n"));
28764653Sdougm 			} else {
28775331Samw 				ret = SA_OK;
28784653Sdougm 			}
28795331Samw 		}
28804653Sdougm 	} else {
28814653Sdougm 		sa_group_t parent;
28824653Sdougm 		char *zfsold;
28834653Sdougm 		char *zfsnew;
28844653Sdougm 
28853034Sdougm 		if (sharepath == NULL) {
28864653Sdougm 			(void) printf(gettext(
28874653Sdougm 			    "sharepath must be specified with the -s "
28884653Sdougm 			    "option\n"));
28894653Sdougm 			return (SA_BAD_PATH);
28904653Sdougm 		}
28913910Sdougm 		group = sa_get_group(handle, argv[optind]);
28924653Sdougm 		if (group == NULL) {
28934653Sdougm 			(void) printf(gettext("Group \"%s\" not found\n"),
28944653Sdougm 			    argv[optind]);
28954653Sdougm 			return (SA_NO_SUCH_GROUP);
28964653Sdougm 		}
28974653Sdougm 		share = sa_find_share(handle, sharepath);
28985885Sdougm 		/*
28995885Sdougm 		 * If a share wasn't found, it may have been a symlink
29005885Sdougm 		 * or has a trailing '/'. Try again after resolving
29015885Sdougm 		 * with realpath().
29025885Sdougm 		 */
29035885Sdougm 		if (share == NULL) {
29045885Sdougm 			if (realpath(sharepath, dir) == NULL) {
29055885Sdougm 				(void) printf(gettext("Path "
29065885Sdougm 				    "is not valid: %s\n"),
29075885Sdougm 				    sharepath);
29085885Sdougm 				return (SA_BAD_PATH);
29095885Sdougm 			}
29105885Sdougm 			sharepath = dir;
29115885Sdougm 			share = sa_find_share(handle, sharepath);
29125885Sdougm 		}
29134653Sdougm 		if (share == NULL) {
29143034Sdougm 			(void) printf(gettext("Share not found: %s\n"),
29154653Sdougm 			    sharepath);
29164653Sdougm 			return (SA_NO_SUCH_PATH);
29174653Sdougm 		}
29185885Sdougm 		authdst = check_authorizations(argv[optind], flags);
29194653Sdougm 
29204653Sdougm 		parent = sa_get_parent_group(share);
29214653Sdougm 		if (parent != NULL) {
29224653Sdougm 			char *pname;
29234653Sdougm 			pname = sa_get_group_attr(parent, "name");
29244653Sdougm 			if (pname != NULL) {
29253034Sdougm 				authsrc = check_authorizations(pname, flags);
29263034Sdougm 				sa_free_attr_string(pname);
29274653Sdougm 			}
29284653Sdougm 			zfsold = sa_get_group_attr(parent, "zfs");
29294653Sdougm 			zfsnew = sa_get_group_attr(group, "zfs");
29304653Sdougm 			if ((zfsold != NULL && zfsnew == NULL) ||
29314653Sdougm 			    (zfsold == NULL && zfsnew != NULL)) {
29323034Sdougm 				ret = SA_NOT_ALLOWED;
29333034Sdougm 			}
29344653Sdougm 			if (zfsold != NULL)
29354653Sdougm 				sa_free_attr_string(zfsold);
29364653Sdougm 			if (zfsnew != NULL)
29374653Sdougm 				sa_free_attr_string(zfsnew);
29384653Sdougm 		}
29394653Sdougm 
29404653Sdougm 		if (ret == SA_OK && parent != group && !dryrun) {
29414653Sdougm 			char *oldstate;
29424653Sdougm 			/*
29434653Sdougm 			 * Note that the share may need to be
29445331Samw 			 * "unshared" if the new group is disabled and
29455331Samw 			 * the old was enabled or it may need to be
29465331Samw 			 * share to update if the new group is
29475331Samw 			 * enabled. We disable before the move and
29485331Samw 			 * will have to enable after the move in order
29495331Samw 			 * to cleanup entries for protocols that
29505331Samw 			 * aren't in the new group.
29514653Sdougm 			 */
29524653Sdougm 			oldstate = sa_get_group_attr(parent, "state");
29534653Sdougm 
29544653Sdougm 			/* enable_share determines what to do */
29555331Samw 			if (strcmp(oldstate, "enabled") == 0)
29563034Sdougm 				(void) sa_disable_share(share, NULL);
29575331Samw 
29584653Sdougm 			if (oldstate != NULL)
29593034Sdougm 				sa_free_attr_string(oldstate);
29603034Sdougm 		}
29614653Sdougm 
29625331Samw 		if (!dryrun && ret == SA_OK)
29635331Samw 			ret = sa_move_share(group, share);
29645331Samw 
29655331Samw 		/*
29665331Samw 		 * Reenable and update any config information.
29675331Samw 		 */
29685331Samw 		if (ret == SA_OK && parent != group && !dryrun) {
29695331Samw 			ret = sa_update_config(handle);
29705331Samw 
29715331Samw 			(void) enable_share(handle, group, share, 1);
29725331Samw 		}
29735331Samw 
29744653Sdougm 		if (ret != SA_OK)
29754653Sdougm 			(void) printf(gettext("Could not move share: %s\n"),
29764653Sdougm 			    sa_errorstr(ret));
29774653Sdougm 
29784653Sdougm 		if (dryrun && ret == SA_OK && !(authsrc & authdst) &&
29794653Sdougm 		    verbose) {
29804653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
29814653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
29824653Sdougm 		}
29833034Sdougm 	}
29843034Sdougm 	return (ret);
29853034Sdougm }
29863034Sdougm 
29873034Sdougm /*
29883034Sdougm  * sa_removeshare(flags, argc, argv)
29893034Sdougm  *
29903034Sdougm  * implements remove-share subcommand.
29913034Sdougm  */
29923034Sdougm 
29933034Sdougm int
29943910Sdougm sa_removeshare(sa_handle_t handle, int flags, int argc, char *argv[])
29953034Sdougm {
29963034Sdougm 	int verbose = 0;
29973034Sdougm 	int dryrun = 0;
29983034Sdougm 	int force = 0;
29993034Sdougm 	int c;
30003034Sdougm 	int ret = SA_OK;
30013034Sdougm 	sa_group_t group;
30025331Samw 	sa_resource_t resource = NULL;
30035331Samw 	sa_share_t share = NULL;
30045331Samw 	char *rsrcname = NULL;
30053034Sdougm 	char *sharepath = NULL;
30063034Sdougm 	char dir[MAXPATHLEN];
30073034Sdougm 	int auth;
30083034Sdougm 
30095331Samw 	while ((c = getopt(argc, argv, "?hfnr:s:v")) != EOF) {
30104653Sdougm 		switch (c) {
30114653Sdougm 		case 'n':
30124653Sdougm 			dryrun++;
30134653Sdougm 			break;
30144653Sdougm 		case 'v':
30154653Sdougm 			verbose++;
30164653Sdougm 			break;
30174653Sdougm 		case 'f':
30184653Sdougm 			force++;
30194653Sdougm 			break;
30204653Sdougm 		case 's':
30214653Sdougm 			/*
30224653Sdougm 			 * Remove share path from group. Currently limit
30234653Sdougm 			 * to one share per command.
30244653Sdougm 			 */
30254653Sdougm 			if (sharepath != NULL) {
30264653Sdougm 				(void) printf(gettext(
30274653Sdougm 				    "Removing multiple shares not "
30283034Sdougm 				    "supported\n"));
30294653Sdougm 				return (SA_SYNTAX_ERR);
30304653Sdougm 			}
30314653Sdougm 			sharepath = optarg;
30324653Sdougm 			break;
30335331Samw 		case 'r':
30345331Samw 			/*
30355331Samw 			 * Remove share from group if last resource or remove
30365331Samw 			 * resource from share if multiple resources.
30375331Samw 			 */
30385331Samw 			if (rsrcname != NULL) {
30395331Samw 				(void) printf(gettext(
30405331Samw 				    "Removing multiple resource names not "
30415331Samw 				    "supported\n"));
30425331Samw 				return (SA_SYNTAX_ERR);
30435331Samw 			}
30445331Samw 			rsrcname = optarg;
30455331Samw 			break;
30466019Sdougm 		case 'h':
30476019Sdougm 			/* optopt on valid arg isn't defined */
30486019Sdougm 			optopt = c;
30496019Sdougm 			/*FALLTHROUGH*/
30506019Sdougm 		case '?':
30514653Sdougm 		default:
30526019Sdougm 			/*
30536019Sdougm 			 * Since a bad option gets to here, sort it
30546019Sdougm 			 * out and return a syntax error return value
30556019Sdougm 			 * if necessary.
30566019Sdougm 			 */
30576019Sdougm 			switch (optopt) {
30586019Sdougm 			default:
30596019Sdougm 				ret = SA_SYNTAX_ERR;
30606019Sdougm 				break;
30616019Sdougm 			case 'h':
30626019Sdougm 			case '?':
30636019Sdougm 				break;
30646019Sdougm 			}
30654653Sdougm 			(void) printf(gettext("usage: %s\n"),
30664653Sdougm 			    sa_get_usage(USAGE_REMOVE_SHARE));
30676019Sdougm 			return (ret);
30683034Sdougm 		}
30693034Sdougm 	}
30703034Sdougm 
30715331Samw 	if (optind >= argc || (rsrcname == NULL && sharepath == NULL)) {
30725331Samw 		if (sharepath == NULL && rsrcname == NULL) {
30733034Sdougm 			(void) printf(gettext("usage: %s\n"),
30744653Sdougm 			    sa_get_usage(USAGE_REMOVE_SHARE));
30755331Samw 			(void) printf(gettext("\t-s sharepath or -r resource"
30765331Samw 			    " must be specified\n"));
30774653Sdougm 			ret = SA_BAD_PATH;
30784653Sdougm 		} else {
30794653Sdougm 			ret = SA_OK;
30804653Sdougm 		}
30813034Sdougm 	}
30824653Sdougm 	if (ret != SA_OK) {
30834653Sdougm 		return (ret);
30844653Sdougm 	}
30854653Sdougm 
30864653Sdougm 	if (optind < argc) {
30873034Sdougm 		if ((optind + 1) < argc) {
30884653Sdougm 			(void) printf(gettext("Extraneous group(s) at end of "
30894653Sdougm 			    "command\n"));
30904653Sdougm 			ret = SA_SYNTAX_ERR;
30913034Sdougm 		} else {
30924653Sdougm 			group = sa_get_group(handle, argv[optind]);
30934653Sdougm 			if (group == NULL) {
30944653Sdougm 				(void) printf(gettext(
30954653Sdougm 				    "Group \"%s\" not found\n"), argv[optind]);
30964653Sdougm 				ret = SA_NO_SUCH_GROUP;
30974653Sdougm 			}
30983034Sdougm 		}
30994653Sdougm 	} else {
31003034Sdougm 		group = NULL;
31014653Sdougm 	}
31024653Sdougm 
31035331Samw 	if (rsrcname != NULL) {
31045331Samw 		resource = sa_find_resource(handle, rsrcname);
31055331Samw 		if (resource == NULL) {
31065331Samw 			ret = SA_NO_SUCH_RESOURCE;
31075331Samw 			(void) printf(gettext(
31085331Samw 			    "Resource name not found for share: %s\n"),
31095331Samw 			    rsrcname);
31105331Samw 		}
31115331Samw 	}
31125331Samw 
31134653Sdougm 	/*
31144653Sdougm 	 * Lookup the path in the internal configuration. Care
31154653Sdougm 	 * must be taken to handle the case where the
31164653Sdougm 	 * underlying path has been removed since we need to
31174653Sdougm 	 * be able to deal with that as well.
31184653Sdougm 	 */
31194653Sdougm 	if (ret == SA_OK) {
31205331Samw 		if (sharepath != NULL) {
31215331Samw 			if (group != NULL)
31225331Samw 				share = sa_get_share(group, sharepath);
31235331Samw 			else
31245331Samw 				share = sa_find_share(handle, sharepath);
31255331Samw 		}
31265331Samw 
31275331Samw 		if (resource != NULL) {
31285331Samw 			sa_share_t rsrcshare;
31295331Samw 			rsrcshare = sa_get_resource_parent(resource);
31305331Samw 			if (share == NULL)
31315331Samw 				share = rsrcshare;
31325331Samw 			else if (share != rsrcshare) {
31335331Samw 				ret = SA_NO_SUCH_RESOURCE;
31345331Samw 				(void) printf(gettext(
31355331Samw 				    "Bad resource name for share: %s\n"),
31365331Samw 				    rsrcname);
31375331Samw 				share = NULL;
31385331Samw 			}
31395331Samw 		}
31405331Samw 
31413663Sdougm 		/*
31423663Sdougm 		 * If we didn't find the share with the provided path,
31433663Sdougm 		 * it may be a symlink so attempt to resolve it using
31443663Sdougm 		 * realpath and try again. Realpath will resolve any
31453663Sdougm 		 * symlinks and place them in "dir". Note that
31463663Sdougm 		 * sharepath is only used for the lookup the first
31473663Sdougm 		 * time and later for error messages. dir will be used
31483663Sdougm 		 * on the second attempt. Once a share is found, all
31493663Sdougm 		 * operations are based off of the share variable.
31503663Sdougm 		 */
31513663Sdougm 		if (share == NULL) {
31524653Sdougm 			if (realpath(sharepath, dir) == NULL) {
31534653Sdougm 				ret = SA_BAD_PATH;
31544653Sdougm 				(void) printf(gettext(
31554653Sdougm 				    "Path is not valid: %s\n"), sharepath);
31564653Sdougm 			} else {
31574653Sdougm 				if (group != NULL)
31584653Sdougm 					share = sa_get_share(group, dir);
31594653Sdougm 				else
31604653Sdougm 					share = sa_find_share(handle, dir);
31614653Sdougm 			}
31623663Sdougm 		}
31634653Sdougm 	}
31644653Sdougm 
31654653Sdougm 	/*
31664653Sdougm 	 * If there hasn't been an error, there was likely a
31674653Sdougm 	 * path found. If not, give the appropriate error
31684653Sdougm 	 * message and set the return error. If it was found,
31694653Sdougm 	 * then disable the share and then remove it from the
31704653Sdougm 	 * configuration.
31714653Sdougm 	 */
31724653Sdougm 	if (ret != SA_OK) {
31734653Sdougm 		return (ret);
31744653Sdougm 	}
31754653Sdougm 	if (share == NULL) {
31764653Sdougm 		if (group != NULL)
31773034Sdougm 			(void) printf(gettext("Share not found in group %s:"
31784653Sdougm 			    " %s\n"), argv[optind], sharepath);
31794653Sdougm 		else
31803034Sdougm 			(void) printf(gettext("Share not found: %s\n"),
31814653Sdougm 			    sharepath);
31825331Samw 		ret = SA_NO_SUCH_PATH;
31834653Sdougm 	} else {
31844653Sdougm 		if (group == NULL)
31853034Sdougm 			group = sa_get_parent_group(share);
31864653Sdougm 		if (!dryrun) {
31873034Sdougm 			if (ret == SA_OK) {
31885331Samw 				if (resource != NULL)
31895331Samw 					ret = sa_disable_resource(resource,
31905331Samw 					    NULL);
31915331Samw 				else
31925331Samw 					ret = sa_disable_share(share, NULL);
31933034Sdougm 				/*
31944653Sdougm 				 * We don't care if it fails since it
31953663Sdougm 				 * could be disabled already. Some
31963663Sdougm 				 * unexpected errors could occur that
31973663Sdougm 				 * prevent removal, so also check for
31983663Sdougm 				 * force being set.
31993034Sdougm 				 */
32005331Samw 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
32015331Samw 				    ret == SA_NOT_SUPPORTED ||
32025331Samw 				    ret == SA_SYSTEM_ERR || force) &&
32035331Samw 				    resource == NULL)
32045331Samw 					ret = sa_remove_share(share);
32055331Samw 
32065331Samw 				if ((ret == SA_OK || ret == SA_NO_SUCH_PATH ||
32074653Sdougm 				    ret == SA_NOT_SUPPORTED ||
32085331Samw 				    ret == SA_SYSTEM_ERR || force) &&
32095331Samw 				    resource != NULL) {
32105331Samw 					ret = sa_remove_resource(resource);
32115331Samw 					if (ret == SA_OK) {
32125331Samw 						/*
32135331Samw 						 * If this was the
32145331Samw 						 * last one, remove
32155331Samw 						 * the share as well.
32165331Samw 						 */
32175331Samw 						resource =
32185331Samw 						    sa_get_share_resource(
32195331Samw 						    share, NULL);
32205331Samw 						if (resource == NULL)
32215331Samw 							ret = sa_remove_share(
32225331Samw 							    share);
32235331Samw 					}
32244653Sdougm 				}
32254653Sdougm 				if (ret == SA_OK)
32264653Sdougm 					ret = sa_update_config(handle);
32273034Sdougm 			}
32284653Sdougm 			if (ret != SA_OK)
32295331Samw 				(void) printf(gettext("Could not remove share:"
32305331Samw 				    " %s\n"), sa_errorstr(ret));
32314653Sdougm 		} else if (ret == SA_OK) {
32323034Sdougm 			char *pname;
32333034Sdougm 			pname = sa_get_group_attr(group, "name");
32343034Sdougm 			if (pname != NULL) {
32354653Sdougm 				auth = check_authorizations(pname, flags);
32364653Sdougm 				sa_free_attr_string(pname);
32373034Sdougm 			}
32383034Sdougm 			if (!auth && verbose) {
32394653Sdougm 				(void) printf(gettext(
32404653Sdougm 				    "Command would fail: %s\n"),
32414653Sdougm 				    sa_errorstr(SA_NO_PERMISSION));
32423034Sdougm 			}
32433034Sdougm 		}
32443034Sdougm 	}
32453034Sdougm 	return (ret);
32463034Sdougm }
32473034Sdougm 
32483034Sdougm /*
32493034Sdougm  * sa_set_share(flags, argc, argv)
32503034Sdougm  *
32513034Sdougm  * implements set-share subcommand.
32523034Sdougm  */
32533034Sdougm 
32543034Sdougm int
32553910Sdougm sa_set_share(sa_handle_t handle, int flags, int argc, char *argv[])
32563034Sdougm {
32573034Sdougm 	int dryrun = 0;
32583034Sdougm 	int c;
32593034Sdougm 	int ret = SA_OK;
32603034Sdougm 	sa_group_t group, sharegroup;
32615772Sas200622 	sa_share_t share = NULL;
32625331Samw 	sa_resource_t resource = NULL;
32633034Sdougm 	char *sharepath = NULL;
32643034Sdougm 	char *description = NULL;
32655331Samw 	char *rsrcname = NULL;
32665331Samw 	char *rsrc = NULL;
32675331Samw 	char *newname = NULL;
32685331Samw 	char *newrsrc;
32695331Samw 	char *groupname = NULL;
32703034Sdougm 	int auth;
32713034Sdougm 	int verbose = 0;
32723034Sdougm 
32733034Sdougm 	while ((c = getopt(argc, argv, "?hnd:r:s:")) != EOF) {
32744653Sdougm 		switch (c) {
32754653Sdougm 		case 'n':
32764653Sdougm 			dryrun++;
32774653Sdougm 			break;
32784653Sdougm 		case 'd':
32794653Sdougm 			description = optarg;
32804653Sdougm 			break;
32814653Sdougm 		case 'v':
32824653Sdougm 			verbose++;
32834653Sdougm 			break;
32845331Samw 		case 'r':
32855331Samw 			/*
32865331Samw 			 * Update share by resource name
32875331Samw 			 */
32885331Samw 			if (rsrcname != NULL) {
32895331Samw 				(void) printf(gettext(
32905331Samw 				    "Updating multiple resource names not "
32915331Samw 				    "supported\n"));
32925331Samw 				return (SA_SYNTAX_ERR);
32935331Samw 			}
32945331Samw 			rsrcname = optarg;
32955331Samw 			break;
32964653Sdougm 		case 's':
32974653Sdougm 			/*
32984653Sdougm 			 * Save share path into group. Currently limit
32994653Sdougm 			 * to one share per command.
33004653Sdougm 			 */
33014653Sdougm 			if (sharepath != NULL) {
33024653Sdougm 				(void) printf(gettext(
33034653Sdougm 				    "Updating multiple shares not "
33043034Sdougm 				    "supported\n"));
33055331Samw 				return (SA_SYNTAX_ERR);
33064653Sdougm 			}
33074653Sdougm 			sharepath = optarg;
33084653Sdougm 			break;
33096019Sdougm 		case 'h':
33106019Sdougm 			/* optopt on valid arg isn't defined */
33116019Sdougm 			optopt = c;
33126019Sdougm 			/*FALLTHROUGH*/
33136019Sdougm 		case '?':
33144653Sdougm 		default:
33156019Sdougm 			/*
33166019Sdougm 			 * Since a bad option gets to here, sort it
33176019Sdougm 			 * out and return a syntax error return value
33186019Sdougm 			 * if necessary.
33196019Sdougm 			 */
33206019Sdougm 			switch (optopt) {
33216019Sdougm 			default:
33226019Sdougm 				ret = SA_SYNTAX_ERR;
33236019Sdougm 				break;
33246019Sdougm 			case 'h':
33256019Sdougm 			case '?':
33266019Sdougm 				break;
33276019Sdougm 			}
33284653Sdougm 			(void) printf(gettext("usage: %s\n"),
33294653Sdougm 			    sa_get_usage(USAGE_SET_SHARE));
33306019Sdougm 			return (ret);
33313034Sdougm 		}
33323034Sdougm 	}
33334653Sdougm 
33345331Samw 	if (optind >= argc && sharepath == NULL && rsrcname == NULL) {
33354653Sdougm 		if (sharepath == NULL) {
33364653Sdougm 			(void) printf(gettext("usage: %s\n"),
33374653Sdougm 			    sa_get_usage(USAGE_SET_SHARE));
33384653Sdougm 			(void) printf(gettext("\tgroup must be specified\n"));
33394653Sdougm 			ret = SA_BAD_PATH;
33404653Sdougm 		} else {
33414653Sdougm 			ret = SA_OK;
33424653Sdougm 		}
33433034Sdougm 	}
33443034Sdougm 	if ((optind + 1) < argc) {
33454653Sdougm 		(void) printf(gettext("usage: %s\n"),
33464653Sdougm 		    sa_get_usage(USAGE_SET_SHARE));
33474653Sdougm 		(void) printf(gettext("\tExtraneous group(s) at end\n"));
33484653Sdougm 		ret = SA_SYNTAX_ERR;
33493034Sdougm 	}
33504653Sdougm 
33515331Samw 	/*
33525331Samw 	 * Must have at least one of sharepath and rsrcrname.
33535331Samw 	 * It is a syntax error to be missing both.
33545331Samw 	 */
33555331Samw 	if (sharepath == NULL && rsrcname == NULL) {
33565331Samw 		(void) printf(gettext("usage: %s\n"),
33575331Samw 		    sa_get_usage(USAGE_SET_SHARE));
33585331Samw 		ret = SA_SYNTAX_ERR;
33595331Samw 	}
33605331Samw 
33614653Sdougm 	if (ret != SA_OK)
33624653Sdougm 		return (ret);
33634653Sdougm 
33644653Sdougm 	if (optind < argc) {
33653034Sdougm 		groupname = argv[optind];
33663910Sdougm 		group = sa_get_group(handle, groupname);
33674653Sdougm 	} else {
33683034Sdougm 		group = NULL;
33693034Sdougm 		groupname = NULL;
33704653Sdougm 	}
33715331Samw 	if (rsrcname != NULL) {
33725331Samw 		/*
33735331Samw 		 * If rsrcname exists, split rename syntax and then
33745331Samw 		 * convert to utf 8 if no errors.
33755331Samw 		 */
33765331Samw 		newname = strchr(rsrcname, '=');
33775331Samw 		if (newname != NULL) {
33785331Samw 			*newname++ = '\0';
33795331Samw 		}
33805331Samw 		if (!validresource(rsrcname)) {
33815331Samw 			ret = SA_INVALID_NAME;
33825331Samw 			(void) printf(gettext("Invalid resource name: "
33835331Samw 			    "\"%s\"\n"), rsrcname);
33845331Samw 		} else {
33855331Samw 			rsrc = conv_to_utf8(rsrcname);
33865331Samw 		}
33875331Samw 		if (newname != NULL) {
33885331Samw 			if (!validresource(newname)) {
33895331Samw 				ret = SA_INVALID_NAME;
33905331Samw 				(void) printf(gettext("Invalid resource name: "
33915331Samw 				    "%s\n"), newname);
3392*7637SDoug.McCallum@Sun.COM 				newname = NULL;
33935331Samw 			} else {
33945331Samw 				newrsrc = conv_to_utf8(newname);
33955331Samw 			}
33965331Samw 		}
33975331Samw 	}
33985331Samw 
33995331Samw 	if (ret != SA_OK) {
34005331Samw 		if (rsrcname != NULL && rsrcname != rsrc)
34015331Samw 			sa_free_attr_string(rsrc);
34025331Samw 		if (newname != NULL && newname != newrsrc)
34035331Samw 			sa_free_attr_string(newrsrc);
34045331Samw 		return (ret);
34055331Samw 	}
34065331Samw 
34075331Samw 	if (sharepath != NULL) {
34085331Samw 		share = sa_find_share(handle, sharepath);
34095331Samw 	} else if (rsrcname != NULL) {
34105331Samw 		resource = sa_find_resource(handle, rsrc);
34115772Sas200622 		if (resource != NULL)
34125331Samw 			share = sa_get_resource_parent(resource);
34135772Sas200622 		else
34145772Sas200622 			ret = SA_NO_SUCH_RESOURCE;
34155331Samw 	}
34165331Samw 	if (share != NULL) {
34175331Samw 		sharegroup = sa_get_parent_group(share);
34185331Samw 		if (group != NULL && group != sharegroup) {
34195331Samw 			(void) printf(gettext("Group \"%s\" does not contain "
34205331Samw 			    "share %s\n"),
34215331Samw 			    argv[optind], sharepath);
34225331Samw 			ret = SA_BAD_PATH;
34235331Samw 		} else {
34245331Samw 			int delgroupname = 0;
34255331Samw 			if (groupname == NULL) {
34265331Samw 				groupname = sa_get_group_attr(sharegroup,
34275331Samw 				    "name");
34285331Samw 				delgroupname = 1;
34295331Samw 			}
34305331Samw 			if (groupname != NULL) {
34315331Samw 				auth = check_authorizations(groupname, flags);
34325331Samw 				if (delgroupname) {
34335331Samw 					sa_free_attr_string(groupname);
34345331Samw 					groupname = NULL;
34355331Samw 				}
34365331Samw 			} else {
34375331Samw 				ret = SA_NO_MEMORY;
34385331Samw 			}
34395331Samw 			if (rsrcname != NULL) {
34405331Samw 				resource = sa_find_resource(handle, rsrc);
34415331Samw 				if (!dryrun) {
34425331Samw 					if (newname != NULL &&
34435331Samw 					    resource != NULL)
34445331Samw 						ret = sa_rename_resource(
34455331Samw 						    resource, newrsrc);
34465331Samw 					else if (newname != NULL)
34475331Samw 						ret = SA_NO_SUCH_RESOURCE;
34485331Samw 					if (newname != NULL &&
34495331Samw 					    newname != newrsrc)
34505331Samw 						sa_free_attr_string(newrsrc);
34515331Samw 				}
34525331Samw 				if (rsrc != rsrcname)
34535331Samw 					sa_free_attr_string(rsrc);
34545331Samw 			}
34555331Samw 
34565331Samw 			/*
34575331Samw 			 * If the user has set a description, it will be
34585331Samw 			 * on the resource if -r was used otherwise it
34595331Samw 			 * must be on the share.
34605331Samw 			 */
34615967Scp160787 			if (!dryrun && ret == SA_OK && description != NULL) {
34625967Scp160787 				char *desc;
34635967Scp160787 				desc = conv_to_utf8(description);
34645331Samw 				if (resource != NULL)
34655967Scp160787 					ret = sa_set_resource_description(
34665967Scp160787 					    resource, desc);
34675331Samw 				else
34685967Scp160787 					ret = sa_set_share_description(share,
34695967Scp160787 					    desc);
34705967Scp160787 				if (desc != description)
34715967Scp160787 					sa_free_share_description(desc);
34725331Samw 			}
34735331Samw 		}
34745331Samw 		if (!dryrun && ret == SA_OK) {
34755331Samw 			if (resource != NULL)
34765331Samw 				(void) sa_enable_resource(resource, NULL);
34775331Samw 			ret = sa_update_config(handle);
34785331Samw 		}
34795331Samw 		switch (ret) {
34805331Samw 		case SA_DUPLICATE_NAME:
34815331Samw 			(void) printf(gettext("Resource name in use: %s\n"),
34825331Samw 			    rsrcname);
34835331Samw 			break;
34845331Samw 		default:
34855331Samw 			(void) printf(gettext("Could not set: %s\n"),
34865331Samw 			    sa_errorstr(ret));
34875331Samw 			break;
34885331Samw 		case SA_OK:
34895331Samw 			if (dryrun && !auth && verbose) {
34905331Samw 				(void) printf(gettext(
34915331Samw 				    "Command would fail: %s\n"),
34925331Samw 				    sa_errorstr(SA_NO_PERMISSION));
34935331Samw 			}
34945331Samw 			break;
34955331Samw 		}
34965331Samw 	} else {
34975772Sas200622 		switch (ret) {
34985772Sas200622 		case SA_NO_SUCH_RESOURCE:
34995772Sas200622 			(void) printf(gettext("Resource \"%s\" not found\n"),
35005772Sas200622 			    rsrcname);
35015772Sas200622 			break;
35025772Sas200622 		default:
35035772Sas200622 			if (sharepath != NULL) {
35045772Sas200622 				(void) printf(
35055772Sas200622 				    gettext("Share path \"%s\" not found\n"),
35065772Sas200622 				    sharepath);
35075772Sas200622 				ret = SA_NO_SUCH_PATH;
35085772Sas200622 			} else {
35095772Sas200622 				(void) printf(gettext("Set failed: %s\n"),
35105772Sas200622 				    sa_errorstr(ret));
35115772Sas200622 			}
35125772Sas200622 		}
35133034Sdougm 	}
35144653Sdougm 
35153034Sdougm 	return (ret);
35163034Sdougm }
35173034Sdougm 
35183034Sdougm /*
35193034Sdougm  * add_security(group, sectype, optlist, proto, *err)
35203034Sdougm  *
35213034Sdougm  * Helper function to add a security option (named optionset) to the
35223034Sdougm  * group.
35233034Sdougm  */
35243034Sdougm 
35253034Sdougm static int
35263034Sdougm add_security(sa_group_t group, char *sectype,
35275331Samw     struct options *optlist, char *proto, int *err)
35283034Sdougm {
35293034Sdougm 	sa_security_t security;
35303034Sdougm 	int ret = SA_OK;
35313034Sdougm 	int result = 0;
35326214Sdougm 	sa_handle_t handle;
35333034Sdougm 
35343034Sdougm 	sectype = sa_proto_space_alias(proto, sectype);
35353034Sdougm 	security = sa_get_security(group, sectype, proto);
35364653Sdougm 	if (security == NULL)
35374653Sdougm 		security = sa_create_security(group, sectype, proto);
35384653Sdougm 
35393034Sdougm 	if (sectype != NULL)
35404653Sdougm 		sa_free_attr_string(sectype);
35414653Sdougm 
35424653Sdougm 	if (security == NULL)
35436214Sdougm 		goto done;
35446214Sdougm 
35456214Sdougm 	handle = sa_find_group_handle(group);
35466214Sdougm 	if (handle == NULL) {
35476214Sdougm 		ret = SA_CONFIG_ERR;
35486214Sdougm 		goto done;
35496214Sdougm 	}
35504653Sdougm 	while (optlist != NULL) {
35513034Sdougm 		sa_property_t prop;
35523034Sdougm 		prop = sa_get_property(security, optlist->optname);
35533034Sdougm 		if (prop == NULL) {
35543034Sdougm 			/*
35554653Sdougm 			 * Add the property, but only if it is
35563034Sdougm 			 * a non-NULL or non-zero length value
35573034Sdougm 			 */
35584653Sdougm 			if (optlist->optvalue != NULL) {
35594653Sdougm 				prop = sa_create_property(optlist->optname,
35604653Sdougm 				    optlist->optvalue);
35614653Sdougm 				if (prop != NULL) {
35626214Sdougm 					ret = sa_valid_property(handle,
35636214Sdougm 					    security, proto, prop);
35644653Sdougm 					if (ret != SA_OK) {
35654653Sdougm 						(void) sa_remove_property(prop);
35664653Sdougm 						(void) printf(gettext(
35674653Sdougm 						    "Could not add "
35684653Sdougm 						    "property %s: %s\n"),
35694653Sdougm 						    optlist->optname,
35704653Sdougm 						    sa_errorstr(ret));
35714653Sdougm 					}
35724653Sdougm 					if (ret == SA_OK) {
35734653Sdougm 						ret = sa_add_property(security,
35744653Sdougm 						    prop);
35754653Sdougm 						if (ret != SA_OK) {
35764653Sdougm 							(void) printf(gettext(
35774653Sdougm 							    "Could not add "
35785331Samw 							    "property (%s=%s):"
35795331Samw 							    " %s\n"),
35804653Sdougm 							    optlist->optname,
35814653Sdougm 							    optlist->optvalue,
35824653Sdougm 							    sa_errorstr(ret));
35834653Sdougm 						} else {
35844653Sdougm 							result = 1;
35854653Sdougm 						}
35864653Sdougm 					}
35873034Sdougm 				}
35883034Sdougm 			}
35893034Sdougm 		} else {
35904653Sdougm 			ret = sa_update_property(prop, optlist->optvalue);
35914653Sdougm 			result = 1; /* should check if really changed */
35923034Sdougm 		}
35933034Sdougm 		optlist = optlist->next;
35944653Sdougm 	}
35954653Sdougm 	/*
35964653Sdougm 	 * When done, properties may have all been removed but
35974653Sdougm 	 * we need to keep the security type itself until
35984653Sdougm 	 * explicitly removed.
35994653Sdougm 	 */
36004653Sdougm 	if (result)
36013034Sdougm 		ret = sa_commit_properties(security, 0);
36026214Sdougm done:
36033034Sdougm 	*err = ret;
36043034Sdougm 	return (result);
36053034Sdougm }
36063034Sdougm 
36073034Sdougm /*
36085089Sdougm  * zfscheck(group, share)
36095089Sdougm  *
36105089Sdougm  * For the special case where a share was provided, make sure it is a
36115089Sdougm  * compatible path for a ZFS property change.  The only path
36125089Sdougm  * acceptable is the path that defines the zfs sub-group (dataset with
36135089Sdougm  * the sharenfs property set) and not one of the paths that inherited
36145089Sdougm  * the NFS properties. Returns SA_OK if it is usable and
36155089Sdougm  * SA_NOT_ALLOWED if it isn't.
36165089Sdougm  *
36175089Sdougm  * If group is not a ZFS group/subgroup, we assume OK since the check
36185089Sdougm  * on return will catch errors for those cases.  What we are looking
36195089Sdougm  * for here is that the group is ZFS and the share is not the defining
36205089Sdougm  * share.  All else is SA_OK.
36215089Sdougm  */
36225089Sdougm 
36235089Sdougm static int
36245089Sdougm zfscheck(sa_group_t group, sa_share_t share)
36255089Sdougm {
36265089Sdougm 	int ret = SA_OK;
36275089Sdougm 	char *attr;
36285089Sdougm 
36295089Sdougm 	if (sa_group_is_zfs(group)) {
36305089Sdougm 		/*
36315089Sdougm 		 * The group is a ZFS group.  Does the share represent
36325089Sdougm 		 * the dataset that defined the group? It is only OK
36335089Sdougm 		 * if the attribute "subgroup" exists on the share and
36345089Sdougm 		 * has a value of "true".
36355089Sdougm 		 */
36365089Sdougm 
36375089Sdougm 		ret = SA_NOT_ALLOWED;
36385089Sdougm 		attr = sa_get_share_attr(share, "subgroup");
36395089Sdougm 		if (attr != NULL) {
36405089Sdougm 			if (strcmp(attr, "true") == 0)
36415089Sdougm 				ret = SA_OK;
36425089Sdougm 			sa_free_attr_string(attr);
36435089Sdougm 		}
36445089Sdougm 	}
36455089Sdougm 	return (ret);
36465089Sdougm }
36475089Sdougm 
36485089Sdougm /*
36495331Samw  * basic_set(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
36503034Sdougm  *
36513034Sdougm  * This function implements "set" when a name space (-S) is not
36523034Sdougm  * specified. It is a basic set. Options and other CLI parsing has
36533034Sdougm  * already been done.
36545331Samw  *
36555331Samw  * "rsrcname" is a "resource name". If it is non-NULL, it must match
36565331Samw  * the sharepath if present or group if present, otherwise it is used
36575331Samw  * to set options.
36585331Samw  *
36595331Samw  * Resource names may take options if the protocol supports it. If the
36605331Samw  * protocol doesn't support resource level options, rsrcname is just
36615331Samw  * an alias for the share.
36623034Sdougm  */
36633034Sdougm 
36643034Sdougm static int
36653910Sdougm basic_set(sa_handle_t handle, char *groupname, struct options *optlist,
36665331Samw     char *protocol, char *sharepath, char *rsrcname, int dryrun)
36673034Sdougm {
36683034Sdougm 	sa_group_t group;
36693034Sdougm 	int ret = SA_OK;
36703034Sdougm 	int change = 0;
36713034Sdougm 	struct list *worklist = NULL;
36723034Sdougm 
36733910Sdougm 	group = sa_get_group(handle, groupname);
36743034Sdougm 	if (group != NULL) {
36754653Sdougm 		sa_share_t share = NULL;
36765331Samw 		sa_resource_t resource = NULL;
36775331Samw 
36785331Samw 		/*
36795331Samw 		 * If there is a sharepath, make sure it belongs to
36805331Samw 		 * the group.
36815331Samw 		 */
36824653Sdougm 		if (sharepath != NULL) {
36834653Sdougm 			share = sa_get_share(group, sharepath);
36844653Sdougm 			if (share == NULL) {
36854653Sdougm 				(void) printf(gettext(
36864653Sdougm 				    "Share does not exist in group %s\n"),
36874653Sdougm 				    groupname, sharepath);
36884653Sdougm 				ret = SA_NO_SUCH_PATH;
36895089Sdougm 			} else {
36905089Sdougm 				/* if ZFS and OK, then only group */
36915089Sdougm 				ret = zfscheck(group, share);
36925089Sdougm 				if (ret == SA_OK &&
36935089Sdougm 				    sa_group_is_zfs(group))
36945089Sdougm 					share = NULL;
36955089Sdougm 				if (ret == SA_NOT_ALLOWED)
36965089Sdougm 					(void) printf(gettext(
36975089Sdougm 					    "Properties on ZFS group shares "
36985089Sdougm 					    "not supported: %s\n"), sharepath);
36994653Sdougm 			}
37003034Sdougm 		}
37015331Samw 
37025331Samw 		/*
37035331Samw 		 * If a resource name exists, make sure it belongs to
37045331Samw 		 * the share if present else it belongs to the
37055331Samw 		 * group. Also check the protocol to see if it
37065331Samw 		 * supports resource level properties or not. If not,
37075331Samw 		 * use share only.
37085331Samw 		 */
37095331Samw 		if (rsrcname != NULL) {
37105331Samw 			if (share != NULL) {
37115331Samw 				resource = sa_get_share_resource(share,
37125331Samw 				    rsrcname);
37135331Samw 				if (resource == NULL)
37145331Samw 					ret = SA_NO_SUCH_RESOURCE;
37155331Samw 			} else {
37165331Samw 				resource = sa_get_resource(group, rsrcname);
37175331Samw 				if (resource != NULL)
37185331Samw 					share = sa_get_resource_parent(
37195331Samw 					    resource);
37205331Samw 				else
37215331Samw 					ret = SA_NO_SUCH_RESOURCE;
37225331Samw 			}
37235331Samw 			if (ret == SA_OK && resource != NULL) {
37245331Samw 				uint64_t features;
37255331Samw 				/*
37265331Samw 				 * Check to see if the resource can take
37275331Samw 				 * properties. If so, stick the resource into
37285331Samw 				 * "share" so it will all just work.
37295331Samw 				 */
37305331Samw 				features = sa_proto_get_featureset(protocol);
37315331Samw 				if (features & SA_FEATURE_RESOURCE)
37325331Samw 					share = (sa_share_t)resource;
37335331Samw 			}
37345331Samw 		}
37355331Samw 
37364653Sdougm 		if (ret == SA_OK) {
37374653Sdougm 			/* group must exist */
37386214Sdougm 			ret = valid_options(handle, optlist, protocol,
37394653Sdougm 			    share == NULL ? group : share, NULL);
37404653Sdougm 			if (ret == SA_OK && !dryrun) {
37414653Sdougm 				if (share != NULL)
37424653Sdougm 					change |= add_optionset(share, optlist,
37434653Sdougm 					    protocol, &ret);
37444653Sdougm 				else
37454653Sdougm 					change |= add_optionset(group, optlist,
37464653Sdougm 					    protocol, &ret);
37474653Sdougm 				if (ret == SA_OK && change)
37484653Sdougm 					worklist = add_list(worklist, group,
37495331Samw 					    share, protocol);
37504653Sdougm 			}
37513034Sdougm 		}
37524653Sdougm 		free_opt(optlist);
37533034Sdougm 	} else {
37543034Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
37553034Sdougm 		ret = SA_NO_SUCH_GROUP;
37563034Sdougm 	}
37573034Sdougm 	/*
37583034Sdougm 	 * we have a group and potentially legal additions
37593034Sdougm 	 */
37603034Sdougm 
37614653Sdougm 	/*
37624653Sdougm 	 * Commit to configuration if not a dryrunp and properties
37634653Sdougm 	 * have changed.
37644653Sdougm 	 */
37654653Sdougm 	if (!dryrun && ret == SA_OK && change && worklist != NULL)
37663034Sdougm 		/* properties changed, so update all shares */
37675331Samw 		(void) enable_all_groups(handle, worklist, 0, 0, protocol,
37685331Samw 		    B_TRUE);
37694653Sdougm 
37703034Sdougm 	if (worklist != NULL)
37714653Sdougm 		free_list(worklist);
37723034Sdougm 	return (ret);
37733034Sdougm }
37743034Sdougm 
37753034Sdougm /*
37763034Sdougm  * space_set(groupname, optlist, protocol, sharepath, dryrun)
37773034Sdougm  *
37783034Sdougm  * This function implements "set" when a name space (-S) is
37793034Sdougm  * specified. It is a namespace set. Options and other CLI parsing has
37803034Sdougm  * already been done.
37813034Sdougm  */
37823034Sdougm 
37833034Sdougm static int
37843910Sdougm space_set(sa_handle_t handle, char *groupname, struct options *optlist,
37855331Samw     char *protocol, char *sharepath, int dryrun, char *sectype)
37863034Sdougm {
37873034Sdougm 	sa_group_t group;
37883034Sdougm 	int ret = SA_OK;
37893034Sdougm 	int change = 0;
37903034Sdougm 	struct list *worklist = NULL;
37913034Sdougm 
37923034Sdougm 	/*
37933034Sdougm 	 * make sure protcol and sectype are valid
37943034Sdougm 	 */
37953034Sdougm 
37963034Sdougm 	if (sa_proto_valid_space(protocol, sectype) == 0) {
37974653Sdougm 		(void) printf(gettext("Option space \"%s\" not valid "
37984653Sdougm 		    "for protocol.\n"), sectype);
37994653Sdougm 		return (SA_INVALID_SECURITY);
38003034Sdougm 	}
38013034Sdougm 
38023910Sdougm 	group = sa_get_group(handle, groupname);
38033034Sdougm 	if (group != NULL) {
38044653Sdougm 		sa_share_t share = NULL;
38054653Sdougm 		if (sharepath != NULL) {
38064653Sdougm 			share = sa_get_share(group, sharepath);
38074653Sdougm 			if (share == NULL) {
38084653Sdougm 				(void) printf(gettext(
38094653Sdougm 				    "Share does not exist in group %s\n"),
38104653Sdougm 				    groupname, sharepath);
38114653Sdougm 				ret = SA_NO_SUCH_PATH;
38125089Sdougm 			} else {
38135089Sdougm 				/* if ZFS and OK, then only group */
38145089Sdougm 				ret = zfscheck(group, share);
38155089Sdougm 				if (ret == SA_OK &&
38165089Sdougm 				    sa_group_is_zfs(group))
38175089Sdougm 					share = NULL;
38185089Sdougm 				if (ret == SA_NOT_ALLOWED)
38195089Sdougm 					(void) printf(gettext(
38205089Sdougm 					    "Properties on ZFS group shares "
38215089Sdougm 					    "not supported: %s\n"), sharepath);
38224653Sdougm 			}
38233034Sdougm 		}
38244653Sdougm 		if (ret == SA_OK) {
38254653Sdougm 			/* group must exist */
38266214Sdougm 			ret = valid_options(handle, optlist, protocol,
38274653Sdougm 			    share == NULL ? group : share, sectype);
38284653Sdougm 			if (ret == SA_OK && !dryrun) {
38294653Sdougm 				if (share != NULL)
38304653Sdougm 					change = add_security(share, sectype,
38314653Sdougm 					    optlist, protocol, &ret);
38324653Sdougm 				else
38334653Sdougm 					change = add_security(group, sectype,
38344653Sdougm 					    optlist, protocol, &ret);
38354653Sdougm 				if (ret != SA_OK)
38364653Sdougm 					(void) printf(gettext(
38374653Sdougm 					    "Could not set property: %s\n"),
38384653Sdougm 					    sa_errorstr(ret));
38394653Sdougm 			}
38404653Sdougm 			if (ret == SA_OK && change)
38415331Samw 				worklist = add_list(worklist, group, share,
38425331Samw 				    protocol);
38433034Sdougm 		}
38444653Sdougm 		free_opt(optlist);
38453034Sdougm 	} else {
38463034Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
38473034Sdougm 		ret = SA_NO_SUCH_GROUP;
38483034Sdougm 	}
38495331Samw 
38503034Sdougm 	/*
38515331Samw 	 * We have a group and potentially legal additions.
38523034Sdougm 	 */
38533034Sdougm 
38544653Sdougm 	/* Commit to configuration if not a dryrun */
38553034Sdougm 	if (!dryrun && ret == 0) {
38564653Sdougm 		if (change && worklist != NULL) {
38574653Sdougm 			/* properties changed, so update all shares */
38584653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
38595331Samw 			    protocol, B_TRUE);
38604653Sdougm 		}
38614653Sdougm 		ret = sa_update_config(handle);
38623034Sdougm 	}
38633034Sdougm 	if (worklist != NULL)
38644653Sdougm 		free_list(worklist);
38653034Sdougm 	return (ret);
38663034Sdougm }
38673034Sdougm 
38683034Sdougm /*
38693034Sdougm  * sa_set(flags, argc, argv)
38703034Sdougm  *
38713034Sdougm  * Implements the set subcommand. It keys off of -S to determine which
38723034Sdougm  * set of operations to actually do.
38733034Sdougm  */
38743034Sdougm 
38753034Sdougm int
38763910Sdougm sa_set(sa_handle_t handle, int flags, int argc, char *argv[])
38773034Sdougm {
38783034Sdougm 	char *groupname;
38793034Sdougm 	int verbose = 0;
38803034Sdougm 	int dryrun = 0;
38813034Sdougm 	int c;
38823034Sdougm 	char *protocol = NULL;
38833034Sdougm 	int ret = SA_OK;
38843034Sdougm 	struct options *optlist = NULL;
38855331Samw 	char *rsrcname = NULL;
38863034Sdougm 	char *sharepath = NULL;
38873034Sdougm 	char *optset = NULL;
38883034Sdougm 	int auth;
38893034Sdougm 
38905331Samw 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
38914653Sdougm 		switch (c) {
38924653Sdougm 		case 'v':
38934653Sdougm 			verbose++;
38944653Sdougm 			break;
38954653Sdougm 		case 'n':
38964653Sdougm 			dryrun++;
38974653Sdougm 			break;
38984653Sdougm 		case 'P':
38995331Samw 			if (protocol != NULL) {
39005331Samw 				(void) printf(gettext(
39015331Samw 				    "Specifying multiple protocols "
39025331Samw 				    "not supported: %s\n"), protocol);
39035331Samw 				return (SA_SYNTAX_ERR);
39045331Samw 			}
39054653Sdougm 			protocol = optarg;
39064653Sdougm 			if (!sa_valid_protocol(protocol)) {
39074653Sdougm 				(void) printf(gettext(
39084653Sdougm 				    "Invalid protocol specified: %s\n"),
39094653Sdougm 				    protocol);
39104653Sdougm 				return (SA_INVALID_PROTOCOL);
39114653Sdougm 			}
39124653Sdougm 			break;
39134653Sdougm 		case 'p':
39144653Sdougm 			ret = add_opt(&optlist, optarg, 0);
39154653Sdougm 			switch (ret) {
39164653Sdougm 			case OPT_ADD_SYNTAX:
39174653Sdougm 				(void) printf(gettext("Property syntax error:"
39184653Sdougm 				    " %s\n"), optarg);
39194653Sdougm 				return (SA_SYNTAX_ERR);
39204653Sdougm 			case OPT_ADD_MEMORY:
39214653Sdougm 				(void) printf(gettext("No memory to set "
39224653Sdougm 				    "property: %s\n"), optarg);
39234653Sdougm 				return (SA_NO_MEMORY);
39244653Sdougm 			default:
39254653Sdougm 				break;
39264653Sdougm 			}
39274653Sdougm 			break;
39285331Samw 		case 'r':
39295331Samw 			if (rsrcname != NULL) {
39305331Samw 				(void) printf(gettext(
39315331Samw 				    "Setting multiple resource names not"
39325331Samw 				    " supported\n"));
39335331Samw 				return (SA_SYNTAX_ERR);
39345331Samw 			}
39355331Samw 			rsrcname = optarg;
39365331Samw 			break;
39374653Sdougm 		case 's':
39385331Samw 			if (sharepath != NULL) {
39395331Samw 				(void) printf(gettext(
39405331Samw 				    "Setting multiple shares not supported\n"));
39415331Samw 				return (SA_SYNTAX_ERR);
39425331Samw 			}
39434653Sdougm 			sharepath = optarg;
39444653Sdougm 			break;
39454653Sdougm 		case 'S':
39465331Samw 			if (optset != NULL) {
39475331Samw 				(void) printf(gettext(
39485331Samw 				    "Specifying multiple property "
39495331Samw 				    "spaces not supported: %s\n"), optset);
39505331Samw 				return (SA_SYNTAX_ERR);
39515331Samw 			}
39524653Sdougm 			optset = optarg;
39534653Sdougm 			break;
39546019Sdougm 		case 'h':
39556019Sdougm 			/* optopt on valid arg isn't defined */
39566019Sdougm 			optopt = c;
39576019Sdougm 			/*FALLTHROUGH*/
39586019Sdougm 		case '?':
39594653Sdougm 		default:
39606019Sdougm 			/*
39616019Sdougm 			 * Since a bad option gets to here, sort it
39626019Sdougm 			 * out and return a syntax error return value
39636019Sdougm 			 * if necessary.
39646019Sdougm 			 */
39656019Sdougm 			switch (optopt) {
39666019Sdougm 			default:
39676019Sdougm 				ret = SA_SYNTAX_ERR;
39686019Sdougm 				break;
39696019Sdougm 			case 'h':
39706019Sdougm 			case '?':
39716019Sdougm 				break;
39726019Sdougm 			}
39734653Sdougm 			(void) printf(gettext("usage: %s\n"),
39744653Sdougm 			    sa_get_usage(USAGE_SET));
39756019Sdougm 			return (ret);
39763034Sdougm 		}
39773034Sdougm 	}
39783034Sdougm 
39793034Sdougm 	if (optlist != NULL)
39804653Sdougm 		ret = chk_opt(optlist, optset != NULL, protocol);
39813034Sdougm 
39823034Sdougm 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
39834653Sdougm 	    protocol == NULL || ret != OPT_ADD_OK) {
39844653Sdougm 		char *sep = "\t";
39854653Sdougm 
39864653Sdougm 		(void) printf(gettext("usage: %s\n"), sa_get_usage(USAGE_SET));
39874653Sdougm 		if (optind >= argc) {
39884653Sdougm 			(void) printf(gettext("%sgroup must be specified"),
39894653Sdougm 			    sep);
39904653Sdougm 			sep = ", ";
39914653Sdougm 		}
39924653Sdougm 		if (optlist == NULL) {
39934653Sdougm 			(void) printf(gettext("%sat least one property must be"
39944653Sdougm 			    " specified"), sep);
39954653Sdougm 			sep = ", ";
39964653Sdougm 		}
39974653Sdougm 		if (protocol == NULL) {
39984653Sdougm 			(void) printf(gettext("%sprotocol must be specified"),
39994653Sdougm 			    sep);
40004653Sdougm 			sep = ", ";
40014653Sdougm 		}
40024653Sdougm 		(void) printf("\n");
40034653Sdougm 		ret = SA_SYNTAX_ERR;
40043034Sdougm 	} else {
40053034Sdougm 		/*
40065089Sdougm 		 * Group already exists so we can proceed after a few
40075089Sdougm 		 * additional checks related to ZFS handling.
40083034Sdougm 		 */
40093034Sdougm 
40104653Sdougm 		groupname = argv[optind];
40115089Sdougm 		if (strcmp(groupname, "zfs") == 0) {
40125089Sdougm 			(void) printf(gettext("Changing properties for group "
40135089Sdougm 			    "\"zfs\" not allowed\n"));
40145089Sdougm 			return (SA_NOT_ALLOWED);
40155089Sdougm 		}
40165089Sdougm 
40174653Sdougm 		auth = check_authorizations(groupname, flags);
40184653Sdougm 		if (optset == NULL)
40194653Sdougm 			ret = basic_set(handle, groupname, optlist, protocol,
40205331Samw 			    sharepath, rsrcname, dryrun);
40214653Sdougm 		else
40224653Sdougm 			ret = space_set(handle, groupname, optlist, protocol,
40234653Sdougm 			    sharepath, dryrun, optset);
40244653Sdougm 		if (dryrun && ret == SA_OK && !auth && verbose) {
40254653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
40264653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
40274653Sdougm 		}
40283034Sdougm 	}
40293034Sdougm 	return (ret);
40303034Sdougm }
40313034Sdougm 
40323034Sdougm /*
40333034Sdougm  * remove_options(group, optlist, proto, *err)
40343034Sdougm  *
40354653Sdougm  * Helper function to actually remove options from a group after all
40363034Sdougm  * preprocessing is done.
40373034Sdougm  */
40383034Sdougm 
40393034Sdougm static int
40403034Sdougm remove_options(sa_group_t group, struct options *optlist,
40415331Samw     char *proto, int *err)
40423034Sdougm {
40433034Sdougm 	struct options *cur;
40443034Sdougm 	sa_optionset_t optionset;
40453034Sdougm 	sa_property_t prop;
40463034Sdougm 	int change = 0;
40473034Sdougm 	int ret = SA_OK;
40483034Sdougm 
40493034Sdougm 	optionset = sa_get_optionset(group, proto);
40503034Sdougm 	if (optionset != NULL) {
40514653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
40524653Sdougm 			prop = sa_get_property(optionset, cur->optname);
40534653Sdougm 			if (prop != NULL) {
40544653Sdougm 				ret = sa_remove_property(prop);
40554653Sdougm 				if (ret != SA_OK)
40564653Sdougm 					break;
40574653Sdougm 				change = 1;
40584653Sdougm 			}
40593034Sdougm 		}
40603034Sdougm 	}
40613034Sdougm 	if (ret == SA_OK && change)
40624653Sdougm 		ret = sa_commit_properties(optionset, 0);
40633034Sdougm 
40643034Sdougm 	if (err != NULL)
40654653Sdougm 		*err = ret;
40663034Sdougm 	return (change);
40673034Sdougm }
40683034Sdougm 
40693034Sdougm /*
40703034Sdougm  * valid_unset(group, optlist, proto)
40713034Sdougm  *
40723034Sdougm  * Sanity check the optlist to make sure they can be removed. Issue an
40733034Sdougm  * error if a property doesn't exist.
40743034Sdougm  */
40753034Sdougm 
40763034Sdougm static int
40773034Sdougm valid_unset(sa_group_t group, struct options *optlist, char *proto)
40783034Sdougm {
40793034Sdougm 	struct options *cur;
40803034Sdougm 	sa_optionset_t optionset;
40813034Sdougm 	sa_property_t prop;
40823034Sdougm 	int ret = SA_OK;
40833034Sdougm 
40843034Sdougm 	optionset = sa_get_optionset(group, proto);
40853034Sdougm 	if (optionset != NULL) {
40864653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
40874653Sdougm 			prop = sa_get_property(optionset, cur->optname);
40884653Sdougm 			if (prop == NULL) {
40894653Sdougm 				(void) printf(gettext(
40904653Sdougm 				    "Could not unset property %s: not set\n"),
40914653Sdougm 				    cur->optname);
40924653Sdougm 				ret = SA_NO_SUCH_PROP;
40934653Sdougm 			}
40943034Sdougm 		}
40953034Sdougm 	}
40963034Sdougm 	return (ret);
40973034Sdougm }
40983034Sdougm 
40993034Sdougm /*
41003034Sdougm  * valid_unset_security(group, optlist, proto)
41013034Sdougm  *
41023034Sdougm  * Sanity check the optlist to make sure they can be removed. Issue an
41033034Sdougm  * error if a property doesn't exist.
41043034Sdougm  */
41053034Sdougm 
41063034Sdougm static int
41073034Sdougm valid_unset_security(sa_group_t group, struct options *optlist, char *proto,
41085331Samw     char *sectype)
41093034Sdougm {
41103034Sdougm 	struct options *cur;
41113034Sdougm 	sa_security_t security;
41123034Sdougm 	sa_property_t prop;
41133034Sdougm 	int ret = SA_OK;
41143034Sdougm 	char *sec;
41153034Sdougm 
41163034Sdougm 	sec = sa_proto_space_alias(proto, sectype);
41173034Sdougm 	security = sa_get_security(group, sec, proto);
41183034Sdougm 	if (security != NULL) {
41194653Sdougm 		for (cur = optlist; cur != NULL; cur = cur->next) {
41204653Sdougm 			prop = sa_get_property(security, cur->optname);
41214653Sdougm 			if (prop == NULL) {
41224653Sdougm 				(void) printf(gettext(
41234653Sdougm 				    "Could not unset property %s: not set\n"),
41244653Sdougm 				    cur->optname);
41254653Sdougm 				ret = SA_NO_SUCH_PROP;
41264653Sdougm 			}
41273034Sdougm 		}
41283034Sdougm 	} else {
41294653Sdougm 		(void) printf(gettext(
41304653Sdougm 		    "Could not unset %s: space not defined\n"), sectype);
41314653Sdougm 		ret = SA_NO_SUCH_SECURITY;
41323034Sdougm 	}
41333034Sdougm 	if (sec != NULL)
41344653Sdougm 		sa_free_attr_string(sec);
41353034Sdougm 	return (ret);
41363034Sdougm }
41373034Sdougm 
41383034Sdougm /*
41393034Sdougm  * remove_security(group, optlist, proto)
41403034Sdougm  *
41413034Sdougm  * Remove the properties since they were checked as valid.
41423034Sdougm  */
41433034Sdougm 
41443034Sdougm static int
41453034Sdougm remove_security(sa_group_t group, char *sectype,
41465331Samw     struct options *optlist, char *proto, int *err)
41473034Sdougm {
41483034Sdougm 	sa_security_t security;
41493034Sdougm 	int ret = SA_OK;
41503034Sdougm 	int change = 0;
41513034Sdougm 
41523034Sdougm 	sectype = sa_proto_space_alias(proto, sectype);
41533034Sdougm 	security = sa_get_security(group, sectype, proto);
41543034Sdougm 	if (sectype != NULL)
41554653Sdougm 		sa_free_attr_string(sectype);
41563034Sdougm 
41573034Sdougm 	if (security != NULL) {
41584653Sdougm 		while (optlist != NULL) {
41594653Sdougm 			sa_property_t prop;
41604653Sdougm 			prop = sa_get_property(security, optlist->optname);
41614653Sdougm 			if (prop != NULL) {
41624653Sdougm 				ret = sa_remove_property(prop);
41634653Sdougm 				if (ret != SA_OK)
41644653Sdougm 					break;
41654653Sdougm 				change = 1;
41664653Sdougm 			}
41674653Sdougm 			optlist = optlist->next;
41683034Sdougm 		}
41693034Sdougm 		/*
41703034Sdougm 		 * when done, properties may have all been removed but
41713034Sdougm 		 * we need to keep the security type itself until
41723034Sdougm 		 * explicitly removed.
41733034Sdougm 		 */
41744653Sdougm 		if (ret == SA_OK && change)
41754653Sdougm 			ret = sa_commit_properties(security, 0);
41763034Sdougm 	} else {
41774653Sdougm 		ret = SA_NO_SUCH_PROP;
41783034Sdougm 	}
41793034Sdougm 	if (err != NULL)
41804653Sdougm 		*err = ret;
41813034Sdougm 	return (change);
41823034Sdougm }
41833034Sdougm 
41843034Sdougm /*
41855331Samw  * basic_unset(groupname, optlist, protocol, sharepath, rsrcname, dryrun)
41863034Sdougm  *
41874653Sdougm  * Unset non-named optionset properties.
41883034Sdougm  */
41893034Sdougm 
41903034Sdougm static int
41913910Sdougm basic_unset(sa_handle_t handle, char *groupname, struct options *optlist,
41925331Samw     char *protocol, char *sharepath, char *rsrcname, int dryrun)
41933034Sdougm {
41943034Sdougm 	sa_group_t group;
41953034Sdougm 	int ret = SA_OK;
41963034Sdougm 	int change = 0;
41973034Sdougm 	struct list *worklist = NULL;
41984653Sdougm 	sa_share_t share = NULL;
41995331Samw 	sa_resource_t resource = NULL;
42003034Sdougm 
42013910Sdougm 	group = sa_get_group(handle, groupname);
42024653Sdougm 	if (group == NULL)
42034653Sdougm 		return (ret);
42044653Sdougm 
42055331Samw 	/*
42065331Samw 	 * If there is a sharepath, make sure it belongs to
42075331Samw 	 * the group.
42085331Samw 	 */
42094653Sdougm 	if (sharepath != NULL) {
42103034Sdougm 		share = sa_get_share(group, sharepath);
42113034Sdougm 		if (share == NULL) {
42124653Sdougm 			(void) printf(gettext(
42134653Sdougm 			    "Share does not exist in group %s\n"),
42144653Sdougm 			    groupname, sharepath);
42154653Sdougm 			ret = SA_NO_SUCH_PATH;
42163034Sdougm 		}
42174653Sdougm 	}
42185331Samw 	/*
42195331Samw 	 * If a resource name exists, make sure it belongs to
42205331Samw 	 * the share if present else it belongs to the
42215331Samw 	 * group. Also check the protocol to see if it
42225331Samw 	 * supports resource level properties or not. If not,
42235331Samw 	 * use share only.
42245331Samw 	 */
42255331Samw 	if (rsrcname != NULL) {
42265331Samw 		if (share != NULL) {
42275331Samw 			resource = sa_get_share_resource(share, rsrcname);
42285331Samw 			if (resource == NULL)
42295331Samw 				ret = SA_NO_SUCH_RESOURCE;
42305331Samw 		} else {
42315331Samw 			resource = sa_get_resource(group, rsrcname);
42325331Samw 			if (resource != NULL) {
42335331Samw 				share = sa_get_resource_parent(resource);
42345331Samw 			} else {
42355331Samw 				ret = SA_NO_SUCH_RESOURCE;
42365331Samw 			}
42375331Samw 		}
42385331Samw 		if (ret == SA_OK && resource != NULL) {
42395331Samw 			uint64_t features;
42405331Samw 			/*
42415331Samw 			 * Check to see if the resource can take
42425331Samw 			 * properties. If so, stick the resource into
42435331Samw 			 * "share" so it will all just work.
42445331Samw 			 */
42455331Samw 			features = sa_proto_get_featureset(protocol);
42465331Samw 			if (features & SA_FEATURE_RESOURCE)
42475331Samw 				share = (sa_share_t)resource;
42485331Samw 		}
42495331Samw 	}
42505331Samw 
42514653Sdougm 	if (ret == SA_OK) {
42523034Sdougm 		/* group must exist */
42533034Sdougm 		ret = valid_unset(share != NULL ? share : group,
42544653Sdougm 		    optlist, protocol);
42553034Sdougm 		if (ret == SA_OK && !dryrun) {
42564653Sdougm 			if (share != NULL) {
42574653Sdougm 				sa_optionset_t optionset;
42584653Sdougm 				sa_property_t prop;
42594653Sdougm 				change |= remove_options(share, optlist,
42604653Sdougm 				    protocol, &ret);
42614653Sdougm 				/*
42624653Sdougm 				 * If a share optionset is
42634653Sdougm 				 * empty, remove it.
42644653Sdougm 				 */
42654653Sdougm 				optionset = sa_get_optionset((sa_share_t)share,
42664653Sdougm 				    protocol);
42674653Sdougm 				if (optionset != NULL) {
42684653Sdougm 					prop = sa_get_property(optionset, NULL);
42694653Sdougm 					if (prop == NULL)
42704653Sdougm 						(void) sa_destroy_optionset(
42714653Sdougm 						    optionset);
42724653Sdougm 				}
42734653Sdougm 			} else {
42744653Sdougm 				change |= remove_options(group,
42754653Sdougm 				    optlist, protocol, &ret);
42763034Sdougm 			}
42774653Sdougm 			if (ret == SA_OK && change)
42785331Samw 				worklist = add_list(worklist, group, share,
42795331Samw 				    protocol);
42804653Sdougm 			if (ret != SA_OK)
42814653Sdougm 				(void) printf(gettext(
42824653Sdougm 				    "Could not remove properties: "
42834653Sdougm 				    "%s\n"), sa_errorstr(ret));
42843034Sdougm 		}
42854653Sdougm 	} else {
42865331Samw 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
42873034Sdougm 		ret = SA_NO_SUCH_GROUP;
42883034Sdougm 	}
42894653Sdougm 	free_opt(optlist);
42903034Sdougm 
42913034Sdougm 	/*
42924653Sdougm 	 * We have a group and potentially legal additions
42934653Sdougm 	 *
42944653Sdougm 	 * Commit to configuration if not a dryrun
42953034Sdougm 	 */
42963034Sdougm 	if (!dryrun && ret == SA_OK) {
42974653Sdougm 		if (change && worklist != NULL) {
42984653Sdougm 			/* properties changed, so update all shares */
42994653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
43005331Samw 			    protocol, B_TRUE);
43014653Sdougm 		}
43023034Sdougm 	}
43033034Sdougm 	if (worklist != NULL)
43044653Sdougm 		free_list(worklist);
43053034Sdougm 	return (ret);
43063034Sdougm }
43073034Sdougm 
43083034Sdougm /*
43093034Sdougm  * space_unset(groupname, optlist, protocol, sharepath, dryrun)
43103034Sdougm  *
43114653Sdougm  * Unset named optionset properties.
43123034Sdougm  */
43133034Sdougm static int
43143910Sdougm space_unset(sa_handle_t handle, char *groupname, struct options *optlist,
43155331Samw     char *protocol, char *sharepath, int dryrun, char *sectype)
43163034Sdougm {
43173034Sdougm 	sa_group_t group;
43183034Sdougm 	int ret = SA_OK;
43193034Sdougm 	int change = 0;
43203034Sdougm 	struct list *worklist = NULL;
43214653Sdougm 	sa_share_t share = NULL;
43223034Sdougm 
43233910Sdougm 	group = sa_get_group(handle, groupname);
43244653Sdougm 	if (group == NULL) {
43254653Sdougm 		(void) printf(gettext("Group \"%s\" not found\n"), groupname);
43264653Sdougm 		return (SA_NO_SUCH_GROUP);
43274653Sdougm 	}
43284653Sdougm 	if (sharepath != NULL) {
43293034Sdougm 		share = sa_get_share(group, sharepath);
43303034Sdougm 		if (share == NULL) {
43314653Sdougm 			(void) printf(gettext(
43324653Sdougm 			    "Share does not exist in group %s\n"),
43334653Sdougm 			    groupname, sharepath);
43344653Sdougm 			return (SA_NO_SUCH_PATH);
43353034Sdougm 		}
43364653Sdougm 	}
43375331Samw 	ret = valid_unset_security(share != NULL ? share : group,
43385331Samw 	    optlist, protocol, sectype);
43394653Sdougm 
43404653Sdougm 	if (ret == SA_OK && !dryrun) {
43414653Sdougm 		if (optlist != NULL) {
43423034Sdougm 			if (share != NULL) {
43434653Sdougm 				sa_security_t optionset;
43444653Sdougm 				sa_property_t prop;
43454653Sdougm 				change = remove_security(share,
43464653Sdougm 				    sectype, optlist, protocol, &ret);
43474653Sdougm 
43484653Sdougm 				/* If a share security is empty, remove it */
43494653Sdougm 				optionset = sa_get_security((sa_group_t)share,
43504653Sdougm 				    sectype, protocol);
43514653Sdougm 				if (optionset != NULL) {
43524653Sdougm 					prop = sa_get_property(optionset,
43534653Sdougm 					    NULL);
43544653Sdougm 					if (prop == NULL)
43554653Sdougm 						ret = sa_destroy_security(
43564653Sdougm 						    optionset);
43574653Sdougm 				}
43583034Sdougm 			} else {
43594653Sdougm 				change = remove_security(group, sectype,
43604653Sdougm 				    optlist, protocol, &ret);
43613034Sdougm 			}
43624653Sdougm 		} else {
43633034Sdougm 			sa_security_t security;
43643034Sdougm 			char *sec;
43653034Sdougm 			sec = sa_proto_space_alias(protocol, sectype);
43663034Sdougm 			security = sa_get_security(group, sec, protocol);
43673034Sdougm 			if (sec != NULL)
43684653Sdougm 				sa_free_attr_string(sec);
43693034Sdougm 			if (security != NULL) {
43704653Sdougm 				ret = sa_destroy_security(security);
43714653Sdougm 				if (ret == SA_OK)
43724653Sdougm 					change = 1;
43733034Sdougm 			} else {
43744653Sdougm 				ret = SA_NO_SUCH_PROP;
43753034Sdougm 			}
43764653Sdougm 		}
43774653Sdougm 		if (ret != SA_OK)
43783034Sdougm 			(void) printf(gettext("Could not unset property: %s\n"),
43794653Sdougm 			    sa_errorstr(ret));
43803034Sdougm 	}
43814653Sdougm 
43824653Sdougm 	if (ret == SA_OK && change)
43835331Samw 		worklist = add_list(worklist, group, 0, protocol);
43844653Sdougm 
43853034Sdougm 	free_opt(optlist);
43863034Sdougm 	/*
43874653Sdougm 	 * We have a group and potentially legal additions
43883034Sdougm 	 */
43893034Sdougm 
43904653Sdougm 	/* Commit to configuration if not a dryrun */
43913034Sdougm 	if (!dryrun && ret == 0) {
43923034Sdougm 		/* properties changed, so update all shares */
43934653Sdougm 		if (change && worklist != NULL)
43944653Sdougm 			(void) enable_all_groups(handle, worklist, 0, 0,
43955331Samw 			    protocol, B_TRUE);
43964653Sdougm 		ret = sa_update_config(handle);
43973034Sdougm 	}
43983034Sdougm 	if (worklist != NULL)
43994653Sdougm 		free_list(worklist);
44003034Sdougm 	return (ret);
44013034Sdougm }
44023034Sdougm 
44033034Sdougm /*
44043034Sdougm  * sa_unset(flags, argc, argv)
44053034Sdougm  *
44064653Sdougm  * Implements the unset subcommand. Parsing done here and then basic
44073034Sdougm  * or space versions of the real code are called.
44083034Sdougm  */
44093034Sdougm 
44103034Sdougm int
44113910Sdougm sa_unset(sa_handle_t handle, int flags, int argc, char *argv[])
44123034Sdougm {
44133034Sdougm 	char *groupname;
44143034Sdougm 	int verbose = 0;
44153034Sdougm 	int dryrun = 0;
44163034Sdougm 	int c;
44173034Sdougm 	char *protocol = NULL;
44183034Sdougm 	int ret = SA_OK;
44193034Sdougm 	struct options *optlist = NULL;
44205331Samw 	char *rsrcname = NULL;
44213034Sdougm 	char *sharepath = NULL;
44223034Sdougm 	char *optset = NULL;
44233034Sdougm 	int auth;
44243034Sdougm 
44255331Samw 	while ((c = getopt(argc, argv, "?hvnP:p:r:s:S:")) != EOF) {
44264653Sdougm 		switch (c) {
44274653Sdougm 		case 'v':
44284653Sdougm 			verbose++;
44294653Sdougm 			break;
44304653Sdougm 		case 'n':
44314653Sdougm 			dryrun++;
44324653Sdougm 			break;
44334653Sdougm 		case 'P':
44345331Samw 			if (protocol != NULL) {
44355331Samw 				(void) printf(gettext(
44365331Samw 				    "Specifying multiple protocols "
44375331Samw 				    "not supported: %s\n"), protocol);
44385331Samw 				return (SA_SYNTAX_ERR);
44395331Samw 			}
44404653Sdougm 			protocol = optarg;
44414653Sdougm 			if (!sa_valid_protocol(protocol)) {
44424653Sdougm 				(void) printf(gettext(
44434653Sdougm 				    "Invalid protocol specified: %s\n"),
44444653Sdougm 				    protocol);
44454653Sdougm 				return (SA_INVALID_PROTOCOL);
44464653Sdougm 			}
44474653Sdougm 			break;
44484653Sdougm 		case 'p':
44494653Sdougm 			ret = add_opt(&optlist, optarg, 1);
44504653Sdougm 			switch (ret) {
44514653Sdougm 			case OPT_ADD_SYNTAX:
44524653Sdougm 				(void) printf(gettext("Property syntax error "
44534653Sdougm 				    "for property %s\n"), optarg);
44544653Sdougm 				return (SA_SYNTAX_ERR);
44554653Sdougm 
44564653Sdougm 			case OPT_ADD_PROPERTY:
44574653Sdougm 				(void) printf(gettext("Properties need to be "
44584653Sdougm 				    "set with set command: %s\n"), optarg);
44594653Sdougm 				return (SA_SYNTAX_ERR);
44604653Sdougm 
44614653Sdougm 			default:
44624653Sdougm 				break;
44634653Sdougm 			}
44644653Sdougm 			break;
44655331Samw 		case 'r':
44665331Samw 			/*
44675331Samw 			 * Unset properties on resource if applicable or on
44685331Samw 			 * share if resource for this protocol doesn't use
44695331Samw 			 * resources.
44705331Samw 			 */
44715331Samw 			if (rsrcname != NULL) {
44725331Samw 				(void) printf(gettext(
44735331Samw 				    "Unsetting multiple resource "
44745331Samw 				    "names not supported\n"));
44755331Samw 				return (SA_SYNTAX_ERR);
44765331Samw 			}
44775331Samw 			rsrcname = optarg;
44785331Samw 			break;
44794653Sdougm 		case 's':
44805331Samw 			if (sharepath != NULL) {
44815331Samw 				(void) printf(gettext(
44825331Samw 				    "Adding multiple shares not supported\n"));
44835331Samw 				return (SA_SYNTAX_ERR);
44845331Samw 			}
44854653Sdougm 			sharepath = optarg;
44864653Sdougm 			break;
44874653Sdougm 		case 'S':
44885331Samw 			if (optset != NULL) {
44895331Samw 				(void) printf(gettext(
44905331Samw 				    "Specifying multiple property "
44915331Samw 				    "spaces not supported: %s\n"), optset);
44925331Samw 				return (SA_SYNTAX_ERR);
44935331Samw 			}
44944653Sdougm 			optset = optarg;
44954653Sdougm 			break;
44966019Sdougm 		case 'h':
44976019Sdougm 			/* optopt on valid arg isn't defined */
44986019Sdougm 			optopt = c;
44996019Sdougm 			/*FALLTHROUGH*/
45006019Sdougm 		case '?':
45014653Sdougm 		default:
45026019Sdougm 			/*
45036019Sdougm 			 * Since a bad option gets to here, sort it
45046019Sdougm 			 * out and return a syntax error return value
45056019Sdougm 			 * if necessary.
45066019Sdougm 			 */
45076019Sdougm 			switch (optopt) {
45086019Sdougm 			default:
45096019Sdougm 				ret = SA_SYNTAX_ERR;
45106019Sdougm 				break;
45116019Sdougm 			case 'h':
45126019Sdougm 			case '?':
45136019Sdougm 				break;
45146019Sdougm 			}
45154653Sdougm 			(void) printf(gettext("usage: %s\n"),
45164653Sdougm 			    sa_get_usage(USAGE_UNSET));
45176019Sdougm 			return (ret);
45183034Sdougm 		}
45193034Sdougm 	}
45203034Sdougm 
45213034Sdougm 	if (optlist != NULL)
45224653Sdougm 		ret = chk_opt(optlist, optset != NULL, protocol);
45233034Sdougm 
45243034Sdougm 	if (optind >= argc || (optlist == NULL && optset == NULL) ||
45253034Sdougm 	    protocol == NULL) {
45264653Sdougm 		char *sep = "\t";
45274653Sdougm 		(void) printf(gettext("usage: %s\n"),
45284653Sdougm 		    sa_get_usage(USAGE_UNSET));
45294653Sdougm 		if (optind >= argc) {
45304653Sdougm 			(void) printf(gettext("%sgroup must be specified"),
45314653Sdougm 			    sep);
45324653Sdougm 			sep = ", ";
45334653Sdougm 		}
45344653Sdougm 		if (optlist == NULL) {
45354653Sdougm 			(void) printf(gettext("%sat least one property must "
45364653Sdougm 			    "be specified"), sep);
45374653Sdougm 			sep = ", ";
45384653Sdougm 		}
45394653Sdougm 		if (protocol == NULL) {
45404653Sdougm 			(void) printf(gettext("%sprotocol must be specified"),
45414653Sdougm 			    sep);
45424653Sdougm 			sep = ", ";
45434653Sdougm 		}
45444653Sdougm 		(void) printf("\n");
45454653Sdougm 		ret = SA_SYNTAX_ERR;
45463034Sdougm 	} else {
45473034Sdougm 
45483034Sdougm 		/*
45494653Sdougm 		 * If a group already exists, we can only add a new
45503034Sdougm 		 * protocol to it and not create a new one or add the
45513034Sdougm 		 * same protocol again.
45523034Sdougm 		 */
45533034Sdougm 
45544653Sdougm 		groupname = argv[optind];
45554653Sdougm 		auth = check_authorizations(groupname, flags);
45564653Sdougm 		if (optset == NULL)
45574653Sdougm 			ret = basic_unset(handle, groupname, optlist, protocol,
45585331Samw 			    sharepath, rsrcname, dryrun);
45594653Sdougm 		else
45604653Sdougm 			ret = space_unset(handle, groupname, optlist, protocol,
45614653Sdougm 			    sharepath, dryrun, optset);
45624653Sdougm 
45634653Sdougm 		if (dryrun && ret == SA_OK && !auth && verbose)
45644653Sdougm 			(void) printf(gettext("Command would fail: %s\n"),
45654653Sdougm 			    sa_errorstr(SA_NO_PERMISSION));
45663034Sdougm 	}
45673034Sdougm 	return (ret);
45683034Sdougm }
45693034Sdougm 
45703034Sdougm /*
45713034Sdougm  * sa_enable_group(flags, argc, argv)
45723034Sdougm  *
45733034Sdougm  * Implements the enable subcommand
45743034Sdougm  */
45753034Sdougm 
45763034Sdougm int
45773910Sdougm sa_enable_group(sa_handle_t handle, int flags, int argc, char *argv[])
45783034Sdougm {
45793034Sdougm 	int verbose = 0;
45803034Sdougm 	int dryrun = 0;
45813034Sdougm 	int all = 0;
45823034Sdougm 	int c;
45833034Sdougm 	int ret = SA_OK;
45843034Sdougm 	char *protocol = NULL;
45853034Sdougm 	char *state;
45863034Sdougm 	struct list *worklist = NULL;
45873034Sdougm 	int auth = 1;
45884653Sdougm 	sa_group_t group;
45893034Sdougm 
45903034Sdougm 	while ((c = getopt(argc, argv, "?havnP:")) != EOF) {
45914653Sdougm 		switch (c) {
45924653Sdougm 		case 'a':
45934653Sdougm 			all = 1;
45944653Sdougm 			break;
45954653Sdougm 		case 'n':
45964653Sdougm 			dryrun++;
45974653Sdougm 			break;
45984653Sdougm 		case 'P':
45995331Samw 			if (protocol != NULL) {
46005331Samw 				(void) printf(gettext(
46015331Samw 				    "Specifying multiple protocols "
46025331Samw 				    "not supported: %s\n"), protocol);
46035331Samw 				return (SA_SYNTAX_ERR);
46045331Samw 			}
46054653Sdougm 			protocol = optarg;
46064653Sdougm 			if (!sa_valid_protocol(protocol)) {
46074653Sdougm 				(void) printf(gettext(
46084653Sdougm 				    "Invalid protocol specified: %s\n"),
46093034Sdougm 				    protocol);
46104653Sdougm 				return (SA_INVALID_PROTOCOL);
46114653Sdougm 			}
46124653Sdougm 			break;
46134653Sdougm 		case 'v':
46144653Sdougm 			verbose++;
46154653Sdougm 			break;
46164653Sdougm 		case 'h':
46176019Sdougm 			/* optopt on valid arg isn't defined */
46186019Sdougm 			optopt = c;
46196019Sdougm 			/*FALLTHROUGH*/
46204653Sdougm 		case '?':
46216019Sdougm 		default:
46226019Sdougm 			/*
46236019Sdougm 			 * Since a bad option gets to here, sort it
46246019Sdougm 			 * out and return a syntax error return value
46256019Sdougm 			 * if necessary.
46266019Sdougm 			 */
46276019Sdougm 			switch (optopt) {
46286019Sdougm 			default:
46296019Sdougm 				ret = SA_SYNTAX_ERR;
46306019Sdougm 				break;
46316019Sdougm 			case 'h':
46326019Sdougm 			case '?':
46336019Sdougm 				(void) printf(gettext("usage: %s\n"),
46346019Sdougm 				    sa_get_usage(USAGE_ENABLE));
46356019Sdougm 				return (ret);
46366019Sdougm 			}
46373034Sdougm 		}
46383034Sdougm 	}
46393034Sdougm 
46403034Sdougm 	if (optind == argc && !all) {
46414653Sdougm 		(void) printf(gettext("usage: %s\n"),
46424653Sdougm 		    sa_get_usage(USAGE_ENABLE));
46434653Sdougm 		(void) printf(gettext("\tmust specify group\n"));
46444653Sdougm 		return (SA_NO_SUCH_PATH);
46454653Sdougm 	}
46464653Sdougm 	if (!all) {
46473034Sdougm 		while (optind < argc) {
46484653Sdougm 			group = sa_get_group(handle, argv[optind]);
46494653Sdougm 			if (group != NULL) {
46504653Sdougm 				auth &= check_authorizations(argv[optind],
46514653Sdougm 				    flags);
46524653Sdougm 				state = sa_get_group_attr(group, "state");
46534653Sdougm 				if (state != NULL &&
46544653Sdougm 				    strcmp(state, "enabled") == 0) {
46554653Sdougm 					/* already enabled */
46564653Sdougm 					if (verbose)
46574653Sdougm 						(void) printf(gettext(
46584653Sdougm 						    "Group \"%s\" is already "
46594653Sdougm 						    "enabled\n"),
46604653Sdougm 						    argv[optind]);
46614653Sdougm 					ret = SA_BUSY; /* already enabled */
46624653Sdougm 				} else {
46634653Sdougm 					worklist = add_list(worklist, group,
46645331Samw 					    0, protocol);
46654653Sdougm 					if (verbose)
46664653Sdougm 						(void) printf(gettext(
46674653Sdougm 						    "Enabling group \"%s\"\n"),
46684653Sdougm 						    argv[optind]);
46694653Sdougm 				}
46704653Sdougm 				if (state != NULL)
46714653Sdougm 					sa_free_attr_string(state);
46723034Sdougm 			} else {
46734653Sdougm 				ret = SA_NO_SUCH_GROUP;
46743034Sdougm 			}
46754653Sdougm 			optind++;
46763034Sdougm 		}
46774653Sdougm 	} else {
46784653Sdougm 		for (group = sa_get_group(handle, NULL);
46794653Sdougm 		    group != NULL;
46803034Sdougm 		    group = sa_get_next_group(group)) {
46815331Samw 			worklist = add_list(worklist, group, 0, protocol);
46823034Sdougm 		}
46834653Sdougm 	}
46844653Sdougm 	if (!dryrun && ret == SA_OK)
46855331Samw 		ret = enable_all_groups(handle, worklist, 1, 0, NULL, B_FALSE);
46864653Sdougm 
46874653Sdougm 	if (ret != SA_OK && ret != SA_BUSY)
46883034Sdougm 		(void) printf(gettext("Could not enable group: %s\n"),
46894653Sdougm 		    sa_errorstr(ret));
46904653Sdougm 	if (ret == SA_BUSY)
46913034Sdougm 		ret = SA_OK;
46924653Sdougm 
46933034Sdougm 	if (worklist != NULL)
46944653Sdougm 		free_list(worklist);
46953034Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose) {
46964653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
46974653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
46983034Sdougm 	}
46993034Sdougm 	return (ret);
47003034Sdougm }
47013034Sdougm 
47023034Sdougm /*
47035331Samw  * disable_group(group, proto)
47043034Sdougm  *
47055331Samw  * Disable all the shares in the specified group.. This is a helper
47065331Samw  * for disable_all_groups in order to simplify regular and subgroup
47075331Samw  * (zfs) disabling. Group has already been checked for non-NULL.
47083034Sdougm  */
47093034Sdougm 
47103034Sdougm static int
47115331Samw disable_group(sa_group_t group, char *proto)
47123034Sdougm {
47133034Sdougm 	sa_share_t share;
47143034Sdougm 	int ret = SA_OK;
47153034Sdougm 
47165331Samw 	/*
47175331Samw 	 * If the protocol isn't enabled, skip it and treat as
47185331Samw 	 * successful.
47195331Samw 	 */
47205331Samw 	if (!has_protocol(group, proto))
47215331Samw 		return (ret);
47225331Samw 
47233034Sdougm 	for (share = sa_get_share(group, NULL);
47243034Sdougm 	    share != NULL && ret == SA_OK;
47253034Sdougm 	    share = sa_get_next_share(share)) {
47265331Samw 		ret = sa_disable_share(share, proto);
47274653Sdougm 		if (ret == SA_NO_SUCH_PATH) {
47284653Sdougm 			/*
47294653Sdougm 			 * this is OK since the path is gone. we can't
47304653Sdougm 			 * re-share it anyway so no error.
47314653Sdougm 			 */
47324653Sdougm 			ret = SA_OK;
47334653Sdougm 		}
47343034Sdougm 	}
47353034Sdougm 	return (ret);
47363034Sdougm }
47373034Sdougm 
47383034Sdougm /*
47393034Sdougm  * disable_all_groups(work, setstate)
47403034Sdougm  *
47413034Sdougm  * helper function that disables the shares in the list of groups
47423034Sdougm  * provided. It optionally marks the group as disabled. Used by both
47433034Sdougm  * enable and start subcommands.
47443034Sdougm  */
47453034Sdougm 
47463034Sdougm static int
47473910Sdougm disable_all_groups(sa_handle_t handle, struct list *work, int setstate)
47483034Sdougm {
47493034Sdougm 	int ret = SA_OK;
47503034Sdougm 	sa_group_t subgroup, group;
47513034Sdougm 
47523034Sdougm 	while (work != NULL && ret == SA_OK) {
47534653Sdougm 		group = (sa_group_t)work->item;
47544653Sdougm 		if (setstate)
47554653Sdougm 			ret = sa_set_group_attr(group, "state", "disabled");
47564653Sdougm 		if (ret == SA_OK) {
47574653Sdougm 			char *name;
47584653Sdougm 			name = sa_get_group_attr(group, "name");
47594653Sdougm 			if (name != NULL && strcmp(name, "zfs") == 0) {
47604653Sdougm 				/* need to get the sub-groups for stopping */
47614653Sdougm 				for (subgroup = sa_get_sub_group(group);
47624653Sdougm 				    subgroup != NULL;
47634653Sdougm 				    subgroup = sa_get_next_group(subgroup)) {
47645331Samw 					ret = disable_group(subgroup,
47655331Samw 					    work->proto);
47664653Sdougm 				}
47674653Sdougm 			} else {
47685331Samw 				ret = disable_group(group, work->proto);
47694653Sdougm 			}
47704653Sdougm 			/*
47714653Sdougm 			 * We don't want to "disable" since it won't come
47724653Sdougm 			 * up after a reboot.  The SMF framework should do
47734653Sdougm 			 * the right thing. On enable we do want to do
47744653Sdougm 			 * something.
47754653Sdougm 			 */
47763034Sdougm 		}
47774653Sdougm 		work = work->next;
47783034Sdougm 	}
47793034Sdougm 	if (ret == SA_OK)
47804653Sdougm 		ret = sa_update_config(handle);
47813034Sdougm 	return (ret);
47823034Sdougm }
47833034Sdougm 
47843034Sdougm /*
47853034Sdougm  * sa_disable_group(flags, argc, argv)
47863034Sdougm  *
47873034Sdougm  * Implements the disable subcommand
47883034Sdougm  */
47893034Sdougm 
47903034Sdougm int
47913910Sdougm sa_disable_group(sa_handle_t handle, int flags, int argc, char *argv[])
47923034Sdougm {
47933034Sdougm 	int verbose = 0;
47943034Sdougm 	int dryrun = 0;
47953034Sdougm 	int all = 0;
47963034Sdougm 	int c;
47973034Sdougm 	int ret = SA_OK;
47985331Samw 	char *protocol = NULL;
47993034Sdougm 	char *state;
48003034Sdougm 	struct list *worklist = NULL;
48014653Sdougm 	sa_group_t group;
48023034Sdougm 	int auth = 1;
48033034Sdougm 
48043034Sdougm 	while ((c = getopt(argc, argv, "?havn")) != EOF) {
48054653Sdougm 		switch (c) {
48064653Sdougm 		case 'a':
48074653Sdougm 			all = 1;
48084653Sdougm 			break;
48094653Sdougm 		case 'n':
48104653Sdougm 			dryrun++;
48114653Sdougm 			break;
48124653Sdougm 		case 'P':
48135331Samw 			if (protocol != NULL) {
48145331Samw 				(void) printf(gettext(
48155331Samw 				    "Specifying multiple protocols "
48165331Samw 				    "not supported: %s\n"), protocol);
48175331Samw 				return (SA_SYNTAX_ERR);
48185331Samw 			}
48194653Sdougm 			protocol = optarg;
48204653Sdougm 			if (!sa_valid_protocol(protocol)) {
48214653Sdougm 				(void) printf(gettext(
48224653Sdougm 				    "Invalid protocol specified: %s\n"),
48234653Sdougm 				    protocol);
48244653Sdougm 				return (SA_INVALID_PROTOCOL);
48254653Sdougm 			}
48264653Sdougm 			break;
48274653Sdougm 		case 'v':
48284653Sdougm 			verbose++;
48294653Sdougm 			break;
48306019Sdougm 		case 'h':
48316019Sdougm 			/* optopt on valid arg isn't defined */
48326019Sdougm 			optopt = c;
48336019Sdougm 			/*FALLTHROUGH*/
48346019Sdougm 		case '?':
48354653Sdougm 		default:
48366019Sdougm 			/*
48376019Sdougm 			 * Since a bad option gets to here, sort it
48386019Sdougm 			 * out and return a syntax error return value
48396019Sdougm 			 * if necessary.
48406019Sdougm 			 */
48416019Sdougm 			switch (optopt) {
48426019Sdougm 			default:
48436019Sdougm 				ret = SA_SYNTAX_ERR;
48446019Sdougm 				break;
48456019Sdougm 			case 'h':
48466019Sdougm 			case '?':
48476019Sdougm 				break;
48486019Sdougm 			}
48494653Sdougm 			(void) printf(gettext("usage: %s\n"),
48504653Sdougm 			    sa_get_usage(USAGE_DISABLE));
48516019Sdougm 			return (ret);
48523034Sdougm 		}
48533034Sdougm 	}
48543034Sdougm 
48553034Sdougm 	if (optind == argc && !all) {
48563034Sdougm 		(void) printf(gettext("usage: %s\n"),
48574653Sdougm 		    sa_get_usage(USAGE_DISABLE));
48583034Sdougm 		(void) printf(gettext("\tmust specify group\n"));
48594653Sdougm 		return (SA_NO_SUCH_PATH);
48604653Sdougm 	}
48614653Sdougm 	if (!all) {
48624653Sdougm 		while (optind < argc) {
48633910Sdougm 			group = sa_get_group(handle, argv[optind]);
48643034Sdougm 			if (group != NULL) {
48654653Sdougm 				auth &= check_authorizations(argv[optind],
48664653Sdougm 				    flags);
48674653Sdougm 				state = sa_get_group_attr(group, "state");
48684653Sdougm 				if (state == NULL ||
48694653Sdougm 				    strcmp(state, "disabled") == 0) {
48704653Sdougm 					/* already disabled */
48714653Sdougm 					if (verbose)
48724653Sdougm 						(void) printf(gettext(
48734653Sdougm 						    "Group \"%s\" is "
48744653Sdougm 						    "already disabled\n"),
48754653Sdougm 						    argv[optind]);
48765331Samw 					ret = SA_BUSY; /* already disabled */
48774653Sdougm 				} else {
48785331Samw 					worklist = add_list(worklist, group, 0,
48795331Samw 					    protocol);
48804653Sdougm 					if (verbose)
48814653Sdougm 						(void) printf(gettext(
48824653Sdougm 						    "Disabling group "
48834653Sdougm 						    "\"%s\"\n"), argv[optind]);
48844653Sdougm 				}
48854653Sdougm 				if (state != NULL)
48864653Sdougm 					sa_free_attr_string(state);
48873034Sdougm 			} else {
48884653Sdougm 				ret = SA_NO_SUCH_GROUP;
48893034Sdougm 			}
48903034Sdougm 			optind++;
48914653Sdougm 		}
48924653Sdougm 	} else {
48934653Sdougm 		for (group = sa_get_group(handle, NULL);
48944653Sdougm 		    group != NULL;
48954653Sdougm 		    group = sa_get_next_group(group))
48965331Samw 			worklist = add_list(worklist, group, 0, protocol);
48973034Sdougm 	}
48984653Sdougm 
48994653Sdougm 	if (ret == SA_OK && !dryrun)
49004653Sdougm 		ret = disable_all_groups(handle, worklist, 1);
49014653Sdougm 	if (ret != SA_OK && ret != SA_BUSY)
49024653Sdougm 		(void) printf(gettext("Could not disable group: %s\n"),
49034653Sdougm 		    sa_errorstr(ret));
49044653Sdougm 	if (ret == SA_BUSY)
49054653Sdougm 		ret = SA_OK;
49063034Sdougm 	if (worklist != NULL)
49074653Sdougm 		free_list(worklist);
49084653Sdougm 	if (dryrun && ret == SA_OK && !auth && verbose)
49094653Sdougm 		(void) printf(gettext("Command would fail: %s\n"),
49104653Sdougm 		    sa_errorstr(SA_NO_PERMISSION));
49113034Sdougm 	return (ret);
49123034Sdougm }
49133034Sdougm 
49143034Sdougm /*
49153034Sdougm  * sa_start_group(flags, argc, argv)
49163034Sdougm  *
49173034Sdougm  * Implements the start command.
49183034Sdougm  * This is similar to enable except it doesn't change the state
49193034Sdougm  * of the group(s) and only enables shares if the group is already
49203034Sdougm  * enabled.
49213034Sdougm  */
49225331Samw 
49233034Sdougm int
49243910Sdougm sa_start_group(sa_handle_t handle, int flags, int argc, char *argv[])
49253034Sdougm {
49263034Sdougm 	int verbose = 0;
49273034Sdougm 	int all = 0;
49283034Sdougm 	int c;
49293034Sdougm 	int ret = SMF_EXIT_OK;
49303034Sdougm 	char *protocol = NULL;
49313034Sdougm 	char *state;
49323034Sdougm 	struct list *worklist = NULL;
49334653Sdougm 	sa_group_t group;
49345331Samw #ifdef lint
49355331Samw 	flags = flags;
49365331Samw #endif
49373034Sdougm 
49383034Sdougm 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
49394653Sdougm 		switch (c) {
49404653Sdougm 		case 'a':
49414653Sdougm 			all = 1;
49424653Sdougm 			break;
49434653Sdougm 		case 'P':
49445331Samw 			if (protocol != NULL) {
49455331Samw 				(void) printf(gettext(
49465331Samw 				    "Specifying multiple protocols "
49475331Samw 				    "not supported: %s\n"), protocol);
49485331Samw 				return (SA_SYNTAX_ERR);
49495331Samw 			}
49504653Sdougm 			protocol = optarg;
49514653Sdougm 			if (!sa_valid_protocol(protocol)) {
49524653Sdougm 				(void) printf(gettext(
49534653Sdougm 				    "Invalid protocol specified: %s\n"),
49543034Sdougm 				    protocol);
49554653Sdougm 				return (SA_INVALID_PROTOCOL);
49564653Sdougm 			}
49574653Sdougm 			break;
49584653Sdougm 		case 'v':
49594653Sdougm 			verbose++;
49604653Sdougm 			break;
49616019Sdougm 		case 'h':
49626019Sdougm 			/* optopt on valid arg isn't defined */
49636019Sdougm 			optopt = c;
49646019Sdougm 			/*FALLTHROUGH*/
49656019Sdougm 		case '?':
49664653Sdougm 		default:
49676019Sdougm 			/*
49686019Sdougm 			 * Since a bad option gets to here, sort it
49696019Sdougm 			 * out and return a syntax error return value
49706019Sdougm 			 * if necessary.
49716019Sdougm 			 */
49726019Sdougm 			ret = SA_OK;
49736019Sdougm 			switch (optopt) {
49746019Sdougm 			default:
49756019Sdougm 				ret = SA_SYNTAX_ERR;
49766019Sdougm 				break;
49776019Sdougm 			case 'h':
49786019Sdougm 			case '?':
49796019Sdougm 				break;
49806019Sdougm 			}
49814653Sdougm 			(void) printf(gettext("usage: %s\n"),
49824653Sdougm 			    sa_get_usage(USAGE_START));
49836019Sdougm 			return (ret);
49843034Sdougm 		}
49853034Sdougm 	}
49863034Sdougm 
49873034Sdougm 	if (optind == argc && !all) {
49883034Sdougm 		(void) printf(gettext("usage: %s\n"),
49894653Sdougm 		    sa_get_usage(USAGE_START));
49904653Sdougm 		return (SMF_EXIT_ERR_FATAL);
49914653Sdougm 	}
49924653Sdougm 
49934653Sdougm 	if (!all) {
49944653Sdougm 		while (optind < argc) {
49953910Sdougm 			group = sa_get_group(handle, argv[optind]);
49963034Sdougm 			if (group != NULL) {
49974653Sdougm 				state = sa_get_group_attr(group, "state");
49984653Sdougm 				if (state == NULL ||
49994653Sdougm 				    strcmp(state, "enabled") == 0) {
50005331Samw 					worklist = add_list(worklist, group, 0,
50015331Samw 					    protocol);
50024653Sdougm 					if (verbose)
50034653Sdougm 						(void) printf(gettext(
50044653Sdougm 						    "Starting group \"%s\"\n"),
50054653Sdougm 						    argv[optind]);
50064653Sdougm 				} else {
50074653Sdougm 					/*
50084653Sdougm 					 * Determine if there are any
50095331Samw 					 * protocols.  If there aren't any,
50104653Sdougm 					 * then there isn't anything to do in
50114653Sdougm 					 * any case so no error.
50124653Sdougm 					 */
50134653Sdougm 					if (sa_get_optionset(group,
50144653Sdougm 					    protocol) != NULL) {
50154653Sdougm 						ret = SMF_EXIT_OK;
50164653Sdougm 					}
50173034Sdougm 				}
50184653Sdougm 				if (state != NULL)
50194653Sdougm 					sa_free_attr_string(state);
50203034Sdougm 			}
50213034Sdougm 			optind++;
50224653Sdougm 		}
50234653Sdougm 	} else {
50245331Samw 		for (group = sa_get_group(handle, NULL);
50255331Samw 		    group != NULL;
50264653Sdougm 		    group = sa_get_next_group(group)) {
50273034Sdougm 			state = sa_get_group_attr(group, "state");
50283034Sdougm 			if (state == NULL || strcmp(state, "enabled") == 0)
50295331Samw 				worklist = add_list(worklist, group, 0,
50305331Samw 				    protocol);
50313034Sdougm 			if (state != NULL)
50324653Sdougm 				sa_free_attr_string(state);
50333034Sdougm 		}
50343034Sdougm 	}
50354653Sdougm 
50365331Samw 	(void) enable_all_groups(handle, worklist, 0, 1, protocol, B_FALSE);
50374653Sdougm 
50383034Sdougm 	if (worklist != NULL)
50394653Sdougm 		free_list(worklist);
50403034Sdougm 	return (ret);
50413034Sdougm }
50423034Sdougm 
50433034Sdougm /*
50443034Sdougm  * sa_stop_group(flags, argc, argv)
50453034Sdougm  *
50463034Sdougm  * Implements the stop command.
50473034Sdougm  * This is similar to disable except it doesn't change the state
50483034Sdougm  * of the group(s) and only disables shares if the group is already
50493034Sdougm  * enabled.
50503034Sdougm  */
50513034Sdougm int
50523910Sdougm sa_stop_group(sa_handle_t handle, int flags, int argc, char *argv[])
50533034Sdougm {
50543034Sdougm 	int verbose = 0;
50553034Sdougm 	int all = 0;
50563034Sdougm 	int c;
50573034Sdougm 	int ret = SMF_EXIT_OK;
50583034Sdougm 	char *protocol = NULL;
50593034Sdougm 	char *state;
50603034Sdougm 	struct list *worklist = NULL;
50614653Sdougm 	sa_group_t group;
50625331Samw #ifdef lint
50635331Samw 	flags = flags;
50645331Samw #endif
50653034Sdougm 
50663034Sdougm 	while ((c = getopt(argc, argv, "?havP:")) != EOF) {
50674653Sdougm 		switch (c) {
50684653Sdougm 		case 'a':
50694653Sdougm 			all = 1;
50704653Sdougm 			break;
50714653Sdougm 		case 'P':
50725331Samw 			if (protocol != NULL) {
50735331Samw 				(void) printf(gettext(
50745331Samw 				    "Specifying multiple protocols "
50755331Samw 				    "not supported: %s\n"), protocol);
50765331Samw 				return (SA_SYNTAX_ERR);
50775331Samw 			}
50784653Sdougm 			protocol = optarg;
50794653Sdougm 			if (!sa_valid_protocol(protocol)) {
50804653Sdougm 				(void) printf(gettext(
50814653Sdougm 				    "Invalid protocol specified: %s\n"),
50824653Sdougm 				    protocol);
50834653Sdougm 				return (SA_INVALID_PROTOCOL);
50844653Sdougm 			}
50854653Sdougm 			break;
50864653Sdougm 		case 'v':
50874653Sdougm 			verbose++;
50884653Sdougm 			break;
50896019Sdougm 		case 'h':
50906019Sdougm 			/* optopt on valid arg isn't defined */
50916019Sdougm 			optopt = c;
50926019Sdougm 			/*FALLTHROUGH*/
50936019Sdougm 		case '?':
50944653Sdougm 		default:
50956019Sdougm 			/*
50966019Sdougm 			 * Since a bad option gets to here, sort it
50976019Sdougm 			 * out and return a syntax error return value
50986019Sdougm 			 * if necessary.
50996019Sdougm 			 */
51006019Sdougm 			ret = SA_OK;
51016019Sdougm 			switch (optopt) {
51026019Sdougm 			default:
51036019Sdougm 				ret = SA_SYNTAX_ERR;
51046019Sdougm 				break;
51056019Sdougm 			case 'h':
51066019Sdougm 			case '?':
51076019Sdougm 				break;
51086019Sdougm 			}
51094653Sdougm 			(void) printf(gettext("usage: %s\n"),
51104653Sdougm 			    sa_get_usage(USAGE_STOP));
51116019Sdougm 			return (ret);
51123034Sdougm 		}
51133034Sdougm 	}
51143034Sdougm 
51153034Sdougm 	if (optind == argc && !all) {
51164653Sdougm 		(void) printf(gettext("usage: %s\n"),
51174653Sdougm 		    sa_get_usage(USAGE_STOP));
51184653Sdougm 		return (SMF_EXIT_ERR_FATAL);
51194653Sdougm 	} else if (!all) {
51204653Sdougm 		while (optind < argc) {
51213910Sdougm 			group = sa_get_group(handle, argv[optind]);
51223034Sdougm 			if (group != NULL) {
51234653Sdougm 				state = sa_get_group_attr(group, "state");
51244653Sdougm 				if (state == NULL ||
51254653Sdougm 				    strcmp(state, "enabled") == 0) {
51265331Samw 					worklist = add_list(worklist, group, 0,
51275331Samw 					    protocol);
51284653Sdougm 					if (verbose)
51294653Sdougm 						(void) printf(gettext(
51304653Sdougm 						    "Stopping group \"%s\"\n"),
51314653Sdougm 						    argv[optind]);
51324653Sdougm 				} else {
51334653Sdougm 					ret = SMF_EXIT_OK;
51344653Sdougm 				}
51354653Sdougm 				if (state != NULL)
51364653Sdougm 					sa_free_attr_string(state);
51373034Sdougm 			}
51383034Sdougm 			optind++;
51394653Sdougm 		}
51404653Sdougm 	} else {
51415331Samw 		for (group = sa_get_group(handle, NULL);
51425331Samw 		    group != NULL;
51434653Sdougm 		    group = sa_get_next_group(group)) {
51443034Sdougm 			state = sa_get_group_attr(group, "state");
51453034Sdougm 			if (state == NULL || strcmp(state, "enabled") == 0)
51465331Samw 				worklist = add_list(worklist, group, 0,
51475331Samw 				    protocol);
51483034Sdougm 			if (state != NULL)
51494653Sdougm 				sa_free_attr_string(state);
51503034Sdougm 		}
51513034Sdougm 	}
51524653Sdougm 	(void) disable_all_groups(handle, worklist, 0);
51534653Sdougm 	ret = sa_update_config(handle);
51544653Sdougm 
51553034Sdougm 	if (worklist != NULL)
51564653Sdougm 		free_list(worklist);
51573034Sdougm 	return (ret);
51583034Sdougm }
51593034Sdougm 
51603034Sdougm /*
51613034Sdougm  * remove_all_options(share, proto)
51623034Sdougm  *
51633034Sdougm  * Removes all options on a share.
51643034Sdougm  */
51653034Sdougm 
51663034Sdougm static void
51673034Sdougm remove_all_options(sa_share_t share, char *proto)
51683034Sdougm {
51693034Sdougm 	sa_optionset_t optionset;
51703034Sdougm 	sa_security_t security;
51713034Sdougm 	sa_security_t prevsec = NULL;
51723034Sdougm 
51733034Sdougm 	optionset = sa_get_optionset(share, proto);
51743034Sdougm 	if (optionset != NULL)
51754653Sdougm 		(void) sa_destroy_optionset(optionset);
51763034Sdougm 	for (security = sa_get_security(share, NULL, NULL);
51773034Sdougm 	    security != NULL;
51783034Sdougm 	    security = sa_get_next_security(security)) {
51794653Sdougm 		char *type;
51803034Sdougm 		/*
51814653Sdougm 		 * We walk through the list.  prevsec keeps the
51823034Sdougm 		 * previous security so we can delete it without
51833034Sdougm 		 * destroying the list.
51843034Sdougm 		 */
51854653Sdougm 		if (prevsec != NULL) {
51864653Sdougm 			/* remove the previously seen security */
51874653Sdougm 			(void) sa_destroy_security(prevsec);
51884653Sdougm 			/* set to NULL so we don't try multiple times */
51894653Sdougm 			prevsec = NULL;
51904653Sdougm 		}
51914653Sdougm 		type = sa_get_security_attr(security, "type");
51924653Sdougm 		if (type != NULL) {
51934653Sdougm 			/*
51944653Sdougm 			 * if the security matches the specified protocol, we
51954653Sdougm 			 * want to remove it. prevsec holds it until either
51964653Sdougm 			 * the next pass or we fall out of the loop.
51974653Sdougm 			 */
51984653Sdougm 			if (strcmp(type, proto) == 0)
51994653Sdougm 				prevsec = security;
52004653Sdougm 			sa_free_attr_string(type);
52014653Sdougm 		}
52023034Sdougm 	}
52033034Sdougm 	/* in case there is one left */
52043034Sdougm 	if (prevsec != NULL)
52054653Sdougm 		(void) sa_destroy_security(prevsec);
52063034Sdougm }
52073034Sdougm 
52083034Sdougm 
52093034Sdougm /*
52103034Sdougm  * for legacy support, we need to handle the old syntax. This is what
52113034Sdougm  * we get if sharemgr is called with the name "share" rather than
52123034Sdougm  * sharemgr.
52133034Sdougm  */
52143034Sdougm 
52153034Sdougm static int
52163034Sdougm format_legacy_path(char *buff, int buffsize, char *proto, char *cmd)
52173034Sdougm {
52183034Sdougm 	int err;
52193034Sdougm 
52203034Sdougm 	err = snprintf(buff, buffsize, "/usr/lib/fs/%s/%s", proto, cmd);
52213034Sdougm 	if (err > buffsize)
52224653Sdougm 		return (-1);
52233034Sdougm 	return (0);
52243034Sdougm }
52253034Sdougm 
52263034Sdougm 
52273034Sdougm /*
52283034Sdougm  * check_legacy_cmd(proto, cmd)
52293034Sdougm  *
52303034Sdougm  * Check to see if the cmd exists in /usr/lib/fs/<proto>/<cmd> and is
52313034Sdougm  * executable.
52323034Sdougm  */
52333034Sdougm 
52343034Sdougm static int
52353034Sdougm check_legacy_cmd(char *path)
52363034Sdougm {
52373034Sdougm 	struct stat st;
52383034Sdougm 	int ret = 0;
52393034Sdougm 
52403034Sdougm 	if (stat(path, &st) == 0) {
52414653Sdougm 		if (S_ISREG(st.st_mode) &&
52424653Sdougm 		    st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
52434653Sdougm 			ret = 1;
52443034Sdougm 	}
52453034Sdougm 	return (ret);
52463034Sdougm }
52473034Sdougm 
52483034Sdougm /*
52493034Sdougm  * run_legacy_command(proto, cmd, argv)
52503034Sdougm  *
52514653Sdougm  * We know the command exists, so attempt to execute it with all the
52523034Sdougm  * arguments. This implements full legacy share support for those
52533034Sdougm  * protocols that don't have plugin providers.
52543034Sdougm  */
52553034Sdougm 
52563034Sdougm static int
52573034Sdougm run_legacy_command(char *path, char *argv[])
52583034Sdougm {
52593034Sdougm 	int ret;
52603034Sdougm 
52613034Sdougm 	ret = execv(path, argv);
52623034Sdougm 	if (ret < 0) {
52634653Sdougm 		switch (errno) {
52644653Sdougm 		case EACCES:
52654653Sdougm 			ret = SA_NO_PERMISSION;
52664653Sdougm 			break;
52674653Sdougm 		default:
52684653Sdougm 			ret = SA_SYSTEM_ERR;
52694653Sdougm 			break;
52704653Sdougm 		}
52713034Sdougm 	}
52723034Sdougm 	return (ret);
52733034Sdougm }
52743034Sdougm 
52753034Sdougm /*
52763348Sdougm  * out_share(out, group, proto)
52773034Sdougm  *
52783034Sdougm  * Display the share information in the format that the "share"
52793034Sdougm  * command has traditionally used.
52803034Sdougm  */
52813034Sdougm 
52823034Sdougm static void
52833348Sdougm out_share(FILE *out, sa_group_t group, char *proto)
52843034Sdougm {
52853034Sdougm 	sa_share_t share;
52863034Sdougm 	char resfmt[128];
52875331Samw 	char *defprop;
52885331Samw 
52895331Samw 	/*
52905331Samw 	 * The original share command defaulted to displaying NFS
52915331Samw 	 * shares or allowed a protocol to be specified. We want to
52925331Samw 	 * skip those shares that are not the specified protocol.
52935331Samw 	 */
52945331Samw 	if (proto != NULL && sa_get_optionset(group, proto) == NULL)
52955331Samw 		return;
52965331Samw 
52975331Samw 	if (proto == NULL)
52985331Samw 		proto = "nfs";
52995331Samw 
53005331Samw 	/*
53015331Samw 	 * get the default property string.  NFS uses "rw" but
53025331Samw 	 * everything else will use "".
53035331Samw 	 */
53045331Samw 	if (proto != NULL && strcmp(proto, "nfs") != 0)
53055331Samw 		defprop = "\"\"";
53065331Samw 	else
53075331Samw 		defprop = "rw";
53083034Sdougm 
53094653Sdougm 	for (share = sa_get_share(group, NULL);
53104653Sdougm 	    share != NULL;
53114653Sdougm 	    share = sa_get_next_share(share)) {
53124653Sdougm 		char *path;
53134653Sdougm 		char *type;
53144653Sdougm 		char *resource;
53154653Sdougm 		char *description;
53164653Sdougm 		char *groupname;
53174653Sdougm 		char *sharedstate;
53184653Sdougm 		int shared = 1;
53194653Sdougm 		char *soptions;
53205331Samw 		char shareopts[MAXNAMLEN];
53214653Sdougm 
53224653Sdougm 		sharedstate = sa_get_share_attr(share, "shared");
53234653Sdougm 		path = sa_get_share_attr(share, "path");
53244653Sdougm 		type = sa_get_share_attr(share, "type");
53255331Samw 		resource = get_resource(share);
53264653Sdougm 		groupname = sa_get_group_attr(group, "name");
53274653Sdougm 
53284653Sdougm 		if (groupname != NULL && strcmp(groupname, "default") == 0) {
53294653Sdougm 			sa_free_attr_string(groupname);
53304653Sdougm 			groupname = NULL;
53314653Sdougm 		}
53324653Sdougm 		description = sa_get_share_description(share);
53334653Sdougm 
53345331Samw 		/*
53355331Samw 		 * Want the sharetab version if it exists, defaulting
53365331Samw 		 * to NFS if no protocol specified.
53375331Samw 		 */
53385331Samw 		(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s", proto);
53395331Samw 		soptions = sa_get_share_attr(share, shareopts);
53404653Sdougm 
53414653Sdougm 		if (sharedstate == NULL)
53424653Sdougm 			shared = 0;
53434653Sdougm 
53444653Sdougm 		if (soptions == NULL)
53454653Sdougm 			soptions = sa_proto_legacy_format(proto, share, 1);
53464653Sdougm 
53474653Sdougm 		if (shared) {
53484653Sdougm 			/* only active shares go here */
53494653Sdougm 			(void) snprintf(resfmt, sizeof (resfmt), "%s%s%s",
53504653Sdougm 			    resource != NULL ? resource : "-",
53514653Sdougm 			    groupname != NULL ? "@" : "",
53524653Sdougm 			    groupname != NULL ? groupname : "");
53534653Sdougm 			(void) fprintf(out, "%-14.14s  %s   %s   \"%s\"  \n",
53544653Sdougm 			    resfmt, path,
53554653Sdougm 			    (soptions != NULL && strlen(soptions) > 0) ?
53565331Samw 			    soptions : defprop,
53574653Sdougm 			    (description != NULL) ? description : "");
53584653Sdougm 		}
53594653Sdougm 
53604653Sdougm 		if (path != NULL)
53614653Sdougm 			sa_free_attr_string(path);
53624653Sdougm 		if (type != NULL)
53634653Sdougm 			sa_free_attr_string(type);
53644653Sdougm 		if (resource != NULL)
53654653Sdougm 			sa_free_attr_string(resource);
53664653Sdougm 		if (groupname != NULL)
53674653Sdougm 			sa_free_attr_string(groupname);
53684653Sdougm 		if (description != NULL)
53694653Sdougm 			sa_free_share_description(description);
53704653Sdougm 		if (sharedstate != NULL)
53714653Sdougm 			sa_free_attr_string(sharedstate);
53724653Sdougm 		if (soptions != NULL)
53734653Sdougm 			sa_format_free(soptions);
53743034Sdougm 	}
53753034Sdougm }
53763034Sdougm 
53773034Sdougm /*
53783034Sdougm  * output_legacy_file(out, proto)
53793034Sdougm  *
53803034Sdougm  * Walk all of the groups for the specified protocol and call
53813034Sdougm  * out_share() to format and write in the format displayed by the
53823034Sdougm  * "share" command with no arguments.
53833034Sdougm  */
53843034Sdougm 
53853034Sdougm static void
53863910Sdougm output_legacy_file(FILE *out, char *proto, sa_handle_t handle)
53873034Sdougm {
53883034Sdougm 	sa_group_t group;
53893034Sdougm 
53905331Samw 	for (group = sa_get_group(handle, NULL);
53915331Samw 	    group != NULL;
53924653Sdougm 	    group = sa_get_next_group(group)) {
53934653Sdougm 		char *zfs;
53943034Sdougm 
53953034Sdougm 		/*
53965331Samw 		 * Go through all the groups and ZFS
53975331Samw 		 * sub-groups. out_share() will format the shares in
53985331Samw 		 * the group appropriately.
53993034Sdougm 		 */
54003034Sdougm 
54014653Sdougm 		zfs = sa_get_group_attr(group, "zfs");
54024653Sdougm 		if (zfs != NULL) {
54034653Sdougm 			sa_group_t zgroup;
54044653Sdougm 			sa_free_attr_string(zfs);
54054653Sdougm 			for (zgroup = sa_get_sub_group(group);
54064653Sdougm 			    zgroup != NULL;
54074653Sdougm 			    zgroup = sa_get_next_group(zgroup)) {
54084653Sdougm 
54094653Sdougm 				/* got a group, so display it */
54104653Sdougm 				out_share(out, zgroup, proto);
54114653Sdougm 			}
54124653Sdougm 		} else {
54134653Sdougm 			out_share(out, group, proto);
54143034Sdougm 		}
54153034Sdougm 	}
54163034Sdougm }
54173034Sdougm 
54183034Sdougm int
54193910Sdougm sa_legacy_share(sa_handle_t handle, int flags, int argc, char *argv[])
54203034Sdougm {
54213034Sdougm 	char *protocol = "nfs";
54223034Sdougm 	char *options = NULL;
54233034Sdougm 	char *description = NULL;
54243034Sdougm 	char *groupname = NULL;
54253034Sdougm 	char *sharepath = NULL;
54263034Sdougm 	char *resource = NULL;
54273034Sdougm 	char *groupstatus = NULL;
54283034Sdougm 	int persist = SA_SHARE_TRANSIENT;
54293034Sdougm 	int argsused = 0;
54303034Sdougm 	int c;
54313034Sdougm 	int ret = SA_OK;
54323034Sdougm 	int zfs = 0;
54333034Sdougm 	int true_legacy = 0;
54343034Sdougm 	int curtype = SA_SHARE_TRANSIENT;
54353034Sdougm 	char cmd[MAXPATHLEN];
54364653Sdougm 	sa_group_t group = NULL;
54375331Samw 	sa_resource_t rsrc = NULL;
54384653Sdougm 	sa_share_t share;
54394653Sdougm 	char dir[MAXPATHLEN];
54405331Samw 	uint64_t features;
54415331Samw #ifdef lint
54425331Samw 	flags = flags;
54435331Samw #endif
54443034Sdougm 
54453034Sdougm 	while ((c = getopt(argc, argv, "?hF:d:o:p")) != EOF) {
54464653Sdougm 		switch (c) {
54474653Sdougm 		case 'd':
54484653Sdougm 			description = optarg;
54494653Sdougm 			argsused++;
54504653Sdougm 			break;
54514653Sdougm 		case 'F':
54524653Sdougm 			protocol = optarg;
54534653Sdougm 			if (!sa_valid_protocol(protocol)) {
54544653Sdougm 				if (format_legacy_path(cmd, MAXPATHLEN,
54554653Sdougm 				    protocol, "share") == 0 &&
54564653Sdougm 				    check_legacy_cmd(cmd)) {
54574653Sdougm 					true_legacy++;
54584653Sdougm 				} else {
54594653Sdougm 					(void) fprintf(stderr, gettext(
54604653Sdougm 					    "Invalid protocol specified: "
54614653Sdougm 					    "%s\n"), protocol);
54624653Sdougm 					return (SA_INVALID_PROTOCOL);
54634653Sdougm 				}
54644653Sdougm 			}
54654653Sdougm 			break;
54664653Sdougm 		case 'o':
54674653Sdougm 			options = optarg;
54684653Sdougm 			argsused++;
54694653Sdougm 			break;
54704653Sdougm 		case 'p':
54714653Sdougm 			persist = SA_SHARE_PERMANENT;
54724653Sdougm 			argsused++;
54734653Sdougm 			break;
54744653Sdougm 		case 'h':
54756019Sdougm 			/* optopt on valid arg isn't defined */
54766019Sdougm 			optopt = c;
54776019Sdougm 			/*FALLTHROUGH*/
54784653Sdougm 		case '?':
54794653Sdougm 		default:
54806019Sdougm 			/*
54816019Sdougm 			 * Since a bad option gets to here, sort it
54826019Sdougm 			 * out and return a syntax error return value
54836019Sdougm 			 * if necessary.
54846019Sdougm 			 */
54856019Sdougm 			switch (optopt) {
54866019Sdougm 			default:
54876019Sdougm 				ret = SA_LEGACY_ERR;
54886019Sdougm 				break;
54896019Sdougm 			case 'h':
54906019Sdougm 			case '?':
54916019Sdougm 				break;
54926019Sdougm 			}
54934653Sdougm 			(void) fprintf(stderr, gettext("usage: %s\n"),
54944653Sdougm 			    sa_get_usage(USAGE_SHARE));
54956019Sdougm 			return (ret);
54963034Sdougm 		}
54974653Sdougm 	}
54984653Sdougm 
54994653Sdougm 	/* Have the info so construct what is needed */
55004653Sdougm 	if (!argsused && optind == argc) {
55014653Sdougm 		/* display current info in share format */
55025331Samw 		(void) output_legacy_file(stdout, protocol, handle);
55034653Sdougm 		return (ret);
55043034Sdougm 	}
55053034Sdougm 
55064653Sdougm 	/* We are modifying the configuration */
55074653Sdougm 	if (optind == argc) {
55083034Sdougm 		(void) fprintf(stderr, gettext("usage: %s\n"),
55094653Sdougm 		    sa_get_usage(USAGE_SHARE));
55103034Sdougm 		return (SA_LEGACY_ERR);
55114653Sdougm 	}
55124653Sdougm 	if (true_legacy) {
55134653Sdougm 		/* If still using legacy share/unshare, exec it */
55143034Sdougm 		ret = run_legacy_command(cmd, argv);
55153034Sdougm 		return (ret);
55164653Sdougm 	}
55174653Sdougm 
55184653Sdougm 	sharepath = argv[optind++];
55194653Sdougm 	if (optind < argc) {
55203034Sdougm 		resource = argv[optind];
55213034Sdougm 		groupname = strchr(resource, '@');
55223034Sdougm 		if (groupname != NULL)
55234653Sdougm 			*groupname++ = '\0';
55244653Sdougm 	}
55254653Sdougm 	if (realpath(sharepath, dir) == NULL)
55263034Sdougm 		ret = SA_BAD_PATH;
55274653Sdougm 	else
55283034Sdougm 		sharepath = dir;
55294653Sdougm 	if (ret == SA_OK)
55303910Sdougm 		share = sa_find_share(handle, sharepath);
55314653Sdougm 	else
55323034Sdougm 		share = NULL;
55334653Sdougm 
55345331Samw 	features = sa_proto_get_featureset(protocol);
55355331Samw 
55364653Sdougm 	if (groupname != NULL) {
55374653Sdougm 		ret = SA_NOT_ALLOWED;
55384653Sdougm 	} else if (ret == SA_OK) {
55395331Samw 		char *legacygroup;
55403034Sdougm 		/*
55414653Sdougm 		 * The legacy group is always present and zfs groups
55423034Sdougm 		 * come and go.  zfs shares may be in sub-groups and
55433034Sdougm 		 * the zfs share will already be in that group so it
55445331Samw 		 * isn't an error. If the protocol is "smb", the group
55455331Samw 		 * "smb" is used when "default" would otherwise be
55465331Samw 		 * used.  "default" is NFS only and "smb" is SMB only.
55473034Sdougm 		 */
55485331Samw 		if (strcmp(protocol, "smb") == 0)
55495331Samw 			legacygroup = "smb";
55505331Samw 		else
55515331Samw 			legacygroup = "default";
55525331Samw 
55533034Sdougm 		/*
55544653Sdougm 		 * If the share exists (not NULL), then make sure it
55554653Sdougm 		 * is one we want to handle by getting the parent
55564653Sdougm 		 * group.
55573034Sdougm 		 */
55585331Samw 		if (share != NULL) {
55594653Sdougm 			group = sa_get_parent_group(share);
55605331Samw 		} else {
55614653Sdougm 			group = sa_get_group(handle, legacygroup);
55625331Samw 			if (group == NULL && strcmp(legacygroup, "smb") == 0) {
55635331Samw 				/*
55645331Samw 				 * This group may not exist, so create
55655331Samw 				 * as necessary. It only contains the
55665331Samw 				 * "smb" protocol.
55675331Samw 				 */
55685331Samw 				group = sa_create_group(handle, legacygroup,
55695331Samw 				    &ret);
55705331Samw 				if (group != NULL)
55715331Samw 					(void) sa_create_optionset(group,
55725331Samw 					    protocol);
55735331Samw 			}
55745331Samw 		}
55755331Samw 
55765331Samw 		if (group == NULL) {
55775331Samw 			ret = SA_SYSTEM_ERR;
55785331Samw 			goto err;
55795331Samw 		}
55805331Samw 
55815331Samw 		groupstatus = group_status(group);
55825331Samw 		if (share == NULL) {
55835331Samw 			share = sa_add_share(group, sharepath,
55845331Samw 			    persist, &ret);
55855331Samw 			if (share == NULL &&
55865331Samw 			    ret == SA_DUPLICATE_NAME) {
55875331Samw 				/*
55885331Samw 				 * Could be a ZFS path being started
55895331Samw 				 */
55905331Samw 				if (sa_zfs_is_shared(handle,
55915331Samw 				    sharepath)) {
55925331Samw 					ret = SA_OK;
55935331Samw 					group = sa_get_group(handle,
55945331Samw 					    "zfs");
55955331Samw 					if (group == NULL) {
55965331Samw 						/*
55975331Samw 						 * This shouldn't
55985331Samw 						 * happen.
55995331Samw 						 */
56005331Samw 						ret = SA_CONFIG_ERR;
56015331Samw 					} else {
56025331Samw 						share = sa_add_share(
56035331Samw 						    group, sharepath,
56045331Samw 						    persist, &ret);
56054653Sdougm 					}
56063034Sdougm 				}
56075331Samw 			}
56085331Samw 		} else {
56095331Samw 			char *type;
56105331Samw 			/*
56115331Samw 			 * May want to change persist state, but the
56125331Samw 			 * important thing is to change options. We
56135331Samw 			 * need to change them regardless of the
56145331Samw 			 * source.
56155331Samw 			 */
56165331Samw 
56175331Samw 			if (sa_zfs_is_shared(handle, sharepath)) {
56185331Samw 				zfs = 1;
56195331Samw 			}
56205331Samw 			remove_all_options(share, protocol);
56215331Samw 			type = sa_get_share_attr(share, "type");
56225331Samw 			if (type != NULL &&
56235331Samw 			    strcmp(type, "transient") != 0) {
56245331Samw 				curtype = SA_SHARE_PERMANENT;
56255331Samw 			}
56265331Samw 			if (type != NULL)
56275331Samw 				sa_free_attr_string(type);
56285331Samw 			if (curtype != persist) {
56295331Samw 				(void) sa_set_share_attr(share, "type",
56305331Samw 				    persist == SA_SHARE_PERMANENT ?
56315331Samw 				    "persist" : "transient");
56325331Samw 			}
56335331Samw 		}
56345331Samw 
56355331Samw 		/*
56365331Samw 		 * If there is a resource name, we may
56375331Samw 		 * actually care about it if this is share for
56385331Samw 		 * a protocol that uses resource level sharing
56395331Samw 		 * (SMB). We need to find the resource and, if
56405331Samw 		 * it exists, make sure it belongs to the
56415331Samw 		 * current share. If it doesn't exist, attempt
56425331Samw 		 * to create it.
56435331Samw 		 */
56445331Samw 
56455331Samw 		if (ret == SA_OK && resource != NULL) {
56465331Samw 			rsrc = sa_find_resource(handle, resource);
56475331Samw 			if (rsrc != NULL) {
56485331Samw 				if (share != sa_get_resource_parent(rsrc))
56495331Samw 					ret = SA_DUPLICATE_NAME;
56505331Samw 				} else {
56515331Samw 					rsrc = sa_add_resource(share, resource,
56525331Samw 					    persist, &ret);
56533034Sdougm 				}
56545331Samw 				if (features & SA_FEATURE_RESOURCE)
56555331Samw 					share = rsrc;
56563108Sdougm 			}
56575331Samw 
56584653Sdougm 			/* Have a group to hold this share path */
56594653Sdougm 			if (ret == SA_OK && options != NULL &&
56604653Sdougm 			    strlen(options) > 0) {
56614653Sdougm 				ret = sa_parse_legacy_options(share,
56624653Sdougm 				    options,
56634653Sdougm 				    protocol);
56643034Sdougm 			}
56654653Sdougm 			if (!zfs) {
56664653Sdougm 				/*
56675331Samw 				 * ZFS shares never have a description
56685331Samw 				 * and we can't store the values so
56695331Samw 				 * don't try.
56704653Sdougm 				 */
56714653Sdougm 				if (ret == SA_OK && description != NULL)
56724653Sdougm 					ret = sa_set_share_description(share,
56734653Sdougm 					    description);
56743034Sdougm 			}
56755331Samw 			if (ret == SA_OK &&
56765331Samw 			    strcmp(groupstatus, "enabled") == 0) {
56775331Samw 				if (rsrc != share)
56784653Sdougm 					ret = sa_enable_share(share, protocol);
56795331Samw 				else
56805331Samw 					ret = sa_enable_resource(rsrc,
56815331Samw 					    protocol);
56824653Sdougm 				if (ret == SA_OK &&
56834653Sdougm 				    persist == SA_SHARE_PERMANENT) {
56844653Sdougm 					(void) sa_update_legacy(share,
56854653Sdougm 					    protocol);
56864653Sdougm 				}
56874653Sdougm 				if (ret == SA_OK)
56884653Sdougm 					ret = sa_update_config(handle);
56894653Sdougm 			}
56903034Sdougm 	}
56915331Samw err:
56923034Sdougm 	if (ret != SA_OK) {
56934653Sdougm 		(void) fprintf(stderr, gettext("Could not share: %s: %s\n"),
56944653Sdougm 		    sharepath, sa_errorstr(ret));
56954653Sdougm 		ret = SA_LEGACY_ERR;
56963034Sdougm 	}
56973034Sdougm 	return (ret);
56983034Sdougm }
56993034Sdougm 
57003034Sdougm /*
57013034Sdougm  * sa_legacy_unshare(flags, argc, argv)
57023034Sdougm  *
57033034Sdougm  * Implements the original unshare command.
57043034Sdougm  */
57053034Sdougm int
57063910Sdougm sa_legacy_unshare(sa_handle_t handle, int flags, int argc, char *argv[])
57073034Sdougm {
57083034Sdougm 	char *protocol = "nfs"; /* for now */
57093034Sdougm 	char *options = NULL;
57103034Sdougm 	char *sharepath = NULL;
57113034Sdougm 	int persist = SA_SHARE_TRANSIENT;
57123034Sdougm 	int argsused = 0;
57133034Sdougm 	int c;
57143034Sdougm 	int ret = SA_OK;
57153034Sdougm 	int true_legacy = 0;
57165331Samw 	uint64_t features = 0;
57175331Samw 	sa_resource_t resource = NULL;
57183034Sdougm 	char cmd[MAXPATHLEN];
57195331Samw #ifdef lint
57205331Samw 	flags = flags;
57215331Samw 	options = options;
57225331Samw #endif
57233034Sdougm 
57243034Sdougm 	while ((c = getopt(argc, argv, "?hF:o:p")) != EOF) {
57254653Sdougm 		switch (c) {
57264653Sdougm 		case 'F':
57274653Sdougm 			protocol = optarg;
57284653Sdougm 			if (!sa_valid_protocol(protocol)) {
57294653Sdougm 				if (format_legacy_path(cmd, MAXPATHLEN,
57304653Sdougm 				    protocol, "unshare") == 0 &&
57314653Sdougm 				    check_legacy_cmd(cmd)) {
57324653Sdougm 					true_legacy++;
57334653Sdougm 				} else {
57344653Sdougm 					(void) printf(gettext(
57354653Sdougm 					    "Invalid file system name\n"));
57364653Sdougm 					return (SA_INVALID_PROTOCOL);
57374653Sdougm 				}
57384653Sdougm 			}
57394653Sdougm 			break;
57404653Sdougm 		case 'o':
57414653Sdougm 			options = optarg;
57424653Sdougm 			argsused++;
57434653Sdougm 			break;
57444653Sdougm 		case 'p':
57454653Sdougm 			persist = SA_SHARE_PERMANENT;
57464653Sdougm 			argsused++;
57474653Sdougm 			break;
57486019Sdougm 		case 'h':
57496019Sdougm 			/* optopt on valid arg isn't defined */
57506019Sdougm 			optopt = c;
57516019Sdougm 			/*FALLTHROUGH*/
57526019Sdougm 		case '?':
57534653Sdougm 		default:
57546019Sdougm 			/*
57556019Sdougm 			 * Since a bad option gets to here, sort it
57566019Sdougm 			 * out and return a syntax error return value
57576019Sdougm 			 * if necessary.
57586019Sdougm 			 */
57596019Sdougm 			switch (optopt) {
57606019Sdougm 			default:
57616019Sdougm 				ret = SA_LEGACY_ERR;
57626019Sdougm 				break;
57636019Sdougm 			case 'h':
57646019Sdougm 			case '?':
57656019Sdougm 				break;
57666019Sdougm 			}
57674653Sdougm 			(void) printf(gettext("usage: %s\n"),
57684653Sdougm 			    sa_get_usage(USAGE_UNSHARE));
57696019Sdougm 			return (ret);
57703034Sdougm 		}
57713034Sdougm 	}
57723034Sdougm 
57734653Sdougm 	/* Have the info so construct what is needed */
57744653Sdougm 	if (optind == argc || (optind + 1) < argc || options != NULL) {
57754653Sdougm 		ret = SA_SYNTAX_ERR;
57763034Sdougm 	} else {
57774653Sdougm 		sa_share_t share;
57784653Sdougm 		char dir[MAXPATHLEN];
57794653Sdougm 		if (true_legacy) {
57804653Sdougm 			/* if still using legacy share/unshare, exec it */
57814653Sdougm 			ret = run_legacy_command(cmd, argv);
57824653Sdougm 			return (ret);
57834653Sdougm 		}
57843663Sdougm 		/*
57853663Sdougm 		 * Find the path in the internal configuration. If it
57863663Sdougm 		 * isn't found, attempt to resolve the path via
57873663Sdougm 		 * realpath() and try again.
57883663Sdougm 		 */
57894653Sdougm 		sharepath = argv[optind++];
57904653Sdougm 		share = sa_find_share(handle, sharepath);
57914653Sdougm 		if (share == NULL) {
57924653Sdougm 			if (realpath(sharepath, dir) == NULL) {
57934653Sdougm 				ret = SA_NO_SUCH_PATH;
57944653Sdougm 			} else {
57954653Sdougm 				share = sa_find_share(handle, dir);
57964653Sdougm 			}
57973663Sdougm 		}
57985331Samw 		if (share == NULL) {
57995331Samw 			/* Could be a resource name so check that next */
58005331Samw 			features = sa_proto_get_featureset(protocol);
58015331Samw 			resource = sa_find_resource(handle, sharepath);
58025331Samw 			if (resource != NULL) {
58035331Samw 				share = sa_get_resource_parent(resource);
58045331Samw 				if (features & SA_FEATURE_RESOURCE)
58055331Samw 					(void) sa_disable_resource(resource,
58065331Samw 					    protocol);
58075331Samw 				if (persist == SA_SHARE_PERMANENT) {
58085331Samw 					ret = sa_remove_resource(resource);
58095331Samw 					if (ret == SA_OK)
58105331Samw 						ret = sa_update_config(handle);
58115331Samw 				}
58125331Samw 				/*
58135331Samw 				 * If we still have a resource on the
58145331Samw 				 * share, we don't disable the share
58155331Samw 				 * itself. IF there aren't anymore, we
58165331Samw 				 * need to remove the share. The
58175331Samw 				 * removal will be done in the next
58185331Samw 				 * section if appropriate.
58195331Samw 				 */
58205331Samw 				resource = sa_get_share_resource(share, NULL);
58215331Samw 				if (resource != NULL)
58225331Samw 					share = NULL;
58235331Samw 			} else if (ret == SA_OK) {
58245331Samw 				/* Didn't find path and no  resource */
58255331Samw 				ret = SA_BAD_PATH;
58265331Samw 			}
58275331Samw 		}
58285331Samw 		if (share != NULL && resource == NULL) {
58294653Sdougm 			ret = sa_disable_share(share, protocol);
58304653Sdougm 			/*
58314653Sdougm 			 * Errors are ok and removal should still occur. The
58324653Sdougm 			 * legacy unshare is more forgiving of errors than the
58334653Sdougm 			 * remove-share subcommand which may need the force
58344653Sdougm 			 * flag set for some error conditions. That is, the
58354653Sdougm 			 * "unshare" command will always unshare if it can
58364653Sdougm 			 * while "remove-share" might require the force option.
58374653Sdougm 			 */
58384653Sdougm 			if (persist == SA_SHARE_PERMANENT) {
58394653Sdougm 				ret = sa_remove_share(share);
58404653Sdougm 				if (ret == SA_OK)
58414653Sdougm 					ret = sa_update_config(handle);
58424653Sdougm 			}
58435331Samw 		} else if (ret == SA_OK && share == NULL && resource == NULL) {
58445331Samw 			/*
58455331Samw 			 * If both share and resource are NULL, then
58465331Samw 			 * share not found. If one or the other was
58475331Samw 			 * found or there was an earlier error, we
58485331Samw 			 * assume it was handled earlier.
58495331Samw 			 */
58504653Sdougm 			ret = SA_NOT_SHARED;
58513663Sdougm 		}
58523034Sdougm 	}
58533034Sdougm 	switch (ret) {
58543034Sdougm 	default:
58554653Sdougm 		(void) printf("%s: %s\n", sharepath, sa_errorstr(ret));
58564653Sdougm 		ret = SA_LEGACY_ERR;
58574653Sdougm 		break;
58583034Sdougm 	case SA_SYNTAX_ERR:
58594653Sdougm 		(void) printf(gettext("usage: %s\n"),
58604653Sdougm 		    sa_get_usage(USAGE_UNSHARE));
58614653Sdougm 		break;
58623034Sdougm 	case SA_OK:
58634653Sdougm 		break;
58643034Sdougm 	}
58653034Sdougm 	return (ret);
58663034Sdougm }
58673034Sdougm 
58683034Sdougm /*
58694653Sdougm  * Common commands that implement the sub-commands used by all
58705331Samw  * protocols. The entries are found via the lookup command
58713034Sdougm  */
58723034Sdougm 
58733034Sdougm static sa_command_t commands[] = {
58743034Sdougm 	{"add-share", 0, sa_addshare, USAGE_ADD_SHARE, SVC_SET},
58753034Sdougm 	{"create", 0, sa_create, USAGE_CREATE, SVC_SET|SVC_ACTION},
58763034Sdougm 	{"delete", 0, sa_delete, USAGE_DELETE, SVC_SET|SVC_ACTION},
58773034Sdougm 	{"disable", 0, sa_disable_group, USAGE_DISABLE, SVC_SET|SVC_ACTION},
58783034Sdougm 	{"enable", 0, sa_enable_group, USAGE_ENABLE, SVC_SET|SVC_ACTION},
58793034Sdougm 	{"list", 0, sa_list, USAGE_LIST},
58803034Sdougm 	{"move-share", 0, sa_moveshare, USAGE_MOVE_SHARE, SVC_SET},
58813034Sdougm 	{"remove-share", 0, sa_removeshare, USAGE_REMOVE_SHARE, SVC_SET},
58823034Sdougm 	{"set", 0, sa_set, USAGE_SET, SVC_SET},
58833034Sdougm 	{"set-share", 0, sa_set_share, USAGE_SET_SHARE, SVC_SET},
58843034Sdougm 	{"show", 0, sa_show, USAGE_SHOW},
58853034Sdougm 	{"share", 0, sa_legacy_share, USAGE_SHARE, SVC_SET|SVC_ACTION},
58863034Sdougm 	{"start", CMD_NODISPLAY, sa_start_group, USAGE_START,
58875331Samw 	    SVC_SET|SVC_ACTION},
58883034Sdougm 	{"stop", CMD_NODISPLAY, sa_stop_group, USAGE_STOP, SVC_SET|SVC_ACTION},
58893034Sdougm 	{"unset", 0, sa_unset, USAGE_UNSET, SVC_SET},
58903034Sdougm 	{"unshare", 0, sa_legacy_unshare, USAGE_UNSHARE, SVC_SET|SVC_ACTION},
58913034Sdougm 	{NULL, 0, NULL, NULL}
58923034Sdougm };
58933034Sdougm 
58943034Sdougm static char *
58953034Sdougm sa_get_usage(sa_usage_t index)
58963034Sdougm {
58973034Sdougm 	char *ret = NULL;
58983034Sdougm 	switch (index) {
58993034Sdougm 	case USAGE_ADD_SHARE:
59004653Sdougm 		ret = gettext("add-share [-nth] [-r resource-name] "
59014653Sdougm 		    "[-d \"description text\"] -s sharepath group");
59024653Sdougm 		break;
59033034Sdougm 	case USAGE_CREATE:
59044653Sdougm 		ret = gettext(
59054653Sdougm 		    "create [-nvh] [-P proto [-p property=value]] group");
59064653Sdougm 		break;
59073034Sdougm 	case USAGE_DELETE:
59084653Sdougm 		ret = gettext("delete [-nvh] [-P proto] [-f] group");
59094653Sdougm 		break;
59103034Sdougm 	case USAGE_DISABLE:
59114653Sdougm 		ret = gettext("disable [-nvh] {-a | group ...}");
59124653Sdougm 		break;
59133034Sdougm 	case USAGE_ENABLE:
59144653Sdougm 		ret = gettext("enable [-nvh] {-a | group ...}");
59154653Sdougm 		break;
59163034Sdougm 	case USAGE_LIST:
59174653Sdougm 		ret = gettext("list [-vh] [-P proto]");
59184653Sdougm 		break;
59193034Sdougm 	case USAGE_MOVE_SHARE:
59204653Sdougm 		ret = gettext(
59214653Sdougm 		    "move-share [-nvh] -s sharepath destination-group");
59224653Sdougm 		break;
59233034Sdougm 	case USAGE_REMOVE_SHARE:
59245331Samw 		ret = gettext(
59255331Samw 		    "remove-share [-fnvh] {-s sharepath | -r resource} "
59265331Samw 		    "group");
59274653Sdougm 		break;
59283034Sdougm 	case USAGE_SET:
59294653Sdougm 		ret = gettext("set [-nvh] -P proto [-S optspace] "
59305331Samw 		    "[-p property=value]* [-s sharepath] [-r resource]] "
59315331Samw 		    "group");
59324653Sdougm 		break;
59333034Sdougm 	case USAGE_SET_SECURITY:
59344653Sdougm 		ret = gettext("set-security [-nvh] -P proto -S security-type "
59354653Sdougm 		    "[-p property=value]* group");
59364653Sdougm 		break;
59373034Sdougm 	case USAGE_SET_SHARE:
59384653Sdougm 		ret = gettext("set-share [-nh] [-r resource] "
59394653Sdougm 		    "[-d \"description text\"] -s sharepath group");
59404653Sdougm 		break;
59413034Sdougm 	case USAGE_SHOW:
59424653Sdougm 		ret = gettext("show [-pvxh] [-P proto] [group ...]");
59434653Sdougm 		break;
59443034Sdougm 	case USAGE_SHARE:
59454653Sdougm 		ret = gettext("share [-F fstype] [-p] [-o optionlist]"
59464653Sdougm 		    "[-d description] [pathname [resourcename]]");
59474653Sdougm 		break;
59483034Sdougm 	case USAGE_START:
59494653Sdougm 		ret = gettext("start [-vh] [-P proto] {-a | group ...}");
59504653Sdougm 		break;
59513034Sdougm 	case USAGE_STOP:
59524653Sdougm 		ret = gettext("stop [-vh] [-P proto] {-a | group ...}");
59534653Sdougm 		break;
59543034Sdougm 	case USAGE_UNSET:
59554653Sdougm 		ret = gettext("unset [-nvh] -P proto [-S optspace] "
59564653Sdougm 		    "[-p property]* group");
59574653Sdougm 		break;
59583034Sdougm 	case USAGE_UNSET_SECURITY:
59595331Samw 		ret = gettext("unset-security [-nvh] -P proto "
59605331Samw 		    "-S security-type [-p property]* group");
59614653Sdougm 		break;
59623034Sdougm 	case USAGE_UNSHARE:
59634653Sdougm 		ret = gettext(
59645331Samw 		    "unshare [-F fstype] [-p] [-o optionlist] sharepath");
59654653Sdougm 		break;
59663034Sdougm 	}
59673034Sdougm 	return (ret);
59683034Sdougm }
59693034Sdougm 
59703034Sdougm /*
59713034Sdougm  * sa_lookup(cmd, proto)
59723034Sdougm  *
59733034Sdougm  * Lookup the sub-command. proto isn't currently used, but it may
59743034Sdougm  * eventually provide a way to provide protocol specific sub-commands.
59753034Sdougm  */
59763034Sdougm sa_command_t *
59773034Sdougm sa_lookup(char *cmd, char *proto)
59783034Sdougm {
59793034Sdougm 	int i;
59803034Sdougm 	size_t len;
59815331Samw #ifdef lint
59825331Samw 	proto = proto;
59835331Samw #endif
59843034Sdougm 
59853034Sdougm 	len = strlen(cmd);
59863034Sdougm 	for (i = 0; commands[i].cmdname != NULL; i++) {
59874653Sdougm 		if (strncmp(cmd, commands[i].cmdname, len) == 0)
59884653Sdougm 			return (&commands[i]);
59893034Sdougm 	}
59903034Sdougm 	return (NULL);
59913034Sdougm }
59923034Sdougm 
59933034Sdougm void
59943034Sdougm sub_command_help(char *proto)
59953034Sdougm {
59963034Sdougm 	int i;
59975331Samw #ifdef lint
59985331Samw 	proto = proto;
59995331Samw #endif
60003034Sdougm 
60013034Sdougm 	(void) printf(gettext("\tsub-commands:\n"));
60023034Sdougm 	for (i = 0; commands[i].cmdname != NULL; i++) {
60034653Sdougm 		if (!(commands[i].flags & (CMD_ALIAS|CMD_NODISPLAY)))
60044653Sdougm 			(void) printf("\t%s\n",
60054653Sdougm 			    sa_get_usage((sa_usage_t)commands[i].cmdidx));
60063034Sdougm 	}
60073034Sdougm }
6008